about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--Doc/Zsh/compsys.yo28
-rw-r--r--Functions/Zle/predict-on54
2 files changed, 72 insertions, 10 deletions
diff --git a/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo
index e895868cb..a6ebeef6b 100644
--- a/Doc/Zsh/compsys.yo
+++ b/Doc/Zsh/compsys.yo
@@ -1786,4 +1786,32 @@ item(tt(incremental_list))(
 If set to a non-empty string, the matches will be listed on every
 key-press.
 )
+item(tt(predict_completer))(
+The keys starting with tt(predict_) are used by the functions in the
+tt(predict-on) file in the tt(Functions/Zle) directory of the tt(zsh)
+source distribution.
+
+A colon separated list of completer functions (like the tt(completer)
+key for normal completion) to be used when attempting completion.
+)
+item(tt(predict_cursor))(
+This describes where the cursor should be left after completion was
+attempted. If it is set to `tt(complete)' the cursor is left where
+completion put it if it is after the character typed, otherwise this
+behaves like the value `tt(key)'. If it is set to `tt(key)' the
+prediction function will try to place the cursor after the character
+which corresponds to the last character typed. This is useful if one
+uses global match specifications with patterns for partial word
+completion. If no sensible place could be found or this configuration
+key is set to any other value (or unset), the cursor is moved back to
+the original position. With global match specifications as described
+above this sometimes means that the character typed does not appear on 
+the line.
+)
+item(tt(predict_list))(
+If this is set to `tt(always)' the list of possible matches when
+completion was tried will always be shown, even if there is only one
+match. Otherwise the listing behavior is as usual, i.e. the list will
+only be shown if there are multiple matches.
+)
 enditem()
diff --git a/Functions/Zle/predict-on b/Functions/Zle/predict-on
index db129e45c..159f723d4 100644
--- a/Functions/Zle/predict-on
+++ b/Functions/Zle/predict-on
@@ -23,17 +23,28 @@
 # Note that all functions are defined when you first type the predict-on
 # key, which means typing the predict-off key before that gives a harmless
 # error message.
+#
+# This uses the configuration keys starting with `predict_'.
 
 predict-on() {
-    zle -N self-insert insert-and-predict
-    zle -N magic-space insert-and-predict
-    zle -N backward-delete-char delete-backward-and-predict
-    zle -N delete-char-or-list delete-no-predict
+  zle -N self-insert insert-and-predict
+  zle -N magic-space insert-and-predict
+  zle -N backward-delete-char delete-backward-and-predict
+  zle -N delete-char-or-list delete-no-predict
+
+  # Prediction doesn't work well with automenu set, so we unset it here
+  # and restore it in predict-off().
+  if [[ -o automenu ]]; then
+    unsetopt automenu
+    _predict_am=yes
+  fi
 }
 predict-off() {
-    zle -A .self-insert self-insert
-    zle -A .magic-space magic-space
-    zle -A .backward-delete-char backward-delete-char
+  zle -A .self-insert self-insert
+  zle -A .magic-space magic-space
+  zle -A .backward-delete-char backward-delete-char
+
+  [[ -n $_predict_am ]] && setopt automenu
 }
 insert-and-predict () {
   emulate -L zsh
@@ -50,11 +61,33 @@ insert-and-predict () {
 	RBUFFER=""
 	if [[ ${KEYS[-1]} != ' ' ]]
 	then
-	  integer curs=$CURSOR
+	  integer curs=$CURSOR pos nchar=${#LBUFFER//[^${KEYS[-1]}]}
 	  local -a +h comppostfuncs
 	  comppostfuncs=( predict-limit-list )
-	  zle complete-word
-	  CURSOR=$curs
+	  zle complete-word ${(s.:.)compconfig[predict_completer]}
+	  # Decide where to leave the cursor. The dummy loop is used to
+	  # get out of that `case'.
+	  while true; do
+	    case $compconfig[predict_cursor] in
+	    (complete)
+	      # At the place where the completion left it, if it is after
+	      # the character typed.
+	      [[ ${LBUFFER[-1]} = ${KEYS[-1]} ]] && break
+	      ;&
+	    (key)
+	      # Or maybe at the n'th occurrence of the character typed.
+	      pos=${BUFFER[(in:nchar:)${KEYS[-1]}]}
+	      if [[ pos -gt curs ]]; then
+	        CURSOR=$pos
+	        break
+	      fi
+	      ;&
+	    (*)
+	      # Or else at the previous position.
+	      CURSOR=$curs
+	    esac
+	    break
+	  done
 	fi
       fi
     fi
@@ -91,6 +124,7 @@ predict-limit-list() {
     compstate[list]=''
     compstate[force_list]=yes
   fi
+  [[ $compconfig[predict_list] = always ]] && compstate[force_list]=yes
 }
 
 # Handle zsh autoloading conventions