Release 6.28
[org-mode.git] / lisp / org-remember.el
blob9fe9d716d4244b20b8d0b3aa57ba0f6a15e0093c
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.28
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 (function :tag "Function")
190 (const :tag "Use `org-remember-default-headline'" nil)
191 (const :tag "At beginning of file" top)
192 (const :tag "At end of file" bottom))
193 (choice :tag "Context"
194 (const :tag "Use in all contexts" nil)
195 (const :tag "Use in all contexts" t)
196 (repeat :tag "Use only if in major mode"
197 (symbol :tag "Major mode"))
198 (function :tag "Perform a check against function")))))
200 (defcustom org-remember-before-finalize-hook nil
201 "Hook that is run right before a remember process is finalized.
202 The remember buffer is still current when this hook runs."
203 :group 'org-remember
204 :type 'hook)
206 (defvar org-remember-mode-map (make-sparse-keymap)
207 "Keymap for org-remember-mode, a minor mode.
208 Use this map to set additional keybindings for when Org-mode is used
209 for a Remember buffer.")
210 (defvar org-remember-mode-hook nil
211 "Hook for the minor `org-remember-mode'.")
213 (define-minor-mode org-remember-mode
214 "Minor mode for special key bindings in a remember buffer."
215 nil " Rem" org-remember-mode-map
216 (run-hooks 'org-remember-mode-hook))
217 (define-key org-remember-mode-map "\C-c\C-c" 'org-remember-finalize)
218 (define-key org-remember-mode-map "\C-c\C-k" 'org-remember-kill)
220 (defcustom org-remember-clock-out-on-exit 'query
221 "Non-nil means, stop the clock when exiting a clocking remember buffer.
222 This only applies if the clock is running in the remember buffer. If the
223 clock is not stopped, it continues to run in the storage location.
224 Instead of nil or t, this may also be the symbol `query' to prompt the
225 user each time a remember buffer with a running clock is filed away. "
226 :group 'org-remember
227 :type '(choice
228 (const :tag "Never" nil)
229 (const :tag "Always" t)
230 (const :tag "Query user" query)))
232 (defcustom org-remember-backup-directory nil
233 "Directory where to store all remember buffers, for backup purposes.
234 After a remember buffer has been stored successfully, the backup file
235 will be removed. However, if you forget to finish the remember process,
236 the file will remain there.
237 See also `org-remember-auto-remove-backup-files'."
238 :group 'org-remember
239 :type '(choice
240 (const :tag "No backups" nil)
241 (directory :tag "Directory")))
243 (defcustom org-remember-auto-remove-backup-files t
244 "Non-nil means, remove remember backup files after successfully storage.
245 When remember is finished successfully, with storing the note at the
246 desired target, remove the backup files related to this remember process
247 and show a message about remaining backup files, from previous, unfinished
248 remember sessions.
249 Backup files will only be made at all, when `org-remember-backup-directory'
250 is set."
251 :group 'org-remember
252 :type 'boolean)
254 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
255 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
257 ;;;###autoload
258 (defun org-remember-insinuate ()
259 "Setup remember.el for use with Org-mode."
260 (org-require-remember)
261 (setq remember-annotation-functions '(org-remember-annotation))
262 (setq remember-handler-functions '(org-remember-handler))
263 (add-hook 'remember-mode-hook 'org-remember-apply-template))
265 ;;;###autoload
266 (defun org-remember-annotation ()
267 "Return a link to the current location as an annotation for remember.el.
268 If you are using Org-mode files as target for data storage with
269 remember.el, then the annotations should include a link compatible with the
270 conventions in Org-mode. This function returns such a link."
271 (org-store-link nil))
273 (defconst org-remember-help
274 "Select a destination location for the note.
275 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
276 RET on headline -> Store as sublevel entry to current headline
277 RET at beg-of-buf -> Append to file as level 2 headline
278 <left>/<right> -> before/after current headline, same headings level")
280 (defvar org-jump-to-target-location nil)
281 (defvar org-remember-previous-location nil)
282 (defvar org-force-remember-template-char) ;; dynamically scoped
284 ;; Save the major mode of the buffer we called remember from
285 (defvar org-select-template-temp-major-mode nil)
287 ;; Temporary store the buffer where remember was called from
288 (defvar org-select-template-original-buffer nil)
290 (defun org-select-remember-template (&optional use-char)
291 (when org-remember-templates
292 (let* ((pre-selected-templates
293 (mapcar
294 (lambda (tpl)
295 (let ((ctxt (nth 5 tpl))
296 (mode org-select-template-temp-major-mode)
297 (buf org-select-template-original-buffer))
298 (and (or (not ctxt) (eq ctxt t)
299 (and (listp ctxt) (memq mode ctxt))
300 (and (functionp ctxt)
301 (with-current-buffer buf
302 ;; Protect the user-defined function from error
303 (condition-case nil (funcall ctxt) (error nil)))))
304 tpl)))
305 org-remember-templates))
306 ;; If no template at this point, add the default templates:
307 (pre-selected-templates1
308 (if (not (delq nil pre-selected-templates))
309 (mapcar (lambda(x) (if (not (nth 5 x)) x))
310 org-remember-templates)
311 pre-selected-templates))
312 ;; Then unconditionally add template for any contexts
313 (pre-selected-templates2
314 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
315 org-remember-templates)
316 (delq nil pre-selected-templates1)))
317 (templates (mapcar (lambda (x)
318 (if (stringp (car x))
319 (append (list (nth 1 x) (car x)) (cddr x))
320 (append (list (car x) "") (cdr x))))
321 (delq nil pre-selected-templates2)))
323 (char (or use-char
324 (cond
325 ((= (length templates) 1)
326 (caar templates))
327 ((and (boundp 'org-force-remember-template-char)
328 org-force-remember-template-char)
329 (if (stringp org-force-remember-template-char)
330 (string-to-char org-force-remember-template-char)
331 org-force-remember-template-char))
333 (setq msg (format
334 "Select template: %s"
335 (mapconcat
336 (lambda (x)
337 (cond
338 ((not (string-match "\\S-" (nth 1 x)))
339 (format "[%c]" (car x)))
340 ((equal (downcase (car x))
341 (downcase (aref (nth 1 x) 0)))
342 (format "[%c]%s" (car x)
343 (substring (nth 1 x) 1)))
344 (t (format "[%c]%s" (car x) (nth 1 x)))))
345 templates " ")))
346 (let ((inhibit-quit t) char0)
347 (while (not char0)
348 (message msg)
349 (setq char0 (read-char-exclusive))
350 (when (and (not (assoc char0 templates))
351 (not (equal char0 ?\C-g)))
352 (message "No suche template \"%c\"" char0)
353 (ding) (sit-for 1)
354 (setq char0 nil)))
355 (when (equal char0 ?\C-g)
356 (jump-to-register remember-register)
357 (kill-buffer remember-buffer)
358 (error "Abort"))
359 char0))))))
360 (cddr (assoc char templates)))))
362 (defun org-get-x-clipboard (value)
363 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
364 (if (eq window-system 'x)
365 (let ((x (org-get-x-clipboard-compat value)))
366 (if x (org-no-properties x)))))
368 ;;;###autoload
369 (defun org-remember-apply-template (&optional use-char skip-interactive)
370 "Initialize *remember* buffer with template, invoke `org-mode'.
371 This function should be placed into `remember-mode-hook' and in fact requires
372 to be run from that hook to function properly."
373 (when (and (boundp 'initial) (stringp initial))
374 (setq initial (org-no-properties initial))
375 (remove-text-properties 0 (length initial) '(read-only t) initial))
376 (if org-remember-templates
377 (let* ((entry (org-select-remember-template use-char))
378 (ct (or org-overriding-default-time (org-current-time)))
379 (dct (decode-time ct))
380 (ct1
381 (if (< (nth 2 dct) org-extend-today-until)
382 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
383 ct))
384 (tpl (car entry))
385 (plist-p (if org-store-link-plist t nil))
386 (file (if (and (nth 1 entry)
387 (or (and (stringp (nth 1 entry))
388 (string-match "\\S-" (nth 1 entry)))
389 (functionp (nth 1 entry))))
390 (nth 1 entry)
391 org-default-notes-file))
392 (headline (nth 2 entry))
393 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
394 (v-x (or (org-get-x-clipboard 'PRIMARY)
395 (org-get-x-clipboard 'CLIPBOARD)
396 (org-get-x-clipboard 'SECONDARY)))
397 (v-t (format-time-string (car org-time-stamp-formats) ct))
398 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
399 (v-u (concat "[" (substring v-t 1 -1) "]"))
400 (v-U (concat "[" (substring v-T 1 -1) "]"))
401 ;; `initial' and `annotation' are bound in `remember'.
402 ;; But if the property list has them, we prefer those values
403 (v-i (or (plist-get org-store-link-plist :initial)
404 (and (boundp 'initial) initial)
405 ""))
406 (v-a (or (plist-get org-store-link-plist :annotation)
407 (and (boundp 'annotation) annotation)
408 ""))
409 ;; Is the link empty? Then we do not want it...
410 (v-a (if (equal v-a "[[]]") "" v-a))
411 (clipboards (remove nil (list v-i
412 (org-get-x-clipboard 'PRIMARY)
413 (org-get-x-clipboard 'CLIPBOARD)
414 (org-get-x-clipboard 'SECONDARY)
415 v-c)))
416 (v-A (if (and v-a
417 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
418 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
419 v-a))
420 (v-n user-full-name)
421 (v-k (if (marker-buffer org-clock-marker)
422 (org-substring-no-properties org-clock-heading)))
423 (v-K (if (marker-buffer org-clock-marker)
424 (org-make-link-string
425 (buffer-file-name (marker-buffer org-clock-marker))
426 org-clock-heading)))
428 (org-startup-folded nil)
429 (org-inhibit-startup t)
430 org-time-was-given org-end-time-was-given x
431 prompt completions char time pos default histvar)
433 (when (functionp file)
434 (setq file (funcall file)))
435 (when (functionp headline)
436 (setq headline (funcall headline)))
437 (when (and file (not (file-name-absolute-p file)))
438 (setq file (expand-file-name file org-directory)))
440 (setq org-store-link-plist
441 (plist-put org-store-link-plist :annotation v-a)
442 org-store-link-plist
443 (plist-put org-store-link-plist :initial v-i))
445 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
446 (erase-buffer)
447 (insert (substitute-command-keys
448 (format
449 "## %s \"%s\" -> \"* %s\"
450 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
451 ## C-0 C-c C-c \"%s\" -> \"* %s\"
452 ## %s to select file and header location interactively.
453 ## C-2 C-c C-c as child of the currently clocked item
454 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
455 (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
456 (abbreviate-file-name (or file org-default-notes-file))
457 (or headline "")
458 (or (car org-remember-previous-location) "???")
459 (or (cdr org-remember-previous-location) "???")
460 (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
461 (insert tpl)
462 (goto-char (point-min))
464 ;; Simple %-escapes
465 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
466 (when (and initial (equal (match-string 0) "%i"))
467 (save-match-data
468 (let* ((lead (buffer-substring
469 (point-at-bol) (match-beginning 0))))
470 (setq v-i (mapconcat 'identity
471 (org-split-string initial "\n")
472 (concat "\n" lead))))))
473 (replace-match
474 (or (eval (intern (concat "v-" (match-string 1)))) "")
475 t t))
477 ;; %[] Insert contents of a file.
478 (goto-char (point-min))
479 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
480 (let ((start (match-beginning 0))
481 (end (match-end 0))
482 (filename (expand-file-name (match-string 1))))
483 (goto-char start)
484 (delete-region start end)
485 (condition-case error
486 (insert-file-contents filename)
487 (error (insert (format "%%![Couldn't insert %s: %s]"
488 filename error))))))
489 ;; %() embedded elisp
490 (goto-char (point-min))
491 (while (re-search-forward "%\\((.+)\\)" nil t)
492 (goto-char (match-beginning 0))
493 (let ((template-start (point)))
494 (forward-char 1)
495 (let ((result
496 (condition-case error
497 (eval (read (current-buffer)))
498 (error (format "%%![Error: %s]" error)))))
499 (delete-region template-start (point))
500 (insert result))))
502 ;; From the property list
503 (when plist-p
504 (goto-char (point-min))
505 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
506 (and (setq x (or (plist-get org-store-link-plist
507 (intern (match-string 1))) ""))
508 (replace-match x t t))))
510 ;; Turn on org-mode in the remember buffer, set local variables
511 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1))
512 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
513 (org-set-local 'org-default-notes-file file))
514 (if headline
515 (org-set-local 'org-remember-default-headline headline))
516 ;; Interactive template entries
517 (goto-char (point-min))
518 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
519 (setq char (if (match-end 3) (match-string 3))
520 prompt (if (match-end 2) (match-string 2)))
521 (goto-char (match-beginning 0))
522 (replace-match "")
523 (setq completions nil default nil)
524 (when prompt
525 (setq completions (org-split-string prompt "|")
526 prompt (pop completions)
527 default (car completions)
528 histvar (intern (concat
529 "org-remember-template-prompt-history::"
530 (or prompt "")))
531 completions (mapcar 'list completions)))
532 (cond
533 ((member char '("G" "g"))
534 (let* ((org-last-tags-completion-table
535 (org-global-tags-completion-table
536 (if (equal char "G") (org-agenda-files) (and file (list file)))))
537 (org-add-colon-after-tag-completion t)
538 (ins (org-ido-completing-read
539 (if prompt (concat prompt ": ") "Tags: ")
540 'org-tags-completion-function nil nil nil
541 'org-tags-history)))
542 (setq ins (mapconcat 'identity
543 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
544 ":"))
545 (when (string-match "\\S-" ins)
546 (or (equal (char-before) ?:) (insert ":"))
547 (insert ins)
548 (or (equal (char-after) ?:) (insert ":")))))
549 ((equal char "C")
550 (cond ((= (length clipboards) 1) (insert (car clipboards)))
551 ((> (length clipboards) 1)
552 (insert (read-string "Clipboard/kill value: "
553 (car clipboards) '(clipboards . 1)
554 (car clipboards))))))
555 ((equal char "L")
556 (cond ((= (length clipboards) 1)
557 (org-insert-link 0 (car clipboards)))
558 ((> (length clipboards) 1)
559 (org-insert-link 0 (read-string "Clipboard/kill value: "
560 (car clipboards)
561 '(clipboards . 1)
562 (car clipboards))))))
563 ((equal char "p")
564 (let*
565 ((prop (org-substring-no-properties prompt))
566 (pall (concat prop "_ALL"))
567 (allowed
568 (with-current-buffer
569 (get-buffer (file-name-nondirectory file))
570 (or (cdr (assoc pall org-file-properties))
571 (cdr (assoc pall org-global-properties))
572 (cdr (assoc pall org-global-properties-fixed)))))
573 (existing (with-current-buffer
574 (get-buffer (file-name-nondirectory file))
575 (mapcar 'list (org-property-values prop))))
576 (propprompt (concat "Value for " prop ": "))
577 (val (if allowed
578 (org-completing-read
579 propprompt
580 (mapcar 'list (org-split-string allowed "[ \t]+"))
581 nil 'req-match)
582 (org-completing-read-no-ido propprompt existing nil nil
583 "" nil ""))))
584 (org-set-property prop val)))
585 (char
586 ;; These are the date/time related ones
587 (setq org-time-was-given (equal (upcase char) char))
588 (setq time (org-read-date (equal (upcase char) "U") t nil
589 prompt))
590 (org-insert-time-stamp time org-time-was-given
591 (member char '("u" "U"))
592 nil nil (list org-end-time-was-given)))
594 (let (org-completion-use-ido)
595 (insert (org-completing-read-no-ido
596 (concat (if prompt prompt "Enter string")
597 (if default (concat " [" default "]"))
598 ": ")
599 completions nil nil nil histvar default))))))
600 (goto-char (point-min))
601 (if (re-search-forward "%\\?" nil t)
602 (replace-match "")
603 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
604 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1)))
605 (when (save-excursion
606 (goto-char (point-min))
607 (re-search-forward "%&" nil t))
608 (replace-match "")
609 (org-set-local 'org-jump-to-target-location t))
610 (when org-remember-backup-directory
611 (unless (file-directory-p org-remember-backup-directory)
612 (make-directory org-remember-backup-directory))
613 (org-set-local 'auto-save-file-name-transforms nil)
614 (setq buffer-file-name
615 (expand-file-name
616 (format-time-string "remember-%Y-%m-%d-%H-%M-%S")
617 org-remember-backup-directory))
618 (save-buffer)
619 (org-set-local 'auto-save-visited-file-name t)
620 (auto-save-mode 1))
621 (when (save-excursion
622 (goto-char (point-min))
623 (re-search-forward "%!" nil t))
624 (replace-match "")
625 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
627 (defun org-remember-finish-immediately ()
628 "File remember note immediately.
629 This should be run in `post-command-hook' and will remove itself
630 from that hook."
631 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
632 (org-remember-finalize))
634 (defun org-remember-visit-immediately ()
635 "File remember note immediately.
636 This should be run in `post-command-hook' and will remove itself
637 from that hook."
638 (org-remember '(16))
639 (goto-char (or (text-property-any
640 (point) (save-excursion (org-end-of-subtree t t))
641 'org-position-cursor t)
642 (point)))
643 (message "%s"
644 (format
645 (substitute-command-keys
646 "Restore window configuration with \\[jump-to-register] %c")
647 remember-register)))
649 (defvar org-clock-marker) ; Defined in org.el
650 (defun org-remember-finalize ()
651 "Finalize the remember process."
652 (interactive)
653 (unless org-remember-mode
654 (error "This does not seem to be a remember buffer for Org-mode"))
655 (run-hooks 'org-remember-before-finalize-hook)
656 (unless (fboundp 'remember-finalize)
657 (defalias 'remember-finalize 'remember-buffer))
658 (when (and org-clock-marker
659 (equal (marker-buffer org-clock-marker) (current-buffer)))
660 ;; the clock is running in this buffer.
661 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
662 (or (eq org-remember-clock-out-on-exit t)
663 (and org-remember-clock-out-on-exit
664 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
665 (let (org-log-note-clock-out) (org-clock-out))))
666 (when buffer-file-name
667 (save-buffer))
668 (remember-finalize))
670 (defun org-remember-kill ()
671 "Abort the current remember process."
672 (interactive)
673 (let ((org-note-abort t))
674 (org-remember-finalize)))
676 ;;;###autoload
677 (defun org-remember (&optional goto org-force-remember-template-char)
678 "Call `remember'. If this is already a remember buffer, re-apply template.
679 If there is an active region, make sure remember uses it as initial content
680 of the remember buffer.
682 When called interactively with a `C-u' prefix argument GOTO, don't remember
683 anything, just go to the file/headline where the selected template usually
684 stores its notes. With a double prefix arg `C-u C-u', go to the last
685 note stored by remember.
687 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
688 associated with a template in `org-remember-templates'."
689 (interactive "P")
690 (org-require-remember)
691 (cond
692 ((equal goto '(4)) (org-go-to-remember-target))
693 ((equal goto '(16)) (org-remember-goto-last-stored))
695 ;; set temporary variables that will be needed in
696 ;; `org-select-remember-template'
697 (setq org-select-template-temp-major-mode major-mode)
698 (setq org-select-template-original-buffer (current-buffer))
699 (if org-remember-mode
700 (progn
701 (when (< (length org-remember-templates) 2)
702 (error "No other template available"))
703 (erase-buffer)
704 (let ((annotation (plist-get org-store-link-plist :annotation))
705 (initial (plist-get org-store-link-plist :initial)))
706 (org-remember-apply-template))
707 (message "Press C-c C-c to remember data"))
708 (if (org-region-active-p)
709 (org-do-remember (buffer-substring (point) (mark)))
710 (org-do-remember))))))
712 (defvar org-remember-last-stored-marker (make-marker)
713 "Marker pointing to the entry most recently stored with `org-remember'.")
715 (defun org-remember-goto-last-stored ()
716 "Go to the location where the last remember note was stored."
717 (interactive)
718 (org-goto-marker-or-bmk org-remember-last-stored-marker
719 "org-remember-last-stored")
720 (message "This is the last note stored by remember"))
722 (defun org-go-to-remember-target (&optional template-key)
723 "Go to the target location of a remember template.
724 The user is queried for the template."
725 (interactive)
726 (let* (org-select-template-temp-major-mode
727 (entry (org-select-remember-template template-key))
728 (file (nth 1 entry))
729 (heading (nth 2 entry))
730 visiting)
731 (unless (and file (stringp file) (string-match "\\S-" file))
732 (setq file org-default-notes-file))
733 (when (and file (not (file-name-absolute-p file)))
734 (setq file (expand-file-name file org-directory)))
735 (unless (and heading (stringp heading) (string-match "\\S-" heading))
736 (setq heading org-remember-default-headline))
737 (setq visiting (org-find-base-buffer-visiting file))
738 (if (not visiting) (find-file-noselect file))
739 (switch-to-buffer (or visiting (get-file-buffer file)))
740 (widen)
741 (goto-char (point-min))
742 (if (re-search-forward
743 (concat "^\\*+[ \t]+" (regexp-quote heading)
744 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
745 nil t)
746 (goto-char (match-beginning 0))
747 (error "Target headline not found: %s" heading))))
749 ;;;###autoload
750 (defun org-remember-handler ()
751 "Store stuff from remember.el into an org file.
752 When the template has specified a file and a headline, the entry is filed
753 there, or in the location defined by `org-default-notes-file' and
754 `org-remember-default-headline'.
756 If no defaults have been defined, or if the current prefix argument
757 is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
758 process is used to select the target location.
760 When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
761 the entry is filed to the same location as the previous note.
763 When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
764 the entry is filed as a subentry of the entry where the clock is
765 currently running.
767 When `C-u' has been used as prefix argument, the note is stored and emacs
768 moves point to the new location of the note, so that editing can be
769 continued there (similar to inserting \"%&\" into the template).
771 Before storing the note, the function ensures that the text has an
772 org-mode-style headline, i.e. a first line that starts with
773 a \"*\". If not, a headline is constructed from the current date and
774 some additional data.
776 If the variable `org-adapt-indentation' is non-nil, the entire text is
777 also indented so that it starts in the same column as the headline
778 \(i.e. after the stars).
780 See also the variable `org-reverse-note-order'."
781 (when (and (equal current-prefix-arg 2)
782 (not (marker-buffer org-clock-marker)))
783 (error "No running clock"))
784 (when (org-bound-and-true-p org-jump-to-target-location)
785 (let* ((end (min (point-max) (1+ (point))))
786 (beg (point)))
787 (if (= end beg) (setq beg (1- beg)))
788 (put-text-property beg end 'org-position-cursor t)))
789 (goto-char (point-min))
790 (while (looking-at "^[ \t]*\n\\|^##.*\n")
791 (replace-match ""))
792 (goto-char (point-max))
793 (beginning-of-line 1)
794 (while (and (looking-at "[ \t]*$\\|##.*") (> (point) 1))
795 (delete-region (1- (point)) (point-max))
796 (beginning-of-line 1))
797 (catch 'quit
798 (if org-note-abort (throw 'quit t))
799 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
800 (backup-file
801 (and buffer-file-name
802 (equal (file-name-directory buffer-file-name)
803 (file-name-as-directory
804 (expand-file-name org-remember-backup-directory)))
805 (string-match "^remember-[0-9]\\{4\\}"
806 (file-name-nondirectory buffer-file-name))
807 buffer-file-name))
809 (dummy
810 (unless (string-match "\\S-" (buffer-string))
811 (message "Nothing to remember")
812 (and backup-file
813 (ignore-errors
814 (delete-file backup-file)
815 (delete-file (concat backup-file "~"))))
816 (set-buffer-modified-p nil)
817 (throw 'quit t)))
818 (previousp (and (member current-prefix-arg '((16) 0))
819 org-remember-previous-location))
820 (clockp (equal current-prefix-arg 2))
821 (fastp (org-xor (equal current-prefix-arg 1)
822 org-remember-store-without-prompt))
823 (file (cond
824 (fastp org-default-notes-file)
825 ((and (eq org-remember-interactive-interface 'refile)
826 org-refile-targets)
827 org-default-notes-file)
828 ((not previousp)
829 (org-get-org-file))))
830 (heading org-remember-default-headline)
831 (visiting (and file (org-find-base-buffer-visiting file)))
832 (org-startup-folded nil)
833 (org-startup-align-all-tables nil)
834 (org-goto-start-pos 1)
835 spos exitcmd level reversed txt text-before-node-creation)
836 (when (equal current-prefix-arg '(4))
837 (setq visitp t))
838 (when previousp
839 (setq file (car org-remember-previous-location)
840 visiting (and file (org-find-base-buffer-visiting file))
841 heading (cdr org-remember-previous-location)
842 fastp t))
843 (when clockp
844 (setq file (buffer-file-name (marker-buffer org-clock-marker))
845 visiting (and file (org-find-base-buffer-visiting file))
846 heading org-clock-heading-for-remember
847 fastp t))
848 (setq current-prefix-arg nil)
849 ;; Modify text so that it becomes a nice subtree which can be inserted
850 ;; into an org tree.
851 (goto-char (point-min))
852 (if (re-search-forward "[ \t\n]+\\'" nil t)
853 ;; remove empty lines at end
854 (replace-match ""))
855 (goto-char (point-min))
856 (unless (looking-at org-outline-regexp)
857 ;; add a headline
858 (setq text-before-node-creation (buffer-string))
859 (insert (concat "* " (current-time-string)
860 " (" (remember-buffer-desc) ")\n"))
861 (backward-char 1)
862 (when org-adapt-indentation
863 (while (re-search-forward "^" nil t)
864 (insert " "))))
865 (goto-char (point-min))
866 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
867 (replace-match "\n\n")
868 (if (re-search-forward "[ \t\n]*\\'")
869 (replace-match "\n")))
870 (goto-char (point-min))
871 (setq txt (buffer-string))
872 (org-save-markers-in-region (point-min) (point-max))
873 (set-buffer-modified-p nil)
874 (when (and (eq org-remember-interactive-interface 'refile)
875 (not fastp))
876 (org-refile nil (or visiting (find-file-noselect file)))
877 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
878 (save-excursion
879 (bookmark-jump "org-refile-last-stored")
880 (bookmark-set "org-remember-last-stored")
881 (move-marker org-remember-last-stored-marker (point)))
882 (throw 'quit t))
883 ;; Find the file
884 (with-current-buffer (or visiting (find-file-noselect file))
885 (unless (or (org-mode-p) (member heading '(top bottom)))
886 (error "Target files for notes must be in Org-mode if not filing to top/bottom"))
887 (save-excursion
888 (save-restriction
889 (widen)
890 (setq reversed (org-notes-order-reversed-p))
892 ;; Find the default location
893 (when heading
894 (cond
895 ((not (org-mode-p))
896 (if (eq heading 'top)
897 (goto-char (point-min))
898 (goto-char (point-max))
899 (or (bolp) (newline)))
900 (insert text-before-node-creation)
901 (when remember-save-after-remembering
902 (save-buffer)
903 (if (not visiting) (kill-buffer (current-buffer))))
904 (throw 'quit t))
905 ((eq heading 'top)
906 (goto-char (point-min))
907 (or (looking-at org-outline-regexp)
908 (re-search-forward org-outline-regexp nil t))
909 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
910 ((eq heading 'bottom)
911 (goto-char (point-max))
912 (or (bolp) (newline))
913 (setq org-goto-start-pos (point)))
914 ((and (stringp heading) (string-match "\\S-" heading))
915 (goto-char (point-min))
916 (if (re-search-forward
917 (concat "^\\*+[ \t]+" (regexp-quote heading)
918 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
919 nil t)
920 (setq org-goto-start-pos (match-beginning 0))
921 (when fastp
922 (goto-char (point-max))
923 (unless (bolp) (newline))
924 (insert "* " heading "\n")
925 (setq org-goto-start-pos (point-at-bol 0)))))
926 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
927 heading 'top))))
929 ;; Ask the User for a location, using the appropriate interface
930 (cond
931 ((and fastp (memq heading '(top bottom)))
932 (setq spos org-goto-start-pos
933 exitcmd (if (eq heading 'top) 'left nil)))
934 (fastp (setq spos org-goto-start-pos
935 exitcmd 'return))
936 ((eq org-remember-interactive-interface 'outline)
937 (setq spos (org-get-location (current-buffer)
938 org-remember-help)
939 exitcmd (cdr spos)
940 spos (car spos)))
941 ((eq org-remember-interactive-interface 'outline-path-completion)
942 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
943 (org-refile-use-outline-path t))
944 (setq spos (org-refile-get-location "Heading: ")
945 exitcmd 'return
946 spos (nth 3 spos))))
947 (t (error "This should not happen")))
948 (if (not spos) (throw 'quit nil)) ; return nil to show we did
949 ; not handle this note
950 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
951 (goto-char spos)
952 (cond ((org-on-heading-p t)
953 (org-back-to-heading t)
954 (setq level (funcall outline-level))
955 (cond
956 ((eq exitcmd 'return)
957 ;; sublevel of current
958 (setq org-remember-previous-location
959 (cons (abbreviate-file-name file)
960 (org-get-heading 'notags)))
961 (if reversed
962 (outline-next-heading)
963 (org-end-of-subtree t)
964 (if (not (bolp))
965 (if (looking-at "[ \t]*\n")
966 (beginning-of-line 2)
967 (end-of-line 1)
968 (insert "\n"))))
969 (org-paste-subtree (org-get-valid-level level 1) txt)
970 (and org-auto-align-tags (org-set-tags nil t))
971 (bookmark-set "org-remember-last-stored")
972 (move-marker org-remember-last-stored-marker (point)))
973 ((eq exitcmd 'left)
974 ;; before current
975 (org-paste-subtree level txt)
976 (and org-auto-align-tags (org-set-tags nil t))
977 (bookmark-set "org-remember-last-stored")
978 (move-marker org-remember-last-stored-marker (point)))
979 ((eq exitcmd 'right)
980 ;; after current
981 (org-end-of-subtree t)
982 (org-paste-subtree level txt)
983 (and org-auto-align-tags (org-set-tags nil t))
984 (bookmark-set "org-remember-last-stored")
985 (move-marker org-remember-last-stored-marker (point)))
986 (t (error "This should not happen"))))
988 ((eq heading 'bottom)
989 (org-paste-subtree 1 txt)
990 (and org-auto-align-tags (org-set-tags nil t))
991 (bookmark-set "org-remember-last-stored")
992 (move-marker org-remember-last-stored-marker (point)))
994 ((and (bobp) (not reversed))
995 ;; Put it at the end, one level below level 1
996 (save-restriction
997 (widen)
998 (goto-char (point-max))
999 (if (not (bolp)) (newline))
1000 (org-paste-subtree (org-get-valid-level 1 1) txt)
1001 (and org-auto-align-tags (org-set-tags nil t))
1002 (bookmark-set "org-remember-last-stored")
1003 (move-marker org-remember-last-stored-marker (point))))
1005 ((and (bobp) reversed)
1006 ;; Put it at the start, as level 1
1007 (save-restriction
1008 (widen)
1009 (goto-char (point-min))
1010 (re-search-forward "^\\*+ " nil t)
1011 (beginning-of-line 1)
1012 (org-paste-subtree 1 txt)
1013 (and org-auto-align-tags (org-set-tags nil t))
1014 (bookmark-set "org-remember-last-stored")
1015 (move-marker org-remember-last-stored-marker (point))))
1017 ;; Put it right there, with automatic level determined by
1018 ;; org-paste-subtree or from prefix arg
1019 (org-paste-subtree
1020 (if (numberp current-prefix-arg) current-prefix-arg)
1021 txt)
1022 (and org-auto-align-tags (org-set-tags nil t))
1023 (bookmark-set "org-remember-last-stored")
1024 (move-marker org-remember-last-stored-marker (point))))
1026 (when remember-save-after-remembering
1027 (save-buffer)
1028 (if (and (not visiting)
1029 (not (equal (marker-buffer org-clock-marker)
1030 (current-buffer))))
1031 (kill-buffer (current-buffer))))
1032 (when org-remember-auto-remove-backup-files
1033 (when backup-file
1034 (ignore-errors
1035 (delete-file backup-file)
1036 (delete-file (concat backup-file "~"))))
1037 (when org-remember-backup-directory
1038 (let ((n (length
1039 (directory-files
1040 org-remember-backup-directory nil
1041 "^remember-.*[0-9]$"))))
1042 (when (> n 0)
1043 (message
1044 "%d backup files (unfinished remember calls) in %s"
1045 n org-remember-backup-directory))))))))))
1047 t) ;; return t to indicate that we took care of this note.
1049 (defun org-do-remember (&optional initial)
1050 "Call remember."
1051 (remember initial))
1053 (defun org-require-remember ()
1054 "Make sure remember is loaded, or install our own emergency version of it."
1055 (condition-case nil
1056 (require 'remember)
1057 (error
1058 ;; Lets install our own micro version of remember
1059 (defvar remember-register ?R)
1060 (defvar remember-mode-hook nil)
1061 (defvar remember-handler-functions nil)
1062 (defvar remember-buffer "*Remember*")
1063 (defvar remember-save-after-remembering t)
1064 (defvar remember-annotation-functions '(buffer-file-name))
1065 (defun remember-finalize ()
1066 (run-hook-with-args-until-success 'remember-handler-functions)
1067 (when (equal remember-buffer (buffer-name))
1068 (kill-buffer (current-buffer))
1069 (jump-to-register remember-register)))
1070 (defun remember-mode ()
1071 (fundamental-mode)
1072 (setq mode-name "Remember")
1073 (run-hooks 'remember-mode-hook))
1074 (defun remember (&optional initial)
1075 (window-configuration-to-register remember-register)
1076 (let* ((annotation (run-hook-with-args-until-success
1077 'remember-annotation-functions)))
1078 (switch-to-buffer-other-window (get-buffer-create remember-buffer))
1079 (remember-mode)))
1080 (defun remember-buffer-desc ()
1081 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
1082 (point-at-eol)))))))
1084 (provide 'org-remember)
1086 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
1088 ;;; org-remember.el ends here