You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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