From 8df69384f3951356dfb539e2cf72d905f82d00ae Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sat, 7 May 2022 13:19:49 +0200 Subject: [PATCH] Allow dabbrev to ignore binary buffers * doc/emacs/abbrevs.texi (Dynamic Abbrevs): Document it. * lisp/dabbrev.el (dabbrev-ignored-buffer-names) (dabbrev-ignored-buffer-regexps): Link to it. (dabbrev-ignored-buffer-modes): New user option (bug#19392). (dabbrev--filter-buffer-modes): New function. (dabbrev--select-buffers, dabbrev--make-friend-buffer-list): Use it. --- doc/emacs/abbrevs.texi | 5 ++++- etc/NEWS | 7 +++++++ lisp/dabbrev.el | 40 ++++++++++++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/doc/emacs/abbrevs.texi b/doc/emacs/abbrevs.texi index 9f339a03577..07f66ec10ac 100644 --- a/doc/emacs/abbrevs.texi +++ b/doc/emacs/abbrevs.texi @@ -411,10 +411,13 @@ away in the buffer to search for an expansion. @vindex dabbrev-check-all-buffers @vindex dabbrev-check-other-buffers +@vindex dabbrev-ignored-buffer-modes After scanning the current buffer, @kbd{M-/} normally searches other buffers. The variables @code{dabbrev-check-all-buffers} and @code{dabbrev-check-other-buffers} can be used to determine which -other buffers, if any, are searched. +other buffers, if any, are searched. Buffers that have major modes +derived from any of the modes in @code{dabbrev-ignored-buffer-modes} +are ignored. @vindex dabbrev-ignored-buffer-names @vindex dabbrev-ignored-buffer-regexps diff --git a/etc/NEWS b/etc/NEWS index a2f7f038524..2e7a1d86386 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -734,6 +734,13 @@ script that was used in ancient South Asia. A new input method, * Changes in Specialized Modes and Packages in Emacs 29.1 +** dabbrev + ++++ +*** New user option 'dabbrev-ignored-buffer-modes'. +Buffers with major modes in this list will be ignored. By default, +this includes "binary" buffers like 'archive-mode' and 'image-mode'. + ** Package +++ diff --git a/lisp/dabbrev.el b/lisp/dabbrev.el index 220a2f52e92..adbfca01a30 100644 --- a/lisp/dabbrev.el +++ b/lisp/dabbrev.el @@ -225,18 +225,28 @@ or matched by `dabbrev-ignored-buffer-regexps'." (defcustom dabbrev-ignored-buffer-names '("*Messages*" "*Buffer List*") "List of buffer names that dabbrev should not check. -See also `dabbrev-ignored-buffer-regexps'." +See also `dabbrev-ignored-buffer-regexps' and +`dabbrev-ignored-buffer-modes'." :type '(repeat (string :tag "Buffer name")) :group 'dabbrev :version "20.3") (defcustom dabbrev-ignored-buffer-regexps nil "List of regexps matching names of buffers that dabbrev should not check. -See also `dabbrev-ignored-buffer-names'." +See also `dabbrev-ignored-buffer-names' and +`dabbrev-ignored-buffer-modes'." :type '(repeat regexp) :group 'dabbrev :version "21.1") +(defcustom dabbrev-ignored-buffer-modes + '(archive-mode image-mode tar-mode) + "Inhibit looking for abbreviations in buffers derived from these modes. +See also `dabbrev-ignored-buffer-names' and +`dabbrev-ignored-buffer-regexps'." + :type '(repeat symbol) + :version "29.1") + (defcustom dabbrev-check-other-buffers t "Should \\[dabbrev-expand] look in other buffers? nil: Don't look in other buffers. @@ -632,19 +642,29 @@ See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]." "Return a list of other buffers to search for a possible abbrev. The current buffer is not included in the list. -This function makes a list of all the buffers returned by `buffer-list', -then discards buffers whose names match `dabbrev-ignored-buffer-names' -or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which -`dabbrev-friend-buffer-function', if it is bound, returns nil when called -with the buffer as argument. -It returns the list of the buffers that are not discarded." +This function makes a list of all the buffers returned by +`buffer-list', then discards buffers whose names match +`dabbrev-ignored-buffer-names' or +`dabbrev-ignored-buffer-regexps', and major modes that match +`dabbrev-ignored-buffer-modes'. It also discards buffers for +which `dabbrev-friend-buffer-function', if it is bound, returns +nil when called with the buffer as argument. It returns the list +of the buffers that are not discarded." (dabbrev-filter-elements - buffer (buffer-list) + buffer (dabbrev--filter-buffer-modes) (and (not (eq (current-buffer) buffer)) (not (dabbrev--ignore-buffer-p buffer)) (boundp 'dabbrev-friend-buffer-function) (funcall dabbrev-friend-buffer-function buffer)))) +(defun dabbrev--filter-buffer-modes () + (seq-filter (lambda (buffer) + (not (apply + #'provided-mode-derived-p + (buffer-local-value 'major-mode buffer) + dabbrev-ignored-buffer-modes))) + (buffer-list))) + (defun dabbrev--try-find (abbrev reverse n ignore-case) "Search for ABBREV, backwards if REVERSE, N times. If IGNORE-CASE is non-nil, ignore case while searching. @@ -779,7 +799,7 @@ of the start of the occurrence." (setq list (append list (dabbrev-filter-elements - buffer (buffer-list) + buffer (dabbrev--filter-buffer-modes) (and (not (memq buffer list)) (not (dabbrev--ignore-buffer-p buffer))))))) ;; Remove the current buffer. -- 2.39.2