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

1097 行
26 KiB

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