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