-
Notifications
You must be signed in to change notification settings - Fork 0
/
winpoint.el
272 lines (241 loc) · 8.8 KB
/
winpoint.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
;;; winpoint.el --- Remember buffer positions per-window, not per buffer
;; Copyright (C) 2006 Jorgen Schaefer
;; Copyright (C) 2009 Andy Stewart
;; Filename: winpoint.el
;; Description: Remember buffer positions per-window, not per buffer
;; Author: Jorgen Schaefer <[email protected]>
;; Maintainer: Jorgen Schaefer <[email protected]>
;; Andy Stewart <[email protected]>
;; Created: 2006
;; Version: 1.3
;; Last-Updated: 2009-01-13 12:01:14
;; By: Andy Stewart
;; URL: http://www.emacswiki.org/emacs/download/winpoint.el
;; Keywords: winpoint
;; Compatibility: GNU Emacs 23.0.60.1
;; GNU Emacs 22
;; GNU Emacs 21
;;
;; Features that might be required by this library:
;;
;;
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; 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 2, 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.
;;; Commentary:
;;
;; When two windows view the same buffer at the same time, and one
;; window is switched to another buffer and back, point is now the
;; same as in the other window, not as it was before we switched away.
;; This mode tries to work around this problem by storing and
;; restoring per-window positions for each buffer.
;;
;;; Installation:
;;
;; Put winpoint.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'winpoint)
;;
;; And add below to enable `winpoint'.
;;
;; (window-point-remember-mode 1)
;;
;;; Customize:
;;
;; `winpoint-non-restore-buffer-list'
;;
;; This option contain a buffer that don't want to restore point.
;;
;; For example, setup *Group* buffer like below:
;;
;; (setq winpoint-non-restore-buffer-list '("*Group*"))
;;
;;; Change log:
;;
;; 2009/01/13
;; * Andy Stewart:
;; * Make code compatibility with Emacs 23.
;; * Add new option `winpoint-non-restore-buffer-list'
;; to avoid some buffer restored window point.
;;
;; 2006
;; * Jorgen Schaefer:
;; * First released.
;;
;;; Require
;;; Code:
(defgroup winpoint nil
"Remember buffer positions per-window, not per buffer."
:group 'window)
(defcustom winpoint-non-restore-buffer-list
'()
"The buffer list that don't restore point."
:type 'list
:group 'winpoint)
(defvar winpoint-frame-windows nil
"The current frame's windows and their buffers.
This is an alist mapping windows to their current buffers.")
;; Emacs 23 not need below setup anymore.
(when (> emacs-major-version 22)
(eval-when-compile
(with-no-warnings
(make-variable-frame-local 'winpoint-frame-windows))))
(defvar winpoint-frame-positions nil
"The current frame's windows and their associated buffer positions.
This is an alist mapping windows to an alist mapping buffers to
their stored point marker.")
;; Emacs 23 not need below setup anymore.
(when (> emacs-major-version 22)
(eval-when-compile
(with-no-warnings
(make-variable-frame-local 'winpoint-frame-positions))))
(defalias 'window-point-remember-mode 'winpoint-mode)
(define-minor-mode winpoint-mode
"Remember positions in a buffer per-window, not per-buffer.
That is, when you have the same buffer open in two different
windows, and you switch the buffer in one window and back again,
the position is the same as it was when you switched away, not
the same as in the other window."
:global t
(cond
(winpoint-mode
(add-hook 'post-command-hook 'winpoint-remember)
(add-hook 'window-configuration-change-hook
'winpoint-remember-configuration))
(t
(remove-hook 'post-command-hook 'winpoint-remember)
(remove-hook 'window-configuration-change-hook
'winpoint-remember-configuration))))
(defun winpoint-remember ()
"Remember the currently visible buffer's positions.
This should be put on `post-command-hook'."
(walk-windows (lambda (win)
(let ((buf (window-buffer win)))
(winpoint-put win
buf
(window-point win))))))
(defun winpoint-remember-configuration ()
"This remembers the currently shown windows.
If any buffer wasn't shown before, point in that window is
restored.
If any window isn't shown anymore, forget about it."
(winpoint-clean)
(setq winpoint-frame-windows
(mapcar (lambda (win)
(let ((old (assq win winpoint-frame-windows))
(newbuf (window-buffer win)))
(when (and old
(not (eq (cdr old)
newbuf)))
(winpoint-restore win))
(cons win newbuf)))
(window-list))))
;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Emacs 21 compatibility
(eval-when-compile
(when (not (fboundp 'with-selected-window))
(defmacro with-selected-window (window &rest body)
"Execute the forms in BODY with WINDOW as the selected window."
`(save-selected-window
(select-window ,window)
,@body))))
(defun winpoint-restore (win)
"Restore point in the window WIN."
(with-selected-window win
(let ((point (winpoint-get win (current-buffer))))
(when (and point
(not (winpoint-match-non-restore-buffer (buffer-name))))
(goto-char point)))))
(defun winpoint-match-non-restore-buffer (bufname)
"Function document"
(let (buffer-match-p)
(catch 'match
(dolist (element winpoint-non-restore-buffer-list)
(when (string-equal element bufname)
(setq buffer-match-p t)
(throw 'match "Match non restored buffer."))))
buffer-match-p))
(defun winpoint-get (win buf)
"Return a cons cell of the stored point of BUF in WIN."
(let ((window-entry (assq win winpoint-frame-positions)))
(when window-entry
(let ((buffer (assq buf (cdr window-entry))))
(when buffer
(cdr buffer))))))
(defun winpoint-put (win buf point)
"Store POINT as the current point for BUF in WIN."
(let ((window-entry (assq win winpoint-frame-positions)))
(if window-entry
(let ((buffer (assq buf (cdr window-entry))))
(if buffer
(set-marker (cdr buffer) point buf)
(setcdr window-entry
(cons `(,buf . ,(set-marker (make-marker)
point
buf))
(cdr window-entry)))))
(setq winpoint-frame-positions
(cons `(,win . ((,buf . ,(set-marker (make-marker)
point
buf))))
winpoint-frame-positions)))))
(defun winpoint-clean ()
"Remove unknown windows."
(let ((windows (window-list)))
(setq winpoint-frame-positions
(filter-map! (lambda (entry)
(let ((bufs (filter! (lambda (buf+pos)
(buffer-live-p (car buf+pos)))
(cdr entry))))
(when (not (null bufs))
(cons (car entry)
bufs))))
winpoint-frame-positions))))
(defun filter-map! (fun list)
"Map FUN over LIST, retaining all non-nil elements."
(while (and list
(not (setcar list (funcall fun (car list)))))
(setq list (cdr list)))
(when list
(let ((list list))
(while (cdr list)
(if (not (setcar (cdr list)
(funcall fun (cadr list))))
(setcdr list (cddr list))
(setq list (cdr list))))))
list)
(defun filter! (pred list)
"Return all elements of LIST for which PRED returns non-nil.
This modifies LIST whenever possible."
(while (and list
(not (funcall pred (car list))))
(setq list (cdr list)))
(when list
(let ((list list))
(while (cdr list)
(if (not (funcall pred (cadr list)))
(setcdr list (cddr list))
(setq list (cdr list))))))
list)
(provide 'winpoint)
;;; winpoint.el ends here
;;; LocalWords: winpoint Jorgen Schaefer buf newbuf defmacro bufname bufs pos
;;; LocalWords: pred