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

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