-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
diminish-buffer.el
171 lines (141 loc) · 5.67 KB
/
diminish-buffer.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
;;; diminish-buffer.el --- Diminish (hide) buffers from buffer-menu -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2025 Shen, Jen-Chieh
;; Created date 2019-08-31 00:02:54
;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/jcs-elpa/diminish-buffer
;; Version: 0.2.0
;; Package-Requires: ((emacs "24.4"))
;; Keywords: convenience diminish hide buffer menu
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Diminish (hide) buffers from buffer-menu.
;;
;;; Code:
(require 'cl-lib)
(defgroup diminish-buffer nil
"Diminish (hide) buffers from `buffer-menu'."
:prefix "diminish-buffer-"
:group 'convenience
:link '(url-link :tag "Repository" "https://github.com/jcs-elpa/diminish-buffer"))
(defcustom diminish-buffer-list nil
"List of buffer name that you want to hide in the `buffer-menu'."
:type 'list
:group 'diminish-buffer)
(defcustom diminish-buffer-mode-list nil
"List of buffer mode that you want to hide in the `buffer-menu'."
:type 'list
:group 'diminish-buffer)
(defcustom diminish-buffer-refresh-instead-revert t
"Refresh buffer instead revert."
:type 'boolean
:group 'diminish-buffer)
(defconst diminish-buffer-menu-name "*Buffer List*"
"Buffer name for *Buffer List*.")
(defvar-local diminish-buffer--cache nil
"Cache to recording filter.")
;;
;; (@* "Util" )
;;
(defun diminish-buffer--contain-list-string-regex (elt list)
"Return non-nil if ELT is listed in LIST."
(let ((elt (format "%s" elt)))
(cl-some (lambda (elm) (string-match-p elm elt)) list)))
;;
;; (@* "Entry" )
;;
(defun diminish-buffer--enable ()
"Enable `diminish-buffer'."
(advice-add 'list-buffers--refresh :around #'diminish-buffer--refresh-list)
(diminish-buffer--refresh-buffer-menu))
(defun diminish-buffer--disable ()
"Disable `diminish-buffer'."
(advice-remove 'list-buffers--refresh #'diminish-buffer--refresh-list)
(diminish-buffer--refresh-buffer-menu)
(diminish-buffer-clear-cache))
;;;###autoload
(define-minor-mode diminish-buffer-mode
"Minor mode `diminish-buffer-mode'."
:global t
:require 'diminish-buffer
:group 'diminish-buffer
(if diminish-buffer-mode (diminish-buffer--enable) (diminish-buffer--disable)))
;;
;; (@* "Core" )
;;
(defun diminish-buffer-clear-cache ()
"Clear the cache."
(interactive)
(dolist (buf (buffer-list))
(with-current-buffer buf
(kill-local-variable 'diminish-buffer--cache))))
(defun diminish-buffer--filter (buffer)
"Filter out the BUFFER."
(with-current-buffer buffer
(let* ((name (buffer-name))
(answer (cond (diminish-buffer--cache
(equal diminish-buffer--cache t))
(t
(or (diminish-buffer--contain-list-string-regex
major-mode diminish-buffer-mode-list)
(diminish-buffer--contain-list-string-regex
name diminish-buffer-list))))))
(setq diminish-buffer--cache (if answer t 'no))
answer)))
;; XXX This is the default filter from Emacs itself; leave this feature as is it.
(defun diminish-buffer--default-filter (buffer)
"Copy it from function `list-buffers--refresh'."
(let ((buffer-menu-buffer (current-buffer))
(show-non-file (not Buffer-menu-files-only)))
(with-current-buffer buffer
(let* ((name (buffer-name))
(file buffer-file-name))
(and (buffer-live-p buffer)
(or (not (string= (substring name 0 1) " "))
file)
(not (eq buffer buffer-menu-buffer))
(or file show-non-file))))))
;;;###autoload
(defun diminish-buffer-default-list (&optional buffer-list)
"Return the default BUFFER-LIST generated from `buffer-menu'."
(unless buffer-list
(setq buffer-list (buffer-list (if Buffer-menu-use-frame-buffer-list
(selected-frame)))
buffer-list (cl-remove-if-not #'diminish-buffer--default-filter buffer-list)))
buffer-list)
;;;###autoload
(defun diminish-buffer-diminished-list (&optional buffer-list)
"Return diminished BUFFER-LIST."
(unless buffer-list
(setq buffer-list (diminish-buffer-default-list buffer-list)
buffer-list (cl-remove-if #'diminish-buffer--filter buffer-list))) ; filter
buffer-list)
(defun diminish-buffer--refresh-list (fnc &rest args)
"Modified argument `buffer-list' before display the buffer menu.
Override FNC and ARGS."
(let ((buffer-list (nth 0 args)))
(unless buffer-list
(setq buffer-list (diminish-buffer-diminished-list buffer-list))
(pop args) (push buffer-list args))) ; update
(apply fnc args))
(defun diminish-buffer--refresh-buffer-menu ()
"Refresh buffer menu at time when enabled/disabled."
(save-window-excursion
(let ((inhibit-message t) message-log-max)
(when (get-buffer diminish-buffer-menu-name)
(with-current-buffer diminish-buffer-menu-name
(if diminish-buffer-refresh-instead-revert (buffer-menu)
(tabulated-list-revert)))))
(bury-buffer)))
(provide 'diminish-buffer)
;;; diminish-buffer.el ends here