org-agenda-append-agenda: check that we are in an agenda buffer.
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-capture.el
blob1390f2143ef4cf1279385480a4bb3fd49515135f
1 ;;; org-capture.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2010 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.4
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 an alternative implementation of the same functionality
29 ;; that is also provided by org-remember.el. The implementation is more
30 ;; streamlined, can produce more target types (e.g. plain list items or
31 ;; table lines). Also, it does not use a temporary buffer for editing
32 ;; the captured entry - instead it uses an indirect buffer that visits
33 ;; the new entry already in the target buffer (this was an idea by Samuel
34 ;; Wales). John Wiegley's excellent `remember.el' is not needed for this
35 ;; implementation, even though we borrow heavily from its ideas.
37 ;; This implementation heavily draws on ideas by James TD Smith and
38 ;; Samuel Wales, and, of cause, uses John Wiegley's remember.el as inspiration.
40 ;;; TODO
42 ;; - find a clever way to not always insert an annotation maybe a
43 ;; predicate function that can check for conditions for %a to be
44 ;; used. This could be one of the properties.
46 ;; - Should there be plist members that arrange for properties to be
47 ;; asked for, like James proposed in his RFC?
49 ;;; Code:
51 (eval-when-compile
52 (require 'cl))
53 (require 'org)
54 (require 'org-mks)
56 (declare-function org-datetree-find-date-create "org-datetree"
57 (DATE &optional KEEP-RESTRICTION))
58 (declare-function org-table-get-specials "org-table" ())
59 (declare-function org-table-goto-line "org-table" (N))
60 (defvar org-remember-default-headline)
61 (defvar org-remember-templates)
62 (defvar org-table-hlines)
64 (defvar org-capture-clock-was-started nil
65 "Internal flag, noting if the clock was started.")
67 (defvar org-capture-last-stored-marker (make-marker)
68 "Marker pointing to the entry most recently stored with `org-capture'.")
70 ;; The following variable is scoped dynamically by org-protocol
71 ;; to indicate that the link properties have already been stored
72 (defvar org-capture-link-is-already-stored nil)
74 (defgroup org-capture nil
75 "Options concerning capturing new entries."
76 :tag "Org Capture"
77 :group 'org)
79 (defcustom org-capture-templates nil
80 "Templates for the creation of new entries.
82 Each entry is a list with the following items:
84 keys The keys that will select the template, as a string, characters
85 only, for example \"a\" for a template to be selected with a
86 single key, or \"bt\" for selection with two keys. When using
87 several keys, keys using the same prefix key must be together
88 in the list and preceded by a 2-element entry explaining the
89 prefix key, for example
91 (\"b\" \"Templates for marking stuff to buy\")
93 The \"C\" key is used by default for quick access to the
94 customization of the template variable. But if you want to use
95 that key for a template, you can.
97 description A short string describing the template, will be shown during
98 selection.
100 type The type of entry. Valid types are:
101 entry an Org-mode node, with a headline. Will be
102 filed as the child of the target entry or as
103 a top-level entry.
104 item a plain list item, will be placed in the
105 first plain list at the target
106 location.
107 checkitem a checkbox item. This differs from the
108 plain list item only is so far as it uses a
109 different default template.
110 table-line a new line in the first table at target location.
111 plain text to be inserted as it is.
113 target Specification of where the captured item should be placed.
114 In Org-mode files, targets usually define a node. Entries will
115 become children of this node, other types will be added to the
116 table or list in the body of this node.
118 Valid values are:
120 (file \"path/to/file\")
121 Text will be placed at the beginning or end of that file
123 (currentfile)
124 Text will be placed at the beginning or end of the file
125 org-capture is called from
127 (id \"id of existing org entry\")
128 File as child of this entry, or in the body of the entry
130 (file+headline \"path/to/file\" \"node headline\")
131 Fast configuration if the target heading is unique in the file
133 (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
134 For non-unique headings, the full path is safer
136 (file+regexp \"path/to/file\" \"regexp to find location\")
137 File to the entry matching regexp
139 (file+datetree \"path/to/file\")
140 Will create a heading in a date tree for today's date
142 (file+datetree+prompt \"path/to/file\")
143 Will create a heading in a date tree, prompts for date
145 (file+function \"path/to/file\" function-finding-location)
146 A function to find the right location in the file
148 (clock)
149 File to the entry that is currently being clocked
151 (function function-finding-location)
152 Most general way, write your own function to find both
153 file and location
155 template The template for creating the capture item. If you leave this
156 empty, an appropriate default template will be used. See below
157 for more details. Instead of a string, this may also be one of
159 (file \"/path/to/template-file\")
160 (function function-returning-the-template)
162 in order to get a template from a file, or dynamically
163 from a function.
165 The rest of the entry is a property list of additional options. Recognized
166 properties are:
168 :prepend Normally newly captured information will be appended at
169 the target location (last child, last table line,
170 last list item...). Setting this property will
171 change that.
173 :immediate-finish When set, do not offer to edit the information, just
174 file it away immediately. This makes sense if the
175 template only needs information that can be added
176 automatically.
178 :empty-lines Set this to the number of lines the should be inserted
179 before and after the new item. Default 0, only common
180 other value is 1.
182 :clock-in Start the clock in this item.
184 :clock-resume Start the interrupted clock when finishing the capture.
186 :unnarrowed Do not narrow the target buffer, simply show the
187 full buffer. Default is to narrow it so that you
188 only see the new stuff.
190 :table-line-pos Specification of the location in the table where the
191 new line should be inserted. It looks like \"II-3\"
192 which means that the new line should become the third
193 line before the second horizontal separator line.
195 :kill-buffer If the target file was not yet visited by a buffer when
196 capture was invoked, kill the buffer again after capture
197 is finalized.
199 The template defines the text to be inserted. Often this is an org-mode
200 entry (so the first line should start with a star) that will be filed as a
201 child of the target headline. It can also be freely formatted text.
202 Furthermore, the following %-escapes will be replaced with content:
204 %^{prompt} prompt the user for a string and replace this sequence with it.
205 A default value and a completion table ca be specified like this:
206 %^{prompt|default|completion2|completion3|...}
207 %t time stamp, date only
208 %T time stamp with date and time
209 %u, %U like the above, but inactive time stamps
210 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
211 You may define a prompt like %^{Please specify birthday
212 %n user name (taken from `user-full-name')
213 %a annotation, normally the link created with `org-store-link'
214 %i initial content, copied from the active region. If %i is
215 indented, the entire inserted text will be indented as well.
216 %c current kill ring head
217 %x content of the X clipboard
218 %^C interactive selection of which kill or clip to use
219 %^L like %^C, but insert as link
220 %k title of currently clocked task
221 %K link to currently clocked task
222 %f file visited by current buffer when org-capture was called
223 %F like @code{%f}, but include full path
224 %^g prompt for tags, with completion on tags in target file
225 %^G prompt for tags, with completion on all tags in all agenda files
226 %^{prop}p prompt the user for a value for property `prop'
227 %:keyword specific information for certain link types, see below
228 %[pathname] insert the contents of the file given by `pathname'
229 %(sexp) evaluate elisp `(sexp)' and replace with the result
231 %? After completing the template, position cursor here.
233 Apart from these general escapes, you can access information specific to the
234 link type that is created. For example, calling `org-capture' in emails
235 or gnus will record the author and the subject of the message, which you
236 can access with \"%:from\" and \"%:subject\", respectively. Here is a
237 complete list of what is recorded for each link type.
239 Link type | Available information
240 ------------------------+------------------------------------------------------
241 bbdb | %:type %:name %:company
242 vm, wl, mh, mew, rmail | %:type %:subject %:message-id
243 | %:from %:fromname %:fromaddress
244 | %:to %:toname %:toaddress
245 | %:fromto (either \"to NAME\" or \"from NAME\")
246 | %:date
247 | %:date-timestamp (as active timestamp)
248 | %:date-timestamp-inactive (as inactive timestamp)
249 gnus | %:group, for messages also all email fields
250 w3, w3m | %:type %:url
251 info | %:type %:file %:node
252 calendar | %:type %:date"
253 :group 'org-capture
254 :type
255 '(repeat
256 (choice :value ("" "" entry (file "~/org/notes.org") "")
257 (list :tag "Multikey description"
258 (string :tag "Keys ")
259 (string :tag "Description"))
260 (list :tag "Template entry"
261 (string :tag "Keys ")
262 (string :tag "Description ")
263 (choice :tag "Capture Type " :value entry
264 (const :tag "Org entry" entry)
265 (const :tag "Plain list item" item)
266 (const :tag "Checkbox item" checkitem)
267 (const :tag "Plain text" plain)
268 (const :tag "Table line" table-line))
269 (choice :tag "Target location"
270 (list :tag "File"
271 (const :format "" file)
272 (file :tag " File"))
273 (list :tag "Current file"
274 (const :format "" currentfile))
275 (list :tag "ID"
276 (const :format "" id)
277 (string :tag " ID"))
278 (list :tag "File & Headline"
279 (const :format "" file+headline)
280 (file :tag " File ")
281 (string :tag " Headline"))
282 (list :tag "File & Outline path"
283 (const :format "" file+olp)
284 (file :tag " File ")
285 (repeat :tag "Outline path" :inline t
286 (string :tag "Headline")))
287 (list :tag "File & Regexp"
288 (const :format "" file+regexp)
289 (file :tag " File ")
290 (regexp :tag " Regexp"))
291 (list :tag "File & Date tree"
292 (const :format "" file+datetree)
293 (file :tag " File"))
294 (list :tag "File & Date tree, prompt for date"
295 (const :format "" file+datetree+prompt)
296 (file :tag " File"))
297 (list :tag "File & function"
298 (const :format "" file+function)
299 (file :tag " File ")
300 (sexp :tag " Function"))
301 (list :tag "Current clocking task"
302 (const :format "" clock))
303 (list :tag "Function"
304 (const :format "" function)
305 (sexp :tag " Function")))
306 (choice :tag "Template"
307 (string)
308 (list :tag "File"
309 (const :format "" file)
310 (file :tag "Template file"))
311 (list :tag "Function"
312 (const :format "" function)
313 (function :tag "Template function")))
314 (plist :inline t
315 ;; Give the most common options as checkboxes
316 :options (((const :format "%v " :prepend) (const t))
317 ((const :format "%v " :immediate-finish) (const t))
318 ((const :format "%v " :empty-lines) (const 1))
319 ((const :format "%v " :clock-in) (const t))
320 ((const :format "%v " :clock-resume) (const t))
321 ((const :format "%v " :unnarrowed) (const t))
322 ((const :format "%v " :kill-buffer) (const t))))))))
324 (defcustom org-capture-before-finalize-hook nil
325 "Hook that is run right before a remember process is finalized.
326 The remember buffer is still current when this hook runs."
327 :group 'org-capture
328 :type 'hook)
330 (defcustom org-capture-after-finalize-hook nil
331 "Hook that is run right after a capture process is finalized.
332 Suitable for window cleanup"
333 :group 'org-capture
334 :type 'hook)
336 ;;; The property list for keeping information about the capture process
338 (defvar org-capture-plist nil
339 "Plist for the current capture process, global, to avoid having to pass it.")
340 (defvar org-capture-current-plist nil
341 "Local variable holding the plist in a capture buffer.
342 This is used to store the plist for use when finishing a capture process.
343 Another such process might have changed the global variable by then.")
345 (defun org-capture-put (&rest stuff)
346 (while stuff
347 (setq org-capture-plist (plist-put org-capture-plist
348 (pop stuff) (pop stuff)))))
349 (defun org-capture-get (prop &optional local)
350 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
352 (defun org-capture-member (prop)
353 (plist-get org-capture-plist prop))
355 ;;; The minor mode
357 (defvar org-capture-mode-map (make-sparse-keymap)
358 "Keymap for `org-capture-mode', a minor mode.
359 Use this map to set additional keybindings for when Org-mode is used
360 for a Remember buffer.")
362 (defvar org-capture-mode-hook nil
363 "Hook for the minor `org-capture-mode'.")
365 (define-minor-mode org-capture-mode
366 "Minor mode for special key bindings in a remember buffer."
367 nil " Rem" org-capture-mode-map
368 (org-set-local
369 'header-line-format
370 "Capture buffer. Finish `C-c C-c', refile `C-c C-w', abort `C-c C-k'.")
371 (run-hooks 'org-capture-mode-hook))
372 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
373 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
374 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
376 ;;; The main commands
378 ;;;###autoload
379 (defun org-capture (&optional goto keys)
380 "Capture something.
381 \\<org-capture-mode-map>
382 This will let you select a template from `org-capture-templates', and then
383 file the newly captured information. The text is immediately inserted
384 at the target location, and an indirect buffer is shown where you can
385 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
386 of Emacs, so that you can continue your work.
388 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
389 anything, just go to the file/headline where the selected template
390 stores its notes. With a double prefix argument \
391 \\[universal-argument] \\[universal-argument], go to the last note
392 stored.
394 When called with a `C-0' (zero) prefix, insert a template at point.
396 Lisp programs can set KEYS to a string associated with a template in
397 `org-capture-templates'. In this case, interactive selection will be
398 bypassed."
399 (interactive "P")
400 (cond
401 ((equal goto '(4)) (org-capture-goto-target))
402 ((equal goto '(16)) (org-capture-goto-last-stored))
404 ;; FIXME: Are these needed?
405 (let* ((orig-buf (current-buffer))
406 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
407 org-capture-link-is-already-stored)
408 (plist-get org-store-link-plist :annotation)
409 (org-store-link nil)))
410 (initial (and (org-region-active-p)
411 (buffer-substring (point) (mark))))
412 (entry (org-capture-select-template keys)))
413 (when (stringp initial)
414 (remove-text-properties 0 (length initial) '(read-only t) initial))
415 (when (stringp annotation)
416 (remove-text-properties 0 (length annotation)
417 '(read-only t) annotation))
418 (cond
419 ((equal entry "C")
420 (customize-variable 'org-capture-templates))
421 ((equal entry "q")
422 (error "Abort"))
424 (org-capture-set-plist entry)
425 (org-capture-get-template)
426 (org-capture-put :original-buffer orig-buf
427 :original-file (buffer-file-name orig-buf)
428 :original-file-nondirectory
429 (and (buffer-file-name orig-buf)
430 (file-name-nondirectory
431 (buffer-file-name orig-buf)))
432 :annotation annotation
433 :initial initial)
434 (org-capture-put :default-time
435 (or org-overriding-default-time
436 (org-current-time)))
437 (org-capture-set-target-location)
438 (condition-case error
439 (org-capture-put :template (org-capture-fill-template))
440 ((error quit)
441 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
442 (error "Capture abort: %s" error)))
444 (if (equal goto 0)
445 ;;insert at point
446 (org-capture-insert-template-here)
447 (condition-case error
448 (org-capture-place-template)
449 ((error quit)
450 (if (and (buffer-base-buffer (current-buffer))
451 (string-match "\\`CAPTURE-" (buffer-name)))
452 (kill-buffer (current-buffer)))
453 (set-window-configuration (org-capture-get :return-to-wconf))
454 (error "Capture template `%s': %s"
455 (org-capture-get :key)
456 (nth 1 error))))
457 (if (org-capture-get :immediate-finish)
458 (org-capture-finalize)
459 (if (and (org-mode-p)
460 (org-capture-get :clock-in))
461 (condition-case nil
462 (progn
463 (if (org-clock-is-active)
464 (org-capture-put :interrupted-clock
465 (copy-marker org-clock-marker)))
466 (org-clock-in)
467 (org-set-local 'org-capture-clock-was-started t))
468 (error
469 "Could not start the clock in this capture buffer")))))))))))
472 (defun org-capture-get-template ()
473 "Get the template from a file or a function if necessary."
474 (let ((txt (org-capture-get :template)) file)
475 (cond
476 ((and (listp txt) (eq (car txt) 'file))
477 (if (file-exists-p
478 (setq file (expand-file-name (nth 1 txt) org-directory)))
479 (setq txt (org-file-contents file))
480 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
481 ((and (listp txt) (eq (car txt) 'function))
482 (if (fboundp (nth 1 txt))
483 (setq txt (funcall (nth 1 txt)))
484 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
485 ((not txt) (setq txt ""))
486 ((stringp txt))
487 (t (setq txt "* Invalid capture template")))
488 (org-capture-put :template txt)))
490 (defun org-capture-finalize (&optional stay-with-capture)
491 "Finalize the capture process.
492 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
493 captured item after finalizing."
494 (interactive "P")
495 (unless (and org-capture-mode
496 (buffer-base-buffer (current-buffer)))
497 (error "This does not seem to be a capture buffer for Org-mode"))
499 ;; Did we start the clock in this capture buffer?
500 (when (and org-capture-clock-was-started
501 org-clock-marker (marker-buffer org-clock-marker)
502 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
503 (> org-clock-marker (point-min))
504 (< org-clock-marker (point-max)))
505 ;; Looks like the clock we started is still running. Clock out.
506 (let (org-log-note-clock-out) (org-clock-out))
507 (when (and (org-capture-get :clock-resume 'local)
508 (markerp (org-capture-get :interrupted-clock 'local))
509 (buffer-live-p (marker-buffer
510 (org-capture-get :interrupted-clock 'local))))
511 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
512 (org-with-point-at clock-in-task
513 (org-clock-in)))
514 (message "Interrupted clock has been resumed")))
516 (let ((beg (point-min))
517 (end (point-max))
518 (abort-note nil))
519 (widen)
521 (if org-note-abort
522 (let ((m1 (org-capture-get :begin-marker 'local))
523 (m2 (org-capture-get :end-marker 'local)))
524 (if (and m1 m2 (= m1 beg) (= m2 end))
525 (progn
526 (setq abort-note 'clean)
527 (kill-region m1 m2))
528 (setq abort-note 'dirty)))
530 ;; Make sure that the empty lines after are correct
531 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
532 (member (org-capture-get :type 'local)
533 '(entry item checkitem plain)))
534 (save-excursion
535 (goto-char end)
536 (or (bolp) (newline))
537 (org-capture-empty-lines-after
538 (or (org-capture-get :empty-lines 'local) 0))))
539 ;; Postprocessing: Update Statistics cookies, do the sorting
540 (when (org-mode-p)
541 (save-excursion
542 (when (ignore-errors (org-back-to-heading))
543 (org-update-parent-todo-statistics)
544 (org-update-checkbox-count)))
545 ;; FIXME Here we should do the sorting
546 ;; If we have added a table line, maybe recompute?
547 (when (and (eq (org-capture-get :type 'local) 'table-line)
548 (org-at-table-p))
549 (if (org-table-get-stored-formulas)
550 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
551 (org-table-align)))
553 ;; Store this place as the last one where we stored something
554 ;; Do the marking in the base buffer, so that it makes sense after
555 ;; the indirect buffer has been killed.
556 (org-capture-bookmark-last-stored-position)
558 ;; Run the hook
559 (run-hooks 'org-capture-before-finalize-hook)
562 ;; Kill the indirect buffer
563 (save-buffer)
564 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
565 (new-buffer (org-capture-get :new-buffer 'local))
566 (kill-buffer (org-capture-get :kill-buffer 'local))
567 (base-buffer (buffer-base-buffer (current-buffer))))
569 ;; Kill the indiret buffer
570 (kill-buffer (current-buffer))
572 ;; Kill the target buffer if that is desired
573 (when (and base-buffer new-buffer kill-buffer)
574 (with-current-buffer base-buffer (save-buffer))
575 (kill-buffer base-buffer))
577 ;; Restore the window configuration before capture
578 (set-window-configuration return-wconf))
580 (run-hooks 'org-capture-after-finalize-hook)
581 ;; Special cases
582 (cond
583 (abort-note
584 (cond
585 ((equal abort-note 'clean)
586 (message "Capture process aborted and target buffer cleaned up"))
587 ((equal abort-note 'dirty)
588 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
589 (stay-with-capture
590 (org-capture-goto-last-stored)))
591 ;; Return if we did store something
592 (not abort-note)))
594 (defun org-capture-refile ()
595 "Finalize the current capture and then refile the entry.
596 Refiling is done from the base buffer, because the indirect buffer is then
597 already gone. Any prefix argument will be passed to the refile command."
598 (interactive)
599 (unless (eq (org-capture-get :type 'local) 'entry)
600 (error
601 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
602 (let ((pos (point))
603 (base (buffer-base-buffer (current-buffer)))
604 (org-refile-for-capture t))
605 (org-capture-finalize)
606 (save-window-excursion
607 (with-current-buffer (or base (current-buffer))
608 (save-excursion
609 (save-restriction
610 (widen)
611 (goto-char pos)
612 (call-interactively 'org-refile)))))))
614 (defun org-capture-kill ()
615 "Abort the current capture process."
616 (interactive)
617 ;; FIXME: This does not do the right thing, we need to remove the new stuff
618 ;; By hand it is easy: undo, then kill the buffer
619 (let ((org-note-abort t) (org-capture-before-finalize-hook nil))
620 (org-capture-finalize)))
622 (defun org-capture-goto-last-stored ()
623 "Go to the location where the last remember note was stored."
624 (interactive)
625 (org-goto-marker-or-bmk org-capture-last-stored-marker
626 "org-capture-last-stored")
627 (message "This is the last note stored by a capture process"))
629 ;;; Supporting functions for handling the process
631 (defun org-capture-set-target-location (&optional target)
632 "Find target buffer and position and store then in the property list."
633 (let ((target-entry-p t))
634 (setq target (or target (org-capture-get :target)))
635 (save-excursion
636 (cond
637 ((eq (car target) 'file)
638 (set-buffer (org-capture-target-buffer (nth 1 target)))
639 (setq target-entry-p nil))
641 ((eq (car target) 'currentfile)
642 (if (not (and (buffer-file-name) (org-mode-p)))
643 (error "Cannot call this capture template outside of an Org buffer")
644 (set-buffer (org-capture-target-buffer (buffer-file-name)))
645 (setq target-entry-p nil)))
647 ((eq (car target) 'id)
648 (let ((loc (org-id-find (nth 1 target))))
649 (if (not loc)
650 (error "Cannot find target ID \"%s\"" (nth 1 target))
651 (set-buffer (org-capture-target-buffer (car loc)))
652 (goto-char (cdr loc)))))
654 ((eq (car target) 'file+headline)
655 (set-buffer (org-capture-target-buffer (nth 1 target)))
656 (let ((hd (nth 2 target)))
657 (goto-char (point-min))
658 (unless (org-mode-p)
659 (error
660 "Target buffer \"%s\" for file+headline should be in Org mode"
661 (current-buffer)))
662 (if (re-search-forward
663 (format org-complex-heading-regexp-format (regexp-quote hd))
664 nil t)
665 (goto-char (point-at-bol))
666 (goto-char (point-max))
667 (or (bolp) (insert "\n"))
668 (insert "* " hd "\n")
669 (beginning-of-line 0))))
671 ((eq (car target) 'file+olp)
672 (let ((m (org-find-olp
673 (cons (org-capture-expand-file (nth 1 target))
674 (cddr target)))))
675 (set-buffer (marker-buffer m))
676 (goto-char m)))
678 ((eq (car target) 'file+regexp)
679 (set-buffer (org-capture-target-buffer (nth 1 target)))
680 (goto-char (point-min))
681 (if (re-search-forward (nth 2 target) nil t)
682 (progn
683 (goto-char (if (org-capture-get :prepend)
684 (match-beginning 0) (match-end 0)))
685 (org-capture-put :exact-position (point))
686 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
687 (error "No match for target regexp in file %s" (nth 1 target))))
689 ((memq (car target) '(file+datetree file+datetree+prompt))
690 (require 'org-datetree)
691 (set-buffer (org-capture-target-buffer (nth 1 target)))
692 ;; Make a date tree entry, with the current date (or yesterday,
693 ;; if we are extending dates for a couple of hours)
694 (org-datetree-find-date-create
695 (calendar-gregorian-from-absolute
696 (cond
698 (org-overriding-default-time
699 ;; use the overriding default time
700 (time-to-days org-overriding-default-time))
702 ((eq (car target) 'file+datetree+prompt)
703 ;; prompt for date
704 (time-to-days (org-read-date
705 nil t nil "Date for tree entry:"
706 (current-time))))
708 ;; current date, possible corrected for late night workers
709 (org-today))))))
711 ((eq (car target) 'file+function)
712 (set-buffer (org-capture-target-buffer (nth 1 target)))
713 (funcall (nth 2 target))
714 (org-capture-put :exact-position (point))
715 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
717 ((eq (car target) 'function)
718 (funcall (nth 1 target))
719 (org-capture-put :exact-position (point))
720 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
722 ((eq (car target) 'clock)
723 (if (and (markerp org-clock-hd-marker)
724 (marker-buffer org-clock-hd-marker))
725 (progn (set-buffer (marker-buffer org-clock-hd-marker))
726 (goto-char org-clock-hd-marker))
727 (error "No running clock that could be used as capture target")))
729 (t (error "Invalid capture target specification")))
731 (org-capture-put :buffer (current-buffer) :pos (point)
732 :target-entry-p target-entry-p))))
734 (defun org-capture-expand-file (file)
735 "Expand functions and symbols for FILE.
736 When FILE is a function, call it. When it is a form, evaluate
737 it. When it is a variable, retrieve the value. Return whatever we get."
738 (cond
739 ((org-string-nw-p file) file)
740 ((functionp file) (funcall file))
741 ((and (symbolp file) (boundp file)) (symbol-value file))
742 ((and file (consp file)) (eval file))
743 (t file)))
745 (defun org-capture-target-buffer (file)
746 "Get a buffer for FILE."
747 (setq file (org-capture-expand-file file))
748 (setq file (or (org-string-nw-p file)
749 org-default-notes-file
750 (error "No notes file specified, and no default available")))
751 (or (org-find-base-buffer-visiting file)
752 (progn (org-capture-put :new-buffer t)
753 (find-file-noselect (expand-file-name file org-directory)))))
755 (defun org-capture-steal-local-variables (buffer)
756 "Install Org-mode local variables."
757 (mapc (lambda (v)
758 (ignore-errors (org-set-local (car v) (cdr v))))
759 (buffer-local-variables buffer)))
761 (defun org-capture-place-template ()
762 "Insert the template at the target location, and display the buffer."
763 (org-capture-put :return-to-wconf (current-window-configuration))
764 (delete-other-windows)
765 (org-switch-to-buffer-other-window
766 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
767 (widen)
768 (show-all)
769 (goto-char (org-capture-get :pos))
770 (org-set-local 'org-capture-target-marker
771 (move-marker (make-marker) (point)))
772 (let* ((template (org-capture-get :template))
773 (type (org-capture-get :type)))
774 (case type
775 ((nil entry) (org-capture-place-entry))
776 (table-line (org-capture-place-table-line))
777 (plain (org-capture-place-plain-text))
778 (item (org-capture-place-item))
779 (checkitem (org-capture-place-item))))
780 (org-capture-mode 1)
781 (org-set-local 'org-capture-current-plist org-capture-plist))
783 (defun org-capture-place-entry ()
784 "Place the template as a new Org entry."
785 (let* ((txt (org-capture-get :template))
786 (reversed (org-capture-get :prepend))
787 (target-entry-p (org-capture-get :target-entry-p))
788 level beg end file)
790 (cond
791 ((org-capture-get :exact-position)
792 (goto-char (org-capture-get :exact-position)))
793 ((not target-entry-p)
794 ;; Insert as top-level entry, either at beginning or at end of file
795 (setq level 1)
796 (if reversed
797 (progn (goto-char (point-min))
798 (or (org-at-heading-p)
799 (outline-next-heading)))
800 (goto-char (point-max))
801 (or (bolp) (insert "\n"))))
803 ;; Insert as a child of the current entry
804 (and (looking-at "\\*+")
805 (setq level (- (match-end 0) (match-beginning 0))))
806 (setq level (org-get-valid-level (or level 1) 1))
807 (if reversed
808 (progn
809 (outline-next-heading)
810 (or (bolp) (insert "\n")))
811 (org-end-of-subtree t t)
812 (or (bolp) (insert "\n")))))
813 (org-capture-empty-lines-before)
814 (setq beg (point))
815 (org-capture-verify-tree txt)
816 (org-paste-subtree level txt 'for-yank)
817 (org-capture-empty-lines-after 1)
818 (org-capture-position-for-last-stored beg)
819 (outline-next-heading)
820 (setq end (point))
821 (org-capture-mark-kill-region beg (1- end))
822 (org-capture-narrow beg (1- end))
823 (goto-char beg)
824 (if (re-search-forward "%\\?" end t) (replace-match ""))))
826 (defun org-capture-place-item ()
827 "Place the template as a new plain list item."
828 (let* ((txt (org-capture-get :template))
829 (target-entry-p (org-capture-get :target-entry-p))
830 (ind 0)
831 beg end)
832 (cond
833 ((org-capture-get :exact-position)
834 (goto-char (org-capture-get :exact-position)))
835 ((not target-entry-p)
836 ;; Insert as top-level entry, either at beginning or at end of file
837 (setq beg (point-min) end (point-max)))
839 (setq beg (1+ (point-at-eol))
840 end (save-excursion (outline-next-heading) (point)))))
841 (if (org-capture-get :prepend)
842 (progn
843 (goto-char beg)
844 (if (org-list-search-forward (org-item-beginning-re) end t)
845 (progn
846 (goto-char (match-beginning 0))
847 (setq ind (org-get-indentation)))
848 (goto-char end)
849 (setq ind 0)))
850 (goto-char end)
851 (if (org-list-search-backward (org-item-beginning-re) beg t)
852 (progn
853 (setq ind (org-get-indentation))
854 (org-end-of-item))
855 (setq ind 0)))
856 ;; Remove common indentation
857 (setq txt (org-remove-indentation txt))
858 ;; Make sure this is indeed an item
859 (unless (string-match (concat "\\`" (org-item-re)) txt)
860 (setq txt (concat "- "
861 (mapconcat 'identity (split-string txt "\n")
862 "\n "))))
863 ;; Set the correct indentation, depending on context
864 (setq ind (make-string ind ?\ ))
865 (setq txt (concat ind
866 (mapconcat 'identity (split-string txt "\n")
867 (concat "\n" ind))
868 "\n"))
869 ;; Insert, with surrounding empty lines
870 (org-capture-empty-lines-before)
871 (setq beg (point))
872 (insert txt)
873 (or (bolp) (insert "\n"))
874 (org-capture-empty-lines-after 1)
875 (org-capture-position-for-last-stored beg)
876 (forward-char 1)
877 (setq end (point))
878 (org-capture-mark-kill-region beg (1- end))
879 (org-capture-narrow beg (1- end))
880 (if (re-search-forward "%\\?" end t) (replace-match ""))))
882 (defun org-capture-place-table-line ()
883 "Place the template as a table line."
884 (require 'org-table)
885 (let* ((txt (org-capture-get :template))
886 (target-entry-p (org-capture-get :target-entry-p))
887 (table-line-pos (org-capture-get :table-line-pos))
888 ind beg end)
889 (cond
890 ((org-capture-get :exact-position)
891 (goto-char (org-capture-get :exact-position)))
892 ((not target-entry-p)
893 ;; Table is not necessarily under a heading
894 (setq beg (point-min) end (point-max)))
896 ;; WE are at a heading, limit search to the body
897 (setq beg (1+ (point-at-eol))
898 end (save-excursion (outline-next-heading) (point)))))
899 (if (re-search-forward org-table-dataline-regexp end t)
900 (let ((b (org-table-begin)) (e (org-table-end)))
901 (goto-char e)
902 (if (looking-at "[ \t]*#\\+TBLFM:")
903 (forward-line 1))
904 (narrow-to-region b (point)))
905 (goto-char end)
906 (insert "\n| |\n|----|\n| |\n")
907 (narrow-to-region (1+ end) (point)))
908 ;; We are narrowed to the table, or to an empty line if there was no table
910 ;; Check if the template is good
911 (if (not (string-match org-table-dataline-regexp txt))
912 (setq txt "| %?Bad template |\n"))
913 (cond
914 ((and table-line-pos
915 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
916 ;; we have a complex line specification
917 (goto-char (point-min))
918 (let ((nh (- (match-end 1) (match-beginning 1)))
919 (delta (string-to-number (match-string 2 table-line-pos)))
921 ;; The user wants a special position in the table
922 (org-table-get-specials)
923 (setq ll (ignore-errors (aref org-table-hlines nh)))
924 (unless ll (error "Invalid table line specification \"%s\""
925 table-line-pos))
926 (setq ll (+ ll delta (if (< delta 0) 0 -1)))
927 (org-goto-line ll)
928 (org-table-insert-row 'below)
929 (beginning-of-line 1)
930 (delete-region (point) (1+ (point-at-eol)))
931 (setq beg (point))
932 (insert txt)
933 (setq end (point))))
934 ((org-capture-get :prepend)
935 (goto-char (point-min))
936 (re-search-forward org-table-hline-regexp nil t)
937 (beginning-of-line 1)
938 (re-search-forward org-table-dataline-regexp nil t)
939 (beginning-of-line 1)
940 (setq beg (point))
941 (org-table-insert-row)
942 (beginning-of-line 1)
943 (delete-region (point) (1+ (point-at-eol)))
944 (insert txt)
945 (setq end (point)))
947 (goto-char (point-max))
948 (re-search-backward org-table-dataline-regexp nil t)
949 (beginning-of-line 1)
950 (org-table-insert-row 'below)
951 (beginning-of-line 1)
952 (delete-region (point) (1+ (point-at-eol)))
953 (setq beg (point))
954 (insert txt)
955 (setq end (point))))
956 (goto-char beg)
957 (org-capture-position-for-last-stored 'table-line)
958 (if (re-search-forward "%\\?" end t) (replace-match ""))
959 (org-table-align)))
961 (defun org-capture-place-plain-text ()
962 "Place the template plainly."
963 (let* ((txt (org-capture-get :template))
964 beg end)
965 (goto-char (cond
966 ((org-capture-get :exact-position))
967 ((org-capture-get :prepend) (point-min))
968 (t (point-max))))
969 (or (bolp) (newline))
970 (org-capture-empty-lines-before)
971 (setq beg (point))
972 (insert txt)
973 (org-capture-empty-lines-after 1)
974 (org-capture-position-for-last-stored beg)
975 (setq end (point))
976 (org-capture-mark-kill-region beg (1- end))
977 (org-capture-narrow beg (1- end))
978 (if (re-search-forward "%\\?" end t) (replace-match ""))))
980 (defun org-capture-mark-kill-region (beg end)
981 "Mark the region that will have to be killed when aborting capture."
982 (let ((m1 (move-marker (make-marker) beg))
983 (m2 (move-marker (make-marker) end)))
984 (org-capture-put :begin-marker m1)
985 (org-capture-put :end-marker m2)))
987 (defun org-capture-position-for-last-stored (where)
988 "Memorize the position that should later become the position of last capture."
989 (cond
990 ((integerp where)
991 (org-capture-put :position-for-last-stored
992 (move-marker (make-marker) where
993 (or (buffer-base-buffer (current-buffer))
994 (current-buffer)))))
995 ((eq where 'table-line)
996 (org-capture-put :position-for-last-stored
997 (list 'table-line
998 (org-table-current-dline))))
999 (t (error "This should not happen"))))
1001 (defun org-capture-bookmark-last-stored-position ()
1002 "Bookmark the last-captured position."
1003 (let* ((where (org-capture-get :position-for-last-stored 'local))
1004 (pos (cond
1005 ((markerp where)
1006 (prog1 (marker-position where)
1007 (move-marker where nil)))
1008 ((and (listp where) (eq (car where) 'table-line))
1009 (if (org-at-table-p)
1010 (save-excursion
1011 (org-table-goto-line (nth 1 where))
1012 (point-at-bol))
1013 (point))))))
1014 (with-current-buffer (buffer-base-buffer (current-buffer))
1015 (save-excursion
1016 (save-restriction
1017 (widen)
1018 (goto-char pos)
1019 (bookmark-set "org-capture-last-stored")
1020 (move-marker org-capture-last-stored-marker (point)))))))
1022 (defun org-capture-narrow (beg end)
1023 "Narrow, unless configuration says not to narrow."
1024 (unless (org-capture-get :unnarrowed)
1025 (narrow-to-region beg end)
1026 (goto-char beg)))
1028 (defun org-capture-empty-lines-before (&optional n)
1029 "Arrange for the correct number of empty lines before the insertion point.
1030 Point will be after the empty lines, so insertion can directly be done."
1031 (setq n (or n (org-capture-get :empty-lines) 0))
1032 (let ((pos (point)))
1033 (org-back-over-empty-lines)
1034 (delete-region (point) pos)
1035 (if (> n 0) (newline n))))
1037 (defun org-capture-empty-lines-after (&optional n)
1038 "Arrange for the correct number of empty lines after the inserted string.
1039 Point will remain at the first line after the inserted text."
1040 (setq n (or n (org-capture-get :empty-lines) 0))
1041 (org-back-over-empty-lines)
1042 (while (looking-at "[ \t]*\n") (replace-match ""))
1043 (let ((pos (point)))
1044 (if (> n 0) (newline n))
1045 (goto-char pos)))
1047 (defvar org-clock-marker) ; Defined in org.el
1048 ;;;###autoload
1049 (defun org-capture-insert-template-here ()
1050 (let* ((template (org-capture-get :template))
1051 (type (org-capture-get :type))
1052 beg end pp)
1053 (or (bolp) (newline))
1054 (setq beg (point))
1055 (cond
1056 ((and (eq type 'entry) (org-mode-p))
1057 (org-capture-verify-tree (org-capture-get :template))
1058 (org-paste-subtree nil template t))
1059 ((and (memq type '(item checkitem))
1060 (org-mode-p)
1061 (save-excursion (skip-chars-backward " \t\n")
1062 (setq pp (point))
1063 (org-in-item-p)))
1064 (goto-char pp)
1065 (org-insert-item)
1066 (skip-chars-backward " ")
1067 (skip-chars-backward "-+*0123456789).")
1068 (delete-region (point) (point-at-eol))
1069 (setq beg (point))
1070 (org-remove-indentation template)
1071 (insert template)
1072 (org-capture-empty-lines-after)
1073 (goto-char beg)
1074 (org-list-repair)
1075 (org-end-of-item)
1076 (setq end (point)))
1077 (t (insert template)))
1078 (setq end (point))
1079 (goto-char beg)
1080 (if (re-search-forward "%\\?" end t)
1081 (replace-match ""))))
1083 (defun org-capture-set-plist (entry)
1084 "Initialize the property list from the template definition."
1085 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1086 (org-capture-put :key (car entry) :description (nth 1 entry)
1087 :target (nth 3 entry))
1088 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1089 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1090 ;; The template may be empty or omitted for special types.
1091 ;; Here we insert the default templates for such cases.
1092 (cond
1093 ((eq type 'item) (setq txt "- %?"))
1094 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1095 ((eq type 'table-line) (setq txt "| %? |"))
1096 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1097 (org-capture-put :template txt :type type)))
1099 (defun org-capture-goto-target (&optional template-key)
1100 "Go to the target location of a capture template.
1101 The user is queried for the template."
1102 (interactive)
1103 (let* (org-select-template-temp-major-mode
1104 (entry (org-capture-select-template template-key)))
1105 (unless entry
1106 (error "No capture template selected"))
1107 (org-capture-set-plist entry)
1108 (org-capture-set-target-location)
1109 (switch-to-buffer (org-capture-get :buffer))
1110 (goto-char (org-capture-get :pos))))
1112 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1113 "Make an indirect buffer for a capture process.
1114 Use PREFIX as a prefix for the name of the indirect buffer."
1115 (setq buffer (or buffer (current-buffer)))
1116 (let ((n 1) (base (buffer-name buffer)) bname)
1117 (setq bname (concat prefix "-" base))
1118 (while (buffer-live-p (get-buffer bname))
1119 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1120 (condition-case nil
1121 (make-indirect-buffer buffer bname 'clone)
1122 (error (make-indirect-buffer buffer bname)))))
1125 (defun org-capture-verify-tree (tree)
1126 "Throw error if TREE is not a valid tree"
1127 (unless (org-kill-is-subtree-p tree)
1128 (error "Template is not a valid Org entry or tree")))
1130 ;;; The template code
1132 (defun org-capture-select-template (&optional keys)
1133 "Select a capture template.
1134 Lisp programs can force the template by setting KEYS to a string."
1135 (let ((org-capture-templates
1136 (or org-capture-templates
1137 '(("t" "Task" entry (file+headline "" "Tasks")
1138 "* TODO %?\n %u\n %a")))))
1139 (if keys
1140 (or (assoc keys org-capture-templates)
1141 (error "No capture template referred to by \"%s\" keys" keys))
1142 (org-mks org-capture-templates
1143 "Select a capture template\n========================="
1144 "Template key: "
1145 '(("C" "Customize org-capture-templates")
1146 ("q" "Abort"))))))
1148 (defun org-capture-fill-template (&optional template initial annotation)
1149 "Fill a template and return the filled template as a string.
1150 The template may still contain \"%?\" for cursor positioning."
1151 (setq template (or template (org-capture-get :template)))
1152 (when (stringp initial)
1153 (setq initial (org-no-properties initial))
1154 (remove-text-properties 0 (length initial) '(read-only t) initial))
1155 (let* ((buffer (org-capture-get :buffer))
1156 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1157 (ct (org-capture-get :default-time))
1158 (dct (decode-time ct))
1159 (ct1
1160 (if (< (nth 2 dct) org-extend-today-until)
1161 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
1162 ct))
1163 (plist-p (if org-store-link-plist t nil))
1164 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
1165 (v-x (or (org-get-x-clipboard 'PRIMARY)
1166 (org-get-x-clipboard 'CLIPBOARD)
1167 (org-get-x-clipboard 'SECONDARY)))
1168 (v-t (format-time-string (car org-time-stamp-formats) ct))
1169 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
1170 (v-u (concat "[" (substring v-t 1 -1) "]"))
1171 (v-U (concat "[" (substring v-T 1 -1) "]"))
1172 ;; `initial' and `annotation' might habe been passed.
1173 ;; But if the property list has them, we prefer those values
1174 (v-i (or (plist-get org-store-link-plist :initial)
1175 initial
1176 (org-capture-get :initial)
1177 ""))
1178 (v-a (or (plist-get org-store-link-plist :annotation)
1179 annotation
1180 (org-capture-get :annotation)
1181 ""))
1182 ;; Is the link empty? Then we do not want it...
1183 (v-a (if (equal v-a "[[]]") "" v-a))
1184 (clipboards (remove nil (list v-i
1185 (org-get-x-clipboard 'PRIMARY)
1186 (org-get-x-clipboard 'CLIPBOARD)
1187 (org-get-x-clipboard 'SECONDARY)
1188 v-c)))
1189 (v-A (if (and v-a
1190 (string-match
1191 "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
1192 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
1193 v-a))
1194 (v-n user-full-name)
1195 (v-k (if (marker-buffer org-clock-marker)
1196 (org-substring-no-properties org-clock-heading)))
1197 (v-K (if (marker-buffer org-clock-marker)
1198 (org-make-link-string
1199 (buffer-file-name (marker-buffer org-clock-marker))
1200 org-clock-heading)))
1201 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1202 (v-F (or (org-capture-get :original-file) ""))
1204 (org-startup-folded nil)
1205 (org-inhibit-startup t)
1206 org-time-was-given org-end-time-was-given x
1207 prompt completions char time pos default histvar)
1209 (setq org-store-link-plist
1210 (plist-put org-store-link-plist :annotation v-a)
1211 org-store-link-plist
1212 (plist-put org-store-link-plist :initial v-i))
1213 (setq initial v-i)
1215 (unless template (setq template "") (message "No template") (ding)
1216 (sit-for 1))
1217 (save-window-excursion
1218 (delete-other-windows)
1219 (switch-to-buffer (get-buffer-create "*Capture*"))
1220 (erase-buffer)
1221 (insert template)
1222 (goto-char (point-min))
1223 (org-capture-steal-local-variables buffer)
1224 (setq buffer-file-name nil)
1226 ;; %[] Insert contents of a file.
1227 (goto-char (point-min))
1228 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1229 (unless (org-capture-escaped-%)
1230 (let ((start (match-beginning 0))
1231 (end (match-end 0))
1232 (filename (expand-file-name (match-string 1))))
1233 (goto-char start)
1234 (delete-region start end)
1235 (condition-case error
1236 (insert-file-contents filename)
1237 (error (insert (format "%%![Couldn't insert %s: %s]"
1238 filename error)))))))
1239 ;; %() embedded elisp
1240 (goto-char (point-min))
1241 (while (re-search-forward "%\\((.+)\\)" nil t)
1242 (unless (org-capture-escaped-%)
1243 (goto-char (match-beginning 0))
1244 (let ((template-start (point)))
1245 (forward-char 1)
1246 (let ((result (org-eval (read (current-buffer)))))
1247 (delete-region template-start (point))
1248 (insert result)))))
1250 ;; Simple %-escapes
1251 (goto-char (point-min))
1252 (while (re-search-forward "%\\([tTuUaiAcxkKInfF]\\)" nil t)
1253 (unless (org-capture-escaped-%)
1254 (when (and initial (equal (match-string 0) "%i"))
1255 (save-match-data
1256 (let* ((lead (buffer-substring
1257 (point-at-bol) (match-beginning 0))))
1258 (setq v-i (mapconcat 'identity
1259 (org-split-string initial "\n")
1260 (concat "\n" lead))))))
1261 (replace-match
1262 (or (eval (intern (concat "v-" (match-string 1)))) "")
1263 t t)))
1265 ;; From the property list
1266 (when plist-p
1267 (goto-char (point-min))
1268 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
1269 (unless (org-capture-escaped-%)
1270 (and (setq x (or (plist-get org-store-link-plist
1271 (intern (match-string 1))) ""))
1272 (replace-match x t t)))))
1274 ;; Turn on org-mode in temp buffer, set local variables
1275 ;; This is to support completion in interactive prompts
1276 (let ((org-inhibit-startup t)) (org-mode))
1277 ;; Interactive template entries
1278 (goto-char (point-min))
1279 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
1280 nil t)
1281 (unless (org-capture-escaped-%)
1282 (setq char (if (match-end 3) (match-string-no-properties 3))
1283 prompt (if (match-end 2) (match-string-no-properties 2)))
1284 (goto-char (match-beginning 0))
1285 (replace-match "")
1286 (setq completions nil default nil)
1287 (when prompt
1288 (setq completions (org-split-string prompt "|")
1289 prompt (pop completions)
1290 default (car completions)
1291 histvar (intern (concat
1292 "org-capture-template-prompt-history::"
1293 (or prompt "")))
1294 completions (mapcar 'list completions)))
1295 (unless (boundp histvar) (set histvar nil))
1296 (cond
1297 ((member char '("G" "g"))
1298 (let* ((org-last-tags-completion-table
1299 (org-global-tags-completion-table
1300 (if (equal char "G")
1301 (org-agenda-files)
1302 (and file (list file)))))
1303 (org-add-colon-after-tag-completion t)
1304 (ins (org-icompleting-read
1305 (if prompt (concat prompt ": ") "Tags: ")
1306 'org-tags-completion-function nil nil nil
1307 'org-tags-history)))
1308 (setq ins (mapconcat 'identity
1309 (org-split-string
1310 ins (org-re "[^[:alnum:]_@#%]+"))
1311 ":"))
1312 (when (string-match "\\S-" ins)
1313 (or (equal (char-before) ?:) (insert ":"))
1314 (insert ins)
1315 (or (equal (char-after) ?:) (insert ":"))
1316 (and (org-on-heading-p) (org-set-tags nil 'align)))))
1317 ((equal char "C")
1318 (cond ((= (length clipboards) 1) (insert (car clipboards)))
1319 ((> (length clipboards) 1)
1320 (insert (read-string "Clipboard/kill value: "
1321 (car clipboards) '(clipboards . 1)
1322 (car clipboards))))))
1323 ((equal char "L")
1324 (cond ((= (length clipboards) 1)
1325 (org-insert-link 0 (car clipboards)))
1326 ((> (length clipboards) 1)
1327 (org-insert-link 0 (read-string "Clipboard/kill value: "
1328 (car clipboards)
1329 '(clipboards . 1)
1330 (car clipboards))))))
1331 ((equal char "p")
1332 (org-set-property (org-substring-no-properties prompt) nil))
1333 (char
1334 ;; These are the date/time related ones
1335 (setq org-time-was-given (equal (upcase char) char))
1336 (setq time (org-read-date (equal (upcase char) char) t nil
1337 prompt))
1338 (if (equal (upcase char) char) (setq org-time-was-given t))
1339 (org-insert-time-stamp time org-time-was-given
1340 (member char '("u" "U"))
1341 nil nil (list org-end-time-was-given)))
1343 (let (org-completion-use-ido)
1344 (insert (org-completing-read-no-i
1345 (concat (if prompt prompt "Enter string")
1346 (if default (concat " [" default "]"))
1347 ": ")
1348 completions nil nil nil histvar default)))))))
1349 ;; Make sure there are no empty lines before the text, and that
1350 ;; it ends with a newline character
1351 (goto-char (point-min))
1352 (while (looking-at "[ \t]*\n") (replace-match ""))
1353 (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
1354 ;; Return the expanded tempate and kill the temporary buffer
1355 (untabify (point-min) (point-max))
1356 (set-buffer-modified-p nil)
1357 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1359 (defun org-capture-escaped-% ()
1360 "Check if % was escaped - if yes, unescape it now."
1361 (if (equal (char-before (match-beginning 0)) ?\\)
1362 (progn
1363 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1365 nil))
1367 ;;;###autoload
1368 (defun org-capture-import-remember-templates ()
1369 "Set org-capture-templates to be similar to `org-remember-templates'."
1370 (interactive)
1371 (when (and (yes-or-no-p
1372 "Import old remember templates into org-capture-templates? ")
1373 (yes-or-no-p
1374 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1375 (require 'org-remember)
1376 (setq org-capture-templates
1377 (mapcar
1378 (lambda (entry)
1379 (let ((desc (car entry))
1380 (key (char-to-string (nth 1 entry)))
1381 (template (nth 2 entry))
1382 (file (or (nth 3 entry) org-default-notes-file))
1383 (position (or (nth 4 entry) org-remember-default-headline))
1384 (type 'entry)
1385 (prepend org-reverse-note-order)
1386 immediate target)
1387 (cond
1388 ((member position '(top bottom))
1389 (setq target (list 'file file)
1390 prepend (eq position 'top)))
1391 ((eq position 'date-tree)
1392 (setq target (list 'file+datetree file)
1393 prepend nil))
1394 (t (setq target (list 'file+headline file position))))
1396 (when (string-match "%!" template)
1397 (setq template (replace-match "" t t template)
1398 immediate t))
1400 (append (list key desc type target template)
1401 (if prepend '(:prepend t))
1402 (if immediate '(:immediate-finish t)))))
1404 org-remember-templates))))
1406 (provide 'org-capture)
1408 ;; arch-tag: 986bf41b-8ada-4e28-bf20-e8388a7205a0
1410 ;;; org-capture.el ends here