Prevent "Selecting deleted buffer" error with dabbrev-expand
authorStephen Berman <stephen.berman@gmx.net>
Sat, 30 Nov 2024 22:28:06 +0000 (23:28 +0100)
committerStephen Berman <stephen.berman@gmx.net>
Sat, 30 Nov 2024 22:28:06 +0000 (23:28 +0100)
* 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.

lisp/dabbrev.el
test/lisp/dabbrev-tests.el

index bbe6a64b62606951ac4e3058ecbca6de52fbb11e..84306fb3ae7f6af758ba8f8972e35bb58a8ccd3e 100644 (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))))
index 987106aa5af2f9ea278e780bb247eaaa365ab367..b573737387573d0e708a4ea75c407c3b3aacbe17 100644 (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