annotate lisp/packages/saveplace.el @ 0:376386a54a3c r19-14

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