diff options
Diffstat (limited to 'Functions/Misc')
-rw-r--r-- | Functions/Misc/add-zle-hook-widget | 4 | ||||
-rw-r--r-- | Functions/Misc/colors | 11 | ||||
-rw-r--r-- | Functions/Misc/regexp-replace | 102 | ||||
-rw-r--r-- | Functions/Misc/run-help | 15 | ||||
-rw-r--r-- | Functions/Misc/run-help-btrfs | 18 | ||||
-rw-r--r-- | Functions/Misc/run-help-git | 10 | ||||
-rw-r--r-- | Functions/Misc/run-help-ip | 4 | ||||
-rw-r--r-- | Functions/Misc/run-help-p4 | 2 | ||||
-rw-r--r-- | Functions/Misc/run-help-sudo | 2 | ||||
-rw-r--r-- | Functions/Misc/run-help-svk | 2 | ||||
-rw-r--r-- | Functions/Misc/run-help-svn | 2 | ||||
-rw-r--r-- | Functions/Misc/zargs | 54 | ||||
-rw-r--r-- | Functions/Misc/zed | 62 | ||||
-rw-r--r-- | Functions/Misc/zmathfuncdef | 2 |
14 files changed, 201 insertions, 89 deletions
diff --git a/Functions/Misc/add-zle-hook-widget b/Functions/Misc/add-zle-hook-widget index 9cc35496f..4d8049083 100644 --- a/Functions/Misc/add-zle-hook-widget +++ b/Functions/Misc/add-zle-hook-widget @@ -47,9 +47,9 @@ function azhw:${^hooktypes} { for hook in "${(@)${(@on)hook_widgets[@]}#<->:}"; do if [[ "$hook" = user:* ]]; then # Preserve $WIDGET within the renamed widget - zle "$hook" -N -- "$@" + zle "$hook" -f "nolast" -N -- "$@" else - zle "$hook" -Nw -- "$@" + zle "$hook" -f "nolast" -Nw -- "$@" fi || return done return 0 diff --git a/Functions/Misc/colors b/Functions/Misc/colors index 027ca9a14..5e9d77d10 100644 --- a/Functions/Misc/colors +++ b/Functions/Misc/colors @@ -14,11 +14,12 @@ color=( 00 none # 20 gothic 01 bold # 21 double-underline 02 faint 22 normal - 03 standout 23 no-standout + 03 italic 23 no-italic # no-gothic 04 underline 24 no-underline 05 blink 25 no-blink # 06 fast-blink # 26 proportional 07 reverse 27 no-reverse +# 07 standout 27 no-standout 08 conceal 28 no-conceal # 09 strikethrough # 29 no-strikethrough @@ -82,9 +83,11 @@ for k in ${color[(I)3?]}; do color[fg-${color[$k]}]=$k; done # This is inaccurate, but the prompt theme system needs it. -color[grey]=${color[black]} -color[fg-grey]=${color[grey]} -color[bg-grey]=${color[bg-black]} +for k in grey gray; do + color[$k]=${color[black]} + color[fg-$k]=${color[$k]} + color[bg-$k]=${color[bg-black]} +done # Assistance for the color-blind. diff --git a/Functions/Misc/regexp-replace b/Functions/Misc/regexp-replace index dec105524..d4408f0f7 100644 --- a/Functions/Misc/regexp-replace +++ b/Functions/Misc/regexp-replace @@ -8,36 +8,84 @@ # $ and backtick substitutions; in particular, $MATCH will be replaced # by the portion of the string matched by the regular expression. -integer pcre +# we use positional parameters instead of variables to avoid +# clashing with the user's variable. Make sure we start with 3 and only +# 3 elements: +argv=("$1" "$2" "$3") -[[ -o re_match_pcre ]] && pcre=1 +# $4 records whether pcre is enabled as that information would otherwise +# be lost after emulate -L zsh +4=0 +[[ -o re_match_pcre ]] && 4=1 emulate -L zsh -(( pcre )) && setopt re_match_pcre - -# $4 is the string to be matched -4=${(P)1} -# $5 is the final string -5= -# 6 indicates if we made a change -6= + + local MATCH MBEGIN MEND local -a match mbegin mend -while [[ -n $4 ]]; do - if [[ $4 =~ $2 ]]; then - # append initial part and subsituted match - 5+=${4[1,MBEGIN-1]}${(e)3} - # truncate remaining string - 4=${4[MEND+1,-1]} - # indicate we did something - 6=1 - else - break - fi -done -5+=$4 - -eval ${1}=${(q)5} -# status 0 if we did something, else 1. -[[ -n $6 ]] +if (( $4 )); then + # if using pcre, we're using pcre_match and a running offset + # That's needed for ^, \A, \b, and look-behind operators to work + # properly. + + zmodload zsh/pcre || return 2 + pcre_compile -- "$2" && pcre_study || return 2 + + # $4 is the current *byte* offset, $5, $6 reserved for later use + 4=0 6= + + local ZPCRE_OP + while pcre_match -b -n $4 -- "${(P)1}"; do + # append offsets and computed replacement to the array + # we need to perform the evaluation in a scalar assignment so that if + # it generates an array, the elements are converted to string (by + # joining with the first character of $IFS as usual) + 5=${(e)3} + argv+=(${(s: :)ZPCRE_OP} "$5") + + # for 0-width matches, increase offset by 1 to avoid + # infinite loop + 4=$((argv[-2] + (argv[-3] == argv[-2]))) + done + + (($# > 6)) || return # no match + + set +o multibyte + + # $5 contains the result, $6 the current offset + 5= 6=1 + for 2 3 4 in "$@[7,-1]"; do + 5+=${(P)1[$6,$2]}$4 + 6=$(($3 + 1)) + done + 5+=${(P)1[$6,-1]} +else + # in ERE, we can't use an offset so ^, (and \<, \b, \B, [[:<:]] where + # available) won't work properly. + + # $4 is the string to be matched + 4=${(P)1} + + while [[ -n $4 ]]; do + if [[ $4 =~ $2 ]]; then + # append initial part and substituted match + 5+=${4[1,MBEGIN-1]}${(e)3} + # truncate remaining string + if ((MEND < MBEGIN)); then + # zero-width match, skip one character for the next match + ((MEND++)) + 5+=${4[1]} + fi + 4=${4[MEND+1,-1]} + # indicate we did something + 6=1 + else + break + fi + done + [[ -n $6 ]] || return # no match + 5+=$4 +fi + +eval $1=\$5 diff --git a/Functions/Misc/run-help b/Functions/Misc/run-help index e351dd6a6..d52c1b032 100644 --- a/Functions/Misc/run-help +++ b/Functions/Misc/run-help @@ -101,12 +101,15 @@ do builtin getln cmd_args builtin print -z "$cmd_args" cmd_args=( ${(z)cmd_args} ) - # Discard environment assignments, etc. - while [[ $cmd_args[1] != ${run_help_orig_cmd:-$1} ]] - do - shift cmd_args || return 1 - done - eval "run-help-$1:t ${(q@)cmd_args[2,-1]}" + + # Discard the command itself & everything before it. + shift $cmd_args[(i)${run_help_orig_cmd:-$1}] cmd_args || + return + + # Discard options, parameter assignments & paths. + cmd_args=( ${cmd_args[@]:#([-+]*|*=*|*/*|\~*)} ) + + eval "run-help-$1:t ${(@q)cmd_args}" else POSIXLY_CORRECT=1 man $@:t fi diff --git a/Functions/Misc/run-help-btrfs b/Functions/Misc/run-help-btrfs new file mode 100644 index 000000000..cb139e9b7 --- /dev/null +++ b/Functions/Misc/run-help-btrfs @@ -0,0 +1,18 @@ +case $1 in + (b*) man btrfs-balance ;; + (c*) man btrfs-check ;; + (d*) man btrfs-device ;; + (f*) man btrfs-filesystem ;; + (i*) man btrfs-inspect-internal ;; + (p*) man btrfs-property ;; + (qg*) man btrfs-qgroup ;; + (qu*) man btrfs-quota ;; + (rec*) man btrfs-receive ;; + (rep*) man btrfs-replace ;; + (resc*) man btrfs-rescue ;; + (rest*) man btrfs-restore ;; + (sc*) man btrfs-scrub ;; + (se*) man btrfs-send ;; + (su*) man btrfs-subvolume ;; + (*) man btrfs ;; +esac diff --git a/Functions/Misc/run-help-git b/Functions/Misc/run-help-git index ce94d0d02..a841f89d6 100644 --- a/Functions/Misc/run-help-git +++ b/Functions/Misc/run-help-git @@ -1,9 +1 @@ -if [ $# -eq 0 ]; then - man git -else - local al - if al=$(git config --get "alias.$1"); then - 1=${al%% *} - fi - man git-$1 -fi +git help ${1:-git} diff --git a/Functions/Misc/run-help-ip b/Functions/Misc/run-help-ip index 8807f9ef1..b811ce352 100644 --- a/Functions/Misc/run-help-ip +++ b/Functions/Misc/run-help-ip @@ -14,10 +14,6 @@ if ! man -w ip-address >/dev/null 2>&1; then return fi -while [[ $# != 0 && $1 == -* ]]; do - shift -done - case $1 in (addrl*) man ip-addrlabel ;; (a*) man ip-address ;; diff --git a/Functions/Misc/run-help-p4 b/Functions/Misc/run-help-p4 index 662ce94fe..e48a4d068 100644 --- a/Functions/Misc/run-help-p4 +++ b/Functions/Misc/run-help-p4 @@ -2,4 +2,4 @@ if (( ! $# )); then p4 help commands else p4 help $1 -fi | ${=PAGER:-less} +fi | ${=PAGER:-more} diff --git a/Functions/Misc/run-help-sudo b/Functions/Misc/run-help-sudo index f38696e19..a8fa7aa0f 100644 --- a/Functions/Misc/run-help-sudo +++ b/Functions/Misc/run-help-sudo @@ -2,6 +2,6 @@ if [ $# -eq 0 ]; then man sudo else - man $1 + run-help $1 fi diff --git a/Functions/Misc/run-help-svk b/Functions/Misc/run-help-svk index 92438a53f..782538246 100644 --- a/Functions/Misc/run-help-svk +++ b/Functions/Misc/run-help-svk @@ -1 +1 @@ -svk help ${${@:#-*}[1]} | ${=PAGER:-more} +svk help $1 | ${=PAGER:-more} diff --git a/Functions/Misc/run-help-svn b/Functions/Misc/run-help-svn index 5d1068588..d55a493a6 100644 --- a/Functions/Misc/run-help-svn +++ b/Functions/Misc/run-help-svn @@ -1 +1 @@ -svn help ${${@:#-*}[1]} | ${=PAGER:-more} +svn help $1 | ${=PAGER:-more} diff --git a/Functions/Misc/zargs b/Functions/Misc/zargs index 28ebca78f..81916a3ac 100644 --- a/Functions/Misc/zargs +++ b/Functions/Misc/zargs @@ -43,14 +43,12 @@ # than 127 for "command not found" so this function incorrectly returns # 123 in that case if used with zsh 4.0.x. # -# With the --max-procs option, zargs may not correctly capture the exit -# status of the backgrounded jobs, because of limitations of the "wait" -# builtin. If the zsh/parameter module is not available, the status is -# NEVER correctly returned, otherwise the status of the longest-running -# job in each batch is captured. +# Because of "wait" limitations, --max-procs spawns max-procs jobs, then +# waits for all of those, then spawns another batch, etc. # -# Also because of "wait" limitations, --max-procs spawns max-procs jobs, -# then waits for all of those, then spawns another batch, etc. +# The maximum number of parallel jobs for which exit status is available +# is determined by the sysconf CHILD_MAX parameter, which can't be read +# or changed from within the shell. # # Differences from POSIX xargs: # @@ -69,11 +67,27 @@ # -I/-L and implementations reportedly differ.) In zargs, -i/-I have # this behavior, as do -l/-L, but when -i/-I appear anywhere then -l/-L # are ignored (forced to 1). +# +# * The use of SIGUSR1 and SIGUSR2 to change the number of parallel jobs +# is not supported. + +# First, capture the current setopts as "sticky emulation" +if zmodload zsh/parameter +then + emulate $(emulate -l) -c "\ + _zarun() { + options=( ${(j: :kv)options[@]} monitor off zle off )"' + eval "$@" + }' +else + # Warning? + emulate $(emulate -l) -c '_zarun() { eval "$@" }' +fi emulate -L zsh || return 1 local -a opts eof n s l P i -local ZARGS_VERSION="1.5" +local ZARGS_VERSION="1.7" if zparseopts -a opts -D -- \ -eof::=eof e::=eof \ @@ -186,8 +200,8 @@ local execute=' elif (( $opts[(I)-(-verbose|t)] )) then print -u2 -r -- "$call" fi - eval "{ - \"\${(@)call}\" + _zarun "{ + \"\${call[@]}\" } $bg"' local ret=0 analyze=' case $? in @@ -251,17 +265,19 @@ if (( P != 1 && ARGC > 1 )) then # These setopts are necessary for "wait" on multiple jobs to work. setopt nonotify nomonitor - bg='&' - if zmodload -i zsh/parameter 2>/dev/null - then - wait='wait ${${jobstates[(R)running:*]/#*:/}/%=*/}' - else - wait='wait' - fi + local -a _zajobs + local j + bg='& _zajobs+=( $! )' + wait='wait' + analyze=' + for j in $_zajobs; do + wait $j + '"$analyze"' + done; _zajobs=()' fi -# Everything has to be in a subshell just in case of backgrounding jobs, -# so that we don't unintentionally "wait" for jobs of the parent shell. +# Everything has to be in a subshell so that we don't "wait" for any +# unrelated jobs of the parent shell. ( while ((ARGC)) diff --git a/Functions/Misc/zed b/Functions/Misc/zed index 9eb4b2d93..7d0d590db 100644 --- a/Functions/Misc/zed +++ b/Functions/Misc/zed @@ -5,16 +5,18 @@ # Edit small files with the command line editor. # Use ^X^W to save (or ZZ in vicmd mode), ^C to abort. # Option -f: edit shell functions. (Also if called as fned.) +# Option -h: edit shell history. (Also if called as histed.) setopt localoptions noksharrays local var opts zed_file_name # We do not want timeout while we are editing a file -integer TMOUT=0 okargs=1 fun bind +integer TMOUT=0 okargs=1 fun hist bind local -a expand -zparseopts -D -A opts f b x: +zparseopts -D -A opts f h b x: fun=$+opts[-f] +hist=$+opts[-h] bind=$+opts[-b] if [[ $opts[-x] == <-> ]]; then expand=(-x $opts[-x]) @@ -24,23 +26,28 @@ elif (( $+opts[-x] )); then fi [[ $0 = fned ]] && fun=1 +[[ $0 = histed ]] && hist=1 +(( hist && $# <= 2 )) && okargs=$# (( bind )) && okargs=0 -if (( $# != okargs )); then +if (( $# != okargs || bind + fun + hist > 1 )); then echo 'Usage: zed filename zed -f [ -x N ] function +zed -h [ filename [ size ] ] zed -b' >&2 return 1 fi local curcontext=zed::: -# Matching used in zstyle -m: hide result from caller. -# Variables not used directly here. -local -a match mbegin mend -zstyle -m ":completion:zed:*" insert-tab '*' || - zstyle ":completion:zed:*" insert-tab yes +() { + # Matching used in zstyle -m: hide result from caller. + # Variables not used directly here. + local -a match mbegin mend + zstyle -m ":completion:zed:*" insert-tab '*' || + zstyle ":completion:zed:*" insert-tab yes +} zmodload zsh/terminfo 2>/dev/null @@ -124,22 +131,51 @@ fi setopt localoptions nobanghist if ((fun)) then - var="$(functions $expand -- $1)" + var="$(functions $expand -- "$1")" # If function is undefined but autoloadable, load it if [[ $var = *\#\ undefined* ]] then - var="$(autoload +X $1; functions -- $1)" + var="$(autoload +X "$1"; functions -- "$1")" elif [[ -z $var ]] then var="${(q-)1} () { }" fi vared -M zed -m zed-vicmd -i __zed_init var && eval function "$var" +elif ((hist)) then + if [[ -n $1 ]]; then + { fc -p -a "$1" ${2:-$({ wc -l <"$1" } 2>/dev/null)} || return } + let HISTSIZE++ + print -s "" # Work around fc -p limitation + fi + # When editing the current shell history, the "zed -h" command is not + # itself included because the current event is not added to the ring + # until the next prompt is printed. This means "zed -h" is prepended + # to the result of the edit, because of the way "print -s" is defined. + var=( "${(@Oav)history}" ) + IFS=$'\n' vared -M zed -m zed-vicmd -i __zed_init var + if (( ? )); then + [[ -n $1 ]] && unset HISTFILE + else + local HISTSIZE=0 savehist=$#var + fc -R /dev/null # Remove entries other than those added here + HISTSIZE=$savehist # Resets on function exit because local + [[ -n $1 ]] && SAVEHIST=$savehist # Resets via foregoing fc -a + for (( hist=1; hist <= savehist; hist++ )) + do print -rs -- "$var[hist]" + done + if [[ -n $zed_file_name ]]; then + fc -W "$zed_file_name" + [[ -n $1 ]] && unset HISTFILE + fi + # Note prepend effect when global HISTSIZE greater than $savehist. + # This does not affect file editing. + fi else - zed_file_name=$1 - [[ -f $1 ]] && var="$(<$1)" + zed_file_name="$1" + [[ -f $1 ]] && var="$(<"$1")" while vared -M zed -m zed-vicmd -i __zed_init var do { - print -r -- "$var" >| $zed_file_name + print -r -- "$var" >| "$zed_file_name" } always { (( TRY_BLOCK_ERROR = 0 )) } && break diff --git a/Functions/Misc/zmathfuncdef b/Functions/Misc/zmathfuncdef index e5692e769..5ed991f68 100644 --- a/Functions/Misc/zmathfuncdef +++ b/Functions/Misc/zmathfuncdef @@ -78,7 +78,7 @@ if ! zmodload -e zsh/mathfunc; then fi { - eval "$fname() { (( $body )) }" + eval "$fname() { (( $body )); true }" } always { # Remove math function if shell function definition failed. if (( TRY_BLOCK_ERROR )); then |