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