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