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