Subject: 31.0.50; completion-at-point doesn't work in custom buffers

From: Stefan Monnier <[email-masked]>
Date: Sun, 17 Nov 2024 18:15:36 -0500

Package: Emacs
Version: 31.0.50

For example:

    src/emacs -Q --eval '(customize-face `fixed-pitch)'
    ... move point to the font family field ...
    ... delete the "space" from "Monospace" then do:
    M-x completion-at-point RET

This will presumably do nothing at all.
`widget-complete` completes it back to "Monospace" (at least on
`master` where I installed a patch for that).

The problem is that cus-edit doesn't setup
`completion-at-point-functions`.  Instead it sets up a special keymap to
remap M-TAB to `widget-complete` instead of relying on the
global binding.

It works OK for a default config, but it doesn't interact well with
setups that use different keybindings or different completion UIs based
on `completion-at-point-functions`.  E.g. `corfu-mode` partly works but
not fully (e.g. `corfu-auto` doesn't have any effect).

The patch below leaves the key remapping for now, but adds an
appropriate function to `completion-at-point-functions` so that
other UIs such as `completion-at-point` can do their job properly.

Comments/objection?

        Stefan

diff --git a/lisp/cus-edit.el b/lisp/cus-edit.el
index e0668d4ba86..02e0b541f05 100644
--- a/lisp/cus-edit.el
+++ b/lisp/cus-edit.el
@@ -5503,7 +5503,8 @@ Custom-mode
   (make-local-variable 'custom-options)
   (make-local-variable 'custom-local-buffer)
   (custom--initialize-widget-variables)
-  (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t))
+  (add-hook 'completion-at-point-functions #'widget-completions-at-point nil t)
+  (add-hook 'widget-edit-functions #'custom-state-buffer-message nil t))
 
 (defun custom--revert-buffer (_ignore-auto _noconfirm)
   (unless custom--invocation-options
diff --git a/lisp/wid-edit.el b/lisp/wid-edit.el
index 58a36820ac7..1538720adef 100644
--- a/lisp/wid-edit.el
+++ b/lisp/wid-edit.el
@@ -1399,7 +1399,7 @@ widget-complete
   "Complete content of editable field from point.
 When not inside a field, signal an error."
   (interactive)
-  (let ((data (widget-completions-at-point)))
+  (let ((data (widget-completions-at-point 'error)))
     (cond
      ((functionp data) (funcall data))
      ((consp data)
@@ -1414,11 +1414,11 @@ widget-complete
 ;; so it's not really obsolete (yet).
 ;; (make-obsolete 'widget-complete 'completion-at-point "24.1")
 
-(defun widget-completions-at-point ()
+(defun widget-completions-at-point (&optional error)
   (let ((field (widget-field-find (point))))
     (if field
         (widget-apply field :completions-function)
-      (error "Not in an editable field"))))
+      (if error (error "Not in an editable field")))))
 
 ;;; Setting up the buffer.