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