Fix a docstring
[org-mode/org-tableheadings.git] / lisp / org-capture.el
blob5a2ad9b4b10eb414787ac1515c70f1acf7d4f571
1 ;;; org-capture.el --- Fast note taking in Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2016 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains an alternative implementation of the functionality
28 ;; that used to be provided by org-remember.el. The implementation is more
29 ;; streamlined, can produce more target types (e.g. plain list items or
30 ;; table lines). Also, it does not use a temporary buffer for editing
31 ;; the captured entry - instead it uses an indirect buffer that visits
32 ;; the new entry already in the target buffer (this was an idea by Samuel
33 ;; Wales). John Wiegley's excellent `remember.el' is not needed anymore
34 ;; for this implementation, even though we borrow heavily from its ideas.
36 ;; This implementation heavily draws on ideas by James TD Smith and
37 ;; Samuel Wales, and, of cause, uses John Wiegley's remember.el as inspiration.
39 ;;; TODO
41 ;; - find a clever way to not always insert an annotation maybe a
42 ;; predicate function that can check for conditions for %a to be
43 ;; used. This could be one of the properties.
45 ;; - Should there be plist members that arrange for properties to be
46 ;; asked for, like James proposed in his RFC?
48 ;;; Code:
50 (require 'cl-lib)
51 (require 'org)
53 (declare-function org-at-encrypted-entry-p "org-crypt" ())
54 (declare-function org-datetree-find-date-create "org-datetree"
55 (date &optional keep-restriction))
56 (declare-function org-decrypt-entry "org-crypt" ())
57 (declare-function org-encrypt-entry "org-crypt" ())
58 (declare-function org-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 %\\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 \\[org-capture-finalize], \
467 refile \\[org-capture-refile], abort \\[org-capture-kill].")))
468 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
469 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
470 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
472 ;;; The main commands
474 (defvar org-capture-initial nil)
475 (defvar org-capture-entry nil)
477 ;;;###autoload
478 (defun org-capture-string (string &optional keys)
479 "Capture STRING with the template selected by KEYS."
480 (interactive "sInitial text: \n")
481 (let ((org-capture-initial string)
482 (org-capture-entry (org-capture-select-template keys)))
483 (org-capture)))
485 (defcustom org-capture-templates-contexts nil
486 "Alist of capture templates and valid contexts.
488 For example, if you have a capture template \"c\" and you want
489 this template to be accessible only from `message-mode' buffers,
490 use this:
492 \\='((\"c\" ((in-mode . \"message-mode\"))))
494 Here are the available contexts definitions:
496 in-file: command displayed only in matching files
497 in-mode: command displayed only in matching modes
498 not-in-file: command not displayed in matching files
499 not-in-mode: command not displayed in matching modes
500 in-buffer: command displayed only in matching buffers
501 not-in-buffer: command not displayed in matching buffers
502 [function]: a custom function taking no argument
504 If you define several checks, the agenda command will be
505 accessible if there is at least one valid check.
507 You can also bind a key to another agenda custom command
508 depending on contextual rules.
510 \\='((\"c\" \"d\" ((in-mode . \"message-mode\"))))
512 Here it means: in `message-mode buffers', use \"c\" as the
513 key for the capture template otherwise associated with \"d\".
514 \(The template originally associated with \"d\" is not displayed
515 to avoid duplicates.)"
516 :version "24.3"
517 :group 'org-capture
518 :type '(repeat (list :tag "Rule"
519 (string :tag " Capture key")
520 (string :tag "Replace by template")
521 (repeat :tag "Available when"
522 (choice
523 (cons :tag "Condition"
524 (choice
525 (const :tag "In file" in-file)
526 (const :tag "Not in file" not-in-file)
527 (const :tag "In buffer" in-buffer)
528 (const :tag "Not in buffer" not-in-buffer)
529 (const :tag "In mode" in-mode)
530 (const :tag "Not in mode" not-in-mode))
531 (regexp))
532 (function :tag "Custom function"))))))
534 (defcustom org-capture-use-agenda-date nil
535 "Non-nil means use the date at point when capturing from agendas.
536 When nil, you can still capture using the date at point with \\[org-agenda-capture]."
537 :group 'org-capture
538 :version "24.3"
539 :type 'boolean)
541 ;;;###autoload
542 (defun org-capture (&optional goto keys)
543 "Capture something.
544 \\<org-capture-mode-map>
545 This will let you select a template from `org-capture-templates', and then
546 file the newly captured information. The text is immediately inserted
547 at the target location, and an indirect buffer is shown where you can
548 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
549 of Emacs, so that you can continue your work.
551 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
552 anything, just go to the file/headline where the selected template
553 stores its notes. With a double prefix argument \
554 \\[universal-argument] \\[universal-argument], go to the last note
555 stored.
557 When called with a `C-0' (zero) prefix, insert a template at point.
559 ELisp programs can set KEYS to a string associated with a template
560 in `org-capture-templates'. In this case, interactive selection
561 will be bypassed.
563 If `org-capture-use-agenda-date' is non-nil, capturing from the
564 agenda will use the date at point as the default date. Then, a
565 `C-1' prefix will tell the capture process to use the HH:MM time
566 of the day at point (if any) or the current HH:MM time."
567 (interactive "P")
568 (when (and org-capture-use-agenda-date
569 (eq major-mode 'org-agenda-mode))
570 (setq org-overriding-default-time
571 (org-get-cursor-date (equal goto 1))))
572 (cond
573 ((equal goto '(4)) (org-capture-goto-target))
574 ((equal goto '(16)) (org-capture-goto-last-stored))
576 ;; FIXME: Are these needed?
577 (let* ((orig-buf (current-buffer))
578 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
579 org-capture-link-is-already-stored)
580 (plist-get org-store-link-plist :annotation)
581 (ignore-errors (org-store-link nil))))
582 (entry (or org-capture-entry (org-capture-select-template keys)))
583 initial)
584 (setq initial (or org-capture-initial
585 (and (org-region-active-p)
586 (buffer-substring (point) (mark)))))
587 (when (stringp initial)
588 (remove-text-properties 0 (length initial) '(read-only t) initial))
589 (when (stringp annotation)
590 (remove-text-properties 0 (length annotation)
591 '(read-only t) annotation))
592 (cond
593 ((equal entry "C")
594 (customize-variable 'org-capture-templates))
595 ((equal entry "q")
596 (user-error "Abort"))
598 (org-capture-set-plist entry)
599 (org-capture-get-template)
600 (org-capture-put :original-buffer orig-buf
601 :original-file (or (buffer-file-name orig-buf)
602 (and (featurep 'dired)
603 (car (rassq orig-buf
604 dired-buffers))))
605 :original-file-nondirectory
606 (and (buffer-file-name orig-buf)
607 (file-name-nondirectory
608 (buffer-file-name orig-buf)))
609 :annotation annotation
610 :initial initial
611 :return-to-wconf (current-window-configuration)
612 :default-time
613 (or org-overriding-default-time
614 (org-current-time)))
615 (org-capture-set-target-location)
616 (condition-case error
617 (org-capture-put :template (org-capture-fill-template))
618 ((error quit)
619 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
620 (error "Capture abort: %s" error)))
622 (setq org-capture-clock-keep (org-capture-get :clock-keep))
623 (if (equal goto 0)
624 ;;insert at point
625 (org-capture-insert-template-here)
626 (condition-case error
627 (org-capture-place-template
628 (equal (car (org-capture-get :target)) 'function))
629 ((error quit)
630 (if (and (buffer-base-buffer (current-buffer))
631 (string-prefix-p "CAPTURE-" (buffer-name)))
632 (kill-buffer (current-buffer)))
633 (set-window-configuration (org-capture-get :return-to-wconf))
634 (error "Capture template `%s': %s"
635 (org-capture-get :key)
636 (nth 1 error))))
637 (if (and (derived-mode-p 'org-mode)
638 (org-capture-get :clock-in))
639 (condition-case nil
640 (progn
641 (if (org-clock-is-active)
642 (org-capture-put :interrupted-clock
643 (copy-marker org-clock-marker)))
644 (org-clock-in)
645 (setq-local org-capture-clock-was-started t))
646 (error
647 "Could not start the clock in this capture buffer")))
648 (if (org-capture-get :immediate-finish)
649 (org-capture-finalize)))))))))
651 (defun org-capture-get-template ()
652 "Get the template from a file or a function if necessary."
653 (let ((txt (org-capture-get :template)) file)
654 (cond
655 ((and (listp txt) (eq (car txt) 'file))
656 (if (file-exists-p
657 (setq file (expand-file-name (nth 1 txt) org-directory)))
658 (setq txt (org-file-contents file))
659 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
660 ((and (listp txt) (eq (car txt) 'function))
661 (if (fboundp (nth 1 txt))
662 (setq txt (funcall (nth 1 txt)))
663 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
664 ((not txt) (setq txt ""))
665 ((stringp txt))
666 (t (setq txt "* Invalid capture template")))
667 (org-capture-put :template txt)))
669 (defun org-capture-finalize (&optional stay-with-capture)
670 "Finalize the capture process.
671 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
672 captured item after finalizing."
673 (interactive "P")
674 (when (org-capture-get :jump-to-captured)
675 (setq stay-with-capture t))
676 (unless (and org-capture-mode
677 (buffer-base-buffer (current-buffer)))
678 (error "This does not seem to be a capture buffer for Org-mode"))
680 (run-hooks 'org-capture-prepare-finalize-hook)
682 ;; Did we start the clock in this capture buffer?
683 (when (and org-capture-clock-was-started
684 org-clock-marker (marker-buffer org-clock-marker)
685 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
686 (> org-clock-marker (point-min))
687 (< org-clock-marker (point-max)))
688 ;; Looks like the clock we started is still running. Clock out.
689 (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
690 (when (and (not org-capture-clock-keep)
691 (org-capture-get :clock-resume 'local)
692 (markerp (org-capture-get :interrupted-clock 'local))
693 (buffer-live-p (marker-buffer
694 (org-capture-get :interrupted-clock 'local))))
695 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
696 (org-with-point-at clock-in-task
697 (org-clock-in)))
698 (message "Interrupted clock has been resumed")))
700 (let ((beg (point-min))
701 (end (point-max))
702 (abort-note nil))
703 ;; Store the size of the capture buffer
704 (org-capture-put :captured-entry-size (- (point-max) (point-min)))
705 (widen)
706 ;; Store the insertion point in the target buffer
707 (org-capture-put :insertion-point (point))
709 (if org-note-abort
710 (let ((m1 (org-capture-get :begin-marker 'local))
711 (m2 (org-capture-get :end-marker 'local)))
712 (if (and m1 m2 (= m1 beg) (= m2 end))
713 (progn
714 (setq m2 (if (cdr (assq 'heading org-blank-before-new-entry))
715 m2 (1+ m2))
716 m2 (if (< (point-max) m2) (point-max) m2))
717 (setq abort-note 'clean)
718 (kill-region m1 m2))
719 (setq abort-note 'dirty)))
721 ;; Make sure that the empty lines after are correct
722 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
723 (member (org-capture-get :type 'local)
724 '(entry item checkitem plain)))
725 (save-excursion
726 (goto-char end)
727 (or (bolp) (newline))
728 (org-capture-empty-lines-after
729 (or (org-capture-get :empty-lines-after 'local)
730 (org-capture-get :empty-lines 'local) 0))))
731 ;; Postprocessing: Update Statistics cookies, do the sorting
732 (when (derived-mode-p 'org-mode)
733 (save-excursion
734 (when (ignore-errors (org-back-to-heading))
735 (org-update-parent-todo-statistics)
736 (org-update-checkbox-count)))
737 ;; FIXME Here we should do the sorting
738 ;; If we have added a table line, maybe recompute?
739 (when (and (eq (org-capture-get :type 'local) 'table-line)
740 (org-at-table-p))
741 (if (org-table-get-stored-formulas)
742 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
743 (org-table-align))))
744 ;; Store this place as the last one where we stored something
745 ;; Do the marking in the base buffer, so that it makes sense after
746 ;; the indirect buffer has been killed.
747 (when org-capture-bookmark
748 (org-capture-bookmark-last-stored-position))
750 ;; Run the hook
751 (run-hooks 'org-capture-before-finalize-hook))
753 (when (org-capture-get :decrypted)
754 (save-excursion
755 (goto-char (org-capture-get :decrypted))
756 (org-encrypt-entry)))
758 ;; Kill the indirect buffer
759 (save-buffer)
760 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
761 (new-buffer (org-capture-get :new-buffer 'local))
762 (kill-buffer (org-capture-get :kill-buffer 'local))
763 (base-buffer (buffer-base-buffer (current-buffer))))
765 ;; Kill the indirect buffer
766 (kill-buffer (current-buffer))
768 ;; Narrow back the target buffer to its previous state
769 (with-current-buffer (org-capture-get :buffer)
770 (let ((reg (org-capture-get :initial-target-region))
771 (pos (org-capture-get :initial-target-position))
772 (ipt (org-capture-get :insertion-point))
773 (size (org-capture-get :captured-entry-size)))
774 (if (not reg)
775 (widen)
776 (cond ((< ipt (car reg))
777 ;; insertion point is before the narrowed region
778 (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
779 ((> ipt (cdr reg))
780 ;; insertion point is after the narrowed region
781 (narrow-to-region (car reg) (cdr reg)))
783 ;; insertion point is within the narrowed region
784 (narrow-to-region (car reg) (+ size (cdr reg)))))
785 ;; now place back the point at its original position
786 (if (< ipt (car reg))
787 (goto-char (+ size pos))
788 (goto-char (if (< ipt pos) (+ size pos) pos))))))
790 ;; Kill the target buffer if that is desired
791 (when (and base-buffer new-buffer kill-buffer)
792 (with-current-buffer base-buffer (save-buffer))
793 (kill-buffer base-buffer))
795 ;; Restore the window configuration before capture
796 (set-window-configuration return-wconf))
798 (run-hooks 'org-capture-after-finalize-hook)
799 ;; Special cases
800 (cond
801 (abort-note
802 (cond
803 ((equal abort-note 'clean)
804 (message "Capture process aborted and target buffer cleaned up"))
805 ((equal abort-note 'dirty)
806 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
807 (stay-with-capture
808 (org-capture-goto-last-stored)))
809 ;; Return if we did store something
810 (not abort-note)))
812 (defun org-capture-refile ()
813 "Finalize the current capture and then refile the entry.
814 Refiling is done from the base buffer, because the indirect buffer is then
815 already gone. Any prefix argument will be passed to the refile command."
816 (interactive)
817 (unless (eq (org-capture-get :type 'local) 'entry)
818 (error
819 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
820 (let ((pos (point))
821 (base (buffer-base-buffer (current-buffer)))
822 (org-capture-is-refiling t)
823 (kill-buffer (org-capture-get :kill-buffer 'local)))
824 (org-capture-put :kill-buffer nil)
825 (org-capture-finalize)
826 (save-window-excursion
827 (with-current-buffer (or base (current-buffer))
828 (org-with-wide-buffer
829 (goto-char pos)
830 (call-interactively 'org-refile))))
831 (when kill-buffer (kill-buffer base))))
833 (defun org-capture-kill ()
834 "Abort the current capture process."
835 (interactive)
836 ;; FIXME: This does not do the right thing, we need to remove the
837 ;; new stuff by hand it is easy: undo, then kill the buffer
838 (let ((org-note-abort t)
839 (org-capture-before-finalize-hook nil))
840 (org-capture-finalize)))
842 (defun org-capture-goto-last-stored ()
843 "Go to the location where the last capture note was stored."
844 (interactive)
845 (org-goto-marker-or-bmk org-capture-last-stored-marker
846 (plist-get org-bookmark-names-plist
847 :last-capture))
848 (message "This is the last note stored by a capture process"))
850 ;;; Supporting functions for handling the process
852 (defun org-capture-put-target-region-and-position ()
853 "Store the initial region with `org-capture-put'."
854 (org-capture-put
855 :initial-target-region
856 ;; Check if the buffer is currently narrowed
857 (when (org-buffer-narrowed-p)
858 (cons (point-min) (point-max))))
859 ;; store the current point
860 (org-capture-put :initial-target-position (point)))
862 (defvar org-time-was-given) ; dynamically scoped parameter
863 (defun org-capture-set-target-location (&optional target)
864 "Find TARGET buffer and position.
865 Store them in the capture property list."
866 (let ((target-entry-p t) decrypted-hl-pos)
867 (setq target (or target (org-capture-get :target)))
868 (save-excursion
869 (cond
870 ((eq (car target) 'file)
871 (set-buffer (org-capture-target-buffer (nth 1 target)))
872 (org-capture-put-target-region-and-position)
873 (widen)
874 (setq target-entry-p nil))
876 ((eq (car target) 'id)
877 (let ((loc (org-id-find (nth 1 target))))
878 (if (not loc)
879 (error "Cannot find target ID \"%s\"" (nth 1 target))
880 (set-buffer (org-capture-target-buffer (car loc)))
881 (widen)
882 (org-capture-put-target-region-and-position)
883 (goto-char (cdr loc)))))
885 ((eq (car target) 'file+headline)
886 (set-buffer (org-capture-target-buffer (nth 1 target)))
887 (org-capture-put-target-region-and-position)
888 (widen)
889 (let ((hd (nth 2 target)))
890 (goto-char (point-min))
891 (unless (derived-mode-p 'org-mode)
892 (error
893 "Target buffer \"%s\" for file+headline should be in Org mode"
894 (current-buffer)))
895 (if (re-search-forward
896 (format org-complex-heading-regexp-format (regexp-quote hd))
897 nil t)
898 (goto-char (point-at-bol))
899 (goto-char (point-max))
900 (or (bolp) (insert "\n"))
901 (insert "* " hd "\n")
902 (beginning-of-line 0))))
904 ((eq (car target) 'file+olp)
905 (let ((m (org-find-olp
906 (cons (org-capture-expand-file (nth 1 target))
907 (cddr target)))))
908 (set-buffer (marker-buffer m))
909 (org-capture-put-target-region-and-position)
910 (widen)
911 (goto-char m)))
913 ((eq (car target) 'file+regexp)
914 (set-buffer (org-capture-target-buffer (nth 1 target)))
915 (org-capture-put-target-region-and-position)
916 (widen)
917 (goto-char (point-min))
918 (if (re-search-forward (nth 2 target) nil t)
919 (progn
920 (goto-char (if (org-capture-get :prepend)
921 (match-beginning 0) (match-end 0)))
922 (org-capture-put :exact-position (point))
923 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
924 (error "No match for target regexp in file %s" (nth 1 target))))
926 ((memq (car target) '(file+datetree file+datetree+prompt file+weektree file+weektree+prompt))
927 (require 'org-datetree)
928 (set-buffer (org-capture-target-buffer (nth 1 target)))
929 (org-capture-put-target-region-and-position)
930 (widen)
931 ;; Make a date/week tree entry, with the current date (or
932 ;; yesterday, if we are extending dates for a couple of hours)
933 (funcall
934 (cond
935 ((memq (car target) '(file+weektree file+weektree+prompt))
936 #'org-datetree-find-iso-week-create)
937 (t #'org-datetree-find-date-create))
938 (calendar-gregorian-from-absolute
939 (cond
940 (org-overriding-default-time
941 ;; use the overriding default time
942 (time-to-days org-overriding-default-time))
944 ((memq (car target) '(file+datetree+prompt file+weektree+prompt))
945 ;; prompt for date
946 (let ((prompt-time (org-read-date
947 nil t nil "Date for tree entry:"
948 (current-time))))
949 (org-capture-put
950 :default-time
951 (cond ((and (or (not (boundp 'org-time-was-given))
952 (not org-time-was-given))
953 (not (= (time-to-days prompt-time) (org-today))))
954 ;; Use 00:00 when no time is given for another date than today?
955 (apply #'encode-time
956 (append '(0 0 0)
957 (cl-cdddr (decode-time prompt-time)))))
958 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer)
959 ;; Replace any time range by its start
960 (apply 'encode-time
961 (org-read-date-analyze
962 (replace-match "\\1 \\2" nil nil org-read-date-final-answer)
963 prompt-time (decode-time prompt-time))))
964 (t prompt-time)))
965 (time-to-days prompt-time)))
967 ;; current date, possibly corrected for late night workers
968 (org-today))))))
970 ((eq (car target) 'file+function)
971 (set-buffer (org-capture-target-buffer (nth 1 target)))
972 (org-capture-put-target-region-and-position)
973 (widen)
974 (funcall (nth 2 target))
975 (org-capture-put :exact-position (point))
976 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
978 ((eq (car target) 'function)
979 (funcall (nth 1 target))
980 (org-capture-put :exact-position (point))
981 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
983 ((eq (car target) 'clock)
984 (if (and (markerp org-clock-hd-marker)
985 (marker-buffer org-clock-hd-marker))
986 (progn (set-buffer (marker-buffer org-clock-hd-marker))
987 (org-capture-put-target-region-and-position)
988 (widen)
989 (goto-char org-clock-hd-marker))
990 (error "No running clock that could be used as capture target")))
992 (t (error "Invalid capture target specification")))
994 (when (and (featurep 'org-crypt) (org-at-encrypted-entry-p))
995 (org-decrypt-entry)
996 (setq decrypted-hl-pos
997 (save-excursion (and (org-back-to-heading t) (point)))))
999 (org-capture-put :buffer (current-buffer) :pos (point)
1000 :target-entry-p target-entry-p
1001 :decrypted decrypted-hl-pos))))
1003 (defun org-capture-expand-file (file)
1004 "Expand functions and symbols for FILE.
1005 When FILE is a function, call it. When it is a variable,
1006 retrieve its value. When it is the empty string, return
1007 `org-default-notes-file'. In any other case, return FILE as-is."
1008 (cond
1009 ((equal file "") org-default-notes-file)
1010 ((functionp file) (funcall file))
1011 ((and (symbolp file) (boundp file)) (symbol-value file))
1012 (t file)))
1014 (defun org-capture-target-buffer (file)
1015 "Get a buffer for FILE."
1016 (setq file (org-capture-expand-file file))
1017 (setq file (or (org-string-nw-p file)
1018 org-default-notes-file
1019 (error "No notes file specified, and no default available")))
1020 (or (org-find-base-buffer-visiting file)
1021 (progn (org-capture-put :new-buffer t)
1022 (find-file-noselect (expand-file-name file org-directory)))))
1024 (defun org-capture-place-template (&optional inhibit-wconf-store)
1025 "Insert the template at the target location, and display the buffer.
1026 When `inhibit-wconf-store', don't store the window configuration, as it
1027 may have been stored before."
1028 (unless inhibit-wconf-store
1029 (org-capture-put :return-to-wconf (current-window-configuration)))
1030 (delete-other-windows)
1031 (org-switch-to-buffer-other-window
1032 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
1033 (widen)
1034 (outline-show-all)
1035 (goto-char (org-capture-get :pos))
1036 (setq-local outline-level 'org-outline-level)
1037 (pcase (org-capture-get :type)
1038 ((or `nil `entry) (org-capture-place-entry))
1039 (`table-line (org-capture-place-table-line))
1040 (`plain (org-capture-place-plain-text))
1041 (`item (org-capture-place-item))
1042 (`checkitem (org-capture-place-item)))
1043 (org-capture-mode 1)
1044 (setq-local org-capture-current-plist org-capture-plist))
1046 (defun org-capture-place-entry ()
1047 "Place the template as a new Org entry."
1048 (let* ((txt (org-capture-get :template))
1049 (reversed (org-capture-get :prepend))
1050 (target-entry-p (org-capture-get :target-entry-p))
1051 level beg end)
1053 (and (org-capture-get :exact-position)
1054 (goto-char (org-capture-get :exact-position)))
1055 (cond
1056 ((not target-entry-p)
1057 ;; Insert as top-level entry, either at beginning or at end of
1058 ;; file.
1059 (setq level 1)
1060 (if reversed
1061 (progn (goto-char (point-min))
1062 (or (org-at-heading-p)
1063 (outline-next-heading)))
1064 (goto-char (point-max))
1065 (or (bolp) (insert "\n"))))
1067 ;; Insert as a child of the current entry
1068 (and (looking-at "\\*+")
1069 (setq level (- (match-end 0) (match-beginning 0))))
1070 (setq level (org-get-valid-level (or level 1) 1))
1071 (if reversed
1072 (progn
1073 (outline-next-heading)
1074 (or (bolp) (insert "\n")))
1075 (org-end-of-subtree t nil)
1076 (or (bolp) (insert "\n")))))
1077 (org-capture-empty-lines-before)
1078 (setq beg (point))
1079 (org-capture-verify-tree txt)
1080 (org-paste-subtree level txt 'for-yank)
1081 (org-capture-empty-lines-after 1)
1082 (org-capture-position-for-last-stored beg)
1083 (outline-next-heading)
1084 (setq end (point))
1085 (org-capture-mark-kill-region beg (1- end))
1086 (org-capture-narrow beg (1- end))
1087 (if (or (re-search-backward "%\\?" beg t)
1088 (re-search-forward "%\\?" end t))
1089 (replace-match ""))))
1091 (defun org-capture-place-item ()
1092 "Place the template as a new plain list item."
1093 (let* ((txt (org-capture-get :template))
1094 (target-entry-p (org-capture-get :target-entry-p))
1095 (ind 0)
1096 beg end)
1097 (if (org-capture-get :exact-position)
1098 (goto-char (org-capture-get :exact-position))
1099 (cond
1100 ((not target-entry-p)
1101 ;; Insert as top-level entry, either at beginning or at end of file
1102 (setq beg (point-min) end (point-max)))
1104 (setq beg (1+ (point-at-eol))
1105 end (save-excursion (outline-next-heading) (point)))))
1106 (setq ind nil)
1107 (if (org-capture-get :prepend)
1108 (progn
1109 (goto-char beg)
1110 (when (org-list-search-forward (org-item-beginning-re) end t)
1111 (goto-char (match-beginning 0))
1112 (setq ind (org-get-indentation))))
1113 (goto-char end)
1114 (when (org-list-search-backward (org-item-beginning-re) beg t)
1115 (setq ind (org-get-indentation))
1116 (org-end-of-item)))
1117 (unless ind (goto-char end)))
1118 ;; Remove common indentation
1119 (setq txt (org-remove-indentation txt))
1120 ;; Make sure this is indeed an item
1121 (unless (string-match (concat "\\`" (org-item-re)) txt)
1122 (setq txt (concat "- "
1123 (mapconcat 'identity (split-string txt "\n")
1124 "\n "))))
1125 ;; Prepare surrounding empty lines.
1126 (org-capture-empty-lines-before)
1127 (setq beg (point))
1128 (unless (eolp) (save-excursion (insert "\n")))
1129 (unless ind
1130 (org-indent-line)
1131 (setq ind (org-get-indentation))
1132 (delete-region beg (point)))
1133 ;; Set the correct indentation, depending on context
1134 (setq ind (make-string ind ?\ ))
1135 (setq txt (concat ind
1136 (mapconcat 'identity (split-string txt "\n")
1137 (concat "\n" ind))
1138 "\n"))
1139 ;; Insert item.
1140 (insert txt)
1141 (org-capture-empty-lines-after 1)
1142 (org-capture-position-for-last-stored beg)
1143 (forward-char 1)
1144 (setq end (point))
1145 (org-capture-mark-kill-region beg (1- end))
1146 (org-capture-narrow beg (1- end))
1147 (if (or (re-search-backward "%\\?" beg t)
1148 (re-search-forward "%\\?" end t))
1149 (replace-match ""))))
1151 (defun org-capture-place-table-line ()
1152 "Place the template as a table line."
1153 (require 'org-table)
1154 (let* ((txt (org-capture-get :template))
1155 (target-entry-p (org-capture-get :target-entry-p))
1156 (table-line-pos (org-capture-get :table-line-pos))
1157 beg end)
1158 (cond
1159 ((org-capture-get :exact-position)
1160 (goto-char (org-capture-get :exact-position)))
1161 ((not target-entry-p)
1162 ;; Table is not necessarily under a heading
1163 (setq beg (point-min) end (point-max)))
1165 ;; WE are at a heading, limit search to the body
1166 (setq beg (1+ (point-at-eol))
1167 end (save-excursion (outline-next-heading) (point)))))
1168 (if (re-search-forward org-table-dataline-regexp end t)
1169 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t))
1170 (goto-char e)
1171 (if (looking-at "[ \t]*#\\+tblfm:")
1172 (forward-line 1))
1173 (narrow-to-region b (point)))
1174 (goto-char end)
1175 (insert "\n| |\n|----|\n| |\n")
1176 (narrow-to-region (1+ end) (point)))
1177 ;; We are narrowed to the table, or to an empty line if there was no table
1179 ;; Check if the template is good
1180 (if (not (string-match org-table-dataline-regexp txt))
1181 (setq txt "| %?Bad template |\n"))
1182 (if (functionp table-line-pos)
1183 (setq table-line-pos (funcall table-line-pos))
1184 (setq table-line-pos (eval table-line-pos)))
1185 (cond
1186 ((and table-line-pos
1187 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
1188 (goto-char (point-min))
1189 ;; we have a complex line specification
1190 (let ((ll (ignore-errors
1191 (save-match-data (org-table-analyze))
1192 (aref org-table-hlines
1193 (- (match-end 1) (match-beginning 1)))))
1194 (delta (string-to-number (match-string 2 table-line-pos))))
1195 ;; The user wants a special position in the table
1196 (unless ll
1197 (error "Invalid table line specification \"%s\"" table-line-pos))
1198 (goto-char org-table-current-begin-pos)
1199 (forward-line (+ ll delta (if (< delta 0) 0 -1)))
1200 (org-table-insert-row 'below)
1201 (beginning-of-line 1)
1202 (delete-region (point) (1+ (point-at-eol)))
1203 (setq beg (point))
1204 (insert txt)
1205 (setq end (point))))
1206 ((org-capture-get :prepend)
1207 (goto-char (point-min))
1208 (re-search-forward org-table-hline-regexp nil t)
1209 (beginning-of-line 1)
1210 (re-search-forward org-table-dataline-regexp nil t)
1211 (beginning-of-line 1)
1212 (setq beg (point))
1213 (org-table-insert-row)
1214 (beginning-of-line 1)
1215 (delete-region (point) (1+ (point-at-eol)))
1216 (insert txt)
1217 (setq end (point)))
1219 (goto-char (point-max))
1220 (re-search-backward org-table-dataline-regexp nil t)
1221 (beginning-of-line 1)
1222 (org-table-insert-row 'below)
1223 (beginning-of-line 1)
1224 (delete-region (point) (1+ (point-at-eol)))
1225 (setq beg (point))
1226 (insert txt)
1227 (setq end (point))))
1228 (goto-char beg)
1229 (org-capture-position-for-last-stored 'table-line)
1230 (if (or (re-search-backward "%\\?" beg t)
1231 (re-search-forward "%\\?" end t))
1232 (replace-match ""))
1233 (org-table-align)))
1235 (defun org-capture-place-plain-text ()
1236 "Place the template plainly.
1237 If the target locator points at an Org node, place the template into
1238 the text of the entry, before the first child. If not, place the
1239 template at the beginning or end of the file.
1240 Of course, if exact position has been required, just put it there."
1241 (let* ((txt (org-capture-get :template))
1242 beg end)
1243 (cond
1244 ((org-capture-get :exact-position)
1245 (goto-char (org-capture-get :exact-position)))
1246 ((and (org-capture-get :target-entry-p)
1247 (bolp)
1248 (looking-at org-outline-regexp))
1249 ;; we should place the text into this entry
1250 (if (org-capture-get :prepend)
1251 ;; Skip meta data and drawers
1252 (org-end-of-meta-data t)
1253 ;; go to ent of the entry text, before the next headline
1254 (outline-next-heading)))
1256 ;; beginning or end of file
1257 (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
1258 (or (bolp) (newline))
1259 (org-capture-empty-lines-before)
1260 (setq beg (point))
1261 (insert txt)
1262 (org-capture-empty-lines-after 1)
1263 (org-capture-position-for-last-stored beg)
1264 (setq end (point))
1265 (org-capture-mark-kill-region beg (1- end))
1266 (org-capture-narrow beg (1- end))
1267 (if (or (re-search-backward "%\\?" beg t)
1268 (re-search-forward "%\\?" end t))
1269 (replace-match ""))))
1271 (defun org-capture-mark-kill-region (beg end)
1272 "Mark the region that will have to be killed when aborting capture."
1273 (let ((m1 (move-marker (make-marker) beg))
1274 (m2 (move-marker (make-marker) end)))
1275 (org-capture-put :begin-marker m1)
1276 (org-capture-put :end-marker m2)))
1278 (defun org-capture-position-for-last-stored (where)
1279 "Memorize the position that should later become the position of last capture."
1280 (cond
1281 ((integerp where)
1282 (org-capture-put :position-for-last-stored
1283 (move-marker (make-marker) where
1284 (or (buffer-base-buffer (current-buffer))
1285 (current-buffer)))))
1286 ((eq where 'table-line)
1287 (org-capture-put :position-for-last-stored
1288 (list 'table-line
1289 (org-table-current-dline))))
1290 (t (error "This should not happen"))))
1292 (defun org-capture-bookmark-last-stored-position ()
1293 "Bookmark the last-captured position."
1294 (let* ((where (org-capture-get :position-for-last-stored 'local))
1295 (pos (cond
1296 ((markerp where)
1297 (prog1 (marker-position where)
1298 (move-marker where nil)))
1299 ((and (listp where) (eq (car where) 'table-line))
1300 (if (org-at-table-p)
1301 (save-excursion
1302 (org-table-goto-line (nth 1 where))
1303 (point-at-bol))
1304 (point))))))
1305 (with-current-buffer (buffer-base-buffer (current-buffer))
1306 (org-with-wide-buffer
1307 (goto-char pos)
1308 (let ((bookmark-name (plist-get org-bookmark-names-plist
1309 :last-capture)))
1310 (when bookmark-name
1311 (with-demoted-errors
1312 (bookmark-set bookmark-name))))
1313 (move-marker org-capture-last-stored-marker (point))))))
1315 (defun org-capture-narrow (beg end)
1316 "Narrow, unless configuration says not to narrow."
1317 (unless (org-capture-get :unnarrowed)
1318 (narrow-to-region beg end)
1319 (goto-char beg)))
1321 (defun org-capture-empty-lines-before (&optional n)
1322 "Set the correct number of empty lines before the insertion point.
1323 Point will be after the empty lines, so insertion can directly be done."
1324 (setq n (or n (org-capture-get :empty-lines-before)
1325 (org-capture-get :empty-lines) 0))
1326 (let ((pos (point)))
1327 (org-back-over-empty-lines)
1328 (delete-region (point) pos)
1329 (if (> n 0) (newline n))))
1331 (defun org-capture-empty-lines-after (&optional n)
1332 "Set the correct number of empty lines after the inserted string.
1333 Point will remain at the first line after the inserted text."
1334 (setq n (or n (org-capture-get :empty-lines-after)
1335 (org-capture-get :empty-lines) 0))
1336 (org-back-over-empty-lines)
1337 (while (looking-at "[ \t]*\n") (replace-match ""))
1338 (let ((pos (point)))
1339 (if (> n 0) (newline n))
1340 (goto-char pos)))
1342 (defvar org-clock-marker) ; Defined in org.el
1344 (defun org-capture-insert-template-here ()
1345 "Insert the capture template at point."
1346 (let* ((template (org-capture-get :template))
1347 (type (org-capture-get :type))
1348 beg end pp)
1349 (or (bolp) (newline))
1350 (setq beg (point))
1351 (cond
1352 ((and (eq type 'entry) (derived-mode-p 'org-mode))
1353 (org-capture-verify-tree (org-capture-get :template))
1354 (org-paste-subtree nil template t))
1355 ((and (memq type '(item checkitem))
1356 (derived-mode-p 'org-mode)
1357 (save-excursion (skip-chars-backward " \t\n")
1358 (setq pp (point))
1359 (org-in-item-p)))
1360 (goto-char pp)
1361 (org-insert-item)
1362 (skip-chars-backward " ")
1363 (skip-chars-backward "-+*0123456789).")
1364 (delete-region (point) (point-at-eol))
1365 (setq beg (point))
1366 (org-remove-indentation template)
1367 (insert template)
1368 (org-capture-empty-lines-after)
1369 (goto-char beg)
1370 (org-list-repair)
1371 (org-end-of-item)
1372 (setq end (point)))
1373 (t (insert template)))
1374 (setq end (point))
1375 (goto-char beg)
1376 (if (re-search-forward "%\\?" end t)
1377 (replace-match ""))))
1379 (defun org-capture-set-plist (entry)
1380 "Initialize the property list from the template definition."
1381 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1382 (org-capture-put :key (car entry) :description (nth 1 entry)
1383 :target (nth 3 entry))
1384 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1385 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1386 ;; The template may be empty or omitted for special types.
1387 ;; Here we insert the default templates for such cases.
1388 (cond
1389 ((eq type 'item) (setq txt "- %?"))
1390 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1391 ((eq type 'table-line) (setq txt "| %? |"))
1392 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1393 (org-capture-put :template txt :type type)))
1395 (defun org-capture-goto-target (&optional template-key)
1396 "Go to the target location of a capture template.
1397 The user is queried for the template."
1398 (interactive)
1399 (let ((entry (org-capture-select-template template-key)))
1400 (unless entry (error "No capture template selected"))
1401 (org-capture-set-plist entry)
1402 (org-capture-set-target-location)
1403 (pop-to-buffer-same-window (org-capture-get :buffer))
1404 (goto-char (org-capture-get :pos))))
1406 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1407 "Make an indirect buffer for a capture process.
1408 Use PREFIX as a prefix for the name of the indirect buffer."
1409 (setq buffer (or buffer (current-buffer)))
1410 (let ((n 1) (base (buffer-name buffer)) bname)
1411 (setq bname (concat prefix "-" base))
1412 (while (buffer-live-p (get-buffer bname))
1413 (setq bname (concat prefix "-" (number-to-string (cl-incf n)) "-" base)))
1414 (condition-case nil
1415 (make-indirect-buffer buffer bname 'clone)
1416 (error
1417 (let ((buf (make-indirect-buffer buffer bname)))
1418 (with-current-buffer buf (org-mode))
1419 buf)))))
1421 (defun org-capture-verify-tree (tree)
1422 "Throw error if TREE is not a valid tree."
1423 (unless (org-kill-is-subtree-p tree)
1424 (error "Template is not a valid Org entry or tree")))
1426 (defun org-mks (table title &optional prompt specials)
1427 "Select a member of an alist with multiple keys.
1428 TABLE is the alist which should contain entries where the car is a string.
1429 There should be two types of entries.
1431 1. prefix descriptions like (\"a\" \"Description\")
1432 This indicates that `a' is a prefix key for multi-letter selection, and
1433 that there are entries following with keys like \"ab\", \"ax\"...
1435 2. Selectable members must have more than two elements, with the first
1436 being the string of keys that lead to selecting it, and the second a
1437 short description string of the item.
1439 The command will then make a temporary buffer listing all entries
1440 that can be selected with a single key, and all the single key
1441 prefixes. When you press the key for a single-letter entry, it is selected.
1442 When you press a prefix key, the commands (and maybe further prefixes)
1443 under this key will be shown and offered for selection.
1445 TITLE will be placed over the selection in the temporary buffer,
1446 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1447 also (\"key\" \"description\") entries. When one of these is selection,
1448 only the bare key is returned."
1449 (setq prompt (or prompt "Select: "))
1450 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1451 current prefix rtn re pressed buffer (inhibit-quit t))
1452 (save-window-excursion
1453 (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
1454 (setq orig-table table)
1455 (catch 'exit
1456 (while t
1457 (erase-buffer)
1458 (insert title "\n\n")
1459 (setq tbl table
1460 des-keys nil
1461 allowed-keys nil
1462 cursor-type nil)
1463 (setq prefix (if current (concat current " ") ""))
1464 (while tbl
1465 (cond
1466 ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
1467 ;; This is a description on this level
1468 (setq dkey (caar tbl) ddesc (cl-cadar tbl))
1469 (pop tbl)
1470 (push dkey des-keys)
1471 (push dkey allowed-keys)
1472 (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
1473 ;; Skip keys which are below this prefix
1474 (setq re (concat "\\`" (regexp-quote dkey)))
1475 (let (case-fold-search)
1476 (while (and tbl (string-match re (caar tbl))) (pop tbl))))
1477 ((= 2 (length (car tbl)))
1478 ;; Not yet a usable description, skip it
1481 ;; usable entry on this level
1482 (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
1483 (push (caar tbl) allowed-keys)
1484 (pop tbl))))
1485 (when specials
1486 (insert "-------------------------------------------------------------------------------\n")
1487 (let ((sp specials))
1488 (while sp
1489 (insert (format "[%s] %s\n"
1490 (caar sp) (nth 1 (car sp))))
1491 (push (caar sp) allowed-keys)
1492 (pop sp))))
1493 (push "\C-g" allowed-keys)
1494 (goto-char (point-min))
1495 (if (not (pos-visible-in-window-p (point-max)))
1496 (org-fit-window-to-buffer))
1497 (message prompt)
1498 (setq pressed (char-to-string (read-char-exclusive)))
1499 (while (not (member pressed allowed-keys))
1500 (message "Invalid key `%s'" pressed) (sit-for 1)
1501 (message prompt)
1502 (setq pressed (char-to-string (read-char-exclusive))))
1503 (when (equal pressed "\C-g")
1504 (kill-buffer buffer)
1505 (user-error "Abort"))
1506 (when (and (not (assoc pressed table))
1507 (not (member pressed des-keys))
1508 (assoc pressed specials))
1509 (throw 'exit (setq rtn pressed)))
1510 (unless (member pressed des-keys)
1511 (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
1512 orig-table))))
1513 (setq current (concat current pressed))
1514 (setq table (mapcar
1515 (lambda (x)
1516 (if (and (> (length (car x)) 1)
1517 (equal (substring (car x) 0 1) pressed))
1518 (cons (substring (car x) 1) (cdr x))
1519 nil))
1520 table))
1521 (setq table (remove nil table)))))
1522 (when buffer (kill-buffer buffer))
1523 rtn))
1525 ;;; The template code
1526 (defun org-capture-select-template (&optional keys)
1527 "Select a capture template.
1528 Lisp programs can force the template by setting KEYS to a string."
1529 (let ((org-capture-templates
1530 (or (org-contextualize-keys
1531 org-capture-templates org-capture-templates-contexts)
1532 '(("t" "Task" entry (file+headline "" "Tasks")
1533 "* TODO %?\n %u\n %a")))))
1534 (if keys
1535 (or (assoc keys org-capture-templates)
1536 (error "No capture template referred to by \"%s\" keys" keys))
1537 (org-mks org-capture-templates
1538 "Select a capture template\n========================="
1539 "Template key: "
1540 '(("C" "Customize org-capture-templates")
1541 ("q" "Abort"))))))
1543 (defun org-capture-fill-template (&optional template initial annotation)
1544 "Fill a template and return the filled template as a string.
1545 The template may still contain \"%?\" for cursor positioning."
1546 (let* ((template (or template (org-capture-get :template)))
1547 (buffer (org-capture-get :buffer))
1548 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1549 (time (let* ((c (or (org-capture-get :default-time) (current-time)))
1550 (d (decode-time c)))
1551 (if (< (nth 2 d) org-extend-today-until)
1552 (encode-time 0 59 23 (1- (nth 3 d)) (nth 4 d) (nth 5 d))
1553 c)))
1554 (v-t (format-time-string (org-time-stamp-format nil) time))
1555 (v-T (format-time-string (org-time-stamp-format t) time))
1556 (v-u (format-time-string (org-time-stamp-format nil t) time))
1557 (v-U (format-time-string (org-time-stamp-format t t) time))
1558 (v-c (and kill-ring (current-kill 0)))
1559 (v-x (or (org-get-x-clipboard 'PRIMARY)
1560 (org-get-x-clipboard 'CLIPBOARD)
1561 (org-get-x-clipboard 'SECONDARY)))
1562 ;; `initial' and `annotation' might have been passed. But if
1563 ;; the property list has them, we prefer those values.
1564 (v-i (or (plist-get org-store-link-plist :initial)
1565 (and (stringp initial) (org-no-properties initial))
1566 (org-capture-get :initial)
1567 ""))
1568 (v-a
1569 (let ((a (or (plist-get org-store-link-plist :annotation)
1570 annotation
1571 (org-capture-get :annotation)
1572 "")))
1573 ;; Is the link empty? Then we do not want it...
1574 (if (equal a "[[]]") "" a)))
1575 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1576 (v-A (if (and v-a (string-match l-re v-a))
1577 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
1578 v-a))
1579 (v-l (if (and v-a (string-match l-re v-a))
1580 (replace-match "\\1" nil nil v-a)
1581 v-a))
1582 (v-n user-full-name)
1583 (v-k (and (marker-buffer org-clock-marker)
1584 (org-no-properties org-clock-heading)))
1585 (v-K (if (marker-buffer org-clock-marker)
1586 (org-make-link-string
1587 (buffer-file-name (marker-buffer org-clock-marker))
1588 org-clock-heading)))
1589 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1590 (v-F (or (org-capture-get :original-file) ""))
1591 (clipboards (delq nil
1592 (list v-i
1593 (org-get-x-clipboard 'PRIMARY)
1594 (org-get-x-clipboard 'CLIPBOARD)
1595 (org-get-x-clipboard 'SECONDARY)
1596 v-c))))
1598 (setq org-store-link-plist (plist-put org-store-link-plist :annotation v-a))
1599 (setq org-store-link-plist (plist-put org-store-link-plist :initial v-i))
1601 (unless template
1602 (setq template "")
1603 (message "no template") (ding)
1604 (sit-for 1))
1605 (save-window-excursion
1606 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1607 (erase-buffer)
1608 (setq buffer-file-name nil)
1609 (setq mark-active nil)
1610 (insert template)
1611 (goto-char (point-min))
1613 ;; %[] insert contents of a file.
1614 (save-excursion
1615 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1616 (let ((filename (expand-file-name (match-string 1)))
1617 (beg (copy-marker (match-beginning 0)))
1618 (end (copy-marker (match-end 0))))
1619 (unless (org-capture-escaped-%)
1620 (delete-region beg end)
1621 (set-marker beg nil)
1622 (set-marker end nil)
1623 (condition-case error
1624 (insert-file-contents filename)
1625 (error
1626 (insert (format "%%![couldn not insert %s: %s]"
1627 filename
1628 error))))))))
1630 ;; Mark %() embedded elisp for later evaluation.
1631 (org-capture-expand-embedded-elisp 'mark)
1633 ;; Expand non-interactive templates.
1634 (let ((regexp "%\\(:[-a-za-z]+\\|<\\([^>\n]+\\)>\\|[aAcfFikKlntTuUx]\\)"))
1635 (save-excursion
1636 (while (re-search-forward regexp nil t)
1637 ;; `org-capture-escaped-%' may modify buffer and cripple
1638 ;; match-data. Use markers instead. Ditto for other
1639 ;; templates.
1640 (let ((pos (copy-marker (match-beginning 0)))
1641 (end (copy-marker (match-end 0)))
1642 (value (match-string 1))
1643 (time-string (match-string 2)))
1644 (unless (org-capture-escaped-%)
1645 (delete-region pos end)
1646 (set-marker pos nil)
1647 (set-marker end nil)
1648 (let ((replacement
1649 (pcase (string-to-char value)
1650 (?< (format-time-string time-string))
1652 (or (plist-get org-store-link-plist (intern value))
1653 ""))
1654 (?i (let ((lead (buffer-substring-no-properties
1655 (line-beginning-position) (point))))
1656 (mapconcat #'identity
1657 (split-string v-i "\n")
1658 (concat "\n" lead))))
1659 (?a v-a)
1660 (?A v-A)
1661 (?c v-c)
1662 (?f v-f)
1663 (?F v-F)
1664 (?k v-k)
1665 (?K v-K)
1666 (?l v-l)
1667 (?n v-n)
1668 (?t v-t)
1669 (?T v-T)
1670 (?u v-u)
1671 (?U v-U)
1672 (?x v-x))))
1673 (insert
1674 (if (org-capture-inside-embedded-elisp-p)
1675 (replace-regexp-in-string "\"" "\\\\\"" replacement)
1676 replacement))))))))
1678 ;; Expand %() embedded Elisp. Limit to Sexp originally marked.
1679 (org-capture-expand-embedded-elisp)
1681 ;; Expand interactive templates. This is the last step so that
1682 ;; template is mostly expanded when prompting happens. Turn on
1683 ;; Org mode and set local variables. This is to support
1684 ;; completion in interactive prompts.
1685 (let ((org-inhibit-startup t)) (org-mode))
1686 (org-clone-local-variables buffer "\\`org-")
1687 (let (strings) ; Stores interactive answers.
1688 (save-excursion
1689 (let ((regexp "%\\^\\(?:{\\([^}]*\\)}\\)?\\([CgGLptTuU]\\)?"))
1690 (while (re-search-forward regexp nil t)
1691 (let* ((items (and (match-end 1)
1692 (save-match-data
1693 (split-string (match-string-no-properties 1)
1694 "|"))))
1695 (key (match-string 2))
1696 (beg (copy-marker (match-beginning 0)))
1697 (end (copy-marker (match-end 0)))
1698 (prompt (nth 0 items))
1699 (default (nth 1 items))
1700 (completions (nthcdr 2 items)))
1701 (unless (org-capture-escaped-%)
1702 (delete-region beg end)
1703 (set-marker beg nil)
1704 (set-marker end nil)
1705 (pcase key
1706 ((or "G" "g")
1707 (let* ((org-last-tags-completion-table
1708 (org-global-tags-completion-table
1709 (cond ((equal key "G") (org-agenda-files))
1710 (file (list file))
1711 (t nil))))
1712 (org-add-colon-after-tag-completion t)
1713 (ins (mapconcat
1714 #'identity
1715 (org-split-string
1716 (completing-read
1717 (if prompt (concat prompt ": ") "Tags: ")
1718 'org-tags-completion-function nil nil nil
1719 'org-tags-history)
1720 "[^[:alnum:]_@#%]+")
1721 ":")))
1722 (when (org-string-nw-p ins)
1723 (unless (eq (char-before) ?:) (insert ":"))
1724 (insert ins)
1725 (unless (eq (char-after) ?:) (insert ":"))
1726 (and (org-at-heading-p)
1727 (let ((org-ignore-region t))
1728 (org-set-tags nil 'align))))))
1729 ("C"
1730 (cond
1731 ((= (length clipboards) 1) (insert (car clipboards)))
1732 ((> (length clipboards) 1)
1733 (insert (read-string "Clipboard/kill value: "
1734 (car clipboards)
1735 '(clipboards . 1)
1736 (car clipboards))))))
1737 ("L"
1738 (cond ((= (length clipboards) 1)
1739 (org-insert-link 0 (car clipboards)))
1740 ((> (length clipboards) 1)
1741 (org-insert-link
1743 (read-string "Clipboard/kill value: "
1744 (car clipboards)
1745 '(clipboards . 1)
1746 (car clipboards))))))
1747 ("p" (org-set-property prompt nil))
1748 ((guard key)
1749 ;; These are the date/time related ones.
1750 (let* ((upcase? (equal (upcase key) key))
1751 (org-time-was-given upcase?)
1752 (org-end-time-was-given)
1753 (time (org-read-date upcase? t nil prompt)))
1754 (org-insert-time-stamp
1755 time org-time-was-given
1756 (member key '("u" "U"))
1757 nil nil (list org-end-time-was-given))))
1759 (push (org-completing-read
1760 (concat (or prompt "Enter string")
1761 (and default (format " [%s]" default))
1762 ": ")
1763 completions nil nil nil nil default)
1764 strings)
1765 (insert (car strings)))))))))
1767 ;; Replace %n escapes with nth %^{...} string.
1768 (setq strings (nreverse strings))
1769 (save-excursion
1770 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
1771 (unless (org-capture-escaped-%)
1772 (replace-match
1773 (nth (1- (string-to-number (match-string 1))) strings)
1774 nil t)))))
1776 ;; Make sure there are no empty lines before the text, and that
1777 ;; it ends with a newline character.
1778 (skip-chars-forward " \t\n")
1779 (delete-region (point-min) (line-beginning-position))
1780 (goto-char (point-max))
1781 (skip-chars-backward " \t\n")
1782 (delete-region (point) (point-max))
1783 (insert "\n")
1785 ;; Return the expanded template and kill the capture buffer.
1786 (untabify (point-min) (point-max))
1787 (set-buffer-modified-p nil)
1788 (prog1 (buffer-substring-no-properties (point-min) (point-max))
1789 (kill-buffer (current-buffer))))))
1791 (defun org-capture-escaped-% ()
1792 "Non-nil if % was escaped.
1793 If yes, unescape it now. Assume match-data contains the
1794 placeholder to check."
1795 (save-excursion
1796 (goto-char (match-beginning 0))
1797 (let ((n (abs (skip-chars-backward "\\\\"))))
1798 (delete-char (/ (1+ n) 2))
1799 (= (% n 2) 1))))
1801 (defun org-capture-expand-embedded-elisp (&optional mark)
1802 "Evaluate embedded elisp %(sexp) and replace with the result.
1803 When optional MARK argument is non-nil, mark Sexp with a text
1804 property (`org-embedded-elisp') for later evaluation. Only
1805 marked Sexp are evaluated when this argument is nil."
1806 (save-excursion
1807 (goto-char (point-min))
1808 (while (re-search-forward "%(" nil t)
1809 (cond
1810 ((get-text-property (match-beginning 0) 'org-embedded-elisp)
1811 (goto-char (match-beginning 0))
1812 (let ((template-start (point)))
1813 (forward-char 1)
1814 (let* ((sexp (read (current-buffer)))
1815 (result (org-eval
1816 (org-capture--expand-keyword-in-embedded-elisp
1817 sexp))))
1818 (delete-region template-start (point))
1819 (cond
1820 ((not result) nil)
1821 ((stringp result) (insert result))
1822 (t (error
1823 "Capture template sexp `%s' must evaluate to string or nil"
1824 sexp))))))
1825 ((not mark) nil)
1826 ;; Only mark valid and non-escaped sexp.
1827 ((org-capture-escaped-%) nil)
1829 (let ((end (with-syntax-table emacs-lisp-mode-syntax-table
1830 (ignore-errors (scan-sexps (1- (point)) 1)))))
1831 (when end
1832 (put-text-property (- (point) 2) end 'org-embedded-elisp t))))))))
1834 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1835 "Recursively replace capture link keywords in ATTR sexp.
1836 Such keywords are prefixed with \"%:\". See
1837 `org-capture-template' for more information."
1838 (cond ((consp attr)
1839 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr))
1840 ((symbolp attr)
1841 (let* ((attr-symbol (symbol-name attr))
1842 (key (and (string-match "%\\(:.*\\)" attr-symbol)
1843 (intern (match-string 1 attr-symbol)))))
1844 (or (plist-get org-store-link-plist key)
1845 attr)))
1846 (t attr)))
1848 (defun org-capture-inside-embedded-elisp-p ()
1849 "Non-nil if point is inside of embedded elisp %(sexp).
1850 Assume sexps have been marked with
1851 `org-capture-expand-embedded-elisp' beforehand."
1852 (get-text-property (point) 'org-embedded-elisp))
1854 ;;;###autoload
1855 (defun org-capture-import-remember-templates ()
1856 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1857 (interactive)
1858 (when (and (yes-or-no-p
1859 "Import old remember templates into org-capture-templates? ")
1860 (yes-or-no-p
1861 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1862 (require 'org-remember)
1863 (setq org-capture-templates
1864 (mapcar
1865 (lambda (entry)
1866 (let ((desc (car entry))
1867 (key (char-to-string (nth 1 entry)))
1868 (template (nth 2 entry))
1869 (file (or (nth 3 entry) org-default-notes-file))
1870 (position (or (nth 4 entry) org-remember-default-headline))
1871 (type 'entry)
1872 (prepend org-reverse-note-order)
1873 immediate target jump-to-captured)
1874 (cond
1875 ((member position '(top bottom))
1876 (setq target (list 'file file)
1877 prepend (eq position 'top)))
1878 ((eq position 'date-tree)
1879 (setq target (list 'file+datetree file)
1880 prepend nil))
1881 (t (setq target (list 'file+headline file position))))
1883 (when (string-match "%!" template)
1884 (setq template (replace-match "" t t template)
1885 immediate t))
1887 (when (string-match "%&" template)
1888 (setq jump-to-captured t))
1890 (append (list key desc type target template)
1891 (if prepend '(:prepend t))
1892 (if immediate '(:immediate-finish t))
1893 (if jump-to-captured '(:jump-to-captured t)))))
1895 org-remember-templates))))
1896 ;;; The function was made obsolete by commit 65399674d5 of
1897 ;;; 2013-02-22. This make-obsolete call was added 2016-09-01.
1898 (make-obsolete 'org-capture-import-remember-templates "use the `org-capture-templates' variable instead." "Org 9.0")
1900 (provide 'org-capture)
1902 ;;; org-capture.el ends here