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