Mercurial > emacs
annotate lisp/saveplace.el @ 7506:9fa47d36798a
(Fcurrent_time_zone): Add cast.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 15 May 1994 19:25:21 +0000 |
parents | 6347a8d838c8 |
children | 5554c5e451ae |
rev | line source |
---|---|
4596 | 1 ;;; saveplace.el --- automatically save place in files. |
2 | |
6662
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc. |
4596 | 4 |
5 ;; Author: Karl Fogel <kfogel@cs.oberlin.edu> | |
6 ;; Maintainer: FSF | |
7 ;; Created: July, 1993 | |
6700
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
8 ;; Version: 1.2 |
4596 | 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 ;; | |
6662
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
33 ;; Thanks to Stefan Schoef, who sent a patch with the |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
34 ;; `save-place-version-control' stuff in it. |
4596 | 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 | |
6662
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
62 (defvar save-place-version-control 'nospecial |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
63 "*Controls whether to make numbered backups of master save-place file. |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
64 It can have four values: t, nil, `never', and `nospecial'. The first |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
65 three have the same meaning that they do for the variable |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
66 `version-control', and the final value `nospecial' means just use the |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
67 value of `version-control'.") |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
68 |
4596 | 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. | |
74 If this mode is enabled, point is recorded when you kill the buffer | |
75 or exit Emacs. Visiting this file again will go to that position, | |
76 even in a later Emacs session. | |
77 | |
78 If called with a prefix arg, the mode is enabled if and only if | |
79 the argument is positive. | |
80 | |
81 To 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 | |
110 (cons (cons buffer-file-name (point)) | |
111 save-place-alist)))))) | |
112 | |
113 (defun save-place-alist-to-file () | |
114 (let ((file (expand-file-name save-place-file))) | |
115 (save-excursion | |
116 (message (format "Saving places to %s..." file)) | |
117 (set-buffer (get-buffer-create " *Saved Places*")) | |
118 (delete-region (point-min) (point-max)) | |
119 (if (file-readable-p file) | |
120 (insert-file-contents file)) | |
121 (delete-region (point-min) (point-max)) | |
122 (goto-char (point-min)) | |
123 (print save-place-alist (current-buffer)) | |
6662
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
124 (let ((version-control |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
125 (cond |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
126 ((null save-place-version-control) nil) |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
127 ((eq 'never save-place-version-control) 'never) |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
128 ((eq 'nospecial save-place-version-control) version-control) |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
129 (t |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
130 t)))) |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
131 (write-file file) |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
132 (kill-buffer (current-buffer)) |
bc7bc2a395c6
(save-place-version-control): New var, for determining
Richard M. Stallman <rms@gnu.org>
parents:
4596
diff
changeset
|
133 (message (format "Saving places to %s... done." file)))))) |
4596 | 134 |
135 (defun load-save-place-alist-from-file () | |
136 (if (not save-place-loaded) | |
137 (progn | |
138 (setq save-place-loaded t) | |
139 (let ((file (expand-file-name save-place-file))) | |
140 ;; make sure that the alist does not get overwritten, and then | |
141 ;; load it if it exists: | |
142 (if (file-readable-p file) | |
143 (save-excursion | |
144 (message (format "Loading places from %s..." | |
145 save-place-file)) | |
146 ;; don't want to use find-file because we have been | |
147 ;; adding hooks to it. | |
148 (set-buffer (get-buffer-create " *Saved Places*")) | |
149 (delete-region (point-min) (point-max)) | |
150 (insert-file-contents file) | |
151 (goto-char (point-min)) | |
152 (setq save-place-alist | |
153 (car (read-from-string | |
154 (buffer-substring (point-min) (point-max))))) | |
155 (kill-buffer (current-buffer)) | |
156 (message (format "Loading places from %s... done." file)) | |
157 t) | |
158 t) | |
159 nil)))) | |
160 | |
161 (defun save-places-to-alist () | |
162 ;; go through buffer-list, saving places to alist if save-place is | |
163 ;; non-nil, deleting them from alist if it is nil. | |
164 (let ((buf-list (buffer-list))) | |
165 (while buf-list | |
166 ;; put this into a save-excursion in case someone is counting on | |
167 ;; another function in kill-emacs-hook to act on the last buffer | |
168 ;; they were in: | |
169 (save-excursion | |
170 (set-buffer (car buf-list)) | |
171 ;; save-place checks buffer-file-name too, but we can avoid | |
172 ;; overhead of function call by checking here too. | |
173 (and buffer-file-name (save-place-to-alist)) | |
174 (setq buf-list (cdr buf-list)))))) | |
175 | |
6700
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
176 (defun save-place-find-file-hook () |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
177 (or save-place-loaded (load-save-place-alist-from-file)) |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
178 (let ((cell (assoc buffer-file-name save-place-alist))) |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
179 (if cell |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
180 (progn |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
181 (goto-char (cdr cell)) |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
182 ;; and make sure it will be saved again for later |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
183 (setq save-place t))))) |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
184 |
6877
6347a8d838c8
(save-place-kill-emacs-hook): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
6700
diff
changeset
|
185 (defun save-place-kill-emacs-hook () |
6700
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
186 (save-places-to-alist) |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
187 (save-place-alist-to-file)) |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
188 |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
189 (add-hook 'find-file-hooks 'save-place-find-file-hook t) |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
190 |
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
191 (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook) |
4596 | 192 |
193 (add-hook 'kill-buffer-hook 'save-place-to-alist) | |
194 | |
195 (provide 'saveplace) ; why not... | |
196 | |
197 ;;; saveplace.el ends here | |
6700
db2292ce4894
(save-place-find-file-hook, save-place-kill-emacs-hook): New functions.
Richard M. Stallman <rms@gnu.org>
parents:
6662
diff
changeset
|
198 |