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