Fix last change of grow_mini_window.
[bpt/emacs.git] / lisp / saveplace.el
CommitLineData
55535639 1;;; saveplace.el --- automatically save place in files
722074ef 2
ab422c4d 3;; Copyright (C) 1993-1994, 2001-2013 Free Software Foundation, Inc.
722074ef 4
19a448e3 5;; Author: Karl Fogel <kfogel@red-bean.com>
722074ef
RS
6;; Maintainer: FSF
7;; Created: July, 1993
722074ef
RS
8;; Keywords: bookmarks, placeholders
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
722074ef 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
722074ef
RS
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
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b578f267 24
6f96f4c9
RS
25;;; Commentary:
26
722074ef
RS
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;;
e7dc77f6
RS
33;; Thanks to Stefan Schoef, who sent a patch with the
34;; `save-place-version-control' stuff in it.
722074ef 35
6f96f4c9
RS
36;;; Code:
37
722074ef 38;; this is what I was using during testing:
afa29d95 39;; (define-key ctl-x-map "p" 'toggle-save-place-globally)
722074ef 40
20606f3d
RS
41(defgroup save-place nil
42 "Automatically save place in files."
43 :group 'data)
44
45
722074ef
RS
46(defvar save-place-alist nil
47 "Alist of saved places to go back to when revisiting files.
48Each element looks like (FILENAME . POSITION);
49visiting file FILENAME goes automatically to position POSITION
50rather than the beginning of the buffer.
51This alist is saved between Emacs sessions.")
52
20606f3d 53(defcustom save-place nil
9201cc28 54 "Non-nil means automatically save place in each file.
722074ef
RS
55This means when you visit a file, point goes to the last place
56where it was when you previously visited the same file.
57This variable is automatically buffer-local.
58
865fe16f
CY
59If you wish your place in any file to always be automatically
60saved, set this to t using the Customize facility, or put the
61following code in your init file:
722074ef 62
c73dca25 63\(setq-default save-place t)
865fe16f 64\(require 'saveplace)"
20606f3d 65 :type 'boolean
d749f559 66 :require 'saveplace
20606f3d 67 :group 'save-place)
722074ef
RS
68
69(make-variable-buffer-local 'save-place)
70
940e5099 71(defcustom save-place-file (locate-user-emacs-file "places" ".emacs-places")
9201cc28 72 "Name of the file that records `save-place-alist' value."
20606f3d
RS
73 :type 'file
74 :group 'save-place)
722074ef 75
a4ce5ede 76(defcustom save-place-version-control nil
9201cc28 77 "Controls whether to make numbered backups of master save-place file.
e7dc77f6
RS
78It can have four values: t, nil, `never', and `nospecial'. The first
79three have the same meaning that they do for the variable
80`version-control', and the final value `nospecial' means just use the
20606f3d
RS
81value of `version-control'."
82 :type '(radio (const :tag "Unconditionally" t)
83 (const :tag "For VC Files" nil)
84 (const never)
a4ce5ede 85 (const :tag "Use value of `version-control'" nospecial))
20606f3d 86 :group 'save-place)
e7dc77f6 87
722074ef
RS
88(defvar save-place-loaded nil
89 "Non-nil means that the `save-place-file' has been loaded.")
90
2f6a3e79 91(defcustom save-place-limit 400
20606f3d 92 "Maximum number of entries to retain in the list; nil means no limit."
2f6a3e79 93 :version "24.1" ; nil -> 400
20606f3d
RS
94 :type '(choice (integer :tag "Entries" :value 1)
95 (const :tag "No Limit" nil))
96 :group 'save-place)
6cf02570 97
467997b7
EZ
98(defcustom save-place-forget-unreadable-files t
99 "Non-nil means forget place in unreadable files.
100
101The filenames in `save-place-alist' that do not match
102`save-place-skip-check-regexp' are filtered through
103`file-readable-p'. if nil, their alist entries are removed.
104
105You may do this anytime by calling the complementary function,
106`save-place-forget-unreadable-files'. When this option is turned on,
107this happens automatically before saving `save-place-alist' to
108`save-place-file'."
109 :type 'boolean :group 'save-place)
110
111(defcustom save-place-save-skipped t
112 "If non-nil, remember files matching `save-place-skip-check-regexp'.
113
114When filtering `save-place-alist' for unreadable files, some will not
115be checked, based on said regexp, and instead saved or forgotten based
116on this flag."
117 :type 'boolean :group 'save-place)
118
119(defcustom save-place-skip-check-regexp
120 ;; thanks to ange-ftp-name-format
121 "\\`/\\(?:cdrom\\|floppy\\|mnt\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)"
122 "Regexp whose file names shall not be checked for readability.
123
124When forgetting unreadable files, file names matching this regular
125expression shall not be checked for readability, but instead be
126subject to `save-place-save-skipped'.
127
128Files for which such a check may be inconvenient include those on
129removable and network volumes."
130 :type 'regexp :group 'save-place)
131
0311a3fc
TH
132(defcustom save-place-ignore-files-regexp
133 "\\(?:COMMIT_EDITMSG\\|hg-editor-[[:alnum:]]+\\.txt\\|svn-commit\\.tmp\\|bzr_log\\.[[:alnum:]]+\\)$"
5dd11cfe 134 "Regexp matching files for which no position should be recorded.
0311a3fc 135Useful for temporary file such as commit message files that are
5dd11cfe
TH
136automatically created by the VCS. If set to nil, this feature is
137disabled, i.e., the position is recorded for all files."
e23a3fbe 138 :version "24.1"
0311a3fc
TH
139 :type 'regexp :group 'save-place)
140
722074ef
RS
141(defun toggle-save-place (&optional parg)
142 "Toggle whether to save your place in this file between sessions.
143If this mode is enabled, point is recorded when you kill the buffer
144or exit Emacs. Visiting this file again will go to that position,
145even in a later Emacs session.
146
147If called with a prefix arg, the mode is enabled if and only if
148the argument is positive.
149
865fe16f
CY
150To save places automatically in all files, put this in your init
151file:
722074ef
RS
152
153\(setq-default save-place t\)"
154 (interactive "P")
1d42e5b6
JL
155 (if (not (or buffer-file-name dired-directory))
156 (message "Buffer `%s' not visiting a file or directory" (buffer-name))
722074ef
RS
157 (if (and save-place (or (not parg) (<= parg 0)))
158 (progn
44a75d48 159 (message "No place will be saved in this file")
722074ef 160 (setq save-place nil))
44a75d48 161 (message "Place will be saved")
722074ef
RS
162 (setq save-place t))))
163
1d42e5b6
JL
164(declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
165
722074ef
RS
166(defun save-place-to-alist ()
167 ;; put filename and point in a cons box and then cons that onto the
168 ;; front of the save-place-alist, if save-place is non-nil.
169 ;; Otherwise, just delete that file from the alist.
170 ;; first check to make sure alist has been loaded in from the master
171 ;; file. If not, do so, then feel free to modify the alist. It
172 ;; will be saved again when Emacs is killed.
173 (or save-place-loaded (load-save-place-alist-from-file))
d4466a91 174 (let ((item (or buffer-file-name
1d42e5b6
JL
175 (and dired-directory
176 (if (consp dired-directory)
177 (expand-file-name (car dired-directory))
178 (expand-file-name dired-directory))))))
d4466a91
IK
179 (when (and item
180 (or (not save-place-ignore-files-regexp)
181 (not (string-match save-place-ignore-files-regexp
182 item))))
183 (let ((cell (assoc item save-place-alist))
1d42e5b6
JL
184 (position (cond ((eq major-mode 'hexl-mode)
185 (with-no-warnings
186 (1+ (hexl-current-address))))
187 (dired-directory
188 (let ((filename (dired-get-filename nil t)))
189 (if filename
190 `((dired-filename . ,filename))
191 (point))))
192 (t (point)))))
d4466a91
IK
193 (if cell
194 (setq save-place-alist (delq cell save-place-alist)))
195 (if (and save-place
1d42e5b6
JL
196 (not (and (integerp position)
197 (= position 1)))) ;; Optimize out the degenerate case.
d4466a91
IK
198 (setq save-place-alist
199 (cons (cons item position)
200 save-place-alist)))))))
722074ef 201
467997b7
EZ
202(defun save-place-forget-unreadable-files ()
203 "Remove unreadable files from `save-place-alist'.
204For each entry in the alist, if `file-readable-p' returns nil for the
205filename, remove the entry. Save the new alist \(as the first pair
206may have changed\) back to `save-place-alist'."
207 (interactive)
208 ;; the following was adapted from an in-place filtering function,
209 ;; `filter-mod', used in the original.
210 (unless (null save-place-alist) ;says it better than `when'
211 ;; first, check all except first
212 (let ((fmprev save-place-alist) (fmcur (cdr save-place-alist)))
213 (while fmcur ;not null
214 ;; a value is only saved when it becomes FMPREV.
215 (if (if (string-match save-place-skip-check-regexp (caar fmcur))
216 save-place-save-skipped
217 (file-readable-p (caar fmcur)))
218 (setq fmprev fmcur)
219 (setcdr fmprev (cdr fmcur)))
220 (setq fmcur (cdr fmcur))))
221 ;; test first pair, keep it if OK, otherwise 2nd element, which
222 ;; may be '()
223 (unless (if (string-match save-place-skip-check-regexp
224 (caar save-place-alist))
225 save-place-save-skipped
226 (file-readable-p (caar save-place-alist)))
227 (setq save-place-alist (cdr save-place-alist)))))
228
722074ef 229(defun save-place-alist-to-file ()
e1970249
KF
230 (let ((file (expand-file-name save-place-file))
231 (coding-system-for-write 'utf-8))
7fdbcd83 232 (with-current-buffer (get-buffer-create " *Saved Places*")
722074ef 233 (delete-region (point-min) (point-max))
467997b7
EZ
234 (when save-place-forget-unreadable-files
235 (save-place-forget-unreadable-files))
e1970249
KF
236 (insert (format ";;; -*- coding: %s -*-\n"
237 (symbol-name coding-system-for-write)))
5f29a6c8
KF
238 (let ((print-length nil)
239 (print-level nil))
1c4a85ed 240 (pp save-place-alist (current-buffer)))
e7dc77f6
RS
241 (let ((version-control
242 (cond
243 ((null save-place-version-control) nil)
244 ((eq 'never save-place-version-control) 'never)
245 ((eq 'nospecial save-place-version-control) version-control)
246 (t
247 t))))
6b98170f 248 (condition-case nil
cfde584f 249 ;; Don't use write-file; we don't want this buffer to visit it.
e1970249 250 (write-region (point-min) (point-max) file)
bedb08d4
KF
251 (file-error (message "Saving places: can't write %s" file)))
252 (kill-buffer (current-buffer))))))
722074ef
RS
253
254(defun load-save-place-alist-from-file ()
255 (if (not save-place-loaded)
256 (progn
257 (setq save-place-loaded t)
258 (let ((file (expand-file-name save-place-file)))
259 ;; make sure that the alist does not get overwritten, and then
260 ;; load it if it exists:
261 (if (file-readable-p file)
7fdbcd83
SM
262 ;; don't want to use find-file because we have been
263 ;; adding hooks to it.
264 (with-current-buffer (get-buffer-create " *Saved Places*")
722074ef
RS
265 (delete-region (point-min) (point-max))
266 (insert-file-contents file)
267 (goto-char (point-min))
f1180544 268 (setq save-place-alist
30213927 269 (with-demoted-errors "Error reading save-place-file: %S"
144e38fe 270 (car (read-from-string
30213927 271 (buffer-substring (point-min) (point-max))))))
6cf02570
RS
272
273 ;; If there is a limit, and we're over it, then we'll
274 ;; have to truncate the end of the list:
275 (if save-place-limit
276 (if (<= save-place-limit 0)
277 ;; Zero gets special cased. I'm not thrilled
278 ;; with this, but the loop for >= 1 is tight.
279 (setq save-place-alist nil)
280 ;; Else the limit is >= 1, so enforce it by
281 ;; counting and then `setcdr'ing.
282 (let ((s save-place-alist)
283 (count 1))
284 (while s
285 (if (>= count save-place-limit)
286 (setcdr s nil)
287 (setq count (1+ count)))
288 (setq s (cdr s))))))
f1180544 289
bedb08d4 290 (kill-buffer (current-buffer))))
722074ef
RS
291 nil))))
292
293(defun save-places-to-alist ()
294 ;; go through buffer-list, saving places to alist if save-place is
295 ;; non-nil, deleting them from alist if it is nil.
296 (let ((buf-list (buffer-list)))
297 (while buf-list
298 ;; put this into a save-excursion in case someone is counting on
299 ;; another function in kill-emacs-hook to act on the last buffer
300 ;; they were in:
7fdbcd83 301 (with-current-buffer (car buf-list)
722074ef
RS
302 ;; save-place checks buffer-file-name too, but we can avoid
303 ;; overhead of function call by checking here too.
1d42e5b6
JL
304 (and (or buffer-file-name dired-directory)
305 (save-place-to-alist))
722074ef 306 (setq buf-list (cdr buf-list))))))
b6ace927
RS
307
308(defun save-place-find-file-hook ()
309 (or save-place-loaded (load-save-place-alist-from-file))
310 (let ((cell (assoc buffer-file-name save-place-alist)))
311 (if cell
6f96f4c9 312 (progn
3e214b50 313 (or revert-buffer-in-progress-p
1d42e5b6
JL
314 (and (integerp (cdr cell))
315 (goto-char (cdr cell))))
b6ace927
RS
316 ;; and make sure it will be saved again for later
317 (setq save-place t)))))
318
1d42e5b6
JL
319(declare-function dired-goto-file "dired" (file))
320
d4466a91
IK
321(defun save-place-dired-hook ()
322 "Position the point in a dired buffer."
323 (or save-place-loaded (load-save-place-alist-from-file))
1d42e5b6
JL
324 (let ((cell (assoc (if (consp dired-directory)
325 (expand-file-name (car dired-directory))
326 (expand-file-name dired-directory))
327 save-place-alist)))
d4466a91
IK
328 (if cell
329 (progn
330 (or revert-buffer-in-progress-p
1d42e5b6
JL
331 (if (integerp (cdr cell))
332 (goto-char (cdr cell))
333 (and (assq 'dired-filename (cdr cell))
334 (dired-goto-file (cdr (assq 'dired-filename (cdr cell)))))))
d4466a91
IK
335 ;; and make sure it will be saved again for later
336 (setq save-place t)))))
337
4d2c9a10 338(defun save-place-kill-emacs-hook ()
18b1fecd
RS
339 ;; First update the alist. This loads the old save-place-file if nec.
340 (save-places-to-alist)
341 ;; Now save the alist in the file, if we have ever loaded the file
342 ;; (including just now).
52b9a931 343 (if save-place-loaded
18b1fecd 344 (save-place-alist-to-file)))
b6ace927 345
1a4914f3 346(add-hook 'find-file-hook 'save-place-find-file-hook t)
b6ace927 347
1d42e5b6
JL
348(add-hook 'dired-initial-position-hook 'save-place-dired-hook)
349
845fc5e5
JB
350(unless noninteractive
351 (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
722074ef
RS
352
353(add-hook 'kill-buffer-hook 'save-place-to-alist)
354
355(provide 'saveplace) ; why not...
356
357;;; saveplace.el ends here