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.
 
 
 
 
 
 

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