Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; saveplace.el --- automatically save place in files |
722074ef | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1993-1994, 2001-2011 Free Software Foundation, Inc. |
722074ef | 4 | |
19a448e3 | 5 | ;; Author: Karl Fogel <kfogel@red-bean.com> |
722074ef RS |
6 | ;; Maintainer: FSF |
7 | ;; Created: July, 1993 | |
722074ef RS |
8 | ;; Keywords: bookmarks, placeholders |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
722074ef | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
722074ef RS |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
b578f267 | 24 | |
6f96f4c9 RS |
25 | ;;; Commentary: |
26 | ||
722074ef RS |
27 | ;; Automatically save place in files, so that visiting them later |
28 | ;; (even during a different Emacs session) automatically moves point | |
29 | ;; to the saved position, when the file is first found. Uses the | |
30 | ;; value of buffer-local variable save-place to determine whether to | |
31 | ;; save position or not. | |
32 | ;; | |
e7dc77f6 RS |
33 | ;; Thanks to Stefan Schoef, who sent a patch with the |
34 | ;; `save-place-version-control' stuff in it. | |
722074ef | 35 | |
6f96f4c9 RS |
36 | ;;; Code: |
37 | ||
722074ef | 38 | ;; this is what I was using during testing: |
afa29d95 | 39 | ;; (define-key ctl-x-map "p" 'toggle-save-place-globally) |
722074ef | 40 | |
20606f3d RS |
41 | (defgroup save-place nil |
42 | "Automatically save place in files." | |
43 | :group 'data) | |
44 | ||
45 | ||
722074ef RS |
46 | (defvar save-place-alist nil |
47 | "Alist of saved places to go back to when revisiting files. | |
48 | Each element looks like (FILENAME . POSITION); | |
49 | visiting file FILENAME goes automatically to position POSITION | |
50 | rather than the beginning of the buffer. | |
51 | This alist is saved between Emacs sessions.") | |
52 | ||
20606f3d | 53 | (defcustom save-place nil |
9201cc28 | 54 | "Non-nil means automatically save place in each file. |
722074ef RS |
55 | This means when you visit a file, point goes to the last place |
56 | where it was when you previously visited the same file. | |
57 | This variable is automatically buffer-local. | |
58 | ||
59 | If you wish your place in any file to always be automatically saved, | |
60 | simply put this in your `~/.emacs' file: | |
61 | ||
c73dca25 RS |
62 | \(setq-default save-place t) |
63 | \(require 'saveplace) | |
64 | ||
65 | or else use the Custom facility to set this option." | |
20606f3d | 66 | :type 'boolean |
d749f559 | 67 | :require 'saveplace |
20606f3d | 68 | :group 'save-place) |
722074ef RS |
69 | |
70 | (make-variable-buffer-local 'save-place) | |
71 | ||
20606f3d | 72 | (defcustom save-place-file (convert-standard-filename "~/.emacs-places") |
9201cc28 | 73 | "Name of the file that records `save-place-alist' value." |
20606f3d RS |
74 | :type 'file |
75 | :group 'save-place) | |
722074ef | 76 | |
a4ce5ede | 77 | (defcustom save-place-version-control nil |
9201cc28 | 78 | "Controls whether to make numbered backups of master save-place file. |
e7dc77f6 RS |
79 | It can have four values: t, nil, `never', and `nospecial'. The first |
80 | three have the same meaning that they do for the variable | |
81 | `version-control', and the final value `nospecial' means just use the | |
20606f3d RS |
82 | value of `version-control'." |
83 | :type '(radio (const :tag "Unconditionally" t) | |
84 | (const :tag "For VC Files" nil) | |
85 | (const never) | |
a4ce5ede | 86 | (const :tag "Use value of `version-control'" nospecial)) |
20606f3d | 87 | :group 'save-place) |
e7dc77f6 | 88 | |
722074ef RS |
89 | (defvar save-place-loaded nil |
90 | "Non-nil means that the `save-place-file' has been loaded.") | |
91 | ||
20606f3d RS |
92 | (defcustom save-place-limit nil |
93 | "Maximum number of entries to retain in the list; nil means no limit." | |
94 | :type '(choice (integer :tag "Entries" :value 1) | |
95 | (const :tag "No Limit" nil)) | |
96 | :group 'save-place) | |
6cf02570 | 97 | |
467997b7 EZ |
98 | (defcustom save-place-forget-unreadable-files t |
99 | "Non-nil means forget place in unreadable files. | |
100 | ||
101 | The filenames in `save-place-alist' that do not match | |
102 | `save-place-skip-check-regexp' are filtered through | |
103 | `file-readable-p'. if nil, their alist entries are removed. | |
104 | ||
105 | You may do this anytime by calling the complementary function, | |
106 | `save-place-forget-unreadable-files'. When this option is turned on, | |
107 | this happens automatically before saving `save-place-alist' to | |
108 | `save-place-file'." | |
109 | :type 'boolean :group 'save-place) | |
110 | ||
111 | (defcustom save-place-save-skipped t | |
112 | "If non-nil, remember files matching `save-place-skip-check-regexp'. | |
113 | ||
114 | When filtering `save-place-alist' for unreadable files, some will not | |
115 | be checked, based on said regexp, and instead saved or forgotten based | |
116 | on this flag." | |
117 | :type 'boolean :group 'save-place) | |
118 | ||
119 | (defcustom save-place-skip-check-regexp | |
120 | ;; thanks to ange-ftp-name-format | |
121 | "\\`/\\(?:cdrom\\|floppy\\|mnt\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)" | |
122 | "Regexp whose file names shall not be checked for readability. | |
123 | ||
124 | When forgetting unreadable files, file names matching this regular | |
125 | expression shall not be checked for readability, but instead be | |
126 | subject to `save-place-save-skipped'. | |
127 | ||
128 | Files for which such a check may be inconvenient include those on | |
129 | removable and network volumes." | |
130 | :type 'regexp :group 'save-place) | |
131 | ||
722074ef RS |
132 | (defun toggle-save-place (&optional parg) |
133 | "Toggle whether to save your place in this file between sessions. | |
134 | If this mode is enabled, point is recorded when you kill the buffer | |
135 | or exit Emacs. Visiting this file again will go to that position, | |
136 | even in a later Emacs session. | |
137 | ||
138 | If called with a prefix arg, the mode is enabled if and only if | |
139 | the argument is positive. | |
140 | ||
141 | To save places automatically in all files, put this in your `.emacs' file: | |
142 | ||
143 | \(setq-default save-place t\)" | |
144 | (interactive "P") | |
145 | (if (not buffer-file-name) | |
44a75d48 | 146 | (message "Buffer `%s' not visiting a file" (buffer-name)) |
722074ef RS |
147 | (if (and save-place (or (not parg) (<= parg 0))) |
148 | (progn | |
44a75d48 | 149 | (message "No place will be saved in this file") |
722074ef | 150 | (setq save-place nil)) |
44a75d48 | 151 | (message "Place will be saved") |
722074ef RS |
152 | (setq save-place t)))) |
153 | ||
154 | (defun save-place-to-alist () | |
155 | ;; put filename and point in a cons box and then cons that onto the | |
156 | ;; front of the save-place-alist, if save-place is non-nil. | |
157 | ;; Otherwise, just delete that file from the alist. | |
158 | ;; first check to make sure alist has been loaded in from the master | |
159 | ;; file. If not, do so, then feel free to modify the alist. It | |
160 | ;; will be saved again when Emacs is killed. | |
161 | (or save-place-loaded (load-save-place-alist-from-file)) | |
162 | (if buffer-file-name | |
163 | (progn | |
a59f6ba3 KH |
164 | (let ((cell (assoc buffer-file-name save-place-alist)) |
165 | (position (if (not (eq major-mode 'hexl-mode)) | |
166 | (point) | |
06e24ef7 RS |
167 | (with-no-warnings |
168 | (1+ (hexl-current-address)))))) | |
722074ef | 169 | (if cell |
a59f6ba3 KH |
170 | (setq save-place-alist (delq cell save-place-alist))) |
171 | (if (and save-place | |
172 | (not (= position 1))) ;; Optimize out the degenerate case. | |
173 | (setq save-place-alist | |
174 | (cons (cons buffer-file-name position) | |
175 | save-place-alist))))))) | |
722074ef | 176 | |
467997b7 EZ |
177 | (defun save-place-forget-unreadable-files () |
178 | "Remove unreadable files from `save-place-alist'. | |
179 | For each entry in the alist, if `file-readable-p' returns nil for the | |
180 | filename, remove the entry. Save the new alist \(as the first pair | |
181 | may have changed\) back to `save-place-alist'." | |
182 | (interactive) | |
183 | ;; the following was adapted from an in-place filtering function, | |
184 | ;; `filter-mod', used in the original. | |
185 | (unless (null save-place-alist) ;says it better than `when' | |
186 | ;; first, check all except first | |
187 | (let ((fmprev save-place-alist) (fmcur (cdr save-place-alist))) | |
188 | (while fmcur ;not null | |
189 | ;; a value is only saved when it becomes FMPREV. | |
190 | (if (if (string-match save-place-skip-check-regexp (caar fmcur)) | |
191 | save-place-save-skipped | |
192 | (file-readable-p (caar fmcur))) | |
193 | (setq fmprev fmcur) | |
194 | (setcdr fmprev (cdr fmcur))) | |
195 | (setq fmcur (cdr fmcur)))) | |
196 | ;; test first pair, keep it if OK, otherwise 2nd element, which | |
197 | ;; may be '() | |
198 | (unless (if (string-match save-place-skip-check-regexp | |
199 | (caar save-place-alist)) | |
200 | save-place-save-skipped | |
201 | (file-readable-p (caar save-place-alist))) | |
202 | (setq save-place-alist (cdr save-place-alist))))) | |
203 | ||
722074ef | 204 | (defun save-place-alist-to-file () |
e1970249 KF |
205 | (let ((file (expand-file-name save-place-file)) |
206 | (coding-system-for-write 'utf-8)) | |
7fdbcd83 | 207 | (with-current-buffer (get-buffer-create " *Saved Places*") |
722074ef | 208 | (delete-region (point-min) (point-max)) |
467997b7 EZ |
209 | (when save-place-forget-unreadable-files |
210 | (save-place-forget-unreadable-files)) | |
e1970249 KF |
211 | (insert (format ";;; -*- coding: %s -*-\n" |
212 | (symbol-name coding-system-for-write))) | |
5f29a6c8 KF |
213 | (let ((print-length nil) |
214 | (print-level nil)) | |
6e457cc9 KF |
215 | (pp (sort save-place-alist |
216 | (lambda (a b) (string< (car a) (car b)))) | |
217 | (current-buffer))) | |
e7dc77f6 RS |
218 | (let ((version-control |
219 | (cond | |
220 | ((null save-place-version-control) nil) | |
221 | ((eq 'never save-place-version-control) 'never) | |
222 | ((eq 'nospecial save-place-version-control) version-control) | |
223 | (t | |
224 | t)))) | |
6b98170f | 225 | (condition-case nil |
cfde584f | 226 | ;; Don't use write-file; we don't want this buffer to visit it. |
e1970249 | 227 | (write-region (point-min) (point-max) file) |
bedb08d4 KF |
228 | (file-error (message "Saving places: can't write %s" file))) |
229 | (kill-buffer (current-buffer)))))) | |
722074ef RS |
230 | |
231 | (defun load-save-place-alist-from-file () | |
232 | (if (not save-place-loaded) | |
233 | (progn | |
234 | (setq save-place-loaded t) | |
235 | (let ((file (expand-file-name save-place-file))) | |
236 | ;; make sure that the alist does not get overwritten, and then | |
237 | ;; load it if it exists: | |
238 | (if (file-readable-p file) | |
7fdbcd83 SM |
239 | ;; don't want to use find-file because we have been |
240 | ;; adding hooks to it. | |
241 | (with-current-buffer (get-buffer-create " *Saved Places*") | |
722074ef RS |
242 | (delete-region (point-min) (point-max)) |
243 | (insert-file-contents file) | |
244 | (goto-char (point-min)) | |
f1180544 | 245 | (setq save-place-alist |
722074ef RS |
246 | (car (read-from-string |
247 | (buffer-substring (point-min) (point-max))))) | |
6cf02570 RS |
248 | |
249 | ;; If there is a limit, and we're over it, then we'll | |
250 | ;; have to truncate the end of the list: | |
251 | (if save-place-limit | |
252 | (if (<= save-place-limit 0) | |
253 | ;; Zero gets special cased. I'm not thrilled | |
254 | ;; with this, but the loop for >= 1 is tight. | |
255 | (setq save-place-alist nil) | |
256 | ;; Else the limit is >= 1, so enforce it by | |
257 | ;; counting and then `setcdr'ing. | |
258 | (let ((s save-place-alist) | |
259 | (count 1)) | |
260 | (while s | |
261 | (if (>= count save-place-limit) | |
262 | (setcdr s nil) | |
263 | (setq count (1+ count))) | |
264 | (setq s (cdr s)))))) | |
f1180544 | 265 | |
bedb08d4 | 266 | (kill-buffer (current-buffer)))) |
722074ef RS |
267 | nil)))) |
268 | ||
269 | (defun save-places-to-alist () | |
270 | ;; go through buffer-list, saving places to alist if save-place is | |
271 | ;; non-nil, deleting them from alist if it is nil. | |
272 | (let ((buf-list (buffer-list))) | |
273 | (while buf-list | |
274 | ;; put this into a save-excursion in case someone is counting on | |
275 | ;; another function in kill-emacs-hook to act on the last buffer | |
276 | ;; they were in: | |
7fdbcd83 | 277 | (with-current-buffer (car buf-list) |
722074ef RS |
278 | ;; save-place checks buffer-file-name too, but we can avoid |
279 | ;; overhead of function call by checking here too. | |
280 | (and buffer-file-name (save-place-to-alist)) | |
281 | (setq buf-list (cdr buf-list)))))) | |
b6ace927 RS |
282 | |
283 | (defun save-place-find-file-hook () | |
284 | (or save-place-loaded (load-save-place-alist-from-file)) | |
285 | (let ((cell (assoc buffer-file-name save-place-alist))) | |
286 | (if cell | |
6f96f4c9 | 287 | (progn |
3e214b50 | 288 | (or revert-buffer-in-progress-p |
6f96f4c9 | 289 | (goto-char (cdr cell))) |
b6ace927 RS |
290 | ;; and make sure it will be saved again for later |
291 | (setq save-place t))))) | |
292 | ||
4d2c9a10 | 293 | (defun save-place-kill-emacs-hook () |
18b1fecd RS |
294 | ;; First update the alist. This loads the old save-place-file if nec. |
295 | (save-places-to-alist) | |
296 | ;; Now save the alist in the file, if we have ever loaded the file | |
297 | ;; (including just now). | |
52b9a931 | 298 | (if save-place-loaded |
18b1fecd | 299 | (save-place-alist-to-file))) |
b6ace927 | 300 | |
1a4914f3 | 301 | (add-hook 'find-file-hook 'save-place-find-file-hook t) |
b6ace927 | 302 | |
845fc5e5 JB |
303 | (unless noninteractive |
304 | (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook)) | |
722074ef RS |
305 | |
306 | (add-hook 'kill-buffer-hook 'save-place-to-alist) | |
307 | ||
308 | (provide 'saveplace) ; why not... | |
309 | ||
310 | ;;; saveplace.el ends here |