0
|
1 ;;; saveplace.el --- automatically save place in files.
|
|
2
|
|
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Karl Fogel <kfogel@cs.oberlin.edu>
|
|
6 ;; Maintainer: FSF
|
|
7 ;; Created: July, 1993
|
|
8 ;; Version: 1.0
|
|
9 ;; Keywords: bookmarks, placeholders
|
|
10
|
|
11 ;; This file is part of XEmacs.
|
|
12
|
|
13 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
14 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 ;; General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
25 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
26
|
|
27 ;;; Synched up with: Not synched with FSF but close to 19.30.
|
|
28
|
|
29 ;; Automatically save place in files, so that visiting them later
|
|
30 ;; (even during a different Emacs session) automatically moves point
|
|
31 ;; to the saved position, when the file is first found. Uses the
|
|
32 ;; value of buffer-local variable save-place to determine whether to
|
|
33 ;; save position or not.
|
|
34 ;;
|
|
35 ;; Don't autoload this, rather, load it, since it modifies
|
|
36 ;; find-file-hooks and other hooks.
|
|
37
|
|
38 ;; this is what I was using during testing:
|
|
39 ;; (define-key ctl-x-map "p" 'toggle-save-place)
|
|
40
|
|
41 (defvar save-place-alist nil
|
|
42 "Alist of saved places to go back to when revisiting files.
|
|
43 Each element looks like (FILENAME . POSITION);
|
|
44 visiting file FILENAME goes automatically to position POSITION
|
|
45 rather than the beginning of the buffer.
|
|
46 This alist is saved between Emacs sessions.")
|
|
47
|
|
48 (defvar save-place nil
|
|
49 "*Non-nil means automatically save place in each file.
|
|
50 This means when you visit a file, point goes to the last place
|
|
51 where it was when you previously visited the same file.
|
|
52 This variable is automatically buffer-local.
|
|
53
|
|
54 If you wish your place in any file to always be automatically saved,
|
|
55 simply put this in your `~/.emacs' file:
|
|
56
|
|
57 \(setq-default save-place t\)")
|
|
58
|
|
59 (make-variable-buffer-local 'save-place)
|
|
60
|
|
61 (defvar save-place-file "~/.emacs-places"
|
|
62 "*Name of the file that records `save-place-alist' value.")
|
|
63
|
|
64 (defvar save-place-loaded nil
|
|
65 "Non-nil means that the `save-place-file' has been loaded.")
|
|
66
|
|
67 (defun toggle-save-place (&optional parg)
|
|
68 "Toggle whether to save your place in this file between sessions.
|
|
69 If this mode is enabled, point is recorded when you kill the buffer
|
|
70 or exit Emacs. Visiting this file again will go to that position,
|
|
71 even in a later Emacs session.
|
|
72
|
|
73 If called with a prefix arg, the mode is enabled if and only if
|
|
74 the argument is positive.
|
|
75
|
|
76 To save places automatically in all files, put this in your `.emacs' file:
|
|
77
|
|
78 \(setq-default save-place t\)"
|
|
79 (interactive "P")
|
|
80 (if (not buffer-file-name)
|
|
81 (message
|
|
82 (format "Buffer \"%s\" not visiting a file." (buffer-name)))
|
|
83 (if (and save-place (or (not parg) (<= parg 0)))
|
|
84 (progn
|
|
85 (message "No place will be saved in this file.")
|
|
86 (setq save-place nil))
|
|
87 (message "Place will be saved.")
|
|
88 (setq save-place t))))
|
|
89
|
|
90 (defun save-place-to-alist ()
|
|
91 ;; put filename and point in a cons box and then cons that onto the
|
|
92 ;; front of the save-place-alist, if save-place is non-nil.
|
|
93 ;; Otherwise, just delete that file from the alist.
|
|
94 ;; first check to make sure alist has been loaded in from the master
|
|
95 ;; file. If not, do so, then feel free to modify the alist. It
|
|
96 ;; will be saved again when Emacs is killed.
|
|
97 (or save-place-loaded (load-save-place-alist-from-file))
|
|
98 (if buffer-file-name
|
|
99 (progn
|
|
100 (let ((cell (assoc buffer-file-name save-place-alist)))
|
|
101 (if cell
|
|
102 (setq save-place-alist (delq cell save-place-alist))))
|
|
103 (if save-place
|
|
104 (setq save-place-alist
|
|
105 (cons (cons buffer-file-name (point))
|
|
106 save-place-alist))))))
|
|
107
|
|
108 (defun save-place-alist-to-file ()
|
|
109 (let ((file (expand-file-name save-place-file)))
|
|
110 (save-excursion
|
|
111 (message (format "Saving places to %s..." file))
|
|
112 (set-buffer (get-buffer-create " *Saved Places*"))
|
|
113 (delete-region (point-min) (point-max))
|
|
114 (if (file-readable-p file)
|
|
115 (insert-file-contents file))
|
|
116 (delete-region (point-min) (point-max))
|
|
117 (goto-char (point-min))
|
|
118 (print save-place-alist (current-buffer))
|
|
119 (write-file file)
|
|
120 (kill-buffer (current-buffer))
|
|
121 (message (format "Saving places to %s... done." file)))))
|
|
122
|
|
123 (defun load-save-place-alist-from-file ()
|
|
124 (if (not save-place-loaded)
|
|
125 (progn
|
|
126 (setq save-place-loaded t)
|
|
127 (let ((file (expand-file-name save-place-file)))
|
|
128 ;; make sure that the alist does not get overwritten, and then
|
|
129 ;; load it if it exists:
|
|
130 (if (file-readable-p file)
|
|
131 (save-excursion
|
|
132 (message (format "Loading places from %s..."
|
|
133 save-place-file))
|
|
134 ;; don't want to use find-file because we have been
|
|
135 ;; adding hooks to it.
|
|
136 (set-buffer (get-buffer-create " *Saved Places*"))
|
|
137 (delete-region (point-min) (point-max))
|
|
138 (insert-file-contents file)
|
|
139 (goto-char (point-min))
|
|
140 (setq save-place-alist
|
|
141 (car (read-from-string
|
|
142 (buffer-substring (point-min) (point-max)))))
|
|
143 (kill-buffer (current-buffer))
|
|
144 (message (format "Loading places from %s... done." file))
|
|
145 t)
|
|
146 t)
|
|
147 nil))))
|
|
148
|
|
149 (defun save-places-to-alist ()
|
|
150 ;; go through buffer-list, saving places to alist if save-place is
|
|
151 ;; non-nil, deleting them from alist if it is nil.
|
|
152 (let ((buf-list (buffer-list)))
|
|
153 (while buf-list
|
|
154 ;; put this into a save-excursion in case someone is counting on
|
|
155 ;; another function in kill-emacs-hook to act on the last buffer
|
|
156 ;; they were in:
|
|
157 (save-excursion
|
|
158 (set-buffer (car buf-list))
|
|
159 ;; save-place checks buffer-file-name too, but we can avoid
|
|
160 ;; overhead of function call by checking here too.
|
|
161 (and buffer-file-name (save-place-to-alist))
|
|
162 (setq buf-list (cdr buf-list))))))
|
|
163
|
|
164 (add-hook
|
|
165 'find-file-hooks
|
|
166 (function
|
|
167 (lambda ()
|
|
168 (or save-place-loaded (load-save-place-alist-from-file))
|
|
169 (let ((cell (assoc buffer-file-name save-place-alist)))
|
|
170 (if cell
|
|
171 (progn
|
|
172 (goto-char (cdr cell))
|
|
173 ;; and make sure it will be saved again for later.
|
|
174 (setq save-place t)))))))
|
|
175
|
|
176 (add-hook 'kill-emacs-hook
|
|
177 (function
|
|
178 (lambda ()
|
|
179 (progn
|
|
180 (save-places-to-alist)
|
|
181 (save-place-alist-to-file)))))
|
|
182
|
|
183 (add-hook 'kill-buffer-hook 'save-place-to-alist)
|
|
184
|
|
185 (provide 'saveplace) ; why not...
|
|
186
|
|
187 ;;; saveplace.el ends here
|
|
188
|