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

PATCH: _list completer



Peter Stephenson wrote:

> Actually, I tend to type the first part and then use ^D until I have a
> small enough list. Rather than type, get partial completion, look at list,
> type, get partial completion... I can just type a few letters and go BAM
> BAM BAM BAM with the tab key, which is very satsifying.  Well, relatively
> speaking.

The _list completer below is intended to be put at the front of 
`compconf completer=...'. It has the effect of delaying the insertion
of matches until completion was attempted twice on the same word.
After the first attempt only the list of matches is shown.

Of course, there are also some configuration keys.

Maybe I should have mentioned that his may be more interesting for
those using menucompletion, but someone using menucompletion please
decide on this and add the comment if they think this is right.

The attentive reader will see that the test to find out if the line
did not change isn't perfect. To make this better we would need
something like a special zle parameter `HISTNO' giving the current
history number. And while I'm at it: how about a parameter giving the
contents of the whole buffer, e.g. as an array containing the lines?
(Both of these readonly, I think, at least for now.) Obviously the
best name for this would be `buffer', if only `BUFFER' weren't taken
(and this is a misnomer, anyway, considering the names of the builtin
zle widgets -- and an ugly one, I think).

Apart from all this: is this of any use for someone?

Bye
 Sven

diff -u -r oc/Core/_list Completion/Core/_list
--- oc/Core/_list	Wed Mar 24 11:28:12 1999
+++ Completion/Core/_list	Wed Mar 24 11:39:08 1999
@@ -0,0 +1,61 @@
+#autoload
+
+# This completer function makes the other completer functions used
+# insert possible completions only after once the list has been
+# shown.
+#
+# Configuration keys:
+#
+#  list_condition
+#    If this key is unset or set to the empty string, this completer
+#    will delay the insertion of matches unconditionally. However,
+#    if this value is set, it should be set to an expression usable
+#    inside a $[...] arithmetical expression. In this case, delaying
+#    will be done if the expression evaluates to `1'.
+#    For example, with
+#
+#      compconf list_condition='NUMERIC != 1'
+#
+#    delaying will be done only if given an explicit numeric argument
+#    other than `1'.
+#
+#  list_word
+#    To find out if only listing should be done, the code normally
+#    compares the contents of the line with the contents the line
+#    had at the time of the last invocation. If this key is set to
+#    an non-empty string comparison is done using only the current
+#    word. So if it is set, attempting completion on a word equal
+#    to the one completion was called on the last time will not
+#    delay the generation of matches.
+
+local pre suf
+
+# Get the strings to compare.
+
+if [[ -z "$compconfig[list_word]" ]]; then
+  pre="$LBUFFER"
+  suf="$RBUFFER"
+else
+  pre="$PREFIX"
+  suf="$SUFFIX"
+fi
+
+# Should we only show a list now?
+
+if [[ ( -z "$compconfig[list_condition]" ||
+        "${(e):-\$[$compconfig[expand_glob]]}" -eq 1 ) &&
+      ( "$pre" != "$_list_prefix" || "$suf" != "$_list_suffix" ) ]]; then
+
+  # Yes. Tell the completion code about it and save the new values
+  # to compare the next time.
+
+  compstate[insert]=''
+  compstate[list]=list
+  compstate[force_list]=yes
+  _list_prefix="$pre"
+  _list_suffix="$suf"
+fi
+
+# We always return one, because we don't really do any work here.
+
+return 1
diff -u -r oc/README Completion/README
--- oc/README	Wed Mar 24 11:27:48 1999
+++ Completion/README	Wed Mar 24 11:42:25 1999
@@ -41,9 +41,14 @@
   _correct
     A completer function that attempts correction on the word from the
     line. Unlike _approximate this does only correction, not completion.
+  _expand
+    A completer function for expanding the word on the line.
   _files
     A frontend to _path_files which will default to any old file if the
     specified file was not found.
+  _list
+    A completer function that allows showing only a list on the first
+    TAB and insert completions on the second one.
   _match
     A completer function that temporarily swicthes on pattern matching
     when comparing the string from the line with possible completions.
@@ -300,6 +305,33 @@
     possible expansions. This is given to the -X option and thus may
     contain the control sequences `%n', `%B', etc. Also, the sequence
     `%o' in this string will be replaced by the original string.
+
+The _list completer allows one to delay the insertion of matches until
+completion is attempted a second time without the word on the line 
+being changed. On the first attempt, only the list of matches will
+be shown. Configuration keys understood are:
+
+  list_condition
+    If this key is unset or set to the empty string, this completer
+    will delay the insertion of matches unconditionally. However,
+    if this value is set, it should be set to an expression usable
+    inside a $[...] arithmetical expression. In this case, delaying
+    will be done if the expression evaluates to `1'.
+    For example, with
+
+      compconf list_condition='NUMERIC != 1'
+
+    delaying will be done only if given an explicit numeric argument
+    other than `1'.
+
+  list_word
+    To find out if only listing should be done, the code normally
+    compares the contents of the line with the contents the line
+    had at the time of the last invocation. If this key is set to
+    an non-empty string comparison is done using only the current
+    word. So if it is set, attempting completion on a word equal
+    to the one completion was called on the last time will not
+    delay the generation of matches.
 
 
 For more information about what the completers do, see the files

--
Sven Wischnowsky                         wischnow@xxxxxxxxxxxxxxxxxxxxxxx



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