local_var_alist_ is shared by threads.
[emacs.git] / lisp / saveplace.el
blob35625ca276f1cad85b0dfcec7025d972dc5c5b5a
1 ;;; saveplace.el --- automatically save place in files
3 ;; Copyright (C) 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Karl Fogel <kfogel@red-bean.com>
7 ;; Maintainer: FSF
8 ;; Created: July, 1993
9 ;; Keywords: bookmarks, placeholders
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; Automatically save place in files, so that visiting them later
29 ;; (even during a different Emacs session) automatically moves point
30 ;; to the saved position, when the file is first found. Uses the
31 ;; value of buffer-local variable save-place to determine whether to
32 ;; save position or not.
34 ;; Thanks to Stefan Schoef, who sent a patch with the
35 ;; `save-place-version-control' stuff in it.
37 ;;; Code:
39 ;; this is what I was using during testing:
40 ;; (define-key ctl-x-map "p" 'toggle-save-place-globally)
42 (defgroup save-place nil
43 "Automatically save place in files."
44 :group 'data)
47 (defvar save-place-alist nil
48 "Alist of saved places to go back to when revisiting files.
49 Each element looks like (FILENAME . POSITION);
50 visiting file FILENAME goes automatically to position POSITION
51 rather than the beginning of the buffer.
52 This alist is saved between Emacs sessions.")
54 (defcustom save-place nil
55 "Non-nil means automatically save place in each file.
56 This means when you visit a file, point goes to the last place
57 where it was when you previously visited the same file.
58 This variable is automatically buffer-local.
60 If you wish your place in any file to always be automatically saved,
61 simply put this in your `~/.emacs' file:
63 \(setq-default save-place t)
64 \(require 'saveplace)
66 or else use the Custom facility to set this option."
67 :type 'boolean
68 :require 'saveplace
69 :group 'save-place)
71 (make-variable-buffer-local 'save-place)
73 (defcustom save-place-file (convert-standard-filename "~/.emacs-places")
74 "Name of the file that records `save-place-alist' value."
75 :type 'file
76 :group 'save-place)
78 (defcustom save-place-version-control nil
79 "Controls whether to make numbered backups of master save-place file.
80 It can have four values: t, nil, `never', and `nospecial'. The first
81 three have the same meaning that they do for the variable
82 `version-control', and the final value `nospecial' means just use the
83 value of `version-control'."
84 :type '(radio (const :tag "Unconditionally" t)
85 (const :tag "For VC Files" nil)
86 (const never)
87 (const :tag "Use value of `version-control'" nospecial))
88 :group 'save-place)
90 (defvar save-place-loaded nil
91 "Non-nil means that the `save-place-file' has been loaded.")
93 (defcustom save-place-limit nil
94 "Maximum number of entries to retain in the list; nil means no limit."
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 (defun toggle-save-place (&optional parg)
134 "Toggle whether to save your place in this file between sessions.
135 If this mode is enabled, point is recorded when you kill the buffer
136 or exit Emacs. Visiting this file again will go to that position,
137 even in a later Emacs session.
139 If called with a prefix arg, the mode is enabled if and only if
140 the argument is positive.
142 To save places automatically in all files, put this in your `.emacs' file:
144 \(setq-default save-place t\)"
145 (interactive "P")
146 (if (not buffer-file-name)
147 (message "Buffer `%s' not visiting a file" (buffer-name))
148 (if (and save-place (or (not parg) (<= parg 0)))
149 (progn
150 (message "No place will be saved in this file")
151 (setq save-place nil))
152 (message "Place will be saved")
153 (setq save-place t))))
155 (defun save-place-to-alist ()
156 ;; put filename and point in a cons box and then cons that onto the
157 ;; front of the save-place-alist, if save-place is non-nil.
158 ;; Otherwise, just delete that file from the alist.
159 ;; first check to make sure alist has been loaded in from the master
160 ;; file. If not, do so, then feel free to modify the alist. It
161 ;; will be saved again when Emacs is killed.
162 (or save-place-loaded (load-save-place-alist-from-file))
163 (if buffer-file-name
164 (progn
165 (let ((cell (assoc buffer-file-name save-place-alist))
166 (position (if (not (eq major-mode 'hexl-mode))
167 (point)
168 (with-no-warnings
169 (1+ (hexl-current-address))))))
170 (if cell
171 (setq save-place-alist (delq cell save-place-alist)))
172 (if (and save-place
173 (not (= position 1))) ;; Optimize out the degenerate case.
174 (setq save-place-alist
175 (cons (cons buffer-file-name position)
176 save-place-alist)))))))
178 (defun save-place-forget-unreadable-files ()
179 "Remove unreadable files from `save-place-alist'.
180 For each entry in the alist, if `file-readable-p' returns nil for the
181 filename, remove the entry. Save the new alist \(as the first pair
182 may have changed\) back to `save-place-alist'."
183 (interactive)
184 ;; the following was adapted from an in-place filtering function,
185 ;; `filter-mod', used in the original.
186 (unless (null save-place-alist) ;says it better than `when'
187 ;; first, check all except first
188 (let ((fmprev save-place-alist) (fmcur (cdr save-place-alist)))
189 (while fmcur ;not null
190 ;; a value is only saved when it becomes FMPREV.
191 (if (if (string-match save-place-skip-check-regexp (caar fmcur))
192 save-place-save-skipped
193 (file-readable-p (caar fmcur)))
194 (setq fmprev fmcur)
195 (setcdr fmprev (cdr fmcur)))
196 (setq fmcur (cdr fmcur))))
197 ;; test first pair, keep it if OK, otherwise 2nd element, which
198 ;; may be '()
199 (unless (if (string-match save-place-skip-check-regexp
200 (caar save-place-alist))
201 save-place-save-skipped
202 (file-readable-p (caar save-place-alist)))
203 (setq save-place-alist (cdr save-place-alist)))))
205 (defun save-place-alist-to-file ()
206 (let ((file (expand-file-name save-place-file))
207 (coding-system-for-write 'utf-8))
208 (with-current-buffer (get-buffer-create " *Saved Places*")
209 (delete-region (point-min) (point-max))
210 (when save-place-forget-unreadable-files
211 (save-place-forget-unreadable-files))
212 (insert (format ";;; -*- coding: %s -*-\n"
213 (symbol-name coding-system-for-write)))
214 (let ((print-length nil)
215 (print-level nil))
216 (print save-place-alist (current-buffer)))
217 (let ((version-control
218 (cond
219 ((null save-place-version-control) nil)
220 ((eq 'never save-place-version-control) 'never)
221 ((eq 'nospecial save-place-version-control) version-control)
223 t))))
224 (condition-case nil
225 ;; Don't use write-file; we don't want this buffer to visit it.
226 (write-region (point-min) (point-max) file)
227 (file-error (message "Saving places: can't write %s" file)))
228 (kill-buffer (current-buffer))))))
230 (defun load-save-place-alist-from-file ()
231 (if (not save-place-loaded)
232 (progn
233 (setq save-place-loaded t)
234 (let ((file (expand-file-name save-place-file)))
235 ;; make sure that the alist does not get overwritten, and then
236 ;; load it if it exists:
237 (if (file-readable-p file)
238 ;; don't want to use find-file because we have been
239 ;; adding hooks to it.
240 (with-current-buffer (get-buffer-create " *Saved Places*")
241 (delete-region (point-min) (point-max))
242 (insert-file-contents file)
243 (goto-char (point-min))
244 (setq save-place-alist
245 (car (read-from-string
246 (buffer-substring (point-min) (point-max)))))
248 ;; If there is a limit, and we're over it, then we'll
249 ;; have to truncate the end of the list:
250 (if save-place-limit
251 (if (<= save-place-limit 0)
252 ;; Zero gets special cased. I'm not thrilled
253 ;; with this, but the loop for >= 1 is tight.
254 (setq save-place-alist nil)
255 ;; Else the limit is >= 1, so enforce it by
256 ;; counting and then `setcdr'ing.
257 (let ((s save-place-alist)
258 (count 1))
259 (while s
260 (if (>= count save-place-limit)
261 (setcdr s nil)
262 (setq count (1+ count)))
263 (setq s (cdr s))))))
265 (kill-buffer (current-buffer))))
266 nil))))
268 (defun save-places-to-alist ()
269 ;; go through buffer-list, saving places to alist if save-place is
270 ;; non-nil, deleting them from alist if it is nil.
271 (let ((buf-list (buffer-list)))
272 (while buf-list
273 ;; put this into a save-excursion in case someone is counting on
274 ;; another function in kill-emacs-hook to act on the last buffer
275 ;; they were in:
276 (with-current-buffer (car buf-list)
277 ;; save-place checks buffer-file-name too, but we can avoid
278 ;; overhead of function call by checking here too.
279 (and buffer-file-name (save-place-to-alist))
280 (setq buf-list (cdr buf-list))))))
282 (defun save-place-find-file-hook ()
283 (or save-place-loaded (load-save-place-alist-from-file))
284 (let ((cell (assoc buffer-file-name save-place-alist)))
285 (if cell
286 (progn
287 (or after-find-file-from-revert-buffer
288 (goto-char (cdr cell)))
289 ;; and make sure it will be saved again for later
290 (setq save-place t)))))
292 (defun save-place-kill-emacs-hook ()
293 ;; First update the alist. This loads the old save-place-file if nec.
294 (save-places-to-alist)
295 ;; Now save the alist in the file, if we have ever loaded the file
296 ;; (including just now).
297 (if save-place-loaded
298 (save-place-alist-to-file)))
300 (add-hook 'find-file-hook 'save-place-find-file-hook t)
302 (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook)
304 (add-hook 'kill-buffer-hook 'save-place-to-alist)
306 (provide 'saveplace) ; why not...
308 ;; arch-tag: 3c2ef47b-0a22-4558-b116-118c9ef454a0
309 ;;; saveplace.el ends here