Release 6.07a.
[org-mode.git] / lisp / org-remember.el
blobe0561239be5585093772bd62bb11f3ab612d57e4
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.07a
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 '(nil)))
168 ((= (length x) 3) (append x '(nil nil)))
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 (dct (decode-time ct))
321 (ct1
322 (if (< (nth 2 dct) org-extend-today-until)
323 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
324 ct))
325 (tpl (car entry))
326 (plist-p (if org-store-link-plist t nil))
327 (file (if (and (nth 1 entry)
328 (or (and (stringp (nth 1 entry))
329 (string-match "\\S-" (nth 1 entry)))
330 (functionp (nth 1 entry))))
331 (nth 1 entry)
332 org-default-notes-file))
333 (headline (nth 2 entry))
334 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
335 (v-x (or (org-get-x-clipboard 'PRIMARY)
336 (org-get-x-clipboard 'CLIPBOARD)
337 (org-get-x-clipboard 'SECONDARY)))
338 (v-t (format-time-string (car org-time-stamp-formats) ct))
339 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
340 (v-u (concat "[" (substring v-t 1 -1) "]"))
341 (v-U (concat "[" (substring v-T 1 -1) "]"))
342 ;; `initial' and `annotation' are bound in `remember'
343 (v-i (if (boundp 'initial) initial))
344 (v-a (if (and (boundp 'annotation) annotation)
345 (if (equal annotation "[[]]") "" annotation)
346 ""))
347 (clipboards (remove nil (list v-i
348 (org-get-x-clipboard 'PRIMARY)
349 (org-get-x-clipboard 'CLIPBOARD)
350 (org-get-x-clipboard 'SECONDARY)
351 v-c)))
352 (v-A (if (and v-a
353 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
354 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
355 v-a))
356 (v-n user-full-name)
357 (org-startup-folded nil)
358 (org-inhibit-startup t)
359 org-time-was-given org-end-time-was-given x
360 prompt completions char time pos default histvar)
362 (when (functionp file)
363 (setq file (funcall file)))
364 (when (and file (not (file-name-absolute-p file)))
365 (setq file (expand-file-name file org-directory)))
366 (setq org-store-link-plist
367 (append (list :annotation v-a :initial v-i)
368 org-store-link-plist))
369 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
370 (erase-buffer)
371 (insert (substitute-command-keys
372 (format
373 "## Filing location: Select interactively, default, or last used:
374 ## %s to select file and header location interactively.
375 ## %s \"%s\" -> \"* %s\"
376 ## C-0 C-c C-c \"%s\" -> \"* %s\"
377 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
378 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
379 (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c")
380 (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
381 (abbreviate-file-name (or file org-default-notes-file))
382 (or headline "")
383 (or (car org-remember-previous-location) "???")
384 (or (cdr org-remember-previous-location) "???"))))
385 (insert tpl) (goto-char (point-min))
386 ;; Simple %-escapes
387 (while (re-search-forward "%\\([tTuUaiAcx]\\)" nil t)
388 (when (and initial (equal (match-string 0) "%i"))
389 (save-match-data
390 (let* ((lead (buffer-substring
391 (point-at-bol) (match-beginning 0))))
392 (setq v-i (mapconcat 'identity
393 (org-split-string initial "\n")
394 (concat "\n" lead))))))
395 (replace-match
396 (or (eval (intern (concat "v-" (match-string 1)))) "")
397 t t))
399 ;; %[] Insert contents of a file.
400 (goto-char (point-min))
401 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
402 (let ((start (match-beginning 0))
403 (end (match-end 0))
404 (filename (expand-file-name (match-string 1))))
405 (goto-char start)
406 (delete-region start end)
407 (condition-case error
408 (insert-file-contents filename)
409 (error (insert (format "%%![Couldn't insert %s: %s]"
410 filename error))))))
411 ;; %() embedded elisp
412 (goto-char (point-min))
413 (while (re-search-forward "%\\((.+)\\)" nil t)
414 (goto-char (match-beginning 0))
415 (let ((template-start (point)))
416 (forward-char 1)
417 (let ((result
418 (condition-case error
419 (eval (read (current-buffer)))
420 (error (format "%%![Error: %s]" error)))))
421 (delete-region template-start (point))
422 (insert result))))
424 ;; From the property list
425 (when plist-p
426 (goto-char (point-min))
427 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
428 (and (setq x (or (plist-get org-store-link-plist
429 (intern (match-string 1))) ""))
430 (replace-match x t t))))
432 ;; Turn on org-mode in the remember buffer, set local variables
433 (let ((org-inhibit-startup t)) (org-mode))
434 (org-set-local 'org-finish-function 'org-remember-finalize)
435 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
436 (org-set-local 'org-default-notes-file file))
437 (if headline
438 (org-set-local 'org-remember-default-headline headline))
439 ;; Interactive template entries
440 (goto-char (point-min))
441 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCL]\\)?" nil t)
442 (setq char (if (match-end 3) (match-string 3))
443 prompt (if (match-end 2) (match-string 2)))
444 (goto-char (match-beginning 0))
445 (replace-match "")
446 (setq completions nil default nil)
447 (when prompt
448 (setq completions (org-split-string prompt "|")
449 prompt (pop completions)
450 default (car completions)
451 histvar (intern (concat
452 "org-remember-template-prompt-history::"
453 (or prompt "")))
454 completions (mapcar 'list completions)))
455 (cond
456 ((member char '("G" "g"))
457 (let* ((org-last-tags-completion-table
458 (org-global-tags-completion-table
459 (if (equal char "G") (org-agenda-files) (and file (list file)))))
460 (org-add-colon-after-tag-completion t)
461 (ins (completing-read
462 (if prompt (concat prompt ": ") "Tags: ")
463 'org-tags-completion-function nil nil nil
464 'org-tags-history)))
465 (setq ins (mapconcat 'identity
466 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
467 ":"))
468 (when (string-match "\\S-" ins)
469 (or (equal (char-before) ?:) (insert ":"))
470 (insert ins)
471 (or (equal (char-after) ?:) (insert ":")))))
472 ((equal char "C")
473 (cond ((= (length clipboards) 1) (insert (car clipboards)))
474 ((> (length clipboards) 1)
475 (insert (read-string "Clipboard/kill value: "
476 (car clipboards) '(clipboards . 1)
477 (car clipboards))))))
478 ((equal char "L")
479 (cond ((= (length clipboards) 1)
480 (org-insert-link 0 (car clipboards)))
481 ((> (length clipboards) 1)
482 (org-insert-link 0 (read-string "Clipboard/kill value: "
483 (car clipboards)
484 '(clipboards . 1)
485 (car clipboards))))))
486 (char
487 ;; These are the date/time related ones
488 (setq org-time-was-given (equal (upcase char) char))
489 (setq time (org-read-date (equal (upcase char) "U") t nil
490 prompt))
491 (org-insert-time-stamp time org-time-was-given
492 (member char '("u" "U"))
493 nil nil (list org-end-time-was-given)))
495 (insert (org-completing-read
496 (concat (if prompt prompt "Enter string")
497 (if default (concat " [" default "]"))
498 ": ")
499 completions nil nil nil histvar default)))))
500 (goto-char (point-min))
501 (if (re-search-forward "%\\?" nil t)
502 (replace-match "")
503 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
504 (let ((org-inhibit-startup t)) (org-mode))
505 (org-set-local 'org-finish-function 'org-remember-finalize))
506 (when (save-excursion
507 (goto-char (point-min))
508 (re-search-forward "%&" nil t))
509 (replace-match "")
510 (org-set-local 'org-jump-to-target-location t))
511 (when (save-excursion
512 (goto-char (point-min))
513 (re-search-forward "%!" nil t))
514 (replace-match "")
515 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
517 (defun org-remember-finish-immediately ()
518 "File remember note immediately.
519 This should be run in `post-command-hook' and will remove itself
520 from that hook."
521 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
522 (when org-finish-function
523 (funcall org-finish-function)))
525 (defun org-remember-visit-immediately ()
526 "File remember note immediately.
527 This should be run in `post-command-hook' and will remove itself
528 from that hook."
529 (org-remember '(16))
530 (goto-char (or (text-property-any
531 (point) (save-excursion (org-end-of-subtree t t))
532 'org-position-cursor t)
533 (point)))
534 (message "%s"
535 (format
536 (substitute-command-keys
537 "Restore window configuration with \\[jump-to-register] %c")
538 remember-register)))
540 (defvar org-clock-marker) ; Defined in org.el
541 (defun org-remember-finalize ()
542 "Finalize the remember process."
543 (unless (fboundp 'remember-finalize)
544 (defalias 'remember-finalize 'remember-buffer))
545 (when (and org-clock-marker
546 (equal (marker-buffer org-clock-marker) (current-buffer)))
547 ;; the clock is running in this buffer.
548 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
549 (or (eq org-remember-clock-out-on-exit t)
550 (and org-remember-clock-out-on-exit
551 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
552 (let (org-log-note-clock-out) (org-clock-out))))
553 (when buffer-file-name
554 (save-buffer)
555 (setq buffer-file-name nil))
556 (remember-finalize))
558 ;;;###autoload
559 (defun org-remember (&optional goto org-force-remember-template-char)
560 "Call `remember'. If this is already a remember buffer, re-apply template.
561 If there is an active region, make sure remember uses it as initial content
562 of the remember buffer.
564 When called interactively with a `C-u' prefix argument GOTO, don't remember
565 anything, just go to the file/headline where the selected template usually
566 stores its notes. With a double prefix arg `C-u C-u', go to the last
567 note stored by remember.
569 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
570 associated with a template in `org-remember-templates'."
571 (interactive "P")
572 (cond
573 ((equal goto '(4)) (org-go-to-remember-target))
574 ((equal goto '(16)) (org-remember-goto-last-stored))
576 ;; set temporary variables that will be needed in
577 ;; `org-select-remember-template'
578 (setq org-select-template-temp-major-mode major-mode)
579 (setq org-select-template-original-buffer (current-buffer))
580 (if (eq org-finish-function 'org-remember-finalize)
581 (progn
582 (when (< (length org-remember-templates) 2)
583 (error "No other template available"))
584 (erase-buffer)
585 (let ((annotation (plist-get org-store-link-plist :annotation))
586 (initial (plist-get org-store-link-plist :initial)))
587 (org-remember-apply-template))
588 (message "Press C-c C-c to remember data"))
589 (if (org-region-active-p)
590 (org-do-remember (buffer-substring (point) (mark)))
591 (org-do-remember))))))
593 (defvar org-remember-last-stored-marker (make-marker)
594 "Marker pointing to the entry most recently stored with `org-remember'.")
596 (defun org-remember-goto-last-stored ()
597 "Go to the location where the last remember note was stored."
598 (interactive)
599 (org-goto-marker-or-bmk org-remember-last-stored-marker
600 "org-remember-last-stored")
601 (message "This is the last note stored by remember"))
603 (defun org-go-to-remember-target (&optional template-key)
604 "Go to the target location of a remember template.
605 The user is queried for the template."
606 (interactive)
607 (let* (org-select-template-temp-major-mode
608 (entry (org-select-remember-template template-key))
609 (file (nth 1 entry))
610 (heading (nth 2 entry))
611 visiting)
612 (unless (and file (stringp file) (string-match "\\S-" file))
613 (setq file org-default-notes-file))
614 (when (and file (not (file-name-absolute-p file)))
615 (setq file (expand-file-name file org-directory)))
616 (unless (and heading (stringp heading) (string-match "\\S-" heading))
617 (setq heading org-remember-default-headline))
618 (setq visiting (org-find-base-buffer-visiting file))
619 (if (not visiting) (find-file-noselect file))
620 (switch-to-buffer (or visiting (get-file-buffer file)))
621 (widen)
622 (goto-char (point-min))
623 (if (re-search-forward
624 (concat "^\\*+[ \t]+" (regexp-quote heading)
625 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
626 nil t)
627 (goto-char (match-beginning 0))
628 (error "Target headline not found: %s" heading))))
630 ;;;###autoload
631 (defun org-remember-handler ()
632 "Store stuff from remember.el into an org file.
633 When the template has specified a file and a headline, the entry is filed
634 there, or in the location defined by `org-default-notes-file' and
635 `org-remember-default-headline'.
637 If no defaults have been defined, or if the current prefix argument
638 is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
639 process is used to select the target location.
641 When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
642 the entry is filed to the same location as the previous note.
644 When `C-u' has been used as prefix argument, the note is stored and emacs
645 moves point to the new location of the note, so that editing can be
646 continued there (smilar to inserting \"%&\" into the tempate).
648 Before storing the note, the function ensures that the text has an
649 org-mode-style headline, i.e. a first line that starts with
650 a \"*\". If not, a headline is constructed from the current date and
651 some additional data.
653 If the variable `org-adapt-indentation' is non-nil, the entire text is
654 also indented so that it starts in the same column as the headline
655 \(i.e. after the stars).
657 See also the variable `org-reverse-note-order'."
658 (when (org-bound-and-true-p org-jump-to-target-location)
659 (let* ((end (min (point-max) (1+ (point))))
660 (beg (point)))
661 (if (= end beg) (setq beg (1- beg)))
662 (put-text-property beg end 'org-position-cursor t)))
663 (goto-char (point-min))
664 (while (looking-at "^[ \t]*\n\\|^##.*\n")
665 (replace-match ""))
666 (goto-char (point-max))
667 (beginning-of-line 1)
668 (while (looking-at "[ \t]*$\\|##.*")
669 (delete-region (1- (point)) (point-max))
670 (beginning-of-line 1))
671 (catch 'quit
672 (if org-note-abort (throw 'quit nil))
673 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
674 (previousp (and (member current-prefix-arg '((16) 0))
675 org-remember-previous-location))
676 (fastp (org-xor (equal current-prefix-arg 1)
677 org-remember-store-without-prompt))
678 (file (cond
679 (fastp org-default-notes-file)
680 ((and (eq org-remember-interactive-interface 'refile)
681 org-refile-targets)
682 org-default-notes-file)
683 ((not previousp)
684 (org-get-org-file))))
685 (heading org-remember-default-headline)
686 (visiting (and file (org-find-base-buffer-visiting file)))
687 (org-startup-folded nil)
688 (org-startup-align-all-tables nil)
689 (org-goto-start-pos 1)
690 spos exitcmd level reversed txt)
691 (when (equal current-prefix-arg '(4))
692 (setq visitp t))
693 (when previousp
694 (setq file (car org-remember-previous-location)
695 heading (cdr org-remember-previous-location)
696 fastp t))
697 (setq current-prefix-arg nil)
698 ;; Modify text so that it becomes a nice subtree which can be inserted
699 ;; into an org tree.
700 (goto-char (point-min))
701 (if (re-search-forward "[ \t\n]+\\'" nil t)
702 ;; remove empty lines at end
703 (replace-match ""))
704 (goto-char (point-min))
705 (unless (looking-at org-outline-regexp)
706 ;; add a headline
707 (insert (concat "* " (current-time-string)
708 " (" (remember-buffer-desc) ")\n"))
709 (backward-char 1)
710 (when org-adapt-indentation
711 (while (re-search-forward "^" nil t)
712 (insert " "))))
713 (goto-char (point-min))
714 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
715 (replace-match "\n\n")
716 (if (re-search-forward "[ \t\n]*\\'")
717 (replace-match "\n")))
718 (goto-char (point-min))
719 (setq txt (buffer-string))
720 (org-save-markers-in-region (point-min) (point-max))
721 (when (and (eq org-remember-interactive-interface 'refile)
722 (not fastp))
723 (org-refile nil (or visiting (find-file-noselect file)))
724 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
725 (throw 'quit t))
726 ;; Find the file
727 (if (not visiting) (find-file-noselect file))
728 (with-current-buffer (or visiting (get-file-buffer file))
729 (unless (org-mode-p)
730 (error "Target files for remember notes must be in Org-mode"))
731 (save-excursion
732 (save-restriction
733 (widen)
734 (and (goto-char (point-min))
735 (not (re-search-forward "^\\* " nil t))
736 (insert "\n* " (or (and (stringp heading) heading)
737 "Notes") "\n"))
738 (setq reversed (org-notes-order-reversed-p))
740 ;; Find the default location
741 (when heading
742 (cond
743 ((eq heading 'top)
744 (goto-char (point-min))
745 (or (looking-at org-outline-regexp)
746 (re-search-forward org-outline-regexp nil t))
747 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
748 ((eq heading 'bottom)
749 (goto-char (point-max))
750 (re-search-backward "^\\* " nil t)
751 (or (bolp) (newline))
752 (setq org-goto-start-pos (point)))
753 ((and (stringp heading) (string-match "\\S-" heading))
754 (goto-char (point-min))
755 (if (re-search-forward
756 (concat "^\\*+[ \t]+" (regexp-quote heading)
757 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
758 nil t)
759 (setq org-goto-start-pos (match-beginning 0))
760 (when fastp
761 (goto-char (point-max))
762 (unless (bolp) (newline))
763 (insert "* " heading "\n")
764 (setq org-goto-start-pos (point-at-bol 0)))))
765 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
766 heading 'top))))
768 ;; Ask the User for a location, using the appropriate interface
769 (cond
770 ((and fastp (memq heading '(top bottom)))
771 (setq spos org-goto-start-pos
772 exitcmd (if (eq heading 'top) 'left 'right)))
773 (fastp (setq spos org-goto-start-pos
774 exitcmd 'return))
775 ((eq org-remember-interactive-interface 'outline)
776 (setq spos (org-get-location (current-buffer)
777 org-remember-help)
778 exitcmd (cdr spos)
779 spos (car spos)))
780 ((eq org-remember-interactive-interface 'outline-path-completion)
781 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
782 (org-refile-use-outline-path t))
783 (setq spos (org-refile-get-location "Heading: ")
784 exitcmd 'return
785 spos (nth 3 spos))))
786 (t (error "This should not happen")))
787 (if (not spos) (throw 'quit nil)) ; return nil to show we did
788 ; not handle this note
789 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
790 (goto-char spos)
791 (cond ((org-on-heading-p t)
792 (org-back-to-heading t)
793 (setq level (funcall outline-level))
794 (cond
795 ((eq exitcmd 'return)
796 ;; sublevel of current
797 (setq org-remember-previous-location
798 (cons (abbreviate-file-name file)
799 (org-get-heading 'notags)))
800 (if reversed
801 (outline-next-heading)
802 (org-end-of-subtree t)
803 (if (not (bolp))
804 (if (looking-at "[ \t]*\n")
805 (beginning-of-line 2)
806 (end-of-line 1)
807 (insert "\n"))))
808 (org-paste-subtree (org-get-valid-level level 1) 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 'left)
813 ;; before current
814 (org-paste-subtree level txt)
815 (and org-auto-align-tags (org-set-tags nil t))
816 (bookmark-set "org-remember-last-stored")
817 (move-marker org-remember-last-stored-marker (point)))
818 ((eq exitcmd 'right)
819 ;; after current
820 (org-end-of-subtree t)
821 (org-paste-subtree level txt)
822 (and org-auto-align-tags (org-set-tags nil t))
823 (bookmark-set "org-remember-last-stored")
824 (move-marker org-remember-last-stored-marker (point)))
825 (t (error "This should not happen"))))
827 ((and (bobp) (not reversed))
828 ;; Put it at the end, one level below level 1
829 (save-restriction
830 (widen)
831 (goto-char (point-max))
832 (if (not (bolp)) (newline))
833 (org-paste-subtree (org-get-valid-level 1 1) txt)
834 (and org-auto-align-tags (org-set-tags nil t))
835 (bookmark-set "org-remember-last-stored")
836 (move-marker org-remember-last-stored-marker (point))))
838 ((and (bobp) reversed)
839 ;; Put it at the start, as level 1
840 (save-restriction
841 (widen)
842 (goto-char (point-min))
843 (re-search-forward "^\\*+ " nil t)
844 (beginning-of-line 1)
845 (org-paste-subtree 1 txt)
846 (and org-auto-align-tags (org-set-tags nil t))
847 (bookmark-set "org-remember-last-stored")
848 (move-marker org-remember-last-stored-marker (point))))
850 ;; Put it right there, with automatic level determined by
851 ;; org-paste-subtree or from prefix arg
852 (org-paste-subtree
853 (if (numberp current-prefix-arg) current-prefix-arg)
854 txt)
855 (and org-auto-align-tags (org-set-tags nil t))
856 (bookmark-set "org-remember-last-stored")
857 (move-marker org-remember-last-stored-marker (point))))
859 (when remember-save-after-remembering
860 (save-buffer)
861 (if (and (not visiting)
862 (not (equal (marker-buffer org-clock-marker)
863 (current-buffer))))
864 (kill-buffer (current-buffer)))))))))
866 t) ;; return t to indicate that we took care of this note.
869 (defun org-do-remember (&optional initial)
870 "Call remember."
871 (remember initial))
873 (provide 'org-remember)
875 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
877 ;;; org-remember.el ends here