Fix choose-completion in CRM with boundaries

* lisp/emacs-lisp/crm.el (crm--choose-completion-string):
Delete.
(completing-read-multiple): Set buffer-local
completion-no-auto-exit instead of using
choose-completion-string-functions.  Do not set
completion-list-insert-choice-function.  (Bug#81411)

* test/lisp/emacs-lisp/crm-tests.el: New file.
This commit is contained in:
Aaron L. Zeng 2026-08-13 17:25:06 -04:00 committed by Sean Whitton
parent e420d0d978
commit e8b46219ca
2 changed files with 104 additions and 26 deletions

View file

@ -217,17 +217,6 @@ adjusted for the presence of multiple elements."
(goto-char (match-end 0))) (goto-char (match-end 0)))
(if doexit (exit-minibuffer)))) (if doexit (exit-minibuffer))))
(defun crm--choose-completion-string (choice buffer base-position
&rest _ignored)
"Completion string chooser for `completing-read-multiple'.
This is called from `choose-completion-string-functions'.
It replaces the string that is currently being completed, without
exiting the minibuffer."
(let ((completion-no-auto-exit t)
(choose-completion-string-functions nil))
(choose-completion-string choice buffer base-position)
t))
;; superemulates behavior of completing_read in src/minibuf.c ;; superemulates behavior of completing_read in src/minibuf.c
;; Use \\<crm-local-completion-map> so that help-enable-autoload can ;; Use \\<crm-local-completion-map> so that help-enable-autoload can
;; do its thing. Any keymap that is defined will do. ;; do its thing. Any keymap that is defined will do.
@ -259,21 +248,7 @@ with empty strings removed."
input) input)
(minibuffer-with-setup-hook (minibuffer-with-setup-hook
(lambda () (lambda ()
(add-hook 'choose-completion-string-functions (setq-local completion-no-auto-exit t)
'crm--choose-completion-string nil 'local)
(setq-local completion-list-insert-choice-function
(lambda (_start _end choice)
(let* ((beg (save-excursion
(if (search-backward-regexp crm-separator
(field-beginning)
t)
(1+ (point))
(minibuffer-prompt-end))))
(end (save-excursion
(if (search-forward-regexp crm-separator nil t)
(1- (point))
(point-max)))))
(completion--replace beg end choice))))
(setq-local crm-completion-table table) (setq-local crm-completion-table table)
(use-local-map map)) (use-local-map map))
(setq input (completing-read (setq input (completing-read

View file

@ -0,0 +1,103 @@
;;; crm-tests.el --- Tests for crm.el -*- lexical-binding: t; -*-
;; Copyright (C) 2026-2026 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'crm)
(require 'ert)
;; Copied from minibuffer-tests.el
(defmacro with-minibuffer-setup (completing-read &rest body)
(declare (indent 1) (debug t))
`(catch 'result
(minibuffer-with-setup-hook
(lambda ()
(let ((redisplay-skip-initial-frame nil)
(executing-kbd-macro nil)) ; Don't skip redisplay
(throw 'result (progn . ,body))))
(let ((executing-kbd-macro t)) ; Force the real minibuffer
,completing-read))))
(defmacro crm-test-with-file-name-table (initial-input &rest body)
(declare (indent 1) (debug t))
`(let* ((root (make-temp-file "crm-boundary-" 'directory))
(default-directory root))
(unwind-protect
(progn
(make-directory "dir")
(make-directory "dir/subdir")
(with-temp-file "dir/alpha" (insert ""))
(with-temp-file "dir/beta" (insert ""))
(with-temp-file "dir/subdir/gamma" (insert ""))
(with-minibuffer-setup
(completing-read-multiple "Pick: "
#'completion-file-name-table
nil t ,initial-input)
,@body))
(delete-directory root t))))
(ert-deftest crm-test-complete-uses-boundaries ()
(crm-test-with-file-name-table "dir/a"
(save-excursion (insert ",d/b"))
(should (equal (minibuffer-contents) "dir/a,d/b"))
(execute-kbd-macro (kbd "TAB"))
(should (equal (minibuffer-contents) "dir/alpha,d/b"))
;; complete between d and /
(execute-kbd-macro (kbd "C-f C-f TAB"))
(should (equal (minibuffer-contents) "dir/alpha,dir/b"))
(execute-kbd-macro (kbd "C-e TAB"))
(should (equal (minibuffer-contents) "dir/alpha,dir/beta"))
(execute-kbd-macro (kbd ", d/s/g TAB"))
(should (equal (minibuffer-contents) "dir/alpha,dir/beta,dir/subdir/gamma"))))
(ert-deftest crm-test-choose-completion-uses-boundaries ()
"Regression test for Bug#81411."
(crm-test-with-file-name-table "dir/a"
(save-excursion (insert ",d/b"))
(should (equal (minibuffer-contents) "dir/a,d/b"))
(execute-kbd-macro (kbd "? M-<down> M-RET"))
(should (equal (minibuffer-contents) "dir/alpha,d/b"))
;; complete between d and /
(execute-kbd-macro (kbd "C-f C-f ? M-<down> M-RET"))
(should (equal (minibuffer-contents) "dir/alpha,dir/b"))
(execute-kbd-macro (kbd "C-e ? M-<down> M-RET"))
(should (equal (minibuffer-contents) "dir/alpha,dir/beta"))
(execute-kbd-macro (kbd ", d/s/g ? M-<down> M-RET"))
(should (equal (minibuffer-contents) "dir/alpha,dir/beta,dir/subdir/gamma"))))
(ert-deftest crm-test-choose-completion-multi-char-separator ()
"Regression test for Bug#81411."
(crm-test-with-file-name-table "dir/a"
(save-excursion (insert " , d/b"))
(should (equal (minibuffer-contents) "dir/a , d/b"))
(execute-kbd-macro (kbd "? M-<down> M-RET"))
(should (equal (minibuffer-contents) "dir/alpha , d/b"))
;; complete between d and /
(forward-char 5)
(execute-kbd-macro (kbd "? M-<down> M-RET"))
(should (equal (minibuffer-contents) "dir/alpha , dir/b"))
(execute-kbd-macro (kbd "C-e ? M-<down> M-RET"))
(should (equal (minibuffer-contents) "dir/alpha , dir/beta"))))
(provide 'crm-tests)
;;; crm-tests.el ends here