Release 6.33e
[org-mode/org-tableheadings.git] / lisp / org-remember.el
blobf8f13269af88ca10c89f7c84ed01824aa6e97091
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.33e
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)
37 (require 'org-datetree)
39 (declare-function remember-mode "remember" ())
40 (declare-function remember "remember" (&optional initial))
41 (declare-function remember-buffer-desc "remember" ())
42 (declare-function remember-finalize "remember" ())
43 (defvar remember-save-after-remembering)
44 (defvar remember-register)
45 (defvar remember-buffer)
46 (defvar remember-handler-functions)
47 (defvar remember-annotation-functions)
48 (defvar org-clock-heading)
49 (defvar org-clock-heading-for-remember)
51 (defgroup org-remember nil
52 "Options concerning interaction with remember.el."
53 :tag "Org Remember"
54 :group 'org)
56 (defcustom org-remember-store-without-prompt t
57 "Non-nil means, `C-c C-c' stores remember note without further prompts.
58 It then uses the file and headline specified by the template or (if the
59 template does not specify them) by the variables `org-default-notes-file'
60 and `org-remember-default-headline'. To force prompting anyway, use
61 `C-u C-c C-c' to file the note.
63 When this variable is nil, `C-c C-c' gives you the prompts, and
64 `C-u C-c C-c' triggers the fasttrack."
65 :group 'org-remember
66 :type 'boolean)
68 (defcustom org-remember-interactive-interface 'refile
69 "The interface to be used for interactive filing of remember notes.
70 This is only used when the interactive mode for selecting a filing
71 location is used (see the variable `org-remember-store-without-prompt').
72 Allowed values are:
73 outline The interface shows an outline of the relevant file
74 and the correct heading is found by moving through
75 the outline or by searching with incremental search.
76 outline-path-completion Headlines in the current buffer are offered via
77 completion.
78 refile Use the refile interface, and offer headlines,
79 possibly from different buffers."
80 :group 'org-remember
81 :type '(choice
82 (const :tag "Refile" refile)
83 (const :tag "Outline" outline)
84 (const :tag "Outline-path-completion" outline-path-completion)))
86 (defcustom org-remember-default-headline ""
87 "The headline that should be the default location in the notes file.
88 When filing remember notes, the cursor will start at that position.
89 You can set this on a per-template basis with the variable
90 `org-remember-templates'."
91 :group 'org-remember
92 :type 'string)
94 (defcustom org-remember-templates nil
95 "Templates for the creation of remember buffers.
96 When nil, just let remember make the buffer.
97 When non-nil, this is a list of 5-element lists. In each entry, the first
98 element is the name of the template, which should be a single short word.
99 The second element is a character, a unique key to select this template.
100 The third element is the template.
102 The fourth element is optional and can specify a destination file for
103 remember items created with this template. The default file is given
104 by `org-default-notes-file'. If the file name is not an absolute path,
105 it will be interpreted relative to `org-directory'.
107 An optional fifth element can specify the headline in that file that should
108 be offered first when the user is asked to file the entry. The default
109 headline is given in the variable `org-remember-default-headline'. When
110 this element is `top' or `bottom', the note will be placed as a level-1
111 entry at the beginning or end of the file, respectively.
113 An optional sixth element specifies the contexts in which the template
114 will be offered to the user. This element can be a list of major modes
115 or a function, and the template will only be offered if `org-remember'
116 is called from a mode in the list, or if the function returns t.
117 Templates that specify t or nil for the context will be always be added
118 to the list of selectable templates.
120 The template specifies the structure of the remember buffer. It should have
121 a first line starting with a star, to act as the org-mode headline.
122 Furthermore, the following %-escapes will be replaced with content:
124 %^{prompt} Prompt the user for a string and replace this sequence with it.
125 A default value and a completion table ca be specified like this:
126 %^{prompt|default|completion2|completion3|...}
127 %t time stamp, date only
128 %T time stamp with date and time
129 %u, %U like the above, but inactive time stamps
130 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
131 You may define a prompt like %^{Please specify birthday
132 %n user name (taken from `user-full-name')
133 %a annotation, normally the link created with org-store-link
134 %i initial content, copied from the active region. If %i is
135 indented, the entire inserted text will be indented as well.
136 %c current kill ring head
137 %x content of the X clipboard
138 %^C Interactive selection of which kill or clip to use
139 %^L Like %^C, but insert as link
140 %k title of currently clocked task
141 %K link to currently clocked task
142 %^g prompt for tags, with completion on tags in target file
143 %^G prompt for tags, with completion all tags in all agenda files
144 %^{prop}p Prompt the user for a value for property `prop'
145 %:keyword specific information for certain link types, see below
146 %[pathname] insert the contents of the file given by `pathname'
147 %(sexp) evaluate elisp `(sexp)' and replace with the result
148 %! Store this note immediately after filling the template
149 %& Visit note immediately after storing it
151 %? After completing the template, position cursor here.
153 Apart from these general escapes, you can access information specific to the
154 link type that is created. For example, calling `remember' in emails or gnus
155 will record the author and the subject of the message, which you can access
156 with %:author and %:subject, respectively. Here is a complete list of what
157 is recorded for each link type.
159 Link type | Available information
160 -------------------+------------------------------------------------------
161 bbdb | %:type %:name %:company
162 vm, wl, mh, rmail | %:type %:subject %:message-id
163 | %:from %:fromname %:fromaddress
164 | %:to %:toname %:toaddress
165 | %:fromto (either \"to NAME\" or \"from NAME\")
166 gnus | %:group, for messages also all email fields
167 w3, w3m | %:type %:url
168 info | %:type %:file %:node
169 calendar | %:type %:date"
170 :group 'org-remember
171 :get (lambda (var) ; Make sure all entries have at least 5 elements
172 (mapcar (lambda (x)
173 (if (not (stringp (car x))) (setq x (cons "" x)))
174 (cond ((= (length x) 4) (append x '(nil)))
175 ((= (length x) 3) (append x '(nil nil)))
176 (t x)))
177 (default-value var)))
178 :type '(repeat
179 :tag "enabled"
180 (list :value ("" ?a "\n" nil nil nil)
181 (string :tag "Name")
182 (character :tag "Selection Key")
183 (string :tag "Template")
184 (choice :tag "Destination file"
185 (file :tag "Specify")
186 (function :tag "Function")
187 (const :tag "Use `org-default-notes-file'" nil))
188 (choice :tag "Destin. headline"
189 (string :tag "Specify")
190 (function :tag "Function")
191 (const :tag "Use `org-remember-default-headline'" nil)
192 (const :tag "At beginning of file" top)
193 (const :tag "At end of file" bottom)
194 (const :tag "In a date tree" date-tree))
195 (choice :tag "Context"
196 (const :tag "Use in all contexts" nil)
197 (const :tag "Use in all contexts" t)
198 (repeat :tag "Use only if in major mode"
199 (symbol :tag "Major mode"))
200 (function :tag "Perform a check against function")))))
202 (defcustom org-remember-delete-empty-lines-at-end t
203 "Non-nil means clean up final empty lines in remember buffer."
204 :group 'org-remember
205 :type 'boolean)
207 (defcustom org-remember-before-finalize-hook nil
208 "Hook that is run right before a remember process is finalized.
209 The remember buffer is still current when this hook runs."
210 :group 'org-remember
211 :type 'hook)
213 (defvar org-remember-mode-map (make-sparse-keymap)
214 "Keymap for org-remember-mode, a minor mode.
215 Use this map to set additional keybindings for when Org-mode is used
216 for a Remember buffer.")
217 (defvar org-remember-mode-hook nil
218 "Hook for the minor `org-remember-mode'.")
220 (define-minor-mode org-remember-mode
221 "Minor mode for special key bindings in a remember buffer."
222 nil " Rem" org-remember-mode-map
223 (run-hooks 'org-remember-mode-hook))
224 (define-key org-remember-mode-map "\C-c\C-c" 'org-remember-finalize)
225 (define-key org-remember-mode-map "\C-c\C-k" 'org-remember-kill)
227 (defcustom org-remember-clock-out-on-exit 'query
228 "Non-nil means, stop the clock when exiting a clocking remember buffer.
229 This only applies if the clock is running in the remember buffer. If the
230 clock is not stopped, it continues to run in the storage location.
231 Instead of nil or t, this may also be the symbol `query' to prompt the
232 user each time a remember buffer with a running clock is filed away. "
233 :group 'org-remember
234 :type '(choice
235 (const :tag "Never" nil)
236 (const :tag "Always" t)
237 (const :tag "Query user" query)))
239 (defcustom org-remember-backup-directory nil
240 "Directory where to store all remember buffers, for backup purposes.
241 After a remember buffer has been stored successfully, the backup file
242 will be removed. However, if you forget to finish the remember process,
243 the file will remain there.
244 See also `org-remember-auto-remove-backup-files'."
245 :group 'org-remember
246 :type '(choice
247 (const :tag "No backups" nil)
248 (directory :tag "Directory")))
250 (defcustom org-remember-auto-remove-backup-files t
251 "Non-nil means, remove remember backup files after successfully storage.
252 When remember is finished successfully, with storing the note at the
253 desired target, remove the backup files related to this remember process
254 and show a message about remaining backup files, from previous, unfinished
255 remember sessions.
256 Backup files will only be made at all, when `org-remember-backup-directory'
257 is set."
258 :group 'org-remember
259 :type 'boolean)
261 (defcustom org-remember-warn-about-backups t
262 "Non-nil means warn about backup files in `org-remember-backup-directory'.
264 Set this to nil if you find that you don't need the warning.
266 If you cancel remember calls frequently and know when they
267 contain useful information (because you know that you made an
268 error or emacs crashed, for example) nil is more useful. In the
269 opposite case, the default, t, is more useful."
270 :group 'org-remember
271 :type 'boolean)
273 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
274 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
276 ;;;###autoload
277 (defun org-remember-insinuate ()
278 "Setup remember.el for use with Org-mode."
279 (org-require-remember)
280 (setq remember-annotation-functions '(org-remember-annotation))
281 (setq remember-handler-functions '(org-remember-handler))
282 (add-hook 'remember-mode-hook 'org-remember-apply-template))
284 ;;;###autoload
285 (defun org-remember-annotation ()
286 "Return a link to the current location as an annotation for remember.el.
287 If you are using Org-mode files as target for data storage with
288 remember.el, then the annotations should include a link compatible with the
289 conventions in Org-mode. This function returns such a link."
290 (org-store-link nil))
292 (defconst org-remember-help
293 "Select a destination location for the note.
294 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
295 RET on headline -> Store as sublevel entry to current headline
296 RET at beg-of-buf -> Append to file as level 2 headline
297 <left>/<right> -> before/after current headline, same headings level")
299 (defvar org-jump-to-target-location nil)
300 (defvar org-remember-previous-location nil)
301 (defvar org-remember-reference-date nil)
302 (defvar org-force-remember-template-char) ;; dynamically scoped
304 ;; Save the major mode of the buffer we called remember from
305 (defvar org-select-template-temp-major-mode nil)
307 ;; Temporary store the buffer where remember was called from
308 (defvar org-select-template-original-buffer nil)
310 (defun org-select-remember-template (&optional use-char)
311 (when org-remember-templates
312 (let* ((pre-selected-templates
313 (mapcar
314 (lambda (tpl)
315 (let ((ctxt (nth 5 tpl))
316 (mode org-select-template-temp-major-mode)
317 (buf org-select-template-original-buffer))
318 (and (or (not ctxt) (eq ctxt t)
319 (and (listp ctxt) (memq mode ctxt))
320 (and (functionp ctxt)
321 (with-current-buffer buf
322 ;; Protect the user-defined function from error
323 (condition-case nil (funcall ctxt) (error nil)))))
324 tpl)))
325 org-remember-templates))
326 ;; If no template at this point, add the default templates:
327 (pre-selected-templates1
328 (if (not (delq nil pre-selected-templates))
329 (mapcar (lambda(x) (if (not (nth 5 x)) x))
330 org-remember-templates)
331 pre-selected-templates))
332 ;; Then unconditionally add template for any contexts
333 (pre-selected-templates2
334 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
335 org-remember-templates)
336 (delq nil pre-selected-templates1)))
337 (templates (mapcar (lambda (x)
338 (if (stringp (car x))
339 (append (list (nth 1 x) (car x)) (cddr x))
340 (append (list (car x) "") (cdr x))))
341 (delq nil pre-selected-templates2)))
343 (char (or use-char
344 (cond
345 ((= (length templates) 1)
346 (caar templates))
347 ((and (boundp 'org-force-remember-template-char)
348 org-force-remember-template-char)
349 (if (stringp org-force-remember-template-char)
350 (string-to-char org-force-remember-template-char)
351 org-force-remember-template-char))
353 (setq msg (format
354 "Select template: %s"
355 (mapconcat
356 (lambda (x)
357 (cond
358 ((not (string-match "\\S-" (nth 1 x)))
359 (format "[%c]" (car x)))
360 ((equal (downcase (car x))
361 (downcase (aref (nth 1 x) 0)))
362 (format "[%c]%s" (car x)
363 (substring (nth 1 x) 1)))
364 (t (format "[%c]%s" (car x) (nth 1 x)))))
365 templates " ")))
366 (let ((inhibit-quit t) char0)
367 (while (not char0)
368 (message msg)
369 (setq char0 (read-char-exclusive))
370 (when (and (not (assoc char0 templates))
371 (not (equal char0 ?\C-g)))
372 (message "No such template \"%c\"" char0)
373 (ding) (sit-for 1)
374 (setq char0 nil)))
375 (when (equal char0 ?\C-g)
376 (jump-to-register remember-register)
377 (kill-buffer remember-buffer)
378 (error "Abort"))
379 char0))))))
380 (cddr (assoc char templates)))))
382 (defun org-get-x-clipboard (value)
383 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
384 (if (eq window-system 'x)
385 (let ((x (org-get-x-clipboard-compat value)))
386 (if x (org-no-properties x)))))
388 ;;;###autoload
389 (defun org-remember-apply-template (&optional use-char skip-interactive)
390 "Initialize *remember* buffer with template, invoke `org-mode'.
391 This function should be placed into `remember-mode-hook' and in fact requires
392 to be run from that hook to function properly."
393 (when (and (boundp 'initial) (stringp initial))
394 (setq initial (org-no-properties initial))
395 (remove-text-properties 0 (length initial) '(read-only t) initial))
396 (if org-remember-templates
397 (let* ((entry (org-select-remember-template use-char))
398 (ct (or org-overriding-default-time (org-current-time)))
399 (dct (decode-time ct))
400 (ct1
401 (if (< (nth 2 dct) org-extend-today-until)
402 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
403 ct))
404 (tpl (car entry))
405 (plist-p (if org-store-link-plist t nil))
406 (file (if (and (nth 1 entry)
407 (or (and (stringp (nth 1 entry))
408 (string-match "\\S-" (nth 1 entry)))
409 (functionp (nth 1 entry))))
410 (nth 1 entry)
411 org-default-notes-file))
412 (headline (nth 2 entry))
413 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
414 (v-x (or (org-get-x-clipboard 'PRIMARY)
415 (org-get-x-clipboard 'CLIPBOARD)
416 (org-get-x-clipboard 'SECONDARY)))
417 (v-t (format-time-string (car org-time-stamp-formats) ct))
418 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
419 (v-u (concat "[" (substring v-t 1 -1) "]"))
420 (v-U (concat "[" (substring v-T 1 -1) "]"))
421 ;; `initial' and `annotation' are bound in `remember'.
422 ;; But if the property list has them, we prefer those values
423 (v-i (or (plist-get org-store-link-plist :initial)
424 (and (boundp 'initial) initial)
425 ""))
426 (v-a (or (plist-get org-store-link-plist :annotation)
427 (and (boundp 'annotation) annotation)
428 ""))
429 ;; Is the link empty? Then we do not want it...
430 (v-a (if (equal v-a "[[]]") "" v-a))
431 (clipboards (remove nil (list v-i
432 (org-get-x-clipboard 'PRIMARY)
433 (org-get-x-clipboard 'CLIPBOARD)
434 (org-get-x-clipboard 'SECONDARY)
435 v-c)))
436 (v-A (if (and v-a
437 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
438 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
439 v-a))
440 (v-n user-full-name)
441 (v-k (if (marker-buffer org-clock-marker)
442 (org-substring-no-properties org-clock-heading)))
443 (v-K (if (marker-buffer org-clock-marker)
444 (org-make-link-string
445 (buffer-file-name (marker-buffer org-clock-marker))
446 org-clock-heading)))
448 (org-startup-folded nil)
449 (org-inhibit-startup t)
450 org-time-was-given org-end-time-was-given x
451 prompt completions char time pos default histvar)
453 (when (functionp file)
454 (setq file (funcall file)))
455 (when (functionp headline)
456 (setq headline (funcall headline)))
457 (when (and file (not (file-name-absolute-p file)))
458 (setq file (expand-file-name file org-directory)))
460 (setq org-store-link-plist
461 (plist-put org-store-link-plist :annotation v-a)
462 org-store-link-plist
463 (plist-put org-store-link-plist :initial v-i))
465 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
466 (erase-buffer)
467 (insert (substitute-command-keys
468 (format
469 "## %s \"%s\" -> \"* %s\"
470 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
471 ## C-0 C-c C-c \"%s\" -> \"* %s\"
472 ## %s to select file and header location interactively.
473 ## C-2 C-c C-c as child of the currently clocked item
474 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
475 (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
476 (abbreviate-file-name (or file org-default-notes-file))
477 (or headline "")
478 (or (car org-remember-previous-location) "???")
479 (or (cdr org-remember-previous-location) "???")
480 (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
481 (insert tpl)
482 (goto-char (point-min))
484 ;; Simple %-escapes
485 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
486 (unless (org-remember-escaped-%)
487 (when (and initial (equal (match-string 0) "%i"))
488 (save-match-data
489 (let* ((lead (buffer-substring
490 (point-at-bol) (match-beginning 0))))
491 (setq v-i (mapconcat 'identity
492 (org-split-string initial "\n")
493 (concat "\n" lead))))))
494 (replace-match
495 (or (eval (intern (concat "v-" (match-string 1)))) "")
496 t t)))
498 ;; %[] Insert contents of a file.
499 (goto-char (point-min))
500 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
501 (unless (org-remember-escaped-%)
502 (let ((start (match-beginning 0))
503 (end (match-end 0))
504 (filename (expand-file-name (match-string 1))))
505 (goto-char start)
506 (delete-region start end)
507 (condition-case error
508 (insert-file-contents filename)
509 (error (insert (format "%%![Couldn't insert %s: %s]"
510 filename error)))))))
511 ;; %() embedded elisp
512 (goto-char (point-min))
513 (while (re-search-forward "%\\((.+)\\)" nil t)
514 (unless (org-remember-escaped-%)
515 (goto-char (match-beginning 0))
516 (let ((template-start (point)))
517 (forward-char 1)
518 (let ((result
519 (condition-case error
520 (eval (read (current-buffer)))
521 (error (format "%%![Error: %s]" error)))))
522 (delete-region template-start (point))
523 (insert result)))))
525 ;; From the property list
526 (when plist-p
527 (goto-char (point-min))
528 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
529 (unless (org-remember-escaped-%)
530 (and (setq x (or (plist-get org-store-link-plist
531 (intern (match-string 1))) ""))
532 (replace-match x t t)))))
534 ;; Turn on org-mode in the remember buffer, set local variables
535 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1))
536 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
537 (org-set-local 'org-default-notes-file file))
538 (if headline
539 (org-set-local 'org-remember-default-headline headline))
540 (org-set-local 'org-remember-reference-date
541 (list (nth 4 dct) (nth 3 dct) (nth 5 dct)))
542 ;; Interactive template entries
543 (goto-char (point-min))
544 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
545 (unless (org-remember-escaped-%)
546 (setq char (if (match-end 3) (match-string 3))
547 prompt (if (match-end 2) (match-string 2)))
548 (goto-char (match-beginning 0))
549 (replace-match "")
550 (setq completions nil default nil)
551 (when prompt
552 (setq completions (org-split-string prompt "|")
553 prompt (pop completions)
554 default (car completions)
555 histvar (intern (concat
556 "org-remember-template-prompt-history::"
557 (or prompt "")))
558 completions (mapcar 'list completions)))
559 (cond
560 ((member char '("G" "g"))
561 (let* ((org-last-tags-completion-table
562 (org-global-tags-completion-table
563 (if (equal char "G") (org-agenda-files) (and file (list file)))))
564 (org-add-colon-after-tag-completion t)
565 (ins (org-icompleting-read
566 (if prompt (concat prompt ": ") "Tags: ")
567 'org-tags-completion-function nil nil nil
568 'org-tags-history)))
569 (setq ins (mapconcat 'identity
570 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
571 ":"))
572 (when (string-match "\\S-" ins)
573 (or (equal (char-before) ?:) (insert ":"))
574 (insert ins)
575 (or (equal (char-after) ?:) (insert ":")))))
576 ((equal char "C")
577 (cond ((= (length clipboards) 1) (insert (car clipboards)))
578 ((> (length clipboards) 1)
579 (insert (read-string "Clipboard/kill value: "
580 (car clipboards) '(clipboards . 1)
581 (car clipboards))))))
582 ((equal char "L")
583 (cond ((= (length clipboards) 1)
584 (org-insert-link 0 (car clipboards)))
585 ((> (length clipboards) 1)
586 (org-insert-link 0 (read-string "Clipboard/kill value: "
587 (car clipboards)
588 '(clipboards . 1)
589 (car clipboards))))))
590 ((equal char "p")
591 (let*
592 ((prop (org-substring-no-properties prompt))
593 (pall (concat prop "_ALL"))
594 (allowed
595 (with-current-buffer
596 (or (find-buffer-visiting file)
597 (find-file-noselect file))
598 (or (cdr (assoc pall org-file-properties))
599 (cdr (assoc pall org-global-properties))
600 (cdr (assoc pall org-global-properties-fixed)))))
601 (existing (with-current-buffer
602 (or (find-buffer-visiting file)
603 (find-file-noselect file))
604 (mapcar 'list (org-property-values prop))))
605 (propprompt (concat "Value for " prop ": "))
606 (val (if allowed
607 (org-completing-read
608 propprompt
609 (mapcar 'list (org-split-string allowed "[ \t]+"))
610 nil 'req-match)
611 (org-completing-read-no-i propprompt existing nil nil
612 "" nil ""))))
613 (org-set-property prop val)))
614 (char
615 ;; These are the date/time related ones
616 (setq org-time-was-given (equal (upcase char) char))
617 (setq time (org-read-date (equal (upcase char) "U") t nil
618 prompt))
619 (org-insert-time-stamp time org-time-was-given
620 (member char '("u" "U"))
621 nil nil (list org-end-time-was-given)))
623 (let (org-completion-use-ido)
624 (insert (org-without-partial-completion
625 (org-completing-read-no-i
626 (concat (if prompt prompt "Enter string")
627 (if default (concat " [" default "]"))
628 ": ")
629 completions nil nil nil histvar default))))))))
631 (goto-char (point-min))
632 (if (re-search-forward "%\\?" nil t)
633 (replace-match "")
634 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
635 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1)))
636 (when (save-excursion
637 (goto-char (point-min))
638 (re-search-forward "%&" nil t))
639 (replace-match "")
640 (org-set-local 'org-jump-to-target-location t))
641 (when org-remember-backup-directory
642 (unless (file-directory-p org-remember-backup-directory)
643 (make-directory org-remember-backup-directory))
644 (org-set-local 'auto-save-file-name-transforms nil)
645 (setq buffer-file-name
646 (expand-file-name
647 (format-time-string "remember-%Y-%m-%d-%H-%M-%S")
648 org-remember-backup-directory))
649 (save-buffer)
650 (org-set-local 'auto-save-visited-file-name t)
651 (auto-save-mode 1))
652 (when (save-excursion
653 (goto-char (point-min))
654 (re-search-forward "%!" nil t))
655 (replace-match "")
656 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
658 (defun org-remember-escaped-% ()
659 (if (equal (char-before (match-beginning 0)) ?\\)
660 (progn
661 (delete-region (1- (match-beginning 0)) (match-beginning 0))
663 nil))
666 (defun org-remember-finish-immediately ()
667 "File remember note immediately.
668 This should be run in `post-command-hook' and will remove itself
669 from that hook."
670 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
671 (org-remember-finalize))
673 (defun org-remember-visit-immediately ()
674 "File remember note immediately.
675 This should be run in `post-command-hook' and will remove itself
676 from that hook."
677 (org-remember '(16))
678 (goto-char (or (text-property-any
679 (point) (save-excursion (org-end-of-subtree t t))
680 'org-position-cursor t)
681 (point)))
682 (message "%s"
683 (format
684 (substitute-command-keys
685 "Restore window configuration with \\[jump-to-register] %c")
686 remember-register)))
688 (defvar org-clock-marker) ; Defined in org.el
689 (defun org-remember-finalize ()
690 "Finalize the remember process."
691 (interactive)
692 (unless org-remember-mode
693 (error "This does not seem to be a remember buffer for Org-mode"))
694 (run-hooks 'org-remember-before-finalize-hook)
695 (unless (fboundp 'remember-finalize)
696 (defalias 'remember-finalize 'remember-buffer))
697 (when (and org-clock-marker
698 (equal (marker-buffer org-clock-marker) (current-buffer)))
699 ;; the clock is running in this buffer.
700 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
701 (or (eq org-remember-clock-out-on-exit t)
702 (and org-remember-clock-out-on-exit
703 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
704 (let (org-log-note-clock-out) (org-clock-out))))
705 (when buffer-file-name
706 (do-auto-save))
707 (remember-finalize))
709 (defun org-remember-kill ()
710 "Abort the current remember process."
711 (interactive)
712 (let ((org-note-abort t))
713 (org-remember-finalize)))
715 ;;;###autoload
716 (defun org-remember (&optional goto org-force-remember-template-char)
717 "Call `remember'. If this is already a remember buffer, re-apply template.
718 If there is an active region, make sure remember uses it as initial content
719 of the remember buffer.
721 When called interactively with a `C-u' prefix argument GOTO, don't remember
722 anything, just go to the file/headline where the selected template usually
723 stores its notes. With a double prefix arg `C-u C-u', go to the last
724 note stored by remember.
726 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
727 associated with a template in `org-remember-templates'."
728 (interactive "P")
729 (org-require-remember)
730 (cond
731 ((equal goto '(4)) (org-go-to-remember-target))
732 ((equal goto '(16)) (org-remember-goto-last-stored))
734 ;; set temporary variables that will be needed in
735 ;; `org-select-remember-template'
736 (setq org-select-template-temp-major-mode major-mode)
737 (setq org-select-template-original-buffer (current-buffer))
738 (if org-remember-mode
739 (progn
740 (when (< (length org-remember-templates) 2)
741 (error "No other template available"))
742 (erase-buffer)
743 (let ((annotation (plist-get org-store-link-plist :annotation))
744 (initial (plist-get org-store-link-plist :initial)))
745 (org-remember-apply-template))
746 (message "Press C-c C-c to remember data"))
747 (if (org-region-active-p)
748 (org-do-remember (buffer-substring (point) (mark)))
749 (org-do-remember))))))
751 (defvar org-remember-last-stored-marker (make-marker)
752 "Marker pointing to the entry most recently stored with `org-remember'.")
754 (defun org-remember-goto-last-stored ()
755 "Go to the location where the last remember note was stored."
756 (interactive)
757 (org-goto-marker-or-bmk org-remember-last-stored-marker
758 "org-remember-last-stored")
759 (message "This is the last note stored by remember"))
761 (defun org-go-to-remember-target (&optional template-key)
762 "Go to the target location of a remember template.
763 The user is queried for the template."
764 (interactive)
765 (let* (org-select-template-temp-major-mode
766 (entry (org-select-remember-template template-key))
767 (file (nth 1 entry))
768 (heading (nth 2 entry))
769 visiting)
770 (unless (and file (stringp file) (string-match "\\S-" file))
771 (setq file org-default-notes-file))
772 (when (and file (not (file-name-absolute-p file)))
773 (setq file (expand-file-name file org-directory)))
774 (unless (and heading (stringp heading) (string-match "\\S-" heading))
775 (setq heading org-remember-default-headline))
776 (setq visiting (org-find-base-buffer-visiting file))
777 (if (not visiting) (find-file-noselect file))
778 (switch-to-buffer (or visiting (get-file-buffer file)))
779 (widen)
780 (goto-char (point-min))
781 (if (re-search-forward
782 (format org-complex-heading-regexp-format (regexp-quote heading))
783 nil t)
784 (goto-char (match-beginning 0))
785 (error "Target headline not found: %s" heading))))
787 ;; FIXME (bzg): let's clean up of final empty lines happen only once
788 ;; (see the org-remember-delete-empty-lines-at-end option below)
789 ;;;###autoload
790 (defun org-remember-handler ()
791 "Store stuff from remember.el into an org file.
792 When the template has specified a file and a headline, the entry is filed
793 there, or in the location defined by `org-default-notes-file' and
794 `org-remember-default-headline'.
796 If no defaults have been defined, or if the current prefix argument
797 is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
798 process is used to select the target location.
800 When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
801 the entry is filed to the same location as the previous note.
803 When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
804 the entry is filed as a subentry of the entry where the clock is
805 currently running.
807 When `C-u' has been used as prefix argument, the note is stored and emacs
808 moves point to the new location of the note, so that editing can be
809 continued there (similar to inserting \"%&\" into the template).
811 Before storing the note, the function ensures that the text has an
812 org-mode-style headline, i.e. a first line that starts with
813 a \"*\". If not, a headline is constructed from the current date and
814 some additional data.
816 If the variable `org-adapt-indentation' is non-nil, the entire text is
817 also indented so that it starts in the same column as the headline
818 \(i.e. after the stars).
820 See also the variable `org-reverse-note-order'."
821 (when (and (equal current-prefix-arg 2)
822 (not (marker-buffer org-clock-marker)))
823 (error "No running clock"))
824 (when (org-bound-and-true-p org-jump-to-target-location)
825 (let* ((end (min (point-max) (1+ (point))))
826 (beg (point)))
827 (if (= end beg) (setq beg (1- beg)))
828 (put-text-property beg end 'org-position-cursor t)))
829 (goto-char (point-min))
830 (while (looking-at "^[ \t]*\n\\|^##.*\n")
831 (replace-match ""))
832 (when org-remember-delete-empty-lines-at-end
833 (goto-char (point-max))
834 (beginning-of-line 1)
835 (while (and (looking-at "[ \t]*$\\|##.*") (> (point) 1))
836 (delete-region (1- (point)) (point-max))
837 (beginning-of-line 1)))
838 (catch 'quit
839 (if org-note-abort (throw 'quit t))
840 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
841 (backup-file
842 (and buffer-file-name
843 (equal (file-name-directory buffer-file-name)
844 (file-name-as-directory
845 (expand-file-name org-remember-backup-directory)))
846 (string-match "^remember-[0-9]\\{4\\}"
847 (file-name-nondirectory buffer-file-name))
848 buffer-file-name))
850 (dummy
851 (unless (string-match "\\S-" (buffer-string))
852 (message "Nothing to remember")
853 (and backup-file
854 (ignore-errors
855 (delete-file backup-file)
856 (delete-file (concat backup-file "~"))))
857 (set-buffer-modified-p nil)
858 (throw 'quit t)))
859 (reference-date org-remember-reference-date)
860 (previousp (and (member current-prefix-arg '((16) 0))
861 org-remember-previous-location))
862 (clockp (equal current-prefix-arg 2))
863 (fastp (org-xor (equal current-prefix-arg 1)
864 org-remember-store-without-prompt))
865 (file (cond
866 (fastp org-default-notes-file)
867 ((and (eq org-remember-interactive-interface 'refile)
868 org-refile-targets)
869 org-default-notes-file)
870 ((not previousp)
871 (org-get-org-file))))
872 (heading org-remember-default-headline)
873 (visiting (and file (org-find-base-buffer-visiting file)))
874 (org-startup-folded nil)
875 (org-startup-align-all-tables nil)
876 (org-goto-start-pos 1)
877 spos exitcmd level reversed txt text-before-node-creation)
878 (when (equal current-prefix-arg '(4))
879 (setq visitp t))
880 (when previousp
881 (setq file (car org-remember-previous-location)
882 visiting (and file (org-find-base-buffer-visiting file))
883 heading (cdr org-remember-previous-location)
884 fastp t))
885 (when clockp
886 (setq file (buffer-file-name (marker-buffer org-clock-marker))
887 visiting (and file (org-find-base-buffer-visiting file))
888 heading org-clock-heading-for-remember
889 fastp t))
890 (setq current-prefix-arg nil)
891 ;; Modify text so that it becomes a nice subtree which can be inserted
892 ;; into an org tree.
893 (when org-remember-delete-empty-lines-at-end
894 (goto-char (point-min))
895 (if (re-search-forward "[ \t\n]+\\'" nil t)
896 ;; remove empty lines at end
897 (replace-match "")))
898 (goto-char (point-min))
899 (setq text-before-node-creation (buffer-string))
900 (unless (looking-at org-outline-regexp)
901 ;; add a headline
902 (insert (concat "* " (current-time-string)
903 " (" (remember-buffer-desc) ")\n"))
904 (backward-char 1)
905 (when org-adapt-indentation
906 (while (re-search-forward "^" nil t)
907 (insert " "))))
908 ;; Delete final empty lines
909 (when org-remember-delete-empty-lines-at-end
910 (goto-char (point-min))
911 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
912 (replace-match "\n\n")
913 (if (re-search-forward "[ \t\n]*\\'")
914 (replace-match "\n"))))
915 (goto-char (point-min))
916 (setq txt (buffer-string))
917 (org-save-markers-in-region (point-min) (point-max))
918 (set-buffer-modified-p nil)
919 (when (and (eq org-remember-interactive-interface 'refile)
920 (not fastp))
921 (org-refile nil (or visiting (find-file-noselect file)))
922 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
923 (save-excursion
924 (bookmark-jump "org-refile-last-stored")
925 (bookmark-set "org-remember-last-stored")
926 (move-marker org-remember-last-stored-marker (point)))
927 (throw 'quit t))
928 ;; Find the file
929 (with-current-buffer (or visiting (find-file-noselect file))
930 (unless (or (org-mode-p) (member heading '(top bottom)))
931 (error "Target files for notes must be in Org-mode if not filing to top/bottom"))
932 (save-excursion
933 (save-restriction
934 (widen)
935 (setq reversed (org-notes-order-reversed-p))
937 ;; Find the default location
938 (when heading
939 (cond
940 ((not (org-mode-p))
941 (if (eq heading 'top)
942 (goto-char (point-min))
943 (goto-char (point-max))
944 (or (bolp) (newline)))
945 (insert text-before-node-creation)
946 (when remember-save-after-remembering
947 (save-buffer)
948 (if (not visiting) (kill-buffer (current-buffer))))
949 (throw 'quit t))
950 ((eq heading 'top)
951 (goto-char (point-min))
952 (or (looking-at org-outline-regexp)
953 (re-search-forward org-outline-regexp nil t))
954 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
955 ((eq heading 'bottom)
956 (goto-char (point-max))
957 (or (bolp) (newline))
958 (setq org-goto-start-pos (point)))
959 ((eq heading 'date-tree)
960 (org-datetree-find-date-create reference-date)
961 (setq reversed nil)
962 (setq org-goto-start-pos (point)))
963 ((and (stringp heading) (string-match "\\S-" heading))
964 (goto-char (point-min))
965 (if (re-search-forward
966 (format org-complex-heading-regexp-format
967 (regexp-quote heading))
968 nil t)
969 (setq org-goto-start-pos (match-beginning 0))
970 (when fastp
971 (goto-char (point-max))
972 (unless (bolp) (newline))
973 (insert "* " heading "\n")
974 (setq org-goto-start-pos (point-at-bol 0)))))
975 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
976 heading 'top))))
978 ;; Ask the User for a location, using the appropriate interface
979 (cond
980 ((and fastp (memq heading '(top bottom)))
981 (setq spos org-goto-start-pos
982 exitcmd (if (eq heading 'top) 'left nil)))
983 (fastp (setq spos org-goto-start-pos
984 exitcmd 'return))
985 ((eq org-remember-interactive-interface 'outline)
986 (setq spos (org-get-location (current-buffer)
987 org-remember-help)
988 exitcmd (cdr spos)
989 spos (car spos)))
990 ((eq org-remember-interactive-interface 'outline-path-completion)
991 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
992 (org-refile-use-outline-path t))
993 (setq spos (org-refile-get-location "Heading: ")
994 exitcmd 'return
995 spos (nth 3 spos))))
996 (t (error "This should not happen")))
997 (if (not spos) (throw 'quit nil)) ; return nil to show we did
998 ; not handle this note
999 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
1000 (goto-char spos)
1001 (cond ((org-on-heading-p t)
1002 (org-back-to-heading t)
1003 (setq level (funcall outline-level))
1004 (cond
1005 ((eq exitcmd 'return)
1006 ;; sublevel of current
1007 (setq org-remember-previous-location
1008 (cons (abbreviate-file-name file)
1009 (org-get-heading 'notags)))
1010 (if reversed
1011 (outline-next-heading)
1012 (org-end-of-subtree t)
1013 (if (not (bolp))
1014 (if (looking-at "[ \t]*\n")
1015 (beginning-of-line 2)
1016 (end-of-line 1)
1017 (insert "\n"))))
1018 (org-paste-subtree (org-get-valid-level level 1) txt)
1019 (and org-auto-align-tags (org-set-tags nil t))
1020 (bookmark-set "org-remember-last-stored")
1021 (move-marker org-remember-last-stored-marker (point)))
1022 ((eq exitcmd 'left)
1023 ;; before current
1024 (org-paste-subtree level txt)
1025 (and org-auto-align-tags (org-set-tags nil t))
1026 (bookmark-set "org-remember-last-stored")
1027 (move-marker org-remember-last-stored-marker (point)))
1028 ((eq exitcmd 'right)
1029 ;; after current
1030 (org-end-of-subtree t)
1031 (org-paste-subtree level txt)
1032 (and org-auto-align-tags (org-set-tags nil t))
1033 (bookmark-set "org-remember-last-stored")
1034 (move-marker org-remember-last-stored-marker (point)))
1035 (t (error "This should not happen"))))
1037 ((eq heading 'bottom)
1038 (org-paste-subtree 1 txt)
1039 (and org-auto-align-tags (org-set-tags nil t))
1040 (bookmark-set "org-remember-last-stored")
1041 (move-marker org-remember-last-stored-marker (point)))
1043 ((and (bobp) (not reversed))
1044 ;; Put it at the end, one level below level 1
1045 (save-restriction
1046 (widen)
1047 (goto-char (point-max))
1048 (if (not (bolp)) (newline))
1049 (org-paste-subtree (org-get-valid-level 1 1) txt)
1050 (and org-auto-align-tags (org-set-tags nil t))
1051 (bookmark-set "org-remember-last-stored")
1052 (move-marker org-remember-last-stored-marker (point))))
1054 ((and (bobp) reversed)
1055 ;; Put it at the start, as level 1
1056 (save-restriction
1057 (widen)
1058 (goto-char (point-min))
1059 (re-search-forward "^\\*+ " nil t)
1060 (beginning-of-line 1)
1061 (org-paste-subtree 1 txt)
1062 (and org-auto-align-tags (org-set-tags nil t))
1063 (bookmark-set "org-remember-last-stored")
1064 (move-marker org-remember-last-stored-marker (point))))
1066 ;; Put it right there, with automatic level determined by
1067 ;; org-paste-subtree or from prefix arg
1068 (org-paste-subtree
1069 (if (numberp current-prefix-arg) current-prefix-arg)
1070 txt)
1071 (and org-auto-align-tags (org-set-tags nil t))
1072 (bookmark-set "org-remember-last-stored")
1073 (move-marker org-remember-last-stored-marker (point))))
1075 (when remember-save-after-remembering
1076 (save-buffer)
1077 (if (and (not visiting)
1078 (not (equal (marker-buffer org-clock-marker)
1079 (current-buffer))))
1080 (kill-buffer (current-buffer))))
1081 (when org-remember-auto-remove-backup-files
1082 (when backup-file
1083 (ignore-errors
1084 (delete-file backup-file)
1085 (delete-file (concat backup-file "~"))))
1086 (when org-remember-backup-directory
1087 (let ((n (length
1088 (directory-files
1089 org-remember-backup-directory nil
1090 "^remember-.*[0-9]$"))))
1091 (when (and org-remember-warn-about-backups
1092 (> n 0))
1093 (message
1094 "%d backup files (unfinished remember calls) in %s"
1095 n org-remember-backup-directory))))))))))
1097 t) ;; return t to indicate that we took care of this note.
1099 (defun org-do-remember (&optional initial)
1100 "Call remember."
1101 (remember initial))
1103 (defun org-require-remember ()
1104 "Make sure remember is loaded, or install our own emergency version of it."
1105 (condition-case nil
1106 (require 'remember)
1107 (error
1108 ;; Lets install our own micro version of remember
1109 (defvar remember-register ?R)
1110 (defvar remember-mode-hook nil)
1111 (defvar remember-handler-functions nil)
1112 (defvar remember-buffer "*Remember*")
1113 (defvar remember-save-after-remembering t)
1114 (defvar remember-annotation-functions '(buffer-file-name))
1115 (defun remember-finalize ()
1116 (run-hook-with-args-until-success 'remember-handler-functions)
1117 (when (equal remember-buffer (buffer-name))
1118 (kill-buffer (current-buffer))
1119 (jump-to-register remember-register)))
1120 (defun remember-mode ()
1121 (fundamental-mode)
1122 (setq mode-name "Remember")
1123 (run-hooks 'remember-mode-hook))
1124 (defun remember (&optional initial)
1125 (window-configuration-to-register remember-register)
1126 (let* ((annotation (run-hook-with-args-until-success
1127 'remember-annotation-functions)))
1128 (switch-to-buffer-other-window (get-buffer-create remember-buffer))
1129 (remember-mode)))
1130 (defun remember-buffer-desc ()
1131 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
1132 (point-at-eol)))))))
1134 (provide 'org-remember)
1136 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
1138 ;;; org-remember.el ends here