Release 6.25b
[org-mode/org-tableheadings.git] / lisp / org-remember.el
blobcc6699e09eb0e7cabf869ff7c520156c80f810c3
1 ;;; org-remember.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.25b
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the system to take fast notes with Org-mode.
30 ;; This system is used together with John Wiegley's `remember.el'.
32 ;;; Code:
34 (eval-when-compile
35 (require 'cl))
36 (require 'org)
38 (declare-function remember-mode "remember" ())
39 (declare-function remember "remember" (&optional initial))
40 (declare-function remember-buffer-desc "remember" ())
41 (declare-function remember-finalize "remember" ())
42 (defvar remember-save-after-remembering)
43 (defvar remember-register)
44 (defvar remember-buffer)
45 (defvar remember-handler-functions)
46 (defvar remember-annotation-functions)
47 (defvar org-clock-heading)
48 (defvar org-clock-heading-for-remember)
50 (defgroup org-remember nil
51 "Options concerning interaction with remember.el."
52 :tag "Org Remember"
53 :group 'org)
55 (defcustom org-remember-store-without-prompt t
56 "Non-nil means, `C-c C-c' stores remember note without further prompts.
57 It then uses the file and headline specified by the template or (if the
58 template does not specify them) by the variables `org-default-notes-file'
59 and `org-remember-default-headline'. To force prompting anyway, use
60 `C-u C-c C-c' to file the note.
62 When this variable is nil, `C-c C-c' gives you the prompts, and
63 `C-u C-c C-c' triggers the fasttrack."
64 :group 'org-remember
65 :type 'boolean)
67 (defcustom org-remember-interactive-interface 'refile
68 "The interface to be used for interactive filing of remember notes.
69 This is only used when the interactive mode for selecting a filing
70 location is used (see the variable `org-remember-store-without-prompt').
71 Allowed values are:
72 outline The interface shows an outline of the relevant file
73 and the correct heading is found by moving through
74 the outline or by searching with incremental search.
75 outline-path-completion Headlines in the current buffer are offered via
76 completion.
77 refile Use the refile interface, and offer headlines,
78 possibly from different buffers."
79 :group 'org-remember
80 :type '(choice
81 (const :tag "Refile" refile)
82 (const :tag "Outline" outline)
83 (const :tag "Outline-path-completion" outline-path-completion)))
85 (defcustom org-remember-default-headline ""
86 "The headline that should be the default location in the notes file.
87 When filing remember notes, the cursor will start at that position.
88 You can set this on a per-template basis with the variable
89 `org-remember-templates'."
90 :group 'org-remember
91 :type 'string)
93 (defcustom org-remember-templates nil
94 "Templates for the creation of remember buffers.
95 When nil, just let remember make the buffer.
96 When non-nil, this is a list of 5-element lists. In each entry, the first
97 element is the name of the template, which should be a single short word.
98 The second element is a character, a unique key to select this template.
99 The third element is the template.
101 The fourth element is optional and can specify a destination file for
102 remember items created with this template. The default file is given
103 by `org-default-notes-file'. If the file name is not an absolute path,
104 it will be interpreted relative to `org-directory'.
106 An optional fifth element can specify the headline in that file that should
107 be offered first when the user is asked to file the entry. The default
108 headline is given in the variable `org-remember-default-headline'. When
109 this element is `top' or `bottom', the note will be placed as a level-1
110 entry at the beginning or end of the file, respectively.
112 An optional sixth element specifies the contexts in which the template
113 will be offered to the user. This element can be a list of major modes
114 or a function, and the template will only be offered if `org-remember'
115 is called from a mode in the list, or if the function returns t.
116 Templates that specify t or nil for the context will be always be added
117 to the list of selectable templates.
119 The template specifies the structure of the remember buffer. It should have
120 a first line starting with a star, to act as the org-mode headline.
121 Furthermore, the following %-escapes will be replaced with content:
123 %^{prompt} Prompt the user for a string and replace this sequence with it.
124 A default value and a completion table ca be specified like this:
125 %^{prompt|default|completion2|completion3|...}
126 %t time stamp, date only
127 %T time stamp with date and time
128 %u, %U like the above, but inactive time stamps
129 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
130 You may define a prompt like %^{Please specify birthday
131 %n user name (taken from `user-full-name')
132 %a annotation, normally the link created with org-store-link
133 %i initial content, 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"
169 :group 'org-remember
170 :get (lambda (var) ; Make sure all entries have at least 5 elements
171 (mapcar (lambda (x)
172 (if (not (stringp (car x))) (setq x (cons "" x)))
173 (cond ((= (length x) 4) (append x '(nil)))
174 ((= (length x) 3) (append x '(nil nil)))
175 (t x)))
176 (default-value var)))
177 :type '(repeat
178 :tag "enabled"
179 (list :value ("" ?a "\n" nil nil nil)
180 (string :tag "Name")
181 (character :tag "Selection Key")
182 (string :tag "Template")
183 (choice :tag "Destination file"
184 (file :tag "Specify")
185 (function :tag "Function")
186 (const :tag "Use `org-default-notes-file'" nil))
187 (choice :tag "Destin. headline"
188 (string :tag "Specify")
189 (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."
202 :group 'org-remember
203 :type 'hook)
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. "
225 :group 'org-remember
226 :type '(choice
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'."
237 :group 'org-remember
238 :type '(choice
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
247 remember sessions.
248 Backup files will only be made at all, when `org-remember-backup-directory'
249 is set."
250 :group 'org-remember
251 :type 'boolean)
253 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
254 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
256 ;;;###autoload
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))
264 ;;;###autoload
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
292 (mapcar
293 (lambda (tpl)
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)))))
303 tpl)))
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)))
321 (char (or use-char
322 (cond
323 ((= (length templates) 1)
324 (caar templates))
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"
332 (mapconcat
333 (lambda (x)
334 (cond
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)))))
342 templates " "))
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))
347 char0))))))
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)))))
356 ;;;###autoload
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))
368 (ct1
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))
371 ct))
372 (tpl (car entry))
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))))
378 (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)
393 ""))
394 (v-a (or (plist-get org-store-link-plist :annotation)
395 (and (boundp 'annotation) annotation)
396 ""))
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)
403 v-c)))
404 (v-A (if (and v-a
405 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
406 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
407 v-a))
408 (v-n user-full-name)
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))
414 org-clock-heading)))
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)
428 org-store-link-plist
429 (plist-put org-store-link-plist :initial v-i))
431 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
432 (erase-buffer)
433 (insert (substitute-command-keys
434 (format
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))
443 (or headline "")
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"))))
447 (insert tpl)
448 (goto-char (point-min))
450 ;; Simple %-escapes
451 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
452 (when (and initial (equal (match-string 0) "%i"))
453 (save-match-data
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))))))
459 (replace-match
460 (or (eval (intern (concat "v-" (match-string 1)))) "")
461 t t))
463 ;; %[] Insert contents of a file.
464 (goto-char (point-min))
465 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
466 (let ((start (match-beginning 0))
467 (end (match-end 0))
468 (filename (expand-file-name (match-string 1))))
469 (goto-char start)
470 (delete-region start end)
471 (condition-case error
472 (insert-file-contents filename)
473 (error (insert (format "%%![Couldn't insert %s: %s]"
474 filename error))))))
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)))
480 (forward-char 1)
481 (let ((result
482 (condition-case error
483 (eval (read (current-buffer)))
484 (error (format "%%![Error: %s]" error)))))
485 (delete-region template-start (point))
486 (insert result))))
488 ;; From the property list
489 (when plist-p
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))
500 (if headline
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))
508 (replace-match "")
509 (setq completions nil default nil)
510 (when prompt
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::"
516 (or prompt "")))
517 completions (mapcar 'list completions)))
518 (cond
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
527 'org-tags-history)))
528 (setq ins (mapconcat 'identity
529 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
530 ":"))
531 (when (string-match "\\S-" ins)
532 (or (equal (char-before) ?:) (insert ":"))
533 (insert ins)
534 (or (equal (char-after) ?:) (insert ":")))))
535 ((equal char "C")
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))))))
541 ((equal char "L")
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: "
546 (car clipboards)
547 '(clipboards . 1)
548 (car clipboards))))))
549 ((equal char "p")
550 (let*
551 ((prop (org-substring-no-properties prompt))
552 (pall (concat prop "_ALL"))
553 (allowed
554 (with-current-buffer
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 ": "))
563 (val (if allowed
564 (org-completing-read
565 propprompt
566 (mapcar 'list (org-split-string allowed "[ \t]+"))
567 nil 'req-match)
568 (org-completing-read-no-ido propprompt existing nil nil
569 "" nil ""))))
570 (org-set-property prop val)))
571 (char
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
575 prompt))
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 "]"))
584 ": ")
585 completions nil nil nil histvar default))))))
586 (goto-char (point-min))
587 (if (re-search-forward "%\\?" nil t)
588 (replace-match "")
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))
594 (replace-match "")
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
600 (expand-file-name
601 (format-time-string "remember-%Y-%m-%d-%H-%M-%S")
602 org-remember-backup-directory))
603 (save-buffer)
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))
608 (replace-match "")
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
614 from that hook."
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
621 from that hook."
622 (org-remember '(16))
623 (goto-char (or (text-property-any
624 (point) (save-excursion (org-end-of-subtree t t))
625 'org-position-cursor t)
626 (point)))
627 (message "%s"
628 (format
629 (substitute-command-keys
630 "Restore window configuration with \\[jump-to-register] %c")
631 remember-register)))
633 (defvar org-clock-marker) ; Defined in org.el
634 (defun org-remember-finalize ()
635 "Finalize the remember process."
636 (interactive)
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
651 (save-buffer))
652 (remember-finalize))
654 (defun org-remember-kill ()
655 "Abort the current remember process."
656 (interactive)
657 (let ((org-note-abort t))
658 (org-remember-finalize)))
660 ;;;###autoload
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'."
673 (interactive "P")
674 (org-require-remember)
675 (cond
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
684 (progn
685 (when (< (length org-remember-templates) 2)
686 (error "No other template available"))
687 (erase-buffer)
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."
701 (interactive)
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."
709 (interactive)
710 (let* (org-select-template-temp-major-mode
711 (entry (org-select-remember-template template-key))
712 (file (nth 1 entry))
713 (heading (nth 2 entry))
714 visiting)
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)))
724 (widen)
725 (goto-char (point-min))
726 (if (re-search-forward
727 (concat "^\\*+[ \t]+" (regexp-quote heading)
728 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
729 nil t)
730 (goto-char (match-beginning 0))
731 (error "Target headline not found: %s" heading))))
733 ;;;###autoload
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
749 currently running.
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))))
770 (beg (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")
775 (replace-match ""))
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))
781 (catch 'quit
782 (if org-note-abort (throw 'quit nil))
783 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
784 (backup-file
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))
791 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))
797 (file (cond
798 (fastp org-default-notes-file)
799 ((and (eq org-remember-interactive-interface 'refile)
800 org-refile-targets)
801 org-default-notes-file)
802 ((not previousp)
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)
810 (when (equal current-prefix-arg '(4))
811 (setq visitp t))
812 (when previousp
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)
816 fastp t))
817 (when clockp
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
821 fastp t))
822 (setq current-prefix-arg nil)
823 ;; Modify text so that it becomes a nice subtree which can be inserted
824 ;; into an org tree.
825 (goto-char (point-min))
826 (if (re-search-forward "[ \t\n]+\\'" nil t)
827 ;; remove empty lines at end
828 (replace-match ""))
829 (goto-char (point-min))
830 (unless (looking-at org-outline-regexp)
831 ;; add a headline
832 (insert (concat "* " (current-time-string)
833 " (" (remember-buffer-desc) ")\n"))
834 (backward-char 1)
835 (when org-adapt-indentation
836 (while (re-search-forward "^" nil t)
837 (insert " "))))
838 (goto-char (point-min))
839 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
840 (replace-match "\n\n")
841 (if (re-search-forward "[ \t\n]*\\'")
842 (replace-match "\n")))
843 (goto-char (point-min))
844 (setq txt (buffer-string))
845 (org-save-markers-in-region (point-min) (point-max))
846 (set-buffer-modified-p nil)
847 (when (and (eq org-remember-interactive-interface 'refile)
848 (not fastp))
849 (org-refile nil (or visiting (find-file-noselect file)))
850 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
851 (save-excursion
852 (bookmark-jump "org-refile-last-stored")
853 (bookmark-set "org-remember-last-stored")
854 (move-marker org-remember-last-stored-marker (point)))
855 (throw 'quit t))
856 ;; Find the file
857 (with-current-buffer (or visiting (find-file-noselect file))
858 (unless (org-mode-p)
859 (error "Target files for remember notes must be in Org-mode"))
860 (save-excursion
861 (save-restriction
862 (widen)
863 (and (goto-char (point-min))
864 (not (re-search-forward "^\\* " nil t))
865 (insert "\n* " (or (and (stringp heading) heading)
866 "Notes") "\n"))
867 (setq reversed (org-notes-order-reversed-p))
869 ;; Find the default location
870 (when heading
871 (cond
872 ((eq heading 'top)
873 (goto-char (point-min))
874 (or (looking-at org-outline-regexp)
875 (re-search-forward org-outline-regexp nil t))
876 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
877 ((eq heading 'bottom)
878 (goto-char (point-max))
879 (or (bolp) (newline))
880 (setq org-goto-start-pos (point)))
881 ((and (stringp heading) (string-match "\\S-" heading))
882 (goto-char (point-min))
883 (if (re-search-forward
884 (concat "^\\*+[ \t]+" (regexp-quote heading)
885 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
886 nil t)
887 (setq org-goto-start-pos (match-beginning 0))
888 (when fastp
889 (goto-char (point-max))
890 (unless (bolp) (newline))
891 (insert "* " heading "\n")
892 (setq org-goto-start-pos (point-at-bol 0)))))
893 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
894 heading 'top))))
896 ;; Ask the User for a location, using the appropriate interface
897 (cond
898 ((and fastp (memq heading '(top bottom)))
899 (setq spos org-goto-start-pos
900 exitcmd (if (eq heading 'top) 'left nil)))
901 (fastp (setq spos org-goto-start-pos
902 exitcmd 'return))
903 ((eq org-remember-interactive-interface 'outline)
904 (setq spos (org-get-location (current-buffer)
905 org-remember-help)
906 exitcmd (cdr spos)
907 spos (car spos)))
908 ((eq org-remember-interactive-interface 'outline-path-completion)
909 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
910 (org-refile-use-outline-path t))
911 (setq spos (org-refile-get-location "Heading: ")
912 exitcmd 'return
913 spos (nth 3 spos))))
914 (t (error "This should not happen")))
915 (if (not spos) (throw 'quit nil)) ; return nil to show we did
916 ; not handle this note
917 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
918 (goto-char spos)
919 (cond ((org-on-heading-p t)
920 (org-back-to-heading t)
921 (setq level (funcall outline-level))
922 (cond
923 ((eq exitcmd 'return)
924 ;; sublevel of current
925 (setq org-remember-previous-location
926 (cons (abbreviate-file-name file)
927 (org-get-heading 'notags)))
928 (if reversed
929 (outline-next-heading)
930 (org-end-of-subtree t)
931 (if (not (bolp))
932 (if (looking-at "[ \t]*\n")
933 (beginning-of-line 2)
934 (end-of-line 1)
935 (insert "\n"))))
936 (org-paste-subtree (org-get-valid-level level 1) txt)
937 (and org-auto-align-tags (org-set-tags nil t))
938 (bookmark-set "org-remember-last-stored")
939 (move-marker org-remember-last-stored-marker (point)))
940 ((eq exitcmd 'left)
941 ;; before current
942 (org-paste-subtree level txt)
943 (and org-auto-align-tags (org-set-tags nil t))
944 (bookmark-set "org-remember-last-stored")
945 (move-marker org-remember-last-stored-marker (point)))
946 ((eq exitcmd 'right)
947 ;; after current
948 (org-end-of-subtree t)
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)))
953 (t (error "This should not happen"))))
955 ((eq heading 'bottom)
956 (org-paste-subtree 1 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)))
961 ((and (bobp) (not reversed))
962 ;; Put it at the end, one level below level 1
963 (save-restriction
964 (widen)
965 (goto-char (point-max))
966 (if (not (bolp)) (newline))
967 (org-paste-subtree (org-get-valid-level 1 1) txt)
968 (and org-auto-align-tags (org-set-tags nil t))
969 (bookmark-set "org-remember-last-stored")
970 (move-marker org-remember-last-stored-marker (point))))
972 ((and (bobp) reversed)
973 ;; Put it at the start, as level 1
974 (save-restriction
975 (widen)
976 (goto-char (point-min))
977 (re-search-forward "^\\*+ " nil t)
978 (beginning-of-line 1)
979 (org-paste-subtree 1 txt)
980 (and org-auto-align-tags (org-set-tags nil t))
981 (bookmark-set "org-remember-last-stored")
982 (move-marker org-remember-last-stored-marker (point))))
984 ;; Put it right there, with automatic level determined by
985 ;; org-paste-subtree or from prefix arg
986 (org-paste-subtree
987 (if (numberp current-prefix-arg) current-prefix-arg)
988 txt)
989 (and org-auto-align-tags (org-set-tags nil t))
990 (bookmark-set "org-remember-last-stored")
991 (move-marker org-remember-last-stored-marker (point))))
993 (when remember-save-after-remembering
994 (save-buffer)
995 (if (and (not visiting)
996 (not (equal (marker-buffer org-clock-marker)
997 (current-buffer))))
998 (kill-buffer (current-buffer))))
999 (when org-remember-auto-remove-backup-files
1000 (when backup-file
1001 (ignore-errors
1002 (delete-file backup-file)
1003 (delete-file (concat backup-file "~"))))
1004 (when org-remember-backup-directory
1005 (let ((n (length
1006 (directory-files
1007 org-remember-backup-directory nil
1008 "^remember-.*[0-9]$"))))
1009 (when (> n 0)
1010 (message
1011 "%d backup files (unfinished remember calls) in %s"
1012 n org-remember-backup-directory))))))))))
1014 t) ;; return t to indicate that we took care of this note.
1016 (defun org-do-remember (&optional initial)
1017 "Call remember."
1018 (remember initial))
1020 (defun org-require-remember ()
1021 "Make sure remember is loaded, or install our own emergency version of it."
1022 (condition-case nil
1023 (require 'remember)
1024 (error
1025 ;; Lets install our own micro version of remember
1026 (defvar remember-register ?R)
1027 (defvar remember-mode-hook nil)
1028 (defvar remember-handler-functions nil)
1029 (defvar remember-buffer "*Remember*")
1030 (defvar remember-save-after-remembering t)
1031 (defvar remember-annotation-functions '(buffer-file-name))
1032 (defun remember-finalize ()
1033 (run-hook-with-args-until-success 'remember-handler-functions)
1034 (when (equal remember-buffer (buffer-name))
1035 (kill-buffer (current-buffer))
1036 (jump-to-register remember-register)))
1037 (defun remember-mode ()
1038 (fundamental-mode)
1039 (setq mode-name "Remember")
1040 (run-hooks 'remember-mode-hook))
1041 (defun remember (&optional initial)
1042 (window-configuration-to-register remember-register)
1043 (let* ((annotation (run-hook-with-args-until-success
1044 'remember-annotation-functions)))
1045 (switch-to-buffer-other-window (get-buffer-create remember-buffer))
1046 (remember-mode)))
1047 (defun remember-buffer-desc ()
1048 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
1049 (point-at-eol)))))))
1051 (provide 'org-remember)
1053 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
1055 ;;; org-remember.el ends here