Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: _history_complete_word and history word begin with quote

On Apr 3,  2:16pm, Han Pingtian wrote:
} Looks like if a history word begins with quote, _history_complete_word
} cannot complete it?

This is a more generic issue which just happens to be more easily seen
with _history_complete_word.  The gist of it is that, if the word has
an quote mark to the left of the cursor, then completion does not treat
the quote as part of the word to be matched, but instead tries to append
new text to the quoted string.

This applies everywhere, not just for history words; e.g. if you type an
open quote and then press TAB to complete file names, those names will
appear after the opening quote and a closing quote will be offered if
the file is not a directory.  This is meant to allow the user to choose
how a file name that contains spaces or metacharacters will be quoted in
the final command line, rather than forcing a quoting style.  (It would
be a whole lot easier for the internals if we forced the quoting, and
completion did that at first years ago, but too many users griped.)

There is presently no way to turn this off or otherwise force the quote
to become part of the match target.  $compstate[quote] is read-only.

The closest we could get is to run "compset -q" when then produces
completions like this:

torch% echo "\"foo bar\"
world        hello        print        \"foo bar\"  print  

Or without the quote on the command line:

torch% echo 
world         hello         print         \"foo\ bar\"  print       

We could instead peel the quotes off the history words before offering
them as completions (diff below) but this might have unwanted side-
effects elsewhere.

diff --git a/Completion/Base/Completer/_history b/Completion/Base/Completer/_history
index 63878ac..cd69ca1 100644
--- a/Completion/Base/Completer/_history
+++ b/Completion/Base/Completer/_history
@@ -51,9 +51,14 @@ ISUFFIX=
 # We skip the first element of historywords so the current word doesn't
 # interfere with the completion
+local -a hslice
 while [[ $compstate[nmatches] -eq 0 && beg -lt max ]]; do
+  if [[ -n $compstate[quote] ]]
+  then hslice=( ${(Q)historywords[beg,beg+slice]} )
+  else hslice=( ${historywords[beg,beg+slice]} )
+  fi
   _wanted "$opt" history-words expl 'history word' \
-      compadd -Q -a 'historywords[beg,beg+slice]'
+      compadd -Q -a hslice
   (( beg+=slice ))

Messages sorted by: Reverse Date, Date, Thread, Author