org-capture.el: make :immediate-finish cooperate with :clock-in.
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-capture.el
blobf11266392eff505da8733a046b42cdb6f1d5837d
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 nil t)
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")))))))))))
471 (defun org-capture-get-template ()
472 "Get the template from a file or a function if necessary."
473 (let ((txt (org-capture-get :template)) file)
474 (cond
475 ((and (listp txt) (eq (car txt) 'file))
476 (if (file-exists-p
477 (setq file (expand-file-name (nth 1 txt) org-directory)))
478 (setq txt (org-file-contents file))
479 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
480 ((and (listp txt) (eq (car txt) 'function))
481 (if (fboundp (nth 1 txt))
482 (setq txt (funcall (nth 1 txt)))
483 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
484 ((not txt) (setq txt ""))
485 ((stringp txt))
486 (t (setq txt "* Invalid capture template")))
487 (org-capture-put :template txt)))
489 (defun org-capture-finalize (&optional stay-with-capture clock-out)
490 "Finalize the capture process.
491 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
492 captured item after finalizing.
493 A second optional argument tells whether finalizing the capture
494 process should clock-out the captured entry."
495 (interactive "P")
496 (unless (and org-capture-mode
497 (buffer-base-buffer (current-buffer)))
498 (error "This does not seem to be a capture buffer for Org-mode"))
500 ;; Did we start the clock in this capture buffer?
501 (when (and org-capture-clock-was-started
502 org-clock-marker (marker-buffer org-clock-marker)
503 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
504 (> org-clock-marker (point-min))
505 (< org-clock-marker (point-max)))
506 ;; Looks like the clock we started is still running. Clock out.
507 (when clock-out (let (org-log-note-clock-out) (org-clock-out)))
508 (when (and (org-capture-get :clock-resume 'local)
509 (markerp (org-capture-get :interrupted-clock 'local))
510 (buffer-live-p (marker-buffer
511 (org-capture-get :interrupted-clock 'local))))
512 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
513 (org-with-point-at clock-in-task
514 (org-clock-in)))
515 (message "Interrupted clock has been resumed")))
517 (let ((beg (point-min))
518 (end (point-max))
519 (abort-note nil))
520 (widen)
522 (if org-note-abort
523 (let ((m1 (org-capture-get :begin-marker 'local))
524 (m2 (org-capture-get :end-marker 'local)))
525 (if (and m1 m2 (= m1 beg) (= m2 end))
526 (progn
527 (setq abort-note 'clean)
528 (kill-region m1 m2))
529 (setq abort-note 'dirty)))
531 ;; Make sure that the empty lines after are correct
532 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
533 (member (org-capture-get :type 'local)
534 '(entry item checkitem plain)))
535 (save-excursion
536 (goto-char end)
537 (or (bolp) (newline))
538 (org-capture-empty-lines-after
539 (or (org-capture-get :empty-lines 'local) 0))))
540 ;; Postprocessing: Update Statistics cookies, do the sorting
541 (when (org-mode-p)
542 (save-excursion
543 (when (ignore-errors (org-back-to-heading))
544 (org-update-parent-todo-statistics)
545 (org-update-checkbox-count)))
546 ;; FIXME Here we should do the sorting
547 ;; If we have added a table line, maybe recompute?
548 (when (and (eq (org-capture-get :type 'local) 'table-line)
549 (org-at-table-p))
550 (if (org-table-get-stored-formulas)
551 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
552 (org-table-align)))
554 ;; Store this place as the last one where we stored something
555 ;; Do the marking in the base buffer, so that it makes sense after
556 ;; the indirect buffer has been killed.
557 (org-capture-bookmark-last-stored-position)
559 ;; Run the hook
560 (run-hooks 'org-capture-before-finalize-hook)
563 ;; Kill the indirect buffer
564 (save-buffer)
565 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
566 (new-buffer (org-capture-get :new-buffer 'local))
567 (kill-buffer (org-capture-get :kill-buffer 'local))
568 (base-buffer (buffer-base-buffer (current-buffer))))
570 ;; Kill the indiret buffer
571 (kill-buffer (current-buffer))
573 ;; Kill the target buffer if that is desired
574 (when (and base-buffer new-buffer kill-buffer)
575 (with-current-buffer base-buffer (save-buffer))
576 (kill-buffer base-buffer))
578 ;; Restore the window configuration before capture
579 (set-window-configuration return-wconf))
581 (run-hooks 'org-capture-after-finalize-hook)
582 ;; Special cases
583 (cond
584 (abort-note
585 (cond
586 ((equal abort-note 'clean)
587 (message "Capture process aborted and target buffer cleaned up"))
588 ((equal abort-note 'dirty)
589 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
590 (stay-with-capture
591 (org-capture-goto-last-stored)))
592 ;; Return if we did store something
593 (not abort-note)))
595 (defun org-capture-refile ()
596 "Finalize the current capture and then refile the entry.
597 Refiling is done from the base buffer, because the indirect buffer is then
598 already gone. Any prefix argument will be passed to the refile command."
599 (interactive)
600 (unless (eq (org-capture-get :type 'local) 'entry)
601 (error
602 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
603 (let ((pos (point))
604 (base (buffer-base-buffer (current-buffer)))
605 (org-refile-for-capture t))
606 (org-capture-finalize)
607 (save-window-excursion
608 (with-current-buffer (or base (current-buffer))
609 (save-excursion
610 (save-restriction
611 (widen)
612 (goto-char pos)
613 (call-interactively 'org-refile)))))))
615 (defun org-capture-kill ()
616 "Abort the current capture process."
617 (interactive)
618 ;; FIXME: This does not do the right thing, we need to remove the new stuff
619 ;; By hand it is easy: undo, then kill the buffer
620 (let ((org-note-abort t) (org-capture-before-finalize-hook nil))
621 (org-capture-finalize)))
623 (defun org-capture-goto-last-stored ()
624 "Go to the location where the last remember note was stored."
625 (interactive)
626 (org-goto-marker-or-bmk org-capture-last-stored-marker
627 "org-capture-last-stored")
628 (message "This is the last note stored by a capture process"))
630 ;;; Supporting functions for handling the process
632 (defun org-capture-set-target-location (&optional target)
633 "Find target buffer and position and store then in the property list."
634 (let ((target-entry-p t))
635 (setq target (or target (org-capture-get :target)))
636 (save-excursion
637 (cond
638 ((eq (car target) 'file)
639 (set-buffer (org-capture-target-buffer (nth 1 target)))
640 (setq target-entry-p nil))
642 ((eq (car target) 'currentfile)
643 (if (not (and (buffer-file-name) (org-mode-p)))
644 (error "Cannot call this capture template outside of an Org buffer")
645 (set-buffer (org-capture-target-buffer (buffer-file-name)))
646 (setq target-entry-p nil)))
648 ((eq (car target) 'id)
649 (let ((loc (org-id-find (nth 1 target))))
650 (if (not loc)
651 (error "Cannot find target ID \"%s\"" (nth 1 target))
652 (set-buffer (org-capture-target-buffer (car loc)))
653 (goto-char (cdr loc)))))
655 ((eq (car target) 'file+headline)
656 (set-buffer (org-capture-target-buffer (nth 1 target)))
657 (let ((hd (nth 2 target)))
658 (goto-char (point-min))
659 (unless (org-mode-p)
660 (error
661 "Target buffer \"%s\" for file+headline should be in Org mode"
662 (current-buffer)))
663 (if (re-search-forward
664 (format org-complex-heading-regexp-format (regexp-quote hd))
665 nil t)
666 (goto-char (point-at-bol))
667 (goto-char (point-max))
668 (or (bolp) (insert "\n"))
669 (insert "* " hd "\n")
670 (beginning-of-line 0))))
672 ((eq (car target) 'file+olp)
673 (let ((m (org-find-olp
674 (cons (org-capture-expand-file (nth 1 target))
675 (cddr target)))))
676 (set-buffer (marker-buffer m))
677 (goto-char m)))
679 ((eq (car target) 'file+regexp)
680 (set-buffer (org-capture-target-buffer (nth 1 target)))
681 (goto-char (point-min))
682 (if (re-search-forward (nth 2 target) nil t)
683 (progn
684 (goto-char (if (org-capture-get :prepend)
685 (match-beginning 0) (match-end 0)))
686 (org-capture-put :exact-position (point))
687 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
688 (error "No match for target regexp in file %s" (nth 1 target))))
690 ((memq (car target) '(file+datetree file+datetree+prompt))
691 (require 'org-datetree)
692 (set-buffer (org-capture-target-buffer (nth 1 target)))
693 ;; Make a date tree entry, with the current date (or yesterday,
694 ;; if we are extending dates for a couple of hours)
695 (org-datetree-find-date-create
696 (calendar-gregorian-from-absolute
697 (cond
699 (org-overriding-default-time
700 ;; use the overriding default time
701 (time-to-days org-overriding-default-time))
703 ((eq (car target) 'file+datetree+prompt)
704 ;; prompt for date
705 (time-to-days (org-read-date
706 nil t nil "Date for tree entry:"
707 (current-time))))
709 ;; current date, possible corrected for late night workers
710 (org-today))))))
712 ((eq (car target) 'file+function)
713 (set-buffer (org-capture-target-buffer (nth 1 target)))
714 (funcall (nth 2 target))
715 (org-capture-put :exact-position (point))
716 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
718 ((eq (car target) 'function)
719 (funcall (nth 1 target))
720 (org-capture-put :exact-position (point))
721 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
723 ((eq (car target) 'clock)
724 (if (and (markerp org-clock-hd-marker)
725 (marker-buffer org-clock-hd-marker))
726 (progn (set-buffer (marker-buffer org-clock-hd-marker))
727 (goto-char org-clock-hd-marker))
728 (error "No running clock that could be used as capture target")))
730 (t (error "Invalid capture target specification")))
732 (org-capture-put :buffer (current-buffer) :pos (point)
733 :target-entry-p target-entry-p))))
735 (defun org-capture-expand-file (file)
736 "Expand functions and symbols for FILE.
737 When FILE is a function, call it. When it is a form, evaluate
738 it. When it is a variable, retrieve the value. Return whatever we get."
739 (cond
740 ((org-string-nw-p file) file)
741 ((functionp file) (funcall file))
742 ((and (symbolp file) (boundp file)) (symbol-value file))
743 ((and file (consp file)) (eval file))
744 (t file)))
746 (defun org-capture-target-buffer (file)
747 "Get a buffer for FILE."
748 (setq file (org-capture-expand-file file))
749 (setq file (or (org-string-nw-p file)
750 org-default-notes-file
751 (error "No notes file specified, and no default available")))
752 (or (org-find-base-buffer-visiting file)
753 (progn (org-capture-put :new-buffer t)
754 (find-file-noselect (expand-file-name file org-directory)))))
756 (defun org-capture-steal-local-variables (buffer)
757 "Install Org-mode local variables."
758 (mapc (lambda (v)
759 (ignore-errors (org-set-local (car v) (cdr v))))
760 (buffer-local-variables buffer)))
762 (defun org-capture-place-template ()
763 "Insert the template at the target location, and display the buffer."
764 (org-capture-put :return-to-wconf (current-window-configuration))
765 (delete-other-windows)
766 (org-switch-to-buffer-other-window
767 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
768 (widen)
769 (show-all)
770 (goto-char (org-capture-get :pos))
771 (org-set-local 'org-capture-target-marker
772 (move-marker (make-marker) (point)))
773 (let* ((template (org-capture-get :template))
774 (type (org-capture-get :type)))
775 (case type
776 ((nil entry) (org-capture-place-entry))
777 (table-line (org-capture-place-table-line))
778 (plain (org-capture-place-plain-text))
779 (item (org-capture-place-item))
780 (checkitem (org-capture-place-item))))
781 (org-capture-mode 1)
782 (org-set-local 'org-capture-current-plist org-capture-plist))
784 (defun org-capture-place-entry ()
785 "Place the template as a new Org entry."
786 (let* ((txt (org-capture-get :template))
787 (reversed (org-capture-get :prepend))
788 (target-entry-p (org-capture-get :target-entry-p))
789 level beg end file)
791 (cond
792 ((org-capture-get :exact-position)
793 (goto-char (org-capture-get :exact-position)))
794 ((not target-entry-p)
795 ;; Insert as top-level entry, either at beginning or at end of file
796 (setq level 1)
797 (if reversed
798 (progn (goto-char (point-min))
799 (or (org-at-heading-p)
800 (outline-next-heading)))
801 (goto-char (point-max))
802 (or (bolp) (insert "\n"))))
804 ;; Insert as a child of the current entry
805 (and (looking-at "\\*+")
806 (setq level (- (match-end 0) (match-beginning 0))))
807 (setq level (org-get-valid-level (or level 1) 1))
808 (if reversed
809 (progn
810 (outline-next-heading)
811 (or (bolp) (insert "\n")))
812 (org-end-of-subtree t t)
813 (or (bolp) (insert "\n")))))
814 (org-capture-empty-lines-before)
815 (setq beg (point))
816 (org-capture-verify-tree txt)
817 (org-paste-subtree level txt 'for-yank)
818 (org-capture-empty-lines-after 1)
819 (org-capture-position-for-last-stored beg)
820 (outline-next-heading)
821 (setq end (point))
822 (org-capture-mark-kill-region beg (1- end))
823 (org-capture-narrow beg (1- end))
824 (goto-char beg)
825 (if (re-search-forward "%\\?" end t) (replace-match ""))))
827 (defun org-capture-place-item ()
828 "Place the template as a new plain list item."
829 (let* ((txt (org-capture-get :template))
830 (target-entry-p (org-capture-get :target-entry-p))
831 (ind 0)
832 beg end)
833 (cond
834 ((org-capture-get :exact-position)
835 (goto-char (org-capture-get :exact-position)))
836 ((not target-entry-p)
837 ;; Insert as top-level entry, either at beginning or at end of file
838 (setq beg (point-min) end (point-max)))
840 (setq beg (1+ (point-at-eol))
841 end (save-excursion (outline-next-heading) (point)))))
842 (if (org-capture-get :prepend)
843 (progn
844 (goto-char beg)
845 (if (org-list-search-forward (org-item-beginning-re) end t)
846 (progn
847 (goto-char (match-beginning 0))
848 (setq ind (org-get-indentation)))
849 (goto-char end)
850 (setq ind 0)))
851 (goto-char end)
852 (if (org-list-search-backward (org-item-beginning-re) beg t)
853 (progn
854 (setq ind (org-get-indentation))
855 (org-end-of-item))
856 (setq ind 0)))
857 ;; Remove common indentation
858 (setq txt (org-remove-indentation txt))
859 ;; Make sure this is indeed an item
860 (unless (string-match (concat "\\`" (org-item-re)) txt)
861 (setq txt (concat "- "
862 (mapconcat 'identity (split-string txt "\n")
863 "\n "))))
864 ;; Set the correct indentation, depending on context
865 (setq ind (make-string ind ?\ ))
866 (setq txt (concat ind
867 (mapconcat 'identity (split-string txt "\n")
868 (concat "\n" ind))
869 "\n"))
870 ;; Insert, with surrounding empty lines
871 (org-capture-empty-lines-before)
872 (setq beg (point))
873 (insert txt)
874 (or (bolp) (insert "\n"))
875 (org-capture-empty-lines-after 1)
876 (org-capture-position-for-last-stored beg)
877 (forward-char 1)
878 (setq end (point))
879 (org-capture-mark-kill-region beg (1- end))
880 (org-capture-narrow beg (1- end))
881 (if (re-search-forward "%\\?" end t) (replace-match ""))))
883 (defun org-capture-place-table-line ()
884 "Place the template as a table line."
885 (require 'org-table)
886 (let* ((txt (org-capture-get :template))
887 (target-entry-p (org-capture-get :target-entry-p))
888 (table-line-pos (org-capture-get :table-line-pos))
889 ind beg end)
890 (cond
891 ((org-capture-get :exact-position)
892 (goto-char (org-capture-get :exact-position)))
893 ((not target-entry-p)
894 ;; Table is not necessarily under a heading
895 (setq beg (point-min) end (point-max)))
897 ;; WE are at a heading, limit search to the body
898 (setq beg (1+ (point-at-eol))
899 end (save-excursion (outline-next-heading) (point)))))
900 (if (re-search-forward org-table-dataline-regexp end t)
901 (let ((b (org-table-begin)) (e (org-table-end)))
902 (goto-char e)
903 (if (looking-at "[ \t]*#\\+TBLFM:")
904 (forward-line 1))
905 (narrow-to-region b (point)))
906 (goto-char end)
907 (insert "\n| |\n|----|\n| |\n")
908 (narrow-to-region (1+ end) (point)))
909 ;; We are narrowed to the table, or to an empty line if there was no table
911 ;; Check if the template is good
912 (if (not (string-match org-table-dataline-regexp txt))
913 (setq txt "| %?Bad template |\n"))
914 (cond
915 ((and table-line-pos
916 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
917 ;; we have a complex line specification
918 (goto-char (point-min))
919 (let ((nh (- (match-end 1) (match-beginning 1)))
920 (delta (string-to-number (match-string 2 table-line-pos)))
922 ;; The user wants a special position in the table
923 (org-table-get-specials)
924 (setq ll (ignore-errors (aref org-table-hlines nh)))
925 (unless ll (error "Invalid table line specification \"%s\""
926 table-line-pos))
927 (setq ll (+ ll delta (if (< delta 0) 0 -1)))
928 (org-goto-line ll)
929 (org-table-insert-row 'below)
930 (beginning-of-line 1)
931 (delete-region (point) (1+ (point-at-eol)))
932 (setq beg (point))
933 (insert txt)
934 (setq end (point))))
935 ((org-capture-get :prepend)
936 (goto-char (point-min))
937 (re-search-forward org-table-hline-regexp nil t)
938 (beginning-of-line 1)
939 (re-search-forward org-table-dataline-regexp nil t)
940 (beginning-of-line 1)
941 (setq beg (point))
942 (org-table-insert-row)
943 (beginning-of-line 1)
944 (delete-region (point) (1+ (point-at-eol)))
945 (insert txt)
946 (setq end (point)))
948 (goto-char (point-max))
949 (re-search-backward org-table-dataline-regexp nil t)
950 (beginning-of-line 1)
951 (org-table-insert-row 'below)
952 (beginning-of-line 1)
953 (delete-region (point) (1+ (point-at-eol)))
954 (setq beg (point))
955 (insert txt)
956 (setq end (point))))
957 (goto-char beg)
958 (org-capture-position-for-last-stored 'table-line)
959 (if (re-search-forward "%\\?" end t) (replace-match ""))
960 (org-table-align)))
962 (defun org-capture-place-plain-text ()
963 "Place the template plainly."
964 (let* ((txt (org-capture-get :template))
965 beg end)
966 (goto-char (cond
967 ((org-capture-get :exact-position))
968 ((org-capture-get :prepend) (point-min))
969 (t (point-max))))
970 (or (bolp) (newline))
971 (org-capture-empty-lines-before)
972 (setq beg (point))
973 (insert txt)
974 (org-capture-empty-lines-after 1)
975 (org-capture-position-for-last-stored beg)
976 (setq end (point))
977 (org-capture-mark-kill-region beg (1- end))
978 (org-capture-narrow beg (1- end))
979 (if (re-search-forward "%\\?" end t) (replace-match ""))))
981 (defun org-capture-mark-kill-region (beg end)
982 "Mark the region that will have to be killed when aborting capture."
983 (let ((m1 (move-marker (make-marker) beg))
984 (m2 (move-marker (make-marker) end)))
985 (org-capture-put :begin-marker m1)
986 (org-capture-put :end-marker m2)))
988 (defun org-capture-position-for-last-stored (where)
989 "Memorize the position that should later become the position of last capture."
990 (cond
991 ((integerp where)
992 (org-capture-put :position-for-last-stored
993 (move-marker (make-marker) where
994 (or (buffer-base-buffer (current-buffer))
995 (current-buffer)))))
996 ((eq where 'table-line)
997 (org-capture-put :position-for-last-stored
998 (list 'table-line
999 (org-table-current-dline))))
1000 (t (error "This should not happen"))))
1002 (defun org-capture-bookmark-last-stored-position ()
1003 "Bookmark the last-captured position."
1004 (let* ((where (org-capture-get :position-for-last-stored 'local))
1005 (pos (cond
1006 ((markerp where)
1007 (prog1 (marker-position where)
1008 (move-marker where nil)))
1009 ((and (listp where) (eq (car where) 'table-line))
1010 (if (org-at-table-p)
1011 (save-excursion
1012 (org-table-goto-line (nth 1 where))
1013 (point-at-bol))
1014 (point))))))
1015 (with-current-buffer (buffer-base-buffer (current-buffer))
1016 (save-excursion
1017 (save-restriction
1018 (widen)
1019 (goto-char pos)
1020 (bookmark-set "org-capture-last-stored")
1021 (move-marker org-capture-last-stored-marker (point)))))))
1023 (defun org-capture-narrow (beg end)
1024 "Narrow, unless configuration says not to narrow."
1025 (unless (org-capture-get :unnarrowed)
1026 (narrow-to-region beg end)
1027 (goto-char beg)))
1029 (defun org-capture-empty-lines-before (&optional n)
1030 "Arrange for the correct number of empty lines before the insertion point.
1031 Point will be after the empty lines, so insertion can directly be done."
1032 (setq n (or n (org-capture-get :empty-lines) 0))
1033 (let ((pos (point)))
1034 (org-back-over-empty-lines)
1035 (delete-region (point) pos)
1036 (if (> n 0) (newline n))))
1038 (defun org-capture-empty-lines-after (&optional n)
1039 "Arrange for the correct number of empty lines after the inserted string.
1040 Point will remain at the first line after the inserted text."
1041 (setq n (or n (org-capture-get :empty-lines) 0))
1042 (org-back-over-empty-lines)
1043 (while (looking-at "[ \t]*\n") (replace-match ""))
1044 (let ((pos (point)))
1045 (if (> n 0) (newline n))
1046 (goto-char pos)))
1048 (defvar org-clock-marker) ; Defined in org.el
1049 ;;;###autoload
1050 (defun org-capture-insert-template-here ()
1051 (let* ((template (org-capture-get :template))
1052 (type (org-capture-get :type))
1053 beg end pp)
1054 (or (bolp) (newline))
1055 (setq beg (point))
1056 (cond
1057 ((and (eq type 'entry) (org-mode-p))
1058 (org-capture-verify-tree (org-capture-get :template))
1059 (org-paste-subtree nil template t))
1060 ((and (memq type '(item checkitem))
1061 (org-mode-p)
1062 (save-excursion (skip-chars-backward " \t\n")
1063 (setq pp (point))
1064 (org-in-item-p)))
1065 (goto-char pp)
1066 (org-insert-item)
1067 (skip-chars-backward " ")
1068 (skip-chars-backward "-+*0123456789).")
1069 (delete-region (point) (point-at-eol))
1070 (setq beg (point))
1071 (org-remove-indentation template)
1072 (insert template)
1073 (org-capture-empty-lines-after)
1074 (goto-char beg)
1075 (org-list-repair)
1076 (org-end-of-item)
1077 (setq end (point)))
1078 (t (insert template)))
1079 (setq end (point))
1080 (goto-char beg)
1081 (if (re-search-forward "%\\?" end t)
1082 (replace-match ""))))
1084 (defun org-capture-set-plist (entry)
1085 "Initialize the property list from the template definition."
1086 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1087 (org-capture-put :key (car entry) :description (nth 1 entry)
1088 :target (nth 3 entry))
1089 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1090 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1091 ;; The template may be empty or omitted for special types.
1092 ;; Here we insert the default templates for such cases.
1093 (cond
1094 ((eq type 'item) (setq txt "- %?"))
1095 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1096 ((eq type 'table-line) (setq txt "| %? |"))
1097 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1098 (org-capture-put :template txt :type type)))
1100 (defun org-capture-goto-target (&optional template-key)
1101 "Go to the target location of a capture template.
1102 The user is queried for the template."
1103 (interactive)
1104 (let* (org-select-template-temp-major-mode
1105 (entry (org-capture-select-template template-key)))
1106 (unless entry
1107 (error "No capture template selected"))
1108 (org-capture-set-plist entry)
1109 (org-capture-set-target-location)
1110 (switch-to-buffer (org-capture-get :buffer))
1111 (goto-char (org-capture-get :pos))))
1113 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1114 "Make an indirect buffer for a capture process.
1115 Use PREFIX as a prefix for the name of the indirect buffer."
1116 (setq buffer (or buffer (current-buffer)))
1117 (let ((n 1) (base (buffer-name buffer)) bname)
1118 (setq bname (concat prefix "-" base))
1119 (while (buffer-live-p (get-buffer bname))
1120 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1121 (condition-case nil
1122 (make-indirect-buffer buffer bname 'clone)
1123 (error (make-indirect-buffer buffer bname)))))
1126 (defun org-capture-verify-tree (tree)
1127 "Throw error if TREE is not a valid tree"
1128 (unless (org-kill-is-subtree-p tree)
1129 (error "Template is not a valid Org entry or tree")))
1131 ;;; The template code
1133 (defun org-capture-select-template (&optional keys)
1134 "Select a capture template.
1135 Lisp programs can force the template by setting KEYS to a string."
1136 (let ((org-capture-templates
1137 (or org-capture-templates
1138 '(("t" "Task" entry (file+headline "" "Tasks")
1139 "* TODO %?\n %u\n %a")))))
1140 (if keys
1141 (or (assoc keys org-capture-templates)
1142 (error "No capture template referred to by \"%s\" keys" keys))
1143 (org-mks org-capture-templates
1144 "Select a capture template\n========================="
1145 "Template key: "
1146 '(("C" "Customize org-capture-templates")
1147 ("q" "Abort"))))))
1149 (defun org-capture-fill-template (&optional template initial annotation)
1150 "Fill a template and return the filled template as a string.
1151 The template may still contain \"%?\" for cursor positioning."
1152 (setq template (or template (org-capture-get :template)))
1153 (when (stringp initial)
1154 (setq initial (org-no-properties initial))
1155 (remove-text-properties 0 (length initial) '(read-only t) initial))
1156 (let* ((buffer (org-capture-get :buffer))
1157 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1158 (ct (org-capture-get :default-time))
1159 (dct (decode-time ct))
1160 (ct1
1161 (if (< (nth 2 dct) org-extend-today-until)
1162 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
1163 ct))
1164 (plist-p (if org-store-link-plist t nil))
1165 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
1166 (v-x (or (org-get-x-clipboard 'PRIMARY)
1167 (org-get-x-clipboard 'CLIPBOARD)
1168 (org-get-x-clipboard 'SECONDARY)))
1169 (v-t (format-time-string (car org-time-stamp-formats) ct))
1170 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
1171 (v-u (concat "[" (substring v-t 1 -1) "]"))
1172 (v-U (concat "[" (substring v-T 1 -1) "]"))
1173 ;; `initial' and `annotation' might habe been passed.
1174 ;; But if the property list has them, we prefer those values
1175 (v-i (or (plist-get org-store-link-plist :initial)
1176 initial
1177 (org-capture-get :initial)
1178 ""))
1179 (v-a (or (plist-get org-store-link-plist :annotation)
1180 annotation
1181 (org-capture-get :annotation)
1182 ""))
1183 ;; Is the link empty? Then we do not want it...
1184 (v-a (if (equal v-a "[[]]") "" v-a))
1185 (clipboards (remove nil (list v-i
1186 (org-get-x-clipboard 'PRIMARY)
1187 (org-get-x-clipboard 'CLIPBOARD)
1188 (org-get-x-clipboard 'SECONDARY)
1189 v-c)))
1190 (v-A (if (and v-a
1191 (string-match
1192 "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
1193 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
1194 v-a))
1195 (v-n user-full-name)
1196 (v-k (if (marker-buffer org-clock-marker)
1197 (org-substring-no-properties org-clock-heading)))
1198 (v-K (if (marker-buffer org-clock-marker)
1199 (org-make-link-string
1200 (buffer-file-name (marker-buffer org-clock-marker))
1201 org-clock-heading)))
1202 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1203 (v-F (or (org-capture-get :original-file) ""))
1205 (org-startup-folded nil)
1206 (org-inhibit-startup t)
1207 org-time-was-given org-end-time-was-given x
1208 prompt completions char time pos default histvar)
1210 (setq org-store-link-plist
1211 (plist-put org-store-link-plist :annotation v-a)
1212 org-store-link-plist
1213 (plist-put org-store-link-plist :initial v-i))
1214 (setq initial v-i)
1216 (unless template (setq template "") (message "No template") (ding)
1217 (sit-for 1))
1218 (save-window-excursion
1219 (delete-other-windows)
1220 (switch-to-buffer (get-buffer-create "*Capture*"))
1221 (erase-buffer)
1222 (insert template)
1223 (goto-char (point-min))
1224 (org-capture-steal-local-variables buffer)
1225 (setq buffer-file-name nil)
1227 ;; %[] Insert contents of a file.
1228 (goto-char (point-min))
1229 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1230 (unless (org-capture-escaped-%)
1231 (let ((start (match-beginning 0))
1232 (end (match-end 0))
1233 (filename (expand-file-name (match-string 1))))
1234 (goto-char start)
1235 (delete-region start end)
1236 (condition-case error
1237 (insert-file-contents filename)
1238 (error (insert (format "%%![Couldn't insert %s: %s]"
1239 filename error)))))))
1240 ;; %() embedded elisp
1241 (goto-char (point-min))
1242 (while (re-search-forward "%\\((.+)\\)" nil t)
1243 (unless (org-capture-escaped-%)
1244 (goto-char (match-beginning 0))
1245 (let ((template-start (point)))
1246 (forward-char 1)
1247 (let ((result (org-eval (read (current-buffer)))))
1248 (delete-region template-start (point))
1249 (insert result)))))
1251 ;; Simple %-escapes
1252 (goto-char (point-min))
1253 (while (re-search-forward "%\\([tTuUaiAcxkKInfF]\\)" nil t)
1254 (unless (org-capture-escaped-%)
1255 (when (and initial (equal (match-string 0) "%i"))
1256 (save-match-data
1257 (let* ((lead (buffer-substring
1258 (point-at-bol) (match-beginning 0))))
1259 (setq v-i (mapconcat 'identity
1260 (org-split-string initial "\n")
1261 (concat "\n" lead))))))
1262 (replace-match
1263 (or (eval (intern (concat "v-" (match-string 1)))) "")
1264 t t)))
1266 ;; From the property list
1267 (when plist-p
1268 (goto-char (point-min))
1269 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
1270 (unless (org-capture-escaped-%)
1271 (and (setq x (or (plist-get org-store-link-plist
1272 (intern (match-string 1))) ""))
1273 (replace-match x t t)))))
1275 ;; Turn on org-mode in temp buffer, set local variables
1276 ;; This is to support completion in interactive prompts
1277 (let ((org-inhibit-startup t)) (org-mode))
1278 ;; Interactive template entries
1279 (goto-char (point-min))
1280 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
1281 nil t)
1282 (unless (org-capture-escaped-%)
1283 (setq char (if (match-end 3) (match-string-no-properties 3))
1284 prompt (if (match-end 2) (match-string-no-properties 2)))
1285 (goto-char (match-beginning 0))
1286 (replace-match "")
1287 (setq completions nil default nil)
1288 (when prompt
1289 (setq completions (org-split-string prompt "|")
1290 prompt (pop completions)
1291 default (car completions)
1292 histvar (intern (concat
1293 "org-capture-template-prompt-history::"
1294 (or prompt "")))
1295 completions (mapcar 'list completions)))
1296 (unless (boundp histvar) (set histvar nil))
1297 (cond
1298 ((member char '("G" "g"))
1299 (let* ((org-last-tags-completion-table
1300 (org-global-tags-completion-table
1301 (if (equal char "G")
1302 (org-agenda-files)
1303 (and file (list file)))))
1304 (org-add-colon-after-tag-completion t)
1305 (ins (org-icompleting-read
1306 (if prompt (concat prompt ": ") "Tags: ")
1307 'org-tags-completion-function nil nil nil
1308 'org-tags-history)))
1309 (setq ins (mapconcat 'identity
1310 (org-split-string
1311 ins (org-re "[^[:alnum:]_@#%]+"))
1312 ":"))
1313 (when (string-match "\\S-" ins)
1314 (or (equal (char-before) ?:) (insert ":"))
1315 (insert ins)
1316 (or (equal (char-after) ?:) (insert ":"))
1317 (and (org-on-heading-p) (org-set-tags nil 'align)))))
1318 ((equal char "C")
1319 (cond ((= (length clipboards) 1) (insert (car clipboards)))
1320 ((> (length clipboards) 1)
1321 (insert (read-string "Clipboard/kill value: "
1322 (car clipboards) '(clipboards . 1)
1323 (car clipboards))))))
1324 ((equal char "L")
1325 (cond ((= (length clipboards) 1)
1326 (org-insert-link 0 (car clipboards)))
1327 ((> (length clipboards) 1)
1328 (org-insert-link 0 (read-string "Clipboard/kill value: "
1329 (car clipboards)
1330 '(clipboards . 1)
1331 (car clipboards))))))
1332 ((equal char "p")
1333 (org-set-property (org-substring-no-properties prompt) nil))
1334 (char
1335 ;; These are the date/time related ones
1336 (setq org-time-was-given (equal (upcase char) char))
1337 (setq time (org-read-date (equal (upcase char) char) t nil
1338 prompt))
1339 (if (equal (upcase char) char) (setq org-time-was-given t))
1340 (org-insert-time-stamp time org-time-was-given
1341 (member char '("u" "U"))
1342 nil nil (list org-end-time-was-given)))
1344 (let (org-completion-use-ido)
1345 (insert (org-completing-read-no-i
1346 (concat (if prompt prompt "Enter string")
1347 (if default (concat " [" default "]"))
1348 ": ")
1349 completions nil nil nil histvar default)))))))
1350 ;; Make sure there are no empty lines before the text, and that
1351 ;; it ends with a newline character
1352 (goto-char (point-min))
1353 (while (looking-at "[ \t]*\n") (replace-match ""))
1354 (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
1355 ;; Return the expanded tempate and kill the temporary buffer
1356 (untabify (point-min) (point-max))
1357 (set-buffer-modified-p nil)
1358 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1360 (defun org-capture-escaped-% ()
1361 "Check if % was escaped - if yes, unescape it now."
1362 (if (equal (char-before (match-beginning 0)) ?\\)
1363 (progn
1364 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1366 nil))
1368 ;;;###autoload
1369 (defun org-capture-import-remember-templates ()
1370 "Set org-capture-templates to be similar to `org-remember-templates'."
1371 (interactive)
1372 (when (and (yes-or-no-p
1373 "Import old remember templates into org-capture-templates? ")
1374 (yes-or-no-p
1375 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1376 (require 'org-remember)
1377 (setq org-capture-templates
1378 (mapcar
1379 (lambda (entry)
1380 (let ((desc (car entry))
1381 (key (char-to-string (nth 1 entry)))
1382 (template (nth 2 entry))
1383 (file (or (nth 3 entry) org-default-notes-file))
1384 (position (or (nth 4 entry) org-remember-default-headline))
1385 (type 'entry)
1386 (prepend org-reverse-note-order)
1387 immediate target)
1388 (cond
1389 ((member position '(top bottom))
1390 (setq target (list 'file file)
1391 prepend (eq position 'top)))
1392 ((eq position 'date-tree)
1393 (setq target (list 'file+datetree file)
1394 prepend nil))
1395 (t (setq target (list 'file+headline file position))))
1397 (when (string-match "%!" template)
1398 (setq template (replace-match "" t t template)
1399 immediate t))
1401 (append (list key desc type target template)
1402 (if prepend '(:prepend t))
1403 (if immediate '(:immediate-finish t)))))
1405 org-remember-templates))))
1407 (provide 'org-capture)
1409 ;; arch-tag: 986bf41b-8ada-4e28-bf20-e8388a7205a0
1411 ;;; org-capture.el ends here