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'.
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, `C-c C-c' stores remember note without further prompts.
57 It then uses the file and headline specified by the template or (if the
58 template does not specify them) by the variables `org-default-notes-file'
59 and `org-remember-default-headline'. To force prompting anyway, use
60 `C-u C-c C-c' to file the note.
62 When this variable is nil, `C-c C-c' gives you the prompts, and
63 `C-u C-c C-c' triggers the fasttrack."
67 (defcustom org-remember-interactive-interface
'refile
68 "The interface to be used for interactive filing of remember notes.
69 This is only used when the interactive mode for selecting a filing
70 location is used (see the variable `org-remember-store-without-prompt').
72 outline The interface shows an outline of the relevant file
73 and the correct heading is found by moving through
74 the outline or by searching with incremental search.
75 outline-path-completion Headlines in the current buffer are offered via
77 refile Use the refile interface, and offer headlines,
78 possibly from different buffers."
81 (const :tag
"Refile" refile
)
82 (const :tag
"Outline" outline
)
83 (const :tag
"Outline-path-completion" outline-path-completion
)))
85 (defcustom org-remember-default-headline
""
86 "The headline that should be the default location in the notes file.
87 When filing remember notes, the cursor will start at that position.
88 You can set this on a per-template basis with the variable
89 `org-remember-templates'."
93 (defcustom org-remember-templates nil
94 "Templates for the creation of remember buffers.
95 When nil, just let remember make the buffer.
96 When non-nil, this is a list of 5-element lists. In each entry, the first
97 element is the name of the template, which should be a single short word.
98 The second element is a character, a unique key to select this template.
99 The third element is the template.
101 The fourth element is optional and can specify a destination file for
102 remember items created with this template. The default file is given
103 by `org-default-notes-file'. If the file name is not an absolute path,
104 it will be interpreted relative to `org-directory'.
106 An optional fifth element can specify the headline in that file that should
107 be offered first when the user is asked to file the entry. The default
108 headline is given in the variable `org-remember-default-headline'. When
109 this element is `top' or `bottom', the note will be placed as a level-1
110 entry at the beginning or end of the file, respectively.
112 An optional sixth element specifies the contexts in which the template
113 will be offered to the user. This element can be a list of major modes
114 or a function, and the template will only be offered if `org-remember'
115 is called from a mode in the list, or if the function returns t.
116 Templates that specify t or nil for the context will be always be added
117 to the list of selectable templates.
119 The template specifies the structure of the remember buffer. It should have
120 a first line starting with a star, to act as the org-mode headline.
121 Furthermore, the following %-escapes will be replaced with content:
123 %^{prompt} Prompt the user for a string and replace this sequence with it.
124 A default value and a completion table ca be specified like this:
125 %^{prompt|default|completion2|completion3|...}
126 %t time stamp, date only
127 %T time stamp with date and time
128 %u, %U like the above, but inactive time stamps
129 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
130 You may define a prompt like %^{Please specify birthday
131 %n user name (taken from `user-full-name')
132 %a annotation, normally the link created with org-store-link
133 %i initial content, the region active. If %i is indented,
134 the entire inserted text will be indented as well.
135 %c current kill ring head
136 %x content of the X clipboard
137 %^C Interactive selection of which kill or clip to use
138 %^L Like %^C, but insert as link
139 %k title of currently clocked task
140 %K link to currently clocked task
141 %^g prompt for tags, with completion on tags in target file
142 %^G prompt for tags, with completion all tags in all agenda files
143 %^{prop}p Prompt the user for a value for property `prop'
144 %:keyword specific information for certain link types, see below
145 %[pathname] insert the contents of the file given by `pathname'
146 %(sexp) evaluate elisp `(sexp)' and replace with the result
147 %! Store this note immediately after filling the template
148 %& Visit note immediately after storing it
150 %? After completing the template, position cursor here.
152 Apart from these general escapes, you can access information specific to the
153 link type that is created. For example, calling `remember' in emails or gnus
154 will record the author and the subject of the message, which you can access
155 with %:author and %:subject, respectively. Here is a complete list of what
156 is recorded for each link type.
158 Link type | Available information
159 -------------------+------------------------------------------------------
160 bbdb | %:type %:name %:company
161 vm, wl, mh, rmail | %:type %:subject %:message-id
162 | %:from %:fromname %:fromaddress
163 | %:to %:toname %:toaddress
164 | %:fromto (either \"to NAME\" or \"from NAME\")
165 gnus | %:group, for messages also all email fields
166 w3, w3m | %:type %:url
167 info | %:type %:file %:node
168 calendar | %:type %:date"
170 :get
(lambda (var) ; Make sure all entries have at least 5 elements
172 (if (not (stringp (car x
))) (setq x
(cons "" x
)))
173 (cond ((= (length x
) 4) (append x
'(nil)))
174 ((= (length x
) 3) (append x
'(nil nil
)))
176 (default-value var
)))
179 (list :value
("" ?a
"\n" nil nil nil
)
181 (character :tag
"Selection Key")
182 (string :tag
"Template")
183 (choice :tag
"Destination file"
184 (file :tag
"Specify")
185 (function :tag
"Function")
186 (const :tag
"Use `org-default-notes-file'" nil
))
187 (choice :tag
"Destin. headline"
188 (string :tag
"Specify")
189 (const :tag
"Use `org-remember-default-headline'" nil
)
190 (const :tag
"Level 1 at beginning of file" top
)
191 (const :tag
"Level 1 at end of file" bottom
))
192 (choice :tag
"Context"
193 (const :tag
"Use in all contexts" nil
)
194 (const :tag
"Use in all contexts" t
)
195 (repeat :tag
"Use only if in major mode"
196 (symbol :tag
"Major mode"))
197 (function :tag
"Perform a check against function")))))
199 (defcustom org-remember-before-finalize-hook nil
200 "Hook that is run right before a remember process is finalized.
201 The remember buffer is still current when this hook runs."
205 (defvar org-remember-mode-map
(make-sparse-keymap)
206 "Keymap for org-remember-mode, a minor mode.
207 Use this map to set additional keybindings for when Org-mode is used
208 for a Remember buffer.")
209 (defvar org-remember-mode-hook nil
210 "Hook for the minor `org-remember-mode'.")
212 (define-minor-mode org-remember-mode
213 "Minor mode for special key bindings in a remember buffer."
214 nil
" Rem" org-remember-mode-map
215 (run-hooks 'org-remember-mode-hook
))
216 (define-key org-remember-mode-map
"\C-c\C-c" 'org-remember-finalize
)
217 (define-key org-remember-mode-map
"\C-c\C-k" 'org-remember-kill
)
219 (defcustom org-remember-clock-out-on-exit
'query
220 "Non-nil means, stop the clock when exiting a clocking remember buffer.
221 This only applies if the clock is running in the remember buffer. If the
222 clock is not stopped, it continues to run in the storage location.
223 Instead of nil or t, this may also be the symbol `query' to prompt the
224 user each time a remember buffer with a running clock is filed away. "
227 (const :tag
"Never" nil
)
228 (const :tag
"Always" t
)
229 (const :tag
"Query user" query
)))
231 (defcustom org-remember-backup-directory nil
232 "Directory where to store all remember buffers, for backup purposes.
233 After a remember buffer has been stored successfully, the backup file
234 will be removed. However, if you forget to finish the remember process,
235 the file will remain there.
236 See also `org-remember-auto-remove-backup-files'."
239 (const :tag
"No backups" nil
)
240 (directory :tag
"Directory")))
242 (defcustom org-remember-auto-remove-backup-files t
243 "Non-nil means, remove remember backup files after successfully storage.
244 When remember is finished successfully, with storing the note at the
245 desired target, remove the backup files related to this remember process
246 and show a message about remaining backup files, from previous, unfinished
248 Backup files will only be made at all, when `org-remember-backup-directory'
253 (defvar annotation
) ; from remember.el, dynamically scoped in `remember-mode'
254 (defvar initial
) ; from remember.el, dynamically scoped in `remember-mode'
257 (defun org-remember-insinuate ()
258 "Setup remember.el for use with Org-mode."
259 (org-require-remember)
260 (setq remember-annotation-functions
'(org-remember-annotation))
261 (setq remember-handler-functions
'(org-remember-handler))
262 (add-hook 'remember-mode-hook
'org-remember-apply-template
))
265 (defun org-remember-annotation ()
266 "Return a link to the current location as an annotation for remember.el.
267 If you are using Org-mode files as target for data storage with
268 remember.el, then the annotations should include a link compatible with the
269 conventions in Org-mode. This function returns such a link."
270 (org-store-link nil
))
272 (defconst org-remember-help
273 "Select a destination location for the note.
274 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
275 RET on headline -> Store as sublevel entry to current headline
276 RET at beg-of-buf -> Append to file as level 2 headline
277 <left>/<right> -> before/after current headline, same headings level")
279 (defvar org-jump-to-target-location nil
)
280 (defvar org-remember-previous-location nil
)
281 (defvar org-force-remember-template-char
) ;; dynamically scoped
283 ;; Save the major mode of the buffer we called remember from
284 (defvar org-select-template-temp-major-mode nil
)
286 ;; Temporary store the buffer where remember was called from
287 (defvar org-select-template-original-buffer nil
)
289 (defun org-select-remember-template (&optional use-char
)
290 (when org-remember-templates
291 (let* ((pre-selected-templates
294 (let ((ctxt (nth 5 tpl
))
295 (mode org-select-template-temp-major-mode
)
296 (buf org-select-template-original-buffer
))
297 (and (or (not ctxt
) (eq ctxt t
)
298 (and (listp ctxt
) (memq mode ctxt
))
299 (and (functionp ctxt
)
300 (with-current-buffer buf
301 ;; Protect the user-defined function from error
302 (condition-case nil
(funcall ctxt
) (error nil
)))))
304 org-remember-templates
))
305 ;; If no template at this point, add the default templates:
306 (pre-selected-templates1
307 (if (not (delq nil pre-selected-templates
))
308 (mapcar (lambda(x) (if (not (nth 5 x
)) x
))
309 org-remember-templates
)
310 pre-selected-templates
))
311 ;; Then unconditionally add template for any contexts
312 (pre-selected-templates2
313 (append (mapcar (lambda(x) (if (eq (nth 5 x
) t
) x
))
314 org-remember-templates
)
315 (delq nil pre-selected-templates1
)))
316 (templates (mapcar (lambda (x)
317 (if (stringp (car x
))
318 (append (list (nth 1 x
) (car x
)) (cddr x
))
319 (append (list (car x
) "") (cdr x
))))
320 (delq nil pre-selected-templates2
)))
323 ((= (length templates
) 1)
325 ((and (boundp 'org-force-remember-template-char
)
326 org-force-remember-template-char
)
327 (if (stringp org-force-remember-template-char
)
328 (string-to-char org-force-remember-template-char
)
329 org-force-remember-template-char
))
331 (message "Select template: %s"
335 ((not (string-match "\\S-" (nth 1 x
)))
336 (format "[%c]" (car x
)))
337 ((equal (downcase (car x
))
338 (downcase (aref (nth 1 x
) 0)))
339 (format "[%c]%s" (car x
)
340 (substring (nth 1 x
) 1)))
341 (t (format "[%c]%s" (car x
) (nth 1 x
)))))
343 (let ((inhibit-quit t
) (char0 (read-char-exclusive)))
344 (when (equal char0 ?\C-g
)
345 (jump-to-register remember-register
)
346 (kill-buffer remember-buffer
))
348 (cddr (assoc char templates
)))))
350 (defun org-get-x-clipboard (value)
351 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
352 (if (eq window-system
'x
)
353 (let ((x (org-get-x-clipboard-compat value
)))
354 (if x
(org-no-properties x
)))))
357 (defun org-remember-apply-template (&optional use-char skip-interactive
)
358 "Initialize *remember* buffer with template, invoke `org-mode'.
359 This function should be placed into `remember-mode-hook' and in fact requires
360 to be run from that hook to function properly."
361 (when (and (boundp 'initial
) (stringp initial
))
362 (setq initial
(org-no-properties initial
))
363 (remove-text-properties 0 (length initial
) '(read-only t
) initial
))
364 (if org-remember-templates
365 (let* ((entry (org-select-remember-template use-char
))
366 (ct (or org-overriding-default-time
(org-current-time)))
367 (dct (decode-time ct
))
369 (if (< (nth 2 dct
) org-extend-today-until
)
370 (encode-time 0 59 23 (1- (nth 3 dct
)) (nth 4 dct
) (nth 5 dct
))
373 (plist-p (if org-store-link-plist t nil
))
374 (file (if (and (nth 1 entry
)
375 (or (and (stringp (nth 1 entry
))
376 (string-match "\\S-" (nth 1 entry
)))
377 (functionp (nth 1 entry
))))
379 org-default-notes-file
))
380 (headline (nth 2 entry
))
381 (v-c (and (> (length kill-ring
) 0) (current-kill 0)))
382 (v-x (or (org-get-x-clipboard 'PRIMARY
)
383 (org-get-x-clipboard 'CLIPBOARD
)
384 (org-get-x-clipboard 'SECONDARY
)))
385 (v-t (format-time-string (car org-time-stamp-formats
) ct
))
386 (v-T (format-time-string (cdr org-time-stamp-formats
) ct
))
387 (v-u (concat "[" (substring v-t
1 -
1) "]"))
388 (v-U (concat "[" (substring v-T
1 -
1) "]"))
389 ;; `initial' and `annotation' are bound in `remember'.
390 ;; But if the property list has them, we prefer those values
391 (v-i (or (plist-get org-store-link-plist
:initial
)
392 (and (boundp 'initial
) initial
)
394 (v-a (or (plist-get org-store-link-plist
:annotation
)
395 (and (boundp 'annotation
) annotation
)
397 ;; Is the link empty? Then we do not want it...
398 (v-a (if (equal v-a
"[[]]") "" v-a
))
399 (clipboards (remove nil
(list v-i
400 (org-get-x-clipboard 'PRIMARY
)
401 (org-get-x-clipboard 'CLIPBOARD
)
402 (org-get-x-clipboard 'SECONDARY
)
405 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a
))
406 (replace-match "[\\1[%^{Link description}]]" nil nil v-a
)
409 (v-k (if (marker-buffer org-clock-marker
)
410 (org-substring-no-properties org-clock-heading
)))
411 (v-K (if (marker-buffer org-clock-marker
)
412 (org-make-link-string
413 (buffer-file-name (marker-buffer org-clock-marker
))
416 (org-startup-folded nil
)
417 (org-inhibit-startup t
)
418 org-time-was-given org-end-time-was-given x
419 prompt completions char time pos default histvar
)
421 (when (functionp file
)
422 (setq file
(funcall file
)))
423 (when (and file
(not (file-name-absolute-p file
)))
424 (setq file
(expand-file-name file org-directory
)))
426 (setq org-store-link-plist
427 (plist-put org-store-link-plist
:annotation v-a
)
429 (plist-put org-store-link-plist
:initial v-i
))
431 (unless tpl
(setq tpl
"") (message "No template") (ding) (sit-for 1))
433 (insert (substitute-command-keys
435 "## %s \"%s\" -> \"* %s\"
436 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
437 ## C-0 C-c C-c \"%s\" -> \"* %s\"
438 ## %s to select file and header location interactively.
439 ## C-2 C-c C-c as child of the currently clocked item
440 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
441 (if org-remember-store-without-prompt
" C-c C-c" " C-1 C-c C-c")
442 (abbreviate-file-name (or file org-default-notes-file
))
444 (or (car org-remember-previous-location
) "???")
445 (or (cdr org-remember-previous-location
) "???")
446 (if org-remember-store-without-prompt
"C-1 C-c C-c" " C-c C-c"))))
448 (goto-char (point-min))
451 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t
)
452 (when (and initial
(equal (match-string 0) "%i"))
454 (let* ((lead (buffer-substring
455 (point-at-bol) (match-beginning 0))))
456 (setq v-i
(mapconcat 'identity
457 (org-split-string initial
"\n")
458 (concat "\n" lead
))))))
460 (or (eval (intern (concat "v-" (match-string 1)))) "")
463 ;; %[] Insert contents of a file.
464 (goto-char (point-min))
465 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t
)
466 (let ((start (match-beginning 0))
468 (filename (expand-file-name (match-string 1))))
470 (delete-region start end
)
471 (condition-case error
472 (insert-file-contents filename
)
473 (error (insert (format "%%![Couldn't insert %s: %s]"
475 ;; %() embedded elisp
476 (goto-char (point-min))
477 (while (re-search-forward "%\\((.+)\\)" nil t
)
478 (goto-char (match-beginning 0))
479 (let ((template-start (point)))
482 (condition-case error
483 (eval (read (current-buffer)))
484 (error (format "%%![Error: %s]" error
)))))
485 (delete-region template-start
(point))
488 ;; From the property list
490 (goto-char (point-min))
491 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t
)
492 (and (setq x
(or (plist-get org-store-link-plist
493 (intern (match-string 1))) ""))
494 (replace-match x t t
))))
496 ;; Turn on org-mode in the remember buffer, set local variables
497 (let ((org-inhibit-startup t
)) (org-mode) (org-remember-mode 1))
498 (if (and file
(string-match "\\S-" file
) (not (file-directory-p file
)))
499 (org-set-local 'org-default-notes-file file
))
501 (org-set-local 'org-remember-default-headline headline
))
502 ;; Interactive template entries
503 (goto-char (point-min))
504 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t
)
505 (setq char
(if (match-end 3) (match-string 3))
506 prompt
(if (match-end 2) (match-string 2)))
507 (goto-char (match-beginning 0))
509 (setq completions nil default nil
)
511 (setq completions
(org-split-string prompt
"|")
512 prompt
(pop completions
)
513 default
(car completions
)
514 histvar
(intern (concat
515 "org-remember-template-prompt-history::"
517 completions
(mapcar 'list completions
)))
519 ((member char
'("G" "g"))
520 (let* ((org-last-tags-completion-table
521 (org-global-tags-completion-table
522 (if (equal char
"G") (org-agenda-files) (and file
(list file
)))))
523 (org-add-colon-after-tag-completion t
)
524 (ins (org-ido-completing-read
525 (if prompt
(concat prompt
": ") "Tags: ")
526 'org-tags-completion-function nil nil nil
528 (setq ins
(mapconcat 'identity
529 (org-split-string ins
(org-re "[^[:alnum:]_@]+"))
531 (when (string-match "\\S-" ins
)
532 (or (equal (char-before) ?
:) (insert ":"))
534 (or (equal (char-after) ?
:) (insert ":")))))
536 (cond ((= (length clipboards
) 1) (insert (car clipboards
)))
537 ((> (length clipboards
) 1)
538 (insert (read-string "Clipboard/kill value: "
539 (car clipboards
) '(clipboards .
1)
540 (car clipboards
))))))
542 (cond ((= (length clipboards
) 1)
543 (org-insert-link 0 (car clipboards
)))
544 ((> (length clipboards
) 1)
545 (org-insert-link 0 (read-string "Clipboard/kill value: "
548 (car clipboards
))))))
551 ((prop (org-substring-no-properties prompt
))
552 (pall (concat prop
"_ALL"))
555 (get-buffer (file-name-nondirectory file
))
556 (or (cdr (assoc pall org-file-properties
))
557 (cdr (assoc pall org-global-properties
))
558 (cdr (assoc pall org-global-properties-fixed
)))))
559 (existing (with-current-buffer
560 (get-buffer (file-name-nondirectory file
))
561 (mapcar 'list
(org-property-values prop
))))
562 (propprompt (concat "Value for " prop
": "))
566 (mapcar 'list
(org-split-string allowed
"[ \t]+"))
568 (org-completing-read-no-ido propprompt existing nil nil
570 (org-set-property prop val
)))
572 ;; These are the date/time related ones
573 (setq org-time-was-given
(equal (upcase char
) char
))
574 (setq time
(org-read-date (equal (upcase char
) "U") t nil
576 (org-insert-time-stamp time org-time-was-given
577 (member char
'("u" "U"))
578 nil nil
(list org-end-time-was-given
)))
580 (let (org-completion-use-ido)
581 (insert (org-completing-read-no-ido
582 (concat (if prompt prompt
"Enter string")
583 (if default
(concat " [" default
"]"))
585 completions nil nil nil histvar default
))))))
586 (goto-char (point-min))
587 (if (re-search-forward "%\\?" nil t
)
589 (and (re-search-forward "^[^#\n]" nil t
) (backward-char 1))))
590 (let ((org-inhibit-startup t
)) (org-mode) (org-remember-mode 1)))
591 (when (save-excursion
592 (goto-char (point-min))
593 (re-search-forward "%&" nil t
))
595 (org-set-local 'org-jump-to-target-location t
))
596 (when org-remember-backup-directory
597 (unless (file-directory-p org-remember-backup-directory
)
598 (make-directory org-remember-backup-directory
))
599 (setq buffer-file-name
601 (format-time-string "remember-%Y-%m-%d-%H-%M-%S")
602 org-remember-backup-directory
))
604 (org-set-local 'auto-save-visited-file-name t
))
605 (when (save-excursion
606 (goto-char (point-min))
607 (re-search-forward "%!" nil t
))
609 (add-hook 'post-command-hook
'org-remember-finish-immediately
'append
)))
611 (defun org-remember-finish-immediately ()
612 "File remember note immediately.
613 This should be run in `post-command-hook' and will remove itself
615 (remove-hook 'post-command-hook
'org-remember-finish-immediately
)
616 (org-remember-finalize))
618 (defun org-remember-visit-immediately ()
619 "File remember note immediately.
620 This should be run in `post-command-hook' and will remove itself
623 (goto-char (or (text-property-any
624 (point) (save-excursion (org-end-of-subtree t t
))
625 'org-position-cursor t
)
629 (substitute-command-keys
630 "Restore window configuration with \\[jump-to-register] %c")
633 (defvar org-clock-marker
) ; Defined in org.el
634 (defun org-remember-finalize ()
635 "Finalize the remember process."
637 (unless org-remember-mode
638 (error "This does not seem to be a remember buffer for Org-mode"))
639 (run-hooks 'org-remember-before-finalize-hook
)
640 (unless (fboundp 'remember-finalize
)
641 (defalias 'remember-finalize
'remember-buffer
))
642 (when (and org-clock-marker
643 (equal (marker-buffer org-clock-marker
) (current-buffer)))
644 ;; the clock is running in this buffer.
645 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
646 (or (eq org-remember-clock-out-on-exit t
)
647 (and org-remember-clock-out-on-exit
648 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
649 (let (org-log-note-clock-out) (org-clock-out))))
650 (when buffer-file-name
654 (defun org-remember-kill ()
655 "Abort the current remember process."
657 (let ((org-note-abort t
))
658 (org-remember-finalize)))
661 (defun org-remember (&optional goto org-force-remember-template-char
)
662 "Call `remember'. If this is already a remember buffer, re-apply template.
663 If there is an active region, make sure remember uses it as initial content
664 of the remember buffer.
666 When called interactively with a `C-u' prefix argument GOTO, don't remember
667 anything, just go to the file/headline where the selected template usually
668 stores its notes. With a double prefix arg `C-u C-u', go to the last
669 note stored by remember.
671 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
672 associated with a template in `org-remember-templates'."
674 (org-require-remember)
676 ((equal goto
'(4)) (org-go-to-remember-target))
677 ((equal goto
'(16)) (org-remember-goto-last-stored))
679 ;; set temporary variables that will be needed in
680 ;; `org-select-remember-template'
681 (setq org-select-template-temp-major-mode major-mode
)
682 (setq org-select-template-original-buffer
(current-buffer))
683 (if org-remember-mode
685 (when (< (length org-remember-templates
) 2)
686 (error "No other template available"))
688 (let ((annotation (plist-get org-store-link-plist
:annotation
))
689 (initial (plist-get org-store-link-plist
:initial
)))
690 (org-remember-apply-template))
691 (message "Press C-c C-c to remember data"))
692 (if (org-region-active-p)
693 (org-do-remember (buffer-substring (point) (mark)))
694 (org-do-remember))))))
696 (defvar org-remember-last-stored-marker
(make-marker)
697 "Marker pointing to the entry most recently stored with `org-remember'.")
699 (defun org-remember-goto-last-stored ()
700 "Go to the location where the last remember note was stored."
702 (org-goto-marker-or-bmk org-remember-last-stored-marker
703 "org-remember-last-stored")
704 (message "This is the last note stored by remember"))
706 (defun org-go-to-remember-target (&optional template-key
)
707 "Go to the target location of a remember template.
708 The user is queried for the template."
710 (let* (org-select-template-temp-major-mode
711 (entry (org-select-remember-template template-key
))
713 (heading (nth 2 entry
))
715 (unless (and file
(stringp file
) (string-match "\\S-" file
))
716 (setq file org-default-notes-file
))
717 (when (and file
(not (file-name-absolute-p file
)))
718 (setq file
(expand-file-name file org-directory
)))
719 (unless (and heading
(stringp heading
) (string-match "\\S-" heading
))
720 (setq heading org-remember-default-headline
))
721 (setq visiting
(org-find-base-buffer-visiting file
))
722 (if (not visiting
) (find-file-noselect file
))
723 (switch-to-buffer (or visiting
(get-file-buffer file
)))
725 (goto-char (point-min))
726 (if (re-search-forward
727 (concat "^\\*+[ \t]+" (regexp-quote heading
)
728 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
730 (goto-char (match-beginning 0))
731 (error "Target headline not found: %s" heading
))))
734 (defun org-remember-handler ()
735 "Store stuff from remember.el into an org file.
736 When the template has specified a file and a headline, the entry is filed
737 there, or in the location defined by `org-default-notes-file' and
738 `org-remember-default-headline'.
740 If no defaults have been defined, or if the current prefix argument
741 is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
742 process is used to select the target location.
744 When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
745 the entry is filed to the same location as the previous note.
747 When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
748 the entry is filed as a subentry of the entry where the clock is
751 When `C-u' has been used as prefix argument, the note is stored and emacs
752 moves point to the new location of the note, so that editing can be
753 continued there (similar to inserting \"%&\" into the template).
755 Before storing the note, the function ensures that the text has an
756 org-mode-style headline, i.e. a first line that starts with
757 a \"*\". If not, a headline is constructed from the current date and
758 some additional data.
760 If the variable `org-adapt-indentation' is non-nil, the entire text is
761 also indented so that it starts in the same column as the headline
762 \(i.e. after the stars).
764 See also the variable `org-reverse-note-order'."
765 (when (and (equal current-prefix-arg
2)
766 (not (marker-buffer org-clock-marker
)))
767 (error "No running clock"))
768 (when (org-bound-and-true-p org-jump-to-target-location
)
769 (let* ((end (min (point-max) (1+ (point))))
771 (if (= end beg
) (setq beg
(1- beg
)))
772 (put-text-property beg end
'org-position-cursor t
)))
773 (goto-char (point-min))
774 (while (looking-at "^[ \t]*\n\\|^##.*\n")
776 (goto-char (point-max))
777 (beginning-of-line 1)
778 (while (looking-at "[ \t]*$\\|##.*")
779 (delete-region (1- (point)) (point-max))
780 (beginning-of-line 1))
782 (if org-note-abort
(throw 'quit nil
))
783 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location
))
785 (and buffer-file-name
786 (equal (file-name-directory buffer-file-name
)
787 (file-name-as-directory
788 (expand-file-name org-remember-backup-directory
)))
789 (string-match "^remember-[0-9]\\{4\\}"
790 (file-name-nondirectory buffer-file-name
))
792 (previousp (and (member current-prefix-arg
'((16) 0))
793 org-remember-previous-location
))
794 (clockp (equal current-prefix-arg
2))
795 (fastp (org-xor (equal current-prefix-arg
1)
796 org-remember-store-without-prompt
))
798 (fastp org-default-notes-file
)
799 ((and (eq org-remember-interactive-interface
'refile
)
801 org-default-notes-file
)
803 (org-get-org-file))))
804 (heading org-remember-default-headline
)
805 (visiting (and file
(org-find-base-buffer-visiting file
)))
806 (org-startup-folded nil
)
807 (org-startup-align-all-tables nil
)
808 (org-goto-start-pos 1)
809 spos exitcmd level reversed txt text-before-node-creation
)
810 (when (equal current-prefix-arg
'(4))
813 (setq file
(car org-remember-previous-location
)
814 visiting
(and file
(org-find-base-buffer-visiting file
))
815 heading
(cdr org-remember-previous-location
)
818 (setq file
(buffer-file-name (marker-buffer org-clock-marker
))
819 visiting
(and file
(org-find-base-buffer-visiting file
))
820 heading org-clock-heading-for-remember
822 (setq current-prefix-arg nil
)
823 ;; Modify text so that it becomes a nice subtree which can be inserted
825 (goto-char (point-min))
826 (if (re-search-forward "[ \t\n]+\\'" nil t
)
827 ;; remove empty lines at end
829 (goto-char (point-min))
830 (unless (looking-at org-outline-regexp
)
832 (setq text-before-node-creation
(buffer-string))
833 (insert (concat "* " (current-time-string)
834 " (" (remember-buffer-desc) ")\n"))
836 (when org-adapt-indentation
837 (while (re-search-forward "^" nil t
)
839 (goto-char (point-min))
840 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t
)
841 (replace-match "\n\n")
842 (if (re-search-forward "[ \t\n]*\\'")
843 (replace-match "\n")))
844 (goto-char (point-min))
845 (setq txt
(buffer-string))
846 (org-save-markers-in-region (point-min) (point-max))
847 (set-buffer-modified-p nil
)
848 (when (and (eq org-remember-interactive-interface
'refile
)
850 (org-refile nil
(or visiting
(find-file-noselect file
)))
851 (and visitp
(run-with-idle-timer 0.01 nil
'org-remember-visit-immediately
))
853 (bookmark-jump "org-refile-last-stored")
854 (bookmark-set "org-remember-last-stored")
855 (move-marker org-remember-last-stored-marker
(point)))
858 (with-current-buffer (or visiting
(find-file-noselect file
))
859 (unless (or (org-mode-p) (member heading
'(top bottom
)))
860 (error "Target files for notes must be in Org-mode if not filing to top/bottom"))
864 (setq reversed
(org-notes-order-reversed-p))
866 ;; Find the default location
870 (if (eq heading
'top
)
871 (goto-char (point-min))
872 (goto-char (point-max))
873 (or (bolp) (newline)))
874 (insert text-before-node-creation
)
875 (when remember-save-after-remembering
877 (if (not visiting
) (kill-buffer (current-buffer))))
880 (goto-char (point-min))
881 (or (looking-at org-outline-regexp
)
882 (re-search-forward org-outline-regexp nil t
))
883 (setq org-goto-start-pos
(or (match-beginning 0) (point-min))))
884 ((eq heading
'bottom
)
885 (goto-char (point-max))
886 (or (bolp) (newline))
887 (setq org-goto-start-pos
(point)))
888 ((and (stringp heading
) (string-match "\\S-" heading
))
889 (goto-char (point-min))
890 (if (re-search-forward
891 (concat "^\\*+[ \t]+" (regexp-quote heading
)
892 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
894 (setq org-goto-start-pos
(match-beginning 0))
896 (goto-char (point-max))
897 (unless (bolp) (newline))
898 (insert "* " heading
"\n")
899 (setq org-goto-start-pos
(point-at-bol 0)))))
900 (t (goto-char (point-min)) (setq org-goto-start-pos
(point)
903 ;; Ask the User for a location, using the appropriate interface
905 ((and fastp
(memq heading
'(top bottom
)))
906 (setq spos org-goto-start-pos
907 exitcmd
(if (eq heading
'top
) 'left nil
)))
908 (fastp (setq spos org-goto-start-pos
910 ((eq org-remember-interactive-interface
'outline
)
911 (setq spos
(org-get-location (current-buffer)
915 ((eq org-remember-interactive-interface
'outline-path-completion
)
916 (let ((org-refile-targets '((nil .
(:maxlevel .
10))))
917 (org-refile-use-outline-path t
))
918 (setq spos
(org-refile-get-location "Heading: ")
921 (t (error "This should not happen")))
922 (if (not spos
) (throw 'quit nil
)) ; return nil to show we did
923 ; not handle this note
924 (and visitp
(run-with-idle-timer 0.01 nil
'org-remember-visit-immediately
))
926 (cond ((org-on-heading-p t
)
927 (org-back-to-heading t
)
928 (setq level
(funcall outline-level
))
930 ((eq exitcmd
'return
)
931 ;; sublevel of current
932 (setq org-remember-previous-location
933 (cons (abbreviate-file-name file
)
934 (org-get-heading 'notags
)))
936 (outline-next-heading)
937 (org-end-of-subtree t
)
939 (if (looking-at "[ \t]*\n")
940 (beginning-of-line 2)
943 (org-paste-subtree (org-get-valid-level level
1) txt
)
944 (and org-auto-align-tags
(org-set-tags nil t
))
945 (bookmark-set "org-remember-last-stored")
946 (move-marker org-remember-last-stored-marker
(point)))
949 (org-paste-subtree level txt
)
950 (and org-auto-align-tags
(org-set-tags nil t
))
951 (bookmark-set "org-remember-last-stored")
952 (move-marker org-remember-last-stored-marker
(point)))
955 (org-end-of-subtree t
)
956 (org-paste-subtree level txt
)
957 (and org-auto-align-tags
(org-set-tags nil t
))
958 (bookmark-set "org-remember-last-stored")
959 (move-marker org-remember-last-stored-marker
(point)))
960 (t (error "This should not happen"))))
962 ((eq heading
'bottom
)
963 (org-paste-subtree 1 txt
)
964 (and org-auto-align-tags
(org-set-tags nil t
))
965 (bookmark-set "org-remember-last-stored")
966 (move-marker org-remember-last-stored-marker
(point)))
968 ((and (bobp) (not reversed
))
969 ;; Put it at the end, one level below level 1
972 (goto-char (point-max))
973 (if (not (bolp)) (newline))
974 (org-paste-subtree (org-get-valid-level 1 1) txt
)
975 (and org-auto-align-tags
(org-set-tags nil t
))
976 (bookmark-set "org-remember-last-stored")
977 (move-marker org-remember-last-stored-marker
(point))))
979 ((and (bobp) reversed
)
980 ;; Put it at the start, as level 1
983 (goto-char (point-min))
984 (re-search-forward "^\\*+ " nil t
)
985 (beginning-of-line 1)
986 (org-paste-subtree 1 txt
)
987 (and org-auto-align-tags
(org-set-tags nil t
))
988 (bookmark-set "org-remember-last-stored")
989 (move-marker org-remember-last-stored-marker
(point))))
991 ;; Put it right there, with automatic level determined by
992 ;; org-paste-subtree or from prefix arg
994 (if (numberp current-prefix-arg
) current-prefix-arg
)
996 (and org-auto-align-tags
(org-set-tags nil t
))
997 (bookmark-set "org-remember-last-stored")
998 (move-marker org-remember-last-stored-marker
(point))))
1000 (when remember-save-after-remembering
1002 (if (and (not visiting
)
1003 (not (equal (marker-buffer org-clock-marker
)
1005 (kill-buffer (current-buffer))))
1006 (when org-remember-auto-remove-backup-files
1009 (delete-file backup-file
)
1010 (delete-file (concat backup-file
"~"))))
1011 (when org-remember-backup-directory
1014 org-remember-backup-directory nil
1015 "^remember-.*[0-9]$"))))
1018 "%d backup files (unfinished remember calls) in %s"
1019 n org-remember-backup-directory
))))))))))
1021 t
) ;; return t to indicate that we took care of this note.
1023 (defun org-do-remember (&optional initial
)
1027 (defun org-require-remember ()
1028 "Make sure remember is loaded, or install our own emergency version of it."
1032 ;; Lets install our own micro version of remember
1033 (defvar remember-register ?R
)
1034 (defvar remember-mode-hook nil
)
1035 (defvar remember-handler-functions nil
)
1036 (defvar remember-buffer
"*Remember*")
1037 (defvar remember-save-after-remembering t
)
1038 (defvar remember-annotation-functions
'(buffer-file-name))
1039 (defun remember-finalize ()
1040 (run-hook-with-args-until-success 'remember-handler-functions
)
1041 (when (equal remember-buffer
(buffer-name))
1042 (kill-buffer (current-buffer))
1043 (jump-to-register remember-register
)))
1044 (defun remember-mode ()
1046 (setq mode-name
"Remember")
1047 (run-hooks 'remember-mode-hook
))
1048 (defun remember (&optional initial
)
1049 (window-configuration-to-register remember-register
)
1050 (let* ((annotation (run-hook-with-args-until-success
1051 'remember-annotation-functions
)))
1052 (switch-to-buffer-other-window (get-buffer-create remember-buffer
))
1054 (defun remember-buffer-desc ()
1055 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
1056 (point-at-eol)))))))
1058 (provide 'org-remember
)
1060 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
1062 ;;; org-remember.el ends here