Merge branch 'maint'
[org-mode.git] / lisp / org-capture.el
blob93a7f2ab265ec1f702e600204758bdbf0286786f
1 ;;; org-capture.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2010-2015 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 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains an alternative implementation of the functionality
28 ;; that used to be provided by org-remember.el. The implementation is more
29 ;; streamlined, can produce more target types (e.g. plain list items or
30 ;; table lines). Also, it does not use a temporary buffer for editing
31 ;; the captured entry - instead it uses an indirect buffer that visits
32 ;; the new entry already in the target buffer (this was an idea by Samuel
33 ;; Wales). John Wiegley's excellent `remember.el' is not needed anymore
34 ;; for this implementation, even though we borrow heavily from its ideas.
36 ;; This implementation heavily draws on ideas by James TD Smith and
37 ;; Samuel Wales, and, of cause, uses John Wiegley's remember.el as inspiration.
39 ;;; TODO
41 ;; - find a clever way to not always insert an annotation maybe a
42 ;; predicate function that can check for conditions for %a to be
43 ;; used. This could be one of the properties.
45 ;; - Should there be plist members that arrange for properties to be
46 ;; asked for, like James proposed in his RFC?
48 ;;; Code:
50 (eval-when-compile
51 (require 'cl))
52 (require 'org)
54 (declare-function org-datetree-find-date-create "org-datetree"
55 (date &optional keep-restriction))
56 (declare-function org-table-analyze "org-table" ())
57 (declare-function org-table-goto-line "org-table" (N))
58 (declare-function org-pop-to-buffer-same-window "org-compat"
59 (&optional buffer-or-name norecord label))
60 (declare-function org-at-encrypted-entry-p "org-crypt" ())
61 (declare-function org-encrypt-entry "org-crypt" ())
62 (declare-function org-decrypt-entry "org-crypt" ())
64 (defvar org-remember-default-headline)
65 (defvar org-remember-templates)
66 (defvar org-table-hlines)
67 (defvar org-table-current-begin-pos)
68 (defvar dired-buffers)
70 (defvar org-capture-clock-was-started nil
71 "Internal flag, noting if the clock was started.")
73 (defvar org-capture-last-stored-marker (make-marker)
74 "Marker pointing to the entry most recently stored with `org-capture'.")
76 ;; The following variable is scoped dynamically by org-protocol
77 ;; to indicate that the link properties have already been stored
78 (defvar org-capture-link-is-already-stored nil)
80 (defgroup org-capture nil
81 "Options concerning capturing new entries."
82 :tag "Org Capture"
83 :group 'org)
85 (defcustom org-capture-templates nil
86 "Templates for the creation of new entries.
88 Each entry is a list with the following items:
90 keys The keys that will select the template, as a string, characters
91 only, for example \"a\" for a template to be selected with a
92 single key, or \"bt\" for selection with two keys. When using
93 several keys, keys using the same prefix key must be together
94 in the list and preceded by a 2-element entry explaining the
95 prefix key, for example
97 (\"b\" \"Templates for marking stuff to buy\")
99 The \"C\" key is used by default for quick access to the
100 customization of the template variable. But if you want to use
101 that key for a template, you can.
103 description A short string describing the template, will be shown during
104 selection.
106 type The type of entry. Valid types are:
107 entry an Org-mode node, with a headline. Will be
108 filed as the child of the target entry or as
109 a top-level entry.
110 item a plain list item, will be placed in the
111 first plain list at the target
112 location.
113 checkitem a checkbox item. This differs from the
114 plain list item only is so far as it uses a
115 different default template.
116 table-line a new line in the first table at target location.
117 plain text to be inserted as it is.
119 target Specification of where the captured item should be placed.
120 In Org-mode files, targets usually define a node. Entries will
121 become children of this node, other types will be added to the
122 table or list in the body of this node.
124 Most target specifications contain a file name. If that file
125 name is the empty string, it defaults to `org-default-notes-file'.
126 A file can also be given as a variable, function, or Emacs Lisp
127 form.
129 Valid values are:
131 (file \"path/to/file\")
132 Text will be placed at the beginning or end of that file
134 (id \"id of existing org entry\")
135 File as child of this entry, or in the body of the entry
137 (file+headline \"path/to/file\" \"node headline\")
138 Fast configuration if the target heading is unique in the file
140 (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
141 For non-unique headings, the full path is safer
143 (file+regexp \"path/to/file\" \"regexp to find location\")
144 File to the entry matching regexp
146 (file+datetree \"path/to/file\")
147 Will create a heading in a date tree for today's date
149 (file+datetree+prompt \"path/to/file\")
150 Will create a heading in a date tree, prompts for date
152 (file+function \"path/to/file\" function-finding-location)
153 A function to find the right location in the file
155 (clock)
156 File to the entry that is currently being clocked
158 (function function-finding-location)
159 Most general way, write your own function to find both
160 file and location
162 template The template for creating the capture item. If you leave this
163 empty, an appropriate default template will be used. See below
164 for more details. Instead of a string, this may also be one of
166 (file \"/path/to/template-file\")
167 (function function-returning-the-template)
169 in order to get a template from a file, or dynamically
170 from a function.
172 The rest of the entry is a property list of additional options. Recognized
173 properties are:
175 :prepend Normally newly captured information will be appended at
176 the target location (last child, last table line,
177 last list item...). Setting this property will
178 change that.
180 :immediate-finish When set, do not offer to edit the information, just
181 file it away immediately. This makes sense if the
182 template only needs information that can be added
183 automatically.
185 :jump-to-captured When set, jump to the captured entry when finished.
187 :empty-lines Set this to the number of lines the should be inserted
188 before and after the new item. Default 0, only common
189 other value is 1.
191 :empty-lines-before Set this to the number of lines the should be inserted
192 before the new item. Overrides :empty-lines for the
193 number lines inserted before.
195 :empty-lines-after Set this to the number of lines the should be inserted
196 after the new item. Overrides :empty-lines for the
197 number of lines inserted after.
199 :clock-in Start the clock in this item.
201 :clock-keep Keep the clock running when filing the captured entry.
203 :clock-resume Start the interrupted clock when finishing the capture.
204 Note that :clock-keep has precedence over :clock-resume.
205 When setting both to t, the current clock will run and
206 the previous one will not be resumed.
208 :unnarrowed Do not narrow the target buffer, simply show the
209 full buffer. Default is to narrow it so that you
210 only see the new stuff.
212 :table-line-pos Specification of the location in the table where the
213 new line should be inserted. It should be a string like
214 \"II-3\", meaning that the new line should become the
215 third line before the second horizontal separator line.
217 :kill-buffer If the target file was not yet visited by a buffer when
218 capture was invoked, kill the buffer again after capture
219 is finalized.
221 The template defines the text to be inserted. Often this is an
222 org-mode entry (so the first line should start with a star) that
223 will be filed as a child of the target headline. It can also be
224 freely formatted text. Furthermore, the following %-escapes will
225 be replaced with content and expanded in this order:
227 %[pathname] Insert the contents of the file given by `pathname'.
228 %(sexp) Evaluate elisp `(sexp)' and replace it with the results.
229 For convenience, %:keyword (see below) placeholders within
230 the expression will be expanded prior to this.
231 %<...> The result of format-time-string on the ... format specification.
232 %t Time stamp, date only.
233 %T Time stamp with date and time.
234 %u, %U Like the above, but inactive time stamps.
235 %i Initial content, copied from the active region. If %i is
236 indented, the entire inserted text will be indented as well.
237 %a Annotation, normally the link created with `org-store-link'.
238 %A Like %a, but prompt for the description part.
239 %l Like %a, but only insert the literal link.
240 %c Current kill ring head.
241 %x Content of the X clipboard.
242 %k Title of currently clocked task.
243 %K Link to currently clocked task.
244 %n User name (taken from the variable `user-full-name').
245 %f File visited by current buffer when org-capture was called.
246 %F Full path of the file or directory visited by current buffer.
247 %:keyword Specific information for certain link types, see below.
248 %^g Prompt for tags, with completion on tags in target file.
249 %^G Prompt for tags, with completion on all tags in all agenda files.
250 %^t Like %t, but prompt for date. Similarly %^T, %^u, %^U.
251 You may define a prompt like: %^{Please specify birthday}t
252 %^C Interactive selection of which kill or clip to use.
253 %^L Like %^C, but insert as link.
254 %^{prop}p Prompt the user for a value for property `prop'.
255 %^{prompt} Prompt the user for a string and replace this sequence with it.
256 A default value and a completion table ca be specified like this:
257 %^{prompt|default|completion2|completion3|...}.
258 %? After completing the template, position cursor here.
259 %\\n Insert the text entered at the nth %^{prompt}, where `n' is
260 a number, starting from 1.
262 Apart from these general escapes, you can access information specific to
263 the link type that is created. For example, calling `org-capture' in emails
264 or in Gnus will record the author and the subject of the message, which you
265 can access with \"%:from\" and \"%:subject\", respectively. Here is a
266 complete list of what is recorded for each link type.
268 Link type | Available information
269 ------------------------+------------------------------------------------------
270 bbdb | %:type %:name %:company
271 vm, wl, mh, mew, rmail, | %:type %:subject %:message-id
272 gnus | %:from %:fromname %:fromaddress
273 | %:to %:toname %:toaddress
274 | %:fromto (either \"to NAME\" or \"from NAME\")
275 | %:date %:date-timestamp (as active timestamp)
276 | %:date-timestamp-inactive (as inactive timestamp)
277 gnus | %:group, for messages also all email fields
278 w3, w3m | %:type %:url
279 info | %:type %:file %:node
280 calendar | %:type %:date"
281 :group 'org-capture
282 :version "24.1"
283 :type
284 '(repeat
285 (choice :value ("" "" entry (file "~/org/notes.org") "")
286 (list :tag "Multikey description"
287 (string :tag "Keys ")
288 (string :tag "Description"))
289 (list :tag "Template entry"
290 (string :tag "Keys ")
291 (string :tag "Description ")
292 (choice :tag "Capture Type " :value entry
293 (const :tag "Org entry" entry)
294 (const :tag "Plain list item" item)
295 (const :tag "Checkbox item" checkitem)
296 (const :tag "Plain text" plain)
297 (const :tag "Table line" table-line))
298 (choice :tag "Target location"
299 (list :tag "File"
300 (const :format "" file)
301 (file :tag " File"))
302 (list :tag "ID"
303 (const :format "" id)
304 (string :tag " ID"))
305 (list :tag "File & Headline"
306 (const :format "" file+headline)
307 (file :tag " File ")
308 (string :tag " Headline"))
309 (list :tag "File & Outline path"
310 (const :format "" file+olp)
311 (file :tag " File ")
312 (repeat :tag "Outline path" :inline t
313 (string :tag "Headline")))
314 (list :tag "File & Regexp"
315 (const :format "" file+regexp)
316 (file :tag " File ")
317 (regexp :tag " Regexp"))
318 (list :tag "File & Date tree"
319 (const :format "" file+datetree)
320 (file :tag " File"))
321 (list :tag "File & Date tree, prompt for date"
322 (const :format "" file+datetree+prompt)
323 (file :tag " File"))
324 (list :tag "File & function"
325 (const :format "" file+function)
326 (file :tag " File ")
327 (sexp :tag " Function"))
328 (list :tag "Current clocking task"
329 (const :format "" clock))
330 (list :tag "Function"
331 (const :format "" function)
332 (sexp :tag " Function")))
333 (choice :tag "Template"
334 (string)
335 (list :tag "File"
336 (const :format "" file)
337 (file :tag "Template file"))
338 (list :tag "Function"
339 (const :format "" function)
340 (function :tag "Template function")))
341 (plist :inline t
342 ;; Give the most common options as checkboxes
343 :options (((const :format "%v " :prepend) (const t))
344 ((const :format "%v " :immediate-finish) (const t))
345 ((const :format "%v " :jump-to-captured) (const t))
346 ((const :format "%v " :empty-lines) (const 1))
347 ((const :format "%v " :empty-lines-before) (const 1))
348 ((const :format "%v " :empty-lines-after) (const 1))
349 ((const :format "%v " :clock-in) (const t))
350 ((const :format "%v " :clock-keep) (const t))
351 ((const :format "%v " :clock-resume) (const t))
352 ((const :format "%v " :unnarrowed) (const t))
353 ((const :format "%v " :table-line-pos) (const t))
354 ((const :format "%v " :kill-buffer) (const t))))))))
356 (defcustom org-capture-before-finalize-hook nil
357 "Hook that is run right before a capture process is finalized.
358 The capture buffer is still current when this hook runs and it is
359 widened to the entire buffer."
360 :group 'org-capture
361 :version "24.1"
362 :type 'hook)
364 (defcustom org-capture-after-finalize-hook nil
365 "Hook that is run right after a capture process is finalized.
366 Suitable for window cleanup."
367 :group 'org-capture
368 :version "24.1"
369 :type 'hook)
371 (defcustom org-capture-prepare-finalize-hook nil
372 "Hook that is run before the finalization starts.
373 The capture buffer is current and still narrowed."
374 :group 'org-capture
375 :version "24.1"
376 :type 'hook)
378 (defcustom org-capture-bookmark t
379 "When non-nil, add a bookmark pointing at the last stored
380 position when capturing."
381 :group 'org-capture
382 :version "24.3"
383 :type 'boolean)
385 ;;; The property list for keeping information about the capture process
387 (defvar org-capture-plist nil
388 "Plist for the current capture process, global, to avoid having to pass it.")
390 (defvar org-capture-current-plist nil
391 "Local variable holding the plist in a capture buffer.
392 This is used to store the plist for use when finishing a capture process
393 because another such process might have changed the global variable by then.
395 Each time a new capture buffer has been set up, the global `org-capture-plist'
396 is copied to this variable, which is local in the indirect buffer.")
398 (defvar org-capture-clock-keep nil
399 "Local variable to store the value of the :clock-keep parameter.
400 This is needed in case org-capture-finalize is called interactively.")
402 (defun org-capture-put (&rest stuff)
403 "Add properties to the capture property list `org-capture-plist'."
404 (while stuff
405 (setq org-capture-plist (plist-put org-capture-plist
406 (pop stuff) (pop stuff)))))
407 (defun org-capture-get (prop &optional local)
408 "Get properties from the capture property list `org-capture-plist'.
409 When LOCAL is set, use the local variable `org-capture-current-plist',
410 this is necessary after initialization of the capture process,
411 to avoid conflicts with other active capture processes."
412 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
414 (defun org-capture-member (prop &optional local)
415 "Is PROP a property in `org-capture-plist'.
416 When LOCAL is set, use the local variable `org-capture-current-plist',
417 this is necessary after initialization of the capture process,
418 to avoid conflicts with other active capture processes."
419 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
421 ;;; The minor mode
423 (defvar org-capture-mode-map (make-sparse-keymap)
424 "Keymap for `org-capture-mode', a minor mode.
425 Use this map to set additional keybindings for when Org-mode is used
426 for a capture buffer.")
428 (defvar org-capture-mode-hook nil
429 "Hook for the minor `org-capture-mode'.")
431 (define-minor-mode org-capture-mode
432 "Minor mode for special key bindings in a capture buffer.
434 Turning on this mode runs the normal hook `org-capture-mode-hook'."
435 nil " Rem" org-capture-mode-map
436 (org-set-local
437 'header-line-format
438 (substitute-command-keys
439 "\\<org-capture-mode-map>Capture buffer. Finish \\[org-capture-finalize], \
440 refile \\[org-capture-refile], abort \\[org-capture-kill].")))
441 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
442 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
443 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
445 ;;; The main commands
447 (defvar org-capture-initial nil)
448 (defvar org-capture-entry nil)
450 ;;;###autoload
451 (defun org-capture-string (string &optional keys)
452 "Capture STRING with the template selected by KEYS."
453 (interactive "sInitial text: \n")
454 (let ((org-capture-initial string)
455 (org-capture-entry (org-capture-select-template keys)))
456 (org-capture)))
458 (defcustom org-capture-templates-contexts nil
459 "Alist of capture templates and valid contexts.
461 For example, if you have a capture template \"c\" and you want
462 this template to be accessible only from `message-mode' buffers,
463 use this:
465 '((\"c\" ((in-mode . \"message-mode\"))))
467 Here are the available contexts definitions:
469 in-file: command displayed only in matching files
470 in-mode: command displayed only in matching modes
471 not-in-file: command not displayed in matching files
472 not-in-mode: command not displayed in matching modes
473 in-buffer: command displayed only in matching buffers
474 not-in-buffer: command not displayed in matching buffers
475 [function]: a custom function taking no argument
477 If you define several checks, the agenda command will be
478 accessible if there is at least one valid check.
480 You can also bind a key to another agenda custom command
481 depending on contextual rules.
483 '((\"c\" \"d\" ((in-mode . \"message-mode\"))))
485 Here it means: in `message-mode buffers', use \"c\" as the
486 key for the capture template otherwise associated with \"d\".
487 \(The template originally associated with \"d\" is not displayed
488 to avoid duplicates.)"
489 :version "24.3"
490 :group 'org-capture
491 :type '(repeat (list :tag "Rule"
492 (string :tag " Capture key")
493 (string :tag "Replace by template")
494 (repeat :tag "Available when"
495 (choice
496 (cons :tag "Condition"
497 (choice
498 (const :tag "In file" in-file)
499 (const :tag "Not in file" not-in-file)
500 (const :tag "In buffer" in-buffer)
501 (const :tag "Not in buffer" not-in-buffer)
502 (const :tag "In mode" in-mode)
503 (const :tag "Not in mode" not-in-mode))
504 (regexp))
505 (function :tag "Custom function"))))))
507 (defcustom org-capture-use-agenda-date nil
508 "Non-nil means use the date at point when capturing from agendas.
509 When nil, you can still capture using the date at point with \\[org-agenda-capture]."
510 :group 'org-capture
511 :version "24.3"
512 :type 'boolean)
514 ;;;###autoload
515 (defun org-capture (&optional goto keys)
516 "Capture something.
517 \\<org-capture-mode-map>
518 This will let you select a template from `org-capture-templates', and then
519 file the newly captured information. The text is immediately inserted
520 at the target location, and an indirect buffer is shown where you can
521 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
522 of Emacs, so that you can continue your work.
524 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
525 anything, just go to the file/headline where the selected template
526 stores its notes. With a double prefix argument \
527 \\[universal-argument] \\[universal-argument], go to the last note
528 stored.
530 When called with a `C-0' (zero) prefix, insert a template at point.
532 ELisp programs can set KEYS to a string associated with a template
533 in `org-capture-templates'. In this case, interactive selection
534 will be bypassed.
536 If `org-capture-use-agenda-date' is non-nil, capturing from the
537 agenda will use the date at point as the default date. Then, a
538 `C-1' prefix will tell the capture process to use the HH:MM time
539 of the day at point (if any) or the current HH:MM time."
540 (interactive "P")
541 (when (and org-capture-use-agenda-date
542 (eq major-mode 'org-agenda-mode))
543 (setq org-overriding-default-time
544 (org-get-cursor-date (equal goto 1))))
545 (cond
546 ((equal goto '(4)) (org-capture-goto-target))
547 ((equal goto '(16)) (org-capture-goto-last-stored))
549 ;; FIXME: Are these needed?
550 (let* ((orig-buf (current-buffer))
551 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
552 org-capture-link-is-already-stored)
553 (plist-get org-store-link-plist :annotation)
554 (ignore-errors (org-store-link nil))))
555 (entry (or org-capture-entry (org-capture-select-template keys)))
556 initial)
557 (setq initial (or org-capture-initial
558 (and (org-region-active-p)
559 (buffer-substring (point) (mark)))))
560 (when (stringp initial)
561 (remove-text-properties 0 (length initial) '(read-only t) initial))
562 (when (stringp annotation)
563 (remove-text-properties 0 (length annotation)
564 '(read-only t) annotation))
565 (cond
566 ((equal entry "C")
567 (customize-variable 'org-capture-templates))
568 ((equal entry "q")
569 (error "Abort"))
571 (org-capture-set-plist entry)
572 (org-capture-get-template)
573 (org-capture-put :original-buffer orig-buf
574 :original-file (or (buffer-file-name orig-buf)
575 (and (featurep 'dired)
576 (car (rassq orig-buf
577 dired-buffers))))
578 :original-file-nondirectory
579 (and (buffer-file-name orig-buf)
580 (file-name-nondirectory
581 (buffer-file-name orig-buf)))
582 :annotation annotation
583 :initial initial
584 :return-to-wconf (current-window-configuration)
585 :default-time
586 (or org-overriding-default-time
587 (org-current-time)))
588 (org-capture-set-target-location)
589 (condition-case error
590 (org-capture-put :template (org-capture-fill-template))
591 ((error quit)
592 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
593 (error "Capture abort: %s" error)))
595 (setq org-capture-clock-keep (org-capture-get :clock-keep))
596 (if (equal goto 0)
597 ;;insert at point
598 (org-capture-insert-template-here)
599 (condition-case error
600 (org-capture-place-template
601 (equal (car (org-capture-get :target)) 'function))
602 ((error quit)
603 (if (and (buffer-base-buffer (current-buffer))
604 (string-match "\\`CAPTURE-" (buffer-name)))
605 (kill-buffer (current-buffer)))
606 (set-window-configuration (org-capture-get :return-to-wconf))
607 (error "Capture template `%s': %s"
608 (org-capture-get :key)
609 (nth 1 error))))
610 (if (and (derived-mode-p 'org-mode)
611 (org-capture-get :clock-in))
612 (condition-case nil
613 (progn
614 (if (org-clock-is-active)
615 (org-capture-put :interrupted-clock
616 (copy-marker org-clock-marker)))
617 (org-clock-in)
618 (org-set-local 'org-capture-clock-was-started t))
619 (error
620 "Could not start the clock in this capture buffer")))
621 (if (org-capture-get :immediate-finish)
622 (org-capture-finalize)))))))))
624 (defun org-capture-get-template ()
625 "Get the template from a file or a function if necessary."
626 (let ((txt (org-capture-get :template)) file)
627 (cond
628 ((and (listp txt) (eq (car txt) 'file))
629 (if (file-exists-p
630 (setq file (expand-file-name (nth 1 txt) org-directory)))
631 (setq txt (org-file-contents file))
632 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
633 ((and (listp txt) (eq (car txt) 'function))
634 (if (fboundp (nth 1 txt))
635 (setq txt (funcall (nth 1 txt)))
636 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
637 ((not txt) (setq txt ""))
638 ((stringp txt))
639 (t (setq txt "* Invalid capture template")))
640 (org-capture-put :template txt)))
642 (defun org-capture-finalize (&optional stay-with-capture)
643 "Finalize the capture process.
644 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
645 captured item after finalizing."
646 (interactive "P")
647 (when (org-capture-get :jump-to-captured)
648 (setq stay-with-capture t))
649 (unless (and org-capture-mode
650 (buffer-base-buffer (current-buffer)))
651 (error "This does not seem to be a capture buffer for Org-mode"))
653 (run-hooks 'org-capture-prepare-finalize-hook)
655 ;; Did we start the clock in this capture buffer?
656 (when (and org-capture-clock-was-started
657 org-clock-marker (marker-buffer org-clock-marker)
658 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
659 (> org-clock-marker (point-min))
660 (< org-clock-marker (point-max)))
661 ;; Looks like the clock we started is still running. Clock out.
662 (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
663 (when (and (not org-capture-clock-keep)
664 (org-capture-get :clock-resume 'local)
665 (markerp (org-capture-get :interrupted-clock 'local))
666 (buffer-live-p (marker-buffer
667 (org-capture-get :interrupted-clock 'local))))
668 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
669 (org-with-point-at clock-in-task
670 (org-clock-in)))
671 (message "Interrupted clock has been resumed")))
673 (let ((beg (point-min))
674 (end (point-max))
675 (abort-note nil))
676 ;; Store the size of the capture buffer
677 (org-capture-put :captured-entry-size (- (point-max) (point-min)))
678 (widen)
679 ;; Store the insertion point in the target buffer
680 (org-capture-put :insertion-point (point))
682 (if org-note-abort
683 (let ((m1 (org-capture-get :begin-marker 'local))
684 (m2 (org-capture-get :end-marker 'local)))
685 (if (and m1 m2 (= m1 beg) (= m2 end))
686 (progn
687 (setq m2 (if (cdr (assoc 'heading org-blank-before-new-entry))
688 m2 (1+ m2))
689 m2 (if (< (point-max) m2) (point-max) m2))
690 (setq abort-note 'clean)
691 (kill-region m1 m2))
692 (setq abort-note 'dirty)))
694 ;; Make sure that the empty lines after are correct
695 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
696 (member (org-capture-get :type 'local)
697 '(entry item checkitem plain)))
698 (save-excursion
699 (goto-char end)
700 (or (bolp) (newline))
701 (org-capture-empty-lines-after
702 (or (org-capture-get :empty-lines-after 'local)
703 (org-capture-get :empty-lines 'local) 0))))
704 ;; Postprocessing: Update Statistics cookies, do the sorting
705 (when (derived-mode-p 'org-mode)
706 (save-excursion
707 (when (ignore-errors (org-back-to-heading))
708 (org-update-parent-todo-statistics)
709 (org-update-checkbox-count)))
710 ;; FIXME Here we should do the sorting
711 ;; If we have added a table line, maybe recompute?
712 (when (and (eq (org-capture-get :type 'local) 'table-line)
713 (org-at-table-p))
714 (if (org-table-get-stored-formulas)
715 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
716 (org-table-align))))
717 ;; Store this place as the last one where we stored something
718 ;; Do the marking in the base buffer, so that it makes sense after
719 ;; the indirect buffer has been killed.
720 (when org-capture-bookmark
721 (org-capture-bookmark-last-stored-position))
723 ;; Run the hook
724 (run-hooks 'org-capture-before-finalize-hook))
726 (when (org-capture-get :decrypted)
727 (save-excursion
728 (goto-char (org-capture-get :decrypted))
729 (org-encrypt-entry)))
731 ;; Kill the indirect buffer
732 (save-buffer)
733 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
734 (new-buffer (org-capture-get :new-buffer 'local))
735 (kill-buffer (org-capture-get :kill-buffer 'local))
736 (base-buffer (buffer-base-buffer (current-buffer))))
738 ;; Kill the indirect buffer
739 (kill-buffer (current-buffer))
741 ;; Narrow back the target buffer to its previous state
742 (with-current-buffer (org-capture-get :buffer)
743 (let ((reg (org-capture-get :initial-target-region))
744 (pos (org-capture-get :initial-target-position))
745 (ipt (org-capture-get :insertion-point))
746 (size (org-capture-get :captured-entry-size)))
747 (if (not reg)
748 (widen)
749 (cond ((< ipt (car reg))
750 ;; insertion point is before the narrowed region
751 (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
752 ((> ipt (cdr reg))
753 ;; insertion point is after the narrowed region
754 (narrow-to-region (car reg) (cdr reg)))
756 ;; insertion point is within the narrowed region
757 (narrow-to-region (car reg) (+ size (cdr reg)))))
758 ;; now place back the point at its original position
759 (if (< ipt (car reg))
760 (goto-char (+ size pos))
761 (goto-char (if (< ipt pos) (+ size pos) pos))))))
763 ;; Kill the target buffer if that is desired
764 (when (and base-buffer new-buffer kill-buffer)
765 (with-current-buffer base-buffer (save-buffer))
766 (kill-buffer base-buffer))
768 ;; Restore the window configuration before capture
769 (set-window-configuration return-wconf))
771 (run-hooks 'org-capture-after-finalize-hook)
772 ;; Special cases
773 (cond
774 (abort-note
775 (cond
776 ((equal abort-note 'clean)
777 (message "Capture process aborted and target buffer cleaned up"))
778 ((equal abort-note 'dirty)
779 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
780 (stay-with-capture
781 (org-capture-goto-last-stored)))
782 ;; Return if we did store something
783 (not abort-note)))
785 (defun org-capture-refile ()
786 "Finalize the current capture and then refile the entry.
787 Refiling is done from the base buffer, because the indirect buffer is then
788 already gone. Any prefix argument will be passed to the refile command."
789 (interactive)
790 (unless (eq (org-capture-get :type 'local) 'entry)
791 (error
792 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
793 (let ((pos (point))
794 (base (buffer-base-buffer (current-buffer)))
795 (org-refile-for-capture t))
796 (save-window-excursion
797 (with-current-buffer (or base (current-buffer))
798 (save-excursion
799 (save-restriction
800 (widen)
801 (goto-char pos)
802 (call-interactively 'org-refile)))))
803 (org-capture-finalize)))
805 (defun org-capture-kill ()
806 "Abort the current capture process."
807 (interactive)
808 ;; FIXME: This does not do the right thing, we need to remove the
809 ;; new stuff by hand it is easy: undo, then kill the buffer
810 (let ((org-note-abort t)
811 (org-capture-before-finalize-hook nil))
812 (org-capture-finalize)))
814 (defun org-capture-goto-last-stored ()
815 "Go to the location where the last capture note was stored."
816 (interactive)
817 (org-goto-marker-or-bmk org-capture-last-stored-marker
818 (plist-get org-bookmark-names-plist
819 :last-capture))
820 (message "This is the last note stored by a capture process"))
822 ;;; Supporting functions for handling the process
824 (defun org-capture-put-target-region-and-position ()
825 "Store the initial region with `org-capture-put'."
826 (org-capture-put
827 :initial-target-region
828 ;; Check if the buffer is currently narrowed
829 (when (org-buffer-narrowed-p)
830 (cons (point-min) (point-max))))
831 ;; store the current point
832 (org-capture-put :initial-target-position (point)))
834 (defvar org-time-was-given) ; dynamically scoped parameter
835 (defun org-capture-set-target-location (&optional target)
836 "Find TARGET buffer and position.
837 Store them in the capture property list."
838 (let ((target-entry-p t) decrypted-hl-pos)
839 (setq target (or target (org-capture-get :target)))
840 (save-excursion
841 (cond
842 ((eq (car target) 'file)
843 (set-buffer (org-capture-target-buffer (nth 1 target)))
844 (org-capture-put-target-region-and-position)
845 (widen)
846 (setq target-entry-p nil))
848 ((eq (car target) 'id)
849 (let ((loc (org-id-find (nth 1 target))))
850 (if (not loc)
851 (error "Cannot find target ID \"%s\"" (nth 1 target))
852 (set-buffer (org-capture-target-buffer (car loc)))
853 (widen)
854 (org-capture-put-target-region-and-position)
855 (goto-char (cdr loc)))))
857 ((eq (car target) 'file+headline)
858 (set-buffer (org-capture-target-buffer (nth 1 target)))
859 (org-capture-put-target-region-and-position)
860 (widen)
861 (let ((hd (nth 2 target)))
862 (goto-char (point-min))
863 (unless (derived-mode-p 'org-mode)
864 (error
865 "Target buffer \"%s\" for file+headline should be in Org mode"
866 (current-buffer)))
867 (if (re-search-forward
868 (format org-complex-heading-regexp-format (regexp-quote hd))
869 nil t)
870 (goto-char (point-at-bol))
871 (goto-char (point-max))
872 (or (bolp) (insert "\n"))
873 (insert "* " hd "\n")
874 (beginning-of-line 0))))
876 ((eq (car target) 'file+olp)
877 (let ((m (org-find-olp
878 (cons (org-capture-expand-file (nth 1 target))
879 (cddr target)))))
880 (set-buffer (marker-buffer m))
881 (org-capture-put-target-region-and-position)
882 (widen)
883 (goto-char m)))
885 ((eq (car target) 'file+regexp)
886 (set-buffer (org-capture-target-buffer (nth 1 target)))
887 (org-capture-put-target-region-and-position)
888 (widen)
889 (goto-char (point-min))
890 (if (re-search-forward (nth 2 target) nil t)
891 (progn
892 (goto-char (if (org-capture-get :prepend)
893 (match-beginning 0) (match-end 0)))
894 (org-capture-put :exact-position (point))
895 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
896 (error "No match for target regexp in file %s" (nth 1 target))))
898 ((memq (car target) '(file+datetree file+datetree+prompt))
899 (require 'org-datetree)
900 (set-buffer (org-capture-target-buffer (nth 1 target)))
901 (org-capture-put-target-region-and-position)
902 (widen)
903 ;; Make a date tree entry, with the current date (or yesterday,
904 ;; if we are extending dates for a couple of hours)
905 (org-datetree-find-date-create
906 (calendar-gregorian-from-absolute
907 (cond
908 (org-overriding-default-time
909 ;; use the overriding default time
910 (time-to-days org-overriding-default-time))
912 ((eq (car target) 'file+datetree+prompt)
913 ;; prompt for date
914 (let ((prompt-time (org-read-date
915 nil t nil "Date for tree entry:"
916 (current-time))))
917 (org-capture-put
918 :default-time
919 (cond ((and (or (not (boundp 'org-time-was-given))
920 (not org-time-was-given))
921 (not (= (time-to-days prompt-time) (org-today))))
922 ;; Use 00:00 when no time is given for another date than today?
923 (apply 'encode-time (append '(0 0 0) (cdddr (decode-time prompt-time)))))
924 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer)
925 ;; Replace any time range by its start
926 (apply 'encode-time
927 (org-read-date-analyze
928 (replace-match "\\1 \\2" nil nil org-read-date-final-answer)
929 prompt-time (decode-time prompt-time))))
930 (t prompt-time)))
931 (time-to-days prompt-time)))
933 ;; current date, possibly corrected for late night workers
934 (org-today))))))
936 ((eq (car target) 'file+function)
937 (set-buffer (org-capture-target-buffer (nth 1 target)))
938 (org-capture-put-target-region-and-position)
939 (widen)
940 (funcall (nth 2 target))
941 (org-capture-put :exact-position (point))
942 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
944 ((eq (car target) 'function)
945 (funcall (nth 1 target))
946 (org-capture-put :exact-position (point))
947 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
949 ((eq (car target) 'clock)
950 (if (and (markerp org-clock-hd-marker)
951 (marker-buffer org-clock-hd-marker))
952 (progn (set-buffer (marker-buffer org-clock-hd-marker))
953 (org-capture-put-target-region-and-position)
954 (widen)
955 (goto-char org-clock-hd-marker))
956 (error "No running clock that could be used as capture target")))
958 (t (error "Invalid capture target specification")))
960 (when (and (featurep 'org-crypt) (org-at-encrypted-entry-p))
961 (org-decrypt-entry)
962 (setq decrypted-hl-pos
963 (save-excursion (and (org-back-to-heading t) (point)))))
965 (org-capture-put :buffer (current-buffer) :pos (point)
966 :target-entry-p target-entry-p
967 :decrypted decrypted-hl-pos))))
969 (defun org-capture-expand-file (file)
970 "Expand functions and symbols for FILE.
971 When FILE is a function, call it. When it is a form, evaluate
972 it. When it is a variable, retrieve the value. When it is
973 a string, return it. However, if it is the empty string, return
974 `org-default-notes-file' instead."
975 (cond
976 ((equal file "") org-default-notes-file)
977 ((org-string-nw-p file) file)
978 ((functionp file) (funcall file))
979 ((and (symbolp file) (boundp file)) (symbol-value file))
980 ((consp file) (eval file))
981 (t file)))
983 (defun org-capture-target-buffer (file)
984 "Get a buffer for FILE."
985 (setq file (org-capture-expand-file file))
986 (setq file (or (org-string-nw-p file)
987 org-default-notes-file
988 (error "No notes file specified, and no default available")))
989 (or (org-find-base-buffer-visiting file)
990 (progn (org-capture-put :new-buffer t)
991 (find-file-noselect (expand-file-name file org-directory)))))
993 (defun org-capture-steal-local-variables (buffer)
994 "Install Org-mode local variables of BUFFER."
995 (mapc (lambda (v)
996 (ignore-errors (org-set-local (car v) (cdr v))))
997 (buffer-local-variables buffer)))
999 (defun org-capture-place-template (&optional inhibit-wconf-store)
1000 "Insert the template at the target location, and display the buffer.
1001 When `inhibit-wconf-store', don't store the window configuration, as it
1002 may have been stored before."
1003 (unless inhibit-wconf-store
1004 (org-capture-put :return-to-wconf (current-window-configuration)))
1005 (delete-other-windows)
1006 (org-switch-to-buffer-other-window
1007 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
1008 (widen)
1009 (show-all)
1010 (goto-char (org-capture-get :pos))
1011 (org-set-local 'org-capture-target-marker
1012 (point-marker))
1013 (org-set-local 'outline-level 'org-outline-level)
1014 (let* ((template (org-capture-get :template))
1015 (type (org-capture-get :type)))
1016 (case type
1017 ((nil entry) (org-capture-place-entry))
1018 (table-line (org-capture-place-table-line))
1019 (plain (org-capture-place-plain-text))
1020 (item (org-capture-place-item))
1021 (checkitem (org-capture-place-item))))
1022 (org-capture-mode 1)
1023 (org-set-local 'org-capture-current-plist org-capture-plist))
1025 (defun org-capture-place-entry ()
1026 "Place the template as a new Org entry."
1027 (let* ((txt (org-capture-get :template))
1028 (reversed (org-capture-get :prepend))
1029 (target-entry-p (org-capture-get :target-entry-p))
1030 level beg end file)
1032 (and (org-capture-get :exact-position)
1033 (goto-char (org-capture-get :exact-position)))
1034 (cond
1035 ((not target-entry-p)
1036 ;; Insert as top-level entry, either at beginning or at end of file
1037 (setq level 1)
1038 (if reversed
1039 (progn (goto-char (point-min))
1040 (or (org-at-heading-p)
1041 (outline-next-heading)))
1042 (goto-char (point-max))
1043 (or (bolp) (insert "\n"))))
1045 ;; Insert as a child of the current entry
1046 (and (looking-at "\\*+")
1047 (setq level (- (match-end 0) (match-beginning 0))))
1048 (setq level (org-get-valid-level (or level 1) 1))
1049 (if reversed
1050 (progn
1051 (outline-next-heading)
1052 (or (bolp) (insert "\n")))
1053 (org-end-of-subtree t nil)
1054 (or (bolp) (insert "\n")))))
1055 (org-capture-empty-lines-before)
1056 (setq beg (point))
1057 (org-capture-verify-tree txt)
1058 (org-paste-subtree level txt 'for-yank)
1059 (org-capture-empty-lines-after 1)
1060 (org-capture-position-for-last-stored beg)
1061 (outline-next-heading)
1062 (setq end (point))
1063 (org-capture-mark-kill-region beg (1- end))
1064 (org-capture-narrow beg (1- end))
1065 (if (or (re-search-backward "%\\?" beg t)
1066 (re-search-forward "%\\?" end t))
1067 (replace-match ""))))
1069 (defun org-capture-place-item ()
1070 "Place the template as a new plain list item."
1071 (let* ((txt (org-capture-get :template))
1072 (target-entry-p (org-capture-get :target-entry-p))
1073 (ind 0)
1074 beg end)
1075 (if (org-capture-get :exact-position)
1076 (goto-char (org-capture-get :exact-position))
1077 (cond
1078 ((not target-entry-p)
1079 ;; Insert as top-level entry, either at beginning or at end of file
1080 (setq beg (point-min) end (point-max)))
1082 (setq beg (1+ (point-at-eol))
1083 end (save-excursion (outline-next-heading) (point)))))
1084 (setq ind nil)
1085 (if (org-capture-get :prepend)
1086 (progn
1087 (goto-char beg)
1088 (when (org-list-search-forward (org-item-beginning-re) end t)
1089 (goto-char (match-beginning 0))
1090 (setq ind (org-get-indentation))))
1091 (goto-char end)
1092 (when (org-list-search-backward (org-item-beginning-re) beg t)
1093 (setq ind (org-get-indentation))
1094 (org-end-of-item)))
1095 (unless ind (goto-char end)))
1096 ;; Remove common indentation
1097 (setq txt (org-remove-indentation txt))
1098 ;; Make sure this is indeed an item
1099 (unless (string-match (concat "\\`" (org-item-re)) txt)
1100 (setq txt (concat "- "
1101 (mapconcat 'identity (split-string txt "\n")
1102 "\n "))))
1103 ;; Prepare surrounding empty lines.
1104 (org-capture-empty-lines-before)
1105 (setq beg (point))
1106 (unless (eolp) (save-excursion (insert "\n")))
1107 (unless ind
1108 (org-indent-line)
1109 (setq ind (org-get-indentation))
1110 (delete-region beg (point)))
1111 ;; Set the correct indentation, depending on context
1112 (setq ind (make-string ind ?\ ))
1113 (setq txt (concat ind
1114 (mapconcat 'identity (split-string txt "\n")
1115 (concat "\n" ind))
1116 "\n"))
1117 ;; Insert item.
1118 (insert txt)
1119 (org-capture-empty-lines-after 1)
1120 (org-capture-position-for-last-stored beg)
1121 (forward-char 1)
1122 (setq end (point))
1123 (org-capture-mark-kill-region beg (1- end))
1124 (org-capture-narrow beg (1- end))
1125 (if (or (re-search-backward "%\\?" beg t)
1126 (re-search-forward "%\\?" end t))
1127 (replace-match ""))))
1129 (defun org-capture-place-table-line ()
1130 "Place the template as a table line."
1131 (require 'org-table)
1132 (let* ((txt (org-capture-get :template))
1133 (target-entry-p (org-capture-get :target-entry-p))
1134 (table-line-pos (org-capture-get :table-line-pos))
1135 ind beg end)
1136 (cond
1137 ((org-capture-get :exact-position)
1138 (goto-char (org-capture-get :exact-position)))
1139 ((not target-entry-p)
1140 ;; Table is not necessarily under a heading
1141 (setq beg (point-min) end (point-max)))
1143 ;; WE are at a heading, limit search to the body
1144 (setq beg (1+ (point-at-eol))
1145 end (save-excursion (outline-next-heading) (point)))))
1146 (if (re-search-forward org-table-dataline-regexp end t)
1147 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t))
1148 (goto-char e)
1149 (if (looking-at "[ \t]*#\\+tblfm:")
1150 (forward-line 1))
1151 (narrow-to-region b (point)))
1152 (goto-char end)
1153 (insert "\n| |\n|----|\n| |\n")
1154 (narrow-to-region (1+ end) (point)))
1155 ;; We are narrowed to the table, or to an empty line if there was no table
1157 ;; Check if the template is good
1158 (if (not (string-match org-table-dataline-regexp txt))
1159 (setq txt "| %?Bad template |\n"))
1160 (if (functionp table-line-pos)
1161 (setq table-line-pos (funcall table-line-pos))
1162 (setq table-line-pos (eval table-line-pos)))
1163 (cond
1164 ((and table-line-pos
1165 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
1166 ;; we have a complex line specification
1167 (let ((ll (ignore-errors
1168 (save-match-data (org-table-analyze))
1169 (aref org-table-hlines
1170 (- (match-end 1) (match-beginning 1)))))
1171 (delta (string-to-number (match-string 2 table-line-pos))))
1172 ;; The user wants a special position in the table
1173 (unless ll
1174 (error "Invalid table line specification \"%s\"" table-line-pos))
1175 (goto-char org-table-current-begin-pos)
1176 (forward-line (+ ll delta (if (< delta 0) 0 -1)))
1177 (org-table-insert-row 'below)
1178 (beginning-of-line 1)
1179 (delete-region (point) (1+ (point-at-eol)))
1180 (setq beg (point))
1181 (insert txt)
1182 (setq end (point))))
1183 ((org-capture-get :prepend)
1184 (goto-char (point-min))
1185 (re-search-forward org-table-hline-regexp nil t)
1186 (beginning-of-line 1)
1187 (re-search-forward org-table-dataline-regexp nil t)
1188 (beginning-of-line 1)
1189 (setq beg (point))
1190 (org-table-insert-row)
1191 (beginning-of-line 1)
1192 (delete-region (point) (1+ (point-at-eol)))
1193 (insert txt)
1194 (setq end (point)))
1196 (goto-char (point-max))
1197 (re-search-backward org-table-dataline-regexp nil t)
1198 (beginning-of-line 1)
1199 (org-table-insert-row 'below)
1200 (beginning-of-line 1)
1201 (delete-region (point) (1+ (point-at-eol)))
1202 (setq beg (point))
1203 (insert txt)
1204 (setq end (point))))
1205 (goto-char beg)
1206 (org-capture-position-for-last-stored 'table-line)
1207 (if (or (re-search-backward "%\\?" beg t)
1208 (re-search-forward "%\\?" end t))
1209 (replace-match ""))
1210 (org-table-align)))
1212 (defun org-capture-place-plain-text ()
1213 "Place the template plainly.
1214 If the target locator points at an Org node, place the template into
1215 the text of the entry, before the first child. If not, place the
1216 template at the beginning or end of the file.
1217 Of course, if exact position has been required, just put it there."
1218 (let* ((txt (org-capture-get :template))
1219 beg end)
1220 (cond
1221 ((org-capture-get :exact-position)
1222 (goto-char (org-capture-get :exact-position)))
1223 ((and (org-capture-get :target-entry-p)
1224 (bolp)
1225 (looking-at org-outline-regexp))
1226 ;; we should place the text into this entry
1227 (if (org-capture-get :prepend)
1228 ;; Skip meta data and drawers
1229 (org-end-of-meta-data t)
1230 ;; go to ent of the entry text, before the next headline
1231 (outline-next-heading)))
1233 ;; beginning or end of file
1234 (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
1235 (or (bolp) (newline))
1236 (org-capture-empty-lines-before)
1237 (setq beg (point))
1238 (insert txt)
1239 (org-capture-empty-lines-after 1)
1240 (org-capture-position-for-last-stored beg)
1241 (setq end (point))
1242 (org-capture-mark-kill-region beg (1- end))
1243 (org-capture-narrow beg (1- end))
1244 (if (or (re-search-backward "%\\?" beg t)
1245 (re-search-forward "%\\?" end t))
1246 (replace-match ""))))
1248 (defun org-capture-mark-kill-region (beg end)
1249 "Mark the region that will have to be killed when aborting capture."
1250 (let ((m1 (move-marker (make-marker) beg))
1251 (m2 (move-marker (make-marker) end)))
1252 (org-capture-put :begin-marker m1)
1253 (org-capture-put :end-marker m2)))
1255 (defun org-capture-position-for-last-stored (where)
1256 "Memorize the position that should later become the position of last capture."
1257 (cond
1258 ((integerp where)
1259 (org-capture-put :position-for-last-stored
1260 (move-marker (make-marker) where
1261 (or (buffer-base-buffer (current-buffer))
1262 (current-buffer)))))
1263 ((eq where 'table-line)
1264 (org-capture-put :position-for-last-stored
1265 (list 'table-line
1266 (org-table-current-dline))))
1267 (t (error "This should not happen"))))
1269 (defun org-capture-bookmark-last-stored-position ()
1270 "Bookmark the last-captured position."
1271 (let* ((where (org-capture-get :position-for-last-stored 'local))
1272 (pos (cond
1273 ((markerp where)
1274 (prog1 (marker-position where)
1275 (move-marker where nil)))
1276 ((and (listp where) (eq (car where) 'table-line))
1277 (if (org-at-table-p)
1278 (save-excursion
1279 (org-table-goto-line (nth 1 where))
1280 (point-at-bol))
1281 (point))))))
1282 (with-current-buffer (buffer-base-buffer (current-buffer))
1283 (save-excursion
1284 (save-restriction
1285 (widen)
1286 (goto-char pos)
1287 (let ((bookmark-name (plist-get org-bookmark-names-plist
1288 :last-capture)))
1289 (when bookmark-name
1290 (with-demoted-errors
1291 (bookmark-set bookmark-name))))
1292 (move-marker org-capture-last-stored-marker (point)))))))
1294 (defun org-capture-narrow (beg end)
1295 "Narrow, unless configuration says not to narrow."
1296 (unless (org-capture-get :unnarrowed)
1297 (narrow-to-region beg end)
1298 (goto-char beg)))
1300 (defun org-capture-empty-lines-before (&optional n)
1301 "Set the correct number of empty lines before the insertion point.
1302 Point will be after the empty lines, so insertion can directly be done."
1303 (setq n (or n (org-capture-get :empty-lines-before)
1304 (org-capture-get :empty-lines) 0))
1305 (let ((pos (point)))
1306 (org-back-over-empty-lines)
1307 (delete-region (point) pos)
1308 (if (> n 0) (newline n))))
1310 (defun org-capture-empty-lines-after (&optional n)
1311 "Set the correct number of empty lines after the inserted string.
1312 Point will remain at the first line after the inserted text."
1313 (setq n (or n (org-capture-get :empty-lines-after)
1314 (org-capture-get :empty-lines) 0))
1315 (org-back-over-empty-lines)
1316 (while (looking-at "[ \t]*\n") (replace-match ""))
1317 (let ((pos (point)))
1318 (if (> n 0) (newline n))
1319 (goto-char pos)))
1321 (defvar org-clock-marker) ; Defined in org.el
1323 (defun org-capture-insert-template-here ()
1324 "Insert the capture template at point."
1325 (let* ((template (org-capture-get :template))
1326 (type (org-capture-get :type))
1327 beg end pp)
1328 (or (bolp) (newline))
1329 (setq beg (point))
1330 (cond
1331 ((and (eq type 'entry) (derived-mode-p 'org-mode))
1332 (org-capture-verify-tree (org-capture-get :template))
1333 (org-paste-subtree nil template t))
1334 ((and (memq type '(item checkitem))
1335 (derived-mode-p 'org-mode)
1336 (save-excursion (skip-chars-backward " \t\n")
1337 (setq pp (point))
1338 (org-in-item-p)))
1339 (goto-char pp)
1340 (org-insert-item)
1341 (skip-chars-backward " ")
1342 (skip-chars-backward "-+*0123456789).")
1343 (delete-region (point) (point-at-eol))
1344 (setq beg (point))
1345 (org-remove-indentation template)
1346 (insert template)
1347 (org-capture-empty-lines-after)
1348 (goto-char beg)
1349 (org-list-repair)
1350 (org-end-of-item)
1351 (setq end (point)))
1352 (t (insert template)))
1353 (setq end (point))
1354 (goto-char beg)
1355 (if (re-search-forward "%\\?" end t)
1356 (replace-match ""))))
1358 (defun org-capture-set-plist (entry)
1359 "Initialize the property list from the template definition."
1360 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1361 (org-capture-put :key (car entry) :description (nth 1 entry)
1362 :target (nth 3 entry))
1363 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1364 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1365 ;; The template may be empty or omitted for special types.
1366 ;; Here we insert the default templates for such cases.
1367 (cond
1368 ((eq type 'item) (setq txt "- %?"))
1369 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1370 ((eq type 'table-line) (setq txt "| %? |"))
1371 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1372 (org-capture-put :template txt :type type)))
1374 (defun org-capture-goto-target (&optional template-key)
1375 "Go to the target location of a capture template.
1376 The user is queried for the template."
1377 (interactive)
1378 (let* (org-select-template-temp-major-mode
1379 (entry (org-capture-select-template template-key)))
1380 (unless entry
1381 (error "No capture template selected"))
1382 (org-capture-set-plist entry)
1383 (org-capture-set-target-location)
1384 (org-pop-to-buffer-same-window (org-capture-get :buffer))
1385 (goto-char (org-capture-get :pos))))
1387 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1388 "Make an indirect buffer for a capture process.
1389 Use PREFIX as a prefix for the name of the indirect buffer."
1390 (setq buffer (or buffer (current-buffer)))
1391 (let ((n 1) (base (buffer-name buffer)) bname)
1392 (setq bname (concat prefix "-" base))
1393 (while (buffer-live-p (get-buffer bname))
1394 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1395 (condition-case nil
1396 (make-indirect-buffer buffer bname 'clone)
1397 (error
1398 (let ((buf (make-indirect-buffer buffer bname)))
1399 (with-current-buffer buf (org-mode))
1400 buf)))))
1402 (defun org-capture-verify-tree (tree)
1403 "Throw error if TREE is not a valid tree."
1404 (unless (org-kill-is-subtree-p tree)
1405 (error "Template is not a valid Org entry or tree")))
1407 (defun org-mks (table title &optional prompt specials)
1408 "Select a member of an alist with multiple keys.
1409 TABLE is the alist which should contain entries where the car is a string.
1410 There should be two types of entries.
1412 1. prefix descriptions like (\"a\" \"Description\")
1413 This indicates that `a' is a prefix key for multi-letter selection, and
1414 that there are entries following with keys like \"ab\", \"ax\"...
1416 2. Selectable members must have more than two elements, with the first
1417 being the string of keys that lead to selecting it, and the second a
1418 short description string of the item.
1420 The command will then make a temporary buffer listing all entries
1421 that can be selected with a single key, and all the single key
1422 prefixes. When you press the key for a single-letter entry, it is selected.
1423 When you press a prefix key, the commands (and maybe further prefixes)
1424 under this key will be shown and offered for selection.
1426 TITLE will be placed over the selection in the temporary buffer,
1427 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1428 also (\"key\" \"description\") entries. When one of these is selection,
1429 only the bare key is returned."
1430 (setq prompt (or prompt "Select: "))
1431 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1432 current prefix rtn re pressed buffer (inhibit-quit t))
1433 (save-window-excursion
1434 (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
1435 (setq orig-table table)
1436 (catch 'exit
1437 (while t
1438 (erase-buffer)
1439 (insert title "\n\n")
1440 (setq tbl table
1441 des-keys nil
1442 allowed-keys nil
1443 cursor-type nil)
1444 (setq prefix (if current (concat current " ") ""))
1445 (while tbl
1446 (cond
1447 ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
1448 ;; This is a description on this level
1449 (setq dkey (caar tbl) ddesc (cadar tbl))
1450 (pop tbl)
1451 (push dkey des-keys)
1452 (push dkey allowed-keys)
1453 (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
1454 ;; Skip keys which are below this prefix
1455 (setq re (concat "\\`" (regexp-quote dkey)))
1456 (let (case-fold-search)
1457 (while (and tbl (string-match re (caar tbl))) (pop tbl))))
1458 ((= 2 (length (car tbl)))
1459 ;; Not yet a usable description, skip it
1462 ;; usable entry on this level
1463 (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
1464 (push (caar tbl) allowed-keys)
1465 (pop tbl))))
1466 (when specials
1467 (insert "-------------------------------------------------------------------------------\n")
1468 (let ((sp specials))
1469 (while sp
1470 (insert (format "[%s] %s\n"
1471 (caar sp) (nth 1 (car sp))))
1472 (push (caar sp) allowed-keys)
1473 (pop sp))))
1474 (push "\C-g" allowed-keys)
1475 (goto-char (point-min))
1476 (if (not (pos-visible-in-window-p (point-max)))
1477 (org-fit-window-to-buffer))
1478 (message prompt)
1479 (setq pressed (char-to-string (read-char-exclusive)))
1480 (while (not (member pressed allowed-keys))
1481 (message "Invalid key `%s'" pressed) (sit-for 1)
1482 (message prompt)
1483 (setq pressed (char-to-string (read-char-exclusive))))
1484 (when (equal pressed "\C-g")
1485 (kill-buffer buffer)
1486 (error "Abort"))
1487 (when (and (not (assoc pressed table))
1488 (not (member pressed des-keys))
1489 (assoc pressed specials))
1490 (throw 'exit (setq rtn pressed)))
1491 (unless (member pressed des-keys)
1492 (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
1493 orig-table))))
1494 (setq current (concat current pressed))
1495 (setq table (mapcar
1496 (lambda (x)
1497 (if (and (> (length (car x)) 1)
1498 (equal (substring (car x) 0 1) pressed))
1499 (cons (substring (car x) 1) (cdr x))
1500 nil))
1501 table))
1502 (setq table (remove nil table)))))
1503 (when buffer (kill-buffer buffer))
1504 rtn))
1506 ;;; The template code
1507 (defun org-capture-select-template (&optional keys)
1508 "Select a capture template.
1509 Lisp programs can force the template by setting KEYS to a string."
1510 (let ((org-capture-templates
1511 (or (org-contextualize-keys
1512 org-capture-templates org-capture-templates-contexts)
1513 '(("t" "Task" entry (file+headline "" "Tasks")
1514 "* TODO %?\n %u\n %a")))))
1515 (if keys
1516 (or (assoc keys org-capture-templates)
1517 (error "No capture template referred to by \"%s\" keys" keys))
1518 (org-mks org-capture-templates
1519 "Select a capture template\n========================="
1520 "Template key: "
1521 '(("C" "Customize org-capture-templates")
1522 ("q" "Abort"))))))
1524 (defun org-capture-fill-template (&optional template initial annotation)
1525 "Fill a template and return the filled template as a string.
1526 The template may still contain \"%?\" for cursor positioning."
1527 (setq template (or template (org-capture-get :template)))
1528 (when (stringp initial)
1529 (setq initial (org-no-properties initial)))
1530 (let* ((buffer (org-capture-get :buffer))
1531 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1532 (ct (org-capture-get :default-time))
1533 (dct (decode-time ct))
1534 (ct1
1535 (if (< (nth 2 dct) org-extend-today-until)
1536 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
1537 ct))
1538 (plist-p (if org-store-link-plist t nil))
1539 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
1540 (v-x (or (org-get-x-clipboard 'PRIMARY)
1541 (org-get-x-clipboard 'CLIPBOARD)
1542 (org-get-x-clipboard 'SECONDARY)))
1543 (v-t (format-time-string (car org-time-stamp-formats) ct1))
1544 (v-T (format-time-string (cdr org-time-stamp-formats) ct1))
1545 (v-u (concat "[" (substring v-t 1 -1) "]"))
1546 (v-U (concat "[" (substring v-T 1 -1) "]"))
1547 ;; `initial' and `annotation' might habe been passed.
1548 ;; But if the property list has them, we prefer those values
1549 (v-i (or (plist-get org-store-link-plist :initial)
1550 initial
1551 (org-capture-get :initial)
1552 ""))
1553 (v-a (or (plist-get org-store-link-plist :annotation)
1554 annotation
1555 (org-capture-get :annotation)
1556 ""))
1557 ;; Is the link empty? Then we do not want it...
1558 (v-a (if (equal v-a "[[]]") "" v-a))
1559 (clipboards (remove nil (list v-i
1560 (org-get-x-clipboard 'PRIMARY)
1561 (org-get-x-clipboard 'CLIPBOARD)
1562 (org-get-x-clipboard 'SECONDARY)
1563 v-c)))
1564 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1565 (v-A (if (and v-a (string-match l-re v-a))
1566 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
1567 v-a))
1568 (v-l (if (and v-a (string-match l-re v-a))
1569 (replace-match "\\1" nil nil v-a)
1570 v-a))
1571 (v-n user-full-name)
1572 (v-k (if (marker-buffer org-clock-marker)
1573 (org-no-properties org-clock-heading)))
1574 (v-K (if (marker-buffer org-clock-marker)
1575 (org-make-link-string
1576 (buffer-file-name (marker-buffer org-clock-marker))
1577 org-clock-heading)))
1578 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1579 (v-F (or (org-capture-get :original-file) ""))
1581 (org-startup-folded nil)
1582 (org-inhibit-startup t)
1583 org-time-was-given org-end-time-was-given x
1584 prompt completions char time pos default histvar strings)
1586 (setq org-store-link-plist
1587 (plist-put org-store-link-plist :annotation v-a)
1588 org-store-link-plist
1589 (plist-put org-store-link-plist :initial v-i))
1590 (setq initial v-i)
1592 (unless template (setq template "") (message "No template") (ding)
1593 (sit-for 1))
1594 (save-window-excursion
1595 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1596 (erase-buffer)
1597 (insert template)
1598 (goto-char (point-min))
1599 (org-capture-steal-local-variables buffer)
1600 (setq buffer-file-name nil mark-active nil)
1602 ;; %[] Insert contents of a file.
1603 (goto-char (point-min))
1604 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1605 (unless (org-capture-escaped-%)
1606 (let ((start (match-beginning 0))
1607 (end (match-end 0))
1608 (filename (expand-file-name (match-string 1))))
1609 (goto-char start)
1610 (delete-region start end)
1611 (condition-case error
1612 (insert-file-contents filename)
1613 (error (insert (format "%%![Couldn't insert %s: %s]"
1614 filename error)))))))
1616 ;; The current time
1617 (goto-char (point-min))
1618 (while (re-search-forward "%<\\([^>\n]+\\)>" nil t)
1619 (replace-match (format-time-string (match-string 1)) t t))
1621 ;; Simple %-escapes
1622 (goto-char (point-min))
1623 (while (re-search-forward "%\\([tTuUaliAcxkKInfF]\\)" nil t)
1624 (unless (org-capture-escaped-%)
1625 (when (and initial (equal (match-string 0) "%i"))
1626 (save-match-data
1627 (let* ((lead (buffer-substring
1628 (point-at-bol) (match-beginning 0))))
1629 (setq v-i (mapconcat 'identity
1630 (org-split-string initial "\n")
1631 (concat "\n" lead))))))
1632 (replace-match (or (eval (intern (concat "v-" (match-string 1)))) "")
1633 t t)))
1635 ;; From the property list
1636 (when plist-p
1637 (goto-char (point-min))
1638 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
1639 (unless (org-capture-escaped-%)
1640 (and (setq x (or (plist-get org-store-link-plist
1641 (intern (match-string 1))) ""))
1642 (replace-match x t t)))))
1644 ;; %() embedded elisp
1645 (goto-char (point-min))
1646 (org-capture-expand-embedded-elisp)
1648 ;; Turn on org-mode in temp buffer, set local variables
1649 ;; This is to support completion in interactive prompts
1650 (let ((org-inhibit-startup t)) (org-mode))
1651 ;; Interactive template entries
1652 (goto-char (point-min))
1653 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
1654 (unless (org-capture-escaped-%)
1655 (setq char (if (match-end 3) (match-string-no-properties 3))
1656 prompt (if (match-end 2) (match-string-no-properties 2)))
1657 (goto-char (match-beginning 0))
1658 (replace-match "")
1659 (setq completions nil default nil)
1660 (when prompt
1661 (setq completions (org-split-string prompt "|")
1662 prompt (pop completions)
1663 default (car completions)
1664 histvar (intern (concat
1665 "org-capture-template-prompt-history::"
1666 (or prompt "")))
1667 completions (mapcar 'list completions)))
1668 (unless (boundp histvar) (set histvar nil))
1669 (cond
1670 ((member char '("G" "g"))
1671 (let* ((org-last-tags-completion-table
1672 (org-global-tags-completion-table
1673 (if (equal char "G")
1674 (org-agenda-files)
1675 (and file (list file)))))
1676 (org-add-colon-after-tag-completion t)
1677 (ins (org-icompleting-read
1678 (if prompt (concat prompt ": ") "Tags: ")
1679 'org-tags-completion-function nil nil nil
1680 'org-tags-history)))
1681 (setq ins (mapconcat 'identity
1682 (org-split-string
1683 ins (org-re "[^[:alnum:]_@#%]+"))
1684 ":"))
1685 (when (string-match "\\S-" ins)
1686 (or (equal (char-before) ?:) (insert ":"))
1687 (insert ins)
1688 (or (equal (char-after) ?:) (insert ":"))
1689 (and (org-at-heading-p)
1690 (let ((org-ignore-region t))
1691 (org-set-tags nil 'align))))))
1692 ((equal char "C")
1693 (cond ((= (length clipboards) 1) (insert (car clipboards)))
1694 ((> (length clipboards) 1)
1695 (insert (read-string "Clipboard/kill value: "
1696 (car clipboards) '(clipboards . 1)
1697 (car clipboards))))))
1698 ((equal char "L")
1699 (cond ((= (length clipboards) 1)
1700 (org-insert-link 0 (car clipboards)))
1701 ((> (length clipboards) 1)
1702 (org-insert-link 0 (read-string "Clipboard/kill value: "
1703 (car clipboards)
1704 '(clipboards . 1)
1705 (car clipboards))))))
1706 ((equal char "p")
1707 (org-set-property (org-no-properties prompt) nil))
1708 (char
1709 ;; These are the date/time related ones
1710 (setq org-time-was-given (equal (upcase char) char))
1711 (setq time (org-read-date (equal (upcase char) char) t nil
1712 prompt))
1713 (if (equal (upcase char) char) (setq org-time-was-given t))
1714 (org-insert-time-stamp time org-time-was-given
1715 (member char '("u" "U"))
1716 nil nil (list org-end-time-was-given)))
1718 (let (org-completion-use-ido)
1719 (push (org-completing-read-no-i
1720 (concat (if prompt prompt "Enter string")
1721 (if default (concat " [" default "]"))
1722 ": ")
1723 completions nil nil nil histvar default)
1724 strings)
1725 (insert (car strings)))))))
1726 ;; Replace %n escapes with nth %^{...} string
1727 (setq strings (nreverse strings))
1728 (goto-char (point-min))
1729 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
1730 (unless (org-capture-escaped-%)
1731 (replace-match
1732 (nth (1- (string-to-number (match-string 1))) strings)
1733 nil t)))
1734 ;; Make sure there are no empty lines before the text, and that
1735 ;; it ends with a newline character
1736 (goto-char (point-min))
1737 (while (looking-at "[ \t]*\n") (replace-match ""))
1738 (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
1739 ;; Return the expanded template and kill the temporary buffer
1740 (untabify (point-min) (point-max))
1741 (set-buffer-modified-p nil)
1742 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1744 (defun org-capture-escaped-% ()
1745 "Check if % was escaped - if yes, unescape it now."
1746 (if (equal (char-before (match-beginning 0)) ?\\)
1747 (progn
1748 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1750 nil))
1752 (defun org-capture-expand-embedded-elisp ()
1753 "Evaluate embedded elisp %(sexp) and replace with the result."
1754 (goto-char (point-min))
1755 (while (re-search-forward "%(" nil t)
1756 (unless (org-capture-escaped-%)
1757 (goto-char (match-beginning 0))
1758 (let ((template-start (point)))
1759 (forward-char 1)
1760 (let* ((sexp (read (current-buffer)))
1761 (result (org-eval
1762 (org-capture--expand-keyword-in-embedded-elisp sexp))))
1763 (delete-region template-start (point))
1764 (when result
1765 (if (stringp result)
1766 (insert result)
1767 (error "Capture template sexp `%s' must evaluate to string or nil"
1768 sexp))))))))
1770 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1771 "Recursively replace capture link keywords in ATTR sexp.
1772 Such keywords are prefixed with \"%:\". See
1773 `org-capture-template' for more information."
1774 (cond ((consp attr)
1775 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr))
1776 ((symbolp attr)
1777 (let* ((attr-symbol (symbol-name attr))
1778 (key (and (string-match "%\\(:.*\\)" attr-symbol)
1779 (intern (match-string 1 attr-symbol)))))
1780 (or (plist-get org-store-link-plist key)
1781 attr)))
1782 (t attr)))
1784 (defun org-capture-inside-embedded-elisp-p ()
1785 "Return non-nil if point is inside of embedded elisp %(sexp)."
1786 (let (beg end)
1787 (with-syntax-table emacs-lisp-mode-syntax-table
1788 (save-excursion
1789 ;; `looking-at' and `search-backward' below do not match the "%(" if
1790 ;; point is in its middle
1791 (when (equal (char-before) ?%)
1792 (backward-char))
1793 (save-match-data
1794 (when (or (looking-at "%(") (search-backward "%(" nil t))
1795 (setq beg (point))
1796 (setq end (progn (forward-char) (forward-sexp) (1- (point)))))))
1797 (when (and beg end)
1798 (and (<= (point) end) (>= (point) beg))))))
1800 ;;;###autoload
1801 (defun org-capture-import-remember-templates ()
1802 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1803 (interactive)
1804 (when (and (yes-or-no-p
1805 "Import old remember templates into org-capture-templates? ")
1806 (yes-or-no-p
1807 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1808 (require 'org-remember)
1809 (setq org-capture-templates
1810 (mapcar
1811 (lambda (entry)
1812 (let ((desc (car entry))
1813 (key (char-to-string (nth 1 entry)))
1814 (template (nth 2 entry))
1815 (file (or (nth 3 entry) org-default-notes-file))
1816 (position (or (nth 4 entry) org-remember-default-headline))
1817 (type 'entry)
1818 (prepend org-reverse-note-order)
1819 immediate target jump-to-captured)
1820 (cond
1821 ((member position '(top bottom))
1822 (setq target (list 'file file)
1823 prepend (eq position 'top)))
1824 ((eq position 'date-tree)
1825 (setq target (list 'file+datetree file)
1826 prepend nil))
1827 (t (setq target (list 'file+headline file position))))
1829 (when (string-match "%!" template)
1830 (setq template (replace-match "" t t template)
1831 immediate t))
1833 (when (string-match "%&" template)
1834 (setq jump-to-captured t))
1836 (append (list key desc type target template)
1837 (if prepend '(:prepend t))
1838 (if immediate '(:immediate-finish t))
1839 (if jump-to-captured '(:jump-to-captured t)))))
1841 org-remember-templates))))
1843 (provide 'org-capture)
1845 ;;; org-capture.el ends here