Fix display of keyboard layouts for right-to-left scripts
[emacs.git] / lisp / saveplace.el
Commit [+]AuthorDateLineData
55535639 Pavel Janík2001-07-15 16:15:35 +00001;;; saveplace.el --- automatically save place in files
722074ef Richard M. Stallman1993-08-13 01:08:54 +00002
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 1993-1994, 2001-2015 Free Software Foundation, Inc.
722074ef Richard M. Stallman1993-08-13 01:08:54 +00004
19a448e3 Karl Fogel2001-06-28 16:31:51 +00005;; Author: Karl Fogel <kfogel@red-bean.com>
34dc21db Glenn Morris2014-02-09 17:34:22 -08006;; Maintainer: emacs-devel@gnu.org
722074ef Richard M. Stallman1993-08-13 01:08:54 +00007;; Created: July, 1993
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +00008;; Keywords: bookmarks, placeholders
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000012;; GNU Emacs is free software: you can redistribute it and/or modify
722074ef Richard M. Stallman1993-08-13 01:08:54 +000013;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
Glenn Morris2008-05-06 08:06:51 +000014;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +000016
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000023;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b578f267 Erik Naggum1996-01-14 07:34:30 +000024
6f96f4c9
RS
Richard M. Stallman1996-05-08 20:37:19 +000025;;; Commentary:
26
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +000027;; 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
Richard M. Stallman1994-04-04 01:05:07 +000033;; Thanks to Stefan Schoef, who sent a patch with the
34;; `save-place-version-control' stuff in it.
722074ef Richard M. Stallman1993-08-13 01:08:54 +000035
6f96f4c9
RS
Richard M. Stallman1996-05-08 20:37:19 +000036;;; Code:
37
722074ef Richard M. Stallman1993-08-13 01:08:54 +000038;; this is what I was using during testing:
afa29d95 Michael Olson2008-03-23 22:03:11 +000039;; (define-key ctl-x-map "p" 'toggle-save-place-globally)
722074ef Richard M. Stallman1993-08-13 01:08:54 +000040
20606f3d
RS
Richard M. Stallman1997-04-15 20:33:18 +000041(defgroup save-place nil
42 "Automatically save place in files."
43 :group 'data)
44
45
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +000046(defvar save-place-alist nil
47 "Alist of saved places to go back to when revisiting files.
48Each element looks like (FILENAME . POSITION);
49visiting file FILENAME goes automatically to position POSITION
50rather than the beginning of the buffer.
51This alist is saved between Emacs sessions.")
52
940e5099 Stefan Monnier2013-03-11 22:08:21 -040053(defcustom save-place-file (locate-user-emacs-file "places" ".emacs-places")
9201cc28 Lute Kamstra2008-12-03 05:48:14 +000054 "Name of the file that records `save-place-alist' value."
ece4bae5 Glenn Morris2014-01-26 18:02:28 -080055 :version "24.4" ; added locate-user-emacs-file
9c3e1e4e Stefan Monnier2015-04-27 19:12:11 -040056 :type 'file)
722074ef Richard M. Stallman1993-08-13 01:08:54 +000057
a4ce5ede Richard M. Stallman1997-09-25 00:53:39 +000058(defcustom save-place-version-control nil
9201cc28 Lute Kamstra2008-12-03 05:48:14 +000059 "Controls whether to make numbered backups of master save-place file.
e7dc77f6
RS
Richard M. Stallman1994-04-04 01:05:07 +000060It can have four values: t, nil, `never', and `nospecial'. The first
61three have the same meaning that they do for the variable
62`version-control', and the final value `nospecial' means just use the
20606f3d
RS
Richard M. Stallman1997-04-15 20:33:18 +000063value of `version-control'."
64 :type '(radio (const :tag "Unconditionally" t)
65 (const :tag "For VC Files" nil)
66 (const never)
9c3e1e4e Stefan Monnier2015-04-27 19:12:11 -040067 (const :tag "Use value of `version-control'" nospecial)))
e7dc77f6 Richard M. Stallman1994-04-04 01:05:07 +000068
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +000069(defvar save-place-loaded nil
70 "Non-nil means that the `save-place-file' has been loaded.")
71
2f6a3e79 Glenn Morris2011-08-26 20:52:47 -040072(defcustom save-place-limit 400
20606f3d Richard M. Stallman1997-04-15 20:33:18 +000073 "Maximum number of entries to retain in the list; nil means no limit."
2f6a3e79 Glenn Morris2011-08-26 20:52:47 -040074 :version "24.1" ; nil -> 400
20606f3d Richard M. Stallman1997-04-15 20:33:18 +000075 :type '(choice (integer :tag "Entries" :value 1)
9c3e1e4e Stefan Monnier2015-04-27 19:12:11 -040076 (const :tag "No Limit" nil)))
6cf02570 Richard M. Stallman1995-11-30 00:51:26 +000077
467997b7
EZ
Eli Zaretskii2004-02-21 14:07:11 +000078(defcustom save-place-forget-unreadable-files t
79 "Non-nil means forget place in unreadable files.
80
81The filenames in `save-place-alist' that do not match
82`save-place-skip-check-regexp' are filtered through
99e15133 Juanma Barranquero2014-02-21 01:35:08 +010083`file-readable-p'. If nil, their alist entries are removed.
467997b7
EZ
Eli Zaretskii2004-02-21 14:07:11 +000084
85You may do this anytime by calling the complementary function,
86`save-place-forget-unreadable-files'. When this option is turned on,
87this happens automatically before saving `save-place-alist' to
88`save-place-file'."
9c3e1e4e Stefan Monnier2015-04-27 19:12:11 -040089 :type 'boolean)
467997b7
EZ
Eli Zaretskii2004-02-21 14:07:11 +000090
91(defcustom save-place-save-skipped t
92 "If non-nil, remember files matching `save-place-skip-check-regexp'.
93
94When filtering `save-place-alist' for unreadable files, some will not
95be checked, based on said regexp, and instead saved or forgotten based
96on this flag."
9c3e1e4e Stefan Monnier2015-04-27 19:12:11 -040097 :type 'boolean)
467997b7
EZ
Eli Zaretskii2004-02-21 14:07:11 +000098
99(defcustom save-place-skip-check-regexp
100 ;; thanks to ange-ftp-name-format
101 "\\`/\\(?:cdrom\\|floppy\\|mnt\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)"
102 "Regexp whose file names shall not be checked for readability.
103
104When forgetting unreadable files, file names matching this regular
105expression shall not be checked for readability, but instead be
106subject to `save-place-save-skipped'.
107
108Files for which such a check may be inconvenient include those on
109removable and network volumes."
9c3e1e4e Stefan Monnier2015-04-27 19:12:11 -0400110 :type 'regexp)
467997b7 Eli Zaretskii2004-02-21 14:07:11 +0000111
0311a3fc
TH
Tassilo Horn2012-02-17 21:19:30 +0100112(defcustom save-place-ignore-files-regexp
113 "\\(?:COMMIT_EDITMSG\\|hg-editor-[[:alnum:]]+\\.txt\\|svn-commit\\.tmp\\|bzr_log\\.[[:alnum:]]+\\)$"
5dd11cfe Tassilo Horn2012-03-04 17:56:21 +0100114 "Regexp matching files for which no position should be recorded.
0311a3fc Tassilo Horn2012-02-17 21:19:30 +0100115Useful for temporary file such as commit message files that are
5dd11cfe
TH
Tassilo Horn2012-03-04 17:56:21 +0100116automatically created by the VCS. If set to nil, this feature is
117disabled, i.e., the position is recorded for all files."
e23a3fbe Glenn Morris2012-02-18 11:04:28 -0800118 :version "24.1"
9c3e1e4e Stefan Monnier2015-04-27 19:12:11 -0400119 :type 'regexp)
0311a3fc Tassilo Horn2012-02-17 21:19:30 +0100120
9bb10cbd
JL
Juri Linkov2014-12-28 02:52:50 +0200121(declare-function dired-current-directory "dired" (&optional localp))
122
9c3e1e4e
SM
Stefan Monnier2015-04-27 19:12:11 -0400123(define-obsolete-variable-alias 'save-place 'save-place-mode "25.1")
124;;;###autoload
125(define-minor-mode save-place-mode
126 "Non-nil means automatically save place in each file.
127This means when you visit a file, point goes to the last place
128where it was when you previously visited the same file."
129 :global t
130 :group 'save-place
131 (cond
132 (save-place-mode
133 (add-hook 'find-file-hook 'save-place-find-file-hook t)
134 (add-hook 'dired-initial-position-hook 'save-place-dired-hook)
135 (unless noninteractive
136 (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
137 (add-hook 'kill-buffer-hook 'save-place-to-alist))
138 (t
139 (remove-hook 'find-file-hook 'save-place-find-file-hook t)
140 (remove-hook 'dired-initial-position-hook 'save-place-dired-hook)
141 (remove-hook 'kill-emacs-hook 'save-place-kill-emacs-hook)
142 (remove-hook 'kill-buffer-hook 'save-place-to-alist))))
143
144(make-variable-buffer-local 'save-place-mode) ; Hysterical raisins.
145
146(defun toggle-save-place (&optional parg) ;FIXME: save-place-local-mode!
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +0000147 "Toggle whether to save your place in this file between sessions.
148If this mode is enabled, point is recorded when you kill the buffer
149or exit Emacs. Visiting this file again will go to that position,
150even in a later Emacs session.
151
152If called with a prefix arg, the mode is enabled if and only if
153the argument is positive.
154
865fe16f
CY
Chong Yidong2012-09-17 13:41:04 +0800155To save places automatically in all files, put this in your init
156file:
722074ef Richard M. Stallman1993-08-13 01:08:54 +0000157
99e15133 Juanma Barranquero2014-02-21 01:35:08 +0100158\(setq-default save-place t)"
722074ef Richard M. Stallman1993-08-13 01:08:54 +0000159 (interactive "P")
71f16bda Juri Linkov2014-01-20 10:52:44 +0200160 (if (not (or buffer-file-name (and (derived-mode-p 'dired-mode)
d5145924 Juri Linkov2014-12-27 02:38:34 +0200161 (dired-current-directory))))
1d42e5b6 Juri Linkov2013-12-20 22:20:39 +0200162 (message "Buffer `%s' not visiting a file or directory" (buffer-name))
65b77347
AS
Andy Sawyer2014-02-27 22:44:49 -0800163 (setq save-place (if parg
164 (> (prefix-numeric-value parg) 0)
165 (not save-place)))
166 (message (if save-place
167 "Place will be saved"
168 "No place will be saved in this file"))))
722074ef Richard M. Stallman1993-08-13 01:08:54 +0000169
1d42e5b6
JL
Juri Linkov2013-12-20 22:20:39 +0200170(declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
171
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +0000172(defun save-place-to-alist ()
173 ;; put filename and point in a cons box and then cons that onto the
174 ;; front of the save-place-alist, if save-place is non-nil.
175 ;; Otherwise, just delete that file from the alist.
176 ;; first check to make sure alist has been loaded in from the master
177 ;; file. If not, do so, then feel free to modify the alist. It
178 ;; will be saved again when Emacs is killed.
179 (or save-place-loaded (load-save-place-alist-from-file))
d5145924
JL
Juri Linkov2014-12-27 02:38:34 +0200180 (let* ((directory (and (derived-mode-p 'dired-mode)
181 (dired-current-directory)))
182 (item (or buffer-file-name
183 (and directory
184 (expand-file-name (if (consp directory)
185 (car directory)
186 directory))))))
d4466a91
IK
Ivan Kanis2013-06-14 11:32:01 +0200187 (when (and item
188 (or (not save-place-ignore-files-regexp)
189 (not (string-match save-place-ignore-files-regexp
190 item))))
191 (let ((cell (assoc item save-place-alist))
1d42e5b6
JL
Juri Linkov2013-12-20 22:20:39 +0200192 (position (cond ((eq major-mode 'hexl-mode)
193 (with-no-warnings
194 (1+ (hexl-current-address))))
d5145924 Juri Linkov2014-12-27 02:38:34 +0200195 ((and (derived-mode-p 'dired-mode) directory)
1d42e5b6
JL
Juri Linkov2013-12-20 22:20:39 +0200196 (let ((filename (dired-get-filename nil t)))
197 (if filename
198 `((dired-filename . ,filename))
199 (point))))
200 (t (point)))))
d4466a91
IK
Ivan Kanis2013-06-14 11:32:01 +0200201 (if cell
202 (setq save-place-alist (delq cell save-place-alist)))
203 (if (and save-place
1d42e5b6
JL
Juri Linkov2013-12-20 22:20:39 +0200204 (not (and (integerp position)
205 (= position 1)))) ;; Optimize out the degenerate case.
d4466a91
IK
Ivan Kanis2013-06-14 11:32:01 +0200206 (setq save-place-alist
207 (cons (cons item position)
208 save-place-alist)))))))
722074ef Richard M. Stallman1993-08-13 01:08:54 +0000209
467997b7
EZ
Eli Zaretskii2004-02-21 14:07:11 +0000210(defun save-place-forget-unreadable-files ()
211 "Remove unreadable files from `save-place-alist'.
212For each entry in the alist, if `file-readable-p' returns nil for the
99e15133
JB
Juanma Barranquero2014-02-21 01:35:08 +0100213filename, remove the entry. Save the new alist (as the first pair
214may have changed) back to `save-place-alist'."
467997b7
EZ
Eli Zaretskii2004-02-21 14:07:11 +0000215 (interactive)
216 ;; the following was adapted from an in-place filtering function,
217 ;; `filter-mod', used in the original.
218 (unless (null save-place-alist) ;says it better than `when'
219 ;; first, check all except first
220 (let ((fmprev save-place-alist) (fmcur (cdr save-place-alist)))
221 (while fmcur ;not null
222 ;; a value is only saved when it becomes FMPREV.
223 (if (if (string-match save-place-skip-check-regexp (caar fmcur))
224 save-place-save-skipped
225 (file-readable-p (caar fmcur)))
226 (setq fmprev fmcur)
227 (setcdr fmprev (cdr fmcur)))
228 (setq fmcur (cdr fmcur))))
229 ;; test first pair, keep it if OK, otherwise 2nd element, which
230 ;; may be '()
231 (unless (if (string-match save-place-skip-check-regexp
232 (caar save-place-alist))
233 save-place-save-skipped
234 (file-readable-p (caar save-place-alist)))
235 (setq save-place-alist (cdr save-place-alist)))))
236
722074ef Richard M. Stallman1993-08-13 01:08:54 +0000237(defun save-place-alist-to-file ()
e1970249
KF
Karl Fogel2007-12-02 21:24:38 +0000238 (let ((file (expand-file-name save-place-file))
239 (coding-system-for-write 'utf-8))
7fdbcd83 Stefan Monnier2009-11-13 22:19:45 +0000240 (with-current-buffer (get-buffer-create " *Saved Places*")
722074ef Richard M. Stallman1993-08-13 01:08:54 +0000241 (delete-region (point-min) (point-max))
467997b7
EZ
Eli Zaretskii2004-02-21 14:07:11 +0000242 (when save-place-forget-unreadable-files
243 (save-place-forget-unreadable-files))
e1970249
KF
Karl Fogel2007-12-02 21:24:38 +0000244 (insert (format ";;; -*- coding: %s -*-\n"
245 (symbol-name coding-system-for-write)))
5f29a6c8
KF
Karl Fogel2004-06-08 00:36:04 +0000246 (let ((print-length nil)
247 (print-level nil))
1c4a85ed Karl Fogel2013-03-13 13:54:05 -0500248 (pp save-place-alist (current-buffer)))
e7dc77f6
RS
Richard M. Stallman1994-04-04 01:05:07 +0000249 (let ((version-control
250 (cond
251 ((null save-place-version-control) nil)
252 ((eq 'never save-place-version-control) 'never)
253 ((eq 'nospecial save-place-version-control) version-control)
254 (t
255 t))))
6b98170f Richard M. Stallman2005-04-10 23:32:00 +0000256 (condition-case nil
cfde584f Richard M. Stallman2005-05-29 08:36:26 +0000257 ;; Don't use write-file; we don't want this buffer to visit it.
e1970249 Karl Fogel2007-12-02 21:24:38 +0000258 (write-region (point-min) (point-max) file)
bedb08d4
KF
Karl Fogel2007-12-03 21:37:30 +0000259 (file-error (message "Saving places: can't write %s" file)))
260 (kill-buffer (current-buffer))))))
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +0000261
262(defun load-save-place-alist-from-file ()
263 (if (not save-place-loaded)
264 (progn
265 (setq save-place-loaded t)
266 (let ((file (expand-file-name save-place-file)))
267 ;; make sure that the alist does not get overwritten, and then
268 ;; load it if it exists:
269 (if (file-readable-p file)
7fdbcd83
SM
Stefan Monnier2009-11-13 22:19:45 +0000270 ;; don't want to use find-file because we have been
271 ;; adding hooks to it.
272 (with-current-buffer (get-buffer-create " *Saved Places*")
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +0000273 (delete-region (point-min) (point-max))
274 (insert-file-contents file)
275 (goto-char (point-min))
f1180544 Juanma Barranquero2003-02-04 12:29:42 +0000276 (setq save-place-alist
30213927 Glenn Morris2013-09-11 22:32:57 -0700277 (with-demoted-errors "Error reading save-place-file: %S"
144e38fe Glenn Morris2013-09-08 16:15:17 -0700278 (car (read-from-string
30213927 Glenn Morris2013-09-11 22:32:57 -0700279 (buffer-substring (point-min) (point-max))))))
6cf02570
RS
Richard M. Stallman1995-11-30 00:51:26 +0000280
281 ;; If there is a limit, and we're over it, then we'll
282 ;; have to truncate the end of the list:
283 (if save-place-limit
284 (if (<= save-place-limit 0)
285 ;; Zero gets special cased. I'm not thrilled
286 ;; with this, but the loop for >= 1 is tight.
287 (setq save-place-alist nil)
288 ;; Else the limit is >= 1, so enforce it by
289 ;; counting and then `setcdr'ing.
290 (let ((s save-place-alist)
291 (count 1))
292 (while s
293 (if (>= count save-place-limit)
294 (setcdr s nil)
295 (setq count (1+ count)))
296 (setq s (cdr s))))))
f1180544 Juanma Barranquero2003-02-04 12:29:42 +0000297
bedb08d4 Karl Fogel2007-12-03 21:37:30 +0000298 (kill-buffer (current-buffer))))
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +0000299 nil))))
300
301(defun save-places-to-alist ()
302 ;; go through buffer-list, saving places to alist if save-place is
303 ;; non-nil, deleting them from alist if it is nil.
304 (let ((buf-list (buffer-list)))
305 (while buf-list
306 ;; put this into a save-excursion in case someone is counting on
307 ;; another function in kill-emacs-hook to act on the last buffer
308 ;; they were in:
7fdbcd83 Stefan Monnier2009-11-13 22:19:45 +0000309 (with-current-buffer (car buf-list)
722074ef
RS
Richard M. Stallman1993-08-13 01:08:54 +0000310 ;; save-place checks buffer-file-name too, but we can avoid
311 ;; overhead of function call by checking here too.
71f16bda Juri Linkov2014-01-20 10:52:44 +0200312 (and (or buffer-file-name (and (derived-mode-p 'dired-mode)
d5145924 Juri Linkov2014-12-27 02:38:34 +0200313 (dired-current-directory)))
1d42e5b6 Juri Linkov2013-12-20 22:20:39 +0200314 (save-place-to-alist))
722074ef Richard M. Stallman1993-08-13 01:08:54 +0000315 (setq buf-list (cdr buf-list))))))
b6ace927
RS
Richard M. Stallman1994-04-06 05:04:40 +0000316
317(defun save-place-find-file-hook ()
318 (or save-place-loaded (load-save-place-alist-from-file))
319 (let ((cell (assoc buffer-file-name save-place-alist)))
320 (if cell
6f96f4c9 Richard M. Stallman1996-05-08 20:37:19 +0000321 (progn
3e214b50 Juanma Barranquero2011-04-06 21:38:46 +0200322 (or revert-buffer-in-progress-p
1d42e5b6
JL
Juri Linkov2013-12-20 22:20:39 +0200323 (and (integerp (cdr cell))
324 (goto-char (cdr cell))))
b6ace927
RS
Richard M. Stallman1994-04-06 05:04:40 +0000325 ;; and make sure it will be saved again for later
326 (setq save-place t)))))
327
1d42e5b6
JL
Juri Linkov2013-12-20 22:20:39 +0200328(declare-function dired-goto-file "dired" (file))
329
d4466a91 Ivan Kanis2013-06-14 11:32:01 +0200330(defun save-place-dired-hook ()
99e15133 Juanma Barranquero2014-02-21 01:35:08 +0100331 "Position the point in a Dired buffer."
d4466a91 Ivan Kanis2013-06-14 11:32:01 +0200332 (or save-place-loaded (load-save-place-alist-from-file))
d5145924
JL
Juri Linkov2014-12-27 02:38:34 +0200333 (let* ((directory (and (derived-mode-p 'dired-mode)
334 (dired-current-directory)))
335 (cell (assoc (and directory
336 (expand-file-name (if (consp directory)
337 (car directory)
338 directory)))
339 save-place-alist)))
d4466a91
IK
Ivan Kanis2013-06-14 11:32:01 +0200340 (if cell
341 (progn
342 (or revert-buffer-in-progress-p
d5145924
JL
Juri Linkov2014-12-27 02:38:34 +0200343 (cond
344 ((integerp (cdr cell))
345 (goto-char (cdr cell)))
346 ((and (listp (cdr cell)) (assq 'dired-filename (cdr cell)))
347 (dired-goto-file (cdr (assq 'dired-filename (cdr cell)))))))
d4466a91
IK
Ivan Kanis2013-06-14 11:32:01 +0200348 ;; and make sure it will be saved again for later
349 (setq save-place t)))))
350
4d2c9a10 Richard M. Stallman1994-04-14 12:04:19 +0000351(defun save-place-kill-emacs-hook ()
18b1fecd
RS
Richard M. Stallman1996-03-06 06:28:39 +0000352 ;; First update the alist. This loads the old save-place-file if nec.
353 (save-places-to-alist)
354 ;; Now save the alist in the file, if we have ever loaded the file
355 ;; (including just now).
52b9a931 Richard M. Stallman1996-03-05 04:22:25 +0000356 (if save-place-loaded
18b1fecd Richard M. Stallman1996-03-06 06:28:39 +0000357 (save-place-alist-to-file)))
b6ace927 Richard M. Stallman1994-04-06 05:04:40 +0000358
9c3e1e4e Stefan Monnier2015-04-27 19:12:11 -0400359(provide 'saveplace)
722074ef Richard M. Stallman1993-08-13 01:08:54 +0000360;;; saveplace.el ends here