| 1 | ;;; saveplace.el --- automatically save place in files |
| 2 | |
| 3 | ;; Copyright (C) 1993-1994, 2001-2014 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Karl Fogel <kfogel@red-bean.com> |
| 6 | ;; Maintainer: emacs-devel@gnu.org |
| 7 | ;; Created: July, 1993 |
| 8 | ;; Keywords: bookmarks, placeholders |
| 9 | |
| 10 | ;; This file is part of GNU Emacs. |
| 11 | |
| 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 13 | ;; it under the terms of the GNU General Public License as published by |
| 14 | ;; the Free Software Foundation, either version 3 of the License, or |
| 15 | ;; (at your option) any later version. |
| 16 | |
| 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 |
| 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | ;;; Commentary: |
| 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 | ;; |
| 33 | ;; Thanks to Stefan Schoef, who sent a patch with the |
| 34 | ;; `save-place-version-control' stuff in it. |
| 35 | |
| 36 | ;;; Code: |
| 37 | |
| 38 | ;; this is what I was using during testing: |
| 39 | ;; (define-key ctl-x-map "p" 'toggle-save-place-globally) |
| 40 | |
| 41 | (defgroup save-place nil |
| 42 | "Automatically save place in files." |
| 43 | :group 'data) |
| 44 | |
| 45 | |
| 46 | (defvar save-place-alist nil |
| 47 | "Alist of saved places to go back to when revisiting files. |
| 48 | Each element looks like (FILENAME . POSITION); |
| 49 | visiting file FILENAME goes automatically to position POSITION |
| 50 | rather than the beginning of the buffer. |
| 51 | This alist is saved between Emacs sessions.") |
| 52 | |
| 53 | (defcustom save-place nil |
| 54 | "Non-nil means automatically save place in each file. |
| 55 | This means when you visit a file, point goes to the last place |
| 56 | where it was when you previously visited the same file. |
| 57 | |
| 58 | If you wish your place in any file to always be automatically |
| 59 | saved, set this to t using the Customize facility, or put the |
| 60 | following code in your init file: |
| 61 | |
| 62 | \(setq-default save-place t) |
| 63 | \(require 'saveplace)" |
| 64 | :type 'boolean |
| 65 | :require 'saveplace |
| 66 | :group 'save-place) |
| 67 | |
| 68 | (make-variable-buffer-local 'save-place) |
| 69 | |
| 70 | (defcustom save-place-file (locate-user-emacs-file "places" ".emacs-places") |
| 71 | "Name of the file that records `save-place-alist' value." |
| 72 | :version "24.4" ; added locate-user-emacs-file |
| 73 | :type 'file |
| 74 | :group 'save-place) |
| 75 | |
| 76 | (defcustom save-place-version-control nil |
| 77 | "Controls whether to make numbered backups of master save-place file. |
| 78 | It can have four values: t, nil, `never', and `nospecial'. The first |
| 79 | three have the same meaning that they do for the variable |
| 80 | `version-control', and the final value `nospecial' means just use the |
| 81 | value of `version-control'." |
| 82 | :type '(radio (const :tag "Unconditionally" t) |
| 83 | (const :tag "For VC Files" nil) |
| 84 | (const never) |
| 85 | (const :tag "Use value of `version-control'" nospecial)) |
| 86 | :group 'save-place) |
| 87 | |
| 88 | (defvar save-place-loaded nil |
| 89 | "Non-nil means that the `save-place-file' has been loaded.") |
| 90 | |
| 91 | (defcustom save-place-limit 400 |
| 92 | "Maximum number of entries to retain in the list; nil means no limit." |
| 93 | :version "24.1" ; nil -> 400 |
| 94 | :type '(choice (integer :tag "Entries" :value 1) |
| 95 | (const :tag "No Limit" nil)) |
| 96 | :group 'save-place) |
| 97 | |
| 98 | (defcustom save-place-forget-unreadable-files t |
| 99 | "Non-nil means forget place in unreadable files. |
| 100 | |
| 101 | The filenames in `save-place-alist' that do not match |
| 102 | `save-place-skip-check-regexp' are filtered through |
| 103 | `file-readable-p'. If nil, their alist entries are removed. |
| 104 | |
| 105 | You may do this anytime by calling the complementary function, |
| 106 | `save-place-forget-unreadable-files'. When this option is turned on, |
| 107 | this happens automatically before saving `save-place-alist' to |
| 108 | `save-place-file'." |
| 109 | :type 'boolean :group 'save-place) |
| 110 | |
| 111 | (defcustom save-place-save-skipped t |
| 112 | "If non-nil, remember files matching `save-place-skip-check-regexp'. |
| 113 | |
| 114 | When filtering `save-place-alist' for unreadable files, some will not |
| 115 | be checked, based on said regexp, and instead saved or forgotten based |
| 116 | on this flag." |
| 117 | :type 'boolean :group 'save-place) |
| 118 | |
| 119 | (defcustom save-place-skip-check-regexp |
| 120 | ;; thanks to ange-ftp-name-format |
| 121 | "\\`/\\(?:cdrom\\|floppy\\|mnt\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)" |
| 122 | "Regexp whose file names shall not be checked for readability. |
| 123 | |
| 124 | When forgetting unreadable files, file names matching this regular |
| 125 | expression shall not be checked for readability, but instead be |
| 126 | subject to `save-place-save-skipped'. |
| 127 | |
| 128 | Files for which such a check may be inconvenient include those on |
| 129 | removable and network volumes." |
| 130 | :type 'regexp :group 'save-place) |
| 131 | |
| 132 | (defcustom save-place-ignore-files-regexp |
| 133 | "\\(?:COMMIT_EDITMSG\\|hg-editor-[[:alnum:]]+\\.txt\\|svn-commit\\.tmp\\|bzr_log\\.[[:alnum:]]+\\)$" |
| 134 | "Regexp matching files for which no position should be recorded. |
| 135 | Useful for temporary file such as commit message files that are |
| 136 | automatically created by the VCS. If set to nil, this feature is |
| 137 | disabled, i.e., the position is recorded for all files." |
| 138 | :version "24.1" |
| 139 | :type 'regexp :group 'save-place) |
| 140 | |
| 141 | (defun toggle-save-place (&optional parg) |
| 142 | "Toggle whether to save your place in this file between sessions. |
| 143 | If this mode is enabled, point is recorded when you kill the buffer |
| 144 | or exit Emacs. Visiting this file again will go to that position, |
| 145 | even in a later Emacs session. |
| 146 | |
| 147 | If called with a prefix arg, the mode is enabled if and only if |
| 148 | the argument is positive. |
| 149 | |
| 150 | To save places automatically in all files, put this in your init |
| 151 | file: |
| 152 | |
| 153 | \(setq-default save-place t)" |
| 154 | (interactive "P") |
| 155 | (if (not (or buffer-file-name (and (derived-mode-p 'dired-mode) |
| 156 | dired-directory))) |
| 157 | (message "Buffer `%s' not visiting a file or directory" (buffer-name)) |
| 158 | (setq save-place (if parg |
| 159 | (> (prefix-numeric-value parg) 0) |
| 160 | (not save-place))) |
| 161 | (message (if save-place |
| 162 | "Place will be saved" |
| 163 | "No place will be saved in this file")))) |
| 164 | |
| 165 | (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep)) |
| 166 | |
| 167 | (defun save-place-to-alist () |
| 168 | ;; put filename and point in a cons box and then cons that onto the |
| 169 | ;; front of the save-place-alist, if save-place is non-nil. |
| 170 | ;; Otherwise, just delete that file from the alist. |
| 171 | ;; first check to make sure alist has been loaded in from the master |
| 172 | ;; file. If not, do so, then feel free to modify the alist. It |
| 173 | ;; will be saved again when Emacs is killed. |
| 174 | (or save-place-loaded (load-save-place-alist-from-file)) |
| 175 | (let ((item (or buffer-file-name |
| 176 | (and (derived-mode-p 'dired-mode) |
| 177 | dired-directory |
| 178 | (expand-file-name (if (consp dired-directory) |
| 179 | (car dired-directory) |
| 180 | dired-directory)))))) |
| 181 | (when (and item |
| 182 | (or (not save-place-ignore-files-regexp) |
| 183 | (not (string-match save-place-ignore-files-regexp |
| 184 | item)))) |
| 185 | (let ((cell (assoc item save-place-alist)) |
| 186 | (position (cond ((eq major-mode 'hexl-mode) |
| 187 | (with-no-warnings |
| 188 | (1+ (hexl-current-address)))) |
| 189 | ((and (derived-mode-p 'dired-mode) |
| 190 | dired-directory) |
| 191 | (let ((filename (dired-get-filename nil t))) |
| 192 | (if filename |
| 193 | `((dired-filename . ,filename)) |
| 194 | (point)))) |
| 195 | (t (point))))) |
| 196 | (if cell |
| 197 | (setq save-place-alist (delq cell save-place-alist))) |
| 198 | (if (and save-place |
| 199 | (not (and (integerp position) |
| 200 | (= position 1)))) ;; Optimize out the degenerate case. |
| 201 | (setq save-place-alist |
| 202 | (cons (cons item position) |
| 203 | save-place-alist))))))) |
| 204 | |
| 205 | (defun save-place-forget-unreadable-files () |
| 206 | "Remove unreadable files from `save-place-alist'. |
| 207 | For each entry in the alist, if `file-readable-p' returns nil for the |
| 208 | filename, remove the entry. Save the new alist (as the first pair |
| 209 | may have changed) back to `save-place-alist'." |
| 210 | (interactive) |
| 211 | ;; the following was adapted from an in-place filtering function, |
| 212 | ;; `filter-mod', used in the original. |
| 213 | (unless (null save-place-alist) ;says it better than `when' |
| 214 | ;; first, check all except first |
| 215 | (let ((fmprev save-place-alist) (fmcur (cdr save-place-alist))) |
| 216 | (while fmcur ;not null |
| 217 | ;; a value is only saved when it becomes FMPREV. |
| 218 | (if (if (string-match save-place-skip-check-regexp (caar fmcur)) |
| 219 | save-place-save-skipped |
| 220 | (file-readable-p (caar fmcur))) |
| 221 | (setq fmprev fmcur) |
| 222 | (setcdr fmprev (cdr fmcur))) |
| 223 | (setq fmcur (cdr fmcur)))) |
| 224 | ;; test first pair, keep it if OK, otherwise 2nd element, which |
| 225 | ;; may be '() |
| 226 | (unless (if (string-match save-place-skip-check-regexp |
| 227 | (caar save-place-alist)) |
| 228 | save-place-save-skipped |
| 229 | (file-readable-p (caar save-place-alist))) |
| 230 | (setq save-place-alist (cdr save-place-alist))))) |
| 231 | |
| 232 | (defun save-place-alist-to-file () |
| 233 | (let ((file (expand-file-name save-place-file)) |
| 234 | (coding-system-for-write 'utf-8)) |
| 235 | (with-current-buffer (get-buffer-create " *Saved Places*") |
| 236 | (delete-region (point-min) (point-max)) |
| 237 | (when save-place-forget-unreadable-files |
| 238 | (save-place-forget-unreadable-files)) |
| 239 | (insert (format ";;; -*- coding: %s -*-\n" |
| 240 | (symbol-name coding-system-for-write))) |
| 241 | (let ((print-length nil) |
| 242 | (print-level nil)) |
| 243 | (pp save-place-alist (current-buffer))) |
| 244 | (let ((version-control |
| 245 | (cond |
| 246 | ((null save-place-version-control) nil) |
| 247 | ((eq 'never save-place-version-control) 'never) |
| 248 | ((eq 'nospecial save-place-version-control) version-control) |
| 249 | (t |
| 250 | t)))) |
| 251 | (condition-case nil |
| 252 | ;; Don't use write-file; we don't want this buffer to visit it. |
| 253 | (write-region (point-min) (point-max) file) |
| 254 | (file-error (message "Saving places: can't write %s" file))) |
| 255 | (kill-buffer (current-buffer)))))) |
| 256 | |
| 257 | (defun load-save-place-alist-from-file () |
| 258 | (if (not save-place-loaded) |
| 259 | (progn |
| 260 | (setq save-place-loaded t) |
| 261 | (let ((file (expand-file-name save-place-file))) |
| 262 | ;; make sure that the alist does not get overwritten, and then |
| 263 | ;; load it if it exists: |
| 264 | (if (file-readable-p file) |
| 265 | ;; don't want to use find-file because we have been |
| 266 | ;; adding hooks to it. |
| 267 | (with-current-buffer (get-buffer-create " *Saved Places*") |
| 268 | (delete-region (point-min) (point-max)) |
| 269 | (insert-file-contents file) |
| 270 | (goto-char (point-min)) |
| 271 | (setq save-place-alist |
| 272 | (with-demoted-errors "Error reading save-place-file: %S" |
| 273 | (car (read-from-string |
| 274 | (buffer-substring (point-min) (point-max)))))) |
| 275 | |
| 276 | ;; If there is a limit, and we're over it, then we'll |
| 277 | ;; have to truncate the end of the list: |
| 278 | (if save-place-limit |
| 279 | (if (<= save-place-limit 0) |
| 280 | ;; Zero gets special cased. I'm not thrilled |
| 281 | ;; with this, but the loop for >= 1 is tight. |
| 282 | (setq save-place-alist nil) |
| 283 | ;; Else the limit is >= 1, so enforce it by |
| 284 | ;; counting and then `setcdr'ing. |
| 285 | (let ((s save-place-alist) |
| 286 | (count 1)) |
| 287 | (while s |
| 288 | (if (>= count save-place-limit) |
| 289 | (setcdr s nil) |
| 290 | (setq count (1+ count))) |
| 291 | (setq s (cdr s)))))) |
| 292 | |
| 293 | (kill-buffer (current-buffer)))) |
| 294 | nil)))) |
| 295 | |
| 296 | (defun save-places-to-alist () |
| 297 | ;; go through buffer-list, saving places to alist if save-place is |
| 298 | ;; non-nil, deleting them from alist if it is nil. |
| 299 | (let ((buf-list (buffer-list))) |
| 300 | (while buf-list |
| 301 | ;; put this into a save-excursion in case someone is counting on |
| 302 | ;; another function in kill-emacs-hook to act on the last buffer |
| 303 | ;; they were in: |
| 304 | (with-current-buffer (car buf-list) |
| 305 | ;; save-place checks buffer-file-name too, but we can avoid |
| 306 | ;; overhead of function call by checking here too. |
| 307 | (and (or buffer-file-name (and (derived-mode-p 'dired-mode) |
| 308 | dired-directory)) |
| 309 | (save-place-to-alist)) |
| 310 | (setq buf-list (cdr buf-list)))))) |
| 311 | |
| 312 | (defun save-place-find-file-hook () |
| 313 | (or save-place-loaded (load-save-place-alist-from-file)) |
| 314 | (let ((cell (assoc buffer-file-name save-place-alist))) |
| 315 | (if cell |
| 316 | (progn |
| 317 | (or revert-buffer-in-progress-p |
| 318 | (and (integerp (cdr cell)) |
| 319 | (goto-char (cdr cell)))) |
| 320 | ;; and make sure it will be saved again for later |
| 321 | (setq save-place t))))) |
| 322 | |
| 323 | (declare-function dired-goto-file "dired" (file)) |
| 324 | |
| 325 | (defun save-place-dired-hook () |
| 326 | "Position the point in a Dired buffer." |
| 327 | (or save-place-loaded (load-save-place-alist-from-file)) |
| 328 | (let ((cell (assoc (and (derived-mode-p 'dired-mode) |
| 329 | dired-directory |
| 330 | (expand-file-name (if (consp dired-directory) |
| 331 | (car dired-directory) |
| 332 | dired-directory))) |
| 333 | save-place-alist))) |
| 334 | (if cell |
| 335 | (progn |
| 336 | (or revert-buffer-in-progress-p |
| 337 | (if (integerp (cdr cell)) |
| 338 | (goto-char (cdr cell)) |
| 339 | (and (assq 'dired-filename (cdr cell)) |
| 340 | (dired-goto-file (cdr (assq 'dired-filename (cdr cell))))))) |
| 341 | ;; and make sure it will be saved again for later |
| 342 | (setq save-place t))))) |
| 343 | |
| 344 | (defun save-place-kill-emacs-hook () |
| 345 | ;; First update the alist. This loads the old save-place-file if nec. |
| 346 | (save-places-to-alist) |
| 347 | ;; Now save the alist in the file, if we have ever loaded the file |
| 348 | ;; (including just now). |
| 349 | (if save-place-loaded |
| 350 | (save-place-alist-to-file))) |
| 351 | |
| 352 | (add-hook 'find-file-hook 'save-place-find-file-hook t) |
| 353 | |
| 354 | (add-hook 'dired-initial-position-hook 'save-place-dired-hook) |
| 355 | |
| 356 | (unless noninteractive |
| 357 | (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook)) |
| 358 | |
| 359 | (add-hook 'kill-buffer-hook 'save-place-to-alist) |
| 360 | |
| 361 | (provide 'saveplace) ; why not... |
| 362 | |
| 363 | ;;; saveplace.el ends here |