summaryrefslogtreecommitdiff
path: root/Functions/Misc
diff options
context:
space:
mode:
authorJoe Rayhawk <jrayhawk@fairlystable.org>2025-04-30 02:07:56 -0700
committerJoe Rayhawk <jrayhawk@fairlystable.org>2025-04-30 02:07:56 -0700
commit26e09889646be3ea65b4a3dfeda26213e4bb6a27 (patch)
tree4f3c73a9416bf47ad7e125383d23cf42879e38d7 /Functions/Misc
parent841bce705a58b04220b1f257abcc00ae71cbdbdc (diff)
parent001cba48ce3b964cf01fb3e2af54b20eacbc9bf5 (diff)
downloadzsh-26e09889646be3ea65b4a3dfeda26213e4bb6a27.tar.gz
zsh-26e09889646be3ea65b4a3dfeda26213e4bb6a27.zip
Merge branch 'upstream' into debian
Diffstat (limited to 'Functions/Misc')
-rw-r--r--Functions/Misc/add-zle-hook-widget4
-rw-r--r--Functions/Misc/colors20
-rw-r--r--Functions/Misc/is-at-least8
-rw-r--r--Functions/Misc/mkshadow11
-rw-r--r--Functions/Misc/run-help12
-rw-r--r--Functions/Misc/run-help-docker9
-rw-r--r--Functions/Misc/run-help-ip2
-rw-r--r--Functions/Misc/run-help-openssl8
-rw-r--r--Functions/Misc/run-help-perf1
-rw-r--r--Functions/Misc/run-help-podman9
-rw-r--r--Functions/Misc/run-help-ssh6
-rw-r--r--Functions/Misc/run-help-svk1
-rw-r--r--Functions/Misc/run-help-svnadmin1
-rw-r--r--Functions/Misc/zargs40
-rw-r--r--Functions/Misc/zcalc4
-rw-r--r--Functions/Misc/zed14
-rw-r--r--Functions/Misc/zmv14
-rw-r--r--Functions/Misc/zslurp31
18 files changed, 153 insertions, 42 deletions
diff --git a/Functions/Misc/add-zle-hook-widget b/Functions/Misc/add-zle-hook-widget
index 4d8049083..4293a07dd 100644
--- a/Functions/Misc/add-zle-hook-widget
+++ b/Functions/Misc/add-zle-hook-widget
@@ -39,7 +39,7 @@ zstyle zle-hook types ${hooktypes#zle-}
# Relying on multifuncdef option here
function azhw:${^hooktypes} {
- local -a hook_widgets
+ local -a hook_widgets match mbegin mend
local hook
# Values of these styles look like number:name
# and we run them in number order
@@ -58,7 +58,7 @@ function azhw:${^hooktypes} {
# Redefine ourself with the setup left out
function add-zle-hook-widget {
- local -a hooktypes
+ local -a hooktypes match mbegin mend
zstyle -a zle-hook types hooktypes
# This part copied from add-zsh-hook
diff --git a/Functions/Misc/colors b/Functions/Misc/colors
index 5e9d77d10..8a0cec383 100644
--- a/Functions/Misc/colors
+++ b/Functions/Misc/colors
@@ -44,7 +44,7 @@ color=(
35 magenta 45 bg-magenta
36 cyan 46 bg-cyan
37 white 47 bg-white
-# 38 iso-8316-6 # 48 bg-iso-8316-6
+# 38 iso-8613-6 # 48 bg-iso-8613-6
39 default 49 bg-default
# Other codes:
@@ -63,6 +63,16 @@ color=(
# 63 double-overline-or-left
# 64 stress
# 65 no-ideogram-marking
+
+# Bright color codes (xterm extension)
+ 90 bright-gray 100 bg-bright-gray
+ 91 bright-red 101 bg-bright-red
+ 92 bright-green 102 bg-bright-green
+ 93 bright-yellow 103 bg-bright-yellow
+ 94 bright-blue 104 bg-bright-blue
+ 95 bright-magenta 105 bg-bright-magenta
+ 96 bright-cyan 106 bg-bright-cyan
+ 97 bright-white 107 bg-bright-white
)
# A word about black and white: The "normal" shade of white is really a
@@ -79,7 +89,7 @@ for k in ${(k)color}; do color[${color[$k]}]=$k; done
# Add "fg-" keys for all the text colors, for clarity.
-for k in ${color[(I)3?]}; do color[fg-${color[$k]}]=$k; done
+for k in ${color[(I)[39]?]}; do color[fg-${color[$k]}]=$k; done
# This is inaccurate, but the prompt theme system needs it.
@@ -89,7 +99,11 @@ for k in grey gray; do
color[bg-$k]=${color[bg-black]}
done
-# Assistance for the color-blind.
+# Assistance for the colo(u)r-blind.
+
+for k in '' fg- bg-; do
+ color[${k}bright-grey]=${color[${k}bright-gray]}
+done
colour=(${(kv)color}) # A case where ksh namerefs would be useful ...
diff --git a/Functions/Misc/is-at-least b/Functions/Misc/is-at-least
index d4ff3552a..5985684be 100644
--- a/Functions/Misc/is-at-least
+++ b/Functions/Misc/is-at-least
@@ -24,8 +24,14 @@ emulate -L zsh
local IFS=".-" min_cnt=0 ver_cnt=0 part min_ver version order
+: ${2=$ZSH_VERSION}
+
+# sort out the easy corner cases first
+[[ $1 = $2 ]] && return 0 # same version
+[[ -n $2 ]] || return 1 # no version
+
min_ver=(${=1})
-version=(${=2:-$ZSH_VERSION} 0)
+version=(${=2} 0)
while (( $min_cnt <= ${#min_ver} )); do
while [[ "$part" != <-> ]]; do
diff --git a/Functions/Misc/mkshadow b/Functions/Misc/mkshadow
new file mode 100644
index 000000000..2ae3a0f2c
--- /dev/null
+++ b/Functions/Misc/mkshadow
@@ -0,0 +1,11 @@
+#autoload
+# Front-end to the completion helper _shadow for use outside completion.
+# This just forces proper autoload of _shadow/_unshadow and calls them.
+
+autoload _shadow
+mkshadow() { unset REPLY; _shadow "$@" }
+rmshadow() { unset REPLY; _unshadow }
+
+# Bootstrap because of autoload special case
+unset REPLY
+_shadow "$@"
diff --git a/Functions/Misc/run-help b/Functions/Misc/run-help
index d52c1b032..462044b72 100644
--- a/Functions/Misc/run-help
+++ b/Functions/Misc/run-help
@@ -58,11 +58,11 @@ do
case $what in
(*( is an alias for (noglob|nocorrect))*)
[[ ${what[(w)7]:t} != ${what[(w)1]} ]] &&
- run_help_orig_cmd=${what[(w)1]} run-help ${what[(w)7]:t}
+ run_help_orig_cmd=${what[(w)1]} run-help ${what[(w)7]:t} ${(z)${what[(w)8,-1]}}
;;
(*( is an alias)*)
[[ ${what[(w)6]:t} != ${what[(w)1]} ]] &&
- run_help_orig_cmd=${what[(w)1]} run-help ${what[(w)6]:t}
+ run_help_orig_cmd=${what[(w)1]} run-help ${what[(w)6]:t} ${(z)${what[(w)7,-1]}}
;;
(*( is a * function))
case ${what[(w)1]} in
@@ -98,13 +98,13 @@ do
if whence "run-help-$1:t" >/dev/null
then
local cmd_args
- builtin getln cmd_args
+ builtin getln cmd_args &&
builtin print -z "$cmd_args"
- cmd_args=( ${(z)cmd_args} )
+ cmd_args=( ${(z)${cmd_args:-"$*"}} )
# Discard the command itself & everything before it.
- shift $cmd_args[(i)${run_help_orig_cmd:-$1}] cmd_args ||
- return
+ shift $cmd_args[(i)(${run_help_orig_cmd}|$1)] cmd_args 2>/dev/null ||
+ continue
# Discard options, parameter assignments & paths.
cmd_args=( ${cmd_args[@]:#([-+]*|*=*|*/*|\~*)} )
diff --git a/Functions/Misc/run-help-docker b/Functions/Misc/run-help-docker
new file mode 100644
index 000000000..09a8a221a
--- /dev/null
+++ b/Functions/Misc/run-help-docker
@@ -0,0 +1,9 @@
+if [[ $# == 0 ]]
+then
+ man docker
+elif [[ $# > 1 && $1 == (builder|checkpoint|config|container|context|image|manifest|network|node|plugin|secret|service|stack|swarm|system|trust|volume) ]]
+then
+ man docker-$1-$2
+else
+ man docker-$1
+fi
diff --git a/Functions/Misc/run-help-ip b/Functions/Misc/run-help-ip
index b811ce352..f635cce71 100644
--- a/Functions/Misc/run-help-ip
+++ b/Functions/Misc/run-help-ip
@@ -18,7 +18,7 @@ case $1 in
(addrl*) man ip-addrlabel ;;
(a*) man ip-address ;;
(l2*) man ip-l2tp ;;
- (li*) man ip-link ;;
+ (l*) man ip-link ;;
(ma*) man ip-maddress ;;
(mo*) man ip-monitor ;;
(mr*) man ip-mroute ;;
diff --git a/Functions/Misc/run-help-openssl b/Functions/Misc/run-help-openssl
index c528418c8..e4e45070e 100644
--- a/Functions/Misc/run-help-openssl
+++ b/Functions/Misc/run-help-openssl
@@ -1,7 +1 @@
-
-if [ $# -eq 0 ]; then
- man openssl
-else
- man $1
-fi
-
+man openssl${1:+-$1} || man ${1:-openssl}
diff --git a/Functions/Misc/run-help-perf b/Functions/Misc/run-help-perf
new file mode 100644
index 000000000..2e0695af2
--- /dev/null
+++ b/Functions/Misc/run-help-perf
@@ -0,0 +1 @@
+man perf${1:+-$1}
diff --git a/Functions/Misc/run-help-podman b/Functions/Misc/run-help-podman
new file mode 100644
index 000000000..64d9cd83f
--- /dev/null
+++ b/Functions/Misc/run-help-podman
@@ -0,0 +1,9 @@
+if [[ $# == 0 ]]
+then
+ man podman
+elif [[ $# > 1 && $1 == (container|generate|healthcheck|image|kube|machine|manifest|network|pod|secret|system|volume) ]]
+then
+ man podman-$1-$2
+else
+ man podman-$1
+fi
diff --git a/Functions/Misc/run-help-ssh b/Functions/Misc/run-help-ssh
new file mode 100644
index 000000000..9c48596ff
--- /dev/null
+++ b/Functions/Misc/run-help-ssh
@@ -0,0 +1,6 @@
+if [[ $# < 2 ]]
+then
+ man ssh
+else
+ run-help $2
+fi
diff --git a/Functions/Misc/run-help-svk b/Functions/Misc/run-help-svk
deleted file mode 100644
index 782538246..000000000
--- a/Functions/Misc/run-help-svk
+++ /dev/null
@@ -1 +0,0 @@
-svk help $1 | ${=PAGER:-more}
diff --git a/Functions/Misc/run-help-svnadmin b/Functions/Misc/run-help-svnadmin
new file mode 100644
index 000000000..dbddd6396
--- /dev/null
+++ b/Functions/Misc/run-help-svnadmin
@@ -0,0 +1 @@
+svnadmin help $1 | ${=PAGER:-more}
diff --git a/Functions/Misc/zargs b/Functions/Misc/zargs
index 81916a3ac..782d6811e 100644
--- a/Functions/Misc/zargs
+++ b/Functions/Misc/zargs
@@ -39,9 +39,14 @@
#
# "Killed by a signal" is determined by the usual shell rule that $? is
# the signal number plus 128, so zargs can be fooled by a command that
-# explicitly exits with 129+. Also, zsh prior to 4.1.x returns 1 rather
-# than 127 for "command not found" so this function incorrectly returns
-# 123 in that case if used with zsh 4.0.x.
+# explicitly exits with 129+. If the command passed to zargs is a shell
+# function which uses "exit" instead of "return", zsh interprets 129+ as
+# a signal sent to the process group and may terminate zargs with that
+# status. This is avoided when running zargs -P 2 or greater.
+#
+# ZARGS_VERSION 1.5 is the last to support zsh 4.x. Also, zsh prior to
+# 4.1.x returns 1 rather than 127 for "command not found" so zargs
+# incorrectly returned 123 in that case if used with zsh 4.0.x.
#
# Because of "wait" limitations, --max-procs spawns max-procs jobs, then
# waits for all of those, then spawns another batch, etc.
@@ -71,6 +76,10 @@
# * The use of SIGUSR1 and SIGUSR2 to change the number of parallel jobs
# is not supported.
+{ # Begin "always" block to reset locally defined functions
+
+local ZARGS_VERSION="1.8"
+
# First, capture the current setopts as "sticky emulation"
if zmodload zsh/parameter
then
@@ -84,11 +93,20 @@ else
emulate $(emulate -l) -c '_zarun() { eval "$@" }'
fi
+local _zaTRAPS="$(trap)"
+_zatraps() {
+ # In children, these traps may be reset to default behavior, even
+ # if the calling shell has traps. Restore to surrounding context,
+ # but assure that if zargs itself is signaled, children will exit.
+ [[ -o interactive ]] &&
+ function TRAP{HUP,INT,QUIT,TERM} { exit $((128 + $1)) }
+ [[ -n "$_zaTRAPS" ]] && eval "$_zaTRAPS"
+ unset _zaTRAPS
+}
+
emulate -L zsh || return 1
local -a opts eof n s l P i
-local ZARGS_VERSION="1.7"
-
if zparseopts -a opts -D -- \
-eof::=eof e::=eof \
-exit x \
@@ -193,14 +211,14 @@ then (( c = $#command - 1 ))
else command=( print -r -- )
fi
-local wait bg
-local execute='
+local bg execute='
if (( $opts[(I)-(-interactive|p)] ))
then read -q "?$call?..." || continue
elif (( $opts[(I)-(-verbose|t)] ))
then print -u2 -r -- "$call"
fi
_zarun "{
+ _zatraps
\"\${call[@]}\"
} $bg"'
local ret=0 analyze='
@@ -263,12 +281,10 @@ fi
if (( P != 1 && ARGC > 1 ))
then
- # These setopts are necessary for "wait" on multiple jobs to work.
- setopt nonotify nomonitor
+ setopt nonotify # Do not report each exiting job
local -a _zajobs
local j
bg='& _zajobs+=( $! )'
- wait='wait'
analyze='
for j in $_zajobs; do
wait $j
@@ -316,4 +332,8 @@ return $ret
)
+} always {
+ builtin unfunction _zarun _zatraps
+}
+
# }
diff --git a/Functions/Misc/zcalc b/Functions/Misc/zcalc
index 480373345..6cd2822c9 100644
--- a/Functions/Misc/zcalc
+++ b/Functions/Misc/zcalc
@@ -124,8 +124,10 @@ integer _rpn_mode _matched _show_stack _i _n
integer _max_stack _push
local -a _expressions stack
+
# We use our own history file with an automatic pop on exit.
-history -ap "${ZDOTDIR:-$HOME}/.zcalc_history"
+history -ap "${ZCALC_HISTFILE:-${ZDOTDIR:-$HOME}/.zcalc_history}"
+
_forms=( '%2$g' '%.*g' '%.*f' '%.*E' '')
diff --git a/Functions/Misc/zed b/Functions/Misc/zed
index 7d0d590db..bb075512c 100644
--- a/Functions/Misc/zed
+++ b/Functions/Misc/zed
@@ -14,15 +14,17 @@ local var opts zed_file_name
integer TMOUT=0 okargs=1 fun hist bind
local -a expand
-zparseopts -D -A opts f h b x:
+zparseopts -D -A opts f h b x: || return 1
fun=$+opts[-f]
hist=$+opts[-h]
bind=$+opts[-b]
-if [[ $opts[-x] == <-> ]]; then
- expand=(-x $opts[-x])
-elif (( $+opts[-x] )); then
- print -r "Integer expected after -x: $opts[-x]" >&2
- return 1
+if (( $+opts[-x] )); then
+ if [[ $opts[-x] == <-> ]]; then
+ expand=(-x $opts[-x])
+ else
+ print -r "Integer expected after -x: $opts[-x]" >&2
+ return 1
+ fi
fi
[[ $0 = fned ]] && fun=1
diff --git a/Functions/Misc/zmv b/Functions/Misc/zmv
index 269fe5ba5..2002af5a6 100644
--- a/Functions/Misc/zmv
+++ b/Functions/Misc/zmv
@@ -236,20 +236,26 @@ if [[ $pat = (#b)(*)\((\*\*##/)\)(*) ]]; then
else
fpat=$pat
fi
-files=(${~fpat})
[[ -n $hasglobqual ]] && pat=$opat
errs=()
+() {
+ # (#qN) breaks bareglobqual -Q option, so:
+ setopt localoptions nullglob
+ files=(${~fpat})
+}
+(( ${#files} )) || errs=( "no files matched \`$fpat'" )
+
for f in $files; do
- if [[ $pat = (#b)(*)\(\*\*##/\)(*) ]]; then
+ if [[ $pat = (#b)(*)(\(\*\*##/\)|\*\*##/)(*) ]]; then
# This looks like a recursive glob. This isn't good enough,
- # because we should really enforce that $match[1] and $match[2]
+ # because we should really enforce that $match[1] and $match[3]
# don't match slashes unless they were explicitly given. But
# it's a start. It's fine for the classic case where (**/) is
# at the start of the pattern.
- pat="$match[1](*/|)$match[2]"
+ pat="$match[1](*/|)$match[3]"
fi
[[ -e $f && $f = (#b)${~pat} ]] || continue
set -- "$match[@]"
diff --git a/Functions/Misc/zslurp b/Functions/Misc/zslurp
new file mode 100644
index 000000000..84df0c948
--- /dev/null
+++ b/Functions/Misc/zslurp
@@ -0,0 +1,31 @@
+#!/bin/zsh -f
+
+# Read stdin verbatim and as efficiently as possible into $REPLY,
+# stopping without any change to $REPLY in the event of any error.
+# Benchmarked by Roman Perepelitsa in zsh-users/29472
+
+# Although this function faithfully records the input stream, later
+# references to $REPLY with the multibyte option back in effect will
+# (re-)interpret the content as multibyte characters. This may not be
+# what is desired.
+
+emulate -L zsh -o no_multibyte
+
+### Alternate formulation, faster on bigger files
+# # /dev/fd/0 is treated specially by -f so also check /dev/fd
+# if [[ -d /dev/fd && -f /dev/fd/0 ]] && zmodload zsh/mapfile
+# then
+# local +h -Ar mapfile
+# typeset -g REPLY="${mapfile[/dev/fd/0]}" && return
+# fi
+# # else fall through to read from pipe/socket
+
+zmodload zsh/system || return
+local -a content
+local -i i=0
+while true; do
+ sysread 'content[++i]' && continue
+ (( $? == 5 )) || return
+ break
+done
+typeset -g REPLY=${(j::)content}