Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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