diff options
Diffstat (limited to 'Functions')
-rw-r--r-- | Functions/MIME/zsh-mime-handler | 2 | ||||
-rw-r--r-- | Functions/Misc/is-at-least | 8 | ||||
-rw-r--r-- | Functions/Misc/mkshadow | 11 | ||||
-rw-r--r-- | Functions/Misc/run-help | 12 | ||||
-rw-r--r-- | Functions/Misc/run-help-docker | 9 | ||||
-rw-r--r-- | Functions/Misc/run-help-ip | 2 | ||||
-rw-r--r-- | Functions/Misc/run-help-openssl | 8 | ||||
-rw-r--r-- | Functions/Misc/run-help-perf | 1 | ||||
-rw-r--r-- | Functions/Misc/run-help-podman | 9 | ||||
-rw-r--r-- | Functions/Misc/run-help-ssh | 6 | ||||
-rw-r--r-- | Functions/Misc/run-help-svk | 1 | ||||
-rw-r--r-- | Functions/Misc/run-help-svnadmin | 1 | ||||
-rw-r--r-- | Functions/Misc/zmv | 8 | ||||
-rw-r--r-- | Functions/Misc/zslurp | 31 | ||||
-rw-r--r-- | Functions/Prompts/prompt_sprint2_setup | 128 | ||||
-rw-r--r-- | Functions/Zle/edit-command-line | 23 | ||||
-rw-r--r-- | Functions/Zle/incarg | 322 |
17 files changed, 529 insertions, 53 deletions
diff --git a/Functions/MIME/zsh-mime-handler b/Functions/MIME/zsh-mime-handler index 288a0796d..2feacf063 100644 --- a/Functions/MIME/zsh-mime-handler +++ b/Functions/MIME/zsh-mime-handler @@ -193,7 +193,7 @@ if ! zsh-mime-contexts -s $suffix flags flags; then # Same again for flags. s=$suffix while true; do - flags="${zsh_mime_flags[$suffix]}" + flags="${zsh_mime_flags[$s]}" if [[ -n $flags ]]; then break fi 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/zmv b/Functions/Misc/zmv index 269fe5ba5..5c03e9ea1 100644 --- a/Functions/Misc/zmv +++ b/Functions/Misc/zmv @@ -236,12 +236,18 @@ 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 # This looks like a recursive glob. This isn't good enough, 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} diff --git a/Functions/Prompts/prompt_sprint2_setup b/Functions/Prompts/prompt_sprint2_setup new file mode 100644 index 000000000..0eaea4473 --- /dev/null +++ b/Functions/Prompts/prompt_sprint2_setup @@ -0,0 +1,128 @@ +# Created by Sebastian Gniazdowski + +prompt_sprint2_help () { + cat <<EOF +This prompt is color themable. You can invoke it in following way: + +prompt sprint2 <time/line color> <braces clr.> <text clr.> <at/colon clr.> <prompt clr.> + +You can provide only N first arguments, N=1..5. + +The default colors are: 39 green blue green yellow +EOF +} + +prompt_sprint2_setup () { + local col_time_line=${1:-'39'} + local col_parens=${2:-'green'} + local col_text=${3:-'blue'} + local col_at_colon=${4:-'green'} + local col_prompt=${5:-'yellow'} + + autoload -Uz vcs_info + + typeset -gA _psvar + + local cl_time_line="%B%F{$col_time_line}" + local cl_text="%b%F{$col_text}" + local cl_parens="%B%F{$col_parens}" + local cl_at_colon="%b%F{$col_at_colon}" + local cl_prompt="%B%F{$col_prompt}" + local cl_rst="%b%f" + + local _lpar="${cl_parens}[" + local _rpar="${cl_parens}]" + + local _time="$cl_time_line%D{%H:%M}" + _psvar[user]='%n' + local _user="$cl_text"'${_psvar[user]}' + _psvar[at]="@" + _psvar[host]='%m' + local _at="$cl_at_colon"'${_psvar[at]}'"$cl_text"'${_psvar[host]}' + _psvar[colon]=':' + local _path="$cl_at_colon"'${_psvar[colon]}'"$cl_text%28<*<%/%<<" + local _line="$cl_time_line%i" + local _prompt="$cl_prompt# " + + # You can instantly shorten the prompt by setting PSSHORT=1 + if [[ "$COLUMNS" -le 94 || "$PSSHORT" = "1" ]]; then + _psvar=() + else + _psvar[user]='%n' + _psvar[at]="@" + _psvar[host]='%m' + _psvar[colon]=':' + fi + + PS1="$_time$_lpar$_user$_at$_path$_rpar$_line$_prompt$cl_rst" + + PS2="$cl_parens> $cl_rst" + + RPS1='${vcs_info_msg_0_}' + prompt_opts=(cr subst percent) + + zstyle ':vcs_info:*' enable git svn darcs bzr hg + zstyle ':vcs_info:*' stagedstr "%F{green}●$cl_rst" + zstyle ':vcs_info:*' unstagedstr "%F{yellow}●$cl_rst" + zstyle ':vcs_info:*' check-for-changes true + zstyle ':vcs_info:*' formats '(%s)-[%b%u%c]-' + + add-zsh-hook precmd prompt_sprint2_precmd +} + +prompt_sprint2_precmd () { + setopt localoptions noxtrace noksharrays + + # You can instantly shorten the prompt by setting PSSHORT=1 + if [[ "$COLUMNS" -le 94 || "$PSSHORT" = "1" ]]; then + _psvar=() + else + _psvar[user]='%n' + _psvar[at]="@" + _psvar[host]='%m' + _psvar[colon]=':' + fi + + local -a changed_files + changed_files=( ) + git diff --quiet 2>/dev/null || changed_files=( ${(f)"$( git diff --name-only 2>/dev/null )"} ) + changed_files=( $^changed_files(N) ) + if [[ "${#changed_files}" -gt 0 ]]; then + local basedir + basedir="$(git rev-parse --show-toplevel)/" + changed_files=( ${(f)"$( find "${basedir}${^changed_files[@]}" -mtime +2 )"} ) + fi + + if [[ "${#changed_files}" -eq 0 ]]; then + zstyle ':vcs_info:*' formats ' (%s)-[%b%u%c]' + else + zstyle ':vcs_info:*' formats ' (%s)-[%b%u%B%F{yellow}-OLD-%c%%b%f]' + fi + + vcs_info +} + +prompt_sprint2_preview () { + local -a colors_time_line colors_text + colors_time_line=(red yellow green blue magenta cyan) + colors_text=(red yellow green blue magenta cyan) + + local ctime_line ctext i j + + if (( ! $#* )); then + for (( i = 1; i <= ${#colors_time_line}; i++ )); do + ctime_line="${colors_time_line[$i]}" + for (( j = 1; j <= ${#colors_text}; j++ )); do + ctext="${colors_text[$j]}" + prompt_preview_theme sprint2 "$ctime_line" "red" "$ctext" + (( i != ${#colors_time_line} || j != ${#colors_text} )) && print + done + done + else + prompt_preview_theme sprint2 "$@" + fi +} + +prompt_sprint2_setup "$@" + +# vim:ft=zsh diff --git a/Functions/Zle/edit-command-line b/Functions/Zle/edit-command-line index 5f7ea321f..d4b405eaf 100644 --- a/Functions/Zle/edit-command-line +++ b/Functions/Zle/edit-command-line @@ -11,15 +11,30 @@ local left right prebuffer buffer=$BUFFER lbuffer=$LBUFFER local TMPSUFFIX=.zsh # set up parameters depending on which context we are called from, # see below comment for more details -if (( REGION_ACTIVE )); then +if (( REGION_ACTIVE == 1 )); then if (( CURSOR < MARK )); then left=$CURSOR right=$MARK - lbuffer= else left=$MARK right=$CURSOR - lbuffer[right-left,-1]= fi - (( left++ )) + lbuffer=$lbuffer[++left,-1] + buffer=$BUFFER[left,++right] +elif (( REGION_ACTIVE == 2 )); then + local nl=$'\n' + if (( CURSOR < MARK )); then + left=${${BUFFER[1,CURSOR]}[(I)$nl]} + right=${${BUFFER[MARK+1,-1]}[(i)$nl]} + (( right += MARK )) + else + left=${${BUFFER[1,MARK]}[(I)$nl]} + right=${${BUFFER[CURSOR+1,-1]}[(i)$nl]} + (( right += CURSOR )) + fi + lbuffer=$lbuffer[++left,-1] + if [[ $BUFFER[right] = $nl ]]; then + # Keep the newline because "$(<$1)" below trims it + (( --right )) + fi buffer=$BUFFER[left,right] elif (( ! ZLE_RECURSIVE )); then prebuffer=$PREBUFFER diff --git a/Functions/Zle/incarg b/Functions/Zle/incarg index cff0cfe4c..0cfaf9ff5 100644 --- a/Functions/Zle/incarg +++ b/Functions/Zle/incarg @@ -1,43 +1,303 @@ -# Shell function to increment an integer either under the cursor or just -# to the left of it. Use +emulate -L zsh + +# A ZLE widget to increment an integer. +# +# In addition to decimals, it can handle hexadecimals prefixed with "0x", +# binaries with "0b", and octals with "0o". +# +# By default, the target integer will be incremented by one. With a numeric +# argument, the integer is incremented by the amount of the argument. The shell +# parameter "incarg" may be set to change the default increment to something +# other than one. +# +# The behavior of this widget changes depending on how it is named. +# +# - incarg / decarg +# +# incarg will increment an integer either under the cursor or just to the left +# of it. decarg, on the other hand, will decrement it. +# +# For example, +# +# echo 41 +# ^^^ cursor anywhere here +# +# with incarg gives +# +# echo 42 +# ^ cursor will move here +# +# - sync-incarg / sync-decarg +# +# The sync- variant is used for creating a sequence of numbers on split +# terminals with synchronized key input. The first pane won't be incremented +# at all, but each pane after that will have the number incremented once more +# than the previous pane. +# +# Currently supports tmux and iTerm2. +# +# - vim-incarg / vim-decarg +# +# This behaves like Vim's CTRL-A / CTRL-X. It moves the cursor to the nearest +# number after the cursor and increments or decrements it. +# +# - vim-backward-incarg / vim-backward-decarg +# +# This behaves like vim-incarg & vim-decarg, but it searches backwards for a +# number. +# +# - vim-sync-incarg / vim-sync-decarg +# +# This combines the behavior of the vim- and sync- variants. It's inspired by +# Vim's g_CTRL-A / g_CTRL-X. +# +# - vim-backward-sync-incarg / vim-backward-sync-decarg +# +# This combines the behavior of the vim-backward- and sync- variants. +# +# Example Usage: +# # autoload -Uz incarg -# zle -N incarg -# bindkey "..." incarg -# to define it. For example, -# echo 41 -# ^^^ cursor anywhere here -# with incarg gives -# echo 42 -# with the cursor in the same place. -# -# A numeric argument gives a number other than 1 to add (may be negative). -# If you're going to do it a lot with one particular number, you can set -# the parameter incarg to that number (a numeric argument still takes -# precedence). +# for widget in vim-{,sync-}{inc,dec}arg; do +# zle -N "$widget" incarg +# done +# bindkey -a \ +# '^A' vim-incarg \ +# '^X' vim-decarg \ +# 'g^A' vim-sync-incarg \ +# 'g^X' vim-sync-decarg -emulate -L zsh -setopt extendedglob +zle -f vichange -local rrest lrest num +setopt localoptions extended_glob +local match mbegin mend MATCH MBEGIN MEND i -rrest=${RBUFFER##[0-9]#} -if [[ $RBUFFER = [0-9]* ]]; then - if [[ -z $rrest ]]; then - num=$RBUFFER - else - num=${RBUFFER[1,-$#rrest-1]} +[[ -z "$BUFFER" ]] && return 1 + +# find the number and determine the base +integer pos=$(( CURSOR + 1 )) base=0 + +# avoid miscalculating positions when cursor is at the end of the line +while (( pos > 0 )) && [[ "$BUFFER[pos]" == '' ]]; do + (( pos-- )) +done + +# check for a prefix (e.g., 0x) before the cursor +for (( i = 0; i < 2; i++ )); do + case "$BUFFER[1,pos]" in + *0[xX][0-9a-fA-F]##) base=16 ;; + *0[oO][0-7]##) base=8 ;; + *0[bB][01]##) base=2 ;; + *[1-9]) base=10 ;; + *0) ;; # there may be a prefix right after the cursor + *) + # the non-Vim variant looks right before the cursor too, but not after it + if [[ "$WIDGET" != vi* ]]; then + if (( i == 0 )); then + (( pos-- )) + continue + else + return 1 + fi + fi + ;; + esac + + break +done + +# check for a prefix on the cursor +if (( base == 0 && pos < $#BUFFER )); then + case "$BUFFER[1,pos+1]" in + *0[xX][0-9a-fA-F]) base=16; (( pos++ )) ;; + *0[oO][0-7]) base=8; (( pos++ )) ;; + *0[bB][01]) base=2; (( pos++ )) ;; + esac +fi + +if (( base == 0 )); then + if [[ "$WIDGET" == vi* ]]; then + if [[ "$WIDGET" == *backward-* ]]; then + # search backwards for a number + while true; do + case "$BUFFER[1,pos]" in + *0[xX][0-9a-fA-F]##) base=16 ;; + *0[oO][0-7]##) base=8 ;; + *0[bB][01]##) base=2 ;; + *[0-9]) base=10 ;; + *-) + case "$BUFFER[pos,-1]" in + -0[xX][0-9a-fA-F]*) ;; + -0[oO][0-7]*) ;; + -0[bB][01]*) ;; + -[0-9]*) base=10 ;; + esac + ;; + esac + (( base != 0 )) && break + + (( pos-- )) + (( pos <= 0 )) && return 1 + done + else + # jump to the nearest number after the cursor + while [[ "$BUFFER[pos]" == [^0-9] ]]; do + (( pos++ )) + (( pos > $#BUFFER )) && return 1 + done + fi + fi + + # check for a prefix right after the cursor and jump right after it, if any + if (( pos <= 1 )) || [[ "$BUFFER[pos-1]" == [^0-9] ]]; then + case "$BUFFER[pos,-1]" in + 0[xX][0-9a-fA-F]*) base=16; (( pos += 2 )) ;; + 0[oO][0-7]*) base=8; (( pos += 2 )) ;; + 0[bB][01]*) base=2; (( pos += 2 )) ;; + esac fi fi -lrest=${LBUFFER%%[0-9]#} -if [[ $LBUFFER = *[0-9] ]]; then - if [[ -z $lrest ]]; then - num="$LBUFFER$num" +if (( base == 0 )); then + base=10 +fi + +# find the start of the number +integer first="$pos" +case "$base" in + 10) + while [[ "$BUFFER[first-1]" == [0-9] ]]; do + (( first-- )) + done + if [[ $BUFFER[first-1] = - ]]; then + (( first-- )) + fi + ;; + 2) + while [[ "$BUFFER[first-1]" == [01] ]]; do + (( first-- )) + done + ;; + 8) + while [[ "$BUFFER[first-1]" == [0-7] ]]; do + (( first-- )) + done + ;; + 16) + while [[ "$BUFFER[first-1]" == [0-9a-fA-F] ]]; do + (( first-- )) + done + ;; +esac + +# find the end of the number +integer last="$pos" +case "$base" in + 10) + while [[ "$BUFFER[last+1]" == [0-9] ]]; do + (( last++ )) + done + ;; + 2) + while [[ "$BUFFER[last+1]" == [01] ]]; do + (( last++ )) + done + ;; + 8) + while [[ "$BUFFER[last+1]" == [0-7] ]]; do + (( last++ )) + done + ;; + 16) + while [[ "$BUFFER[last+1]" == [0-9a-fA-F] ]]; do + (( last++ )) + done + ;; +esac + +# calculate the number of digits +integer ndigits=0 +case "$BUFFER[first,first+1]" in + 0*|-0) ndigits=$(( last - first + 1 )) ;; +esac + +# determine the amount to increment +integer delta=${NUMERIC:-${incarg:-1}} +if [[ "$WIDGET" = *decarg ]]; then + (( delta = -delta )) +fi +if [[ "$WIDGET" = *sync-* ]]; then + integer pane_index=0 + if [[ -n "$TMUX_PANE" ]]; then + pane_index="$(tmux display-message -pt "$TMUX_PANE" '#{pane_index}')" + elif [[ "$ITERM_SESSION_ID" =~ '^w[0-9]+t[0-9]+p([0-9]+)' ]]; then + pane_index="$match[1]" else - num="${LBUFFER[$#lrest+1,-1]}$num" + zle -M "[$WIDGET] unsupported terminal" + return 1 fi + (( delta *= pane_index )) fi -[[ -n $num ]] && (( num += ${NUMERIC:-${incarg:-1}} )) +local old="$BUFFER[first,last]" +integer oldlen=$#BUFFER +integer oldnum="$base#$old" 2> /dev/null + +# -00 should increment to 01 instead of 001 +if [[ "$BUFFER[first]" == '-' ]] && (( oldnum == 0 )); then + (( ndigits-- )) +fi + +local fmt1 fmt2 +case "$base" in + 10) fmt1=d; fmt2='#10' ;; + 2) fmt1=s; fmt2='##2' ;; + 8) fmt1=s; fmt2='##8' ;; + 16) fmt1="$BUFFER[first-1]"; fmt2='#16' ;; +esac + +local raw_result padded +# $(( )) outputs an error message to stderr when integer truncation occurs +printf -v raw_result "%0$ndigits$fmt1" $(( [$fmt2] "$base#$old" + delta )) 2> /dev/null +padded="${raw_result// /0}" +integer newnum="$base#$padded" 2> /dev/null + +# try to detect integer truncation +if (( base != 10 && newnum < 0 + || delta > 0 && newnum < oldnum + || delta < 0 && newnum > oldnum )); then + zle -M "[$WIDGET] The resulting number is either too big or too small." + return 1 +fi + +# adjust the number of leading zeros if the sign of the integer changed +local new +if (( base == 10 && ndigits == $#padded )); then + if (( oldnum < 0 && newnum >= 0 )); then + new="${padded#0}" + elif (( oldnum >= 0 && newnum < 0 )); then + new="-0${padded#-}" + fi +fi +if [[ -z "$new" ]]; then + new="$padded" +fi + +if zstyle -t ":zle:$WIDGET" debug; then + zle -M "[$WIDGET] base: $base delta: $delta old: '$old' new: '$new'" +fi + +if (( 0 < first && first <= last && last <= $#BUFFER )); then + BUFFER[first,last]="$new" +else + zle -M "[$WIDGET] The detected location of the integer was invalid. [location=BUFFER[$first,$last]]" + return 1 +fi + +integer offset=0 +if [[ "$WIDGET" == vi* ]]; then + offset=-1 +fi +(( CURSOR = last + $#BUFFER - oldlen + offset )) -BUFFER="$lrest$num$rrest" +return 0 |