-
Notifications
You must be signed in to change notification settings - Fork 7
/
iflipb.el
378 lines (330 loc) · 14.1 KB
/
iflipb.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
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
368
369
370
371
372
373
374
375
376
377
378
;;; iflipb.el --- Interactively flip between recently visited buffers
;;
;; Copyright (C) 2007-2022 Joel Rosdahl
;;
;; Author: Joel Rosdahl <[email protected]>
;; Version: 1.6
;; License: BSD-3-clause
;; URL: https://github.com/jrosdahl/iflipb
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; 3. Neither the name of the author nor the names of its contributors
;; may be used to endorse or promote products derived from this software
;; without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;; DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
;; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
;; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;
;; ============================================================================
;;
;;; Commentary:
;;
;; iflipb lets you flip between recently visited buffers in a way that
;; resembles what Alt-(Shift-)TAB does in Microsoft Windows and other graphical
;; window managers.
;;
;; See <https://github.com/jrosdahl/iflipb/blob/master/README.md> for more
;; information and configuration instructions.
;;
;;; Code:
(defgroup :iflipb nil
"Interactively flip between recently visited buffers."
:group 'convenience)
(defcustom iflipb-ignore-buffers "^[*]"
"Which buffers to ignore by default.
This variable determines which buffers to ignore when a prefix
argument has not been given to `iflipb-next-buffer'. The value
should be a regexp string, a function or a list. If the value is
a regexp string, it describes buffer names to exclude from the
buffer list. If the value is a function, the function will get a
buffer name as an argument and should return `nil' if the buffer
should be excluded, otherwise non-`nil'. If the value is a list,
the filter matches if any of the list elements match."
:type '(choice
(regexp :tag "Regexp that describes buffer names to exclude")
(function :tag "Function that takes a buffer name")
(list (string :tag "Buffer to exclude")))
:group 'iflipb)
(defcustom iflipb-always-ignore-buffers "^ "
"Which buffers to always ignore.
This variable determines which buffers to always ignore. The
value should be a regexp string, a function or a list. If the
value is a regexp string, it describes buffer names to exclude
from the buffer list. If the value is a function, the function
will get a buffer name as an argument and should return `nil' if
the buffer should be excluded, otherwise non-`nil'. If the value
is a list, the filter matches if any of the list elements match."
:type '(choice
(regexp :tag "Regexp that describes buffer names to exclude")
(function :tag "Function that takes a buffer name")
(list (string :tag "Buffer to always exclude")))
:group 'iflipb)
(defcustom iflipb-wrap-around nil
"Whether buffer cycling should wrap around edges.
This variable determines whether buffer cycling should wrap
around when an edge is reached in the buffer list."
:type 'boolean
:safe #'booleanp
:group 'iflipb)
(defcustom iflipb-permissive-flip-back nil
"Whether flipping backward should be allowed as the first command.
This variable determines whether `iflipb-previous-buffer' should
be able to go to the last buffer when it's the first
iflipb-*-buffer command in a row. In other words: Running
`iflipb-previous-buffer' after editing a buffer will act as if
the current buffer was not visited; it will stay in its original
place in the buffer list."
:type 'boolean
:safe #'booleanp
:group 'iflipb)
(defface iflipb-other-buffer-face
'((t (:inherit default)))
"Face used for a non-current buffer name."
:group 'iflipb)
(defface iflipb-current-buffer-face
'((t (:inherit minibuffer-prompt)))
"Face used for the current buffer name."
:group 'iflipb)
(defcustom iflipb-other-buffer-template
"%s"
"String template for displaying other buffers.
This template string says how to display a non-current buffer
name. \"%s\" expands to the buffer name. Note: don't enter the
surrounding quotes in the input field."
:type 'string
:group 'iflipb)
(defcustom iflipb-current-buffer-template
"[%s]"
"String template for displaying the current buffer.
This is the template string that will be applied to the current
buffer name. Use \"%s\" to refer to the buffer name. Note: don't
enter the surrounding quotes in the input field."
:type 'strings
:group 'iflipb)
(defcustom iflipb-buffer-list-function
#'iflipb-buffer-list
"The function to be used to retrieve the buffer list.
The current options are `iflipb-buffer-list' and
`iflipb-ido-buffer-list'."
:type 'function
:group 'iflipb)
(defcustom iflipb-format-buffers-function
#'iflipb-format-buffers-horizontally
"The function to be used to format buffers.
This function is used to format buffer names. The function will
get the current buffer and a buffer list as arguments. A return
value is a string to be displayed. Predefined functions are
`iflipb-format-buffers-horizontally' for a horizontal list and
`iflipb-format-buffers-vertically' for a vertical list. See also
`iflipb-format-buffers-height'."
:type '(choice
(function-item
:tag "Horizontally" iflipb-format-buffers-horizontally)
(function-item
:tag "Vertically" iflipb-format-buffers-vertically)
(function :tag "Other function"))
:group 'iflipb)
(defcustom iflipb-format-buffers-height 5
"Minibuffer height for displaying buffers when using
`iflipb-format-buffers-vertically'.
The actual height will not exceed the height indicated by
`max-mini-window-height'."
:type 'integer
:group 'iflipb)
(defvar iflipb-current-buffer-index 0
"Index of the currently displayed buffer in the buffer list.")
(defvar iflipb-include-more-buffers nil
"Whether all buffers should be included while flipping.")
(defvar iflipb-saved-buffers nil
"Saved buffer list state.
This is the original order of buffers to the left of
`iflipb-current-buffer-index'.")
(defun iflipb-first-n (n list)
"Return the first N elements of LIST."
(butlast list (- (length list) n)))
(defun iflipb-filter (pred elements)
"Return elements from ELEMENTS that satisfy predicate PRED."
(let ((result nil))
(while elements
(let ((elem (car elements))
(rest (cdr elements)))
(when (funcall pred elem)
(setq result (cons elem result)))
(setq elements rest)))
(nreverse result)))
(defun iflipb-any (elements)
"Return non-nil if and only if any element in ELEMENTS is non-nil."
(iflipb-filter (lambda (x) (not (null x))) elements))
(defun iflipb-match-filter (string filter)
"Return non-nil if STRING matches FILTER, otherwise nil."
(cond ((null filter) nil)
((functionp filter) (funcall filter string))
((listp filter)
(iflipb-any (mapcar (lambda (f) (iflipb-match-filter string f))
filter)))
((stringp filter) (string-match filter string))
(t (error "Bad iflipb ignore filter element: %s" filter))))
(defun iflipb-buffer-list ()
"Buffer list for iflipb."
(buffer-list (selected-frame)))
(defun iflipb-ido-buffer-list ()
"Ido buffer list for iflipb."
(require 'ido)
(declare-function ido-make-buffer-list "ido")
(let* ((ido-process-ignore-lists t)
ido-ignored-list
ido-ignore-buffers
ido-use-virtual-buffers
(bufs (mapcar 'get-buffer (ido-make-buffer-list nil))))
(remove nil
(mapcar (lambda (b) (if (memq b bufs) b))
(buffer-list (selected-frame))))))
(defun iflipb-buffers-not-matching-filter (filter)
"Return a list of buffer names not matching FILTER."
(iflipb-filter
(lambda (b) (not (iflipb-match-filter (buffer-name b) filter)))
(funcall iflipb-buffer-list-function)))
(defun iflipb-interesting-buffers ()
"Return buffers that will be in the displayed buffer list."
(iflipb-buffers-not-matching-filter
(append
(list iflipb-always-ignore-buffers)
(if iflipb-include-more-buffers
nil
(list iflipb-ignore-buffers)))))
(defun iflipb-first-iflipb-buffer-switch-command ()
"Determine if this is the first iflipb invocation this round."
(and (symbolp last-command) ; Could be a lambda
(not (string-match "^iflipb-" (symbol-name last-command)))))
(defun iflipb-restore-buffers ()
"Helper function that restores the buffer list to the original state."
(mapc 'switch-to-buffer (reverse iflipb-saved-buffers)))
(defun iflipb-format-buffer (current-buffer buffer)
"Format a buffer name for inclusion in the minibuffer buffer list."
(let* ((type (if (eq current-buffer buffer) "current" "other"))
(face (intern (format "iflipb-%s-buffer-face" type)))
(template (intern (format "iflipb-%s-buffer-template" type)))
(name (concat (buffer-name buffer)))) ; concat to copy the string
(add-text-properties 0 (length name) `(face ,face) name)
(format (symbol-value template) name)))
(defun iflipb-format-buffers (current-buffer buffers)
"Format buffer names for displaying them in the minibuffer."
(funcall iflipb-format-buffers-function current-buffer buffers))
(defun iflipb-format-buffers-horizontally (current-buffer buffers)
"Format buffer names for displaying them horizontally."
(truncate-string-to-width
(mapconcat
(lambda (buffer)
(iflipb-format-buffer current-buffer buffer))
buffers
" ")
(1- (window-width (minibuffer-window)))))
(defun iflipb-format-buffers-vertically (current-buffer buffers)
"Format buffer names for displaying them vertically."
(let ((height
(min iflipb-format-buffers-height
(cond ((integerp max-mini-window-height)
max-mini-window-height)
((floatp max-mini-window-height)
(floor (* (frame-height)
max-mini-window-height)))
(t (error "Invalid value of `max-mini-window-height': %s"
max-mini-window-height)))))
(rest buffers)
(n 0)
nrest buffs)
(while (and rest (or (null nrest) (> nrest 0)))
(let ((b (car rest)))
(setq buffs (cons b buffs)
rest (cdr rest)
n (1+ n))
(cond ((eq current-buffer b)
(setq nrest (- height (min n (ceiling height 2)))))
((numberp nrest)
(setq nrest (1- nrest))))))
(mapconcat
(lambda (b) (iflipb-format-buffer current-buffer b))
(nreverse (iflipb-first-n height buffs))
"\n")))
(defun iflipb-message (text)
(let (message-log-max)
(message "%s" text)))
(defun iflipb-select-buffer (index)
"Helper function that shows the buffer with a given index."
(iflipb-restore-buffers)
(setq iflipb-saved-buffers nil)
(let* ((buffers (iflipb-interesting-buffers))
(current-buffer (nth index buffers)))
(setq iflipb-current-buffer-index index)
(setq iflipb-saved-buffers (iflipb-first-n index buffers))
(iflipb-message (iflipb-format-buffers current-buffer buffers))
(switch-to-buffer current-buffer)))
;;;###autoload
(defun iflipb-next-buffer (arg)
"Flip to the next buffer in the buffer list.
Consecutive invocations switch to less recent buffers in the
buffer list. Buffers matching `iflipb-always-ignore-buffers' are
always ignored. Without a prefix argument, buffers matching
`iflipb-ignore-buffers' are also ignored."
(interactive "P")
(when (iflipb-first-iflipb-buffer-switch-command)
(setq iflipb-current-buffer-index 0)
(setq iflipb-saved-buffers nil))
(if arg
(setq iflipb-include-more-buffers t)
(when (iflipb-first-iflipb-buffer-switch-command)
(setq iflipb-include-more-buffers nil)))
(let ((buffers (iflipb-interesting-buffers)))
(if (or (null buffers)
(and (memq (window-buffer) buffers)
(= iflipb-current-buffer-index
(1- (length buffers)))))
(if iflipb-wrap-around
(iflipb-select-buffer 0)
(iflipb-message "No more buffers."))
(iflipb-select-buffer (1+ iflipb-current-buffer-index)))
(setq last-command 'iflipb-next-buffer)))
;;;###autoload
(defun iflipb-previous-buffer ()
"Flip to the previous buffer in the buffer list.
Consecutive invocations switch to more recent buffers in the
buffer list."
(interactive)
(when (and (not iflipb-permissive-flip-back)
(iflipb-first-iflipb-buffer-switch-command))
(setq iflipb-current-buffer-index 0)
(setq iflipb-saved-buffers nil))
(if (= iflipb-current-buffer-index 0)
(if iflipb-wrap-around
(iflipb-select-buffer (1- (length (iflipb-interesting-buffers))))
(iflipb-message "You are already looking at the top buffer."))
(iflipb-select-buffer (1- iflipb-current-buffer-index)))
(setq last-command 'iflipb-previous-buffer))
;;;###autoload
(defun iflipb-kill-buffer ()
"Same as `kill-buffer' but keep the iflipb buffer list state."
(interactive)
(call-interactively #'kill-buffer)
(if (iflipb-first-iflipb-buffer-switch-command)
(setq last-command 'kill-buffer)
(if (< iflipb-current-buffer-index (length (iflipb-interesting-buffers)))
(iflipb-select-buffer iflipb-current-buffer-index)
(iflipb-select-buffer (1- iflipb-current-buffer-index)))
(setq last-command 'iflipb-kill-buffer)))
(provide 'iflipb)
;;; iflipb.el ends here