Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

1110 lignes
26 KiB

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