2012-02-17 Tassilo Horn <tassilo@member.fsf.org>
[bpt/emacs.git] / lisp / saveplace.el
1 ;;; saveplace.el --- automatically save place in files
2
3 ;; Copyright (C) 1993-1994, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: FSF
7 ;; Created: July, 1993
8 ;; Keywords: bookmarks, placeholders
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
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.
32 ;;
33 ;; Thanks to Stefan Schoef, who sent a patch with the
34 ;; `save-place-version-control' stuff in it.
35
36 ;;; Code:
37
38 ;; this is what I was using during testing:
39 ;; (define-key ctl-x-map "p" 'toggle-save-place-globally)
40
41 (defgroup save-place nil
42 "Automatically save place in files."
43 :group 'data)
44
45
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.")
52
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.
58
59 If you wish your place in any file to always be automatically saved,
60 simply put this in your `~/.emacs' file:
61
62 \(setq-default save-place t)
63 \(require 'saveplace)
64
65 or else use the Custom facility to set this option."
66 :type 'boolean
67 :require 'saveplace
68 :group 'save-place)
69
70 (make-variable-buffer-local 'save-place)
71
72 (defcustom save-place-file (convert-standard-filename "~/.emacs-places")
73 "Name of the file that records `save-place-alist' value."
74 :type 'file
75 :group 'save-place)
76
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)
88
89 (defvar save-place-loaded nil
90 "Non-nil means that the `save-place-file' has been loaded.")
91
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)
98
99 (defcustom save-place-forget-unreadable-files t
100 "Non-nil means forget place in unreadable files.
101
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.
105
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)
111
112 (defcustom save-place-save-skipped t
113 "If non-nil, remember files matching `save-place-skip-check-regexp'.
114
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)
119
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.
124
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'.
128
129 Files for which such a check may be inconvenient include those on
130 removable and network volumes."
131 :type 'regexp :group 'save-place)
132
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 location should be recorded.
136 Useful for temporary file such as commit message files that are
137 automatically created by the VCS."
138 :type 'regexp :group 'save-place)
139
140 (defun toggle-save-place (&optional parg)
141 "Toggle whether to save your place in this file between sessions.
142 If this mode is enabled, point is recorded when you kill the buffer
143 or exit Emacs. Visiting this file again will go to that position,
144 even in a later Emacs session.
145
146 If called with a prefix arg, the mode is enabled if and only if
147 the argument is positive.
148
149 To save places automatically in all files, put this in your `.emacs' file:
150
151 \(setq-default save-place t\)"
152 (interactive "P")
153 (if (not buffer-file-name)
154 (message "Buffer `%s' not visiting a file" (buffer-name))
155 (if (and save-place (or (not parg) (<= parg 0)))
156 (progn
157 (message "No place will be saved in this file")
158 (setq save-place nil))
159 (message "Place will be saved")
160 (setq save-place t))))
161
162 (defun save-place-to-alist ()
163 ;; put filename and point in a cons box and then cons that onto the
164 ;; front of the save-place-alist, if save-place is non-nil.
165 ;; Otherwise, just delete that file from the alist.
166 ;; first check to make sure alist has been loaded in from the master
167 ;; file. If not, do so, then feel free to modify the alist. It
168 ;; will be saved again when Emacs is killed.
169 (or save-place-loaded (load-save-place-alist-from-file))
170 (when (and buffer-file-name
171 (not (string-match save-place-ignore-files-regexp
172 buffer-file-name)))
173 (let ((cell (assoc buffer-file-name save-place-alist))
174 (position (if (not (eq major-mode 'hexl-mode))
175 (point)
176 (with-no-warnings
177 (1+ (hexl-current-address))))))
178 (if cell
179 (setq save-place-alist (delq cell save-place-alist)))
180 (if (and save-place
181 (not (= position 1))) ;; Optimize out the degenerate case.
182 (setq save-place-alist
183 (cons (cons buffer-file-name position)
184 save-place-alist))))))
185
186 (defun save-place-forget-unreadable-files ()
187 "Remove unreadable files from `save-place-alist'.
188 For each entry in the alist, if `file-readable-p' returns nil for the
189 filename, remove the entry. Save the new alist \(as the first pair
190 may have changed\) back to `save-place-alist'."
191 (interactive)
192 ;; the following was adapted from an in-place filtering function,
193 ;; `filter-mod', used in the original.
194 (unless (null save-place-alist) ;says it better than `when'
195 ;; first, check all except first
196 (let ((fmprev save-place-alist) (fmcur (cdr save-place-alist)))
197 (while fmcur ;not null
198 ;; a value is only saved when it becomes FMPREV.
199 (if (if (string-match save-place-skip-check-regexp (caar fmcur))
200 save-place-save-skipped
201 (file-readable-p (caar fmcur)))
202 (setq fmprev fmcur)
203 (setcdr fmprev (cdr fmcur)))
204 (setq fmcur (cdr fmcur))))
205 ;; test first pair, keep it if OK, otherwise 2nd element, which
206 ;; may be '()
207 (unless (if (string-match save-place-skip-check-regexp
208 (caar save-place-alist))
209 save-place-save-skipped
210 (file-readable-p (caar save-place-alist)))
211 (setq save-place-alist (cdr save-place-alist)))))
212
213 (defun save-place-alist-to-file ()
214 (let ((file (expand-file-name save-place-file))
215 (coding-system-for-write 'utf-8))
216 (with-current-buffer (get-buffer-create " *Saved Places*")
217 (delete-region (point-min) (point-max))
218 (when save-place-forget-unreadable-files
219 (save-place-forget-unreadable-files))
220 (insert (format ";;; -*- coding: %s -*-\n"
221 (symbol-name coding-system-for-write)))
222 (let ((print-length nil)
223 (print-level nil))
224 (pp (sort save-place-alist
225 (lambda (a b) (string< (car a) (car b))))
226 (current-buffer)))
227 (let ((version-control
228 (cond
229 ((null save-place-version-control) nil)
230 ((eq 'never save-place-version-control) 'never)
231 ((eq 'nospecial save-place-version-control) version-control)
232 (t
233 t))))
234 (condition-case nil
235 ;; Don't use write-file; we don't want this buffer to visit it.
236 (write-region (point-min) (point-max) file)
237 (file-error (message "Saving places: can't write %s" file)))
238 (kill-buffer (current-buffer))))))
239
240 (defun load-save-place-alist-from-file ()
241 (if (not save-place-loaded)
242 (progn
243 (setq save-place-loaded t)
244 (let ((file (expand-file-name save-place-file)))
245 ;; make sure that the alist does not get overwritten, and then
246 ;; load it if it exists:
247 (if (file-readable-p file)
248 ;; don't want to use find-file because we have been
249 ;; adding hooks to it.
250 (with-current-buffer (get-buffer-create " *Saved Places*")
251 (delete-region (point-min) (point-max))
252 (insert-file-contents file)
253 (goto-char (point-min))
254 (setq save-place-alist
255 (car (read-from-string
256 (buffer-substring (point-min) (point-max)))))
257
258 ;; If there is a limit, and we're over it, then we'll
259 ;; have to truncate the end of the list:
260 (if save-place-limit
261 (if (<= save-place-limit 0)
262 ;; Zero gets special cased. I'm not thrilled
263 ;; with this, but the loop for >= 1 is tight.
264 (setq save-place-alist nil)
265 ;; Else the limit is >= 1, so enforce it by
266 ;; counting and then `setcdr'ing.
267 (let ((s save-place-alist)
268 (count 1))
269 (while s
270 (if (>= count save-place-limit)
271 (setcdr s nil)
272 (setq count (1+ count)))
273 (setq s (cdr s))))))
274
275 (kill-buffer (current-buffer))))
276 nil))))
277
278 (defun save-places-to-alist ()
279 ;; go through buffer-list, saving places to alist if save-place is
280 ;; non-nil, deleting them from alist if it is nil.
281 (let ((buf-list (buffer-list)))
282 (while buf-list
283 ;; put this into a save-excursion in case someone is counting on
284 ;; another function in kill-emacs-hook to act on the last buffer
285 ;; they were in:
286 (with-current-buffer (car buf-list)
287 ;; save-place checks buffer-file-name too, but we can avoid
288 ;; overhead of function call by checking here too.
289 (and buffer-file-name (save-place-to-alist))
290 (setq buf-list (cdr buf-list))))))
291
292 (defun save-place-find-file-hook ()
293 (or save-place-loaded (load-save-place-alist-from-file))
294 (let ((cell (assoc buffer-file-name save-place-alist)))
295 (if cell
296 (progn
297 (or revert-buffer-in-progress-p
298 (goto-char (cdr cell)))
299 ;; and make sure it will be saved again for later
300 (setq save-place t)))))
301
302 (defun save-place-kill-emacs-hook ()
303 ;; First update the alist. This loads the old save-place-file if nec.
304 (save-places-to-alist)
305 ;; Now save the alist in the file, if we have ever loaded the file
306 ;; (including just now).
307 (if save-place-loaded
308 (save-place-alist-to-file)))
309
310 (add-hook 'find-file-hook 'save-place-find-file-hook t)
311
312 (unless noninteractive
313 (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
314
315 (add-hook 'kill-buffer-hook 'save-place-to-alist)
316
317 (provide 'saveplace) ; why not...
318
319 ;;; saveplace.el ends here