PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lisp/saveplace.el

https://github.com/T-force/emacs
Emacs Lisp | 310 lines | 197 code | 48 blank | 65 comment | 10 complexity | 3d00650791f9bad4532c7cac5479e014 MD5 | raw file
  1. ;;; saveplace.el --- automatically save place in files
  2. ;; Copyright (C) 1993-1994, 2001-2011 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 nil
  71. "Maximum number of entries to retain in the list; nil means no limit."
  72. :type '(choice (integer :tag "Entries" :value 1)
  73. (const :tag "No Limit" nil))
  74. :group 'save-place)
  75. (defcustom save-place-forget-unreadable-files t
  76. "Non-nil means forget place in unreadable files.
  77. The filenames in `save-place-alist' that do not match
  78. `save-place-skip-check-regexp' are filtered through
  79. `file-readable-p'. if nil, their alist entries are removed.
  80. You may do this anytime by calling the complementary function,
  81. `save-place-forget-unreadable-files'. When this option is turned on,
  82. this happens automatically before saving `save-place-alist' to
  83. `save-place-file'."
  84. :type 'boolean :group 'save-place)
  85. (defcustom save-place-save-skipped t
  86. "If non-nil, remember files matching `save-place-skip-check-regexp'.
  87. When filtering `save-place-alist' for unreadable files, some will not
  88. be checked, based on said regexp, and instead saved or forgotten based
  89. on this flag."
  90. :type 'boolean :group 'save-place)
  91. (defcustom save-place-skip-check-regexp
  92. ;; thanks to ange-ftp-name-format
  93. "\\`/\\(?:cdrom\\|floppy\\|mnt\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)"
  94. "Regexp whose file names shall not be checked for readability.
  95. When forgetting unreadable files, file names matching this regular
  96. expression shall not be checked for readability, but instead be
  97. subject to `save-place-save-skipped'.
  98. Files for which such a check may be inconvenient include those on
  99. removable and network volumes."
  100. :type 'regexp :group 'save-place)
  101. (defun toggle-save-place (&optional parg)
  102. "Toggle whether to save your place in this file between sessions.
  103. If this mode is enabled, point is recorded when you kill the buffer
  104. or exit Emacs. Visiting this file again will go to that position,
  105. even in a later Emacs session.
  106. If called with a prefix arg, the mode is enabled if and only if
  107. the argument is positive.
  108. To save places automatically in all files, put this in your `.emacs' file:
  109. \(setq-default save-place t\)"
  110. (interactive "P")
  111. (if (not buffer-file-name)
  112. (message "Buffer `%s' not visiting a file" (buffer-name))
  113. (if (and save-place (or (not parg) (<= parg 0)))
  114. (progn
  115. (message "No place will be saved in this file")
  116. (setq save-place nil))
  117. (message "Place will be saved")
  118. (setq save-place t))))
  119. (defun save-place-to-alist ()
  120. ;; put filename and point in a cons box and then cons that onto the
  121. ;; front of the save-place-alist, if save-place is non-nil.
  122. ;; Otherwise, just delete that file from the alist.
  123. ;; first check to make sure alist has been loaded in from the master
  124. ;; file. If not, do so, then feel free to modify the alist. It
  125. ;; will be saved again when Emacs is killed.
  126. (or save-place-loaded (load-save-place-alist-from-file))
  127. (if buffer-file-name
  128. (progn
  129. (let ((cell (assoc buffer-file-name save-place-alist))
  130. (position (if (not (eq major-mode 'hexl-mode))
  131. (point)
  132. (with-no-warnings
  133. (1+ (hexl-current-address))))))
  134. (if cell
  135. (setq save-place-alist (delq cell save-place-alist)))
  136. (if (and save-place
  137. (not (= position 1))) ;; Optimize out the degenerate case.
  138. (setq save-place-alist
  139. (cons (cons buffer-file-name position)
  140. save-place-alist)))))))
  141. (defun save-place-forget-unreadable-files ()
  142. "Remove unreadable files from `save-place-alist'.
  143. For each entry in the alist, if `file-readable-p' returns nil for the
  144. filename, remove the entry. Save the new alist \(as the first pair
  145. may have changed\) back to `save-place-alist'."
  146. (interactive)
  147. ;; the following was adapted from an in-place filtering function,
  148. ;; `filter-mod', used in the original.
  149. (unless (null save-place-alist) ;says it better than `when'
  150. ;; first, check all except first
  151. (let ((fmprev save-place-alist) (fmcur (cdr save-place-alist)))
  152. (while fmcur ;not null
  153. ;; a value is only saved when it becomes FMPREV.
  154. (if (if (string-match save-place-skip-check-regexp (caar fmcur))
  155. save-place-save-skipped
  156. (file-readable-p (caar fmcur)))
  157. (setq fmprev fmcur)
  158. (setcdr fmprev (cdr fmcur)))
  159. (setq fmcur (cdr fmcur))))
  160. ;; test first pair, keep it if OK, otherwise 2nd element, which
  161. ;; may be '()
  162. (unless (if (string-match save-place-skip-check-regexp
  163. (caar save-place-alist))
  164. save-place-save-skipped
  165. (file-readable-p (caar save-place-alist)))
  166. (setq save-place-alist (cdr save-place-alist)))))
  167. (defun save-place-alist-to-file ()
  168. (let ((file (expand-file-name save-place-file))
  169. (coding-system-for-write 'utf-8))
  170. (with-current-buffer (get-buffer-create " *Saved Places*")
  171. (delete-region (point-min) (point-max))
  172. (when save-place-forget-unreadable-files
  173. (save-place-forget-unreadable-files))
  174. (insert (format ";;; -*- coding: %s -*-\n"
  175. (symbol-name coding-system-for-write)))
  176. (let ((print-length nil)
  177. (print-level nil))
  178. (pp (sort save-place-alist
  179. (lambda (a b) (string< (car a) (car b))))
  180. (current-buffer)))
  181. (let ((version-control
  182. (cond
  183. ((null save-place-version-control) nil)
  184. ((eq 'never save-place-version-control) 'never)
  185. ((eq 'nospecial save-place-version-control) version-control)
  186. (t
  187. t))))
  188. (condition-case nil
  189. ;; Don't use write-file; we don't want this buffer to visit it.
  190. (write-region (point-min) (point-max) file)
  191. (file-error (message "Saving places: can't write %s" file)))
  192. (kill-buffer (current-buffer))))))
  193. (defun load-save-place-alist-from-file ()
  194. (if (not save-place-loaded)
  195. (progn
  196. (setq save-place-loaded t)
  197. (let ((file (expand-file-name save-place-file)))
  198. ;; make sure that the alist does not get overwritten, and then
  199. ;; load it if it exists:
  200. (if (file-readable-p file)
  201. ;; don't want to use find-file because we have been
  202. ;; adding hooks to it.
  203. (with-current-buffer (get-buffer-create " *Saved Places*")
  204. (delete-region (point-min) (point-max))
  205. (insert-file-contents file)
  206. (goto-char (point-min))
  207. (setq save-place-alist
  208. (car (read-from-string
  209. (buffer-substring (point-min) (point-max)))))
  210. ;; If there is a limit, and we're over it, then we'll
  211. ;; have to truncate the end of the list:
  212. (if save-place-limit
  213. (if (<= save-place-limit 0)
  214. ;; Zero gets special cased. I'm not thrilled
  215. ;; with this, but the loop for >= 1 is tight.
  216. (setq save-place-alist nil)
  217. ;; Else the limit is >= 1, so enforce it by
  218. ;; counting and then `setcdr'ing.
  219. (let ((s save-place-alist)
  220. (count 1))
  221. (while s
  222. (if (>= count save-place-limit)
  223. (setcdr s nil)
  224. (setq count (1+ count)))
  225. (setq s (cdr s))))))
  226. (kill-buffer (current-buffer))))
  227. nil))))
  228. (defun save-places-to-alist ()
  229. ;; go through buffer-list, saving places to alist if save-place is
  230. ;; non-nil, deleting them from alist if it is nil.
  231. (let ((buf-list (buffer-list)))
  232. (while buf-list
  233. ;; put this into a save-excursion in case someone is counting on
  234. ;; another function in kill-emacs-hook to act on the last buffer
  235. ;; they were in:
  236. (with-current-buffer (car buf-list)
  237. ;; save-place checks buffer-file-name too, but we can avoid
  238. ;; overhead of function call by checking here too.
  239. (and buffer-file-name (save-place-to-alist))
  240. (setq buf-list (cdr buf-list))))))
  241. (defun save-place-find-file-hook ()
  242. (or save-place-loaded (load-save-place-alist-from-file))
  243. (let ((cell (assoc buffer-file-name save-place-alist)))
  244. (if cell
  245. (progn
  246. (or revert-buffer-in-progress-p
  247. (goto-char (cdr cell)))
  248. ;; and make sure it will be saved again for later
  249. (setq save-place t)))))
  250. (defun save-place-kill-emacs-hook ()
  251. ;; First update the alist. This loads the old save-place-file if nec.
  252. (save-places-to-alist)
  253. ;; Now save the alist in the file, if we have ever loaded the file
  254. ;; (including just now).
  255. (if save-place-loaded
  256. (save-place-alist-to-file)))
  257. (add-hook 'find-file-hook 'save-place-find-file-hook t)
  258. (unless noninteractive
  259. (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
  260. (add-hook 'kill-buffer-hook 'save-place-to-alist)
  261. (provide 'saveplace) ; why not...
  262. ;;; saveplace.el ends here