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