Move markers with some cut-and-paste operations.
[org-mode.git] / lisp / org-remember.el
blobf7d446eb9fcb5ef1a477fa78b11eee8f5e949aec
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.02b
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 (defcustom org-remember-clock-out-on-exit 'query
183 "Non-nil means, stop the clock when exiting a clocking remember buffer.
184 This only applies of the clock is running in the remember buffer. If the
185 clock is not stopped, it continues to run in the storage location.
186 Instead of nil or t, this may also be the symbol `query' to prompt the
187 user each time a remember buffer with a running clock is filed away. "
188 :group 'org-remember
189 :type '(choice
190 (const :tag "Never" nil)
191 (const :tag "Always" t)
192 (const :tag "Query user" query)))
195 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
196 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
198 ;;;###autoload
199 (defun org-remember-insinuate ()
200 "Setup remember.el for use wiht Org-mode."
201 (require 'remember)
202 (setq remember-annotation-functions '(org-remember-annotation))
203 (setq remember-handler-functions '(org-remember-handler))
204 (add-hook 'remember-mode-hook 'org-remember-apply-template))
206 ;;;###autoload
207 (defun org-remember-annotation ()
208 "Return a link to the current location as an annotation for remember.el.
209 If you are using Org-mode files as target for data storage with
210 remember.el, then the annotations should include a link compatible with the
211 conventions in Org-mode. This function returns such a link."
212 (org-store-link nil))
214 (defconst org-remember-help
215 "Select a destination location for the note.
216 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
217 RET on headline -> Store as sublevel entry to current headline
218 RET at beg-of-buf -> Append to file as level 2 headline
219 <left>/<right> -> before/after current headline, same headings level")
221 (defvar org-remember-previous-location nil)
222 (defvar org-force-remember-template-char) ;; dynamically scoped
224 ;; Save the major mode of the buffer we called remember from
225 (defvar org-select-template-temp-major-mode nil)
227 ;; Temporary store the buffer where remember was called from
228 (defvar org-select-template-original-buffer nil)
230 (defun org-select-remember-template (&optional use-char)
231 (when org-remember-templates
232 (let* ((pre-selected-templates
233 (mapcar
234 (lambda (tpl)
235 (let ((ctxt (nth 5 tpl))
236 (mode org-select-template-temp-major-mode)
237 (buf org-select-template-original-buffer))
238 (and (or (not ctxt) (eq ctxt t)
239 (and (listp ctxt) (memq mode ctxt))
240 (and (functionp ctxt)
241 (with-current-buffer buf
242 ;; Protect the user-defined function from error
243 (condition-case nil (funcall ctxt) (error nil)))))
244 tpl)))
245 org-remember-templates))
246 ;; If no template at this point, add the default templates:
247 (pre-selected-templates1
248 (if (not (delq nil pre-selected-templates))
249 (mapcar (lambda(x) (if (not (nth 5 x)) x))
250 org-remember-templates)
251 pre-selected-templates))
252 ;; Then unconditionnally add template for any contexts
253 (pre-selected-templates2
254 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
255 org-remember-templates)
256 (delq nil pre-selected-templates1)))
257 (templates (mapcar (lambda (x)
258 (if (stringp (car x))
259 (append (list (nth 1 x) (car x)) (cddr x))
260 (append (list (car x) "") (cdr x))))
261 (delq nil pre-selected-templates2)))
262 (char (or use-char
263 (cond
264 ((= (length templates) 1)
265 (caar templates))
266 ((and (boundp 'org-force-remember-template-char)
267 org-force-remember-template-char)
268 (if (stringp org-force-remember-template-char)
269 (string-to-char org-force-remember-template-char)
270 org-force-remember-template-char))
272 (message "Select template: %s"
273 (mapconcat
274 (lambda (x)
275 (cond
276 ((not (string-match "\\S-" (nth 1 x)))
277 (format "[%c]" (car x)))
278 ((equal (downcase (car x))
279 (downcase (aref (nth 1 x) 0)))
280 (format "[%c]%s" (car x)
281 (substring (nth 1 x) 1)))
282 (t (format "[%c]%s" (car x) (nth 1 x)))))
283 templates " "))
284 (let ((inhibit-quit t) (char0 (read-char-exclusive)))
285 (when (equal char0 ?\C-g)
286 (jump-to-register remember-register)
287 (kill-buffer remember-buffer))
288 char0))))))
289 (cddr (assoc char templates)))))
291 (defun org-get-x-clipboard (value)
292 "Get the value of the x clibboard, in a way that also works with XEmacs."
293 (if (eq window-system 'x)
294 (let ((x (if org-xemacs-p
295 (org-no-warnings (get-selection-no-error value))
296 (and (fboundp 'x-selection-value)
297 (x-selection-value value)))))
298 (and (> (length x) 0) (set-text-properties 0 (length x) nil x) x))))
300 ;;;###autoload
301 (defun org-remember-apply-template (&optional use-char skip-interactive)
302 "Initialize *remember* buffer with template, invoke `org-mode'.
303 This function should be placed into `remember-mode-hook' and in fact requires
304 to be run from that hook to function properly."
305 (if org-remember-templates
306 (let* ((entry (org-select-remember-template use-char))
307 (tpl (car entry))
308 (plist-p (if org-store-link-plist t nil))
309 (file (if (and (nth 1 entry) (stringp (nth 1 entry))
310 (string-match "\\S-" (nth 1 entry)))
311 (nth 1 entry)
312 org-default-notes-file))
313 (headline (nth 2 entry))
314 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
315 (v-x (or (org-get-x-clipboard 'PRIMARY)
316 (org-get-x-clipboard 'CLIPBOARD)
317 (org-get-x-clipboard 'SECONDARY)))
318 (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
319 (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
320 (v-u (concat "[" (substring v-t 1 -1) "]"))
321 (v-U (concat "[" (substring v-T 1 -1) "]"))
322 ;; `initial' and `annotation' are bound in `remember'
323 (v-i (if (boundp 'initial) initial))
324 (v-a (if (and (boundp 'annotation) annotation)
325 (if (equal annotation "[[]]") "" annotation)
326 ""))
327 (clipboards (remove nil (list v-i
328 (org-get-x-clipboard 'PRIMARY)
329 (org-get-x-clipboard 'CLIPBOARD)
330 (org-get-x-clipboard 'SECONDARY)
331 v-c)))
332 (v-A (if (and v-a
333 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
334 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
335 v-a))
336 (v-n user-full-name)
337 (org-startup-folded nil)
338 org-time-was-given org-end-time-was-given x
339 prompt completions char time pos default histvar)
340 (when (and file (not (file-name-absolute-p file)))
341 (setq file (expand-file-name file org-directory)))
342 (setq org-store-link-plist
343 (append (list :annotation v-a :initial v-i)
344 org-store-link-plist))
345 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
346 (erase-buffer)
347 (insert (substitute-command-keys
348 (format
349 "## Filing location: Select interactively, default, or last used:
350 ## %s to select file and header location interactively.
351 ## %s \"%s\" -> \"* %s\"
352 ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
353 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
354 (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
355 (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
356 (abbreviate-file-name (or file org-default-notes-file))
357 (or headline "")
358 (or (car org-remember-previous-location) "???")
359 (or (cdr org-remember-previous-location) "???"))))
360 (insert tpl) (goto-char (point-min))
361 ;; Simple %-escapes
362 (while (re-search-forward "%\\([tTuUaiAcx]\\)" nil t)
363 (when (and initial (equal (match-string 0) "%i"))
364 (save-match-data
365 (let* ((lead (buffer-substring
366 (point-at-bol) (match-beginning 0))))
367 (setq v-i (mapconcat 'identity
368 (org-split-string initial "\n")
369 (concat "\n" lead))))))
370 (replace-match
371 (or (eval (intern (concat "v-" (match-string 1)))) "")
372 t t))
374 ;; %[] Insert contents of a file.
375 (goto-char (point-min))
376 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
377 (let ((start (match-beginning 0))
378 (end (match-end 0))
379 (filename (expand-file-name (match-string 1))))
380 (goto-char start)
381 (delete-region start end)
382 (condition-case error
383 (insert-file-contents filename)
384 (error (insert (format "%%![Couldn't insert %s: %s]"
385 filename error))))))
386 ;; %() embedded elisp
387 (goto-char (point-min))
388 (while (re-search-forward "%\\((.+)\\)" nil t)
389 (goto-char (match-beginning 0))
390 (let ((template-start (point)))
391 (forward-char 1)
392 (let ((result
393 (condition-case error
394 (eval (read (current-buffer)))
395 (error (format "%%![Error: %s]" error)))))
396 (delete-region template-start (point))
397 (insert result))))
399 ;; From the property list
400 (when plist-p
401 (goto-char (point-min))
402 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
403 (and (setq x (or (plist-get org-store-link-plist
404 (intern (match-string 1))) ""))
405 (replace-match x t t))))
407 ;; Turn on org-mode in the remember buffer, set local variables
408 (org-mode)
409 (org-set-local 'org-finish-function 'org-remember-finalize)
410 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
411 (org-set-local 'org-default-notes-file file))
412 (if (and headline (stringp headline) (string-match "\\S-" headline))
413 (org-set-local 'org-remember-default-headline headline))
414 ;; Interactive template entries
415 (goto-char (point-min))
416 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtTCL]\\)?" nil t)
417 (setq char (if (match-end 3) (match-string 3))
418 prompt (if (match-end 2) (match-string 2)))
419 (goto-char (match-beginning 0))
420 (replace-match "")
421 (setq completions nil default nil)
422 (when prompt
423 (setq completions (org-split-string prompt "|")
424 prompt (pop completions)
425 default (car completions)
426 histvar (intern (concat
427 "org-remember-template-prompt-history::"
428 (or prompt "")))
429 completions (mapcar 'list completions)))
430 (cond
431 ((member char '("G" "g"))
432 (let* ((org-last-tags-completion-table
433 (org-global-tags-completion-table
434 (if (equal char "G") (org-agenda-files) (and file (list file)))))
435 (org-add-colon-after-tag-completion t)
436 (ins (completing-read
437 (if prompt (concat prompt ": ") "Tags: ")
438 'org-tags-completion-function nil nil nil
439 'org-tags-history)))
440 (setq ins (mapconcat 'identity
441 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
442 ":"))
443 (when (string-match "\\S-" ins)
444 (or (equal (char-before) ?:) (insert ":"))
445 (insert ins)
446 (or (equal (char-after) ?:) (insert ":")))))
447 ((equal char "C")
448 (cond ((= (length clipboards) 1) (insert (car clipboards)))
449 ((> (length clipboards) 1)
450 (insert (read-string "Clipboard/kill value: "
451 (car clipboards) '(clipboards . 1)
452 (car clipboards))))))
453 ((equal char "L")
454 (cond ((= (length clipboards) 1)
455 (org-insert-link 0 (car clipboards)))
456 ((> (length clipboards) 1)
457 (org-insert-link 0 (read-string "Clipboard/kill value: "
458 (car clipboards)
459 '(clipboards . 1)
460 (car clipboards))))))
461 (char
462 (setq org-time-was-given (equal (upcase char) char))
463 (setq time (org-read-date (equal (upcase char) "U") t nil
464 prompt))
465 (org-insert-time-stamp time org-time-was-given
466 (member char '("u" "U"))
467 nil nil (list org-end-time-was-given)))
469 (insert (org-completing-read
470 (concat (if prompt prompt "Enter string")
471 (if default (concat " [" default "]"))
472 ": ")
473 completions nil nil nil histvar default)))))
474 (goto-char (point-min))
475 (if (re-search-forward "%\\?" nil t)
476 (replace-match "")
477 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
478 (org-mode)
479 (org-set-local 'org-finish-function 'org-remember-finalize))
480 (when (save-excursion
481 (goto-char (point-min))
482 (re-search-forward "%!" nil t))
483 (replace-match "")
484 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
486 (defun org-remember-finish-immediately ()
487 "File remember note immediately.
488 This should be run in `post-command-hook' and will remove itself
489 from that hook."
490 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
491 (when org-finish-function
492 (funcall org-finish-function)))
494 (defvar org-clock-marker) ; Defined in org.el
495 (defun org-remember-finalize ()
496 "Finalize the remember process."
497 (unless (fboundp 'remember-finalize)
498 (defalias 'remember-finalize 'remember-buffer))
499 (when (and org-clock-marker
500 (equal (marker-buffer org-clock-marker) (current-buffer)))
501 ;; the clock is running in this buffer.
502 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
503 (or (eq org-remember-clock-out-on-exit t)
504 (and org-remember-clock-out-on-exit
505 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
506 (let (org-log-note-clock-out) (org-clock-out))))
507 (when buffer-file-name
508 (save-buffer)
509 (setq buffer-file-name nil))
510 (remember-finalize))
512 ;;;###autoload
513 (defun org-remember (&optional goto org-force-remember-template-char)
514 "Call `remember'. If this is already a remember buffer, re-apply template.
515 If there is an active region, make sure remember uses it as initial content
516 of the remember buffer.
518 When called interactively with a `C-u' prefix argument GOTO, don't remember
519 anything, just go to the file/headline where the selected template usually
520 stores its notes. With a double prefix arg `C-u C-u', go to the last
521 note stored by remember.
523 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
524 associated with a template in `org-remember-templates'."
525 (interactive "P")
526 (cond
527 ((equal goto '(4)) (org-go-to-remember-target))
528 ((equal goto '(16)) (org-remember-goto-last-stored))
530 ;; set temporary variables that will be needed in
531 ;; `org-select-remember-template'
532 (setq org-select-template-temp-major-mode major-mode)
533 (setq org-select-template-original-buffer (current-buffer))
534 (if (eq org-finish-function 'org-remember-finalize)
535 (progn
536 (when (< (length org-remember-templates) 2)
537 (error "No other template available"))
538 (erase-buffer)
539 (let ((annotation (plist-get org-store-link-plist :annotation))
540 (initial (plist-get org-store-link-plist :initial)))
541 (org-remember-apply-template))
542 (message "Press C-c C-c to remember data"))
543 (if (org-region-active-p)
544 (org-do-remember (buffer-substring (point) (mark)))
545 (org-do-remember))))))
547 (defun org-remember-goto-last-stored ()
548 "Go to the location where the last remember note was stored."
549 (interactive)
550 (bookmark-jump "org-remember-last-stored")
551 (message "This is the last note stored by remember"))
553 (defun org-go-to-remember-target (&optional template-key)
554 "Go to the target location of a remember template.
555 The user is queried for the template."
556 (interactive)
557 (let* (org-select-template-temp-major-mode
558 (entry (org-select-remember-template template-key))
559 (file (nth 1 entry))
560 (heading (nth 2 entry))
561 visiting)
562 (unless (and file (stringp file) (string-match "\\S-" file))
563 (setq file org-default-notes-file))
564 (when (and file (not (file-name-absolute-p file)))
565 (setq file (expand-file-name file org-directory)))
566 (unless (and heading (stringp heading) (string-match "\\S-" heading))
567 (setq heading org-remember-default-headline))
568 (setq visiting (org-find-base-buffer-visiting file))
569 (if (not visiting) (find-file-noselect file))
570 (switch-to-buffer (or visiting (get-file-buffer file)))
571 (widen)
572 (goto-char (point-min))
573 (if (re-search-forward
574 (concat "^\\*+[ \t]+" (regexp-quote heading)
575 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
576 nil t)
577 (goto-char (match-beginning 0))
578 (error "Target headline not found: %s" heading))))
580 ;;;###autoload
581 (defun org-remember-handler ()
582 "Store stuff from remember.el into an org file.
583 First prompts for an org file. If the user just presses return, the value
584 of `org-default-notes-file' is used.
585 Then the command offers the headings tree of the selected file in order to
586 file the text at a specific location.
587 You can either immediately press RET to get the note appended to the
588 file, or you can use vertical cursor motion and visibility cycling (TAB) to
589 find a better place. Then press RET or <left> or <right> in insert the note.
591 Key Cursor position Note gets inserted
592 -----------------------------------------------------------------------------
593 RET buffer-start as level 1 heading at end of file
594 RET on headline as sublevel of the heading at cursor
595 RET no heading at cursor position, level taken from context.
596 Or use prefix arg to specify level manually.
597 <left> on headline as same level, before current heading
598 <right> on headline as same level, after current heading
600 So the fastest way to store the note is to press RET RET to append it to
601 the default file. This way your current train of thought is not
602 interrupted, in accordance with the principles of remember.el.
603 You can also get the fast execution without prompting by using
604 C-u C-c C-c to exit the remember buffer. See also the variable
605 `org-remember-store-without-prompt'.
607 Before being stored away, the function ensures that the text has a
608 headline, i.e. a first line that starts with a \"*\". If not, a headline
609 is constructed from the current date and some additional data.
611 If the variable `org-adapt-indentation' is non-nil, the entire text is
612 also indented so that it starts in the same column as the headline
613 \(i.e. after the stars).
615 See also the variable `org-reverse-note-order'."
616 (goto-char (point-min))
617 (while (looking-at "^[ \t]*\n\\|^##.*\n")
618 (replace-match ""))
619 (goto-char (point-max))
620 (beginning-of-line 1)
621 (while (looking-at "[ \t]*$\\|##.*")
622 (delete-region (1- (point)) (point-max))
623 (beginning-of-line 1))
624 (catch 'quit
625 (if org-note-abort (throw 'quit nil))
626 (let* ((fastp (org-xor (equal current-prefix-arg '(4))
627 org-remember-store-without-prompt))
628 (file (cond
629 (fastp org-default-notes-file)
630 ((and (eq org-remember-interactive-interface 'refile)
631 org-refile-targets)
632 org-default-notes-file)
633 ((not (and (equal current-prefix-arg '(16))
634 org-remember-previous-location))
635 (org-get-org-file))))
636 (heading org-remember-default-headline)
637 (visiting (and file (org-find-base-buffer-visiting file)))
638 (org-startup-folded nil)
639 (org-startup-align-all-tables nil)
640 (org-goto-start-pos 1)
641 spos exitcmd level reversed txt)
642 (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
643 (setq file (car org-remember-previous-location)
644 heading (cdr org-remember-previous-location)
645 fastp t))
646 (setq current-prefix-arg nil)
647 ;; Modify text so that it becomes a nice subtree which can be inserted
648 ;; into an org tree.
649 (goto-char (point-min))
650 (if (re-search-forward "[ \t\n]+\\'" nil t)
651 ;; remove empty lines at end
652 (replace-match ""))
653 (goto-char (point-min))
654 (unless (looking-at org-outline-regexp)
655 ;; add a headline
656 (insert (concat "* " (current-time-string)
657 " (" (remember-buffer-desc) ")\n"))
658 (backward-char 1)
659 (when org-adapt-indentation
660 (while (re-search-forward "^" nil t)
661 (insert " "))))
662 (goto-char (point-min))
663 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
664 (replace-match "\n\n")
665 (if (re-search-forward "[ \t\n]*\\'")
666 (replace-match "\n")))
667 (goto-char (point-min))
668 (setq txt (buffer-string))
669 (org-save-markers-in-region (point-min) (point-max))
670 (when (and (eq org-remember-interactive-interface 'refile)
671 (not fastp))
672 (org-refile nil (or visiting (find-file-noselect file)))
673 (throw 'quit t))
674 ;; Find the file
675 (if (not visiting) (find-file-noselect file))
676 (with-current-buffer (or visiting (get-file-buffer file))
677 (unless (org-mode-p)
678 (error "Target files for remember notes must be in Org-mode"))
679 (save-excursion
680 (save-restriction
681 (widen)
682 (and (goto-char (point-min))
683 (not (re-search-forward "^\\* " nil t))
684 (insert "\n* " (or heading "Notes") "\n"))
685 (setq reversed (org-notes-order-reversed-p))
687 ;; Find the default location
688 (when (and heading (stringp heading) (string-match "\\S-" heading))
689 (goto-char (point-min))
690 (if (re-search-forward
691 (concat "^\\*+[ \t]+" (regexp-quote heading)
692 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
693 nil t)
694 (setq org-goto-start-pos (match-beginning 0))
695 (when fastp
696 (goto-char (point-max))
697 (unless (bolp) (newline))
698 (insert "* " heading "\n")
699 (setq org-goto-start-pos (point-at-bol 0)))))
701 ;; Ask the User for a location, using the appropriate interface
702 (cond
703 (fastp (setq spos org-goto-start-pos
704 exitcmd 'return))
705 ((eq org-remember-interactive-interface 'outline)
706 (setq spos (org-get-location (current-buffer)
707 org-remember-help)
708 exitcmd (cdr spos)
709 spos (car spos)))
710 ((eq org-remember-interactive-interface 'outline-path-completion)
711 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
712 (org-refile-use-outline-path t))
713 (setq spos (org-refile-get-location "Heading: ")
714 exitcmd 'return
715 spos (nth 3 spos))))
716 (t (error "This should not happen")))
717 (if (not spos) (throw 'quit nil)) ; return nil to show we did
718 ; not handle this note
719 (goto-char spos)
720 (cond ((org-on-heading-p t)
721 (org-back-to-heading t)
722 (setq level (funcall outline-level))
723 (cond
724 ((eq exitcmd 'return)
725 ;; sublevel of current
726 (setq org-remember-previous-location
727 (cons (abbreviate-file-name file)
728 (org-get-heading 'notags)))
729 (if reversed
730 (outline-next-heading)
731 (org-end-of-subtree t)
732 (if (not (bolp))
733 (if (looking-at "[ \t]*\n")
734 (beginning-of-line 2)
735 (end-of-line 1)
736 (insert "\n"))))
737 (bookmark-set "org-remember-last-stored")
738 (org-paste-subtree (org-get-valid-level level 1) txt))
739 ((eq exitcmd 'left)
740 ;; before current
741 (bookmark-set "org-remember-last-stored")
742 (org-paste-subtree level txt))
743 ((eq exitcmd 'right)
744 ;; after current
745 (org-end-of-subtree t)
746 (bookmark-set "org-remember-last-stored")
747 (org-paste-subtree level txt))
748 (t (error "This should not happen"))))
750 ((and (bobp) (not reversed))
751 ;; Put it at the end, one level below level 1
752 (save-restriction
753 (widen)
754 (goto-char (point-max))
755 (if (not (bolp)) (newline))
756 (bookmark-set "org-remember-last-stored")
757 (org-paste-subtree (org-get-valid-level 1 1) txt)))
759 ((and (bobp) reversed)
760 ;; Put it at the start, as level 1
761 (save-restriction
762 (widen)
763 (goto-char (point-min))
764 (re-search-forward "^\\*+ " nil t)
765 (beginning-of-line 1)
766 (bookmark-set "org-remember-last-stored")
767 (org-paste-subtree 1 txt)))
769 ;; Put it right there, with automatic level determined by
770 ;; org-paste-subtree or from prefix arg
771 (bookmark-set "org-remember-last-stored")
772 (org-paste-subtree
773 (if (numberp current-prefix-arg) current-prefix-arg)
774 txt)))
775 (when remember-save-after-remembering
776 (save-buffer)
777 (if (and (not visiting)
778 (not (equal (marker-buffer org-clock-marker)
779 (current-buffer))))
780 (kill-buffer (current-buffer)))))))))
782 t) ;; return t to indicate that we took care of this note.
785 (defun org-do-remember (&optional initial)
786 "Call remember."
787 (remember initial))
789 (provide 'org-remember)
791 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
793 ;;; org-remember.el ends here