remember.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. ;;; remember --- a mode for quickly jotting down things to remember
  2. ;; Copyright (C) 1999-2001, 2003-2017 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@gnu.org>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Created: 29 Mar 1999
  6. ;; Version: 2.0
  7. ;; Keywords: data memory todo pim
  8. ;; URL: http://gna.org/projects/remember-el/
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; * The idea
  22. ;;
  23. ;; Todo lists, schedules, phone databases... everything we use
  24. ;; databases for is really just a way to extend the power of our
  25. ;; memory. To be able to remember what our conscious mind may not
  26. ;; currently have access to.
  27. ;;
  28. ;; There are many different databases out there -- and good ones --
  29. ;; which this mode is not trying to replace. Rather, it's how that
  30. ;; data gets there that's the question. Most of the time, we just
  31. ;; want to say "Remember so-and-so's phone number, or that I have to
  32. ;; buy dinner for the cats tonight." That's the FACT. How it's
  33. ;; stored is really the computer's problem. But at this point in
  34. ;; time, it's most definitely also the user's problem, and sometimes
  35. ;; so laboriously so that people just let data slip, rather than
  36. ;; expend the effort to record it.
  37. ;;
  38. ;; "Remember" is a mode for remembering data. It uses whatever
  39. ;; back-end is appropriate to record and correlate the data, but it's
  40. ;; main intention is to allow you to express as _little_ structure as
  41. ;; possible up front. If you later want to express more powerful
  42. ;; relationships between your data, or state assumptions that were at
  43. ;; first too implicit to be recognized, you can "study" the data later
  44. ;; and rearrange it. But the initial "just remember this" impulse
  45. ;; should be as close to simply throwing the data at Emacs as
  46. ;; possible.
  47. ;;
  48. ;; * Implementation
  49. ;;
  50. ;; Hyperbole, as a data presentation tool, always struck me as being
  51. ;; very powerful, but it seemed to require a lot of "front-end" work
  52. ;; before that data was really available. The problem with BBDB, or
  53. ;; keeping up a Bibl-mode file, is that you have to use different
  54. ;; functions to record the data, and it always takes time to stop what
  55. ;; you're doing, format the data in the manner expected by that
  56. ;; particular data interface, and then resume your work.
  57. ;;
  58. ;; With "remember", you just hit `M-x remember' (you'd probably want
  59. ;; to bind this to an easily accessible keystroke, like C-x M-r), slam
  60. ;; in your text however you like, and then hit C-c C-c. It will file
  61. ;; the data away for later retrieval, and possibly indexing.
  62. ;;
  63. ;; Indexing is to data what "studying" is in the real world. What you
  64. ;; do when you study (or lucubrate, for some of us) is to realize
  65. ;; certain relationships implicit in the data, so that you can make
  66. ;; use of those relationships. Expressing that a certain quote you
  67. ;; remembered was a religious quote, and that you want the ability to
  68. ;; pull up all quotes of a religious nature, is what studying does.
  69. ;; This is a more labor intensive task than the original remembering
  70. ;; of the data, and it's typical in real life to set aside a special
  71. ;; period of time for doing this work.
  72. ;;
  73. ;; "Remember" works in the same way. When you enter data, either by
  74. ;; typing it into a buffer, or using the contents of the selected
  75. ;; region, it will store that data -- unindexed, uninterpreted -- in a
  76. ;; data pool. It will also try to remember as much context
  77. ;; information as possible (any text properties that were set, where
  78. ;; you copied it from, when, how, etc). Later, you can walk through
  79. ;; your accumulated set of data (both organized, and unorganized) and
  80. ;; easily begin moving things around, and making annotations that will
  81. ;; express the full meaning of that data, as far as you know it.
  82. ;;
  83. ;; Obviously this latter stage is more user-interface intensive, and
  84. ;; it would be nice if "remember" could do it as elegantly as
  85. ;; possible, rather than requiring a billion keystrokes to reorganize
  86. ;; your hierarchy. Well, as the future arrives, hopefully experience
  87. ;; and user feedback will help to make this as intuitive a tool as
  88. ;; possible.
  89. ;;
  90. ;; * Future Goals
  91. ;;
  92. ;; This tool hopes to track (and by doing it with as little new code
  93. ;; as possible):
  94. ;;
  95. ;; - The raw data that gets entered
  96. ;;
  97. ;; - The relationships between that data (either determined
  98. ;; implicitly by parsing the input, or explicitly by the user's
  99. ;; studying the data).
  100. ;;
  101. ;; - Revisioning of the data
  102. ;;
  103. ;; - Where it came from, and any context information that can be
  104. ;; programmatically determined.
  105. ;;
  106. ;; - Allowing particular views of the initially amorphous data pool
  107. ;; (ala the Xanadu concept).
  108. ;;
  109. ;; - Storage of the data in a manner most appropriate to that data,
  110. ;; such as keeping address-book type information in BBDB, etc.
  111. ;;
  112. ;; * Using "remember"
  113. ;;
  114. ;; As a rough beginning, what I do is to keep my `remember-data-file' in
  115. ;; outline-mode format, with a final entry called "* Raw data". Then,
  116. ;; at intervals, I can move the data that gets appended there into
  117. ;; other places. But certainly this should evolve into an intuitive
  118. ;; mechanism for shuffling data off to its appropriate corner of the
  119. ;; universe.
  120. ;;
  121. ;; To map the primary remember function to the keystroke F8, do the
  122. ;; following.
  123. ;;
  124. ;; (autoload 'remember "remember" nil t)
  125. ;;
  126. ;; (define-key global-map [f8] 'remember)
  127. ;;
  128. ;; * Feedback
  129. ;;
  130. ;; If Emacs could become a more intelligent data store, where
  131. ;; brainstorming would focus on the IDEAS involved -- rather than the
  132. ;; structuring and format of those ideas, or having to stop your
  133. ;; current flow of work in order to record them -- it would map much
  134. ;; more closely to how the mind (well, at least mine) works, and hence
  135. ;; would eliminate that very manual-ness which computers from the very
  136. ;; beginning have been championed as being able to reduce.
  137. ;;
  138. ;; Have you ever noticed that having a laptop to write on doesn't
  139. ;; _actually_ increase the amount of quality material that you turn
  140. ;; out, in the long run? Perhaps its because the time we save
  141. ;; electronically in one way, we're losing electronically in another;
  142. ;; the tool should never dominate one's focus. As the mystic
  143. ;; Faridu'd-Din `Attar wrote: "Be occupied as little as possible with
  144. ;; things of the outer world but much with things of the inner world;
  145. ;; then right action will overcome inaction."
  146. ;;
  147. ;; * Diary integration
  148. ;;
  149. ;; To use, add the following to your .emacs:
  150. ;;
  151. ;; ;; This should be before other entries that may return t
  152. ;; (add-to-list 'remember-handler-functions 'remember-diary-extract-entries)
  153. ;;
  154. ;; This module recognizes entries of the form
  155. ;;
  156. ;; DIARY: ....
  157. ;;
  158. ;; and puts them in your ~/.diary (or remember-diary-file) together
  159. ;; with an annotation. Dates in the form YYYY.MM.DD are converted to
  160. ;; YYYY-MM-DD so that diary can understand them.
  161. ;;
  162. ;; For example:
  163. ;;
  164. ;; DIARY: 2003.08.12 Sacha's birthday
  165. ;;
  166. ;; is stored as
  167. ;;
  168. ;; 2003.08.12 Sacha's birthday
  169. ;;; History:
  170. ;;; Code:
  171. (defconst remember-version "2.0"
  172. "This version of remember.")
  173. (defgroup remember nil
  174. "A mode to remember information."
  175. :group 'data)
  176. ;;; User Variables:
  177. (defcustom remember-mode-hook nil
  178. "Functions run upon entering `remember-mode'."
  179. :type 'hook
  180. :options '(flyspell-mode turn-on-auto-fill org-remember-apply-template)
  181. :group 'remember)
  182. (defcustom remember-in-new-frame nil
  183. "Non-nil means use a separate frame for capturing remember data."
  184. :type 'boolean
  185. :group 'remember)
  186. (defcustom remember-register ?R
  187. "The register in which the window configuration is stored."
  188. :type 'character
  189. :group 'remember)
  190. (defcustom remember-filter-functions nil
  191. "Functions run to filter remember data.
  192. All functions are run in the remember buffer."
  193. :type 'hook
  194. :group 'remember)
  195. (defcustom remember-handler-functions '(remember-append-to-file)
  196. "Functions run to process remember data.
  197. Each function is called with the current buffer narrowed to what the
  198. user wants remembered.
  199. If any function returns non-nil, the data is assumed to have been
  200. recorded somewhere by that function."
  201. :type 'hook
  202. :options '(remember-store-in-mailbox
  203. remember-append-to-file
  204. remember-store-in-files
  205. remember-diary-extract-entries
  206. org-remember-handler)
  207. :group 'remember)
  208. (defcustom remember-all-handler-functions nil
  209. "If non-nil every function in `remember-handler-functions' is called."
  210. :type 'boolean
  211. :group 'remember)
  212. ;; See below for more user variables.
  213. ;;; Internal Variables:
  214. (defvar remember-buffer "*Remember*"
  215. "The name of the remember data entry buffer.")
  216. (defcustom remember-save-after-remembering t
  217. "Non-nil means automatically save after remembering."
  218. :type 'boolean
  219. :group 'remember)
  220. ;;; User Functions:
  221. (defcustom remember-annotation-functions '(buffer-file-name)
  222. "Hook that returns an annotation to be inserted into the remember buffer."
  223. :type 'hook
  224. :options '(org-remember-annotation buffer-file-name)
  225. :group 'remember)
  226. (defvar remember-annotation nil
  227. "Current annotation.")
  228. (defvar remember-initial-contents nil
  229. "Initial contents to place into *Remember* buffer.")
  230. (defcustom remember-before-remember-hook nil
  231. "Functions run before switching to the *Remember* buffer."
  232. :type 'hook
  233. :group 'remember)
  234. (defcustom remember-run-all-annotation-functions-flag nil
  235. "Non-nil means use all annotations returned by `remember-annotation-functions'."
  236. :type 'boolean
  237. :group 'remember)
  238. ;;;###autoload
  239. (defun remember (&optional initial)
  240. "Remember an arbitrary piece of data.
  241. INITIAL is the text to initially place in the *Remember* buffer,
  242. or nil to bring up a blank *Remember* buffer.
  243. With a prefix or a visible region, use the region as INITIAL."
  244. (interactive
  245. (list (when (or current-prefix-arg
  246. (and mark-active
  247. transient-mark-mode))
  248. (buffer-substring (region-beginning) (region-end)))))
  249. (funcall (if remember-in-new-frame
  250. #'frameset-to-register
  251. #'window-configuration-to-register) remember-register)
  252. (let* ((annotation
  253. (if remember-run-all-annotation-functions-flag
  254. (mapconcat 'identity
  255. (delq nil
  256. (mapcar 'funcall remember-annotation-functions))
  257. "\n")
  258. (run-hook-with-args-until-success
  259. 'remember-annotation-functions)))
  260. (buf (get-buffer-create remember-buffer)))
  261. (run-hooks 'remember-before-remember-hook)
  262. (funcall (if remember-in-new-frame
  263. #'switch-to-buffer-other-frame
  264. #'switch-to-buffer-other-window) buf)
  265. (if remember-in-new-frame
  266. (set-window-dedicated-p
  267. (get-buffer-window (current-buffer) (selected-frame)) t))
  268. (setq buffer-offer-save t)
  269. (remember-mode)
  270. (when (= (point-max) (point-min))
  271. (when initial (insert initial))
  272. (setq remember-annotation annotation)
  273. (when remember-initial-contents (insert remember-initial-contents))
  274. (when (and (stringp annotation)
  275. (not (equal annotation "")))
  276. (insert "\n\n" annotation))
  277. (setq remember-initial-contents nil)
  278. (goto-char (point-min)))
  279. (message "Use C-c C-c to remember the data.")))
  280. ;;;###autoload
  281. (defun remember-other-frame (&optional initial)
  282. "Call `remember' in another frame."
  283. (interactive
  284. (list (when current-prefix-arg
  285. (buffer-substring (point) (mark)))))
  286. (let ((remember-in-new-frame t))
  287. (remember initial)))
  288. (defsubst remember-mail-date (&optional rfc822-p)
  289. "Return a simple date. Nothing fancy."
  290. (format-time-string (if rfc822-p "%a, %e %b %Y %T %z" "%a %b %e %T %Y")))
  291. (defun remember-buffer-desc ()
  292. "Using the first line of the current buffer, create a short description."
  293. (buffer-substring (point-min)
  294. (save-excursion
  295. (goto-char (point-min))
  296. (end-of-line)
  297. (if (> (- (point) (point-min)) 60)
  298. (goto-char (+ (point-min) 60)))
  299. (point))))
  300. ;; Remembering to UNIX mailboxes
  301. (defcustom remember-mailbox "~/Mail/remember"
  302. "The file in which to store remember data as mail."
  303. :type 'file
  304. :group 'remember)
  305. (defcustom remember-default-priority "medium"
  306. "The default priority for remembered mail messages."
  307. :type 'string
  308. :group 'remember)
  309. (defun remember-store-in-mailbox ()
  310. "Store remember data as if it were incoming mail.
  311. In which case `remember-mailbox' should be the name of the mailbox.
  312. Each piece of pseudo-mail created will have an `X-Todo-Priority'
  313. field, for the purpose of appropriate splitting."
  314. (let ((who (read-string "Who is this item related to? "))
  315. (moment (format "%.0f" (float-time)))
  316. (desc (remember-buffer-desc))
  317. (text (buffer-string)))
  318. (with-temp-buffer
  319. (insert (format "From %s %s
  320. Date: %s
  321. From: %s
  322. Message-Id: <remember-%s@%s>
  323. X-Todo-Priority: %s
  324. To: %s <%s>
  325. Subject: %s\n\n"
  326. (user-login-name)
  327. (remember-mail-date)
  328. (remember-mail-date t)
  329. who
  330. moment (system-name)
  331. remember-default-priority
  332. (user-full-name) user-mail-address
  333. desc))
  334. (let ((here (point)))
  335. (insert text)
  336. (unless (bolp)
  337. (insert "\n"))
  338. (insert "\n")
  339. (goto-char here)
  340. (while (re-search-forward "^\\(From[: ]\\)" nil t)
  341. (replace-match ">\\1")))
  342. (append-to-file (point-min) (point-max) remember-mailbox)
  343. t)))
  344. ;; Remembering to plain files
  345. (defcustom remember-data-file (locate-user-emacs-file "notes" ".notes")
  346. "The file in which to store unprocessed data.
  347. When set via customize, visited file of the notes buffer (if it
  348. exists) might be changed."
  349. :version "24.4" ; added locate-user-emacs-file
  350. :type 'file
  351. :set (lambda (symbol value)
  352. (let ((buf (find-buffer-visiting (default-value symbol))))
  353. (set-default symbol value)
  354. (when (buffer-live-p buf)
  355. (with-current-buffer buf
  356. (set-visited-file-name
  357. (expand-file-name remember-data-file))))))
  358. :initialize 'custom-initialize-default
  359. :group 'remember)
  360. (defcustom remember-leader-text "** "
  361. "The text used to begin each remember item."
  362. :type 'string
  363. :group 'remember)
  364. (defun remember-append-to-file ()
  365. "Remember, with description DESC, the given TEXT."
  366. (let* ((text (buffer-string))
  367. (desc (remember-buffer-desc))
  368. (remember-text (concat "\n" remember-leader-text (current-time-string)
  369. " (" desc ")\n\n" text
  370. (save-excursion (goto-char (point-max))
  371. (if (bolp) nil "\n"))))
  372. (buf (find-buffer-visiting remember-data-file)))
  373. (if buf
  374. (with-current-buffer buf
  375. (save-excursion
  376. (goto-char (point-max))
  377. (insert remember-text))
  378. (if remember-save-after-remembering (save-buffer)))
  379. (append-to-file remember-text nil remember-data-file))))
  380. (defun remember-region (&optional beg end)
  381. "Remember the data from BEG to END.
  382. It is called from within the *Remember* buffer to save the text
  383. that was entered.
  384. If BEG and END are nil, the entire buffer will be remembered.
  385. If you want to remember a region, supply a universal prefix to
  386. `remember' instead. For example: \\[universal-argument] \\[remember] RET."
  387. ;; Sacha: I have no idea where remember.el gets this context information, but
  388. ;; you can just use remember-annotation-functions.
  389. (interactive)
  390. (let ((b (or beg (min (point) (or (mark) (point-min)))))
  391. (e (or end (max (point) (or (mark) (point-max))))))
  392. (save-restriction
  393. (narrow-to-region b e)
  394. (if remember-all-handler-functions
  395. (run-hooks 'remember-handler-functions)
  396. (run-hook-with-args-until-success 'remember-handler-functions))
  397. (remember-destroy))))
  398. (defcustom remember-data-directory "~/remember"
  399. "The directory in which to store remember data as files.
  400. Used by `remember-store-in-files'."
  401. :type 'directory
  402. :version "24.4"
  403. :group 'remember)
  404. (defcustom remember-directory-file-name-format "%Y-%m-%d_%T-%z"
  405. "Format string for the file name in which to store unprocessed data.
  406. This is passed to `format-time-string'.
  407. Used by `remember-store-in-files'."
  408. :type 'string
  409. :version "24.4"
  410. :group 'remember)
  411. (defun remember-store-in-files ()
  412. "Store remember data in a file in `remember-data-directory'.
  413. The file is named by calling `format-time-string' using
  414. `remember-directory-file-name-format' as the format string."
  415. (let ((name (format-time-string remember-directory-file-name-format))
  416. (text (buffer-string)))
  417. (with-temp-buffer
  418. (insert text)
  419. (write-file (convert-standard-filename
  420. (format "%s/%s" remember-data-directory name))))))
  421. ;;;###autoload
  422. (defun remember-clipboard ()
  423. "Remember the contents of the current clipboard.
  424. Most useful for remembering things from other applications."
  425. (interactive)
  426. (remember (current-kill 0)))
  427. (defun remember-finalize ()
  428. "Remember the contents of the current buffer."
  429. (interactive)
  430. (remember-region (point-min) (point-max)))
  431. ;; Org needs this
  432. (define-obsolete-function-alias 'remember-buffer 'remember-finalize "23.1")
  433. (defun remember-destroy ()
  434. "Destroy the current *Remember* buffer."
  435. (interactive)
  436. (when (equal remember-buffer (buffer-name))
  437. (kill-buffer (current-buffer))
  438. (jump-to-register remember-register)))
  439. ;;; Diary integration
  440. (defcustom remember-diary-file nil
  441. "File for extracted diary entries.
  442. If this is nil, then `diary-file' will be used instead."
  443. :type '(choice (const :tag "diary-file" nil) file)
  444. :group 'remember)
  445. (defvar calendar-date-style) ; calendar.el
  446. (defun remember-diary-convert-entry (entry)
  447. "Translate MSG to an entry readable by diary."
  448. (save-match-data
  449. (when remember-annotation
  450. (setq entry (concat entry " " remember-annotation)))
  451. (if (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)" entry)
  452. (progn
  453. ;; For calendar-date-style. This costs us nothing because
  454. ;; the call to diary-make-entry below loads diary-lib
  455. ;; which requires calendar.
  456. (require 'calendar)
  457. (replace-match
  458. (cond ((eq calendar-date-style 'european)
  459. (concat (match-string 3 entry) "/"
  460. (match-string 2 entry) "/"
  461. (match-string 1 entry)))
  462. ((eq calendar-date-style 'iso)
  463. (concat (match-string 1 entry) "-"
  464. (match-string 2 entry) "-"
  465. (match-string 3 entry)))
  466. (t (concat (match-string 2 entry) "/"
  467. (match-string 3 entry) "/"
  468. (match-string 1 entry))))
  469. t t entry))
  470. entry)))
  471. (autoload 'diary-make-entry "diary-lib")
  472. ;;;###autoload
  473. (defun remember-diary-extract-entries ()
  474. "Extract diary entries from the region."
  475. (save-excursion
  476. (goto-char (point-min))
  477. (let (list)
  478. (while (re-search-forward "^DIARY:\\s-*\\(.+\\)" nil t)
  479. (push (remember-diary-convert-entry (match-string 1)) list))
  480. (when list
  481. (diary-make-entry (mapconcat 'identity list "\n")
  482. nil remember-diary-file))
  483. nil))) ;; Continue processing
  484. ;;; Internal Functions:
  485. (defvar remember-mode-map
  486. (let ((map (make-sparse-keymap)))
  487. (define-key map "\C-x\C-s" 'remember-finalize)
  488. (define-key map "\C-c\C-c" 'remember-finalize)
  489. (define-key map "\C-c\C-k" 'remember-destroy)
  490. map)
  491. "Keymap used in `remember-mode'.")
  492. (define-derived-mode remember-mode indented-text-mode "Remember"
  493. "Major mode for output from \\[remember].
  494. This buffer is used to collect data that you want to remember.
  495. \\<remember-mode-map>
  496. Just hit \\[remember-finalize] when you're done entering, and it will file
  497. the data away for latter retrieval, and possible indexing.
  498. \\{remember-mode-map}"
  499. (set-keymap-parent remember-mode-map nil))
  500. ;; Notes buffer showing the notes:
  501. (defcustom remember-notes-buffer-name "*notes*"
  502. "Name of the notes buffer.
  503. Setting it to *scratch* will hijack the *scratch* buffer for the
  504. purpose of storing notes."
  505. :type 'string
  506. :version "24.4")
  507. (defcustom remember-notes-initial-major-mode nil
  508. "Major mode to use in the notes buffer when it's created.
  509. If this is nil, use `initial-major-mode'."
  510. :type '(choice (const :tag "Use `initial-major-mode'" nil)
  511. (function :tag "Major mode" text-mode))
  512. :version "24.4")
  513. (defcustom remember-notes-bury-on-kill t
  514. "Non-nil means `kill-buffer' will bury the notes buffer instead of killing."
  515. :type 'boolean
  516. :version "24.4")
  517. (defun remember-notes-save-and-bury-buffer ()
  518. "Save (if it is modified) and bury the current buffer."
  519. (interactive)
  520. (when (buffer-modified-p)
  521. (save-buffer))
  522. (bury-buffer))
  523. (defvar remember-notes-mode-map
  524. (let ((map (make-sparse-keymap)))
  525. (define-key map "\C-c\C-c" 'remember-notes-save-and-bury-buffer)
  526. map)
  527. "Keymap used in `remember-notes-mode'.")
  528. (define-minor-mode remember-notes-mode
  529. "Minor mode for the `remember-notes' buffer.
  530. This sets `buffer-save-without-query' so that `save-some-buffers' will
  531. save the notes buffer without asking.
  532. \\{remember-notes-mode-map}"
  533. nil nil nil
  534. (cond
  535. (remember-notes-mode
  536. (add-hook 'kill-buffer-query-functions
  537. #'remember-notes--kill-buffer-query nil t)
  538. (setq buffer-save-without-query t))))
  539. ;;;###autoload
  540. (defun remember-notes (&optional switch-to)
  541. "Return the notes buffer, creating it if needed, and maybe switch to it.
  542. This buffer is for notes that you want to preserve across Emacs sessions.
  543. The notes are saved in `remember-data-file'.
  544. If a buffer is already visiting that file, just return it.
  545. Otherwise, create the buffer, and rename it to `remember-notes-buffer-name',
  546. unless a buffer of that name already exists. Set the major mode according
  547. to `remember-notes-initial-major-mode', and enable `remember-notes-mode'
  548. minor mode.
  549. Use \\<remember-notes-mode-map>\\[remember-notes-save-and-bury-buffer] to save and bury the notes buffer.
  550. Interactively, or if SWITCH-TO is non-nil, switch to the buffer.
  551. Return the buffer.
  552. Set `initial-buffer-choice' to `remember-notes' to visit your notes buffer
  553. when Emacs starts. Set `remember-notes-buffer-name' to \"*scratch*\"
  554. to turn the *scratch* buffer into your notes buffer."
  555. (interactive "p")
  556. (let ((buf (or (find-buffer-visiting remember-data-file)
  557. (with-current-buffer (find-file-noselect remember-data-file)
  558. (and remember-notes-buffer-name
  559. (not (get-buffer remember-notes-buffer-name))
  560. (rename-buffer remember-notes-buffer-name))
  561. (funcall (or remember-notes-initial-major-mode
  562. initial-major-mode))
  563. (remember-notes-mode 1)
  564. (current-buffer)))))
  565. (when switch-to
  566. (switch-to-buffer buf))
  567. buf))
  568. (defun remember-notes--kill-buffer-query ()
  569. "Function that `remember-notes-mode' adds to `kill-buffer-query-functions'.
  570. Save the current buffer if modified. If `remember-notes-bury-on-kill'
  571. is non-nil, bury it and return nil; otherwise return t."
  572. (when (buffer-modified-p)
  573. (save-buffer))
  574. (if remember-notes-bury-on-kill
  575. (progn
  576. ;; bury-buffer always returns nil, but let's be explicit.
  577. (bury-buffer)
  578. nil)
  579. t))
  580. (provide 'remember)
  581. ;;; remember.el ends here