Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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