Release 6.05b.
[org-mode.git] / lisp / org-remember.el
blobee8bc3e0d4c497c2e2fe7433d6aa03d3e952402a
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.05b
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 "remember" (&optional initial))
38 (declare-function remember-buffer-desc "remember" ())
39 (declare-function remember-finalize "remember" ())
40 (defvar remember-save-after-remembering)
41 (defvar remember-data-file)
42 (defvar remember-register)
43 (defvar remember-buffer)
44 (defvar remember-handler-functions)
45 (defvar remember-annotation-functions)
47 (defgroup org-remember nil
48 "Options concerning interaction with remember.el."
49 :tag "Org Remember"
50 :group 'org)
52 (defcustom org-remember-store-without-prompt t
53 "Non-nil means, `C-c C-c' stores remember note without further prompts.
54 It then uses the file and headline specified by the template or (if the
55 themplate does not specify them) by the variables `org-default-notes-file'
56 and `org-remember-default-headline'. To force prompting anyway, use
57 `C-u C-c C-c' to file the note.
59 When this variable is nil, `C-c C-c' gives you the prompts, and
60 `C-u C-c C-c' triggers the fasttrack."
61 :group 'org-remember
62 :type 'boolean)
64 (defcustom org-remember-interactive-interface 'refile
65 "The interface to be used for interactive filing of remember notes.
66 This is only used when the interactive mode for selecting a filing
67 location is used (see the variable `org-remember-store-without-prompt').
68 Allowed vaues are:
69 outline The interface shows an outline of the relevant file
70 and the correct heading is found by moving through
71 the outline or by searching with incremental search.
72 outline-path-completion Headlines in the current buffer are offered via
73 completion.
74 refile Use the refile interface, and offer headlines,
75 possibly from different buffers."
76 :group 'org-remember
77 :type '(choice
78 (const :tag "Refile" refile)
79 (const :tag "Outline" outline)
80 (const :tag "Outline-path-completion" outline-path-completion)))
82 (defcustom org-remember-default-headline ""
83 "The headline that should be the default location in the notes file.
84 When filing remember notes, the cursor will start at that position.
85 You can set this on a per-template basis with the variable
86 `org-remember-templates'."
87 :group 'org-remember
88 :type 'string)
90 (defcustom org-remember-templates nil
91 "Templates for the creation of remember buffers.
92 When nil, just let remember make the buffer.
93 When non-nil, this is a list of 5-element lists. In each entry, the first
94 element is the name of the template, which should be a single short word.
95 The second element is a character, a unique key to select this template.
96 The third element is the template.
98 The fourth element is optional and can specify a destination file for
99 remember items created with this template. The default file is given
100 by `org-default-notes-file'. If the file name is not an absolute path,
101 it will be interpreted relative to `org-directory'.
103 An optional fifth element can specify the headline in that file that should
104 be offered first when the user is asked to file the entry. The default
105 headline is given in the variable `org-remember-default-headline'. When
106 this element is `top' or `bottom', the note will be placed as a level-1
107 entry at the beginning or end of the file, respectively.
109 An optional sixth element specifies the contexts in which the template
110 will be offered to the user. This element can be a list of major modes
111 or a function, and the template will only be offered if `org-remember'
112 is called from a mode in the list, or if the function returns t.
113 Templates that specify t or nil for the context will be always be added
114 to the list of selectable templates.
116 The template specifies the structure of the remember buffer. It should have
117 a first line starting with a star, to act as the org-mode headline.
118 Furthermore, the following %-escapes will be replaced with content:
120 %^{prompt} Prompt the user for a string and replace this sequence with it.
121 A default value and a completion table ca be specified like this:
122 %^{prompt|default|completion2|completion3|...}
123 %t time stamp, date only
124 %T time stamp with date and time
125 %u, %U like the above, but inactive time stamps
126 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
127 You may define a prompt like %^{Please specify birthday
128 %n user name (taken from `user-full-name')
129 %a annotation, normally the link created with org-store-link
130 %i initial content, the region active. If %i is indented,
131 the entire inserted text will be indented as well.
132 %c current kill ring head
133 %x content of the X clipboard
134 %^C Interactive selection of which kill or clip to use
135 %^L Like %^C, but insert as link
136 %^g prompt for tags, with completion on tags in target file
137 %^G prompt for tags, with completion all tags in all agenda files
138 %:keyword specific information for certain link types, see below
139 %[pathname] insert the contents of the file given by `pathname'
140 %(sexp) evaluate elisp `(sexp)' and replace with the result
141 %! Store this note immediately after filling the template
142 %& Visit note immediately after storing it
144 %? After completing the template, position cursor here.
146 Apart from these general escapes, you can access information specific to the
147 link type that is created. For example, calling `remember' in emails or gnus
148 will record the author and the subject of the message, which you can access
149 with %:author and %:subject, respectively. Here is a complete list of what
150 is recorded for each link type.
152 Link type | Available information
153 -------------------+------------------------------------------------------
154 bbdb | %:type %:name %:company
155 vm, wl, mh, rmail | %:type %:subject %:message-id
156 | %:from %:fromname %:fromaddress
157 | %:to %:toname %:toaddress
158 | %:fromto (either \"to NAME\" or \"from NAME\")
159 gnus | %:group, for messages also all email fields
160 w3, w3m | %:type %:url
161 info | %:type %:file %:node
162 calendar | %:type %:date"
163 :group 'org-remember
164 :get (lambda (var) ; Make sure all entries have at least 5 elements
165 (mapcar (lambda (x)
166 (if (not (stringp (car x))) (setq x (cons "" x)))
167 (cond ((= (length x) 4) (append x '("")))
168 ((= (length x) 3) (append x '("" "")))
169 (t x)))
170 (default-value var)))
171 :type '(repeat
172 :tag "enabled"
173 (list :value ("" ?a "\n" nil nil nil)
174 (string :tag "Name")
175 (character :tag "Selection Key")
176 (string :tag "Template")
177 (choice :tag "Destination file"
178 (file :tag "Specify")
179 (const :tag "Use `org-default-notes-file'" nil))
180 (choice :tag "Destin. headline"
181 (string :tag "Specify")
182 (const :tag "Use `org-remember-default-headline'" nil)
183 (const :tag "Level 1 at beginning of file" top)
184 (const :tag "Level 1 at end of file" bottom))
185 (choice :tag "Context"
186 (const :tag "Use in all contexts" nil)
187 (const :tag "Use in all contexts" t)
188 (repeat :tag "Use only if in major mode"
189 (symbol :tag "Major mode"))
190 (function :tag "Perform a check against function")))))
192 (defcustom org-remember-clock-out-on-exit 'query
193 "Non-nil means, stop the clock when exiting a clocking remember buffer.
194 This only applies if the clock is running in the remember buffer. If the
195 clock is not stopped, it continues to run in the storage location.
196 Instead of nil or t, this may also be the symbol `query' to prompt the
197 user each time a remember buffer with a running clock is filed away. "
198 :group 'org-remember
199 :type '(choice
200 (const :tag "Never" nil)
201 (const :tag "Always" t)
202 (const :tag "Query user" query)))
205 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
206 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
208 ;;;###autoload
209 (defun org-remember-insinuate ()
210 "Setup remember.el for use wiht Org-mode."
211 (require 'remember)
212 (setq remember-annotation-functions '(org-remember-annotation))
213 (setq remember-handler-functions '(org-remember-handler))
214 (add-hook 'remember-mode-hook 'org-remember-apply-template))
216 ;;;###autoload
217 (defun org-remember-annotation ()
218 "Return a link to the current location as an annotation for remember.el.
219 If you are using Org-mode files as target for data storage with
220 remember.el, then the annotations should include a link compatible with the
221 conventions in Org-mode. This function returns such a link."
222 (org-store-link nil))
224 (defconst org-remember-help
225 "Select a destination location for the note.
226 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
227 RET on headline -> Store as sublevel entry to current headline
228 RET at beg-of-buf -> Append to file as level 2 headline
229 <left>/<right> -> before/after current headline, same headings level")
231 (defvar org-jump-to-target-location nil)
232 (defvar org-remember-previous-location nil)
233 (defvar org-force-remember-template-char) ;; dynamically scoped
235 ;; Save the major mode of the buffer we called remember from
236 (defvar org-select-template-temp-major-mode nil)
238 ;; Temporary store the buffer where remember was called from
239 (defvar org-select-template-original-buffer nil)
241 (defun org-select-remember-template (&optional use-char)
242 (when org-remember-templates
243 (let* ((pre-selected-templates
244 (mapcar
245 (lambda (tpl)
246 (let ((ctxt (nth 5 tpl))
247 (mode org-select-template-temp-major-mode)
248 (buf org-select-template-original-buffer))
249 (and (or (not ctxt) (eq ctxt t)
250 (and (listp ctxt) (memq mode ctxt))
251 (and (functionp ctxt)
252 (with-current-buffer buf
253 ;; Protect the user-defined function from error
254 (condition-case nil (funcall ctxt) (error nil)))))
255 tpl)))
256 org-remember-templates))
257 ;; If no template at this point, add the default templates:
258 (pre-selected-templates1
259 (if (not (delq nil pre-selected-templates))
260 (mapcar (lambda(x) (if (not (nth 5 x)) x))
261 org-remember-templates)
262 pre-selected-templates))
263 ;; Then unconditionnally add template for any contexts
264 (pre-selected-templates2
265 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
266 org-remember-templates)
267 (delq nil pre-selected-templates1)))
268 (templates (mapcar (lambda (x)
269 (if (stringp (car x))
270 (append (list (nth 1 x) (car x)) (cddr x))
271 (append (list (car x) "") (cdr x))))
272 (delq nil pre-selected-templates2)))
273 (char (or use-char
274 (cond
275 ((= (length templates) 1)
276 (caar templates))
277 ((and (boundp 'org-force-remember-template-char)
278 org-force-remember-template-char)
279 (if (stringp org-force-remember-template-char)
280 (string-to-char org-force-remember-template-char)
281 org-force-remember-template-char))
283 (message "Select template: %s"
284 (mapconcat
285 (lambda (x)
286 (cond
287 ((not (string-match "\\S-" (nth 1 x)))
288 (format "[%c]" (car x)))
289 ((equal (downcase (car x))
290 (downcase (aref (nth 1 x) 0)))
291 (format "[%c]%s" (car x)
292 (substring (nth 1 x) 1)))
293 (t (format "[%c]%s" (car x) (nth 1 x)))))
294 templates " "))
295 (let ((inhibit-quit t) (char0 (read-char-exclusive)))
296 (when (equal char0 ?\C-g)
297 (jump-to-register remember-register)
298 (kill-buffer remember-buffer))
299 char0))))))
300 (cddr (assoc char templates)))))
302 (defun org-get-x-clipboard (value)
303 "Get the value of the x clibboard, in a way that also works with XEmacs."
304 (if (eq window-system 'x)
305 (let ((x (if org-xemacs-p
306 (org-no-warnings (get-selection-no-error value))
307 (and (fboundp 'x-selection-value)
308 (x-selection-value value)))))
309 (and (> (length x) 0) (set-text-properties 0 (length x) nil x) x))))
311 ;;;###autoload
312 (defun org-remember-apply-template (&optional use-char skip-interactive)
313 "Initialize *remember* buffer with template, invoke `org-mode'.
314 This function should be placed into `remember-mode-hook' and in fact requires
315 to be run from that hook to function properly."
316 (if org-remember-templates
317 (let* ((entry (org-select-remember-template use-char))
318 (ct (or org-overriding-default-time (org-current-time)))
319 (tpl (car entry))
320 (plist-p (if org-store-link-plist t nil))
321 (file (if (and (nth 1 entry) (stringp (nth 1 entry))
322 (string-match "\\S-" (nth 1 entry)))
323 (nth 1 entry)
324 org-default-notes-file))
325 (headline (nth 2 entry))
326 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
327 (v-x (or (org-get-x-clipboard 'PRIMARY)
328 (org-get-x-clipboard 'CLIPBOARD)
329 (org-get-x-clipboard 'SECONDARY)))
330 (v-t (format-time-string (car org-time-stamp-formats) ct))
331 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
332 (v-u (concat "[" (substring v-t 1 -1) "]"))
333 (v-U (concat "[" (substring v-T 1 -1) "]"))
334 ;; `initial' and `annotation' are bound in `remember'
335 (v-i (if (boundp 'initial) initial))
336 (v-a (if (and (boundp 'annotation) annotation)
337 (if (equal annotation "[[]]") "" annotation)
338 ""))
339 (clipboards (remove nil (list v-i
340 (org-get-x-clipboard 'PRIMARY)
341 (org-get-x-clipboard 'CLIPBOARD)
342 (org-get-x-clipboard 'SECONDARY)
343 v-c)))
344 (v-A (if (and v-a
345 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
346 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
347 v-a))
348 (v-n user-full-name)
349 (org-startup-folded nil)
350 org-time-was-given org-end-time-was-given x
351 prompt completions char time pos default histvar)
352 (when (and file (not (file-name-absolute-p file)))
353 (setq file (expand-file-name file org-directory)))
354 (setq org-store-link-plist
355 (append (list :annotation v-a :initial v-i)
356 org-store-link-plist))
357 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
358 (erase-buffer)
359 (insert (substitute-command-keys
360 (format
361 "## Filing location: Select interactively, default, or last used:
362 ## %s to select file and header location interactively.
363 ## %s \"%s\" -> \"* %s\"
364 ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
365 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
366 (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
367 (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
368 (abbreviate-file-name (or file org-default-notes-file))
369 (or headline "")
370 (or (car org-remember-previous-location) "???")
371 (or (cdr org-remember-previous-location) "???"))))
372 (insert tpl) (goto-char (point-min))
373 ;; Simple %-escapes
374 (while (re-search-forward "%\\([tTuUaiAcx]\\)" nil t)
375 (when (and initial (equal (match-string 0) "%i"))
376 (save-match-data
377 (let* ((lead (buffer-substring
378 (point-at-bol) (match-beginning 0))))
379 (setq v-i (mapconcat 'identity
380 (org-split-string initial "\n")
381 (concat "\n" lead))))))
382 (replace-match
383 (or (eval (intern (concat "v-" (match-string 1)))) "")
384 t t))
386 ;; %[] Insert contents of a file.
387 (goto-char (point-min))
388 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
389 (let ((start (match-beginning 0))
390 (end (match-end 0))
391 (filename (expand-file-name (match-string 1))))
392 (goto-char start)
393 (delete-region start end)
394 (condition-case error
395 (insert-file-contents filename)
396 (error (insert (format "%%![Couldn't insert %s: %s]"
397 filename error))))))
398 ;; %() embedded elisp
399 (goto-char (point-min))
400 (while (re-search-forward "%\\((.+)\\)" nil t)
401 (goto-char (match-beginning 0))
402 (let ((template-start (point)))
403 (forward-char 1)
404 (let ((result
405 (condition-case error
406 (eval (read (current-buffer)))
407 (error (format "%%![Error: %s]" error)))))
408 (delete-region template-start (point))
409 (insert result))))
411 ;; From the property list
412 (when plist-p
413 (goto-char (point-min))
414 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
415 (and (setq x (or (plist-get org-store-link-plist
416 (intern (match-string 1))) ""))
417 (replace-match x t t))))
419 ;; Turn on org-mode in the remember buffer, set local variables
420 (org-mode)
421 (org-set-local 'org-finish-function 'org-remember-finalize)
422 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
423 (org-set-local 'org-default-notes-file file))
424 (if headline
425 (org-set-local 'org-remember-default-headline headline))
426 ;; Interactive template entries
427 (goto-char (point-min))
428 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCL]\\)?" nil t)
429 (setq char (if (match-end 3) (match-string 3))
430 prompt (if (match-end 2) (match-string 2)))
431 (goto-char (match-beginning 0))
432 (replace-match "")
433 (setq completions nil default nil)
434 (when prompt
435 (setq completions (org-split-string prompt "|")
436 prompt (pop completions)
437 default (car completions)
438 histvar (intern (concat
439 "org-remember-template-prompt-history::"
440 (or prompt "")))
441 completions (mapcar 'list completions)))
442 (cond
443 ((member char '("G" "g"))
444 (let* ((org-last-tags-completion-table
445 (org-global-tags-completion-table
446 (if (equal char "G") (org-agenda-files) (and file (list file)))))
447 (org-add-colon-after-tag-completion t)
448 (ins (completing-read
449 (if prompt (concat prompt ": ") "Tags: ")
450 'org-tags-completion-function nil nil nil
451 'org-tags-history)))
452 (setq ins (mapconcat 'identity
453 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
454 ":"))
455 (when (string-match "\\S-" ins)
456 (or (equal (char-before) ?:) (insert ":"))
457 (insert ins)
458 (or (equal (char-after) ?:) (insert ":")))))
459 ((equal char "C")
460 (cond ((= (length clipboards) 1) (insert (car clipboards)))
461 ((> (length clipboards) 1)
462 (insert (read-string "Clipboard/kill value: "
463 (car clipboards) '(clipboards . 1)
464 (car clipboards))))))
465 ((equal char "L")
466 (cond ((= (length clipboards) 1)
467 (org-insert-link 0 (car clipboards)))
468 ((> (length clipboards) 1)
469 (org-insert-link 0 (read-string "Clipboard/kill value: "
470 (car clipboards)
471 '(clipboards . 1)
472 (car clipboards))))))
473 (char
474 ;; These are the date/time related ones
475 (setq org-time-was-given (equal (upcase char) char))
476 (setq time (org-read-date (equal (upcase char) "U") t nil
477 prompt))
478 (org-insert-time-stamp time org-time-was-given
479 (member char '("u" "U"))
480 nil nil (list org-end-time-was-given)))
482 (insert (org-completing-read
483 (concat (if prompt prompt "Enter string")
484 (if default (concat " [" default "]"))
485 ": ")
486 completions nil nil nil histvar default)))))
487 (goto-char (point-min))
488 (if (re-search-forward "%\\?" nil t)
489 (replace-match "")
490 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
491 (org-mode)
492 (org-set-local 'org-finish-function 'org-remember-finalize))
493 (when (save-excursion
494 (goto-char (point-min))
495 (re-search-forward "%&" nil t))
496 (replace-match "")
497 (org-set-local 'org-jump-to-target-location t))
498 (when (save-excursion
499 (goto-char (point-min))
500 (re-search-forward "%!" nil t))
501 (replace-match "")
502 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
504 (defun org-remember-finish-immediately ()
505 "File remember note immediately.
506 This should be run in `post-command-hook' and will remove itself
507 from that hook."
508 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
509 (when org-finish-function
510 (funcall org-finish-function)))
512 (defun org-remember-visit-immediately ()
513 "File remember note immediately.
514 This should be run in `post-command-hook' and will remove itself
515 from that hook."
516 (org-remember '(16))
517 (goto-char (or (text-property-any
518 (point) (save-excursion (org-end-of-subtree t t))
519 'org-position-cursor t)
520 (point)))
521 (message "%s"
522 (format
523 (substitute-command-keys
524 "Restore window configuration with \\[jump-to-register] %c")
525 remember-register)))
527 (defvar org-clock-marker) ; Defined in org.el
528 (defun org-remember-finalize ()
529 "Finalize the remember process."
530 (unless (fboundp 'remember-finalize)
531 (defalias 'remember-finalize 'remember-buffer))
532 (when (and org-clock-marker
533 (equal (marker-buffer org-clock-marker) (current-buffer)))
534 ;; the clock is running in this buffer.
535 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
536 (or (eq org-remember-clock-out-on-exit t)
537 (and org-remember-clock-out-on-exit
538 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
539 (let (org-log-note-clock-out) (org-clock-out))))
540 (when buffer-file-name
541 (save-buffer)
542 (setq buffer-file-name nil))
543 (remember-finalize))
545 ;;;###autoload
546 (defun org-remember (&optional goto org-force-remember-template-char)
547 "Call `remember'. If this is already a remember buffer, re-apply template.
548 If there is an active region, make sure remember uses it as initial content
549 of the remember buffer.
551 When called interactively with a `C-u' prefix argument GOTO, don't remember
552 anything, just go to the file/headline where the selected template usually
553 stores its notes. With a double prefix arg `C-u C-u', go to the last
554 note stored by remember.
556 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
557 associated with a template in `org-remember-templates'."
558 (interactive "P")
559 (cond
560 ((equal goto '(4)) (org-go-to-remember-target))
561 ((equal goto '(16)) (org-remember-goto-last-stored))
563 ;; set temporary variables that will be needed in
564 ;; `org-select-remember-template'
565 (setq org-select-template-temp-major-mode major-mode)
566 (setq org-select-template-original-buffer (current-buffer))
567 (if (eq org-finish-function 'org-remember-finalize)
568 (progn
569 (when (< (length org-remember-templates) 2)
570 (error "No other template available"))
571 (erase-buffer)
572 (let ((annotation (plist-get org-store-link-plist :annotation))
573 (initial (plist-get org-store-link-plist :initial)))
574 (org-remember-apply-template))
575 (message "Press C-c C-c to remember data"))
576 (if (org-region-active-p)
577 (org-do-remember (buffer-substring (point) (mark)))
578 (org-do-remember))))))
580 (defvar org-remember-last-stored-marker (make-marker)
581 "Marker pointing to the entry most recently stored with `org-remember'.")
583 (defun org-remember-goto-last-stored ()
584 "Go to the location where the last remember note was stored."
585 (interactive)
586 (org-goto-marker-or-bmk org-remember-last-stored-marker
587 "org-remember-last-stored")
588 (message "This is the last note stored by remember"))
590 (defun org-go-to-remember-target (&optional template-key)
591 "Go to the target location of a remember template.
592 The user is queried for the template."
593 (interactive)
594 (let* (org-select-template-temp-major-mode
595 (entry (org-select-remember-template template-key))
596 (file (nth 1 entry))
597 (heading (nth 2 entry))
598 visiting)
599 (unless (and file (stringp file) (string-match "\\S-" file))
600 (setq file org-default-notes-file))
601 (when (and file (not (file-name-absolute-p file)))
602 (setq file (expand-file-name file org-directory)))
603 (unless (and heading (stringp heading) (string-match "\\S-" heading))
604 (setq heading org-remember-default-headline))
605 (setq visiting (org-find-base-buffer-visiting file))
606 (if (not visiting) (find-file-noselect file))
607 (switch-to-buffer (or visiting (get-file-buffer file)))
608 (widen)
609 (goto-char (point-min))
610 (if (re-search-forward
611 (concat "^\\*+[ \t]+" (regexp-quote heading)
612 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
613 nil t)
614 (goto-char (match-beginning 0))
615 (error "Target headline not found: %s" heading))))
617 ;;;###autoload
618 (defun org-remember-handler ()
619 "Store stuff from remember.el into an org file.
620 First prompts for an org file. If the user just presses return, the value
621 of `org-default-notes-file' is used.
622 Then the command offers the headings tree of the selected file in order to
623 file the text at a specific location.
624 You can either immediately press RET to get the note appended to the
625 file, or you can use vertical cursor motion and visibility cycling (TAB) to
626 find a better place. Then press RET or <left> or <right> in insert the note.
628 Key Cursor position Note gets inserted
629 -----------------------------------------------------------------------------
630 RET buffer-start as level 1 heading at end of file
631 RET on headline as sublevel of the heading at cursor
632 RET no heading at cursor position, level taken from context.
633 Or use prefix arg to specify level manually.
634 <left> on headline as same level, before current heading
635 <right> on headline as same level, after current heading
637 So the fastest way to store the note is to press RET RET to append it to
638 the default file. This way your current train of thought is not
639 interrupted, in accordance with the principles of remember.el.
640 You can also get the fast execution without prompting by using
641 C-u C-c C-c to exit the remember buffer. See also the variable
642 `org-remember-store-without-prompt'.
644 Before being stored away, the function ensures that the text has a
645 headline, i.e. a first line that starts with a \"*\". If not, a headline
646 is constructed from the current date and some additional data.
648 If the variable `org-adapt-indentation' is non-nil, the entire text is
649 also indented so that it starts in the same column as the headline
650 \(i.e. after the stars).
652 See also the variable `org-reverse-note-order'."
653 (when (org-bound-and-true-p org-jump-to-target-location)
654 (let* ((end (min (point-max) (1+ (point))))
655 (beg (point)))
656 (if (= end beg) (setq beg (1- beg)))
657 (put-text-property beg end 'org-position-cursor t)))
658 (goto-char (point-min))
659 (while (looking-at "^[ \t]*\n\\|^##.*\n")
660 (replace-match ""))
661 (goto-char (point-max))
662 (beginning-of-line 1)
663 (while (looking-at "[ \t]*$\\|##.*")
664 (delete-region (1- (point)) (point-max))
665 (beginning-of-line 1))
666 (catch 'quit
667 (if org-note-abort (throw 'quit nil))
668 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
669 (fastp (org-xor (equal current-prefix-arg '(4))
670 org-remember-store-without-prompt))
671 (file (cond
672 (fastp org-default-notes-file)
673 ((and (eq org-remember-interactive-interface 'refile)
674 org-refile-targets)
675 org-default-notes-file)
676 ((not (and (equal current-prefix-arg '(16))
677 org-remember-previous-location))
678 (org-get-org-file))))
679 (heading org-remember-default-headline)
680 (visiting (and file (org-find-base-buffer-visiting file)))
681 (org-startup-folded nil)
682 (org-startup-align-all-tables nil)
683 (org-goto-start-pos 1)
684 spos exitcmd level reversed txt)
685 (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
686 (setq file (car org-remember-previous-location)
687 heading (cdr org-remember-previous-location)
688 fastp t))
689 (setq current-prefix-arg nil)
690 ;; Modify text so that it becomes a nice subtree which can be inserted
691 ;; into an org tree.
692 (goto-char (point-min))
693 (if (re-search-forward "[ \t\n]+\\'" nil t)
694 ;; remove empty lines at end
695 (replace-match ""))
696 (goto-char (point-min))
697 (unless (looking-at org-outline-regexp)
698 ;; add a headline
699 (insert (concat "* " (current-time-string)
700 " (" (remember-buffer-desc) ")\n"))
701 (backward-char 1)
702 (when org-adapt-indentation
703 (while (re-search-forward "^" nil t)
704 (insert " "))))
705 (goto-char (point-min))
706 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
707 (replace-match "\n\n")
708 (if (re-search-forward "[ \t\n]*\\'")
709 (replace-match "\n")))
710 (goto-char (point-min))
711 (setq txt (buffer-string))
712 (org-save-markers-in-region (point-min) (point-max))
713 (when (and (eq org-remember-interactive-interface 'refile)
714 (not fastp))
715 (org-refile nil (or visiting (find-file-noselect file)))
716 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
717 (throw 'quit t))
718 ;; Find the file
719 (if (not visiting) (find-file-noselect file))
720 (with-current-buffer (or visiting (get-file-buffer file))
721 (unless (org-mode-p)
722 (error "Target files for remember notes must be in Org-mode"))
723 (save-excursion
724 (save-restriction
725 (widen)
726 (and (goto-char (point-min))
727 (not (re-search-forward "^\\* " nil t))
728 (insert "\n* " (or (and (stringp heading) heading)
729 "Notes") "\n"))
730 (setq reversed (org-notes-order-reversed-p))
732 ;; Find the default location
733 (when heading
734 (cond
735 ((eq heading 'top)
736 (goto-char (point-min))
737 (or (looking-at org-outline-regexp)
738 (re-search-forward org-outline-regexp nil t))
739 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
740 ((eq heading 'bottom)
741 (goto-char (point-max))
742 (re-search-backward "^\\* " nil t)
743 (or (bolp) (newline))
744 (setq org-goto-start-pos (point)))
745 ((and (stringp heading) (string-match "\\S-" heading))
746 (goto-char (point-min))
747 (if (re-search-forward
748 (concat "^\\*+[ \t]+" (regexp-quote heading)
749 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
750 nil t)
751 (setq org-goto-start-pos (match-beginning 0))
752 (when fastp
753 (goto-char (point-max))
754 (unless (bolp) (newline))
755 (insert "* " heading "\n")
756 (setq org-goto-start-pos (point-at-bol 0)))))
757 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
758 heading 'top))))
760 ;; Ask the User for a location, using the appropriate interface
761 (cond
762 ((and fastp (memq heading '(top bottom)))
763 (setq spos org-goto-start-pos
764 exitcmd (if (eq heading 'top) 'left 'right)))
765 (fastp (setq spos org-goto-start-pos
766 exitcmd 'return))
767 ((eq org-remember-interactive-interface 'outline)
768 (setq spos (org-get-location (current-buffer)
769 org-remember-help)
770 exitcmd (cdr spos)
771 spos (car spos)))
772 ((eq org-remember-interactive-interface 'outline-path-completion)
773 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
774 (org-refile-use-outline-path t))
775 (setq spos (org-refile-get-location "Heading: ")
776 exitcmd 'return
777 spos (nth 3 spos))))
778 (t (error "This should not happen")))
779 (if (not spos) (throw 'quit nil)) ; return nil to show we did
780 ; not handle this note
781 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
782 (goto-char spos)
783 (cond ((org-on-heading-p t)
784 (org-back-to-heading t)
785 (setq level (funcall outline-level))
786 (cond
787 ((eq exitcmd 'return)
788 ;; sublevel of current
789 (setq org-remember-previous-location
790 (cons (abbreviate-file-name file)
791 (org-get-heading 'notags)))
792 (if reversed
793 (outline-next-heading)
794 (org-end-of-subtree t)
795 (if (not (bolp))
796 (if (looking-at "[ \t]*\n")
797 (beginning-of-line 2)
798 (end-of-line 1)
799 (insert "\n"))))
800 (org-paste-subtree (org-get-valid-level level 1) txt)
801 (and org-auto-align-tags (org-set-tags nil t))
802 (bookmark-set "org-remember-last-stored")
803 (move-marker org-remember-last-stored-marker (point)))
804 ((eq exitcmd 'left)
805 ;; before current
806 (org-paste-subtree level txt)
807 (and org-auto-align-tags (org-set-tags nil t))
808 (bookmark-set "org-remember-last-stored")
809 (move-marker org-remember-last-stored-marker (point)))
810 ((eq exitcmd 'right)
811 ;; after current
812 (org-end-of-subtree t)
813 (org-paste-subtree level txt)
814 (and org-auto-align-tags (org-set-tags nil t))
815 (bookmark-set "org-remember-last-stored")
816 (move-marker org-remember-last-stored-marker (point)))
817 (t (error "This should not happen"))))
819 ((and (bobp) (not reversed))
820 ;; Put it at the end, one level below level 1
821 (save-restriction
822 (widen)
823 (goto-char (point-max))
824 (if (not (bolp)) (newline))
825 (org-paste-subtree (org-get-valid-level 1 1) txt)
826 (and org-auto-align-tags (org-set-tags nil t))
827 (bookmark-set "org-remember-last-stored")
828 (move-marker org-remember-last-stored-marker (point))))
830 ((and (bobp) reversed)
831 ;; Put it at the start, as level 1
832 (save-restriction
833 (widen)
834 (goto-char (point-min))
835 (re-search-forward "^\\*+ " nil t)
836 (beginning-of-line 1)
837 (org-paste-subtree 1 txt)
838 (and org-auto-align-tags (org-set-tags nil t))
839 (bookmark-set "org-remember-last-stored")
840 (move-marker org-remember-last-stored-marker (point))))
842 ;; Put it right there, with automatic level determined by
843 ;; org-paste-subtree or from prefix arg
844 (org-paste-subtree
845 (if (numberp current-prefix-arg) current-prefix-arg)
846 txt)
847 (and org-auto-align-tags (org-set-tags nil t))
848 (bookmark-set "org-remember-last-stored")
849 (move-marker org-remember-last-stored-marker (point))))
851 (when remember-save-after-remembering
852 (save-buffer)
853 (if (and (not visiting)
854 (not (equal (marker-buffer org-clock-marker)
855 (current-buffer))))
856 (kill-buffer (current-buffer)))))))))
858 t) ;; return t to indicate that we took care of this note.
861 (defun org-do-remember (&optional initial)
862 "Call remember."
863 (remember initial))
865 (provide 'org-remember)
867 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
869 ;;; org-remember.el ends here