Remember: Keep changes to `auto-save-visited-file-name' buffer-local
[org-mode.git] / lisp / org-remember.el
blob8ac8452af068e6bbfb0f72b302c872b9c525fde9
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.24trans
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 :group 'org-remember
237 :type '(choice
238 (const :tag "No backups" nil)
239 (directory :tag "Directory")))
241 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
242 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
244 ;;;###autoload
245 (defun org-remember-insinuate ()
246 "Setup remember.el for use with Org-mode."
247 (org-require-remember)
248 (setq remember-annotation-functions '(org-remember-annotation))
249 (setq remember-handler-functions '(org-remember-handler))
250 (add-hook 'remember-mode-hook 'org-remember-apply-template))
252 ;;;###autoload
253 (defun org-remember-annotation ()
254 "Return a link to the current location as an annotation for remember.el.
255 If you are using Org-mode files as target for data storage with
256 remember.el, then the annotations should include a link compatible with the
257 conventions in Org-mode. This function returns such a link."
258 (org-store-link nil))
260 (defconst org-remember-help
261 "Select a destination location for the note.
262 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
263 RET on headline -> Store as sublevel entry to current headline
264 RET at beg-of-buf -> Append to file as level 2 headline
265 <left>/<right> -> before/after current headline, same headings level")
267 (defvar org-jump-to-target-location nil)
268 (defvar org-remember-previous-location nil)
269 (defvar org-force-remember-template-char) ;; dynamically scoped
271 ;; Save the major mode of the buffer we called remember from
272 (defvar org-select-template-temp-major-mode nil)
274 ;; Temporary store the buffer where remember was called from
275 (defvar org-select-template-original-buffer nil)
277 (defun org-select-remember-template (&optional use-char)
278 (when org-remember-templates
279 (let* ((pre-selected-templates
280 (mapcar
281 (lambda (tpl)
282 (let ((ctxt (nth 5 tpl))
283 (mode org-select-template-temp-major-mode)
284 (buf org-select-template-original-buffer))
285 (and (or (not ctxt) (eq ctxt t)
286 (and (listp ctxt) (memq mode ctxt))
287 (and (functionp ctxt)
288 (with-current-buffer buf
289 ;; Protect the user-defined function from error
290 (condition-case nil (funcall ctxt) (error nil)))))
291 tpl)))
292 org-remember-templates))
293 ;; If no template at this point, add the default templates:
294 (pre-selected-templates1
295 (if (not (delq nil pre-selected-templates))
296 (mapcar (lambda(x) (if (not (nth 5 x)) x))
297 org-remember-templates)
298 pre-selected-templates))
299 ;; Then unconditionally add template for any contexts
300 (pre-selected-templates2
301 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
302 org-remember-templates)
303 (delq nil pre-selected-templates1)))
304 (templates (mapcar (lambda (x)
305 (if (stringp (car x))
306 (append (list (nth 1 x) (car x)) (cddr x))
307 (append (list (car x) "") (cdr x))))
308 (delq nil pre-selected-templates2)))
309 (char (or use-char
310 (cond
311 ((= (length templates) 1)
312 (caar templates))
313 ((and (boundp 'org-force-remember-template-char)
314 org-force-remember-template-char)
315 (if (stringp org-force-remember-template-char)
316 (string-to-char org-force-remember-template-char)
317 org-force-remember-template-char))
319 (message "Select template: %s"
320 (mapconcat
321 (lambda (x)
322 (cond
323 ((not (string-match "\\S-" (nth 1 x)))
324 (format "[%c]" (car x)))
325 ((equal (downcase (car x))
326 (downcase (aref (nth 1 x) 0)))
327 (format "[%c]%s" (car x)
328 (substring (nth 1 x) 1)))
329 (t (format "[%c]%s" (car x) (nth 1 x)))))
330 templates " "))
331 (let ((inhibit-quit t) (char0 (read-char-exclusive)))
332 (when (equal char0 ?\C-g)
333 (jump-to-register remember-register)
334 (kill-buffer remember-buffer))
335 char0))))))
336 (cddr (assoc char templates)))))
338 (defun org-get-x-clipboard (value)
339 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
340 (if (eq window-system 'x)
341 (let ((x (org-get-x-clipboard-compat value)))
342 (if x (org-no-properties x)))))
344 ;;;###autoload
345 (defun org-remember-apply-template (&optional use-char skip-interactive)
346 "Initialize *remember* buffer with template, invoke `org-mode'.
347 This function should be placed into `remember-mode-hook' and in fact requires
348 to be run from that hook to function properly."
349 (when (and (boundp 'initial) (stringp initial))
350 (setq initial (org-no-properties initial))
351 (remove-text-properties 0 (length initial) '(read-only t) initial))
352 (if org-remember-templates
353 (let* ((entry (org-select-remember-template use-char))
354 (ct (or org-overriding-default-time (org-current-time)))
355 (dct (decode-time ct))
356 (ct1
357 (if (< (nth 2 dct) org-extend-today-until)
358 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
359 ct))
360 (tpl (car entry))
361 (plist-p (if org-store-link-plist t nil))
362 (file (if (and (nth 1 entry)
363 (or (and (stringp (nth 1 entry))
364 (string-match "\\S-" (nth 1 entry)))
365 (functionp (nth 1 entry))))
366 (nth 1 entry)
367 org-default-notes-file))
368 (headline (nth 2 entry))
369 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
370 (v-x (or (org-get-x-clipboard 'PRIMARY)
371 (org-get-x-clipboard 'CLIPBOARD)
372 (org-get-x-clipboard 'SECONDARY)))
373 (v-t (format-time-string (car org-time-stamp-formats) ct))
374 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
375 (v-u (concat "[" (substring v-t 1 -1) "]"))
376 (v-U (concat "[" (substring v-T 1 -1) "]"))
377 ;; `initial' and `annotation' are bound in `remember'
378 (v-i (if (boundp 'initial) initial))
379 (v-a (if (and (boundp 'annotation) annotation)
380 (if (equal annotation "[[]]") "" annotation)
381 ""))
382 (clipboards (remove nil (list v-i
383 (org-get-x-clipboard 'PRIMARY)
384 (org-get-x-clipboard 'CLIPBOARD)
385 (org-get-x-clipboard 'SECONDARY)
386 v-c)))
387 (v-A (if (and v-a
388 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
389 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
390 v-a))
391 (v-n user-full-name)
392 (v-k (if (marker-buffer org-clock-marker)
393 (org-substring-no-properties org-clock-heading)))
394 (v-K (if (marker-buffer org-clock-marker)
395 (org-make-link-string
396 (buffer-file-name (marker-buffer org-clock-marker))
397 org-clock-heading)))
399 (org-startup-folded nil)
400 (org-inhibit-startup t)
401 org-time-was-given org-end-time-was-given x
402 prompt completions char time pos default histvar)
404 (when (functionp file)
405 (setq file (funcall file)))
406 (when (and file (not (file-name-absolute-p file)))
407 (setq file (expand-file-name file org-directory)))
410 (setq org-store-link-plist
411 (append (list :annotation v-a :initial v-i)
412 org-store-link-plist))
413 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
414 (erase-buffer)
415 (insert (substitute-command-keys
416 (format
417 "## %s \"%s\" -> \"* %s\"
418 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
419 ## C-0 C-c C-c \"%s\" -> \"* %s\"
420 ## %s to select file and header location interactively.
421 ## C-2 C-c C-c as child of the currently clocked item
422 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
423 (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
424 (abbreviate-file-name (or file org-default-notes-file))
425 (or headline "")
426 (or (car org-remember-previous-location) "???")
427 (or (cdr org-remember-previous-location) "???")
428 (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
429 (insert tpl)
430 (goto-char (point-min))
432 ;; Simple %-escapes
433 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
434 (when (and initial (equal (match-string 0) "%i"))
435 (save-match-data
436 (let* ((lead (buffer-substring
437 (point-at-bol) (match-beginning 0))))
438 (setq v-i (mapconcat 'identity
439 (org-split-string initial "\n")
440 (concat "\n" lead))))))
441 (replace-match
442 (or (eval (intern (concat "v-" (match-string 1)))) "")
443 t t))
445 ;; %[] Insert contents of a file.
446 (goto-char (point-min))
447 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
448 (let ((start (match-beginning 0))
449 (end (match-end 0))
450 (filename (expand-file-name (match-string 1))))
451 (goto-char start)
452 (delete-region start end)
453 (condition-case error
454 (insert-file-contents filename)
455 (error (insert (format "%%![Couldn't insert %s: %s]"
456 filename error))))))
457 ;; %() embedded elisp
458 (goto-char (point-min))
459 (while (re-search-forward "%\\((.+)\\)" nil t)
460 (goto-char (match-beginning 0))
461 (let ((template-start (point)))
462 (forward-char 1)
463 (let ((result
464 (condition-case error
465 (eval (read (current-buffer)))
466 (error (format "%%![Error: %s]" error)))))
467 (delete-region template-start (point))
468 (insert result))))
470 ;; From the property list
471 (when plist-p
472 (goto-char (point-min))
473 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
474 (and (setq x (or (plist-get org-store-link-plist
475 (intern (match-string 1))) ""))
476 (replace-match x t t))))
478 ;; Turn on org-mode in the remember buffer, set local variables
479 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1))
480 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
481 (org-set-local 'org-default-notes-file file))
482 (if headline
483 (org-set-local 'org-remember-default-headline headline))
484 ;; Interactive template entries
485 (goto-char (point-min))
486 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
487 (setq char (if (match-end 3) (match-string 3))
488 prompt (if (match-end 2) (match-string 2)))
489 (goto-char (match-beginning 0))
490 (replace-match "")
491 (setq completions nil default nil)
492 (when prompt
493 (setq completions (org-split-string prompt "|")
494 prompt (pop completions)
495 default (car completions)
496 histvar (intern (concat
497 "org-remember-template-prompt-history::"
498 (or prompt "")))
499 completions (mapcar 'list completions)))
500 (cond
501 ((member char '("G" "g"))
502 (let* ((org-last-tags-completion-table
503 (org-global-tags-completion-table
504 (if (equal char "G") (org-agenda-files) (and file (list file)))))
505 (org-add-colon-after-tag-completion t)
506 (ins (org-ido-completing-read
507 (if prompt (concat prompt ": ") "Tags: ")
508 'org-tags-completion-function nil nil nil
509 'org-tags-history)))
510 (setq ins (mapconcat 'identity
511 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
512 ":"))
513 (when (string-match "\\S-" ins)
514 (or (equal (char-before) ?:) (insert ":"))
515 (insert ins)
516 (or (equal (char-after) ?:) (insert ":")))))
517 ((equal char "C")
518 (cond ((= (length clipboards) 1) (insert (car clipboards)))
519 ((> (length clipboards) 1)
520 (insert (read-string "Clipboard/kill value: "
521 (car clipboards) '(clipboards . 1)
522 (car clipboards))))))
523 ((equal char "L")
524 (cond ((= (length clipboards) 1)
525 (org-insert-link 0 (car clipboards)))
526 ((> (length clipboards) 1)
527 (org-insert-link 0 (read-string "Clipboard/kill value: "
528 (car clipboards)
529 '(clipboards . 1)
530 (car clipboards))))))
531 ((equal char "p")
532 (let*
533 ((prop (org-substring-no-properties prompt))
534 (pall (concat prop "_ALL"))
535 (allowed
536 (with-current-buffer
537 (get-buffer (file-name-nondirectory file))
538 (or (cdr (assoc pall org-file-properties))
539 (cdr (assoc pall org-global-properties))
540 (cdr (assoc pall org-global-properties-fixed)))))
541 (existing (with-current-buffer
542 (get-buffer (file-name-nondirectory file))
543 (mapcar 'list (org-property-values prop))))
544 (propprompt (concat "Value for " prop ": "))
545 (val (if allowed
546 (org-completing-read
547 propprompt
548 (mapcar 'list (org-split-string allowed "[ \t]+"))
549 nil 'req-match)
550 (org-completing-read-no-ido propprompt existing nil nil
551 "" nil ""))))
552 (org-set-property prop val)))
553 (char
554 ;; These are the date/time related ones
555 (setq org-time-was-given (equal (upcase char) char))
556 (setq time (org-read-date (equal (upcase char) "U") t nil
557 prompt))
558 (org-insert-time-stamp time org-time-was-given
559 (member char '("u" "U"))
560 nil nil (list org-end-time-was-given)))
562 (let (org-completion-use-ido)
563 (insert (org-completing-read-no-ido
564 (concat (if prompt prompt "Enter string")
565 (if default (concat " [" default "]"))
566 ": ")
567 completions nil nil nil histvar default))))))
568 (goto-char (point-min))
569 (if (re-search-forward "%\\?" nil t)
570 (replace-match "")
571 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
572 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1)))
573 (when (save-excursion
574 (goto-char (point-min))
575 (re-search-forward "%&" nil t))
576 (replace-match "")
577 (org-set-local 'org-jump-to-target-location t))
578 (when org-remember-backup-directory
579 (unless (file-directory-p org-remember-backup-directory)
580 (make-directory org-remember-backup-directory))
581 (setq buffer-file-name
582 (expand-file-name
583 (format-time-string "remember-%Y-%m-%d-%H-%M-%S")
584 org-remember-backup-directory))
585 (save-buffer)
586 (org-set-local 'auto-save-visited-file-name t))
587 (when (save-excursion
588 (goto-char (point-min))
589 (re-search-forward "%!" nil t))
590 (replace-match "")
591 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
593 (defun org-remember-finish-immediately ()
594 "File remember note immediately.
595 This should be run in `post-command-hook' and will remove itself
596 from that hook."
597 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
598 (org-remember-finalize))
600 (defun org-remember-visit-immediately ()
601 "File remember note immediately.
602 This should be run in `post-command-hook' and will remove itself
603 from that hook."
604 (org-remember '(16))
605 (goto-char (or (text-property-any
606 (point) (save-excursion (org-end-of-subtree t t))
607 'org-position-cursor t)
608 (point)))
609 (message "%s"
610 (format
611 (substitute-command-keys
612 "Restore window configuration with \\[jump-to-register] %c")
613 remember-register)))
615 (defvar org-clock-marker) ; Defined in org.el
616 (defun org-remember-finalize ()
617 "Finalize the remember process."
618 (interactive)
619 (unless org-remember-mode
620 (error "This does not seem to be a remember buffer for Org-mode"))
621 (run-hooks 'org-remember-before-finalize-hook)
622 (unless (fboundp 'remember-finalize)
623 (defalias 'remember-finalize 'remember-buffer))
624 (when (and org-clock-marker
625 (equal (marker-buffer org-clock-marker) (current-buffer)))
626 ;; the clock is running in this buffer.
627 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
628 (or (eq org-remember-clock-out-on-exit t)
629 (and org-remember-clock-out-on-exit
630 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
631 (let (org-log-note-clock-out) (org-clock-out))))
632 (when buffer-file-name
633 (save-buffer)
634 (setq buffer-file-name nil))
635 (remember-finalize))
637 (defun org-remember-kill ()
638 "Abort the current remember process."
639 (interactive)
640 (let ((org-note-abort t))
641 (org-remember-finalize)))
643 ;;;###autoload
644 (defun org-remember (&optional goto org-force-remember-template-char)
645 "Call `remember'. If this is already a remember buffer, re-apply template.
646 If there is an active region, make sure remember uses it as initial content
647 of the remember buffer.
649 When called interactively with a `C-u' prefix argument GOTO, don't remember
650 anything, just go to the file/headline where the selected template usually
651 stores its notes. With a double prefix arg `C-u C-u', go to the last
652 note stored by remember.
654 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
655 associated with a template in `org-remember-templates'."
656 (interactive "P")
657 (org-require-remember)
658 (cond
659 ((equal goto '(4)) (org-go-to-remember-target))
660 ((equal goto '(16)) (org-remember-goto-last-stored))
662 ;; set temporary variables that will be needed in
663 ;; `org-select-remember-template'
664 (setq org-select-template-temp-major-mode major-mode)
665 (setq org-select-template-original-buffer (current-buffer))
666 (if org-remember-mode
667 (progn
668 (when (< (length org-remember-templates) 2)
669 (error "No other template available"))
670 (erase-buffer)
671 (let ((annotation (plist-get org-store-link-plist :annotation))
672 (initial (plist-get org-store-link-plist :initial)))
673 (org-remember-apply-template))
674 (message "Press C-c C-c to remember data"))
675 (if (org-region-active-p)
676 (org-do-remember (buffer-substring (point) (mark)))
677 (org-do-remember))))))
679 (defvar org-remember-last-stored-marker (make-marker)
680 "Marker pointing to the entry most recently stored with `org-remember'.")
682 (defun org-remember-goto-last-stored ()
683 "Go to the location where the last remember note was stored."
684 (interactive)
685 (org-goto-marker-or-bmk org-remember-last-stored-marker
686 "org-remember-last-stored")
687 (message "This is the last note stored by remember"))
689 (defun org-go-to-remember-target (&optional template-key)
690 "Go to the target location of a remember template.
691 The user is queried for the template."
692 (interactive)
693 (let* (org-select-template-temp-major-mode
694 (entry (org-select-remember-template template-key))
695 (file (nth 1 entry))
696 (heading (nth 2 entry))
697 visiting)
698 (unless (and file (stringp file) (string-match "\\S-" file))
699 (setq file org-default-notes-file))
700 (when (and file (not (file-name-absolute-p file)))
701 (setq file (expand-file-name file org-directory)))
702 (unless (and heading (stringp heading) (string-match "\\S-" heading))
703 (setq heading org-remember-default-headline))
704 (setq visiting (org-find-base-buffer-visiting file))
705 (if (not visiting) (find-file-noselect file))
706 (switch-to-buffer (or visiting (get-file-buffer file)))
707 (widen)
708 (goto-char (point-min))
709 (if (re-search-forward
710 (concat "^\\*+[ \t]+" (regexp-quote heading)
711 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
712 nil t)
713 (goto-char (match-beginning 0))
714 (error "Target headline not found: %s" heading))))
716 ;;;###autoload
717 (defun org-remember-handler ()
718 "Store stuff from remember.el into an org file.
719 When the template has specified a file and a headline, the entry is filed
720 there, or in the location defined by `org-default-notes-file' and
721 `org-remember-default-headline'.
723 If no defaults have been defined, or if the current prefix argument
724 is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
725 process is used to select the target location.
727 When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
728 the entry is filed to the same location as the previous note.
730 When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
731 the entry is filed as a subentry of the entry where the clock is
732 currently running.
734 When `C-u' has been used as prefix argument, the note is stored and emacs
735 moves point to the new location of the note, so that editing can be
736 continued there (similar to inserting \"%&\" into the template).
738 Before storing the note, the function ensures that the text has an
739 org-mode-style headline, i.e. a first line that starts with
740 a \"*\". If not, a headline is constructed from the current date and
741 some additional data.
743 If the variable `org-adapt-indentation' is non-nil, the entire text is
744 also indented so that it starts in the same column as the headline
745 \(i.e. after the stars).
747 See also the variable `org-reverse-note-order'."
748 (when (and (equal current-prefix-arg 2)
749 (not (marker-buffer org-clock-marker)))
750 (error "No running clock"))
751 (when (org-bound-and-true-p org-jump-to-target-location)
752 (let* ((end (min (point-max) (1+ (point))))
753 (beg (point)))
754 (if (= end beg) (setq beg (1- beg)))
755 (put-text-property beg end 'org-position-cursor t)))
756 (goto-char (point-min))
757 (while (looking-at "^[ \t]*\n\\|^##.*\n")
758 (replace-match ""))
759 (goto-char (point-max))
760 (beginning-of-line 1)
761 (while (looking-at "[ \t]*$\\|##.*")
762 (delete-region (1- (point)) (point-max))
763 (beginning-of-line 1))
764 (catch 'quit
765 (if org-note-abort (throw 'quit nil))
766 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
767 (backup-file
768 (and buffer-file-name
769 (equal (file-name-directory buffer-file-name)
770 (file-name-as-directory
771 (expand-file-name org-remember-backup-directory)))
772 (string-match "^remember-[0-9]\\{4\\}"
773 (file-name-nondirectory buffer-file-name))
774 buffer-file-name))
775 (previousp (and (member current-prefix-arg '((16) 0))
776 org-remember-previous-location))
777 (clockp (equal current-prefix-arg 2))
778 (fastp (org-xor (equal current-prefix-arg 1)
779 org-remember-store-without-prompt))
780 (file (cond
781 (fastp org-default-notes-file)
782 ((and (eq org-remember-interactive-interface 'refile)
783 org-refile-targets)
784 org-default-notes-file)
785 ((not previousp)
786 (org-get-org-file))))
787 (heading org-remember-default-headline)
788 (visiting (and file (org-find-base-buffer-visiting file)))
789 (org-startup-folded nil)
790 (org-startup-align-all-tables nil)
791 (org-goto-start-pos 1)
792 spos exitcmd level reversed txt)
793 (when (equal current-prefix-arg '(4))
794 (setq visitp t))
795 (when previousp
796 (setq file (car org-remember-previous-location)
797 visiting (and file (org-find-base-buffer-visiting file))
798 heading (cdr org-remember-previous-location)
799 fastp t))
800 (when clockp
801 (setq file (buffer-file-name (marker-buffer org-clock-marker))
802 visiting (and file (org-find-base-buffer-visiting file))
803 heading org-clock-heading-for-remember
804 fastp t))
805 (setq current-prefix-arg nil)
806 ;; Modify text so that it becomes a nice subtree which can be inserted
807 ;; into an org tree.
808 (goto-char (point-min))
809 (if (re-search-forward "[ \t\n]+\\'" nil t)
810 ;; remove empty lines at end
811 (replace-match ""))
812 (goto-char (point-min))
813 (unless (looking-at org-outline-regexp)
814 ;; add a headline
815 (insert (concat "* " (current-time-string)
816 " (" (remember-buffer-desc) ")\n"))
817 (backward-char 1)
818 (when org-adapt-indentation
819 (while (re-search-forward "^" nil t)
820 (insert " "))))
821 (goto-char (point-min))
822 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
823 (replace-match "\n\n")
824 (if (re-search-forward "[ \t\n]*\\'")
825 (replace-match "\n")))
826 (goto-char (point-min))
827 (setq txt (buffer-string))
828 (org-save-markers-in-region (point-min) (point-max))
829 (when (and (eq org-remember-interactive-interface 'refile)
830 (not fastp))
831 (org-refile nil (or visiting (find-file-noselect file)))
832 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
833 (save-excursion
834 (bookmark-jump "org-refile-last-stored")
835 (bookmark-set "org-remember-last-stored")
836 (move-marker org-remember-last-stored-marker (point)))
837 (throw 'quit t))
838 ;; Find the file
839 (with-current-buffer (or visiting (find-file-noselect file))
840 (unless (org-mode-p)
841 (error "Target files for remember notes must be in Org-mode"))
842 (save-excursion
843 (save-restriction
844 (widen)
845 (and (goto-char (point-min))
846 (not (re-search-forward "^\\* " nil t))
847 (insert "\n* " (or (and (stringp heading) heading)
848 "Notes") "\n"))
849 (setq reversed (org-notes-order-reversed-p))
851 ;; Find the default location
852 (when heading
853 (cond
854 ((eq heading 'top)
855 (goto-char (point-min))
856 (or (looking-at org-outline-regexp)
857 (re-search-forward org-outline-regexp nil t))
858 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
859 ((eq heading 'bottom)
860 (goto-char (point-max))
861 (or (bolp) (newline))
862 (setq org-goto-start-pos (point)))
863 ((and (stringp heading) (string-match "\\S-" heading))
864 (goto-char (point-min))
865 (if (re-search-forward
866 (concat "^\\*+[ \t]+" (regexp-quote heading)
867 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
868 nil t)
869 (setq org-goto-start-pos (match-beginning 0))
870 (when fastp
871 (goto-char (point-max))
872 (unless (bolp) (newline))
873 (insert "* " heading "\n")
874 (setq org-goto-start-pos (point-at-bol 0)))))
875 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
876 heading 'top))))
878 ;; Ask the User for a location, using the appropriate interface
879 (cond
880 ((and fastp (memq heading '(top bottom)))
881 (setq spos org-goto-start-pos
882 exitcmd (if (eq heading 'top) 'left nil)))
883 (fastp (setq spos org-goto-start-pos
884 exitcmd 'return))
885 ((eq org-remember-interactive-interface 'outline)
886 (setq spos (org-get-location (current-buffer)
887 org-remember-help)
888 exitcmd (cdr spos)
889 spos (car spos)))
890 ((eq org-remember-interactive-interface 'outline-path-completion)
891 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
892 (org-refile-use-outline-path t))
893 (setq spos (org-refile-get-location "Heading: ")
894 exitcmd 'return
895 spos (nth 3 spos))))
896 (t (error "This should not happen")))
897 (if (not spos) (throw 'quit nil)) ; return nil to show we did
898 ; not handle this note
899 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
900 (goto-char spos)
901 (cond ((org-on-heading-p t)
902 (org-back-to-heading t)
903 (setq level (funcall outline-level))
904 (cond
905 ((eq exitcmd 'return)
906 ;; sublevel of current
907 (setq org-remember-previous-location
908 (cons (abbreviate-file-name file)
909 (org-get-heading 'notags)))
910 (if reversed
911 (outline-next-heading)
912 (org-end-of-subtree t)
913 (if (not (bolp))
914 (if (looking-at "[ \t]*\n")
915 (beginning-of-line 2)
916 (end-of-line 1)
917 (insert "\n"))))
918 (org-paste-subtree (org-get-valid-level level 1) txt)
919 (and org-auto-align-tags (org-set-tags nil t))
920 (bookmark-set "org-remember-last-stored")
921 (move-marker org-remember-last-stored-marker (point)))
922 ((eq exitcmd 'left)
923 ;; before current
924 (org-paste-subtree level txt)
925 (and org-auto-align-tags (org-set-tags nil t))
926 (bookmark-set "org-remember-last-stored")
927 (move-marker org-remember-last-stored-marker (point)))
928 ((eq exitcmd 'right)
929 ;; after current
930 (org-end-of-subtree t)
931 (org-paste-subtree level txt)
932 (and org-auto-align-tags (org-set-tags nil t))
933 (bookmark-set "org-remember-last-stored")
934 (move-marker org-remember-last-stored-marker (point)))
935 (t (error "This should not happen"))))
937 ((eq heading 'bottom)
938 (org-paste-subtree 1 txt)
939 (and org-auto-align-tags (org-set-tags nil t))
940 (bookmark-set "org-remember-last-stored")
941 (move-marker org-remember-last-stored-marker (point)))
943 ((and (bobp) (not reversed))
944 ;; Put it at the end, one level below level 1
945 (save-restriction
946 (widen)
947 (goto-char (point-max))
948 (if (not (bolp)) (newline))
949 (org-paste-subtree (org-get-valid-level 1 1) 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))))
954 ((and (bobp) reversed)
955 ;; Put it at the start, as level 1
956 (save-restriction
957 (widen)
958 (goto-char (point-min))
959 (re-search-forward "^\\*+ " nil t)
960 (beginning-of-line 1)
961 (org-paste-subtree 1 txt)
962 (and org-auto-align-tags (org-set-tags nil t))
963 (bookmark-set "org-remember-last-stored")
964 (move-marker org-remember-last-stored-marker (point))))
966 ;; Put it right there, with automatic level determined by
967 ;; org-paste-subtree or from prefix arg
968 (org-paste-subtree
969 (if (numberp current-prefix-arg) current-prefix-arg)
970 txt)
971 (and org-auto-align-tags (org-set-tags nil t))
972 (bookmark-set "org-remember-last-stored")
973 (move-marker org-remember-last-stored-marker (point))))
975 (when remember-save-after-remembering
976 (save-buffer)
977 (if (and (not visiting)
978 (not (equal (marker-buffer org-clock-marker)
979 (current-buffer))))
980 (kill-buffer (current-buffer))))
981 (when backup-file
982 (delete-file backup-file)))))))
984 t) ;; return t to indicate that we took care of this note.
986 (defun org-do-remember (&optional initial)
987 "Call remember."
988 (remember initial))
990 (defun org-require-remember ()
991 "Make sure remember is loaded, or install our own emergency version of it."
992 (condition-case nil
993 (require 'remember)
994 (error
995 ;; Lets install our own micro version of remember
996 (defvar remember-register ?R)
997 (defvar remember-mode-hook nil)
998 (defvar remember-handler-functions nil)
999 (defvar remember-buffer "*Remember*")
1000 (defvar remember-save-after-remembering t)
1001 (defvar remember-annotation-functions '(buffer-file-name))
1002 (defun remember-finalize ()
1003 (run-hook-with-args-until-success 'remember-handler-functions)
1004 (when (equal remember-buffer (buffer-name))
1005 (kill-buffer (current-buffer))
1006 (jump-to-register remember-register)))
1007 (defun remember-mode ()
1008 (fundamental-mode)
1009 (setq mode-name "Remember")
1010 (run-hooks 'remember-mode-hook))
1011 (defun remember (&optional initial)
1012 (window-configuration-to-register remember-register)
1013 (let* ((annotation (run-hook-with-args-until-success
1014 'remember-annotation-functions)))
1015 (switch-to-buffer-other-window (get-buffer-create remember-buffer))
1016 (remember-mode)))
1017 (defun remember-buffer-desc ()
1018 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
1019 (point-at-eol)))))))
1021 (provide 'org-remember)
1023 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
1025 ;;; org-remember.el ends here