25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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