(Fcall_interactively): In `c' case, clear out the message
[bpt/emacs.git] / lisp / saveplace.el
CommitLineData
722074ef
RS
1;;; saveplace.el --- automatically save place in files.
2
e7dc77f6 3;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
722074ef
RS
4
5;; Author: Karl Fogel <kfogel@cs.oberlin.edu>
6;; Maintainer: FSF
7;; Created: July, 1993
b6ace927 8;; Version: 1.2
722074ef
RS
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;;
e7dc77f6
RS
33;; Thanks to Stefan Schoef, who sent a patch with the
34;; `save-place-version-control' stuff in it.
722074ef
RS
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.
41Each element looks like (FILENAME . POSITION);
42visiting file FILENAME goes automatically to position POSITION
43rather than the beginning of the buffer.
44This alist is saved between Emacs sessions.")
45
46(defvar save-place nil
47 "*Non-nil means automatically save place in each file.
48This means when you visit a file, point goes to the last place
49where it was when you previously visited the same file.
50This variable is automatically buffer-local.
51
52If you wish your place in any file to always be automatically saved,
53simply 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
e7dc77f6
RS
62(defvar save-place-version-control 'nospecial
63 "*Controls whether to make numbered backups of master save-place file.
64It can have four values: t, nil, `never', and `nospecial'. The first
65three have the same meaning that they do for the variable
66`version-control', and the final value `nospecial' means just use the
67value of `version-control'.")
68
722074ef
RS
69(defvar save-place-loaded nil
70 "Non-nil means that the `save-place-file' has been loaded.")
71
72(defun toggle-save-place (&optional parg)
73 "Toggle whether to save your place in this file between sessions.
74If this mode is enabled, point is recorded when you kill the buffer
75or exit Emacs. Visiting this file again will go to that position,
76even in a later Emacs session.
77
78If called with a prefix arg, the mode is enabled if and only if
79the argument is positive.
80
81To save places automatically in all files, put this in your `.emacs' file:
82
83\(setq-default save-place t\)"
84 (interactive "P")
85 (if (not buffer-file-name)
86 (message
87 (format "Buffer \"%s\" not visiting a file." (buffer-name)))
88 (if (and save-place (or (not parg) (<= parg 0)))
89 (progn
90 (message "No place will be saved in this file.")
91 (setq save-place nil))
92 (message "Place will be saved.")
93 (setq save-place t))))
94
95(defun save-place-to-alist ()
96 ;; put filename and point in a cons box and then cons that onto the
97 ;; front of the save-place-alist, if save-place is non-nil.
98 ;; Otherwise, just delete that file from the alist.
99 ;; first check to make sure alist has been loaded in from the master
100 ;; file. If not, do so, then feel free to modify the alist. It
101 ;; will be saved again when Emacs is killed.
102 (or save-place-loaded (load-save-place-alist-from-file))
103 (if buffer-file-name
104 (progn
105 (let ((cell (assoc buffer-file-name save-place-alist)))
106 (if cell
107 (setq save-place-alist (delq cell save-place-alist))))
108 (if save-place
109 (setq save-place-alist
19d0c48a
RS
110 (cons (cons buffer-file-name
111 (if (not (eq major-mode 'hexl-mode))
112 (point)
113 (1+ (hexl-current-address))))
722074ef
RS
114 save-place-alist))))))
115
116(defun save-place-alist-to-file ()
117 (let ((file (expand-file-name save-place-file)))
118 (save-excursion
119 (message (format "Saving places to %s..." file))
120 (set-buffer (get-buffer-create " *Saved Places*"))
121 (delete-region (point-min) (point-max))
122 (if (file-readable-p file)
123 (insert-file-contents file))
124 (delete-region (point-min) (point-max))
125 (goto-char (point-min))
126 (print save-place-alist (current-buffer))
e7dc77f6
RS
127 (let ((version-control
128 (cond
129 ((null save-place-version-control) nil)
130 ((eq 'never save-place-version-control) 'never)
131 ((eq 'nospecial save-place-version-control) version-control)
132 (t
133 t))))
134 (write-file file)
135 (kill-buffer (current-buffer))
136 (message (format "Saving places to %s... done." file))))))
722074ef
RS
137
138(defun load-save-place-alist-from-file ()
139 (if (not save-place-loaded)
140 (progn
141 (setq save-place-loaded t)
142 (let ((file (expand-file-name save-place-file)))
143 ;; make sure that the alist does not get overwritten, and then
144 ;; load it if it exists:
145 (if (file-readable-p file)
146 (save-excursion
147 (message (format "Loading places from %s..."
148 save-place-file))
149 ;; don't want to use find-file because we have been
150 ;; adding hooks to it.
151 (set-buffer (get-buffer-create " *Saved Places*"))
152 (delete-region (point-min) (point-max))
153 (insert-file-contents file)
154 (goto-char (point-min))
155 (setq save-place-alist
156 (car (read-from-string
157 (buffer-substring (point-min) (point-max)))))
158 (kill-buffer (current-buffer))
159 (message (format "Loading places from %s... done." file))
160 t)
161 t)
162 nil))))
163
164(defun save-places-to-alist ()
165 ;; go through buffer-list, saving places to alist if save-place is
166 ;; non-nil, deleting them from alist if it is nil.
167 (let ((buf-list (buffer-list)))
168 (while buf-list
169 ;; put this into a save-excursion in case someone is counting on
170 ;; another function in kill-emacs-hook to act on the last buffer
171 ;; they were in:
172 (save-excursion
173 (set-buffer (car buf-list))
174 ;; save-place checks buffer-file-name too, but we can avoid
175 ;; overhead of function call by checking here too.
176 (and buffer-file-name (save-place-to-alist))
177 (setq buf-list (cdr buf-list))))))
b6ace927
RS
178
179(defun save-place-find-file-hook ()
180 (or save-place-loaded (load-save-place-alist-from-file))
181 (let ((cell (assoc buffer-file-name save-place-alist)))
182 (if cell
183 (progn
47c5a471
RS
184 (or after-find-file-from-revert-buffer
185 (goto-char (cdr cell)))
b6ace927
RS
186 ;; and make sure it will be saved again for later
187 (setq save-place t)))))
188
4d2c9a10 189(defun save-place-kill-emacs-hook ()
b6ace927
RS
190 (save-places-to-alist)
191 (save-place-alist-to-file))
192
193(add-hook 'find-file-hooks 'save-place-find-file-hook t)
194
195(add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook)
722074ef
RS
196
197(add-hook 'kill-buffer-hook 'save-place-to-alist)
198
199(provide 'saveplace) ; why not...
200
201;;; saveplace.el ends here
b6ace927 202