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