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