diff --git a/lisp/dabbrev.el b/lisp/dabbrev.el index bbe6a64b626..84306fb3ae7 100644 --- a/lisp/dabbrev.el +++ b/lisp/dabbrev.el @@ -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)))) diff --git a/test/lisp/dabbrev-tests.el b/test/lisp/dabbrev-tests.el index 987106aa5af..b5737373875 100644 --- a/test/lisp/dabbrev-tests.el +++ b/test/lisp/dabbrev-tests.el @@ -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