1 ;;; org-capture.el --- Fast note taking in Org -*- 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
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-goto-line
"org-table" (N))
61 (defvar org-end-time-was-given
)
62 (defvar org-remember-default-headline
)
63 (defvar org-remember-templates
)
64 (defvar org-table-hlines
)
65 (defvar org-table-current-begin-pos
)
66 (defvar dired-buffers
)
68 (defvar org-capture-clock-was-started nil
69 "Internal flag, noting if the clock was started.")
71 (defvar org-capture-last-stored-marker
(make-marker)
72 "Marker pointing to the entry most recently stored with `org-capture'.")
74 ;; The following variable is scoped dynamically by org-protocol
75 ;; to indicate that the link properties have already been stored
76 (defvar org-capture-link-is-already-stored nil
)
78 (defvar org-capture-is-refiling nil
79 "Non-nil when capture process is refiling an entry.")
81 (defgroup org-capture nil
82 "Options concerning capturing new entries."
86 (defcustom org-capture-templates nil
87 "Templates for the creation of new entries.
89 Each entry is a list with the following items:
91 keys The keys that will select the template, as a string, characters
92 only, for example \"a\" for a template to be selected with a
93 single key, or \"bt\" for selection with two keys. When using
94 several keys, keys using the same prefix key must be together
95 in the list and preceded by a 2-element entry explaining the
96 prefix key, for example
98 (\"b\" \"Templates for marking stuff to buy\")
100 The \"C\" key is used by default for quick access to the
101 customization of the template variable. But if you want to use
102 that key for a template, you can.
104 description A short string describing the template, will be shown during
107 type The type of entry. Valid types are:
108 entry an Org node, with a headline. Will be filed
109 as the child of the target entry or as a
111 item a plain list item, will be placed in the
112 first plain list at the target
114 checkitem a checkbox item. This differs from the
115 plain list item only is so far as it uses a
116 different default template.
117 table-line a new line in the first table at target location.
118 plain text to be inserted as it is.
120 target Specification of where the captured item should be placed.
121 In Org files, targets usually define a node. Entries will
122 become children of this node, other types will be added to the
123 table or list in the body of this node.
125 Most target specifications contain a file name. If that file
126 name is the empty string, it defaults to `org-default-notes-file'.
127 A file can also be given as a variable, function, or Emacs Lisp
128 form. When an absolute path is not specified for a
129 target, it is taken as relative to `org-directory'.
133 (file \"path/to/file\")
134 Text will be placed at the beginning or end of that file
136 (id \"id of existing org entry\")
137 File as child of this entry, or in the body of the entry
139 (file+headline \"path/to/file\" \"node headline\")
140 Fast configuration if the target heading is unique in the file
142 (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
143 For non-unique headings, the full path is safer
145 (file+regexp \"path/to/file\" \"regexp to find location\")
146 File to the entry matching regexp
148 (file+datetree \"path/to/file\")
149 Will create a heading in a date tree for today's date
151 (file+datetree+prompt \"path/to/file\")
152 Will create a heading in a date tree, prompts for date
154 (file+weektree \"path/to/file\")
155 Will create a heading in a week tree for today's date
157 (file+weektree+prompt \"path/to/file\")
158 Will create a heading in a week tree, prompts for date
160 (file+function \"path/to/file\" function-finding-location)
161 A function to find the right location in the file
164 File to the entry that is currently being clocked
166 (function function-finding-location)
167 Most general way: write your own function which both visits
168 the file and moves point to the right location
170 template The template for creating the capture item. If you leave this
171 empty, an appropriate default template will be used. See below
172 for more details. Instead of a string, this may also be one of
174 (file \"/path/to/template-file\")
175 (function function-returning-the-template)
177 in order to get a template from a file, or dynamically
180 The rest of the entry is a property list of additional options. Recognized
183 :prepend Normally newly captured information will be appended at
184 the target location (last child, last table line,
185 last list item...). Setting this property will
188 :immediate-finish When set, do not offer to edit the information, just
189 file it away immediately. This makes sense if the
190 template only needs information that can be added
193 :jump-to-captured When set, jump to the captured entry when finished.
195 :empty-lines Set this to the number of lines the should be inserted
196 before and after the new item. Default 0, only common
199 :empty-lines-before Set this to the number of lines the should be inserted
200 before the new item. Overrides :empty-lines for the
201 number lines inserted before.
203 :empty-lines-after Set this to the number of lines the should be inserted
204 after the new item. Overrides :empty-lines for the
205 number of lines inserted after.
207 :clock-in Start the clock in this item.
209 :clock-keep Keep the clock running when filing the captured entry.
211 :clock-resume Start the interrupted clock when finishing the capture.
212 Note that :clock-keep has precedence over :clock-resume.
213 When setting both to t, the current clock will run and
214 the previous one will not be resumed.
216 :unnarrowed Do not narrow the target buffer, simply show the
217 full buffer. Default is to narrow it so that you
218 only see the new stuff.
220 :table-line-pos Specification of the location in the table where the
221 new line should be inserted. It should be a string like
222 \"II-3\", meaning that the new line should become the
223 third line before the second horizontal separator line.
225 :kill-buffer If the target file was not yet visited by a buffer when
226 capture was invoked, kill the buffer again after capture
229 The template defines the text to be inserted. Often this is an
230 Org mode entry (so the first line should start with a star) that
231 will be filed as a child of the target headline. It can also be
232 freely formatted text. Furthermore, the following %-escapes will
233 be replaced with content and expanded:
235 %[pathname] Insert the contents of the file given by
236 `pathname'. These placeholders are expanded at the very
237 beginning of the process so they can be used to extend the
239 %(sexp) Evaluate elisp `(sexp)' and replace it with the results.
240 Only placeholders pre-existing within the template, or
241 introduced with %[pathname] are expanded this way. Since this
242 happens after expanding non-interactive %-escapes, those can
243 be used to fill the expression.
244 %<...> The result of format-time-string on the ... format specification.
245 %t Time stamp, date only.
246 %T Time stamp with date and time.
247 %u, %U Like the above, but inactive time stamps.
248 %i Initial content, copied from the active region. If %i is
249 indented, the entire inserted text will be indented as well.
250 %a Annotation, normally the link created with `org-store-link'.
251 %A Like %a, but prompt for the description part.
252 %l Like %a, but only insert the literal link.
253 %c Current kill ring head.
254 %x Content of the X clipboard.
255 %k Title of currently clocked task.
256 %K Link to currently clocked task.
257 %n User name (taken from the variable `user-full-name').
258 %f File visited by current buffer when org-capture was called.
259 %F Full path of the file or directory visited by current buffer.
260 %:keyword Specific information for certain link types, see below.
261 %^g Prompt for tags, with completion on tags in target file.
262 %^G Prompt for tags, with completion on all tags in all agenda files.
263 %^t Like %t, but prompt for date. Similarly %^T, %^u, %^U.
264 You may define a prompt like: %^{Please specify birthday}t
265 %^C Interactive selection of which kill or clip to use.
266 %^L Like %^C, but insert as link.
267 %^{prop}p Prompt the user for a value for property `prop'.
268 %^{prompt} Prompt the user for a string and replace this sequence with it.
269 A default value and a completion table ca be specified like this:
270 %^{prompt|default|completion2|completion3|...}.
271 %? After completing the template, position cursor here.
272 %\\N Insert the text entered at the nth %^{prompt}, where N
273 is a number, starting from 1.
275 Apart from these general escapes, you can access information specific to
276 the link type that is created. For example, calling `org-capture' in emails
277 or in Gnus will record the author and the subject of the message, which you
278 can access with \"%:from\" and \"%:subject\", respectively. Here is a
279 complete list of what is recorded for each link type.
281 Link type | Available information
282 ------------------------+------------------------------------------------------
283 bbdb | %:type %:name %:company
284 vm, wl, mh, mew, rmail, | %:type %:subject %:message-id
285 gnus | %:from %:fromname %:fromaddress
286 | %:to %:toname %:toaddress
287 | %:fromto (either \"to NAME\" or \"from NAME\")
288 | %:date %:date-timestamp (as active timestamp)
289 | %:date-timestamp-inactive (as inactive timestamp)
290 gnus | %:group, for messages also all email fields
291 eww, w3, w3m | %:type %:url
292 info | %:type %:file %:node
293 calendar | %:type %:date
295 When you need to insert a literal percent sign in the template,
296 you can escape ambiguous cases with a backward slash, e.g., \\%i."
300 (let ((file-variants '(choice :tag
"Filename "
301 (file :tag
"Literal")
302 (function :tag
"Function")
303 (variable :tag
"Variable")
304 (sexp :tag
"Form"))))
306 (choice :value
("" "" entry
(file "~/org/notes.org") "")
307 (list :tag
"Multikey description"
308 (string :tag
"Keys ")
309 (string :tag
"Description"))
310 (list :tag
"Template entry"
311 (string :tag
"Keys ")
312 (string :tag
"Description ")
313 (choice :tag
"Capture Type " :value entry
314 (const :tag
"Org entry" entry
)
315 (const :tag
"Plain list item" item
)
316 (const :tag
"Checkbox item" checkitem
)
317 (const :tag
"Plain text" plain
)
318 (const :tag
"Table line" table-line
))
319 (choice :tag
"Target location"
321 (const :format
"" file
)
324 (const :format
"" id
)
326 (list :tag
"File & Headline"
327 (const :format
"" file
+headline
)
329 (string :tag
" Headline"))
330 (list :tag
"File & Outline path"
331 (const :format
"" file
+olp
)
333 (repeat :tag
"Outline path" :inline t
334 (string :tag
"Headline")))
335 (list :tag
"File & Regexp"
336 (const :format
"" file
+regexp
)
338 (regexp :tag
" Regexp"))
339 (list :tag
"File & Date tree"
340 (const :format
"" file
+datetree
)
342 (list :tag
"File & Date tree, prompt for date"
343 (const :format
"" file
+datetree
+prompt
)
345 (list :tag
"File & Week tree"
346 (const :format
"" file
+weektree
)
348 (list :tag
"File & Week tree, prompt for date"
349 (const :format
"" file
+weektree
+prompt
)
351 (list :tag
"File & function"
352 (const :format
"" file
+function
)
354 (sexp :tag
" Function"))
355 (list :tag
"Current clocking task"
356 (const :format
"" clock
))
357 (list :tag
"Function"
358 (const :format
"" function
)
359 (sexp :tag
" Function")))
360 (choice :tag
"Template "
363 (const :format
"" file
)
364 (file :tag
"Template file"))
365 (list :tag
"Function"
366 (const :format
"" function
)
367 (function :tag
"Template function")))
369 ;; Give the most common options as checkboxes
370 :options
(((const :format
"%v " :prepend
) (const t
))
371 ((const :format
"%v " :immediate-finish
) (const t
))
372 ((const :format
"%v " :jump-to-captured
) (const t
))
373 ((const :format
"%v " :empty-lines
) (const 1))
374 ((const :format
"%v " :empty-lines-before
) (const 1))
375 ((const :format
"%v " :empty-lines-after
) (const 1))
376 ((const :format
"%v " :clock-in
) (const t
))
377 ((const :format
"%v " :clock-keep
) (const t
))
378 ((const :format
"%v " :clock-resume
) (const t
))
379 ((const :format
"%v " :unnarrowed
) (const t
))
380 ((const :format
"%v " :table-line-pos
) (const t
))
381 ((const :format
"%v " :kill-buffer
) (const t
)))))))))
383 (defcustom org-capture-before-finalize-hook nil
384 "Hook that is run right before a capture process is finalized.
385 The capture buffer is still current when this hook runs and it is
386 widened to the entire buffer."
391 (defcustom org-capture-after-finalize-hook nil
392 "Hook that is run right after a capture process is finalized.
393 Suitable for window cleanup."
398 (defcustom org-capture-prepare-finalize-hook nil
399 "Hook that is run before the finalization starts.
400 The capture buffer is current and still narrowed."
405 (defcustom org-capture-bookmark t
406 "When non-nil, add a bookmark pointing at the last stored
407 position when capturing."
412 ;;; The property list for keeping information about the capture process
414 (defvar org-capture-plist nil
415 "Plist for the current capture process, global, to avoid having to pass it.")
417 (defvar org-capture-current-plist nil
418 "Local variable holding the plist in a capture buffer.
419 This is used to store the plist for use when finishing a capture process
420 because another such process might have changed the global variable by then.
422 Each time a new capture buffer has been set up, the global `org-capture-plist'
423 is copied to this variable, which is local in the indirect buffer.")
425 (defvar org-capture-clock-keep nil
426 "Local variable to store the value of the :clock-keep parameter.
427 This is needed in case org-capture-finalize is called interactively.")
429 (defun org-capture-put (&rest stuff
)
430 "Add properties to the capture property list `org-capture-plist'."
432 (setq org-capture-plist
(plist-put org-capture-plist
433 (pop stuff
) (pop stuff
)))))
434 (defun org-capture-get (prop &optional local
)
435 "Get properties from the capture property list `org-capture-plist'.
436 When LOCAL is set, use the local variable `org-capture-current-plist',
437 this is necessary after initialization of the capture process,
438 to avoid conflicts with other active capture processes."
439 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
441 (defun org-capture-member (prop &optional local
)
442 "Is PROP a property in `org-capture-plist'.
443 When LOCAL is set, use the local variable `org-capture-current-plist',
444 this is necessary after initialization of the capture process,
445 to avoid conflicts with other active capture processes."
446 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
450 (defvar org-capture-mode-map
(make-sparse-keymap)
451 "Keymap for `org-capture-mode', a minor mode.
452 Use this map to set additional keybindings for when Org mode is used
453 for a capture buffer.")
455 (defvar org-capture-mode-hook nil
456 "Hook for the minor `org-capture-mode'.")
458 (define-minor-mode org-capture-mode
459 "Minor mode for special key bindings in a capture buffer.
461 Turning on this mode runs the normal hook `org-capture-mode-hook'."
462 nil
" Rem" org-capture-mode-map
465 (substitute-command-keys
466 "\\<org-capture-mode-map>Capture buffer. Finish \
467 `\\[org-capture-finalize]', refile `\\[org-capture-refile]', \
468 abort `\\[org-capture-kill]'.")))
469 (define-key org-capture-mode-map
"\C-c\C-c" 'org-capture-finalize
)
470 (define-key org-capture-mode-map
"\C-c\C-k" 'org-capture-kill
)
471 (define-key org-capture-mode-map
"\C-c\C-w" 'org-capture-refile
)
473 ;;; The main commands
475 (defvar org-capture-initial nil
)
476 (defvar org-capture-entry nil
)
479 (defun org-capture-string (string &optional keys
)
480 "Capture STRING with the template selected by KEYS."
481 (interactive "sInitial text: \n")
482 (let ((org-capture-initial string
)
483 (org-capture-entry (org-capture-select-template keys
)))
486 (defcustom org-capture-templates-contexts nil
487 "Alist of capture templates and valid contexts.
489 For example, if you have a capture template \"c\" and you want
490 this template to be accessible only from `message-mode' buffers,
493 \\='((\"c\" ((in-mode . \"message-mode\"))))
495 Here are the available contexts definitions:
497 in-file: command displayed only in matching files
498 in-mode: command displayed only in matching modes
499 not-in-file: command not displayed in matching files
500 not-in-mode: command not displayed in matching modes
501 in-buffer: command displayed only in matching buffers
502 not-in-buffer: command not displayed in matching buffers
503 [function]: a custom function taking no argument
505 If you define several checks, the agenda command will be
506 accessible if there is at least one valid check.
508 You can also bind a key to another agenda custom command
509 depending on contextual rules.
511 \\='((\"c\" \"d\" ((in-mode . \"message-mode\"))))
513 Here it means: in `message-mode buffers', use \"c\" as the
514 key for the capture template otherwise associated with \"d\".
515 \(The template originally associated with \"d\" is not displayed
516 to avoid duplicates.)"
519 :type
'(repeat (list :tag
"Rule"
520 (string :tag
" Capture key")
521 (string :tag
"Replace by template")
522 (repeat :tag
"Available when"
524 (cons :tag
"Condition"
526 (const :tag
"In file" in-file
)
527 (const :tag
"Not in file" not-in-file
)
528 (const :tag
"In buffer" in-buffer
)
529 (const :tag
"Not in buffer" not-in-buffer
)
530 (const :tag
"In mode" in-mode
)
531 (const :tag
"Not in mode" not-in-mode
))
533 (function :tag
"Custom function"))))))
535 (defcustom org-capture-use-agenda-date nil
536 "Non-nil means use the date at point when capturing from agendas.
537 When nil, you can still capture using the date at point with
538 `\\[org-agenda-capture]'."
544 (defun org-capture (&optional goto keys
)
546 \\<org-capture-mode-map>
547 This will let you select a template from `org-capture-templates', and
548 then file the newly captured information. The text is immediately
549 inserted at the target location, and an indirect buffer is shown where
550 you can edit it. Pressing `\\[org-capture-finalize]' brings you back to the \
552 state of Emacs, so that you can continue your work.
554 When called interactively with a `\\[universal-argument]' prefix argument \
556 capture anything, just go to the file/headline where the selected
557 template stores its notes.
559 With a `\\[universal-argument] \\[universal-argument]' prefix argument, go to \
560 the last note stored.
562 When called with a `C-0' (zero) prefix, insert a template at point.
564 ELisp programs can set KEYS to a string associated with a template
565 in `org-capture-templates'. In this case, interactive selection
568 If `org-capture-use-agenda-date' is non-nil, capturing from the
569 agenda will use the date at point as the default date. Then, a
570 `C-1' prefix will tell the capture process to use the HH:MM time
571 of the day at point (if any) or the current HH:MM time."
573 (when (and org-capture-use-agenda-date
574 (eq major-mode
'org-agenda-mode
))
575 (setq org-overriding-default-time
576 (org-get-cursor-date (equal goto
1))))
578 ((equal goto
'(4)) (org-capture-goto-target))
579 ((equal goto
'(16)) (org-capture-goto-last-stored))
581 ;; FIXME: Are these needed?
582 (let* ((orig-buf (current-buffer))
583 (annotation (if (and (boundp 'org-capture-link-is-already-stored
)
584 org-capture-link-is-already-stored
)
585 (plist-get org-store-link-plist
:annotation
)
586 (ignore-errors (org-store-link nil
))))
587 (entry (or org-capture-entry
(org-capture-select-template keys
)))
589 (setq initial
(or org-capture-initial
590 (and (org-region-active-p)
591 (buffer-substring (point) (mark)))))
592 (when (stringp initial
)
593 (remove-text-properties 0 (length initial
) '(read-only t
) initial
))
594 (when (stringp annotation
)
595 (remove-text-properties 0 (length annotation
)
596 '(read-only t
) annotation
))
599 (customize-variable 'org-capture-templates
))
601 (user-error "Abort"))
603 (org-capture-set-plist entry
)
604 (org-capture-get-template)
605 (org-capture-put :original-buffer orig-buf
606 :original-file
(or (buffer-file-name orig-buf
)
607 (and (featurep 'dired
)
610 :original-file-nondirectory
611 (and (buffer-file-name orig-buf
)
612 (file-name-nondirectory
613 (buffer-file-name orig-buf
)))
614 :annotation annotation
616 :return-to-wconf
(current-window-configuration)
618 (or org-overriding-default-time
620 (org-capture-set-target-location)
621 (condition-case error
622 (org-capture-put :template
(org-capture-fill-template))
624 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
625 (error "Capture abort: %s" error
)))
627 (setq org-capture-clock-keep
(org-capture-get :clock-keep
))
630 (org-capture-insert-template-here)
631 (condition-case error
632 (org-capture-place-template
633 (eq (car (org-capture-get :target
)) 'function
))
635 (if (and (buffer-base-buffer (current-buffer))
636 (string-prefix-p "CAPTURE-" (buffer-name)))
637 (kill-buffer (current-buffer)))
638 (set-window-configuration (org-capture-get :return-to-wconf
))
639 (error "Capture template `%s': %s"
640 (org-capture-get :key
)
642 (if (and (derived-mode-p 'org-mode
)
643 (org-capture-get :clock-in
))
646 (if (org-clock-is-active)
647 (org-capture-put :interrupted-clock
648 (copy-marker org-clock-marker
)))
650 (setq-local org-capture-clock-was-started t
))
652 "Could not start the clock in this capture buffer")))
653 (if (org-capture-get :immediate-finish
)
654 (org-capture-finalize)))))))))
656 (defun org-capture-get-template ()
657 "Get the template from a file or a function if necessary."
658 (let ((txt (org-capture-get :template
)) file
)
660 ((and (listp txt
) (eq (car txt
) 'file
))
662 (setq file
(expand-file-name (nth 1 txt
) org-directory
)))
663 (setq txt
(org-file-contents file
))
664 (setq txt
(format "* Template file %s not found" (nth 1 txt
)))))
665 ((and (listp txt
) (eq (car txt
) 'function
))
666 (if (fboundp (nth 1 txt
))
667 (setq txt
(funcall (nth 1 txt
)))
668 (setq txt
(format "* Template function %s not found" (nth 1 txt
)))))
669 ((not txt
) (setq txt
""))
671 (t (setq txt
"* Invalid capture template")))
672 (org-capture-put :template txt
)))
674 (defun org-capture-finalize (&optional stay-with-capture
)
675 "Finalize the capture process.
676 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
677 captured item after finalizing."
679 (when (org-capture-get :jump-to-captured
)
680 (setq stay-with-capture t
))
681 (unless (and org-capture-mode
682 (buffer-base-buffer (current-buffer)))
683 (error "This does not seem to be a capture buffer for Org mode"))
685 (run-hooks 'org-capture-prepare-finalize-hook
)
687 ;; Did we start the clock in this capture buffer?
688 (when (and org-capture-clock-was-started
689 org-clock-marker
(marker-buffer org-clock-marker
)
690 (equal (marker-buffer org-clock-marker
) (buffer-base-buffer))
691 (> org-clock-marker
(point-min))
692 (< org-clock-marker
(point-max)))
693 ;; Looks like the clock we started is still running. Clock out.
694 (when (not org-capture-clock-keep
) (let (org-log-note-clock-out) (org-clock-out)))
695 (when (and (not org-capture-clock-keep
)
696 (org-capture-get :clock-resume
'local
)
697 (markerp (org-capture-get :interrupted-clock
'local
))
698 (buffer-live-p (marker-buffer
699 (org-capture-get :interrupted-clock
'local
))))
700 (let ((clock-in-task (org-capture-get :interrupted-clock
'local
)))
701 (org-with-point-at clock-in-task
703 (message "Interrupted clock has been resumed")))
705 (let ((beg (point-min))
708 ;; Store the size of the capture buffer
709 (org-capture-put :captured-entry-size
(- (point-max) (point-min)))
711 ;; Store the insertion point in the target buffer
712 (org-capture-put :insertion-point
(point))
715 (let ((m1 (org-capture-get :begin-marker
'local
))
716 (m2 (org-capture-get :end-marker
'local
)))
717 (if (and m1 m2
(= m1 beg
) (= m2 end
))
719 (setq m2
(if (cdr (assq 'heading org-blank-before-new-entry
))
721 m2
(if (< (point-max) m2
) (point-max) m2
))
722 (setq abort-note
'clean
)
724 (setq abort-note
'dirty
)))
726 ;; Make sure that the empty lines after are correct
727 (when (and (> (point-max) end
) ; indeed, the buffer was still narrowed
728 (member (org-capture-get :type
'local
)
729 '(entry item checkitem plain
)))
732 (or (bolp) (newline))
733 (org-capture-empty-lines-after
734 (or (org-capture-get :empty-lines-after
'local
)
735 (org-capture-get :empty-lines
'local
) 0))))
736 ;; Postprocessing: Update Statistics cookies, do the sorting
737 (when (derived-mode-p 'org-mode
)
739 (when (ignore-errors (org-back-to-heading))
740 (org-update-parent-todo-statistics)
741 (org-update-checkbox-count)))
742 ;; FIXME Here we should do the sorting
743 ;; If we have added a table line, maybe recompute?
744 (when (and (eq (org-capture-get :type
'local
) 'table-line
)
746 (if (org-table-get-stored-formulas)
747 (org-table-recalculate 'all
) ;; FIXME: Should we iterate???
749 ;; Store this place as the last one where we stored something
750 ;; Do the marking in the base buffer, so that it makes sense after
751 ;; the indirect buffer has been killed.
752 (when org-capture-bookmark
753 (org-capture-bookmark-last-stored-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
) decrypted-hl-pos
)
873 (setq target
(or target
(org-capture-get :target
)))
876 ((eq (car target
) 'file
)
877 (set-buffer (org-capture-target-buffer (nth 1 target
)))
878 (org-capture-put-target-region-and-position)
880 (setq target-entry-p nil
))
882 ((eq (car target
) 'id
)
883 (let ((loc (org-id-find (nth 1 target
))))
885 (error "Cannot find target ID \"%s\"" (nth 1 target
))
886 (set-buffer (org-capture-target-buffer (car loc
)))
888 (org-capture-put-target-region-and-position)
889 (goto-char (cdr loc
)))))
891 ((eq (car target
) 'file
+headline
)
892 (set-buffer (org-capture-target-buffer (nth 1 target
)))
893 (org-capture-put-target-region-and-position)
895 (let ((hd (nth 2 target
)))
896 (goto-char (point-min))
897 (unless (derived-mode-p 'org-mode
)
899 "Target buffer \"%s\" for file+headline should be in Org mode"
901 (if (re-search-forward
902 (format org-complex-heading-regexp-format
(regexp-quote hd
))
904 (goto-char (point-at-bol))
905 (goto-char (point-max))
906 (or (bolp) (insert "\n"))
907 (insert "* " hd
"\n")
908 (beginning-of-line 0))))
910 ((eq (car target
) 'file
+olp
)
911 (let ((m (org-find-olp
912 (cons (org-capture-expand-file (nth 1 target
))
914 (set-buffer (marker-buffer m
))
915 (org-capture-put-target-region-and-position)
919 ((eq (car target
) 'file
+regexp
)
920 (set-buffer (org-capture-target-buffer (nth 1 target
)))
921 (org-capture-put-target-region-and-position)
923 (goto-char (point-min))
924 (if (re-search-forward (nth 2 target
) nil t
)
926 (goto-char (if (org-capture-get :prepend
)
927 (match-beginning 0) (match-end 0)))
928 (org-capture-put :exact-position
(point))
929 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
930 (error "No match for target regexp in file %s" (nth 1 target
))))
932 ((memq (car target
) '(file+datetree file
+datetree
+prompt file
+weektree file
+weektree
+prompt
))
933 (require 'org-datetree
)
934 (set-buffer (org-capture-target-buffer (nth 1 target
)))
935 (org-capture-put-target-region-and-position)
937 ;; Make a date/week tree entry, with the current date (or
938 ;; yesterday, if we are extending dates for a couple of hours)
941 ((memq (car target
) '(file+weektree file
+weektree
+prompt
))
942 #'org-datetree-find-iso-week-create
)
943 (t #'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
))
950 ((memq (car target
) '(file+datetree
+prompt file
+weektree
+prompt
))
952 (let ((prompt-time (org-read-date
953 nil t nil
"Date for tree entry:"
957 (cond ((and (or (not (boundp 'org-time-was-given
))
958 (not org-time-was-given
))
959 (not (= (time-to-days prompt-time
) (org-today))))
960 ;; Use 00:00 when no time is given for another date than today?
963 (cl-cdddr (decode-time prompt-time
)))))
964 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer
)
965 ;; Replace any time range by its start
967 (org-read-date-analyze
968 (replace-match "\\1 \\2" nil nil org-read-date-final-answer
)
969 prompt-time
(decode-time prompt-time
))))
971 (time-to-days prompt-time
)))
973 ;; current date, possibly corrected for late night workers
976 ((eq (car target
) 'file
+function
)
977 (set-buffer (org-capture-target-buffer (nth 1 target
)))
978 (org-capture-put-target-region-and-position)
980 (funcall (nth 2 target
))
981 (org-capture-put :exact-position
(point))
982 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
984 ((eq (car target
) 'function
)
985 (funcall (nth 1 target
))
986 (org-capture-put :exact-position
(point))
987 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
989 ((eq (car target
) 'clock
)
990 (if (and (markerp org-clock-hd-marker
)
991 (marker-buffer org-clock-hd-marker
))
992 (progn (set-buffer (marker-buffer org-clock-hd-marker
))
993 (org-capture-put-target-region-and-position)
995 (goto-char org-clock-hd-marker
))
996 (error "No running clock that could be used as capture target")))
998 (t (error "Invalid capture target specification")))
1000 (when (and (featurep 'org-crypt
) (org-at-encrypted-entry-p))
1002 (setq decrypted-hl-pos
1003 (save-excursion (and (org-back-to-heading t
) (point)))))
1005 (org-capture-put :buffer
(current-buffer) :pos
(point)
1006 :target-entry-p target-entry-p
1007 :decrypted decrypted-hl-pos
))))
1009 (defun org-capture-expand-file (file)
1010 "Expand functions and symbols for FILE.
1011 When FILE is a function, call it. When it is a variable,
1012 retrieve its value. When it is the empty string, return
1013 `org-default-notes-file'. In any other case, return FILE as-is."
1015 ((equal file
"") org-default-notes-file
)
1016 ((functionp file
) (funcall file
))
1017 ((and (symbolp file
) (boundp file
)) (symbol-value file
))
1020 (defun org-capture-target-buffer (file)
1021 "Get a buffer for FILE."
1022 (setq file
(org-capture-expand-file file
))
1023 (setq file
(or (org-string-nw-p file
)
1024 org-default-notes-file
1025 (error "No notes file specified, and no default available")))
1026 (or (org-find-base-buffer-visiting file
)
1027 (progn (org-capture-put :new-buffer t
)
1028 (find-file-noselect (expand-file-name file org-directory
)))))
1030 (defun org-capture-place-template (&optional inhibit-wconf-store
)
1031 "Insert the template at the target location, and display the buffer.
1032 When `inhibit-wconf-store', don't store the window configuration, as it
1033 may have been stored before."
1034 (unless inhibit-wconf-store
1035 (org-capture-put :return-to-wconf
(current-window-configuration)))
1036 (delete-other-windows)
1037 (org-switch-to-buffer-other-window
1038 (org-capture-get-indirect-buffer (org-capture-get :buffer
) "CAPTURE"))
1041 (goto-char (org-capture-get :pos
))
1042 (setq-local outline-level
'org-outline-level
)
1043 (pcase (org-capture-get :type
)
1044 ((or `nil
`entry
) (org-capture-place-entry))
1045 (`table-line
(org-capture-place-table-line))
1046 (`plain
(org-capture-place-plain-text))
1047 (`item
(org-capture-place-item))
1048 (`checkitem
(org-capture-place-item)))
1049 (org-capture-mode 1)
1050 (setq-local org-capture-current-plist org-capture-plist
))
1052 (defun org-capture-place-entry ()
1053 "Place the template as a new Org entry."
1054 (let* ((txt (org-capture-get :template
))
1055 (reversed (org-capture-get :prepend
))
1056 (target-entry-p (org-capture-get :target-entry-p
))
1059 (and (org-capture-get :exact-position
)
1060 (goto-char (org-capture-get :exact-position
)))
1062 ((not target-entry-p
)
1063 ;; Insert as top-level entry, either at beginning or at end of
1067 (progn (goto-char (point-min))
1068 (or (org-at-heading-p)
1069 (outline-next-heading)))
1070 (goto-char (point-max))
1071 (or (bolp) (insert "\n"))))
1073 ;; Insert as a child of the current entry
1074 (and (looking-at "\\*+")
1075 (setq level
(- (match-end 0) (match-beginning 0))))
1076 (setq level
(org-get-valid-level (or level
1) 1))
1079 (outline-next-heading)
1080 (or (bolp) (insert "\n")))
1081 (org-end-of-subtree t nil
)
1082 (or (bolp) (insert "\n")))))
1083 (org-capture-empty-lines-before)
1085 (org-capture-verify-tree txt
)
1086 (org-paste-subtree level txt
'for-yank
)
1087 (org-capture-empty-lines-after 1)
1088 (org-capture-position-for-last-stored beg
)
1089 (outline-next-heading)
1091 (org-capture-mark-kill-region beg
(1- end
))
1092 (org-capture-narrow beg
(1- end
))
1093 (if (or (re-search-backward "%\\?" beg t
)
1094 (re-search-forward "%\\?" end t
))
1095 (replace-match ""))))
1097 (defun org-capture-place-item ()
1098 "Place the template as a new plain list item."
1099 (let* ((txt (org-capture-get :template
))
1100 (target-entry-p (org-capture-get :target-entry-p
))
1103 (if (org-capture-get :exact-position
)
1104 (goto-char (org-capture-get :exact-position
))
1106 ((not target-entry-p
)
1107 ;; Insert as top-level entry, either at beginning or at end of file
1108 (setq beg
(point-min) end
(point-max)))
1110 (setq beg
(1+ (point-at-eol))
1111 end
(save-excursion (outline-next-heading) (point)))))
1113 (if (org-capture-get :prepend
)
1116 (when (org-list-search-forward (org-item-beginning-re) end t
)
1117 (goto-char (match-beginning 0))
1118 (setq ind
(org-get-indentation))))
1120 (when (org-list-search-backward (org-item-beginning-re) beg t
)
1121 (setq ind
(org-get-indentation))
1123 (unless ind
(goto-char end
)))
1124 ;; Remove common indentation
1125 (setq txt
(org-remove-indentation txt
))
1126 ;; Make sure this is indeed an item
1127 (unless (string-match (concat "\\`" (org-item-re)) txt
)
1128 (setq txt
(concat "- "
1129 (mapconcat 'identity
(split-string txt
"\n")
1131 ;; Prepare surrounding empty lines.
1132 (org-capture-empty-lines-before)
1134 (unless (eolp) (save-excursion (insert "\n")))
1137 (setq ind
(org-get-indentation))
1138 (delete-region beg
(point)))
1139 ;; Set the correct indentation, depending on context
1140 (setq ind
(make-string ind ?\
))
1141 (setq txt
(concat ind
1142 (mapconcat 'identity
(split-string txt
"\n")
1147 (org-capture-empty-lines-after 1)
1148 (org-capture-position-for-last-stored beg
)
1151 (org-capture-mark-kill-region beg
(1- end
))
1152 (org-capture-narrow beg
(1- end
))
1153 (if (or (re-search-backward "%\\?" beg t
)
1154 (re-search-forward "%\\?" end t
))
1155 (replace-match ""))))
1157 (defun org-capture-place-table-line ()
1158 "Place the template as a table line."
1159 (require 'org-table
)
1160 (let* ((txt (org-capture-get :template
))
1161 (target-entry-p (org-capture-get :target-entry-p
))
1162 (table-line-pos (org-capture-get :table-line-pos
))
1165 ((org-capture-get :exact-position
)
1166 (goto-char (org-capture-get :exact-position
)))
1167 ((not target-entry-p
)
1168 ;; Table is not necessarily under a heading
1169 (setq beg
(point-min) end
(point-max)))
1171 ;; WE are at a heading, limit search to the body
1172 (setq beg
(1+ (point-at-eol))
1173 end
(save-excursion (outline-next-heading) (point)))))
1174 (if (re-search-forward org-table-dataline-regexp end t
)
1175 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t
))
1177 (if (looking-at "[ \t]*#\\+tblfm:")
1179 (narrow-to-region b
(point)))
1181 (insert "\n| |\n|----|\n| |\n")
1182 (narrow-to-region (1+ end
) (point)))
1183 ;; We are narrowed to the table, or to an empty line if there was no table
1185 ;; Check if the template is good
1186 (if (not (string-match org-table-dataline-regexp txt
))
1187 (setq txt
"| %?Bad template |\n"))
1188 (if (functionp table-line-pos
)
1189 (setq table-line-pos
(funcall table-line-pos
))
1190 (setq table-line-pos
(eval table-line-pos
)))
1192 ((and table-line-pos
1193 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos
))
1194 (goto-char (point-min))
1195 ;; we have a complex line specification
1196 (let ((ll (ignore-errors
1197 (save-match-data (org-table-analyze))
1198 (aref org-table-hlines
1199 (- (match-end 1) (match-beginning 1)))))
1200 (delta (string-to-number (match-string 2 table-line-pos
))))
1201 ;; The user wants a special position in the table
1203 (error "Invalid table line specification \"%s\"" table-line-pos
))
1204 (goto-char org-table-current-begin-pos
)
1205 (forward-line (+ ll delta
(if (< delta
0) 0 -
1)))
1206 (org-table-insert-row 'below
)
1207 (beginning-of-line 1)
1208 (delete-region (point) (1+ (point-at-eol)))
1211 (setq end
(point))))
1212 ((org-capture-get :prepend
)
1213 (goto-char (point-min))
1214 (re-search-forward org-table-hline-regexp nil t
)
1215 (beginning-of-line 1)
1216 (re-search-forward org-table-dataline-regexp nil t
)
1217 (beginning-of-line 1)
1219 (org-table-insert-row)
1220 (beginning-of-line 1)
1221 (delete-region (point) (1+ (point-at-eol)))
1225 (goto-char (point-max))
1226 (re-search-backward org-table-dataline-regexp nil t
)
1227 (beginning-of-line 1)
1228 (org-table-insert-row 'below
)
1229 (beginning-of-line 1)
1230 (delete-region (point) (1+ (point-at-eol)))
1233 (setq end
(point))))
1235 (org-capture-position-for-last-stored 'table-line
)
1236 (if (or (re-search-backward "%\\?" beg t
)
1237 (re-search-forward "%\\?" end t
))
1241 (defun org-capture-place-plain-text ()
1242 "Place the template plainly.
1243 If the target locator points at an Org node, place the template into
1244 the text of the entry, before the first child. If not, place the
1245 template at the beginning or end of the file.
1246 Of course, if exact position has been required, just put it there."
1247 (let* ((txt (org-capture-get :template
))
1250 ((org-capture-get :exact-position
)
1251 (goto-char (org-capture-get :exact-position
)))
1252 ((and (org-capture-get :target-entry-p
)
1254 (looking-at org-outline-regexp
))
1255 ;; we should place the text into this entry
1256 (if (org-capture-get :prepend
)
1257 ;; Skip meta data and drawers
1258 (org-end-of-meta-data t
)
1259 ;; go to ent of the entry text, before the next headline
1260 (outline-next-heading)))
1262 ;; beginning or end of file
1263 (goto-char (if (org-capture-get :prepend
) (point-min) (point-max)))))
1264 (or (bolp) (newline))
1265 (org-capture-empty-lines-before)
1268 (org-capture-empty-lines-after 1)
1269 (org-capture-position-for-last-stored beg
)
1271 (org-capture-mark-kill-region beg
(1- end
))
1272 (org-capture-narrow beg
(1- end
))
1273 (if (or (re-search-backward "%\\?" beg t
)
1274 (re-search-forward "%\\?" end t
))
1275 (replace-match ""))))
1277 (defun org-capture-mark-kill-region (beg end
)
1278 "Mark the region that will have to be killed when aborting capture."
1279 (let ((m1 (move-marker (make-marker) beg
))
1280 (m2 (move-marker (make-marker) end
)))
1281 (org-capture-put :begin-marker m1
)
1282 (org-capture-put :end-marker m2
)))
1284 (defun org-capture-position-for-last-stored (where)
1285 "Memorize the position that should later become the position of last capture."
1288 (org-capture-put :position-for-last-stored
1289 (move-marker (make-marker) where
1290 (or (buffer-base-buffer (current-buffer))
1291 (current-buffer)))))
1292 ((eq where
'table-line
)
1293 (org-capture-put :position-for-last-stored
1295 (org-table-current-dline))))
1296 (t (error "This should not happen"))))
1298 (defun org-capture-bookmark-last-stored-position ()
1299 "Bookmark the last-captured position."
1300 (let* ((where (org-capture-get :position-for-last-stored
'local
))
1303 (prog1 (marker-position where
)
1304 (move-marker where nil
)))
1305 ((and (listp where
) (eq (car where
) 'table-line
))
1306 (if (org-at-table-p)
1308 (org-table-goto-line (nth 1 where
))
1311 (with-current-buffer (buffer-base-buffer (current-buffer))
1312 (org-with-wide-buffer
1314 (let ((bookmark-name (plist-get org-bookmark-names-plist
1317 (with-demoted-errors
1318 (bookmark-set bookmark-name
))))
1319 (move-marker org-capture-last-stored-marker
(point))))))
1321 (defun org-capture-narrow (beg end
)
1322 "Narrow, unless configuration says not to narrow."
1323 (unless (org-capture-get :unnarrowed
)
1324 (narrow-to-region beg end
)
1327 (defun org-capture-empty-lines-before (&optional n
)
1328 "Set the correct number of empty lines before the insertion point.
1329 Point will be after the empty lines, so insertion can directly be done."
1330 (setq n
(or n
(org-capture-get :empty-lines-before
)
1331 (org-capture-get :empty-lines
) 0))
1332 (let ((pos (point)))
1333 (org-back-over-empty-lines)
1334 (delete-region (point) pos
)
1335 (if (> n
0) (newline n
))))
1337 (defun org-capture-empty-lines-after (&optional n
)
1338 "Set the correct number of empty lines after the inserted string.
1339 Point will remain at the first line after the inserted text."
1340 (setq n
(or n
(org-capture-get :empty-lines-after
)
1341 (org-capture-get :empty-lines
) 0))
1342 (org-back-over-empty-lines)
1343 (while (looking-at "[ \t]*\n") (replace-match ""))
1344 (let ((pos (point)))
1345 (if (> n
0) (newline n
))
1348 (defvar org-clock-marker
) ; Defined in org.el
1350 (defun org-capture-insert-template-here ()
1351 "Insert the capture template at point."
1352 (let* ((template (org-capture-get :template
))
1353 (type (org-capture-get :type
))
1355 (or (bolp) (newline))
1358 ((and (eq type
'entry
) (derived-mode-p 'org-mode
))
1359 (org-capture-verify-tree (org-capture-get :template
))
1360 (org-paste-subtree nil template t
))
1361 ((and (memq type
'(item checkitem
))
1362 (derived-mode-p 'org-mode
)
1363 (save-excursion (skip-chars-backward " \t\n")
1368 (skip-chars-backward " ")
1369 (skip-chars-backward "-+*0123456789).")
1370 (delete-region (point) (point-at-eol))
1372 (org-remove-indentation template
)
1374 (org-capture-empty-lines-after)
1379 (t (insert template
)))
1382 (if (re-search-forward "%\\?" end t
)
1383 (replace-match ""))))
1385 (defun org-capture-set-plist (entry)
1386 "Initialize the property list from the template definition."
1387 (setq org-capture-plist
(copy-sequence (nthcdr 5 entry
)))
1388 (org-capture-put :key
(car entry
) :description
(nth 1 entry
)
1389 :target
(nth 3 entry
))
1390 (let ((txt (nth 4 entry
)) (type (or (nth 2 entry
) 'entry
)))
1391 (when (or (not txt
) (and (stringp txt
) (not (string-match "\\S-" txt
))))
1392 ;; The template may be empty or omitted for special types.
1393 ;; Here we insert the default templates for such cases.
1395 ((eq type
'item
) (setq txt
"- %?"))
1396 ((eq type
'checkitem
) (setq txt
"- [ ] %?"))
1397 ((eq type
'table-line
) (setq txt
"| %? |"))
1398 ((member type
'(nil entry
)) (setq txt
"* %?\n %a"))))
1399 (org-capture-put :template txt
:type type
)))
1401 (defun org-capture-goto-target (&optional template-key
)
1402 "Go to the target location of a capture template.
1403 The user is queried for the template."
1405 (let ((entry (org-capture-select-template template-key
)))
1406 (unless entry
(error "No capture template selected"))
1407 (org-capture-set-plist entry
)
1408 (org-capture-set-target-location)
1409 (pop-to-buffer-same-window (org-capture-get :buffer
))
1410 (goto-char (org-capture-get :pos
))))
1412 (defun org-capture-get-indirect-buffer (&optional buffer prefix
)
1413 "Make an indirect buffer for a capture process.
1414 Use PREFIX as a prefix for the name of the indirect buffer."
1415 (setq buffer
(or buffer
(current-buffer)))
1416 (let ((n 1) (base (buffer-name buffer
)) bname
)
1417 (setq bname
(concat prefix
"-" base
))
1418 (while (buffer-live-p (get-buffer bname
))
1419 (setq bname
(concat prefix
"-" (number-to-string (cl-incf n
)) "-" base
)))
1421 (make-indirect-buffer buffer bname
'clone
)
1423 (let ((buf (make-indirect-buffer buffer bname
)))
1424 (with-current-buffer buf
(org-mode))
1427 (defun org-capture-verify-tree (tree)
1428 "Throw error if TREE is not a valid tree."
1429 (unless (org-kill-is-subtree-p tree
)
1430 (error "Template is not a valid Org entry or tree")))
1432 (defun org-mks (table title
&optional prompt specials
)
1433 "Select a member of an alist with multiple keys.
1434 TABLE is the alist which should contain entries where the car is a string.
1435 There should be two types of entries.
1437 1. prefix descriptions like (\"a\" \"Description\")
1438 This indicates that `a' is a prefix key for multi-letter selection, and
1439 that there are entries following with keys like \"ab\", \"ax\"...
1441 2. Selectable members must have more than two elements, with the first
1442 being the string of keys that lead to selecting it, and the second a
1443 short description string of the item.
1445 The command will then make a temporary buffer listing all entries
1446 that can be selected with a single key, and all the single key
1447 prefixes. When you press the key for a single-letter entry, it is selected.
1448 When you press a prefix key, the commands (and maybe further prefixes)
1449 under this key will be shown and offered for selection.
1451 TITLE will be placed over the selection in the temporary buffer,
1452 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1453 also (\"key\" \"description\") entries. When one of these is selection,
1454 only the bare key is returned."
1455 (setq prompt
(or prompt
"Select: "))
1456 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1457 current prefix rtn re pressed buffer
(inhibit-quit t
))
1458 (save-window-excursion
1459 (setq buffer
(org-switch-to-buffer-other-window "*Org Select*"))
1460 (setq orig-table table
)
1464 (insert title
"\n\n")
1469 (setq prefix
(if current
(concat current
" ") ""))
1472 ((and (= 2 (length (car tbl
))) (= (length (caar tbl
)) 1))
1473 ;; This is a description on this level
1474 (setq dkey
(caar tbl
) ddesc
(cl-cadar tbl
))
1476 (push dkey des-keys
)
1477 (push dkey allowed-keys
)
1478 (insert prefix
"[" dkey
"]" "..." " " ddesc
"..." "\n")
1479 ;; Skip keys which are below this prefix
1480 (setq re
(concat "\\`" (regexp-quote dkey
)))
1481 (let (case-fold-search)
1482 (while (and tbl
(string-match re
(caar tbl
))) (pop tbl
))))
1483 ((= 2 (length (car tbl
)))
1484 ;; Not yet a usable description, skip it
1487 ;; usable entry on this level
1488 (insert prefix
"[" (caar tbl
) "]" " " (nth 1 (car tbl
)) "\n")
1489 (push (caar tbl
) allowed-keys
)
1492 (insert "-------------------------------------------------------------------------------\n")
1493 (let ((sp specials
))
1495 (insert (format "[%s] %s\n"
1496 (caar sp
) (nth 1 (car sp
))))
1497 (push (caar sp
) allowed-keys
)
1499 (push "\C-g" allowed-keys
)
1500 (goto-char (point-min))
1501 (if (not (pos-visible-in-window-p (point-max)))
1502 (org-fit-window-to-buffer))
1504 (setq 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 (when (equal pressed
"\C-g")
1510 (kill-buffer buffer
)
1511 (user-error "Abort"))
1512 (when (and (not (assoc pressed table
))
1513 (not (member pressed des-keys
))
1514 (assoc pressed specials
))
1515 (throw 'exit
(setq rtn pressed
)))
1516 (unless (member pressed des-keys
)
1517 (throw 'exit
(setq rtn
(rassoc (cdr (assoc pressed table
))
1519 (setq current
(concat current pressed
))
1522 (if (and (> (length (car x
)) 1)
1523 (equal (substring (car x
) 0 1) pressed
))
1524 (cons (substring (car x
) 1) (cdr x
))
1527 (setq table
(remove nil table
)))))
1528 (when buffer
(kill-buffer buffer
))
1531 ;;; The template code
1532 (defun org-capture-select-template (&optional keys
)
1533 "Select a capture template.
1534 Lisp programs can force the template by setting KEYS to a string."
1535 (let ((org-capture-templates
1536 (or (org-contextualize-keys
1537 org-capture-templates org-capture-templates-contexts
)
1538 '(("t" "Task" entry
(file+headline
"" "Tasks")
1539 "* TODO %?\n %u\n %a")))))
1541 (or (assoc keys org-capture-templates
)
1542 (error "No capture template referred to by \"%s\" keys" keys
))
1543 (org-mks org-capture-templates
1544 "Select a capture template\n========================="
1546 '(("C" "Customize org-capture-templates")
1549 (defun org-capture-fill-template (&optional template initial annotation
)
1550 "Fill a template and return the filled template as a string.
1551 The template may still contain \"%?\" for cursor positioning."
1552 (let* ((template (or template
(org-capture-get :template
)))
1553 (buffer (org-capture-get :buffer
))
1554 (file (buffer-file-name (or (buffer-base-buffer buffer
) buffer
)))
1555 (time (let* ((c (or (org-capture-get :default-time
) (current-time)))
1556 (d (decode-time c
)))
1557 (if (< (nth 2 d
) org-extend-today-until
)
1558 (encode-time 0 59 23 (1- (nth 3 d
)) (nth 4 d
) (nth 5 d
))
1560 (v-t (format-time-string (org-time-stamp-format nil
) time
))
1561 (v-T (format-time-string (org-time-stamp-format t
) time
))
1562 (v-u (format-time-string (org-time-stamp-format nil t
) time
))
1563 (v-U (format-time-string (org-time-stamp-format t t
) time
))
1564 (v-c (and kill-ring
(current-kill 0)))
1565 (v-x (or (org-get-x-clipboard 'PRIMARY
)
1566 (org-get-x-clipboard 'CLIPBOARD
)
1567 (org-get-x-clipboard 'SECONDARY
)))
1568 ;; `initial' and `annotation' might have been passed. But if
1569 ;; the property list has them, we prefer those values.
1570 (v-i (or (plist-get org-store-link-plist
:initial
)
1571 (and (stringp initial
) (org-no-properties initial
))
1572 (org-capture-get :initial
)
1575 (let ((a (or (plist-get org-store-link-plist
:annotation
)
1577 (org-capture-get :annotation
)
1579 ;; Is the link empty? Then we do not want it...
1580 (if (equal a
"[[]]") "" a
)))
1581 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1582 (v-A (if (and v-a
(string-match l-re v-a
))
1583 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a
)
1585 (v-l (if (and v-a
(string-match l-re v-a
))
1586 (replace-match "\\1" nil nil v-a
)
1588 (v-n user-full-name
)
1589 (v-k (and (marker-buffer org-clock-marker
)
1590 (org-no-properties org-clock-heading
)))
1591 (v-K (if (marker-buffer org-clock-marker
)
1592 (org-make-link-string
1593 (buffer-file-name (marker-buffer org-clock-marker
))
1594 org-clock-heading
)))
1595 (v-f (or (org-capture-get :original-file-nondirectory
) ""))
1596 (v-F (or (org-capture-get :original-file
) ""))
1597 (clipboards (delq nil
1599 (org-get-x-clipboard 'PRIMARY
)
1600 (org-get-x-clipboard 'CLIPBOARD
)
1601 (org-get-x-clipboard 'SECONDARY
)
1604 (setq org-store-link-plist
(plist-put org-store-link-plist
:annotation v-a
))
1605 (setq org-store-link-plist
(plist-put org-store-link-plist
:initial v-i
))
1609 (message "no template") (ding)
1611 (save-window-excursion
1612 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1614 (setq buffer-file-name nil
)
1615 (setq mark-active nil
)
1617 (goto-char (point-min))
1619 ;; %[] insert contents of a file.
1621 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t
)
1622 (let ((filename (expand-file-name (match-string 1)))
1623 (beg (copy-marker (match-beginning 0)))
1624 (end (copy-marker (match-end 0))))
1625 (unless (org-capture-escaped-%
)
1626 (delete-region beg end
)
1627 (set-marker beg nil
)
1628 (set-marker end nil
)
1629 (condition-case error
1630 (insert-file-contents filename
)
1632 (insert (format "%%![couldn not insert %s: %s]"
1636 ;; Mark %() embedded elisp for later evaluation.
1637 (org-capture-expand-embedded-elisp 'mark
)
1639 ;; Expand non-interactive templates.
1640 (let ((regexp "%\\(:[-a-za-z]+\\|<\\([^>\n]+\\)>\\|[aAcfFikKlntTuUx]\\)"))
1642 (while (re-search-forward regexp nil t
)
1643 ;; `org-capture-escaped-%' may modify buffer and cripple
1644 ;; match-data. Use markers instead. Ditto for other
1646 (let ((pos (copy-marker (match-beginning 0)))
1647 (end (copy-marker (match-end 0)))
1648 (value (match-string 1))
1649 (time-string (match-string 2)))
1650 (unless (org-capture-escaped-%
)
1651 (delete-region pos end
)
1652 (set-marker pos nil
)
1653 (set-marker end nil
)
1655 (pcase (string-to-char value
)
1656 (?
< (format-time-string time-string
))
1658 (or (plist-get org-store-link-plist
(intern value
))
1660 (?i
(let ((lead (buffer-substring-no-properties
1661 (line-beginning-position) (point))))
1662 (mapconcat #'identity
1663 (split-string v-i
"\n")
1664 (concat "\n" lead
))))
1680 (if (org-capture-inside-embedded-elisp-p)
1681 (replace-regexp-in-string "\"" "\\\\\"" replacement
)
1684 ;; Expand %() embedded Elisp. Limit to Sexp originally marked.
1685 (org-capture-expand-embedded-elisp)
1687 ;; Expand interactive templates. This is the last step so that
1688 ;; template is mostly expanded when prompting happens. Turn on
1689 ;; Org mode and set local variables. This is to support
1690 ;; completion in interactive prompts.
1691 (let ((org-inhibit-startup t
)) (org-mode))
1692 (org-clone-local-variables buffer
"\\`org-")
1693 (let (strings) ; Stores interactive answers.
1695 (let ((regexp "%\\^\\(?:{\\([^}]*\\)}\\)?\\([CgGLptTuU]\\)?"))
1696 (while (re-search-forward regexp nil t
)
1697 (let* ((items (and (match-end 1)
1699 (split-string (match-string-no-properties 1)
1701 (key (match-string 2))
1702 (beg (copy-marker (match-beginning 0)))
1703 (end (copy-marker (match-end 0)))
1704 (prompt (nth 0 items
))
1705 (default (nth 1 items
))
1706 (completions (nthcdr 2 items
)))
1707 (unless (org-capture-escaped-%
)
1708 (delete-region beg end
)
1709 (set-marker beg nil
)
1710 (set-marker end nil
)
1713 (let* ((org-last-tags-completion-table
1714 (org-global-tags-completion-table
1715 (cond ((equal key
"G") (org-agenda-files))
1718 (org-add-colon-after-tag-completion t
)
1723 (if prompt
(concat prompt
": ") "Tags: ")
1724 'org-tags-completion-function nil nil nil
1726 "[^[:alnum:]_@#%]+")
1728 (when (org-string-nw-p ins
)
1729 (unless (eq (char-before) ?
:) (insert ":"))
1731 (unless (eq (char-after) ?
:) (insert ":"))
1732 (and (org-at-heading-p)
1733 (let ((org-ignore-region t
))
1734 (org-set-tags nil
'align
))))))
1737 ((= (length clipboards
) 1) (insert (car clipboards
)))
1738 ((> (length clipboards
) 1)
1739 (insert (read-string "Clipboard/kill value: "
1742 (car clipboards
))))))
1744 (cond ((= (length clipboards
) 1)
1745 (org-insert-link 0 (car clipboards
)))
1746 ((> (length clipboards
) 1)
1749 (read-string "Clipboard/kill value: "
1752 (car clipboards
))))))
1753 ("p" (org-set-property prompt nil
))
1755 ;; These are the date/time related ones.
1756 (let* ((upcase?
(equal (upcase key
) key
))
1757 (org-time-was-given upcase?
)
1758 (org-end-time-was-given)
1759 (time (org-read-date upcase? t nil prompt
)))
1760 (org-insert-time-stamp
1761 time org-time-was-given
1762 (member key
'("u" "U"))
1763 nil nil
(list org-end-time-was-given
))))
1765 (push (org-completing-read
1766 (concat (or prompt
"Enter string")
1767 (and default
(format " [%s]" default
))
1769 completions nil nil nil nil default
)
1771 (insert (car strings
)))))))))
1773 ;; Replace %n escapes with nth %^{...} string.
1774 (setq strings
(nreverse strings
))
1776 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t
)
1777 (unless (org-capture-escaped-%
)
1779 (nth (1- (string-to-number (match-string 1))) strings
)
1782 ;; Make sure there are no empty lines before the text, and that
1783 ;; it ends with a newline character.
1784 (skip-chars-forward " \t\n")
1785 (delete-region (point-min) (line-beginning-position))
1786 (goto-char (point-max))
1787 (skip-chars-backward " \t\n")
1788 (delete-region (point) (point-max))
1791 ;; Return the expanded template and kill the capture buffer.
1792 (untabify (point-min) (point-max))
1793 (set-buffer-modified-p nil
)
1794 (prog1 (buffer-substring-no-properties (point-min) (point-max))
1795 (kill-buffer (current-buffer))))))
1797 (defun org-capture-escaped-%
()
1798 "Non-nil if % was escaped.
1799 If yes, unescape it now. Assume match-data contains the
1800 placeholder to check."
1802 (goto-char (match-beginning 0))
1803 (let ((n (abs (skip-chars-backward "\\\\"))))
1804 (delete-char (/ (1+ n
) 2))
1807 (defun org-capture-expand-embedded-elisp (&optional mark
)
1808 "Evaluate embedded elisp %(sexp) and replace with the result.
1809 When optional MARK argument is non-nil, mark Sexp with a text
1810 property (`org-embedded-elisp') for later evaluation. Only
1811 marked Sexp are evaluated when this argument is nil."
1813 (goto-char (point-min))
1814 (while (re-search-forward "%(" nil t
)
1816 ((get-text-property (match-beginning 0) 'org-embedded-elisp
)
1817 (goto-char (match-beginning 0))
1818 (let ((template-start (point)))
1820 (let* ((sexp (read (current-buffer)))
1822 (org-capture--expand-keyword-in-embedded-elisp
1824 (delete-region template-start
(point))
1827 ((stringp result
) (insert result
))
1829 "Capture template sexp `%s' must evaluate to string or nil"
1832 ;; Only mark valid and non-escaped sexp.
1833 ((org-capture-escaped-%
) nil
)
1835 (let ((end (with-syntax-table emacs-lisp-mode-syntax-table
1836 (ignore-errors (scan-sexps (1- (point)) 1)))))
1838 (put-text-property (- (point) 2) end
'org-embedded-elisp t
))))))))
1840 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1841 "Recursively replace capture link keywords in ATTR sexp.
1842 Such keywords are prefixed with \"%:\". See
1843 `org-capture-template' for more information."
1845 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr
))
1847 (let* ((attr-symbol (symbol-name attr
))
1848 (key (and (string-match "%\\(:.*\\)" attr-symbol
)
1849 (intern (match-string 1 attr-symbol
)))))
1850 (or (plist-get org-store-link-plist key
)
1854 (defun org-capture-inside-embedded-elisp-p ()
1855 "Non-nil if point is inside of embedded elisp %(sexp).
1856 Assume sexps have been marked with
1857 `org-capture-expand-embedded-elisp' beforehand."
1858 (get-text-property (point) 'org-embedded-elisp
))
1861 (defun org-capture-import-remember-templates ()
1862 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1864 (when (and (yes-or-no-p
1865 "Import old remember templates into org-capture-templates? ")
1867 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1868 (require 'org-remember
)
1869 (setq org-capture-templates
1872 (let ((desc (car entry
))
1873 (key (char-to-string (nth 1 entry
)))
1874 (template (nth 2 entry
))
1875 (file (or (nth 3 entry
) org-default-notes-file
))
1876 (position (or (nth 4 entry
) org-remember-default-headline
))
1878 (prepend org-reverse-note-order
)
1879 immediate target jump-to-captured
)
1881 ((member position
'(top bottom
))
1882 (setq target
(list 'file file
)
1883 prepend
(eq position
'top
)))
1884 ((eq position
'date-tree
)
1885 (setq target
(list 'file
+datetree file
)
1887 (t (setq target
(list 'file
+headline file position
))))
1889 (when (string-match "%!" template
)
1890 (setq template
(replace-match "" t t template
)
1893 (when (string-match "%&" template
)
1894 (setq jump-to-captured t
))
1896 (append (list key desc type target template
)
1897 (if prepend
'(:prepend t
))
1898 (if immediate
'(:immediate-finish t
))
1899 (if jump-to-captured
'(:jump-to-captured t
)))))
1901 org-remember-templates
))))
1902 ;;; The function was made obsolete by commit 65399674d5 of
1903 ;;; 2013-02-22. This make-obsolete call was added 2016-09-01.
1904 (make-obsolete 'org-capture-import-remember-templates
"use the `org-capture-templates' variable instead." "Org 9.0")
1906 (provide 'org-capture
)
1908 ;;; org-capture.el ends here