1 ;;; org-capture.el --- Fast note taking in Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
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?
53 (declare-function org-at-encrypted-entry-p
"org-crypt" ())
54 (declare-function org-datetree-find-date-create
"org-datetree"
55 (date &optional keep-restriction
))
56 (declare-function org-decrypt-entry
"org-crypt" ())
57 (declare-function org-encrypt-entry
"org-crypt" ())
58 (declare-function org-table-analyze
"org-table" ())
59 (declare-function org-table-current-dline
"org-table" ())
60 (declare-function org-table-goto-line
"org-table" (N))
62 (defvar org-end-time-was-given
)
63 (defvar org-remember-default-headline
)
64 (defvar org-remember-templates
)
65 (defvar org-table-hlines
)
66 (defvar org-table-current-begin-pos
)
67 (defvar dired-buffers
)
69 (defvar org-capture-clock-was-started nil
70 "Internal flag, noting if the clock was started.")
72 (defvar org-capture-last-stored-marker
(make-marker)
73 "Marker pointing to the entry most recently stored with `org-capture'.")
75 ;; The following variable is scoped dynamically by org-protocol
76 ;; to indicate that the link properties have already been stored
77 (defvar org-capture-link-is-already-stored nil
)
79 (defvar org-capture-is-refiling nil
80 "Non-nil when capture process is refiling an entry.")
82 (defgroup org-capture nil
83 "Options concerning capturing new entries."
87 (defcustom org-capture-templates nil
88 "Templates for the creation of new entries.
90 Each entry is a list with the following items:
92 keys The keys that will select the template, as a string, characters
93 only, for example \"a\" for a template to be selected with a
94 single key, or \"bt\" for selection with two keys. When using
95 several keys, keys using the same prefix key must be together
96 in the list and preceded by a 2-element entry explaining the
97 prefix key, for example
99 (\"b\" \"Templates for marking stuff to buy\")
101 The \"C\" key is used by default for quick access to the
102 customization of the template variable. But if you want to use
103 that key for a template, you can.
105 description A short string describing the template, will be shown during
108 type The type of entry. Valid types are:
109 entry an Org node, with a headline. Will be filed
110 as the child of the target entry or as a
112 item a plain list item, will be placed in the
113 first plain list at the target
115 checkitem a checkbox item. This differs from the
116 plain list item only is so far as it uses a
117 different default template.
118 table-line a new line in the first table at target location.
119 plain text to be inserted as it is.
121 target Specification of where the captured item should be placed.
122 In Org files, targets usually define a node. Entries will
123 become children of this node, other types will be added to the
124 table or list in the body of this node.
126 Most target specifications contain a file name. If that file
127 name is the empty string, it defaults to `org-default-notes-file'.
128 A file can also be given as a variable or as a function called
129 with no argument. When an absolute path is not specified for a
130 target, it is taken as relative to `org-directory'.
134 (file \"path/to/file\")
135 Text will be placed at the beginning or end of that file
137 (id \"id of existing Org entry\")
138 File as child of this entry, or in the body of the entry
140 (file+headline \"path/to/file\" \"node headline\")
141 Fast configuration if the target heading is unique in the file
143 (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
144 For non-unique headings, the full path is safer
146 (file+regexp \"path/to/file\" \"regexp to find location\")
147 File to the entry matching regexp
149 (file+datetree \"path/to/file\")
150 Will create a heading in a date tree for today's date
152 (file+datetree+prompt \"path/to/file\")
153 Will create a heading in a date tree, prompts for date
155 (file+weektree \"path/to/file\")
156 Will create a heading in a week tree for today's date
158 (file+weektree+prompt \"path/to/file\")
159 Will create a heading in a week tree, prompts for date
161 (file+function \"path/to/file\" function-finding-location)
162 A function to find the right location in the file
165 File to the entry that is currently being clocked
167 (function function-finding-location)
168 Most general way: write your own function which both visits
169 the file and moves point to the right location
171 template The template for creating the capture item. If you leave this
172 empty, an appropriate default template will be used. See below
173 for more details. Instead of a string, this may also be one of
175 (file \"/path/to/template-file\")
176 (function function-returning-the-template)
178 in order to get a template from a file, or dynamically
181 The rest of the entry is a property list of additional options. Recognized
184 :prepend Normally newly captured information will be appended at
185 the target location (last child, last table line,
186 last list item...). Setting this property will
189 :immediate-finish When set, do not offer to edit the information, just
190 file it away immediately. This makes sense if the
191 template only needs information that can be added
194 :jump-to-captured When set, jump to the captured entry when finished.
196 :empty-lines Set this to the number of lines the should be inserted
197 before and after the new item. Default 0, only common
200 :empty-lines-before Set this to the number of lines the should be inserted
201 before the new item. Overrides :empty-lines for the
202 number lines inserted before.
204 :empty-lines-after Set this to the number of lines the should be inserted
205 after the new item. Overrides :empty-lines for the
206 number of lines inserted after.
208 :clock-in Start the clock in this item.
210 :clock-keep Keep the clock running when filing the captured entry.
212 :clock-resume Start the interrupted clock when finishing the capture.
213 Note that :clock-keep has precedence over :clock-resume.
214 When setting both to t, the current clock will run and
215 the previous one will not be resumed.
217 :unnarrowed Do not narrow the target buffer, simply show the
218 full buffer. Default is to narrow it so that you
219 only see the new stuff.
221 :table-line-pos Specification of the location in the table where the
222 new line should be inserted. It should be a string like
223 \"II-3\", meaning that the new line should become the
224 third line before the second horizontal separator line.
226 :kill-buffer If the target file was not yet visited by a buffer when
227 capture was invoked, kill the buffer again after capture
230 The template defines the text to be inserted. Often this is an
231 Org mode entry (so the first line should start with a star) that
232 will be filed as a child of the target headline. It can also be
233 freely formatted text. Furthermore, the following %-escapes will
234 be replaced with content and expanded:
236 %[pathname] Insert the contents of the file given by
237 `pathname'. These placeholders are expanded at the very
238 beginning of the process so they can be used to extend the
240 %(sexp) Evaluate elisp `(sexp)' and replace it with the results.
241 Only placeholders pre-existing within the template, or
242 introduced with %[pathname] are expanded this way. Since this
243 happens after expanding non-interactive %-escapes, those can
244 be used to fill the expression.
245 %<...> The result of format-time-string on the ... format specification.
246 %t Time stamp, date only.
247 %T Time stamp with date and time.
248 %u, %U Like the above, but inactive time stamps.
249 %i Initial content, copied from the active region. If %i is
250 indented, the entire inserted text will be indented as well.
251 %a Annotation, normally the link created with `org-store-link'.
252 %A Like %a, but prompt for the description part.
253 %l Like %a, but only insert the literal link.
254 %c Current kill ring head.
255 %x Content of the X clipboard.
256 %k Title of currently clocked task.
257 %K Link to currently clocked task.
258 %n User name (taken from the variable `user-full-name').
259 %f File visited by current buffer when org-capture was called.
260 %F Full path of the file or directory visited by current buffer.
261 %:keyword Specific information for certain link types, see below.
262 %^g Prompt for tags, with completion on tags in target file.
263 %^G Prompt for tags, with completion on all tags in all agenda files.
264 %^t Like %t, but prompt for date. Similarly %^T, %^u, %^U.
265 You may define a prompt like: %^{Please specify birthday}t
266 %^C Interactive selection of which kill or clip to use.
267 %^L Like %^C, but insert as link.
268 %^{prop}p Prompt the user for a value for property `prop'.
269 %^{prompt} Prompt the user for a string and replace this sequence with it.
270 A default value and a completion table ca be specified like this:
271 %^{prompt|default|completion2|completion3|...}.
272 %? After completing the template, position cursor here.
273 %\\1 ... %\\N Insert the text entered at the nth %^{prompt}, where N
274 is a number, starting from 1.
276 Apart from these general escapes, you can access information specific to
277 the link type that is created. For example, calling `org-capture' in emails
278 or in Gnus will record the author and the subject of the message, which you
279 can access with \"%:from\" and \"%:subject\", respectively. Here is a
280 complete list of what is recorded for each link type.
282 Link type | Available information
283 ------------------------+------------------------------------------------------
284 bbdb | %:type %:name %:company
285 vm, wl, mh, mew, rmail, | %:type %:subject %:message-id
286 gnus | %:from %:fromname %:fromaddress
287 | %:to %:toname %:toaddress
288 | %:fromto (either \"to NAME\" or \"from NAME\")
289 | %:date %:date-timestamp (as active timestamp)
290 | %:date-timestamp-inactive (as inactive timestamp)
291 gnus | %:group, for messages also all email fields
292 eww, w3, w3m | %:type %:url
293 info | %:type %:file %:node
294 calendar | %:type %:date
296 When you need to insert a literal percent sign in the template,
297 you can escape ambiguous cases with a backward slash, e.g., \\%i."
301 (let ((file-variants '(choice :tag
"Filename "
302 (file :tag
"Literal")
303 (function :tag
"Function")
304 (variable :tag
"Variable")
305 (sexp :tag
"Form"))))
307 (choice :value
("" "" entry
(file "~/org/notes.org") "")
308 (list :tag
"Multikey description"
309 (string :tag
"Keys ")
310 (string :tag
"Description"))
311 (list :tag
"Template entry"
312 (string :tag
"Keys ")
313 (string :tag
"Description ")
314 (choice :tag
"Capture Type " :value entry
315 (const :tag
"Org entry" entry
)
316 (const :tag
"Plain list item" item
)
317 (const :tag
"Checkbox item" checkitem
)
318 (const :tag
"Plain text" plain
)
319 (const :tag
"Table line" table-line
))
320 (choice :tag
"Target location"
322 (const :format
"" file
)
325 (const :format
"" id
)
327 (list :tag
"File & Headline"
328 (const :format
"" file
+headline
)
330 (string :tag
" Headline"))
331 (list :tag
"File & Outline path"
332 (const :format
"" file
+olp
)
334 (repeat :tag
"Outline path" :inline t
335 (string :tag
"Headline")))
336 (list :tag
"File & Regexp"
337 (const :format
"" file
+regexp
)
339 (regexp :tag
" Regexp"))
340 (list :tag
"File & Date tree"
341 (const :format
"" file
+datetree
)
343 (list :tag
"File & Date tree, prompt for date"
344 (const :format
"" file
+datetree
+prompt
)
346 (list :tag
"File & Week tree"
347 (const :format
"" file
+weektree
)
349 (list :tag
"File & Week tree, prompt for date"
350 (const :format
"" file
+weektree
+prompt
)
352 (list :tag
"File & function"
353 (const :format
"" file
+function
)
355 (sexp :tag
" Function"))
356 (list :tag
"Current clocking task"
357 (const :format
"" clock
))
358 (list :tag
"Function"
359 (const :format
"" function
)
360 (sexp :tag
" Function")))
361 (choice :tag
"Template "
364 (const :format
"" file
)
365 (file :tag
"Template file"))
366 (list :tag
"Function"
367 (const :format
"" function
)
368 (function :tag
"Template function")))
370 ;; Give the most common options as checkboxes
371 :options
(((const :format
"%v " :prepend
) (const t
))
372 ((const :format
"%v " :immediate-finish
) (const t
))
373 ((const :format
"%v " :jump-to-captured
) (const t
))
374 ((const :format
"%v " :empty-lines
) (const 1))
375 ((const :format
"%v " :empty-lines-before
) (const 1))
376 ((const :format
"%v " :empty-lines-after
) (const 1))
377 ((const :format
"%v " :clock-in
) (const t
))
378 ((const :format
"%v " :clock-keep
) (const t
))
379 ((const :format
"%v " :clock-resume
) (const t
))
380 ((const :format
"%v " :unnarrowed
) (const t
))
381 ((const :format
"%v " :table-line-pos
) (const t
))
382 ((const :format
"%v " :kill-buffer
) (const t
)))))))))
384 (defcustom org-capture-before-finalize-hook nil
385 "Hook that is run right before a capture process is finalized.
386 The capture buffer is still current when this hook runs and it is
387 widened to the entire buffer."
392 (defcustom org-capture-after-finalize-hook nil
393 "Hook that is run right after a capture process is finalized.
394 Suitable for window cleanup."
399 (defcustom org-capture-prepare-finalize-hook nil
400 "Hook that is run before the finalization starts.
401 The capture buffer is current and still narrowed."
406 (defcustom org-capture-bookmark t
407 "When non-nil, add a bookmark pointing at the last stored
408 position when capturing."
413 ;;; The property list for keeping information about the capture process
415 (defvar org-capture-plist nil
416 "Plist for the current capture process, global, to avoid having to pass it.")
418 (defvar org-capture-current-plist nil
419 "Local variable holding the plist in a capture buffer.
420 This is used to store the plist for use when finishing a capture process
421 because another such process might have changed the global variable by then.
423 Each time a new capture buffer has been set up, the global `org-capture-plist'
424 is copied to this variable, which is local in the indirect buffer.")
426 (defvar org-capture-clock-keep nil
427 "Local variable to store the value of the :clock-keep parameter.
428 This is needed in case org-capture-finalize is called interactively.")
430 (defun org-capture-put (&rest stuff
)
431 "Add properties to the capture property list `org-capture-plist'."
433 (setq org-capture-plist
(plist-put org-capture-plist
434 (pop stuff
) (pop stuff
)))))
435 (defun org-capture-get (prop &optional local
)
436 "Get properties from the capture property list `org-capture-plist'.
437 When LOCAL is set, use the local variable `org-capture-current-plist',
438 this is necessary after initialization of the capture process,
439 to avoid conflicts with other active capture processes."
440 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
442 (defun org-capture-member (prop &optional local
)
443 "Is PROP a property in `org-capture-plist'.
444 When LOCAL is set, use the local variable `org-capture-current-plist',
445 this is necessary after initialization of the capture process,
446 to avoid conflicts with other active capture processes."
447 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
451 (defvar org-capture-mode-map
(make-sparse-keymap)
452 "Keymap for `org-capture-mode', a minor mode.
453 Use this map to set additional keybindings for when Org mode is used
454 for a capture buffer.")
456 (defvar org-capture-mode-hook nil
457 "Hook for the minor `org-capture-mode'.")
459 (define-minor-mode org-capture-mode
460 "Minor mode for special key bindings in a capture buffer.
462 Turning on this mode runs the normal hook `org-capture-mode-hook'."
463 nil
" Rem" org-capture-mode-map
466 (substitute-command-keys
467 "\\<org-capture-mode-map>Capture buffer. Finish \
468 `\\[org-capture-finalize]', refile `\\[org-capture-refile]', \
469 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
)
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
)))
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,
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.)"
520 :type
'(repeat (list :tag
"Rule"
521 (string :tag
" Capture key")
522 (string :tag
"Replace by template")
523 (repeat :tag
"Available when"
525 (cons :tag
"Condition"
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
))
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
539 `\\[org-agenda-capture]'."
545 (defun org-capture (&optional goto keys
)
547 \\<org-capture-mode-map>
548 This will let you select a template from `org-capture-templates', and
549 then file the newly captured information. The text is immediately
550 inserted at the target location, and an indirect buffer is shown where
551 you can edit it. Pressing `\\[org-capture-finalize]' brings you back to the \
553 state of Emacs, so that you can continue your work.
555 When called interactively with a `\\[universal-argument]' prefix argument \
557 capture anything, just go to the file/headline where the selected
558 template stores its notes.
560 With a `\\[universal-argument] \\[universal-argument]' prefix argument, go to \
561 the last note stored.
563 When called with a `C-0' (zero) prefix, insert a template at point.
565 ELisp programs can set KEYS to a string associated with a template
566 in `org-capture-templates'. In this case, interactive selection
569 If `org-capture-use-agenda-date' is non-nil, capturing from the
570 agenda will use the date at point as the default date. Then, a
571 `C-1' prefix will tell the capture process to use the HH:MM time
572 of the day at point (if any) or the current HH:MM time."
574 (when (and org-capture-use-agenda-date
575 (eq major-mode
'org-agenda-mode
))
576 (setq org-overriding-default-time
577 (org-get-cursor-date (equal goto
1))))
579 ((equal goto
'(4)) (org-capture-goto-target))
580 ((equal goto
'(16)) (org-capture-goto-last-stored))
582 ;; FIXME: Are these needed?
583 (let* ((orig-buf (current-buffer))
584 (annotation (if (and (boundp 'org-capture-link-is-already-stored
)
585 org-capture-link-is-already-stored
)
586 (plist-get org-store-link-plist
:annotation
)
587 (ignore-errors (org-store-link nil
))))
588 (entry (or org-capture-entry
(org-capture-select-template keys
)))
590 (setq initial
(or org-capture-initial
591 (and (org-region-active-p)
592 (buffer-substring (point) (mark)))))
593 (when (stringp initial
)
594 (remove-text-properties 0 (length initial
) '(read-only t
) initial
))
595 (when (stringp annotation
)
596 (remove-text-properties 0 (length annotation
)
597 '(read-only t
) annotation
))
600 (customize-variable 'org-capture-templates
))
602 (user-error "Abort"))
604 (org-capture-set-plist entry
)
605 (org-capture-get-template)
606 (org-capture-put :original-buffer orig-buf
607 :original-file
(or (buffer-file-name orig-buf
)
608 (and (featurep 'dired
)
611 :original-file-nondirectory
612 (and (buffer-file-name orig-buf
)
613 (file-name-nondirectory
614 (buffer-file-name orig-buf
)))
615 :annotation annotation
617 :return-to-wconf
(current-window-configuration)
619 (or org-overriding-default-time
621 (org-capture-set-target-location)
622 (condition-case error
623 (org-capture-put :template
(org-capture-fill-template))
625 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
626 (error "Capture abort: %s" error
)))
628 (setq org-capture-clock-keep
(org-capture-get :clock-keep
))
631 (org-capture-insert-template-here)
632 (condition-case error
633 (org-capture-place-template
634 (eq (car (org-capture-get :target
)) 'function
))
636 (if (and (buffer-base-buffer (current-buffer))
637 (string-prefix-p "CAPTURE-" (buffer-name)))
638 (kill-buffer (current-buffer)))
639 (set-window-configuration (org-capture-get :return-to-wconf
))
640 (error "Capture template `%s': %s"
641 (org-capture-get :key
)
643 (if (and (derived-mode-p 'org-mode
)
644 (org-capture-get :clock-in
))
647 (if (org-clock-is-active)
648 (org-capture-put :interrupted-clock
649 (copy-marker org-clock-marker
)))
651 (setq-local org-capture-clock-was-started t
))
653 "Could not start the clock in this capture buffer")))
654 (if (org-capture-get :immediate-finish
)
655 (org-capture-finalize)))))))))
657 (defun org-capture-get-template ()
658 "Get the template from a file or a function if necessary."
659 (let ((txt (org-capture-get :template
)) file
)
661 ((and (listp txt
) (eq (car txt
) 'file
))
663 (setq file
(expand-file-name (nth 1 txt
) org-directory
)))
664 (setq txt
(org-file-contents file
))
665 (setq txt
(format "* Template file %s not found" (nth 1 txt
)))))
666 ((and (listp txt
) (eq (car txt
) 'function
))
667 (if (fboundp (nth 1 txt
))
668 (setq txt
(funcall (nth 1 txt
)))
669 (setq txt
(format "* Template function %s not found" (nth 1 txt
)))))
670 ((not txt
) (setq txt
""))
672 (t (setq txt
"* Invalid capture template")))
673 (org-capture-put :template txt
)))
675 (defun org-capture-finalize (&optional stay-with-capture
)
676 "Finalize the capture process.
677 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
678 captured item after finalizing."
680 (when (org-capture-get :jump-to-captured
)
681 (setq stay-with-capture t
))
682 (unless (and org-capture-mode
683 (buffer-base-buffer (current-buffer)))
684 (error "This does not seem to be a capture buffer for Org mode"))
686 (run-hooks 'org-capture-prepare-finalize-hook
)
688 ;; Did we start the clock in this capture buffer?
689 (when (and org-capture-clock-was-started
690 org-clock-marker
(marker-buffer org-clock-marker
)
691 (equal (marker-buffer org-clock-marker
) (buffer-base-buffer))
692 (> org-clock-marker
(point-min))
693 (< org-clock-marker
(point-max)))
694 ;; Looks like the clock we started is still running. Clock out.
695 (when (not org-capture-clock-keep
) (let (org-log-note-clock-out) (org-clock-out)))
696 (when (and (not org-capture-clock-keep
)
697 (org-capture-get :clock-resume
'local
)
698 (markerp (org-capture-get :interrupted-clock
'local
))
699 (buffer-live-p (marker-buffer
700 (org-capture-get :interrupted-clock
'local
))))
701 (let ((clock-in-task (org-capture-get :interrupted-clock
'local
)))
702 (org-with-point-at clock-in-task
704 (message "Interrupted clock has been resumed")))
706 (let ((beg (point-min))
709 ;; Store the size of the capture buffer
710 (org-capture-put :captured-entry-size
(- (point-max) (point-min)))
712 ;; Store the insertion point in the target buffer
713 (org-capture-put :insertion-point
(point))
716 (let ((m1 (org-capture-get :begin-marker
'local
))
717 (m2 (org-capture-get :end-marker
'local
)))
718 (if (and m1 m2
(= m1 beg
) (= m2 end
))
720 (setq m2
(if (cdr (assq 'heading org-blank-before-new-entry
))
722 m2
(if (< (point-max) m2
) (point-max) m2
))
723 (setq abort-note
'clean
)
725 (setq abort-note
'dirty
)))
727 ;; Make sure that the empty lines after are correct
728 (when (and (> (point-max) end
) ; indeed, the buffer was still narrowed
729 (member (org-capture-get :type
'local
)
730 '(entry item checkitem plain
)))
733 (or (bolp) (newline))
734 (org-capture-empty-lines-after
735 (or (org-capture-get :empty-lines-after
'local
)
736 (org-capture-get :empty-lines
'local
) 0))))
737 ;; Postprocessing: Update Statistics cookies, do the sorting
738 (when (derived-mode-p 'org-mode
)
740 (when (ignore-errors (org-back-to-heading))
741 (org-update-parent-todo-statistics)
742 (org-update-checkbox-count)))
743 ;; FIXME Here we should do the sorting
744 ;; If we have added a table line, maybe recompute?
745 (when (and (eq (org-capture-get :type
'local
) 'table-line
)
747 (if (org-table-get-stored-formulas)
748 (org-table-recalculate 'all
) ;; FIXME: Should we iterate???
750 ;; Store this place as the last one where we stored something
751 ;; Do the marking in the base buffer, so that it makes sense after
752 ;; the indirect buffer has been killed.
753 (org-capture-store-last-position)
756 (run-hooks 'org-capture-before-finalize-hook
))
758 (when (org-capture-get :decrypted
)
760 (goto-char (org-capture-get :decrypted
))
761 (org-encrypt-entry)))
763 ;; Kill the indirect buffer
765 (let ((return-wconf (org-capture-get :return-to-wconf
'local
))
766 (new-buffer (org-capture-get :new-buffer
'local
))
767 (kill-buffer (org-capture-get :kill-buffer
'local
))
768 (base-buffer (buffer-base-buffer (current-buffer))))
770 ;; Kill the indirect buffer
771 (kill-buffer (current-buffer))
773 ;; Narrow back the target buffer to its previous state
774 (with-current-buffer (org-capture-get :buffer
)
775 (let ((reg (org-capture-get :initial-target-region
))
776 (pos (org-capture-get :initial-target-position
))
777 (ipt (org-capture-get :insertion-point
))
778 (size (org-capture-get :captured-entry-size
)))
781 (cond ((< ipt
(car reg
))
782 ;; insertion point is before the narrowed region
783 (narrow-to-region (+ size
(car reg
)) (+ size
(cdr reg
))))
785 ;; insertion point is after the narrowed region
786 (narrow-to-region (car reg
) (cdr reg
)))
788 ;; insertion point is within the narrowed region
789 (narrow-to-region (car reg
) (+ size
(cdr reg
)))))
790 ;; now place back the point at its original position
791 (if (< ipt
(car reg
))
792 (goto-char (+ size pos
))
793 (goto-char (if (< ipt pos
) (+ size pos
) pos
))))))
795 ;; Kill the target buffer if that is desired
796 (when (and base-buffer new-buffer kill-buffer
)
797 (with-current-buffer base-buffer
(save-buffer))
798 (kill-buffer base-buffer
))
800 ;; Restore the window configuration before capture
801 (set-window-configuration return-wconf
))
803 (run-hooks 'org-capture-after-finalize-hook
)
809 (message "Capture process aborted and target buffer cleaned up"))
811 (error "Capture process aborted, but target buffer could not be \
812 cleaned up correctly"))))
814 (org-capture-goto-last-stored)))
815 ;; Return if we did store something
818 (defun org-capture-refile ()
819 "Finalize the current capture and then refile the entry.
820 Refiling is done from the base buffer, because the indirect buffer is then
821 already gone. Any prefix argument will be passed to the refile command."
823 (unless (eq (org-capture-get :type
'local
) 'entry
)
825 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
827 (base (buffer-base-buffer (current-buffer)))
828 (org-capture-is-refiling t
)
829 (kill-buffer (org-capture-get :kill-buffer
'local
)))
830 (org-capture-put :kill-buffer nil
)
831 (org-capture-finalize)
832 (save-window-excursion
833 (with-current-buffer (or base
(current-buffer))
834 (org-with-wide-buffer
836 (call-interactively 'org-refile
))))
837 (when kill-buffer
(kill-buffer base
))))
839 (defun org-capture-kill ()
840 "Abort the current capture process."
842 ;; FIXME: This does not do the right thing, we need to remove the
843 ;; new stuff by hand it is easy: undo, then kill the buffer
844 (let ((org-note-abort t
)
845 (org-capture-before-finalize-hook nil
))
846 (org-capture-finalize)))
848 (defun org-capture-goto-last-stored ()
849 "Go to the location where the last capture note was stored."
851 (org-goto-marker-or-bmk org-capture-last-stored-marker
852 (plist-get org-bookmark-names-plist
854 (message "This is the last note stored by a capture process"))
856 ;;; Supporting functions for handling the process
858 (defun org-capture-put-target-region-and-position ()
859 "Store the initial region with `org-capture-put'."
861 :initial-target-region
862 ;; Check if the buffer is currently narrowed
863 (when (org-buffer-narrowed-p)
864 (cons (point-min) (point-max))))
865 ;; store the current point
866 (org-capture-put :initial-target-position
(point)))
868 (defvar org-time-was-given
) ; dynamically scoped parameter
869 (defun org-capture-set-target-location (&optional target
)
870 "Find TARGET buffer and position.
871 Store them in the capture property list."
872 (let ((target-entry-p t
))
874 (pcase (or target
(org-capture-get :target
))
876 (set-buffer (org-capture-target-buffer path
))
877 (org-capture-put-target-region-and-position)
879 (setq target-entry-p nil
))
881 (pcase (org-id-find id
)
882 (`(,path .
,position
)
883 (set-buffer (org-capture-target-buffer path
))
885 (org-capture-put-target-region-and-position)
886 (goto-char position
))
887 (_ (error "Cannot find target ID \"%s\"" id
))))
888 (`(file+headline
,path
,headline
)
889 (set-buffer (org-capture-target-buffer path
))
890 (unless (derived-mode-p 'org-mode
)
891 (error "Target buffer \"%s\" for file+headline not in Org mode"
893 (org-capture-put-target-region-and-position)
895 (goto-char (point-min))
896 (if (re-search-forward (format org-complex-heading-regexp-format
897 (regexp-quote headline
))
899 (goto-char (line-beginning-position))
900 (goto-char (point-max))
901 (or (bolp) (insert "\n"))
902 (insert "* " headline
"\n")
903 (beginning-of-line 0)))
904 (`(file+olp
,path .
,outline-path
)
905 (let ((m (org-find-olp (cons (org-capture-expand-file path
)
907 (set-buffer (marker-buffer m
))
908 (org-capture-put-target-region-and-position)
912 (`(file+regexp
,path
,regexp
)
913 (set-buffer (org-capture-target-buffer path
))
914 (org-capture-put-target-region-and-position)
916 (goto-char (point-min))
917 (if (not (re-search-forward regexp nil t
))
918 (error "No match for target regexp in file %s" path
)
919 (goto-char (if (org-capture-get :prepend
)
922 (org-capture-put :exact-position
(point))
924 (and (derived-mode-p 'org-mode
) (org-at-heading-p)))))
925 (`(,(and type
(or `file
+datetree
926 `file
+datetree
+prompt
928 `file
+weektree
+prompt
))
930 (set-buffer (org-capture-target-buffer path
))
931 (unless (derived-mode-p 'org-mode
)
932 (error "Target buffer \"%s\" for %s should be in Org mode"
935 (require 'org-datetree
)
936 (org-capture-put-target-region-and-position)
938 ;; Make a date/week tree entry, with the current date (or
939 ;; yesterday, if we are extending dates for a couple of hours)
941 (if (memq type
'(file+weektree file
+weektree
+prompt
))
942 #'org-datetree-find-iso-week-create
943 #'org-datetree-find-date-create
)
944 (calendar-gregorian-from-absolute
946 (org-overriding-default-time
947 ;; Use the overriding default time.
948 (time-to-days org-overriding-default-time
))
949 ((memq type
'(file+datetree
+prompt file
+weektree
+prompt
))
951 (let ((prompt-time (org-read-date
952 nil t nil
"Date for tree entry:"
956 (cond ((and (or (not (boundp 'org-time-was-given
))
957 (not org-time-was-given
))
958 (not (= (time-to-days prompt-time
) (org-today))))
959 ;; Use 00:00 when no time is given for another
963 (cl-cdddr (decode-time prompt-time
)))))
964 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)"
965 org-read-date-final-answer
)
966 ;; Replace any time range by its start.
968 (org-read-date-analyze
969 (replace-match "\\1 \\2" nil nil
970 org-read-date-final-answer
)
971 prompt-time
(decode-time prompt-time
))))
973 (time-to-days prompt-time
)))
975 ;; Current date, possibly corrected for late night
978 (`(file+function
,path
,function
)
979 (set-buffer (org-capture-target-buffer path
))
980 (org-capture-put-target-region-and-position)
983 (org-capture-put :exact-position
(point))
985 (and (derived-mode-p 'org-mode
) (org-at-heading-p))))
988 (org-capture-put :exact-position
(point))
990 (and (derived-mode-p 'org-mode
) (org-at-heading-p))))
992 (if (and (markerp org-clock-hd-marker
)
993 (marker-buffer org-clock-hd-marker
))
994 (progn (set-buffer (marker-buffer org-clock-hd-marker
))
995 (org-capture-put-target-region-and-position)
997 (goto-char org-clock-hd-marker
))
998 (error "No running clock that could be used as capture target")))
999 (target (error "Invalid capture target specification: %S" target
)))
1001 (org-capture-put :buffer
(current-buffer)
1003 :target-entry-p target-entry-p
1005 (and (featurep 'org-crypt
)
1006 (org-at-encrypted-entry-p)
1009 (and (org-back-to-heading t
) (point))))))))
1011 (defun org-capture-expand-file (file)
1012 "Expand functions, symbols and file names for FILE.
1013 When FILE is a function, call it. When it is a form, evaluate
1014 it. When it is a variable, return its value. When it is
1015 a string, treat it as a file name, possibly expanding it
1016 according to `org-directory', and return it. If it is the empty
1017 string, however, return `org-default-notes-file'. In any other
1018 case, raise an error."
1019 (let ((location (cond ((equal file
"") org-default-notes-file
)
1020 ((stringp file
) (expand-file-name file org-directory
))
1021 ((functionp file
) (funcall file
))
1022 ((and (symbolp file
) (bound-and-true-p file
)))
1024 (or (org-string-nw-p location
)
1025 (error "Invalid file location: %S" location
))))
1027 (defun org-capture-target-buffer (file)
1028 "Get a buffer for FILE.
1029 FILE is a generalized file location, as handled by
1030 `org-capture-expand-file'."
1031 (let ((file (org-capture-expand-file file
)))
1032 (or (org-find-base-buffer-visiting file
)
1033 (progn (org-capture-put :new-buffer t
)
1034 (find-file-noselect file
)))))
1036 (defun org-capture-place-template (&optional inhibit-wconf-store
)
1037 "Insert the template at the target location, and display the buffer.
1038 When `inhibit-wconf-store', don't store the window configuration, as it
1039 may have been stored before."
1040 (unless inhibit-wconf-store
1041 (org-capture-put :return-to-wconf
(current-window-configuration)))
1042 (delete-other-windows)
1043 (org-switch-to-buffer-other-window
1044 (org-capture-get-indirect-buffer (org-capture-get :buffer
) "CAPTURE"))
1047 (goto-char (org-capture-get :pos
))
1048 (setq-local outline-level
'org-outline-level
)
1049 (pcase (org-capture-get :type
)
1050 ((or `nil
`entry
) (org-capture-place-entry))
1051 (`table-line
(org-capture-place-table-line))
1052 (`plain
(org-capture-place-plain-text))
1053 (`item
(org-capture-place-item))
1054 (`checkitem
(org-capture-place-item)))
1055 (org-capture-mode 1)
1056 (setq-local org-capture-current-plist org-capture-plist
))
1058 (defun org-capture-place-entry ()
1059 "Place the template as a new Org entry."
1060 (let* ((txt (org-capture-get :template
))
1061 (reversed (org-capture-get :prepend
))
1062 (target-entry-p (org-capture-get :target-entry-p
))
1065 (and (org-capture-get :exact-position
)
1066 (goto-char (org-capture-get :exact-position
)))
1068 ((not target-entry-p
)
1069 ;; Insert as top-level entry, either at beginning or at end of
1073 (progn (goto-char (point-min))
1074 (or (org-at-heading-p)
1075 (outline-next-heading)))
1076 (goto-char (point-max))
1077 (or (bolp) (insert "\n"))))
1079 ;; Insert as a child of the current entry
1080 (and (looking-at "\\*+")
1081 (setq level
(- (match-end 0) (match-beginning 0))))
1082 (setq level
(org-get-valid-level (or level
1) 1))
1085 (outline-next-heading)
1086 (or (bolp) (insert "\n")))
1087 (org-end-of-subtree t nil
)
1088 (or (bolp) (insert "\n")))))
1089 (org-capture-empty-lines-before)
1091 (org-capture-verify-tree txt
)
1092 (org-paste-subtree level txt
'for-yank
)
1093 (org-capture-empty-lines-after 1)
1094 (org-capture-position-for-last-stored beg
)
1095 (outline-next-heading)
1097 (org-capture-mark-kill-region beg
(1- end
))
1098 (org-capture-narrow beg
(1- end
))
1099 (if (or (re-search-backward "%\\?" beg t
)
1100 (re-search-forward "%\\?" end t
))
1101 (replace-match ""))))
1103 (defun org-capture-place-item ()
1104 "Place the template as a new plain list item."
1105 (let* ((txt (org-capture-get :template
))
1106 (target-entry-p (org-capture-get :target-entry-p
))
1109 (if (org-capture-get :exact-position
)
1110 (goto-char (org-capture-get :exact-position
))
1112 ((not target-entry-p
)
1113 ;; Insert as top-level entry, either at beginning or at end of file
1114 (setq beg
(point-min) end
(point-max)))
1116 (setq beg
(1+ (point-at-eol))
1117 end
(save-excursion (outline-next-heading) (point)))))
1119 (if (org-capture-get :prepend
)
1122 (when (org-list-search-forward (org-item-beginning-re) end t
)
1123 (goto-char (match-beginning 0))
1124 (setq ind
(org-get-indentation))))
1126 (when (org-list-search-backward (org-item-beginning-re) beg t
)
1127 (setq ind
(org-get-indentation))
1129 (unless ind
(goto-char end
)))
1130 ;; Remove common indentation
1131 (setq txt
(org-remove-indentation txt
))
1132 ;; Make sure this is indeed an item
1133 (unless (string-match (concat "\\`" (org-item-re)) txt
)
1134 (setq txt
(concat "- "
1135 (mapconcat 'identity
(split-string txt
"\n")
1137 ;; Prepare surrounding empty lines.
1138 (org-capture-empty-lines-before)
1140 (unless (eolp) (save-excursion (insert "\n")))
1143 (setq ind
(org-get-indentation))
1144 (delete-region beg
(point)))
1145 ;; Set the correct indentation, depending on context
1146 (setq ind
(make-string ind ?\
))
1147 (setq txt
(concat ind
1148 (mapconcat 'identity
(split-string txt
"\n")
1153 (org-capture-empty-lines-after 1)
1154 (org-capture-position-for-last-stored beg
)
1157 (org-capture-mark-kill-region beg
(1- end
))
1158 (org-capture-narrow beg
(1- end
))
1159 (if (or (re-search-backward "%\\?" beg t
)
1160 (re-search-forward "%\\?" end t
))
1161 (replace-match ""))))
1163 (defun org-capture-place-table-line ()
1164 "Place the template as a table line."
1165 (require 'org-table
)
1166 (let* ((txt (org-capture-get :template
))
1167 (target-entry-p (org-capture-get :target-entry-p
))
1168 (table-line-pos (org-capture-get :table-line-pos
))
1171 ((org-capture-get :exact-position
)
1172 (goto-char (org-capture-get :exact-position
)))
1173 ((not target-entry-p
)
1174 ;; Table is not necessarily under a heading
1175 (setq beg
(point-min) end
(point-max)))
1177 ;; WE are at a heading, limit search to the body
1178 (setq beg
(1+ (point-at-eol))
1179 end
(save-excursion (outline-next-heading) (point)))))
1180 (if (re-search-forward org-table-dataline-regexp end t
)
1181 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t
))
1183 (if (looking-at "[ \t]*#\\+tblfm:")
1185 (narrow-to-region b
(point)))
1187 (insert "\n| |\n|----|\n| |\n")
1188 (narrow-to-region (1+ end
) (point)))
1189 ;; We are narrowed to the table, or to an empty line if there was no table
1191 ;; Check if the template is good
1192 (if (not (string-match org-table-dataline-regexp txt
))
1193 (setq txt
"| %?Bad template |\n"))
1194 (if (functionp table-line-pos
)
1195 (setq table-line-pos
(funcall table-line-pos
))
1196 (setq table-line-pos
(eval table-line-pos
)))
1198 ((and table-line-pos
1199 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos
))
1200 (goto-char (point-min))
1201 ;; we have a complex line specification
1202 (let ((ll (ignore-errors
1203 (save-match-data (org-table-analyze))
1204 (aref org-table-hlines
1205 (- (match-end 1) (match-beginning 1)))))
1206 (delta (string-to-number (match-string 2 table-line-pos
))))
1207 ;; The user wants a special position in the table
1209 (error "Invalid table line specification \"%s\"" table-line-pos
))
1210 (goto-char org-table-current-begin-pos
)
1211 (forward-line (+ ll delta
(if (< delta
0) 0 -
1)))
1212 (org-table-insert-row 'below
)
1213 (beginning-of-line 1)
1214 (delete-region (point) (1+ (point-at-eol)))
1217 (setq end
(point))))
1218 ((org-capture-get :prepend
)
1219 (goto-char (point-min))
1220 (re-search-forward org-table-hline-regexp nil t
)
1221 (beginning-of-line 1)
1222 (re-search-forward org-table-dataline-regexp nil t
)
1223 (beginning-of-line 1)
1225 (org-table-insert-row)
1226 (beginning-of-line 1)
1227 (delete-region (point) (1+ (point-at-eol)))
1231 (goto-char (point-max))
1232 (re-search-backward org-table-dataline-regexp nil t
)
1233 (beginning-of-line 1)
1234 (org-table-insert-row 'below
)
1235 (beginning-of-line 1)
1236 (delete-region (point) (1+ (point-at-eol)))
1239 (setq end
(point))))
1241 (org-capture-position-for-last-stored 'table-line
)
1242 (if (or (re-search-backward "%\\?" beg t
)
1243 (re-search-forward "%\\?" end t
))
1247 (defun org-capture-place-plain-text ()
1248 "Place the template plainly.
1249 If the target locator points at an Org node, place the template into
1250 the text of the entry, before the first child. If not, place the
1251 template at the beginning or end of the file.
1252 Of course, if exact position has been required, just put it there."
1253 (let* ((txt (org-capture-get :template
))
1256 ((org-capture-get :exact-position
)
1257 (goto-char (org-capture-get :exact-position
)))
1258 ((and (org-capture-get :target-entry-p
)
1260 (looking-at org-outline-regexp
))
1261 ;; we should place the text into this entry
1262 (if (org-capture-get :prepend
)
1263 ;; Skip meta data and drawers
1264 (org-end-of-meta-data t
)
1265 ;; go to ent of the entry text, before the next headline
1266 (outline-next-heading)))
1268 ;; beginning or end of file
1269 (goto-char (if (org-capture-get :prepend
) (point-min) (point-max)))))
1270 (or (bolp) (newline))
1271 (org-capture-empty-lines-before)
1274 (org-capture-empty-lines-after 1)
1275 (org-capture-position-for-last-stored beg
)
1277 (org-capture-mark-kill-region beg
(1- end
))
1278 (org-capture-narrow beg
(1- end
))
1279 (if (or (re-search-backward "%\\?" beg t
)
1280 (re-search-forward "%\\?" end t
))
1281 (replace-match ""))))
1283 (defun org-capture-mark-kill-region (beg end
)
1284 "Mark the region that will have to be killed when aborting capture."
1285 (let ((m1 (move-marker (make-marker) beg
))
1286 (m2 (move-marker (make-marker) end
)))
1287 (org-capture-put :begin-marker m1
)
1288 (org-capture-put :end-marker m2
)))
1290 (defun org-capture-position-for-last-stored (where)
1291 "Memorize the position that should later become the position of last capture."
1294 (org-capture-put :position-for-last-stored
1295 (move-marker (make-marker) where
1296 (or (buffer-base-buffer (current-buffer))
1297 (current-buffer)))))
1298 ((eq where
'table-line
)
1299 (org-capture-put :position-for-last-stored
1301 (org-table-current-dline))))
1302 (t (error "This should not happen"))))
1304 (defun org-capture-store-last-position ()
1305 "Store the last-captured position."
1306 (let* ((where (org-capture-get :position-for-last-stored
'local
))
1309 (prog1 (marker-position where
)
1310 (move-marker where nil
)))
1311 ((and (listp where
) (eq (car where
) 'table-line
))
1312 (if (org-at-table-p)
1314 (org-table-goto-line (nth 1 where
))
1317 (with-current-buffer (buffer-base-buffer (current-buffer))
1318 (org-with-point-at pos
1319 (when org-capture-bookmark
1320 (let ((bookmark (plist-get org-bookmark-names-plist
:last-capture
)))
1321 (when bookmark
(with-demoted-errors (bookmark-set bookmark
)))))
1322 (move-marker org-capture-last-stored-marker
(point))))))
1324 (defun org-capture-narrow (beg end
)
1325 "Narrow, unless configuration says not to narrow."
1326 (unless (org-capture-get :unnarrowed
)
1327 (narrow-to-region beg end
)
1330 (defun org-capture-empty-lines-before (&optional n
)
1331 "Set the correct number of empty lines before the insertion point.
1332 Point will be after the empty lines, so insertion can directly be done."
1333 (setq n
(or n
(org-capture-get :empty-lines-before
)
1334 (org-capture-get :empty-lines
) 0))
1335 (let ((pos (point)))
1336 (org-back-over-empty-lines)
1337 (delete-region (point) pos
)
1338 (if (> n
0) (newline n
))))
1340 (defun org-capture-empty-lines-after (&optional n
)
1341 "Set the correct number of empty lines after the inserted string.
1342 Point will remain at the first line after the inserted text."
1343 (setq n
(or n
(org-capture-get :empty-lines-after
)
1344 (org-capture-get :empty-lines
) 0))
1345 (org-back-over-empty-lines)
1346 (while (looking-at "[ \t]*\n") (replace-match ""))
1347 (let ((pos (point)))
1348 (if (> n
0) (newline n
))
1351 (defvar org-clock-marker
) ; Defined in org.el
1353 (defun org-capture-insert-template-here ()
1354 "Insert the capture template at point."
1355 (let* ((template (org-capture-get :template
))
1356 (type (org-capture-get :type
))
1358 (or (bolp) (newline))
1361 ((and (eq type
'entry
) (derived-mode-p 'org-mode
))
1362 (org-capture-verify-tree (org-capture-get :template
))
1363 (org-paste-subtree nil template t
))
1364 ((and (memq type
'(item checkitem
))
1365 (derived-mode-p 'org-mode
)
1366 (save-excursion (skip-chars-backward " \t\n")
1371 (skip-chars-backward " ")
1372 (skip-chars-backward "-+*0123456789).")
1373 (delete-region (point) (point-at-eol))
1375 (org-remove-indentation template
)
1377 (org-capture-empty-lines-after)
1382 (t (insert template
)))
1385 (if (re-search-forward "%\\?" end t
)
1386 (replace-match ""))))
1388 (defun org-capture-set-plist (entry)
1389 "Initialize the property list from the template definition."
1390 (setq org-capture-plist
(copy-sequence (nthcdr 5 entry
)))
1391 (org-capture-put :key
(car entry
) :description
(nth 1 entry
)
1392 :target
(nth 3 entry
))
1393 (let ((txt (nth 4 entry
)) (type (or (nth 2 entry
) 'entry
)))
1394 (when (or (not txt
) (and (stringp txt
) (not (string-match "\\S-" txt
))))
1395 ;; The template may be empty or omitted for special types.
1396 ;; Here we insert the default templates for such cases.
1398 ((eq type
'item
) (setq txt
"- %?"))
1399 ((eq type
'checkitem
) (setq txt
"- [ ] %?"))
1400 ((eq type
'table-line
) (setq txt
"| %? |"))
1401 ((member type
'(nil entry
)) (setq txt
"* %?\n %a"))))
1402 (org-capture-put :template txt
:type type
)))
1404 (defun org-capture-goto-target (&optional template-key
)
1405 "Go to the target location of a capture template.
1406 The user is queried for the template."
1408 (let ((entry (org-capture-select-template template-key
)))
1409 (unless entry
(error "No capture template selected"))
1410 (org-capture-set-plist entry
)
1411 (org-capture-set-target-location)
1412 (pop-to-buffer-same-window (org-capture-get :buffer
))
1413 (goto-char (org-capture-get :pos
))))
1415 (defun org-capture-get-indirect-buffer (&optional buffer prefix
)
1416 "Make an indirect buffer for a capture process.
1417 Use PREFIX as a prefix for the name of the indirect buffer."
1418 (setq buffer
(or buffer
(current-buffer)))
1419 (let ((n 1) (base (buffer-name buffer
)) bname
)
1420 (setq bname
(concat prefix
"-" base
))
1421 (while (buffer-live-p (get-buffer bname
))
1422 (setq bname
(concat prefix
"-" (number-to-string (cl-incf n
)) "-" base
)))
1424 (make-indirect-buffer buffer bname
'clone
)
1426 (let ((buf (make-indirect-buffer buffer bname
)))
1427 (with-current-buffer buf
(org-mode))
1430 (defun org-capture-verify-tree (tree)
1431 "Throw error if TREE is not a valid tree."
1432 (unless (org-kill-is-subtree-p tree
)
1433 (error "Template is not a valid Org entry or tree")))
1435 (defun org-mks (table title
&optional prompt specials
)
1436 "Select a member of an alist with multiple keys.
1438 TABLE is the alist which should contain entries where the car is a string.
1439 There should be two types of entries.
1441 1. prefix descriptions like (\"a\" \"Description\")
1442 This indicates that `a' is a prefix key for multi-letter selection, and
1443 that there are entries following with keys like \"ab\", \"ax\"...
1445 2. Select-able members must have more than two elements, with the first
1446 being the string of keys that lead to selecting it, and the second a
1447 short description string of the item.
1449 The command will then make a temporary buffer listing all entries
1450 that can be selected with a single key, and all the single key
1451 prefixes. When you press the key for a single-letter entry, it is selected.
1452 When you press a prefix key, the commands (and maybe further prefixes)
1453 under this key will be shown and offered for selection.
1455 TITLE will be placed over the selection in the temporary buffer,
1456 PROMPT will be used when prompting for a key. SPECIAL is an
1457 alist with (\"key\" \"description\") entries. When one of these
1458 is selected, only the bare key is returned."
1459 (save-window-excursion
1460 (let ((inhibit-quit t
)
1461 (buffer (org-switch-to-buffer-other-window "*Org Select*"))
1462 (prompt (or prompt
"Select: "))
1468 (insert title
"\n\n")
1469 (let ((des-keys nil
)
1470 (allowed-keys '("\C-g"))
1472 ;; Populate allowed keys and descriptions keys
1473 ;; available with CURRENT selector.
1474 (let ((re (format "\\`%s\\(.\\)\\'"
1475 (if current
(regexp-quote current
) "")))
1476 (prefix (if current
(concat current
" ") "")))
1477 (dolist (entry table
)
1480 (`(,(and key
(pred (string-match re
))) ,desc
)
1481 (let ((k (match-string 1 key
)))
1483 (push k allowed-keys
)
1484 (insert prefix
"[" k
"]" "..." " " desc
"..." "\n")))
1486 (`(,(and key
(pred (string-match re
))) ,desc .
,_
)
1487 (let ((k (match-string 1 key
)))
1488 (insert prefix
"[" k
"]" " " desc
"\n")
1489 (push k allowed-keys
)))
1491 ;; Insert special entries, if any.
1493 (insert "----------------------------------------------------\
1494 ---------------------------\n")
1495 (pcase-dolist (`(,key
,description
) specials
)
1496 (insert (format "[%s] %s\n" key description
))
1497 (push key allowed-keys
)))
1498 ;; Display UI and let user select an entry or
1499 ;; a sub-level prefix.
1500 (goto-char (point-min))
1501 (unless (pos-visible-in-window-p (point-max))
1502 (org-fit-window-to-buffer))
1504 (let ((pressed (char-to-string (read-char-exclusive))))
1505 (while (not (member pressed allowed-keys
))
1506 (message "Invalid key `%s'" pressed
) (sit-for 1)
1508 (setq pressed
(char-to-string (read-char-exclusive))))
1509 (setq current
(concat current pressed
))
1511 ((equal pressed
"\C-g") (user-error "Abort"))
1512 ;; Selection is a prefix: open a new menu.
1513 ((member pressed des-keys
))
1514 ;; Selection matches an association: return it.
1515 ((let ((entry (assoc current table
)))
1516 (and entry
(throw 'exit entry
))))
1517 ;; Selection matches a special entry: return the
1518 ;; selection prefix.
1519 ((assoc current specials
) (throw 'exit current
))
1520 (t (error "No entry available")))))))
1521 (when buffer
(kill-buffer buffer
))))))
1523 ;;; The template code
1524 (defun org-capture-select-template (&optional keys
)
1525 "Select a capture template.
1526 Lisp programs can force the template by setting KEYS to a string."
1527 (let ((org-capture-templates
1528 (or (org-contextualize-keys
1529 org-capture-templates org-capture-templates-contexts
)
1530 '(("t" "Task" entry
(file+headline
"" "Tasks")
1531 "* TODO %?\n %u\n %a")))))
1533 (or (assoc keys org-capture-templates
)
1534 (error "No capture template referred to by \"%s\" keys" keys
))
1535 (org-mks org-capture-templates
1536 "Select a capture template\n========================="
1538 '(("C" "Customize org-capture-templates")
1541 (defun org-capture-fill-template (&optional template initial annotation
)
1542 "Fill a template and return the filled template as a string.
1543 The template may still contain \"%?\" for cursor positioning."
1544 (let* ((template (or template
(org-capture-get :template
)))
1545 (buffer (org-capture-get :buffer
))
1546 (file (buffer-file-name (or (buffer-base-buffer buffer
) buffer
)))
1547 (time (let* ((c (or (org-capture-get :default-time
) (current-time)))
1548 (d (decode-time c
)))
1549 (if (< (nth 2 d
) org-extend-today-until
)
1550 (encode-time 0 59 23 (1- (nth 3 d
)) (nth 4 d
) (nth 5 d
))
1552 (v-t (format-time-string (org-time-stamp-format nil
) time
))
1553 (v-T (format-time-string (org-time-stamp-format t
) time
))
1554 (v-u (format-time-string (org-time-stamp-format nil t
) time
))
1555 (v-U (format-time-string (org-time-stamp-format t t
) time
))
1556 (v-c (and kill-ring
(current-kill 0)))
1557 (v-x (or (org-get-x-clipboard 'PRIMARY
)
1558 (org-get-x-clipboard 'CLIPBOARD
)
1559 (org-get-x-clipboard 'SECONDARY
)))
1560 ;; `initial' and `annotation' might have been passed. But if
1561 ;; the property list has them, we prefer those values.
1562 (v-i (or (plist-get org-store-link-plist
:initial
)
1563 (and (stringp initial
) (org-no-properties initial
))
1564 (org-capture-get :initial
)
1567 (let ((a (or (plist-get org-store-link-plist
:annotation
)
1569 (org-capture-get :annotation
)
1571 ;; Is the link empty? Then we do not want it...
1572 (if (equal a
"[[]]") "" a
)))
1573 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1574 (v-A (if (and v-a
(string-match l-re v-a
))
1575 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a
)
1577 (v-l (if (and v-a
(string-match l-re v-a
))
1578 (replace-match "\\1" nil nil v-a
)
1580 (v-n user-full-name
)
1581 (v-k (and (marker-buffer org-clock-marker
)
1582 (org-no-properties org-clock-heading
)))
1583 (v-K (if (marker-buffer org-clock-marker
)
1584 (org-make-link-string
1585 (buffer-file-name (marker-buffer org-clock-marker
))
1586 org-clock-heading
)))
1587 (v-f (or (org-capture-get :original-file-nondirectory
) ""))
1588 (v-F (or (org-capture-get :original-file
) ""))
1589 (clipboards (delq nil
1591 (org-get-x-clipboard 'PRIMARY
)
1592 (org-get-x-clipboard 'CLIPBOARD
)
1593 (org-get-x-clipboard 'SECONDARY
)
1596 (setq org-store-link-plist
(plist-put org-store-link-plist
:annotation v-a
))
1597 (setq org-store-link-plist
(plist-put org-store-link-plist
:initial v-i
))
1601 (message "no template") (ding)
1603 (save-window-excursion
1604 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1606 (setq buffer-file-name nil
)
1607 (setq mark-active nil
)
1609 (goto-char (point-min))
1611 ;; %[] insert contents of a file.
1613 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t
)
1614 (let ((filename (expand-file-name (match-string 1)))
1615 (beg (copy-marker (match-beginning 0)))
1616 (end (copy-marker (match-end 0))))
1617 (unless (org-capture-escaped-%
)
1618 (delete-region beg end
)
1619 (set-marker beg nil
)
1620 (set-marker end nil
)
1621 (condition-case error
1622 (insert-file-contents filename
)
1624 (insert (format "%%![couldn not insert %s: %s]"
1628 ;; Mark %() embedded elisp for later evaluation.
1629 (org-capture-expand-embedded-elisp 'mark
)
1631 ;; Expand non-interactive templates.
1632 (let ((regexp "%\\(:[-a-za-z]+\\|<\\([^>\n]+\\)>\\|[aAcfFikKlntTuUx]\\)"))
1634 (while (re-search-forward regexp nil t
)
1635 ;; `org-capture-escaped-%' may modify buffer and cripple
1636 ;; match-data. Use markers instead. Ditto for other
1638 (let ((pos (copy-marker (match-beginning 0)))
1639 (end (copy-marker (match-end 0)))
1640 (value (match-string 1))
1641 (time-string (match-string 2)))
1642 (unless (org-capture-escaped-%
)
1643 (delete-region pos end
)
1644 (set-marker pos nil
)
1645 (set-marker end nil
)
1647 (pcase (string-to-char value
)
1648 (?
< (format-time-string time-string
))
1650 (or (plist-get org-store-link-plist
(intern value
))
1652 (?i
(let ((lead (buffer-substring-no-properties
1653 (line-beginning-position) (point))))
1654 (mapconcat #'identity
1655 (split-string v-i
"\n")
1656 (concat "\n" lead
))))
1672 (if (org-capture-inside-embedded-elisp-p)
1673 (replace-regexp-in-string "\"" "\\\\\"" replacement
)
1676 ;; Expand %() embedded Elisp. Limit to Sexp originally marked.
1677 (org-capture-expand-embedded-elisp)
1679 ;; Expand interactive templates. This is the last step so that
1680 ;; template is mostly expanded when prompting happens. Turn on
1681 ;; Org mode and set local variables. This is to support
1682 ;; completion in interactive prompts.
1683 (let ((org-inhibit-startup t
)) (org-mode))
1684 (org-clone-local-variables buffer
"\\`org-")
1685 (let (strings) ; Stores interactive answers.
1687 (let ((regexp "%\\^\\(?:{\\([^}]*\\)}\\)?\\([CgGLptTuU]\\)?"))
1688 (while (re-search-forward regexp nil t
)
1689 (let* ((items (and (match-end 1)
1691 (split-string (match-string-no-properties 1)
1693 (key (match-string 2))
1694 (beg (copy-marker (match-beginning 0)))
1695 (end (copy-marker (match-end 0)))
1696 (prompt (nth 0 items
))
1697 (default (nth 1 items
))
1698 (completions (nthcdr 2 items
)))
1699 (unless (org-capture-escaped-%
)
1700 (delete-region beg end
)
1701 (set-marker beg nil
)
1702 (set-marker end nil
)
1705 (let* ((org-last-tags-completion-table
1706 (org-global-tags-completion-table
1707 (cond ((equal key
"G") (org-agenda-files))
1710 (org-add-colon-after-tag-completion t
)
1715 (if prompt
(concat prompt
": ") "Tags: ")
1716 'org-tags-completion-function nil nil nil
1718 "[^[:alnum:]_@#%]+")
1720 (when (org-string-nw-p ins
)
1721 (unless (eq (char-before) ?
:) (insert ":"))
1723 (unless (eq (char-after) ?
:) (insert ":"))
1724 (and (org-at-heading-p)
1725 (let ((org-ignore-region t
))
1726 (org-set-tags nil
'align
))))))
1729 ((= (length clipboards
) 1) (insert (car clipboards
)))
1730 ((> (length clipboards
) 1)
1731 (insert (read-string "Clipboard/kill value: "
1734 (car clipboards
))))))
1736 (cond ((= (length clipboards
) 1)
1737 (org-insert-link 0 (car clipboards
)))
1738 ((> (length clipboards
) 1)
1741 (read-string "Clipboard/kill value: "
1744 (car clipboards
))))))
1745 ("p" (org-set-property prompt nil
))
1747 ;; These are the date/time related ones.
1748 (let* ((upcase?
(equal (upcase key
) key
))
1749 (org-time-was-given upcase?
)
1750 (org-end-time-was-given)
1751 (time (org-read-date upcase? t nil prompt
)))
1752 (org-insert-time-stamp
1753 time org-time-was-given
1754 (member key
'("u" "U"))
1755 nil nil
(list org-end-time-was-given
))))
1757 (push (org-completing-read
1758 (concat (or prompt
"Enter string")
1759 (and default
(format " [%s]" default
))
1761 completions nil nil nil nil default
)
1763 (insert (car strings
)))))))))
1765 ;; Replace %n escapes with nth %^{...} string.
1766 (setq strings
(nreverse strings
))
1768 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t
)
1769 (unless (org-capture-escaped-%
)
1771 (nth (1- (string-to-number (match-string 1))) strings
)
1774 ;; Make sure there are no empty lines before the text, and that
1775 ;; it ends with a newline character.
1776 (skip-chars-forward " \t\n")
1777 (delete-region (point-min) (line-beginning-position))
1778 (goto-char (point-max))
1779 (skip-chars-backward " \t\n")
1780 (delete-region (point) (point-max))
1783 ;; Return the expanded template and kill the capture buffer.
1784 (untabify (point-min) (point-max))
1785 (set-buffer-modified-p nil
)
1786 (prog1 (buffer-substring-no-properties (point-min) (point-max))
1787 (kill-buffer (current-buffer))))))
1789 (defun org-capture-escaped-%
()
1790 "Non-nil if % was escaped.
1791 If yes, unescape it now. Assume match-data contains the
1792 placeholder to check."
1794 (goto-char (match-beginning 0))
1795 (let ((n (abs (skip-chars-backward "\\\\"))))
1796 (delete-char (/ (1+ n
) 2))
1799 (defun org-capture-expand-embedded-elisp (&optional mark
)
1800 "Evaluate embedded elisp %(sexp) and replace with the result.
1801 When optional MARK argument is non-nil, mark Sexp with a text
1802 property (`org-embedded-elisp') for later evaluation. Only
1803 marked Sexp are evaluated when this argument is nil."
1805 (goto-char (point-min))
1806 (while (re-search-forward "%(" nil t
)
1808 ((get-text-property (match-beginning 0) 'org-embedded-elisp
)
1809 (goto-char (match-beginning 0))
1810 (let ((template-start (point)))
1812 (let* ((sexp (read (current-buffer)))
1814 (org-capture--expand-keyword-in-embedded-elisp
1816 (delete-region template-start
(point))
1819 ((stringp result
) (insert result
))
1821 "Capture template sexp `%s' must evaluate to string or nil"
1824 ;; Only mark valid and non-escaped sexp.
1825 ((org-capture-escaped-%
) nil
)
1827 (let ((end (with-syntax-table emacs-lisp-mode-syntax-table
1828 (ignore-errors (scan-sexps (1- (point)) 1)))))
1830 (put-text-property (- (point) 2) end
'org-embedded-elisp t
))))))))
1832 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1833 "Recursively replace capture link keywords in ATTR sexp.
1834 Such keywords are prefixed with \"%:\". See
1835 `org-capture-template' for more information."
1837 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr
))
1839 (let* ((attr-symbol (symbol-name attr
))
1840 (key (and (string-match "%\\(:.*\\)" attr-symbol
)
1841 (intern (match-string 1 attr-symbol
)))))
1842 (or (plist-get org-store-link-plist key
)
1846 (defun org-capture-inside-embedded-elisp-p ()
1847 "Non-nil if point is inside of embedded elisp %(sexp).
1848 Assume sexps have been marked with
1849 `org-capture-expand-embedded-elisp' beforehand."
1850 (get-text-property (point) 'org-embedded-elisp
))
1853 (defun org-capture-import-remember-templates ()
1854 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1856 (when (and (yes-or-no-p
1857 "Import old remember templates into org-capture-templates? ")
1859 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1860 (require 'org-remember
)
1861 (setq org-capture-templates
1864 (let ((desc (car entry
))
1865 (key (char-to-string (nth 1 entry
)))
1866 (template (nth 2 entry
))
1867 (file (or (nth 3 entry
) org-default-notes-file
))
1868 (position (or (nth 4 entry
) org-remember-default-headline
))
1870 (prepend org-reverse-note-order
)
1871 immediate target jump-to-captured
)
1873 ((member position
'(top bottom
))
1874 (setq target
(list 'file file
)
1875 prepend
(eq position
'top
)))
1876 ((eq position
'date-tree
)
1877 (setq target
(list 'file
+datetree file
)
1879 (t (setq target
(list 'file
+headline file position
))))
1881 (when (string-match "%!" template
)
1882 (setq template
(replace-match "" t t template
)
1885 (when (string-match "%&" template
)
1886 (setq jump-to-captured t
))
1888 (append (list key desc type target template
)
1889 (if prepend
'(:prepend t
))
1890 (if immediate
'(:immediate-finish t
))
1891 (if jump-to-captured
'(:jump-to-captured t
)))))
1893 org-remember-templates
))))
1894 ;;; The function was made obsolete by commit 65399674d5 of
1895 ;;; 2013-02-22. This make-obsolete call was added 2016-09-01.
1896 (make-obsolete 'org-capture-import-remember-templates
"use the `org-capture-templates' variable instead." "Org 9.0")
1898 (provide 'org-capture
)
1900 ;;; org-capture.el ends here