Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

1284 řádky
31 KiB

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