1 ;;; saveplace.el --- automatically save place in files
3 ;; Copyright (C) 1993-1994, 2001-2016 Free Software Foundation, Inc.
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: emacs-devel@gnu.org
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/>.
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.
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."
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-file
(locate-user-emacs-file "places" ".emacs-places")
54 "Name of the file that records `save-place-alist' value."
55 :version
"24.4" ; added locate-user-emacs-file
58 (defcustom save-place-version-control nil
59 "Controls whether to make numbered backups of master save-place file.
60 It can have four values: t, nil, `never', and `nospecial'. The first
61 three have the same meaning that they do for the variable
62 `version-control', and the final value `nospecial' means just use the
63 value of `version-control'."
64 :type
'(radio (const :tag
"Unconditionally" t
)
65 (const :tag
"For VC Files" nil
)
67 (const :tag
"Use value of `version-control'" nospecial
)))
69 (defvar save-place-loaded nil
70 "Non-nil means that the `save-place-file' has been loaded.")
72 (defcustom save-place-limit
400
73 "Maximum number of entries to retain in the list; nil means no limit."
74 :version
"24.1" ; nil -> 400
75 :type
'(choice (integer :tag
"Entries" :value
1)
76 (const :tag
"No Limit" nil
)))
78 (defcustom save-place-forget-unreadable-files t
79 "Non-nil means forget place in unreadable files.
81 The filenames in `save-place-alist' that do not match
82 `save-place-skip-check-regexp' are filtered through
83 `file-readable-p'. If nil, their alist entries are removed.
85 You may do this anytime by calling the complementary function,
86 `save-place-forget-unreadable-files'. When this option is turned on,
87 this happens automatically before saving `save-place-alist' to
91 (defcustom save-place-save-skipped t
92 "If non-nil, remember files matching `save-place-skip-check-regexp'.
94 When filtering `save-place-alist' for unreadable files, some will not
95 be checked, based on said regexp, and instead saved or forgotten based
99 (defcustom save-place-skip-check-regexp
100 ;; thanks to ange-ftp-name-format
101 "\\`/\\(?:cdrom\\|floppy\\|mnt\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)"
102 "Regexp whose file names shall not be checked for readability.
104 When forgetting unreadable files, file names matching this regular
105 expression shall not be checked for readability, but instead be
106 subject to `save-place-save-skipped'.
108 Files for which such a check may be inconvenient include those on
109 removable and network volumes."
112 (defcustom save-place-ignore-files-regexp
113 "\\(?:COMMIT_EDITMSG\\|hg-editor-[[:alnum:]]+\\.txt\\|svn-commit\\.tmp\\|bzr_log\\.[[:alnum:]]+\\)$"
114 "Regexp matching files for which no position should be recorded.
115 Useful for temporary file such as commit message files that are
116 automatically created by the VCS. If set to nil, this feature is
117 disabled, i.e., the position is recorded for all files."
121 (declare-function dired-current-directory
"dired" (&optional localp
))
123 (defun save-place--setup-hooks (add)
126 (add-hook 'find-file-hook
#'save-place-find-file-hook t
)
127 (add-hook 'dired-initial-position-hook
#'save-place-dired-hook
)
128 (unless noninteractive
129 (add-hook 'kill-emacs-hook
#'save-place-kill-emacs-hook
))
130 (add-hook 'kill-buffer-hook
#'save-place-to-alist
))
132 ;; We should remove the hooks, but only if save-place-mode
133 ;; is nil everywhere. Is it worth the trouble, tho?
134 ;; (unless (or (default-value 'save-place-mode)
135 ;; (cl-some <save-place-local-mode-p> (buffer-list)))
136 ;; (remove-hook 'find-file-hook #'save-place-find-file-hook)
137 ;; (remove-hook 'dired-initial-position-hook #'save-place-dired-hook)
138 ;; (remove-hook 'kill-emacs-hook #'save-place-kill-emacs-hook)
139 ;; (remove-hook 'kill-buffer-hook #'save-place-to-alist))
142 (define-obsolete-variable-alias 'save-place
'save-place-mode
"25.1")
144 (define-minor-mode save-place-mode
145 "Non-nil means automatically save place in each file.
146 This means when you visit a file, point goes to the last place
147 where it was when you previously visited the same file."
150 (save-place--setup-hooks save-place-mode
))
152 (make-variable-buffer-local 'save-place-mode
)
154 (define-obsolete-function-alias 'toggle-save-place
155 #'save-place-local-mode
"25.1")
157 (define-minor-mode save-place-local-mode
158 "Toggle whether to save your place in this file between sessions.
159 If this mode is enabled, point is recorded when you kill the buffer
160 or exit Emacs. Visiting this file again will go to that position,
161 even in a later Emacs session.
163 If called with a prefix arg, the mode is enabled if and only if
164 the argument is positive.
166 To save places automatically in all files, put this in your init
169 \(save-place-mode 1)"
170 :variable save-place-mode
171 (if (not (or buffer-file-name
(and (derived-mode-p 'dired-mode
)
172 (boundp 'dired-subdir-alist
)
174 (dired-current-directory))))
175 (message "Buffer `%s' not visiting a file or directory" (buffer-name))
176 (save-place--setup-hooks save-place-mode
)))
178 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
180 (defun save-place-to-alist ()
181 ;; put filename and point in a cons box and then cons that onto the
182 ;; front of the save-place-alist, if save-place is non-nil.
183 ;; Otherwise, just delete that file from the alist.
184 ;; first check to make sure alist has been loaded in from the master
185 ;; file. If not, do so, then feel free to modify the alist. It
186 ;; will be saved again when Emacs is killed.
187 (or save-place-loaded
(load-save-place-alist-from-file))
188 (let* ((directory (and (derived-mode-p 'dired-mode
)
189 (boundp 'dired-subdir-alist
)
191 (dired-current-directory)))
192 (item (or buffer-file-name
194 (expand-file-name (if (consp directory
)
198 (or (not save-place-ignore-files-regexp
)
199 (not (string-match save-place-ignore-files-regexp
201 (let ((cell (assoc item save-place-alist
))
202 (position (cond ((eq major-mode
'hexl-mode
)
204 (1+ (hexl-current-address))))
205 ((and (derived-mode-p 'dired-mode
) directory
)
206 (let ((filename (dired-get-filename nil t
)))
208 `((dired-filename .
,filename
))
212 (setq save-place-alist
(delq cell save-place-alist
)))
214 (not (and (integerp position
)
215 (= position
1)))) ;; Optimize out the degenerate case.
216 (setq save-place-alist
217 (cons (cons item position
)
218 save-place-alist
)))))))
220 (defun save-place-forget-unreadable-files ()
221 "Remove unreadable files from `save-place-alist'.
222 For each entry in the alist, if `file-readable-p' returns nil for the
223 filename, remove the entry. Save the new alist (as the first pair
224 may have changed) back to `save-place-alist'."
226 ;; the following was adapted from an in-place filtering function,
227 ;; `filter-mod', used in the original.
228 (unless (null save-place-alist
) ;says it better than `when'
229 ;; first, check all except first
230 (let ((fmprev save-place-alist
) (fmcur (cdr save-place-alist
)))
231 (while fmcur
;not null
232 ;; a value is only saved when it becomes FMPREV.
233 (if (if (string-match save-place-skip-check-regexp
(caar fmcur
))
234 save-place-save-skipped
235 (file-readable-p (caar fmcur
)))
237 (setcdr fmprev
(cdr fmcur
)))
238 (setq fmcur
(cdr fmcur
))))
239 ;; test first pair, keep it if OK, otherwise 2nd element, which
241 (unless (if (string-match save-place-skip-check-regexp
242 (caar save-place-alist
))
243 save-place-save-skipped
244 (file-readable-p (caar save-place-alist
)))
245 (setq save-place-alist
(cdr save-place-alist
)))))
247 (defun save-place-alist-to-file ()
248 (let ((file (expand-file-name save-place-file
))
249 (coding-system-for-write 'utf-8
))
250 (with-current-buffer (get-buffer-create " *Saved Places*")
251 (delete-region (point-min) (point-max))
252 (when save-place-forget-unreadable-files
253 (save-place-forget-unreadable-files))
254 (insert (format ";;; -*- coding: %s -*-\n"
255 (symbol-name coding-system-for-write
)))
256 (let ((print-length nil
)
258 (pp save-place-alist
(current-buffer)))
259 (let ((version-control
261 ((null save-place-version-control
) nil
)
262 ((eq 'never save-place-version-control
) 'never
)
263 ((eq 'nospecial save-place-version-control
) version-control
)
267 ;; Don't use write-file; we don't want this buffer to visit it.
268 (write-region (point-min) (point-max) file
)
269 (file-error (message "Saving places: can't write %s" file
)))
270 (kill-buffer (current-buffer))))))
272 (defun load-save-place-alist-from-file ()
273 (if (not save-place-loaded
)
275 (setq save-place-loaded t
)
276 (let ((file (expand-file-name save-place-file
)))
277 ;; make sure that the alist does not get overwritten, and then
278 ;; load it if it exists:
279 (if (file-readable-p file
)
280 ;; don't want to use find-file because we have been
281 ;; adding hooks to it.
282 (with-current-buffer (get-buffer-create " *Saved Places*")
283 (delete-region (point-min) (point-max))
284 (insert-file-contents file
)
285 (goto-char (point-min))
286 (setq save-place-alist
287 (with-demoted-errors "Error reading save-place-file: %S"
288 (car (read-from-string
289 (buffer-substring (point-min) (point-max))))))
291 ;; If there is a limit, and we're over it, then we'll
292 ;; have to truncate the end of the list:
294 (if (<= save-place-limit
0)
295 ;; Zero gets special cased. I'm not thrilled
296 ;; with this, but the loop for >= 1 is tight.
297 (setq save-place-alist nil
)
298 ;; Else the limit is >= 1, so enforce it by
299 ;; counting and then `setcdr'ing.
300 (let ((s save-place-alist
)
303 (if (>= count save-place-limit
)
305 (setq count
(1+ count
)))
308 (kill-buffer (current-buffer))))
311 (defun save-places-to-alist ()
312 ;; go through buffer-list, saving places to alist if save-place is
313 ;; non-nil, deleting them from alist if it is nil.
314 (let ((buf-list (buffer-list)))
316 ;; put this into a save-excursion in case someone is counting on
317 ;; another function in kill-emacs-hook to act on the last buffer
319 (with-current-buffer (car buf-list
)
320 ;; save-place checks buffer-file-name too, but we can avoid
321 ;; overhead of function call by checking here too.
322 (and (or buffer-file-name
(and (derived-mode-p 'dired-mode
)
323 (boundp 'dired-subdir-alist
)
325 (dired-current-directory)))
326 (save-place-to-alist))
327 (setq buf-list
(cdr buf-list
))))))
329 (defun save-place-find-file-hook ()
330 (or save-place-loaded
(load-save-place-alist-from-file))
331 (let ((cell (assoc buffer-file-name save-place-alist
)))
334 (or revert-buffer-in-progress-p
335 (and (integerp (cdr cell
))
336 (goto-char (cdr cell
))))
337 ;; and make sure it will be saved again for later
338 (setq save-place t
)))))
340 (declare-function dired-goto-file
"dired" (file))
342 (defun save-place-dired-hook ()
343 "Position the point in a Dired buffer."
344 (or save-place-loaded
(load-save-place-alist-from-file))
345 (let* ((directory (and (derived-mode-p 'dired-mode
)
346 (boundp 'dired-subdir-alist
)
348 (dired-current-directory)))
349 (cell (assoc (and directory
350 (expand-file-name (if (consp directory
)
356 (or revert-buffer-in-progress-p
358 ((integerp (cdr cell
))
359 (goto-char (cdr cell
)))
360 ((and (listp (cdr cell
)) (assq 'dired-filename
(cdr cell
)))
361 (dired-goto-file (cdr (assq 'dired-filename
(cdr cell
)))))))
362 ;; and make sure it will be saved again for later
363 (setq save-place t
)))))
365 (defun save-place-kill-emacs-hook ()
366 ;; First update the alist. This loads the old save-place-file if nec.
367 (save-places-to-alist)
368 ;; Now save the alist in the file, if we have ever loaded the file
369 ;; (including just now).
370 (if save-place-loaded
371 (save-place-alist-to-file)))
374 ;;; saveplace.el ends here