* doc/misc/mh-e.texi: Replace another `:' in an index entry.
[emacs/old-mirror.git] / lisp / saveplace.el
blob91da103ba9e4dced3f13a2c476b7861c2358da2d
1 ;;; saveplace.el --- automatically save place in files
3 ;; Copyright (C) 1993-1994, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: FSF
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.
57 This variable is automatically buffer-local.
59 If you wish your place in any file to always be automatically
60 saved, set this to t using the Customize facility, or put the
61 following code in your init file:
63 \(setq-default save-place t)
64 \(require 'saveplace)"
65 :type 'boolean
66 :require 'saveplace
67 :group 'save-place)
69 (make-variable-buffer-local 'save-place)
71 (defcustom save-place-file (locate-user-emacs-file "places" ".emacs-places")
72 "Name of the file that records `save-place-alist' value."
73 :version "24.4" ; added locate-user-emacs-file
74 :type 'file
75 :group 'save-place)
77 (defcustom save-place-version-control nil
78 "Controls whether to make numbered backups of master save-place file.
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
82 value of `version-control'."
83 :type '(radio (const :tag "Unconditionally" t)
84 (const :tag "For VC Files" nil)
85 (const never)
86 (const :tag "Use value of `version-control'" nospecial))
87 :group 'save-place)
89 (defvar save-place-loaded nil
90 "Non-nil means that the `save-place-file' has been loaded.")
92 (defcustom save-place-limit 400
93 "Maximum number of entries to retain in the list; nil means no limit."
94 :version "24.1" ; nil -> 400
95 :type '(choice (integer :tag "Entries" :value 1)
96 (const :tag "No Limit" nil))
97 :group 'save-place)
99 (defcustom save-place-forget-unreadable-files t
100 "Non-nil means forget place in unreadable files.
102 The 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.
106 You may do this anytime by calling the complementary function,
107 `save-place-forget-unreadable-files'. When this option is turned on,
108 this happens automatically before saving `save-place-alist' to
109 `save-place-file'."
110 :type 'boolean :group 'save-place)
112 (defcustom save-place-save-skipped t
113 "If non-nil, remember files matching `save-place-skip-check-regexp'.
115 When filtering `save-place-alist' for unreadable files, some will not
116 be checked, based on said regexp, and instead saved or forgotten based
117 on this flag."
118 :type 'boolean :group 'save-place)
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.
125 When forgetting unreadable files, file names matching this regular
126 expression shall not be checked for readability, but instead be
127 subject to `save-place-save-skipped'.
129 Files for which such a check may be inconvenient include those on
130 removable and network volumes."
131 :type 'regexp :group 'save-place)
133 (defcustom save-place-ignore-files-regexp
134 "\\(?:COMMIT_EDITMSG\\|hg-editor-[[:alnum:]]+\\.txt\\|svn-commit\\.tmp\\|bzr_log\\.[[:alnum:]]+\\)$"
135 "Regexp matching files for which no position should be recorded.
136 Useful for temporary file such as commit message files that are
137 automatically created by the VCS. If set to nil, this feature is
138 disabled, i.e., the position is recorded for all files."
139 :version "24.1"
140 :type 'regexp :group 'save-place)
142 (defun toggle-save-place (&optional parg)
143 "Toggle whether to save your place in this file between sessions.
144 If this mode is enabled, point is recorded when you kill the buffer
145 or exit Emacs. Visiting this file again will go to that position,
146 even in a later Emacs session.
148 If called with a prefix arg, the mode is enabled if and only if
149 the argument is positive.
151 To save places automatically in all files, put this in your init
152 file:
154 \(setq-default save-place t\)"
155 (interactive "P")
156 (if (not (or buffer-file-name (and (derived-mode-p 'dired-mode)
157 dired-directory)))
158 (message "Buffer `%s' not visiting a file or directory" (buffer-name))
159 (if (and save-place (or (not parg) (<= parg 0)))
160 (progn
161 (message "No place will be saved in this file")
162 (setq save-place nil))
163 (message "Place will be saved")
164 (setq save-place t))))
166 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
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))
176 (let ((item (or buffer-file-name
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))))))
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))
187 (position (cond ((eq major-mode 'hexl-mode)
188 (with-no-warnings
189 (1+ (hexl-current-address))))
190 ((and (derived-mode-p 'dired-mode)
191 dired-directory)
192 (let ((filename (dired-get-filename nil t)))
193 (if filename
194 `((dired-filename . ,filename))
195 (point))))
196 (t (point)))))
197 (if cell
198 (setq save-place-alist (delq cell save-place-alist)))
199 (if (and save-place
200 (not (and (integerp position)
201 (= position 1)))) ;; Optimize out the degenerate case.
202 (setq save-place-alist
203 (cons (cons item position)
204 save-place-alist)))))))
206 (defun save-place-forget-unreadable-files ()
207 "Remove unreadable files from `save-place-alist'.
208 For each entry in the alist, if `file-readable-p' returns nil for the
209 filename, remove the entry. Save the new alist \(as the first pair
210 may 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)))))
233 (defun save-place-alist-to-file ()
234 (let ((file (expand-file-name save-place-file))
235 (coding-system-for-write 'utf-8))
236 (with-current-buffer (get-buffer-create " *Saved Places*")
237 (delete-region (point-min) (point-max))
238 (when save-place-forget-unreadable-files
239 (save-place-forget-unreadable-files))
240 (insert (format ";;; -*- coding: %s -*-\n"
241 (symbol-name coding-system-for-write)))
242 (let ((print-length nil)
243 (print-level nil))
244 (pp save-place-alist (current-buffer)))
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)
251 t))))
252 (condition-case nil
253 ;; Don't use write-file; we don't want this buffer to visit it.
254 (write-region (point-min) (point-max) file)
255 (file-error (message "Saving places: can't write %s" file)))
256 (kill-buffer (current-buffer))))))
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)
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*")
269 (delete-region (point-min) (point-max))
270 (insert-file-contents file)
271 (goto-char (point-min))
272 (setq save-place-alist
273 (with-demoted-errors "Error reading save-place-file: %S"
274 (car (read-from-string
275 (buffer-substring (point-min) (point-max))))))
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))))))
294 (kill-buffer (current-buffer))))
295 nil))))
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:
305 (with-current-buffer (car buf-list)
306 ;; save-place checks buffer-file-name too, but we can avoid
307 ;; overhead of function call by checking here too.
308 (and (or buffer-file-name (and (derived-mode-p 'dired-mode)
309 dired-directory))
310 (save-place-to-alist))
311 (setq buf-list (cdr buf-list))))))
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
317 (progn
318 (or revert-buffer-in-progress-p
319 (and (integerp (cdr cell))
320 (goto-char (cdr cell))))
321 ;; and make sure it will be saved again for later
322 (setq save-place t)))))
324 (declare-function dired-goto-file "dired" (file))
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))
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)))
334 save-place-alist)))
335 (if cell
336 (progn
337 (or revert-buffer-in-progress-p
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)))))))
342 ;; and make sure it will be saved again for later
343 (setq save-place t)))))
345 (defun save-place-kill-emacs-hook ()
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).
350 (if save-place-loaded
351 (save-place-alist-to-file)))
353 (add-hook 'find-file-hook 'save-place-find-file-hook t)
355 (add-hook 'dired-initial-position-hook 'save-place-dired-hook)
357 (unless noninteractive
358 (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
360 (add-hook 'kill-buffer-hook 'save-place-to-alist)
362 (provide 'saveplace) ; why not...
364 ;;; saveplace.el ends here