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