1 ;;; org-remember.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains the system to take fast notes with Org-mode.
29 ;; This system is used together with John Wiegley's `remember.el'.
36 (require 'org-datetree
)
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."
55 (defcustom org-remember-store-without-prompt t
56 "Non-nil means \\<org-remember-mode-map>\\[org-remember-finalize] \
57 stores the remember note without further prompts.
58 It then uses the file and headline specified by the template or (if the
59 template does not specify them) by the variables `org-default-notes-file'
60 and `org-remember-default-headline'. To force prompting anyway, use
61 \\[universal-argument] \\[org-remember-finalize] to file the note.
63 When this variable is nil, \\[org-remember-finalize] gives you the prompts, and
64 \\[universal-argument] \\[org-remember-finalize] triggers the fasttrack."
68 (defcustom org-remember-interactive-interface
'refile
69 "The interface to be used for interactive filing of remember notes.
70 This is only used when the interactive mode for selecting a filing
71 location is used (see the variable `org-remember-store-without-prompt').
73 outline The interface shows an outline of the relevant file
74 and the correct heading is found by moving through
75 the outline or by searching with incremental search.
76 outline-path-completion Headlines in the current buffer are offered via
78 refile Use the refile interface, and offer headlines,
79 possibly from different buffers."
82 (const :tag
"Refile" refile
)
83 (const :tag
"Outline" outline
)
84 (const :tag
"Outline-path-completion" outline-path-completion
)))
86 (defcustom org-remember-default-headline
""
87 "The headline that should be the default location in the notes file.
88 When filing remember notes, the cursor will start at that position.
89 You can set this on a per-template basis with the variable
90 `org-remember-templates'."
94 (defcustom org-remember-templates nil
95 "Templates for the creation of remember buffers.
96 When nil, just let remember make the buffer.
97 When non-nil, this is a list of (up to) 6-element lists. In each entry,
98 the first element is the name of the template, which should be a single
99 short word. The second element is a character, a unique key to select
100 this template. The third element is the template.
102 The fourth element is optional and can specify a destination file for
103 remember items created with this template. The default file is given
104 by `org-default-notes-file'. If the file name is not an absolute path,
105 it will be interpreted relative to `org-directory'.
107 An optional fifth element can specify the headline in that file that should
108 be offered first when the user is asked to file the entry. The default
109 headline is given in the variable `org-remember-default-headline'. When
110 this element is `top' or `bottom', the note will be placed as a level-1
111 entry at the beginning or end of the file, respectively.
113 An optional sixth element specifies the contexts in which the template
114 will be offered to the user. This element can be a list of major modes
115 or a function, and the template will only be offered if `org-remember'
116 is called from a mode in the list, or if the function returns t.
117 Templates that specify t or nil for the context will always be added
118 to the list of selectable templates.
120 The template specifies the structure of the remember buffer. It should have
121 a first line starting with a star, to act as the org-mode headline.
122 Furthermore, the following %-escapes will be replaced with content:
124 %^{PROMPT} prompt the user for a string and replace this sequence with it.
125 A default value and a completion table can be specified like this:
126 %^{prompt|default|completion2|completion3|...}
127 The arrow keys access a prompt-specific history.
128 %a annotation, normally the link created with `org-store-link'
129 %A like %a, but prompt for the description part
130 %i initial content, copied from the active region. If %i is
131 indented, the entire inserted text will be indented as well.
132 %t time stamp, date only
133 %T time stamp with date and time
134 %u, %U like the above, but inactive time stamps
135 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
136 You may define a prompt like %^{Please specify birthday}t
137 %n user name (taken from `user-full-name')
138 %c current kill ring head
139 %x content of the X clipboard
140 %:keyword specific information for certain link types, see below
141 %^C interactive selection of which kill or clip to use
142 %^L like %^C, but insert as link
143 %k title of the currently clocked task
144 %K link to the currently clocked task
145 %^g prompt for tags, completing tags in the target file
146 %^G prompt for tags, completing all tags in all agenda files
147 %^{PROP}p Prompt the user for a value for property PROP
148 %[PATHNAME] insert the contents of the file given by PATHNAME
149 %(SEXP) evaluate elisp `(SEXP)' and replace with the result
150 %! store this note immediately after completing the template\
151 \\<org-remember-mode-map>
152 (skipping the \\[org-remember-finalize] that normally triggers storing)
153 %& jump to target location immediately after storing note
154 %? after completing the template, position cursor here.
156 Apart from these general escapes, you can access information specific to the
157 link type that is created. For example, calling `remember' in emails or gnus
158 will record the author and the subject of the message, which you can access
159 with %:fromname and %:subject, respectively. Here is a complete list of what
160 is recorded for each link type.
162 Link type | Available information
163 -------------------+------------------------------------------------------
164 bbdb | %:type %:name %:company
165 vm, wl, mh, rmail | %:type %:subject %:message-id
166 | %:from %:fromname %:fromaddress
167 | %:to %:toname %:toaddress
168 | %:fromto (either \"to NAME\" or \"from NAME\")
169 gnus | %:group, for messages also all email fields and
170 | %:org-date (the Date: header in Org format)
171 w3, w3m | %:type %:url
172 info | %:type %:file %:node
173 calendar | %:type %:date"
175 :get
(lambda (var) ; Make sure all entries have at least 5 elements
177 (if (not (stringp (car x
))) (setq x
(cons "" x
)))
178 (cond ((= (length x
) 4) (append x
'(nil)))
179 ((= (length x
) 3) (append x
'(nil nil
)))
181 (default-value var
)))
184 (list :value
("" ?a
"\n" nil nil nil
)
186 (character :tag
"Selection Key")
187 (string :tag
"Template")
188 (choice :tag
"Destination file"
189 (file :tag
"Specify")
190 (function :tag
"Function")
191 (const :tag
"Use `org-default-notes-file'" nil
))
192 (choice :tag
"Destin. headline"
193 (string :tag
"Specify")
194 (function :tag
"Function")
195 (const :tag
"Use `org-remember-default-headline'" nil
)
196 (const :tag
"At beginning of file" top
)
197 (const :tag
"At end of file" bottom
)
198 (const :tag
"In a date tree" date-tree
))
199 (choice :tag
"Context"
200 (const :tag
"Use in all contexts" nil
)
201 (const :tag
"Use in all contexts" t
)
202 (repeat :tag
"Use only if in major mode"
203 (symbol :tag
"Major mode"))
204 (function :tag
"Perform a check against function")))))
206 (defcustom org-remember-delete-empty-lines-at-end t
207 "Non-nil means clean up final empty lines in remember buffer."
211 (defcustom org-remember-before-finalize-hook nil
212 "Hook that is run right before a remember process is finalized.
213 The remember buffer is still current when this hook runs."
217 (defvar org-remember-mode-map
(make-sparse-keymap)
218 "Keymap for `org-remember-mode', a minor mode.
219 Use this map to set additional keybindings for when Org-mode is used
220 for a Remember buffer.")
221 (defvar org-remember-mode-hook nil
222 "Hook for the minor `org-remember-mode'.")
224 (define-minor-mode org-remember-mode
225 "Minor mode for special key bindings in a remember buffer."
226 nil
" Rem" org-remember-mode-map
)
227 (define-key org-remember-mode-map
"\C-c\C-c" 'org-remember-finalize
)
228 (define-key org-remember-mode-map
"\C-c\C-k" 'org-remember-kill
)
230 (defcustom org-remember-clock-out-on-exit
'query
231 "Non-nil means stop the clock when exiting a clocking remember buffer.
232 This only applies if the clock is running in the remember buffer. If the
233 clock is not stopped, it continues to run in the storage location.
234 Instead of nil or t, this may also be the symbol `query' to prompt the
235 user each time a remember buffer with a running clock is filed away."
238 (const :tag
"Never" nil
)
239 (const :tag
"Always" t
)
240 (const :tag
"Query user" query
)))
242 (defcustom org-remember-backup-directory nil
243 "Directory where to store all remember buffers, for backup purposes.
244 After a remember buffer has been stored successfully, the backup file
245 will be removed. However, if you forget to finish the remember process,
246 the file will remain there.
247 See also `org-remember-auto-remove-backup-files'."
250 (const :tag
"No backups" nil
)
251 (directory :tag
"Directory")))
253 (defcustom org-remember-auto-remove-backup-files t
254 "Non-nil means remove remember backup files after successfully storage.
255 When remember is finished successfully, with storing the note at the
256 desired target, remove the backup files related to this remember process
257 and show a message about remaining backup files, from previous, unfinished
259 Backup files will only be made at all, when `org-remember-backup-directory'
264 (defcustom org-remember-warn-about-backups t
265 "Non-nil means warn about backup files in `org-remember-backup-directory'.
267 Set this to nil if you find that you don't need the warning.
269 If you cancel remember calls frequently and know when they
270 contain useful information (because you know that you made an
271 error or Emacs crashed, for example) nil is more useful. In the
272 opposite case, the default, t, is more useful."
276 (defvar annotation
) ; from remember.el, dynamically scoped in `remember-mode'
277 (defvar initial
) ; from remember.el, dynamically scoped in `remember-mode'
280 (defun org-remember-insinuate ()
281 "Setup remember.el for use with Org-mode."
282 (org-require-remember)
283 (setq remember-annotation-functions
'(org-remember-annotation))
284 (setq remember-handler-functions
'(org-remember-handler))
285 (add-hook 'remember-mode-hook
'org-remember-apply-template
))
288 (defun org-remember-annotation ()
289 "Return a link to the current location as an annotation for remember.el.
290 If you are using Org-mode files as target for data storage with
291 remember.el, then the annotations should include a link compatible with the
292 conventions in Org-mode. This function returns such a link."
293 (org-store-link nil
))
295 (defconst org-remember-help
296 "Select a destination location for the note.
297 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
298 RET on headline -> Store as sublevel entry to current headline
299 RET at beg-of-buf -> Append to file as level 2 headline
300 <left>/<right> -> before/after current headline, same headings level")
302 (defvar org-jump-to-target-location nil
)
303 (defvar org-remember-previous-location nil
)
304 (defvar org-remember-reference-date nil
)
305 (defvar org-force-remember-template-char
) ;; dynamically scoped
307 ;; Save the major mode of the buffer we called remember from
308 (defvar org-select-template-temp-major-mode nil
)
310 ;; Temporary store the buffer where remember was called from
311 (defvar org-select-template-original-buffer nil
)
313 (defun org-select-remember-template (&optional use-char
)
314 (when org-remember-templates
315 (let* ((pre-selected-templates
318 (let ((ctxt (nth 5 tpl
))
319 (mode org-select-template-temp-major-mode
)
320 (buf org-select-template-original-buffer
))
321 (and (or (not ctxt
) (eq ctxt t
)
322 (and (listp ctxt
) (memq mode ctxt
))
323 (and (functionp ctxt
)
324 (with-current-buffer buf
325 ;; Protect the user-defined function from error
326 (condition-case nil
(funcall ctxt
) (error nil
)))))
328 org-remember-templates
))
329 ;; If no template at this point, add the default templates:
330 (pre-selected-templates1
331 (if (not (delq nil pre-selected-templates
))
332 (mapcar (lambda(x) (if (not (nth 5 x
)) x
))
333 org-remember-templates
)
334 pre-selected-templates
))
335 ;; Then unconditionally add template for any contexts
336 (pre-selected-templates2
337 (append (mapcar (lambda(x) (if (eq (nth 5 x
) t
) x
))
338 org-remember-templates
)
339 (delq nil pre-selected-templates1
)))
340 (templates (mapcar (lambda (x)
341 (if (stringp (car x
))
342 (append (list (nth 1 x
) (car x
)) (cddr x
))
343 (append (list (car x
) "") (cdr x
))))
344 (delq nil pre-selected-templates2
)))
348 ((= (length templates
) 1)
350 ((and (boundp 'org-force-remember-template-char
)
351 org-force-remember-template-char
)
352 (if (stringp org-force-remember-template-char
)
353 (string-to-char org-force-remember-template-char
)
354 org-force-remember-template-char
))
357 "Select template: %s%s"
361 ((not (string-match "\\S-" (nth 1 x
)))
362 (format "[%c]" (car x
)))
363 ((equal (downcase (car x
))
364 (downcase (aref (nth 1 x
) 0)))
365 (format "[%c]%s" (car x
)
366 (substring (nth 1 x
) 1)))
367 (t (format "[%c]%s" (car x
) (nth 1 x
)))))
369 (if (assoc ?C templates
)
371 " [C]customize templates")))
372 (let ((inhibit-quit t
) char0
)
375 (setq char0
(read-char-exclusive))
376 (when (and (not (assoc char0 templates
))
377 (not (equal char0 ?\C-g
))
378 (not (equal char0 ?C
)))
379 (message "No such template \"%c\"" char0
)
382 (when (equal char0 ?\C-g
)
383 (jump-to-register remember-register
)
384 (kill-buffer remember-buffer
)
386 (when (not (assoc char0 templates
))
387 (jump-to-register remember-register
)
388 (kill-buffer remember-buffer
)
389 (customize-variable 'org-remember-templates
)
390 (error "Customize templates"))
392 (cddr (assoc char templates
)))))
395 (defun org-remember-apply-template (&optional use-char skip-interactive
)
396 "Initialize *remember* buffer with template, invoke `org-mode'.
397 This function should be placed into `remember-mode-hook' and in fact requires
398 to be run from that hook to function properly."
399 (when (and (boundp 'initial
) (stringp initial
))
400 (setq initial
(org-no-properties initial
))
401 (remove-text-properties 0 (length initial
) '(read-only t
) initial
))
402 (if org-remember-templates
403 (let* ((entry (org-select-remember-template use-char
))
404 (ct (or org-overriding-default-time
(org-current-time)))
405 (dct (decode-time ct
))
407 (if (< (nth 2 dct
) org-extend-today-until
)
408 (encode-time 0 59 23 (1- (nth 3 dct
)) (nth 4 dct
) (nth 5 dct
))
411 (plist-p (if org-store-link-plist t nil
))
412 (file (if (and (nth 1 entry
)
413 (or (and (stringp (nth 1 entry
))
414 (string-match "\\S-" (nth 1 entry
)))
415 (functionp (nth 1 entry
))))
417 org-default-notes-file
))
418 (headline (nth 2 entry
))
419 (v-c (and (> (length kill-ring
) 0) (current-kill 0)))
420 (v-x (or (org-get-x-clipboard 'PRIMARY
)
421 (org-get-x-clipboard 'CLIPBOARD
)
422 (org-get-x-clipboard 'SECONDARY
)))
423 (v-t (format-time-string (car org-time-stamp-formats
) ct
))
424 (v-T (format-time-string (cdr org-time-stamp-formats
) ct
))
425 (v-u (concat "[" (substring v-t
1 -
1) "]"))
426 (v-U (concat "[" (substring v-T
1 -
1) "]"))
427 ;; `initial' and `annotation' are bound in `remember'.
428 ;; But if the property list has them, we prefer those values
429 (v-i (or (plist-get org-store-link-plist
:initial
)
430 (and (boundp 'initial
) initial
)
432 (v-a (or (plist-get org-store-link-plist
:annotation
)
433 (and (boundp 'annotation
) annotation
)
435 ;; Is the link empty? Then we do not want it...
436 (v-a (if (equal v-a
"[[]]") "" v-a
))
437 (clipboards (remove nil
(list v-i
438 (org-get-x-clipboard 'PRIMARY
)
439 (org-get-x-clipboard 'CLIPBOARD
)
440 (org-get-x-clipboard 'SECONDARY
)
443 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a
))
444 (replace-match "[\\1[%^{Link description}]]" nil nil v-a
)
447 (v-k (if (marker-buffer org-clock-marker
)
448 (org-substring-no-properties org-clock-heading
)))
449 (v-K (if (marker-buffer org-clock-marker
)
450 (org-make-link-string
451 (buffer-file-name (marker-buffer org-clock-marker
))
454 (org-startup-folded nil
)
455 (org-inhibit-startup t
)
456 org-time-was-given org-end-time-was-given x
457 prompt completions char time pos default histvar
)
459 (when (functionp file
)
460 (setq file
(funcall file
)))
461 (when (functionp headline
)
462 (setq headline
(funcall headline
)))
463 (when (and file
(not (file-name-absolute-p file
)))
464 (setq file
(expand-file-name file org-directory
)))
466 (setq org-store-link-plist
467 (plist-put org-store-link-plist
:annotation v-a
)
469 (plist-put org-store-link-plist
:initial v-i
))
471 (unless tpl
(setq tpl
"") (message "No template") (ding) (sit-for 1))
473 (insert (substitute-command-keys
475 "## %s \"%s\" -> \"* %s\"
476 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
477 ## C-0 C-c C-c \"%s\" -> \"* %s\"
478 ## %s to select file and header location interactively.
479 ## C-2 C-c C-c as child (C-3: as sibling) of the currently clocked item
480 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
481 (if org-remember-store-without-prompt
" C-c C-c" " C-1 C-c C-c")
482 (abbreviate-file-name (or file org-default-notes-file
))
484 (or (car org-remember-previous-location
) "???")
485 (or (cdr org-remember-previous-location
) "???")
486 (if org-remember-store-without-prompt
"C-1 C-c C-c" " C-c C-c"))))
489 ;; %[] Insert contents of a file.
490 (goto-char (point-min))
491 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t
)
492 (unless (org-remember-escaped-%
)
493 (let ((start (match-beginning 0))
495 (filename (expand-file-name (match-string 1))))
497 (delete-region start end
)
498 (condition-case error
499 (insert-file-contents filename
)
500 (error (insert (format "%%![Couldn't insert %s: %s]"
501 filename error
)))))))
503 (goto-char (point-min))
504 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t
)
505 (unless (org-remember-escaped-%
)
506 (when (and initial
(equal (match-string 0) "%i"))
508 (let* ((lead (buffer-substring
509 (point-at-bol) (match-beginning 0))))
510 (setq v-i
(mapconcat 'identity
511 (org-split-string initial
"\n")
512 (concat "\n" lead
))))))
514 (or (eval (intern (concat "v-" (match-string 1)))) "")
517 ;; %() embedded elisp
518 (goto-char (point-min))
519 (while (re-search-forward "%\\((.+)\\)" nil t
)
520 (unless (org-remember-escaped-%
)
521 (goto-char (match-beginning 0))
522 (let ((template-start (point)))
525 (condition-case error
526 (eval (read (current-buffer)))
527 (error (format "%%![Error: %s]" error
)))))
528 (delete-region template-start
(point))
531 ;; From the property list
533 (goto-char (point-min))
534 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t
)
535 (unless (org-remember-escaped-%
)
536 (and (setq x
(or (plist-get org-store-link-plist
537 (intern (match-string 1))) ""))
538 (replace-match x t t
)))))
540 ;; Turn on org-mode in the remember buffer, set local variables
541 (let ((org-inhibit-startup t
)) (org-mode) (org-remember-mode 1))
542 (if (and file
(string-match "\\S-" file
) (not (file-directory-p file
)))
543 (org-set-local 'org-default-notes-file file
))
545 (org-set-local 'org-remember-default-headline headline
))
546 (org-set-local 'org-remember-reference-date
547 (list (nth 4 dct
) (nth 3 dct
) (nth 5 dct
)))
548 ;; Interactive template entries
549 (goto-char (point-min))
550 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t
)
551 (unless (org-remember-escaped-%
)
552 (setq char
(if (match-end 3) (match-string 3))
553 prompt
(if (match-end 2) (match-string 2)))
554 (goto-char (match-beginning 0))
556 (setq completions nil default nil
)
558 (setq completions
(org-split-string prompt
"|")
559 prompt
(pop completions
)
560 default
(car completions
)
561 histvar
(intern (concat
562 "org-remember-template-prompt-history::"
564 completions
(mapcar 'list completions
)))
566 ((member char
'("G" "g"))
567 (let* ((org-last-tags-completion-table
568 (org-global-tags-completion-table
569 (if (equal char
"G") (org-agenda-files) (and file
(list file
)))))
570 (org-add-colon-after-tag-completion t
)
571 (ins (org-icompleting-read
572 (if prompt
(concat prompt
": ") "Tags: ")
573 'org-tags-completion-function nil nil nil
575 (setq ins
(mapconcat 'identity
576 (org-split-string ins
(org-re "[^[:alnum:]_@#%]+"))
578 (when (string-match "\\S-" ins
)
579 (or (equal (char-before) ?
:) (insert ":"))
581 (or (equal (char-after) ?
:) (insert ":")))))
583 (cond ((= (length clipboards
) 1) (insert (car clipboards
)))
584 ((> (length clipboards
) 1)
585 (insert (read-string "Clipboard/kill value: "
586 (car clipboards
) '(clipboards .
1)
587 (car clipboards
))))))
589 (cond ((= (length clipboards
) 1)
590 (org-insert-link 0 (car clipboards
)))
591 ((> (length clipboards
) 1)
592 (org-insert-link 0 (read-string "Clipboard/kill value: "
595 (car clipboards
))))))
598 ((prop (org-substring-no-properties prompt
))
599 (pall (concat prop
"_ALL"))
602 (or (find-buffer-visiting file
)
603 (find-file-noselect file
))
604 (or (cdr (assoc pall org-file-properties
))
605 (cdr (assoc pall org-global-properties
))
606 (cdr (assoc pall org-global-properties-fixed
)))))
607 (existing (with-current-buffer
608 (or (find-buffer-visiting file
)
609 (find-file-noselect file
))
610 (mapcar 'list
(org-property-values prop
))))
611 (propprompt (concat "Value for " prop
": "))
615 (mapcar 'list
(org-split-string allowed
"[ \t]+"))
617 (org-completing-read-no-i propprompt existing nil nil
619 (org-set-property prop val
)))
621 ;; These are the date/time related ones
622 (setq org-time-was-given
(equal (upcase char
) char
))
623 (setq time
(org-read-date (equal (upcase char
) "U") t nil
625 (org-insert-time-stamp time org-time-was-given
626 (member char
'("u" "U"))
627 nil nil
(list org-end-time-was-given
)))
629 (let (org-completion-use-ido)
630 (insert (org-without-partial-completion
631 (org-completing-read-no-i
632 (concat (if prompt prompt
"Enter string")
633 (if default
(concat " [" default
"]"))
635 completions nil nil nil histvar default
))))))))
637 (goto-char (point-min))
638 (if (re-search-forward "%\\?" nil t
)
640 (and (re-search-forward "^[^#\n]" nil t
) (backward-char 1))))
641 (let ((org-inhibit-startup t
)) (org-mode) (org-remember-mode 1)))
642 (when (save-excursion
643 (goto-char (point-min))
644 (re-search-forward "%&" nil t
))
646 (org-set-local 'org-jump-to-target-location t
))
647 (when org-remember-backup-directory
648 (unless (file-directory-p org-remember-backup-directory
)
649 (make-directory org-remember-backup-directory
))
650 (org-set-local 'auto-save-file-name-transforms nil
)
651 (setq buffer-file-name
653 (format-time-string "remember-%Y-%m-%d-%H-%M-%S")
654 org-remember-backup-directory
))
656 (org-set-local 'auto-save-visited-file-name t
)
658 (when (save-excursion
659 (goto-char (point-min))
660 (re-search-forward "%!" nil t
))
662 (add-hook 'post-command-hook
'org-remember-finish-immediately
'append
)))
664 (defun org-remember-escaped-%
()
665 (if (equal (char-before (match-beginning 0)) ?
\\)
667 (delete-region (1- (match-beginning 0)) (match-beginning 0))
672 (defun org-remember-finish-immediately ()
673 "File remember note immediately.
674 This should be run in `post-command-hook' and will remove itself
676 (remove-hook 'post-command-hook
'org-remember-finish-immediately
)
677 (org-remember-finalize))
679 (defun org-remember-visit-immediately ()
680 "File remember note immediately.
681 This should be run in `post-command-hook' and will remove itself
684 (goto-char (or (text-property-any
685 (point) (save-excursion (org-end-of-subtree t t
))
686 'org-position-cursor t
)
690 (substitute-command-keys
691 "Restore window configuration with \\[jump-to-register] %c")
694 (defvar org-clock-marker
) ; Defined in org.el
695 (defun org-remember-finalize ()
696 "Finalize the remember process."
698 (unless org-remember-mode
699 (error "This does not seem to be a remember buffer for Org-mode"))
700 (run-hooks 'org-remember-before-finalize-hook
)
701 (unless (fboundp 'remember-finalize
)
702 (defalias 'remember-finalize
'remember-buffer
))
703 (when (and org-clock-marker
704 (equal (marker-buffer org-clock-marker
) (current-buffer)))
705 ;; the clock is running in this buffer.
706 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
707 (or (eq org-remember-clock-out-on-exit t
)
708 (and org-remember-clock-out-on-exit
709 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
710 (let (org-log-note-clock-out) (org-clock-out))))
711 (when buffer-file-name
715 (defun org-remember-kill ()
716 "Abort the current remember process."
718 (let ((org-note-abort t
))
719 (org-remember-finalize)))
722 (defun org-remember (&optional goto org-force-remember-template-char
)
723 "Call `remember'. If this is already a remember buffer, re-apply template.
724 If there is an active region, make sure remember uses it as initial content
725 of the remember buffer.
727 When called interactively with a \\[universal-argument] \
728 prefix argument GOTO, don't remember
729 anything, just go to the file/headline where the selected template usually
730 stores its notes. With a double prefix argument \
731 \\[universal-argument] \\[universal-argument], go to the last
732 note stored by remember.
734 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
735 associated with a template in `org-remember-templates'."
737 (org-require-remember)
739 ((equal goto
'(4)) (org-go-to-remember-target))
740 ((equal goto
'(16)) (org-remember-goto-last-stored))
742 ;; set temporary variables that will be needed in
743 ;; `org-select-remember-template'
744 (setq org-select-template-temp-major-mode major-mode
)
745 (setq org-select-template-original-buffer
(current-buffer))
746 (if org-remember-mode
748 (when (< (length org-remember-templates
) 2)
749 (error "No other template available"))
751 (let ((annotation (plist-get org-store-link-plist
:annotation
))
752 (initial (plist-get org-store-link-plist
:initial
)))
753 (org-remember-apply-template))
754 (message "Press C-c C-c to remember data"))
755 (if (org-region-active-p)
756 (org-do-remember (buffer-substring (point) (mark)))
757 (org-do-remember))))))
759 (defvar org-remember-last-stored-marker
(make-marker)
760 "Marker pointing to the entry most recently stored with `org-remember'.")
762 (defun org-remember-goto-last-stored ()
763 "Go to the location where the last remember note was stored."
765 (org-goto-marker-or-bmk org-remember-last-stored-marker
766 "org-remember-last-stored")
767 (message "This is the last note stored by remember"))
769 (defun org-go-to-remember-target (&optional template-key
)
770 "Go to the target location of a remember template.
771 The user is queried for the template."
773 (let* (org-select-template-temp-major-mode
774 (entry (org-select-remember-template template-key
))
776 (heading (nth 2 entry
))
778 (unless (and file
(stringp file
) (string-match "\\S-" file
))
779 (setq file org-default-notes-file
))
780 (when (and file
(not (file-name-absolute-p file
)))
781 (setq file
(expand-file-name file org-directory
)))
782 (unless (and heading
(stringp heading
) (string-match "\\S-" heading
))
783 (setq heading org-remember-default-headline
))
784 (setq visiting
(org-find-base-buffer-visiting file
))
785 (if (not visiting
) (find-file-noselect file
))
786 (switch-to-buffer (or visiting
(get-file-buffer file
)))
788 (goto-char (point-min))
789 (if (re-search-forward
790 (format org-complex-heading-regexp-format
(regexp-quote heading
))
792 (goto-char (match-beginning 0))
793 (error "Target headline not found: %s" heading
))))
795 ;; FIXME (bzg): let's clean up of final empty lines happen only once
796 ;; (see the org-remember-delete-empty-lines-at-end option below)
798 (defun org-remember-handler ()
799 "Store stuff from remember.el into an org file.
800 When the template has specified a file and a headline, the entry is filed
801 there, or in the location defined by `org-default-notes-file' and
802 `org-remember-default-headline'.
803 \\<org-remember-mode-map>
804 If no defaults have been defined, or if the current prefix argument
805 is 1 (using C-1 \\[org-remember-finalize] to exit remember), an interactive
806 process is used to select the target location.
808 When the prefix is 0 (i.e. when remember is exited with \
809 C-0 \\[org-remember-finalize]),
810 the entry is filed to the same location as the previous note.
812 When the prefix is 2 (i.e. when remember is exited with \
813 C-2 \\[org-remember-finalize]),
814 the entry is filed as a subentry of the entry where the clock is
817 When \\[universal-argument] has been used as prefix argument, the
818 note is stored and Emacs moves point to the new location of the
819 note, so that editing can be continued there (similar to
820 inserting \"%&\" into the template).
822 Before storing the note, the function ensures that the text has an
823 org-mode-style headline, i.e. a first line that starts with
824 a \"*\". If not, a headline is constructed from the current date and
825 some additional data.
827 If the variable `org-adapt-indentation' is non-nil, the entire text is
828 also indented so that it starts in the same column as the headline
829 \(i.e. after the stars).
831 See also the variable `org-reverse-note-order'."
832 (when (and (equal current-prefix-arg
2)
833 (not (marker-buffer org-clock-marker
)))
834 (error "No running clock"))
835 (when (org-bound-and-true-p org-jump-to-target-location
)
836 (let* ((end (min (point-max) (1+ (point))))
838 (if (= end beg
) (setq beg
(1- beg
)))
839 (put-text-property beg end
'org-position-cursor t
)))
840 (goto-char (point-min))
841 (while (looking-at "^[ \t]*\n\\|^##.*\n")
843 (when org-remember-delete-empty-lines-at-end
844 (goto-char (point-max))
845 (beginning-of-line 1)
846 (while (and (looking-at "[ \t]*$\\|##.*") (> (point) 1))
847 (delete-region (1- (point)) (point-max))
848 (beginning-of-line 1)))
850 (if org-note-abort
(throw 'quit t
))
851 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location
))
853 (and buffer-file-name
854 (equal (file-name-directory buffer-file-name
)
855 (file-name-as-directory
856 (expand-file-name org-remember-backup-directory
)))
857 (string-match "^remember-[0-9]\\{4\\}"
858 (file-name-nondirectory buffer-file-name
))
862 (unless (string-match "\\S-" (buffer-string))
863 (message "Nothing to remember")
866 (delete-file backup-file
)
867 (delete-file (concat backup-file
"~"))))
868 (set-buffer-modified-p nil
)
870 (reference-date org-remember-reference-date
)
871 (previousp (and (member current-prefix-arg
'((16) 0))
872 org-remember-previous-location
))
873 (clockp (equal current-prefix-arg
2))
874 (clocksp (equal current-prefix-arg
3))
875 (fastp (org-xor (equal current-prefix-arg
1)
876 org-remember-store-without-prompt
))
878 (fastp org-default-notes-file
)
879 ((and (eq org-remember-interactive-interface
'refile
)
881 org-default-notes-file
)
883 (org-get-org-file))))
884 (heading org-remember-default-headline
)
885 (visiting (and file
(org-find-base-buffer-visiting file
)))
886 (org-startup-folded nil
)
887 (org-startup-align-all-tables nil
)
888 (org-goto-start-pos 1)
889 spos exitcmd level reversed txt text-before-node-creation
)
890 (when (equal current-prefix-arg
'(4))
893 (setq file
(car org-remember-previous-location
)
894 visiting
(and file
(org-find-base-buffer-visiting file
))
895 heading
(cdr org-remember-previous-location
)
897 (when (or clockp clocksp
)
898 (setq file
(buffer-file-name (marker-buffer org-clock-marker
))
899 visiting
(and file
(org-find-base-buffer-visiting file
))
900 heading org-clock-heading-for-remember
902 (setq current-prefix-arg nil
)
903 ;; Modify text so that it becomes a nice subtree which can be inserted
905 (when org-remember-delete-empty-lines-at-end
906 (goto-char (point-min))
907 (if (re-search-forward "[ \t\n]+\\'" nil t
)
908 ;; remove empty lines at end
910 (goto-char (point-min))
911 (setq text-before-node-creation
(buffer-string))
912 (unless (looking-at org-outline-regexp
)
914 (insert (concat "* " (current-time-string)
915 " (" (remember-buffer-desc) ")\n"))
917 (when org-adapt-indentation
918 (while (re-search-forward "^" nil t
)
920 ;; Delete final empty lines
921 (when org-remember-delete-empty-lines-at-end
922 (goto-char (point-min))
923 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t
)
924 (replace-match "\n\n")
925 (if (re-search-forward "[ \t\n]*\\'")
926 (replace-match "\n"))))
927 (goto-char (point-min))
928 (setq txt
(buffer-string))
929 (org-save-markers-in-region (point-min) (point-max))
930 (set-buffer-modified-p nil
)
931 (when (and (eq org-remember-interactive-interface
'refile
)
933 (org-refile nil
(or visiting
(find-file-noselect file
)))
934 (and visitp
(run-with-idle-timer 0.01 nil
'org-remember-visit-immediately
))
936 (bookmark-jump "org-refile-last-stored")
937 (bookmark-set "org-remember-last-stored")
938 (move-marker org-remember-last-stored-marker
(point)))
941 (with-current-buffer (or visiting
(find-file-noselect file
))
942 (unless (or (org-mode-p) (member heading
'(top bottom
)))
943 (error "Target files for notes must be in Org-mode if not filing to top/bottom"))
947 (setq reversed
(org-notes-order-reversed-p))
949 ;; Find the default location
953 (if (eq heading
'top
)
954 (goto-char (point-min))
955 (goto-char (point-max))
956 (or (bolp) (newline)))
957 (insert text-before-node-creation
)
958 (when remember-save-after-remembering
960 (if (not visiting
) (kill-buffer (current-buffer))))
963 (goto-char (point-min))
964 (or (looking-at org-outline-regexp
)
965 (re-search-forward org-outline-regexp nil t
))
966 (setq org-goto-start-pos
(or (match-beginning 0) (point-min))))
967 ((eq heading
'bottom
)
968 (goto-char (point-max))
969 (or (bolp) (newline))
970 (setq org-goto-start-pos
(point)))
971 ((eq heading
'date-tree
)
972 (org-datetree-find-date-create reference-date
)
974 (setq org-goto-start-pos
(point)))
975 ((and (stringp heading
) (string-match "\\S-" heading
))
976 (goto-char (point-min))
977 (if (re-search-forward
978 (format org-complex-heading-regexp-format
979 (regexp-quote heading
))
981 (setq org-goto-start-pos
(match-beginning 0))
983 (goto-char (point-max))
984 (unless (bolp) (newline))
985 (insert "* " heading
"\n")
986 (setq org-goto-start-pos
(point-at-bol 0)))))
987 (t (goto-char (point-min)) (setq org-goto-start-pos
(point)
990 ;; Ask the User for a location, using the appropriate interface
992 ((and fastp
(memq heading
'(top bottom
)))
993 (setq spos org-goto-start-pos
994 exitcmd
(if (eq heading
'top
) 'left nil
)))
995 (fastp (setq spos org-goto-start-pos
997 ((eq org-remember-interactive-interface
'outline
)
998 (setq spos
(org-get-location (current-buffer)
1002 ((eq org-remember-interactive-interface
'outline-path-completion
)
1003 (let ((org-refile-targets '((nil .
(:maxlevel .
10))))
1004 (org-refile-use-outline-path t
))
1005 (setq spos
(org-refile-get-location "Heading: ")
1007 spos
(nth 3 spos
))))
1008 (t (error "This should not happen")))
1009 (if (not spos
) (throw 'quit nil
)) ; return nil to show we did
1010 ; not handle this note
1011 (and visitp
(run-with-idle-timer 0.01 nil
'org-remember-visit-immediately
))
1013 (cond ((org-on-heading-p t
)
1014 (org-back-to-heading t
)
1015 (setq level
(funcall outline-level
))
1017 ((eq exitcmd
'return
)
1018 ;; sublevel of current
1019 (setq org-remember-previous-location
1020 (cons (abbreviate-file-name file
)
1021 (org-get-heading 'notags
)))
1023 (outline-next-heading)
1024 (org-end-of-subtree t
)
1026 (if (looking-at "[ \t]*\n")
1027 (beginning-of-line 2)
1030 (org-paste-subtree (if clocksp
1032 (org-get-valid-level level
1)) txt
)
1033 (and org-auto-align-tags
(org-set-tags nil t
))
1034 (bookmark-set "org-remember-last-stored")
1035 (move-marker org-remember-last-stored-marker
(point)))
1038 (org-paste-subtree level txt
)
1039 (and org-auto-align-tags
(org-set-tags nil t
))
1040 (bookmark-set "org-remember-last-stored")
1041 (move-marker org-remember-last-stored-marker
(point)))
1042 ((eq exitcmd
'right
)
1044 (org-end-of-subtree t
)
1045 (org-paste-subtree level txt
)
1046 (and org-auto-align-tags
(org-set-tags nil t
))
1047 (bookmark-set "org-remember-last-stored")
1048 (move-marker org-remember-last-stored-marker
(point)))
1049 (t (error "This should not happen"))))
1051 ((eq heading
'bottom
)
1052 (org-paste-subtree 1 txt
)
1053 (and org-auto-align-tags
(org-set-tags nil t
))
1054 (bookmark-set "org-remember-last-stored")
1055 (move-marker org-remember-last-stored-marker
(point)))
1057 ((and (bobp) (not reversed
))
1058 ;; Put it at the end, one level below level 1
1061 (goto-char (point-max))
1062 (if (not (bolp)) (newline))
1063 (org-paste-subtree (org-get-valid-level 1 1) txt
)
1064 (and org-auto-align-tags
(org-set-tags nil t
))
1065 (bookmark-set "org-remember-last-stored")
1066 (move-marker org-remember-last-stored-marker
(point))))
1068 ((and (bobp) reversed
)
1069 ;; Put it at the start, as level 1
1072 (goto-char (point-min))
1073 (re-search-forward "^\\*+ " nil t
)
1074 (beginning-of-line 1)
1075 (org-paste-subtree 1 txt
)
1076 (and org-auto-align-tags
(org-set-tags nil t
))
1077 (bookmark-set "org-remember-last-stored")
1078 (move-marker org-remember-last-stored-marker
(point))))
1080 ;; Put it right there, with automatic level determined by
1081 ;; org-paste-subtree or from prefix arg
1083 (if (numberp current-prefix-arg
) current-prefix-arg
)
1085 (and org-auto-align-tags
(org-set-tags nil t
))
1086 (bookmark-set "org-remember-last-stored")
1087 (move-marker org-remember-last-stored-marker
(point))))
1089 (when remember-save-after-remembering
1091 (if (and (not visiting
)
1092 (not (equal (marker-buffer org-clock-marker
)
1094 (kill-buffer (current-buffer))))
1095 (when org-remember-auto-remove-backup-files
1098 (delete-file backup-file
)
1099 (delete-file (concat backup-file
"~"))))
1100 (when org-remember-backup-directory
1103 org-remember-backup-directory nil
1104 "^remember-.*[0-9]$"))))
1105 (when (and org-remember-warn-about-backups
1108 "%d backup files (unfinished remember calls) in %s"
1109 n org-remember-backup-directory
))))))))))
1111 t
) ;; return t to indicate that we took care of this note.
1113 (defun org-do-remember (&optional initial
)
1117 (defun org-require-remember ()
1118 "Make sure remember is loaded, or install our own emergency version of it."
1122 ;; Lets install our own micro version of remember
1123 (defvar remember-register ?R
)
1124 (defvar remember-mode-hook nil
)
1125 (defvar remember-handler-functions nil
)
1126 (defvar remember-buffer
"*Remember*")
1127 (defvar remember-save-after-remembering t
)
1128 (defvar remember-annotation-functions
'(buffer-file-name))
1129 (defun remember-finalize ()
1130 (run-hook-with-args-until-success 'remember-handler-functions
)
1131 (when (equal remember-buffer
(buffer-name))
1132 (kill-buffer (current-buffer))
1133 (jump-to-register remember-register
)))
1134 (defun remember-mode ()
1136 (setq mode-name
"Remember")
1137 (run-hooks 'remember-mode-hook
))
1138 (defun remember (&optional initial
)
1139 (window-configuration-to-register remember-register
)
1140 (let* ((annotation (run-hook-with-args-until-success
1141 'remember-annotation-functions
)))
1142 (switch-to-buffer-other-window (get-buffer-create remember-buffer
))
1144 (defun remember-buffer-desc ()
1145 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
1146 (point-at-eol)))))))
1148 (provide 'org-remember
)
1151 ;;; org-remember.el ends here