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