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.
 
 
 
 
 
 

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