(clipboard-yank, clipboard-kill-ring-save)
[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
e7dc77f6 8;; Version: 1.1
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.
35;;
722074ef
RS
36;; Don't autoload this, rather, load it, since it modifies
37;; find-file-hooks and other hooks.
38
39;; this is what I was using during testing:
40;; (define-key ctl-x-map "p" 'toggle-save-place)
41
42(defvar save-place-alist nil
43 "Alist of saved places to go back to when revisiting files.
44Each element looks like (FILENAME . POSITION);
45visiting file FILENAME goes automatically to position POSITION
46rather than the beginning of the buffer.
47This alist is saved between Emacs sessions.")
48
49(defvar save-place nil
50 "*Non-nil means automatically save place in each file.
51This means when you visit a file, point goes to the last place
52where it was when you previously visited the same file.
53This variable is automatically buffer-local.
54
55If you wish your place in any file to always be automatically saved,
56simply put this in your `~/.emacs' file:
57
58\(setq-default save-place t\)")
59
60(make-variable-buffer-local 'save-place)
61
62(defvar save-place-file "~/.emacs-places"
63 "*Name of the file that records `save-place-alist' value.")
64
e7dc77f6
RS
65(defvar save-place-version-control 'nospecial
66 "*Controls whether to make numbered backups of master save-place file.
67It can have four values: t, nil, `never', and `nospecial'. The first
68three have the same meaning that they do for the variable
69`version-control', and the final value `nospecial' means just use the
70value of `version-control'.")
71
722074ef
RS
72(defvar save-place-loaded nil
73 "Non-nil means that the `save-place-file' has been loaded.")
74
75(defun toggle-save-place (&optional parg)
76 "Toggle whether to save your place in this file between sessions.
77If this mode is enabled, point is recorded when you kill the buffer
78or exit Emacs. Visiting this file again will go to that position,
79even in a later Emacs session.
80
81If called with a prefix arg, the mode is enabled if and only if
82the argument is positive.
83
84To 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 (point))
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))))))
178
179(add-hook
180 'find-file-hooks
181 (function
182 (lambda ()
183 (or save-place-loaded (load-save-place-alist-from-file))
184 (let ((cell (assoc buffer-file-name save-place-alist)))
185 (if cell
186 (progn
187 (goto-char (cdr cell))
188 ;; and make sure it will be saved again for later.
e7dc77f6
RS
189 (setq save-place t))))))
190 t)
722074ef
RS
191
192(add-hook 'kill-emacs-hook
193 (function
194 (lambda ()
195 (progn
196 (save-places-to-alist)
197 (save-place-alist-to-file)))))
198
199(add-hook 'kill-buffer-hook 'save-place-to-alist)
200
201(provide 'saveplace) ; why not...
202
203;;; saveplace.el ends here