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