Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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