Subject: bug#16604: 24.3.50; False negatives in lisp-completion-at-point

From: Dmitry Gutov <[email-masked]>
Date: Tue, 04 Feb 2014 07:37:06 +0200

On 02.02.2014 20:24, Stefan Monnier wrote:
> The behavior I'm looking for in completion-at-point should probably be
> obtained more along the lines of what is done in file-name completion
> where completion-ignored-extensions is used before calling
> try-completion.

I'm not sure I understand. Would there be a variable defining a regexp 
or glob for variable and function names that will be ignored unless they 
are the only candidates?

 > Also, we should try and check that the sub-tables all have "trivial"
boundaries, and no quoting.

Do we do that at "runtime" (after the lambda has been returned)? And 
just blow up calls with action `metadata' or `boundaries . ...' with 
error whenever that's not true?

I figured just documenting problematic cases might be enough (like 
`completion-table-in-turn' does, I suppose it has a similar problem with 
quoting).

 > Also, the outer try-completion could end up returning t if one of the
inner ones returns the string we're trying to complete.
We might be better off always going through "(apply #'append (mapcar
#'all-completions ...))", e.g. via completion-table-dynamic.

Sounds not very efficient. See the updated patch, does this look right 
to you?

I have a hard time testing it, though. lisp-completion-at-point seems to 
suggest any symbols that I've ever typed anyway, so there's no way to 
check that lisp--local-variables-completion-table is even used.

=== modified file 'lisp/emacs-lisp/lisp.el'
--- lisp/emacs-lisp/lisp.el	2014-01-01 07:43:34 +0000
+++ lisp/emacs-lisp/lisp.el	2014-02-02 01:42:32 +0000
@@ -830,7 +830,7 @@
                  ;; use it to provide a more specific completion table 
in some
                  ;; cases.  E.g. filter out keywords that are not 
understood by
                  ;; the macro/function being called.
-                (list nil (completion-table-in-turn
+                (list nil (completion-table-merge
                             lisp--local-variables-completion-table
                             obarray)       ;Could be anything.
                        :annotation-function

=== modified file 'lisp/minibuffer.el'
--- lisp/minibuffer.el	2014-01-07 23:36:29 +0000
+++ lisp/minibuffer.el	2014-02-04 05:25:30 +0000
@@ -393,6 +393,36 @@
                          (complete-with-action action table string pred))
                        tables)))

+(defun completion-table-merge (&rest tables)
+  "Create a completion table that collects completions from all TABLES."
+  ;; FIXME: same caveat as in `completion-table-in-turn', only harder
+  ;; to fix.
+  (lambda (string pred action)
+    (cond
+     ((null action)
+      (let ((retvals (mapcar (lambda (table)
+                               (try-completion string table pred))
+                             tables))
+            (prelim (try-completion string retvals pred)))
+        (cond
+         ((and (stringp prelim) (not (memq t retvals))) prelim)
+         ((null prelim) (and (memq t retvals) t))
+         ;; Here `prelim' is either t, and that means there's at least
+         ;; one string in `retvals', and all of them are equal to
+         ;; STRING.
+         ;; Or `prelim' is a string, but there's a `t' in `retvals',
+         ;; which means those matches won't all match `prelim'.
+         (t string))))
+     ((eq action t)
+
+      (apply #'append (mapcar (lambda (table)
+                                (all-completions string table pred))
+                              tables)))
+     (t
+      (completion--some (lambda (table)
+                          (complete-with-action action table string pred))
+                        tables)))))
+
  (defun completion-table-with-quoting (table unquote requote)
    ;; A difficult part of completion-with-quoting is to map positions 
in the
    ;; quoted string to equivalent positions in the unquoted string and