Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-capture.el
bloba46e9e68500993621483137a055c6d01f3e23f04
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 which both visits
170 the file and moves point to the right 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 (let ((file-variants '(choice :tag "Filename "
303 (file :tag "Literal")
304 (function :tag "Function")
305 (variable :tag "Variable")
306 (sexp :tag "Form"))))
307 `(repeat
308 (choice :value ("" "" entry (file "~/org/notes.org") "")
309 (list :tag "Multikey description"
310 (string :tag "Keys ")
311 (string :tag "Description"))
312 (list :tag "Template entry"
313 (string :tag "Keys ")
314 (string :tag "Description ")
315 (choice :tag "Capture Type " :value entry
316 (const :tag "Org entry" entry)
317 (const :tag "Plain list item" item)
318 (const :tag "Checkbox item" checkitem)
319 (const :tag "Plain text" plain)
320 (const :tag "Table line" table-line))
321 (choice :tag "Target location"
322 (list :tag "File"
323 (const :format "" file)
324 ,file-variants)
325 (list :tag "ID"
326 (const :format "" id)
327 (string :tag " ID"))
328 (list :tag "File & Headline"
329 (const :format "" file+headline)
330 ,file-variants
331 (string :tag " Headline"))
332 (list :tag "File & Outline path"
333 (const :format "" file+olp)
334 ,file-variants
335 (repeat :tag "Outline path" :inline t
336 (string :tag "Headline")))
337 (list :tag "File & Regexp"
338 (const :format "" file+regexp)
339 ,file-variants
340 (regexp :tag " Regexp"))
341 (list :tag "File & Date tree"
342 (const :format "" file+datetree)
343 ,file-variants)
344 (list :tag "File & Date tree, prompt for date"
345 (const :format "" file+datetree+prompt)
346 ,file-variants)
347 (list :tag "File & Week tree"
348 (const :format "" file+weektree)
349 ,file-variants)
350 (list :tag "File & Week tree, prompt for date"
351 (const :format "" file+weektree+prompt)
352 ,file-variants)
353 (list :tag "File & function"
354 (const :format "" file+function)
355 ,file-variants
356 (sexp :tag " Function"))
357 (list :tag "Current clocking task"
358 (const :format "" clock))
359 (list :tag "Function"
360 (const :format "" function)
361 (sexp :tag " Function")))
362 (choice :tag "Template "
363 (string)
364 (list :tag "File"
365 (const :format "" file)
366 (file :tag "Template file"))
367 (list :tag "Function"
368 (const :format "" function)
369 (function :tag "Template function")))
370 (plist :inline t
371 ;; Give the most common options as checkboxes
372 :options (((const :format "%v " :prepend) (const t))
373 ((const :format "%v " :immediate-finish) (const t))
374 ((const :format "%v " :jump-to-captured) (const t))
375 ((const :format "%v " :empty-lines) (const 1))
376 ((const :format "%v " :empty-lines-before) (const 1))
377 ((const :format "%v " :empty-lines-after) (const 1))
378 ((const :format "%v " :clock-in) (const t))
379 ((const :format "%v " :clock-keep) (const t))
380 ((const :format "%v " :clock-resume) (const t))
381 ((const :format "%v " :unnarrowed) (const t))
382 ((const :format "%v " :table-line-pos) (const t))
383 ((const :format "%v " :kill-buffer) (const t)))))))))
385 (defcustom org-capture-before-finalize-hook nil
386 "Hook that is run right before a capture process is finalized.
387 The capture buffer is still current when this hook runs and it is
388 widened to the entire buffer."
389 :group 'org-capture
390 :version "24.1"
391 :type 'hook)
393 (defcustom org-capture-after-finalize-hook nil
394 "Hook that is run right after a capture process is finalized.
395 Suitable for window cleanup."
396 :group 'org-capture
397 :version "24.1"
398 :type 'hook)
400 (defcustom org-capture-prepare-finalize-hook nil
401 "Hook that is run before the finalization starts.
402 The capture buffer is current and still narrowed."
403 :group 'org-capture
404 :version "24.1"
405 :type 'hook)
407 (defcustom org-capture-bookmark t
408 "When non-nil, add a bookmark pointing at the last stored
409 position when capturing."
410 :group 'org-capture
411 :version "24.3"
412 :type 'boolean)
414 ;;; The property list for keeping information about the capture process
416 (defvar org-capture-plist nil
417 "Plist for the current capture process, global, to avoid having to pass it.")
419 (defvar org-capture-current-plist nil
420 "Local variable holding the plist in a capture buffer.
421 This is used to store the plist for use when finishing a capture process
422 because another such process might have changed the global variable by then.
424 Each time a new capture buffer has been set up, the global `org-capture-plist'
425 is copied to this variable, which is local in the indirect buffer.")
427 (defvar org-capture-clock-keep nil
428 "Local variable to store the value of the :clock-keep parameter.
429 This is needed in case org-capture-finalize is called interactively.")
431 (defun org-capture-put (&rest stuff)
432 "Add properties to the capture property list `org-capture-plist'."
433 (while stuff
434 (setq org-capture-plist (plist-put org-capture-plist
435 (pop stuff) (pop stuff)))))
436 (defun org-capture-get (prop &optional local)
437 "Get properties from the capture property list `org-capture-plist'.
438 When LOCAL is set, use the local variable `org-capture-current-plist',
439 this is necessary after initialization of the capture process,
440 to avoid conflicts with other active capture processes."
441 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
443 (defun org-capture-member (prop &optional local)
444 "Is PROP a property in `org-capture-plist'.
445 When LOCAL is set, use the local variable `org-capture-current-plist',
446 this is necessary after initialization of the capture process,
447 to avoid conflicts with other active capture processes."
448 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
450 ;;; The minor mode
452 (defvar org-capture-mode-map (make-sparse-keymap)
453 "Keymap for `org-capture-mode', a minor mode.
454 Use this map to set additional keybindings for when Org-mode is used
455 for a capture buffer.")
457 (defvar org-capture-mode-hook nil
458 "Hook for the minor `org-capture-mode'.")
460 (define-minor-mode org-capture-mode
461 "Minor mode for special key bindings in a capture buffer.
463 Turning on this mode runs the normal hook `org-capture-mode-hook'."
464 nil " Rem" org-capture-mode-map
465 (setq-local
466 header-line-format
467 (substitute-command-keys
468 "\\<org-capture-mode-map>Capture buffer. Finish \\[org-capture-finalize], \
469 refile \\[org-capture-refile], abort \\[org-capture-kill].")))
470 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
471 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
472 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
474 ;;; The main commands
476 (defvar org-capture-initial nil)
477 (defvar org-capture-entry nil)
479 ;;;###autoload
480 (defun org-capture-string (string &optional keys)
481 "Capture STRING with the template selected by KEYS."
482 (interactive "sInitial text: \n")
483 (let ((org-capture-initial string)
484 (org-capture-entry (org-capture-select-template keys)))
485 (org-capture)))
487 (defcustom org-capture-templates-contexts nil
488 "Alist of capture templates and valid contexts.
490 For example, if you have a capture template \"c\" and you want
491 this template to be accessible only from `message-mode' buffers,
492 use this:
494 \\='((\"c\" ((in-mode . \"message-mode\"))))
496 Here are the available contexts definitions:
498 in-file: command displayed only in matching files
499 in-mode: command displayed only in matching modes
500 not-in-file: command not displayed in matching files
501 not-in-mode: command not displayed in matching modes
502 in-buffer: command displayed only in matching buffers
503 not-in-buffer: command not displayed in matching buffers
504 [function]: a custom function taking no argument
506 If you define several checks, the agenda command will be
507 accessible if there is at least one valid check.
509 You can also bind a key to another agenda custom command
510 depending on contextual rules.
512 \\='((\"c\" \"d\" ((in-mode . \"message-mode\"))))
514 Here it means: in `message-mode buffers', use \"c\" as the
515 key for the capture template otherwise associated with \"d\".
516 \(The template originally associated with \"d\" is not displayed
517 to avoid duplicates.)"
518 :version "24.3"
519 :group 'org-capture
520 :type '(repeat (list :tag "Rule"
521 (string :tag " Capture key")
522 (string :tag "Replace by template")
523 (repeat :tag "Available when"
524 (choice
525 (cons :tag "Condition"
526 (choice
527 (const :tag "In file" in-file)
528 (const :tag "Not in file" not-in-file)
529 (const :tag "In buffer" in-buffer)
530 (const :tag "Not in buffer" not-in-buffer)
531 (const :tag "In mode" in-mode)
532 (const :tag "Not in mode" not-in-mode))
533 (regexp))
534 (function :tag "Custom function"))))))
536 (defcustom org-capture-use-agenda-date nil
537 "Non-nil means use the date at point when capturing from agendas.
538 When nil, you can still capture using the date at point with \\[org-agenda-capture]."
539 :group 'org-capture
540 :version "24.3"
541 :type 'boolean)
543 ;;;###autoload
544 (defun org-capture (&optional goto keys)
545 "Capture something.
546 \\<org-capture-mode-map>
547 This will let you select a template from `org-capture-templates', and then
548 file the newly captured information. The text is immediately inserted
549 at the target location, and an indirect buffer is shown where you can
550 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
551 of Emacs, so that you can continue your work.
553 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
554 anything, just go to the file/headline where the selected template
555 stores its notes. With a double prefix argument \
556 \\[universal-argument] \\[universal-argument], go to the last note
557 stored.
559 When called with a `C-0' (zero) prefix, insert a template at point.
561 ELisp programs can set KEYS to a string associated with a template
562 in `org-capture-templates'. In this case, interactive selection
563 will be bypassed.
565 If `org-capture-use-agenda-date' is non-nil, capturing from the
566 agenda will use the date at point as the default date. Then, a
567 `C-1' prefix will tell the capture process to use the HH:MM time
568 of the day at point (if any) or the current HH:MM time."
569 (interactive "P")
570 (when (and org-capture-use-agenda-date
571 (eq major-mode 'org-agenda-mode))
572 (setq org-overriding-default-time
573 (org-get-cursor-date (equal goto 1))))
574 (cond
575 ((equal goto '(4)) (org-capture-goto-target))
576 ((equal goto '(16)) (org-capture-goto-last-stored))
578 ;; FIXME: Are these needed?
579 (let* ((orig-buf (current-buffer))
580 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
581 org-capture-link-is-already-stored)
582 (plist-get org-store-link-plist :annotation)
583 (ignore-errors (org-store-link nil))))
584 (entry (or org-capture-entry (org-capture-select-template keys)))
585 initial)
586 (setq initial (or org-capture-initial
587 (and (org-region-active-p)
588 (buffer-substring (point) (mark)))))
589 (when (stringp initial)
590 (remove-text-properties 0 (length initial) '(read-only t) initial))
591 (when (stringp annotation)
592 (remove-text-properties 0 (length annotation)
593 '(read-only t) annotation))
594 (cond
595 ((equal entry "C")
596 (customize-variable 'org-capture-templates))
597 ((equal entry "q")
598 (user-error "Abort"))
600 (org-capture-set-plist entry)
601 (org-capture-get-template)
602 (org-capture-put :original-buffer orig-buf
603 :original-file (or (buffer-file-name orig-buf)
604 (and (featurep 'dired)
605 (car (rassq orig-buf
606 dired-buffers))))
607 :original-file-nondirectory
608 (and (buffer-file-name orig-buf)
609 (file-name-nondirectory
610 (buffer-file-name orig-buf)))
611 :annotation annotation
612 :initial initial
613 :return-to-wconf (current-window-configuration)
614 :default-time
615 (or org-overriding-default-time
616 (org-current-time)))
617 (org-capture-set-target-location)
618 (condition-case error
619 (org-capture-put :template (org-capture-fill-template))
620 ((error quit)
621 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
622 (error "Capture abort: %s" error)))
624 (setq org-capture-clock-keep (org-capture-get :clock-keep))
625 (if (equal goto 0)
626 ;;insert at point
627 (org-capture-insert-template-here)
628 (condition-case error
629 (org-capture-place-template
630 (equal (car (org-capture-get :target)) 'function))
631 ((error quit)
632 (if (and (buffer-base-buffer (current-buffer))
633 (string-match "\\`CAPTURE-" (buffer-name)))
634 (kill-buffer (current-buffer)))
635 (set-window-configuration (org-capture-get :return-to-wconf))
636 (error "Capture template `%s': %s"
637 (org-capture-get :key)
638 (nth 1 error))))
639 (if (and (derived-mode-p 'org-mode)
640 (org-capture-get :clock-in))
641 (condition-case nil
642 (progn
643 (if (org-clock-is-active)
644 (org-capture-put :interrupted-clock
645 (copy-marker org-clock-marker)))
646 (org-clock-in)
647 (setq-local org-capture-clock-was-started t))
648 (error
649 "Could not start the clock in this capture buffer")))
650 (if (org-capture-get :immediate-finish)
651 (org-capture-finalize)))))))))
653 (defun org-capture-get-template ()
654 "Get the template from a file or a function if necessary."
655 (let ((txt (org-capture-get :template)) file)
656 (cond
657 ((and (listp txt) (eq (car txt) 'file))
658 (if (file-exists-p
659 (setq file (expand-file-name (nth 1 txt) org-directory)))
660 (setq txt (org-file-contents file))
661 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
662 ((and (listp txt) (eq (car txt) 'function))
663 (if (fboundp (nth 1 txt))
664 (setq txt (funcall (nth 1 txt)))
665 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
666 ((not txt) (setq txt ""))
667 ((stringp txt))
668 (t (setq txt "* Invalid capture template")))
669 (org-capture-put :template txt)))
671 (defun org-capture-finalize (&optional stay-with-capture)
672 "Finalize the capture process.
673 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
674 captured item after finalizing."
675 (interactive "P")
676 (when (org-capture-get :jump-to-captured)
677 (setq stay-with-capture t))
678 (unless (and org-capture-mode
679 (buffer-base-buffer (current-buffer)))
680 (error "This does not seem to be a capture buffer for Org-mode"))
682 (run-hooks 'org-capture-prepare-finalize-hook)
684 ;; Did we start the clock in this capture buffer?
685 (when (and org-capture-clock-was-started
686 org-clock-marker (marker-buffer org-clock-marker)
687 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
688 (> org-clock-marker (point-min))
689 (< org-clock-marker (point-max)))
690 ;; Looks like the clock we started is still running. Clock out.
691 (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
692 (when (and (not org-capture-clock-keep)
693 (org-capture-get :clock-resume 'local)
694 (markerp (org-capture-get :interrupted-clock 'local))
695 (buffer-live-p (marker-buffer
696 (org-capture-get :interrupted-clock 'local))))
697 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
698 (org-with-point-at clock-in-task
699 (org-clock-in)))
700 (message "Interrupted clock has been resumed")))
702 (let ((beg (point-min))
703 (end (point-max))
704 (abort-note nil))
705 ;; Store the size of the capture buffer
706 (org-capture-put :captured-entry-size (- (point-max) (point-min)))
707 (widen)
708 ;; Store the insertion point in the target buffer
709 (org-capture-put :insertion-point (point))
711 (if org-note-abort
712 (let ((m1 (org-capture-get :begin-marker 'local))
713 (m2 (org-capture-get :end-marker 'local)))
714 (if (and m1 m2 (= m1 beg) (= m2 end))
715 (progn
716 (setq m2 (if (cdr (assoc 'heading org-blank-before-new-entry))
717 m2 (1+ m2))
718 m2 (if (< (point-max) m2) (point-max) m2))
719 (setq abort-note 'clean)
720 (kill-region m1 m2))
721 (setq abort-note 'dirty)))
723 ;; Make sure that the empty lines after are correct
724 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
725 (member (org-capture-get :type 'local)
726 '(entry item checkitem plain)))
727 (save-excursion
728 (goto-char end)
729 (or (bolp) (newline))
730 (org-capture-empty-lines-after
731 (or (org-capture-get :empty-lines-after 'local)
732 (org-capture-get :empty-lines 'local) 0))))
733 ;; Postprocessing: Update Statistics cookies, do the sorting
734 (when (derived-mode-p 'org-mode)
735 (save-excursion
736 (when (ignore-errors (org-back-to-heading))
737 (org-update-parent-todo-statistics)
738 (org-update-checkbox-count)))
739 ;; FIXME Here we should do the sorting
740 ;; If we have added a table line, maybe recompute?
741 (when (and (eq (org-capture-get :type 'local) 'table-line)
742 (org-at-table-p))
743 (if (org-table-get-stored-formulas)
744 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
745 (org-table-align))))
746 ;; Store this place as the last one where we stored something
747 ;; Do the marking in the base buffer, so that it makes sense after
748 ;; the indirect buffer has been killed.
749 (when org-capture-bookmark
750 (org-capture-bookmark-last-stored-position))
752 ;; Run the hook
753 (run-hooks 'org-capture-before-finalize-hook))
755 (when (org-capture-get :decrypted)
756 (save-excursion
757 (goto-char (org-capture-get :decrypted))
758 (org-encrypt-entry)))
760 ;; Kill the indirect buffer
761 (save-buffer)
762 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
763 (new-buffer (org-capture-get :new-buffer 'local))
764 (kill-buffer (org-capture-get :kill-buffer 'local))
765 (base-buffer (buffer-base-buffer (current-buffer))))
767 ;; Kill the indirect buffer
768 (kill-buffer (current-buffer))
770 ;; Narrow back the target buffer to its previous state
771 (with-current-buffer (org-capture-get :buffer)
772 (let ((reg (org-capture-get :initial-target-region))
773 (pos (org-capture-get :initial-target-position))
774 (ipt (org-capture-get :insertion-point))
775 (size (org-capture-get :captured-entry-size)))
776 (if (not reg)
777 (widen)
778 (cond ((< ipt (car reg))
779 ;; insertion point is before the narrowed region
780 (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
781 ((> ipt (cdr reg))
782 ;; insertion point is after the narrowed region
783 (narrow-to-region (car reg) (cdr reg)))
785 ;; insertion point is within the narrowed region
786 (narrow-to-region (car reg) (+ size (cdr reg)))))
787 ;; now place back the point at its original position
788 (if (< ipt (car reg))
789 (goto-char (+ size pos))
790 (goto-char (if (< ipt pos) (+ size pos) pos))))))
792 ;; Kill the target buffer if that is desired
793 (when (and base-buffer new-buffer kill-buffer)
794 (with-current-buffer base-buffer (save-buffer))
795 (kill-buffer base-buffer))
797 ;; Restore the window configuration before capture
798 (set-window-configuration return-wconf))
800 (run-hooks 'org-capture-after-finalize-hook)
801 ;; Special cases
802 (cond
803 (abort-note
804 (cond
805 ((equal abort-note 'clean)
806 (message "Capture process aborted and target buffer cleaned up"))
807 ((equal abort-note 'dirty)
808 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
809 (stay-with-capture
810 (org-capture-goto-last-stored)))
811 ;; Return if we did store something
812 (not abort-note)))
814 (defun org-capture-refile ()
815 "Finalize the current capture and then refile the entry.
816 Refiling is done from the base buffer, because the indirect buffer is then
817 already gone. Any prefix argument will be passed to the refile command."
818 (interactive)
819 (unless (eq (org-capture-get :type 'local) 'entry)
820 (error
821 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
822 (let ((pos (point))
823 (base (buffer-base-buffer (current-buffer)))
824 (org-capture-is-refiling t)
825 (kill-buffer (org-capture-get :kill-buffer 'local)))
826 (org-capture-put :kill-buffer nil)
827 (org-capture-finalize)
828 (save-window-excursion
829 (with-current-buffer (or base (current-buffer))
830 (save-excursion
831 (save-restriction
832 (widen)
833 (goto-char pos)
834 (call-interactively 'org-refile)))))
835 (when kill-buffer (kill-buffer base))))
837 (defun org-capture-kill ()
838 "Abort the current capture process."
839 (interactive)
840 ;; FIXME: This does not do the right thing, we need to remove the
841 ;; new stuff by hand it is easy: undo, then kill the buffer
842 (let ((org-note-abort t)
843 (org-capture-before-finalize-hook nil))
844 (org-capture-finalize)))
846 (defun org-capture-goto-last-stored ()
847 "Go to the location where the last capture note was stored."
848 (interactive)
849 (org-goto-marker-or-bmk org-capture-last-stored-marker
850 (plist-get org-bookmark-names-plist
851 :last-capture))
852 (message "This is the last note stored by a capture process"))
854 ;;; Supporting functions for handling the process
856 (defun org-capture-put-target-region-and-position ()
857 "Store the initial region with `org-capture-put'."
858 (org-capture-put
859 :initial-target-region
860 ;; Check if the buffer is currently narrowed
861 (when (org-buffer-narrowed-p)
862 (cons (point-min) (point-max))))
863 ;; store the current point
864 (org-capture-put :initial-target-position (point)))
866 (defvar org-time-was-given) ; dynamically scoped parameter
867 (defun org-capture-set-target-location (&optional target)
868 "Find TARGET buffer and position.
869 Store them in the capture property list."
870 (let ((target-entry-p t) decrypted-hl-pos)
871 (setq target (or target (org-capture-get :target)))
872 (save-excursion
873 (cond
874 ((eq (car target) 'file)
875 (set-buffer (org-capture-target-buffer (nth 1 target)))
876 (org-capture-put-target-region-and-position)
877 (widen)
878 (setq target-entry-p nil))
880 ((eq (car target) 'id)
881 (let ((loc (org-id-find (nth 1 target))))
882 (if (not loc)
883 (error "Cannot find target ID \"%s\"" (nth 1 target))
884 (set-buffer (org-capture-target-buffer (car loc)))
885 (widen)
886 (org-capture-put-target-region-and-position)
887 (goto-char (cdr loc)))))
889 ((eq (car target) 'file+headline)
890 (set-buffer (org-capture-target-buffer (nth 1 target)))
891 (org-capture-put-target-region-and-position)
892 (widen)
893 (let ((hd (nth 2 target)))
894 (goto-char (point-min))
895 (unless (derived-mode-p 'org-mode)
896 (error
897 "Target buffer \"%s\" for file+headline should be in Org mode"
898 (current-buffer)))
899 (if (re-search-forward
900 (format org-complex-heading-regexp-format (regexp-quote hd))
901 nil t)
902 (goto-char (point-at-bol))
903 (goto-char (point-max))
904 (or (bolp) (insert "\n"))
905 (insert "* " hd "\n")
906 (beginning-of-line 0))))
908 ((eq (car target) 'file+olp)
909 (let ((m (org-find-olp
910 (cons (org-capture-expand-file (nth 1 target))
911 (cddr target)))))
912 (set-buffer (marker-buffer m))
913 (org-capture-put-target-region-and-position)
914 (widen)
915 (goto-char m)))
917 ((eq (car target) 'file+regexp)
918 (set-buffer (org-capture-target-buffer (nth 1 target)))
919 (org-capture-put-target-region-and-position)
920 (widen)
921 (goto-char (point-min))
922 (if (re-search-forward (nth 2 target) nil t)
923 (progn
924 (goto-char (if (org-capture-get :prepend)
925 (match-beginning 0) (match-end 0)))
926 (org-capture-put :exact-position (point))
927 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
928 (error "No match for target regexp in file %s" (nth 1 target))))
930 ((memq (car target) '(file+datetree file+datetree+prompt file+weektree file+weektree+prompt))
931 (require 'org-datetree)
932 (set-buffer (org-capture-target-buffer (nth 1 target)))
933 (org-capture-put-target-region-and-position)
934 (widen)
935 ;; Make a date/week tree entry, with the current date (or
936 ;; yesterday, if we are extending dates for a couple of hours)
937 (funcall
938 (cond
939 ((memq (car target) '(file+weektree file+weektree+prompt))
940 #'org-datetree-find-iso-week-create)
941 (t #'org-datetree-find-date-create))
942 (calendar-gregorian-from-absolute
943 (cond
944 (org-overriding-default-time
945 ;; use the overriding default time
946 (time-to-days org-overriding-default-time))
948 ((memq (car target) '(file+datetree+prompt file+weektree+prompt))
949 ;; prompt for date
950 (let ((prompt-time (org-read-date
951 nil t nil "Date for tree entry:"
952 (current-time))))
953 (org-capture-put
954 :default-time
955 (cond ((and (or (not (boundp 'org-time-was-given))
956 (not org-time-was-given))
957 (not (= (time-to-days prompt-time) (org-today))))
958 ;; Use 00:00 when no time is given for another date than today?
959 (apply 'encode-time (append '(0 0 0) (cdddr (decode-time prompt-time)))))
960 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer)
961 ;; Replace any time range by its start
962 (apply 'encode-time
963 (org-read-date-analyze
964 (replace-match "\\1 \\2" nil nil org-read-date-final-answer)
965 prompt-time (decode-time prompt-time))))
966 (t prompt-time)))
967 (time-to-days prompt-time)))
969 ;; current date, possibly corrected for late night workers
970 (org-today))))))
972 ((eq (car target) 'file+function)
973 (set-buffer (org-capture-target-buffer (nth 1 target)))
974 (org-capture-put-target-region-and-position)
975 (widen)
976 (funcall (nth 2 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) 'function)
981 (funcall (nth 1 target))
982 (org-capture-put :exact-position (point))
983 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
985 ((eq (car target) 'clock)
986 (if (and (markerp org-clock-hd-marker)
987 (marker-buffer org-clock-hd-marker))
988 (progn (set-buffer (marker-buffer org-clock-hd-marker))
989 (org-capture-put-target-region-and-position)
990 (widen)
991 (goto-char org-clock-hd-marker))
992 (error "No running clock that could be used as capture target")))
994 (t (error "Invalid capture target specification")))
996 (when (and (featurep 'org-crypt) (org-at-encrypted-entry-p))
997 (org-decrypt-entry)
998 (setq decrypted-hl-pos
999 (save-excursion (and (org-back-to-heading t) (point)))))
1001 (org-capture-put :buffer (current-buffer) :pos (point)
1002 :target-entry-p target-entry-p
1003 :decrypted decrypted-hl-pos))))
1005 (defun org-capture-expand-file (file)
1006 "Expand functions and symbols for FILE.
1007 When FILE is a function, call it. When it is a form, evaluate
1008 it. When it is a variable, retrieve the value. When it is
1009 a string, return it. However, if it is the empty string, return
1010 `org-default-notes-file' instead."
1011 (cond
1012 ((equal file "") org-default-notes-file)
1013 ((org-string-nw-p file) file)
1014 ((functionp file) (funcall file))
1015 ((and (symbolp file) (boundp file)) (symbol-value file))
1016 ((consp file) (eval file))
1017 (t file)))
1019 (defun org-capture-target-buffer (file)
1020 "Get a buffer for FILE."
1021 (setq file (org-capture-expand-file file))
1022 (setq file (or (org-string-nw-p file)
1023 org-default-notes-file
1024 (error "No notes file specified, and no default available")))
1025 (or (org-find-base-buffer-visiting file)
1026 (progn (org-capture-put :new-buffer t)
1027 (find-file-noselect (expand-file-name file org-directory)))))
1029 (defun org-capture-place-template (&optional inhibit-wconf-store)
1030 "Insert the template at the target location, and display the buffer.
1031 When `inhibit-wconf-store', don't store the window configuration, as it
1032 may have been stored before."
1033 (unless inhibit-wconf-store
1034 (org-capture-put :return-to-wconf (current-window-configuration)))
1035 (delete-other-windows)
1036 (org-switch-to-buffer-other-window
1037 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
1038 (widen)
1039 (outline-show-all)
1040 (goto-char (org-capture-get :pos))
1041 (setq-local outline-level 'org-outline-level)
1042 (pcase (org-capture-get :type)
1043 ((or `nil `entry) (org-capture-place-entry))
1044 (`table-line (org-capture-place-table-line))
1045 (`plain (org-capture-place-plain-text))
1046 (`item (org-capture-place-item))
1047 (`checkitem (org-capture-place-item)))
1048 (org-capture-mode 1)
1049 (setq-local org-capture-current-plist org-capture-plist))
1051 (defun org-capture-place-entry ()
1052 "Place the template as a new Org entry."
1053 (let* ((txt (org-capture-get :template))
1054 (reversed (org-capture-get :prepend))
1055 (target-entry-p (org-capture-get :target-entry-p))
1056 level beg end)
1058 (and (org-capture-get :exact-position)
1059 (goto-char (org-capture-get :exact-position)))
1060 (cond
1061 ((not target-entry-p)
1062 ;; Insert as top-level entry, either at beginning or at end of
1063 ;; file.
1064 (setq level 1)
1065 (if reversed
1066 (progn (goto-char (point-min))
1067 (or (org-at-heading-p)
1068 (outline-next-heading)))
1069 (goto-char (point-max))
1070 (or (bolp) (insert "\n"))))
1072 ;; Insert as a child of the current entry
1073 (and (looking-at "\\*+")
1074 (setq level (- (match-end 0) (match-beginning 0))))
1075 (setq level (org-get-valid-level (or level 1) 1))
1076 (if reversed
1077 (progn
1078 (outline-next-heading)
1079 (or (bolp) (insert "\n")))
1080 (org-end-of-subtree t nil)
1081 (or (bolp) (insert "\n")))))
1082 (org-capture-empty-lines-before)
1083 (setq beg (point))
1084 (org-capture-verify-tree txt)
1085 (org-paste-subtree level txt 'for-yank)
1086 (org-capture-empty-lines-after 1)
1087 (org-capture-position-for-last-stored beg)
1088 (outline-next-heading)
1089 (setq end (point))
1090 (org-capture-mark-kill-region beg (1- end))
1091 (org-capture-narrow beg (1- end))
1092 (if (or (re-search-backward "%\\?" beg t)
1093 (re-search-forward "%\\?" end t))
1094 (replace-match ""))))
1096 (defun org-capture-place-item ()
1097 "Place the template as a new plain list item."
1098 (let* ((txt (org-capture-get :template))
1099 (target-entry-p (org-capture-get :target-entry-p))
1100 (ind 0)
1101 beg end)
1102 (if (org-capture-get :exact-position)
1103 (goto-char (org-capture-get :exact-position))
1104 (cond
1105 ((not target-entry-p)
1106 ;; Insert as top-level entry, either at beginning or at end of file
1107 (setq beg (point-min) end (point-max)))
1109 (setq beg (1+ (point-at-eol))
1110 end (save-excursion (outline-next-heading) (point)))))
1111 (setq ind nil)
1112 (if (org-capture-get :prepend)
1113 (progn
1114 (goto-char beg)
1115 (when (org-list-search-forward (org-item-beginning-re) end t)
1116 (goto-char (match-beginning 0))
1117 (setq ind (org-get-indentation))))
1118 (goto-char end)
1119 (when (org-list-search-backward (org-item-beginning-re) beg t)
1120 (setq ind (org-get-indentation))
1121 (org-end-of-item)))
1122 (unless ind (goto-char end)))
1123 ;; Remove common indentation
1124 (setq txt (org-remove-indentation txt))
1125 ;; Make sure this is indeed an item
1126 (unless (string-match (concat "\\`" (org-item-re)) txt)
1127 (setq txt (concat "- "
1128 (mapconcat 'identity (split-string txt "\n")
1129 "\n "))))
1130 ;; Prepare surrounding empty lines.
1131 (org-capture-empty-lines-before)
1132 (setq beg (point))
1133 (unless (eolp) (save-excursion (insert "\n")))
1134 (unless ind
1135 (org-indent-line)
1136 (setq ind (org-get-indentation))
1137 (delete-region beg (point)))
1138 ;; Set the correct indentation, depending on context
1139 (setq ind (make-string ind ?\ ))
1140 (setq txt (concat ind
1141 (mapconcat 'identity (split-string txt "\n")
1142 (concat "\n" ind))
1143 "\n"))
1144 ;; Insert item.
1145 (insert txt)
1146 (org-capture-empty-lines-after 1)
1147 (org-capture-position-for-last-stored beg)
1148 (forward-char 1)
1149 (setq end (point))
1150 (org-capture-mark-kill-region beg (1- end))
1151 (org-capture-narrow beg (1- end))
1152 (if (or (re-search-backward "%\\?" beg t)
1153 (re-search-forward "%\\?" end t))
1154 (replace-match ""))))
1156 (defun org-capture-place-table-line ()
1157 "Place the template as a table line."
1158 (require 'org-table)
1159 (let* ((txt (org-capture-get :template))
1160 (target-entry-p (org-capture-get :target-entry-p))
1161 (table-line-pos (org-capture-get :table-line-pos))
1162 beg end)
1163 (cond
1164 ((org-capture-get :exact-position)
1165 (goto-char (org-capture-get :exact-position)))
1166 ((not target-entry-p)
1167 ;; Table is not necessarily under a heading
1168 (setq beg (point-min) end (point-max)))
1170 ;; WE are at a heading, limit search to the body
1171 (setq beg (1+ (point-at-eol))
1172 end (save-excursion (outline-next-heading) (point)))))
1173 (if (re-search-forward org-table-dataline-regexp end t)
1174 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t))
1175 (goto-char e)
1176 (if (looking-at "[ \t]*#\\+tblfm:")
1177 (forward-line 1))
1178 (narrow-to-region b (point)))
1179 (goto-char end)
1180 (insert "\n| |\n|----|\n| |\n")
1181 (narrow-to-region (1+ end) (point)))
1182 ;; We are narrowed to the table, or to an empty line if there was no table
1184 ;; Check if the template is good
1185 (if (not (string-match org-table-dataline-regexp txt))
1186 (setq txt "| %?Bad template |\n"))
1187 (if (functionp table-line-pos)
1188 (setq table-line-pos (funcall table-line-pos))
1189 (setq table-line-pos (eval table-line-pos)))
1190 (cond
1191 ((and table-line-pos
1192 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
1193 (goto-char (point-min))
1194 ;; we have a complex line specification
1195 (let ((ll (ignore-errors
1196 (save-match-data (org-table-analyze))
1197 (aref org-table-hlines
1198 (- (match-end 1) (match-beginning 1)))))
1199 (delta (string-to-number (match-string 2 table-line-pos))))
1200 ;; The user wants a special position in the table
1201 (unless ll
1202 (error "Invalid table line specification \"%s\"" table-line-pos))
1203 (goto-char org-table-current-begin-pos)
1204 (forward-line (+ ll delta (if (< delta 0) 0 -1)))
1205 (org-table-insert-row 'below)
1206 (beginning-of-line 1)
1207 (delete-region (point) (1+ (point-at-eol)))
1208 (setq beg (point))
1209 (insert txt)
1210 (setq end (point))))
1211 ((org-capture-get :prepend)
1212 (goto-char (point-min))
1213 (re-search-forward org-table-hline-regexp nil t)
1214 (beginning-of-line 1)
1215 (re-search-forward org-table-dataline-regexp nil t)
1216 (beginning-of-line 1)
1217 (setq beg (point))
1218 (org-table-insert-row)
1219 (beginning-of-line 1)
1220 (delete-region (point) (1+ (point-at-eol)))
1221 (insert txt)
1222 (setq end (point)))
1224 (goto-char (point-max))
1225 (re-search-backward org-table-dataline-regexp nil t)
1226 (beginning-of-line 1)
1227 (org-table-insert-row 'below)
1228 (beginning-of-line 1)
1229 (delete-region (point) (1+ (point-at-eol)))
1230 (setq beg (point))
1231 (insert txt)
1232 (setq end (point))))
1233 (goto-char beg)
1234 (org-capture-position-for-last-stored 'table-line)
1235 (if (or (re-search-backward "%\\?" beg t)
1236 (re-search-forward "%\\?" end t))
1237 (replace-match ""))
1238 (org-table-align)))
1240 (defun org-capture-place-plain-text ()
1241 "Place the template plainly.
1242 If the target locator points at an Org node, place the template into
1243 the text of the entry, before the first child. If not, place the
1244 template at the beginning or end of the file.
1245 Of course, if exact position has been required, just put it there."
1246 (let* ((txt (org-capture-get :template))
1247 beg end)
1248 (cond
1249 ((org-capture-get :exact-position)
1250 (goto-char (org-capture-get :exact-position)))
1251 ((and (org-capture-get :target-entry-p)
1252 (bolp)
1253 (looking-at org-outline-regexp))
1254 ;; we should place the text into this entry
1255 (if (org-capture-get :prepend)
1256 ;; Skip meta data and drawers
1257 (org-end-of-meta-data t)
1258 ;; go to ent of the entry text, before the next headline
1259 (outline-next-heading)))
1261 ;; beginning or end of file
1262 (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
1263 (or (bolp) (newline))
1264 (org-capture-empty-lines-before)
1265 (setq beg (point))
1266 (insert txt)
1267 (org-capture-empty-lines-after 1)
1268 (org-capture-position-for-last-stored beg)
1269 (setq end (point))
1270 (org-capture-mark-kill-region beg (1- end))
1271 (org-capture-narrow beg (1- end))
1272 (if (or (re-search-backward "%\\?" beg t)
1273 (re-search-forward "%\\?" end t))
1274 (replace-match ""))))
1276 (defun org-capture-mark-kill-region (beg end)
1277 "Mark the region that will have to be killed when aborting capture."
1278 (let ((m1 (move-marker (make-marker) beg))
1279 (m2 (move-marker (make-marker) end)))
1280 (org-capture-put :begin-marker m1)
1281 (org-capture-put :end-marker m2)))
1283 (defun org-capture-position-for-last-stored (where)
1284 "Memorize the position that should later become the position of last capture."
1285 (cond
1286 ((integerp where)
1287 (org-capture-put :position-for-last-stored
1288 (move-marker (make-marker) where
1289 (or (buffer-base-buffer (current-buffer))
1290 (current-buffer)))))
1291 ((eq where 'table-line)
1292 (org-capture-put :position-for-last-stored
1293 (list 'table-line
1294 (org-table-current-dline))))
1295 (t (error "This should not happen"))))
1297 (defun org-capture-bookmark-last-stored-position ()
1298 "Bookmark the last-captured position."
1299 (let* ((where (org-capture-get :position-for-last-stored 'local))
1300 (pos (cond
1301 ((markerp where)
1302 (prog1 (marker-position where)
1303 (move-marker where nil)))
1304 ((and (listp where) (eq (car where) 'table-line))
1305 (if (org-at-table-p)
1306 (save-excursion
1307 (org-table-goto-line (nth 1 where))
1308 (point-at-bol))
1309 (point))))))
1310 (with-current-buffer (buffer-base-buffer (current-buffer))
1311 (save-excursion
1312 (save-restriction
1313 (widen)
1314 (goto-char pos)
1315 (let ((bookmark-name (plist-get org-bookmark-names-plist
1316 :last-capture)))
1317 (when bookmark-name
1318 (with-demoted-errors
1319 (bookmark-set bookmark-name))))
1320 (move-marker org-capture-last-stored-marker (point)))))))
1322 (defun org-capture-narrow (beg end)
1323 "Narrow, unless configuration says not to narrow."
1324 (unless (org-capture-get :unnarrowed)
1325 (narrow-to-region beg end)
1326 (goto-char beg)))
1328 (defun org-capture-empty-lines-before (&optional n)
1329 "Set the correct number of empty lines before the insertion point.
1330 Point will be after the empty lines, so insertion can directly be done."
1331 (setq n (or n (org-capture-get :empty-lines-before)
1332 (org-capture-get :empty-lines) 0))
1333 (let ((pos (point)))
1334 (org-back-over-empty-lines)
1335 (delete-region (point) pos)
1336 (if (> n 0) (newline n))))
1338 (defun org-capture-empty-lines-after (&optional n)
1339 "Set the correct number of empty lines after the inserted string.
1340 Point will remain at the first line after the inserted text."
1341 (setq n (or n (org-capture-get :empty-lines-after)
1342 (org-capture-get :empty-lines) 0))
1343 (org-back-over-empty-lines)
1344 (while (looking-at "[ \t]*\n") (replace-match ""))
1345 (let ((pos (point)))
1346 (if (> n 0) (newline n))
1347 (goto-char pos)))
1349 (defvar org-clock-marker) ; Defined in org.el
1351 (defun org-capture-insert-template-here ()
1352 "Insert the capture template at point."
1353 (let* ((template (org-capture-get :template))
1354 (type (org-capture-get :type))
1355 beg end pp)
1356 (or (bolp) (newline))
1357 (setq beg (point))
1358 (cond
1359 ((and (eq type 'entry) (derived-mode-p 'org-mode))
1360 (org-capture-verify-tree (org-capture-get :template))
1361 (org-paste-subtree nil template t))
1362 ((and (memq type '(item checkitem))
1363 (derived-mode-p 'org-mode)
1364 (save-excursion (skip-chars-backward " \t\n")
1365 (setq pp (point))
1366 (org-in-item-p)))
1367 (goto-char pp)
1368 (org-insert-item)
1369 (skip-chars-backward " ")
1370 (skip-chars-backward "-+*0123456789).")
1371 (delete-region (point) (point-at-eol))
1372 (setq beg (point))
1373 (org-remove-indentation template)
1374 (insert template)
1375 (org-capture-empty-lines-after)
1376 (goto-char beg)
1377 (org-list-repair)
1378 (org-end-of-item)
1379 (setq end (point)))
1380 (t (insert template)))
1381 (setq end (point))
1382 (goto-char beg)
1383 (if (re-search-forward "%\\?" end t)
1384 (replace-match ""))))
1386 (defun org-capture-set-plist (entry)
1387 "Initialize the property list from the template definition."
1388 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1389 (org-capture-put :key (car entry) :description (nth 1 entry)
1390 :target (nth 3 entry))
1391 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1392 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1393 ;; The template may be empty or omitted for special types.
1394 ;; Here we insert the default templates for such cases.
1395 (cond
1396 ((eq type 'item) (setq txt "- %?"))
1397 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1398 ((eq type 'table-line) (setq txt "| %? |"))
1399 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1400 (org-capture-put :template txt :type type)))
1402 (defun org-capture-goto-target (&optional template-key)
1403 "Go to the target location of a capture template.
1404 The user is queried for the template."
1405 (interactive)
1406 (let ((entry (org-capture-select-template template-key)))
1407 (unless entry (error "No capture template selected"))
1408 (org-capture-set-plist entry)
1409 (org-capture-set-target-location)
1410 (org-pop-to-buffer-same-window (org-capture-get :buffer))
1411 (goto-char (org-capture-get :pos))))
1413 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1414 "Make an indirect buffer for a capture process.
1415 Use PREFIX as a prefix for the name of the indirect buffer."
1416 (setq buffer (or buffer (current-buffer)))
1417 (let ((n 1) (base (buffer-name buffer)) bname)
1418 (setq bname (concat prefix "-" base))
1419 (while (buffer-live-p (get-buffer bname))
1420 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1421 (condition-case nil
1422 (make-indirect-buffer buffer bname 'clone)
1423 (error
1424 (let ((buf (make-indirect-buffer buffer bname)))
1425 (with-current-buffer buf (org-mode))
1426 buf)))))
1428 (defun org-capture-verify-tree (tree)
1429 "Throw error if TREE is not a valid tree."
1430 (unless (org-kill-is-subtree-p tree)
1431 (error "Template is not a valid Org entry or tree")))
1433 (defun org-mks (table title &optional prompt specials)
1434 "Select a member of an alist with multiple keys.
1435 TABLE is the alist which should contain entries where the car is a string.
1436 There should be two types of entries.
1438 1. prefix descriptions like (\"a\" \"Description\")
1439 This indicates that `a' is a prefix key for multi-letter selection, and
1440 that there are entries following with keys like \"ab\", \"ax\"...
1442 2. Selectable members must have more than two elements, with the first
1443 being the string of keys that lead to selecting it, and the second a
1444 short description string of the item.
1446 The command will then make a temporary buffer listing all entries
1447 that can be selected with a single key, and all the single key
1448 prefixes. When you press the key for a single-letter entry, it is selected.
1449 When you press a prefix key, the commands (and maybe further prefixes)
1450 under this key will be shown and offered for selection.
1452 TITLE will be placed over the selection in the temporary buffer,
1453 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1454 also (\"key\" \"description\") entries. When one of these is selection,
1455 only the bare key is returned."
1456 (setq prompt (or prompt "Select: "))
1457 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1458 current prefix rtn re pressed buffer (inhibit-quit t))
1459 (save-window-excursion
1460 (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
1461 (setq orig-table table)
1462 (catch 'exit
1463 (while t
1464 (erase-buffer)
1465 (insert title "\n\n")
1466 (setq tbl table
1467 des-keys nil
1468 allowed-keys nil
1469 cursor-type nil)
1470 (setq prefix (if current (concat current " ") ""))
1471 (while tbl
1472 (cond
1473 ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
1474 ;; This is a description on this level
1475 (setq dkey (caar tbl) ddesc (cadar tbl))
1476 (pop tbl)
1477 (push dkey des-keys)
1478 (push dkey allowed-keys)
1479 (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
1480 ;; Skip keys which are below this prefix
1481 (setq re (concat "\\`" (regexp-quote dkey)))
1482 (let (case-fold-search)
1483 (while (and tbl (string-match re (caar tbl))) (pop tbl))))
1484 ((= 2 (length (car tbl)))
1485 ;; Not yet a usable description, skip it
1488 ;; usable entry on this level
1489 (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
1490 (push (caar tbl) allowed-keys)
1491 (pop tbl))))
1492 (when specials
1493 (insert "-------------------------------------------------------------------------------\n")
1494 (let ((sp specials))
1495 (while sp
1496 (insert (format "[%s] %s\n"
1497 (caar sp) (nth 1 (car sp))))
1498 (push (caar sp) allowed-keys)
1499 (pop sp))))
1500 (push "\C-g" allowed-keys)
1501 (goto-char (point-min))
1502 (if (not (pos-visible-in-window-p (point-max)))
1503 (org-fit-window-to-buffer))
1504 (message prompt)
1505 (setq pressed (char-to-string (read-char-exclusive)))
1506 (while (not (member pressed allowed-keys))
1507 (message "Invalid key `%s'" pressed) (sit-for 1)
1508 (message prompt)
1509 (setq pressed (char-to-string (read-char-exclusive))))
1510 (when (equal pressed "\C-g")
1511 (kill-buffer buffer)
1512 (user-error "Abort"))
1513 (when (and (not (assoc pressed table))
1514 (not (member pressed des-keys))
1515 (assoc pressed specials))
1516 (throw 'exit (setq rtn pressed)))
1517 (unless (member pressed des-keys)
1518 (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
1519 orig-table))))
1520 (setq current (concat current pressed))
1521 (setq table (mapcar
1522 (lambda (x)
1523 (if (and (> (length (car x)) 1)
1524 (equal (substring (car x) 0 1) pressed))
1525 (cons (substring (car x) 1) (cdr x))
1526 nil))
1527 table))
1528 (setq table (remove nil table)))))
1529 (when buffer (kill-buffer buffer))
1530 rtn))
1532 ;;; The template code
1533 (defun org-capture-select-template (&optional keys)
1534 "Select a capture template.
1535 Lisp programs can force the template by setting KEYS to a string."
1536 (let ((org-capture-templates
1537 (or (org-contextualize-keys
1538 org-capture-templates org-capture-templates-contexts)
1539 '(("t" "Task" entry (file+headline "" "Tasks")
1540 "* TODO %?\n %u\n %a")))))
1541 (if keys
1542 (or (assoc keys org-capture-templates)
1543 (error "No capture template referred to by \"%s\" keys" keys))
1544 (org-mks org-capture-templates
1545 "Select a capture template\n========================="
1546 "Template key: "
1547 '(("C" "Customize org-capture-templates")
1548 ("q" "Abort"))))))
1550 (defun org-capture-fill-template (&optional template initial annotation)
1551 "Fill a template and return the filled template as a string.
1552 The template may still contain \"%?\" for cursor positioning."
1553 (let* ((template (or template (org-capture-get :template)))
1554 (buffer (org-capture-get :buffer))
1555 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1556 (time (let* ((c (or (org-capture-get :default-time) (current-time)))
1557 (d (decode-time c)))
1558 (if (< (nth 2 d) org-extend-today-until)
1559 (encode-time 0 59 23 (1- (nth 3 d)) (nth 4 d) (nth 5 d))
1560 c)))
1561 (v-t (format-time-string (org-time-stamp-format nil) time))
1562 (v-T (format-time-string (org-time-stamp-format t) time))
1563 (v-u (format-time-string (org-time-stamp-format nil t) time))
1564 (v-U (format-time-string (org-time-stamp-format t t) time))
1565 (v-c (and kill-ring (current-kill 0)))
1566 (v-x (or (org-get-x-clipboard 'PRIMARY)
1567 (org-get-x-clipboard 'CLIPBOARD)
1568 (org-get-x-clipboard 'SECONDARY)))
1569 ;; `initial' and `annotation' might have been passed. But if
1570 ;; the property list has them, we prefer those values.
1571 (v-i (or (plist-get org-store-link-plist :initial)
1572 (and (stringp initial) (org-no-properties initial))
1573 (org-capture-get :initial)
1574 ""))
1575 (v-a
1576 (let ((a (or (plist-get org-store-link-plist :annotation)
1577 annotation
1578 (org-capture-get :annotation)
1579 "")))
1580 ;; Is the link empty? Then we do not want it...
1581 (if (equal a "[[]]") "" a)))
1582 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1583 (v-A (if (and v-a (string-match l-re v-a))
1584 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
1585 v-a))
1586 (v-l (if (and v-a (string-match l-re v-a))
1587 (replace-match "\\1" nil nil v-a)
1588 v-a))
1589 (v-n user-full-name)
1590 (v-k (and (marker-buffer org-clock-marker)
1591 (org-no-properties org-clock-heading)))
1592 (v-K (if (marker-buffer org-clock-marker)
1593 (org-make-link-string
1594 (buffer-file-name (marker-buffer org-clock-marker))
1595 org-clock-heading)))
1596 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1597 (v-F (or (org-capture-get :original-file) ""))
1598 (clipboards (delq nil
1599 (list v-i
1600 (org-get-x-clipboard 'PRIMARY)
1601 (org-get-x-clipboard 'CLIPBOARD)
1602 (org-get-x-clipboard 'SECONDARY)
1603 v-c))))
1605 (setq org-store-link-plist (plist-put org-store-link-plist :annotation v-a))
1606 (setq org-store-link-plist (plist-put org-store-link-plist :initial v-i))
1608 (unless template
1609 (setq template "")
1610 (message "no template") (ding)
1611 (sit-for 1))
1612 (save-window-excursion
1613 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1614 (erase-buffer)
1615 (setq buffer-file-name nil)
1616 (setq mark-active nil)
1617 (insert template)
1618 (goto-char (point-min))
1620 ;; %[] insert contents of a file.
1621 (save-excursion
1622 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1623 (let ((filename (expand-file-name (match-string 1)))
1624 (beg (copy-marker (match-beginning 0)))
1625 (end (copy-marker (match-end 0))))
1626 (unless (org-capture-escaped-%)
1627 (delete-region beg end)
1628 (set-marker beg nil)
1629 (set-marker end nil)
1630 (condition-case error
1631 (insert-file-contents filename)
1632 (error
1633 (insert (format "%%![couldn not insert %s: %s]"
1634 filename
1635 error))))))))
1637 ;; Mark %() embedded elisp for later evaluation.
1638 (org-capture-expand-embedded-elisp 'mark)
1640 ;; Expand non-interactive templates.
1641 (let ((regexp "%\\(:[-a-za-z]+\\|<\\([^>\n]+\\)>\\|[aAcfFikKlntTuUx]\\)"))
1642 (save-excursion
1643 (while (re-search-forward regexp nil t)
1644 ;; `org-capture-escaped-%' may modify buffer and cripple
1645 ;; match-data. Use markers instead. Ditto for other
1646 ;; templates.
1647 (let ((pos (copy-marker (match-beginning 0)))
1648 (end (copy-marker (match-end 0)))
1649 (value (match-string 1))
1650 (time-string (match-string 2)))
1651 (unless (org-capture-escaped-%)
1652 (delete-region pos end)
1653 (set-marker pos nil)
1654 (set-marker end nil)
1655 (let ((replacement
1656 (pcase (string-to-char value)
1657 (?< (format-time-string time-string))
1659 (or (plist-get org-store-link-plist (intern value))
1660 ""))
1661 (?i (let ((lead (buffer-substring-no-properties
1662 (line-beginning-position) (point))))
1663 (mapconcat #'identity
1664 (split-string v-i "\n")
1665 (concat "\n" lead))))
1666 (?a v-a)
1667 (?A v-A)
1668 (?c v-c)
1669 (?f v-f)
1670 (?F v-F)
1671 (?k v-k)
1672 (?K v-K)
1673 (?l v-l)
1674 (?n v-n)
1675 (?t v-t)
1676 (?T v-T)
1677 (?u v-u)
1678 (?U v-U)
1679 (?x v-x))))
1680 (insert
1681 (if (org-capture-inside-embedded-elisp-p)
1682 (replace-regexp-in-string "\"" "\\\\\"" replacement)
1683 replacement))))))))
1685 ;; Expand %() embedded Elisp. Limit to Sexp originally marked.
1686 (org-capture-expand-embedded-elisp)
1688 ;; Expand interactive templates. This is the last step so that
1689 ;; template is mostly expanded when prompting happens. Turn on
1690 ;; Org mode and set local variables. This is to support
1691 ;; completion in interactive prompts.
1692 (let ((org-inhibit-startup t)) (org-mode))
1693 (org-clone-local-variables buffer "\\`org-")
1694 (let (strings) ; Stores interactive answers.
1695 (save-excursion
1696 (let ((regexp "%\\^\\(?:{\\([^}]*\\)}\\)?\\([CgGLptTuU]\\)?"))
1697 (while (re-search-forward regexp nil t)
1698 (let* ((items (and (match-end 1)
1699 (save-match-data
1700 (split-string (match-string-no-properties 1)
1701 "|"))))
1702 (key (match-string 2))
1703 (beg (copy-marker (match-beginning 0)))
1704 (end (copy-marker (match-end 0)))
1705 (prompt (nth 0 items))
1706 (default (nth 1 items))
1707 (completions (nthcdr 2 items))
1708 (histvar
1709 (intern
1710 (concat "org-capture-template-prompt-history::"
1711 (or prompt "")))))
1712 (unless (org-capture-escaped-%)
1713 (delete-region beg end)
1714 (set-marker beg nil)
1715 (set-marker end nil)
1716 (pcase key
1717 ((or "G" "g")
1718 (let* ((org-last-tags-completion-table
1719 (org-global-tags-completion-table
1720 (cond ((equal key "G") (org-agenda-files))
1721 (file (list file))
1722 (t nil))))
1723 (org-add-colon-after-tag-completion t)
1724 (ins (mapconcat
1725 #'identity
1726 (org-split-string
1727 (completing-read
1728 (if prompt (concat prompt ": ") "Tags: ")
1729 'org-tags-completion-function nil nil nil
1730 'org-tags-history)
1731 "[^[:alnum:]_@#%]+")
1732 ":")))
1733 (when (org-string-nw-p ins)
1734 (unless (eq (char-before) ?:) (insert ":"))
1735 (insert ins)
1736 (unless (eq (char-after) ?:) (insert ":"))
1737 (and (org-at-heading-p)
1738 (let ((org-ignore-region t))
1739 (org-set-tags nil 'align))))))
1740 ("C"
1741 (cond
1742 ((= (length clipboards) 1) (insert (car clipboards)))
1743 ((> (length clipboards) 1)
1744 (insert (read-string "Clipboard/kill value: "
1745 (car clipboards)
1746 '(clipboards . 1)
1747 (car clipboards))))))
1748 ("L"
1749 (cond ((= (length clipboards) 1)
1750 (org-insert-link 0 (car clipboards)))
1751 ((> (length clipboards) 1)
1752 (org-insert-link
1754 (read-string "Clipboard/kill value: "
1755 (car clipboards)
1756 '(clipboards . 1)
1757 (car clipboards))))))
1758 ("p" (org-set-property prompt nil))
1759 ((guard key)
1760 ;; These are the date/time related ones.
1761 (let* ((upcase? (equal (upcase key) key))
1762 (org-time-was-given upcase?)
1763 (org-end-time-was-given)
1764 (time (org-read-date upcase? t nil prompt)))
1765 (org-insert-time-stamp
1766 time org-time-was-given
1767 (member key '("u" "U"))
1768 nil nil (list org-end-time-was-given))))
1770 (push (org-completing-read
1771 (concat (or prompt "Enter string")
1772 (and default (format " [%s]" default))
1773 ": ")
1774 completions nil nil nil histvar default)
1775 strings)
1776 (insert (car strings)))))))))
1778 ;; Replace %n escapes with nth %^{...} string.
1779 (setq strings (nreverse strings))
1780 (save-excursion
1781 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
1782 (unless (org-capture-escaped-%)
1783 (replace-match
1784 (nth (1- (string-to-number (match-string 1))) strings)
1785 nil t)))))
1787 ;; Make sure there are no empty lines before the text, and that
1788 ;; it ends with a newline character.
1789 (skip-chars-forward " \t\n")
1790 (delete-region (point-min) (line-beginning-position))
1791 (goto-char (point-max))
1792 (skip-chars-backward " \t\n")
1793 (delete-region (point) (point-max))
1794 (insert "\n")
1796 ;; Return the expanded template and kill the capture buffer.
1797 (untabify (point-min) (point-max))
1798 (set-buffer-modified-p nil)
1799 (prog1 (buffer-substring-no-properties (point-min) (point-max))
1800 (kill-buffer (current-buffer))))))
1802 (defun org-capture-escaped-% ()
1803 "Non-nil if % was escaped.
1804 If yes, unescape it now. Assume match-data contains the
1805 placeholder to check."
1806 (save-excursion
1807 (goto-char (match-beginning 0))
1808 (let ((n (abs (skip-chars-backward "\\\\"))))
1809 (delete-char (/ (1+ n) 2))
1810 (= (% n 2) 1))))
1812 (defun org-capture-expand-embedded-elisp (&optional mark)
1813 "Evaluate embedded elisp %(sexp) and replace with the result.
1814 When optional MARK argument is non-nil, mark Sexp with a text
1815 property (`org-embedded-elisp') for later evaluation. Only
1816 marked Sexp are evaluated when this argument is nil."
1817 (save-excursion
1818 (goto-char (point-min))
1819 (while (re-search-forward "%(" nil t)
1820 (cond
1821 ((get-text-property (match-beginning 0) 'org-embedded-elisp)
1822 (goto-char (match-beginning 0))
1823 (let ((template-start (point)))
1824 (forward-char 1)
1825 (let* ((sexp (read (current-buffer)))
1826 (result (org-eval
1827 (org-capture--expand-keyword-in-embedded-elisp
1828 sexp))))
1829 (delete-region template-start (point))
1830 (cond
1831 ((not result) nil)
1832 ((stringp result) (insert result))
1833 (t (error
1834 "Capture template sexp `%s' must evaluate to string or nil"
1835 sexp))))))
1836 ((not mark) nil)
1837 ;; Only mark valid and non-escaped sexp.
1838 ((org-capture-escaped-%) nil)
1840 (let ((end (with-syntax-table emacs-lisp-mode-syntax-table
1841 (ignore-errors (scan-sexps (1- (point)) 1)))))
1842 (when end
1843 (put-text-property (- (point) 2) end 'org-embedded-elisp t))))))))
1845 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1846 "Recursively replace capture link keywords in ATTR sexp.
1847 Such keywords are prefixed with \"%:\". See
1848 `org-capture-template' for more information."
1849 (cond ((consp attr)
1850 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr))
1851 ((symbolp attr)
1852 (let* ((attr-symbol (symbol-name attr))
1853 (key (and (string-match "%\\(:.*\\)" attr-symbol)
1854 (intern (match-string 1 attr-symbol)))))
1855 (or (plist-get org-store-link-plist key)
1856 attr)))
1857 (t attr)))
1859 (defun org-capture-inside-embedded-elisp-p ()
1860 "Non-nil if point is inside of embedded elisp %(sexp).
1861 Assume sexps have been marked with
1862 `org-capture-expand-embedded-elisp' beforehand."
1863 (get-text-property (point) 'org-embedded-elisp))
1865 ;;;###autoload
1866 (defun org-capture-import-remember-templates ()
1867 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1868 (interactive)
1869 (when (and (yes-or-no-p
1870 "Import old remember templates into org-capture-templates? ")
1871 (yes-or-no-p
1872 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1873 (require 'org-remember)
1874 (setq org-capture-templates
1875 (mapcar
1876 (lambda (entry)
1877 (let ((desc (car entry))
1878 (key (char-to-string (nth 1 entry)))
1879 (template (nth 2 entry))
1880 (file (or (nth 3 entry) org-default-notes-file))
1881 (position (or (nth 4 entry) org-remember-default-headline))
1882 (type 'entry)
1883 (prepend org-reverse-note-order)
1884 immediate target jump-to-captured)
1885 (cond
1886 ((member position '(top bottom))
1887 (setq target (list 'file file)
1888 prepend (eq position 'top)))
1889 ((eq position 'date-tree)
1890 (setq target (list 'file+datetree file)
1891 prepend nil))
1892 (t (setq target (list 'file+headline file position))))
1894 (when (string-match "%!" template)
1895 (setq template (replace-match "" t t template)
1896 immediate t))
1898 (when (string-match "%&" template)
1899 (setq jump-to-captured t))
1901 (append (list key desc type target template)
1902 (if prepend '(:prepend t))
1903 (if immediate '(:immediate-finish t))
1904 (if jump-to-captured '(:jump-to-captured t)))))
1906 org-remember-templates))))
1908 (provide 'org-capture)
1910 ;;; org-capture.el ends here