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