remember.el 19 KB

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