org-capture: Fix interactive template placeholders
[org-mode/org-tableheadings.git] / lisp / org-capture.el
blobe4dff77db0539451d825f0fcc328d73f156ffe4f
1 ;;; org-capture.el --- Fast note taking in Org-mode -*- lexical-binding: t; -*-
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 (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-pop-to-buffer-same-window "org-compat"
59 (&optional buffer-or-name norecord label))
60 (declare-function org-table-analyze "org-table" ())
61 (declare-function org-table-goto-line "org-table" (N))
63 (defvar org-end-time-was-given)
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 (defvar org-capture-is-refiling nil
81 "Non-nil when capture process is refiling an entry.")
83 (defgroup org-capture nil
84 "Options concerning capturing new entries."
85 :tag "Org Capture"
86 :group 'org)
88 (defcustom org-capture-templates nil
89 "Templates for the creation of new entries.
91 Each entry is a list with the following items:
93 keys The keys that will select the template, as a string, characters
94 only, for example \"a\" for a template to be selected with a
95 single key, or \"bt\" for selection with two keys. When using
96 several keys, keys using the same prefix key must be together
97 in the list and preceded by a 2-element entry explaining the
98 prefix key, for example
100 (\"b\" \"Templates for marking stuff to buy\")
102 The \"C\" key is used by default for quick access to the
103 customization of the template variable. But if you want to use
104 that key for a template, you can.
106 description A short string describing the template, will be shown during
107 selection.
109 type The type of entry. Valid types are:
110 entry an Org-mode node, with a headline. Will be
111 filed as the child of the target entry or as
112 a top-level entry.
113 item a plain list item, will be placed in the
114 first plain list at the target
115 location.
116 checkitem a checkbox item. This differs from the
117 plain list item only is so far as it uses a
118 different default template.
119 table-line a new line in the first table at target location.
120 plain text to be inserted as it is.
122 target Specification of where the captured item should be placed.
123 In Org-mode files, targets usually define a node. Entries will
124 become children of this node, other types will be added to the
125 table or list in the body of this node.
127 Most target specifications contain a file name. If that file
128 name is the empty string, it defaults to `org-default-notes-file'.
129 A file can also be given as a variable, function, or Emacs Lisp
130 form.
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 to find both
169 file and 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 %\\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 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 '(repeat
302 (choice :value ("" "" entry (file "~/org/notes.org") "")
303 (list :tag "Multikey description"
304 (string :tag "Keys ")
305 (string :tag "Description"))
306 (list :tag "Template entry"
307 (string :tag "Keys ")
308 (string :tag "Description ")
309 (choice :tag "Capture Type " :value entry
310 (const :tag "Org entry" entry)
311 (const :tag "Plain list item" item)
312 (const :tag "Checkbox item" checkitem)
313 (const :tag "Plain text" plain)
314 (const :tag "Table line" table-line))
315 (choice :tag "Target location"
316 (list :tag "File"
317 (const :format "" file)
318 (file :tag " File"))
319 (list :tag "ID"
320 (const :format "" id)
321 (string :tag " ID"))
322 (list :tag "File & Headline"
323 (const :format "" file+headline)
324 (file :tag " File ")
325 (string :tag " Headline"))
326 (list :tag "File & Outline path"
327 (const :format "" file+olp)
328 (file :tag " File ")
329 (repeat :tag "Outline path" :inline t
330 (string :tag "Headline")))
331 (list :tag "File & Regexp"
332 (const :format "" file+regexp)
333 (file :tag " File ")
334 (regexp :tag " Regexp"))
335 (list :tag "File & Date tree"
336 (const :format "" file+datetree)
337 (file :tag " File"))
338 (list :tag "File & Date tree, prompt for date"
339 (const :format "" file+datetree+prompt)
340 (file :tag " File"))
341 (list :tag "File & Week tree"
342 (const :format "" file+weektree)
343 (file :tag " File"))
344 (list :tag "File & Week tree, prompt for date"
345 (const :format "" file+weektree+prompt)
346 (file :tag " File"))
347 (list :tag "File & function"
348 (const :format "" file+function)
349 (file :tag " File ")
350 (sexp :tag " Function"))
351 (list :tag "Current clocking task"
352 (const :format "" clock))
353 (list :tag "Function"
354 (const :format "" function)
355 (sexp :tag " Function")))
356 (choice :tag "Template"
357 (string)
358 (list :tag "File"
359 (const :format "" file)
360 (file :tag "Template file"))
361 (list :tag "Function"
362 (const :format "" function)
363 (function :tag "Template function")))
364 (plist :inline t
365 ;; Give the most common options as checkboxes
366 :options (((const :format "%v " :prepend) (const t))
367 ((const :format "%v " :immediate-finish) (const t))
368 ((const :format "%v " :jump-to-captured) (const t))
369 ((const :format "%v " :empty-lines) (const 1))
370 ((const :format "%v " :empty-lines-before) (const 1))
371 ((const :format "%v " :empty-lines-after) (const 1))
372 ((const :format "%v " :clock-in) (const t))
373 ((const :format "%v " :clock-keep) (const t))
374 ((const :format "%v " :clock-resume) (const t))
375 ((const :format "%v " :unnarrowed) (const t))
376 ((const :format "%v " :table-line-pos) (const t))
377 ((const :format "%v " :kill-buffer) (const t))))))))
379 (defcustom org-capture-before-finalize-hook nil
380 "Hook that is run right before a capture process is finalized.
381 The capture buffer is still current when this hook runs and it is
382 widened to the entire buffer."
383 :group 'org-capture
384 :version "24.1"
385 :type 'hook)
387 (defcustom org-capture-after-finalize-hook nil
388 "Hook that is run right after a capture process is finalized.
389 Suitable for window cleanup."
390 :group 'org-capture
391 :version "24.1"
392 :type 'hook)
394 (defcustom org-capture-prepare-finalize-hook nil
395 "Hook that is run before the finalization starts.
396 The capture buffer is current and still narrowed."
397 :group 'org-capture
398 :version "24.1"
399 :type 'hook)
401 (defcustom org-capture-bookmark t
402 "When non-nil, add a bookmark pointing at the last stored
403 position when capturing."
404 :group 'org-capture
405 :version "24.3"
406 :type 'boolean)
408 ;;; The property list for keeping information about the capture process
410 (defvar org-capture-plist nil
411 "Plist for the current capture process, global, to avoid having to pass it.")
413 (defvar org-capture-current-plist nil
414 "Local variable holding the plist in a capture buffer.
415 This is used to store the plist for use when finishing a capture process
416 because another such process might have changed the global variable by then.
418 Each time a new capture buffer has been set up, the global `org-capture-plist'
419 is copied to this variable, which is local in the indirect buffer.")
421 (defvar org-capture-clock-keep nil
422 "Local variable to store the value of the :clock-keep parameter.
423 This is needed in case org-capture-finalize is called interactively.")
425 (defun org-capture-put (&rest stuff)
426 "Add properties to the capture property list `org-capture-plist'."
427 (while stuff
428 (setq org-capture-plist (plist-put org-capture-plist
429 (pop stuff) (pop stuff)))))
430 (defun org-capture-get (prop &optional local)
431 "Get properties from the capture property list `org-capture-plist'.
432 When LOCAL is set, use the local variable `org-capture-current-plist',
433 this is necessary after initialization of the capture process,
434 to avoid conflicts with other active capture processes."
435 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
437 (defun org-capture-member (prop &optional local)
438 "Is PROP a property in `org-capture-plist'.
439 When LOCAL is set, use the local variable `org-capture-current-plist',
440 this is necessary after initialization of the capture process,
441 to avoid conflicts with other active capture processes."
442 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
444 ;;; The minor mode
446 (defvar org-capture-mode-map (make-sparse-keymap)
447 "Keymap for `org-capture-mode', a minor mode.
448 Use this map to set additional keybindings for when Org-mode is used
449 for a capture buffer.")
451 (defvar org-capture-mode-hook nil
452 "Hook for the minor `org-capture-mode'.")
454 (define-minor-mode org-capture-mode
455 "Minor mode for special key bindings in a capture buffer.
457 Turning on this mode runs the normal hook `org-capture-mode-hook'."
458 nil " Rem" org-capture-mode-map
459 (setq-local
460 header-line-format
461 (substitute-command-keys
462 "\\<org-capture-mode-map>Capture buffer. Finish \\[org-capture-finalize], \
463 refile \\[org-capture-refile], abort \\[org-capture-kill].")))
464 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
465 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
466 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
468 ;;; The main commands
470 (defvar org-capture-initial nil)
471 (defvar org-capture-entry nil)
473 ;;;###autoload
474 (defun org-capture-string (string &optional keys)
475 "Capture STRING with the template selected by KEYS."
476 (interactive "sInitial text: \n")
477 (let ((org-capture-initial string)
478 (org-capture-entry (org-capture-select-template keys)))
479 (org-capture)))
481 (defcustom org-capture-templates-contexts nil
482 "Alist of capture templates and valid contexts.
484 For example, if you have a capture template \"c\" and you want
485 this template to be accessible only from `message-mode' buffers,
486 use this:
488 \\='((\"c\" ((in-mode . \"message-mode\"))))
490 Here are the available contexts definitions:
492 in-file: command displayed only in matching files
493 in-mode: command displayed only in matching modes
494 not-in-file: command not displayed in matching files
495 not-in-mode: command not displayed in matching modes
496 in-buffer: command displayed only in matching buffers
497 not-in-buffer: command not displayed in matching buffers
498 [function]: a custom function taking no argument
500 If you define several checks, the agenda command will be
501 accessible if there is at least one valid check.
503 You can also bind a key to another agenda custom command
504 depending on contextual rules.
506 \\='((\"c\" \"d\" ((in-mode . \"message-mode\"))))
508 Here it means: in `message-mode buffers', use \"c\" as the
509 key for the capture template otherwise associated with \"d\".
510 \(The template originally associated with \"d\" is not displayed
511 to avoid duplicates.)"
512 :version "24.3"
513 :group 'org-capture
514 :type '(repeat (list :tag "Rule"
515 (string :tag " Capture key")
516 (string :tag "Replace by template")
517 (repeat :tag "Available when"
518 (choice
519 (cons :tag "Condition"
520 (choice
521 (const :tag "In file" in-file)
522 (const :tag "Not in file" not-in-file)
523 (const :tag "In buffer" in-buffer)
524 (const :tag "Not in buffer" not-in-buffer)
525 (const :tag "In mode" in-mode)
526 (const :tag "Not in mode" not-in-mode))
527 (regexp))
528 (function :tag "Custom function"))))))
530 (defcustom org-capture-use-agenda-date nil
531 "Non-nil means use the date at point when capturing from agendas.
532 When nil, you can still capture using the date at point with \\[org-agenda-capture]."
533 :group 'org-capture
534 :version "24.3"
535 :type 'boolean)
537 ;;;###autoload
538 (defun org-capture (&optional goto keys)
539 "Capture something.
540 \\<org-capture-mode-map>
541 This will let you select a template from `org-capture-templates', and then
542 file the newly captured information. The text is immediately inserted
543 at the target location, and an indirect buffer is shown where you can
544 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
545 of Emacs, so that you can continue your work.
547 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
548 anything, just go to the file/headline where the selected template
549 stores its notes. With a double prefix argument \
550 \\[universal-argument] \\[universal-argument], go to the last note
551 stored.
553 When called with a `C-0' (zero) prefix, insert a template at point.
555 ELisp programs can set KEYS to a string associated with a template
556 in `org-capture-templates'. In this case, interactive selection
557 will be bypassed.
559 If `org-capture-use-agenda-date' is non-nil, capturing from the
560 agenda will use the date at point as the default date. Then, a
561 `C-1' prefix will tell the capture process to use the HH:MM time
562 of the day at point (if any) or the current HH:MM time."
563 (interactive "P")
564 (when (and org-capture-use-agenda-date
565 (eq major-mode 'org-agenda-mode))
566 (setq org-overriding-default-time
567 (org-get-cursor-date (equal goto 1))))
568 (cond
569 ((equal goto '(4)) (org-capture-goto-target))
570 ((equal goto '(16)) (org-capture-goto-last-stored))
572 ;; FIXME: Are these needed?
573 (let* ((orig-buf (current-buffer))
574 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
575 org-capture-link-is-already-stored)
576 (plist-get org-store-link-plist :annotation)
577 (ignore-errors (org-store-link nil))))
578 (entry (or org-capture-entry (org-capture-select-template keys)))
579 initial)
580 (setq initial (or org-capture-initial
581 (and (org-region-active-p)
582 (buffer-substring (point) (mark)))))
583 (when (stringp initial)
584 (remove-text-properties 0 (length initial) '(read-only t) initial))
585 (when (stringp annotation)
586 (remove-text-properties 0 (length annotation)
587 '(read-only t) annotation))
588 (cond
589 ((equal entry "C")
590 (customize-variable 'org-capture-templates))
591 ((equal entry "q")
592 (error "Abort"))
594 (org-capture-set-plist entry)
595 (org-capture-get-template)
596 (org-capture-put :original-buffer orig-buf
597 :original-file (or (buffer-file-name orig-buf)
598 (and (featurep 'dired)
599 (car (rassq orig-buf
600 dired-buffers))))
601 :original-file-nondirectory
602 (and (buffer-file-name orig-buf)
603 (file-name-nondirectory
604 (buffer-file-name orig-buf)))
605 :annotation annotation
606 :initial initial
607 :return-to-wconf (current-window-configuration)
608 :default-time
609 (or org-overriding-default-time
610 (org-current-time)))
611 (org-capture-set-target-location)
612 (condition-case error
613 (org-capture-put :template (org-capture-fill-template))
614 ((error quit)
615 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
616 (error "Capture abort: %s" error)))
618 (setq org-capture-clock-keep (org-capture-get :clock-keep))
619 (if (equal goto 0)
620 ;;insert at point
621 (org-capture-insert-template-here)
622 (condition-case error
623 (org-capture-place-template
624 (equal (car (org-capture-get :target)) 'function))
625 ((error quit)
626 (if (and (buffer-base-buffer (current-buffer))
627 (string-match "\\`CAPTURE-" (buffer-name)))
628 (kill-buffer (current-buffer)))
629 (set-window-configuration (org-capture-get :return-to-wconf))
630 (error "Capture template `%s': %s"
631 (org-capture-get :key)
632 (nth 1 error))))
633 (if (and (derived-mode-p 'org-mode)
634 (org-capture-get :clock-in))
635 (condition-case nil
636 (progn
637 (if (org-clock-is-active)
638 (org-capture-put :interrupted-clock
639 (copy-marker org-clock-marker)))
640 (org-clock-in)
641 (setq-local org-capture-clock-was-started t))
642 (error
643 "Could not start the clock in this capture buffer")))
644 (if (org-capture-get :immediate-finish)
645 (org-capture-finalize)))))))))
647 (defun org-capture-get-template ()
648 "Get the template from a file or a function if necessary."
649 (let ((txt (org-capture-get :template)) file)
650 (cond
651 ((and (listp txt) (eq (car txt) 'file))
652 (if (file-exists-p
653 (setq file (expand-file-name (nth 1 txt) org-directory)))
654 (setq txt (org-file-contents file))
655 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
656 ((and (listp txt) (eq (car txt) 'function))
657 (if (fboundp (nth 1 txt))
658 (setq txt (funcall (nth 1 txt)))
659 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
660 ((not txt) (setq txt ""))
661 ((stringp txt))
662 (t (setq txt "* Invalid capture template")))
663 (org-capture-put :template txt)))
665 (defun org-capture-finalize (&optional stay-with-capture)
666 "Finalize the capture process.
667 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
668 captured item after finalizing."
669 (interactive "P")
670 (when (org-capture-get :jump-to-captured)
671 (setq stay-with-capture t))
672 (unless (and org-capture-mode
673 (buffer-base-buffer (current-buffer)))
674 (error "This does not seem to be a capture buffer for Org-mode"))
676 (run-hooks 'org-capture-prepare-finalize-hook)
678 ;; Did we start the clock in this capture buffer?
679 (when (and org-capture-clock-was-started
680 org-clock-marker (marker-buffer org-clock-marker)
681 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
682 (> org-clock-marker (point-min))
683 (< org-clock-marker (point-max)))
684 ;; Looks like the clock we started is still running. Clock out.
685 (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
686 (when (and (not org-capture-clock-keep)
687 (org-capture-get :clock-resume 'local)
688 (markerp (org-capture-get :interrupted-clock 'local))
689 (buffer-live-p (marker-buffer
690 (org-capture-get :interrupted-clock 'local))))
691 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
692 (org-with-point-at clock-in-task
693 (org-clock-in)))
694 (message "Interrupted clock has been resumed")))
696 (let ((beg (point-min))
697 (end (point-max))
698 (abort-note nil))
699 ;; Store the size of the capture buffer
700 (org-capture-put :captured-entry-size (- (point-max) (point-min)))
701 (widen)
702 ;; Store the insertion point in the target buffer
703 (org-capture-put :insertion-point (point))
705 (if org-note-abort
706 (let ((m1 (org-capture-get :begin-marker 'local))
707 (m2 (org-capture-get :end-marker 'local)))
708 (if (and m1 m2 (= m1 beg) (= m2 end))
709 (progn
710 (setq m2 (if (cdr (assoc 'heading org-blank-before-new-entry))
711 m2 (1+ m2))
712 m2 (if (< (point-max) m2) (point-max) m2))
713 (setq abort-note 'clean)
714 (kill-region m1 m2))
715 (setq abort-note 'dirty)))
717 ;; Make sure that the empty lines after are correct
718 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
719 (member (org-capture-get :type 'local)
720 '(entry item checkitem plain)))
721 (save-excursion
722 (goto-char end)
723 (or (bolp) (newline))
724 (org-capture-empty-lines-after
725 (or (org-capture-get :empty-lines-after 'local)
726 (org-capture-get :empty-lines 'local) 0))))
727 ;; Postprocessing: Update Statistics cookies, do the sorting
728 (when (derived-mode-p 'org-mode)
729 (save-excursion
730 (when (ignore-errors (org-back-to-heading))
731 (org-update-parent-todo-statistics)
732 (org-update-checkbox-count)))
733 ;; FIXME Here we should do the sorting
734 ;; If we have added a table line, maybe recompute?
735 (when (and (eq (org-capture-get :type 'local) 'table-line)
736 (org-at-table-p))
737 (if (org-table-get-stored-formulas)
738 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
739 (org-table-align))))
740 ;; Store this place as the last one where we stored something
741 ;; Do the marking in the base buffer, so that it makes sense after
742 ;; the indirect buffer has been killed.
743 (when org-capture-bookmark
744 (org-capture-bookmark-last-stored-position))
746 ;; Run the hook
747 (run-hooks 'org-capture-before-finalize-hook))
749 (when (org-capture-get :decrypted)
750 (save-excursion
751 (goto-char (org-capture-get :decrypted))
752 (org-encrypt-entry)))
754 ;; Kill the indirect buffer
755 (save-buffer)
756 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
757 (new-buffer (org-capture-get :new-buffer 'local))
758 (kill-buffer (org-capture-get :kill-buffer 'local))
759 (base-buffer (buffer-base-buffer (current-buffer))))
761 ;; Kill the indirect buffer
762 (kill-buffer (current-buffer))
764 ;; Narrow back the target buffer to its previous state
765 (with-current-buffer (org-capture-get :buffer)
766 (let ((reg (org-capture-get :initial-target-region))
767 (pos (org-capture-get :initial-target-position))
768 (ipt (org-capture-get :insertion-point))
769 (size (org-capture-get :captured-entry-size)))
770 (if (not reg)
771 (widen)
772 (cond ((< ipt (car reg))
773 ;; insertion point is before the narrowed region
774 (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
775 ((> ipt (cdr reg))
776 ;; insertion point is after the narrowed region
777 (narrow-to-region (car reg) (cdr reg)))
779 ;; insertion point is within the narrowed region
780 (narrow-to-region (car reg) (+ size (cdr reg)))))
781 ;; now place back the point at its original position
782 (if (< ipt (car reg))
783 (goto-char (+ size pos))
784 (goto-char (if (< ipt pos) (+ size pos) pos))))))
786 ;; Kill the target buffer if that is desired
787 (when (and base-buffer new-buffer kill-buffer)
788 (with-current-buffer base-buffer (save-buffer))
789 (kill-buffer base-buffer))
791 ;; Restore the window configuration before capture
792 (set-window-configuration return-wconf))
794 (run-hooks 'org-capture-after-finalize-hook)
795 ;; Special cases
796 (cond
797 (abort-note
798 (cond
799 ((equal abort-note 'clean)
800 (message "Capture process aborted and target buffer cleaned up"))
801 ((equal abort-note 'dirty)
802 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
803 (stay-with-capture
804 (org-capture-goto-last-stored)))
805 ;; Return if we did store something
806 (not abort-note)))
808 (defun org-capture-refile ()
809 "Finalize the current capture and then refile the entry.
810 Refiling is done from the base buffer, because the indirect buffer is then
811 already gone. Any prefix argument will be passed to the refile command."
812 (interactive)
813 (unless (eq (org-capture-get :type 'local) 'entry)
814 (error
815 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
816 (let ((pos (point))
817 (base (buffer-base-buffer (current-buffer)))
818 (org-capture-is-refiling t)
819 (kill-buffer (org-capture-get :kill-buffer 'local)))
820 (org-capture-put :kill-buffer nil)
821 (org-capture-finalize)
822 (save-window-excursion
823 (with-current-buffer (or base (current-buffer))
824 (save-excursion
825 (save-restriction
826 (widen)
827 (goto-char pos)
828 (call-interactively 'org-refile)))))
829 (when kill-buffer (kill-buffer base))))
831 (defun org-capture-kill ()
832 "Abort the current capture process."
833 (interactive)
834 ;; FIXME: This does not do the right thing, we need to remove the
835 ;; new stuff by hand it is easy: undo, then kill the buffer
836 (let ((org-note-abort t)
837 (org-capture-before-finalize-hook nil))
838 (org-capture-finalize)))
840 (defun org-capture-goto-last-stored ()
841 "Go to the location where the last capture note was stored."
842 (interactive)
843 (org-goto-marker-or-bmk org-capture-last-stored-marker
844 (plist-get org-bookmark-names-plist
845 :last-capture))
846 (message "This is the last note stored by a capture process"))
848 ;;; Supporting functions for handling the process
850 (defun org-capture-put-target-region-and-position ()
851 "Store the initial region with `org-capture-put'."
852 (org-capture-put
853 :initial-target-region
854 ;; Check if the buffer is currently narrowed
855 (when (org-buffer-narrowed-p)
856 (cons (point-min) (point-max))))
857 ;; store the current point
858 (org-capture-put :initial-target-position (point)))
860 (defvar org-time-was-given) ; dynamically scoped parameter
861 (defun org-capture-set-target-location (&optional target)
862 "Find TARGET buffer and position.
863 Store them in the capture property list."
864 (let ((target-entry-p t) decrypted-hl-pos)
865 (setq target (or target (org-capture-get :target)))
866 (save-excursion
867 (cond
868 ((eq (car target) 'file)
869 (set-buffer (org-capture-target-buffer (nth 1 target)))
870 (org-capture-put-target-region-and-position)
871 (widen)
872 (setq target-entry-p nil))
874 ((eq (car target) 'id)
875 (let ((loc (org-id-find (nth 1 target))))
876 (if (not loc)
877 (error "Cannot find target ID \"%s\"" (nth 1 target))
878 (set-buffer (org-capture-target-buffer (car loc)))
879 (widen)
880 (org-capture-put-target-region-and-position)
881 (goto-char (cdr loc)))))
883 ((eq (car target) 'file+headline)
884 (set-buffer (org-capture-target-buffer (nth 1 target)))
885 (org-capture-put-target-region-and-position)
886 (widen)
887 (let ((hd (nth 2 target)))
888 (goto-char (point-min))
889 (unless (derived-mode-p 'org-mode)
890 (error
891 "Target buffer \"%s\" for file+headline should be in Org mode"
892 (current-buffer)))
893 (if (re-search-forward
894 (format org-complex-heading-regexp-format (regexp-quote hd))
895 nil t)
896 (goto-char (point-at-bol))
897 (goto-char (point-max))
898 (or (bolp) (insert "\n"))
899 (insert "* " hd "\n")
900 (beginning-of-line 0))))
902 ((eq (car target) 'file+olp)
903 (let ((m (org-find-olp
904 (cons (org-capture-expand-file (nth 1 target))
905 (cddr target)))))
906 (set-buffer (marker-buffer m))
907 (org-capture-put-target-region-and-position)
908 (widen)
909 (goto-char m)))
911 ((eq (car target) 'file+regexp)
912 (set-buffer (org-capture-target-buffer (nth 1 target)))
913 (org-capture-put-target-region-and-position)
914 (widen)
915 (goto-char (point-min))
916 (if (re-search-forward (nth 2 target) nil t)
917 (progn
918 (goto-char (if (org-capture-get :prepend)
919 (match-beginning 0) (match-end 0)))
920 (org-capture-put :exact-position (point))
921 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
922 (error "No match for target regexp in file %s" (nth 1 target))))
924 ((memq (car target) '(file+datetree file+datetree+prompt file+weektree file+weektree+prompt))
925 (require 'org-datetree)
926 (set-buffer (org-capture-target-buffer (nth 1 target)))
927 (org-capture-put-target-region-and-position)
928 (widen)
929 ;; Make a date/week tree entry, with the current date (or
930 ;; yesterday, if we are extending dates for a couple of hours)
931 (funcall
932 (cond
933 ((memq (car target) '(file+weektree file+weektree+prompt))
934 #'org-datetree-find-iso-week-create)
935 (t #'org-datetree-find-date-create))
936 (calendar-gregorian-from-absolute
937 (cond
938 (org-overriding-default-time
939 ;; use the overriding default time
940 (time-to-days org-overriding-default-time))
942 ((memq (car target) '(file+datetree+prompt file+weektree+prompt))
943 ;; prompt for date
944 (let ((prompt-time (org-read-date
945 nil t nil "Date for tree entry:"
946 (current-time))))
947 (org-capture-put
948 :default-time
949 (cond ((and (or (not (boundp 'org-time-was-given))
950 (not org-time-was-given))
951 (not (= (time-to-days prompt-time) (org-today))))
952 ;; Use 00:00 when no time is given for another date than today?
953 (apply 'encode-time (append '(0 0 0) (cdddr (decode-time prompt-time)))))
954 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer)
955 ;; Replace any time range by its start
956 (apply 'encode-time
957 (org-read-date-analyze
958 (replace-match "\\1 \\2" nil nil org-read-date-final-answer)
959 prompt-time (decode-time prompt-time))))
960 (t prompt-time)))
961 (time-to-days prompt-time)))
963 ;; current date, possibly corrected for late night workers
964 (org-today))))))
966 ((eq (car target) 'file+function)
967 (set-buffer (org-capture-target-buffer (nth 1 target)))
968 (org-capture-put-target-region-and-position)
969 (widen)
970 (funcall (nth 2 target))
971 (org-capture-put :exact-position (point))
972 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
974 ((eq (car target) 'function)
975 (funcall (nth 1 target))
976 (org-capture-put :exact-position (point))
977 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
979 ((eq (car target) 'clock)
980 (if (and (markerp org-clock-hd-marker)
981 (marker-buffer org-clock-hd-marker))
982 (progn (set-buffer (marker-buffer org-clock-hd-marker))
983 (org-capture-put-target-region-and-position)
984 (widen)
985 (goto-char org-clock-hd-marker))
986 (error "No running clock that could be used as capture target")))
988 (t (error "Invalid capture target specification")))
990 (when (and (featurep 'org-crypt) (org-at-encrypted-entry-p))
991 (org-decrypt-entry)
992 (setq decrypted-hl-pos
993 (save-excursion (and (org-back-to-heading t) (point)))))
995 (org-capture-put :buffer (current-buffer) :pos (point)
996 :target-entry-p target-entry-p
997 :decrypted decrypted-hl-pos))))
999 (defun org-capture-expand-file (file)
1000 "Expand functions and symbols for FILE.
1001 When FILE is a function, call it. When it is a form, evaluate
1002 it. When it is a variable, retrieve the value. When it is
1003 a string, return it. However, if it is the empty string, return
1004 `org-default-notes-file' instead."
1005 (cond
1006 ((equal file "") org-default-notes-file)
1007 ((org-string-nw-p file) file)
1008 ((functionp file) (funcall file))
1009 ((and (symbolp file) (boundp file)) (symbol-value file))
1010 ((consp file) (eval file))
1011 (t file)))
1013 (defun org-capture-target-buffer (file)
1014 "Get a buffer for FILE."
1015 (setq file (org-capture-expand-file file))
1016 (setq file (or (org-string-nw-p file)
1017 org-default-notes-file
1018 (error "No notes file specified, and no default available")))
1019 (or (org-find-base-buffer-visiting file)
1020 (progn (org-capture-put :new-buffer t)
1021 (find-file-noselect (expand-file-name file org-directory)))))
1023 (defun org-capture-place-template (&optional inhibit-wconf-store)
1024 "Insert the template at the target location, and display the buffer.
1025 When `inhibit-wconf-store', don't store the window configuration, as it
1026 may have been stored before."
1027 (unless inhibit-wconf-store
1028 (org-capture-put :return-to-wconf (current-window-configuration)))
1029 (delete-other-windows)
1030 (org-switch-to-buffer-other-window
1031 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
1032 (widen)
1033 (outline-show-all)
1034 (goto-char (org-capture-get :pos))
1035 (setq-local outline-level 'org-outline-level)
1036 (pcase (org-capture-get :type)
1037 ((or `nil `entry) (org-capture-place-entry))
1038 (`table-line (org-capture-place-table-line))
1039 (`plain (org-capture-place-plain-text))
1040 (`item (org-capture-place-item))
1041 (`checkitem (org-capture-place-item)))
1042 (org-capture-mode 1)
1043 (setq-local org-capture-current-plist org-capture-plist))
1045 (defun org-capture-place-entry ()
1046 "Place the template as a new Org entry."
1047 (let* ((txt (org-capture-get :template))
1048 (reversed (org-capture-get :prepend))
1049 (target-entry-p (org-capture-get :target-entry-p))
1050 level beg end)
1052 (and (org-capture-get :exact-position)
1053 (goto-char (org-capture-get :exact-position)))
1054 (cond
1055 ((not target-entry-p)
1056 ;; Insert as top-level entry, either at beginning or at end of
1057 ;; file.
1058 (setq level 1)
1059 (if reversed
1060 (progn (goto-char (point-min))
1061 (or (org-at-heading-p)
1062 (outline-next-heading)))
1063 (goto-char (point-max))
1064 (or (bolp) (insert "\n"))))
1066 ;; Insert as a child of the current entry
1067 (and (looking-at "\\*+")
1068 (setq level (- (match-end 0) (match-beginning 0))))
1069 (setq level (org-get-valid-level (or level 1) 1))
1070 (if reversed
1071 (progn
1072 (outline-next-heading)
1073 (or (bolp) (insert "\n")))
1074 (org-end-of-subtree t nil)
1075 (or (bolp) (insert "\n")))))
1076 (org-capture-empty-lines-before)
1077 (setq beg (point))
1078 (org-capture-verify-tree txt)
1079 (org-paste-subtree level txt 'for-yank)
1080 (org-capture-empty-lines-after 1)
1081 (org-capture-position-for-last-stored beg)
1082 (outline-next-heading)
1083 (setq end (point))
1084 (org-capture-mark-kill-region beg (1- end))
1085 (org-capture-narrow beg (1- end))
1086 (if (or (re-search-backward "%\\?" beg t)
1087 (re-search-forward "%\\?" end t))
1088 (replace-match ""))))
1090 (defun org-capture-place-item ()
1091 "Place the template as a new plain list item."
1092 (let* ((txt (org-capture-get :template))
1093 (target-entry-p (org-capture-get :target-entry-p))
1094 (ind 0)
1095 beg end)
1096 (if (org-capture-get :exact-position)
1097 (goto-char (org-capture-get :exact-position))
1098 (cond
1099 ((not target-entry-p)
1100 ;; Insert as top-level entry, either at beginning or at end of file
1101 (setq beg (point-min) end (point-max)))
1103 (setq beg (1+ (point-at-eol))
1104 end (save-excursion (outline-next-heading) (point)))))
1105 (setq ind nil)
1106 (if (org-capture-get :prepend)
1107 (progn
1108 (goto-char beg)
1109 (when (org-list-search-forward (org-item-beginning-re) end t)
1110 (goto-char (match-beginning 0))
1111 (setq ind (org-get-indentation))))
1112 (goto-char end)
1113 (when (org-list-search-backward (org-item-beginning-re) beg t)
1114 (setq ind (org-get-indentation))
1115 (org-end-of-item)))
1116 (unless ind (goto-char end)))
1117 ;; Remove common indentation
1118 (setq txt (org-remove-indentation txt))
1119 ;; Make sure this is indeed an item
1120 (unless (string-match (concat "\\`" (org-item-re)) txt)
1121 (setq txt (concat "- "
1122 (mapconcat 'identity (split-string txt "\n")
1123 "\n "))))
1124 ;; Prepare surrounding empty lines.
1125 (org-capture-empty-lines-before)
1126 (setq beg (point))
1127 (unless (eolp) (save-excursion (insert "\n")))
1128 (unless ind
1129 (org-indent-line)
1130 (setq ind (org-get-indentation))
1131 (delete-region beg (point)))
1132 ;; Set the correct indentation, depending on context
1133 (setq ind (make-string ind ?\ ))
1134 (setq txt (concat ind
1135 (mapconcat 'identity (split-string txt "\n")
1136 (concat "\n" ind))
1137 "\n"))
1138 ;; Insert item.
1139 (insert txt)
1140 (org-capture-empty-lines-after 1)
1141 (org-capture-position-for-last-stored beg)
1142 (forward-char 1)
1143 (setq end (point))
1144 (org-capture-mark-kill-region beg (1- end))
1145 (org-capture-narrow beg (1- end))
1146 (if (or (re-search-backward "%\\?" beg t)
1147 (re-search-forward "%\\?" end t))
1148 (replace-match ""))))
1150 (defun org-capture-place-table-line ()
1151 "Place the template as a table line."
1152 (require 'org-table)
1153 (let* ((txt (org-capture-get :template))
1154 (target-entry-p (org-capture-get :target-entry-p))
1155 (table-line-pos (org-capture-get :table-line-pos))
1156 beg end)
1157 (cond
1158 ((org-capture-get :exact-position)
1159 (goto-char (org-capture-get :exact-position)))
1160 ((not target-entry-p)
1161 ;; Table is not necessarily under a heading
1162 (setq beg (point-min) end (point-max)))
1164 ;; WE are at a heading, limit search to the body
1165 (setq beg (1+ (point-at-eol))
1166 end (save-excursion (outline-next-heading) (point)))))
1167 (if (re-search-forward org-table-dataline-regexp end t)
1168 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t))
1169 (goto-char e)
1170 (if (looking-at "[ \t]*#\\+tblfm:")
1171 (forward-line 1))
1172 (narrow-to-region b (point)))
1173 (goto-char end)
1174 (insert "\n| |\n|----|\n| |\n")
1175 (narrow-to-region (1+ end) (point)))
1176 ;; We are narrowed to the table, or to an empty line if there was no table
1178 ;; Check if the template is good
1179 (if (not (string-match org-table-dataline-regexp txt))
1180 (setq txt "| %?Bad template |\n"))
1181 (if (functionp table-line-pos)
1182 (setq table-line-pos (funcall table-line-pos))
1183 (setq table-line-pos (eval table-line-pos)))
1184 (cond
1185 ((and table-line-pos
1186 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
1187 ;; we have a complex line specification
1188 (let ((ll (ignore-errors
1189 (save-match-data (org-table-analyze))
1190 (aref org-table-hlines
1191 (- (match-end 1) (match-beginning 1)))))
1192 (delta (string-to-number (match-string 2 table-line-pos))))
1193 ;; The user wants a special position in the table
1194 (unless ll
1195 (error "Invalid table line specification \"%s\"" table-line-pos))
1196 (goto-char org-table-current-begin-pos)
1197 (forward-line (+ ll delta (if (< delta 0) 0 -1)))
1198 (org-table-insert-row 'below)
1199 (beginning-of-line 1)
1200 (delete-region (point) (1+ (point-at-eol)))
1201 (setq beg (point))
1202 (insert txt)
1203 (setq end (point))))
1204 ((org-capture-get :prepend)
1205 (goto-char (point-min))
1206 (re-search-forward org-table-hline-regexp nil t)
1207 (beginning-of-line 1)
1208 (re-search-forward org-table-dataline-regexp nil t)
1209 (beginning-of-line 1)
1210 (setq beg (point))
1211 (org-table-insert-row)
1212 (beginning-of-line 1)
1213 (delete-region (point) (1+ (point-at-eol)))
1214 (insert txt)
1215 (setq end (point)))
1217 (goto-char (point-max))
1218 (re-search-backward org-table-dataline-regexp nil t)
1219 (beginning-of-line 1)
1220 (org-table-insert-row 'below)
1221 (beginning-of-line 1)
1222 (delete-region (point) (1+ (point-at-eol)))
1223 (setq beg (point))
1224 (insert txt)
1225 (setq end (point))))
1226 (goto-char beg)
1227 (org-capture-position-for-last-stored 'table-line)
1228 (if (or (re-search-backward "%\\?" beg t)
1229 (re-search-forward "%\\?" end t))
1230 (replace-match ""))
1231 (org-table-align)))
1233 (defun org-capture-place-plain-text ()
1234 "Place the template plainly.
1235 If the target locator points at an Org node, place the template into
1236 the text of the entry, before the first child. If not, place the
1237 template at the beginning or end of the file.
1238 Of course, if exact position has been required, just put it there."
1239 (let* ((txt (org-capture-get :template))
1240 beg end)
1241 (cond
1242 ((org-capture-get :exact-position)
1243 (goto-char (org-capture-get :exact-position)))
1244 ((and (org-capture-get :target-entry-p)
1245 (bolp)
1246 (looking-at org-outline-regexp))
1247 ;; we should place the text into this entry
1248 (if (org-capture-get :prepend)
1249 ;; Skip meta data and drawers
1250 (org-end-of-meta-data t)
1251 ;; go to ent of the entry text, before the next headline
1252 (outline-next-heading)))
1254 ;; beginning or end of file
1255 (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
1256 (or (bolp) (newline))
1257 (org-capture-empty-lines-before)
1258 (setq beg (point))
1259 (insert txt)
1260 (org-capture-empty-lines-after 1)
1261 (org-capture-position-for-last-stored beg)
1262 (setq end (point))
1263 (org-capture-mark-kill-region beg (1- end))
1264 (org-capture-narrow beg (1- end))
1265 (if (or (re-search-backward "%\\?" beg t)
1266 (re-search-forward "%\\?" end t))
1267 (replace-match ""))))
1269 (defun org-capture-mark-kill-region (beg end)
1270 "Mark the region that will have to be killed when aborting capture."
1271 (let ((m1 (move-marker (make-marker) beg))
1272 (m2 (move-marker (make-marker) end)))
1273 (org-capture-put :begin-marker m1)
1274 (org-capture-put :end-marker m2)))
1276 (defun org-capture-position-for-last-stored (where)
1277 "Memorize the position that should later become the position of last capture."
1278 (cond
1279 ((integerp where)
1280 (org-capture-put :position-for-last-stored
1281 (move-marker (make-marker) where
1282 (or (buffer-base-buffer (current-buffer))
1283 (current-buffer)))))
1284 ((eq where 'table-line)
1285 (org-capture-put :position-for-last-stored
1286 (list 'table-line
1287 (org-table-current-dline))))
1288 (t (error "This should not happen"))))
1290 (defun org-capture-bookmark-last-stored-position ()
1291 "Bookmark the last-captured position."
1292 (let* ((where (org-capture-get :position-for-last-stored 'local))
1293 (pos (cond
1294 ((markerp where)
1295 (prog1 (marker-position where)
1296 (move-marker where nil)))
1297 ((and (listp where) (eq (car where) 'table-line))
1298 (if (org-at-table-p)
1299 (save-excursion
1300 (org-table-goto-line (nth 1 where))
1301 (point-at-bol))
1302 (point))))))
1303 (with-current-buffer (buffer-base-buffer (current-buffer))
1304 (save-excursion
1305 (save-restriction
1306 (widen)
1307 (goto-char pos)
1308 (let ((bookmark-name (plist-get org-bookmark-names-plist
1309 :last-capture)))
1310 (when bookmark-name
1311 (with-demoted-errors
1312 (bookmark-set bookmark-name))))
1313 (move-marker org-capture-last-stored-marker (point)))))))
1315 (defun org-capture-narrow (beg end)
1316 "Narrow, unless configuration says not to narrow."
1317 (unless (org-capture-get :unnarrowed)
1318 (narrow-to-region beg end)
1319 (goto-char beg)))
1321 (defun org-capture-empty-lines-before (&optional n)
1322 "Set the correct number of empty lines before the insertion point.
1323 Point will be after the empty lines, so insertion can directly be done."
1324 (setq n (or n (org-capture-get :empty-lines-before)
1325 (org-capture-get :empty-lines) 0))
1326 (let ((pos (point)))
1327 (org-back-over-empty-lines)
1328 (delete-region (point) pos)
1329 (if (> n 0) (newline n))))
1331 (defun org-capture-empty-lines-after (&optional n)
1332 "Set the correct number of empty lines after the inserted string.
1333 Point will remain at the first line after the inserted text."
1334 (setq n (or n (org-capture-get :empty-lines-after)
1335 (org-capture-get :empty-lines) 0))
1336 (org-back-over-empty-lines)
1337 (while (looking-at "[ \t]*\n") (replace-match ""))
1338 (let ((pos (point)))
1339 (if (> n 0) (newline n))
1340 (goto-char pos)))
1342 (defvar org-clock-marker) ; Defined in org.el
1344 (defun org-capture-insert-template-here ()
1345 "Insert the capture template at point."
1346 (let* ((template (org-capture-get :template))
1347 (type (org-capture-get :type))
1348 beg end pp)
1349 (or (bolp) (newline))
1350 (setq beg (point))
1351 (cond
1352 ((and (eq type 'entry) (derived-mode-p 'org-mode))
1353 (org-capture-verify-tree (org-capture-get :template))
1354 (org-paste-subtree nil template t))
1355 ((and (memq type '(item checkitem))
1356 (derived-mode-p 'org-mode)
1357 (save-excursion (skip-chars-backward " \t\n")
1358 (setq pp (point))
1359 (org-in-item-p)))
1360 (goto-char pp)
1361 (org-insert-item)
1362 (skip-chars-backward " ")
1363 (skip-chars-backward "-+*0123456789).")
1364 (delete-region (point) (point-at-eol))
1365 (setq beg (point))
1366 (org-remove-indentation template)
1367 (insert template)
1368 (org-capture-empty-lines-after)
1369 (goto-char beg)
1370 (org-list-repair)
1371 (org-end-of-item)
1372 (setq end (point)))
1373 (t (insert template)))
1374 (setq end (point))
1375 (goto-char beg)
1376 (if (re-search-forward "%\\?" end t)
1377 (replace-match ""))))
1379 (defun org-capture-set-plist (entry)
1380 "Initialize the property list from the template definition."
1381 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1382 (org-capture-put :key (car entry) :description (nth 1 entry)
1383 :target (nth 3 entry))
1384 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1385 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1386 ;; The template may be empty or omitted for special types.
1387 ;; Here we insert the default templates for such cases.
1388 (cond
1389 ((eq type 'item) (setq txt "- %?"))
1390 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1391 ((eq type 'table-line) (setq txt "| %? |"))
1392 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1393 (org-capture-put :template txt :type type)))
1395 (defun org-capture-goto-target (&optional template-key)
1396 "Go to the target location of a capture template.
1397 The user is queried for the template."
1398 (interactive)
1399 (let ((entry (org-capture-select-template template-key)))
1400 (unless entry (error "No capture template selected"))
1401 (org-capture-set-plist entry)
1402 (org-capture-set-target-location)
1403 (org-pop-to-buffer-same-window (org-capture-get :buffer))
1404 (goto-char (org-capture-get :pos))))
1406 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1407 "Make an indirect buffer for a capture process.
1408 Use PREFIX as a prefix for the name of the indirect buffer."
1409 (setq buffer (or buffer (current-buffer)))
1410 (let ((n 1) (base (buffer-name buffer)) bname)
1411 (setq bname (concat prefix "-" base))
1412 (while (buffer-live-p (get-buffer bname))
1413 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1414 (condition-case nil
1415 (make-indirect-buffer buffer bname 'clone)
1416 (error
1417 (let ((buf (make-indirect-buffer buffer bname)))
1418 (with-current-buffer buf (org-mode))
1419 buf)))))
1421 (defun org-capture-verify-tree (tree)
1422 "Throw error if TREE is not a valid tree."
1423 (unless (org-kill-is-subtree-p tree)
1424 (error "Template is not a valid Org entry or tree")))
1426 (defun org-mks (table title &optional prompt specials)
1427 "Select a member of an alist with multiple keys.
1428 TABLE is the alist which should contain entries where the car is a string.
1429 There should be two types of entries.
1431 1. prefix descriptions like (\"a\" \"Description\")
1432 This indicates that `a' is a prefix key for multi-letter selection, and
1433 that there are entries following with keys like \"ab\", \"ax\"...
1435 2. Selectable members must have more than two elements, with the first
1436 being the string of keys that lead to selecting it, and the second a
1437 short description string of the item.
1439 The command will then make a temporary buffer listing all entries
1440 that can be selected with a single key, and all the single key
1441 prefixes. When you press the key for a single-letter entry, it is selected.
1442 When you press a prefix key, the commands (and maybe further prefixes)
1443 under this key will be shown and offered for selection.
1445 TITLE will be placed over the selection in the temporary buffer,
1446 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1447 also (\"key\" \"description\") entries. When one of these is selection,
1448 only the bare key is returned."
1449 (setq prompt (or prompt "Select: "))
1450 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1451 current prefix rtn re pressed buffer (inhibit-quit t))
1452 (save-window-excursion
1453 (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
1454 (setq orig-table table)
1455 (catch 'exit
1456 (while t
1457 (erase-buffer)
1458 (insert title "\n\n")
1459 (setq tbl table
1460 des-keys nil
1461 allowed-keys nil
1462 cursor-type nil)
1463 (setq prefix (if current (concat current " ") ""))
1464 (while tbl
1465 (cond
1466 ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
1467 ;; This is a description on this level
1468 (setq dkey (caar tbl) ddesc (cadar tbl))
1469 (pop tbl)
1470 (push dkey des-keys)
1471 (push dkey allowed-keys)
1472 (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
1473 ;; Skip keys which are below this prefix
1474 (setq re (concat "\\`" (regexp-quote dkey)))
1475 (let (case-fold-search)
1476 (while (and tbl (string-match re (caar tbl))) (pop tbl))))
1477 ((= 2 (length (car tbl)))
1478 ;; Not yet a usable description, skip it
1481 ;; usable entry on this level
1482 (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
1483 (push (caar tbl) allowed-keys)
1484 (pop tbl))))
1485 (when specials
1486 (insert "-------------------------------------------------------------------------------\n")
1487 (let ((sp specials))
1488 (while sp
1489 (insert (format "[%s] %s\n"
1490 (caar sp) (nth 1 (car sp))))
1491 (push (caar sp) allowed-keys)
1492 (pop sp))))
1493 (push "\C-g" allowed-keys)
1494 (goto-char (point-min))
1495 (if (not (pos-visible-in-window-p (point-max)))
1496 (org-fit-window-to-buffer))
1497 (message prompt)
1498 (setq pressed (char-to-string (read-char-exclusive)))
1499 (while (not (member pressed allowed-keys))
1500 (message "Invalid key `%s'" pressed) (sit-for 1)
1501 (message prompt)
1502 (setq pressed (char-to-string (read-char-exclusive))))
1503 (when (equal pressed "\C-g")
1504 (kill-buffer buffer)
1505 (error "Abort"))
1506 (when (and (not (assoc pressed table))
1507 (not (member pressed des-keys))
1508 (assoc pressed specials))
1509 (throw 'exit (setq rtn pressed)))
1510 (unless (member pressed des-keys)
1511 (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
1512 orig-table))))
1513 (setq current (concat current pressed))
1514 (setq table (mapcar
1515 (lambda (x)
1516 (if (and (> (length (car x)) 1)
1517 (equal (substring (car x) 0 1) pressed))
1518 (cons (substring (car x) 1) (cdr x))
1519 nil))
1520 table))
1521 (setq table (remove nil table)))))
1522 (when buffer (kill-buffer buffer))
1523 rtn))
1525 ;;; The template code
1526 (defun org-capture-select-template (&optional keys)
1527 "Select a capture template.
1528 Lisp programs can force the template by setting KEYS to a string."
1529 (let ((org-capture-templates
1530 (or (org-contextualize-keys
1531 org-capture-templates org-capture-templates-contexts)
1532 '(("t" "Task" entry (file+headline "" "Tasks")
1533 "* TODO %?\n %u\n %a")))))
1534 (if keys
1535 (or (assoc keys org-capture-templates)
1536 (error "No capture template referred to by \"%s\" keys" keys))
1537 (org-mks org-capture-templates
1538 "Select a capture template\n========================="
1539 "Template key: "
1540 '(("C" "Customize org-capture-templates")
1541 ("q" "Abort"))))))
1543 (defun org-capture-fill-template (&optional template initial annotation)
1544 "Fill a template and return the filled template as a string.
1545 The template may still contain \"%?\" for cursor positioning."
1546 (let* ((template (or template (org-capture-get :template)))
1547 (buffer (org-capture-get :buffer))
1548 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1549 (time (let* ((c (or (org-capture-get :default-time) (current-time)))
1550 (d (decode-time c)))
1551 (if (< (nth 2 d) org-extend-today-until)
1552 (encode-time 0 59 23 (1- (nth 3 d)) (nth 4 d) (nth 5 d))
1553 c)))
1554 (v-t (format-time-string (org-time-stamp-format nil) time))
1555 (v-T (format-time-string (org-time-stamp-format t) time))
1556 (v-u (format-time-string (org-time-stamp-format nil t) time))
1557 (v-U (format-time-string (org-time-stamp-format t t) time))
1558 (v-c (and kill-ring (current-kill 0)))
1559 (v-x (or (org-get-x-clipboard 'PRIMARY)
1560 (org-get-x-clipboard 'CLIPBOARD)
1561 (org-get-x-clipboard 'SECONDARY)))
1562 ;; `initial' and `annotation' might have been passed. But if
1563 ;; the property list has them, we prefer those values.
1564 (v-i (or (plist-get org-store-link-plist :initial)
1565 (and (stringp initial) (org-no-properties initial))
1566 (org-capture-get :initial)
1567 ""))
1568 (v-a
1569 (let ((a (or (plist-get org-store-link-plist :annotation)
1570 annotation
1571 (org-capture-get :annotation)
1572 "")))
1573 ;; Is the link empty? Then we do not want it...
1574 (if (equal a "[[]]") "" a)))
1575 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1576 (v-A (if (and v-a (string-match l-re v-a))
1577 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
1578 v-a))
1579 (v-l (if (and v-a (string-match l-re v-a))
1580 (replace-match "\\1" nil nil v-a)
1581 v-a))
1582 (v-n user-full-name)
1583 (v-k (and (marker-buffer org-clock-marker)
1584 (org-no-properties org-clock-heading)))
1585 (v-K (if (marker-buffer org-clock-marker)
1586 (org-make-link-string
1587 (buffer-file-name (marker-buffer org-clock-marker))
1588 org-clock-heading)))
1589 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1590 (v-F (or (org-capture-get :original-file) ""))
1591 (clipboards (delq nil
1592 (list v-i
1593 (org-get-x-clipboard 'PRIMARY)
1594 (org-get-x-clipboard 'CLIPBOARD)
1595 (org-get-x-clipboard 'SECONDARY)
1596 v-c))))
1598 (setq org-store-link-plist (plist-put org-store-link-plist :annotation v-a))
1599 (setq org-store-link-plist (plist-put org-store-link-plist :initial v-i))
1601 (unless template
1602 (setq template "")
1603 (message "no template") (ding)
1604 (sit-for 1))
1605 (save-window-excursion
1606 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1607 (erase-buffer)
1608 (setq buffer-file-name nil)
1609 (setq mark-active nil)
1610 (insert template)
1611 (goto-char (point-min))
1613 ;; %[] insert contents of a file.
1614 (save-excursion
1615 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1616 (let ((filename (expand-file-name (match-string 1)))
1617 (beg (copy-marker (match-beginning 0)))
1618 (end (copy-marker (match-end 0))))
1619 (unless (org-capture-escaped-%)
1620 (delete-region beg end)
1621 (set-marker beg nil)
1622 (set-marker end nil)
1623 (condition-case error
1624 (insert-file-contents filename)
1625 (error
1626 (insert (format "%%![couldn not insert %s: %s]"
1627 filename
1628 error))))))))
1630 ;; Mark %() embedded elisp for later evaluation.
1631 (org-capture-expand-embedded-elisp 'mark)
1633 ;; Expand non-interactive templates.
1634 (let ((regexp "%\\(:[-a-za-z]+\\|<\\([^>\n]+\\)>\\|[aAcfFikKlntTuUx]\\)"))
1635 (save-excursion
1636 (while (re-search-forward regexp nil t)
1637 ;; `org-capture-escaped-%' may modify buffer and cripple
1638 ;; match-data. Use markers instead. Ditto for other
1639 ;; templates.
1640 (let ((pos (copy-marker (match-beginning 0)))
1641 (end (copy-marker (match-end 0)))
1642 (value (match-string 1))
1643 (time-string (match-string 2)))
1644 (unless (org-capture-escaped-%)
1645 (delete-region pos end)
1646 (set-marker pos nil)
1647 (set-marker end nil)
1648 (let ((replacement
1649 (pcase (string-to-char value)
1650 (?< (format-time-string time-string))
1652 (or (plist-get org-store-link-plist (intern value))
1653 ""))
1654 (?i (let ((lead (buffer-substring-no-properties
1655 (line-beginning-position) (point))))
1656 (mapconcat #'identity
1657 (split-string v-i "\n")
1658 (concat "\n" lead))))
1659 (?a v-a)
1660 (?A v-A)
1661 (?c v-c)
1662 (?f v-f)
1663 (?F v-F)
1664 (?k v-k)
1665 (?K v-K)
1666 (?l v-l)
1667 (?n v-n)
1668 (?t v-t)
1669 (?T v-T)
1670 (?u v-u)
1671 (?U v-U)
1672 (?x v-x))))
1673 (insert
1674 (if (org-capture-inside-embedded-elisp-p)
1675 (replace-regexp-in-string "\"" "\\\\\"" replacement)
1676 replacement))))))))
1678 ;; Expand %() embedded Elisp. Limit to Sexp originally marked.
1679 (org-capture-expand-embedded-elisp)
1681 ;; Expand interactive templates. This is the last step so that
1682 ;; template is mostly expanded when prompting happens. Turn on
1683 ;; Org mode and set local variables. This is to support
1684 ;; completion in interactive prompts.
1685 (let ((org-inhibit-startup t)) (org-mode))
1686 (org-clone-local-variables buffer "\\`org-")
1687 (let (strings) ; Stores interactive answers.
1688 (save-excursion
1689 (let ((regexp "%\\^\\(?:{\\([^}]*\\)}\\)?\\([CgGLptTuU]\\)?"))
1690 (while (re-search-forward regexp nil t)
1691 (let* ((items (and (match-end 1)
1692 (save-match-data
1693 (split-string (match-string-no-properties 1)
1694 "|"))))
1695 (key (match-string 2))
1696 (beg (copy-marker (match-beginning 0)))
1697 (end (copy-marker (match-end 0)))
1698 (prompt (nth 0 items))
1699 (default (nth 1 items))
1700 (completions (nthcdr 2 items))
1701 (histvar
1702 (intern
1703 (concat "org-capture-template-prompt-history::"
1704 (or prompt "")))))
1705 (unless (org-capture-escaped-%)
1706 (delete-region beg end)
1707 (set-marker beg nil)
1708 (set-marker end nil)
1709 (pcase key
1710 ((or "G" "g")
1711 (let* ((org-last-tags-completion-table
1712 (org-global-tags-completion-table
1713 (cond ((equal key "G") (org-agenda-files))
1714 (file (list file))
1715 (t nil))))
1716 (org-add-colon-after-tag-completion t)
1717 (ins (mapconcat
1718 #'identity
1719 (org-split-string
1720 (completing-read
1721 (if prompt (concat prompt ": ") "Tags: ")
1722 'org-tags-completion-function nil nil nil
1723 'org-tags-history)
1724 "[^[:alnum:]_@#%]+")
1725 ":")))
1726 (when (org-string-nw-p ins)
1727 (unless (eq (char-before) ?:) (insert ":"))
1728 (insert ins)
1729 (unless (eq (char-after) ?:) (insert ":"))
1730 (and (org-at-heading-p)
1731 (let ((org-ignore-region t))
1732 (org-set-tags nil 'align))))))
1733 ("C"
1734 (cond
1735 ((= (length clipboards) 1) (insert (car clipboards)))
1736 ((> (length clipboards) 1)
1737 (insert (read-string "Clipboard/kill value: "
1738 (car clipboards)
1739 '(clipboards . 1)
1740 (car clipboards))))))
1741 ("L"
1742 (cond ((= (length clipboards) 1)
1743 (org-insert-link 0 (car clipboards)))
1744 ((> (length clipboards) 1)
1745 (org-insert-link
1747 (read-string "Clipboard/kill value: "
1748 (car clipboards)
1749 '(clipboards . 1)
1750 (car clipboards))))))
1751 ("p" (org-set-property prompt nil))
1752 ((guard key)
1753 ;; These are the date/time related ones.
1754 (let* ((upcase? (equal (upcase key) key))
1755 (org-time-was-given upcase?)
1756 (org-end-time-was-given)
1757 (time (org-read-date upcase? t nil prompt)))
1758 (org-insert-time-stamp
1759 time org-time-was-given
1760 (member key '("u" "U"))
1761 nil nil (list org-end-time-was-given))))
1763 (push (org-completing-read
1764 (concat (or prompt "Enter string")
1765 (and default (format " [%s]" default))
1766 ": ")
1767 completions nil nil nil histvar default)
1768 strings)
1769 (insert (car strings)))))))))
1771 ;; Replace %n escapes with nth %^{...} string.
1772 (setq strings (nreverse strings))
1773 (save-excursion
1774 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
1775 (unless (org-capture-escaped-%)
1776 (replace-match
1777 (nth (1- (string-to-number (match-string 1))) strings)
1778 nil t)))))
1780 ;; Make sure there are no empty lines before the text, and that
1781 ;; it ends with a newline character.
1782 (skip-chars-forward " \t\n")
1783 (delete-region (point-min) (line-beginning-position))
1784 (goto-char (point-max))
1785 (skip-chars-backward " \t\n")
1786 (delete-region (point) (point-max))
1787 (insert "\n")
1789 ;; Return the expanded template and kill the capture buffer.
1790 (untabify (point-min) (point-max))
1791 (set-buffer-modified-p nil)
1792 (prog1 (buffer-substring-no-properties (point-min) (point-max))
1793 (kill-buffer (current-buffer))))))
1795 (defun org-capture-escaped-% ()
1796 "Non-nil if % was escaped.
1797 If yes, unescape it now. Assume match-data contains the
1798 placeholder to check."
1799 (save-excursion
1800 (goto-char (match-beginning 0))
1801 (let ((n (abs (skip-chars-backward "\\\\"))))
1802 (delete-char (/ (1+ n) 2))
1803 (= (% n 2) 1))))
1805 (defun org-capture-expand-embedded-elisp (&optional mark)
1806 "Evaluate embedded elisp %(sexp) and replace with the result.
1807 When optional MARK argument is non-nil, mark Sexp with a text
1808 property (`org-embedded-elisp') for later evaluation. Only
1809 marked Sexp are evaluated when this argument is nil."
1810 (save-excursion
1811 (goto-char (point-min))
1812 (while (re-search-forward "%(" nil t)
1813 (cond
1814 ((get-text-property (match-beginning 0) 'org-embedded-elisp)
1815 (goto-char (match-beginning 0))
1816 (let ((template-start (point)))
1817 (forward-char 1)
1818 (let* ((sexp (read (current-buffer)))
1819 (result (org-eval
1820 (org-capture--expand-keyword-in-embedded-elisp
1821 sexp))))
1822 (delete-region template-start (point))
1823 (cond
1824 ((not result) nil)
1825 ((stringp result) (insert result))
1826 (t (error
1827 "Capture template sexp `%s' must evaluate to string or nil"
1828 sexp))))))
1829 ((not mark) nil)
1830 ;; Only mark valid and non-escaped sexp.
1831 ((org-capture-escaped-%) nil)
1833 (let ((end (with-syntax-table emacs-lisp-mode-syntax-table
1834 (ignore-errors (scan-sexps (1- (point)) 1)))))
1835 (when end
1836 (put-text-property (- (point) 2) end 'org-embedded-elisp t))))))))
1838 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1839 "Recursively replace capture link keywords in ATTR sexp.
1840 Such keywords are prefixed with \"%:\". See
1841 `org-capture-template' for more information."
1842 (cond ((consp attr)
1843 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr))
1844 ((symbolp attr)
1845 (let* ((attr-symbol (symbol-name attr))
1846 (key (and (string-match "%\\(:.*\\)" attr-symbol)
1847 (intern (match-string 1 attr-symbol)))))
1848 (or (plist-get org-store-link-plist key)
1849 attr)))
1850 (t attr)))
1852 (defun org-capture-inside-embedded-elisp-p ()
1853 "Non-nil if point is inside of embedded elisp %(sexp).
1854 Assume sexps have been marked with
1855 `org-capture-expand-embedded-elisp' beforehand."
1856 (get-text-property (point) 'org-embedded-elisp))
1858 ;;;###autoload
1859 (defun org-capture-import-remember-templates ()
1860 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1861 (interactive)
1862 (when (and (yes-or-no-p
1863 "Import old remember templates into org-capture-templates? ")
1864 (yes-or-no-p
1865 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1866 (require 'org-remember)
1867 (setq org-capture-templates
1868 (mapcar
1869 (lambda (entry)
1870 (let ((desc (car entry))
1871 (key (char-to-string (nth 1 entry)))
1872 (template (nth 2 entry))
1873 (file (or (nth 3 entry) org-default-notes-file))
1874 (position (or (nth 4 entry) org-remember-default-headline))
1875 (type 'entry)
1876 (prepend org-reverse-note-order)
1877 immediate target jump-to-captured)
1878 (cond
1879 ((member position '(top bottom))
1880 (setq target (list 'file file)
1881 prepend (eq position 'top)))
1882 ((eq position 'date-tree)
1883 (setq target (list 'file+datetree file)
1884 prepend nil))
1885 (t (setq target (list 'file+headline file position))))
1887 (when (string-match "%!" template)
1888 (setq template (replace-match "" t t template)
1889 immediate t))
1891 (when (string-match "%&" template)
1892 (setq jump-to-captured t))
1894 (append (list key desc type target template)
1895 (if prepend '(:prepend t))
1896 (if immediate '(:immediate-finish t))
1897 (if jump-to-captured '(:jump-to-captured t)))))
1899 org-remember-templates))))
1901 (provide 'org-capture)
1903 ;;; org-capture.el ends here