Create publishing directory if it does not yet exist.
[org-mode.git] / lisp / org-remember.el
blobf488fc8ee1527b42620d2683a6b84c80913d1db7
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.03
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 (defun org-remember-goto-last-stored ()
546 "Go to the location where the last remember note was stored."
547 (interactive)
548 (bookmark-jump "org-remember-last-stored")
549 (message "This is the last note stored by remember"))
551 (defun org-go-to-remember-target (&optional template-key)
552 "Go to the target location of a remember template.
553 The user is queried for the template."
554 (interactive)
555 (let* (org-select-template-temp-major-mode
556 (entry (org-select-remember-template template-key))
557 (file (nth 1 entry))
558 (heading (nth 2 entry))
559 visiting)
560 (unless (and file (stringp file) (string-match "\\S-" file))
561 (setq file org-default-notes-file))
562 (when (and file (not (file-name-absolute-p file)))
563 (setq file (expand-file-name file org-directory)))
564 (unless (and heading (stringp heading) (string-match "\\S-" heading))
565 (setq heading org-remember-default-headline))
566 (setq visiting (org-find-base-buffer-visiting file))
567 (if (not visiting) (find-file-noselect file))
568 (switch-to-buffer (or visiting (get-file-buffer file)))
569 (widen)
570 (goto-char (point-min))
571 (if (re-search-forward
572 (concat "^\\*+[ \t]+" (regexp-quote heading)
573 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
574 nil t)
575 (goto-char (match-beginning 0))
576 (error "Target headline not found: %s" heading))))
578 ;;;###autoload
579 (defun org-remember-handler ()
580 "Store stuff from remember.el into an org file.
581 First prompts for an org file. If the user just presses return, the value
582 of `org-default-notes-file' is used.
583 Then the command offers the headings tree of the selected file in order to
584 file the text at a specific location.
585 You can either immediately press RET to get the note appended to the
586 file, or you can use vertical cursor motion and visibility cycling (TAB) to
587 find a better place. Then press RET or <left> or <right> in insert the note.
589 Key Cursor position Note gets inserted
590 -----------------------------------------------------------------------------
591 RET buffer-start as level 1 heading at end of file
592 RET on headline as sublevel of the heading at cursor
593 RET no heading at cursor position, level taken from context.
594 Or use prefix arg to specify level manually.
595 <left> on headline as same level, before current heading
596 <right> on headline as same level, after current heading
598 So the fastest way to store the note is to press RET RET to append it to
599 the default file. This way your current train of thought is not
600 interrupted, in accordance with the principles of remember.el.
601 You can also get the fast execution without prompting by using
602 C-u C-c C-c to exit the remember buffer. See also the variable
603 `org-remember-store-without-prompt'.
605 Before being stored away, the function ensures that the text has a
606 headline, i.e. a first line that starts with a \"*\". If not, a headline
607 is constructed from the current date and some additional data.
609 If the variable `org-adapt-indentation' is non-nil, the entire text is
610 also indented so that it starts in the same column as the headline
611 \(i.e. after the stars).
613 See also the variable `org-reverse-note-order'."
614 (goto-char (point-min))
615 (while (looking-at "^[ \t]*\n\\|^##.*\n")
616 (replace-match ""))
617 (goto-char (point-max))
618 (beginning-of-line 1)
619 (while (looking-at "[ \t]*$\\|##.*")
620 (delete-region (1- (point)) (point-max))
621 (beginning-of-line 1))
622 (catch 'quit
623 (if org-note-abort (throw 'quit nil))
624 (let* ((fastp (org-xor (equal current-prefix-arg '(4))
625 org-remember-store-without-prompt))
626 (file (cond
627 (fastp org-default-notes-file)
628 ((and (eq org-remember-interactive-interface 'refile)
629 org-refile-targets)
630 org-default-notes-file)
631 ((not (and (equal current-prefix-arg '(16))
632 org-remember-previous-location))
633 (org-get-org-file))))
634 (heading org-remember-default-headline)
635 (visiting (and file (org-find-base-buffer-visiting file)))
636 (org-startup-folded nil)
637 (org-startup-align-all-tables nil)
638 (org-goto-start-pos 1)
639 spos exitcmd level reversed txt)
640 (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
641 (setq file (car org-remember-previous-location)
642 heading (cdr org-remember-previous-location)
643 fastp t))
644 (setq current-prefix-arg nil)
645 ;; Modify text so that it becomes a nice subtree which can be inserted
646 ;; into an org tree.
647 (goto-char (point-min))
648 (if (re-search-forward "[ \t\n]+\\'" nil t)
649 ;; remove empty lines at end
650 (replace-match ""))
651 (goto-char (point-min))
652 (unless (looking-at org-outline-regexp)
653 ;; add a headline
654 (insert (concat "* " (current-time-string)
655 " (" (remember-buffer-desc) ")\n"))
656 (backward-char 1)
657 (when org-adapt-indentation
658 (while (re-search-forward "^" nil t)
659 (insert " "))))
660 (goto-char (point-min))
661 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
662 (replace-match "\n\n")
663 (if (re-search-forward "[ \t\n]*\\'")
664 (replace-match "\n")))
665 (goto-char (point-min))
666 (setq txt (buffer-string))
667 (org-save-markers-in-region (point-min) (point-max))
668 (when (and (eq org-remember-interactive-interface 'refile)
669 (not fastp))
670 (org-refile nil (or visiting (find-file-noselect file)))
671 (throw 'quit t))
672 ;; Find the file
673 (if (not visiting) (find-file-noselect file))
674 (with-current-buffer (or visiting (get-file-buffer file))
675 (unless (org-mode-p)
676 (error "Target files for remember notes must be in Org-mode"))
677 (save-excursion
678 (save-restriction
679 (widen)
680 (and (goto-char (point-min))
681 (not (re-search-forward "^\\* " nil t))
682 (insert "\n* " (or heading "Notes") "\n"))
683 (setq reversed (org-notes-order-reversed-p))
685 ;; Find the default location
686 (when (and heading (stringp heading) (string-match "\\S-" heading))
687 (goto-char (point-min))
688 (if (re-search-forward
689 (concat "^\\*+[ \t]+" (regexp-quote heading)
690 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
691 nil t)
692 (setq org-goto-start-pos (match-beginning 0))
693 (when fastp
694 (goto-char (point-max))
695 (unless (bolp) (newline))
696 (insert "* " heading "\n")
697 (setq org-goto-start-pos (point-at-bol 0)))))
699 ;; Ask the User for a location, using the appropriate interface
700 (cond
701 (fastp (setq spos org-goto-start-pos
702 exitcmd 'return))
703 ((eq org-remember-interactive-interface 'outline)
704 (setq spos (org-get-location (current-buffer)
705 org-remember-help)
706 exitcmd (cdr spos)
707 spos (car spos)))
708 ((eq org-remember-interactive-interface 'outline-path-completion)
709 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
710 (org-refile-use-outline-path t))
711 (setq spos (org-refile-get-location "Heading: ")
712 exitcmd 'return
713 spos (nth 3 spos))))
714 (t (error "This should not happen")))
715 (if (not spos) (throw 'quit nil)) ; return nil to show we did
716 ; not handle this note
717 (goto-char spos)
718 (cond ((org-on-heading-p t)
719 (org-back-to-heading t)
720 (setq level (funcall outline-level))
721 (cond
722 ((eq exitcmd 'return)
723 ;; sublevel of current
724 (setq org-remember-previous-location
725 (cons (abbreviate-file-name file)
726 (org-get-heading 'notags)))
727 (if reversed
728 (outline-next-heading)
729 (org-end-of-subtree t)
730 (if (not (bolp))
731 (if (looking-at "[ \t]*\n")
732 (beginning-of-line 2)
733 (end-of-line 1)
734 (insert "\n"))))
735 (bookmark-set "org-remember-last-stored")
736 (org-paste-subtree (org-get-valid-level level 1) txt))
737 ((eq exitcmd 'left)
738 ;; before current
739 (bookmark-set "org-remember-last-stored")
740 (org-paste-subtree level txt))
741 ((eq exitcmd 'right)
742 ;; after current
743 (org-end-of-subtree t)
744 (bookmark-set "org-remember-last-stored")
745 (org-paste-subtree level txt))
746 (t (error "This should not happen"))))
748 ((and (bobp) (not reversed))
749 ;; Put it at the end, one level below level 1
750 (save-restriction
751 (widen)
752 (goto-char (point-max))
753 (if (not (bolp)) (newline))
754 (bookmark-set "org-remember-last-stored")
755 (org-paste-subtree (org-get-valid-level 1 1) txt)))
757 ((and (bobp) reversed)
758 ;; Put it at the start, as level 1
759 (save-restriction
760 (widen)
761 (goto-char (point-min))
762 (re-search-forward "^\\*+ " nil t)
763 (beginning-of-line 1)
764 (bookmark-set "org-remember-last-stored")
765 (org-paste-subtree 1 txt)))
767 ;; Put it right there, with automatic level determined by
768 ;; org-paste-subtree or from prefix arg
769 (bookmark-set "org-remember-last-stored")
770 (org-paste-subtree
771 (if (numberp current-prefix-arg) current-prefix-arg)
772 txt)))
773 (when remember-save-after-remembering
774 (save-buffer)
775 (if (and (not visiting)
776 (not (equal (marker-buffer org-clock-marker)
777 (current-buffer))))
778 (kill-buffer (current-buffer)))))))))
780 t) ;; return t to indicate that we took care of this note.
783 (defun org-do-remember (&optional initial)
784 "Call remember."
785 (remember initial))
787 (provide 'org-remember)
789 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
791 ;;; org-remember.el ends here