Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

1308 rader
31 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 -z "$1"
  587. then
  588. echo "usage: tx <session_name>"
  589. tmux list-sessions
  590. elif test -z "$TMUX"
  591. then
  592. # -A: attach if already exists
  593. tmux new-session -A -c "$HOME" -s "$1"
  594. else
  595. # create new session if not exists yet
  596. tmux has-session -t "$1" || TMUX= tmux new-session -d -c "$HOME" -s "$1"
  597. tmux switch-client -t "$1"
  598. fi
  599. }
  600. dt(){
  601. # dt [-h] [<name>] [<command ...>]
  602. __dtach_dir="${TMP}/dtach"
  603. mkdir -p "${__dtach_dir}"
  604. if test -n "${__MY_DTACH}"
  605. then
  606. # if already in dtach session print current session
  607. soc_name="`basename "$__MY_DTACH"`"
  608. echo "Current session: ${soc_name}"
  609. fi
  610. if test -z "$1"
  611. then
  612. # if no arg given show list of sessions
  613. echo "Sessions:"
  614. ls "${__dtach_dir}"
  615. return 0
  616. elif test "$1" = "-h"
  617. then
  618. echo "dt: usage: dt [-h] [<name>] [<command ...>]" 1>&2
  619. return 1
  620. fi
  621. # set socket name
  622. soc_name="${__dtach_dir}/$1"
  623. shift
  624. if test -n "$__MY_DTACH"
  625. then
  626. echo "dtach session cannot be nested." 1>&2
  627. return 1
  628. elif test -S "$soc_name"
  629. then
  630. dtach -a "$soc_name" -e ^^
  631. elif test -e "$soc_name"
  632. then
  633. echo "dt: File named $soc_name already exists."
  634. return 1
  635. elif test -z "$1"
  636. then
  637. # if no command given invoke current shell
  638. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  639. # echo "dt: Socket named $soc_name not exists and no command specified."
  640. # return 1
  641. else
  642. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  643. fi
  644. }
  645. scr(){
  646. test -n "$1" && pf="${1}-"
  647. local _tformat="%Y%m%d-%H%M%S%z"
  648. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  649. __MY_SCRIPT=${_file} script ${_file} "$@"
  650. }
  651. dtscr(){
  652. # dtscr <command ...>
  653. if test -z "$1"
  654. then
  655. echo "dtscr: usage: dtscr <command ...>"
  656. return 1
  657. fi
  658. local _cmdstr="`echo $@ | tr ' ' +`"
  659. local _tformat="%Y%m%d-%H%M%S%z"
  660. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  661. local _scr_file="${HOME}/${_name}.script"
  662. local _dt_dir="${TMP}/dtscr"
  663. mkdir -p "$_dt_dir"
  664. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  665. # echo $_name
  666. # echo $_file
  667. }
  668. mcrypt_stream(){
  669. test $# -eq 2 || return 1
  670. case $1 in
  671. en)
  672. mcrypt --key $2 | base64 ;;
  673. de)
  674. base64 -d | mcrypt -d --key $2 ;;
  675. esac
  676. }
  677. gpg_stream(){
  678. test $# -eq 2 || return 1
  679. case $1 in
  680. en)
  681. gpg --passphrase $2 -c --batch |base64 ;;
  682. de)
  683. base64 -d|gpg --passphrase $2 -d --batch ;;
  684. esac
  685. }
  686. dgpg(){
  687. if test "$1" = help || test -z "$2"
  688. then
  689. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  690. return
  691. fi
  692. local srcs="$2"
  693. local dsts="$3"
  694. test -z "$dsts" && dsts="${srcs}.out"
  695. local pw
  696. echo -n "dgpg pw: "
  697. read -s pw
  698. echo ""
  699. test -z "$pw" && return 1
  700. for f in *${srcs}
  701. do
  702. local d="$(basename "$f" "${srcs}")${dsts}"
  703. echo -n "Processing $f to $d..."
  704. if test -d "$f"
  705. then
  706. echo "`printf 'failed (%s is directory)' $f`"
  707. elif test -f "$d"
  708. then
  709. echo "`printf 'failed (%s is already exists)' $d`"
  710. elif <"$f" gpg_stream $1 $pw >"$d" 2>/dev/null
  711. then
  712. echo "done"
  713. else
  714. echo "failed"
  715. test -f "$d" && rm "$d"
  716. fi
  717. done
  718. }
  719. alias enst="gpg_stream en"
  720. alias dest="gpg_stream de"
  721. showinfo(){
  722. echo "Japanese letters are 表示可能"
  723. __safe_run diskinfo
  724. ! $isdarwin && test -n "${DISPLAY}" && {
  725. __safe_run xrandr | \grep --color=never ^Screen
  726. }
  727. $iswindows || __safe_run finger $USER
  728. LANG=C __safe_runc id
  729. __safe_run xset q
  730. }
  731. x(){
  732. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  733. #mkdir -p ~/.var/log
  734. # nohup startx >~/.var/log/xorg.log 2>&1 &
  735. # exit
  736. exec startx
  737. else
  738. echo "X cant be started! Another X is already running?" 1>&2
  739. fi
  740. }
  741. bak(){
  742. for file in "$@"
  743. do
  744. cp -v ${file} ${file}.bak
  745. done
  746. }
  747. di(){
  748. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  749. then
  750. local diffcmd=colordiff
  751. else
  752. local diffcmd=diff
  753. fi
  754. ${diffcmd} -u "$@" | ${PAGER}
  755. }
  756. tb(){
  757. __datenum=`date +%Y%m%d-%H%M%S`
  758. __tb="$HOME/.var/tb/$__datenum"
  759. mkdir -p "$__tb"
  760. mv "$@" "$__tb"
  761. }
  762. mkcd(){
  763. if test -z "$1"
  764. then
  765. echo "mkcd: usage: mkcd <dir>"
  766. return 1
  767. elif test -d "$1"
  768. then
  769. echo "Dir \"$1\" already exists."
  770. else
  771. mkdir -p "$1"
  772. echo "Dir \"$1\" created."
  773. fi
  774. cd "$1"
  775. }
  776. mkcdd(){
  777. # make and change date directory
  778. _d=`date +%Y%m%d-%H%M%S`
  779. mkcd "$_d"
  780. }
  781. if test -n "$TMUX" && null type reattach-to-user-namespace
  782. then
  783. alias pbpaste="reattach-to-user-namespace pbpaste"
  784. alias pbcopy="reattach-to-user-namespace pbcopy"
  785. fi
  786. catclip(){
  787. if $iswindows
  788. then
  789. cat /dev/clipboard | tr -d \\r
  790. elif $isdarwin
  791. then
  792. pbpaste
  793. else
  794. xclip -o -selection "clipboard"
  795. fi
  796. }
  797. setclip(){
  798. if test $# -eq 0
  799. then
  800. exec 3<&0
  801. else
  802. exec 3<<__EOF__
  803. `cat "$@"`
  804. __EOF__
  805. fi
  806. if $iswindows
  807. then
  808. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  809. elif $isdarwin
  810. then
  811. pbcopy 0<&3
  812. else
  813. 0<&3 xclip -i -f -selection "primary" | \
  814. xclip -i -f -selection "clipboard"
  815. fi
  816. exec 3<&-
  817. }
  818. open_file(){
  819. if $iscygwin
  820. then
  821. cygstart "$@"
  822. elif $ismsys
  823. then
  824. cmd.exe //c start "" "$@"
  825. elif $isdarwin
  826. then
  827. touch "$@"
  828. open "$@"
  829. elif $islinux
  830. then
  831. touch "$@"
  832. if null type pcmanfm; then
  833. LC_MESSAGES= pcmanfm "$@"
  834. else
  835. LC_MESSAGES= xdg-open "$@" &
  836. fi
  837. else
  838. cat "$@"
  839. fi
  840. }
  841. o(){
  842. if test $# -eq 0
  843. then
  844. open_file .
  845. else
  846. for f in "$@"
  847. do
  848. open_file "$(realpath "$f")"
  849. done
  850. fi
  851. }
  852. convmv_sjis2utf8_test(){
  853. convmv -r -f sjis -t utf8 *
  854. }
  855. convmv_sjis2utf8_notest(){
  856. convmv -r -f sjis -t utf8 * --notest
  857. }
  858. #################################################
  859. ## pastebin services
  860. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  861. sprunge(){
  862. # http://sprunge.us
  863. if test -z "$1"
  864. then
  865. curl -F 'sprunge=<-' http://sprunge.us
  866. else
  867. curl http://sprunge.us/$1
  868. fi
  869. }
  870. dpaste(){
  871. # http://dpaste.de
  872. if test -z "$1"
  873. then
  874. curl -F 'content=<-' https://dpaste.de/api/
  875. echo
  876. else
  877. curl https://dpaste.de/$1/raw/
  878. fi
  879. }
  880. ######################################
  881. ## Prompt Settings
  882. __my_moc_state(){
  883. type mocp >/dev/null 2>&1 || return
  884. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  885. printf "$1" "`mocp -Q %title 2>/dev/null`"
  886. }
  887. __my_parse_svn_branch() {
  888. local LANG=C
  889. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  890. local svn_repository_root=$(svn info 2>/dev/null | \
  891. sed -ne 's#^Repository Root: ##p')
  892. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  893. awk '{print $1}'
  894. }
  895. __my_svn_ps1(){
  896. if svn status >/dev/null 2>&1
  897. then
  898. local svn_branch=$(__my_parse_svn_branch)
  899. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  900. fi
  901. }
  902. __my_battery_status(){
  903. local dir=/sys/class/power_supply/BAT0
  904. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  905. test -r $dir/charge_now
  906. then
  907. local st=$(cat $dir/status)
  908. local full=$(cat $dir/charge_full)
  909. local now=$(cat $dir/charge_now)
  910. local rate=$(expr $now \* 100 / $full)
  911. printf "$1" "${st}:${rate}%"
  912. fi
  913. }
  914. alias bat='__my_battery_status %s\\n'
  915. ipaddress(){
  916. # ipaddress <fmt>
  917. type ip >/dev/null 2>&1 || return 1
  918. local ip=$(LANG=C ip addr show scope global | \
  919. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  920. test -n "$ip" && printf $1 $ip
  921. }
  922. __my_ps1_str=""
  923. test -n "$__MY_SCRIPT" && \
  924. __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  925. test -n "$SSH_CONNECTION" && \
  926. __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  927. if test -n "$__MY_DTACH"
  928. then
  929. __dt_name="`basename "$__MY_DTACH"`"
  930. __my_ps1_str="${__my_ps1_str}${__my_c5}DT:${__dt_name}${__my_cdef} "
  931. fi
  932. __my_ps1_scale(){
  933. if null type stty && ! $ismsys
  934. then
  935. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  936. fi
  937. }
  938. __my_ps1_tmux(){
  939. null type tmux || return $last
  940. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  941. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  942. }
  943. __my_ps1_moc(){
  944. __my_moc_state "[MOC:%s]"
  945. }
  946. for f in /usr/share/git/git-prompt.sh \
  947. /usr/local/share/git-core/contrib/completion/git-prompt.sh \
  948. /etc/bash_completion.d/git-prompt \
  949. /opt/local/share/git-core/git-prompt.sh \
  950. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  951. do
  952. test -r "$f" && ($inbash || $inzsh) && . "$f" && break
  953. done
  954. GIT_PS1_SHOWDIRTYSTATE=t
  955. GIT_PS1_SHOWUPSTREAM=t
  956. __my_ps1_git(){
  957. null type __git_ps1 || return $last
  958. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  959. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  960. }
  961. __my_ps1_ipaddr(){
  962. ! $iswindows && ipaddress '[Addr:%s]'
  963. }
  964. __my_ps1_bttry(){
  965. local bst="${TMP}/batterystatus"
  966. if test -z "$DISPLAY" && ! $iswindows
  967. then
  968. test -f $bst && local bstr="$(cat $bst)"
  969. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  970. echo "[Battery:$bstr]"
  971. __my_battery_status %s >$bst &
  972. fi
  973. }
  974. __my_ps1_dirs(){
  975. dirs | wc -l
  976. }
  977. __my_ps1_jobs(){
  978. # __my_ps1_jobs [<num>]
  979. if test -n "$1"
  980. then
  981. jobs="$1"
  982. else
  983. jobs="`jobs | wc -l`"
  984. fi
  985. if test "$jobs" -gt 0
  986. then
  987. echo "[JOBS:$jobs] "
  988. fi
  989. }
  990. __my_alert_fail(){
  991. test $laststatus -eq 0 || \
  992. echo "[STATUS:${laststatus}] "
  993. }
  994. # About ansi escape sequences
  995. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  996. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  997. if $inbash || $inzsh
  998. then
  999. if $inzsh
  1000. then
  1001. __attr_beg=$'%{\033['
  1002. __attr_end='m%}'
  1003. else
  1004. __attr_beg='\[\033['
  1005. __attr_end='m\]'
  1006. fi
  1007. __color_default="${__attr_beg}0${__attr_end}"
  1008. __color_black="${__attr_beg}0;30${__attr_end}"
  1009. __color_red="${__attr_beg}0;31${__attr_end}"
  1010. __color_green="${__attr_beg}0;32${__attr_end}"
  1011. __color_brown="${__attr_beg}0;33${__attr_end}"
  1012. __color_blue="${__attr_beg}0;34${__attr_end}"
  1013. __color_purple="${__attr_beg}0;35${__attr_end}"
  1014. __color_cyan="${__attr_beg}0;36${__attr_end}"
  1015. __color_light_gray="${__attr_beg}0;37${__attr_end}"
  1016. __color_dark_gray="${__attr_beg}1;30${__attr_end}"
  1017. __color_light_red="${__attr_beg}1;31${__attr_end}"
  1018. __color_light_green="${__attr_beg}1;32${__attr_end}"
  1019. __color_yellow="${__attr_beg}1;33${__attr_end}"
  1020. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  1021. __color_light_purple="${__attr_beg}1;35${__attr_end}"
  1022. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  1023. __color_white="${__attr_beg}1;37${__attr_end}"
  1024. __color_bg_black="${__attr_beg}40${__attr_end}"
  1025. __color_bg_red="${__attr_beg}41${__attr_end}"
  1026. __color_bg_green="${__attr_beg}42${__attr_end}"
  1027. __color_bg_brown="${__attr_beg}43${__attr_end}"
  1028. __color_bg_blue="${__attr_beg}44${__attr_end}"
  1029. __color_bg_purple="${__attr_beg}45${__attr_end}"
  1030. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  1031. __color_bg_light_gray="${__attr_beg}47${__attr_end}"
  1032. __attr_underline="${__attr_beg}4${__attr_end}"
  1033. __attr_reverse="${__attr_beg}7${__attr_end}"
  1034. __attr_bold="${__attr_beg}1${__attr_end}"
  1035. fi
  1036. # NOTE: tput is another easy way to set colors and background
  1037. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  1038. # color blue.
  1039. # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
  1040. if test "$TERM" != dumb
  1041. then
  1042. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  1043. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  1044. # color for ::
  1045. case "`hostname`" in
  1046. arch-aspireone)
  1047. __my_c4="$__color_yellow"
  1048. ;;
  1049. arch-mba)
  1050. __my_c4="$__color_light_cyan"
  1051. ;;
  1052. newkiwi)
  1053. __my_c4="$__color_light_purple"
  1054. ;;
  1055. freebsd-vb-win7-opti)
  1056. __my_c4="$__color_light_red"
  1057. ;;
  1058. *)
  1059. __my_c4="$__color_light_green"
  1060. ;;
  1061. esac
  1062. __my_c5="$__color_black$__color_bg_light_gray" # color for SCR
  1063. __my_cdef="$__color_default"
  1064. fi
  1065. if $inbash
  1066. then
  1067. __my_ps1_sh="[BASH:$BASH_VERSION]"
  1068. elif $inzsh
  1069. then
  1070. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  1071. fi
  1072. __my_ps1_info(){
  1073. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_ipaddr)$(__my_ps1_moc)"
  1074. }
  1075. __my_ps1_save_pos="\[\033[s\]"
  1076. __my_ps1_restore_pos="\[\033[u\]"
  1077. __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
  1078. __my_ps1_move_15left="\[\033[15D\]"
  1079. # collapse when command line is too long and try to write over this string
  1080. # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
  1081. # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
  1082. if $inzsh
  1083. then
  1084. PROMPT="\
  1085. ${__my_c4}:: ${__my_cdef}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info)
  1086. ${__my_c4}:: ${__my_cdef}\$(__my_ps1_jobs)${__my_ps1_str}\$(__my_alert_fail)%# "
  1087. RPROMPT="%D{%Y/%m/%d %H:%M}"
  1088. elif $inbash
  1089. then
  1090. PS1="\
  1091. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info)\n\
  1092. ${__my_c4}:: ${__my_cdef}\D{%Y/%m/%d %H:%M} \$(__my_ps1_jobs \j)${__my_ps1_str}\$(__my_alert_fail)${__my_ps1_right}\$ "
  1093. else
  1094. true
  1095. # PS1="$(printf $(whoami)@$(hostname)$ )"
  1096. fi
  1097. ###################################
  1098. # set header and titles
  1099. __my_set_header_line(){
  1100. # save current position
  1101. printf "\033[s"
  1102. # move to 0,0
  1103. printf "\033[0;0H"
  1104. # clear curent to eol
  1105. printf "\033[K"
  1106. # inverse color
  1107. printf "\033[7m"
  1108. printf "$1"
  1109. # restore color
  1110. printf "\033[0m"
  1111. # restore saved position
  1112. printf "\033[u"
  1113. }
  1114. __my_set_screen_name(){
  1115. # set window name
  1116. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1117. then
  1118. echo -ne "\033k$1\033\\"
  1119. fi
  1120. }
  1121. __my_set_title(){
  1122. case $TERM in
  1123. (rxvt*|xterm*|aterm|screen*)
  1124. test -t 1 &&
  1125. test -z "$EMACS" &&
  1126. echo -n -e "\033]0;$1\007"
  1127. ;;
  1128. esac
  1129. }
  1130. if test -n "$TMUX"
  1131. then
  1132. # running tmux locally
  1133. __terminal_title="\$(basename \${PWD})"
  1134. elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
  1135. then
  1136. # ssh connect from tmux terminal
  1137. __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
  1138. else
  1139. __terminal_title="`whoami`@`hostname`:\${PWD}"
  1140. fi
  1141. if $inzsh
  1142. then
  1143. precmd(){
  1144. laststatus=$?
  1145. eval __my_set_title ${__terminal_title}
  1146. }
  1147. else
  1148. PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\";$PROMPT_COMMAND"
  1149. fi
  1150. laststatus=0