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