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