(save-place-limit): New variable.
[bpt/emacs.git] / lisp / saveplace.el
1 ;;; saveplace.el --- automatically save place in files.
2
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
4
5 ;; Author: Karl Fogel <kfogel@cs.oberlin.edu>
6 ;; Maintainer: FSF
7 ;; Created: July, 1993
8 ;; Version: 1.2
9 ;; Keywords: bookmarks, placeholders
10
11 ;; This file is part of GNU Emacs.
12
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 2, or (at your option)
16 ;; any later version.
17
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.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
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 ;; this is what I was using during testing:
37 ;; (define-key ctl-x-map "p" 'toggle-save-place)
38
39 (defvar save-place-alist nil
40 "Alist of saved places to go back to when revisiting files.
41 Each element looks like (FILENAME . POSITION);
42 visiting file FILENAME goes automatically to position POSITION
43 rather than the beginning of the buffer.
44 This alist is saved between Emacs sessions.")
45
46 (defvar save-place nil
47 "*Non-nil means automatically save place in each file.
48 This means when you visit a file, point goes to the last place
49 where it was when you previously visited the same file.
50 This variable is automatically buffer-local.
51
52 If you wish your place in any file to always be automatically saved,
53 simply put this in your `~/.emacs' file:
54
55 \(setq-default save-place t\)")
56
57 (make-variable-buffer-local 'save-place)
58
59 (defvar save-place-file "~/.emacs-places"
60 "*Name of the file that records `save-place-alist' value.")
61
62 (defvar save-place-version-control 'nospecial
63 "*Controls whether to make numbered backups of master save-place file.
64 It can have four values: t, nil, `never', and `nospecial'. The first
65 three have the same meaning that they do for the variable
66 `version-control', and the final value `nospecial' means just use the
67 value of `version-control'.")
68
69 (defvar save-place-loaded nil
70 "Non-nil means that the `save-place-file' has been loaded.")
71
72 (defvar save-place-limit nil
73 "Maximum number of entries to retain in the list; nil means no limit.")
74
75 (defun toggle-save-place (&optional parg)
76 "Toggle whether to save your place in this file between sessions.
77 If this mode is enabled, point is recorded when you kill the buffer
78 or exit Emacs. Visiting this file again will go to that position,
79 even in a later Emacs session.
80
81 If called with a prefix arg, the mode is enabled if and only if
82 the argument is positive.
83
84 To save places automatically in all files, put this in your `.emacs' file:
85
86 \(setq-default save-place t\)"
87 (interactive "P")
88 (if (not buffer-file-name)
89 (message
90 (format "Buffer \"%s\" not visiting a file." (buffer-name)))
91 (if (and save-place (or (not parg) (<= parg 0)))
92 (progn
93 (message "No place will be saved in this file.")
94 (setq save-place nil))
95 (message "Place will be saved.")
96 (setq save-place t))))
97
98 (defun save-place-to-alist ()
99 ;; put filename and point in a cons box and then cons that onto the
100 ;; front of the save-place-alist, if save-place is non-nil.
101 ;; Otherwise, just delete that file from the alist.
102 ;; first check to make sure alist has been loaded in from the master
103 ;; file. If not, do so, then feel free to modify the alist. It
104 ;; will be saved again when Emacs is killed.
105 (or save-place-loaded (load-save-place-alist-from-file))
106 (if buffer-file-name
107 (progn
108 (let ((cell (assoc buffer-file-name save-place-alist)))
109 (if cell
110 (setq save-place-alist (delq cell save-place-alist))))
111 (if save-place
112 (setq save-place-alist
113 (cons (cons buffer-file-name
114 (if (not (eq major-mode 'hexl-mode))
115 (point)
116 (1+ (hexl-current-address))))
117 save-place-alist))))))
118
119 (defun save-place-alist-to-file ()
120 (let ((file (expand-file-name save-place-file)))
121 (save-excursion
122 (message (format "Saving places to %s..." file))
123 (set-buffer (get-buffer-create " *Saved Places*"))
124 (delete-region (point-min) (point-max))
125 (if (file-readable-p file)
126 (insert-file-contents file))
127 (delete-region (point-min) (point-max))
128 (goto-char (point-min))
129 (print save-place-alist (current-buffer))
130 (let ((version-control
131 (cond
132 ((null save-place-version-control) nil)
133 ((eq 'never save-place-version-control) 'never)
134 ((eq 'nospecial save-place-version-control) version-control)
135 (t
136 t))))
137 (write-file file)
138 (kill-buffer (current-buffer))
139 (message (format "Saving places to %s...done" file))))))
140
141 (defun load-save-place-alist-from-file ()
142 (if (not save-place-loaded)
143 (progn
144 (setq save-place-loaded t)
145 (let ((file (expand-file-name save-place-file)))
146 ;; make sure that the alist does not get overwritten, and then
147 ;; load it if it exists:
148 (if (file-readable-p file)
149 (save-excursion
150 (message (format "Loading places from %s..."
151 save-place-file))
152 ;; don't want to use find-file because we have been
153 ;; adding hooks to it.
154 (set-buffer (get-buffer-create " *Saved Places*"))
155 (delete-region (point-min) (point-max))
156 (insert-file-contents file)
157 (goto-char (point-min))
158 (setq save-place-alist
159 (car (read-from-string
160 (buffer-substring (point-min) (point-max)))))
161
162 ;; If there is a limit, and we're over it, then we'll
163 ;; have to truncate the end of the list:
164 (if save-place-limit
165 (if (<= save-place-limit 0)
166 ;; Zero gets special cased. I'm not thrilled
167 ;; with this, but the loop for >= 1 is tight.
168 (setq save-place-alist nil)
169 ;; Else the limit is >= 1, so enforce it by
170 ;; counting and then `setcdr'ing.
171 (let ((s save-place-alist)
172 (count 1))
173 (while s
174 (if (>= count save-place-limit)
175 (setcdr s nil)
176 (setq count (1+ count)))
177 (setq s (cdr s))))))
178
179 (kill-buffer (current-buffer))
180 (message (format "Loading places from %s...done" file))
181 t)
182 t)
183 nil))))
184
185 (defun save-places-to-alist ()
186 ;; go through buffer-list, saving places to alist if save-place is
187 ;; non-nil, deleting them from alist if it is nil.
188 (let ((buf-list (buffer-list)))
189 (while buf-list
190 ;; put this into a save-excursion in case someone is counting on
191 ;; another function in kill-emacs-hook to act on the last buffer
192 ;; they were in:
193 (save-excursion
194 (set-buffer (car buf-list))
195 ;; save-place checks buffer-file-name too, but we can avoid
196 ;; overhead of function call by checking here too.
197 (and buffer-file-name (save-place-to-alist))
198 (setq buf-list (cdr buf-list))))))
199
200 (defun save-place-find-file-hook ()
201 (or save-place-loaded (load-save-place-alist-from-file))
202 (let ((cell (assoc buffer-file-name save-place-alist)))
203 (if cell
204 (progn
205 (or after-find-file-from-revert-buffer
206 (goto-char (cdr cell)))
207 ;; and make sure it will be saved again for later
208 (setq save-place t)))))
209
210 (defun save-place-kill-emacs-hook ()
211 (save-places-to-alist)
212 (save-place-alist-to-file))
213
214 (add-hook 'find-file-hooks 'save-place-find-file-hook t)
215
216 (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook)
217
218 (add-hook 'kill-buffer-hook 'save-place-to-alist)
219
220 (provide 'saveplace) ; why not...
221
222 ;;; saveplace.el ends here
223