org-edit-src: now saves window layout when editing source code
[rgr-org-mode.git] / lisp / org-remember.el
blobae83bec77f28a75df58d6c53429aa7e526578b59
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.32trans
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 (or (find-buffer-visiting file) (current-buffer))
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 (format org-complex-heading-regexp-format (regexp-quote heading))
775 nil t)
776 (goto-char (match-beginning 0))
777 (error "Target headline not found: %s" heading))))
779 ;; FIXME (bzg): let's clean up of final empty lines happen only once
780 ;; (see the org-remember-delete-empty-lines-at-end option below)
781 ;;;###autoload
782 (defun org-remember-handler ()
783 "Store stuff from remember.el into an org file.
784 When the template has specified a file and a headline, the entry is filed
785 there, or in the location defined by `org-default-notes-file' and
786 `org-remember-default-headline'.
788 If no defaults have been defined, or if the current prefix argument
789 is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
790 process is used to select the target location.
792 When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
793 the entry is filed to the same location as the previous note.
795 When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
796 the entry is filed as a subentry of the entry where the clock is
797 currently running.
799 When `C-u' has been used as prefix argument, the note is stored and emacs
800 moves point to the new location of the note, so that editing can be
801 continued there (similar to inserting \"%&\" into the template).
803 Before storing the note, the function ensures that the text has an
804 org-mode-style headline, i.e. a first line that starts with
805 a \"*\". If not, a headline is constructed from the current date and
806 some additional data.
808 If the variable `org-adapt-indentation' is non-nil, the entire text is
809 also indented so that it starts in the same column as the headline
810 \(i.e. after the stars).
812 See also the variable `org-reverse-note-order'."
813 (when (and (equal current-prefix-arg 2)
814 (not (marker-buffer org-clock-marker)))
815 (error "No running clock"))
816 (when (org-bound-and-true-p org-jump-to-target-location)
817 (let* ((end (min (point-max) (1+ (point))))
818 (beg (point)))
819 (if (= end beg) (setq beg (1- beg)))
820 (put-text-property beg end 'org-position-cursor t)))
821 (goto-char (point-min))
822 (while (looking-at "^[ \t]*\n\\|^##.*\n")
823 (replace-match ""))
824 (when org-remember-delete-empty-lines-at-end
825 (goto-char (point-max))
826 (beginning-of-line 1)
827 (while (and (looking-at "[ \t]*$\\|##.*") (> (point) 1))
828 (delete-region (1- (point)) (point-max))
829 (beginning-of-line 1)))
830 (catch 'quit
831 (if org-note-abort (throw 'quit t))
832 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
833 (backup-file
834 (and buffer-file-name
835 (equal (file-name-directory buffer-file-name)
836 (file-name-as-directory
837 (expand-file-name org-remember-backup-directory)))
838 (string-match "^remember-[0-9]\\{4\\}"
839 (file-name-nondirectory buffer-file-name))
840 buffer-file-name))
842 (dummy
843 (unless (string-match "\\S-" (buffer-string))
844 (message "Nothing to remember")
845 (and backup-file
846 (ignore-errors
847 (delete-file backup-file)
848 (delete-file (concat backup-file "~"))))
849 (set-buffer-modified-p nil)
850 (throw 'quit t)))
851 (previousp (and (member current-prefix-arg '((16) 0))
852 org-remember-previous-location))
853 (clockp (equal current-prefix-arg 2))
854 (fastp (org-xor (equal current-prefix-arg 1)
855 org-remember-store-without-prompt))
856 (file (cond
857 (fastp org-default-notes-file)
858 ((and (eq org-remember-interactive-interface 'refile)
859 org-refile-targets)
860 org-default-notes-file)
861 ((not previousp)
862 (org-get-org-file))))
863 (heading org-remember-default-headline)
864 (visiting (and file (org-find-base-buffer-visiting file)))
865 (org-startup-folded nil)
866 (org-startup-align-all-tables nil)
867 (org-goto-start-pos 1)
868 spos exitcmd level reversed txt text-before-node-creation)
869 (when (equal current-prefix-arg '(4))
870 (setq visitp t))
871 (when previousp
872 (setq file (car org-remember-previous-location)
873 visiting (and file (org-find-base-buffer-visiting file))
874 heading (cdr org-remember-previous-location)
875 fastp t))
876 (when clockp
877 (setq file (buffer-file-name (marker-buffer org-clock-marker))
878 visiting (and file (org-find-base-buffer-visiting file))
879 heading org-clock-heading-for-remember
880 fastp t))
881 (setq current-prefix-arg nil)
882 ;; Modify text so that it becomes a nice subtree which can be inserted
883 ;; into an org tree.
884 (when org-remember-delete-empty-lines-at-end
885 (goto-char (point-min))
886 (if (re-search-forward "[ \t\n]+\\'" nil t)
887 ;; remove empty lines at end
888 (replace-match "")))
889 (goto-char (point-min))
890 (setq text-before-node-creation (buffer-string))
891 (unless (looking-at org-outline-regexp)
892 ;; add a headline
893 (insert (concat "* " (current-time-string)
894 " (" (remember-buffer-desc) ")\n"))
895 (backward-char 1)
896 (when org-adapt-indentation
897 (while (re-search-forward "^" nil t)
898 (insert " "))))
899 ;; Delete final empty lines
900 (when org-remember-delete-empty-lines-at-end
901 (goto-char (point-min))
902 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
903 (replace-match "\n\n")
904 (if (re-search-forward "[ \t\n]*\\'")
905 (replace-match "\n"))))
906 (goto-char (point-min))
907 (setq txt (buffer-string))
908 (org-save-markers-in-region (point-min) (point-max))
909 (set-buffer-modified-p nil)
910 (when (and (eq org-remember-interactive-interface 'refile)
911 (not fastp))
912 (org-refile nil (or visiting (find-file-noselect file)))
913 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
914 (save-excursion
915 (bookmark-jump "org-refile-last-stored")
916 (bookmark-set "org-remember-last-stored")
917 (move-marker org-remember-last-stored-marker (point)))
918 (throw 'quit t))
919 ;; Find the file
920 (with-current-buffer (or visiting (find-file-noselect file))
921 (unless (or (org-mode-p) (member heading '(top bottom)))
922 (error "Target files for notes must be in Org-mode if not filing to top/bottom"))
923 (save-excursion
924 (save-restriction
925 (widen)
926 (setq reversed (org-notes-order-reversed-p))
928 ;; Find the default location
929 (when heading
930 (cond
931 ((not (org-mode-p))
932 (if (eq heading 'top)
933 (goto-char (point-min))
934 (goto-char (point-max))
935 (or (bolp) (newline)))
936 (insert text-before-node-creation)
937 (when remember-save-after-remembering
938 (save-buffer)
939 (if (not visiting) (kill-buffer (current-buffer))))
940 (throw 'quit t))
941 ((eq heading 'top)
942 (goto-char (point-min))
943 (or (looking-at org-outline-regexp)
944 (re-search-forward org-outline-regexp nil t))
945 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
946 ((eq heading 'bottom)
947 (goto-char (point-max))
948 (or (bolp) (newline))
949 (setq org-goto-start-pos (point)))
950 ((and (stringp heading) (string-match "\\S-" heading))
951 (goto-char (point-min))
952 (if (re-search-forward
953 (format org-complex-heading-regexp-format
954 (regexp-quote heading))
955 nil t)
956 (setq org-goto-start-pos (match-beginning 0))
957 (when fastp
958 (goto-char (point-max))
959 (unless (bolp) (newline))
960 (insert "* " heading "\n")
961 (setq org-goto-start-pos (point-at-bol 0)))))
962 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
963 heading 'top))))
965 ;; Ask the User for a location, using the appropriate interface
966 (cond
967 ((and fastp (memq heading '(top bottom)))
968 (setq spos org-goto-start-pos
969 exitcmd (if (eq heading 'top) 'left nil)))
970 (fastp (setq spos org-goto-start-pos
971 exitcmd 'return))
972 ((eq org-remember-interactive-interface 'outline)
973 (setq spos (org-get-location (current-buffer)
974 org-remember-help)
975 exitcmd (cdr spos)
976 spos (car spos)))
977 ((eq org-remember-interactive-interface 'outline-path-completion)
978 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
979 (org-refile-use-outline-path t))
980 (setq spos (org-refile-get-location "Heading: ")
981 exitcmd 'return
982 spos (nth 3 spos))))
983 (t (error "This should not happen")))
984 (if (not spos) (throw 'quit nil)) ; return nil to show we did
985 ; not handle this note
986 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
987 (goto-char spos)
988 (cond ((org-on-heading-p t)
989 (org-back-to-heading t)
990 (setq level (funcall outline-level))
991 (cond
992 ((eq exitcmd 'return)
993 ;; sublevel of current
994 (setq org-remember-previous-location
995 (cons (abbreviate-file-name file)
996 (org-get-heading 'notags)))
997 (if reversed
998 (outline-next-heading)
999 (org-end-of-subtree t)
1000 (if (not (bolp))
1001 (if (looking-at "[ \t]*\n")
1002 (beginning-of-line 2)
1003 (end-of-line 1)
1004 (insert "\n"))))
1005 (org-paste-subtree (org-get-valid-level level 1) txt)
1006 (and org-auto-align-tags (org-set-tags nil t))
1007 (bookmark-set "org-remember-last-stored")
1008 (move-marker org-remember-last-stored-marker (point)))
1009 ((eq exitcmd 'left)
1010 ;; before current
1011 (org-paste-subtree level txt)
1012 (and org-auto-align-tags (org-set-tags nil t))
1013 (bookmark-set "org-remember-last-stored")
1014 (move-marker org-remember-last-stored-marker (point)))
1015 ((eq exitcmd 'right)
1016 ;; after current
1017 (org-end-of-subtree t)
1018 (org-paste-subtree level 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 (t (error "This should not happen"))))
1024 ((eq heading 'bottom)
1025 (org-paste-subtree 1 txt)
1026 (and org-auto-align-tags (org-set-tags nil t))
1027 (bookmark-set "org-remember-last-stored")
1028 (move-marker org-remember-last-stored-marker (point)))
1030 ((and (bobp) (not reversed))
1031 ;; Put it at the end, one level below level 1
1032 (save-restriction
1033 (widen)
1034 (goto-char (point-max))
1035 (if (not (bolp)) (newline))
1036 (org-paste-subtree (org-get-valid-level 1 1) txt)
1037 (and org-auto-align-tags (org-set-tags nil t))
1038 (bookmark-set "org-remember-last-stored")
1039 (move-marker org-remember-last-stored-marker (point))))
1041 ((and (bobp) reversed)
1042 ;; Put it at the start, as level 1
1043 (save-restriction
1044 (widen)
1045 (goto-char (point-min))
1046 (re-search-forward "^\\*+ " nil t)
1047 (beginning-of-line 1)
1048 (org-paste-subtree 1 txt)
1049 (and org-auto-align-tags (org-set-tags nil t))
1050 (bookmark-set "org-remember-last-stored")
1051 (move-marker org-remember-last-stored-marker (point))))
1053 ;; Put it right there, with automatic level determined by
1054 ;; org-paste-subtree or from prefix arg
1055 (org-paste-subtree
1056 (if (numberp current-prefix-arg) current-prefix-arg)
1057 txt)
1058 (and org-auto-align-tags (org-set-tags nil t))
1059 (bookmark-set "org-remember-last-stored")
1060 (move-marker org-remember-last-stored-marker (point))))
1062 (when remember-save-after-remembering
1063 (save-buffer)
1064 (if (and (not visiting)
1065 (not (equal (marker-buffer org-clock-marker)
1066 (current-buffer))))
1067 (kill-buffer (current-buffer))))
1068 (when org-remember-auto-remove-backup-files
1069 (when backup-file
1070 (ignore-errors
1071 (delete-file backup-file)
1072 (delete-file (concat backup-file "~"))))
1073 (when org-remember-backup-directory
1074 (let ((n (length
1075 (directory-files
1076 org-remember-backup-directory nil
1077 "^remember-.*[0-9]$"))))
1078 (when (and org-remember-warn-about-backups
1079 (> n 0))
1080 (message
1081 "%d backup files (unfinished remember calls) in %s"
1082 n org-remember-backup-directory))))))))))
1084 t) ;; return t to indicate that we took care of this note.
1086 (defun org-do-remember (&optional initial)
1087 "Call remember."
1088 (remember initial))
1090 (defun org-require-remember ()
1091 "Make sure remember is loaded, or install our own emergency version of it."
1092 (condition-case nil
1093 (require 'remember)
1094 (error
1095 ;; Lets install our own micro version of remember
1096 (defvar remember-register ?R)
1097 (defvar remember-mode-hook nil)
1098 (defvar remember-handler-functions nil)
1099 (defvar remember-buffer "*Remember*")
1100 (defvar remember-save-after-remembering t)
1101 (defvar remember-annotation-functions '(buffer-file-name))
1102 (defun remember-finalize ()
1103 (run-hook-with-args-until-success 'remember-handler-functions)
1104 (when (equal remember-buffer (buffer-name))
1105 (kill-buffer (current-buffer))
1106 (jump-to-register remember-register)))
1107 (defun remember-mode ()
1108 (fundamental-mode)
1109 (setq mode-name "Remember")
1110 (run-hooks 'remember-mode-hook))
1111 (defun remember (&optional initial)
1112 (window-configuration-to-register remember-register)
1113 (let* ((annotation (run-hook-with-args-until-success
1114 'remember-annotation-functions)))
1115 (switch-to-buffer-other-window (get-buffer-create remember-buffer))
1116 (remember-mode)))
1117 (defun remember-buffer-desc ()
1118 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
1119 (point-at-eol)))))))
1121 (provide 'org-remember)
1123 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
1125 ;;; org-remember.el ends here