您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

1173 行
27 KiB

  1. #!/bin/sh
  2. expr "$-" : '^.*i' >/dev/null || return
  3. ##########################################
  4. null(){
  5. "$@" >/dev/null 2>&1
  6. }
  7. __safe_run(){
  8. type $1 >/dev/null 2>&1 && "$@"
  9. }
  10. __match(){
  11. # __match str word
  12. # return 0 if word is found in str
  13. expr "$1" : ".*$2.*" >/dev/null
  14. }
  15. #################################
  16. # profile-like setups
  17. # aliases:
  18. # isinteractive: true if the current session is interactive
  19. # issourced: true if this file is sourced from another file (not so assured)
  20. # __firstload: true if this file is sourced for the first time (not so
  21. # assured)
  22. __safe_add_path_r(){
  23. test -d "$1" && PATH="${PATH}:$1"
  24. }
  25. __safe_add_path_l(){
  26. test -d "$1" && PATH="$1:${PATH}"
  27. }
  28. __safe_add_path_l "$HOME/.local/bin"
  29. __safe_add_path_l "$HOME/.local/lib/gems/bin"
  30. __safe_add_path_r "/c/mingw/bin"
  31. __safe_add_path_r "/c/mingw/msys/1.0/bin"
  32. # macports coreutils
  33. # $isdarwin cannot be used it is not defined yet
  34. __safe_add_path_l "/opt/local/bin"
  35. __safe_add_path_l "/opt/local/sbin"
  36. __safe_add_path_l "/opt/local/libexec/gnubin"
  37. __safe_add_path_l \
  38. "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/bin"
  39. test -f "${__dotdir}/rc.py" && export PYTHONSTARTUP="${__dotdir}/rc.py"
  40. test -d "$HOME/.local/lib/python/site-packages" && \
  41. export PYTHONPATH="${PYTHONPATH}:${HOME}/.local/lib/python/site-packages"
  42. export GEM_HOME="$HOME/.local/lib/gems"
  43. export RUBYLIB="$RUBYLIB:$HOME/.local/lib/gems/lib"
  44. # it is not so good
  45. # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  46. # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  47. # without this ENV i cannot run tmux. another way is to use --disable-shared
  48. # when building tmux
  49. if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  50. then
  51. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  52. fi
  53. # in my environment powerdown does not work
  54. test -z "$SSH_CONNECTION" && \
  55. type setterm >/dev/null 2>&1 && \
  56. setterm -blank 30 -powersave on # -powerdown 10
  57. #########################
  58. # shrc.common
  59. # this variable must consistent with setup.sh
  60. __shrc_common="$HOME/.shrc.common"
  61. if test -f "$__shrc_common"
  62. then
  63. . "$__shrc_common"
  64. else
  65. echo "$__shrc_common not found."
  66. echo "Run ./setup.sh first."
  67. return
  68. fi
  69. ##########################
  70. # system type
  71. # aliases:
  72. # with_coreutils, inbash, inzsh
  73. alias with_coreutils=false # for mac
  74. null ls --version && alias with_coreutils=true
  75. alias inbash=false
  76. alias inzsh=false
  77. if test -n "$BASH_VERSION"
  78. then
  79. alias inbash=true
  80. elif test -n "$ZSH_VERSION"
  81. then
  82. alias inzsh=true
  83. fi
  84. #################################
  85. # file pathes:
  86. # shrc: Path to this file
  87. if inbash
  88. then
  89. __shrc="$BASH_SOURCE"
  90. elif inzsh
  91. then
  92. __shrc="$0"
  93. fi
  94. ##################################
  95. # EnvVal definitions
  96. export LANG=ja_JP.UTF-8
  97. export LC_MESSAGES=C
  98. export LC_TIME=C
  99. export TERMCAP="${TERMCAP}:vb="
  100. $ismsys && export HOSTNAME
  101. # export ENV=~/.shrc
  102. if ! with_coreutils
  103. then
  104. export LSCOLORS=gxfxcxdxbxegedabagacad
  105. else
  106. # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
  107. null type dircolors && eval `dircolors`
  108. fi
  109. if false $iswindows
  110. then
  111. export PAGER='tr -d \\r | less'
  112. else
  113. export PAGER="less"
  114. fi
  115. export LESS="-iRMX"
  116. # Style for lesspipe is defined in esc.style
  117. _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  118. for f in /usr/share/source-highlight/src-hilite-lesspipe.sh
  119. do
  120. test -z "$_src_hilite_lp_path" && test -e "$f" && _src_hilite_lp_path="$f"
  121. done
  122. test -n "$_src_hilite_lp_path" && export LESSOPEN="| $_src_hilite_lp_path %s"
  123. if null type vim
  124. then
  125. export EDITOR=vim
  126. else
  127. export EDITOR=vi
  128. fi
  129. # export CDPATH=".:~"
  130. export VISUAL="$EDITOR"
  131. export GIT_PAGER="less -FS"
  132. export GIT_EDITOR="$EDITOR"
  133. export GIT_MERGE_AUTOEDIT=no
  134. if test -n "$TMUX" && \
  135. __match $TERM screen && \
  136. __match `tmux display -p '#{client_termname}'` 256color
  137. then
  138. TERM=screen-256color
  139. fi
  140. if test -z "$TMP"
  141. then
  142. if test -n "$TMPDIR"
  143. then
  144. export TMP=$TMPDIR
  145. elif test -n "$TEMP"
  146. then
  147. export TMP="$TEMP"
  148. else
  149. export TMP=/tmp
  150. fi
  151. fi
  152. __match "$TMP" "${USER}-tmp" >/dev/null || export TMP="${TMP}/${USER}-tmp"
  153. export TEMP="$TMP"
  154. test -d "$TMP" || install -d "$TMP"
  155. if ! $iswindows && null type stty
  156. then
  157. stty stop undef # unbind C-s to stop displaying output
  158. # stty erase '^h'
  159. fi
  160. if $iswindows
  161. then
  162. export USER=$USERNAME
  163. fi
  164. if test -d ~/dbx
  165. then
  166. export CHIT_PATH="$HOME/dbx/.chit"
  167. fi
  168. ######################
  169. # Print welcome messages
  170. $iswindows && alias tty="echo cmd.exe"
  171. type fortune >/dev/null 2>&1 && {
  172. fortune
  173. echo
  174. fortune -o
  175. echo
  176. }
  177. uname -a
  178. echo TERM $TERM $(tput colors) colors connected to $(tty), \
  179. running $BASH $BASH_VERSION
  180. if test -n "$TMUX"
  181. then
  182. tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
  183. 2>/dev/null
  184. echo
  185. fi
  186. ###################################
  187. # some aliases and functions
  188. # __func_name: never used interactively
  189. # _func_name: usually not used interactively
  190. __safe_alias(){
  191. # __safe_alias <name>=<command>
  192. _bin=`expr "$1" : '^[^=]*=\([^ ]*\)'`
  193. test -n "$_bin" && \
  194. null type $_bin && \
  195. alias "$1"
  196. }
  197. ( ! with_coreutils && $isdarwin ) || test "$TERM" = dumb || \
  198. _coloroption=" --color=auto"
  199. ( ! with_coreutils && $isdarwin ) || $iswindows || \
  200. _timeoption=" --time-style=long-iso"
  201. ( ! with_coreutils && $isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  202. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  203. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  204. _timeformat_num="%Y%m%d%H%M%S"
  205. alias datenum="date +$_timeformat_num"
  206. alias ls="ls -hCF${_coloroption}${_timeoption}"
  207. # export GREP_OPTIONS=""
  208. alias gr="grep -n --color=always"
  209. $iswindows && alias grep="grep -n"
  210. # alias ll="ls -l"
  211. # alias la="ls -A"
  212. # alias lla="ls -Al"
  213. alias less="less -F"
  214. __safe_alias em="emacs -nw"
  215. __safe_alias vi=vim
  216. alias pstree="LANG=C pstree"
  217. alias cp="cp -v"
  218. alias mv="mv -v"
  219. alias rm="rm -v"
  220. alias psaux="ps auxww"
  221. alias q=exit
  222. __safe_alias e3=e3em
  223. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  224. alias po=popd
  225. alias pu=pushd
  226. __safe_alias sudo="sudo " # use aliases through sudo
  227. __safe_alias sudoe="sudoedit"
  228. # __safe_alias halt="sudo halt"
  229. # __safe_alias reboot="sudo reboot"
  230. null type dbus-send && {
  231. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  232. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  233. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  234. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  235. }
  236. alias rand="echo \$RANDOM"
  237. __safe_alias xunp="file-roller -h"
  238. __safe_alias pc="sudo \paco -D"
  239. alias pycalc="python -i -c 'from math import *' "
  240. __safe_alias py3=python3
  241. __safe_alias py2=python2
  242. alias _reloadrc="exec \"$SHELL\""
  243. # alias mytime="date +%Y%m%d-%H%M%S"
  244. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  245. # type trash >/dev/null 2>&1 && alias rm=trash
  246. __safe_alias mpg123="mpg123 -C -v --title"
  247. __safe_alias xm="xmms2"
  248. #export PLAYER="mpg123 -C -v --title"
  249. __safe_alias screen="screen -e^z^z"
  250. #alias zcd="cd \`zenity --file-selection --directory\`"
  251. __safe_alias gtags="gtags --verbose"
  252. __safe_alias htags="htags --xhtml --symbol --line-number \
  253. --frame --alphabet --verbose"
  254. __safe_alias au=aunpack
  255. __safe_alias lv="lv|less"
  256. __safe_alias rs="rsync --progress --itemize-changes --compress"
  257. if $iscygwin
  258. then
  259. __my_wget_options=" --no-check-certificate"
  260. __safe_alias wget="wget $__my_wget_options"
  261. fi
  262. if $isdarwin
  263. then
  264. alias updatedb="LC_ALL=C updatedb"
  265. # do not use locate installed by macports
  266. test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  267. fi
  268. cd(){
  269. command cd "$@"
  270. pwd
  271. }
  272. # pad
  273. alias pad=notepad
  274. __safe_alias pad=gedit
  275. __safe_alias pad=leafpad
  276. $isdarwin && alias pad="open -e"
  277. __safe_alias wic=wicd-curses
  278. __safe_alias wil="wicd-cli -y -l | head"
  279. #alias wicn="wicd-cli -y -c -n"
  280. wicn(){
  281. if test $# -eq 0
  282. then
  283. local num
  284. wicd-cli -y -l | head
  285. echo -n "input num: "
  286. read num
  287. test -n "$num" && wicd-cli -y -c -n $num
  288. else
  289. wicd-cli -y -c -n $1
  290. fi
  291. }
  292. __find_latest_vimdir(){
  293. vimdir=/usr/share/vim
  294. if test -d "$vimdir"
  295. then
  296. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  297. else
  298. echo ""
  299. fi
  300. }
  301. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  302. do
  303. test -n "$f" || continue
  304. f="$f/macros/less.sh"
  305. test -f $f && alias vl=$f && break
  306. done
  307. alias pa=pacapt
  308. __safe_alias yt=yaourt
  309. __safe_alias cower="cower --color=auto"
  310. null type pacmatic && {
  311. alias pacman="pacmatic"
  312. export PACMAN="pacmatic"
  313. }
  314. __my_pacman_update_mirrorlist_with_reflector(){
  315. ml=/etc/pacman.d/mirrorlist
  316. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  317. if test -z "$cmd"
  318. then
  319. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  320. fi
  321. echo "Running $cmd ..." 1>&2
  322. sudo $cmd
  323. }
  324. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  325. alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
  326. null type apt-get && {
  327. alias aupgrade="sudo apt-get autoremove --yes && \
  328. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  329. alias aptin="apt-get install"
  330. alias aptsearch="apt-cache search"
  331. alias aptshow="apt-cache show"
  332. }
  333. null type port && {
  334. alias port="port -v"
  335. alias pupgrade="sudo port -v selfupdate && \
  336. { sudo port -v upgrade outdated; }"
  337. }
  338. if $iscygwin
  339. then
  340. null type windate || \
  341. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  342. # alias cygsu="cygstart /cygwinsetup.exe"
  343. fi
  344. g(){
  345. if test $# -eq 0 && null type git-info
  346. then
  347. git info
  348. else
  349. git -c color.ui=always "$@"
  350. fi
  351. }
  352. if null type _git && inbash
  353. then
  354. # enable programmable completion for g
  355. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  356. || complete -o default -o nospace -F _git g
  357. fi
  358. #git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  359. __safe_alias m=gitmemo
  360. alias setup.py3="sudo python3 setup.py install --record files.txt"
  361. randomstr(){
  362. len=$1
  363. test -z "$len" && len=8
  364. uuidgen | tr -d - | cut -c 1-len
  365. }
  366. datestr(){
  367. # datestr yyyyMMdd-hhmmss
  368. if test -z "$1" || test "$1" == "-h"
  369. then
  370. echo "datestr: usage: datestr <yyyyMMddhhmmss>"
  371. return 1
  372. fi
  373. dfmt= # actual format for date command
  374. while test -n "$1"
  375. do
  376. fmt="$1"
  377. while test -n "$fmt"
  378. do
  379. case "$fmt" in
  380. yyyy*) # year
  381. dfmt="${dfmt}%Y"
  382. fmt="`echo "$fmt" | cut -c 5-`"
  383. ;;
  384. yy*) # last two digits of year
  385. dfmt="${dfmt}%y"
  386. fmt="`echo "$fmt" | cut -c 3-`"
  387. ;;
  388. MM*) # month (01..12)
  389. dfmt="${dfmt}%m"
  390. fmt="`echo "$fmt" | cut -c 3-`"
  391. ;;
  392. dd*) # day of month (01..12)
  393. dfmt="${dfmt}%d"
  394. fmt="`echo "$fmt" | cut -c 3-`"
  395. ;;
  396. HH* | hh*) # hour (00..23)
  397. dfmt="${dfmt}%H"
  398. fmt="`echo "$fmt" | cut -c 3-`"
  399. ;;
  400. mm*) # minute (00..59)
  401. dfmt="${dfmt}%M"
  402. fmt="`echo "$fmt" | cut -c 3-`"
  403. ;;
  404. ss*) # second (00..60)
  405. dfmt="${dfmt}%S"
  406. fmt="`echo "$fmt" | cut -c 3-`"
  407. ;;
  408. *)
  409. char=`echo "$fmt" | cut -c 1`
  410. dfmt="${dfmt}${char}"
  411. fmt="`echo "$fmt" | cut -c 2-`"
  412. ;;
  413. esac
  414. done
  415. shift
  416. done
  417. date +"$dfmt"
  418. }
  419. ssh(){
  420. __my_set_screen_title ssh
  421. command ssh "$@"
  422. }
  423. __ssh_with_cd(){
  424. # __ssh_with_cd <host> <directory> [<arg> ...]
  425. if test -z "$2"
  426. then
  427. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  428. return 1
  429. fi
  430. host="$1"
  431. shift
  432. dir="$1"
  433. shift
  434. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  435. }
  436. memo(){
  437. if test -z "$1"
  438. then
  439. $EDITOR memo.txt
  440. else
  441. $EDITOR "$1/memo.txt"
  442. fi
  443. }
  444. now(){
  445. local tformat="%Y/%m/%d %H:%M:%S %z"
  446. cal
  447. REPLY=
  448. printf "\\r`date "+${tformat}"`"
  449. read -t 1
  450. while test $? -ne 0
  451. do
  452. printf "\\r`date "+${tformat}"`"
  453. read -t 1
  454. done
  455. }
  456. s(){
  457. if git rev-parse --git-dir >/dev/null 2>&1
  458. then
  459. echo ">> git grep -n $@" 1>&2
  460. git grep -n "$@"
  461. elif which ag >/dev/null 2>&1
  462. then
  463. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  464. ag --pager="$PAGER" "$@"
  465. elif which ack >/dev/null 2>&1
  466. then
  467. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  468. ack --pager="$PAGER" "$@"
  469. else
  470. echo \
  471. ">> find . " \
  472. "-path '*/.git' -prune -o" \
  473. "-path '*/.svn' -prune -o" \
  474. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  475. if test $# -eq 0
  476. then
  477. echo "No search word given." 1>&2
  478. return 1
  479. fi
  480. find . \
  481. -path '*/.git' -prune -o \
  482. -path '*/.svn' -prune -o \
  483. -type -f -exec grep -nH -e --color=always "$@" {} + \
  484. | $PAGER
  485. fi
  486. }
  487. man(){
  488. env \
  489. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  490. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  491. LESS_TERMCAP_me=$(printf "\e[0m") \
  492. LESS_TERMCAP_se=$(printf "\e[0m") \
  493. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  494. LESS_TERMCAP_ue=$(printf "\e[0m") \
  495. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  496. man "$@"
  497. }
  498. netwait(){
  499. while ! ping -c 1 -t 1 example.com
  500. do
  501. true
  502. done
  503. echo network works.
  504. }
  505. __realpath(){
  506. if type realpath >/dev/null 2>&1
  507. then
  508. command realpath "$@"
  509. else
  510. while ! test -d $1
  511. do
  512. shift
  513. done
  514. (command cd "$d" && echo "$PWD")
  515. # local d="$OLDPWD"
  516. # command cd "$1"
  517. # echo "$PWD"
  518. # command cd "$d"
  519. fi
  520. }
  521. tx(){
  522. if test $# -eq 0
  523. then
  524. echo ":: tx <session> to attach."
  525. tmux ls
  526. elif tmux has -t "$1"
  527. then
  528. tmux attach -t "$1"
  529. else
  530. tmux new -s "$1"
  531. fi
  532. }
  533. _tmux_prefs(){
  534. null type tmux || return 1
  535. tmux set -g mode-keys vi
  536. }
  537. dt(){
  538. # dt [<name>] [<command ...>]
  539. __dtach_dir="${TMP}/dtach"
  540. install -d "${__dtach_dir}"
  541. if test -n "${__MY_DTACH}"
  542. then
  543. echo "Current session: ${__MY_DTACH}"
  544. fi
  545. if test -z "$1"
  546. then
  547. echo "Sessions:"
  548. ls "${__dtach_dir}"
  549. return 0
  550. elif test "$1" = "-h"
  551. then
  552. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  553. return 1
  554. fi
  555. soc_name="${__dtach_dir}/$1"
  556. shift
  557. if test -n "$__MY_DTACH"
  558. then
  559. echo "dtach session cannot be nested." 1>&2
  560. return 1
  561. elif test -S "$soc_name"
  562. then
  563. dtach -a "$soc_name" -e ^^
  564. elif test -e "$soc_name"
  565. then
  566. echo "dt: File named $soc_name already exists."
  567. return 1
  568. elif test -z "$1"
  569. then
  570. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  571. # echo "dt: Socket named $soc_name not exists and no command specified."
  572. # return 1
  573. else
  574. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  575. fi
  576. }
  577. scr(){
  578. test -n "$1" && pf="${1}-"
  579. local _tformat="%Y%m%d-%H%M%S%z"
  580. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  581. __MY_SCRIPT=${_file} script ${_file} "$@"
  582. }
  583. dtscr(){
  584. # dtscr <command ...>
  585. if test -z "$1"
  586. then
  587. echo "dtscr: usage: dtscr <command ...>"
  588. return 1
  589. fi
  590. local _cmdstr="`echo $@ | tr ' ' +`"
  591. local _tformat="%Y%m%d-%H%M%S%z"
  592. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  593. local _scr_file="${HOME}/${_name}.script"
  594. local _dt_dir="${TMP}/dtscr"
  595. install -d "$_dt_dir"
  596. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  597. # echo $_name
  598. # echo $_file
  599. }
  600. mcrypt_stream(){
  601. test $# -eq 2 || return 1
  602. case $1 in
  603. en)
  604. mcrypt --key $2 | base64 ;;
  605. de)
  606. base64 -d | mcrypt -d --key $2 ;;
  607. esac
  608. }
  609. gpg_stream(){
  610. test $# -eq 2 || return 1
  611. case $1 in
  612. en)
  613. gpg --passphrase $2 -c --batch |base64 ;;
  614. de)
  615. base64 -d|gpg --passphrase $2 -d --batch ;;
  616. esac
  617. }
  618. dgpg(){
  619. if test "$1" = help || test -z "$2"
  620. then
  621. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  622. return
  623. fi
  624. local srcs="$2"
  625. local dsts="$3"
  626. test -z "$dsts" && dsts="${srcs}.out"
  627. local pw
  628. echo -n "dgpg pw: "
  629. read -s pw
  630. echo ""
  631. test -z "$pw" && return 1
  632. for f in *${srcs}
  633. do
  634. local d="$(basename "$f" "${srcs}")${dsts}"
  635. echo -n "Processing $f to $d..."
  636. if test -d "$f"
  637. then
  638. echo "`printf 'failed (%s is directory)' $f`"
  639. elif test -f "$d"
  640. then
  641. echo "`printf 'failed (%s is already exists)' $d`"
  642. elif <"$f" gpg_stream $1 $pw >"$d" 2>/dev/null
  643. then
  644. echo "done"
  645. else
  646. echo "failed"
  647. test -f "$d" && rm "$d"
  648. fi
  649. done
  650. }
  651. alias enst="gpg_stream en"
  652. alias dest="gpg_stream de"
  653. showinfo(){
  654. echo "Japanese letters are 表示可能"
  655. __safe_run diskinfo
  656. ! $isdarwin && test -n "${DISPLAY}" && {
  657. __safe_run xrandr | \grep --color=never ^Screen
  658. }
  659. $iswindows || __safe_run finger $USER
  660. LANG=C __safe_runc id
  661. __safe_run xset q
  662. }
  663. x(){
  664. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  665. #mkdir -p ~/.var/log
  666. # nohup startx >~/.var/log/xorg.log 2>&1 &
  667. # exit
  668. exec startx
  669. else
  670. echo "X cant be started! Another X is already running?" 1>&2
  671. fi
  672. }
  673. bak(){
  674. for file in "$@"
  675. do
  676. cp -v ${file} ${file}.bak
  677. done
  678. }
  679. di(){
  680. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  681. then
  682. local diffcmd=colordiff
  683. else
  684. local diffcmd=diff
  685. fi
  686. ${diffcmd} -u "$@" | ${PAGER}
  687. }
  688. tb(){
  689. __datenum=`date +%Y%m%d-%H%M%S`
  690. __tb="$HOME/.var/tb/$__datenum"
  691. install -d "$__tb"
  692. for file in "$@"
  693. do
  694. mv -t "$__tb" "$file"
  695. done
  696. }
  697. mkcd(){
  698. if test -z "$1"
  699. then
  700. echo "mkcd: usage: mkcd <dir>"
  701. return 1
  702. elif test -d "$1"
  703. then
  704. echo "Dir \"$1\" already exists."
  705. else
  706. install -d "$1"
  707. echo "Dir \"$1\" created."
  708. fi
  709. cd "$1"
  710. }
  711. mkcdd(){
  712. # make and change date directory
  713. _d=`date +%Y%m%d-%H%M%S`
  714. mkcd "$_d"
  715. }
  716. if test -n "$TMUX" && null type reattach-to-user-namespace
  717. then
  718. alias pbpaste="reattach-to-user-namespace pbpaste"
  719. alias pbcopy="reattach-to-user-namespace pbcopy"
  720. fi
  721. catclip(){
  722. if $iswindows
  723. then
  724. cat /dev/clipboard | tr -d \\r
  725. elif $isdarwin
  726. then
  727. pbpaste
  728. else
  729. xclip -o -selection "clipboard"
  730. fi
  731. }
  732. setclip(){
  733. if test $# -eq 0
  734. then
  735. exec 3<&0
  736. else
  737. exec 3<<__EOF__
  738. `cat "$@"`
  739. __EOF__
  740. fi
  741. if $iswindows
  742. then
  743. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  744. elif $isdarwin
  745. then
  746. pbcopy 0<&3
  747. else
  748. 0<&3 xclip -i -f -selection "primary" | \
  749. xclip -i -f -selection "clipboard"
  750. fi
  751. exec 3<&-
  752. }
  753. open_file(){
  754. if $iscygwin
  755. then
  756. cygstart "$@"
  757. elif $ismsys
  758. then
  759. cmd.exe //c start "" "$@"
  760. elif $isdarwin
  761. then
  762. touch "$@"
  763. open "$@"
  764. elif $islinux
  765. then
  766. touch "$@"
  767. if null type pcmanfm; then
  768. LC_MESSAGES= pcmanfm "$@"
  769. else
  770. LC_MESSAGES= xdg-open "$@" &
  771. fi
  772. else
  773. cat "$@"
  774. fi
  775. }
  776. o(){
  777. if test $# -eq 0
  778. then
  779. open_file .
  780. else
  781. for f in "$@"
  782. do
  783. open_file "$(realpath "$f")"
  784. done
  785. fi
  786. }
  787. convmv_sjis2utf8_test(){
  788. convmv -r -f sjis -t utf8 *
  789. }
  790. convmv_sjis2utf8_notest(){
  791. convmv -r -f sjis -t utf8 * --notest
  792. }
  793. #################################################
  794. ## pastebin services
  795. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  796. sprunge(){
  797. # http://sprunge.us
  798. if test -z "$1"
  799. then
  800. curl -F 'sprunge=<-' http://sprunge.us
  801. else
  802. curl http://sprunge.us/$1
  803. fi
  804. }
  805. dpaste(){
  806. # http://dpaste.de
  807. if test -z "$1"
  808. then
  809. curl -F 'content=<-' https://dpaste.de/api/
  810. echo
  811. else
  812. curl https://dpaste.de/$1/raw/
  813. fi
  814. }
  815. ##########################
  816. # Zsh specific preferences
  817. if inzsh
  818. then
  819. bindkey -e
  820. # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
  821. autoload compinit; compinit
  822. unsetopt auto_menu
  823. zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
  824. setopt bash_auto_list
  825. autoload colors; colors
  826. autoload -Uz promptinit
  827. promptinit
  828. prompt walters
  829. fi
  830. ######################################
  831. ## Prompt Settings
  832. __my_moc_state(){
  833. type mocp >/dev/null 2>&1 || return
  834. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  835. printf "$1" "`mocp -Q %title 2>/dev/null`"
  836. }
  837. __my_parse_svn_branch() {
  838. local LANG=C
  839. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  840. local svn_repository_root=$(svn info 2>/dev/null | \
  841. sed -ne 's#^Repository Root: ##p')
  842. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  843. awk '{print $1}'
  844. }
  845. __my_svn_ps1(){
  846. if svn status >/dev/null 2>&1
  847. then
  848. local svn_branch=$(__my_parse_svn_branch)
  849. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  850. fi
  851. }
  852. __my_battery_status(){
  853. local dir=/sys/class/power_supply/BAT0
  854. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  855. test -r $dir/charge_now
  856. then
  857. local st=$(cat $dir/status)
  858. local full=$(cat $dir/charge_full)
  859. local now=$(cat $dir/charge_now)
  860. local rate=$(expr $now \* 100 / $full)
  861. printf "$1" "${st}:${rate}%"
  862. fi
  863. }
  864. alias bat='__my_battery_status %s\\n'
  865. ipaddress(){
  866. # ipaddress <fmt>
  867. type ip >/dev/null 2>&1 || return 1
  868. local ip=$(LANG=C ip addr show scope global | \
  869. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  870. test -n "$ip" && printf $1 $ip
  871. }
  872. __my_ps1_str=""
  873. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  874. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  875. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  876. __my_ps1_scale(){
  877. if null type stty && ! $ismsys
  878. then
  879. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  880. fi
  881. }
  882. __my_ps1_tmux(){
  883. null type tmux || return $last
  884. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  885. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  886. }
  887. __my_ps1_moc(){
  888. __my_moc_state "[MOC:%s]"
  889. }
  890. for f in /usr/share/git/git-prompt.sh \
  891. /opt/local/share/git-core/git-prompt.sh \
  892. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  893. do
  894. test -r "$f" && inbash && . "$f" && break
  895. done
  896. GIT_PS1_SHOWDIRTYSTATE=t
  897. GIT_PS1_SHOWUPSTREAM=t
  898. __my_ps1_git(){
  899. null type __git_ps1 || return $last
  900. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  901. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  902. }
  903. __my_ps1_ipaddr(){
  904. ! $iswindows && ipaddress [Addr:%s]
  905. }
  906. __my_ps1_bttry(){
  907. local bst="${TMP}/batterystatus"
  908. if test -z "$DISPLAY" && ! $iswindows
  909. then
  910. test -f $bst && local bstr="$(cat $bst)"
  911. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  912. echo "[Battery:$bstr]"
  913. __my_battery_status %s >$bst &
  914. fi
  915. }
  916. __my_ps1_dirs(){
  917. dirs | wc -l
  918. }
  919. __my_ps1_jobs(){
  920. # __my_ps1_jobs <num>
  921. if test "$1" -ne 0
  922. then
  923. echo "[JOBS:$1] "
  924. fi
  925. }
  926. __my_alert_fail(){
  927. test $laststatus -eq 0 || \
  928. echo "[STATUS:${laststatus}] "
  929. }
  930. # About ansi escape sequences
  931. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  932. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  933. __attr_beg='\[\033['
  934. __attr_end='m\]'
  935. __color_default="${__attr_beg}0${__attr_end}"
  936. __color_black="${__attr_beg}0;30${__attr_end}"
  937. __color_red="${__attr_beg}0;31${__attr_end}"
  938. __color_green="${__attr_beg}0;32${__attr_end}"
  939. __color_brown="${__attr_beg}0;33${__attr_end}"
  940. __color_blue="${__attr_beg}0;34${__attr_end}"
  941. __color_purple="${__attr_beg}0;35${__attr_end}"
  942. __color_cyan="${__attr_beg}0;36${__attr_end}"
  943. __color_light_gray="${__attr_beg}0;37${__attr_end}"
  944. __color_dark_gray="${__attr_beg}1;30${__attr_end}"
  945. __color_light_red="${__attr_beg}1;31${__attr_end}"
  946. __color_light_green="${__attr_beg}1;32${__attr_end}"
  947. __color_yellow="${__attr_beg}1;33${__attr_end}"
  948. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  949. __color_light_purple="${__attr_beg}1;35${__attr_end}"
  950. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  951. __color_white="${__attr_beg}1;37${__attr_end}"
  952. __color_bg_black="${__attr_beg}40${__attr_end}"
  953. __color_bg_red="${__attr_beg}41${__attr_end}"
  954. __color_bg_green="${__attr_beg}42${__attr_end}"
  955. __color_bg_brown="${__attr_beg}43${__attr_end}"
  956. __color_bg_blue="${__attr_beg}44${__attr_end}"
  957. __color_bg_purple="${__attr_beg}45${__attr_end}"
  958. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  959. __color_bg_light_gray="${__attr_beg}47${__attr_end}"
  960. __attr_underline="${__attr_beg}4${__attr_end}"
  961. __attr_reverse="${__attr_beg}7${__attr_end}"
  962. __attr_bold="${__attr_beg}1${__attr_end}"
  963. # NOTE: tput is another easy way to set colors and background
  964. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  965. # color blue.
  966. if test "$TERM" != dumb
  967. then
  968. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  969. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  970. # color for ::
  971. case "`hostname`" in
  972. arch-aspireone)
  973. __my_c4="$__color_light_blue"
  974. ;;
  975. darwin-mba.local)
  976. __my_c4="$__color_light_cyan"
  977. ;;
  978. newkiwi)
  979. __my_c4="$__color_light_purple"
  980. ;;
  981. *)
  982. __my_c4="$__color_light_green"
  983. ;;
  984. esac
  985. __my_c5="$__color_black$__color_bg_light_gray" # color for SCR
  986. __my_cdef="$__color_default"
  987. fi
  988. __my_ps1_info(){
  989. echo "$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_ipaddr)$(__my_ps1_moc)"
  990. }
  991. _ps1_bash="\
  992. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info)\n\
  993. ${__my_c4}:: ${__my_cdef}\$(__my_ps1_jobs \j)${__my_ps1_str}\$(__my_alert_fail)\$ "
  994. inbash && PS1=$_ps1_bash
  995. #inzsh && PS1="$_ps1_zsh"
  996. __my_set_header_line(){
  997. # save current position
  998. printf "\033[s"
  999. printf "\033[0;0H"
  1000. printf "\033[K"
  1001. printf "\033[7m"
  1002. printf "$1"
  1003. printf "\033[0m"
  1004. # restore saved position
  1005. printf "\033[u"
  1006. }
  1007. __my_set_screen_title(){
  1008. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1009. then
  1010. echo -ne "\033k$1\033\\"
  1011. fi
  1012. }
  1013. __my_set_title(){
  1014. case $TERM in
  1015. (rxvt*|xterm*|aterm|screen*)
  1016. test -t 1 &&
  1017. test -n "$DISPLAY" &&
  1018. test -z "$EMACS" &&
  1019. echo -n -e "\033]0;$1\007"
  1020. ;;
  1021. esac
  1022. }
  1023. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  1024. __my_set_screen_title \$(basename \"\$PWD\")/"
  1025. PROMPT_COMMAND="laststatus=\$?;$PROMPT_COMMAND"
  1026. laststatus=0