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