25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

999 lines
23 KiB

  1. #!/bin/bash
  2. # TODO: use tput
  3. # If not running interactively, don't do anything
  4. [[ $- != *i* ]] && return
  5. ##########################################
  6. null(){
  7. "$@" >/dev/null 2>&1
  8. }
  9. __try_exec(){
  10. type $1 >/dev/null 2>&1 && "$@"
  11. }
  12. ##########################
  13. # system type
  14. alias ismsys=false
  15. alias iscygwin=false
  16. alias iswindows=false
  17. alias isdarwin=false
  18. alias islinux=false
  19. alias with_coreutils=false # for mac
  20. case `uname` in
  21. (MINGW*) alias ismsys=true ;;
  22. (CYGWIN*) alias iscygwin=true ;;
  23. (Darwin*) alias isdarwin=true ;;
  24. (Linux*) alias islinux=true ;;
  25. esac
  26. null ls --version && alias with_coreutils=true
  27. ( ismsys || iscygwin ) && alias iswindows=true
  28. alias inbash=false
  29. alias inzsh=false
  30. if test -n "$BASH_VERSION"
  31. then
  32. alias inbash=true
  33. elif test -n "$ZSH_VERSION"
  34. then
  35. alias inzsh=true
  36. fi
  37. #################################
  38. __match(){
  39. # __match str1 str2
  40. # return 0 if str2 is found in str1
  41. expr "$1" : ".*$2.*" >/dev/null
  42. }
  43. if ! __match "$PATH" "$HOME/.local/bin"
  44. then
  45. PATH="${PATH}:${HOME}/.local/bin:$HOME/.local/lib/gems/bin"
  46. fi
  47. if ismsys && ! __match "$PATH" /c/mingw/bin
  48. then
  49. PATH="$PATH:/c/mingw/bin:/c/mingw/msys/1.0/bin"
  50. fi
  51. # # it is not so good
  52. # # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  53. # # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  54. # without this ENV i cannot run tmux. another way is to use --disable-shared
  55. # when building tmux
  56. if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  57. then
  58. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  59. fi
  60. ##################################
  61. export LANG=ja_JP.UTF-8
  62. export LC_MESSAGES=C
  63. export TERMCAP="${TERMCAP}:vb="
  64. ismsys && export HOSTNAME
  65. export ENV=~/.shrc
  66. if false iswindows
  67. then
  68. export PAGER='tr -d \\r | less'
  69. else
  70. export PAGER="less"
  71. fi
  72. export LESS="-iRMX"
  73. _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  74. if test -n "$_src_hilite_lp_path"
  75. then
  76. export LESSOPEN="| $_src_hilite_lp_path %s"
  77. fi
  78. if null type vim
  79. then
  80. export EDITOR=vim
  81. else
  82. export EDITOR=vi
  83. fi
  84. # export CDPATH=".:~"
  85. export VISUAL="$EDITOR"
  86. export GIT_PAGER="less -FS"
  87. export GIT_EDITOR="$EDITOR"
  88. export GIT_MERGE_AUTOEDIT=no
  89. if test -n "$TMUX" && \
  90. echo $TERM | grep screen >/dev/null 2>&1 && \
  91. tmux display -p '#{client_termname}' | grep 256color >/dev/null 2>&1
  92. then
  93. TERM=screen-256color
  94. fi
  95. if test -z "$TMP"
  96. then
  97. if test -n "$TMPDIR"
  98. then
  99. export TMP=$TMPDIR
  100. elif test -n "$TEMP"
  101. then
  102. export TMP="$TEMP"
  103. else
  104. export TMP=/tmp
  105. fi
  106. fi
  107. __match "$TMP" "${USER}-tmp" >/dev/null || export TMP="${TMP}/${USER}-tmp"
  108. export TEMP="$TMP"
  109. mkdir -p "$TMP"
  110. ! iswindows && null type stty && {
  111. stty stop undef # unbind C-s to stop displaying output
  112. # stty erase '^h'
  113. }
  114. if iswindows; then
  115. export USER=$USERNAME
  116. fi
  117. _tmux_prefs(){
  118. null type tmux || return 1
  119. tmux set -g mode-keys vi
  120. }
  121. if test -d ~/dbx
  122. then
  123. export CHIT_PATH="$HOME/dbx/.chit"
  124. fi
  125. #######################
  126. iswindows && alias tty="echo cmd.exe"
  127. type fortune >/dev/null 2>&1 && {
  128. fortune
  129. echo
  130. fortune -o
  131. echo
  132. }
  133. uname -a
  134. echo TERM $TERM $(tput colors) colors connected to $(tty), \
  135. running $BASH $BASH_VERSION
  136. if test -n "$TMUX"
  137. then
  138. tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
  139. 2>/dev/null
  140. echo
  141. fi
  142. ###################################
  143. # some aliases and functions
  144. ( ! with_coreutils && isdarwin ) || test "$TERM" = dumb || \
  145. _coloroption=" --color=auto"
  146. ( ! with_coreutils && isdarwin ) || iswindows || \
  147. _timeoption=" --time-style=long-iso"
  148. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  149. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  150. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  151. _timeformat_num="%Y%m%d%H%M%S"
  152. alias datenum="date +$_timeformat_num"
  153. alias ls="ls -hCF${_coloroption}${_timeoption}"
  154. if ! with_coreutils
  155. then
  156. export LSCOLORS=gxfxcxdxbxegedabagacad
  157. alias ls="ls -G"
  158. fi
  159. # export GREP_OPTIONS=""
  160. alias gr="grep -n --color=always"
  161. iswindows && alias grep="grep -n"
  162. # alias ll="ls -l"
  163. # alias la="ls -A"
  164. # alias lla="ls -Al"
  165. alias less="less -F"
  166. null type emacs && alias em="emacs -nw"
  167. null type vim && alias vi=vim
  168. alias pstree="LANG=C pstree"
  169. alias cp="cp -v"
  170. alias mv="mv -v"
  171. alias rm="rm -v"
  172. alias psaux="ps auxww"
  173. alias q=exit
  174. null type e3em && alias e3=e3em
  175. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  176. alias po=popd
  177. alias pu=pushd
  178. null type sudo && alias sudo="sudo " # use aliases through sudo
  179. null type sudoedit && alias sudoe="sudoedit"
  180. null type halt && alias halt="sudo halt"
  181. null type reboot && alias reboot="sudo reboot"
  182. null type dbus-send && {
  183. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  184. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  185. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  186. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  187. }
  188. alias rand="echo \$RANDOM"
  189. null type file-roller && alias xunp="file-roller -h"
  190. null type paco && alias pc="sudo \paco -D"
  191. alias pycalc="python -i -c 'from math import *' "
  192. null type python3 && alias py3=python3
  193. null type python2 && alias py2=python2
  194. alias _reloadrc="exec \"$SHELL\""
  195. # alias mytime="date +%Y%m%d-%H%M%S"
  196. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  197. # type trash >/dev/null 2>&1 && alias rm=trash
  198. null type mpg123 && alias mpg123="mpg123 -C -v --title"
  199. null type xmms2 && alias xm="xmms2"
  200. #export PLAYER="mpg123 -C -v --title"
  201. null type screen && alias screen="screen -e^z^z"
  202. #alias zcd="cd \`zenity --file-selection --directory\`"
  203. null type gtags && alias gtags="gtags --verbose"
  204. null type htags && alias htags="htags --xhtml --symbol --line-number \
  205. --frame --alphabet --verbose"
  206. null type aunpack && alias au=aunpack
  207. null type lv && alias lv="lv|less"
  208. null type rsync && alias rs="rsync --progress --itemize-changes --compress"
  209. isdarwin && alias updatedb="LC_ALL=C updatedb"
  210. # do not use locate installed by macports
  211. isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  212. # pad
  213. alias pad=notepad
  214. null type gedit && alias pad=gedit
  215. null type leafpad && alias pad=leafpad
  216. isdarwin && alias pad="open -e"
  217. null type wicd-curses && alias wic=wicd-curses
  218. null type wicd-cli && alias wil="wicd-cli -y -l | head"
  219. #alias wicn="wicd-cli -y -c -n"
  220. wicn(){
  221. if test $# -eq 0
  222. then
  223. local num
  224. wicd-cli -y -l | head
  225. echo -n "input num: "
  226. read num
  227. test -n "$num" && wicd-cli -y -c -n $num
  228. else
  229. wicd-cli -y -c -n $1
  230. fi
  231. }
  232. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  233. /usr/share/vim/vim73/macros/less.sh \
  234. /usr/share/vim/vim72/macros/less.sh
  235. do
  236. test -f $f && alias vl=$f && break
  237. done
  238. alias pa=pacapt
  239. null type yaourt && alias yt=yaourt
  240. null type cower && alias cower="cower --color=auto"
  241. null type pacmatic && {
  242. alias pacman="pacmatic"
  243. export PACMAN="pacmatic"
  244. }
  245. _pacman_update_mirrorlist_with_reflector(){
  246. ml=/etc/pacman.d/mirrorlist
  247. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  248. if test -z "$cmd"
  249. then
  250. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  251. fi
  252. echo "Running $cmd ..." 1>&2
  253. sudo $cmd
  254. }
  255. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  256. alias reflect_mirrorlist=_pacman_update_mirrorlist_with_reflector
  257. null type apt-get && {
  258. alias aupgrade="sudo apt-get autoremove --yes && \
  259. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  260. alias aptin="apt-get install"
  261. alias aptsearch="apt-cache search"
  262. alias aptshow="apt-cache show"
  263. }
  264. null type port && {
  265. alias port="port -v"
  266. alias pupgrade="sudo port -v selfupdate && \
  267. { sudo port -v upgrade outdated; }"
  268. }
  269. if iscygwin; then
  270. null type windate || \
  271. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  272. # alias cygsu="cygstart /cygwinsetup.exe"
  273. # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  274. fi
  275. g(){
  276. if test $# -eq 0 && null type git-info
  277. then
  278. git info
  279. else
  280. git "$@"
  281. fi
  282. }
  283. if null type _git && inbash
  284. then
  285. # enable programmable completion for g
  286. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  287. || complete -o default -o nospace -F _git g
  288. fi
  289. git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  290. null type gitmemo && alias m=gitmemo
  291. null type gitmemo && alias m=gitmemo
  292. alias setup.py="sudo python3 setup.py install --record files.txt"
  293. ssh(){
  294. __my_set_screen_title ssh
  295. command ssh "$@"
  296. }
  297. clk(){
  298. local tformat="%Y/%m/%d %H:%M:%S %z"
  299. cal
  300. REPLY=
  301. printf "\\r`date "+${tformat}"`"
  302. read -t 1
  303. while test $? -ne 0
  304. do
  305. printf "\\r`date "+${tformat}"`"
  306. read -t 1
  307. done
  308. }
  309. s(){
  310. if git rev-parse --git-dir >/dev/null 2>&1
  311. then
  312. echo ">> git grep -n $@" 1>&2
  313. git grep -n "$@"
  314. elif which ag >/dev/null 2>&1
  315. then
  316. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  317. ag --pager="$PAGER" "$@"
  318. elif which ack >/dev/null 2>&1
  319. then
  320. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  321. ack --pager="$PAGER" "$@"
  322. else
  323. echo \
  324. ">> find . " \
  325. "-path '*/.git' -prune -o" \
  326. "-path '*/.svn' -prune -o" \
  327. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  328. if test $# -eq 0
  329. then
  330. echo "No search word given." 1>&2
  331. return 1
  332. fi
  333. find . \
  334. -path '*/.git' -prune -o \
  335. -path '*/.svn' -prune -o \
  336. -type -f -exec grep -nH -e --color=always "$@" {} + \
  337. | $PAGER
  338. fi
  339. }
  340. man(){
  341. env \
  342. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  343. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  344. LESS_TERMCAP_me=$(printf "\e[0m") \
  345. LESS_TERMCAP_se=$(printf "\e[0m") \
  346. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  347. LESS_TERMCAP_ue=$(printf "\e[0m") \
  348. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  349. man "$@"
  350. }
  351. netwait(){
  352. while ! ping -c 1 -t 1 example.com
  353. do
  354. true
  355. done
  356. echo network works.
  357. }
  358. cd(){
  359. if test $# -eq 0
  360. then
  361. pushd ~/ >/dev/null
  362. elif test "$1" = -
  363. then
  364. local pwd="$PWD"
  365. command cd "$OLDPWD"
  366. pushd -n "$pwd" >/dev/null # stack last dir
  367. elif ! test -d "$1"
  368. then
  369. echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
  370. return 1
  371. else
  372. pushd "$1" >/dev/null
  373. fi
  374. __dirs_rm_dup "$PWD"
  375. echo "$PWD"
  376. }
  377. __dirs_rm_dup(){
  378. for d in "$@"
  379. do
  380. local next="$(__realpath --strip "$d")"
  381. for l in $(\dirs -v -l | cut -d $'\n' -f 2- | \
  382. \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
  383. do
  384. popd +$l -n >/dev/null
  385. done
  386. done
  387. }
  388. __realpath(){
  389. if type realpath >/dev/null 2>&1
  390. then
  391. command realpath "$@"
  392. else
  393. while ! test -d $1
  394. do
  395. shift
  396. done
  397. local d="$OLDPWD"
  398. command cd "$1"
  399. echo "$PWD"
  400. command cd "$d"
  401. fi
  402. }
  403. dh(){
  404. if test $# -eq 0
  405. then
  406. dirs -v -l
  407. else
  408. local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
  409. cd "$dir"
  410. fi
  411. }
  412. input(){
  413. local foo
  414. stty -echo
  415. read foo
  416. stty echo
  417. echo $foo
  418. }
  419. # tmux(){
  420. # if test $# -eq 0
  421. # then
  422. # (cd ~; command tmux start;)
  423. # if command tmux has -t main
  424. # then
  425. # command tmux attach -t main
  426. # else
  427. # (cd ~; command tmux new -s main;)
  428. # fi
  429. # else
  430. # command tmux "$@"
  431. # fi
  432. # }
  433. tx(){
  434. if test $# -eq 0
  435. then
  436. tmux ls
  437. echo "tx <session> to attach."
  438. elif tmux has -t "$1"
  439. then
  440. tmux attach -t "$1"
  441. else
  442. tmux new -s "$1"
  443. fi
  444. }
  445. dt(){
  446. # dt [<name>] [<command ...>]
  447. __dtach_dir="${TMP}/dtach"
  448. install -d "${__dtach_dir}"
  449. if test -n "${__MY_DTACH}"
  450. then
  451. echo "Current session: ${__MY_DTACH}"
  452. fi
  453. if test -z "$1"
  454. then
  455. echo "Sessions:"
  456. ls "${__dtach_dir}"
  457. return 0
  458. elif test "$1" = "-h"
  459. then
  460. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  461. return 1
  462. fi
  463. soc_name="${__dtach_dir}/$1"
  464. shift
  465. if test -n "$__MY_DTACH"
  466. then
  467. echo "dtach session cannot be nested." 1>&2
  468. return 1
  469. elif test -S "$soc_name"
  470. then
  471. dtach -a "$soc_name" -e ^^
  472. elif test -e "$soc_name"
  473. then
  474. echo "dt: File named $soc_name already exists."
  475. return 1
  476. elif test -z "$1"
  477. then
  478. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  479. # echo "dt: Socket named $soc_name not exists and no command specified."
  480. # return 1
  481. else
  482. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  483. fi
  484. }
  485. scr(){
  486. test -n "$1" && pf="${1}-"
  487. local _tformat="%Y%m%d-%H%M%S%z"
  488. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  489. __MY_SCRIPT=${_file} script ${_file} "$@"
  490. }
  491. dtscr(){
  492. # dtscr <command ...>
  493. if test -z "$1"
  494. then
  495. echo "dtscr: usage: dtscr <command ...>"
  496. return 1
  497. fi
  498. local _cmdstr="`echo $@ | tr ' ' +`"
  499. local _tformat="%Y%m%d-%H%M%S%z"
  500. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  501. local _scr_file="${HOME}/${_name}.script"
  502. local _dt_dir="${TMP}/dtscr"
  503. install -d "$_dt_dir"
  504. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  505. # echo $_name
  506. # echo $_file
  507. }
  508. mcrypt-stream(){
  509. test $# -eq 2 || return 1
  510. case $1 in
  511. en)
  512. mcrypt --key $2 | base64 ;;
  513. de)
  514. base64 -d | mcrypt -d --key $2 ;;
  515. esac
  516. }
  517. gpg-stream(){
  518. test $# -eq 2 || return 1
  519. case $1 in
  520. en)
  521. gpg --passphrase $2 -c --batch |base64 ;;
  522. de)
  523. base64 -d|gpg --passphrase $2 -d --batch ;;
  524. esac
  525. }
  526. dgpg(){
  527. if test "$1" = help || test -z "$2"
  528. then
  529. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  530. return
  531. fi
  532. local srcs="$2"
  533. local dsts="$3"
  534. test -z "$dsts" && dsts="${srcs}.out"
  535. local pw
  536. echo -n "dgpg pw: "
  537. read -s pw
  538. echo ""
  539. test -z "$pw" && return 1
  540. for f in *${srcs}
  541. do
  542. local d="$(basename "$f" "${srcs}")${dsts}"
  543. echo -n "Processing $f to $d..."
  544. if test -d "$f"
  545. then
  546. echo "`printf 'failed (%s is directory)' $f`"
  547. elif test -f "$d"
  548. then
  549. echo "`printf 'failed (%s is already exists)' $d`"
  550. elif <"$f" gpg-stream $1 $pw >"$d" 2>/dev/null
  551. then
  552. echo "done"
  553. else
  554. echo "failed"
  555. test -f "$d" && rm "$d"
  556. fi
  557. done
  558. }
  559. alias enst="gpg-stream en"
  560. alias dest="gpg-stream de"
  561. showinfo(){
  562. echo "Japanese letters are 表示可能"
  563. __try_exec diskinfo
  564. ! isdarwin && test -n "${DISPLAY}" && {
  565. __try_exec xrandr | \grep --color=never ^Screen
  566. }
  567. iswindows || __try_exec finger $USER
  568. LANG=C __try_exec id
  569. __try_exec xset q
  570. }
  571. x(){
  572. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  573. #mkdir -p ~/.my/log
  574. # nohup startx >~/.my/log/xorg.log 2>&1 &
  575. # exit
  576. exec startx
  577. else
  578. echo "X cant be started! Another X is already running?" 1>&2
  579. fi
  580. }
  581. bak(){
  582. for file in "$@"
  583. do
  584. cp -v ${file} ${file}.bak
  585. done
  586. }
  587. di(){
  588. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  589. then
  590. local diffcmd=colordiff
  591. else
  592. local diffcmd=diff
  593. fi
  594. ${diffcmd} -u "$@" | ${PAGER}
  595. }
  596. tb(){
  597. local tb="$HOME/.my/tb"
  598. mkdir -p "$tb"
  599. for file in "$@"
  600. do
  601. mv -t "$tb" "$file"
  602. done
  603. }
  604. mkcd(){
  605. if test -z "$1"
  606. then
  607. echo "mkcd: usage: mkcd <dir>"
  608. return 1
  609. elif test -d "$1"
  610. then
  611. echo "Dir \"$1\" already exists."
  612. else
  613. mkdir -p "$1"
  614. echo "Dir \"$1\" created."
  615. fi
  616. cd "$1"
  617. }
  618. mkcdd(){
  619. # make and change date directory
  620. _d=`date +%Y%m%d-%H%M%S`
  621. mkcd "$_d"
  622. }
  623. if test -n "$TMUX" && null type reattach-to-user-namespace
  624. then
  625. alias pbpaste="reattach-to-user-namespace pbpaste"
  626. alias pbcopy="reattach-to-user-namespace pbcopy"
  627. fi
  628. catclip(){
  629. if iswindows
  630. then
  631. cat /dev/clipboard | tr -d \\r
  632. elif isdarwin
  633. then
  634. pbpaste
  635. else
  636. xclip -o -selection "clipboard"
  637. fi
  638. }
  639. setclip(){
  640. if test $# -eq 0
  641. then
  642. exec 3<&0
  643. else
  644. exec 3<<__EOF__
  645. `cat "$@"`
  646. __EOF__
  647. fi
  648. if iswindows
  649. then
  650. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  651. elif isdarwin
  652. then
  653. pbcopy 0<&3
  654. else
  655. 0<&3 xclip -i -f -selection "primary" | \
  656. xclip -i -f -selection "clipboard"
  657. fi
  658. exec 3<&-
  659. }
  660. open_file(){
  661. if iswindows
  662. then
  663. cmd.exe //c start "" "$@"
  664. elif isdarwin
  665. then
  666. touch "$@"
  667. open "$@"
  668. elif islinux
  669. then
  670. touch "$@"
  671. if null type pcmanfm; then
  672. LC_MESSAGES= pcmanfm "$@"
  673. else
  674. LC_MESSAGES= xdg-open "$@" &
  675. fi
  676. else
  677. cat "$@"
  678. fi
  679. }
  680. o(){
  681. if test $# -eq 0
  682. then
  683. open_file .
  684. else
  685. for f in "$@"
  686. do
  687. open_file "$(realpath "$f")"
  688. done
  689. fi
  690. }
  691. convmv-sjis2utf8-test(){
  692. convmv -r -f sjis -t utf8 *
  693. }
  694. convmv-sjis2utf8-notest(){
  695. convmv -r -f sjis -t utf8 * --notest
  696. }
  697. winln(){
  698. # for windose make link (actually junction)
  699. if test $# -eq 0
  700. then
  701. {
  702. echo "usage: winln TARGET LINK_NAME"
  703. echo "Create a link to TARGET with the name LINK_NAME \
  704. (that is, TARGET must already exist)."
  705. echo "About other features run 'junction'."
  706. } 1>&2
  707. return 1
  708. else
  709. junction "$2" "$1"
  710. fi
  711. }
  712. #################################################
  713. ## pastebin services
  714. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  715. sprunge(){
  716. # http://sprunge.us
  717. if test -z "$1"
  718. then
  719. curl -F 'sprunge=<-' http://sprunge.us
  720. else
  721. curl http://sprunge.us/$1
  722. fi
  723. }
  724. dpaste(){
  725. # http://dpaste.de
  726. if test -z "$1"
  727. then
  728. curl -F 'content=<-' https://dpaste.de/api/
  729. echo
  730. else
  731. curl https://dpaste.de/$1/raw/
  732. fi
  733. }
  734. ######################################
  735. ## Prompt
  736. __my_moc_state(){
  737. type mocp >/dev/null 2>&1 || return
  738. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  739. printf "$1" "`mocp -Q %title 2>/dev/null`"
  740. }
  741. __my_parse_svn_branch() {
  742. local LANG=C
  743. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  744. local svn_repository_root=$(svn info 2>/dev/null | \
  745. sed -ne 's#^Repository Root: ##p')
  746. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  747. awk '{print $1}'
  748. }
  749. __my_svn_ps1(){
  750. if svn status >/dev/null 2>&1
  751. then
  752. local svn_branch=$(__my_parse_svn_branch)
  753. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  754. fi
  755. }
  756. __my_battery_status(){
  757. local dir=/sys/class/power_supply/BAT0
  758. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  759. test -r $dir/charge_now
  760. then
  761. local st=$(cat $dir/status)
  762. local full=$(cat $dir/charge_full)
  763. local now=$(cat $dir/charge_now)
  764. local rate=$(expr $now \* 100 / $full)
  765. printf "$1" "${st}:${rate}%"
  766. fi
  767. }
  768. alias bat='__my_battery_status %s\\n'
  769. ip-address(){
  770. type ip >/dev/null 2>&1 || return 1
  771. local ip=$(LANG=C ip addr show scope global | \
  772. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  773. test -n "$ip" && printf $1 $ip
  774. }
  775. __my_ps1_str=""
  776. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  777. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  778. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  779. __my_ps1_scale(){
  780. local last=$?
  781. if null type stty && ! ismsys
  782. then
  783. stty size | tr -d $'\n' | tr " " x
  784. printf " "
  785. fi
  786. return $last
  787. }
  788. __my_ps1_tmux(){
  789. local last=$?
  790. null type tmux || return $last
  791. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  792. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  793. return $last
  794. }
  795. __my_ps1_moc(){
  796. local last=$?
  797. __my_moc_state "[MOC:%s]"
  798. return $last
  799. }
  800. for f in /usr/share/git/git-prompt.sh \
  801. /opt/local/share/git-core/git-prompt.sh \
  802. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  803. do
  804. test -r "$f" && . "$f" && break
  805. done
  806. GIT_PS1_SHOWDIRTYSTATE=t
  807. GIT_PS1_SHOWUPSTREAM=t
  808. __my_ps1_git(){
  809. local last=$?
  810. null type __git_ps1 || return $last
  811. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  812. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  813. return $last
  814. }
  815. __my_ps1_ipaddr(){
  816. local last=$?
  817. ! iswindows && ip-address [Addr:%s]
  818. return $last
  819. }
  820. __my_ps1_bttry(){
  821. local last=$?
  822. local bst="${TMP}/batterystatus"
  823. if test -z "$DISPLAY" && ! iswindows
  824. then
  825. test -f $bst && local bstr="$(cat $bst)"
  826. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  827. echo "[Battery:$bstr]"
  828. __my_battery_status %s >$bst &
  829. fi
  830. return $last
  831. }
  832. __my_ps1_dirs(){
  833. dirs | wc -l
  834. }
  835. __my_ps1_jobs(){
  836. jobs | wc -l
  837. }
  838. if test "$TERM" != dumb
  839. then
  840. __my_c1="\[\e[0;33m\]" # color for PWD
  841. __my_c2="\[\e[0;36m\]" # color for user
  842. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  843. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  844. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  845. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  846. else __my_c4="\[\e[1;32m\]" # color for ::
  847. fi
  848. __my_c5="\[\e[30;47m\]" # color for SCR
  849. __my_cdef="\[\e[0m\]"
  850. fi
  851. # export _LAST_STATUS=a
  852. # __my_export_last_status(){
  853. # local last=$?
  854. # echo $last
  855. # export _LAST_STATUS=$last
  856. # echo $_LAST_STATUS
  857. # return $last
  858. # }
  859. _ps1_bash="\
  860. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  861. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? $(__my_ps1_scale) \D{%T} ${__my_ps1_str}\$ "
  862. inbash && PS1=$_ps1_bash
  863. __my_set_screen_title(){
  864. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  865. then
  866. echo -ne "\033k$1\033\\"
  867. fi
  868. }
  869. __my_set_title(){
  870. case $TERM in
  871. (rxvt*|xterm*|aterm|screen*)
  872. title="$(echo $@)"
  873. test -t 1 &&
  874. test -n "$DISPLAY" &&
  875. test -z "$EMACS" &&
  876. echo -n -e "\033]0;${title}\007"
  877. ;;
  878. esac
  879. }
  880. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  881. __my_set_screen_title \$(basename \"\$PWD\")/"