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