-
Notifications
You must be signed in to change notification settings - Fork 1
/
unique-window-buffers.el
210 lines (179 loc) · 7.65 KB
/
unique-window-buffers.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
;;; unique-window-buffers.el --- try to avoid showing same buffer in two windows on the same frame
;; this file is not part of Emacs
;; Copyright (C) 2011 Le Wang
;; Author: Le Wang
;; Maintainer: Le Wang
;; Description: try to avoid showing same buffer in two windows on the same frame
;; Author: Le Wang
;; Maintainer: Le Wang
;; Created: Sat Sep 17 20:44:06 2011 (+0800)
;; Version: 0.1
;; Last-Updated: Fri Sep 23 23:54:00 2011 (+0800)
;; By: Le Wang
;; Update #: 31
;; URL: https://github.com/lewang/unique-window-buffers
;; Keywords:
;; Compatibility:
;;; Installation:
;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ;;
;; this is BETA ;;
;; ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;
;; add to .emacs.el:
;;
;; (require 'unique-window-buffers)
;; (setq unique-window-buffers-mode t)
;;
;;; Commentary:
;; I was annoyed that when splitting windows the same buffer shows up in the
;; new window. It also happens when quitting help. So I made this.
;;
;;
;; Some notes for the author:
;;
;; `split-window-*' all call `split-window'
;;
;; `bury-buffer' calls `switch-to-prev-buffer'
;;
;; `quit-window' calls `switch-to-prev-buffer', but also has a separate
;; code-path that restores directly from window parameter `quit-restore'
;;
;; `kill-buffer' first kills the buffer, then calls
;; `replace-buffer-in-windows' to scrub the buffer from other windows.
;; `kill-buffer' has to be adviced to make the replace buffer unique.
;;
;;
;; `replace-buffer-in-windows' calls `switch-to-prev-buffer'
;;
;;
;; So advice functions `switch-to-prev-buffer' `quit-window' `split-window'
;; `kill-buffer'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.a
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code:
(eval-when-compile (require 'cl))
(provide 'unique-window-buffers)
(unless (fboundp 'window-normalize-buffer)
(defsubst window-normalize-buffer (buffer-or-name)
"Return buffer specified by BUFFER-OR-NAME.
BUFFER-OR-NAME must be either a buffer or a string naming a live
buffer and defaults to the current buffer."
(cond
((not buffer-or-name)
(current-buffer))
((bufferp buffer-or-name)
(if (buffer-live-p buffer-or-name)
buffer-or-name
(error "Buffer %s is not a live buffer" buffer-or-name)))
((get-buffer buffer-or-name))
(t
(error "No such buffer %s" buffer-or-name)))))
(defvar unique-window-buffers-uninteresting-filters
(eval-when-compile
(list
'minibufferp
"\\` "
(concat "\\`"
(regexp-quote "*Pp Eval Output*")
"\\'")
(concat "\\`"
(regexp-quote "*Completions*")
"\\'")))
"List of regexps matched against buffer-name or functions
called with a buffer as the only parameter. It should return t
if the buffer is not interesting")
(defvar unique-window-buffers-mode nil
"Set this variable to t if you want to only see unique buffers in different windows on the same frame.
Note: this does not have the baggage of a full minor-mode. It's
just a variable, setting it has immediate effect")
(defun unique-window-buffers-show (&optional window filters)
"Choose a buffer show in no other windows on the same frame to display in window.
If WINDOW is nil, then use the `selected-window'
FILTER is a list similar to `unique-window-buffers-uninteresting-filters'"
(setq window (or window (selected-window)))
(setq filters (or filters unique-window-buffers-uninteresting-filters))
(let ((other-displayed-buffers (delq nil
(mapcar #'(lambda (w)
(when (not (eq w window))
(window-buffer w)))
(window-list nil nil nil)))))
(when (memq (window-buffer window) other-displayed-buffers)
(set-window-buffer
window
(or (dolist (b (buffer-list (selected-frame)))
(unless (or (memq b other-displayed-buffers)
(dolist (f filters)
(let ((res (cond ((stringp f)
(string-match f (buffer-name b)))
((functionp f)
(funcall f b))
(t
(signal 'invalid-argument-error (list f))))))
(when res
(return res)))))
(return b)))
(current-buffer))))))
(defadvice switch-to-prev-buffer (around unique-window-buffers activate compile)
(if unique-window-buffers-mode
(let* ((old-window (or (ad-get-arg 0)
(selected-window)))
(old-buffer (window-buffer old-window)))
ad-do-it
(when (window-live-p old-window)
(unique-window-buffers-show
old-window
(nconc (if (buffer-live-p old-buffer)
;; if the old buffer wasn't killed or buried, we need to
;; prevent it from being switched to
(list (regexp-quote (concat "\\`"
(buffer-name old-buffer)
"\\'")))
nil)
unique-window-buffers-uninteresting-filters))))
ad-do-it))
(defadvice quit-window (around unique-window-buffers activate compile)
(if unique-window-buffers-mode
(let (
;; `switch-to-prev-buffer' possibly called, mayhem ensues if we
;; let them do work
(unique-window-buffers-mode nil)
(old-window (or (ad-get-arg 1)
(selected-window))))
ad-do-it
(when (window-live-p old-window)
(unique-window-buffers-show old-window)))
ad-do-it))
(defadvice kill-buffer (around unique-window-buffers activate compile)
(if unique-window-buffers-mode
(let* ((my-buf (window-normalize-buffer (ad-get-arg 0)))
(old-window (when (eq (window-buffer) my-buf)
(selected-window))))
ad-do-it
(when (window-live-p old-window)
(unique-window-buffers-show old-window)))
ad-do-it))
(defadvice split-window (after unique-window-buffers activate compile)
(when unique-window-buffers-mode
(unique-window-buffers-show ad-return-value)))
;; if you really need to remove the advice: (ad-deactivate-regexp "\\`unique-window-buffers\\'")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; unique-window-buffers.el ends here