Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

1249 lignes
29 KiB

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