org-clone-subtree-with-time-shift: Fix SHIFT check
[org-mode.git] / lisp / org-capture.el
blobea1ed10d59907002206b532818d6aeb7747f1041
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 ;; Make sure that the empty lines after are correct
727 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
728 (member (org-capture-get :type 'local)
729 '(entry item checkitem plain)))
730 (save-excursion
731 (goto-char end)
732 (or (bolp) (newline))
733 (org-capture-empty-lines-after
734 (or (org-capture-get :empty-lines-after 'local)
735 (org-capture-get :empty-lines 'local) 0))))
736 ;; Postprocessing: Update Statistics cookies, do the sorting
737 (when (derived-mode-p 'org-mode)
738 (save-excursion
739 (when (ignore-errors (org-back-to-heading))
740 (org-update-parent-todo-statistics)
741 (org-update-checkbox-count)))
742 ;; FIXME Here we should do the sorting
743 ;; If we have added a table line, maybe recompute?
744 (when (and (eq (org-capture-get :type 'local) 'table-line)
745 (org-at-table-p))
746 (if (org-table-get-stored-formulas)
747 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
748 (org-table-align))))
749 ;; Store this place as the last one where we stored something
750 ;; Do the marking in the base buffer, so that it makes sense after
751 ;; the indirect buffer has been killed.
752 (org-capture-store-last-position)
754 ;; Run the hook
755 (run-hooks 'org-capture-before-finalize-hook))
757 (when (org-capture-get :decrypted)
758 (save-excursion
759 (goto-char (org-capture-get :decrypted))
760 (org-encrypt-entry)))
762 ;; Kill the indirect buffer
763 (save-buffer)
764 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
765 (new-buffer (org-capture-get :new-buffer 'local))
766 (kill-buffer (org-capture-get :kill-buffer 'local))
767 (base-buffer (buffer-base-buffer (current-buffer))))
769 ;; Kill the indirect buffer
770 (kill-buffer (current-buffer))
772 ;; Narrow back the target buffer to its previous state
773 (with-current-buffer (org-capture-get :buffer)
774 (let ((reg (org-capture-get :initial-target-region))
775 (pos (org-capture-get :initial-target-position))
776 (ipt (org-capture-get :insertion-point))
777 (size (org-capture-get :captured-entry-size)))
778 (if (not reg)
779 (widen)
780 (cond ((< ipt (car reg))
781 ;; insertion point is before the narrowed region
782 (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
783 ((> ipt (cdr reg))
784 ;; insertion point is after the narrowed region
785 (narrow-to-region (car reg) (cdr reg)))
787 ;; insertion point is within the narrowed region
788 (narrow-to-region (car reg) (+ size (cdr reg)))))
789 ;; now place back the point at its original position
790 (if (< ipt (car reg))
791 (goto-char (+ size pos))
792 (goto-char (if (< ipt pos) (+ size pos) pos))))))
794 ;; Kill the target buffer if that is desired
795 (when (and base-buffer new-buffer kill-buffer)
796 (with-current-buffer base-buffer (save-buffer))
797 (kill-buffer base-buffer))
799 ;; Restore the window configuration before capture
800 (set-window-configuration return-wconf))
802 (run-hooks 'org-capture-after-finalize-hook)
803 ;; Special cases
804 (cond
805 (abort-note
806 (cl-case abort-note
807 (clean
808 (message "Capture process aborted and target buffer cleaned up"))
809 (dirty
810 (error "Capture process aborted, but target buffer could not be \
811 cleaned up correctly"))))
812 (stay-with-capture
813 (org-capture-goto-last-stored)))
814 ;; Return if we did store something
815 (not abort-note)))
817 (defun org-capture-refile ()
818 "Finalize the current capture and then refile the entry.
819 Refiling is done from the base buffer, because the indirect buffer is then
820 already gone. Any prefix argument will be passed to the refile command."
821 (interactive)
822 (unless (eq (org-capture-get :type 'local) 'entry)
823 (error
824 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
825 (let ((pos (point))
826 (base (buffer-base-buffer (current-buffer)))
827 (org-capture-is-refiling t)
828 (kill-buffer (org-capture-get :kill-buffer 'local)))
829 (org-capture-put :kill-buffer nil)
830 (org-capture-finalize)
831 (save-window-excursion
832 (with-current-buffer (or base (current-buffer))
833 (org-with-wide-buffer
834 (goto-char pos)
835 (call-interactively 'org-refile))))
836 (when kill-buffer (kill-buffer base))))
838 (defun org-capture-kill ()
839 "Abort the current capture process."
840 (interactive)
841 ;; FIXME: This does not do the right thing, we need to remove the
842 ;; new stuff by hand it is easy: undo, then kill the buffer
843 (let ((org-note-abort t)
844 (org-capture-before-finalize-hook nil))
845 (org-capture-finalize)))
847 (defun org-capture-goto-last-stored ()
848 "Go to the location where the last capture note was stored."
849 (interactive)
850 (org-goto-marker-or-bmk org-capture-last-stored-marker
851 (plist-get org-bookmark-names-plist
852 :last-capture))
853 (message "This is the last note stored by a capture process"))
855 ;;; Supporting functions for handling the process
857 (defun org-capture-put-target-region-and-position ()
858 "Store the initial region with `org-capture-put'."
859 (org-capture-put
860 :initial-target-region
861 ;; Check if the buffer is currently narrowed
862 (when (org-buffer-narrowed-p)
863 (cons (point-min) (point-max))))
864 ;; store the current point
865 (org-capture-put :initial-target-position (point)))
867 (defvar org-time-was-given) ; dynamically scoped parameter
868 (defun org-capture-set-target-location (&optional target)
869 "Find TARGET buffer and position.
870 Store them in the capture property list."
871 (let ((target-entry-p t) decrypted-hl-pos)
872 (setq target (or target (org-capture-get :target)))
873 (save-excursion
874 (cond
875 ((eq (car target) 'file)
876 (set-buffer (org-capture-target-buffer (nth 1 target)))
877 (org-capture-put-target-region-and-position)
878 (widen)
879 (setq target-entry-p nil))
881 ((eq (car target) 'id)
882 (let ((loc (org-id-find (nth 1 target))))
883 (if (not loc)
884 (error "Cannot find target ID \"%s\"" (nth 1 target))
885 (set-buffer (org-capture-target-buffer (car loc)))
886 (widen)
887 (org-capture-put-target-region-and-position)
888 (goto-char (cdr loc)))))
890 ((eq (car target) 'file+headline)
891 (set-buffer (org-capture-target-buffer (nth 1 target)))
892 (unless (derived-mode-p 'org-mode)
893 (error
894 "Target buffer \"%s\" for file+headline should be in Org mode"
895 (current-buffer)))
896 (org-capture-put-target-region-and-position)
897 (widen)
898 (let ((hd (nth 2 target)))
899 (goto-char (point-min))
900 (if (re-search-forward
901 (format org-complex-heading-regexp-format (regexp-quote hd))
902 nil t)
903 (goto-char (point-at-bol))
904 (goto-char (point-max))
905 (or (bolp) (insert "\n"))
906 (insert "* " hd "\n")
907 (beginning-of-line 0))))
909 ((eq (car target) 'file+olp)
910 (let ((m (org-find-olp
911 (cons (org-capture-expand-file (nth 1 target))
912 (cddr target)))))
913 (set-buffer (marker-buffer m))
914 (org-capture-put-target-region-and-position)
915 (widen)
916 (goto-char m)))
918 ((eq (car target) 'file+regexp)
919 (set-buffer (org-capture-target-buffer (nth 1 target)))
920 (org-capture-put-target-region-and-position)
921 (widen)
922 (goto-char (point-min))
923 (if (re-search-forward (nth 2 target) nil t)
924 (progn
925 (goto-char (if (org-capture-get :prepend)
926 (match-beginning 0) (match-end 0)))
927 (org-capture-put :exact-position (point))
928 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
929 (error "No match for target regexp in file %s" (nth 1 target))))
931 ((memq (car target) '(file+datetree file+datetree+prompt file+weektree file+weektree+prompt))
932 (require 'org-datetree)
933 (set-buffer (org-capture-target-buffer (nth 1 target)))
934 (unless (derived-mode-p 'org-mode)
935 (error "Target buffer \"%s\" for %s should be in Org mode"
936 (current-buffer)
937 (car target)))
938 (org-capture-put-target-region-and-position)
939 (widen)
940 ;; Make a date/week tree entry, with the current date (or
941 ;; yesterday, if we are extending dates for a couple of hours)
942 (funcall
943 (cond
944 ((memq (car target) '(file+weektree file+weektree+prompt))
945 #'org-datetree-find-iso-week-create)
946 (t #'org-datetree-find-date-create))
947 (calendar-gregorian-from-absolute
948 (cond
949 (org-overriding-default-time
950 ;; use the overriding default time
951 (time-to-days org-overriding-default-time))
953 ((memq (car target) '(file+datetree+prompt file+weektree+prompt))
954 ;; prompt for date
955 (let ((prompt-time (org-read-date
956 nil t nil "Date for tree entry:"
957 (current-time))))
958 (org-capture-put
959 :default-time
960 (cond ((and (or (not (boundp 'org-time-was-given))
961 (not org-time-was-given))
962 (not (= (time-to-days prompt-time) (org-today))))
963 ;; Use 00:00 when no time is given for another date than today?
964 (apply #'encode-time
965 (append '(0 0 0)
966 (cl-cdddr (decode-time prompt-time)))))
967 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer)
968 ;; Replace any time range by its start
969 (apply 'encode-time
970 (org-read-date-analyze
971 (replace-match "\\1 \\2" nil nil org-read-date-final-answer)
972 prompt-time (decode-time prompt-time))))
973 (t prompt-time)))
974 (time-to-days prompt-time)))
976 ;; current date, possibly corrected for late night workers
977 (org-today))))))
979 ((eq (car target) 'file+function)
980 (set-buffer (org-capture-target-buffer (nth 1 target)))
981 (org-capture-put-target-region-and-position)
982 (widen)
983 (funcall (nth 2 target))
984 (org-capture-put :exact-position (point))
985 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
987 ((eq (car target) 'function)
988 (funcall (nth 1 target))
989 (org-capture-put :exact-position (point))
990 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
992 ((eq (car target) 'clock)
993 (if (and (markerp org-clock-hd-marker)
994 (marker-buffer org-clock-hd-marker))
995 (progn (set-buffer (marker-buffer org-clock-hd-marker))
996 (org-capture-put-target-region-and-position)
997 (widen)
998 (goto-char org-clock-hd-marker))
999 (error "No running clock that could be used as capture target")))
1001 (t (error "Invalid capture target specification")))
1003 (when (and (featurep 'org-crypt) (org-at-encrypted-entry-p))
1004 (org-decrypt-entry)
1005 (setq decrypted-hl-pos
1006 (save-excursion (and (org-back-to-heading t) (point)))))
1008 (org-capture-put :buffer (current-buffer) :pos (point)
1009 :target-entry-p target-entry-p
1010 :decrypted decrypted-hl-pos))))
1012 (defun org-capture-expand-file (file)
1013 "Expand functions, symbols and file names for FILE.
1014 When FILE is a function, call it. When it is a form, evaluate
1015 it. When it is a variable, retrieve the value. When it is
1016 a string, treat it as a file name, possibly expanding it
1017 according to `org-directory', and return it. If it is the empty
1018 string, however, return `org-default-notes-file'. In any other
1019 case, raise an error."
1020 (cond
1021 ((equal file "") org-default-notes-file)
1022 ((stringp file) (expand-file-name file org-directory))
1023 ((functionp file) (funcall file))
1024 ((and (symbolp file) (boundp file)) (symbol-value file))
1025 ((consp file) (eval file))
1026 (t file)))
1028 (defun org-capture-target-buffer (file)
1029 "Get a buffer for FILE.
1030 FILE is a generalized file location, as handled by
1031 `org-capture-expand-file'."
1032 (let ((file (or (org-string-nw-p (org-capture-expand-file file))
1033 org-default-notes-file
1034 (error "No notes file specified, and no default available"))))
1035 (or (org-find-base-buffer-visiting file)
1036 (progn (org-capture-put :new-buffer t)
1037 (find-file-noselect file)))))
1039 (defun org-capture-place-template (&optional inhibit-wconf-store)
1040 "Insert the template at the target location, and display the buffer.
1041 When `inhibit-wconf-store', don't store the window configuration, as it
1042 may have been stored before."
1043 (unless inhibit-wconf-store
1044 (org-capture-put :return-to-wconf (current-window-configuration)))
1045 (delete-other-windows)
1046 (org-switch-to-buffer-other-window
1047 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
1048 (widen)
1049 (outline-show-all)
1050 (goto-char (org-capture-get :pos))
1051 (setq-local outline-level 'org-outline-level)
1052 (pcase (org-capture-get :type)
1053 ((or `nil `entry) (org-capture-place-entry))
1054 (`table-line (org-capture-place-table-line))
1055 (`plain (org-capture-place-plain-text))
1056 (`item (org-capture-place-item))
1057 (`checkitem (org-capture-place-item)))
1058 (org-capture-mode 1)
1059 (setq-local org-capture-current-plist org-capture-plist))
1061 (defun org-capture-place-entry ()
1062 "Place the template as a new Org entry."
1063 (let* ((txt (org-capture-get :template))
1064 (reversed (org-capture-get :prepend))
1065 (target-entry-p (org-capture-get :target-entry-p))
1066 level beg end)
1068 (and (org-capture-get :exact-position)
1069 (goto-char (org-capture-get :exact-position)))
1070 (cond
1071 ((not target-entry-p)
1072 ;; Insert as top-level entry, either at beginning or at end of
1073 ;; file.
1074 (setq level 1)
1075 (if reversed
1076 (progn (goto-char (point-min))
1077 (or (org-at-heading-p)
1078 (outline-next-heading)))
1079 (goto-char (point-max))
1080 (or (bolp) (insert "\n"))))
1082 ;; Insert as a child of the current entry
1083 (and (looking-at "\\*+")
1084 (setq level (- (match-end 0) (match-beginning 0))))
1085 (setq level (org-get-valid-level (or level 1) 1))
1086 (if reversed
1087 (progn
1088 (outline-next-heading)
1089 (or (bolp) (insert "\n")))
1090 (org-end-of-subtree t nil)
1091 (or (bolp) (insert "\n")))))
1092 (org-capture-empty-lines-before)
1093 (setq beg (point))
1094 (org-capture-verify-tree txt)
1095 (org-paste-subtree level txt 'for-yank)
1096 (org-capture-empty-lines-after 1)
1097 (org-capture-position-for-last-stored beg)
1098 (outline-next-heading)
1099 (setq end (point))
1100 (org-capture-mark-kill-region beg (1- end))
1101 (org-capture-narrow beg (1- end))
1102 (if (or (re-search-backward "%\\?" beg t)
1103 (re-search-forward "%\\?" end t))
1104 (replace-match ""))))
1106 (defun org-capture-place-item ()
1107 "Place the template as a new plain list item."
1108 (let* ((txt (org-capture-get :template))
1109 (target-entry-p (org-capture-get :target-entry-p))
1110 (ind 0)
1111 beg end)
1112 (if (org-capture-get :exact-position)
1113 (goto-char (org-capture-get :exact-position))
1114 (cond
1115 ((not target-entry-p)
1116 ;; Insert as top-level entry, either at beginning or at end of file
1117 (setq beg (point-min) end (point-max)))
1119 (setq beg (1+ (point-at-eol))
1120 end (save-excursion (outline-next-heading) (point)))))
1121 (setq ind nil)
1122 (if (org-capture-get :prepend)
1123 (progn
1124 (goto-char beg)
1125 (when (org-list-search-forward (org-item-beginning-re) end t)
1126 (goto-char (match-beginning 0))
1127 (setq ind (org-get-indentation))))
1128 (goto-char end)
1129 (when (org-list-search-backward (org-item-beginning-re) beg t)
1130 (setq ind (org-get-indentation))
1131 (org-end-of-item)))
1132 (unless ind (goto-char end)))
1133 ;; Remove common indentation
1134 (setq txt (org-remove-indentation txt))
1135 ;; Make sure this is indeed an item
1136 (unless (string-match (concat "\\`" (org-item-re)) txt)
1137 (setq txt (concat "- "
1138 (mapconcat 'identity (split-string txt "\n")
1139 "\n "))))
1140 ;; Prepare surrounding empty lines.
1141 (org-capture-empty-lines-before)
1142 (setq beg (point))
1143 (unless (eolp) (save-excursion (insert "\n")))
1144 (unless ind
1145 (org-indent-line)
1146 (setq ind (org-get-indentation))
1147 (delete-region beg (point)))
1148 ;; Set the correct indentation, depending on context
1149 (setq ind (make-string ind ?\ ))
1150 (setq txt (concat ind
1151 (mapconcat 'identity (split-string txt "\n")
1152 (concat "\n" ind))
1153 "\n"))
1154 ;; Insert item.
1155 (insert txt)
1156 (org-capture-empty-lines-after 1)
1157 (org-capture-position-for-last-stored beg)
1158 (forward-char 1)
1159 (setq end (point))
1160 (org-capture-mark-kill-region beg (1- end))
1161 (org-capture-narrow beg (1- end))
1162 (if (or (re-search-backward "%\\?" beg t)
1163 (re-search-forward "%\\?" end t))
1164 (replace-match ""))))
1166 (defun org-capture-place-table-line ()
1167 "Place the template as a table line."
1168 (require 'org-table)
1169 (let* ((txt (org-capture-get :template))
1170 (target-entry-p (org-capture-get :target-entry-p))
1171 (table-line-pos (org-capture-get :table-line-pos))
1172 beg end)
1173 (cond
1174 ((org-capture-get :exact-position)
1175 (goto-char (org-capture-get :exact-position)))
1176 ((not target-entry-p)
1177 ;; Table is not necessarily under a heading
1178 (setq beg (point-min) end (point-max)))
1180 ;; WE are at a heading, limit search to the body
1181 (setq beg (1+ (point-at-eol))
1182 end (save-excursion (outline-next-heading) (point)))))
1183 (if (re-search-forward org-table-dataline-regexp end t)
1184 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t))
1185 (goto-char e)
1186 (if (looking-at "[ \t]*#\\+tblfm:")
1187 (forward-line 1))
1188 (narrow-to-region b (point)))
1189 (goto-char end)
1190 (insert "\n| |\n|----|\n| |\n")
1191 (narrow-to-region (1+ end) (point)))
1192 ;; We are narrowed to the table, or to an empty line if there was no table
1194 ;; Check if the template is good
1195 (if (not (string-match org-table-dataline-regexp txt))
1196 (setq txt "| %?Bad template |\n"))
1197 (if (functionp table-line-pos)
1198 (setq table-line-pos (funcall table-line-pos))
1199 (setq table-line-pos (eval table-line-pos)))
1200 (cond
1201 ((and table-line-pos
1202 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
1203 (goto-char (point-min))
1204 ;; we have a complex line specification
1205 (let ((ll (ignore-errors
1206 (save-match-data (org-table-analyze))
1207 (aref org-table-hlines
1208 (- (match-end 1) (match-beginning 1)))))
1209 (delta (string-to-number (match-string 2 table-line-pos))))
1210 ;; The user wants a special position in the table
1211 (unless ll
1212 (error "Invalid table line specification \"%s\"" table-line-pos))
1213 (goto-char org-table-current-begin-pos)
1214 (forward-line (+ ll delta (if (< delta 0) 0 -1)))
1215 (org-table-insert-row 'below)
1216 (beginning-of-line 1)
1217 (delete-region (point) (1+ (point-at-eol)))
1218 (setq beg (point))
1219 (insert txt)
1220 (setq end (point))))
1221 ((org-capture-get :prepend)
1222 (goto-char (point-min))
1223 (re-search-forward org-table-hline-regexp nil t)
1224 (beginning-of-line 1)
1225 (re-search-forward org-table-dataline-regexp nil t)
1226 (beginning-of-line 1)
1227 (setq beg (point))
1228 (org-table-insert-row)
1229 (beginning-of-line 1)
1230 (delete-region (point) (1+ (point-at-eol)))
1231 (insert txt)
1232 (setq end (point)))
1234 (goto-char (point-max))
1235 (re-search-backward org-table-dataline-regexp nil t)
1236 (beginning-of-line 1)
1237 (org-table-insert-row 'below)
1238 (beginning-of-line 1)
1239 (delete-region (point) (1+ (point-at-eol)))
1240 (setq beg (point))
1241 (insert txt)
1242 (setq end (point))))
1243 (goto-char beg)
1244 (org-capture-position-for-last-stored 'table-line)
1245 (if (or (re-search-backward "%\\?" beg t)
1246 (re-search-forward "%\\?" end t))
1247 (replace-match ""))
1248 (org-table-align)))
1250 (defun org-capture-place-plain-text ()
1251 "Place the template plainly.
1252 If the target locator points at an Org node, place the template into
1253 the text of the entry, before the first child. If not, place the
1254 template at the beginning or end of the file.
1255 Of course, if exact position has been required, just put it there."
1256 (let* ((txt (org-capture-get :template))
1257 beg end)
1258 (cond
1259 ((org-capture-get :exact-position)
1260 (goto-char (org-capture-get :exact-position)))
1261 ((and (org-capture-get :target-entry-p)
1262 (bolp)
1263 (looking-at org-outline-regexp))
1264 ;; we should place the text into this entry
1265 (if (org-capture-get :prepend)
1266 ;; Skip meta data and drawers
1267 (org-end-of-meta-data t)
1268 ;; go to ent of the entry text, before the next headline
1269 (outline-next-heading)))
1271 ;; beginning or end of file
1272 (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
1273 (or (bolp) (newline))
1274 (org-capture-empty-lines-before)
1275 (setq beg (point))
1276 (insert txt)
1277 (org-capture-empty-lines-after 1)
1278 (org-capture-position-for-last-stored beg)
1279 (setq end (point))
1280 (org-capture-mark-kill-region beg (1- end))
1281 (org-capture-narrow beg (1- end))
1282 (if (or (re-search-backward "%\\?" beg t)
1283 (re-search-forward "%\\?" end t))
1284 (replace-match ""))))
1286 (defun org-capture-mark-kill-region (beg end)
1287 "Mark the region that will have to be killed when aborting capture."
1288 (let ((m1 (move-marker (make-marker) beg))
1289 (m2 (move-marker (make-marker) end)))
1290 (org-capture-put :begin-marker m1)
1291 (org-capture-put :end-marker m2)))
1293 (defun org-capture-position-for-last-stored (where)
1294 "Memorize the position that should later become the position of last capture."
1295 (cond
1296 ((integerp where)
1297 (org-capture-put :position-for-last-stored
1298 (move-marker (make-marker) where
1299 (or (buffer-base-buffer (current-buffer))
1300 (current-buffer)))))
1301 ((eq where 'table-line)
1302 (org-capture-put :position-for-last-stored
1303 (list 'table-line
1304 (org-table-current-dline))))
1305 (t (error "This should not happen"))))
1307 (defun org-capture-store-last-position ()
1308 "Store the last-captured position."
1309 (let* ((where (org-capture-get :position-for-last-stored 'local))
1310 (pos (cond
1311 ((markerp where)
1312 (prog1 (marker-position where)
1313 (move-marker where nil)))
1314 ((and (listp where) (eq (car where) 'table-line))
1315 (if (org-at-table-p)
1316 (save-excursion
1317 (org-table-goto-line (nth 1 where))
1318 (point-at-bol))
1319 (point))))))
1320 (with-current-buffer (buffer-base-buffer (current-buffer))
1321 (org-with-point-at pos
1322 (when org-capture-bookmark
1323 (let ((bookmark (plist-get org-bookmark-names-plist :last-capture)))
1324 (when bookmark (with-demoted-errors (bookmark-set bookmark)))))
1325 (move-marker org-capture-last-stored-marker (point))))))
1327 (defun org-capture-narrow (beg end)
1328 "Narrow, unless configuration says not to narrow."
1329 (unless (org-capture-get :unnarrowed)
1330 (narrow-to-region beg end)
1331 (goto-char beg)))
1333 (defun org-capture-empty-lines-before (&optional n)
1334 "Set the correct number of empty lines before the insertion point.
1335 Point will be after the empty lines, so insertion can directly be done."
1336 (setq n (or n (org-capture-get :empty-lines-before)
1337 (org-capture-get :empty-lines) 0))
1338 (let ((pos (point)))
1339 (org-back-over-empty-lines)
1340 (delete-region (point) pos)
1341 (if (> n 0) (newline n))))
1343 (defun org-capture-empty-lines-after (&optional n)
1344 "Set the correct number of empty lines after the inserted string.
1345 Point will remain at the first line after the inserted text."
1346 (setq n (or n (org-capture-get :empty-lines-after)
1347 (org-capture-get :empty-lines) 0))
1348 (org-back-over-empty-lines)
1349 (while (looking-at "[ \t]*\n") (replace-match ""))
1350 (let ((pos (point)))
1351 (if (> n 0) (newline n))
1352 (goto-char pos)))
1354 (defvar org-clock-marker) ; Defined in org.el
1356 (defun org-capture-insert-template-here ()
1357 "Insert the capture template at point."
1358 (let* ((template (org-capture-get :template))
1359 (type (org-capture-get :type))
1360 beg end pp)
1361 (or (bolp) (newline))
1362 (setq beg (point))
1363 (cond
1364 ((and (eq type 'entry) (derived-mode-p 'org-mode))
1365 (org-capture-verify-tree (org-capture-get :template))
1366 (org-paste-subtree nil template t))
1367 ((and (memq type '(item checkitem))
1368 (derived-mode-p 'org-mode)
1369 (save-excursion (skip-chars-backward " \t\n")
1370 (setq pp (point))
1371 (org-in-item-p)))
1372 (goto-char pp)
1373 (org-insert-item)
1374 (skip-chars-backward " ")
1375 (skip-chars-backward "-+*0123456789).")
1376 (delete-region (point) (point-at-eol))
1377 (setq beg (point))
1378 (org-remove-indentation template)
1379 (insert template)
1380 (org-capture-empty-lines-after)
1381 (goto-char beg)
1382 (org-list-repair)
1383 (org-end-of-item)
1384 (setq end (point)))
1385 (t (insert template)))
1386 (setq end (point))
1387 (goto-char beg)
1388 (if (re-search-forward "%\\?" end t)
1389 (replace-match ""))))
1391 (defun org-capture-set-plist (entry)
1392 "Initialize the property list from the template definition."
1393 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1394 (org-capture-put :key (car entry) :description (nth 1 entry)
1395 :target (nth 3 entry))
1396 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1397 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1398 ;; The template may be empty or omitted for special types.
1399 ;; Here we insert the default templates for such cases.
1400 (cond
1401 ((eq type 'item) (setq txt "- %?"))
1402 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1403 ((eq type 'table-line) (setq txt "| %? |"))
1404 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1405 (org-capture-put :template txt :type type)))
1407 (defun org-capture-goto-target (&optional template-key)
1408 "Go to the target location of a capture template.
1409 The user is queried for the template."
1410 (interactive)
1411 (let ((entry (org-capture-select-template template-key)))
1412 (unless entry (error "No capture template selected"))
1413 (org-capture-set-plist entry)
1414 (org-capture-set-target-location)
1415 (pop-to-buffer-same-window (org-capture-get :buffer))
1416 (goto-char (org-capture-get :pos))))
1418 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1419 "Make an indirect buffer for a capture process.
1420 Use PREFIX as a prefix for the name of the indirect buffer."
1421 (setq buffer (or buffer (current-buffer)))
1422 (let ((n 1) (base (buffer-name buffer)) bname)
1423 (setq bname (concat prefix "-" base))
1424 (while (buffer-live-p (get-buffer bname))
1425 (setq bname (concat prefix "-" (number-to-string (cl-incf n)) "-" base)))
1426 (condition-case nil
1427 (make-indirect-buffer buffer bname 'clone)
1428 (error
1429 (let ((buf (make-indirect-buffer buffer bname)))
1430 (with-current-buffer buf (org-mode))
1431 buf)))))
1433 (defun org-capture-verify-tree (tree)
1434 "Throw error if TREE is not a valid tree."
1435 (unless (org-kill-is-subtree-p tree)
1436 (error "Template is not a valid Org entry or tree")))
1438 (defun org-mks (table title &optional prompt specials)
1439 "Select a member of an alist with multiple keys.
1440 TABLE is the alist which should contain entries where the car is a string.
1441 There should be two types of entries.
1443 1. prefix descriptions like (\"a\" \"Description\")
1444 This indicates that `a' is a prefix key for multi-letter selection, and
1445 that there are entries following with keys like \"ab\", \"ax\"...
1447 2. Selectable members must have more than two elements, with the first
1448 being the string of keys that lead to selecting it, and the second a
1449 short description string of the item.
1451 The command will then make a temporary buffer listing all entries
1452 that can be selected with a single key, and all the single key
1453 prefixes. When you press the key for a single-letter entry, it is selected.
1454 When you press a prefix key, the commands (and maybe further prefixes)
1455 under this key will be shown and offered for selection.
1457 TITLE will be placed over the selection in the temporary buffer,
1458 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1459 also (\"key\" \"description\") entries. When one of these is selection,
1460 only the bare key is returned."
1461 (setq prompt (or prompt "Select: "))
1462 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1463 current prefix rtn re pressed buffer (inhibit-quit t))
1464 (save-window-excursion
1465 (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
1466 (setq orig-table table)
1467 (catch 'exit
1468 (while t
1469 (erase-buffer)
1470 (insert title "\n\n")
1471 (setq tbl table
1472 des-keys nil
1473 allowed-keys nil
1474 cursor-type nil)
1475 (setq prefix (if current (concat current " ") ""))
1476 (while tbl
1477 (cond
1478 ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
1479 ;; This is a description on this level
1480 (setq dkey (caar tbl) ddesc (cl-cadar tbl))
1481 (pop tbl)
1482 (push dkey des-keys)
1483 (push dkey allowed-keys)
1484 (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
1485 ;; Skip keys which are below this prefix
1486 (setq re (concat "\\`" (regexp-quote dkey)))
1487 (let (case-fold-search)
1488 (while (and tbl (string-match re (caar tbl))) (pop tbl))))
1489 ((= 2 (length (car tbl)))
1490 ;; Not yet a usable description, skip it
1493 ;; usable entry on this level
1494 (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
1495 (push (caar tbl) allowed-keys)
1496 (pop tbl))))
1497 (when specials
1498 (insert "-------------------------------------------------------------------------------\n")
1499 (let ((sp specials))
1500 (while sp
1501 (insert (format "[%s] %s\n"
1502 (caar sp) (nth 1 (car sp))))
1503 (push (caar sp) allowed-keys)
1504 (pop sp))))
1505 (push "\C-g" allowed-keys)
1506 (goto-char (point-min))
1507 (if (not (pos-visible-in-window-p (point-max)))
1508 (org-fit-window-to-buffer))
1509 (message prompt)
1510 (setq pressed (char-to-string (read-char-exclusive)))
1511 (while (not (member pressed allowed-keys))
1512 (message "Invalid key `%s'" pressed) (sit-for 1)
1513 (message prompt)
1514 (setq pressed (char-to-string (read-char-exclusive))))
1515 (when (equal pressed "\C-g")
1516 (kill-buffer buffer)
1517 (user-error "Abort"))
1518 (when (and (not (assoc pressed table))
1519 (not (member pressed des-keys))
1520 (assoc pressed specials))
1521 (throw 'exit (setq rtn pressed)))
1522 (unless (member pressed des-keys)
1523 (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
1524 orig-table))))
1525 (setq current (concat current pressed))
1526 (setq table (mapcar
1527 (lambda (x)
1528 (if (and (> (length (car x)) 1)
1529 (equal (substring (car x) 0 1) pressed))
1530 (cons (substring (car x) 1) (cdr x))
1531 nil))
1532 table))
1533 (setq table (remove nil table)))))
1534 (when buffer (kill-buffer buffer))
1535 rtn))
1537 ;;; The template code
1538 (defun org-capture-select-template (&optional keys)
1539 "Select a capture template.
1540 Lisp programs can force the template by setting KEYS to a string."
1541 (let ((org-capture-templates
1542 (or (org-contextualize-keys
1543 org-capture-templates org-capture-templates-contexts)
1544 '(("t" "Task" entry (file+headline "" "Tasks")
1545 "* TODO %?\n %u\n %a")))))
1546 (if keys
1547 (or (assoc keys org-capture-templates)
1548 (error "No capture template referred to by \"%s\" keys" keys))
1549 (org-mks org-capture-templates
1550 "Select a capture template\n========================="
1551 "Template key: "
1552 '(("C" "Customize org-capture-templates")
1553 ("q" "Abort"))))))
1555 (defun org-capture-fill-template (&optional template initial annotation)
1556 "Fill a template and return the filled template as a string.
1557 The template may still contain \"%?\" for cursor positioning."
1558 (let* ((template (or template (org-capture-get :template)))
1559 (buffer (org-capture-get :buffer))
1560 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1561 (time (let* ((c (or (org-capture-get :default-time) (current-time)))
1562 (d (decode-time c)))
1563 (if (< (nth 2 d) org-extend-today-until)
1564 (encode-time 0 59 23 (1- (nth 3 d)) (nth 4 d) (nth 5 d))
1565 c)))
1566 (v-t (format-time-string (org-time-stamp-format nil) time))
1567 (v-T (format-time-string (org-time-stamp-format t) time))
1568 (v-u (format-time-string (org-time-stamp-format nil t) time))
1569 (v-U (format-time-string (org-time-stamp-format t t) time))
1570 (v-c (and kill-ring (current-kill 0)))
1571 (v-x (or (org-get-x-clipboard 'PRIMARY)
1572 (org-get-x-clipboard 'CLIPBOARD)
1573 (org-get-x-clipboard 'SECONDARY)))
1574 ;; `initial' and `annotation' might have been passed. But if
1575 ;; the property list has them, we prefer those values.
1576 (v-i (or (plist-get org-store-link-plist :initial)
1577 (and (stringp initial) (org-no-properties initial))
1578 (org-capture-get :initial)
1579 ""))
1580 (v-a
1581 (let ((a (or (plist-get org-store-link-plist :annotation)
1582 annotation
1583 (org-capture-get :annotation)
1584 "")))
1585 ;; Is the link empty? Then we do not want it...
1586 (if (equal a "[[]]") "" a)))
1587 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1588 (v-A (if (and v-a (string-match l-re v-a))
1589 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
1590 v-a))
1591 (v-l (if (and v-a (string-match l-re v-a))
1592 (replace-match "\\1" nil nil v-a)
1593 v-a))
1594 (v-n user-full-name)
1595 (v-k (and (marker-buffer org-clock-marker)
1596 (org-no-properties org-clock-heading)))
1597 (v-K (if (marker-buffer org-clock-marker)
1598 (org-make-link-string
1599 (buffer-file-name (marker-buffer org-clock-marker))
1600 org-clock-heading)))
1601 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1602 (v-F (or (org-capture-get :original-file) ""))
1603 (clipboards (delq nil
1604 (list v-i
1605 (org-get-x-clipboard 'PRIMARY)
1606 (org-get-x-clipboard 'CLIPBOARD)
1607 (org-get-x-clipboard 'SECONDARY)
1608 v-c))))
1610 (setq org-store-link-plist (plist-put org-store-link-plist :annotation v-a))
1611 (setq org-store-link-plist (plist-put org-store-link-plist :initial v-i))
1613 (unless template
1614 (setq template "")
1615 (message "no template") (ding)
1616 (sit-for 1))
1617 (save-window-excursion
1618 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1619 (erase-buffer)
1620 (setq buffer-file-name nil)
1621 (setq mark-active nil)
1622 (insert template)
1623 (goto-char (point-min))
1625 ;; %[] insert contents of a file.
1626 (save-excursion
1627 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1628 (let ((filename (expand-file-name (match-string 1)))
1629 (beg (copy-marker (match-beginning 0)))
1630 (end (copy-marker (match-end 0))))
1631 (unless (org-capture-escaped-%)
1632 (delete-region beg end)
1633 (set-marker beg nil)
1634 (set-marker end nil)
1635 (condition-case error
1636 (insert-file-contents filename)
1637 (error
1638 (insert (format "%%![couldn not insert %s: %s]"
1639 filename
1640 error))))))))
1642 ;; Mark %() embedded elisp for later evaluation.
1643 (org-capture-expand-embedded-elisp 'mark)
1645 ;; Expand non-interactive templates.
1646 (let ((regexp "%\\(:[-a-za-z]+\\|<\\([^>\n]+\\)>\\|[aAcfFikKlntTuUx]\\)"))
1647 (save-excursion
1648 (while (re-search-forward regexp nil t)
1649 ;; `org-capture-escaped-%' may modify buffer and cripple
1650 ;; match-data. Use markers instead. Ditto for other
1651 ;; templates.
1652 (let ((pos (copy-marker (match-beginning 0)))
1653 (end (copy-marker (match-end 0)))
1654 (value (match-string 1))
1655 (time-string (match-string 2)))
1656 (unless (org-capture-escaped-%)
1657 (delete-region pos end)
1658 (set-marker pos nil)
1659 (set-marker end nil)
1660 (let ((replacement
1661 (pcase (string-to-char value)
1662 (?< (format-time-string time-string))
1664 (or (plist-get org-store-link-plist (intern value))
1665 ""))
1666 (?i (let ((lead (buffer-substring-no-properties
1667 (line-beginning-position) (point))))
1668 (mapconcat #'identity
1669 (split-string v-i "\n")
1670 (concat "\n" lead))))
1671 (?a v-a)
1672 (?A v-A)
1673 (?c v-c)
1674 (?f v-f)
1675 (?F v-F)
1676 (?k v-k)
1677 (?K v-K)
1678 (?l v-l)
1679 (?n v-n)
1680 (?t v-t)
1681 (?T v-T)
1682 (?u v-u)
1683 (?U v-U)
1684 (?x v-x))))
1685 (insert
1686 (if (org-capture-inside-embedded-elisp-p)
1687 (replace-regexp-in-string "\"" "\\\\\"" replacement)
1688 replacement))))))))
1690 ;; Expand %() embedded Elisp. Limit to Sexp originally marked.
1691 (org-capture-expand-embedded-elisp)
1693 ;; Expand interactive templates. This is the last step so that
1694 ;; template is mostly expanded when prompting happens. Turn on
1695 ;; Org mode and set local variables. This is to support
1696 ;; completion in interactive prompts.
1697 (let ((org-inhibit-startup t)) (org-mode))
1698 (org-clone-local-variables buffer "\\`org-")
1699 (let (strings) ; Stores interactive answers.
1700 (save-excursion
1701 (let ((regexp "%\\^\\(?:{\\([^}]*\\)}\\)?\\([CgGLptTuU]\\)?"))
1702 (while (re-search-forward regexp nil t)
1703 (let* ((items (and (match-end 1)
1704 (save-match-data
1705 (split-string (match-string-no-properties 1)
1706 "|"))))
1707 (key (match-string 2))
1708 (beg (copy-marker (match-beginning 0)))
1709 (end (copy-marker (match-end 0)))
1710 (prompt (nth 0 items))
1711 (default (nth 1 items))
1712 (completions (nthcdr 2 items)))
1713 (unless (org-capture-escaped-%)
1714 (delete-region beg end)
1715 (set-marker beg nil)
1716 (set-marker end nil)
1717 (pcase key
1718 ((or "G" "g")
1719 (let* ((org-last-tags-completion-table
1720 (org-global-tags-completion-table
1721 (cond ((equal key "G") (org-agenda-files))
1722 (file (list file))
1723 (t nil))))
1724 (org-add-colon-after-tag-completion t)
1725 (ins (mapconcat
1726 #'identity
1727 (org-split-string
1728 (completing-read
1729 (if prompt (concat prompt ": ") "Tags: ")
1730 'org-tags-completion-function nil nil nil
1731 'org-tags-history)
1732 "[^[:alnum:]_@#%]+")
1733 ":")))
1734 (when (org-string-nw-p ins)
1735 (unless (eq (char-before) ?:) (insert ":"))
1736 (insert ins)
1737 (unless (eq (char-after) ?:) (insert ":"))
1738 (and (org-at-heading-p)
1739 (let ((org-ignore-region t))
1740 (org-set-tags nil 'align))))))
1741 ("C"
1742 (cond
1743 ((= (length clipboards) 1) (insert (car clipboards)))
1744 ((> (length clipboards) 1)
1745 (insert (read-string "Clipboard/kill value: "
1746 (car clipboards)
1747 '(clipboards . 1)
1748 (car clipboards))))))
1749 ("L"
1750 (cond ((= (length clipboards) 1)
1751 (org-insert-link 0 (car clipboards)))
1752 ((> (length clipboards) 1)
1753 (org-insert-link
1755 (read-string "Clipboard/kill value: "
1756 (car clipboards)
1757 '(clipboards . 1)
1758 (car clipboards))))))
1759 ("p" (org-set-property prompt nil))
1760 ((guard key)
1761 ;; These are the date/time related ones.
1762 (let* ((upcase? (equal (upcase key) key))
1763 (org-time-was-given upcase?)
1764 (org-end-time-was-given)
1765 (time (org-read-date upcase? t nil prompt)))
1766 (org-insert-time-stamp
1767 time org-time-was-given
1768 (member key '("u" "U"))
1769 nil nil (list org-end-time-was-given))))
1771 (push (org-completing-read
1772 (concat (or prompt "Enter string")
1773 (and default (format " [%s]" default))
1774 ": ")
1775 completions nil nil nil nil default)
1776 strings)
1777 (insert (car strings)))))))))
1779 ;; Replace %n escapes with nth %^{...} string.
1780 (setq strings (nreverse strings))
1781 (save-excursion
1782 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
1783 (unless (org-capture-escaped-%)
1784 (replace-match
1785 (nth (1- (string-to-number (match-string 1))) strings)
1786 nil t)))))
1788 ;; Make sure there are no empty lines before the text, and that
1789 ;; it ends with a newline character.
1790 (skip-chars-forward " \t\n")
1791 (delete-region (point-min) (line-beginning-position))
1792 (goto-char (point-max))
1793 (skip-chars-backward " \t\n")
1794 (delete-region (point) (point-max))
1795 (insert "\n")
1797 ;; Return the expanded template and kill the capture buffer.
1798 (untabify (point-min) (point-max))
1799 (set-buffer-modified-p nil)
1800 (prog1 (buffer-substring-no-properties (point-min) (point-max))
1801 (kill-buffer (current-buffer))))))
1803 (defun org-capture-escaped-% ()
1804 "Non-nil if % was escaped.
1805 If yes, unescape it now. Assume match-data contains the
1806 placeholder to check."
1807 (save-excursion
1808 (goto-char (match-beginning 0))
1809 (let ((n (abs (skip-chars-backward "\\\\"))))
1810 (delete-char (/ (1+ n) 2))
1811 (= (% n 2) 1))))
1813 (defun org-capture-expand-embedded-elisp (&optional mark)
1814 "Evaluate embedded elisp %(sexp) and replace with the result.
1815 When optional MARK argument is non-nil, mark Sexp with a text
1816 property (`org-embedded-elisp') for later evaluation. Only
1817 marked Sexp are evaluated when this argument is nil."
1818 (save-excursion
1819 (goto-char (point-min))
1820 (while (re-search-forward "%(" nil t)
1821 (cond
1822 ((get-text-property (match-beginning 0) 'org-embedded-elisp)
1823 (goto-char (match-beginning 0))
1824 (let ((template-start (point)))
1825 (forward-char 1)
1826 (let* ((sexp (read (current-buffer)))
1827 (result (org-eval
1828 (org-capture--expand-keyword-in-embedded-elisp
1829 sexp))))
1830 (delete-region template-start (point))
1831 (cond
1832 ((not result) nil)
1833 ((stringp result) (insert result))
1834 (t (error
1835 "Capture template sexp `%s' must evaluate to string or nil"
1836 sexp))))))
1837 ((not mark) nil)
1838 ;; Only mark valid and non-escaped sexp.
1839 ((org-capture-escaped-%) nil)
1841 (let ((end (with-syntax-table emacs-lisp-mode-syntax-table
1842 (ignore-errors (scan-sexps (1- (point)) 1)))))
1843 (when end
1844 (put-text-property (- (point) 2) end 'org-embedded-elisp t))))))))
1846 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1847 "Recursively replace capture link keywords in ATTR sexp.
1848 Such keywords are prefixed with \"%:\". See
1849 `org-capture-template' for more information."
1850 (cond ((consp attr)
1851 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr))
1852 ((symbolp attr)
1853 (let* ((attr-symbol (symbol-name attr))
1854 (key (and (string-match "%\\(:.*\\)" attr-symbol)
1855 (intern (match-string 1 attr-symbol)))))
1856 (or (plist-get org-store-link-plist key)
1857 attr)))
1858 (t attr)))
1860 (defun org-capture-inside-embedded-elisp-p ()
1861 "Non-nil if point is inside of embedded elisp %(sexp).
1862 Assume sexps have been marked with
1863 `org-capture-expand-embedded-elisp' beforehand."
1864 (get-text-property (point) 'org-embedded-elisp))
1866 ;;;###autoload
1867 (defun org-capture-import-remember-templates ()
1868 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1869 (interactive)
1870 (when (and (yes-or-no-p
1871 "Import old remember templates into org-capture-templates? ")
1872 (yes-or-no-p
1873 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1874 (require 'org-remember)
1875 (setq org-capture-templates
1876 (mapcar
1877 (lambda (entry)
1878 (let ((desc (car entry))
1879 (key (char-to-string (nth 1 entry)))
1880 (template (nth 2 entry))
1881 (file (or (nth 3 entry) org-default-notes-file))
1882 (position (or (nth 4 entry) org-remember-default-headline))
1883 (type 'entry)
1884 (prepend org-reverse-note-order)
1885 immediate target jump-to-captured)
1886 (cond
1887 ((member position '(top bottom))
1888 (setq target (list 'file file)
1889 prepend (eq position 'top)))
1890 ((eq position 'date-tree)
1891 (setq target (list 'file+datetree file)
1892 prepend nil))
1893 (t (setq target (list 'file+headline file position))))
1895 (when (string-match "%!" template)
1896 (setq template (replace-match "" t t template)
1897 immediate t))
1899 (when (string-match "%&" template)
1900 (setq jump-to-captured t))
1902 (append (list key desc type target template)
1903 (if prepend '(:prepend t))
1904 (if immediate '(:immediate-finish t))
1905 (if jump-to-captured '(:jump-to-captured t)))))
1907 org-remember-templates))))
1908 ;;; The function was made obsolete by commit 65399674d5 of
1909 ;;; 2013-02-22. This make-obsolete call was added 2016-09-01.
1910 (make-obsolete 'org-capture-import-remember-templates "use the `org-capture-templates' variable instead." "Org 9.0")
1912 (provide 'org-capture)
1914 ;;; org-capture.el ends here