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