From ce57cdeb9977b9d4d02faada22b62b53c3fb276a Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sat, 18 Mar 2017 22:33:54 +0800 Subject: zsh: Update various settings; with minor fixes Credit: http://chneukirchen.org/dotfiles/.zshrc --- _zsh/40-functions.zsh | 57 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 15 deletions(-) (limited to '_zsh/40-functions.zsh') diff --git a/_zsh/40-functions.zsh b/_zsh/40-functions.zsh index 2b078ed..2a15058 100644 --- a/_zsh/40-functions.zsh +++ b/_zsh/40-functions.zsh @@ -13,17 +13,20 @@ function exists() { type "$1" >/dev/null 2>&1 } + ## Check whether the program is running function is_running() { - pgrep -x -u "${USER}" $1 &> /dev/null + pgrep -x -u "${USER}" "$1" /dev/null 2>&1 } + function zsh_recompile() { autoload -U zrecompile rm -f ~/.zsh/*.zwc [[ -f ~/.zshrc ]] && zrecompile -p ~/.zshrc [[ -f ~/.zshrc.zwc.old ]] && rm -f ~/.zshrc.zwc.old + local f for f in ~/.zsh/**/*.zsh; do [[ -f $f ]] && zrecompile -p $f [[ -f $f.zwc.old ]] && rm -f $f.zwc.old @@ -37,7 +40,7 @@ function zsh_recompile() { function extract() { - echo Extracting $1 ... + echo "Extracting '$1' ..." if [ -f "$1" ] ; then case "$1" in *.tar.bz2) @@ -63,10 +66,10 @@ function extract() { *.7z) 7z x "$1";; *) - echo "'$1' cannot be extracted via extract()" ;; + echo "Unable to extract: '$1'" ;; esac else - echo "'$1' is not a valid file" + echo "Invalid file: '$1'" fi } @@ -89,21 +92,45 @@ function trash() { } -function strip_diff_leading_symbols() { - local color_code_regex="(\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K])" +## Print a horizontal rule +function rule() { + printf "%$(tput cols)s\n" | tr ' ' '-' +} + + +## Interactive move/rename: making renaming long filenames less sucks +# Credit: http://chneukirchen.org/dotfiles/.zshrc +function imv() { + local src dst + for src; do + [[ -e "$src" ]] || { print -u2 "$src: does not exist"; continue } + dst="$src" + vared dst + [[ "$src" != "$dst" ]] && mkdir -p ${dst:h} && mv -n $src $dst + done +} + - # simplify the unified patch diff header - sed -r "s/^($color_code_regex)diff --git .*$//g" | \ - sed -r "s/^($color_code_regex)index .*$/\n\1$(rule)/g" | \ - sed -r "s/^($color_code_regex)\+\+\+(.*)$/\1+++\5\n\1$(rule)\x1B\[m/g" |\ - # actually strips the leading symbols - sed -r "s/^($color_code_regex)[\+\-]/\1 /g" +## Print pre-defined C macros +# Credit: http://chneukirchen.org/dotfiles/.zshrc +ccdef() { + ${1:-cc} $@[2,-1] -dM -E - $# )) && argv[2]=(-- "$argv[2]") && s=2 + zparseopts -D -M -A xopt n: p t P: j:=P v=t i=p # map to xargs(1) flags + (( $@[(i){}] < s )) && xopt[-I]={} + [[ $1 = *'$'* ]] && argv[1]=(zsh -c "$1" --) && (( s += 3 )) + printf '%s\0' "$@[s+1,-1]" | xargs -0 -r -n1 ${(kv)=xopt} "$@[1,s-1]" } # vim: set ts=8 sw=4 tw=0 fenc=utf-8 ft=zsh: # -- cgit v1.2.2