summaryrefslogtreecommitdiff
path: root/lisp/saveplace.el
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2022-09-25 16:15:16 -0400
committerStefan Monnier <monnier@iro.umontreal.ca>2022-09-25 16:15:16 -0400
commit650c20f1ca4e07591a727e1cfcc74b3363d15985 (patch)
tree85d11f6437cde22f410c25e0e5f71a3131ebd07d /lisp/saveplace.el
parent8869332684c2302b5ba1ead4568bbc7ba1c0183e (diff)
parent4b85ae6a24380fb67a3315eaec9233f17a872473 (diff)
downloademacs-650c20f1ca4e07591a727e1cfcc74b3363d15985.tar.gz
emacs-650c20f1ca4e07591a727e1cfcc74b3363d15985.tar.bz2
emacs-650c20f1ca4e07591a727e1cfcc74b3363d15985.zip
Merge 'master' into noverlay
Diffstat (limited to 'lisp/saveplace.el')
-rw-r--r--lisp/saveplace.el85
1 files changed, 57 insertions, 28 deletions
diff --git a/lisp/saveplace.el b/lisp/saveplace.el
index 54599c7e11f..4b133313125 100644
--- a/lisp/saveplace.el
+++ b/lisp/saveplace.el
@@ -1,6 +1,6 @@
-;;; saveplace.el --- automatically save place in files
+;;; saveplace.el --- automatically save place in files -*- lexical-binding:t -*-
-;; Copyright (C) 1993-1994, 2001-2017 Free Software Foundation, Inc.
+;; Copyright (C) 1993-1994, 2001-2022 Free Software Foundation, Inc.
;; Author: Karl Fogel <kfogel@red-bean.com>
;; Maintainer: emacs-devel@gnu.org
@@ -27,7 +27,7 @@
;; Automatically save place in files, so that visiting them later
;; (even during a different Emacs session) automatically moves point
;; to the saved position, when the file is first found. Uses the
-;; value of buffer-local variable save-place to determine whether to
+;; value of buffer-local variable save-place-mode to determine whether to
;; save position or not.
;;
;; Thanks to Stefan Schoef, who sent a patch with the
@@ -42,7 +42,6 @@
"Automatically save place in files."
:group 'data)
-
(defvar save-place-alist nil
"Alist of saved places to go back to when revisiting files.
Each element looks like (FILENAME . POSITION);
@@ -56,7 +55,7 @@ This alist is saved between Emacs sessions.")
:type 'file)
(defcustom save-place-version-control nil
- "Controls whether to make numbered backups of master save-place file.
+ "Controls whether to make numbered backups of master `save-place' file.
It can have four values: t, nil, `never', and `nospecial'. The first
three have the same meaning that they do for the variable
`version-control', and the final value `nospecial' means just use the
@@ -88,6 +87,13 @@ this happens automatically before saving `save-place-alist' to
`save-place-file'."
:type 'boolean)
+(defcustom save-place-abbreviate-file-names nil
+ "If non-nil, abbreviate file names before saving them.
+This can simplify sharing the `save-place-file' file across
+different hosts."
+ :type 'boolean
+ :version "28.1")
+
(defcustom save-place-save-skipped t
"If non-nil, remember files matching `save-place-skip-check-regexp'.
@@ -160,9 +166,6 @@ If this mode is enabled, point is recorded when you kill the buffer
or exit Emacs. Visiting this file again will go to that position,
even in a later Emacs session.
-If called with a prefix arg, the mode is enabled if and only if
-the argument is positive.
-
To save places automatically in all files, put this in your init
file:
@@ -178,13 +181,17 @@ file:
(declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
(defun save-place-to-alist ()
- ;; put filename and point in a cons box and then cons that onto the
- ;; front of the save-place-alist, if save-place is non-nil.
- ;; Otherwise, just delete that file from the alist.
- ;; first check to make sure alist has been loaded in from the master
+ "Add current buffer filename and position to `save-place-alist'.
+Put filename and point in a cons box and then cons that onto the
+front of the `save-place-alist', if `save-place-mode' is non-nil.
+Otherwise, just delete that file from the alist.
+
+If `save-place-abbreviate-file-names' is non-nil, abbreviate the
+file names."
+ ;; First check to make sure alist has been loaded in from the master
;; file. If not, do so, then feel free to modify the alist. It
;; will be saved again when Emacs is killed.
- (or save-place-loaded (load-save-place-alist-from-file))
+ (or save-place-loaded (save-place-load-alist-from-file))
(let* ((directory (and (derived-mode-p 'dired-mode)
(boundp 'dired-subdir-alist)
dired-subdir-alist
@@ -198,6 +205,8 @@ file:
(or (not save-place-ignore-files-regexp)
(not (string-match save-place-ignore-files-regexp
item))))
+ (when save-place-abbreviate-file-names
+ (setq item (abbreviate-file-name item)))
(let ((cell (assoc item save-place-alist))
(position (cond ((eq major-mode 'hexl-mode)
(with-no-warnings
@@ -251,8 +260,8 @@ may have changed) back to `save-place-alist'."
(delete-region (point-min) (point-max))
(when save-place-forget-unreadable-files
(save-place-forget-unreadable-files))
- (insert (format ";;; -*- coding: %s -*-\n"
- (symbol-name coding-system-for-write)))
+ (insert (format ";;; -*- coding: %s; mode: lisp-data -*-\n"
+ coding-system-for-write))
(let ((print-length nil)
(print-level nil))
(pp save-place-alist (current-buffer)))
@@ -269,7 +278,7 @@ may have changed) back to `save-place-alist'."
(file-error (message "Saving places: can't write %s" file)))
(kill-buffer (current-buffer))))))
-(defun load-save-place-alist-from-file ()
+(defun save-place-load-alist-from-file ()
(if (not save-place-loaded)
(progn
(setq save-place-loaded t)
@@ -281,7 +290,11 @@ may have changed) back to `save-place-alist'."
;; adding hooks to it.
(with-current-buffer (get-buffer-create " *Saved Places*")
(delete-region (point-min) (point-max))
- (insert-file-contents file)
+ ;; Make sure our 'coding:' cookie in the save-place
+ ;; file will take effect, in case the caller binds
+ ;; coding-system-for-read.
+ (let (coding-system-for-read)
+ (insert-file-contents file))
(goto-char (point-min))
(setq save-place-alist
(with-demoted-errors "Error reading save-place-file: %S"
@@ -309,8 +322,8 @@ may have changed) back to `save-place-alist'."
nil))))
(defun save-places-to-alist ()
- ;; go through buffer-list, saving places to alist if save-place is
- ;; non-nil, deleting them from alist if it is nil.
+ ;; go through buffer-list, saving places to alist if save-place-mode
+ ;; is non-nil, deleting them from alist if it is nil.
(let ((buf-list (buffer-list)))
(while buf-list
;; put this into a save-excursion in case someone is counting on
@@ -319,15 +332,27 @@ may have changed) back to `save-place-alist'."
(with-current-buffer (car buf-list)
;; save-place checks buffer-file-name too, but we can avoid
;; overhead of function call by checking here too.
- (and (or buffer-file-name (and (derived-mode-p 'dired-mode)
- (boundp 'dired-subdir-alist)
- dired-subdir-alist
- (dired-current-directory)))
- (save-place-to-alist))
+ (when (and (or buffer-file-name
+ (and (derived-mode-p 'dired-mode)
+ (boundp 'dired-subdir-alist)
+ dired-subdir-alist
+ (dired-current-directory)))
+ ;; Don't save place in literally-visited file
+ ;; because this will commonly differ from the place
+ ;; when visiting literally (and
+ ;; `find-file-literally' always places point at the
+ ;; start of the buffer).
+ (not find-file-literally))
+ (save-place-to-alist))
(setq buf-list (cdr buf-list))))))
+(defvar save-place-after-find-file-hook nil
+ "Hook run at the end of `save-place-find-file-hook'.")
+
(defun save-place-find-file-hook ()
- (or save-place-loaded (load-save-place-alist-from-file))
+ "Function added to `find-file-hook' by `save-place-mode'.
+It runs the hook `save-place-after-find-file-hook'."
+ (or save-place-loaded (save-place-load-alist-from-file))
(let ((cell (assoc buffer-file-name save-place-alist)))
(if cell
(progn
@@ -335,13 +360,14 @@ may have changed) back to `save-place-alist'."
(and (integerp (cdr cell))
(goto-char (cdr cell))))
;; and make sure it will be saved again for later
- (setq save-place t)))))
+ (setq save-place-mode t))))
+ (run-hooks 'save-place-after-find-file-hook))
(declare-function dired-goto-file "dired" (file))
(defun save-place-dired-hook ()
"Position the point in a Dired buffer."
- (or save-place-loaded (load-save-place-alist-from-file))
+ (or save-place-loaded (save-place-load-alist-from-file))
(let* ((directory (and (derived-mode-p 'dired-mode)
(boundp 'dired-subdir-alist)
dired-subdir-alist
@@ -360,7 +386,7 @@ may have changed) back to `save-place-alist'."
((and (listp (cdr cell)) (assq 'dired-filename (cdr cell)))
(dired-goto-file (cdr (assq 'dired-filename (cdr cell)))))))
;; and make sure it will be saved again for later
- (setq save-place t)))))
+ (setq save-place-mode t)))))
(defun save-place-kill-emacs-hook ()
;; First update the alist. This loads the old save-place-file if nec.
@@ -370,5 +396,8 @@ may have changed) back to `save-place-alist'."
(if save-place-loaded
(save-place-alist-to-file)))
+(define-obsolete-function-alias 'load-save-place-alist-from-file
+ #'save-place-load-alist-from-file "29.1")
+
(provide 'saveplace)
;;; saveplace.el ends here