Release 6.08a.
[org-mode.git] / lisp / org-remember.el
blob554989559e2068631570ae029e92d41ce28bc29f
1 ;;; org-remember.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 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.08a
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 Wiegleys `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 themplate 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 vaues 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-clock-out-on-exit 'query
199 "Non-nil means, stop the clock when exiting a clocking remember buffer.
200 This only applies if the clock is running in the remember buffer. If the
201 clock is not stopped, it continues to run in the storage location.
202 Instead of nil or t, this may also be the symbol `query' to prompt the
203 user each time a remember buffer with a running clock is filed away. "
204 :group 'org-remember
205 :type '(choice
206 (const :tag "Never" nil)
207 (const :tag "Always" t)
208 (const :tag "Query user" query)))
211 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
212 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
214 ;;;###autoload
215 (defun org-remember-insinuate ()
216 "Setup remember.el for use with Org-mode."
217 (org-require-remember)
218 (setq remember-annotation-functions '(org-remember-annotation))
219 (setq remember-handler-functions '(org-remember-handler))
220 (add-hook 'remember-mode-hook 'org-remember-apply-template))
222 ;;;###autoload
223 (defun org-remember-annotation ()
224 "Return a link to the current location as an annotation for remember.el.
225 If you are using Org-mode files as target for data storage with
226 remember.el, then the annotations should include a link compatible with the
227 conventions in Org-mode. This function returns such a link."
228 (org-store-link nil))
230 (defconst org-remember-help
231 "Select a destination location for the note.
232 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
233 RET on headline -> Store as sublevel entry to current headline
234 RET at beg-of-buf -> Append to file as level 2 headline
235 <left>/<right> -> before/after current headline, same headings level")
237 (defvar org-jump-to-target-location nil)
238 (defvar org-remember-previous-location nil)
239 (defvar org-force-remember-template-char) ;; dynamically scoped
241 ;; Save the major mode of the buffer we called remember from
242 (defvar org-select-template-temp-major-mode nil)
244 ;; Temporary store the buffer where remember was called from
245 (defvar org-select-template-original-buffer nil)
247 (defun org-select-remember-template (&optional use-char)
248 (when org-remember-templates
249 (let* ((pre-selected-templates
250 (mapcar
251 (lambda (tpl)
252 (let ((ctxt (nth 5 tpl))
253 (mode org-select-template-temp-major-mode)
254 (buf org-select-template-original-buffer))
255 (and (or (not ctxt) (eq ctxt t)
256 (and (listp ctxt) (memq mode ctxt))
257 (and (functionp ctxt)
258 (with-current-buffer buf
259 ;; Protect the user-defined function from error
260 (condition-case nil (funcall ctxt) (error nil)))))
261 tpl)))
262 org-remember-templates))
263 ;; If no template at this point, add the default templates:
264 (pre-selected-templates1
265 (if (not (delq nil pre-selected-templates))
266 (mapcar (lambda(x) (if (not (nth 5 x)) x))
267 org-remember-templates)
268 pre-selected-templates))
269 ;; Then unconditionnally add template for any contexts
270 (pre-selected-templates2
271 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
272 org-remember-templates)
273 (delq nil pre-selected-templates1)))
274 (templates (mapcar (lambda (x)
275 (if (stringp (car x))
276 (append (list (nth 1 x) (car x)) (cddr x))
277 (append (list (car x) "") (cdr x))))
278 (delq nil pre-selected-templates2)))
279 (char (or use-char
280 (cond
281 ((= (length templates) 1)
282 (caar templates))
283 ((and (boundp 'org-force-remember-template-char)
284 org-force-remember-template-char)
285 (if (stringp org-force-remember-template-char)
286 (string-to-char org-force-remember-template-char)
287 org-force-remember-template-char))
289 (message "Select template: %s"
290 (mapconcat
291 (lambda (x)
292 (cond
293 ((not (string-match "\\S-" (nth 1 x)))
294 (format "[%c]" (car x)))
295 ((equal (downcase (car x))
296 (downcase (aref (nth 1 x) 0)))
297 (format "[%c]%s" (car x)
298 (substring (nth 1 x) 1)))
299 (t (format "[%c]%s" (car x) (nth 1 x)))))
300 templates " "))
301 (let ((inhibit-quit t) (char0 (read-char-exclusive)))
302 (when (equal char0 ?\C-g)
303 (jump-to-register remember-register)
304 (kill-buffer remember-buffer))
305 char0))))))
306 (cddr (assoc char templates)))))
308 (defun org-get-x-clipboard (value)
309 "Get the value of the x clibboard, compatible with XEmacs, and GNU Emacs 21."
310 (if (eq window-system 'x)
311 (let ((x (org-get-x-clipboard-compat value)))
312 (if x (org-no-properties x)))))
314 ;;;###autoload
315 (defun org-remember-apply-template (&optional use-char skip-interactive)
316 "Initialize *remember* buffer with template, invoke `org-mode'.
317 This function should be placed into `remember-mode-hook' and in fact requires
318 to be run from that hook to function properly."
319 (when (and (boundp 'initial) (stringp initial))
320 (setq initial (org-no-properties initial))
321 (remove-text-properties 0 (length initial) '(read-only t) initial))
322 (if org-remember-templates
323 (let* ((entry (org-select-remember-template use-char))
324 (ct (or org-overriding-default-time (org-current-time)))
325 (dct (decode-time ct))
326 (ct1
327 (if (< (nth 2 dct) org-extend-today-until)
328 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
329 ct))
330 (tpl (car entry))
331 (plist-p (if org-store-link-plist t nil))
332 (file (if (and (nth 1 entry)
333 (or (and (stringp (nth 1 entry))
334 (string-match "\\S-" (nth 1 entry)))
335 (functionp (nth 1 entry))))
336 (nth 1 entry)
337 org-default-notes-file))
338 (headline (nth 2 entry))
339 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
340 (v-x (or (org-get-x-clipboard 'PRIMARY)
341 (org-get-x-clipboard 'CLIPBOARD)
342 (org-get-x-clipboard 'SECONDARY)))
343 (v-t (format-time-string (car org-time-stamp-formats) ct))
344 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
345 (v-u (concat "[" (substring v-t 1 -1) "]"))
346 (v-U (concat "[" (substring v-T 1 -1) "]"))
347 ;; `initial' and `annotation' are bound in `remember'
348 (v-i (if (boundp 'initial) initial))
349 (v-a (if (and (boundp 'annotation) annotation)
350 (if (equal annotation "[[]]") "" annotation)
351 ""))
352 (clipboards (remove nil (list v-i
353 (org-get-x-clipboard 'PRIMARY)
354 (org-get-x-clipboard 'CLIPBOARD)
355 (org-get-x-clipboard 'SECONDARY)
356 v-c)))
357 (v-A (if (and v-a
358 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
359 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
360 v-a))
361 (v-n user-full-name)
362 (v-k (if (marker-buffer org-clock-marker)
363 (substring-no-properties org-clock-heading)))
364 (v-K (if (marker-buffer org-clock-marker)
365 (org-make-link-string
366 (buffer-file-name (marker-buffer org-clock-marker))
367 org-clock-heading)))
369 (org-startup-folded nil)
370 (org-inhibit-startup t)
371 org-time-was-given org-end-time-was-given x
372 prompt completions char time pos default histvar)
374 (when (functionp file)
375 (setq file (funcall file)))
376 (when (and file (not (file-name-absolute-p file)))
377 (setq file (expand-file-name file org-directory)))
380 (setq org-store-link-plist
381 (append (list :annotation v-a :initial v-i)
382 org-store-link-plist))
383 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
384 (erase-buffer)
385 (insert (substitute-command-keys
386 (format
387 "## %s \"%s\" -> \"* %s\"
388 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
389 ## C-0 C-c C-c \"%s\" -> \"* %s\"
390 ## %s to select file and header location interactively.
391 ## C-2 C-c C-c as child of the currently clocked item
392 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
393 (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
394 (abbreviate-file-name (or file org-default-notes-file))
395 (or headline "")
396 (or (car org-remember-previous-location) "???")
397 (or (cdr org-remember-previous-location) "???")
398 (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
399 (insert tpl)
400 (goto-char (point-min))
402 ;; Simple %-escapes
403 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
404 (when (and initial (equal (match-string 0) "%i"))
405 (save-match-data
406 (let* ((lead (buffer-substring
407 (point-at-bol) (match-beginning 0))))
408 (setq v-i (mapconcat 'identity
409 (org-split-string initial "\n")
410 (concat "\n" lead))))))
411 (replace-match
412 (or (eval (intern (concat "v-" (match-string 1)))) "")
413 t t))
415 ;; %[] Insert contents of a file.
416 (goto-char (point-min))
417 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
418 (let ((start (match-beginning 0))
419 (end (match-end 0))
420 (filename (expand-file-name (match-string 1))))
421 (goto-char start)
422 (delete-region start end)
423 (condition-case error
424 (insert-file-contents filename)
425 (error (insert (format "%%![Couldn't insert %s: %s]"
426 filename error))))))
427 ;; %() embedded elisp
428 (goto-char (point-min))
429 (while (re-search-forward "%\\((.+)\\)" nil t)
430 (goto-char (match-beginning 0))
431 (let ((template-start (point)))
432 (forward-char 1)
433 (let ((result
434 (condition-case error
435 (eval (read (current-buffer)))
436 (error (format "%%![Error: %s]" error)))))
437 (delete-region template-start (point))
438 (insert result))))
440 ;; From the property list
441 (when plist-p
442 (goto-char (point-min))
443 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
444 (and (setq x (or (plist-get org-store-link-plist
445 (intern (match-string 1))) ""))
446 (replace-match x t t))))
448 ;; Turn on org-mode in the remember buffer, set local variables
449 (let ((org-inhibit-startup t)) (org-mode))
450 (org-set-local 'org-finish-function 'org-remember-finalize)
451 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
452 (org-set-local 'org-default-notes-file file))
453 (if headline
454 (org-set-local 'org-remember-default-headline headline))
455 ;; Interactive template entries
456 (goto-char (point-min))
457 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
458 (setq char (if (match-end 3) (match-string 3))
459 prompt (if (match-end 2) (match-string 2)))
460 (goto-char (match-beginning 0))
461 (replace-match "")
462 (setq completions nil default nil)
463 (when prompt
464 (setq completions (org-split-string prompt "|")
465 prompt (pop completions)
466 default (car completions)
467 histvar (intern (concat
468 "org-remember-template-prompt-history::"
469 (or prompt "")))
470 completions (mapcar 'list completions)))
471 (cond
472 ((member char '("G" "g"))
473 (let* ((org-last-tags-completion-table
474 (org-global-tags-completion-table
475 (if (equal char "G") (org-agenda-files) (and file (list file)))))
476 (org-add-colon-after-tag-completion t)
477 (ins (completing-read
478 (if prompt (concat prompt ": ") "Tags: ")
479 'org-tags-completion-function nil nil nil
480 'org-tags-history)))
481 (setq ins (mapconcat 'identity
482 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
483 ":"))
484 (when (string-match "\\S-" ins)
485 (or (equal (char-before) ?:) (insert ":"))
486 (insert ins)
487 (or (equal (char-after) ?:) (insert ":")))))
488 ((equal char "C")
489 (cond ((= (length clipboards) 1) (insert (car clipboards)))
490 ((> (length clipboards) 1)
491 (insert (read-string "Clipboard/kill value: "
492 (car clipboards) '(clipboards . 1)
493 (car clipboards))))))
494 ((equal char "L")
495 (cond ((= (length clipboards) 1)
496 (org-insert-link 0 (car clipboards)))
497 ((> (length clipboards) 1)
498 (org-insert-link 0 (read-string "Clipboard/kill value: "
499 (car clipboards)
500 '(clipboards . 1)
501 (car clipboards))))))
502 ((equal char "p")
503 (let*
504 ((prop (substring-no-properties prompt))
505 (allowed (with-current-buffer
506 (get-buffer (file-name-nondirectory file))
507 (org-property-get-allowed-values nil prop 'table)))
508 (existing (with-current-buffer
509 (get-buffer (file-name-nondirectory file))
510 (mapcar 'list (org-property-values prop))))
511 (propprompt (concat "Value for " prop ": "))
512 (val (if allowed
513 (org-completing-read propprompt allowed nil
514 'req-match)
515 (org-completing-read propprompt existing nil nil
516 "" nil ""))))
517 (org-set-property prop val)))
518 (char
519 ;; These are the date/time related ones
520 (setq org-time-was-given (equal (upcase char) char))
521 (setq time (org-read-date (equal (upcase char) "U") t nil
522 prompt))
523 (org-insert-time-stamp time org-time-was-given
524 (member char '("u" "U"))
525 nil nil (list org-end-time-was-given)))
527 (insert (org-completing-read
528 (concat (if prompt prompt "Enter string")
529 (if default (concat " [" default "]"))
530 ": ")
531 completions nil nil nil histvar default)))))
532 (goto-char (point-min))
533 (if (re-search-forward "%\\?" nil t)
534 (replace-match "")
535 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
536 (let ((org-inhibit-startup t)) (org-mode))
537 (org-set-local 'org-finish-function 'org-remember-finalize))
538 (when (save-excursion
539 (goto-char (point-min))
540 (re-search-forward "%&" nil t))
541 (replace-match "")
542 (org-set-local 'org-jump-to-target-location t))
543 (when (save-excursion
544 (goto-char (point-min))
545 (re-search-forward "%!" nil t))
546 (replace-match "")
547 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
549 (defun org-remember-finish-immediately ()
550 "File remember note immediately.
551 This should be run in `post-command-hook' and will remove itself
552 from that hook."
553 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
554 (when org-finish-function
555 (funcall org-finish-function)))
557 (defun org-remember-visit-immediately ()
558 "File remember note immediately.
559 This should be run in `post-command-hook' and will remove itself
560 from that hook."
561 (org-remember '(16))
562 (goto-char (or (text-property-any
563 (point) (save-excursion (org-end-of-subtree t t))
564 'org-position-cursor t)
565 (point)))
566 (message "%s"
567 (format
568 (substitute-command-keys
569 "Restore window configuration with \\[jump-to-register] %c")
570 remember-register)))
572 (defvar org-clock-marker) ; Defined in org.el
573 (defun org-remember-finalize ()
574 "Finalize the remember process."
575 (unless (fboundp 'remember-finalize)
576 (defalias 'remember-finalize 'remember-buffer))
577 (when (and org-clock-marker
578 (equal (marker-buffer org-clock-marker) (current-buffer)))
579 ;; the clock is running in this buffer.
580 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
581 (or (eq org-remember-clock-out-on-exit t)
582 (and org-remember-clock-out-on-exit
583 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
584 (let (org-log-note-clock-out) (org-clock-out))))
585 (when buffer-file-name
586 (save-buffer)
587 (setq buffer-file-name nil))
588 (remember-finalize))
590 ;;;###autoload
591 (defun org-remember (&optional goto org-force-remember-template-char)
592 "Call `remember'. If this is already a remember buffer, re-apply template.
593 If there is an active region, make sure remember uses it as initial content
594 of the remember buffer.
596 When called interactively with a `C-u' prefix argument GOTO, don't remember
597 anything, just go to the file/headline where the selected template usually
598 stores its notes. With a double prefix arg `C-u C-u', go to the last
599 note stored by remember.
601 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
602 associated with a template in `org-remember-templates'."
603 (interactive "P")
604 (org-require-remember)
605 (cond
606 ((equal goto '(4)) (org-go-to-remember-target))
607 ((equal goto '(16)) (org-remember-goto-last-stored))
609 ;; set temporary variables that will be needed in
610 ;; `org-select-remember-template'
611 (setq org-select-template-temp-major-mode major-mode)
612 (setq org-select-template-original-buffer (current-buffer))
613 (if (eq org-finish-function 'org-remember-finalize)
614 (progn
615 (when (< (length org-remember-templates) 2)
616 (error "No other template available"))
617 (erase-buffer)
618 (let ((annotation (plist-get org-store-link-plist :annotation))
619 (initial (plist-get org-store-link-plist :initial)))
620 (org-remember-apply-template))
621 (message "Press C-c C-c to remember data"))
622 (if (org-region-active-p)
623 (org-do-remember (buffer-substring (point) (mark)))
624 (org-do-remember))))))
626 (defvar org-remember-last-stored-marker (make-marker)
627 "Marker pointing to the entry most recently stored with `org-remember'.")
629 (defun org-remember-goto-last-stored ()
630 "Go to the location where the last remember note was stored."
631 (interactive)
632 (org-goto-marker-or-bmk org-remember-last-stored-marker
633 "org-remember-last-stored")
634 (message "This is the last note stored by remember"))
636 (defun org-go-to-remember-target (&optional template-key)
637 "Go to the target location of a remember template.
638 The user is queried for the template."
639 (interactive)
640 (let* (org-select-template-temp-major-mode
641 (entry (org-select-remember-template template-key))
642 (file (nth 1 entry))
643 (heading (nth 2 entry))
644 visiting)
645 (unless (and file (stringp file) (string-match "\\S-" file))
646 (setq file org-default-notes-file))
647 (when (and file (not (file-name-absolute-p file)))
648 (setq file (expand-file-name file org-directory)))
649 (unless (and heading (stringp heading) (string-match "\\S-" heading))
650 (setq heading org-remember-default-headline))
651 (setq visiting (org-find-base-buffer-visiting file))
652 (if (not visiting) (find-file-noselect file))
653 (switch-to-buffer (or visiting (get-file-buffer file)))
654 (widen)
655 (goto-char (point-min))
656 (if (re-search-forward
657 (concat "^\\*+[ \t]+" (regexp-quote heading)
658 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
659 nil t)
660 (goto-char (match-beginning 0))
661 (error "Target headline not found: %s" heading))))
663 ;;;###autoload
664 (defun org-remember-handler ()
665 "Store stuff from remember.el into an org file.
666 When the template has specified a file and a headline, the entry is filed
667 there, or in the location defined by `org-default-notes-file' and
668 `org-remember-default-headline'.
670 If no defaults have been defined, or if the current prefix argument
671 is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
672 process is used to select the target location.
674 When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
675 the entry is filed to the same location as the previous note.
677 When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
678 the entry is fild as a subentry of the entry where the clock is
679 currently running.
681 When `C-u' has been used as prefix argument, the note is stored and emacs
682 moves point to the new location of the note, so that editing can be
683 continued there (smilar to inserting \"%&\" into the tempate).
685 Before storing the note, the function ensures that the text has an
686 org-mode-style headline, i.e. a first line that starts with
687 a \"*\". If not, a headline is constructed from the current date and
688 some additional data.
690 If the variable `org-adapt-indentation' is non-nil, the entire text is
691 also indented so that it starts in the same column as the headline
692 \(i.e. after the stars).
694 See also the variable `org-reverse-note-order'."
695 (when (and (equal current-prefix-arg 2)
696 (not (marker-buffer org-clock-marker)))
697 (error "No runing clock"))
698 (when (org-bound-and-true-p org-jump-to-target-location)
699 (let* ((end (min (point-max) (1+ (point))))
700 (beg (point)))
701 (if (= end beg) (setq beg (1- beg)))
702 (put-text-property beg end 'org-position-cursor t)))
703 (goto-char (point-min))
704 (while (looking-at "^[ \t]*\n\\|^##.*\n")
705 (replace-match ""))
706 (goto-char (point-max))
707 (beginning-of-line 1)
708 (while (looking-at "[ \t]*$\\|##.*")
709 (delete-region (1- (point)) (point-max))
710 (beginning-of-line 1))
711 (catch 'quit
712 (if org-note-abort (throw 'quit nil))
713 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
714 (previousp (and (member current-prefix-arg '((16) 0))
715 org-remember-previous-location))
716 (clockp (equal current-prefix-arg 2))
717 (fastp (org-xor (equal current-prefix-arg 1)
718 org-remember-store-without-prompt))
719 (file (cond
720 (fastp org-default-notes-file)
721 ((and (eq org-remember-interactive-interface 'refile)
722 org-refile-targets)
723 org-default-notes-file)
724 ((not previousp)
725 (org-get-org-file))))
726 (heading org-remember-default-headline)
727 (visiting (and file (org-find-base-buffer-visiting file)))
728 (org-startup-folded nil)
729 (org-startup-align-all-tables nil)
730 (org-goto-start-pos 1)
731 spos exitcmd level reversed txt)
732 (when (equal current-prefix-arg '(4))
733 (setq visitp t))
734 (when previousp
735 (setq file (car org-remember-previous-location)
736 heading (cdr org-remember-previous-location)
737 fastp t))
738 (when clockp
739 (setq file (buffer-file-name (marker-buffer org-clock-marker))
740 heading org-clock-heading-for-remember
741 fastp t))
742 (setq current-prefix-arg nil)
743 ;; Modify text so that it becomes a nice subtree which can be inserted
744 ;; into an org tree.
745 (goto-char (point-min))
746 (if (re-search-forward "[ \t\n]+\\'" nil t)
747 ;; remove empty lines at end
748 (replace-match ""))
749 (goto-char (point-min))
750 (unless (looking-at org-outline-regexp)
751 ;; add a headline
752 (insert (concat "* " (current-time-string)
753 " (" (remember-buffer-desc) ")\n"))
754 (backward-char 1)
755 (when org-adapt-indentation
756 (while (re-search-forward "^" nil t)
757 (insert " "))))
758 (goto-char (point-min))
759 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
760 (replace-match "\n\n")
761 (if (re-search-forward "[ \t\n]*\\'")
762 (replace-match "\n")))
763 (goto-char (point-min))
764 (setq txt (buffer-string))
765 (org-save-markers-in-region (point-min) (point-max))
766 (when (and (eq org-remember-interactive-interface 'refile)
767 (not fastp))
768 (org-refile nil (or visiting (find-file-noselect file)))
769 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
770 (throw 'quit t))
771 ;; Find the file
772 (if (not visiting) (find-file-noselect file))
773 (with-current-buffer (or visiting (get-file-buffer file))
774 (unless (org-mode-p)
775 (error "Target files for remember notes must be in Org-mode"))
776 (save-excursion
777 (save-restriction
778 (widen)
779 (and (goto-char (point-min))
780 (not (re-search-forward "^\\* " nil t))
781 (insert "\n* " (or (and (stringp heading) heading)
782 "Notes") "\n"))
783 (setq reversed (org-notes-order-reversed-p))
785 ;; Find the default location
786 (when heading
787 (cond
788 ((eq heading 'top)
789 (goto-char (point-min))
790 (or (looking-at org-outline-regexp)
791 (re-search-forward org-outline-regexp nil t))
792 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
793 ((eq heading 'bottom)
794 (goto-char (point-max))
795 (or (bolp) (newline))
796 (setq org-goto-start-pos (point)))
797 ((and (stringp heading) (string-match "\\S-" heading))
798 (goto-char (point-min))
799 (if (re-search-forward
800 (concat "^\\*+[ \t]+" (regexp-quote heading)
801 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
802 nil t)
803 (setq org-goto-start-pos (match-beginning 0))
804 (when fastp
805 (goto-char (point-max))
806 (unless (bolp) (newline))
807 (insert "* " heading "\n")
808 (setq org-goto-start-pos (point-at-bol 0)))))
809 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
810 heading 'top))))
812 ;; Ask the User for a location, using the appropriate interface
813 (cond
814 ((and fastp (memq heading '(top bottom)))
815 (setq spos org-goto-start-pos
816 exitcmd (if (eq heading 'top) 'left nil)))
817 (fastp (setq spos org-goto-start-pos
818 exitcmd 'return))
819 ((eq org-remember-interactive-interface 'outline)
820 (setq spos (org-get-location (current-buffer)
821 org-remember-help)
822 exitcmd (cdr spos)
823 spos (car spos)))
824 ((eq org-remember-interactive-interface 'outline-path-completion)
825 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
826 (org-refile-use-outline-path t))
827 (setq spos (org-refile-get-location "Heading: ")
828 exitcmd 'return
829 spos (nth 3 spos))))
830 (t (error "This should not happen")))
831 (if (not spos) (throw 'quit nil)) ; return nil to show we did
832 ; not handle this note
833 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
834 (goto-char spos)
835 (cond ((org-on-heading-p t)
836 (org-back-to-heading t)
837 (setq level (funcall outline-level))
838 (cond
839 ((eq exitcmd 'return)
840 ;; sublevel of current
841 (setq org-remember-previous-location
842 (cons (abbreviate-file-name file)
843 (org-get-heading 'notags)))
844 (if reversed
845 (outline-next-heading)
846 (org-end-of-subtree t)
847 (if (not (bolp))
848 (if (looking-at "[ \t]*\n")
849 (beginning-of-line 2)
850 (end-of-line 1)
851 (insert "\n"))))
852 (org-paste-subtree (org-get-valid-level level 1) txt)
853 (and org-auto-align-tags (org-set-tags nil t))
854 (bookmark-set "org-remember-last-stored")
855 (move-marker org-remember-last-stored-marker (point)))
856 ((eq exitcmd 'left)
857 ;; before current
858 (org-paste-subtree level txt)
859 (and org-auto-align-tags (org-set-tags nil t))
860 (bookmark-set "org-remember-last-stored")
861 (move-marker org-remember-last-stored-marker (point)))
862 ((eq exitcmd 'right)
863 ;; after current
864 (org-end-of-subtree t)
865 (org-paste-subtree level txt)
866 (and org-auto-align-tags (org-set-tags nil t))
867 (bookmark-set "org-remember-last-stored")
868 (move-marker org-remember-last-stored-marker (point)))
869 (t (error "This should not happen"))))
871 ((eq heading 'bottom)
872 (org-paste-subtree 1 txt)
873 (and org-auto-align-tags (org-set-tags nil t))
874 (bookmark-set "org-remember-last-stored")
875 (move-marker org-remember-last-stored-marker (point)))
877 ((and (bobp) (not reversed))
878 ;; Put it at the end, one level below level 1
879 (save-restriction
880 (widen)
881 (goto-char (point-max))
882 (if (not (bolp)) (newline))
883 (org-paste-subtree (org-get-valid-level 1 1) txt)
884 (and org-auto-align-tags (org-set-tags nil t))
885 (bookmark-set "org-remember-last-stored")
886 (move-marker org-remember-last-stored-marker (point))))
888 ((and (bobp) reversed)
889 ;; Put it at the start, as level 1
890 (save-restriction
891 (widen)
892 (goto-char (point-min))
893 (re-search-forward "^\\*+ " nil t)
894 (beginning-of-line 1)
895 (org-paste-subtree 1 txt)
896 (and org-auto-align-tags (org-set-tags nil t))
897 (bookmark-set "org-remember-last-stored")
898 (move-marker org-remember-last-stored-marker (point))))
900 ;; Put it right there, with automatic level determined by
901 ;; org-paste-subtree or from prefix arg
902 (org-paste-subtree
903 (if (numberp current-prefix-arg) current-prefix-arg)
904 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))))
909 (when remember-save-after-remembering
910 (save-buffer)
911 (if (and (not visiting)
912 (not (equal (marker-buffer org-clock-marker)
913 (current-buffer))))
914 (kill-buffer (current-buffer)))))))))
916 t) ;; return t to indicate that we took care of this note.
918 (defun org-do-remember (&optional initial)
919 "Call remember."
920 (remember initial))
922 (defun org-require-remember ()
923 "Make sure remember is loaded, or install our own emergency version of it."
924 (condition-case nil
925 (require 'remember)
926 (error
927 ;; Lets install our own micro version of remember
928 (defvar remember-register ?R)
929 (defvar remember-mode-hook nil)
930 (defvar remember-handler-functions nil)
931 (defvar remember-buffer "*Remember*")
932 (defvar remember-save-after-remembering t)
933 (defvar remember-annotation-functions '(buffer-file-name))
934 (defun remember-finalize ()
935 (run-hook-with-args-until-success 'remember-handler-functions)
936 (when (equal remember-buffer (buffer-name))
937 (kill-buffer (current-buffer))
938 (jump-to-register remember-register)))
939 (defun remember-mode ()
940 (fundamental-mode)
941 (setq mode-name "Remember")
942 (run-hooks 'remember-mode-hook))
943 (defun remember (&optional initial)
944 (window-configuration-to-register remember-register)
945 (let* ((annotation (run-hook-with-args-until-success
946 'remember-annotation-functions)))
947 (switch-to-buffer-other-window (get-buffer-create remember-buffer))
948 (remember-mode)))
949 (defun remember-buffer-desc ()
950 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
951 (point-at-eol)))))))
953 (provide 'org-remember)
955 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
957 ;;; org-remember.el ends here