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ů.
 
 
 
 
 
 

1016 řádky
23 KiB

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