Release 6.21a
[org-mode/org-tableheadings.git] / lisp / org-remember.el
blobfe7a87b007d7d41b9947e940280cc7b797e7644b
1 ;;; org-remember.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.21a
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the system to take fast notes with Org-mode.
30 ;; This system is used together with John Wiegley's `remember.el'.
32 ;;; Code:
34 (eval-when-compile
35 (require 'cl))
36 (require 'org)
38 (declare-function remember-mode "remember" ())
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-register)
44 (defvar remember-buffer)
45 (defvar remember-handler-functions)
46 (defvar remember-annotation-functions)
47 (defvar org-clock-heading)
48 (defvar org-clock-heading-for-remember)
50 (defgroup org-remember nil
51 "Options concerning interaction with remember.el."
52 :tag "Org Remember"
53 :group 'org)
55 (defcustom org-remember-store-without-prompt t
56 "Non-nil means, `C-c C-c' stores remember note without further prompts.
57 It then uses the file and headline specified by the template or (if the
58 template does not specify them) by the variables `org-default-notes-file'
59 and `org-remember-default-headline'. To force prompting anyway, use
60 `C-u C-c C-c' to file the note.
62 When this variable is nil, `C-c C-c' gives you the prompts, and
63 `C-u C-c C-c' triggers the fasttrack."
64 :group 'org-remember
65 :type 'boolean)
67 (defcustom org-remember-interactive-interface 'refile
68 "The interface to be used for interactive filing of remember notes.
69 This is only used when the interactive mode for selecting a filing
70 location is used (see the variable `org-remember-store-without-prompt').
71 Allowed values are:
72 outline The interface shows an outline of the relevant file
73 and the correct heading is found by moving through
74 the outline or by searching with incremental search.
75 outline-path-completion Headlines in the current buffer are offered via
76 completion.
77 refile Use the refile interface, and offer headlines,
78 possibly from different buffers."
79 :group 'org-remember
80 :type '(choice
81 (const :tag "Refile" refile)
82 (const :tag "Outline" outline)
83 (const :tag "Outline-path-completion" outline-path-completion)))
85 (defcustom org-remember-default-headline ""
86 "The headline that should be the default location in the notes file.
87 When filing remember notes, the cursor will start at that position.
88 You can set this on a per-template basis with the variable
89 `org-remember-templates'."
90 :group 'org-remember
91 :type 'string)
93 (defcustom org-remember-templates nil
94 "Templates for the creation of remember buffers.
95 When nil, just let remember make the buffer.
96 When non-nil, this is a list of 5-element lists. In each entry, the first
97 element is the name of the template, which should be a single short word.
98 The second element is a character, a unique key to select this template.
99 The third element is the template.
101 The fourth element is optional and can specify a destination file for
102 remember items created with this template. The default file is given
103 by `org-default-notes-file'. If the file name is not an absolute path,
104 it will be interpreted relative to `org-directory'.
106 An optional fifth element can specify the headline in that file that should
107 be offered first when the user is asked to file the entry. The default
108 headline is given in the variable `org-remember-default-headline'. When
109 this element is `top' or `bottom', the note will be placed as a level-1
110 entry at the beginning or end of the file, respectively.
112 An optional sixth element specifies the contexts in which the template
113 will be offered to the user. This element can be a list of major modes
114 or a function, and the template will only be offered if `org-remember'
115 is called from a mode in the list, or if the function returns t.
116 Templates that specify t or nil for the context will be always be added
117 to the list of selectable templates.
119 The template specifies the structure of the remember buffer. It should have
120 a first line starting with a star, to act as the org-mode headline.
121 Furthermore, the following %-escapes will be replaced with content:
123 %^{prompt} Prompt the user for a string and replace this sequence with it.
124 A default value and a completion table ca be specified like this:
125 %^{prompt|default|completion2|completion3|...}
126 %t time stamp, date only
127 %T time stamp with date and time
128 %u, %U like the above, but inactive time stamps
129 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
130 You may define a prompt like %^{Please specify birthday
131 %n user name (taken from `user-full-name')
132 %a annotation, normally the link created with org-store-link
133 %i initial content, the region active. If %i is indented,
134 the entire inserted text will be indented as well.
135 %c current kill ring head
136 %x content of the X clipboard
137 %^C Interactive selection of which kill or clip to use
138 %^L Like %^C, but insert as link
139 %k title of currently clocked task
140 %K link to currently clocked task
141 %^g prompt for tags, with completion on tags in target file
142 %^G prompt for tags, with completion all tags in all agenda files
143 %^{prop}p Prompt the user for a value for property `prop'
144 %:keyword specific information for certain link types, see below
145 %[pathname] insert the contents of the file given by `pathname'
146 %(sexp) evaluate elisp `(sexp)' and replace with the result
147 %! Store this note immediately after filling the template
148 %& Visit note immediately after storing it
150 %? After completing the template, position cursor here.
152 Apart from these general escapes, you can access information specific to the
153 link type that is created. For example, calling `remember' in emails or gnus
154 will record the author and the subject of the message, which you can access
155 with %:author and %:subject, respectively. Here is a complete list of what
156 is recorded for each link type.
158 Link type | Available information
159 -------------------+------------------------------------------------------
160 bbdb | %:type %:name %:company
161 vm, wl, mh, rmail | %:type %:subject %:message-id
162 | %:from %:fromname %:fromaddress
163 | %:to %:toname %:toaddress
164 | %:fromto (either \"to NAME\" or \"from NAME\")
165 gnus | %:group, for messages also all email fields
166 w3, w3m | %:type %:url
167 info | %:type %:file %:node
168 calendar | %:type %:date"
169 :group 'org-remember
170 :get (lambda (var) ; Make sure all entries have at least 5 elements
171 (mapcar (lambda (x)
172 (if (not (stringp (car x))) (setq x (cons "" x)))
173 (cond ((= (length x) 4) (append x '(nil)))
174 ((= (length x) 3) (append x '(nil nil)))
175 (t x)))
176 (default-value var)))
177 :type '(repeat
178 :tag "enabled"
179 (list :value ("" ?a "\n" nil nil nil)
180 (string :tag "Name")
181 (character :tag "Selection Key")
182 (string :tag "Template")
183 (choice :tag "Destination file"
184 (file :tag "Specify")
185 (function :tag "Function")
186 (const :tag "Use `org-default-notes-file'" nil))
187 (choice :tag "Destin. headline"
188 (string :tag "Specify")
189 (const :tag "Use `org-remember-default-headline'" nil)
190 (const :tag "Level 1 at beginning of file" top)
191 (const :tag "Level 1 at end of file" bottom))
192 (choice :tag "Context"
193 (const :tag "Use in all contexts" nil)
194 (const :tag "Use in all contexts" t)
195 (repeat :tag "Use only if in major mode"
196 (symbol :tag "Major mode"))
197 (function :tag "Perform a check against function")))))
199 (defcustom org-remember-before-finalize-hook nil
200 "Hook that is run right before a remember process is finalized.
201 The remember buffer is still current when this hook runs."
202 :group 'org-remember
203 :type 'hook)
205 (defvar org-remember-mode-map (make-sparse-keymap)
206 "Keymap for org-remember-mode, a minor mode.
207 Use this map to set additional keybindings for when Org-mode is used
208 for a Remember buffer.")
209 (defvar org-remember-mode-hook nil
210 "Hook for the minor `org-remember-mode'.")
212 (define-minor-mode org-remember-mode
213 "Minor mode for special key bindings in a remember buffer."
214 nil " Rem" org-remember-mode-map
215 (run-hooks 'org-remember-mode-hook))
216 (define-key org-remember-mode-map "\C-c\C-c" 'org-remember-finalize)
217 (define-key org-remember-mode-map "\C-c\C-k" 'org-remember-kill)
219 (defcustom org-remember-clock-out-on-exit 'query
220 "Non-nil means, stop the clock when exiting a clocking remember buffer.
221 This only applies if the clock is running in the remember buffer. If the
222 clock is not stopped, it continues to run in the storage location.
223 Instead of nil or t, this may also be the symbol `query' to prompt the
224 user each time a remember buffer with a running clock is filed away. "
225 :group 'org-remember
226 :type '(choice
227 (const :tag "Never" nil)
228 (const :tag "Always" t)
229 (const :tag "Query user" query)))
232 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
233 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
235 ;;;###autoload
236 (defun org-remember-insinuate ()
237 "Setup remember.el for use with Org-mode."
238 (org-require-remember)
239 (setq remember-annotation-functions '(org-remember-annotation))
240 (setq remember-handler-functions '(org-remember-handler))
241 (add-hook 'remember-mode-hook 'org-remember-apply-template))
243 ;;;###autoload
244 (defun org-remember-annotation ()
245 "Return a link to the current location as an annotation for remember.el.
246 If you are using Org-mode files as target for data storage with
247 remember.el, then the annotations should include a link compatible with the
248 conventions in Org-mode. This function returns such a link."
249 (org-store-link nil))
251 (defconst org-remember-help
252 "Select a destination location for the note.
253 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
254 RET on headline -> Store as sublevel entry to current headline
255 RET at beg-of-buf -> Append to file as level 2 headline
256 <left>/<right> -> before/after current headline, same headings level")
258 (defvar org-jump-to-target-location nil)
259 (defvar org-remember-previous-location nil)
260 (defvar org-force-remember-template-char) ;; dynamically scoped
262 ;; Save the major mode of the buffer we called remember from
263 (defvar org-select-template-temp-major-mode nil)
265 ;; Temporary store the buffer where remember was called from
266 (defvar org-select-template-original-buffer nil)
268 (defun org-select-remember-template (&optional use-char)
269 (when org-remember-templates
270 (let* ((pre-selected-templates
271 (mapcar
272 (lambda (tpl)
273 (let ((ctxt (nth 5 tpl))
274 (mode org-select-template-temp-major-mode)
275 (buf org-select-template-original-buffer))
276 (and (or (not ctxt) (eq ctxt t)
277 (and (listp ctxt) (memq mode ctxt))
278 (and (functionp ctxt)
279 (with-current-buffer buf
280 ;; Protect the user-defined function from error
281 (condition-case nil (funcall ctxt) (error nil)))))
282 tpl)))
283 org-remember-templates))
284 ;; If no template at this point, add the default templates:
285 (pre-selected-templates1
286 (if (not (delq nil pre-selected-templates))
287 (mapcar (lambda(x) (if (not (nth 5 x)) x))
288 org-remember-templates)
289 pre-selected-templates))
290 ;; Then unconditionally add template for any contexts
291 (pre-selected-templates2
292 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
293 org-remember-templates)
294 (delq nil pre-selected-templates1)))
295 (templates (mapcar (lambda (x)
296 (if (stringp (car x))
297 (append (list (nth 1 x) (car x)) (cddr x))
298 (append (list (car x) "") (cdr x))))
299 (delq nil pre-selected-templates2)))
300 (char (or use-char
301 (cond
302 ((= (length templates) 1)
303 (caar templates))
304 ((and (boundp 'org-force-remember-template-char)
305 org-force-remember-template-char)
306 (if (stringp org-force-remember-template-char)
307 (string-to-char org-force-remember-template-char)
308 org-force-remember-template-char))
310 (message "Select template: %s"
311 (mapconcat
312 (lambda (x)
313 (cond
314 ((not (string-match "\\S-" (nth 1 x)))
315 (format "[%c]" (car x)))
316 ((equal (downcase (car x))
317 (downcase (aref (nth 1 x) 0)))
318 (format "[%c]%s" (car x)
319 (substring (nth 1 x) 1)))
320 (t (format "[%c]%s" (car x) (nth 1 x)))))
321 templates " "))
322 (let ((inhibit-quit t) (char0 (read-char-exclusive)))
323 (when (equal char0 ?\C-g)
324 (jump-to-register remember-register)
325 (kill-buffer remember-buffer))
326 char0))))))
327 (cddr (assoc char templates)))))
329 (defun org-get-x-clipboard (value)
330 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
331 (if (eq window-system 'x)
332 (let ((x (org-get-x-clipboard-compat value)))
333 (if x (org-no-properties x)))))
335 ;;;###autoload
336 (defun org-remember-apply-template (&optional use-char skip-interactive)
337 "Initialize *remember* buffer with template, invoke `org-mode'.
338 This function should be placed into `remember-mode-hook' and in fact requires
339 to be run from that hook to function properly."
340 (when (and (boundp 'initial) (stringp initial))
341 (setq initial (org-no-properties initial))
342 (remove-text-properties 0 (length initial) '(read-only t) initial))
343 (if org-remember-templates
344 (let* ((entry (org-select-remember-template use-char))
345 (ct (or org-overriding-default-time (org-current-time)))
346 (dct (decode-time ct))
347 (ct1
348 (if (< (nth 2 dct) org-extend-today-until)
349 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
350 ct))
351 (tpl (car entry))
352 (plist-p (if org-store-link-plist t nil))
353 (file (if (and (nth 1 entry)
354 (or (and (stringp (nth 1 entry))
355 (string-match "\\S-" (nth 1 entry)))
356 (functionp (nth 1 entry))))
357 (nth 1 entry)
358 org-default-notes-file))
359 (headline (nth 2 entry))
360 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
361 (v-x (or (org-get-x-clipboard 'PRIMARY)
362 (org-get-x-clipboard 'CLIPBOARD)
363 (org-get-x-clipboard 'SECONDARY)))
364 (v-t (format-time-string (car org-time-stamp-formats) ct))
365 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
366 (v-u (concat "[" (substring v-t 1 -1) "]"))
367 (v-U (concat "[" (substring v-T 1 -1) "]"))
368 ;; `initial' and `annotation' are bound in `remember'
369 (v-i (if (boundp 'initial) initial))
370 (v-a (if (and (boundp 'annotation) annotation)
371 (if (equal annotation "[[]]") "" annotation)
372 ""))
373 (clipboards (remove nil (list v-i
374 (org-get-x-clipboard 'PRIMARY)
375 (org-get-x-clipboard 'CLIPBOARD)
376 (org-get-x-clipboard 'SECONDARY)
377 v-c)))
378 (v-A (if (and v-a
379 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
380 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
381 v-a))
382 (v-n user-full-name)
383 (v-k (if (marker-buffer org-clock-marker)
384 (org-substring-no-properties org-clock-heading)))
385 (v-K (if (marker-buffer org-clock-marker)
386 (org-make-link-string
387 (buffer-file-name (marker-buffer org-clock-marker))
388 org-clock-heading)))
390 (org-startup-folded nil)
391 (org-inhibit-startup t)
392 org-time-was-given org-end-time-was-given x
393 prompt completions char time pos default histvar)
395 (when (functionp file)
396 (setq file (funcall file)))
397 (when (and file (not (file-name-absolute-p file)))
398 (setq file (expand-file-name file org-directory)))
401 (setq org-store-link-plist
402 (append (list :annotation v-a :initial v-i)
403 org-store-link-plist))
404 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
405 (erase-buffer)
406 (insert (substitute-command-keys
407 (format
408 "## %s \"%s\" -> \"* %s\"
409 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
410 ## C-0 C-c C-c \"%s\" -> \"* %s\"
411 ## %s to select file and header location interactively.
412 ## C-2 C-c C-c as child of the currently clocked item
413 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
414 (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
415 (abbreviate-file-name (or file org-default-notes-file))
416 (or headline "")
417 (or (car org-remember-previous-location) "???")
418 (or (cdr org-remember-previous-location) "???")
419 (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
420 (insert tpl)
421 (goto-char (point-min))
423 ;; Simple %-escapes
424 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
425 (when (and initial (equal (match-string 0) "%i"))
426 (save-match-data
427 (let* ((lead (buffer-substring
428 (point-at-bol) (match-beginning 0))))
429 (setq v-i (mapconcat 'identity
430 (org-split-string initial "\n")
431 (concat "\n" lead))))))
432 (replace-match
433 (or (eval (intern (concat "v-" (match-string 1)))) "")
434 t t))
436 ;; %[] Insert contents of a file.
437 (goto-char (point-min))
438 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
439 (let ((start (match-beginning 0))
440 (end (match-end 0))
441 (filename (expand-file-name (match-string 1))))
442 (goto-char start)
443 (delete-region start end)
444 (condition-case error
445 (insert-file-contents filename)
446 (error (insert (format "%%![Couldn't insert %s: %s]"
447 filename error))))))
448 ;; %() embedded elisp
449 (goto-char (point-min))
450 (while (re-search-forward "%\\((.+)\\)" nil t)
451 (goto-char (match-beginning 0))
452 (let ((template-start (point)))
453 (forward-char 1)
454 (let ((result
455 (condition-case error
456 (eval (read (current-buffer)))
457 (error (format "%%![Error: %s]" error)))))
458 (delete-region template-start (point))
459 (insert result))))
461 ;; From the property list
462 (when plist-p
463 (goto-char (point-min))
464 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
465 (and (setq x (or (plist-get org-store-link-plist
466 (intern (match-string 1))) ""))
467 (replace-match x t t))))
469 ;; Turn on org-mode in the remember buffer, set local variables
470 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1))
471 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
472 (org-set-local 'org-default-notes-file file))
473 (if headline
474 (org-set-local 'org-remember-default-headline headline))
475 ;; Interactive template entries
476 (goto-char (point-min))
477 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
478 (setq char (if (match-end 3) (match-string 3))
479 prompt (if (match-end 2) (match-string 2)))
480 (goto-char (match-beginning 0))
481 (replace-match "")
482 (setq completions nil default nil)
483 (when prompt
484 (setq completions (org-split-string prompt "|")
485 prompt (pop completions)
486 default (car completions)
487 histvar (intern (concat
488 "org-remember-template-prompt-history::"
489 (or prompt "")))
490 completions (mapcar 'list completions)))
491 (cond
492 ((member char '("G" "g"))
493 (let* ((org-last-tags-completion-table
494 (org-global-tags-completion-table
495 (if (equal char "G") (org-agenda-files) (and file (list file)))))
496 (org-add-colon-after-tag-completion t)
497 (ins (org-ido-completing-read
498 (if prompt (concat prompt ": ") "Tags: ")
499 'org-tags-completion-function nil nil nil
500 'org-tags-history)))
501 (setq ins (mapconcat 'identity
502 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
503 ":"))
504 (when (string-match "\\S-" ins)
505 (or (equal (char-before) ?:) (insert ":"))
506 (insert ins)
507 (or (equal (char-after) ?:) (insert ":")))))
508 ((equal char "C")
509 (cond ((= (length clipboards) 1) (insert (car clipboards)))
510 ((> (length clipboards) 1)
511 (insert (read-string "Clipboard/kill value: "
512 (car clipboards) '(clipboards . 1)
513 (car clipboards))))))
514 ((equal char "L")
515 (cond ((= (length clipboards) 1)
516 (org-insert-link 0 (car clipboards)))
517 ((> (length clipboards) 1)
518 (org-insert-link 0 (read-string "Clipboard/kill value: "
519 (car clipboards)
520 '(clipboards . 1)
521 (car clipboards))))))
522 ((equal char "p")
523 (let*
524 ((prop (org-substring-no-properties prompt))
525 (pall (concat prop "_ALL"))
526 (allowed
527 (with-current-buffer
528 (get-buffer (file-name-nondirectory file))
529 (or (cdr (assoc pall org-file-properties))
530 (cdr (assoc pall org-global-properties))
531 (cdr (assoc pall org-global-properties-fixed)))))
532 (existing (with-current-buffer
533 (get-buffer (file-name-nondirectory file))
534 (mapcar 'list (org-property-values prop))))
535 (propprompt (concat "Value for " prop ": "))
536 (val (if allowed
537 (org-completing-read
538 propprompt
539 (mapcar 'list (org-split-string allowed "[ \t]+"))
540 nil 'req-match)
541 (org-completing-read propprompt existing nil nil
542 "" nil ""))))
543 (org-set-property prop val)))
544 (char
545 ;; These are the date/time related ones
546 (setq org-time-was-given (equal (upcase char) char))
547 (setq time (org-read-date (equal (upcase char) "U") t nil
548 prompt))
549 (org-insert-time-stamp time org-time-was-given
550 (member char '("u" "U"))
551 nil nil (list org-end-time-was-given)))
553 (let (org-completion-use-ido)
554 (insert (org-completing-read
555 (concat (if prompt prompt "Enter string")
556 (if default (concat " [" default "]"))
557 ": ")
558 completions nil nil nil histvar default))))))
559 (goto-char (point-min))
560 (if (re-search-forward "%\\?" nil t)
561 (replace-match "")
562 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
563 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1)))
564 (when (save-excursion
565 (goto-char (point-min))
566 (re-search-forward "%&" nil t))
567 (replace-match "")
568 (org-set-local 'org-jump-to-target-location t))
569 (when (save-excursion
570 (goto-char (point-min))
571 (re-search-forward "%!" nil t))
572 (replace-match "")
573 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
575 (defun org-remember-finish-immediately ()
576 "File remember note immediately.
577 This should be run in `post-command-hook' and will remove itself
578 from that hook."
579 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
580 (org-remember-finalize))
582 (defun org-remember-visit-immediately ()
583 "File remember note immediately.
584 This should be run in `post-command-hook' and will remove itself
585 from that hook."
586 (org-remember '(16))
587 (goto-char (or (text-property-any
588 (point) (save-excursion (org-end-of-subtree t t))
589 'org-position-cursor t)
590 (point)))
591 (message "%s"
592 (format
593 (substitute-command-keys
594 "Restore window configuration with \\[jump-to-register] %c")
595 remember-register)))
597 (defvar org-clock-marker) ; Defined in org.el
598 (defun org-remember-finalize ()
599 "Finalize the remember process."
600 (interactive)
601 (unless org-remember-mode
602 (error "This does not seem to be a remember buffer for Org-mode"))
603 (run-hooks 'org-remember-before-finalize-hook)
604 (unless (fboundp 'remember-finalize)
605 (defalias 'remember-finalize 'remember-buffer))
606 (when (and org-clock-marker
607 (equal (marker-buffer org-clock-marker) (current-buffer)))
608 ;; the clock is running in this buffer.
609 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
610 (or (eq org-remember-clock-out-on-exit t)
611 (and org-remember-clock-out-on-exit
612 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
613 (let (org-log-note-clock-out) (org-clock-out))))
614 (when buffer-file-name
615 (save-buffer)
616 (setq buffer-file-name nil))
617 (remember-finalize))
619 (defun org-remember-kill ()
620 "Abort the current remember process."
621 (interactive)
622 (let ((org-note-abort t))
623 (org-remember-finalize)))
625 ;;;###autoload
626 (defun org-remember (&optional goto org-force-remember-template-char)
627 "Call `remember'. If this is already a remember buffer, re-apply template.
628 If there is an active region, make sure remember uses it as initial content
629 of the remember buffer.
631 When called interactively with a `C-u' prefix argument GOTO, don't remember
632 anything, just go to the file/headline where the selected template usually
633 stores its notes. With a double prefix arg `C-u C-u', go to the last
634 note stored by remember.
636 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
637 associated with a template in `org-remember-templates'."
638 (interactive "P")
639 (org-require-remember)
640 (cond
641 ((equal goto '(4)) (org-go-to-remember-target))
642 ((equal goto '(16)) (org-remember-goto-last-stored))
644 ;; set temporary variables that will be needed in
645 ;; `org-select-remember-template'
646 (setq org-select-template-temp-major-mode major-mode)
647 (setq org-select-template-original-buffer (current-buffer))
648 (if org-remember-mode
649 (progn
650 (when (< (length org-remember-templates) 2)
651 (error "No other template available"))
652 (erase-buffer)
653 (let ((annotation (plist-get org-store-link-plist :annotation))
654 (initial (plist-get org-store-link-plist :initial)))
655 (org-remember-apply-template))
656 (message "Press C-c C-c to remember data"))
657 (if (org-region-active-p)
658 (org-do-remember (buffer-substring (point) (mark)))
659 (org-do-remember))))))
661 (defvar org-remember-last-stored-marker (make-marker)
662 "Marker pointing to the entry most recently stored with `org-remember'.")
664 (defun org-remember-goto-last-stored ()
665 "Go to the location where the last remember note was stored."
666 (interactive)
667 (org-goto-marker-or-bmk org-remember-last-stored-marker
668 "org-remember-last-stored")
669 (message "This is the last note stored by remember"))
671 (defun org-go-to-remember-target (&optional template-key)
672 "Go to the target location of a remember template.
673 The user is queried for the template."
674 (interactive)
675 (let* (org-select-template-temp-major-mode
676 (entry (org-select-remember-template template-key))
677 (file (nth 1 entry))
678 (heading (nth 2 entry))
679 visiting)
680 (unless (and file (stringp file) (string-match "\\S-" file))
681 (setq file org-default-notes-file))
682 (when (and file (not (file-name-absolute-p file)))
683 (setq file (expand-file-name file org-directory)))
684 (unless (and heading (stringp heading) (string-match "\\S-" heading))
685 (setq heading org-remember-default-headline))
686 (setq visiting (org-find-base-buffer-visiting file))
687 (if (not visiting) (find-file-noselect file))
688 (switch-to-buffer (or visiting (get-file-buffer file)))
689 (widen)
690 (goto-char (point-min))
691 (if (re-search-forward
692 (concat "^\\*+[ \t]+" (regexp-quote heading)
693 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
694 nil t)
695 (goto-char (match-beginning 0))
696 (error "Target headline not found: %s" heading))))
698 ;;;###autoload
699 (defun org-remember-handler ()
700 "Store stuff from remember.el into an org file.
701 When the template has specified a file and a headline, the entry is filed
702 there, or in the location defined by `org-default-notes-file' and
703 `org-remember-default-headline'.
705 If no defaults have been defined, or if the current prefix argument
706 is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
707 process is used to select the target location.
709 When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
710 the entry is filed to the same location as the previous note.
712 When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
713 the entry is filed as a subentry of the entry where the clock is
714 currently running.
716 When `C-u' has been used as prefix argument, the note is stored and emacs
717 moves point to the new location of the note, so that editing can be
718 continued there (similar to inserting \"%&\" into the template).
720 Before storing the note, the function ensures that the text has an
721 org-mode-style headline, i.e. a first line that starts with
722 a \"*\". If not, a headline is constructed from the current date and
723 some additional data.
725 If the variable `org-adapt-indentation' is non-nil, the entire text is
726 also indented so that it starts in the same column as the headline
727 \(i.e. after the stars).
729 See also the variable `org-reverse-note-order'."
730 (when (and (equal current-prefix-arg 2)
731 (not (marker-buffer org-clock-marker)))
732 (error "No running clock"))
733 (when (org-bound-and-true-p org-jump-to-target-location)
734 (let* ((end (min (point-max) (1+ (point))))
735 (beg (point)))
736 (if (= end beg) (setq beg (1- beg)))
737 (put-text-property beg end 'org-position-cursor t)))
738 (goto-char (point-min))
739 (while (looking-at "^[ \t]*\n\\|^##.*\n")
740 (replace-match ""))
741 (goto-char (point-max))
742 (beginning-of-line 1)
743 (while (looking-at "[ \t]*$\\|##.*")
744 (delete-region (1- (point)) (point-max))
745 (beginning-of-line 1))
746 (catch 'quit
747 (if org-note-abort (throw 'quit nil))
748 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
749 (previousp (and (member current-prefix-arg '((16) 0))
750 org-remember-previous-location))
751 (clockp (equal current-prefix-arg 2))
752 (fastp (org-xor (equal current-prefix-arg 1)
753 org-remember-store-without-prompt))
754 (file (cond
755 (fastp org-default-notes-file)
756 ((and (eq org-remember-interactive-interface 'refile)
757 org-refile-targets)
758 org-default-notes-file)
759 ((not previousp)
760 (org-get-org-file))))
761 (heading org-remember-default-headline)
762 (visiting (and file (org-find-base-buffer-visiting file)))
763 (org-startup-folded nil)
764 (org-startup-align-all-tables nil)
765 (org-goto-start-pos 1)
766 spos exitcmd level reversed txt)
767 (when (equal current-prefix-arg '(4))
768 (setq visitp t))
769 (when previousp
770 (setq file (car org-remember-previous-location)
771 visiting (and file (org-find-base-buffer-visiting file))
772 heading (cdr org-remember-previous-location)
773 fastp t))
774 (when clockp
775 (setq file (buffer-file-name (marker-buffer org-clock-marker))
776 visiting (and file (org-find-base-buffer-visiting file))
777 heading org-clock-heading-for-remember
778 fastp t))
779 (setq current-prefix-arg nil)
780 ;; Modify text so that it becomes a nice subtree which can be inserted
781 ;; into an org tree.
782 (goto-char (point-min))
783 (if (re-search-forward "[ \t\n]+\\'" nil t)
784 ;; remove empty lines at end
785 (replace-match ""))
786 (goto-char (point-min))
787 (unless (looking-at org-outline-regexp)
788 ;; add a headline
789 (insert (concat "* " (current-time-string)
790 " (" (remember-buffer-desc) ")\n"))
791 (backward-char 1)
792 (when org-adapt-indentation
793 (while (re-search-forward "^" nil t)
794 (insert " "))))
795 (goto-char (point-min))
796 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
797 (replace-match "\n\n")
798 (if (re-search-forward "[ \t\n]*\\'")
799 (replace-match "\n")))
800 (goto-char (point-min))
801 (setq txt (buffer-string))
802 (org-save-markers-in-region (point-min) (point-max))
803 (when (and (eq org-remember-interactive-interface 'refile)
804 (not fastp))
805 (org-refile nil (or visiting (find-file-noselect file)))
806 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
807 (save-excursion
808 (bookmark-jump "org-refile-last-stored")
809 (bookmark-set "org-remember-last-stored")
810 (move-marker org-remember-last-stored-marker (point)))
811 (throw 'quit t))
812 ;; Find the file
813 (with-current-buffer (or visiting (find-file-noselect file))
814 (unless (org-mode-p)
815 (error "Target files for remember notes must be in Org-mode"))
816 (save-excursion
817 (save-restriction
818 (widen)
819 (and (goto-char (point-min))
820 (not (re-search-forward "^\\* " nil t))
821 (insert "\n* " (or (and (stringp heading) heading)
822 "Notes") "\n"))
823 (setq reversed (org-notes-order-reversed-p))
825 ;; Find the default location
826 (when heading
827 (cond
828 ((eq heading 'top)
829 (goto-char (point-min))
830 (or (looking-at org-outline-regexp)
831 (re-search-forward org-outline-regexp nil t))
832 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
833 ((eq heading 'bottom)
834 (goto-char (point-max))
835 (or (bolp) (newline))
836 (setq org-goto-start-pos (point)))
837 ((and (stringp heading) (string-match "\\S-" heading))
838 (goto-char (point-min))
839 (if (re-search-forward
840 (concat "^\\*+[ \t]+" (regexp-quote heading)
841 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
842 nil t)
843 (setq org-goto-start-pos (match-beginning 0))
844 (when fastp
845 (goto-char (point-max))
846 (unless (bolp) (newline))
847 (insert "* " heading "\n")
848 (setq org-goto-start-pos (point-at-bol 0)))))
849 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
850 heading 'top))))
852 ;; Ask the User for a location, using the appropriate interface
853 (cond
854 ((and fastp (memq heading '(top bottom)))
855 (setq spos org-goto-start-pos
856 exitcmd (if (eq heading 'top) 'left nil)))
857 (fastp (setq spos org-goto-start-pos
858 exitcmd 'return))
859 ((eq org-remember-interactive-interface 'outline)
860 (setq spos (org-get-location (current-buffer)
861 org-remember-help)
862 exitcmd (cdr spos)
863 spos (car spos)))
864 ((eq org-remember-interactive-interface 'outline-path-completion)
865 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
866 (org-refile-use-outline-path t))
867 (setq spos (org-refile-get-location "Heading: ")
868 exitcmd 'return
869 spos (nth 3 spos))))
870 (t (error "This should not happen")))
871 (if (not spos) (throw 'quit nil)) ; return nil to show we did
872 ; not handle this note
873 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
874 (goto-char spos)
875 (cond ((org-on-heading-p t)
876 (org-back-to-heading t)
877 (setq level (funcall outline-level))
878 (cond
879 ((eq exitcmd 'return)
880 ;; sublevel of current
881 (setq org-remember-previous-location
882 (cons (abbreviate-file-name file)
883 (org-get-heading 'notags)))
884 (if reversed
885 (outline-next-heading)
886 (org-end-of-subtree t)
887 (if (not (bolp))
888 (if (looking-at "[ \t]*\n")
889 (beginning-of-line 2)
890 (end-of-line 1)
891 (insert "\n"))))
892 (org-paste-subtree (org-get-valid-level level 1) txt)
893 (and org-auto-align-tags (org-set-tags nil t))
894 (bookmark-set "org-remember-last-stored")
895 (move-marker org-remember-last-stored-marker (point)))
896 ((eq exitcmd 'left)
897 ;; before current
898 (org-paste-subtree level txt)
899 (and org-auto-align-tags (org-set-tags nil t))
900 (bookmark-set "org-remember-last-stored")
901 (move-marker org-remember-last-stored-marker (point)))
902 ((eq exitcmd 'right)
903 ;; after current
904 (org-end-of-subtree t)
905 (org-paste-subtree level txt)
906 (and org-auto-align-tags (org-set-tags nil t))
907 (bookmark-set "org-remember-last-stored")
908 (move-marker org-remember-last-stored-marker (point)))
909 (t (error "This should not happen"))))
911 ((eq heading 'bottom)
912 (org-paste-subtree 1 txt)
913 (and org-auto-align-tags (org-set-tags nil t))
914 (bookmark-set "org-remember-last-stored")
915 (move-marker org-remember-last-stored-marker (point)))
917 ((and (bobp) (not reversed))
918 ;; Put it at the end, one level below level 1
919 (save-restriction
920 (widen)
921 (goto-char (point-max))
922 (if (not (bolp)) (newline))
923 (org-paste-subtree (org-get-valid-level 1 1) txt)
924 (and org-auto-align-tags (org-set-tags nil t))
925 (bookmark-set "org-remember-last-stored")
926 (move-marker org-remember-last-stored-marker (point))))
928 ((and (bobp) reversed)
929 ;; Put it at the start, as level 1
930 (save-restriction
931 (widen)
932 (goto-char (point-min))
933 (re-search-forward "^\\*+ " nil t)
934 (beginning-of-line 1)
935 (org-paste-subtree 1 txt)
936 (and org-auto-align-tags (org-set-tags nil t))
937 (bookmark-set "org-remember-last-stored")
938 (move-marker org-remember-last-stored-marker (point))))
940 ;; Put it right there, with automatic level determined by
941 ;; org-paste-subtree or from prefix arg
942 (org-paste-subtree
943 (if (numberp current-prefix-arg) current-prefix-arg)
944 txt)
945 (and org-auto-align-tags (org-set-tags nil t))
946 (bookmark-set "org-remember-last-stored")
947 (move-marker org-remember-last-stored-marker (point))))
949 (when remember-save-after-remembering
950 (save-buffer)
951 (if (and (not visiting)
952 (not (equal (marker-buffer org-clock-marker)
953 (current-buffer))))
954 (kill-buffer (current-buffer)))))))))
956 t) ;; return t to indicate that we took care of this note.
958 (defun org-do-remember (&optional initial)
959 "Call remember."
960 (remember initial))
962 (defun org-require-remember ()
963 "Make sure remember is loaded, or install our own emergency version of it."
964 (condition-case nil
965 (require 'remember)
966 (error
967 ;; Lets install our own micro version of remember
968 (defvar remember-register ?R)
969 (defvar remember-mode-hook nil)
970 (defvar remember-handler-functions nil)
971 (defvar remember-buffer "*Remember*")
972 (defvar remember-save-after-remembering t)
973 (defvar remember-annotation-functions '(buffer-file-name))
974 (defun remember-finalize ()
975 (run-hook-with-args-until-success 'remember-handler-functions)
976 (when (equal remember-buffer (buffer-name))
977 (kill-buffer (current-buffer))
978 (jump-to-register remember-register)))
979 (defun remember-mode ()
980 (fundamental-mode)
981 (setq mode-name "Remember")
982 (run-hooks 'remember-mode-hook))
983 (defun remember (&optional initial)
984 (window-configuration-to-register remember-register)
985 (let* ((annotation (run-hook-with-args-until-success
986 'remember-annotation-functions)))
987 (switch-to-buffer-other-window (get-buffer-create remember-buffer))
988 (remember-mode)))
989 (defun remember-buffer-desc ()
990 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
991 (point-at-eol)))))))
993 (provide 'org-remember)
995 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
997 ;;; org-remember.el ends here