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.
(window-buffer (get-mru-window)))
;; Otherwise, if we found the expansion in another
;; buffer, use that buffer for further expansions.
(dabbrev--last-buffer-found dabbrev--last-buffer-found)
;; buffer and that buffer is still live, use that
;; buffer for further expansions.
((buffer-live-p dabbrev--last-buffer-found)
dabbrev--last-buffer-found)
;; Otherwise, use the buffer where we invoked
;; dabbrev-expand.
(t (current-buffer))))

View file

@ -238,7 +238,7 @@ entered."
;; FIXME: Why is dabbrev--reset-global-variables needed here?
(ert-deftest dabbrev-expand-test-minibuffer-3 ()
"Test replacing an expansion in the minibuffer using two buffers.
The first expansion should befound in the buffer from which the
The first expansion should be found in the buffer from which the
minibuffer was entered, the replacement should found in another buffer."
(with-dabbrev-test
(find-file (ert-resource-file "INSTALL_BEGIN"))
@ -275,4 +275,36 @@ minibuffer was entered, the replacement should found in another buffer."
(should (string= (minibuffer-contents) "Indic and"))
(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