Merge from origin/emacs-24
[emacs.git] / lisp / saveplace.el
blobfe54743e393f931fc484acd9b397aed3abe9cc1c
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-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
56 :type '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)
66 (const never)
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
88 `save-place-file'."
89 :type 'boolean)
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
96 on this flag."
97 :type 'boolean)
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."
110 :type 'regexp)
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."
118 :version "24.1"
119 :type 'regexp)
121 (declare-function dired-current-directory "dired" (&optional localp))
123 (define-obsolete-variable-alias 'save-place 'save-place-mode "25.1")
124 ;;;###autoload
125 (define-minor-mode save-place-mode
126 "Non-nil means automatically save place in each file.
127 This means when you visit a file, point goes to the last place
128 where it was when you previously visited the same file."
129 :global t
130 :group 'save-place
131 (cond
132 (save-place-mode
133 (add-hook 'find-file-hook 'save-place-find-file-hook t)
134 (add-hook 'dired-initial-position-hook 'save-place-dired-hook)
135 (unless noninteractive
136 (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
137 (add-hook 'kill-buffer-hook 'save-place-to-alist))
139 (remove-hook 'find-file-hook 'save-place-find-file-hook t)
140 (remove-hook 'dired-initial-position-hook 'save-place-dired-hook)
141 (remove-hook 'kill-emacs-hook 'save-place-kill-emacs-hook)
142 (remove-hook 'kill-buffer-hook 'save-place-to-alist))))
144 (make-variable-buffer-local 'save-place-mode) ; Hysterical raisins.
146 (defun toggle-save-place (&optional parg) ;FIXME: save-place-local-mode!
147 "Toggle whether to save your place in this file between sessions.
148 If this mode is enabled, point is recorded when you kill the buffer
149 or exit Emacs. Visiting this file again will go to that position,
150 even in a later Emacs session.
152 If called with a prefix arg, the mode is enabled if and only if
153 the argument is positive.
155 To save places automatically in all files, put this in your init
156 file:
158 \(setq-default save-place t)"
159 (interactive "P")
160 (if (not (or buffer-file-name (and (derived-mode-p 'dired-mode)
161 (dired-current-directory))))
162 (message "Buffer `%s' not visiting a file or directory" (buffer-name))
163 (setq save-place (if parg
164 (> (prefix-numeric-value parg) 0)
165 (not save-place)))
166 (message (if save-place
167 "Place will be saved"
168 "No place will be saved in this file"))))
170 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
172 (defun save-place-to-alist ()
173 ;; put filename and point in a cons box and then cons that onto the
174 ;; front of the save-place-alist, if save-place is non-nil.
175 ;; Otherwise, just delete that file from the alist.
176 ;; first check to make sure alist has been loaded in from the master
177 ;; file. If not, do so, then feel free to modify the alist. It
178 ;; will be saved again when Emacs is killed.
179 (or save-place-loaded (load-save-place-alist-from-file))
180 (let* ((directory (and (derived-mode-p 'dired-mode)
181 (dired-current-directory)))
182 (item (or buffer-file-name
183 (and directory
184 (expand-file-name (if (consp directory)
185 (car directory)
186 directory))))))
187 (when (and item
188 (or (not save-place-ignore-files-regexp)
189 (not (string-match save-place-ignore-files-regexp
190 item))))
191 (let ((cell (assoc item save-place-alist))
192 (position (cond ((eq major-mode 'hexl-mode)
193 (with-no-warnings
194 (1+ (hexl-current-address))))
195 ((and (derived-mode-p 'dired-mode) directory)
196 (let ((filename (dired-get-filename nil t)))
197 (if filename
198 `((dired-filename . ,filename))
199 (point))))
200 (t (point)))))
201 (if cell
202 (setq save-place-alist (delq cell save-place-alist)))
203 (if (and save-place
204 (not (and (integerp position)
205 (= position 1)))) ;; Optimize out the degenerate case.
206 (setq save-place-alist
207 (cons (cons item position)
208 save-place-alist)))))))
210 (defun save-place-forget-unreadable-files ()
211 "Remove unreadable files from `save-place-alist'.
212 For each entry in the alist, if `file-readable-p' returns nil for the
213 filename, remove the entry. Save the new alist (as the first pair
214 may have changed) back to `save-place-alist'."
215 (interactive)
216 ;; the following was adapted from an in-place filtering function,
217 ;; `filter-mod', used in the original.
218 (unless (null save-place-alist) ;says it better than `when'
219 ;; first, check all except first
220 (let ((fmprev save-place-alist) (fmcur (cdr save-place-alist)))
221 (while fmcur ;not null
222 ;; a value is only saved when it becomes FMPREV.
223 (if (if (string-match save-place-skip-check-regexp (caar fmcur))
224 save-place-save-skipped
225 (file-readable-p (caar fmcur)))
226 (setq fmprev fmcur)
227 (setcdr fmprev (cdr fmcur)))
228 (setq fmcur (cdr fmcur))))
229 ;; test first pair, keep it if OK, otherwise 2nd element, which
230 ;; may be '()
231 (unless (if (string-match save-place-skip-check-regexp
232 (caar save-place-alist))
233 save-place-save-skipped
234 (file-readable-p (caar save-place-alist)))
235 (setq save-place-alist (cdr save-place-alist)))))
237 (defun save-place-alist-to-file ()
238 (let ((file (expand-file-name save-place-file))
239 (coding-system-for-write 'utf-8))
240 (with-current-buffer (get-buffer-create " *Saved Places*")
241 (delete-region (point-min) (point-max))
242 (when save-place-forget-unreadable-files
243 (save-place-forget-unreadable-files))
244 (insert (format ";;; -*- coding: %s -*-\n"
245 (symbol-name coding-system-for-write)))
246 (let ((print-length nil)
247 (print-level nil))
248 (pp save-place-alist (current-buffer)))
249 (let ((version-control
250 (cond
251 ((null save-place-version-control) nil)
252 ((eq 'never save-place-version-control) 'never)
253 ((eq 'nospecial save-place-version-control) version-control)
255 t))))
256 (condition-case nil
257 ;; Don't use write-file; we don't want this buffer to visit it.
258 (write-region (point-min) (point-max) file)
259 (file-error (message "Saving places: can't write %s" file)))
260 (kill-buffer (current-buffer))))))
262 (defun load-save-place-alist-from-file ()
263 (if (not save-place-loaded)
264 (progn
265 (setq save-place-loaded t)
266 (let ((file (expand-file-name save-place-file)))
267 ;; make sure that the alist does not get overwritten, and then
268 ;; load it if it exists:
269 (if (file-readable-p file)
270 ;; don't want to use find-file because we have been
271 ;; adding hooks to it.
272 (with-current-buffer (get-buffer-create " *Saved Places*")
273 (delete-region (point-min) (point-max))
274 (insert-file-contents file)
275 (goto-char (point-min))
276 (setq save-place-alist
277 (with-demoted-errors "Error reading save-place-file: %S"
278 (car (read-from-string
279 (buffer-substring (point-min) (point-max))))))
281 ;; If there is a limit, and we're over it, then we'll
282 ;; have to truncate the end of the list:
283 (if save-place-limit
284 (if (<= save-place-limit 0)
285 ;; Zero gets special cased. I'm not thrilled
286 ;; with this, but the loop for >= 1 is tight.
287 (setq save-place-alist nil)
288 ;; Else the limit is >= 1, so enforce it by
289 ;; counting and then `setcdr'ing.
290 (let ((s save-place-alist)
291 (count 1))
292 (while s
293 (if (>= count save-place-limit)
294 (setcdr s nil)
295 (setq count (1+ count)))
296 (setq s (cdr s))))))
298 (kill-buffer (current-buffer))))
299 nil))))
301 (defun save-places-to-alist ()
302 ;; go through buffer-list, saving places to alist if save-place is
303 ;; non-nil, deleting them from alist if it is nil.
304 (let ((buf-list (buffer-list)))
305 (while buf-list
306 ;; put this into a save-excursion in case someone is counting on
307 ;; another function in kill-emacs-hook to act on the last buffer
308 ;; they were in:
309 (with-current-buffer (car buf-list)
310 ;; save-place checks buffer-file-name too, but we can avoid
311 ;; overhead of function call by checking here too.
312 (and (or buffer-file-name (and (derived-mode-p 'dired-mode)
313 (dired-current-directory)))
314 (save-place-to-alist))
315 (setq buf-list (cdr buf-list))))))
317 (defun save-place-find-file-hook ()
318 (or save-place-loaded (load-save-place-alist-from-file))
319 (let ((cell (assoc buffer-file-name save-place-alist)))
320 (if cell
321 (progn
322 (or revert-buffer-in-progress-p
323 (and (integerp (cdr cell))
324 (goto-char (cdr cell))))
325 ;; and make sure it will be saved again for later
326 (setq save-place t)))))
328 (declare-function dired-goto-file "dired" (file))
330 (defun save-place-dired-hook ()
331 "Position the point in a Dired buffer."
332 (or save-place-loaded (load-save-place-alist-from-file))
333 (let* ((directory (and (derived-mode-p 'dired-mode)
334 (dired-current-directory)))
335 (cell (assoc (and directory
336 (expand-file-name (if (consp directory)
337 (car directory)
338 directory)))
339 save-place-alist)))
340 (if cell
341 (progn
342 (or revert-buffer-in-progress-p
343 (cond
344 ((integerp (cdr cell))
345 (goto-char (cdr cell)))
346 ((and (listp (cdr cell)) (assq 'dired-filename (cdr cell)))
347 (dired-goto-file (cdr (assq 'dired-filename (cdr cell)))))))
348 ;; and make sure it will be saved again for later
349 (setq save-place t)))))
351 (defun save-place-kill-emacs-hook ()
352 ;; First update the alist. This loads the old save-place-file if nec.
353 (save-places-to-alist)
354 ;; Now save the alist in the file, if we have ever loaded the file
355 ;; (including just now).
356 (if save-place-loaded
357 (save-place-alist-to-file)))
359 (provide 'saveplace)
360 ;;; saveplace.el ends here