Release 6.19
[org-mode.git] / lisp / org-remember.el
blob5094acc7c0849e8e9716930a152f498d1ee8025e
1 ;;; org-remember.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 6.19
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains the system to take fast notes with Org-mode.
29 ;; This system is used together with John Wiegley's `remember.el'.
31 ;;; Code:
33 (eval-when-compile
34 (require 'cl))
35 (require 'org)
37 (declare-function remember-mode "remember" ())
38 (declare-function remember "remember" (&optional initial))
39 (declare-function remember-buffer-desc "remember" ())
40 (declare-function remember-finalize "remember" ())
41 (defvar remember-save-after-remembering)
42 (defvar remember-register)
43 (defvar remember-buffer)
44 (defvar remember-handler-functions)
45 (defvar remember-annotation-functions)
46 (defvar org-clock-heading)
47 (defvar org-clock-heading-for-remember)
49 (defgroup org-remember nil
50 "Options concerning interaction with remember.el."
51 :tag "Org Remember"
52 :group 'org)
54 (defcustom org-remember-store-without-prompt t
55 "Non-nil means, `C-c C-c' stores remember note without further prompts.
56 It then uses the file and headline specified by the template or (if the
57 template does not specify them) by the variables `org-default-notes-file'
58 and `org-remember-default-headline'. To force prompting anyway, use
59 `C-u C-c C-c' to file the note.
61 When this variable is nil, `C-c C-c' gives you the prompts, and
62 `C-u C-c C-c' triggers the fasttrack."
63 :group 'org-remember
64 :type 'boolean)
66 (defcustom org-remember-interactive-interface 'refile
67 "The interface to be used for interactive filing of remember notes.
68 This is only used when the interactive mode for selecting a filing
69 location is used (see the variable `org-remember-store-without-prompt').
70 Allowed values are:
71 outline The interface shows an outline of the relevant file
72 and the correct heading is found by moving through
73 the outline or by searching with incremental search.
74 outline-path-completion Headlines in the current buffer are offered via
75 completion.
76 refile Use the refile interface, and offer headlines,
77 possibly from different buffers."
78 :group 'org-remember
79 :type '(choice
80 (const :tag "Refile" refile)
81 (const :tag "Outline" outline)
82 (const :tag "Outline-path-completion" outline-path-completion)))
84 (defcustom org-remember-default-headline ""
85 "The headline that should be the default location in the notes file.
86 When filing remember notes, the cursor will start at that position.
87 You can set this on a per-template basis with the variable
88 `org-remember-templates'."
89 :group 'org-remember
90 :type 'string)
92 (defcustom org-remember-templates nil
93 "Templates for the creation of remember buffers.
94 When nil, just let remember make the buffer.
95 When non-nil, this is a list of 5-element lists. In each entry, the first
96 element is the name of the template, which should be a single short word.
97 The second element is a character, a unique key to select this template.
98 The third element is the template.
100 The fourth element is optional and can specify a destination file for
101 remember items created with this template. The default file is given
102 by `org-default-notes-file'. If the file name is not an absolute path,
103 it will be interpreted relative to `org-directory'.
105 An optional fifth element can specify the headline in that file that should
106 be offered first when the user is asked to file the entry. The default
107 headline is given in the variable `org-remember-default-headline'. When
108 this element is `top' or `bottom', the note will be placed as a level-1
109 entry at the beginning or end of the file, respectively.
111 An optional sixth element specifies the contexts in which the template
112 will be offered to the user. This element can be a list of major modes
113 or a function, and the template will only be offered if `org-remember'
114 is called from a mode in the list, or if the function returns t.
115 Templates that specify t or nil for the context will be always be added
116 to the list of selectable templates.
118 The template specifies the structure of the remember buffer. It should have
119 a first line starting with a star, to act as the org-mode headline.
120 Furthermore, the following %-escapes will be replaced with content:
122 %^{prompt} Prompt the user for a string and replace this sequence with it.
123 A default value and a completion table ca be specified like this:
124 %^{prompt|default|completion2|completion3|...}
125 %t time stamp, date only
126 %T time stamp with date and time
127 %u, %U like the above, but inactive time stamps
128 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
129 You may define a prompt like %^{Please specify birthday
130 %n user name (taken from `user-full-name')
131 %a annotation, normally the link created with org-store-link
132 %i initial content, the region active. If %i is indented,
133 the entire inserted text will be indented as well.
134 %c current kill ring head
135 %x content of the X clipboard
136 %^C Interactive selection of which kill or clip to use
137 %^L Like %^C, but insert as link
138 %k title of currently clocked task
139 %K link to currently clocked task
140 %^g prompt for tags, with completion on tags in target file
141 %^G prompt for tags, with completion all tags in all agenda files
142 %^{prop}p Prompt the user for a value for property `prop'
143 %:keyword specific information for certain link types, see below
144 %[pathname] insert the contents of the file given by `pathname'
145 %(sexp) evaluate elisp `(sexp)' and replace with the result
146 %! Store this note immediately after filling the template
147 %& Visit note immediately after storing it
149 %? After completing the template, position cursor here.
151 Apart from these general escapes, you can access information specific to the
152 link type that is created. For example, calling `remember' in emails or gnus
153 will record the author and the subject of the message, which you can access
154 with %:author and %:subject, respectively. Here is a complete list of what
155 is recorded for each link type.
157 Link type | Available information
158 -------------------+------------------------------------------------------
159 bbdb | %:type %:name %:company
160 vm, wl, mh, rmail | %:type %:subject %:message-id
161 | %:from %:fromname %:fromaddress
162 | %:to %:toname %:toaddress
163 | %:fromto (either \"to NAME\" or \"from NAME\")
164 gnus | %:group, for messages also all email fields
165 w3, w3m | %:type %:url
166 info | %:type %:file %:node
167 calendar | %:type %:date"
168 :group 'org-remember
169 :get (lambda (var) ; Make sure all entries have at least 5 elements
170 (mapcar (lambda (x)
171 (if (not (stringp (car x))) (setq x (cons "" x)))
172 (cond ((= (length x) 4) (append x '(nil)))
173 ((= (length x) 3) (append x '(nil nil)))
174 (t x)))
175 (default-value var)))
176 :type '(repeat
177 :tag "enabled"
178 (list :value ("" ?a "\n" nil nil nil)
179 (string :tag "Name")
180 (character :tag "Selection Key")
181 (string :tag "Template")
182 (choice :tag "Destination file"
183 (file :tag "Specify")
184 (function :tag "Function")
185 (const :tag "Use `org-default-notes-file'" nil))
186 (choice :tag "Destin. headline"
187 (string :tag "Specify")
188 (const :tag "Use `org-remember-default-headline'" nil)
189 (const :tag "Level 1 at beginning of file" top)
190 (const :tag "Level 1 at end of file" bottom))
191 (choice :tag "Context"
192 (const :tag "Use in all contexts" nil)
193 (const :tag "Use in all contexts" t)
194 (repeat :tag "Use only if in major mode"
195 (symbol :tag "Major mode"))
196 (function :tag "Perform a check against function")))))
198 (defcustom org-remember-before-finalize-hook nil
199 "Hook that is run right before a remember process is finalized.
200 The remember buffer is still current when this hook runs."
201 :group 'org-remember
202 :type 'hook)
204 (defvar org-remember-mode-map (make-sparse-keymap)
205 "Keymap for org-remember-mode, a minor mode.
206 Use this map to set additional keybindings for when Org-mode is used
207 for a Remember buffer.")
208 (defvar org-remember-mode-hook nil
209 "Hook for the minor `org-remember-mode'.")
211 (define-minor-mode org-remember-mode
212 "Minor mode for special key bindings in a remember buffer."
213 nil " Rem" org-remember-mode-map
214 (run-hooks 'org-remember-mode-hook))
215 (define-key org-remember-mode-map "\C-c\C-c" 'org-remember-finalize)
216 (define-key org-remember-mode-map "\C-c\C-k" 'org-remember-kill)
218 (defcustom org-remember-clock-out-on-exit 'query
219 "Non-nil means, stop the clock when exiting a clocking remember buffer.
220 This only applies if the clock is running in the remember buffer. If the
221 clock is not stopped, it continues to run in the storage location.
222 Instead of nil or t, this may also be the symbol `query' to prompt the
223 user each time a remember buffer with a running clock is filed away. "
224 :group 'org-remember
225 :type '(choice
226 (const :tag "Never" nil)
227 (const :tag "Always" t)
228 (const :tag "Query user" query)))
231 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
232 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
234 ;;;###autoload
235 (defun org-remember-insinuate ()
236 "Setup remember.el for use with Org-mode."
237 (org-require-remember)
238 (setq remember-annotation-functions '(org-remember-annotation))
239 (setq remember-handler-functions '(org-remember-handler))
240 (add-hook 'remember-mode-hook 'org-remember-apply-template))
242 ;;;###autoload
243 (defun org-remember-annotation ()
244 "Return a link to the current location as an annotation for remember.el.
245 If you are using Org-mode files as target for data storage with
246 remember.el, then the annotations should include a link compatible with the
247 conventions in Org-mode. This function returns such a link."
248 (org-store-link nil))
250 (defconst org-remember-help
251 "Select a destination location for the note.
252 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
253 RET on headline -> Store as sublevel entry to current headline
254 RET at beg-of-buf -> Append to file as level 2 headline
255 <left>/<right> -> before/after current headline, same headings level")
257 (defvar org-jump-to-target-location nil)
258 (defvar org-remember-previous-location nil)
259 (defvar org-force-remember-template-char) ;; dynamically scoped
261 ;; Save the major mode of the buffer we called remember from
262 (defvar org-select-template-temp-major-mode nil)
264 ;; Temporary store the buffer where remember was called from
265 (defvar org-select-template-original-buffer nil)
267 (defun org-select-remember-template (&optional use-char)
268 (when org-remember-templates
269 (let* ((pre-selected-templates
270 (mapcar
271 (lambda (tpl)
272 (let ((ctxt (nth 5 tpl))
273 (mode org-select-template-temp-major-mode)
274 (buf org-select-template-original-buffer))
275 (and (or (not ctxt) (eq ctxt t)
276 (and (listp ctxt) (memq mode ctxt))
277 (and (functionp ctxt)
278 (with-current-buffer buf
279 ;; Protect the user-defined function from error
280 (condition-case nil (funcall ctxt) (error nil)))))
281 tpl)))
282 org-remember-templates))
283 ;; If no template at this point, add the default templates:
284 (pre-selected-templates1
285 (if (not (delq nil pre-selected-templates))
286 (mapcar (lambda(x) (if (not (nth 5 x)) x))
287 org-remember-templates)
288 pre-selected-templates))
289 ;; Then unconditionally add template for any contexts
290 (pre-selected-templates2
291 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
292 org-remember-templates)
293 (delq nil pre-selected-templates1)))
294 (templates (mapcar (lambda (x)
295 (if (stringp (car x))
296 (append (list (nth 1 x) (car x)) (cddr x))
297 (append (list (car x) "") (cdr x))))
298 (delq nil pre-selected-templates2)))
299 (char (or use-char
300 (cond
301 ((= (length templates) 1)
302 (caar templates))
303 ((and (boundp 'org-force-remember-template-char)
304 org-force-remember-template-char)
305 (if (stringp org-force-remember-template-char)
306 (string-to-char org-force-remember-template-char)
307 org-force-remember-template-char))
309 (message "Select template: %s"
310 (mapconcat
311 (lambda (x)
312 (cond
313 ((not (string-match "\\S-" (nth 1 x)))
314 (format "[%c]" (car x)))
315 ((equal (downcase (car x))
316 (downcase (aref (nth 1 x) 0)))
317 (format "[%c]%s" (car x)
318 (substring (nth 1 x) 1)))
319 (t (format "[%c]%s" (car x) (nth 1 x)))))
320 templates " "))
321 (let ((inhibit-quit t) (char0 (read-char-exclusive)))
322 (when (equal char0 ?\C-g)
323 (jump-to-register remember-register)
324 (kill-buffer remember-buffer))
325 char0))))))
326 (cddr (assoc char templates)))))
328 (defun org-get-x-clipboard (value)
329 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
330 (if (eq window-system 'x)
331 (let ((x (org-get-x-clipboard-compat value)))
332 (if x (org-no-properties x)))))
334 ;;;###autoload
335 (defun org-remember-apply-template (&optional use-char skip-interactive)
336 "Initialize *remember* buffer with template, invoke `org-mode'.
337 This function should be placed into `remember-mode-hook' and in fact requires
338 to be run from that hook to function properly."
339 (when (and (boundp 'initial) (stringp initial))
340 (setq initial (org-no-properties initial))
341 (remove-text-properties 0 (length initial) '(read-only t) initial))
342 (if org-remember-templates
343 (let* ((entry (org-select-remember-template use-char))
344 (ct (or org-overriding-default-time (org-current-time)))
345 (dct (decode-time ct))
346 (ct1
347 (if (< (nth 2 dct) org-extend-today-until)
348 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
349 ct))
350 (tpl (car entry))
351 (plist-p (if org-store-link-plist t nil))
352 (file (if (and (nth 1 entry)
353 (or (and (stringp (nth 1 entry))
354 (string-match "\\S-" (nth 1 entry)))
355 (functionp (nth 1 entry))))
356 (nth 1 entry)
357 org-default-notes-file))
358 (headline (nth 2 entry))
359 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
360 (v-x (or (org-get-x-clipboard 'PRIMARY)
361 (org-get-x-clipboard 'CLIPBOARD)
362 (org-get-x-clipboard 'SECONDARY)))
363 (v-t (format-time-string (car org-time-stamp-formats) ct))
364 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
365 (v-u (concat "[" (substring v-t 1 -1) "]"))
366 (v-U (concat "[" (substring v-T 1 -1) "]"))
367 ;; `initial' and `annotation' are bound in `remember'
368 (v-i (if (boundp 'initial) initial))
369 (v-a (if (and (boundp 'annotation) annotation)
370 (if (equal annotation "[[]]") "" annotation)
371 ""))
372 (clipboards (remove nil (list v-i
373 (org-get-x-clipboard 'PRIMARY)
374 (org-get-x-clipboard 'CLIPBOARD)
375 (org-get-x-clipboard 'SECONDARY)
376 v-c)))
377 (v-A (if (and v-a
378 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
379 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
380 v-a))
381 (v-n user-full-name)
382 (v-k (if (marker-buffer org-clock-marker)
383 (org-substring-no-properties org-clock-heading)))
384 (v-K (if (marker-buffer org-clock-marker)
385 (org-make-link-string
386 (buffer-file-name (marker-buffer org-clock-marker))
387 org-clock-heading)))
389 (org-startup-folded nil)
390 (org-inhibit-startup t)
391 org-time-was-given org-end-time-was-given x
392 prompt completions char time pos default histvar)
394 (when (functionp file)
395 (setq file (funcall file)))
396 (when (and file (not (file-name-absolute-p file)))
397 (setq file (expand-file-name file org-directory)))
400 (setq org-store-link-plist
401 (append (list :annotation v-a :initial v-i)
402 org-store-link-plist))
403 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
404 (erase-buffer)
405 (insert (substitute-command-keys
406 (format
407 "## %s \"%s\" -> \"* %s\"
408 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
409 ## C-0 C-c C-c \"%s\" -> \"* %s\"
410 ## %s to select file and header location interactively.
411 ## C-2 C-c C-c as child of the currently clocked item
412 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
413 (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
414 (abbreviate-file-name (or file org-default-notes-file))
415 (or headline "")
416 (or (car org-remember-previous-location) "???")
417 (or (cdr org-remember-previous-location) "???")
418 (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
419 (insert tpl)
420 (goto-char (point-min))
422 ;; Simple %-escapes
423 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
424 (when (and initial (equal (match-string 0) "%i"))
425 (save-match-data
426 (let* ((lead (buffer-substring
427 (point-at-bol) (match-beginning 0))))
428 (setq v-i (mapconcat 'identity
429 (org-split-string initial "\n")
430 (concat "\n" lead))))))
431 (replace-match
432 (or (eval (intern (concat "v-" (match-string 1)))) "")
433 t t))
435 ;; %[] Insert contents of a file.
436 (goto-char (point-min))
437 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
438 (let ((start (match-beginning 0))
439 (end (match-end 0))
440 (filename (expand-file-name (match-string 1))))
441 (goto-char start)
442 (delete-region start end)
443 (condition-case error
444 (insert-file-contents filename)
445 (error (insert (format "%%![Couldn't insert %s: %s]"
446 filename error))))))
447 ;; %() embedded elisp
448 (goto-char (point-min))
449 (while (re-search-forward "%\\((.+)\\)" nil t)
450 (goto-char (match-beginning 0))
451 (let ((template-start (point)))
452 (forward-char 1)
453 (let ((result
454 (condition-case error
455 (eval (read (current-buffer)))
456 (error (format "%%![Error: %s]" error)))))
457 (delete-region template-start (point))
458 (insert result))))
460 ;; From the property list
461 (when plist-p
462 (goto-char (point-min))
463 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
464 (and (setq x (or (plist-get org-store-link-plist
465 (intern (match-string 1))) ""))
466 (replace-match x t t))))
468 ;; Turn on org-mode in the remember buffer, set local variables
469 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1))
470 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
471 (org-set-local 'org-default-notes-file file))
472 (if headline
473 (org-set-local 'org-remember-default-headline headline))
474 ;; Interactive template entries
475 (goto-char (point-min))
476 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
477 (setq char (if (match-end 3) (match-string 3))
478 prompt (if (match-end 2) (match-string 2)))
479 (goto-char (match-beginning 0))
480 (replace-match "")
481 (setq completions nil default nil)
482 (when prompt
483 (setq completions (org-split-string prompt "|")
484 prompt (pop completions)
485 default (car completions)
486 histvar (intern (concat
487 "org-remember-template-prompt-history::"
488 (or prompt "")))
489 completions (mapcar 'list completions)))
490 (cond
491 ((member char '("G" "g"))
492 (let* ((org-last-tags-completion-table
493 (org-global-tags-completion-table
494 (if (equal char "G") (org-agenda-files) (and file (list file)))))
495 (org-add-colon-after-tag-completion t)
496 (ins (org-ido-completing-read
497 (if prompt (concat prompt ": ") "Tags: ")
498 'org-tags-completion-function nil nil nil
499 'org-tags-history)))
500 (setq ins (mapconcat 'identity
501 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
502 ":"))
503 (when (string-match "\\S-" ins)
504 (or (equal (char-before) ?:) (insert ":"))
505 (insert ins)
506 (or (equal (char-after) ?:) (insert ":")))))
507 ((equal char "C")
508 (cond ((= (length clipboards) 1) (insert (car clipboards)))
509 ((> (length clipboards) 1)
510 (insert (read-string "Clipboard/kill value: "
511 (car clipboards) '(clipboards . 1)
512 (car clipboards))))))
513 ((equal char "L")
514 (cond ((= (length clipboards) 1)
515 (org-insert-link 0 (car clipboards)))
516 ((> (length clipboards) 1)
517 (org-insert-link 0 (read-string "Clipboard/kill value: "
518 (car clipboards)
519 '(clipboards . 1)
520 (car clipboards))))))
521 ((equal char "p")
522 (let*
523 ((prop (org-substring-no-properties prompt))
524 (pall (concat prop "_ALL"))
525 (allowed
526 (with-current-buffer
527 (get-buffer (file-name-nondirectory file))
528 (or (cdr (assoc pall org-file-properties))
529 (cdr (assoc pall org-global-properties))
530 (cdr (assoc pall org-global-properties-fixed)))))
531 (existing (with-current-buffer
532 (get-buffer (file-name-nondirectory file))
533 (mapcar 'list (org-property-values prop))))
534 (propprompt (concat "Value for " prop ": "))
535 (val (if allowed
536 (org-completing-read
537 propprompt
538 (mapcar 'list (org-split-string allowed "[ \t]+"))
539 nil 'req-match)
540 (org-completing-read propprompt existing nil nil
541 "" nil ""))))
542 (org-set-property prop val)))
543 (char
544 ;; These are the date/time related ones
545 (setq org-time-was-given (equal (upcase char) char))
546 (setq time (org-read-date (equal (upcase char) "U") t nil
547 prompt))
548 (org-insert-time-stamp time org-time-was-given
549 (member char '("u" "U"))
550 nil nil (list org-end-time-was-given)))
552 (let (org-completion-use-ido)
553 (insert (org-completing-read
554 (concat (if prompt prompt "Enter string")
555 (if default (concat " [" default "]"))
556 ": ")
557 completions nil nil nil histvar default))))))
558 (goto-char (point-min))
559 (if (re-search-forward "%\\?" nil t)
560 (replace-match "")
561 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
562 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1)))
563 (when (save-excursion
564 (goto-char (point-min))
565 (re-search-forward "%&" nil t))
566 (replace-match "")
567 (org-set-local 'org-jump-to-target-location t))
568 (when (save-excursion
569 (goto-char (point-min))
570 (re-search-forward "%!" nil t))
571 (replace-match "")
572 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
574 (defun org-remember-finish-immediately ()
575 "File remember note immediately.
576 This should be run in `post-command-hook' and will remove itself
577 from that hook."
578 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
579 (org-remember-finalize))
581 (defun org-remember-visit-immediately ()
582 "File remember note immediately.
583 This should be run in `post-command-hook' and will remove itself
584 from that hook."
585 (org-remember '(16))
586 (goto-char (or (text-property-any
587 (point) (save-excursion (org-end-of-subtree t t))
588 'org-position-cursor t)
589 (point)))
590 (message "%s"
591 (format
592 (substitute-command-keys
593 "Restore window configuration with \\[jump-to-register] %c")
594 remember-register)))
596 (defvar org-clock-marker) ; Defined in org.el
597 (defun org-remember-finalize ()
598 "Finalize the remember process."
599 (interactive)
600 (unless org-remember-mode
601 (error "This does not seem to be a remember buffer for Org-mode"))
602 (run-hooks 'org-remember-before-finalize-hook)
603 (unless (fboundp 'remember-finalize)
604 (defalias 'remember-finalize 'remember-buffer))
605 (when (and org-clock-marker
606 (equal (marker-buffer org-clock-marker) (current-buffer)))
607 ;; the clock is running in this buffer.
608 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
609 (or (eq org-remember-clock-out-on-exit t)
610 (and org-remember-clock-out-on-exit
611 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
612 (let (org-log-note-clock-out) (org-clock-out))))
613 (when buffer-file-name
614 (save-buffer)
615 (setq buffer-file-name nil))
616 (remember-finalize))
618 (defun org-remember-kill ()
619 "Abort the current remember process."
620 (interactive)
621 (let ((org-note-abort t))
622 (org-remember-finalize)))
624 ;;;###autoload
625 (defun org-remember (&optional goto org-force-remember-template-char)
626 "Call `remember'. If this is already a remember buffer, re-apply template.
627 If there is an active region, make sure remember uses it as initial content
628 of the remember buffer.
630 When called interactively with a `C-u' prefix argument GOTO, don't remember
631 anything, just go to the file/headline where the selected template usually
632 stores its notes. With a double prefix arg `C-u C-u', go to the last
633 note stored by remember.
635 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
636 associated with a template in `org-remember-templates'."
637 (interactive "P")
638 (org-require-remember)
639 (cond
640 ((equal goto '(4)) (org-go-to-remember-target))
641 ((equal goto '(16)) (org-remember-goto-last-stored))
643 ;; set temporary variables that will be needed in
644 ;; `org-select-remember-template'
645 (setq org-select-template-temp-major-mode major-mode)
646 (setq org-select-template-original-buffer (current-buffer))
647 (if org-remember-mode
648 (progn
649 (when (< (length org-remember-templates) 2)
650 (error "No other template available"))
651 (erase-buffer)
652 (let ((annotation (plist-get org-store-link-plist :annotation))
653 (initial (plist-get org-store-link-plist :initial)))
654 (org-remember-apply-template))
655 (message "Press C-c C-c to remember data"))
656 (if (org-region-active-p)
657 (org-do-remember (buffer-substring (point) (mark)))
658 (org-do-remember))))))
660 (defvar org-remember-last-stored-marker (make-marker)
661 "Marker pointing to the entry most recently stored with `org-remember'.")
663 (defun org-remember-goto-last-stored ()
664 "Go to the location where the last remember note was stored."
665 (interactive)
666 (org-goto-marker-or-bmk org-remember-last-stored-marker
667 "org-remember-last-stored")
668 (message "This is the last note stored by remember"))
670 (defun org-go-to-remember-target (&optional template-key)
671 "Go to the target location of a remember template.
672 The user is queried for the template."
673 (interactive)
674 (let* (org-select-template-temp-major-mode
675 (entry (org-select-remember-template template-key))
676 (file (nth 1 entry))
677 (heading (nth 2 entry))
678 visiting)
679 (unless (and file (stringp file) (string-match "\\S-" file))
680 (setq file org-default-notes-file))
681 (when (and file (not (file-name-absolute-p file)))
682 (setq file (expand-file-name file org-directory)))
683 (unless (and heading (stringp heading) (string-match "\\S-" heading))
684 (setq heading org-remember-default-headline))
685 (setq visiting (org-find-base-buffer-visiting file))
686 (if (not visiting) (find-file-noselect file))
687 (switch-to-buffer (or visiting (get-file-buffer file)))
688 (widen)
689 (goto-char (point-min))
690 (if (re-search-forward
691 (concat "^\\*+[ \t]+" (regexp-quote heading)
692 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
693 nil t)
694 (goto-char (match-beginning 0))
695 (error "Target headline not found: %s" heading))))
697 ;;;###autoload
698 (defun org-remember-handler ()
699 "Store stuff from remember.el into an org file.
700 When the template has specified a file and a headline, the entry is filed
701 there, or in the location defined by `org-default-notes-file' and
702 `org-remember-default-headline'.
704 If no defaults have been defined, or if the current prefix argument
705 is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
706 process is used to select the target location.
708 When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
709 the entry is filed to the same location as the previous note.
711 When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
712 the entry is filed as a subentry of the entry where the clock is
713 currently running.
715 When `C-u' has been used as prefix argument, the note is stored and emacs
716 moves point to the new location of the note, so that editing can be
717 continued there (similar to inserting \"%&\" into the template).
719 Before storing the note, the function ensures that the text has an
720 org-mode-style headline, i.e. a first line that starts with
721 a \"*\". If not, a headline is constructed from the current date and
722 some additional data.
724 If the variable `org-adapt-indentation' is non-nil, the entire text is
725 also indented so that it starts in the same column as the headline
726 \(i.e. after the stars).
728 See also the variable `org-reverse-note-order'."
729 (when (and (equal current-prefix-arg 2)
730 (not (marker-buffer org-clock-marker)))
731 (error "No running clock"))
732 (when (org-bound-and-true-p org-jump-to-target-location)
733 (let* ((end (min (point-max) (1+ (point))))
734 (beg (point)))
735 (if (= end beg) (setq beg (1- beg)))
736 (put-text-property beg end 'org-position-cursor t)))
737 (goto-char (point-min))
738 (while (looking-at "^[ \t]*\n\\|^##.*\n")
739 (replace-match ""))
740 (goto-char (point-max))
741 (beginning-of-line 1)
742 (while (looking-at "[ \t]*$\\|##.*")
743 (delete-region (1- (point)) (point-max))
744 (beginning-of-line 1))
745 (catch 'quit
746 (if org-note-abort (throw 'quit nil))
747 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
748 (previousp (and (member current-prefix-arg '((16) 0))
749 org-remember-previous-location))
750 (clockp (equal current-prefix-arg 2))
751 (fastp (org-xor (equal current-prefix-arg 1)
752 org-remember-store-without-prompt))
753 (file (cond
754 (fastp org-default-notes-file)
755 ((and (eq org-remember-interactive-interface 'refile)
756 org-refile-targets)
757 org-default-notes-file)
758 ((not previousp)
759 (org-get-org-file))))
760 (heading org-remember-default-headline)
761 (visiting (and file (org-find-base-buffer-visiting file)))
762 (org-startup-folded nil)
763 (org-startup-align-all-tables nil)
764 (org-goto-start-pos 1)
765 spos exitcmd level reversed txt)
766 (when (equal current-prefix-arg '(4))
767 (setq visitp t))
768 (when previousp
769 (setq file (car org-remember-previous-location)
770 visiting (and file (org-find-base-buffer-visiting file))
771 heading (cdr org-remember-previous-location)
772 fastp t))
773 (when clockp
774 (setq file (buffer-file-name (marker-buffer org-clock-marker))
775 visiting (and file (org-find-base-buffer-visiting file))
776 heading org-clock-heading-for-remember
777 fastp t))
778 (setq current-prefix-arg nil)
779 ;; Modify text so that it becomes a nice subtree which can be inserted
780 ;; into an org tree.
781 (goto-char (point-min))
782 (if (re-search-forward "[ \t\n]+\\'" nil t)
783 ;; remove empty lines at end
784 (replace-match ""))
785 (goto-char (point-min))
786 (unless (looking-at org-outline-regexp)
787 ;; add a headline
788 (insert (concat "* " (current-time-string)
789 " (" (remember-buffer-desc) ")\n"))
790 (backward-char 1)
791 (when org-adapt-indentation
792 (while (re-search-forward "^" nil t)
793 (insert " "))))
794 (goto-char (point-min))
795 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
796 (replace-match "\n\n")
797 (if (re-search-forward "[ \t\n]*\\'")
798 (replace-match "\n")))
799 (goto-char (point-min))
800 (setq txt (buffer-string))
801 (org-save-markers-in-region (point-min) (point-max))
802 (when (and (eq org-remember-interactive-interface 'refile)
803 (not fastp))
804 (org-refile nil (or visiting (find-file-noselect file)))
805 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
806 (save-excursion
807 (bookmark-jump "org-refile-last-stored")
808 (bookmark-set "org-remember-last-stored")
809 (move-marker org-remember-last-stored-marker (point)))
810 (throw 'quit t))
811 ;; Find the file
812 (with-current-buffer (or visiting (find-file-noselect file))
813 (unless (org-mode-p)
814 (error "Target files for remember notes must be in Org-mode"))
815 (save-excursion
816 (save-restriction
817 (widen)
818 (and (goto-char (point-min))
819 (not (re-search-forward "^\\* " nil t))
820 (insert "\n* " (or (and (stringp heading) heading)
821 "Notes") "\n"))
822 (setq reversed (org-notes-order-reversed-p))
824 ;; Find the default location
825 (when heading
826 (cond
827 ((eq heading 'top)
828 (goto-char (point-min))
829 (or (looking-at org-outline-regexp)
830 (re-search-forward org-outline-regexp nil t))
831 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
832 ((eq heading 'bottom)
833 (goto-char (point-max))
834 (or (bolp) (newline))
835 (setq org-goto-start-pos (point)))
836 ((and (stringp heading) (string-match "\\S-" heading))
837 (goto-char (point-min))
838 (if (re-search-forward
839 (concat "^\\*+[ \t]+" (regexp-quote heading)
840 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
841 nil t)
842 (setq org-goto-start-pos (match-beginning 0))
843 (when fastp
844 (goto-char (point-max))
845 (unless (bolp) (newline))
846 (insert "* " heading "\n")
847 (setq org-goto-start-pos (point-at-bol 0)))))
848 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
849 heading 'top))))
851 ;; Ask the User for a location, using the appropriate interface
852 (cond
853 ((and fastp (memq heading '(top bottom)))
854 (setq spos org-goto-start-pos
855 exitcmd (if (eq heading 'top) 'left nil)))
856 (fastp (setq spos org-goto-start-pos
857 exitcmd 'return))
858 ((eq org-remember-interactive-interface 'outline)
859 (setq spos (org-get-location (current-buffer)
860 org-remember-help)
861 exitcmd (cdr spos)
862 spos (car spos)))
863 ((eq org-remember-interactive-interface 'outline-path-completion)
864 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
865 (org-refile-use-outline-path t))
866 (setq spos (org-refile-get-location "Heading: ")
867 exitcmd 'return
868 spos (nth 3 spos))))
869 (t (error "This should not happen")))
870 (if (not spos) (throw 'quit nil)) ; return nil to show we did
871 ; not handle this note
872 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
873 (goto-char spos)
874 (cond ((org-on-heading-p t)
875 (org-back-to-heading t)
876 (setq level (funcall outline-level))
877 (cond
878 ((eq exitcmd 'return)
879 ;; sublevel of current
880 (setq org-remember-previous-location
881 (cons (abbreviate-file-name file)
882 (org-get-heading 'notags)))
883 (if reversed
884 (outline-next-heading)
885 (org-end-of-subtree t)
886 (if (not (bolp))
887 (if (looking-at "[ \t]*\n")
888 (beginning-of-line 2)
889 (end-of-line 1)
890 (insert "\n"))))
891 (org-paste-subtree (org-get-valid-level level 1) txt)
892 (and org-auto-align-tags (org-set-tags nil t))
893 (bookmark-set "org-remember-last-stored")
894 (move-marker org-remember-last-stored-marker (point)))
895 ((eq exitcmd 'left)
896 ;; before current
897 (org-paste-subtree level txt)
898 (and org-auto-align-tags (org-set-tags nil t))
899 (bookmark-set "org-remember-last-stored")
900 (move-marker org-remember-last-stored-marker (point)))
901 ((eq exitcmd 'right)
902 ;; after current
903 (org-end-of-subtree t)
904 (org-paste-subtree level txt)
905 (and org-auto-align-tags (org-set-tags nil t))
906 (bookmark-set "org-remember-last-stored")
907 (move-marker org-remember-last-stored-marker (point)))
908 (t (error "This should not happen"))))
910 ((eq heading 'bottom)
911 (org-paste-subtree 1 txt)
912 (and org-auto-align-tags (org-set-tags nil t))
913 (bookmark-set "org-remember-last-stored")
914 (move-marker org-remember-last-stored-marker (point)))
916 ((and (bobp) (not reversed))
917 ;; Put it at the end, one level below level 1
918 (save-restriction
919 (widen)
920 (goto-char (point-max))
921 (if (not (bolp)) (newline))
922 (org-paste-subtree (org-get-valid-level 1 1) txt)
923 (and org-auto-align-tags (org-set-tags nil t))
924 (bookmark-set "org-remember-last-stored")
925 (move-marker org-remember-last-stored-marker (point))))
927 ((and (bobp) reversed)
928 ;; Put it at the start, as level 1
929 (save-restriction
930 (widen)
931 (goto-char (point-min))
932 (re-search-forward "^\\*+ " nil t)
933 (beginning-of-line 1)
934 (org-paste-subtree 1 txt)
935 (and org-auto-align-tags (org-set-tags nil t))
936 (bookmark-set "org-remember-last-stored")
937 (move-marker org-remember-last-stored-marker (point))))
939 ;; Put it right there, with automatic level determined by
940 ;; org-paste-subtree or from prefix arg
941 (org-paste-subtree
942 (if (numberp current-prefix-arg) current-prefix-arg)
943 txt)
944 (and org-auto-align-tags (org-set-tags nil t))
945 (bookmark-set "org-remember-last-stored")
946 (move-marker org-remember-last-stored-marker (point))))
948 (when remember-save-after-remembering
949 (save-buffer)
950 (if (and (not visiting)
951 (not (equal (marker-buffer org-clock-marker)
952 (current-buffer))))
953 (kill-buffer (current-buffer)))))))))
955 t) ;; return t to indicate that we took care of this note.
957 (defun org-do-remember (&optional initial)
958 "Call remember."
959 (remember initial))
961 (defun org-require-remember ()
962 "Make sure remember is loaded, or install our own emergency version of it."
963 (condition-case nil
964 (require 'remember)
965 (error
966 ;; Lets install our own micro version of remember
967 (defvar remember-register ?R)
968 (defvar remember-mode-hook nil)
969 (defvar remember-handler-functions nil)
970 (defvar remember-buffer "*Remember*")
971 (defvar remember-save-after-remembering t)
972 (defvar remember-annotation-functions '(buffer-file-name))
973 (defun remember-finalize ()
974 (run-hook-with-args-until-success 'remember-handler-functions)
975 (when (equal remember-buffer (buffer-name))
976 (kill-buffer (current-buffer))
977 (jump-to-register remember-register)))
978 (defun remember-mode ()
979 (fundamental-mode)
980 (setq mode-name "Remember")
981 (run-hooks 'remember-mode-hook))
982 (defun remember (&optional initial)
983 (window-configuration-to-register remember-register)
984 (let* ((annotation (run-hook-with-args-until-success
985 'remember-annotation-functions)))
986 (switch-to-buffer-other-window (get-buffer-create remember-buffer))
987 (remember-mode)))
988 (defun remember-buffer-desc ()
989 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
990 (point-at-eol)))))))
992 (provide 'org-remember)
994 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
996 ;;; org-remember.el ends here