about summary refs log tree commit diff
path: root/Completion/Zsh/Command/_fc
diff options
context:
space:
mode:
Diffstat (limited to 'Completion/Zsh/Command/_fc')
-rw-r--r--Completion/Zsh/Command/_fc86
1 files changed, 68 insertions, 18 deletions
diff --git a/Completion/Zsh/Command/_fc b/Completion/Zsh/Command/_fc
index 2af5980c2..133145670 100644
--- a/Completion/Zsh/Command/_fc
+++ b/Completion/Zsh/Command/_fc
@@ -1,21 +1,47 @@
 #compdef fc history r
 
+local curcontext="$curcontext" state state_descr line ret=1
+local list events num cmd
+typeset -A opt_args
 local fc_common fc_hist fc_r
 
+# hide any replacements from _arguments
+local cur=${(M)#words[1,CURRENT-1]:#*=*}
+words=( "${(@)words[1,CURRENT-1]:#*=*}" "${(@)words[CURRENT,-1]}" )
+(( CURRENT -= cur ))
+
 fc_common=(
+  -s -S
   '(-A -R -W -I -p -P)-r[reverse order of the commands]'
   '(-A -R -W -I -e -p -P)-n[suppress line numbers]'
-  '(-A -R -W -I -p -P)*::commands:_command_names -e' )
+)
+
+if [[ -n ${words[(r)-[pa](|[ap])]} ]]; then
+  fc_common+=(
+    ':history file:_files'
+    ':history size:'
+    ':saved history size:'
+  )
+  [[ -n ${words[(r)-(|a)p(|a)]} ]] || fc_common+='!-a:option:(-p)'
+elif [[ -n ${words[(r)-*[ARWI]*]} ]]; then
+  fc_common+=( ':history file:_files' )
+else
+  fc_common=( -C "$fc_common[@]"
+    '(-)1:first event:->events' '2:last event:->events'
+  )
+fi
 
 fc_hist=(
-  '(-A -R -W -I -p -p)-m[treat first argument as a pattern]'
-  '(-A -R -W -I -e -f -E -i -t -p -P)-d[print time-stamps]'
-  '(-A -R -W -I -e -d -E -i -t -p -P)-f[mm/dd/yyyy format time-stamps]'
-  '(-A -R -W -I -e -d -f -i -t -p -P)-E[dd.mm.yyyy format time-stamps]'
-  '(-A -R -W -I -e -d -f -E -t -p -P)-i[yyyy-mm-dd format time-stamps]'
-  '(-A -R -W -I -e -d -f -E -i -p -P)-t[print time-stamps in specified format]:date format'
-  '(-A -R -W -I -e -p -P)-D[print elapsed times]'
-  '(- *)-p[push current history to stack]:history file:_files:history size: :saved history size'
+  '(-A -R -W -I -a -p -P 2)-m[treat argument as a pattern]'
+  '(-A -R -W -I -e -f -E -i -t -a -p -P)-d[print time-stamps]'
+  '(-A -R -W -I -e -d -E -i -t -a -p -P)-f[mm/dd/yyyy format time-stamps]'
+  '(-A -R -W -I -e -d -f -i -t -a -p -P)-E[dd.mm.yyyy format time-stamps]'
+  '(-A -R -W -I -e -d -f -E -t -a -p -P)-i[yyyy-mm-dd format time-stamps]'
+  '(-A -R -W -I -e -d -f -E -i -a -p -P)-t[print time-stamps in specified format]:date format'
+  '(-A -R -W -I -e -a -p -P)-D[print elapsed times]'
+
+  '(-A -R -W -I -e -d -f -i -l -m -n -r -D -E -t -P)-a[with -p, automatically pop history on function return]'
+  '(-A -R -W -I -e -d -f -i -l -m -n -r -D -E -t -P)-p[push current history to stack]'
   '(- *)-P[pop history from stack]'
 )
 
@@ -23,18 +49,42 @@ fc_r='(-A -R -W -I -e)-l[list resulting commands on stdout]'
 
 case $service in
   history)
-    _arguments -s -S "$fc_common[@]" "$fc_hist[@]" && return 0
+    _arguments "$fc_common[@]" "$fc_hist[@] && ret=0"
   ;;
   r)
-    _arguments -s -S "$fc_common[@]" "$fc_r" && return 0
+    _arguments "$fc_common[@]" "$fc_r" && ret=0
   ;;
   *)
-  _arguments -s -S \
-    '(-A -R -W -I -l -n -d -f -E -i -D -p -P)-e[specify editor to invoke]:editor to invoke:_command_names -e' \
-    '(-l -m -e -r -n -d -f -E -i -D -A -W -p -P *)-R[read history from file]:history file:_files' \
-    '(-l -m -e -r -n -d -f -E -i -D -R -W -p -P *)-A[append history to file]:history file:_files' \
-    '(-l -m -e -r -n -d -f -E -i -D -R -A -p -P *)-W[write history to file]:history file:_files' \
-    '(-l -m -e -r -n -d -f -E -i -D -A -W -p -P *)-I[read/write new events only]:history file:_files' \
-    "$fc_common[@]" "$fc_hist[@]" "$fc_r" && return 0
+    _arguments "$fc_common[@]" "$fc_hist[@]" "$fc_r" \
+      '(-A -R -W -I -a -l -n -d -f -E -i -r -t -D -p -P)-e+[specify editor to invoke]:editor to invoke:_command_names -e' \
+      '(-a -l -m -e -r -n -d -f -t -E -i -R -D -A -W -p -P *)-'{\
+'R[read history from file]',\
+'A[append history to file]',\
+'W[write history to file]',\
+'I[read/write new events only]'} && ret=0
   ;;
 esac
+
+if [[ -n $state ]]; then
+  if [[ -z ${line:#*=*} ]] && compset -P '*='; then
+    _message -e replacements 'replacement'
+  elif [[ -prefix [0-9] ]]; then
+    events=( ${(ps.\0.)"$(printf '%s:%s\0' ${(kv)history})"} )
+    zformat -a list " -- " "$events[@]"
+    _wanted -2V events expl "$state_descr" compadd -M "B:0=" -ld list - \
+        "${events[@]%%:*}"
+  elif [[ -prefix - ]]; then
+    for num cmd in ${(kv)history}; do
+      (( num=num - HISTNO ))
+      events+=( $num:$cmd )
+    done
+    zformat -a list " -- " "$events[@]"
+    _wanted -2V events expl "$state_descr" compadd -ld list - \
+        "${events[@]%%:*}"
+  else
+    _wanted events expl "$state_descr" compadd -S '' - \
+        ${${history%%[=[:IFS:]]*}:#[0-9-]*} || _guard "[0-9]#" event
+  fi
+fi && ret=0
+
+return ret