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