Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

1310 строки
31 KiB

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