* lisp/gnus/*registry.el: Use slot names rather than initarg names
[emacs.git] / lisp / saveplace.el
blob4c53632affd5ca40b2a75c07128b14c8dd1111b8
1 ;;; saveplace.el --- automatically save place in files
3 ;; Copyright (C) 1993-1994, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Created: July, 1993
8 ;; Keywords: bookmarks, placeholders
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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.
33 ;; Thanks to Stefan Schoef, who sent a patch with the
34 ;; `save-place-version-control' stuff in it.
36 ;;; Code:
38 ;; this is what I was using during testing:
39 ;; (define-key ctl-x-map "p" 'toggle-save-place-globally)
41 (defgroup save-place nil
42 "Automatically save place in files."
43 :group 'data)
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.")
53 (defcustom save-place nil
54 "Non-nil means automatically save place in each file.
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.
58 If you wish your place in any file to always be automatically
59 saved, set this to t using the Customize facility, or put the
60 following code in your init file:
62 \(setq-default save-place t)
63 \(require 'saveplace)"
64 :type 'boolean
65 :require 'saveplace
66 :group 'save-place)
68 (make-variable-buffer-local 'save-place)
70 (defcustom save-place-file (locate-user-emacs-file "places" ".emacs-places")
71 "Name of the file that records `save-place-alist' value."
72 :version "24.4" ; added locate-user-emacs-file
73 :type 'file
74 :group 'save-place)
76 (defcustom save-place-version-control nil
77 "Controls whether to make numbered backups of master save-place file.
78 It can have four values: t, nil, `never', and `nospecial'. The first
79 three have the same meaning that they do for the variable
80 `version-control', and the final value `nospecial' means just use the
81 value of `version-control'."
82 :type '(radio (const :tag "Unconditionally" t)
83 (const :tag "For VC Files" nil)
84 (const never)
85 (const :tag "Use value of `version-control'" nospecial))
86 :group 'save-place)
88 (defvar save-place-loaded nil
89 "Non-nil means that the `save-place-file' has been loaded.")
91 (defcustom save-place-limit 400
92 "Maximum number of entries to retain in the list; nil means no limit."
93 :version "24.1" ; nil -> 400
94 :type '(choice (integer :tag "Entries" :value 1)
95 (const :tag "No Limit" nil))
96 :group 'save-place)
98 (defcustom save-place-forget-unreadable-files t
99 "Non-nil means forget place in unreadable files.
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.
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)
111 (defcustom save-place-save-skipped t
112 "If non-nil, remember files matching `save-place-skip-check-regexp'.
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)
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.
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'.
128 Files for which such a check may be inconvenient include those on
129 removable and network volumes."
130 :type 'regexp :group 'save-place)
132 (defcustom save-place-ignore-files-regexp
133 "\\(?:COMMIT_EDITMSG\\|hg-editor-[[:alnum:]]+\\.txt\\|svn-commit\\.tmp\\|bzr_log\\.[[:alnum:]]+\\)$"
134 "Regexp matching files for which no position should be recorded.
135 Useful for temporary file such as commit message files that are
136 automatically created by the VCS. If set to nil, this feature is
137 disabled, i.e., the position is recorded for all files."
138 :version "24.1"
139 :type 'regexp :group 'save-place)
141 (declare-function dired-current-directory "dired" (&optional localp))
143 (defun toggle-save-place (&optional parg)
144 "Toggle whether to save your place in this file between sessions.
145 If this mode is enabled, point is recorded when you kill the buffer
146 or exit Emacs. Visiting this file again will go to that position,
147 even in a later Emacs session.
149 If called with a prefix arg, the mode is enabled if and only if
150 the argument is positive.
152 To save places automatically in all files, put this in your init
153 file:
155 \(setq-default save-place t)"
156 (interactive "P")
157 (if (not (or buffer-file-name (and (derived-mode-p 'dired-mode)
158 (dired-current-directory))))
159 (message "Buffer `%s' not visiting a file or directory" (buffer-name))
160 (setq save-place (if parg
161 (> (prefix-numeric-value parg) 0)
162 (not save-place)))
163 (message (if save-place
164 "Place will be saved"
165 "No place will be saved in this file"))))
167 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
169 (defun save-place-to-alist ()
170 ;; put filename and point in a cons box and then cons that onto the
171 ;; front of the save-place-alist, if save-place is non-nil.
172 ;; Otherwise, just delete that file from the alist.
173 ;; first check to make sure alist has been loaded in from the master
174 ;; file. If not, do so, then feel free to modify the alist. It
175 ;; will be saved again when Emacs is killed.
176 (or save-place-loaded (load-save-place-alist-from-file))
177 (let* ((directory (and (derived-mode-p 'dired-mode)
178 (dired-current-directory)))
179 (item (or buffer-file-name
180 (and directory
181 (expand-file-name (if (consp directory)
182 (car directory)
183 directory))))))
184 (when (and item
185 (or (not save-place-ignore-files-regexp)
186 (not (string-match save-place-ignore-files-regexp
187 item))))
188 (let ((cell (assoc item save-place-alist))
189 (position (cond ((eq major-mode 'hexl-mode)
190 (with-no-warnings
191 (1+ (hexl-current-address))))
192 ((and (derived-mode-p 'dired-mode) directory)
193 (let ((filename (dired-get-filename nil t)))
194 (if filename
195 `((dired-filename . ,filename))
196 (point))))
197 (t (point)))))
198 (if cell
199 (setq save-place-alist (delq cell save-place-alist)))
200 (if (and save-place
201 (not (and (integerp position)
202 (= position 1)))) ;; Optimize out the degenerate case.
203 (setq save-place-alist
204 (cons (cons item position)
205 save-place-alist)))))))
207 (defun save-place-forget-unreadable-files ()
208 "Remove unreadable files from `save-place-alist'.
209 For each entry in the alist, if `file-readable-p' returns nil for the
210 filename, remove the entry. Save the new alist (as the first pair
211 may have changed) back to `save-place-alist'."
212 (interactive)
213 ;; the following was adapted from an in-place filtering function,
214 ;; `filter-mod', used in the original.
215 (unless (null save-place-alist) ;says it better than `when'
216 ;; first, check all except first
217 (let ((fmprev save-place-alist) (fmcur (cdr save-place-alist)))
218 (while fmcur ;not null
219 ;; a value is only saved when it becomes FMPREV.
220 (if (if (string-match save-place-skip-check-regexp (caar fmcur))
221 save-place-save-skipped
222 (file-readable-p (caar fmcur)))
223 (setq fmprev fmcur)
224 (setcdr fmprev (cdr fmcur)))
225 (setq fmcur (cdr fmcur))))
226 ;; test first pair, keep it if OK, otherwise 2nd element, which
227 ;; may be '()
228 (unless (if (string-match save-place-skip-check-regexp
229 (caar save-place-alist))
230 save-place-save-skipped
231 (file-readable-p (caar save-place-alist)))
232 (setq save-place-alist (cdr save-place-alist)))))
234 (defun save-place-alist-to-file ()
235 (let ((file (expand-file-name save-place-file))
236 (coding-system-for-write 'utf-8))
237 (with-current-buffer (get-buffer-create " *Saved Places*")
238 (delete-region (point-min) (point-max))
239 (when save-place-forget-unreadable-files
240 (save-place-forget-unreadable-files))
241 (insert (format ";;; -*- coding: %s -*-\n"
242 (symbol-name coding-system-for-write)))
243 (let ((print-length nil)
244 (print-level nil))
245 (pp save-place-alist (current-buffer)))
246 (let ((version-control
247 (cond
248 ((null save-place-version-control) nil)
249 ((eq 'never save-place-version-control) 'never)
250 ((eq 'nospecial save-place-version-control) version-control)
252 t))))
253 (condition-case nil
254 ;; Don't use write-file; we don't want this buffer to visit it.
255 (write-region (point-min) (point-max) file)
256 (file-error (message "Saving places: can't write %s" file)))
257 (kill-buffer (current-buffer))))))
259 (defun load-save-place-alist-from-file ()
260 (if (not save-place-loaded)
261 (progn
262 (setq save-place-loaded t)
263 (let ((file (expand-file-name save-place-file)))
264 ;; make sure that the alist does not get overwritten, and then
265 ;; load it if it exists:
266 (if (file-readable-p file)
267 ;; don't want to use find-file because we have been
268 ;; adding hooks to it.
269 (with-current-buffer (get-buffer-create " *Saved Places*")
270 (delete-region (point-min) (point-max))
271 (insert-file-contents file)
272 (goto-char (point-min))
273 (setq save-place-alist
274 (with-demoted-errors "Error reading save-place-file: %S"
275 (car (read-from-string
276 (buffer-substring (point-min) (point-max))))))
278 ;; If there is a limit, and we're over it, then we'll
279 ;; have to truncate the end of the list:
280 (if save-place-limit
281 (if (<= save-place-limit 0)
282 ;; Zero gets special cased. I'm not thrilled
283 ;; with this, but the loop for >= 1 is tight.
284 (setq save-place-alist nil)
285 ;; Else the limit is >= 1, so enforce it by
286 ;; counting and then `setcdr'ing.
287 (let ((s save-place-alist)
288 (count 1))
289 (while s
290 (if (>= count save-place-limit)
291 (setcdr s nil)
292 (setq count (1+ count)))
293 (setq s (cdr s))))))
295 (kill-buffer (current-buffer))))
296 nil))))
298 (defun save-places-to-alist ()
299 ;; go through buffer-list, saving places to alist if save-place is
300 ;; non-nil, deleting them from alist if it is nil.
301 (let ((buf-list (buffer-list)))
302 (while buf-list
303 ;; put this into a save-excursion in case someone is counting on
304 ;; another function in kill-emacs-hook to act on the last buffer
305 ;; they were in:
306 (with-current-buffer (car buf-list)
307 ;; save-place checks buffer-file-name too, but we can avoid
308 ;; overhead of function call by checking here too.
309 (and (or buffer-file-name (and (derived-mode-p 'dired-mode)
310 (dired-current-directory)))
311 (save-place-to-alist))
312 (setq buf-list (cdr buf-list))))))
314 (defun save-place-find-file-hook ()
315 (or save-place-loaded (load-save-place-alist-from-file))
316 (let ((cell (assoc buffer-file-name save-place-alist)))
317 (if cell
318 (progn
319 (or revert-buffer-in-progress-p
320 (and (integerp (cdr cell))
321 (goto-char (cdr cell))))
322 ;; and make sure it will be saved again for later
323 (setq save-place t)))))
325 (declare-function dired-goto-file "dired" (file))
327 (defun save-place-dired-hook ()
328 "Position the point in a Dired buffer."
329 (or save-place-loaded (load-save-place-alist-from-file))
330 (let* ((directory (and (derived-mode-p 'dired-mode)
331 (dired-current-directory)))
332 (cell (assoc (and directory
333 (expand-file-name (if (consp directory)
334 (car directory)
335 directory)))
336 save-place-alist)))
337 (if cell
338 (progn
339 (or revert-buffer-in-progress-p
340 (cond
341 ((integerp (cdr cell))
342 (goto-char (cdr cell)))
343 ((and (listp (cdr cell)) (assq 'dired-filename (cdr cell)))
344 (dired-goto-file (cdr (assq 'dired-filename (cdr cell)))))))
345 ;; and make sure it will be saved again for later
346 (setq save-place t)))))
348 (defun save-place-kill-emacs-hook ()
349 ;; First update the alist. This loads the old save-place-file if nec.
350 (save-places-to-alist)
351 ;; Now save the alist in the file, if we have ever loaded the file
352 ;; (including just now).
353 (if save-place-loaded
354 (save-place-alist-to-file)))
356 (add-hook 'find-file-hook 'save-place-find-file-hook t)
358 (add-hook 'dired-initial-position-hook 'save-place-dired-hook)
360 (unless noninteractive
361 (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
363 (add-hook 'kill-buffer-hook 'save-place-to-alist)
365 (provide 'saveplace) ; why not...
367 ;;; saveplace.el ends here