Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-capture.el
blob8fdb1ceeecf0a37351e65600ba471bd58a7f8eb8
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-pop-to-buffer-same-window "org-compat"
59 (&optional buffer-or-name norecord label))
60 (declare-function org-table-analyze "org-table" ())
61 (declare-function org-table-goto-line "org-table" (N))
63 (defvar org-end-time-was-given)
64 (defvar org-remember-default-headline)
65 (defvar org-remember-templates)
66 (defvar org-table-hlines)
67 (defvar org-table-current-begin-pos)
68 (defvar dired-buffers)
70 (defvar org-capture-clock-was-started nil
71 "Internal flag, noting if the clock was started.")
73 (defvar org-capture-last-stored-marker (make-marker)
74 "Marker pointing to the entry most recently stored with `org-capture'.")
76 ;; The following variable is scoped dynamically by org-protocol
77 ;; to indicate that the link properties have already been stored
78 (defvar org-capture-link-is-already-stored nil)
80 (defvar org-capture-is-refiling nil
81 "Non-nil when capture process is refiling an entry.")
83 (defgroup org-capture nil
84 "Options concerning capturing new entries."
85 :tag "Org Capture"
86 :group 'org)
88 (defcustom org-capture-templates nil
89 "Templates for the creation of new entries.
91 Each entry is a list with the following items:
93 keys The keys that will select the template, as a string, characters
94 only, for example \"a\" for a template to be selected with a
95 single key, or \"bt\" for selection with two keys. When using
96 several keys, keys using the same prefix key must be together
97 in the list and preceded by a 2-element entry explaining the
98 prefix key, for example
100 (\"b\" \"Templates for marking stuff to buy\")
102 The \"C\" key is used by default for quick access to the
103 customization of the template variable. But if you want to use
104 that key for a template, you can.
106 description A short string describing the template, will be shown during
107 selection.
109 type The type of entry. Valid types are:
110 entry an Org-mode node, with a headline. Will be
111 filed as the child of the target entry or as
112 a top-level entry.
113 item a plain list item, will be placed in the
114 first plain list at the target
115 location.
116 checkitem a checkbox item. This differs from the
117 plain list item only is so far as it uses a
118 different default template.
119 table-line a new line in the first table at target location.
120 plain text to be inserted as it is.
122 target Specification of where the captured item should be placed.
123 In Org-mode files, targets usually define a node. Entries will
124 become children of this node, other types will be added to the
125 table or list in the body of this node.
127 Most target specifications contain a file name. If that file
128 name is the empty string, it defaults to `org-default-notes-file'.
129 A file can also be given as a variable, function, or Emacs Lisp
130 form. When an absolute path is not specified for a
131 target, it is taken as relative to `org-directory'.
133 Valid values are:
135 (file \"path/to/file\")
136 Text will be placed at the beginning or end of that file
138 (id \"id of existing org entry\")
139 File as child of this entry, or in the body of the entry
141 (file+headline \"path/to/file\" \"node headline\")
142 Fast configuration if the target heading is unique in the file
144 (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
145 For non-unique headings, the full path is safer
147 (file+regexp \"path/to/file\" \"regexp to find location\")
148 File to the entry matching regexp
150 (file+datetree \"path/to/file\")
151 Will create a heading in a date tree for today's date
153 (file+datetree+prompt \"path/to/file\")
154 Will create a heading in a date tree, prompts for date
156 (file+weektree \"path/to/file\")
157 Will create a heading in a week tree for today's date
159 (file+weektree+prompt \"path/to/file\")
160 Will create a heading in a week tree, prompts for date
162 (file+function \"path/to/file\" function-finding-location)
163 A function to find the right location in the file
165 (clock)
166 File to the entry that is currently being clocked
168 (function function-finding-location)
169 Most general way, write your own function to find both
170 file and location
172 template The template for creating the capture item. If you leave this
173 empty, an appropriate default template will be used. See below
174 for more details. Instead of a string, this may also be one of
176 (file \"/path/to/template-file\")
177 (function function-returning-the-template)
179 in order to get a template from a file, or dynamically
180 from a function.
182 The rest of the entry is a property list of additional options. Recognized
183 properties are:
185 :prepend Normally newly captured information will be appended at
186 the target location (last child, last table line,
187 last list item...). Setting this property will
188 change that.
190 :immediate-finish When set, do not offer to edit the information, just
191 file it away immediately. This makes sense if the
192 template only needs information that can be added
193 automatically.
195 :jump-to-captured When set, jump to the captured entry when finished.
197 :empty-lines Set this to the number of lines the should be inserted
198 before and after the new item. Default 0, only common
199 other value is 1.
201 :empty-lines-before Set this to the number of lines the should be inserted
202 before the new item. Overrides :empty-lines for the
203 number lines inserted before.
205 :empty-lines-after Set this to the number of lines the should be inserted
206 after the new item. Overrides :empty-lines for the
207 number of lines inserted after.
209 :clock-in Start the clock in this item.
211 :clock-keep Keep the clock running when filing the captured entry.
213 :clock-resume Start the interrupted clock when finishing the capture.
214 Note that :clock-keep has precedence over :clock-resume.
215 When setting both to t, the current clock will run and
216 the previous one will not be resumed.
218 :unnarrowed Do not narrow the target buffer, simply show the
219 full buffer. Default is to narrow it so that you
220 only see the new stuff.
222 :table-line-pos Specification of the location in the table where the
223 new line should be inserted. It should be a string like
224 \"II-3\", meaning that the new line should become the
225 third line before the second horizontal separator line.
227 :kill-buffer If the target file was not yet visited by a buffer when
228 capture was invoked, kill the buffer again after capture
229 is finalized.
231 The template defines the text to be inserted. Often this is an
232 Org mode entry (so the first line should start with a star) that
233 will be filed as a child of the target headline. It can also be
234 freely formatted text. Furthermore, the following %-escapes will
235 be replaced with content and expanded:
237 %[pathname] Insert the contents of the file given by
238 `pathname'. These placeholders are expanded at the very
239 beginning of the process so they can be used to extend the
240 current template.
241 %(sexp) Evaluate elisp `(sexp)' and replace it with the results.
242 Only placeholders pre-existing within the template, or
243 introduced with %[pathname] are expanded this way. Since this
244 happens after expanding non-interactive %-escapes, those can
245 be used to fill the expression.
246 %<...> The result of format-time-string on the ... format specification.
247 %t Time stamp, date only.
248 %T Time stamp with date and time.
249 %u, %U Like the above, but inactive time stamps.
250 %i Initial content, copied from the active region. If %i is
251 indented, the entire inserted text will be indented as well.
252 %a Annotation, normally the link created with `org-store-link'.
253 %A Like %a, but prompt for the description part.
254 %l Like %a, but only insert the literal link.
255 %c Current kill ring head.
256 %x Content of the X clipboard.
257 %k Title of currently clocked task.
258 %K Link to currently clocked task.
259 %n User name (taken from the variable `user-full-name').
260 %f File visited by current buffer when org-capture was called.
261 %F Full path of the file or directory visited by current buffer.
262 %:keyword Specific information for certain link types, see below.
263 %^g Prompt for tags, with completion on tags in target file.
264 %^G Prompt for tags, with completion on all tags in all agenda files.
265 %^t Like %t, but prompt for date. Similarly %^T, %^u, %^U.
266 You may define a prompt like: %^{Please specify birthday}t
267 %^C Interactive selection of which kill or clip to use.
268 %^L Like %^C, but insert as link.
269 %^{prop}p Prompt the user for a value for property `prop'.
270 %^{prompt} Prompt the user for a string and replace this sequence with it.
271 A default value and a completion table ca be specified like this:
272 %^{prompt|default|completion2|completion3|...}.
273 %? After completing the template, position cursor here.
274 %\\N Insert the text entered at the nth %^{prompt}, where N
275 is a number, starting from 1.
277 Apart from these general escapes, you can access information specific to
278 the link type that is created. For example, calling `org-capture' in emails
279 or in Gnus will record the author and the subject of the message, which you
280 can access with \"%:from\" and \"%:subject\", respectively. Here is a
281 complete list of what is recorded for each link type.
283 Link type | Available information
284 ------------------------+------------------------------------------------------
285 bbdb | %:type %:name %:company
286 vm, wl, mh, mew, rmail, | %:type %:subject %:message-id
287 gnus | %:from %:fromname %:fromaddress
288 | %:to %:toname %:toaddress
289 | %:fromto (either \"to NAME\" or \"from NAME\")
290 | %:date %:date-timestamp (as active timestamp)
291 | %:date-timestamp-inactive (as inactive timestamp)
292 gnus | %:group, for messages also all email fields
293 w3, w3m | %:type %:url
294 info | %:type %:file %:node
295 calendar | %:type %:date
297 When you need to insert a literal percent sign in the template,
298 you can escape ambiguous cases with a backward slash, e.g., \\%i."
299 :group 'org-capture
300 :version "24.1"
301 :type
302 '(repeat
303 (choice :value ("" "" entry (file "~/org/notes.org") "")
304 (list :tag "Multikey description"
305 (string :tag "Keys ")
306 (string :tag "Description"))
307 (list :tag "Template entry"
308 (string :tag "Keys ")
309 (string :tag "Description ")
310 (choice :tag "Capture Type " :value entry
311 (const :tag "Org entry" entry)
312 (const :tag "Plain list item" item)
313 (const :tag "Checkbox item" checkitem)
314 (const :tag "Plain text" plain)
315 (const :tag "Table line" table-line))
316 (choice :tag "Target location"
317 (list :tag "File"
318 (const :format "" file)
319 (file :tag " File"))
320 (list :tag "ID"
321 (const :format "" id)
322 (string :tag " ID"))
323 (list :tag "File & Headline"
324 (const :format "" file+headline)
325 (file :tag " File ")
326 (string :tag " Headline"))
327 (list :tag "File & Outline path"
328 (const :format "" file+olp)
329 (file :tag " File ")
330 (repeat :tag "Outline path" :inline t
331 (string :tag "Headline")))
332 (list :tag "File & Regexp"
333 (const :format "" file+regexp)
334 (file :tag " File ")
335 (regexp :tag " Regexp"))
336 (list :tag "File & Date tree"
337 (const :format "" file+datetree)
338 (file :tag " File"))
339 (list :tag "File & Date tree, prompt for date"
340 (const :format "" file+datetree+prompt)
341 (file :tag " File"))
342 (list :tag "File & Week tree"
343 (const :format "" file+weektree)
344 (file :tag " File"))
345 (list :tag "File & Week tree, prompt for date"
346 (const :format "" file+weektree+prompt)
347 (file :tag " File"))
348 (list :tag "File & function"
349 (const :format "" file+function)
350 (file :tag " File ")
351 (sexp :tag " Function"))
352 (list :tag "Current clocking task"
353 (const :format "" clock))
354 (list :tag "Function"
355 (const :format "" function)
356 (sexp :tag " Function")))
357 (choice :tag "Template"
358 (string)
359 (list :tag "File"
360 (const :format "" file)
361 (file :tag "Template file"))
362 (list :tag "Function"
363 (const :format "" function)
364 (function :tag "Template function")))
365 (plist :inline t
366 ;; Give the most common options as checkboxes
367 :options (((const :format "%v " :prepend) (const t))
368 ((const :format "%v " :immediate-finish) (const t))
369 ((const :format "%v " :jump-to-captured) (const t))
370 ((const :format "%v " :empty-lines) (const 1))
371 ((const :format "%v " :empty-lines-before) (const 1))
372 ((const :format "%v " :empty-lines-after) (const 1))
373 ((const :format "%v " :clock-in) (const t))
374 ((const :format "%v " :clock-keep) (const t))
375 ((const :format "%v " :clock-resume) (const t))
376 ((const :format "%v " :unnarrowed) (const t))
377 ((const :format "%v " :table-line-pos) (const t))
378 ((const :format "%v " :kill-buffer) (const t))))))))
380 (defcustom org-capture-before-finalize-hook nil
381 "Hook that is run right before a capture process is finalized.
382 The capture buffer is still current when this hook runs and it is
383 widened to the entire buffer."
384 :group 'org-capture
385 :version "24.1"
386 :type 'hook)
388 (defcustom org-capture-after-finalize-hook nil
389 "Hook that is run right after a capture process is finalized.
390 Suitable for window cleanup."
391 :group 'org-capture
392 :version "24.1"
393 :type 'hook)
395 (defcustom org-capture-prepare-finalize-hook nil
396 "Hook that is run before the finalization starts.
397 The capture buffer is current and still narrowed."
398 :group 'org-capture
399 :version "24.1"
400 :type 'hook)
402 (defcustom org-capture-bookmark t
403 "When non-nil, add a bookmark pointing at the last stored
404 position when capturing."
405 :group 'org-capture
406 :version "24.3"
407 :type 'boolean)
409 ;;; The property list for keeping information about the capture process
411 (defvar org-capture-plist nil
412 "Plist for the current capture process, global, to avoid having to pass it.")
414 (defvar org-capture-current-plist nil
415 "Local variable holding the plist in a capture buffer.
416 This is used to store the plist for use when finishing a capture process
417 because another such process might have changed the global variable by then.
419 Each time a new capture buffer has been set up, the global `org-capture-plist'
420 is copied to this variable, which is local in the indirect buffer.")
422 (defvar org-capture-clock-keep nil
423 "Local variable to store the value of the :clock-keep parameter.
424 This is needed in case org-capture-finalize is called interactively.")
426 (defun org-capture-put (&rest stuff)
427 "Add properties to the capture property list `org-capture-plist'."
428 (while stuff
429 (setq org-capture-plist (plist-put org-capture-plist
430 (pop stuff) (pop stuff)))))
431 (defun org-capture-get (prop &optional local)
432 "Get properties from the capture property list `org-capture-plist'.
433 When LOCAL is set, use the local variable `org-capture-current-plist',
434 this is necessary after initialization of the capture process,
435 to avoid conflicts with other active capture processes."
436 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
438 (defun org-capture-member (prop &optional local)
439 "Is PROP a property in `org-capture-plist'.
440 When LOCAL is set, use the local variable `org-capture-current-plist',
441 this is necessary after initialization of the capture process,
442 to avoid conflicts with other active capture processes."
443 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
445 ;;; The minor mode
447 (defvar org-capture-mode-map (make-sparse-keymap)
448 "Keymap for `org-capture-mode', a minor mode.
449 Use this map to set additional keybindings for when Org-mode is used
450 for a capture buffer.")
452 (defvar org-capture-mode-hook nil
453 "Hook for the minor `org-capture-mode'.")
455 (define-minor-mode org-capture-mode
456 "Minor mode for special key bindings in a capture buffer.
458 Turning on this mode runs the normal hook `org-capture-mode-hook'."
459 nil " Rem" org-capture-mode-map
460 (setq-local
461 header-line-format
462 (substitute-command-keys
463 "\\<org-capture-mode-map>Capture buffer. Finish \\[org-capture-finalize], \
464 refile \\[org-capture-refile], abort \\[org-capture-kill].")))
465 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
466 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
467 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
469 ;;; The main commands
471 (defvar org-capture-initial nil)
472 (defvar org-capture-entry nil)
474 ;;;###autoload
475 (defun org-capture-string (string &optional keys)
476 "Capture STRING with the template selected by KEYS."
477 (interactive "sInitial text: \n")
478 (let ((org-capture-initial string)
479 (org-capture-entry (org-capture-select-template keys)))
480 (org-capture)))
482 (defcustom org-capture-templates-contexts nil
483 "Alist of capture templates and valid contexts.
485 For example, if you have a capture template \"c\" and you want
486 this template to be accessible only from `message-mode' buffers,
487 use this:
489 \\='((\"c\" ((in-mode . \"message-mode\"))))
491 Here are the available contexts definitions:
493 in-file: command displayed only in matching files
494 in-mode: command displayed only in matching modes
495 not-in-file: command not displayed in matching files
496 not-in-mode: command not displayed in matching modes
497 in-buffer: command displayed only in matching buffers
498 not-in-buffer: command not displayed in matching buffers
499 [function]: a custom function taking no argument
501 If you define several checks, the agenda command will be
502 accessible if there is at least one valid check.
504 You can also bind a key to another agenda custom command
505 depending on contextual rules.
507 \\='((\"c\" \"d\" ((in-mode . \"message-mode\"))))
509 Here it means: in `message-mode buffers', use \"c\" as the
510 key for the capture template otherwise associated with \"d\".
511 \(The template originally associated with \"d\" is not displayed
512 to avoid duplicates.)"
513 :version "24.3"
514 :group 'org-capture
515 :type '(repeat (list :tag "Rule"
516 (string :tag " Capture key")
517 (string :tag "Replace by template")
518 (repeat :tag "Available when"
519 (choice
520 (cons :tag "Condition"
521 (choice
522 (const :tag "In file" in-file)
523 (const :tag "Not in file" not-in-file)
524 (const :tag "In buffer" in-buffer)
525 (const :tag "Not in buffer" not-in-buffer)
526 (const :tag "In mode" in-mode)
527 (const :tag "Not in mode" not-in-mode))
528 (regexp))
529 (function :tag "Custom function"))))))
531 (defcustom org-capture-use-agenda-date nil
532 "Non-nil means use the date at point when capturing from agendas.
533 When nil, you can still capture using the date at point with \\[org-agenda-capture]."
534 :group 'org-capture
535 :version "24.3"
536 :type 'boolean)
538 ;;;###autoload
539 (defun org-capture (&optional goto keys)
540 "Capture something.
541 \\<org-capture-mode-map>
542 This will let you select a template from `org-capture-templates', and then
543 file the newly captured information. The text is immediately inserted
544 at the target location, and an indirect buffer is shown where you can
545 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
546 of Emacs, so that you can continue your work.
548 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
549 anything, just go to the file/headline where the selected template
550 stores its notes. With a double prefix argument \
551 \\[universal-argument] \\[universal-argument], go to the last note
552 stored.
554 When called with a `C-0' (zero) prefix, insert a template at point.
556 ELisp programs can set KEYS to a string associated with a template
557 in `org-capture-templates'. In this case, interactive selection
558 will be bypassed.
560 If `org-capture-use-agenda-date' is non-nil, capturing from the
561 agenda will use the date at point as the default date. Then, a
562 `C-1' prefix will tell the capture process to use the HH:MM time
563 of the day at point (if any) or the current HH:MM time."
564 (interactive "P")
565 (when (and org-capture-use-agenda-date
566 (eq major-mode 'org-agenda-mode))
567 (setq org-overriding-default-time
568 (org-get-cursor-date (equal goto 1))))
569 (cond
570 ((equal goto '(4)) (org-capture-goto-target))
571 ((equal goto '(16)) (org-capture-goto-last-stored))
573 ;; FIXME: Are these needed?
574 (let* ((orig-buf (current-buffer))
575 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
576 org-capture-link-is-already-stored)
577 (plist-get org-store-link-plist :annotation)
578 (ignore-errors (org-store-link nil))))
579 (entry (or org-capture-entry (org-capture-select-template keys)))
580 initial)
581 (setq initial (or org-capture-initial
582 (and (org-region-active-p)
583 (buffer-substring (point) (mark)))))
584 (when (stringp initial)
585 (remove-text-properties 0 (length initial) '(read-only t) initial))
586 (when (stringp annotation)
587 (remove-text-properties 0 (length annotation)
588 '(read-only t) annotation))
589 (cond
590 ((equal entry "C")
591 (customize-variable 'org-capture-templates))
592 ((equal entry "q")
593 (error "Abort"))
595 (org-capture-set-plist entry)
596 (org-capture-get-template)
597 (org-capture-put :original-buffer orig-buf
598 :original-file (or (buffer-file-name orig-buf)
599 (and (featurep 'dired)
600 (car (rassq orig-buf
601 dired-buffers))))
602 :original-file-nondirectory
603 (and (buffer-file-name orig-buf)
604 (file-name-nondirectory
605 (buffer-file-name orig-buf)))
606 :annotation annotation
607 :initial initial
608 :return-to-wconf (current-window-configuration)
609 :default-time
610 (or org-overriding-default-time
611 (org-current-time)))
612 (org-capture-set-target-location)
613 (condition-case error
614 (org-capture-put :template (org-capture-fill-template))
615 ((error quit)
616 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
617 (error "Capture abort: %s" error)))
619 (setq org-capture-clock-keep (org-capture-get :clock-keep))
620 (if (equal goto 0)
621 ;;insert at point
622 (org-capture-insert-template-here)
623 (condition-case error
624 (org-capture-place-template
625 (equal (car (org-capture-get :target)) 'function))
626 ((error quit)
627 (if (and (buffer-base-buffer (current-buffer))
628 (string-match "\\`CAPTURE-" (buffer-name)))
629 (kill-buffer (current-buffer)))
630 (set-window-configuration (org-capture-get :return-to-wconf))
631 (error "Capture template `%s': %s"
632 (org-capture-get :key)
633 (nth 1 error))))
634 (if (and (derived-mode-p 'org-mode)
635 (org-capture-get :clock-in))
636 (condition-case nil
637 (progn
638 (if (org-clock-is-active)
639 (org-capture-put :interrupted-clock
640 (copy-marker org-clock-marker)))
641 (org-clock-in)
642 (setq-local org-capture-clock-was-started t))
643 (error
644 "Could not start the clock in this capture buffer")))
645 (if (org-capture-get :immediate-finish)
646 (org-capture-finalize)))))))))
648 (defun org-capture-get-template ()
649 "Get the template from a file or a function if necessary."
650 (let ((txt (org-capture-get :template)) file)
651 (cond
652 ((and (listp txt) (eq (car txt) 'file))
653 (if (file-exists-p
654 (setq file (expand-file-name (nth 1 txt) org-directory)))
655 (setq txt (org-file-contents file))
656 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
657 ((and (listp txt) (eq (car txt) 'function))
658 (if (fboundp (nth 1 txt))
659 (setq txt (funcall (nth 1 txt)))
660 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
661 ((not txt) (setq txt ""))
662 ((stringp txt))
663 (t (setq txt "* Invalid capture template")))
664 (org-capture-put :template txt)))
666 (defun org-capture-finalize (&optional stay-with-capture)
667 "Finalize the capture process.
668 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
669 captured item after finalizing."
670 (interactive "P")
671 (when (org-capture-get :jump-to-captured)
672 (setq stay-with-capture t))
673 (unless (and org-capture-mode
674 (buffer-base-buffer (current-buffer)))
675 (error "This does not seem to be a capture buffer for Org-mode"))
677 (run-hooks 'org-capture-prepare-finalize-hook)
679 ;; Did we start the clock in this capture buffer?
680 (when (and org-capture-clock-was-started
681 org-clock-marker (marker-buffer org-clock-marker)
682 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
683 (> org-clock-marker (point-min))
684 (< org-clock-marker (point-max)))
685 ;; Looks like the clock we started is still running. Clock out.
686 (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
687 (when (and (not org-capture-clock-keep)
688 (org-capture-get :clock-resume 'local)
689 (markerp (org-capture-get :interrupted-clock 'local))
690 (buffer-live-p (marker-buffer
691 (org-capture-get :interrupted-clock 'local))))
692 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
693 (org-with-point-at clock-in-task
694 (org-clock-in)))
695 (message "Interrupted clock has been resumed")))
697 (let ((beg (point-min))
698 (end (point-max))
699 (abort-note nil))
700 ;; Store the size of the capture buffer
701 (org-capture-put :captured-entry-size (- (point-max) (point-min)))
702 (widen)
703 ;; Store the insertion point in the target buffer
704 (org-capture-put :insertion-point (point))
706 (if org-note-abort
707 (let ((m1 (org-capture-get :begin-marker 'local))
708 (m2 (org-capture-get :end-marker 'local)))
709 (if (and m1 m2 (= m1 beg) (= m2 end))
710 (progn
711 (setq m2 (if (cdr (assoc 'heading org-blank-before-new-entry))
712 m2 (1+ m2))
713 m2 (if (< (point-max) m2) (point-max) m2))
714 (setq abort-note 'clean)
715 (kill-region m1 m2))
716 (setq abort-note 'dirty)))
718 ;; Make sure that the empty lines after are correct
719 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
720 (member (org-capture-get :type 'local)
721 '(entry item checkitem plain)))
722 (save-excursion
723 (goto-char end)
724 (or (bolp) (newline))
725 (org-capture-empty-lines-after
726 (or (org-capture-get :empty-lines-after 'local)
727 (org-capture-get :empty-lines 'local) 0))))
728 ;; Postprocessing: Update Statistics cookies, do the sorting
729 (when (derived-mode-p 'org-mode)
730 (save-excursion
731 (when (ignore-errors (org-back-to-heading))
732 (org-update-parent-todo-statistics)
733 (org-update-checkbox-count)))
734 ;; FIXME Here we should do the sorting
735 ;; If we have added a table line, maybe recompute?
736 (when (and (eq (org-capture-get :type 'local) 'table-line)
737 (org-at-table-p))
738 (if (org-table-get-stored-formulas)
739 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
740 (org-table-align))))
741 ;; Store this place as the last one where we stored something
742 ;; Do the marking in the base buffer, so that it makes sense after
743 ;; the indirect buffer has been killed.
744 (when org-capture-bookmark
745 (org-capture-bookmark-last-stored-position))
747 ;; Run the hook
748 (run-hooks 'org-capture-before-finalize-hook))
750 (when (org-capture-get :decrypted)
751 (save-excursion
752 (goto-char (org-capture-get :decrypted))
753 (org-encrypt-entry)))
755 ;; Kill the indirect buffer
756 (save-buffer)
757 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
758 (new-buffer (org-capture-get :new-buffer 'local))
759 (kill-buffer (org-capture-get :kill-buffer 'local))
760 (base-buffer (buffer-base-buffer (current-buffer))))
762 ;; Kill the indirect buffer
763 (kill-buffer (current-buffer))
765 ;; Narrow back the target buffer to its previous state
766 (with-current-buffer (org-capture-get :buffer)
767 (let ((reg (org-capture-get :initial-target-region))
768 (pos (org-capture-get :initial-target-position))
769 (ipt (org-capture-get :insertion-point))
770 (size (org-capture-get :captured-entry-size)))
771 (if (not reg)
772 (widen)
773 (cond ((< ipt (car reg))
774 ;; insertion point is before the narrowed region
775 (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
776 ((> ipt (cdr reg))
777 ;; insertion point is after the narrowed region
778 (narrow-to-region (car reg) (cdr reg)))
780 ;; insertion point is within the narrowed region
781 (narrow-to-region (car reg) (+ size (cdr reg)))))
782 ;; now place back the point at its original position
783 (if (< ipt (car reg))
784 (goto-char (+ size pos))
785 (goto-char (if (< ipt pos) (+ size pos) pos))))))
787 ;; Kill the target buffer if that is desired
788 (when (and base-buffer new-buffer kill-buffer)
789 (with-current-buffer base-buffer (save-buffer))
790 (kill-buffer base-buffer))
792 ;; Restore the window configuration before capture
793 (set-window-configuration return-wconf))
795 (run-hooks 'org-capture-after-finalize-hook)
796 ;; Special cases
797 (cond
798 (abort-note
799 (cond
800 ((equal abort-note 'clean)
801 (message "Capture process aborted and target buffer cleaned up"))
802 ((equal abort-note 'dirty)
803 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
804 (stay-with-capture
805 (org-capture-goto-last-stored)))
806 ;; Return if we did store something
807 (not abort-note)))
809 (defun org-capture-refile ()
810 "Finalize the current capture and then refile the entry.
811 Refiling is done from the base buffer, because the indirect buffer is then
812 already gone. Any prefix argument will be passed to the refile command."
813 (interactive)
814 (unless (eq (org-capture-get :type 'local) 'entry)
815 (error
816 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
817 (let ((pos (point))
818 (base (buffer-base-buffer (current-buffer)))
819 (org-capture-is-refiling t)
820 (kill-buffer (org-capture-get :kill-buffer 'local)))
821 (org-capture-put :kill-buffer nil)
822 (org-capture-finalize)
823 (save-window-excursion
824 (with-current-buffer (or base (current-buffer))
825 (save-excursion
826 (save-restriction
827 (widen)
828 (goto-char pos)
829 (call-interactively 'org-refile)))))
830 (when kill-buffer (kill-buffer base))))
832 (defun org-capture-kill ()
833 "Abort the current capture process."
834 (interactive)
835 ;; FIXME: This does not do the right thing, we need to remove the
836 ;; new stuff by hand it is easy: undo, then kill the buffer
837 (let ((org-note-abort t)
838 (org-capture-before-finalize-hook nil))
839 (org-capture-finalize)))
841 (defun org-capture-goto-last-stored ()
842 "Go to the location where the last capture note was stored."
843 (interactive)
844 (org-goto-marker-or-bmk org-capture-last-stored-marker
845 (plist-get org-bookmark-names-plist
846 :last-capture))
847 (message "This is the last note stored by a capture process"))
849 ;;; Supporting functions for handling the process
851 (defun org-capture-put-target-region-and-position ()
852 "Store the initial region with `org-capture-put'."
853 (org-capture-put
854 :initial-target-region
855 ;; Check if the buffer is currently narrowed
856 (when (org-buffer-narrowed-p)
857 (cons (point-min) (point-max))))
858 ;; store the current point
859 (org-capture-put :initial-target-position (point)))
861 (defvar org-time-was-given) ; dynamically scoped parameter
862 (defun org-capture-set-target-location (&optional target)
863 "Find TARGET buffer and position.
864 Store them in the capture property list."
865 (let ((target-entry-p t) decrypted-hl-pos)
866 (setq target (or target (org-capture-get :target)))
867 (save-excursion
868 (cond
869 ((eq (car target) 'file)
870 (set-buffer (org-capture-target-buffer (nth 1 target)))
871 (org-capture-put-target-region-and-position)
872 (widen)
873 (setq target-entry-p nil))
875 ((eq (car target) 'id)
876 (let ((loc (org-id-find (nth 1 target))))
877 (if (not loc)
878 (error "Cannot find target ID \"%s\"" (nth 1 target))
879 (set-buffer (org-capture-target-buffer (car loc)))
880 (widen)
881 (org-capture-put-target-region-and-position)
882 (goto-char (cdr loc)))))
884 ((eq (car target) 'file+headline)
885 (set-buffer (org-capture-target-buffer (nth 1 target)))
886 (org-capture-put-target-region-and-position)
887 (widen)
888 (let ((hd (nth 2 target)))
889 (goto-char (point-min))
890 (unless (derived-mode-p 'org-mode)
891 (error
892 "Target buffer \"%s\" for file+headline should be in Org mode"
893 (current-buffer)))
894 (if (re-search-forward
895 (format org-complex-heading-regexp-format (regexp-quote hd))
896 nil t)
897 (goto-char (point-at-bol))
898 (goto-char (point-max))
899 (or (bolp) (insert "\n"))
900 (insert "* " hd "\n")
901 (beginning-of-line 0))))
903 ((eq (car target) 'file+olp)
904 (let ((m (org-find-olp
905 (cons (org-capture-expand-file (nth 1 target))
906 (cddr target)))))
907 (set-buffer (marker-buffer m))
908 (org-capture-put-target-region-and-position)
909 (widen)
910 (goto-char m)))
912 ((eq (car target) 'file+regexp)
913 (set-buffer (org-capture-target-buffer (nth 1 target)))
914 (org-capture-put-target-region-and-position)
915 (widen)
916 (goto-char (point-min))
917 (if (re-search-forward (nth 2 target) nil t)
918 (progn
919 (goto-char (if (org-capture-get :prepend)
920 (match-beginning 0) (match-end 0)))
921 (org-capture-put :exact-position (point))
922 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
923 (error "No match for target regexp in file %s" (nth 1 target))))
925 ((memq (car target) '(file+datetree file+datetree+prompt file+weektree file+weektree+prompt))
926 (require 'org-datetree)
927 (set-buffer (org-capture-target-buffer (nth 1 target)))
928 (org-capture-put-target-region-and-position)
929 (widen)
930 ;; Make a date/week tree entry, with the current date (or
931 ;; yesterday, if we are extending dates for a couple of hours)
932 (funcall
933 (cond
934 ((memq (car target) '(file+weektree file+weektree+prompt))
935 #'org-datetree-find-iso-week-create)
936 (t #'org-datetree-find-date-create))
937 (calendar-gregorian-from-absolute
938 (cond
939 (org-overriding-default-time
940 ;; use the overriding default time
941 (time-to-days org-overriding-default-time))
943 ((memq (car target) '(file+datetree+prompt file+weektree+prompt))
944 ;; prompt for date
945 (let ((prompt-time (org-read-date
946 nil t nil "Date for tree entry:"
947 (current-time))))
948 (org-capture-put
949 :default-time
950 (cond ((and (or (not (boundp 'org-time-was-given))
951 (not org-time-was-given))
952 (not (= (time-to-days prompt-time) (org-today))))
953 ;; Use 00:00 when no time is given for another date than today?
954 (apply 'encode-time (append '(0 0 0) (cdddr (decode-time prompt-time)))))
955 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer)
956 ;; Replace any time range by its start
957 (apply 'encode-time
958 (org-read-date-analyze
959 (replace-match "\\1 \\2" nil nil org-read-date-final-answer)
960 prompt-time (decode-time prompt-time))))
961 (t prompt-time)))
962 (time-to-days prompt-time)))
964 ;; current date, possibly corrected for late night workers
965 (org-today))))))
967 ((eq (car target) 'file+function)
968 (set-buffer (org-capture-target-buffer (nth 1 target)))
969 (org-capture-put-target-region-and-position)
970 (widen)
971 (funcall (nth 2 target))
972 (org-capture-put :exact-position (point))
973 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
975 ((eq (car target) 'function)
976 (funcall (nth 1 target))
977 (org-capture-put :exact-position (point))
978 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
980 ((eq (car target) 'clock)
981 (if (and (markerp org-clock-hd-marker)
982 (marker-buffer org-clock-hd-marker))
983 (progn (set-buffer (marker-buffer org-clock-hd-marker))
984 (org-capture-put-target-region-and-position)
985 (widen)
986 (goto-char org-clock-hd-marker))
987 (error "No running clock that could be used as capture target")))
989 (t (error "Invalid capture target specification")))
991 (when (and (featurep 'org-crypt) (org-at-encrypted-entry-p))
992 (org-decrypt-entry)
993 (setq decrypted-hl-pos
994 (save-excursion (and (org-back-to-heading t) (point)))))
996 (org-capture-put :buffer (current-buffer) :pos (point)
997 :target-entry-p target-entry-p
998 :decrypted decrypted-hl-pos))))
1000 (defun org-capture-expand-file (file)
1001 "Expand functions and symbols for FILE.
1002 When FILE is a function, call it. When it is a form, evaluate
1003 it. When it is a variable, retrieve the value. When it is
1004 a string, return it. However, if it is the empty string, return
1005 `org-default-notes-file' instead."
1006 (cond
1007 ((equal file "") org-default-notes-file)
1008 ((org-string-nw-p file) file)
1009 ((functionp file) (funcall file))
1010 ((and (symbolp file) (boundp file)) (symbol-value file))
1011 ((consp file) (eval 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 ;; we have a complex line specification
1189 (let ((ll (ignore-errors
1190 (save-match-data (org-table-analyze))
1191 (aref org-table-hlines
1192 (- (match-end 1) (match-beginning 1)))))
1193 (delta (string-to-number (match-string 2 table-line-pos))))
1194 ;; The user wants a special position in the table
1195 (unless ll
1196 (error "Invalid table line specification \"%s\"" table-line-pos))
1197 (goto-char org-table-current-begin-pos)
1198 (forward-line (+ ll delta (if (< delta 0) 0 -1)))
1199 (org-table-insert-row 'below)
1200 (beginning-of-line 1)
1201 (delete-region (point) (1+ (point-at-eol)))
1202 (setq beg (point))
1203 (insert txt)
1204 (setq end (point))))
1205 ((org-capture-get :prepend)
1206 (goto-char (point-min))
1207 (re-search-forward org-table-hline-regexp nil t)
1208 (beginning-of-line 1)
1209 (re-search-forward org-table-dataline-regexp nil t)
1210 (beginning-of-line 1)
1211 (setq beg (point))
1212 (org-table-insert-row)
1213 (beginning-of-line 1)
1214 (delete-region (point) (1+ (point-at-eol)))
1215 (insert txt)
1216 (setq end (point)))
1218 (goto-char (point-max))
1219 (re-search-backward org-table-dataline-regexp nil t)
1220 (beginning-of-line 1)
1221 (org-table-insert-row 'below)
1222 (beginning-of-line 1)
1223 (delete-region (point) (1+ (point-at-eol)))
1224 (setq beg (point))
1225 (insert txt)
1226 (setq end (point))))
1227 (goto-char beg)
1228 (org-capture-position-for-last-stored 'table-line)
1229 (if (or (re-search-backward "%\\?" beg t)
1230 (re-search-forward "%\\?" end t))
1231 (replace-match ""))
1232 (org-table-align)))
1234 (defun org-capture-place-plain-text ()
1235 "Place the template plainly.
1236 If the target locator points at an Org node, place the template into
1237 the text of the entry, before the first child. If not, place the
1238 template at the beginning or end of the file.
1239 Of course, if exact position has been required, just put it there."
1240 (let* ((txt (org-capture-get :template))
1241 beg end)
1242 (cond
1243 ((org-capture-get :exact-position)
1244 (goto-char (org-capture-get :exact-position)))
1245 ((and (org-capture-get :target-entry-p)
1246 (bolp)
1247 (looking-at org-outline-regexp))
1248 ;; we should place the text into this entry
1249 (if (org-capture-get :prepend)
1250 ;; Skip meta data and drawers
1251 (org-end-of-meta-data t)
1252 ;; go to ent of the entry text, before the next headline
1253 (outline-next-heading)))
1255 ;; beginning or end of file
1256 (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
1257 (or (bolp) (newline))
1258 (org-capture-empty-lines-before)
1259 (setq beg (point))
1260 (insert txt)
1261 (org-capture-empty-lines-after 1)
1262 (org-capture-position-for-last-stored beg)
1263 (setq end (point))
1264 (org-capture-mark-kill-region beg (1- end))
1265 (org-capture-narrow beg (1- end))
1266 (if (or (re-search-backward "%\\?" beg t)
1267 (re-search-forward "%\\?" end t))
1268 (replace-match ""))))
1270 (defun org-capture-mark-kill-region (beg end)
1271 "Mark the region that will have to be killed when aborting capture."
1272 (let ((m1 (move-marker (make-marker) beg))
1273 (m2 (move-marker (make-marker) end)))
1274 (org-capture-put :begin-marker m1)
1275 (org-capture-put :end-marker m2)))
1277 (defun org-capture-position-for-last-stored (where)
1278 "Memorize the position that should later become the position of last capture."
1279 (cond
1280 ((integerp where)
1281 (org-capture-put :position-for-last-stored
1282 (move-marker (make-marker) where
1283 (or (buffer-base-buffer (current-buffer))
1284 (current-buffer)))))
1285 ((eq where 'table-line)
1286 (org-capture-put :position-for-last-stored
1287 (list 'table-line
1288 (org-table-current-dline))))
1289 (t (error "This should not happen"))))
1291 (defun org-capture-bookmark-last-stored-position ()
1292 "Bookmark the last-captured position."
1293 (let* ((where (org-capture-get :position-for-last-stored 'local))
1294 (pos (cond
1295 ((markerp where)
1296 (prog1 (marker-position where)
1297 (move-marker where nil)))
1298 ((and (listp where) (eq (car where) 'table-line))
1299 (if (org-at-table-p)
1300 (save-excursion
1301 (org-table-goto-line (nth 1 where))
1302 (point-at-bol))
1303 (point))))))
1304 (with-current-buffer (buffer-base-buffer (current-buffer))
1305 (save-excursion
1306 (save-restriction
1307 (widen)
1308 (goto-char pos)
1309 (let ((bookmark-name (plist-get org-bookmark-names-plist
1310 :last-capture)))
1311 (when bookmark-name
1312 (with-demoted-errors
1313 (bookmark-set bookmark-name))))
1314 (move-marker org-capture-last-stored-marker (point)))))))
1316 (defun org-capture-narrow (beg end)
1317 "Narrow, unless configuration says not to narrow."
1318 (unless (org-capture-get :unnarrowed)
1319 (narrow-to-region beg end)
1320 (goto-char beg)))
1322 (defun org-capture-empty-lines-before (&optional n)
1323 "Set the correct number of empty lines before the insertion point.
1324 Point will be after the empty lines, so insertion can directly be done."
1325 (setq n (or n (org-capture-get :empty-lines-before)
1326 (org-capture-get :empty-lines) 0))
1327 (let ((pos (point)))
1328 (org-back-over-empty-lines)
1329 (delete-region (point) pos)
1330 (if (> n 0) (newline n))))
1332 (defun org-capture-empty-lines-after (&optional n)
1333 "Set the correct number of empty lines after the inserted string.
1334 Point will remain at the first line after the inserted text."
1335 (setq n (or n (org-capture-get :empty-lines-after)
1336 (org-capture-get :empty-lines) 0))
1337 (org-back-over-empty-lines)
1338 (while (looking-at "[ \t]*\n") (replace-match ""))
1339 (let ((pos (point)))
1340 (if (> n 0) (newline n))
1341 (goto-char pos)))
1343 (defvar org-clock-marker) ; Defined in org.el
1345 (defun org-capture-insert-template-here ()
1346 "Insert the capture template at point."
1347 (let* ((template (org-capture-get :template))
1348 (type (org-capture-get :type))
1349 beg end pp)
1350 (or (bolp) (newline))
1351 (setq beg (point))
1352 (cond
1353 ((and (eq type 'entry) (derived-mode-p 'org-mode))
1354 (org-capture-verify-tree (org-capture-get :template))
1355 (org-paste-subtree nil template t))
1356 ((and (memq type '(item checkitem))
1357 (derived-mode-p 'org-mode)
1358 (save-excursion (skip-chars-backward " \t\n")
1359 (setq pp (point))
1360 (org-in-item-p)))
1361 (goto-char pp)
1362 (org-insert-item)
1363 (skip-chars-backward " ")
1364 (skip-chars-backward "-+*0123456789).")
1365 (delete-region (point) (point-at-eol))
1366 (setq beg (point))
1367 (org-remove-indentation template)
1368 (insert template)
1369 (org-capture-empty-lines-after)
1370 (goto-char beg)
1371 (org-list-repair)
1372 (org-end-of-item)
1373 (setq end (point)))
1374 (t (insert template)))
1375 (setq end (point))
1376 (goto-char beg)
1377 (if (re-search-forward "%\\?" end t)
1378 (replace-match ""))))
1380 (defun org-capture-set-plist (entry)
1381 "Initialize the property list from the template definition."
1382 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1383 (org-capture-put :key (car entry) :description (nth 1 entry)
1384 :target (nth 3 entry))
1385 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1386 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1387 ;; The template may be empty or omitted for special types.
1388 ;; Here we insert the default templates for such cases.
1389 (cond
1390 ((eq type 'item) (setq txt "- %?"))
1391 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1392 ((eq type 'table-line) (setq txt "| %? |"))
1393 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1394 (org-capture-put :template txt :type type)))
1396 (defun org-capture-goto-target (&optional template-key)
1397 "Go to the target location of a capture template.
1398 The user is queried for the template."
1399 (interactive)
1400 (let ((entry (org-capture-select-template template-key)))
1401 (unless entry (error "No capture template selected"))
1402 (org-capture-set-plist entry)
1403 (org-capture-set-target-location)
1404 (org-pop-to-buffer-same-window (org-capture-get :buffer))
1405 (goto-char (org-capture-get :pos))))
1407 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1408 "Make an indirect buffer for a capture process.
1409 Use PREFIX as a prefix for the name of the indirect buffer."
1410 (setq buffer (or buffer (current-buffer)))
1411 (let ((n 1) (base (buffer-name buffer)) bname)
1412 (setq bname (concat prefix "-" base))
1413 (while (buffer-live-p (get-buffer bname))
1414 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1415 (condition-case nil
1416 (make-indirect-buffer buffer bname 'clone)
1417 (error
1418 (let ((buf (make-indirect-buffer buffer bname)))
1419 (with-current-buffer buf (org-mode))
1420 buf)))))
1422 (defun org-capture-verify-tree (tree)
1423 "Throw error if TREE is not a valid tree."
1424 (unless (org-kill-is-subtree-p tree)
1425 (error "Template is not a valid Org entry or tree")))
1427 (defun org-mks (table title &optional prompt specials)
1428 "Select a member of an alist with multiple keys.
1429 TABLE is the alist which should contain entries where the car is a string.
1430 There should be two types of entries.
1432 1. prefix descriptions like (\"a\" \"Description\")
1433 This indicates that `a' is a prefix key for multi-letter selection, and
1434 that there are entries following with keys like \"ab\", \"ax\"...
1436 2. Selectable members must have more than two elements, with the first
1437 being the string of keys that lead to selecting it, and the second a
1438 short description string of the item.
1440 The command will then make a temporary buffer listing all entries
1441 that can be selected with a single key, and all the single key
1442 prefixes. When you press the key for a single-letter entry, it is selected.
1443 When you press a prefix key, the commands (and maybe further prefixes)
1444 under this key will be shown and offered for selection.
1446 TITLE will be placed over the selection in the temporary buffer,
1447 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1448 also (\"key\" \"description\") entries. When one of these is selection,
1449 only the bare key is returned."
1450 (setq prompt (or prompt "Select: "))
1451 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1452 current prefix rtn re pressed buffer (inhibit-quit t))
1453 (save-window-excursion
1454 (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
1455 (setq orig-table table)
1456 (catch 'exit
1457 (while t
1458 (erase-buffer)
1459 (insert title "\n\n")
1460 (setq tbl table
1461 des-keys nil
1462 allowed-keys nil
1463 cursor-type nil)
1464 (setq prefix (if current (concat current " ") ""))
1465 (while tbl
1466 (cond
1467 ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
1468 ;; This is a description on this level
1469 (setq dkey (caar tbl) ddesc (cadar tbl))
1470 (pop tbl)
1471 (push dkey des-keys)
1472 (push dkey allowed-keys)
1473 (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
1474 ;; Skip keys which are below this prefix
1475 (setq re (concat "\\`" (regexp-quote dkey)))
1476 (let (case-fold-search)
1477 (while (and tbl (string-match re (caar tbl))) (pop tbl))))
1478 ((= 2 (length (car tbl)))
1479 ;; Not yet a usable description, skip it
1482 ;; usable entry on this level
1483 (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
1484 (push (caar tbl) allowed-keys)
1485 (pop tbl))))
1486 (when specials
1487 (insert "-------------------------------------------------------------------------------\n")
1488 (let ((sp specials))
1489 (while sp
1490 (insert (format "[%s] %s\n"
1491 (caar sp) (nth 1 (car sp))))
1492 (push (caar sp) allowed-keys)
1493 (pop sp))))
1494 (push "\C-g" allowed-keys)
1495 (goto-char (point-min))
1496 (if (not (pos-visible-in-window-p (point-max)))
1497 (org-fit-window-to-buffer))
1498 (message prompt)
1499 (setq pressed (char-to-string (read-char-exclusive)))
1500 (while (not (member pressed allowed-keys))
1501 (message "Invalid key `%s'" pressed) (sit-for 1)
1502 (message prompt)
1503 (setq pressed (char-to-string (read-char-exclusive))))
1504 (when (equal pressed "\C-g")
1505 (kill-buffer buffer)
1506 (error "Abort"))
1507 (when (and (not (assoc pressed table))
1508 (not (member pressed des-keys))
1509 (assoc pressed specials))
1510 (throw 'exit (setq rtn pressed)))
1511 (unless (member pressed des-keys)
1512 (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
1513 orig-table))))
1514 (setq current (concat current pressed))
1515 (setq table (mapcar
1516 (lambda (x)
1517 (if (and (> (length (car x)) 1)
1518 (equal (substring (car x) 0 1) pressed))
1519 (cons (substring (car x) 1) (cdr x))
1520 nil))
1521 table))
1522 (setq table (remove nil table)))))
1523 (when buffer (kill-buffer buffer))
1524 rtn))
1526 ;;; The template code
1527 (defun org-capture-select-template (&optional keys)
1528 "Select a capture template.
1529 Lisp programs can force the template by setting KEYS to a string."
1530 (let ((org-capture-templates
1531 (or (org-contextualize-keys
1532 org-capture-templates org-capture-templates-contexts)
1533 '(("t" "Task" entry (file+headline "" "Tasks")
1534 "* TODO %?\n %u\n %a")))))
1535 (if keys
1536 (or (assoc keys org-capture-templates)
1537 (error "No capture template referred to by \"%s\" keys" keys))
1538 (org-mks org-capture-templates
1539 "Select a capture template\n========================="
1540 "Template key: "
1541 '(("C" "Customize org-capture-templates")
1542 ("q" "Abort"))))))
1544 (defun org-capture-fill-template (&optional template initial annotation)
1545 "Fill a template and return the filled template as a string.
1546 The template may still contain \"%?\" for cursor positioning."
1547 (let* ((template (or template (org-capture-get :template)))
1548 (buffer (org-capture-get :buffer))
1549 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1550 (time (let* ((c (or (org-capture-get :default-time) (current-time)))
1551 (d (decode-time c)))
1552 (if (< (nth 2 d) org-extend-today-until)
1553 (encode-time 0 59 23 (1- (nth 3 d)) (nth 4 d) (nth 5 d))
1554 c)))
1555 (v-t (format-time-string (org-time-stamp-format nil) time))
1556 (v-T (format-time-string (org-time-stamp-format t) time))
1557 (v-u (format-time-string (org-time-stamp-format nil t) time))
1558 (v-U (format-time-string (org-time-stamp-format t t) time))
1559 (v-c (and kill-ring (current-kill 0)))
1560 (v-x (or (org-get-x-clipboard 'PRIMARY)
1561 (org-get-x-clipboard 'CLIPBOARD)
1562 (org-get-x-clipboard 'SECONDARY)))
1563 ;; `initial' and `annotation' might have been passed. But if
1564 ;; the property list has them, we prefer those values.
1565 (v-i (or (plist-get org-store-link-plist :initial)
1566 (and (stringp initial) (org-no-properties initial))
1567 (org-capture-get :initial)
1568 ""))
1569 (v-a
1570 (let ((a (or (plist-get org-store-link-plist :annotation)
1571 annotation
1572 (org-capture-get :annotation)
1573 "")))
1574 ;; Is the link empty? Then we do not want it...
1575 (if (equal a "[[]]") "" a)))
1576 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1577 (v-A (if (and v-a (string-match l-re v-a))
1578 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
1579 v-a))
1580 (v-l (if (and v-a (string-match l-re v-a))
1581 (replace-match "\\1" nil nil v-a)
1582 v-a))
1583 (v-n user-full-name)
1584 (v-k (and (marker-buffer org-clock-marker)
1585 (org-no-properties org-clock-heading)))
1586 (v-K (if (marker-buffer org-clock-marker)
1587 (org-make-link-string
1588 (buffer-file-name (marker-buffer org-clock-marker))
1589 org-clock-heading)))
1590 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1591 (v-F (or (org-capture-get :original-file) ""))
1592 (clipboards (delq nil
1593 (list v-i
1594 (org-get-x-clipboard 'PRIMARY)
1595 (org-get-x-clipboard 'CLIPBOARD)
1596 (org-get-x-clipboard 'SECONDARY)
1597 v-c))))
1599 (setq org-store-link-plist (plist-put org-store-link-plist :annotation v-a))
1600 (setq org-store-link-plist (plist-put org-store-link-plist :initial v-i))
1602 (unless template
1603 (setq template "")
1604 (message "no template") (ding)
1605 (sit-for 1))
1606 (save-window-excursion
1607 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1608 (erase-buffer)
1609 (setq buffer-file-name nil)
1610 (setq mark-active nil)
1611 (insert template)
1612 (goto-char (point-min))
1614 ;; %[] insert contents of a file.
1615 (save-excursion
1616 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1617 (let ((filename (expand-file-name (match-string 1)))
1618 (beg (copy-marker (match-beginning 0)))
1619 (end (copy-marker (match-end 0))))
1620 (unless (org-capture-escaped-%)
1621 (delete-region beg end)
1622 (set-marker beg nil)
1623 (set-marker end nil)
1624 (condition-case error
1625 (insert-file-contents filename)
1626 (error
1627 (insert (format "%%![couldn not insert %s: %s]"
1628 filename
1629 error))))))))
1631 ;; Mark %() embedded elisp for later evaluation.
1632 (org-capture-expand-embedded-elisp 'mark)
1634 ;; Expand non-interactive templates.
1635 (let ((regexp "%\\(:[-a-za-z]+\\|<\\([^>\n]+\\)>\\|[aAcfFikKlntTuUx]\\)"))
1636 (save-excursion
1637 (while (re-search-forward regexp nil t)
1638 ;; `org-capture-escaped-%' may modify buffer and cripple
1639 ;; match-data. Use markers instead. Ditto for other
1640 ;; templates.
1641 (let ((pos (copy-marker (match-beginning 0)))
1642 (end (copy-marker (match-end 0)))
1643 (value (match-string 1))
1644 (time-string (match-string 2)))
1645 (unless (org-capture-escaped-%)
1646 (delete-region pos end)
1647 (set-marker pos nil)
1648 (set-marker end nil)
1649 (let ((replacement
1650 (pcase (string-to-char value)
1651 (?< (format-time-string time-string))
1653 (or (plist-get org-store-link-plist (intern value))
1654 ""))
1655 (?i (let ((lead (buffer-substring-no-properties
1656 (line-beginning-position) (point))))
1657 (mapconcat #'identity
1658 (split-string v-i "\n")
1659 (concat "\n" lead))))
1660 (?a v-a)
1661 (?A v-A)
1662 (?c v-c)
1663 (?f v-f)
1664 (?F v-F)
1665 (?k v-k)
1666 (?K v-K)
1667 (?l v-l)
1668 (?n v-n)
1669 (?t v-t)
1670 (?T v-T)
1671 (?u v-u)
1672 (?U v-U)
1673 (?x v-x))))
1674 (insert
1675 (if (org-capture-inside-embedded-elisp-p)
1676 (replace-regexp-in-string "\"" "\\\\\"" replacement)
1677 replacement))))))))
1679 ;; Expand %() embedded Elisp. Limit to Sexp originally marked.
1680 (org-capture-expand-embedded-elisp)
1682 ;; Expand interactive templates. This is the last step so that
1683 ;; template is mostly expanded when prompting happens. Turn on
1684 ;; Org mode and set local variables. This is to support
1685 ;; completion in interactive prompts.
1686 (let ((org-inhibit-startup t)) (org-mode))
1687 (org-clone-local-variables buffer "\\`org-")
1688 (let (strings) ; Stores interactive answers.
1689 (save-excursion
1690 (let ((regexp "%\\^\\(?:{\\([^}]*\\)}\\)?\\([CgGLptTuU]\\)?"))
1691 (while (re-search-forward regexp nil t)
1692 (let* ((items (and (match-end 1)
1693 (save-match-data
1694 (split-string (match-string-no-properties 1)
1695 "|"))))
1696 (key (match-string 2))
1697 (beg (copy-marker (match-beginning 0)))
1698 (end (copy-marker (match-end 0)))
1699 (prompt (nth 0 items))
1700 (default (nth 1 items))
1701 (completions (nthcdr 2 items))
1702 (histvar
1703 (intern
1704 (concat "org-capture-template-prompt-history::"
1705 (or prompt "")))))
1706 (unless (org-capture-escaped-%)
1707 (delete-region beg end)
1708 (set-marker beg nil)
1709 (set-marker end nil)
1710 (pcase key
1711 ((or "G" "g")
1712 (let* ((org-last-tags-completion-table
1713 (org-global-tags-completion-table
1714 (cond ((equal key "G") (org-agenda-files))
1715 (file (list file))
1716 (t nil))))
1717 (org-add-colon-after-tag-completion t)
1718 (ins (mapconcat
1719 #'identity
1720 (org-split-string
1721 (completing-read
1722 (if prompt (concat prompt ": ") "Tags: ")
1723 'org-tags-completion-function nil nil nil
1724 'org-tags-history)
1725 "[^[:alnum:]_@#%]+")
1726 ":")))
1727 (when (org-string-nw-p ins)
1728 (unless (eq (char-before) ?:) (insert ":"))
1729 (insert ins)
1730 (unless (eq (char-after) ?:) (insert ":"))
1731 (and (org-at-heading-p)
1732 (let ((org-ignore-region t))
1733 (org-set-tags nil 'align))))))
1734 ("C"
1735 (cond
1736 ((= (length clipboards) 1) (insert (car clipboards)))
1737 ((> (length clipboards) 1)
1738 (insert (read-string "Clipboard/kill value: "
1739 (car clipboards)
1740 '(clipboards . 1)
1741 (car clipboards))))))
1742 ("L"
1743 (cond ((= (length clipboards) 1)
1744 (org-insert-link 0 (car clipboards)))
1745 ((> (length clipboards) 1)
1746 (org-insert-link
1748 (read-string "Clipboard/kill value: "
1749 (car clipboards)
1750 '(clipboards . 1)
1751 (car clipboards))))))
1752 ("p" (org-set-property prompt nil))
1753 ((guard key)
1754 ;; These are the date/time related ones.
1755 (let* ((upcase? (equal (upcase key) key))
1756 (org-time-was-given upcase?)
1757 (org-end-time-was-given)
1758 (time (org-read-date upcase? t nil prompt)))
1759 (org-insert-time-stamp
1760 time org-time-was-given
1761 (member key '("u" "U"))
1762 nil nil (list org-end-time-was-given))))
1764 (push (org-completing-read
1765 (concat (or prompt "Enter string")
1766 (and default (format " [%s]" default))
1767 ": ")
1768 completions nil nil nil histvar default)
1769 strings)
1770 (insert (car strings)))))))))
1772 ;; Replace %n escapes with nth %^{...} string.
1773 (setq strings (nreverse strings))
1774 (save-excursion
1775 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
1776 (unless (org-capture-escaped-%)
1777 (replace-match
1778 (nth (1- (string-to-number (match-string 1))) strings)
1779 nil t)))))
1781 ;; Make sure there are no empty lines before the text, and that
1782 ;; it ends with a newline character.
1783 (skip-chars-forward " \t\n")
1784 (delete-region (point-min) (line-beginning-position))
1785 (goto-char (point-max))
1786 (skip-chars-backward " \t\n")
1787 (delete-region (point) (point-max))
1788 (insert "\n")
1790 ;; Return the expanded template and kill the capture buffer.
1791 (untabify (point-min) (point-max))
1792 (set-buffer-modified-p nil)
1793 (prog1 (buffer-substring-no-properties (point-min) (point-max))
1794 (kill-buffer (current-buffer))))))
1796 (defun org-capture-escaped-% ()
1797 "Non-nil if % was escaped.
1798 If yes, unescape it now. Assume match-data contains the
1799 placeholder to check."
1800 (save-excursion
1801 (goto-char (match-beginning 0))
1802 (let ((n (abs (skip-chars-backward "\\\\"))))
1803 (delete-char (/ (1+ n) 2))
1804 (= (% n 2) 1))))
1806 (defun org-capture-expand-embedded-elisp (&optional mark)
1807 "Evaluate embedded elisp %(sexp) and replace with the result.
1808 When optional MARK argument is non-nil, mark Sexp with a text
1809 property (`org-embedded-elisp') for later evaluation. Only
1810 marked Sexp are evaluated when this argument is nil."
1811 (save-excursion
1812 (goto-char (point-min))
1813 (while (re-search-forward "%(" nil t)
1814 (cond
1815 ((get-text-property (match-beginning 0) 'org-embedded-elisp)
1816 (goto-char (match-beginning 0))
1817 (let ((template-start (point)))
1818 (forward-char 1)
1819 (let* ((sexp (read (current-buffer)))
1820 (result (org-eval
1821 (org-capture--expand-keyword-in-embedded-elisp
1822 sexp))))
1823 (delete-region template-start (point))
1824 (cond
1825 ((not result) nil)
1826 ((stringp result) (insert result))
1827 (t (error
1828 "Capture template sexp `%s' must evaluate to string or nil"
1829 sexp))))))
1830 ((not mark) nil)
1831 ;; Only mark valid and non-escaped sexp.
1832 ((org-capture-escaped-%) nil)
1834 (let ((end (with-syntax-table emacs-lisp-mode-syntax-table
1835 (ignore-errors (scan-sexps (1- (point)) 1)))))
1836 (when end
1837 (put-text-property (- (point) 2) end 'org-embedded-elisp t))))))))
1839 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1840 "Recursively replace capture link keywords in ATTR sexp.
1841 Such keywords are prefixed with \"%:\". See
1842 `org-capture-template' for more information."
1843 (cond ((consp attr)
1844 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr))
1845 ((symbolp attr)
1846 (let* ((attr-symbol (symbol-name attr))
1847 (key (and (string-match "%\\(:.*\\)" attr-symbol)
1848 (intern (match-string 1 attr-symbol)))))
1849 (or (plist-get org-store-link-plist key)
1850 attr)))
1851 (t attr)))
1853 (defun org-capture-inside-embedded-elisp-p ()
1854 "Non-nil if point is inside of embedded elisp %(sexp).
1855 Assume sexps have been marked with
1856 `org-capture-expand-embedded-elisp' beforehand."
1857 (get-text-property (point) 'org-embedded-elisp))
1859 ;;;###autoload
1860 (defun org-capture-import-remember-templates ()
1861 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1862 (interactive)
1863 (when (and (yes-or-no-p
1864 "Import old remember templates into org-capture-templates? ")
1865 (yes-or-no-p
1866 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1867 (require 'org-remember)
1868 (setq org-capture-templates
1869 (mapcar
1870 (lambda (entry)
1871 (let ((desc (car entry))
1872 (key (char-to-string (nth 1 entry)))
1873 (template (nth 2 entry))
1874 (file (or (nth 3 entry) org-default-notes-file))
1875 (position (or (nth 4 entry) org-remember-default-headline))
1876 (type 'entry)
1877 (prepend org-reverse-note-order)
1878 immediate target jump-to-captured)
1879 (cond
1880 ((member position '(top bottom))
1881 (setq target (list 'file file)
1882 prepend (eq position 'top)))
1883 ((eq position 'date-tree)
1884 (setq target (list 'file+datetree file)
1885 prepend nil))
1886 (t (setq target (list 'file+headline file position))))
1888 (when (string-match "%!" template)
1889 (setq template (replace-match "" t t template)
1890 immediate t))
1892 (when (string-match "%&" template)
1893 (setq jump-to-captured t))
1895 (append (list key desc type target template)
1896 (if prepend '(:prepend t))
1897 (if immediate '(:immediate-finish t))
1898 (if jump-to-captured '(:jump-to-captured t)))))
1900 org-remember-templates))))
1902 (provide 'org-capture)
1904 ;;; org-capture.el ends here