Replace "Maintainer: FSF" with the emacs-devel mailing address
[bpt/emacs.git] / lisp / saveplace.el
CommitLineData
55535639 1;;; saveplace.el --- automatically save place in files
722074ef 2
ba318903 3;; Copyright (C) 1993-1994, 2001-2014 Free Software Foundation, Inc.
722074ef 4
19a448e3 5;; Author: Karl Fogel <kfogel@red-bean.com>
34dc21db 6;; Maintainer: emacs-devel@gnu.org
722074ef 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.
48Each element looks like (FILENAME . POSITION);
49visiting file FILENAME goes automatically to position POSITION
50rather than the beginning of the buffer.
51This 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
55This means when you visit a file, point goes to the last place
56where it was when you previously visited the same file.
57This variable is automatically buffer-local.
58
865fe16f
CY
59If you wish your place in any file to always be automatically
60saved, set this to t using the Customize facility, or put the
61following code in your init file:
722074ef 62
c73dca25 63\(setq-default save-place t)
865fe16f 64\(require 'saveplace)"
20606f3d 65 :type 'boolean
d749f559 66 :require 'saveplace
20606f3d 67 :group 'save-place)
722074ef
RS
68
69(make-variable-buffer-local 'save-place)
70
940e5099 71(defcustom save-place-file (locate-user-emacs-file "places" ".emacs-places")
9201cc28 72 "Name of the file that records `save-place-alist' value."
ece4bae5 73 :version "24.4" ; added locate-user-emacs-file
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
79It can have four values: t, nil, `never', and `nospecial'. The first
80three have the same meaning that they do for the variable
81`version-control', and the final value `nospecial' means just use the
20606f3d
RS
82value 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
2f6a3e79 92(defcustom save-place-limit 400
20606f3d 93 "Maximum number of entries to retain in the list; nil means no limit."
2f6a3e79 94 :version "24.1" ; nil -> 400
20606f3d
RS
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
0311a3fc
TH
133(defcustom save-place-ignore-files-regexp
134 "\\(?:COMMIT_EDITMSG\\|hg-editor-[[:alnum:]]+\\.txt\\|svn-commit\\.tmp\\|bzr_log\\.[[:alnum:]]+\\)$"
5dd11cfe 135 "Regexp matching files for which no position should be recorded.
0311a3fc 136Useful for temporary file such as commit message files that are
5dd11cfe
TH
137automatically created by the VCS. If set to nil, this feature is
138disabled, i.e., the position is recorded for all files."
e23a3fbe 139 :version "24.1"
0311a3fc
TH
140 :type 'regexp :group 'save-place)
141
722074ef
RS
142(defun toggle-save-place (&optional parg)
143 "Toggle whether to save your place in this file between sessions.
144If this mode is enabled, point is recorded when you kill the buffer
145or exit Emacs. Visiting this file again will go to that position,
146even in a later Emacs session.
147
148If called with a prefix arg, the mode is enabled if and only if
149the argument is positive.
150
865fe16f
CY
151To save places automatically in all files, put this in your init
152file:
722074ef
RS
153
154\(setq-default save-place t\)"
155 (interactive "P")
71f16bda
JL
156 (if (not (or buffer-file-name (and (derived-mode-p 'dired-mode)
157 dired-directory)))
1d42e5b6 158 (message "Buffer `%s' not visiting a file or directory" (buffer-name))
722074ef
RS
159 (if (and save-place (or (not parg) (<= parg 0)))
160 (progn
44a75d48 161 (message "No place will be saved in this file")
722074ef 162 (setq save-place nil))
44a75d48 163 (message "Place will be saved")
722074ef
RS
164 (setq save-place t))))
165
1d42e5b6
JL
166(declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
167
722074ef
RS
168(defun save-place-to-alist ()
169 ;; put filename and point in a cons box and then cons that onto the
170 ;; front of the save-place-alist, if save-place is non-nil.
171 ;; Otherwise, just delete that file from the alist.
172 ;; first check to make sure alist has been loaded in from the master
173 ;; file. If not, do so, then feel free to modify the alist. It
174 ;; will be saved again when Emacs is killed.
175 (or save-place-loaded (load-save-place-alist-from-file))
d4466a91 176 (let ((item (or buffer-file-name
71f16bda
JL
177 (and (derived-mode-p 'dired-mode)
178 dired-directory
179 (expand-file-name (if (consp dired-directory)
180 (car dired-directory)
181 dired-directory))))))
d4466a91
IK
182 (when (and item
183 (or (not save-place-ignore-files-regexp)
184 (not (string-match save-place-ignore-files-regexp
185 item))))
186 (let ((cell (assoc item save-place-alist))
1d42e5b6
JL
187 (position (cond ((eq major-mode 'hexl-mode)
188 (with-no-warnings
189 (1+ (hexl-current-address))))
71f16bda
JL
190 ((and (derived-mode-p 'dired-mode)
191 dired-directory)
1d42e5b6
JL
192 (let ((filename (dired-get-filename nil t)))
193 (if filename
194 `((dired-filename . ,filename))
195 (point))))
196 (t (point)))))
d4466a91
IK
197 (if cell
198 (setq save-place-alist (delq cell save-place-alist)))
199 (if (and save-place
1d42e5b6
JL
200 (not (and (integerp position)
201 (= position 1)))) ;; Optimize out the degenerate case.
d4466a91
IK
202 (setq save-place-alist
203 (cons (cons item position)
204 save-place-alist)))))))
722074ef 205
467997b7
EZ
206(defun save-place-forget-unreadable-files ()
207 "Remove unreadable files from `save-place-alist'.
208For each entry in the alist, if `file-readable-p' returns nil for the
209filename, remove the entry. Save the new alist \(as the first pair
210may have changed\) back to `save-place-alist'."
211 (interactive)
212 ;; the following was adapted from an in-place filtering function,
213 ;; `filter-mod', used in the original.
214 (unless (null save-place-alist) ;says it better than `when'
215 ;; first, check all except first
216 (let ((fmprev save-place-alist) (fmcur (cdr save-place-alist)))
217 (while fmcur ;not null
218 ;; a value is only saved when it becomes FMPREV.
219 (if (if (string-match save-place-skip-check-regexp (caar fmcur))
220 save-place-save-skipped
221 (file-readable-p (caar fmcur)))
222 (setq fmprev fmcur)
223 (setcdr fmprev (cdr fmcur)))
224 (setq fmcur (cdr fmcur))))
225 ;; test first pair, keep it if OK, otherwise 2nd element, which
226 ;; may be '()
227 (unless (if (string-match save-place-skip-check-regexp
228 (caar save-place-alist))
229 save-place-save-skipped
230 (file-readable-p (caar save-place-alist)))
231 (setq save-place-alist (cdr save-place-alist)))))
232
722074ef 233(defun save-place-alist-to-file ()
e1970249
KF
234 (let ((file (expand-file-name save-place-file))
235 (coding-system-for-write 'utf-8))
7fdbcd83 236 (with-current-buffer (get-buffer-create " *Saved Places*")
722074ef 237 (delete-region (point-min) (point-max))
467997b7
EZ
238 (when save-place-forget-unreadable-files
239 (save-place-forget-unreadable-files))
e1970249
KF
240 (insert (format ";;; -*- coding: %s -*-\n"
241 (symbol-name coding-system-for-write)))
5f29a6c8
KF
242 (let ((print-length nil)
243 (print-level nil))
1c4a85ed 244 (pp save-place-alist (current-buffer)))
e7dc77f6
RS
245 (let ((version-control
246 (cond
247 ((null save-place-version-control) nil)
248 ((eq 'never save-place-version-control) 'never)
249 ((eq 'nospecial save-place-version-control) version-control)
250 (t
251 t))))
6b98170f 252 (condition-case nil
cfde584f 253 ;; Don't use write-file; we don't want this buffer to visit it.
e1970249 254 (write-region (point-min) (point-max) file)
bedb08d4
KF
255 (file-error (message "Saving places: can't write %s" file)))
256 (kill-buffer (current-buffer))))))
722074ef
RS
257
258(defun load-save-place-alist-from-file ()
259 (if (not save-place-loaded)
260 (progn
261 (setq save-place-loaded t)
262 (let ((file (expand-file-name save-place-file)))
263 ;; make sure that the alist does not get overwritten, and then
264 ;; load it if it exists:
265 (if (file-readable-p file)
7fdbcd83
SM
266 ;; don't want to use find-file because we have been
267 ;; adding hooks to it.
268 (with-current-buffer (get-buffer-create " *Saved Places*")
722074ef
RS
269 (delete-region (point-min) (point-max))
270 (insert-file-contents file)
271 (goto-char (point-min))
f1180544 272 (setq save-place-alist
30213927 273 (with-demoted-errors "Error reading save-place-file: %S"
144e38fe 274 (car (read-from-string
30213927 275 (buffer-substring (point-min) (point-max))))))
6cf02570
RS
276
277 ;; If there is a limit, and we're over it, then we'll
278 ;; have to truncate the end of the list:
279 (if save-place-limit
280 (if (<= save-place-limit 0)
281 ;; Zero gets special cased. I'm not thrilled
282 ;; with this, but the loop for >= 1 is tight.
283 (setq save-place-alist nil)
284 ;; Else the limit is >= 1, so enforce it by
285 ;; counting and then `setcdr'ing.
286 (let ((s save-place-alist)
287 (count 1))
288 (while s
289 (if (>= count save-place-limit)
290 (setcdr s nil)
291 (setq count (1+ count)))
292 (setq s (cdr s))))))
f1180544 293
bedb08d4 294 (kill-buffer (current-buffer))))
722074ef
RS
295 nil))))
296
297(defun save-places-to-alist ()
298 ;; go through buffer-list, saving places to alist if save-place is
299 ;; non-nil, deleting them from alist if it is nil.
300 (let ((buf-list (buffer-list)))
301 (while buf-list
302 ;; put this into a save-excursion in case someone is counting on
303 ;; another function in kill-emacs-hook to act on the last buffer
304 ;; they were in:
7fdbcd83 305 (with-current-buffer (car buf-list)
722074ef
RS
306 ;; save-place checks buffer-file-name too, but we can avoid
307 ;; overhead of function call by checking here too.
71f16bda
JL
308 (and (or buffer-file-name (and (derived-mode-p 'dired-mode)
309 dired-directory))
1d42e5b6 310 (save-place-to-alist))
722074ef 311 (setq buf-list (cdr buf-list))))))
b6ace927
RS
312
313(defun save-place-find-file-hook ()
314 (or save-place-loaded (load-save-place-alist-from-file))
315 (let ((cell (assoc buffer-file-name save-place-alist)))
316 (if cell
6f96f4c9 317 (progn
3e214b50 318 (or revert-buffer-in-progress-p
1d42e5b6
JL
319 (and (integerp (cdr cell))
320 (goto-char (cdr cell))))
b6ace927
RS
321 ;; and make sure it will be saved again for later
322 (setq save-place t)))))
323
1d42e5b6
JL
324(declare-function dired-goto-file "dired" (file))
325
d4466a91
IK
326(defun save-place-dired-hook ()
327 "Position the point in a dired buffer."
328 (or save-place-loaded (load-save-place-alist-from-file))
71f16bda
JL
329 (let ((cell (assoc (and (derived-mode-p 'dired-mode)
330 dired-directory
331 (expand-file-name (if (consp dired-directory)
332 (car dired-directory)
333 dired-directory)))
1d42e5b6 334 save-place-alist)))
d4466a91
IK
335 (if cell
336 (progn
337 (or revert-buffer-in-progress-p
1d42e5b6
JL
338 (if (integerp (cdr cell))
339 (goto-char (cdr cell))
340 (and (assq 'dired-filename (cdr cell))
341 (dired-goto-file (cdr (assq 'dired-filename (cdr cell)))))))
d4466a91
IK
342 ;; and make sure it will be saved again for later
343 (setq save-place t)))))
344
4d2c9a10 345(defun save-place-kill-emacs-hook ()
18b1fecd
RS
346 ;; First update the alist. This loads the old save-place-file if nec.
347 (save-places-to-alist)
348 ;; Now save the alist in the file, if we have ever loaded the file
349 ;; (including just now).
52b9a931 350 (if save-place-loaded
18b1fecd 351 (save-place-alist-to-file)))
b6ace927 352
1a4914f3 353(add-hook 'find-file-hook 'save-place-find-file-hook t)
b6ace927 354
1d42e5b6
JL
355(add-hook 'dired-initial-position-hook 'save-place-dired-hook)
356
845fc5e5
JB
357(unless noninteractive
358 (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
722074ef
RS
359
360(add-hook 'kill-buffer-hook 'save-place-to-alist)
361
362(provide 'saveplace) ; why not...
363
364;;; saveplace.el ends here