about summary refs log tree commit diff
path: root/Completion/Core/_main_complete
diff options
context:
space:
mode:
Diffstat (limited to 'Completion/Core/_main_complete')
-rw-r--r--Completion/Core/_main_complete118
1 files changed, 75 insertions, 43 deletions
diff --git a/Completion/Core/_main_complete b/Completion/Core/_main_complete
index 34c5a3d3c..3571b712c 100644
--- a/Completion/Core/_main_complete
+++ b/Completion/Core/_main_complete
@@ -3,36 +3,47 @@
 # The main loop of the completion code. This is what is called when 
 # completion is attempted from the command line.
 #
-# This code will automatically try to correct the string on the
-# line based on the strings generated for the context if the
-# parameter `COMPCORRECT' is set and normal completion didn't yield
-# any matches. These corrected strings will be shown in a list and
-# one can cycle through them as in a menucompletion. To use this 
-# feature, `COMPCORRECT' should be set to a number, specifying the
+# This code will automatically try to correct the string on the line
+# based on the strings generated for the context if
+# `compconfig[correct]' is set and normal completion didn't yield any
+# matches. These corrected strings will be shown in a list and one can
+#cycle through them as in a menucompletion. To use this feature,
+#`compconfig[correct]' should be set to a number, specifying the 
 # maximum number of errors that should be accepted. If the string also
 # contains a `n' or `N', the code will use the numeric argument as the
 # maximum number of errors if a numeric argument was given. If no
 # numeric argument was given, the number from the value of
-# `COMPCORRECT' will be used. E.g. with `COMPCORRECT=2n' two errors
-# will be accepted, but if the user gives another number with the
-# numeric argument, this will be prefered. Also, with `COMPCORRECT=0n',
-# normally no automatic correction will be tried, but if a numeric
-# argument is given, automatic correction will be used. Once the
-# number of errors to accept is determined, the code will repeatedly
-# try to generate matches by allowing one error, two errors, and so
-# on.
-# If the parameter `CCORIG' is set (independent of the value), the
-# line will first be left unchanged and consecutive TABs cycle through 
-# the list.
-# When using automatic correction, one can also set the parameter
-# `CCPROMPT' to a string that will be shown when multiple
-# correction results are displayed and the code starts cycling
-# through them (this string is used with the `-X' option and thus may
-# contain the control sequences `%n', `%B',...).
-
-local comp name _comp_correct comax
-
-setopt localoptions nullglob rcexpandparam globdots
+# `compconfig[correct]' will be used. E.g. with `compconfig[correct]=2n'
+# two errors will be accepted, but if the user gives another number
+# with the numeric argument, this will be prefered. Also, with
+# `compconfig[correct]=0n',normally no automatic correction will be
+# tried, but if a numeric argument is given, automatic correction will
+# be used. Once the number of errors to accept is determined, the code
+# will repeatedly try to generate matches by allowing one error, two
+# errors, and so on. Independent of the number of errors the user
+# wants to accept, the code will allow only fewer errors than there
+# are characters in the string from the line.
+# The value of `compconfig[correct_orig]' is used to determine if the
+# original string should be included in the list (and thus be
+# presented to the user when cycling through the corrections). If it
+# is set to any non-empty value, the original string will be
+# offered. If it contains the sub-string `last', the original string
+# will apear as the last string when cycling through the corrections,
+# otherwise it will appear as the first one (so that the command line
+# does not change immediatly). Also, if the value of
+# `compconfig[correct_orig]' contains the sub-string `always', the
+# original string will always be included, whereas normally it is
+# included only if more than one possible correction was generated.
+# Finally, `compconfig[correct_prompt]' may be set to a string that
+# should be printed before the list of corrected strings when cycling
+# through them. This string may contain the control sequences `%n',
+# `%B', etc. known from the `-X' option of `compctl'. Also, the
+# sequence `%e' will be replaced by the number of errors accepted to
+# generate the corrected strings.
+
+local comp name _comp_correct _correct_prompt comax
+
+setopt localoptions nullglob rcexpandparam
 unsetopt markdirs globsubst shwordsplit nounset ksharrays
 
 # Special completion contexts after `~' and `='.
@@ -91,25 +102,30 @@ while true; do
 
   # Use automatic correction?
 
-  if (( $+COMPCORRECT )); then
+  if (( $+compconfig[correct] )); then
 
     # Do we have matches?
     if (( compstate[nmatches] )); then
 
       # Yes, were they added using correction? (More than one match?)
 
-      if [[ -n "$_comp_correct" && compstate[nmatches] -gt 1 ]]; then
+      if [[ -n "$_comp_correct" &&
+            ( "$compconfig[correct_orig]" = *always* ||
+	      compstate[nmatches] -gt 1 ) ]]; then
 
-        # If we got more than one string from correction, we add the 
-	# original string as a possible match, let it not be shown in
-	# the list, and probably display the `CCPROMPT'.
-
-        (( $+CCORIG )) && builtin compadd -nQ - "$PREFIX$SUFFIX"
+        if [[ "$compconfig[correct_orig]" = *last* ]]; then
+	  builtin compadd -V _correct_orig -nQ - "$PREFIX$SUFFIX"
+        elif [[ -n "$compconfig[correct_orig]" ]]; then
+	  builtin compadd -nQ - "$PREFIX$SUFFIX"
+	fi
 
 	# If you always want to see the list of possible corrections,
 	# set `compstate[list]=list' here.
+
+	compstate[force_list]=list
       fi
       # Since we have matches, we don't want to try again.
+
       break
     fi
 
@@ -117,25 +133,33 @@ while true; do
 
     if [[ -n "$_comp_correct" ]]; then
 
-      # Yes, give up if we reached the maximum number of tries,
-      # otherwise increment our counter.
+      # Yes, give up if we reached the maximum number of tries or the
+      # string from the line is too short, otherwise increment our 
+      # counter.
 
-      [[ _comp_correct -eq comax ]] && break
+      [[ _comp_correct -eq comax ||
+         "${#${:-$PREFIX$SUFFIX}}" -le _comp_correct+1 ]] && break
       (( _comp_correct++ ))
 
+      _correct_prompt="${compconfig[correct_prompt]//\%e/$_comp_correct}"
+
     elif [[ compstate[matcher] -eq compstate[total_matchers] ]]; then
 
+      # We don't try correction if the string is too short.
+
+      [[ "${#${:-$PREFIX$SUFFIX}}" -le 1 ]] && return
+
       # No matches and no correction tried yet, but we just tried the
       # last global match specification, so let's see if we should use
       # correction now. First, get the maximum number of errors.
 
-      if [[ "$COMPCORRECT" = *[nN]* && NUMERIC -ne 1 ]]; then
+      if [[ "$compconfig[correct]" = *[nN]* && NUMERIC -ne 1 ]]; then
         # Prefer the numeric argument if that has a sensible value.
         comax="$NUMERIC"
       else
-        comax="${COMPCORRECT//[^0-9]}"
+        comax="${compconfig[correct]//[^0-9]}"
       fi
-      # If the number of errors to accept is to small, give up.
+      # If the number of errors to accept is too small, give up.
 
       [[ "$comax" -lt 1 ]] && break
 
@@ -145,25 +169,31 @@ while true; do
       # ignored prefix).
 
       compadd() {
+        [[ "$*" != *-([a-zA-Z/]#|)U* &&
+           "${#${:-$PREFIX$SUFFIX}}" -le _comp_correct ]] && return
+
         if [[ "$PREFIX" = \~*/* ]]; then
 	  PREFIX="${PREFIX%%/*}/(#a${_comp_correct})${PREFIX#*/}"
 	else
           PREFIX="(#a${_comp_correct})$PREFIX"
 	fi
-	if (( $+CCPROMPT )); then
-	  builtin compadd -X "$CCPROMPT" -J _correct "$@"
+	if [[ -n "$_correct_prompt" ]]; then
+	  builtin compadd -X "$_correct_prompt" -J _correct "$@"
 	else
 	  builtin compadd -J _correct "$@"
 	fi
       }
       compgen() {
+        [[ "$*" != *-([a-zA-Z/]#|)U* &&
+           "${#${:-$PREFIX$SUFFIX}}" -le _comp_correct ]] && return
+
         if [[ "$PREFIX" = \~*/* ]]; then
 	  PREFIX="${PREFIX%%/*}/(#a${_comp_correct})${PREFIX#*/}"
 	else
           PREFIX="(#a${_comp_correct})$PREFIX"
 	fi
-	if (( $+CCPROMPT )); then
-	  builtin compgen "$@" -X "$CCPROMPT" -J _correct
+	if [[ -n "$_correct_prompt" ]]; then
+	  builtin compgen "$@" -X "$_correct_prompt" -J _correct
 	else
 	  builtin compgen "$@" -J _correct
 	fi
@@ -179,6 +209,8 @@ while true; do
       _comp_correct=1
       compstate[matcher]=-1
 
+      _correct_prompt="${compconfig[correct_prompt]//\%e/$_comp_correct}"
+
       # We also need to set `extendedglob' and to make the completion
       # code behave as if globcomplete were set.