saveplace.el 12 KB

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