介绍

Buffer 是编辑文本的 Lisp 对象,即可以是一个文件缓冲区,或者是一个临时缓冲区,临时缓冲区在 Emacs 退出时提示是否保存。在编辑文件时会打开多个 buffer 有时候需要快速切换和关闭某些对象文件。

  • 关闭所有 buffer
  • 关闭除当前 buffer 的其他 buffer
  • 关闭匹配到的 buffer

实现

代码参考doom-emacs

  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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
;;;###autoload
(defvar dotfairy-real-buffer-functions
  '(dotfairy-dired-buffer-p)
  "A list of predicate functions run to determine if a buffer is real, unlike
`dotfairy-unreal-buffer-functions'. They are passed one argument: the buffer to be
tested.
Should any of its function returns non-nil, the rest of the functions are
ignored and the buffer is considered real.
See `dotfairy-real-buffer-p' for more information.")

;;;###autoload
(defvar dotfairy-unreal-buffer-functions
  '(minibufferp dotfairy-special-buffer-p dotfairy-non-file-visiting-buffer-p)
  "A list of predicate functions run to determine if a buffer is *not* real,
unlike `dotfairy-real-buffer-functions'. They are passed one argument: the buffer to
be tested.
Should any of these functions return non-nil, the rest of the functions are
ignored and the buffer is considered unreal.
See `dotfairy-real-buffer-p' for more information.")

;;;###autoload
(defvar-local dotfairy-real-buffer-p nil
  "If non-nil, this buffer should be considered real no matter what. See
`dotfairy-real-buffer-p' for more information.")

;;;###autoload
(defvar dotfairy-fallback-buffer-name "*scratch*"
  "The name of the buffer to fall back to if no other buffers exist (will create
it if it doesn't exist).")

;;
;;; Functions
;;;###autoload
(defun dotfairy-buffer-frame-predicate (buf)
  "To be used as the default frame buffer-predicate parameter. Returns nil if
BUF should be skipped over by functions like `next-buffer' and `other-buffer'."
  (or (dotfairy-real-buffer-p buf)
      (eq buf (dotfairy-fallback-buffer))))

;;;###autoload
(defun dotfairy-fallback-buffer ()
  "Returns the fallback buffer, creating it if necessary. By default this is the
scratch buffer. See `dotfairy-fallback-buffer-name' to change this."
  (let (buffer-list-update-hook)
    (get-buffer-create dotfairy-fallback-buffer-name)))

;;;###autoload
(defalias 'dotfairy-buffer-list #'buffer-list)

;;;###autoload
(defun dotfairy-project-buffer-list (&optional project)
  "Return a list of buffers belonging to the specified PROJECT.
If PROJECT is nil, default to the current project.
If no project is active, return all buffers."
  (let ((buffers (dotfairy-buffer-list)))
    (if-let* ((project-root
               (if project (expand-file-name project)
                 (dotfairy-project-root))))
        (cl-loop for buf in buffers
                 if (projectile-project-buffer-p buf project-root)
                 collect buf)
      buffers)))

;;;###autoload
(defun dotfairy-open-projects ()
  "Return a list of projects with open buffers."
  (cl-loop with projects = (make-hash-table :test 'equal :size 8)
           for buffer in (dotfairy-buffer-list)
           if (buffer-live-p buffer)
           if (dotfairy-real-buffer-p buffer)
           if (with-current-buffer buffer (dotfairy-project-root))
           do (puthash (abbreviate-file-name it) t projects)
           finally return (hash-table-keys projects)))

;;;###autoload
(defun dotfairy-dired-buffer-p (buf)
  "Returns non-nil if BUF is a dired buffer."
  (with-current-buffer buf (derived-mode-p 'dired-mode)))

;;;###autoload
(defun dotfairy-special-buffer-p (buf)
  "Returns non-nil if BUF's name starts and ends with an *."
  (equal (substring (buffer-name buf) 0 1) "*"))

;;;###autoload
(defun dotfairy-temp-buffer-p (buf)
  "Returns non-nil if BUF is temporary."
  (equal (substring (buffer-name buf) 0 1) " "))

;;;###autoload
(defun dotfairy-visible-buffer-p (buf)
  "Return non-nil if BUF is visible."
  (get-buffer-window buf))

;;;###autoload
(defun dotfairy-buried-buffer-p (buf)
  "Return non-nil if BUF is not visible."
  (not (dotfairy-visible-buffer-p buf)))

;;;###autoload
(defun dotfairy-non-file-visiting-buffer-p (buf)
  "Returns non-nil if BUF does not have a value for `buffer-file-name'."
  (not (buffer-file-name buf)))

;;;###autoload
(defun dotfairy-real-buffer-list (&optional buffer-list)
  "Return a list of buffers that satify `dotfairy-real-buffer-p'."
  (cl-remove-if-not #'dotfairy-real-buffer-p (or buffer-list (dotfairy-buffer-list))))

;;;###autoload
(defun dotfairy-real-buffer-p (buffer-or-name)
  "Returns t if BUFFER-OR-NAME is a 'real' buffer.
A real buffer is a useful buffer; a first class citizen in Dotfairy. Real ones
should get special treatment, because we will be spending most of our time in
them. Unreal ones should be low-profile and easy to cast aside, so we can focus
on real ones.
The exact criteria for a real buffer is:
  1. A non-nil value for the buffer-local value of the `dotfairy-real-buffer-p'
     variable OR
  2. Any function in `dotfairy-real-buffer-functions' returns non-nil OR
  3. None of the functions in `dotfairy-unreal-buffer-functions' must return
     non-nil.
If BUFFER-OR-NAME is omitted or nil, the current buffer is tested."
  (or (bufferp buffer-or-name)
      (stringp buffer-or-name)
      (signal 'wrong-type-argument (list '(bufferp stringp) buffer-or-name)))
  (when-let (buf (get-buffer buffer-or-name))
    (when-let (basebuf (buffer-base-buffer buf))
      (setq buf basebuf))
    (and (buffer-live-p buf)
         (not (dotfairy-temp-buffer-p buf))
         (or (buffer-local-value 'dotfairy-real-buffer-p buf)
             (run-hook-with-args-until-success 'dotfairy-real-buffer-functions buf)
             (not (run-hook-with-args-until-success 'dotfairy-unreal-buffer-functions buf))))))

;;;###autoload
(defun dotfairy-unreal-buffer-p (buffer-or-name)
  "Return t if BUFFER-OR-NAME is an 'unreal' buffer.
See `dotfairy-real-buffer-p' for details on what that means."
  (not (dotfairy-real-buffer-p buffer-or-name)))

;;;###autoload
(defun dotfairy-buffers-in-mode (modes &optional buffer-list derived-p)
  "Return a list of buffers whose `major-mode' is `eq' to MODE(S).
If DERIVED-P, test with `derived-mode-p', otherwise use `eq'."
  (let ((modes (dotfairy-enlist modes)))
    (cl-remove-if-not (if derived-p
                          (lambda (buf)
                            (with-current-buffer buf
                              (apply #'derived-mode-p modes)))
                        (lambda (buf)
                          (memq (buffer-local-value 'major-mode buf) modes)))
                      (or buffer-list (dotfairy-buffer-list)))))

;;;###autoload
(defun dotfairy-visible-windows (&optional window-list)
  "Return a list of the visible, non-popup (dedicated) windows."
  (cl-loop for window in (or window-list (window-list))
           when (or (window-parameter window 'visible)
                    (not (window-dedicated-p window)))
           collect window))

;;;###autoload
(defun dotfairy-visible-buffers (&optional buffer-list)
  "Return a list of visible buffers (i.e. not buried)."
  (let ((buffers (delete-dups (mapcar #'window-buffer (window-list)))))
    (if buffer-list
        (cl-delete-if (lambda (b) (memq b buffer-list))
                      buffers)
      (delete-dups buffers))))

;;;###autoload
(defun dotfairy-buried-buffers (&optional buffer-list)
  "Get a list of buffers that are buried."
  (cl-remove-if #'get-buffer-window (or buffer-list (dotfairy-buffer-list))))

;;;###autoload
(defun dotfairy-matching-buffers (pattern &optional buffer-list)
  "Get a list of all buffers that match the regex PATTERN."
  (cl-loop for buf in (or buffer-list (dotfairy-buffer-list))
           when (string-match-p pattern (buffer-name buf))
           collect buf))

;;;###autoload
(defun dotfairy-set-buffer-real (buffer flag)
  "Forcibly mark BUFFER as FLAG (non-nil = real).
See `dotfairy-real-buffer-p' for an explanation for real buffers."
  (with-current-buffer buffer
    (setq dotfairy-real-buffer-p flag)))

;;;###autoload
(defun dotfairy-kill-buffer-and-windows (buffer)
  "Kill the buffer and delete all the windows it's displayed in."
  (dolist (window (get-buffer-window-list buffer))
    (unless (one-window-p t)
      (delete-window window)))
  (kill-buffer buffer))

;;;###autoload
(defun dotfairy-fixup-windows (windows)
  "Ensure that each of WINDOWS is showing a real buffer or the fallback buffer."
  (dolist (window windows)
    (with-selected-window window
      (when (dotfairy-unreal-buffer-p (window-buffer))
        (previous-buffer)
        (when (dotfairy-unreal-buffer-p (window-buffer))
          (switch-to-buffer (dotfairy-fallback-buffer)))))))

;;;###autoload
(defun dotfairy-kill-buffer-fixup-windows (buffer)
  "Kill the BUFFER and ensure all the windows it was displayed in have switched
to a real buffer or the fallback buffer."
  (let ((windows (get-buffer-window-list buffer)))
    (kill-buffer buffer)
    (dotfairy-fixup-windows (cl-remove-if-not #'window-live-p windows))))

;;;###autoload
(defun dotfairy-kill-buffers-fixup-windows (buffers)
  "Kill the BUFFERS and ensure all the windows they were displayed in have
switched to a real buffer or the fallback buffer."
  (let ((seen-windows (make-hash-table :test 'eq :size 8)))
    (dolist (buffer buffers)
      (let ((windows (get-buffer-window-list buffer)))
        (kill-buffer buffer)
        (dolist (window (cl-remove-if-not #'window-live-p windows))
          (puthash window t seen-windows))))
    (dotfairy-fixup-windows (hash-table-keys seen-windows))))

;;;###autoload
(defun dotfairy-kill-matching-buffers (pattern &optional buffer-list)
  "Kill all buffers (in current workspace OR in BUFFER-LIST) that match the
regex PATTERN. Returns the number of killed buffers."
  (let ((buffers (dotfairy-matching-buffers pattern buffer-list)))
    (dolist (buf buffers (length buffers))
      (kill-buffer buf))))


;;
;; Hooks
;;;###autoload
(defun dotfairy-mark-buffer-as-real-h ()
  "Hook function that marks the current buffer as real.
See `dotfairy-real-buffer-p' for an explanation for real buffers."
  (dotfairy-set-buffer-real (current-buffer) t))


;;
;; Interactive commands

;;;###autoload
(defun dotfairy/save-and-kill-buffer ()
  "Save the current buffer to file, then kill it."
  (interactive)
  (save-buffer)
  (kill-current-buffer))

;;;###autoload
(defun dotfairy/kill-this-buffer-in-all-windows (buffer &optional dont-save)
  "Kill BUFFER globally and ensure all windows previously showing this buffer
have switched to a real buffer or the fallback buffer.
If DONT-SAVE, don't prompt to save modified buffers (discarding their changes)."
  (interactive
   (list (current-buffer) current-prefix-arg))
  (cl-assert (bufferp buffer) t)
  (when (and (buffer-modified-p buffer) dont-save)
    (with-current-buffer buffer
      (set-buffer-modified-p nil)))
  (dotfairy-kill-buffer-fixup-windows buffer))


(defun dotfairy--message-or-count (interactive message count)
  (if interactive
      (message message count)
    count))

;;;###autoload
(defun dotfairy/kill-all-buffers (&optional buffer-list interactive)
  "Kill all buffers and closes their windows.
If the prefix arg is passed, doesn't close windows and only kill buffers that
belong to the current project."
  (interactive
   (list (if current-prefix-arg
             (dotfairy-project-buffer-list)
           (dotfairy-buffer-list))
         t))
  (if (null buffer-list)
      (message "No buffers to kill")
    (save-some-buffers)
    (delete-other-windows)
    (when (memq (current-buffer) buffer-list)
      (switch-to-buffer (dotfairy-fallback-buffer)))
    (mapc #'kill-buffer buffer-list)
    (dotfairy--message-or-count
     interactive "Killed %d buffers"
     (- (length buffer-list)
        (length (cl-remove-if-not #'buffer-live-p buffer-list))))))

;;;###autoload
(defun dotfairy/kill-other-buffers (&optional buffer-list interactive)
  "Kill all other buffers (besides the current one).
If the prefix arg is passed, kill only buffers that belong to the current
project."
  (interactive
   (list (delq (current-buffer)
               (if current-prefix-arg
                   (dotfairy-project-buffer-list)
                 (dotfairy-buffer-list)))
         t))
  (mapc #'dotfairy-kill-buffer-and-windows buffer-list)
  (dotfairy--message-or-count
   interactive "Killed %d other buffers"
   (- (length buffer-list)
      (length (cl-remove-if-not #'buffer-live-p buffer-list)))))

;;;###autoload
(defun dotfairy/kill-matching-buffers (pattern &optional buffer-list interactive)
  "Kill buffers that match PATTERN in BUFFER-LIST.
If the prefix arg is passed, only kill matching buffers in the current project."
  (interactive
   (list (read-regexp "Buffer pattern: ")
         (if current-prefix-arg
             (dotfairy-project-buffer-list)
           (dotfairy-buffer-list))
         t))
  (dotfairy-kill-matching-buffers pattern buffer-list)
  (when interactive
    (message "Killed %d buffer(s)"
             (- (length buffer-list)
                (length (cl-remove-if-not #'buffer-live-p buffer-list))))))

;;;###autoload
(defun dotfairy/kill-buried-buffers (&optional buffer-list interactive)
  "Kill buffers that are buried.
If PROJECT-P (universal argument), only kill buried buffers belonging to the
current project."
  (interactive
   (list (dotfairy-buried-buffers
          (if current-prefix-arg (dotfairy-project-buffer-list)))
         t))
  (mapc #'kill-buffer buffer-list)
  (dotfairy--message-or-count
   interactive "Killed %d buried buffers"
   (- (length buffer-list)
      (length (cl-remove-if-not #'buffer-live-p buffer-list)))))

;;;###autoload
(defun dotfairy/kill-project-buffers (project &optional interactive)
  "Kill buffers for the specified PROJECT."
  (interactive
   (list (if-let (open-projects (dotfairy-open-projects))
             (completing-read
              "Kill buffers for project: " open-projects
              nil t nil nil
              (if-let* ((project-root (dotfairy-project-root))
                        (project-root (abbreviate-file-name project-root))
                        ((member project-root open-projects)))
                  project-root))
           (message "No projects are open!")
           nil)
         t))
  (when project
    (let ((buffer-list (dotfairy-project-buffer-list project)))
      (dotfairy-kill-buffers-fixup-windows buffer-list)
      (dotfairy--message-or-count
       interactive "Killed %d project buffers"
       (- (length buffer-list)
          (length (cl-remove-if-not #'buffer-live-p buffer-list)))))))