Release 7.3
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-remember.el
bloba15825a51ecd2b4b198dda37b6179826b9c9d1c7
1 ;;; org-remember.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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: 7.3
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)
37 (require 'org-datetree)
39 (declare-function remember-mode "remember" ())
40 (declare-function remember "remember" (&optional initial))
41 (declare-function remember-buffer-desc "remember" ())
42 (declare-function remember-finalize "remember" ())
43 (defvar remember-save-after-remembering)
44 (defvar remember-register)
45 (defvar remember-buffer)
46 (defvar remember-handler-functions)
47 (defvar remember-annotation-functions)
48 (defvar org-clock-heading)
49 (defvar org-clock-heading-for-remember)
51 (defgroup org-remember nil
52 "Options concerning interaction with remember.el."
53 :tag "Org Remember"
54 :group 'org)
56 (defcustom org-remember-store-without-prompt t
57 "Non-nil means \\<org-remember-mode-map>\\[org-remember-finalize] \
58 stores the remember note without further prompts.
59 It then uses the file and headline specified by the template or (if the
60 template does not specify them) by the variables `org-default-notes-file'
61 and `org-remember-default-headline'. To force prompting anyway, use
62 \\[universal-argument] \\[org-remember-finalize] to file the note.
64 When this variable is nil, \\[org-remember-finalize] gives you the prompts, and
65 \\[universal-argument] \\[org-remember-finalize] triggers the fasttrack."
66 :group 'org-remember
67 :type 'boolean)
69 (defcustom org-remember-interactive-interface 'refile
70 "The interface to be used for interactive filing of remember notes.
71 This is only used when the interactive mode for selecting a filing
72 location is used (see the variable `org-remember-store-without-prompt').
73 Allowed values are:
74 outline The interface shows an outline of the relevant file
75 and the correct heading is found by moving through
76 the outline or by searching with incremental search.
77 outline-path-completion Headlines in the current buffer are offered via
78 completion.
79 refile Use the refile interface, and offer headlines,
80 possibly from different buffers."
81 :group 'org-remember
82 :type '(choice
83 (const :tag "Refile" refile)
84 (const :tag "Outline" outline)
85 (const :tag "Outline-path-completion" outline-path-completion)))
87 (defcustom org-remember-default-headline ""
88 "The headline that should be the default location in the notes file.
89 When filing remember notes, the cursor will start at that position.
90 You can set this on a per-template basis with the variable
91 `org-remember-templates'."
92 :group 'org-remember
93 :type 'string)
95 (defcustom org-remember-templates nil
96 "Templates for the creation of remember buffers.
97 When nil, just let remember make the buffer.
98 When non-nil, this is a list of (up to) 6-element lists. In each entry,
99 the first element is the name of the template, which should be a single
100 short word. The second element is a character, a unique key to select
101 this template. The third element is the template.
103 The fourth element is optional and can specify a destination file for
104 remember items created with this template. The default file is given
105 by `org-default-notes-file'. If the file name is not an absolute path,
106 it will be interpreted relative to `org-directory'.
108 An optional fifth element can specify the headline in that file that should
109 be offered first when the user is asked to file the entry. The default
110 headline is given in the variable `org-remember-default-headline'. When
111 this element is `top' or `bottom', the note will be placed as a level-1
112 entry at the beginning or end of the file, respectively.
114 An optional sixth element specifies the contexts in which the template
115 will be offered to the user. This element can be a list of major modes
116 or a function, and the template will only be offered if `org-remember'
117 is called from a mode in the list, or if the function returns t.
118 Templates that specify t or nil for the context will always be added
119 to the list of selectable templates.
121 The template specifies the structure of the remember buffer. It should have
122 a first line starting with a star, to act as the org-mode headline.
123 Furthermore, the following %-escapes will be replaced with content:
125 %^{PROMPT} prompt the user for a string and replace this sequence with it.
126 A default value and a completion table can be specified like this:
127 %^{prompt|default|completion2|completion3|...}
128 The arrow keys access a prompt-specific history.
129 %a annotation, normally the link created with `org-store-link'
130 %A like %a, but prompt for the description part
131 %i initial content, copied from the active region. If %i is
132 indented, the entire inserted text will be indented as well.
133 %t time stamp, date only
134 %T time stamp with date and time
135 %u, %U like the above, but inactive time stamps
136 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
137 You may define a prompt like %^{Please specify birthday}t
138 %n user name (taken from `user-full-name')
139 %c current kill ring head
140 %x content of the X clipboard
141 %:keyword specific information for certain link types, see below
142 %^C interactive selection of which kill or clip to use
143 %^L like %^C, but insert as link
144 %k title of the currently clocked task
145 %K link to the currently clocked task
146 %^g prompt for tags, completing tags in the target file
147 %^G prompt for tags, completing all tags in all agenda files
148 %^{PROP}p Prompt the user for a value for property PROP
149 %[PATHNAME] insert the contents of the file given by PATHNAME
150 %(SEXP) evaluate elisp `(SEXP)' and replace with the result
151 %! store this note immediately after completing the template\
152 \\<org-remember-mode-map>
153 (skipping the \\[org-remember-finalize] that normally triggers storing)
154 %& jump to target location immediately after storing note
155 %? after completing the template, position cursor here.
157 Apart from these general escapes, you can access information specific to the
158 link type that is created. For example, calling `remember' in emails or gnus
159 will record the author and the subject of the message, which you can access
160 with %:fromname and %:subject, respectively. Here is a complete list of what
161 is recorded for each link type.
163 Link type | Available information
164 -------------------+------------------------------------------------------
165 bbdb | %:type %:name %:company
166 vm, wl, mh, rmail | %:type %:subject %:message-id
167 | %:from %:fromname %:fromaddress
168 | %:to %:toname %:toaddress
169 | %:fromto (either \"to NAME\" or \"from NAME\")
170 gnus | %:group, for messages also all email fields and
171 | %:org-date (the Date: header in Org format)
172 w3, w3m | %:type %:url
173 info | %:type %:file %:node
174 calendar | %:type %:date"
175 :group 'org-remember
176 :get (lambda (var) ; Make sure all entries have at least 5 elements
177 (mapcar (lambda (x)
178 (if (not (stringp (car x))) (setq x (cons "" x)))
179 (cond ((= (length x) 4) (append x '(nil)))
180 ((= (length x) 3) (append x '(nil nil)))
181 (t x)))
182 (default-value var)))
183 :type '(repeat
184 :tag "enabled"
185 (list :value ("" ?a "\n" nil nil nil)
186 (string :tag "Name")
187 (character :tag "Selection Key")
188 (string :tag "Template")
189 (choice :tag "Destination file"
190 (file :tag "Specify")
191 (function :tag "Function")
192 (const :tag "Use `org-default-notes-file'" nil))
193 (choice :tag "Destin. headline"
194 (string :tag "Specify")
195 (function :tag "Function")
196 (const :tag "Use `org-remember-default-headline'" nil)
197 (const :tag "At beginning of file" top)
198 (const :tag "At end of file" bottom)
199 (const :tag "In a date tree" date-tree))
200 (choice :tag "Context"
201 (const :tag "Use in all contexts" nil)
202 (const :tag "Use in all contexts" t)
203 (repeat :tag "Use only if in major mode"
204 (symbol :tag "Major mode"))
205 (function :tag "Perform a check against function")))))
207 (defcustom org-remember-delete-empty-lines-at-end t
208 "Non-nil means clean up final empty lines in remember buffer."
209 :group 'org-remember
210 :type 'boolean)
212 (defcustom org-remember-before-finalize-hook nil
213 "Hook that is run right before a remember process is finalized.
214 The remember buffer is still current when this hook runs."
215 :group 'org-remember
216 :type 'hook)
218 (defvar org-remember-mode-map (make-sparse-keymap)
219 "Keymap for `org-remember-mode', a minor mode.
220 Use this map to set additional keybindings for when Org-mode is used
221 for a Remember buffer.")
222 (defvar org-remember-mode-hook nil
223 "Hook for the minor `org-remember-mode'.")
225 (define-minor-mode org-remember-mode
226 "Minor mode for special key bindings in a remember buffer."
227 nil " Rem" org-remember-mode-map
228 (run-hooks 'org-remember-mode-hook))
229 (define-key org-remember-mode-map "\C-c\C-c" 'org-remember-finalize)
230 (define-key org-remember-mode-map "\C-c\C-k" 'org-remember-kill)
232 (defcustom org-remember-clock-out-on-exit 'query
233 "Non-nil means stop the clock when exiting a clocking remember buffer.
234 This only applies if the clock is running in the remember buffer. If the
235 clock is not stopped, it continues to run in the storage location.
236 Instead of nil or t, this may also be the symbol `query' to prompt the
237 user each time a remember buffer with a running clock is filed away."
238 :group 'org-remember
239 :type '(choice
240 (const :tag "Never" nil)
241 (const :tag "Always" t)
242 (const :tag "Query user" query)))
244 (defcustom org-remember-backup-directory nil
245 "Directory where to store all remember buffers, for backup purposes.
246 After a remember buffer has been stored successfully, the backup file
247 will be removed. However, if you forget to finish the remember process,
248 the file will remain there.
249 See also `org-remember-auto-remove-backup-files'."
250 :group 'org-remember
251 :type '(choice
252 (const :tag "No backups" nil)
253 (directory :tag "Directory")))
255 (defcustom org-remember-auto-remove-backup-files t
256 "Non-nil means remove remember backup files after successfully storage.
257 When remember is finished successfully, with storing the note at the
258 desired target, remove the backup files related to this remember process
259 and show a message about remaining backup files, from previous, unfinished
260 remember sessions.
261 Backup files will only be made at all, when `org-remember-backup-directory'
262 is set."
263 :group 'org-remember
264 :type 'boolean)
266 (defcustom org-remember-warn-about-backups t
267 "Non-nil means warn about backup files in `org-remember-backup-directory'.
269 Set this to nil if you find that you don't need the warning.
271 If you cancel remember calls frequently and know when they
272 contain useful information (because you know that you made an
273 error or Emacs crashed, for example) nil is more useful. In the
274 opposite case, the default, t, is more useful."
275 :group 'org-remember
276 :type 'boolean)
278 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
279 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
281 ;;;###autoload
282 (defun org-remember-insinuate ()
283 "Setup remember.el for use with Org-mode."
284 (org-require-remember)
285 (setq remember-annotation-functions '(org-remember-annotation))
286 (setq remember-handler-functions '(org-remember-handler))
287 (add-hook 'remember-mode-hook 'org-remember-apply-template))
289 ;;;###autoload
290 (defun org-remember-annotation ()
291 "Return a link to the current location as an annotation for remember.el.
292 If you are using Org-mode files as target for data storage with
293 remember.el, then the annotations should include a link compatible with the
294 conventions in Org-mode. This function returns such a link."
295 (org-store-link nil))
297 (defconst org-remember-help
298 "Select a destination location for the note.
299 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
300 RET on headline -> Store as sublevel entry to current headline
301 RET at beg-of-buf -> Append to file as level 2 headline
302 <left>/<right> -> before/after current headline, same headings level")
304 (defvar org-jump-to-target-location nil)
305 (defvar org-remember-previous-location nil)
306 (defvar org-remember-reference-date nil)
307 (defvar org-force-remember-template-char) ;; dynamically scoped
309 ;; Save the major mode of the buffer we called remember from
310 (defvar org-select-template-temp-major-mode nil)
312 ;; Temporary store the buffer where remember was called from
313 (defvar org-select-template-original-buffer nil)
315 (defun org-select-remember-template (&optional use-char)
316 (when org-remember-templates
317 (let* ((pre-selected-templates
318 (mapcar
319 (lambda (tpl)
320 (let ((ctxt (nth 5 tpl))
321 (mode org-select-template-temp-major-mode)
322 (buf org-select-template-original-buffer))
323 (and (or (not ctxt) (eq ctxt t)
324 (and (listp ctxt) (memq mode ctxt))
325 (and (functionp ctxt)
326 (with-current-buffer buf
327 ;; Protect the user-defined function from error
328 (condition-case nil (funcall ctxt) (error nil)))))
329 tpl)))
330 org-remember-templates))
331 ;; If no template at this point, add the default templates:
332 (pre-selected-templates1
333 (if (not (delq nil pre-selected-templates))
334 (mapcar (lambda(x) (if (not (nth 5 x)) x))
335 org-remember-templates)
336 pre-selected-templates))
337 ;; Then unconditionally add template for any contexts
338 (pre-selected-templates2
339 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
340 org-remember-templates)
341 (delq nil pre-selected-templates1)))
342 (templates (mapcar (lambda (x)
343 (if (stringp (car x))
344 (append (list (nth 1 x) (car x)) (cddr x))
345 (append (list (car x) "") (cdr x))))
346 (delq nil pre-selected-templates2)))
348 (char (or use-char
349 (cond
350 ((= (length templates) 1)
351 (caar templates))
352 ((and (boundp 'org-force-remember-template-char)
353 org-force-remember-template-char)
354 (if (stringp org-force-remember-template-char)
355 (string-to-char org-force-remember-template-char)
356 org-force-remember-template-char))
358 (setq msg (format
359 "Select template: %s%s"
360 (mapconcat
361 (lambda (x)
362 (cond
363 ((not (string-match "\\S-" (nth 1 x)))
364 (format "[%c]" (car x)))
365 ((equal (downcase (car x))
366 (downcase (aref (nth 1 x) 0)))
367 (format "[%c]%s" (car x)
368 (substring (nth 1 x) 1)))
369 (t (format "[%c]%s" (car x) (nth 1 x)))))
370 templates " ")
371 (if (assoc ?C templates)
373 " [C]customize templates")))
374 (let ((inhibit-quit t) char0)
375 (while (not char0)
376 (message msg)
377 (setq char0 (read-char-exclusive))
378 (when (and (not (assoc char0 templates))
379 (not (equal char0 ?\C-g))
380 (not (equal char0 ?C)))
381 (message "No such template \"%c\"" char0)
382 (ding) (sit-for 1)
383 (setq char0 nil)))
384 (when (equal char0 ?\C-g)
385 (jump-to-register remember-register)
386 (kill-buffer remember-buffer)
387 (error "Abort"))
388 (when (not (assoc char0 templates))
389 (jump-to-register remember-register)
390 (kill-buffer remember-buffer)
391 (customize-variable 'org-remember-templates)
392 (error "Customize templates"))
393 char0))))))
394 (cddr (assoc char templates)))))
396 ;;;###autoload
397 (defun org-remember-apply-template (&optional use-char skip-interactive)
398 "Initialize *remember* buffer with template, invoke `org-mode'.
399 This function should be placed into `remember-mode-hook' and in fact requires
400 to be run from that hook to function properly."
401 (when (and (boundp 'initial) (stringp initial))
402 (setq initial (org-no-properties initial))
403 (remove-text-properties 0 (length initial) '(read-only t) initial))
404 (if org-remember-templates
405 (let* ((entry (org-select-remember-template use-char))
406 (ct (or org-overriding-default-time (org-current-time)))
407 (dct (decode-time ct))
408 (ct1
409 (if (< (nth 2 dct) org-extend-today-until)
410 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
411 ct))
412 (tpl (car entry))
413 (plist-p (if org-store-link-plist t nil))
414 (file (if (and (nth 1 entry)
415 (or (and (stringp (nth 1 entry))
416 (string-match "\\S-" (nth 1 entry)))
417 (functionp (nth 1 entry))))
418 (nth 1 entry)
419 org-default-notes-file))
420 (headline (nth 2 entry))
421 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
422 (v-x (or (org-get-x-clipboard 'PRIMARY)
423 (org-get-x-clipboard 'CLIPBOARD)
424 (org-get-x-clipboard 'SECONDARY)))
425 (v-t (format-time-string (car org-time-stamp-formats) ct))
426 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
427 (v-u (concat "[" (substring v-t 1 -1) "]"))
428 (v-U (concat "[" (substring v-T 1 -1) "]"))
429 ;; `initial' and `annotation' are bound in `remember'.
430 ;; But if the property list has them, we prefer those values
431 (v-i (or (plist-get org-store-link-plist :initial)
432 (and (boundp 'initial) initial)
433 ""))
434 (v-a (or (plist-get org-store-link-plist :annotation)
435 (and (boundp 'annotation) annotation)
436 ""))
437 ;; Is the link empty? Then we do not want it...
438 (v-a (if (equal v-a "[[]]") "" v-a))
439 (clipboards (remove nil (list v-i
440 (org-get-x-clipboard 'PRIMARY)
441 (org-get-x-clipboard 'CLIPBOARD)
442 (org-get-x-clipboard 'SECONDARY)
443 v-c)))
444 (v-A (if (and v-a
445 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
446 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
447 v-a))
448 (v-n user-full-name)
449 (v-k (if (marker-buffer org-clock-marker)
450 (org-substring-no-properties org-clock-heading)))
451 (v-K (if (marker-buffer org-clock-marker)
452 (org-make-link-string
453 (buffer-file-name (marker-buffer org-clock-marker))
454 org-clock-heading)))
456 (org-startup-folded nil)
457 (org-inhibit-startup t)
458 org-time-was-given org-end-time-was-given x
459 prompt completions char time pos default histvar)
461 (when (functionp file)
462 (setq file (funcall file)))
463 (when (functionp headline)
464 (setq headline (funcall headline)))
465 (when (and file (not (file-name-absolute-p file)))
466 (setq file (expand-file-name file org-directory)))
468 (setq org-store-link-plist
469 (plist-put org-store-link-plist :annotation v-a)
470 org-store-link-plist
471 (plist-put org-store-link-plist :initial v-i))
473 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
474 (erase-buffer)
475 (insert (substitute-command-keys
476 (format
477 "## %s \"%s\" -> \"* %s\"
478 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
479 ## C-0 C-c C-c \"%s\" -> \"* %s\"
480 ## %s to select file and header location interactively.
481 ## C-2 C-c C-c as child (C-3: as sibling) of the currently clocked item
482 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
483 (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
484 (abbreviate-file-name (or file org-default-notes-file))
485 (or headline "")
486 (or (car org-remember-previous-location) "???")
487 (or (cdr org-remember-previous-location) "???")
488 (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
489 (insert tpl)
491 ;; %[] Insert contents of a file.
492 (goto-char (point-min))
493 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
494 (unless (org-remember-escaped-%)
495 (let ((start (match-beginning 0))
496 (end (match-end 0))
497 (filename (expand-file-name (match-string 1))))
498 (goto-char start)
499 (delete-region start end)
500 (condition-case error
501 (insert-file-contents filename)
502 (error (insert (format "%%![Couldn't insert %s: %s]"
503 filename error)))))))
504 ;; Simple %-escapes
505 (goto-char (point-min))
506 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
507 (unless (org-remember-escaped-%)
508 (when (and initial (equal (match-string 0) "%i"))
509 (save-match-data
510 (let* ((lead (buffer-substring
511 (point-at-bol) (match-beginning 0))))
512 (setq v-i (mapconcat 'identity
513 (org-split-string initial "\n")
514 (concat "\n" lead))))))
515 (replace-match
516 (or (eval (intern (concat "v-" (match-string 1)))) "")
517 t t)))
519 ;; %() embedded elisp
520 (goto-char (point-min))
521 (while (re-search-forward "%\\((.+)\\)" nil t)
522 (unless (org-remember-escaped-%)
523 (goto-char (match-beginning 0))
524 (let ((template-start (point)))
525 (forward-char 1)
526 (let ((result
527 (condition-case error
528 (eval (read (current-buffer)))
529 (error (format "%%![Error: %s]" error)))))
530 (delete-region template-start (point))
531 (insert result)))))
533 ;; From the property list
534 (when plist-p
535 (goto-char (point-min))
536 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
537 (unless (org-remember-escaped-%)
538 (and (setq x (or (plist-get org-store-link-plist
539 (intern (match-string 1))) ""))
540 (replace-match x t t)))))
542 ;; Turn on org-mode in the remember buffer, set local variables
543 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1))
544 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
545 (org-set-local 'org-default-notes-file file))
546 (if headline
547 (org-set-local 'org-remember-default-headline headline))
548 (org-set-local 'org-remember-reference-date
549 (list (nth 4 dct) (nth 3 dct) (nth 5 dct)))
550 ;; Interactive template entries
551 (goto-char (point-min))
552 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
553 (unless (org-remember-escaped-%)
554 (setq char (if (match-end 3) (match-string 3))
555 prompt (if (match-end 2) (match-string 2)))
556 (goto-char (match-beginning 0))
557 (replace-match "")
558 (setq completions nil default nil)
559 (when prompt
560 (setq completions (org-split-string prompt "|")
561 prompt (pop completions)
562 default (car completions)
563 histvar (intern (concat
564 "org-remember-template-prompt-history::"
565 (or prompt "")))
566 completions (mapcar 'list completions)))
567 (cond
568 ((member char '("G" "g"))
569 (let* ((org-last-tags-completion-table
570 (org-global-tags-completion-table
571 (if (equal char "G") (org-agenda-files) (and file (list file)))))
572 (org-add-colon-after-tag-completion t)
573 (ins (org-icompleting-read
574 (if prompt (concat prompt ": ") "Tags: ")
575 'org-tags-completion-function nil nil nil
576 'org-tags-history)))
577 (setq ins (mapconcat 'identity
578 (org-split-string ins (org-re "[^[:alnum:]_@#%]+"))
579 ":"))
580 (when (string-match "\\S-" ins)
581 (or (equal (char-before) ?:) (insert ":"))
582 (insert ins)
583 (or (equal (char-after) ?:) (insert ":")))))
584 ((equal char "C")
585 (cond ((= (length clipboards) 1) (insert (car clipboards)))
586 ((> (length clipboards) 1)
587 (insert (read-string "Clipboard/kill value: "
588 (car clipboards) '(clipboards . 1)
589 (car clipboards))))))
590 ((equal char "L")
591 (cond ((= (length clipboards) 1)
592 (org-insert-link 0 (car clipboards)))
593 ((> (length clipboards) 1)
594 (org-insert-link 0 (read-string "Clipboard/kill value: "
595 (car clipboards)
596 '(clipboards . 1)
597 (car clipboards))))))
598 ((equal char "p")
599 (let*
600 ((prop (org-substring-no-properties prompt))
601 (pall (concat prop "_ALL"))
602 (allowed
603 (with-current-buffer
604 (or (find-buffer-visiting file)
605 (find-file-noselect file))
606 (or (cdr (assoc pall org-file-properties))
607 (cdr (assoc pall org-global-properties))
608 (cdr (assoc pall org-global-properties-fixed)))))
609 (existing (with-current-buffer
610 (or (find-buffer-visiting file)
611 (find-file-noselect file))
612 (mapcar 'list (org-property-values prop))))
613 (propprompt (concat "Value for " prop ": "))
614 (val (if allowed
615 (org-completing-read
616 propprompt
617 (mapcar 'list (org-split-string allowed "[ \t]+"))
618 nil 'req-match)
619 (org-completing-read-no-i propprompt existing nil nil
620 "" nil ""))))
621 (org-set-property prop val)))
622 (char
623 ;; These are the date/time related ones
624 (setq org-time-was-given (equal (upcase char) char))
625 (setq time (org-read-date (equal (upcase char) "U") t nil
626 prompt))
627 (org-insert-time-stamp time org-time-was-given
628 (member char '("u" "U"))
629 nil nil (list org-end-time-was-given)))
631 (let (org-completion-use-ido)
632 (insert (org-without-partial-completion
633 (org-completing-read-no-i
634 (concat (if prompt prompt "Enter string")
635 (if default (concat " [" default "]"))
636 ": ")
637 completions nil nil nil histvar default))))))))
639 (goto-char (point-min))
640 (if (re-search-forward "%\\?" nil t)
641 (replace-match "")
642 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
643 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1)))
644 (when (save-excursion
645 (goto-char (point-min))
646 (re-search-forward "%&" nil t))
647 (replace-match "")
648 (org-set-local 'org-jump-to-target-location t))
649 (when org-remember-backup-directory
650 (unless (file-directory-p org-remember-backup-directory)
651 (make-directory org-remember-backup-directory))
652 (org-set-local 'auto-save-file-name-transforms nil)
653 (setq buffer-file-name
654 (expand-file-name
655 (format-time-string "remember-%Y-%m-%d-%H-%M-%S")
656 org-remember-backup-directory))
657 (save-buffer)
658 (org-set-local 'auto-save-visited-file-name t)
659 (auto-save-mode 1))
660 (when (save-excursion
661 (goto-char (point-min))
662 (re-search-forward "%!" nil t))
663 (replace-match "")
664 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
666 (defun org-remember-escaped-% ()
667 (if (equal (char-before (match-beginning 0)) ?\\)
668 (progn
669 (delete-region (1- (match-beginning 0)) (match-beginning 0))
671 nil))
674 (defun org-remember-finish-immediately ()
675 "File remember note immediately.
676 This should be run in `post-command-hook' and will remove itself
677 from that hook."
678 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
679 (org-remember-finalize))
681 (defun org-remember-visit-immediately ()
682 "File remember note immediately.
683 This should be run in `post-command-hook' and will remove itself
684 from that hook."
685 (org-remember '(16))
686 (goto-char (or (text-property-any
687 (point) (save-excursion (org-end-of-subtree t t))
688 'org-position-cursor t)
689 (point)))
690 (message "%s"
691 (format
692 (substitute-command-keys
693 "Restore window configuration with \\[jump-to-register] %c")
694 remember-register)))
696 (defvar org-clock-marker) ; Defined in org.el
697 (defun org-remember-finalize ()
698 "Finalize the remember process."
699 (interactive)
700 (unless org-remember-mode
701 (error "This does not seem to be a remember buffer for Org-mode"))
702 (run-hooks 'org-remember-before-finalize-hook)
703 (unless (fboundp 'remember-finalize)
704 (defalias 'remember-finalize 'remember-buffer))
705 (when (and org-clock-marker
706 (equal (marker-buffer org-clock-marker) (current-buffer)))
707 ;; the clock is running in this buffer.
708 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
709 (or (eq org-remember-clock-out-on-exit t)
710 (and org-remember-clock-out-on-exit
711 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
712 (let (org-log-note-clock-out) (org-clock-out))))
713 (when buffer-file-name
714 (do-auto-save))
715 (remember-finalize))
717 (defun org-remember-kill ()
718 "Abort the current remember process."
719 (interactive)
720 (let ((org-note-abort t))
721 (org-remember-finalize)))
723 ;;;###autoload
724 (defun org-remember (&optional goto org-force-remember-template-char)
725 "Call `remember'. If this is already a remember buffer, re-apply template.
726 If there is an active region, make sure remember uses it as initial content
727 of the remember buffer.
729 When called interactively with a \\[universal-argument] \
730 prefix argument GOTO, don't remember
731 anything, just go to the file/headline where the selected template usually
732 stores its notes. With a double prefix argument \
733 \\[universal-argument] \\[universal-argument], go to the last
734 note stored by remember.
736 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
737 associated with a template in `org-remember-templates'."
738 (interactive "P")
739 (org-require-remember)
740 (cond
741 ((equal goto '(4)) (org-go-to-remember-target))
742 ((equal goto '(16)) (org-remember-goto-last-stored))
744 ;; set temporary variables that will be needed in
745 ;; `org-select-remember-template'
746 (setq org-select-template-temp-major-mode major-mode)
747 (setq org-select-template-original-buffer (current-buffer))
748 (if org-remember-mode
749 (progn
750 (when (< (length org-remember-templates) 2)
751 (error "No other template available"))
752 (erase-buffer)
753 (let ((annotation (plist-get org-store-link-plist :annotation))
754 (initial (plist-get org-store-link-plist :initial)))
755 (org-remember-apply-template))
756 (message "Press C-c C-c to remember data"))
757 (if (org-region-active-p)
758 (org-do-remember (buffer-substring (point) (mark)))
759 (org-do-remember))))))
761 (defvar org-remember-last-stored-marker (make-marker)
762 "Marker pointing to the entry most recently stored with `org-remember'.")
764 (defun org-remember-goto-last-stored ()
765 "Go to the location where the last remember note was stored."
766 (interactive)
767 (org-goto-marker-or-bmk org-remember-last-stored-marker
768 "org-remember-last-stored")
769 (message "This is the last note stored by remember"))
771 (defun org-go-to-remember-target (&optional template-key)
772 "Go to the target location of a remember template.
773 The user is queried for the template."
774 (interactive)
775 (let* (org-select-template-temp-major-mode
776 (entry (org-select-remember-template template-key))
777 (file (nth 1 entry))
778 (heading (nth 2 entry))
779 visiting)
780 (unless (and file (stringp file) (string-match "\\S-" file))
781 (setq file org-default-notes-file))
782 (when (and file (not (file-name-absolute-p file)))
783 (setq file (expand-file-name file org-directory)))
784 (unless (and heading (stringp heading) (string-match "\\S-" heading))
785 (setq heading org-remember-default-headline))
786 (setq visiting (org-find-base-buffer-visiting file))
787 (if (not visiting) (find-file-noselect file))
788 (switch-to-buffer (or visiting (get-file-buffer file)))
789 (widen)
790 (goto-char (point-min))
791 (if (re-search-forward
792 (format org-complex-heading-regexp-format (regexp-quote heading))
793 nil t)
794 (goto-char (match-beginning 0))
795 (error "Target headline not found: %s" heading))))
797 ;; FIXME (bzg): let's clean up of final empty lines happen only once
798 ;; (see the org-remember-delete-empty-lines-at-end option below)
799 ;;;###autoload
800 (defun org-remember-handler ()
801 "Store stuff from remember.el into an org file.
802 When the template has specified a file and a headline, the entry is filed
803 there, or in the location defined by `org-default-notes-file' and
804 `org-remember-default-headline'.
805 \\<org-remember-mode-map>
806 If no defaults have been defined, or if the current prefix argument
807 is 1 (using C-1 \\[org-remember-finalize] to exit remember), an interactive
808 process is used to select the target location.
810 When the prefix is 0 (i.e. when remember is exited with \
811 C-0 \\[org-remember-finalize]),
812 the entry is filed to the same location as the previous note.
814 When the prefix is 2 (i.e. when remember is exited with \
815 C-2 \\[org-remember-finalize]),
816 the entry is filed as a subentry of the entry where the clock is
817 currently running.
819 When \\[universal-argument] has been used as prefix argument, the
820 note is stored and Emacs moves point to the new location of the
821 note, so that editing can be continued there (similar to
822 inserting \"%&\" into the template).
824 Before storing the note, the function ensures that the text has an
825 org-mode-style headline, i.e. a first line that starts with
826 a \"*\". If not, a headline is constructed from the current date and
827 some additional data.
829 If the variable `org-adapt-indentation' is non-nil, the entire text is
830 also indented so that it starts in the same column as the headline
831 \(i.e. after the stars).
833 See also the variable `org-reverse-note-order'."
834 (when (and (equal current-prefix-arg 2)
835 (not (marker-buffer org-clock-marker)))
836 (error "No running clock"))
837 (when (org-bound-and-true-p org-jump-to-target-location)
838 (let* ((end (min (point-max) (1+ (point))))
839 (beg (point)))
840 (if (= end beg) (setq beg (1- beg)))
841 (put-text-property beg end 'org-position-cursor t)))
842 (goto-char (point-min))
843 (while (looking-at "^[ \t]*\n\\|^##.*\n")
844 (replace-match ""))
845 (when org-remember-delete-empty-lines-at-end
846 (goto-char (point-max))
847 (beginning-of-line 1)
848 (while (and (looking-at "[ \t]*$\\|##.*") (> (point) 1))
849 (delete-region (1- (point)) (point-max))
850 (beginning-of-line 1)))
851 (catch 'quit
852 (if org-note-abort (throw 'quit t))
853 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
854 (backup-file
855 (and buffer-file-name
856 (equal (file-name-directory buffer-file-name)
857 (file-name-as-directory
858 (expand-file-name org-remember-backup-directory)))
859 (string-match "^remember-[0-9]\\{4\\}"
860 (file-name-nondirectory buffer-file-name))
861 buffer-file-name))
863 (dummy
864 (unless (string-match "\\S-" (buffer-string))
865 (message "Nothing to remember")
866 (and backup-file
867 (ignore-errors
868 (delete-file backup-file)
869 (delete-file (concat backup-file "~"))))
870 (set-buffer-modified-p nil)
871 (throw 'quit t)))
872 (reference-date org-remember-reference-date)
873 (previousp (and (member current-prefix-arg '((16) 0))
874 org-remember-previous-location))
875 (clockp (equal current-prefix-arg 2))
876 (clocksp (equal current-prefix-arg 3))
877 (fastp (org-xor (equal current-prefix-arg 1)
878 org-remember-store-without-prompt))
879 (file (cond
880 (fastp org-default-notes-file)
881 ((and (eq org-remember-interactive-interface 'refile)
882 org-refile-targets)
883 org-default-notes-file)
884 ((not previousp)
885 (org-get-org-file))))
886 (heading org-remember-default-headline)
887 (visiting (and file (org-find-base-buffer-visiting file)))
888 (org-startup-folded nil)
889 (org-startup-align-all-tables nil)
890 (org-goto-start-pos 1)
891 spos exitcmd level reversed txt text-before-node-creation)
892 (when (equal current-prefix-arg '(4))
893 (setq visitp t))
894 (when previousp
895 (setq file (car org-remember-previous-location)
896 visiting (and file (org-find-base-buffer-visiting file))
897 heading (cdr org-remember-previous-location)
898 fastp t))
899 (when (or clockp clocksp)
900 (setq file (buffer-file-name (marker-buffer org-clock-marker))
901 visiting (and file (org-find-base-buffer-visiting file))
902 heading org-clock-heading-for-remember
903 fastp t))
904 (setq current-prefix-arg nil)
905 ;; Modify text so that it becomes a nice subtree which can be inserted
906 ;; into an org tree.
907 (when org-remember-delete-empty-lines-at-end
908 (goto-char (point-min))
909 (if (re-search-forward "[ \t\n]+\\'" nil t)
910 ;; remove empty lines at end
911 (replace-match "")))
912 (goto-char (point-min))
913 (setq text-before-node-creation (buffer-string))
914 (unless (looking-at org-outline-regexp)
915 ;; add a headline
916 (insert (concat "* " (current-time-string)
917 " (" (remember-buffer-desc) ")\n"))
918 (backward-char 1)
919 (when org-adapt-indentation
920 (while (re-search-forward "^" nil t)
921 (insert " "))))
922 ;; Delete final empty lines
923 (when org-remember-delete-empty-lines-at-end
924 (goto-char (point-min))
925 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
926 (replace-match "\n\n")
927 (if (re-search-forward "[ \t\n]*\\'")
928 (replace-match "\n"))))
929 (goto-char (point-min))
930 (setq txt (buffer-string))
931 (org-save-markers-in-region (point-min) (point-max))
932 (set-buffer-modified-p nil)
933 (when (and (eq org-remember-interactive-interface 'refile)
934 (not fastp))
935 (org-refile nil (or visiting (find-file-noselect file)))
936 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
937 (save-excursion
938 (bookmark-jump "org-refile-last-stored")
939 (bookmark-set "org-remember-last-stored")
940 (move-marker org-remember-last-stored-marker (point)))
941 (throw 'quit t))
942 ;; Find the file
943 (with-current-buffer (or visiting (find-file-noselect file))
944 (unless (or (org-mode-p) (member heading '(top bottom)))
945 (error "Target files for notes must be in Org-mode if not filing to top/bottom"))
946 (save-excursion
947 (save-restriction
948 (widen)
949 (setq reversed (org-notes-order-reversed-p))
951 ;; Find the default location
952 (when heading
953 (cond
954 ((not (org-mode-p))
955 (if (eq heading 'top)
956 (goto-char (point-min))
957 (goto-char (point-max))
958 (or (bolp) (newline)))
959 (insert text-before-node-creation)
960 (when remember-save-after-remembering
961 (save-buffer)
962 (if (not visiting) (kill-buffer (current-buffer))))
963 (throw 'quit t))
964 ((eq heading 'top)
965 (goto-char (point-min))
966 (or (looking-at org-outline-regexp)
967 (re-search-forward org-outline-regexp nil t))
968 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
969 ((eq heading 'bottom)
970 (goto-char (point-max))
971 (or (bolp) (newline))
972 (setq org-goto-start-pos (point)))
973 ((eq heading 'date-tree)
974 (org-datetree-find-date-create reference-date)
975 (setq reversed nil)
976 (setq org-goto-start-pos (point)))
977 ((and (stringp heading) (string-match "\\S-" heading))
978 (goto-char (point-min))
979 (if (re-search-forward
980 (format org-complex-heading-regexp-format
981 (regexp-quote heading))
982 nil t)
983 (setq org-goto-start-pos (match-beginning 0))
984 (when fastp
985 (goto-char (point-max))
986 (unless (bolp) (newline))
987 (insert "* " heading "\n")
988 (setq org-goto-start-pos (point-at-bol 0)))))
989 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
990 heading 'top))))
992 ;; Ask the User for a location, using the appropriate interface
993 (cond
994 ((and fastp (memq heading '(top bottom)))
995 (setq spos org-goto-start-pos
996 exitcmd (if (eq heading 'top) 'left nil)))
997 (fastp (setq spos org-goto-start-pos
998 exitcmd 'return))
999 ((eq org-remember-interactive-interface 'outline)
1000 (setq spos (org-get-location (current-buffer)
1001 org-remember-help)
1002 exitcmd (cdr spos)
1003 spos (car spos)))
1004 ((eq org-remember-interactive-interface 'outline-path-completion)
1005 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
1006 (org-refile-use-outline-path t))
1007 (setq spos (org-refile-get-location "Heading: ")
1008 exitcmd 'return
1009 spos (nth 3 spos))))
1010 (t (error "This should not happen")))
1011 (if (not spos) (throw 'quit nil)) ; return nil to show we did
1012 ; not handle this note
1013 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
1014 (goto-char spos)
1015 (cond ((org-on-heading-p t)
1016 (org-back-to-heading t)
1017 (setq level (funcall outline-level))
1018 (cond
1019 ((eq exitcmd 'return)
1020 ;; sublevel of current
1021 (setq org-remember-previous-location
1022 (cons (abbreviate-file-name file)
1023 (org-get-heading 'notags)))
1024 (if reversed
1025 (outline-next-heading)
1026 (org-end-of-subtree t)
1027 (if (not (bolp))
1028 (if (looking-at "[ \t]*\n")
1029 (beginning-of-line 2)
1030 (end-of-line 1)
1031 (insert "\n"))))
1032 (org-paste-subtree (if clocksp
1033 level
1034 (org-get-valid-level level 1)) txt)
1035 (and org-auto-align-tags (org-set-tags nil t))
1036 (bookmark-set "org-remember-last-stored")
1037 (move-marker org-remember-last-stored-marker (point)))
1038 ((eq exitcmd 'left)
1039 ;; before current
1040 (org-paste-subtree level txt)
1041 (and org-auto-align-tags (org-set-tags nil t))
1042 (bookmark-set "org-remember-last-stored")
1043 (move-marker org-remember-last-stored-marker (point)))
1044 ((eq exitcmd 'right)
1045 ;; after current
1046 (org-end-of-subtree t)
1047 (org-paste-subtree level txt)
1048 (and org-auto-align-tags (org-set-tags nil t))
1049 (bookmark-set "org-remember-last-stored")
1050 (move-marker org-remember-last-stored-marker (point)))
1051 (t (error "This should not happen"))))
1053 ((eq heading 'bottom)
1054 (org-paste-subtree 1 txt)
1055 (and org-auto-align-tags (org-set-tags nil t))
1056 (bookmark-set "org-remember-last-stored")
1057 (move-marker org-remember-last-stored-marker (point)))
1059 ((and (bobp) (not reversed))
1060 ;; Put it at the end, one level below level 1
1061 (save-restriction
1062 (widen)
1063 (goto-char (point-max))
1064 (if (not (bolp)) (newline))
1065 (org-paste-subtree (org-get-valid-level 1 1) txt)
1066 (and org-auto-align-tags (org-set-tags nil t))
1067 (bookmark-set "org-remember-last-stored")
1068 (move-marker org-remember-last-stored-marker (point))))
1070 ((and (bobp) reversed)
1071 ;; Put it at the start, as level 1
1072 (save-restriction
1073 (widen)
1074 (goto-char (point-min))
1075 (re-search-forward "^\\*+ " nil t)
1076 (beginning-of-line 1)
1077 (org-paste-subtree 1 txt)
1078 (and org-auto-align-tags (org-set-tags nil t))
1079 (bookmark-set "org-remember-last-stored")
1080 (move-marker org-remember-last-stored-marker (point))))
1082 ;; Put it right there, with automatic level determined by
1083 ;; org-paste-subtree or from prefix arg
1084 (org-paste-subtree
1085 (if (numberp current-prefix-arg) current-prefix-arg)
1086 txt)
1087 (and org-auto-align-tags (org-set-tags nil t))
1088 (bookmark-set "org-remember-last-stored")
1089 (move-marker org-remember-last-stored-marker (point))))
1091 (when remember-save-after-remembering
1092 (save-buffer)
1093 (if (and (not visiting)
1094 (not (equal (marker-buffer org-clock-marker)
1095 (current-buffer))))
1096 (kill-buffer (current-buffer))))
1097 (when org-remember-auto-remove-backup-files
1098 (when backup-file
1099 (ignore-errors
1100 (delete-file backup-file)
1101 (delete-file (concat backup-file "~"))))
1102 (when org-remember-backup-directory
1103 (let ((n (length
1104 (directory-files
1105 org-remember-backup-directory nil
1106 "^remember-.*[0-9]$"))))
1107 (when (and org-remember-warn-about-backups
1108 (> n 0))
1109 (message
1110 "%d backup files (unfinished remember calls) in %s"
1111 n org-remember-backup-directory))))))))))
1113 t) ;; return t to indicate that we took care of this note.
1115 (defun org-do-remember (&optional initial)
1116 "Call remember."
1117 (remember initial))
1119 (defun org-require-remember ()
1120 "Make sure remember is loaded, or install our own emergency version of it."
1121 (condition-case nil
1122 (require 'remember)
1123 (error
1124 ;; Lets install our own micro version of remember
1125 (defvar remember-register ?R)
1126 (defvar remember-mode-hook nil)
1127 (defvar remember-handler-functions nil)
1128 (defvar remember-buffer "*Remember*")
1129 (defvar remember-save-after-remembering t)
1130 (defvar remember-annotation-functions '(buffer-file-name))
1131 (defun remember-finalize ()
1132 (run-hook-with-args-until-success 'remember-handler-functions)
1133 (when (equal remember-buffer (buffer-name))
1134 (kill-buffer (current-buffer))
1135 (jump-to-register remember-register)))
1136 (defun remember-mode ()
1137 (fundamental-mode)
1138 (setq mode-name "Remember")
1139 (run-hooks 'remember-mode-hook))
1140 (defun remember (&optional initial)
1141 (window-configuration-to-register remember-register)
1142 (let* ((annotation (run-hook-with-args-until-success
1143 'remember-annotation-functions)))
1144 (switch-to-buffer-other-window (get-buffer-create remember-buffer))
1145 (remember-mode)))
1146 (defun remember-buffer-desc ()
1147 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
1148 (point-at-eol)))))))
1150 (provide 'org-remember)
1152 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
1154 ;;; org-remember.el ends here