savehist.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. ;;; savehist.el --- Save minibuffer history
  2. ;; Copyright (C) 1997, 2005-2012 Free Software Foundation, Inc.
  3. ;; Author: Hrvoje Niksic <hniksic@xemacs.org>
  4. ;; Maintainer: FSF
  5. ;; Keywords: minibuffer
  6. ;; Version: 24
  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. ;; Many editors (e.g. Vim) have the feature of saving minibuffer
  20. ;; history to an external file after exit. This package provides the
  21. ;; same feature in Emacs. When set up, it saves recorded minibuffer
  22. ;; histories to a file (`~/.emacs-history' by default). Additional
  23. ;; variables may be specified by customizing
  24. ;; `savehist-additional-variables'.
  25. ;; To use savehist, turn on savehist-mode by putting the following in
  26. ;; `~/.emacs':
  27. ;;
  28. ;; (savehist-mode 1)
  29. ;;
  30. ;; or with customize: `M-x customize-option RET savehist-mode RET'.
  31. ;;
  32. ;; You can also explicitly save history with `M-x savehist-save' and
  33. ;; load it by loading the `savehist-file' with `M-x load-file'.
  34. ;; If you are using a version of Emacs that does not ship with this
  35. ;; package, be sure to have `savehist.el' in a directory that is in
  36. ;; your load-path, and to byte-compile it.
  37. ;;; Code:
  38. (require 'custom)
  39. (eval-when-compile
  40. (require 'cl))
  41. ;; User variables
  42. (defgroup savehist nil
  43. "Save minibuffer history."
  44. :version "22.1"
  45. :group 'minibuffer)
  46. (defcustom savehist-save-minibuffer-history t
  47. "If non-nil, save all recorded minibuffer histories.
  48. If you want to save only specific histories, use `savehist-save-hook' to
  49. modify the value of `savehist-minibuffer-history-variables'."
  50. :type 'boolean
  51. :group 'savehist)
  52. (defcustom savehist-additional-variables ()
  53. "List of additional variables to save.
  54. Each element is a symbol whose value will be persisted across Emacs
  55. sessions that use savehist. The contents of variables should be
  56. printable with the Lisp printer. You don't need to add minibuffer
  57. history variables to this list, all minibuffer histories will be
  58. saved automatically as long as `savehist-save-minibuffer-history' is
  59. non-nil.
  60. User options should be saved with the customize interface. This
  61. list is useful for saving automatically updated variables that are not
  62. minibuffer histories, such as `compile-command' or `kill-ring'."
  63. :type '(repeat variable)
  64. :group 'savehist)
  65. (defcustom savehist-ignored-variables nil ;; '(command-history)
  66. "List of additional variables not to save."
  67. :type '(repeat variable)
  68. :group 'savehist)
  69. (defcustom savehist-file
  70. (locate-user-emacs-file "history" ".emacs-history")
  71. "File name where minibuffer history is saved to and loaded from.
  72. The minibuffer history is a series of Lisp expressions loaded
  73. automatically when `savehist-mode' is turned on. See `savehist-mode'
  74. for more details.
  75. If you want your minibuffer history shared between Emacs and XEmacs,
  76. customize this value and make sure that `savehist-coding-system' is
  77. set to a coding system that exists in both emacsen."
  78. :type 'file
  79. :group 'savehist)
  80. (defcustom savehist-file-modes #o600
  81. "Default permissions of the history file.
  82. This is decimal, not octal. The default is 384 (0600 in octal).
  83. Set to nil to use the default permissions that Emacs uses, typically
  84. mandated by umask. The default is a bit more restrictive to protect
  85. the user's privacy."
  86. :type 'integer
  87. :group 'savehist)
  88. (defcustom savehist-autosave-interval (* 5 60)
  89. "The interval between autosaves of minibuffer history.
  90. If set to nil, disables timer-based autosaving."
  91. :type '(choice (const :tag "Disabled" nil)
  92. (integer :tag "Seconds"))
  93. :group 'savehist)
  94. (defcustom savehist-mode-hook nil
  95. "Hook called when `savehist-mode' is turned on."
  96. :type 'hook
  97. :group 'savehist)
  98. (defcustom savehist-save-hook nil
  99. "Hook called by `savehist-save' before saving the variables.
  100. You can use this hook to influence choice and content of variables to
  101. save."
  102. :type 'hook
  103. :group 'savehist)
  104. ;; This should be capable of representing characters used by Emacs.
  105. ;; We prefer UTF-8 over ISO 2022 because it is well-known outside
  106. ;; Mule. XEmacs prior to 21.5 had UTF-8 provided by an external
  107. ;; package which may not be loaded, which is why we check for version.
  108. (defvar savehist-coding-system (if (and (featurep 'xemacs)
  109. (<= emacs-major-version 21)
  110. (< emacs-minor-version 5))
  111. 'iso-2022-8 'utf-8-unix)
  112. "The coding system savehist uses for saving the minibuffer history.
  113. Changing this value while Emacs is running is supported, but considered
  114. unwise, unless you know what you are doing.")
  115. ;; Internal variables.
  116. (defvar savehist-timer nil)
  117. (defvar savehist-last-checksum nil)
  118. (defvar savehist-minibuffer-history-variables nil
  119. "List of minibuffer histories.
  120. The contents of this variable is built while Emacs is running, and saved
  121. along with minibuffer history. You can change its value off
  122. `savehist-save-hook' to influence which variables are saved.")
  123. (defconst savehist-no-conversion (if (featurep 'xemacs) 'binary 'no-conversion)
  124. "Coding system without any conversion.
  125. This is used for calculating an internal checksum. Should be as fast
  126. as possible, ideally simply exposing the internal representation of
  127. buffer text.")
  128. (defvar savehist-loaded nil
  129. "Whether the history has already been loaded.
  130. This prevents toggling `savehist-mode' from destroying existing
  131. minibuffer history.")
  132. (when (featurep 'xemacs)
  133. ;; Must declare this under XEmacs, which doesn't have built-in
  134. ;; minibuffer history truncation.
  135. (defvar history-length 100))
  136. ;; Functions.
  137. ;;;###autoload
  138. (define-minor-mode savehist-mode
  139. "Toggle saving of minibuffer history (Savehist mode).
  140. With a prefix argument ARG, enable Savehist mode if ARG is
  141. positive, and disable it otherwise. If called from Lisp, enable
  142. the mode if ARG is omitted or nil.
  143. When Savehist mode is enabled, minibuffer history is saved
  144. periodically and when exiting Emacs. When Savehist mode is
  145. enabled for the first time in an Emacs session, it loads the
  146. previous minibuffer history from `savehist-file'.
  147. This mode should normally be turned on from your Emacs init file.
  148. Calling it at any other time replaces your current minibuffer
  149. histories, which is probably undesirable."
  150. :global t
  151. (if (not savehist-mode)
  152. (savehist-uninstall)
  153. (when (and (not savehist-loaded)
  154. (file-exists-p savehist-file))
  155. (condition-case errvar
  156. (progn
  157. ;; Don't set coding-system-for-read -- we rely on the
  158. ;; coding cookie to convey that information. That way, if
  159. ;; the user changes the value of savehist-coding-system,
  160. ;; we can still correctly load the old file.
  161. (load savehist-file nil (not (called-interactively-p 'interactive)))
  162. (setq savehist-loaded t))
  163. (error
  164. ;; Don't install the mode if reading failed. Doing so would
  165. ;; effectively destroy the user's data at the next save.
  166. (setq savehist-mode nil)
  167. (savehist-uninstall)
  168. (signal (car errvar) (cdr errvar)))))
  169. (savehist-install)))
  170. (defun savehist-load ()
  171. "Load the variables stored in `savehist-file' and turn on `savehist-mode'.
  172. If `savehist-file' is in the old format that doesn't record
  173. the value of `savehist-minibuffer-history-variables', that
  174. value is deducted from the contents of the file."
  175. (savehist-mode 1)
  176. ;; Old versions of savehist distributed with XEmacs didn't save
  177. ;; savehist-minibuffer-history-variables. If that variable is nil
  178. ;; after loading the file, try to intuit the intended value.
  179. (when (null savehist-minibuffer-history-variables)
  180. (setq savehist-minibuffer-history-variables
  181. (with-temp-buffer
  182. (ignore-errors
  183. (insert-file-contents savehist-file))
  184. (let ((vars ()) form)
  185. (while (setq form (condition-case nil
  186. (read (current-buffer)) (error nil)))
  187. ;; Each form read is of the form (setq VAR VALUE).
  188. ;; Collect VAR, i.e. (nth form 1).
  189. (push (nth 1 form) vars))
  190. vars)))))
  191. (make-obsolete 'savehist-load 'savehist-mode "22.1")
  192. (defun savehist-install ()
  193. "Hook savehist into Emacs.
  194. Normally invoked by calling `savehist-mode' to set the minor mode.
  195. Installs `savehist-autosave' in `kill-emacs-hook' and on a timer.
  196. To undo this, call `savehist-uninstall'."
  197. (add-hook 'minibuffer-setup-hook 'savehist-minibuffer-hook)
  198. (add-hook 'kill-emacs-hook 'savehist-autosave)
  199. ;; Install an invocation of savehist-autosave on a timer. This
  200. ;; should not cause noticeable delays for users -- savehist-autosave
  201. ;; executes in under 5 ms on my system.
  202. (when (and savehist-autosave-interval
  203. (null savehist-timer))
  204. (setq savehist-timer
  205. (if (featurep 'xemacs)
  206. (start-itimer
  207. "savehist" 'savehist-autosave savehist-autosave-interval
  208. savehist-autosave-interval)
  209. (run-with-timer savehist-autosave-interval
  210. savehist-autosave-interval 'savehist-autosave)))))
  211. (defun savehist-uninstall ()
  212. "Undo installing savehist.
  213. Normally invoked by calling `savehist-mode' to unset the minor mode."
  214. (remove-hook 'minibuffer-setup-hook 'savehist-minibuffer-hook)
  215. (remove-hook 'kill-emacs-hook 'savehist-autosave)
  216. (when savehist-timer
  217. (if (featurep 'xemacs)
  218. (delete-itimer savehist-timer)
  219. (cancel-timer savehist-timer))
  220. (setq savehist-timer nil)))
  221. ;; From XEmacs?
  222. (defvar print-readably)
  223. (defvar print-string-length)
  224. (defun savehist-save (&optional auto-save)
  225. "Save the values of minibuffer history variables.
  226. Unbound symbols referenced in `savehist-additional-variables' are ignored.
  227. If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
  228. and don't save the buffer if they are the same."
  229. (interactive)
  230. (with-temp-buffer
  231. (insert
  232. (format ";; -*- mode: emacs-lisp; coding: %s -*-\n" savehist-coding-system)
  233. ";; Minibuffer history file, automatically generated by `savehist'.\n\n")
  234. (run-hooks 'savehist-save-hook)
  235. (let ((print-length nil)
  236. (print-string-length nil)
  237. (print-level nil)
  238. (print-readably t)
  239. (print-quoted t))
  240. ;; Save the minibuffer histories, along with the value of
  241. ;; savehist-minibuffer-history-variables itself.
  242. (when savehist-save-minibuffer-history
  243. (prin1 `(setq savehist-minibuffer-history-variables
  244. ',savehist-minibuffer-history-variables)
  245. (current-buffer))
  246. (insert ?\n)
  247. (dolist (symbol savehist-minibuffer-history-variables)
  248. (when (and (boundp symbol)
  249. (not (memq symbol savehist-ignored-variables)))
  250. (let ((value (savehist-trim-history (symbol-value symbol)))
  251. excess-space)
  252. (when value ; Don't save empty histories.
  253. (insert "(setq ")
  254. (prin1 symbol (current-buffer))
  255. (insert " '(")
  256. ;; We will print an extra space before the first element.
  257. ;; Record where that is.
  258. (setq excess-space (point))
  259. ;; Print elements of VALUE one by one, carefully.
  260. (dolist (elt value)
  261. (let ((start (point)))
  262. (insert " ")
  263. ;; Try to print and then to read an element.
  264. (condition-case nil
  265. (progn
  266. (prin1 elt (current-buffer))
  267. (save-excursion
  268. (goto-char start)
  269. (read (current-buffer))))
  270. (error
  271. ;; If writing or reading gave an error, comment it out.
  272. (goto-char start)
  273. (insert "\n")
  274. (while (not (eobp))
  275. (insert ";;; ")
  276. (forward-line 1))
  277. (insert "\n")))
  278. (goto-char (point-max))))
  279. ;; Delete the extra space before the first element.
  280. (save-excursion
  281. (goto-char excess-space)
  282. (if (eq (following-char) ?\s)
  283. (delete-region (point) (1+ (point)))))
  284. (insert "))\n"))))))
  285. ;; Save the additional variables.
  286. (dolist (symbol savehist-additional-variables)
  287. (when (boundp symbol)
  288. (let ((value (symbol-value symbol)))
  289. (when (savehist-printable value)
  290. (prin1 `(setq ,symbol ',value) (current-buffer))
  291. (insert ?\n))))))
  292. ;; If autosaving, avoid writing if nothing has changed since the
  293. ;; last write.
  294. (let ((checksum (md5 (current-buffer) nil nil savehist-no-conversion)))
  295. (unless (and auto-save (equal checksum savehist-last-checksum))
  296. ;; Set file-precious-flag when saving the buffer because we
  297. ;; don't want a half-finished write ruining the entire
  298. ;; history. Remember that this is run from a timer and from
  299. ;; kill-emacs-hook, and also that multiple Emacs instances
  300. ;; could write to this file at once.
  301. (let ((file-precious-flag t)
  302. (coding-system-for-write savehist-coding-system))
  303. (write-region (point-min) (point-max) savehist-file nil
  304. (unless (called-interactively-p 'interactive) 'quiet)))
  305. (when savehist-file-modes
  306. (set-file-modes savehist-file savehist-file-modes))
  307. (setq savehist-last-checksum checksum)))))
  308. (defun savehist-autosave ()
  309. "Save the minibuffer history if it has been modified since the last save.
  310. Does nothing if `savehist-mode' is off."
  311. (when savehist-mode
  312. (savehist-save t)))
  313. (defun savehist-trim-history (value)
  314. "Retain only the first `history-length' items in VALUE.
  315. Only used under XEmacs, which doesn't (yet) implement automatic
  316. trimming of history lists to `history-length' items."
  317. (if (and (featurep 'xemacs)
  318. (natnump history-length)
  319. (> (length value) history-length))
  320. ;; Equivalent to `(subseq value 0 history-length)', but doesn't
  321. ;; need cl-extra at run-time.
  322. (loop repeat history-length collect (pop value))
  323. value))
  324. (defun savehist-printable (value)
  325. "Return non-nil if VALUE is printable."
  326. (cond
  327. ;; Quick response for oft-encountered types known to be printable.
  328. ((numberp value))
  329. ((symbolp value))
  330. ;; String without properties
  331. ((and (stringp value)
  332. (equal-including-properties value (substring-no-properties value))))
  333. (t
  334. ;; For others, check explicitly.
  335. (with-temp-buffer
  336. (condition-case nil
  337. (let ((print-readably t) (print-level nil))
  338. ;; Print the value into a buffer...
  339. (prin1 value (current-buffer))
  340. ;; ...and attempt to read it.
  341. (read (point-min-marker))
  342. ;; The attempt worked: the object is printable.
  343. t)
  344. ;; The attempt failed: the object is not printable.
  345. (error nil))))))
  346. (defun savehist-minibuffer-hook ()
  347. (unless (or (eq minibuffer-history-variable t)
  348. ;; XEmacs sets minibuffer-history-variable to t to mean "no
  349. ;; history is being recorded".
  350. (memq minibuffer-history-variable savehist-ignored-variables))
  351. (add-to-list 'savehist-minibuffer-history-variables
  352. minibuffer-history-variable)))
  353. (provide 'savehist)
  354. ;;; savehist.el ends here