1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Prevent "Selecting deleted buffer" error with dabbrev-expand

* lisp/dabbrev.el (dabbrev-expand): Use the buffer where the last
expansion was found only if it is still a live buffer (bug#74090).

* test/lisp/dabbrev-tests.el (dabbrev-expand-test-minibuffer-3):
Fix typo in doc string.
(dabbrev-expand-after-killing-buffer): New test.
This commit is contained in:
Stephen Berman 2024-11-30 23:28:06 +01:00
parent 0a753603a5
commit bd8a6f70fb
2 changed files with 37 additions and 3 deletions

View file

@ -472,8 +472,10 @@ See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
;; minibuffer. ;; minibuffer.
(window-buffer (get-mru-window))) (window-buffer (get-mru-window)))
;; Otherwise, if we found the expansion in another ;; Otherwise, if we found the expansion in another
;; buffer, use that buffer for further expansions. ;; buffer and that buffer is still live, use that
(dabbrev--last-buffer-found dabbrev--last-buffer-found) ;; buffer for further expansions.
((buffer-live-p dabbrev--last-buffer-found)
dabbrev--last-buffer-found)
;; Otherwise, use the buffer where we invoked ;; Otherwise, use the buffer where we invoked
;; dabbrev-expand. ;; dabbrev-expand.
(t (current-buffer)))) (t (current-buffer))))

View file

@ -275,4 +275,36 @@ minibuffer was entered, the replacement should found in another buffer."
(should (string= (minibuffer-contents) "Indic and")) (should (string= (minibuffer-contents) "Indic and"))
(delete-minibuffer-contents)))) (delete-minibuffer-contents))))
(ert-deftest dabbrev-expand-after-killing-buffer ()
"Test expansion after killing buffer containing first expansion.
Finding successive expansions in another live buffer should succeed, but
after killing the buffer, expansion should fail with a user-error."
;; FIXME? The message shown by the user-error is in *Messages* but
;; since the test finishes on hitting the user-error, we cannot test
;; further, either for the content of the message or the content of
;; the current buffer, so apparently cannot reproduce what a user
;; entering these commands manually sees.
(with-dabbrev-test
(with-current-buffer (get-buffer-create "foo")
(insert "abc abd"))
(switch-to-buffer "*scratch*")
(erase-buffer)
(execute-kbd-macro (kbd "ab M-/"))
(should (string= (buffer-string) "abc"))
(execute-kbd-macro (kbd "SPC ab M-/"))
(should (string= (buffer-string) "abc abc"))
(erase-buffer)
(execute-kbd-macro (kbd "abc SPC ab M-/ M-/"))
(should (string= (buffer-string) "abc abd"))
(kill-buffer "foo")
(erase-buffer)
(should-error (execute-kbd-macro (kbd "abc SPC ab M-/ M-/"))
:type 'user-error)
;; (should (string= (buffer-string) "abc abc"))
;; (with-current-buffer "*Messages*"
;; (goto-char (point-max))
;; (should (string= (buffer-substring (pos-bol) (pos-eol))
;; "No further dynamic expansion for ab found")))
))
;;; dabbrev-tests.el ends here ;;; dabbrev-tests.el ends here