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.
 
 
 
 
 
 

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