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