Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-capture.el
blobbfdb4751a7ab4c8d6a59d481ea1a3005860e051a
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 "Capture buffer. Finish `C-c C-c', refile `C-c C-w', abort `C-c C-k'."))
439 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
440 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
441 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
443 ;;; The main commands
445 (defvar org-capture-initial nil)
446 (defvar org-capture-entry nil)
448 ;;;###autoload
449 (defun org-capture-string (string &optional keys)
450 "Capture STRING with the template selected by KEYS."
451 (interactive "sInitial text: \n")
452 (let ((org-capture-initial string)
453 (org-capture-entry (org-capture-select-template keys)))
454 (org-capture)))
456 (defcustom org-capture-templates-contexts nil
457 "Alist of capture templates and valid contexts.
459 For example, if you have a capture template \"c\" and you want
460 this template to be accessible only from `message-mode' buffers,
461 use this:
463 '((\"c\" ((in-mode . \"message-mode\"))))
465 Here are the available contexts definitions:
467 in-file: command displayed only in matching files
468 in-mode: command displayed only in matching modes
469 not-in-file: command not displayed in matching files
470 not-in-mode: command not displayed in matching modes
471 in-buffer: command displayed only in matching buffers
472 not-in-buffer: command not displayed in matching buffers
473 [function]: a custom function taking no argument
475 If you define several checks, the agenda command will be
476 accessible if there is at least one valid check.
478 You can also bind a key to another agenda custom command
479 depending on contextual rules.
481 '((\"c\" \"d\" ((in-mode . \"message-mode\"))))
483 Here it means: in `message-mode buffers', use \"c\" as the
484 key for the capture template otherwise associated with \"d\".
485 \(The template originally associated with \"d\" is not displayed
486 to avoid duplicates.)"
487 :version "24.3"
488 :group 'org-capture
489 :type '(repeat (list :tag "Rule"
490 (string :tag " Capture key")
491 (string :tag "Replace by template")
492 (repeat :tag "Available when"
493 (choice
494 (cons :tag "Condition"
495 (choice
496 (const :tag "In file" in-file)
497 (const :tag "Not in file" not-in-file)
498 (const :tag "In buffer" in-buffer)
499 (const :tag "Not in buffer" not-in-buffer)
500 (const :tag "In mode" in-mode)
501 (const :tag "Not in mode" not-in-mode))
502 (regexp))
503 (function :tag "Custom function"))))))
505 (defcustom org-capture-use-agenda-date nil
506 "Non-nil means use the date at point when capturing from agendas.
507 When nil, you can still capture using the date at point with \\[org-agenda-capture]."
508 :group 'org-capture
509 :version "24.3"
510 :type 'boolean)
512 ;;;###autoload
513 (defun org-capture (&optional goto keys)
514 "Capture something.
515 \\<org-capture-mode-map>
516 This will let you select a template from `org-capture-templates', and then
517 file the newly captured information. The text is immediately inserted
518 at the target location, and an indirect buffer is shown where you can
519 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
520 of Emacs, so that you can continue your work.
522 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
523 anything, just go to the file/headline where the selected template
524 stores its notes. With a double prefix argument \
525 \\[universal-argument] \\[universal-argument], go to the last note
526 stored.
528 When called with a `C-0' (zero) prefix, insert a template at point.
530 ELisp programs can set KEYS to a string associated with a template
531 in `org-capture-templates'. In this case, interactive selection
532 will be bypassed.
534 If `org-capture-use-agenda-date' is non-nil, capturing from the
535 agenda will use the date at point as the default date. Then, a
536 `C-1' prefix will tell the capture process to use the HH:MM time
537 of the day at point (if any) or the current HH:MM time."
538 (interactive "P")
539 (when (and org-capture-use-agenda-date
540 (eq major-mode 'org-agenda-mode))
541 (setq org-overriding-default-time
542 (org-get-cursor-date (equal goto 1))))
543 (cond
544 ((equal goto '(4)) (org-capture-goto-target))
545 ((equal goto '(16)) (org-capture-goto-last-stored))
547 ;; FIXME: Are these needed?
548 (let* ((orig-buf (current-buffer))
549 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
550 org-capture-link-is-already-stored)
551 (plist-get org-store-link-plist :annotation)
552 (ignore-errors (org-store-link nil))))
553 (entry (or org-capture-entry (org-capture-select-template keys)))
554 initial)
555 (setq initial (or org-capture-initial
556 (and (org-region-active-p)
557 (buffer-substring (point) (mark)))))
558 (when (stringp initial)
559 (remove-text-properties 0 (length initial) '(read-only t) initial))
560 (when (stringp annotation)
561 (remove-text-properties 0 (length annotation)
562 '(read-only t) annotation))
563 (cond
564 ((equal entry "C")
565 (customize-variable 'org-capture-templates))
566 ((equal entry "q")
567 (error "Abort"))
569 (org-capture-set-plist entry)
570 (org-capture-get-template)
571 (org-capture-put :original-buffer orig-buf
572 :original-file (or (buffer-file-name orig-buf)
573 (and (featurep 'dired)
574 (car (rassq orig-buf
575 dired-buffers))))
576 :original-file-nondirectory
577 (and (buffer-file-name orig-buf)
578 (file-name-nondirectory
579 (buffer-file-name orig-buf)))
580 :annotation annotation
581 :initial initial
582 :return-to-wconf (current-window-configuration)
583 :default-time
584 (or org-overriding-default-time
585 (org-current-time)))
586 (org-capture-set-target-location)
587 (condition-case error
588 (org-capture-put :template (org-capture-fill-template))
589 ((error quit)
590 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
591 (error "Capture abort: %s" error)))
593 (setq org-capture-clock-keep (org-capture-get :clock-keep))
594 (if (equal goto 0)
595 ;;insert at point
596 (org-capture-insert-template-here)
597 (condition-case error
598 (org-capture-place-template
599 (equal (car (org-capture-get :target)) 'function))
600 ((error quit)
601 (if (and (buffer-base-buffer (current-buffer))
602 (string-match "\\`CAPTURE-" (buffer-name)))
603 (kill-buffer (current-buffer)))
604 (set-window-configuration (org-capture-get :return-to-wconf))
605 (error "Capture template `%s': %s"
606 (org-capture-get :key)
607 (nth 1 error))))
608 (if (and (derived-mode-p 'org-mode)
609 (org-capture-get :clock-in))
610 (condition-case nil
611 (progn
612 (if (org-clock-is-active)
613 (org-capture-put :interrupted-clock
614 (copy-marker org-clock-marker)))
615 (org-clock-in)
616 (org-set-local 'org-capture-clock-was-started t))
617 (error
618 "Could not start the clock in this capture buffer")))
619 (if (org-capture-get :immediate-finish)
620 (org-capture-finalize)))))))))
622 (defun org-capture-get-template ()
623 "Get the template from a file or a function if necessary."
624 (let ((txt (org-capture-get :template)) file)
625 (cond
626 ((and (listp txt) (eq (car txt) 'file))
627 (if (file-exists-p
628 (setq file (expand-file-name (nth 1 txt) org-directory)))
629 (setq txt (org-file-contents file))
630 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
631 ((and (listp txt) (eq (car txt) 'function))
632 (if (fboundp (nth 1 txt))
633 (setq txt (funcall (nth 1 txt)))
634 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
635 ((not txt) (setq txt ""))
636 ((stringp txt))
637 (t (setq txt "* Invalid capture template")))
638 (org-capture-put :template txt)))
640 (defun org-capture-finalize (&optional stay-with-capture)
641 "Finalize the capture process.
642 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
643 captured item after finalizing."
644 (interactive "P")
645 (when (org-capture-get :jump-to-captured)
646 (setq stay-with-capture t))
647 (unless (and org-capture-mode
648 (buffer-base-buffer (current-buffer)))
649 (error "This does not seem to be a capture buffer for Org-mode"))
651 (run-hooks 'org-capture-prepare-finalize-hook)
653 ;; Did we start the clock in this capture buffer?
654 (when (and org-capture-clock-was-started
655 org-clock-marker (marker-buffer org-clock-marker)
656 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
657 (> org-clock-marker (point-min))
658 (< org-clock-marker (point-max)))
659 ;; Looks like the clock we started is still running. Clock out.
660 (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
661 (when (and (not org-capture-clock-keep)
662 (org-capture-get :clock-resume 'local)
663 (markerp (org-capture-get :interrupted-clock 'local))
664 (buffer-live-p (marker-buffer
665 (org-capture-get :interrupted-clock 'local))))
666 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
667 (org-with-point-at clock-in-task
668 (org-clock-in)))
669 (message "Interrupted clock has been resumed")))
671 (let ((beg (point-min))
672 (end (point-max))
673 (abort-note nil))
674 ;; Store the size of the capture buffer
675 (org-capture-put :captured-entry-size (- (point-max) (point-min)))
676 (widen)
677 ;; Store the insertion point in the target buffer
678 (org-capture-put :insertion-point (point))
680 (if org-note-abort
681 (let ((m1 (org-capture-get :begin-marker 'local))
682 (m2 (org-capture-get :end-marker 'local)))
683 (if (and m1 m2 (= m1 beg) (= m2 end))
684 (progn
685 (setq m2 (if (cdr (assoc 'heading org-blank-before-new-entry))
686 m2 (1+ m2))
687 m2 (if (< (point-max) m2) (point-max) m2))
688 (setq abort-note 'clean)
689 (kill-region m1 m2))
690 (setq abort-note 'dirty)))
692 ;; Make sure that the empty lines after are correct
693 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
694 (member (org-capture-get :type 'local)
695 '(entry item checkitem plain)))
696 (save-excursion
697 (goto-char end)
698 (or (bolp) (newline))
699 (org-capture-empty-lines-after
700 (or (org-capture-get :empty-lines-after 'local)
701 (org-capture-get :empty-lines 'local) 0))))
702 ;; Postprocessing: Update Statistics cookies, do the sorting
703 (when (derived-mode-p 'org-mode)
704 (save-excursion
705 (when (ignore-errors (org-back-to-heading))
706 (org-update-parent-todo-statistics)
707 (org-update-checkbox-count)))
708 ;; FIXME Here we should do the sorting
709 ;; If we have added a table line, maybe recompute?
710 (when (and (eq (org-capture-get :type 'local) 'table-line)
711 (org-at-table-p))
712 (if (org-table-get-stored-formulas)
713 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
714 (org-table-align))))
715 ;; Store this place as the last one where we stored something
716 ;; Do the marking in the base buffer, so that it makes sense after
717 ;; the indirect buffer has been killed.
718 (when org-capture-bookmark
719 (org-capture-bookmark-last-stored-position))
721 ;; Run the hook
722 (run-hooks 'org-capture-before-finalize-hook))
724 (when (org-capture-get :decrypted)
725 (save-excursion
726 (goto-char (org-capture-get :decrypted))
727 (org-encrypt-entry)))
729 ;; Kill the indirect buffer
730 (save-buffer)
731 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
732 (new-buffer (org-capture-get :new-buffer 'local))
733 (kill-buffer (org-capture-get :kill-buffer 'local))
734 (base-buffer (buffer-base-buffer (current-buffer))))
736 ;; Kill the indirect buffer
737 (kill-buffer (current-buffer))
739 ;; Narrow back the target buffer to its previous state
740 (with-current-buffer (org-capture-get :buffer)
741 (let ((reg (org-capture-get :initial-target-region))
742 (pos (org-capture-get :initial-target-position))
743 (ipt (org-capture-get :insertion-point))
744 (size (org-capture-get :captured-entry-size)))
745 (if (not reg)
746 (widen)
747 (cond ((< ipt (car reg))
748 ;; insertion point is before the narrowed region
749 (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
750 ((> ipt (cdr reg))
751 ;; insertion point is after the narrowed region
752 (narrow-to-region (car reg) (cdr reg)))
754 ;; insertion point is within the narrowed region
755 (narrow-to-region (car reg) (+ size (cdr reg)))))
756 ;; now place back the point at its original position
757 (if (< ipt (car reg))
758 (goto-char (+ size pos))
759 (goto-char (if (< ipt pos) (+ size pos) pos))))))
761 ;; Kill the target buffer if that is desired
762 (when (and base-buffer new-buffer kill-buffer)
763 (with-current-buffer base-buffer (save-buffer))
764 (kill-buffer base-buffer))
766 ;; Restore the window configuration before capture
767 (set-window-configuration return-wconf))
769 (run-hooks 'org-capture-after-finalize-hook)
770 ;; Special cases
771 (cond
772 (abort-note
773 (cond
774 ((equal abort-note 'clean)
775 (message "Capture process aborted and target buffer cleaned up"))
776 ((equal abort-note 'dirty)
777 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
778 (stay-with-capture
779 (org-capture-goto-last-stored)))
780 ;; Return if we did store something
781 (not abort-note)))
783 (defun org-capture-refile ()
784 "Finalize the current capture and then refile the entry.
785 Refiling is done from the base buffer, because the indirect buffer is then
786 already gone. Any prefix argument will be passed to the refile command."
787 (interactive)
788 (unless (eq (org-capture-get :type 'local) 'entry)
789 (error
790 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
791 (let ((pos (point))
792 (base (buffer-base-buffer (current-buffer)))
793 (org-refile-for-capture t))
794 (save-window-excursion
795 (with-current-buffer (or base (current-buffer))
796 (save-excursion
797 (save-restriction
798 (widen)
799 (goto-char pos)
800 (call-interactively 'org-refile)))))
801 (org-capture-finalize)))
803 (defun org-capture-kill ()
804 "Abort the current capture process."
805 (interactive)
806 ;; FIXME: This does not do the right thing, we need to remove the
807 ;; new stuff by hand it is easy: undo, then kill the buffer
808 (let ((org-note-abort t)
809 (org-capture-before-finalize-hook nil))
810 (org-capture-finalize)))
812 (defun org-capture-goto-last-stored ()
813 "Go to the location where the last capture note was stored."
814 (interactive)
815 (org-goto-marker-or-bmk org-capture-last-stored-marker
816 (plist-get org-bookmark-names-plist
817 :last-capture))
818 (message "This is the last note stored by a capture process"))
820 ;;; Supporting functions for handling the process
822 (defun org-capture-put-target-region-and-position ()
823 "Store the initial region with `org-capture-put'."
824 (org-capture-put
825 :initial-target-region
826 ;; Check if the buffer is currently narrowed
827 (when (org-buffer-narrowed-p)
828 (cons (point-min) (point-max))))
829 ;; store the current point
830 (org-capture-put :initial-target-position (point)))
832 (defvar org-time-was-given) ; dynamically scoped parameter
833 (defun org-capture-set-target-location (&optional target)
834 "Find TARGET buffer and position.
835 Store them in the capture property list."
836 (let ((target-entry-p t) decrypted-hl-pos)
837 (setq target (or target (org-capture-get :target)))
838 (save-excursion
839 (cond
840 ((eq (car target) 'file)
841 (set-buffer (org-capture-target-buffer (nth 1 target)))
842 (org-capture-put-target-region-and-position)
843 (widen)
844 (setq target-entry-p nil))
846 ((eq (car target) 'id)
847 (let ((loc (org-id-find (nth 1 target))))
848 (if (not loc)
849 (error "Cannot find target ID \"%s\"" (nth 1 target))
850 (set-buffer (org-capture-target-buffer (car loc)))
851 (widen)
852 (org-capture-put-target-region-and-position)
853 (goto-char (cdr loc)))))
855 ((eq (car target) 'file+headline)
856 (set-buffer (org-capture-target-buffer (nth 1 target)))
857 (org-capture-put-target-region-and-position)
858 (widen)
859 (let ((hd (nth 2 target)))
860 (goto-char (point-min))
861 (unless (derived-mode-p 'org-mode)
862 (error
863 "Target buffer \"%s\" for file+headline should be in Org mode"
864 (current-buffer)))
865 (if (re-search-forward
866 (format org-complex-heading-regexp-format (regexp-quote hd))
867 nil t)
868 (goto-char (point-at-bol))
869 (goto-char (point-max))
870 (or (bolp) (insert "\n"))
871 (insert "* " hd "\n")
872 (beginning-of-line 0))))
874 ((eq (car target) 'file+olp)
875 (let ((m (org-find-olp
876 (cons (org-capture-expand-file (nth 1 target))
877 (cddr target)))))
878 (set-buffer (marker-buffer m))
879 (org-capture-put-target-region-and-position)
880 (widen)
881 (goto-char m)))
883 ((eq (car target) 'file+regexp)
884 (set-buffer (org-capture-target-buffer (nth 1 target)))
885 (org-capture-put-target-region-and-position)
886 (widen)
887 (goto-char (point-min))
888 (if (re-search-forward (nth 2 target) nil t)
889 (progn
890 (goto-char (if (org-capture-get :prepend)
891 (match-beginning 0) (match-end 0)))
892 (org-capture-put :exact-position (point))
893 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
894 (error "No match for target regexp in file %s" (nth 1 target))))
896 ((memq (car target) '(file+datetree file+datetree+prompt))
897 (require 'org-datetree)
898 (set-buffer (org-capture-target-buffer (nth 1 target)))
899 (org-capture-put-target-region-and-position)
900 (widen)
901 ;; Make a date tree entry, with the current date (or yesterday,
902 ;; if we are extending dates for a couple of hours)
903 (org-datetree-find-date-create
904 (calendar-gregorian-from-absolute
905 (cond
906 (org-overriding-default-time
907 ;; use the overriding default time
908 (time-to-days org-overriding-default-time))
910 ((eq (car target) 'file+datetree+prompt)
911 ;; prompt for date
912 (let ((prompt-time (org-read-date
913 nil t nil "Date for tree entry:"
914 (current-time))))
915 (org-capture-put
916 :default-time
917 (cond ((and (or (not (boundp 'org-time-was-given))
918 (not org-time-was-given))
919 (not (= (time-to-days prompt-time) (org-today))))
920 ;; Use 00:00 when no time is given for another date than today?
921 (apply 'encode-time (append '(0 0 0) (cdddr (decode-time prompt-time)))))
922 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer)
923 ;; Replace any time range by its start
924 (apply 'encode-time
925 (org-read-date-analyze
926 (replace-match "\\1 \\2" nil nil org-read-date-final-answer)
927 prompt-time (decode-time prompt-time))))
928 (t prompt-time)))
929 (time-to-days prompt-time)))
931 ;; current date, possibly corrected for late night workers
932 (org-today))))))
934 ((eq (car target) 'file+function)
935 (set-buffer (org-capture-target-buffer (nth 1 target)))
936 (org-capture-put-target-region-and-position)
937 (widen)
938 (funcall (nth 2 target))
939 (org-capture-put :exact-position (point))
940 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
942 ((eq (car target) 'function)
943 (funcall (nth 1 target))
944 (org-capture-put :exact-position (point))
945 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
947 ((eq (car target) 'clock)
948 (if (and (markerp org-clock-hd-marker)
949 (marker-buffer org-clock-hd-marker))
950 (progn (set-buffer (marker-buffer org-clock-hd-marker))
951 (org-capture-put-target-region-and-position)
952 (widen)
953 (goto-char org-clock-hd-marker))
954 (error "No running clock that could be used as capture target")))
956 (t (error "Invalid capture target specification")))
958 (when (and (featurep 'org-crypt) (org-at-encrypted-entry-p))
959 (org-decrypt-entry)
960 (setq decrypted-hl-pos
961 (save-excursion (and (org-back-to-heading t) (point)))))
963 (org-capture-put :buffer (current-buffer) :pos (point)
964 :target-entry-p target-entry-p
965 :decrypted decrypted-hl-pos))))
967 (defun org-capture-expand-file (file)
968 "Expand functions and symbols for FILE.
969 When FILE is a function, call it. When it is a form, evaluate
970 it. When it is a variable, retrieve the value. When it is
971 a string, return it. However, if it is the empty string, return
972 `org-default-notes-file' instead."
973 (cond
974 ((equal file "") org-default-notes-file)
975 ((org-string-nw-p file) file)
976 ((functionp file) (funcall file))
977 ((and (symbolp file) (boundp file)) (symbol-value file))
978 ((consp file) (eval file))
979 (t file)))
981 (defun org-capture-target-buffer (file)
982 "Get a buffer for FILE."
983 (setq file (org-capture-expand-file file))
984 (setq file (or (org-string-nw-p file)
985 org-default-notes-file
986 (error "No notes file specified, and no default available")))
987 (or (org-find-base-buffer-visiting file)
988 (progn (org-capture-put :new-buffer t)
989 (find-file-noselect (expand-file-name file org-directory)))))
991 (defun org-capture-steal-local-variables (buffer)
992 "Install Org-mode local variables of BUFFER."
993 (mapc (lambda (v)
994 (ignore-errors (org-set-local (car v) (cdr v))))
995 (buffer-local-variables buffer)))
997 (defun org-capture-place-template (&optional inhibit-wconf-store)
998 "Insert the template at the target location, and display the buffer.
999 When `inhibit-wconf-store', don't store the window configuration, as it
1000 may have been stored before."
1001 (unless inhibit-wconf-store
1002 (org-capture-put :return-to-wconf (current-window-configuration)))
1003 (delete-other-windows)
1004 (org-switch-to-buffer-other-window
1005 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
1006 (widen)
1007 (show-all)
1008 (goto-char (org-capture-get :pos))
1009 (org-set-local 'org-capture-target-marker
1010 (point-marker))
1011 (org-set-local 'outline-level 'org-outline-level)
1012 (let* ((template (org-capture-get :template))
1013 (type (org-capture-get :type)))
1014 (case type
1015 ((nil entry) (org-capture-place-entry))
1016 (table-line (org-capture-place-table-line))
1017 (plain (org-capture-place-plain-text))
1018 (item (org-capture-place-item))
1019 (checkitem (org-capture-place-item))))
1020 (org-capture-mode 1)
1021 (org-set-local 'org-capture-current-plist org-capture-plist))
1023 (defun org-capture-place-entry ()
1024 "Place the template as a new Org entry."
1025 (let* ((txt (org-capture-get :template))
1026 (reversed (org-capture-get :prepend))
1027 (target-entry-p (org-capture-get :target-entry-p))
1028 level beg end file)
1030 (and (org-capture-get :exact-position)
1031 (goto-char (org-capture-get :exact-position)))
1032 (cond
1033 ((not target-entry-p)
1034 ;; Insert as top-level entry, either at beginning or at end of file
1035 (setq level 1)
1036 (if reversed
1037 (progn (goto-char (point-min))
1038 (or (org-at-heading-p)
1039 (outline-next-heading)))
1040 (goto-char (point-max))
1041 (or (bolp) (insert "\n"))))
1043 ;; Insert as a child of the current entry
1044 (and (looking-at "\\*+")
1045 (setq level (- (match-end 0) (match-beginning 0))))
1046 (setq level (org-get-valid-level (or level 1) 1))
1047 (if reversed
1048 (progn
1049 (outline-next-heading)
1050 (or (bolp) (insert "\n")))
1051 (org-end-of-subtree t nil)
1052 (or (bolp) (insert "\n")))))
1053 (org-capture-empty-lines-before)
1054 (setq beg (point))
1055 (org-capture-verify-tree txt)
1056 (org-paste-subtree level txt 'for-yank)
1057 (org-capture-empty-lines-after 1)
1058 (org-capture-position-for-last-stored beg)
1059 (outline-next-heading)
1060 (setq end (point))
1061 (org-capture-mark-kill-region beg (1- end))
1062 (org-capture-narrow beg (1- end))
1063 (if (or (re-search-backward "%\\?" beg t)
1064 (re-search-forward "%\\?" end t))
1065 (replace-match ""))))
1067 (defun org-capture-place-item ()
1068 "Place the template as a new plain list item."
1069 (let* ((txt (org-capture-get :template))
1070 (target-entry-p (org-capture-get :target-entry-p))
1071 (ind 0)
1072 beg end)
1073 (if (org-capture-get :exact-position)
1074 (goto-char (org-capture-get :exact-position))
1075 (cond
1076 ((not target-entry-p)
1077 ;; Insert as top-level entry, either at beginning or at end of file
1078 (setq beg (point-min) end (point-max)))
1080 (setq beg (1+ (point-at-eol))
1081 end (save-excursion (outline-next-heading) (point)))))
1082 (setq ind nil)
1083 (if (org-capture-get :prepend)
1084 (progn
1085 (goto-char beg)
1086 (when (org-list-search-forward (org-item-beginning-re) end t)
1087 (goto-char (match-beginning 0))
1088 (setq ind (org-get-indentation))))
1089 (goto-char end)
1090 (when (org-list-search-backward (org-item-beginning-re) beg t)
1091 (setq ind (org-get-indentation))
1092 (org-end-of-item)))
1093 (unless ind (goto-char end)))
1094 ;; Remove common indentation
1095 (setq txt (org-remove-indentation txt))
1096 ;; Make sure this is indeed an item
1097 (unless (string-match (concat "\\`" (org-item-re)) txt)
1098 (setq txt (concat "- "
1099 (mapconcat 'identity (split-string txt "\n")
1100 "\n "))))
1101 ;; Prepare surrounding empty lines.
1102 (org-capture-empty-lines-before)
1103 (setq beg (point))
1104 (unless (eolp) (save-excursion (insert "\n")))
1105 (unless ind
1106 (org-indent-line)
1107 (setq ind (org-get-indentation))
1108 (delete-region beg (point)))
1109 ;; Set the correct indentation, depending on context
1110 (setq ind (make-string ind ?\ ))
1111 (setq txt (concat ind
1112 (mapconcat 'identity (split-string txt "\n")
1113 (concat "\n" ind))
1114 "\n"))
1115 ;; Insert item.
1116 (insert txt)
1117 (org-capture-empty-lines-after 1)
1118 (org-capture-position-for-last-stored beg)
1119 (forward-char 1)
1120 (setq end (point))
1121 (org-capture-mark-kill-region beg (1- end))
1122 (org-capture-narrow beg (1- end))
1123 (if (or (re-search-backward "%\\?" beg t)
1124 (re-search-forward "%\\?" end t))
1125 (replace-match ""))))
1127 (defun org-capture-place-table-line ()
1128 "Place the template as a table line."
1129 (require 'org-table)
1130 (let* ((txt (org-capture-get :template))
1131 (target-entry-p (org-capture-get :target-entry-p))
1132 (table-line-pos (org-capture-get :table-line-pos))
1133 ind beg end)
1134 (cond
1135 ((org-capture-get :exact-position)
1136 (goto-char (org-capture-get :exact-position)))
1137 ((not target-entry-p)
1138 ;; Table is not necessarily under a heading
1139 (setq beg (point-min) end (point-max)))
1141 ;; WE are at a heading, limit search to the body
1142 (setq beg (1+ (point-at-eol))
1143 end (save-excursion (outline-next-heading) (point)))))
1144 (if (re-search-forward org-table-dataline-regexp end t)
1145 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t))
1146 (goto-char e)
1147 (if (looking-at "[ \t]*#\\+tblfm:")
1148 (forward-line 1))
1149 (narrow-to-region b (point)))
1150 (goto-char end)
1151 (insert "\n| |\n|----|\n| |\n")
1152 (narrow-to-region (1+ end) (point)))
1153 ;; We are narrowed to the table, or to an empty line if there was no table
1155 ;; Check if the template is good
1156 (if (not (string-match org-table-dataline-regexp txt))
1157 (setq txt "| %?Bad template |\n"))
1158 (if (functionp table-line-pos)
1159 (setq table-line-pos (funcall table-line-pos))
1160 (setq table-line-pos (eval table-line-pos)))
1161 (cond
1162 ((and table-line-pos
1163 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
1164 ;; we have a complex line specification
1165 (let ((ll (ignore-errors
1166 (save-match-data (org-table-analyze))
1167 (aref org-table-hlines
1168 (- (match-end 1) (match-beginning 1)))))
1169 (delta (string-to-number (match-string 2 table-line-pos))))
1170 ;; The user wants a special position in the table
1171 (unless ll
1172 (error "Invalid table line specification \"%s\"" table-line-pos))
1173 (goto-char org-table-current-begin-pos)
1174 (forward-line (+ ll delta (if (< delta 0) 0 -1)))
1175 (org-table-insert-row 'below)
1176 (beginning-of-line 1)
1177 (delete-region (point) (1+ (point-at-eol)))
1178 (setq beg (point))
1179 (insert txt)
1180 (setq end (point))))
1181 ((org-capture-get :prepend)
1182 (goto-char (point-min))
1183 (re-search-forward org-table-hline-regexp nil t)
1184 (beginning-of-line 1)
1185 (re-search-forward org-table-dataline-regexp nil t)
1186 (beginning-of-line 1)
1187 (setq beg (point))
1188 (org-table-insert-row)
1189 (beginning-of-line 1)
1190 (delete-region (point) (1+ (point-at-eol)))
1191 (insert txt)
1192 (setq end (point)))
1194 (goto-char (point-max))
1195 (re-search-backward org-table-dataline-regexp nil t)
1196 (beginning-of-line 1)
1197 (org-table-insert-row 'below)
1198 (beginning-of-line 1)
1199 (delete-region (point) (1+ (point-at-eol)))
1200 (setq beg (point))
1201 (insert txt)
1202 (setq end (point))))
1203 (goto-char beg)
1204 (org-capture-position-for-last-stored 'table-line)
1205 (if (or (re-search-backward "%\\?" beg t)
1206 (re-search-forward "%\\?" end t))
1207 (replace-match ""))
1208 (org-table-align)))
1210 (defun org-capture-place-plain-text ()
1211 "Place the template plainly.
1212 If the target locator points at an Org node, place the template into
1213 the text of the entry, before the first child. If not, place the
1214 template at the beginning or end of the file.
1215 Of course, if exact position has been required, just put it there."
1216 (let* ((txt (org-capture-get :template))
1217 beg end)
1218 (cond
1219 ((org-capture-get :exact-position)
1220 (goto-char (org-capture-get :exact-position)))
1221 ((and (org-capture-get :target-entry-p)
1222 (bolp)
1223 (looking-at org-outline-regexp))
1224 ;; we should place the text into this entry
1225 (if (org-capture-get :prepend)
1226 ;; Skip meta data and drawers
1227 (org-end-of-meta-data t)
1228 ;; go to ent of the entry text, before the next headline
1229 (outline-next-heading)))
1231 ;; beginning or end of file
1232 (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
1233 (or (bolp) (newline))
1234 (org-capture-empty-lines-before)
1235 (setq beg (point))
1236 (insert txt)
1237 (org-capture-empty-lines-after 1)
1238 (org-capture-position-for-last-stored beg)
1239 (setq end (point))
1240 (org-capture-mark-kill-region beg (1- end))
1241 (org-capture-narrow beg (1- end))
1242 (if (or (re-search-backward "%\\?" beg t)
1243 (re-search-forward "%\\?" end t))
1244 (replace-match ""))))
1246 (defun org-capture-mark-kill-region (beg end)
1247 "Mark the region that will have to be killed when aborting capture."
1248 (let ((m1 (move-marker (make-marker) beg))
1249 (m2 (move-marker (make-marker) end)))
1250 (org-capture-put :begin-marker m1)
1251 (org-capture-put :end-marker m2)))
1253 (defun org-capture-position-for-last-stored (where)
1254 "Memorize the position that should later become the position of last capture."
1255 (cond
1256 ((integerp where)
1257 (org-capture-put :position-for-last-stored
1258 (move-marker (make-marker) where
1259 (or (buffer-base-buffer (current-buffer))
1260 (current-buffer)))))
1261 ((eq where 'table-line)
1262 (org-capture-put :position-for-last-stored
1263 (list 'table-line
1264 (org-table-current-dline))))
1265 (t (error "This should not happen"))))
1267 (defun org-capture-bookmark-last-stored-position ()
1268 "Bookmark the last-captured position."
1269 (let* ((where (org-capture-get :position-for-last-stored 'local))
1270 (pos (cond
1271 ((markerp where)
1272 (prog1 (marker-position where)
1273 (move-marker where nil)))
1274 ((and (listp where) (eq (car where) 'table-line))
1275 (if (org-at-table-p)
1276 (save-excursion
1277 (org-table-goto-line (nth 1 where))
1278 (point-at-bol))
1279 (point))))))
1280 (with-current-buffer (buffer-base-buffer (current-buffer))
1281 (save-excursion
1282 (save-restriction
1283 (widen)
1284 (goto-char pos)
1285 (let ((bookmark-name (plist-get org-bookmark-names-plist
1286 :last-capture)))
1287 (when bookmark-name
1288 (with-demoted-errors
1289 (bookmark-set bookmark-name))))
1290 (move-marker org-capture-last-stored-marker (point)))))))
1292 (defun org-capture-narrow (beg end)
1293 "Narrow, unless configuration says not to narrow."
1294 (unless (org-capture-get :unnarrowed)
1295 (narrow-to-region beg end)
1296 (goto-char beg)))
1298 (defun org-capture-empty-lines-before (&optional n)
1299 "Set the correct number of empty lines before the insertion point.
1300 Point will be after the empty lines, so insertion can directly be done."
1301 (setq n (or n (org-capture-get :empty-lines-before)
1302 (org-capture-get :empty-lines) 0))
1303 (let ((pos (point)))
1304 (org-back-over-empty-lines)
1305 (delete-region (point) pos)
1306 (if (> n 0) (newline n))))
1308 (defun org-capture-empty-lines-after (&optional n)
1309 "Set the correct number of empty lines after the inserted string.
1310 Point will remain at the first line after the inserted text."
1311 (setq n (or n (org-capture-get :empty-lines-after)
1312 (org-capture-get :empty-lines) 0))
1313 (org-back-over-empty-lines)
1314 (while (looking-at "[ \t]*\n") (replace-match ""))
1315 (let ((pos (point)))
1316 (if (> n 0) (newline n))
1317 (goto-char pos)))
1319 (defvar org-clock-marker) ; Defined in org.el
1321 (defun org-capture-insert-template-here ()
1322 "Insert the capture template at point."
1323 (let* ((template (org-capture-get :template))
1324 (type (org-capture-get :type))
1325 beg end pp)
1326 (or (bolp) (newline))
1327 (setq beg (point))
1328 (cond
1329 ((and (eq type 'entry) (derived-mode-p 'org-mode))
1330 (org-capture-verify-tree (org-capture-get :template))
1331 (org-paste-subtree nil template t))
1332 ((and (memq type '(item checkitem))
1333 (derived-mode-p 'org-mode)
1334 (save-excursion (skip-chars-backward " \t\n")
1335 (setq pp (point))
1336 (org-in-item-p)))
1337 (goto-char pp)
1338 (org-insert-item)
1339 (skip-chars-backward " ")
1340 (skip-chars-backward "-+*0123456789).")
1341 (delete-region (point) (point-at-eol))
1342 (setq beg (point))
1343 (org-remove-indentation template)
1344 (insert template)
1345 (org-capture-empty-lines-after)
1346 (goto-char beg)
1347 (org-list-repair)
1348 (org-end-of-item)
1349 (setq end (point)))
1350 (t (insert template)))
1351 (setq end (point))
1352 (goto-char beg)
1353 (if (re-search-forward "%\\?" end t)
1354 (replace-match ""))))
1356 (defun org-capture-set-plist (entry)
1357 "Initialize the property list from the template definition."
1358 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1359 (org-capture-put :key (car entry) :description (nth 1 entry)
1360 :target (nth 3 entry))
1361 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1362 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1363 ;; The template may be empty or omitted for special types.
1364 ;; Here we insert the default templates for such cases.
1365 (cond
1366 ((eq type 'item) (setq txt "- %?"))
1367 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1368 ((eq type 'table-line) (setq txt "| %? |"))
1369 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1370 (org-capture-put :template txt :type type)))
1372 (defun org-capture-goto-target (&optional template-key)
1373 "Go to the target location of a capture template.
1374 The user is queried for the template."
1375 (interactive)
1376 (let* (org-select-template-temp-major-mode
1377 (entry (org-capture-select-template template-key)))
1378 (unless entry
1379 (error "No capture template selected"))
1380 (org-capture-set-plist entry)
1381 (org-capture-set-target-location)
1382 (org-pop-to-buffer-same-window (org-capture-get :buffer))
1383 (goto-char (org-capture-get :pos))))
1385 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1386 "Make an indirect buffer for a capture process.
1387 Use PREFIX as a prefix for the name of the indirect buffer."
1388 (setq buffer (or buffer (current-buffer)))
1389 (let ((n 1) (base (buffer-name buffer)) bname)
1390 (setq bname (concat prefix "-" base))
1391 (while (buffer-live-p (get-buffer bname))
1392 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1393 (condition-case nil
1394 (make-indirect-buffer buffer bname 'clone)
1395 (error
1396 (let ((buf (make-indirect-buffer buffer bname)))
1397 (with-current-buffer buf (org-mode))
1398 buf)))))
1400 (defun org-capture-verify-tree (tree)
1401 "Throw error if TREE is not a valid tree."
1402 (unless (org-kill-is-subtree-p tree)
1403 (error "Template is not a valid Org entry or tree")))
1405 (defun org-mks (table title &optional prompt specials)
1406 "Select a member of an alist with multiple keys.
1407 TABLE is the alist which should contain entries where the car is a string.
1408 There should be two types of entries.
1410 1. prefix descriptions like (\"a\" \"Description\")
1411 This indicates that `a' is a prefix key for multi-letter selection, and
1412 that there are entries following with keys like \"ab\", \"ax\"...
1414 2. Selectable members must have more than two elements, with the first
1415 being the string of keys that lead to selecting it, and the second a
1416 short description string of the item.
1418 The command will then make a temporary buffer listing all entries
1419 that can be selected with a single key, and all the single key
1420 prefixes. When you press the key for a single-letter entry, it is selected.
1421 When you press a prefix key, the commands (and maybe further prefixes)
1422 under this key will be shown and offered for selection.
1424 TITLE will be placed over the selection in the temporary buffer,
1425 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1426 also (\"key\" \"description\") entries. When one of these is selection,
1427 only the bare key is returned."
1428 (setq prompt (or prompt "Select: "))
1429 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1430 current prefix rtn re pressed buffer (inhibit-quit t))
1431 (save-window-excursion
1432 (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
1433 (setq orig-table table)
1434 (catch 'exit
1435 (while t
1436 (erase-buffer)
1437 (insert title "\n\n")
1438 (setq tbl table
1439 des-keys nil
1440 allowed-keys nil
1441 cursor-type nil)
1442 (setq prefix (if current (concat current " ") ""))
1443 (while tbl
1444 (cond
1445 ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
1446 ;; This is a description on this level
1447 (setq dkey (caar tbl) ddesc (cadar tbl))
1448 (pop tbl)
1449 (push dkey des-keys)
1450 (push dkey allowed-keys)
1451 (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
1452 ;; Skip keys which are below this prefix
1453 (setq re (concat "\\`" (regexp-quote dkey)))
1454 (let (case-fold-search)
1455 (while (and tbl (string-match re (caar tbl))) (pop tbl))))
1456 ((= 2 (length (car tbl)))
1457 ;; Not yet a usable description, skip it
1460 ;; usable entry on this level
1461 (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
1462 (push (caar tbl) allowed-keys)
1463 (pop tbl))))
1464 (when specials
1465 (insert "-------------------------------------------------------------------------------\n")
1466 (let ((sp specials))
1467 (while sp
1468 (insert (format "[%s] %s\n"
1469 (caar sp) (nth 1 (car sp))))
1470 (push (caar sp) allowed-keys)
1471 (pop sp))))
1472 (push "\C-g" allowed-keys)
1473 (goto-char (point-min))
1474 (if (not (pos-visible-in-window-p (point-max)))
1475 (org-fit-window-to-buffer))
1476 (message prompt)
1477 (setq pressed (char-to-string (read-char-exclusive)))
1478 (while (not (member pressed allowed-keys))
1479 (message "Invalid key `%s'" pressed) (sit-for 1)
1480 (message prompt)
1481 (setq pressed (char-to-string (read-char-exclusive))))
1482 (when (equal pressed "\C-g")
1483 (kill-buffer buffer)
1484 (error "Abort"))
1485 (when (and (not (assoc pressed table))
1486 (not (member pressed des-keys))
1487 (assoc pressed specials))
1488 (throw 'exit (setq rtn pressed)))
1489 (unless (member pressed des-keys)
1490 (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
1491 orig-table))))
1492 (setq current (concat current pressed))
1493 (setq table (mapcar
1494 (lambda (x)
1495 (if (and (> (length (car x)) 1)
1496 (equal (substring (car x) 0 1) pressed))
1497 (cons (substring (car x) 1) (cdr x))
1498 nil))
1499 table))
1500 (setq table (remove nil table)))))
1501 (when buffer (kill-buffer buffer))
1502 rtn))
1504 ;;; The template code
1505 (defun org-capture-select-template (&optional keys)
1506 "Select a capture template.
1507 Lisp programs can force the template by setting KEYS to a string."
1508 (let ((org-capture-templates
1509 (or (org-contextualize-keys
1510 org-capture-templates org-capture-templates-contexts)
1511 '(("t" "Task" entry (file+headline "" "Tasks")
1512 "* TODO %?\n %u\n %a")))))
1513 (if keys
1514 (or (assoc keys org-capture-templates)
1515 (error "No capture template referred to by \"%s\" keys" keys))
1516 (org-mks org-capture-templates
1517 "Select a capture template\n========================="
1518 "Template key: "
1519 '(("C" "Customize org-capture-templates")
1520 ("q" "Abort"))))))
1522 (defun org-capture-fill-template (&optional template initial annotation)
1523 "Fill a template and return the filled template as a string.
1524 The template may still contain \"%?\" for cursor positioning."
1525 (setq template (or template (org-capture-get :template)))
1526 (when (stringp initial)
1527 (setq initial (org-no-properties initial)))
1528 (let* ((buffer (org-capture-get :buffer))
1529 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1530 (ct (org-capture-get :default-time))
1531 (dct (decode-time ct))
1532 (ct1
1533 (if (< (nth 2 dct) org-extend-today-until)
1534 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
1535 ct))
1536 (plist-p (if org-store-link-plist t nil))
1537 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
1538 (v-x (or (org-get-x-clipboard 'PRIMARY)
1539 (org-get-x-clipboard 'CLIPBOARD)
1540 (org-get-x-clipboard 'SECONDARY)))
1541 (v-t (format-time-string (car org-time-stamp-formats) ct1))
1542 (v-T (format-time-string (cdr org-time-stamp-formats) ct1))
1543 (v-u (concat "[" (substring v-t 1 -1) "]"))
1544 (v-U (concat "[" (substring v-T 1 -1) "]"))
1545 ;; `initial' and `annotation' might habe been passed.
1546 ;; But if the property list has them, we prefer those values
1547 (v-i (or (plist-get org-store-link-plist :initial)
1548 initial
1549 (org-capture-get :initial)
1550 ""))
1551 (v-a (or (plist-get org-store-link-plist :annotation)
1552 annotation
1553 (org-capture-get :annotation)
1554 ""))
1555 ;; Is the link empty? Then we do not want it...
1556 (v-a (if (equal v-a "[[]]") "" v-a))
1557 (clipboards (remove nil (list v-i
1558 (org-get-x-clipboard 'PRIMARY)
1559 (org-get-x-clipboard 'CLIPBOARD)
1560 (org-get-x-clipboard 'SECONDARY)
1561 v-c)))
1562 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1563 (v-A (if (and v-a (string-match l-re v-a))
1564 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
1565 v-a))
1566 (v-l (if (and v-a (string-match l-re v-a))
1567 (replace-match "\\1" nil nil v-a)
1568 v-a))
1569 (v-n user-full-name)
1570 (v-k (if (marker-buffer org-clock-marker)
1571 (org-no-properties org-clock-heading)))
1572 (v-K (if (marker-buffer org-clock-marker)
1573 (org-make-link-string
1574 (buffer-file-name (marker-buffer org-clock-marker))
1575 org-clock-heading)))
1576 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1577 (v-F (or (org-capture-get :original-file) ""))
1579 (org-startup-folded nil)
1580 (org-inhibit-startup t)
1581 org-time-was-given org-end-time-was-given x
1582 prompt completions char time pos default histvar strings)
1584 (setq org-store-link-plist
1585 (plist-put org-store-link-plist :annotation v-a)
1586 org-store-link-plist
1587 (plist-put org-store-link-plist :initial v-i))
1588 (setq initial v-i)
1590 (unless template (setq template "") (message "No template") (ding)
1591 (sit-for 1))
1592 (save-window-excursion
1593 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1594 (erase-buffer)
1595 (insert template)
1596 (goto-char (point-min))
1597 (org-capture-steal-local-variables buffer)
1598 (setq buffer-file-name nil mark-active nil)
1600 ;; %[] Insert contents of a file.
1601 (goto-char (point-min))
1602 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1603 (unless (org-capture-escaped-%)
1604 (let ((start (match-beginning 0))
1605 (end (match-end 0))
1606 (filename (expand-file-name (match-string 1))))
1607 (goto-char start)
1608 (delete-region start end)
1609 (condition-case error
1610 (insert-file-contents filename)
1611 (error (insert (format "%%![Couldn't insert %s: %s]"
1612 filename error)))))))
1614 ;; The current time
1615 (goto-char (point-min))
1616 (while (re-search-forward "%<\\([^>\n]+\\)>" nil t)
1617 (replace-match (format-time-string (match-string 1)) t t))
1619 ;; Simple %-escapes
1620 (goto-char (point-min))
1621 (while (re-search-forward "%\\([tTuUaliAcxkKInfF]\\)" nil t)
1622 (unless (org-capture-escaped-%)
1623 (when (and initial (equal (match-string 0) "%i"))
1624 (save-match-data
1625 (let* ((lead (buffer-substring
1626 (point-at-bol) (match-beginning 0))))
1627 (setq v-i (mapconcat 'identity
1628 (org-split-string initial "\n")
1629 (concat "\n" lead))))))
1630 (replace-match (or (eval (intern (concat "v-" (match-string 1)))) "")
1631 t t)))
1633 ;; From the property list
1634 (when plist-p
1635 (goto-char (point-min))
1636 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
1637 (unless (org-capture-escaped-%)
1638 (and (setq x (or (plist-get org-store-link-plist
1639 (intern (match-string 1))) ""))
1640 (replace-match x t t)))))
1642 ;; %() embedded elisp
1643 (goto-char (point-min))
1644 (org-capture-expand-embedded-elisp)
1646 ;; Turn on org-mode in temp buffer, set local variables
1647 ;; This is to support completion in interactive prompts
1648 (let ((org-inhibit-startup t)) (org-mode))
1649 ;; Interactive template entries
1650 (goto-char (point-min))
1651 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
1652 (unless (org-capture-escaped-%)
1653 (setq char (if (match-end 3) (match-string-no-properties 3))
1654 prompt (if (match-end 2) (match-string-no-properties 2)))
1655 (goto-char (match-beginning 0))
1656 (replace-match "")
1657 (setq completions nil default nil)
1658 (when prompt
1659 (setq completions (org-split-string prompt "|")
1660 prompt (pop completions)
1661 default (car completions)
1662 histvar (intern (concat
1663 "org-capture-template-prompt-history::"
1664 (or prompt "")))
1665 completions (mapcar 'list completions)))
1666 (unless (boundp histvar) (set histvar nil))
1667 (cond
1668 ((member char '("G" "g"))
1669 (let* ((org-last-tags-completion-table
1670 (org-global-tags-completion-table
1671 (if (equal char "G")
1672 (org-agenda-files)
1673 (and file (list file)))))
1674 (org-add-colon-after-tag-completion t)
1675 (ins (org-icompleting-read
1676 (if prompt (concat prompt ": ") "Tags: ")
1677 'org-tags-completion-function nil nil nil
1678 'org-tags-history)))
1679 (setq ins (mapconcat 'identity
1680 (org-split-string
1681 ins (org-re "[^[:alnum:]_@#%]+"))
1682 ":"))
1683 (when (string-match "\\S-" ins)
1684 (or (equal (char-before) ?:) (insert ":"))
1685 (insert ins)
1686 (or (equal (char-after) ?:) (insert ":"))
1687 (and (org-at-heading-p)
1688 (let ((org-ignore-region t))
1689 (org-set-tags nil 'align))))))
1690 ((equal char "C")
1691 (cond ((= (length clipboards) 1) (insert (car clipboards)))
1692 ((> (length clipboards) 1)
1693 (insert (read-string "Clipboard/kill value: "
1694 (car clipboards) '(clipboards . 1)
1695 (car clipboards))))))
1696 ((equal char "L")
1697 (cond ((= (length clipboards) 1)
1698 (org-insert-link 0 (car clipboards)))
1699 ((> (length clipboards) 1)
1700 (org-insert-link 0 (read-string "Clipboard/kill value: "
1701 (car clipboards)
1702 '(clipboards . 1)
1703 (car clipboards))))))
1704 ((equal char "p")
1705 (org-set-property (org-no-properties prompt) nil))
1706 (char
1707 ;; These are the date/time related ones
1708 (setq org-time-was-given (equal (upcase char) char))
1709 (setq time (org-read-date (equal (upcase char) char) t nil
1710 prompt))
1711 (if (equal (upcase char) char) (setq org-time-was-given t))
1712 (org-insert-time-stamp time org-time-was-given
1713 (member char '("u" "U"))
1714 nil nil (list org-end-time-was-given)))
1716 (let (org-completion-use-ido)
1717 (push (org-completing-read-no-i
1718 (concat (if prompt prompt "Enter string")
1719 (if default (concat " [" default "]"))
1720 ": ")
1721 completions nil nil nil histvar default)
1722 strings)
1723 (insert (car strings)))))))
1724 ;; Replace %n escapes with nth %^{...} string
1725 (setq strings (nreverse strings))
1726 (goto-char (point-min))
1727 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
1728 (unless (org-capture-escaped-%)
1729 (replace-match
1730 (nth (1- (string-to-number (match-string 1))) strings)
1731 nil t)))
1732 ;; Make sure there are no empty lines before the text, and that
1733 ;; it ends with a newline character
1734 (goto-char (point-min))
1735 (while (looking-at "[ \t]*\n") (replace-match ""))
1736 (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
1737 ;; Return the expanded template and kill the temporary buffer
1738 (untabify (point-min) (point-max))
1739 (set-buffer-modified-p nil)
1740 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1742 (defun org-capture-escaped-% ()
1743 "Check if % was escaped - if yes, unescape it now."
1744 (if (equal (char-before (match-beginning 0)) ?\\)
1745 (progn
1746 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1748 nil))
1750 (defun org-capture-expand-embedded-elisp ()
1751 "Evaluate embedded elisp %(sexp) and replace with the result."
1752 (goto-char (point-min))
1753 (while (re-search-forward "%(" nil t)
1754 (unless (org-capture-escaped-%)
1755 (goto-char (match-beginning 0))
1756 (let ((template-start (point)))
1757 (forward-char 1)
1758 (let* ((sexp (read (current-buffer)))
1759 (result (org-eval
1760 (org-capture--expand-keyword-in-embedded-elisp sexp))))
1761 (delete-region template-start (point))
1762 (when result
1763 (if (stringp result)
1764 (insert result)
1765 (error "Capture template sexp `%s' must evaluate to string or nil"
1766 sexp))))))))
1768 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1769 "Recursively replace capture link keywords in ATTR sexp.
1770 Such keywords are prefixed with \"%:\". See
1771 `org-capture-template' for more information."
1772 (cond ((consp attr)
1773 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr))
1774 ((symbolp attr)
1775 (let* ((attr-symbol (symbol-name attr))
1776 (key (and (string-match "%\\(:.*\\)" attr-symbol)
1777 (intern (match-string 1 attr-symbol)))))
1778 (or (plist-get org-store-link-plist key)
1779 attr)))
1780 (t attr)))
1782 (defun org-capture-inside-embedded-elisp-p ()
1783 "Return non-nil if point is inside of embedded elisp %(sexp)."
1784 (let (beg end)
1785 (with-syntax-table emacs-lisp-mode-syntax-table
1786 (save-excursion
1787 ;; `looking-at' and `search-backward' below do not match the "%(" if
1788 ;; point is in its middle
1789 (when (equal (char-before) ?%)
1790 (backward-char))
1791 (save-match-data
1792 (when (or (looking-at "%(") (search-backward "%(" nil t))
1793 (setq beg (point))
1794 (setq end (progn (forward-char) (forward-sexp) (1- (point)))))))
1795 (when (and beg end)
1796 (and (<= (point) end) (>= (point) beg))))))
1798 ;;;###autoload
1799 (defun org-capture-import-remember-templates ()
1800 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1801 (interactive)
1802 (when (and (yes-or-no-p
1803 "Import old remember templates into org-capture-templates? ")
1804 (yes-or-no-p
1805 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1806 (require 'org-remember)
1807 (setq org-capture-templates
1808 (mapcar
1809 (lambda (entry)
1810 (let ((desc (car entry))
1811 (key (char-to-string (nth 1 entry)))
1812 (template (nth 2 entry))
1813 (file (or (nth 3 entry) org-default-notes-file))
1814 (position (or (nth 4 entry) org-remember-default-headline))
1815 (type 'entry)
1816 (prepend org-reverse-note-order)
1817 immediate target jump-to-captured)
1818 (cond
1819 ((member position '(top bottom))
1820 (setq target (list 'file file)
1821 prepend (eq position 'top)))
1822 ((eq position 'date-tree)
1823 (setq target (list 'file+datetree file)
1824 prepend nil))
1825 (t (setq target (list 'file+headline file position))))
1827 (when (string-match "%!" template)
1828 (setq template (replace-match "" t t template)
1829 immediate t))
1831 (when (string-match "%&" template)
1832 (setq jump-to-captured t))
1834 (append (list key desc type target template)
1835 (if prepend '(:prepend t))
1836 (if immediate '(:immediate-finish t))
1837 (if jump-to-captured '(:jump-to-captured t)))))
1839 org-remember-templates))))
1841 (provide 'org-capture)
1843 ;;; org-capture.el ends here