Subject: bug#73880: Master: emacs-lisp-mode: Tab completion for a function position fails in a `let' form.

From: Alan Mackenzie <[email-masked]>
Date: Sun, 20 Oct 2024 10:54:22 +0000

Hello, Dmitry and Emacs.

On Sat, Oct 19, 2024 at 13:09:00 +0000, Alan Mackenzie wrote:
> Hello, Emacs.

> In a recent master version, for example this commit:

> commit 5340fdaade1f8fe7af08293619cca89ae0796fcf (HEAD -> master, origin/master, origin/HEAD)
> Author: Alan Mackenzie <[email-masked]>
> Date:   Wed Oct 16 13:17:26 2024 +0000

>     CC Mode: Fix dodgy lisp `let' form.

> , start emacs -Q, followed by entering the following incomplete form:

> (defun foo ()
>   (let (
>         )
>     (match-b

> With point after match-b, type M-TAB.  This should complete to
> match-beginning or show that function as a completion option.  Instead
> it signals the error "No match".  This is a bug.

> It would seem the completion function elisp-completion-at-point thinks
> it is completing a variable symbol rather than a function symbol.

This is indeed the case.  The following patch fixes this by checking
that the symbol being completed begins within the binding list.  It also
adds a test into elisp-mode-tests.el.

Dmitry, could you check the patch is OK, please, before I commit it.
Thanks!

diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el
index 2f931daedc7..3233447a996 100644
--- a/lisp/progmodes/elisp-mode.el
+++ b/lisp/progmodes/elisp-mode.el
@@ -760,7 +760,8 @@ elisp-completion-at-point
                                                (forward-sexp)
                                                (intern-soft
                                                 (buffer-substring pt (point))))))))
-                            (error nil))))
+                            (error nil)))
+                         (parent-end (point)))
                      (pcase parent
                        ;; FIXME: Rather than hardcode special cases here,
                        ;; we should use something like a symbol-property.
@@ -784,18 +785,35 @@ elisp-completion-at-point
                         (list t (elisp--completion-local-symbols)
                               :predicate (lambda (sym)
                                            (get sym 'error-conditions))))
-                       ((and (or ?\( 'let 'let* 'cond 'cond* 'bind*)
-                             (guard (save-excursion
-                                      (goto-char (1- beg))
-                                      (when (eq parent ?\()
-                                        (up-list -1))
-                                      (forward-symbol -1)
-                                      (or
-                                       (looking-at
-                                        "\\_<\\(let\\*?\\|bind\\*\\)\\_>")
-                                       (and (not (eq parent ?\())
+                       ((or
+                         (and (or 'let 'let*)
+                              (guard (save-excursion
+                                       (goto-char parent-end)
+                                       (forward-comment (point-max))
+                                       (let ((bindings-end
+                                              (condition-case nil
+                                                  (progn (forward-list)
+                                                         (point))
+                                                (error (point-max)))))
+                                         (and
+                                          (< beg bindings-end)
+                                          (progn
+                                            (goto-char (1- beg))
+                                            (forward-symbol -1)
                                             (looking-at
-                                             "\\_<cond\\*?\\_>"))))))
+                                             "\\_<let\\*?\\_>")))))))
+                         (and (or ?\( 'cond 'cond* 'bind*)
+                              (guard (save-excursion
+                                       (goto-char (1- beg))
+                                       (when (eq parent ?\()
+                                         (up-list -1))
+                                       (forward-symbol -1)
+                                       (or
+                                        (looking-at
+                                         "\\_<\\(let\\*?\\|bind\\*\\)\\_>")
+                                        (and (not (eq parent ?\())
+                                             (looking-at
+                                              "\\_<cond\\*?\\_>")))))))
                         (list t (elisp--completion-local-symbols)
                               :predicate #'elisp--shorthand-aware-boundp
                               :company-kind (lambda (_) 'variable)
diff --git a/test/lisp/progmodes/elisp-mode-tests.el b/test/lisp/progmodes/elisp-mode-tests.el
index 591c32a8271..45714b3e7d9 100644
--- a/test/lisp/progmodes/elisp-mode-tests.el
+++ b/test/lisp/progmodes/elisp-mode-tests.el
@@ -109,6 +109,14 @@ elisp--test-completions
         (should (member "backup-inhibited" comps))
         (should-not (member "backup-buffer" comps))))))
 
+(ert-deftest elisp-completes-functions-after-empty-let-bindings ()
+  (with-temp-buffer
+    (emacs-lisp-mode)
+    (insert "(let () (ba")
+    (let ((comps (elisp--test-completions)))
+      (should (member "backup-buffer" comps))
+      (should-not (member "backup-inhibited" comps)))))
+
 (ert-deftest elisp-completes-functions-after-let-bindings-2 ()
   (with-temp-buffer
     (emacs-lisp-mode)

-- 
Alan Mackenzie (Nuremberg, Germany).