1 ;;; org-capture.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2010-2015 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?
54 (declare-function org-datetree-find-date-create
"org-datetree"
55 (date &optional keep-restriction
))
56 (declare-function org-table-analyze
"org-table" ())
57 (declare-function org-table-goto-line
"org-table" (N))
58 (declare-function org-pop-to-buffer-same-window
"org-compat"
59 (&optional buffer-or-name norecord label
))
60 (declare-function org-at-encrypted-entry-p
"org-crypt" ())
61 (declare-function org-encrypt-entry
"org-crypt" ())
62 (declare-function org-decrypt-entry
"org-crypt" ())
64 (defvar org-remember-default-headline
)
65 (defvar org-remember-templates
)
66 (defvar org-table-hlines
)
67 (defvar org-table-current-begin-pos
)
68 (defvar dired-buffers
)
70 (defvar org-capture-clock-was-started nil
71 "Internal flag, noting if the clock was started.")
73 (defvar org-capture-last-stored-marker
(make-marker)
74 "Marker pointing to the entry most recently stored with `org-capture'.")
76 ;; The following variable is scoped dynamically by org-protocol
77 ;; to indicate that the link properties have already been stored
78 (defvar org-capture-link-is-already-stored nil
)
80 (defgroup org-capture nil
81 "Options concerning capturing new entries."
85 (defcustom org-capture-templates nil
86 "Templates for the creation of new entries.
88 Each entry is a list with the following items:
90 keys The keys that will select the template, as a string, characters
91 only, for example \"a\" for a template to be selected with a
92 single key, or \"bt\" for selection with two keys. When using
93 several keys, keys using the same prefix key must be together
94 in the list and preceded by a 2-element entry explaining the
95 prefix key, for example
97 (\"b\" \"Templates for marking stuff to buy\")
99 The \"C\" key is used by default for quick access to the
100 customization of the template variable. But if you want to use
101 that key for a template, you can.
103 description A short string describing the template, will be shown during
106 type The type of entry. Valid types are:
107 entry an Org-mode node, with a headline. Will be
108 filed as the child of the target entry or as
110 item a plain list item, will be placed in the
111 first plain list at the target
113 checkitem a checkbox item. This differs from the
114 plain list item only is so far as it uses a
115 different default template.
116 table-line a new line in the first table at target location.
117 plain text to be inserted as it is.
119 target Specification of where the captured item should be placed.
120 In Org-mode files, targets usually define a node. Entries will
121 become children of this node, other types will be added to the
122 table or list in the body of this node.
124 Most target specifications contain a file name. If that file
125 name is the empty string, it defaults to `org-default-notes-file'.
126 A file can also be given as a variable, function, or Emacs Lisp
131 (file \"path/to/file\")
132 Text will be placed at the beginning or end of that file
134 (id \"id of existing org entry\")
135 File as child of this entry, or in the body of the entry
137 (file+headline \"path/to/file\" \"node headline\")
138 Fast configuration if the target heading is unique in the file
140 (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
141 For non-unique headings, the full path is safer
143 (file+regexp \"path/to/file\" \"regexp to find location\")
144 File to the entry matching regexp
146 (file+datetree \"path/to/file\")
147 Will create a heading in a date tree for today's date
149 (file+datetree+prompt \"path/to/file\")
150 Will create a heading in a date tree, prompts for date
152 (file+function \"path/to/file\" function-finding-location)
153 A function to find the right location in the file
156 File to the entry that is currently being clocked
158 (function function-finding-location)
159 Most general way, write your own function to find both
162 template The template for creating the capture item. If you leave this
163 empty, an appropriate default template will be used. See below
164 for more details. Instead of a string, this may also be one of
166 (file \"/path/to/template-file\")
167 (function function-returning-the-template)
169 in order to get a template from a file, or dynamically
172 The rest of the entry is a property list of additional options. Recognized
175 :prepend Normally newly captured information will be appended at
176 the target location (last child, last table line,
177 last list item...). Setting this property will
180 :immediate-finish When set, do not offer to edit the information, just
181 file it away immediately. This makes sense if the
182 template only needs information that can be added
185 :jump-to-captured When set, jump to the captured entry when finished.
187 :empty-lines Set this to the number of lines the should be inserted
188 before and after the new item. Default 0, only common
191 :empty-lines-before Set this to the number of lines the should be inserted
192 before the new item. Overrides :empty-lines for the
193 number lines inserted before.
195 :empty-lines-after Set this to the number of lines the should be inserted
196 after the new item. Overrides :empty-lines for the
197 number of lines inserted after.
199 :clock-in Start the clock in this item.
201 :clock-keep Keep the clock running when filing the captured entry.
203 :clock-resume Start the interrupted clock when finishing the capture.
204 Note that :clock-keep has precedence over :clock-resume.
205 When setting both to t, the current clock will run and
206 the previous one will not be resumed.
208 :unnarrowed Do not narrow the target buffer, simply show the
209 full buffer. Default is to narrow it so that you
210 only see the new stuff.
212 :table-line-pos Specification of the location in the table where the
213 new line should be inserted. It should be a string like
214 \"II-3\", meaning that the new line should become the
215 third line before the second horizontal separator line.
217 :kill-buffer If the target file was not yet visited by a buffer when
218 capture was invoked, kill the buffer again after capture
221 The template defines the text to be inserted. Often this is an
222 Org mode entry (so the first line should start with a star) that
223 will be filed as a child of the target headline. It can also be
224 freely formatted text. Furthermore, the following %-escapes will
225 be replaced with content and expanded:
227 %[pathname] Insert the contents of the file given by
228 `pathname'. These placeholders are expanded at the very
229 beginning of the process so they can be used to extend the
231 %(sexp) Evaluate elisp `(sexp)' and replace it with the results.
232 Only placeholders pre-existing within the template, or
233 introduced with %[pathname] are expanded this way. Since this
234 happens very late in the process, other %-escapes can be used to
236 %<...> The result of format-time-string on the ... format specification.
237 %t Time stamp, date only.
238 %T Time stamp with date and time.
239 %u, %U Like the above, but inactive time stamps.
240 %i Initial content, copied from the active region. If %i is
241 indented, the entire inserted text will be indented as well.
242 %a Annotation, normally the link created with `org-store-link'.
243 %A Like %a, but prompt for the description part.
244 %l Like %a, but only insert the literal link.
245 %c Current kill ring head.
246 %x Content of the X clipboard.
247 %k Title of currently clocked task.
248 %K Link to currently clocked task.
249 %n User name (taken from the variable `user-full-name').
250 %f File visited by current buffer when org-capture was called.
251 %F Full path of the file or directory visited by current buffer.
252 %:keyword Specific information for certain link types, see below.
253 %^g Prompt for tags, with completion on tags in target file.
254 %^G Prompt for tags, with completion on all tags in all agenda files.
255 %^t Like %t, but prompt for date. Similarly %^T, %^u, %^U.
256 You may define a prompt like: %^{Please specify birthday}t
257 %^C Interactive selection of which kill or clip to use.
258 %^L Like %^C, but insert as link.
259 %^{prop}p Prompt the user for a value for property `prop'.
260 %^{prompt} Prompt the user for a string and replace this sequence with it.
261 A default value and a completion table ca be specified like this:
262 %^{prompt|default|completion2|completion3|...}.
263 %? After completing the template, position cursor here.
264 %number Insert the text entered at the nth %^{prompt}, where `number' is a
265 number, starting from 1. These placeholders are expanded as the
266 last step of the process.
268 Apart from these general escapes, you can access information specific to
269 the link type that is created. For example, calling `org-capture' in emails
270 or in Gnus will record the author and the subject of the message, which you
271 can access with \"%:from\" and \"%:subject\", respectively. Here is a
272 complete list of what is recorded for each link type.
274 Link type | Available information
275 ------------------------+------------------------------------------------------
276 bbdb | %:type %:name %:company
277 vm, wl, mh, mew, rmail, | %:type %:subject %:message-id
278 gnus | %:from %:fromname %:fromaddress
279 | %:to %:toname %:toaddress
280 | %:fromto (either \"to NAME\" or \"from NAME\")
281 | %:date %:date-timestamp (as active timestamp)
282 | %:date-timestamp-inactive (as inactive timestamp)
283 gnus | %:group, for messages also all email fields
284 w3, w3m | %:type %:url
285 info | %:type %:file %:node
286 calendar | %:type %:date
288 When you need to insert a literal percent sign in the template,
289 you can escape ambiguous cases with a backward slash, e.g., \\%i."
294 (choice :value
("" "" entry
(file "~/org/notes.org") "")
295 (list :tag
"Multikey description"
296 (string :tag
"Keys ")
297 (string :tag
"Description"))
298 (list :tag
"Template entry"
299 (string :tag
"Keys ")
300 (string :tag
"Description ")
301 (choice :tag
"Capture Type " :value entry
302 (const :tag
"Org entry" entry
)
303 (const :tag
"Plain list item" item
)
304 (const :tag
"Checkbox item" checkitem
)
305 (const :tag
"Plain text" plain
)
306 (const :tag
"Table line" table-line
))
307 (choice :tag
"Target location"
309 (const :format
"" file
)
312 (const :format
"" id
)
314 (list :tag
"File & Headline"
315 (const :format
"" file
+headline
)
317 (string :tag
" Headline"))
318 (list :tag
"File & Outline path"
319 (const :format
"" file
+olp
)
321 (repeat :tag
"Outline path" :inline t
322 (string :tag
"Headline")))
323 (list :tag
"File & Regexp"
324 (const :format
"" file
+regexp
)
326 (regexp :tag
" Regexp"))
327 (list :tag
"File & Date tree"
328 (const :format
"" file
+datetree
)
330 (list :tag
"File & Date tree, prompt for date"
331 (const :format
"" file
+datetree
+prompt
)
333 (list :tag
"File & function"
334 (const :format
"" file
+function
)
336 (sexp :tag
" Function"))
337 (list :tag
"Current clocking task"
338 (const :format
"" clock
))
339 (list :tag
"Function"
340 (const :format
"" function
)
341 (sexp :tag
" Function")))
342 (choice :tag
"Template"
345 (const :format
"" file
)
346 (file :tag
"Template file"))
347 (list :tag
"Function"
348 (const :format
"" function
)
349 (function :tag
"Template function")))
351 ;; Give the most common options as checkboxes
352 :options
(((const :format
"%v " :prepend
) (const t
))
353 ((const :format
"%v " :immediate-finish
) (const t
))
354 ((const :format
"%v " :jump-to-captured
) (const t
))
355 ((const :format
"%v " :empty-lines
) (const 1))
356 ((const :format
"%v " :empty-lines-before
) (const 1))
357 ((const :format
"%v " :empty-lines-after
) (const 1))
358 ((const :format
"%v " :clock-in
) (const t
))
359 ((const :format
"%v " :clock-keep
) (const t
))
360 ((const :format
"%v " :clock-resume
) (const t
))
361 ((const :format
"%v " :unnarrowed
) (const t
))
362 ((const :format
"%v " :table-line-pos
) (const t
))
363 ((const :format
"%v " :kill-buffer
) (const t
))))))))
365 (defcustom org-capture-before-finalize-hook nil
366 "Hook that is run right before a capture process is finalized.
367 The capture buffer is still current when this hook runs and it is
368 widened to the entire buffer."
373 (defcustom org-capture-after-finalize-hook nil
374 "Hook that is run right after a capture process is finalized.
375 Suitable for window cleanup."
380 (defcustom org-capture-prepare-finalize-hook nil
381 "Hook that is run before the finalization starts.
382 The capture buffer is current and still narrowed."
387 (defcustom org-capture-bookmark t
388 "When non-nil, add a bookmark pointing at the last stored
389 position when capturing."
394 ;;; The property list for keeping information about the capture process
396 (defvar org-capture-plist nil
397 "Plist for the current capture process, global, to avoid having to pass it.")
399 (defvar org-capture-current-plist nil
400 "Local variable holding the plist in a capture buffer.
401 This is used to store the plist for use when finishing a capture process
402 because another such process might have changed the global variable by then.
404 Each time a new capture buffer has been set up, the global `org-capture-plist'
405 is copied to this variable, which is local in the indirect buffer.")
407 (defvar org-capture-clock-keep nil
408 "Local variable to store the value of the :clock-keep parameter.
409 This is needed in case org-capture-finalize is called interactively.")
411 (defun org-capture-put (&rest stuff
)
412 "Add properties to the capture property list `org-capture-plist'."
414 (setq org-capture-plist
(plist-put org-capture-plist
415 (pop stuff
) (pop stuff
)))))
416 (defun org-capture-get (prop &optional local
)
417 "Get properties from the capture property list `org-capture-plist'.
418 When LOCAL is set, use the local variable `org-capture-current-plist',
419 this is necessary after initialization of the capture process,
420 to avoid conflicts with other active capture processes."
421 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
423 (defun org-capture-member (prop &optional local
)
424 "Is PROP a property in `org-capture-plist'.
425 When LOCAL is set, use the local variable `org-capture-current-plist',
426 this is necessary after initialization of the capture process,
427 to avoid conflicts with other active capture processes."
428 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
432 (defvar org-capture-mode-map
(make-sparse-keymap)
433 "Keymap for `org-capture-mode', a minor mode.
434 Use this map to set additional keybindings for when Org-mode is used
435 for a capture buffer.")
437 (defvar org-capture-mode-hook nil
438 "Hook for the minor `org-capture-mode'.")
440 (define-minor-mode org-capture-mode
441 "Minor mode for special key bindings in a capture buffer.
443 Turning on this mode runs the normal hook `org-capture-mode-hook'."
444 nil
" Rem" org-capture-mode-map
447 (substitute-command-keys
448 "\\<org-capture-mode-map>Capture buffer. Finish \\[org-capture-finalize], \
449 refile \\[org-capture-refile], abort \\[org-capture-kill].")))
450 (define-key org-capture-mode-map
"\C-c\C-c" 'org-capture-finalize
)
451 (define-key org-capture-mode-map
"\C-c\C-k" 'org-capture-kill
)
452 (define-key org-capture-mode-map
"\C-c\C-w" 'org-capture-refile
)
454 ;;; The main commands
456 (defvar org-capture-initial nil
)
457 (defvar org-capture-entry nil
)
460 (defun org-capture-string (string &optional keys
)
461 "Capture STRING with the template selected by KEYS."
462 (interactive "sInitial text: \n")
463 (let ((org-capture-initial string
)
464 (org-capture-entry (org-capture-select-template keys
)))
467 (defcustom org-capture-templates-contexts nil
468 "Alist of capture templates and valid contexts.
470 For example, if you have a capture template \"c\" and you want
471 this template to be accessible only from `message-mode' buffers,
474 \\='((\"c\" ((in-mode . \"message-mode\"))))
476 Here are the available contexts definitions:
478 in-file: command displayed only in matching files
479 in-mode: command displayed only in matching modes
480 not-in-file: command not displayed in matching files
481 not-in-mode: command not displayed in matching modes
482 in-buffer: command displayed only in matching buffers
483 not-in-buffer: command not displayed in matching buffers
484 [function]: a custom function taking no argument
486 If you define several checks, the agenda command will be
487 accessible if there is at least one valid check.
489 You can also bind a key to another agenda custom command
490 depending on contextual rules.
492 \\='((\"c\" \"d\" ((in-mode . \"message-mode\"))))
494 Here it means: in `message-mode buffers', use \"c\" as the
495 key for the capture template otherwise associated with \"d\".
496 \(The template originally associated with \"d\" is not displayed
497 to avoid duplicates.)"
500 :type
'(repeat (list :tag
"Rule"
501 (string :tag
" Capture key")
502 (string :tag
"Replace by template")
503 (repeat :tag
"Available when"
505 (cons :tag
"Condition"
507 (const :tag
"In file" in-file
)
508 (const :tag
"Not in file" not-in-file
)
509 (const :tag
"In buffer" in-buffer
)
510 (const :tag
"Not in buffer" not-in-buffer
)
511 (const :tag
"In mode" in-mode
)
512 (const :tag
"Not in mode" not-in-mode
))
514 (function :tag
"Custom function"))))))
516 (defcustom org-capture-use-agenda-date nil
517 "Non-nil means use the date at point when capturing from agendas.
518 When nil, you can still capture using the date at point with \\[org-agenda-capture]."
524 (defun org-capture (&optional goto keys
)
526 \\<org-capture-mode-map>
527 This will let you select a template from `org-capture-templates', and then
528 file the newly captured information. The text is immediately inserted
529 at the target location, and an indirect buffer is shown where you can
530 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
531 of Emacs, so that you can continue your work.
533 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
534 anything, just go to the file/headline where the selected template
535 stores its notes. With a double prefix argument \
536 \\[universal-argument] \\[universal-argument], go to the last note
539 When called with a `C-0' (zero) prefix, insert a template at point.
541 ELisp programs can set KEYS to a string associated with a template
542 in `org-capture-templates'. In this case, interactive selection
545 If `org-capture-use-agenda-date' is non-nil, capturing from the
546 agenda will use the date at point as the default date. Then, a
547 `C-1' prefix will tell the capture process to use the HH:MM time
548 of the day at point (if any) or the current HH:MM time."
550 (when (and org-capture-use-agenda-date
551 (eq major-mode
'org-agenda-mode
))
552 (setq org-overriding-default-time
553 (org-get-cursor-date (equal goto
1))))
555 ((equal goto
'(4)) (org-capture-goto-target))
556 ((equal goto
'(16)) (org-capture-goto-last-stored))
558 ;; FIXME: Are these needed?
559 (let* ((orig-buf (current-buffer))
560 (annotation (if (and (boundp 'org-capture-link-is-already-stored
)
561 org-capture-link-is-already-stored
)
562 (plist-get org-store-link-plist
:annotation
)
563 (ignore-errors (org-store-link nil
))))
564 (entry (or org-capture-entry
(org-capture-select-template keys
)))
566 (setq initial
(or org-capture-initial
567 (and (org-region-active-p)
568 (buffer-substring (point) (mark)))))
569 (when (stringp initial
)
570 (remove-text-properties 0 (length initial
) '(read-only t
) initial
))
571 (when (stringp annotation
)
572 (remove-text-properties 0 (length annotation
)
573 '(read-only t
) annotation
))
576 (customize-variable 'org-capture-templates
))
580 (org-capture-set-plist entry
)
581 (org-capture-get-template)
582 (org-capture-put :original-buffer orig-buf
583 :original-file
(or (buffer-file-name orig-buf
)
584 (and (featurep 'dired
)
587 :original-file-nondirectory
588 (and (buffer-file-name orig-buf
)
589 (file-name-nondirectory
590 (buffer-file-name orig-buf
)))
591 :annotation annotation
593 :return-to-wconf
(current-window-configuration)
595 (or org-overriding-default-time
597 (org-capture-set-target-location)
598 (condition-case error
599 (org-capture-put :template
(org-capture-fill-template))
601 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
602 (error "Capture abort: %s" error
)))
604 (setq org-capture-clock-keep
(org-capture-get :clock-keep
))
607 (org-capture-insert-template-here)
608 (condition-case error
609 (org-capture-place-template
610 (equal (car (org-capture-get :target
)) 'function
))
612 (if (and (buffer-base-buffer (current-buffer))
613 (string-match "\\`CAPTURE-" (buffer-name)))
614 (kill-buffer (current-buffer)))
615 (set-window-configuration (org-capture-get :return-to-wconf
))
616 (error "Capture template `%s': %s"
617 (org-capture-get :key
)
619 (if (and (derived-mode-p 'org-mode
)
620 (org-capture-get :clock-in
))
623 (if (org-clock-is-active)
624 (org-capture-put :interrupted-clock
625 (copy-marker org-clock-marker
)))
627 (setq-local org-capture-clock-was-started t
))
629 "Could not start the clock in this capture buffer")))
630 (if (org-capture-get :immediate-finish
)
631 (org-capture-finalize)))))))))
633 (defun org-capture-get-template ()
634 "Get the template from a file or a function if necessary."
635 (let ((txt (org-capture-get :template
)) file
)
637 ((and (listp txt
) (eq (car txt
) 'file
))
639 (setq file
(expand-file-name (nth 1 txt
) org-directory
)))
640 (setq txt
(org-file-contents file
))
641 (setq txt
(format "* Template file %s not found" (nth 1 txt
)))))
642 ((and (listp txt
) (eq (car txt
) 'function
))
643 (if (fboundp (nth 1 txt
))
644 (setq txt
(funcall (nth 1 txt
)))
645 (setq txt
(format "* Template function %s not found" (nth 1 txt
)))))
646 ((not txt
) (setq txt
""))
648 (t (setq txt
"* Invalid capture template")))
649 (org-capture-put :template txt
)))
651 (defun org-capture-finalize (&optional stay-with-capture
)
652 "Finalize the capture process.
653 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
654 captured item after finalizing."
656 (when (org-capture-get :jump-to-captured
)
657 (setq stay-with-capture t
))
658 (unless (and org-capture-mode
659 (buffer-base-buffer (current-buffer)))
660 (error "This does not seem to be a capture buffer for Org-mode"))
662 (run-hooks 'org-capture-prepare-finalize-hook
)
664 ;; Did we start the clock in this capture buffer?
665 (when (and org-capture-clock-was-started
666 org-clock-marker
(marker-buffer org-clock-marker
)
667 (equal (marker-buffer org-clock-marker
) (buffer-base-buffer))
668 (> org-clock-marker
(point-min))
669 (< org-clock-marker
(point-max)))
670 ;; Looks like the clock we started is still running. Clock out.
671 (when (not org-capture-clock-keep
) (let (org-log-note-clock-out) (org-clock-out)))
672 (when (and (not org-capture-clock-keep
)
673 (org-capture-get :clock-resume
'local
)
674 (markerp (org-capture-get :interrupted-clock
'local
))
675 (buffer-live-p (marker-buffer
676 (org-capture-get :interrupted-clock
'local
))))
677 (let ((clock-in-task (org-capture-get :interrupted-clock
'local
)))
678 (org-with-point-at clock-in-task
680 (message "Interrupted clock has been resumed")))
682 (let ((beg (point-min))
685 ;; Store the size of the capture buffer
686 (org-capture-put :captured-entry-size
(- (point-max) (point-min)))
688 ;; Store the insertion point in the target buffer
689 (org-capture-put :insertion-point
(point))
692 (let ((m1 (org-capture-get :begin-marker
'local
))
693 (m2 (org-capture-get :end-marker
'local
)))
694 (if (and m1 m2
(= m1 beg
) (= m2 end
))
696 (setq m2
(if (cdr (assoc 'heading org-blank-before-new-entry
))
698 m2
(if (< (point-max) m2
) (point-max) m2
))
699 (setq abort-note
'clean
)
701 (setq abort-note
'dirty
)))
703 ;; Make sure that the empty lines after are correct
704 (when (and (> (point-max) end
) ; indeed, the buffer was still narrowed
705 (member (org-capture-get :type
'local
)
706 '(entry item checkitem plain
)))
709 (or (bolp) (newline))
710 (org-capture-empty-lines-after
711 (or (org-capture-get :empty-lines-after
'local
)
712 (org-capture-get :empty-lines
'local
) 0))))
713 ;; Postprocessing: Update Statistics cookies, do the sorting
714 (when (derived-mode-p 'org-mode
)
716 (when (ignore-errors (org-back-to-heading))
717 (org-update-parent-todo-statistics)
718 (org-update-checkbox-count)))
719 ;; FIXME Here we should do the sorting
720 ;; If we have added a table line, maybe recompute?
721 (when (and (eq (org-capture-get :type
'local
) 'table-line
)
723 (if (org-table-get-stored-formulas)
724 (org-table-recalculate 'all
) ;; FIXME: Should we iterate???
726 ;; Store this place as the last one where we stored something
727 ;; Do the marking in the base buffer, so that it makes sense after
728 ;; the indirect buffer has been killed.
729 (when org-capture-bookmark
730 (org-capture-bookmark-last-stored-position))
733 (run-hooks 'org-capture-before-finalize-hook
))
735 (when (org-capture-get :decrypted
)
737 (goto-char (org-capture-get :decrypted
))
738 (org-encrypt-entry)))
740 ;; Kill the indirect buffer
742 (let ((return-wconf (org-capture-get :return-to-wconf
'local
))
743 (new-buffer (org-capture-get :new-buffer
'local
))
744 (kill-buffer (org-capture-get :kill-buffer
'local
))
745 (base-buffer (buffer-base-buffer (current-buffer))))
747 ;; Kill the indirect buffer
748 (kill-buffer (current-buffer))
750 ;; Narrow back the target buffer to its previous state
751 (with-current-buffer (org-capture-get :buffer
)
752 (let ((reg (org-capture-get :initial-target-region
))
753 (pos (org-capture-get :initial-target-position
))
754 (ipt (org-capture-get :insertion-point
))
755 (size (org-capture-get :captured-entry-size
)))
758 (cond ((< ipt
(car reg
))
759 ;; insertion point is before the narrowed region
760 (narrow-to-region (+ size
(car reg
)) (+ size
(cdr reg
))))
762 ;; insertion point is after the narrowed region
763 (narrow-to-region (car reg
) (cdr reg
)))
765 ;; insertion point is within the narrowed region
766 (narrow-to-region (car reg
) (+ size
(cdr reg
)))))
767 ;; now place back the point at its original position
768 (if (< ipt
(car reg
))
769 (goto-char (+ size pos
))
770 (goto-char (if (< ipt pos
) (+ size pos
) pos
))))))
772 ;; Kill the target buffer if that is desired
773 (when (and base-buffer new-buffer kill-buffer
)
774 (with-current-buffer base-buffer
(save-buffer))
775 (kill-buffer base-buffer
))
777 ;; Restore the window configuration before capture
778 (set-window-configuration return-wconf
))
780 (run-hooks 'org-capture-after-finalize-hook
)
785 ((equal abort-note
'clean
)
786 (message "Capture process aborted and target buffer cleaned up"))
787 ((equal abort-note
'dirty
)
788 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
790 (org-capture-goto-last-stored)))
791 ;; Return if we did store something
794 (defun org-capture-refile ()
795 "Finalize the current capture and then refile the entry.
796 Refiling is done from the base buffer, because the indirect buffer is then
797 already gone. Any prefix argument will be passed to the refile command."
799 (unless (eq (org-capture-get :type
'local
) 'entry
)
801 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
803 (base (buffer-base-buffer (current-buffer)))
804 (org-refile-for-capture t
)
805 (kill-buffer (org-capture-get :kill-buffer
'local
)))
806 (org-capture-put :kill-buffer nil
)
807 (org-capture-finalize)
808 (save-window-excursion
809 (with-current-buffer (or base
(current-buffer))
814 (call-interactively 'org-refile
)))))
815 (when kill-buffer
(kill-buffer base
))))
817 (defun org-capture-kill ()
818 "Abort the current capture process."
820 ;; FIXME: This does not do the right thing, we need to remove the
821 ;; new stuff by hand it is easy: undo, then kill the buffer
822 (let ((org-note-abort t
)
823 (org-capture-before-finalize-hook nil
))
824 (org-capture-finalize)))
826 (defun org-capture-goto-last-stored ()
827 "Go to the location where the last capture note was stored."
829 (org-goto-marker-or-bmk org-capture-last-stored-marker
830 (plist-get org-bookmark-names-plist
832 (message "This is the last note stored by a capture process"))
834 ;;; Supporting functions for handling the process
836 (defun org-capture-put-target-region-and-position ()
837 "Store the initial region with `org-capture-put'."
839 :initial-target-region
840 ;; Check if the buffer is currently narrowed
841 (when (org-buffer-narrowed-p)
842 (cons (point-min) (point-max))))
843 ;; store the current point
844 (org-capture-put :initial-target-position
(point)))
846 (defvar org-time-was-given
) ; dynamically scoped parameter
847 (defun org-capture-set-target-location (&optional target
)
848 "Find TARGET buffer and position.
849 Store them in the capture property list."
850 (let ((target-entry-p t
) decrypted-hl-pos
)
851 (setq target
(or target
(org-capture-get :target
)))
854 ((eq (car target
) 'file
)
855 (set-buffer (org-capture-target-buffer (nth 1 target
)))
856 (org-capture-put-target-region-and-position)
858 (setq target-entry-p nil
))
860 ((eq (car target
) 'id
)
861 (let ((loc (org-id-find (nth 1 target
))))
863 (error "Cannot find target ID \"%s\"" (nth 1 target
))
864 (set-buffer (org-capture-target-buffer (car loc
)))
866 (org-capture-put-target-region-and-position)
867 (goto-char (cdr loc
)))))
869 ((eq (car target
) 'file
+headline
)
870 (set-buffer (org-capture-target-buffer (nth 1 target
)))
871 (org-capture-put-target-region-and-position)
873 (let ((hd (nth 2 target
)))
874 (goto-char (point-min))
875 (unless (derived-mode-p 'org-mode
)
877 "Target buffer \"%s\" for file+headline should be in Org mode"
879 (if (re-search-forward
880 (format org-complex-heading-regexp-format
(regexp-quote hd
))
882 (goto-char (point-at-bol))
883 (goto-char (point-max))
884 (or (bolp) (insert "\n"))
885 (insert "* " hd
"\n")
886 (beginning-of-line 0))))
888 ((eq (car target
) 'file
+olp
)
889 (let ((m (org-find-olp
890 (cons (org-capture-expand-file (nth 1 target
))
892 (set-buffer (marker-buffer m
))
893 (org-capture-put-target-region-and-position)
897 ((eq (car target
) 'file
+regexp
)
898 (set-buffer (org-capture-target-buffer (nth 1 target
)))
899 (org-capture-put-target-region-and-position)
901 (goto-char (point-min))
902 (if (re-search-forward (nth 2 target
) nil t
)
904 (goto-char (if (org-capture-get :prepend
)
905 (match-beginning 0) (match-end 0)))
906 (org-capture-put :exact-position
(point))
907 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
908 (error "No match for target regexp in file %s" (nth 1 target
))))
910 ((memq (car target
) '(file+datetree file
+datetree
+prompt
))
911 (require 'org-datetree
)
912 (set-buffer (org-capture-target-buffer (nth 1 target
)))
913 (org-capture-put-target-region-and-position)
915 ;; Make a date tree entry, with the current date (or yesterday,
916 ;; if we are extending dates for a couple of hours)
917 (org-datetree-find-date-create
918 (calendar-gregorian-from-absolute
920 (org-overriding-default-time
921 ;; use the overriding default time
922 (time-to-days org-overriding-default-time
))
924 ((eq (car target
) 'file
+datetree
+prompt
)
926 (let ((prompt-time (org-read-date
927 nil t nil
"Date for tree entry:"
931 (cond ((and (or (not (boundp 'org-time-was-given
))
932 (not org-time-was-given
))
933 (not (= (time-to-days prompt-time
) (org-today))))
934 ;; Use 00:00 when no time is given for another date than today?
935 (apply 'encode-time
(append '(0 0 0) (cdddr (decode-time prompt-time
)))))
936 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer
)
937 ;; Replace any time range by its start
939 (org-read-date-analyze
940 (replace-match "\\1 \\2" nil nil org-read-date-final-answer
)
941 prompt-time
(decode-time prompt-time
))))
943 (time-to-days prompt-time
)))
945 ;; current date, possibly corrected for late night workers
948 ((eq (car target
) 'file
+function
)
949 (set-buffer (org-capture-target-buffer (nth 1 target
)))
950 (org-capture-put-target-region-and-position)
952 (funcall (nth 2 target
))
953 (org-capture-put :exact-position
(point))
954 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
956 ((eq (car target
) 'function
)
957 (funcall (nth 1 target
))
958 (org-capture-put :exact-position
(point))
959 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
961 ((eq (car target
) 'clock
)
962 (if (and (markerp org-clock-hd-marker
)
963 (marker-buffer org-clock-hd-marker
))
964 (progn (set-buffer (marker-buffer org-clock-hd-marker
))
965 (org-capture-put-target-region-and-position)
967 (goto-char org-clock-hd-marker
))
968 (error "No running clock that could be used as capture target")))
970 (t (error "Invalid capture target specification")))
972 (when (and (featurep 'org-crypt
) (org-at-encrypted-entry-p))
974 (setq decrypted-hl-pos
975 (save-excursion (and (org-back-to-heading t
) (point)))))
977 (org-capture-put :buffer
(current-buffer) :pos
(point)
978 :target-entry-p target-entry-p
979 :decrypted decrypted-hl-pos
))))
981 (defun org-capture-expand-file (file)
982 "Expand functions and symbols for FILE.
983 When FILE is a function, call it. When it is a form, evaluate
984 it. When it is a variable, retrieve the value. When it is
985 a string, return it. However, if it is the empty string, return
986 `org-default-notes-file' instead."
988 ((equal file
"") org-default-notes-file
)
989 ((org-string-nw-p file
) file
)
990 ((functionp file
) (funcall file
))
991 ((and (symbolp file
) (boundp file
)) (symbol-value file
))
992 ((consp file
) (eval file
))
995 (defun org-capture-target-buffer (file)
996 "Get a buffer for FILE."
997 (setq file
(org-capture-expand-file file
))
998 (setq file
(or (org-string-nw-p file
)
999 org-default-notes-file
1000 (error "No notes file specified, and no default available")))
1001 (or (org-find-base-buffer-visiting file
)
1002 (progn (org-capture-put :new-buffer t
)
1003 (find-file-noselect (expand-file-name file org-directory
)))))
1005 (defun org-capture-steal-local-variables (buffer)
1006 "Install Org-mode local variables of BUFFER."
1008 (ignore-errors (set (make-local-variable (car v
)) (cdr v
))))
1009 (buffer-local-variables buffer
)))
1011 (defun org-capture-place-template (&optional inhibit-wconf-store
)
1012 "Insert the template at the target location, and display the buffer.
1013 When `inhibit-wconf-store', don't store the window configuration, as it
1014 may have been stored before."
1015 (unless inhibit-wconf-store
1016 (org-capture-put :return-to-wconf
(current-window-configuration)))
1017 (delete-other-windows)
1018 (org-switch-to-buffer-other-window
1019 (org-capture-get-indirect-buffer (org-capture-get :buffer
) "CAPTURE"))
1022 (goto-char (org-capture-get :pos
))
1023 (setq-local outline-level
'org-outline-level
)
1024 (let* ((template (org-capture-get :template
))
1025 (type (org-capture-get :type
)))
1027 ((nil entry
) (org-capture-place-entry))
1028 (table-line (org-capture-place-table-line))
1029 (plain (org-capture-place-plain-text))
1030 (item (org-capture-place-item))
1031 (checkitem (org-capture-place-item))))
1032 (org-capture-mode 1)
1033 (setq-local org-capture-current-plist org-capture-plist
))
1035 (defun org-capture-place-entry ()
1036 "Place the template as a new Org entry."
1037 (let* ((txt (org-capture-get :template
))
1038 (reversed (org-capture-get :prepend
))
1039 (target-entry-p (org-capture-get :target-entry-p
))
1042 (and (org-capture-get :exact-position
)
1043 (goto-char (org-capture-get :exact-position
)))
1045 ((not target-entry-p
)
1046 ;; Insert as top-level entry, either at beginning or at end of file
1049 (progn (goto-char (point-min))
1050 (or (org-at-heading-p)
1051 (outline-next-heading)))
1052 (goto-char (point-max))
1053 (or (bolp) (insert "\n"))))
1055 ;; Insert as a child of the current entry
1056 (and (looking-at "\\*+")
1057 (setq level
(- (match-end 0) (match-beginning 0))))
1058 (setq level
(org-get-valid-level (or level
1) 1))
1061 (outline-next-heading)
1062 (or (bolp) (insert "\n")))
1063 (org-end-of-subtree t nil
)
1064 (or (bolp) (insert "\n")))))
1065 (org-capture-empty-lines-before)
1067 (org-capture-verify-tree txt
)
1068 (org-paste-subtree level txt
'for-yank
)
1069 (org-capture-empty-lines-after 1)
1070 (org-capture-position-for-last-stored beg
)
1071 (outline-next-heading)
1073 (org-capture-mark-kill-region beg
(1- end
))
1074 (org-capture-narrow beg
(1- end
))
1075 (if (or (re-search-backward "%\\?" beg t
)
1076 (re-search-forward "%\\?" end t
))
1077 (replace-match ""))))
1079 (defun org-capture-place-item ()
1080 "Place the template as a new plain list item."
1081 (let* ((txt (org-capture-get :template
))
1082 (target-entry-p (org-capture-get :target-entry-p
))
1085 (if (org-capture-get :exact-position
)
1086 (goto-char (org-capture-get :exact-position
))
1088 ((not target-entry-p
)
1089 ;; Insert as top-level entry, either at beginning or at end of file
1090 (setq beg
(point-min) end
(point-max)))
1092 (setq beg
(1+ (point-at-eol))
1093 end
(save-excursion (outline-next-heading) (point)))))
1095 (if (org-capture-get :prepend
)
1098 (when (org-list-search-forward (org-item-beginning-re) end t
)
1099 (goto-char (match-beginning 0))
1100 (setq ind
(org-get-indentation))))
1102 (when (org-list-search-backward (org-item-beginning-re) beg t
)
1103 (setq ind
(org-get-indentation))
1105 (unless ind
(goto-char end
)))
1106 ;; Remove common indentation
1107 (setq txt
(org-remove-indentation txt
))
1108 ;; Make sure this is indeed an item
1109 (unless (string-match (concat "\\`" (org-item-re)) txt
)
1110 (setq txt
(concat "- "
1111 (mapconcat 'identity
(split-string txt
"\n")
1113 ;; Prepare surrounding empty lines.
1114 (org-capture-empty-lines-before)
1116 (unless (eolp) (save-excursion (insert "\n")))
1119 (setq ind
(org-get-indentation))
1120 (delete-region beg
(point)))
1121 ;; Set the correct indentation, depending on context
1122 (setq ind
(make-string ind ?\
))
1123 (setq txt
(concat ind
1124 (mapconcat 'identity
(split-string txt
"\n")
1129 (org-capture-empty-lines-after 1)
1130 (org-capture-position-for-last-stored beg
)
1133 (org-capture-mark-kill-region beg
(1- end
))
1134 (org-capture-narrow beg
(1- end
))
1135 (if (or (re-search-backward "%\\?" beg t
)
1136 (re-search-forward "%\\?" end t
))
1137 (replace-match ""))))
1139 (defun org-capture-place-table-line ()
1140 "Place the template as a table line."
1141 (require 'org-table
)
1142 (let* ((txt (org-capture-get :template
))
1143 (target-entry-p (org-capture-get :target-entry-p
))
1144 (table-line-pos (org-capture-get :table-line-pos
))
1147 ((org-capture-get :exact-position
)
1148 (goto-char (org-capture-get :exact-position
)))
1149 ((not target-entry-p
)
1150 ;; Table is not necessarily under a heading
1151 (setq beg
(point-min) end
(point-max)))
1153 ;; WE are at a heading, limit search to the body
1154 (setq beg
(1+ (point-at-eol))
1155 end
(save-excursion (outline-next-heading) (point)))))
1156 (if (re-search-forward org-table-dataline-regexp end t
)
1157 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t
))
1159 (if (looking-at "[ \t]*#\\+tblfm:")
1161 (narrow-to-region b
(point)))
1163 (insert "\n| |\n|----|\n| |\n")
1164 (narrow-to-region (1+ end
) (point)))
1165 ;; We are narrowed to the table, or to an empty line if there was no table
1167 ;; Check if the template is good
1168 (if (not (string-match org-table-dataline-regexp txt
))
1169 (setq txt
"| %?Bad template |\n"))
1170 (if (functionp table-line-pos
)
1171 (setq table-line-pos
(funcall table-line-pos
))
1172 (setq table-line-pos
(eval table-line-pos
)))
1174 ((and table-line-pos
1175 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos
))
1176 ;; we have a complex line specification
1177 (let ((ll (ignore-errors
1178 (save-match-data (org-table-analyze))
1179 (aref org-table-hlines
1180 (- (match-end 1) (match-beginning 1)))))
1181 (delta (string-to-number (match-string 2 table-line-pos
))))
1182 ;; The user wants a special position in the table
1184 (error "Invalid table line specification \"%s\"" table-line-pos
))
1185 (goto-char org-table-current-begin-pos
)
1186 (forward-line (+ ll delta
(if (< delta
0) 0 -
1)))
1187 (org-table-insert-row 'below
)
1188 (beginning-of-line 1)
1189 (delete-region (point) (1+ (point-at-eol)))
1192 (setq end
(point))))
1193 ((org-capture-get :prepend
)
1194 (goto-char (point-min))
1195 (re-search-forward org-table-hline-regexp nil t
)
1196 (beginning-of-line 1)
1197 (re-search-forward org-table-dataline-regexp nil t
)
1198 (beginning-of-line 1)
1200 (org-table-insert-row)
1201 (beginning-of-line 1)
1202 (delete-region (point) (1+ (point-at-eol)))
1206 (goto-char (point-max))
1207 (re-search-backward org-table-dataline-regexp nil t
)
1208 (beginning-of-line 1)
1209 (org-table-insert-row 'below
)
1210 (beginning-of-line 1)
1211 (delete-region (point) (1+ (point-at-eol)))
1214 (setq end
(point))))
1216 (org-capture-position-for-last-stored 'table-line
)
1217 (if (or (re-search-backward "%\\?" beg t
)
1218 (re-search-forward "%\\?" end t
))
1222 (defun org-capture-place-plain-text ()
1223 "Place the template plainly.
1224 If the target locator points at an Org node, place the template into
1225 the text of the entry, before the first child. If not, place the
1226 template at the beginning or end of the file.
1227 Of course, if exact position has been required, just put it there."
1228 (let* ((txt (org-capture-get :template
))
1231 ((org-capture-get :exact-position
)
1232 (goto-char (org-capture-get :exact-position
)))
1233 ((and (org-capture-get :target-entry-p
)
1235 (looking-at org-outline-regexp
))
1236 ;; we should place the text into this entry
1237 (if (org-capture-get :prepend
)
1238 ;; Skip meta data and drawers
1239 (org-end-of-meta-data t
)
1240 ;; go to ent of the entry text, before the next headline
1241 (outline-next-heading)))
1243 ;; beginning or end of file
1244 (goto-char (if (org-capture-get :prepend
) (point-min) (point-max)))))
1245 (or (bolp) (newline))
1246 (org-capture-empty-lines-before)
1249 (org-capture-empty-lines-after 1)
1250 (org-capture-position-for-last-stored beg
)
1252 (org-capture-mark-kill-region beg
(1- end
))
1253 (org-capture-narrow beg
(1- end
))
1254 (if (or (re-search-backward "%\\?" beg t
)
1255 (re-search-forward "%\\?" end t
))
1256 (replace-match ""))))
1258 (defun org-capture-mark-kill-region (beg end
)
1259 "Mark the region that will have to be killed when aborting capture."
1260 (let ((m1 (move-marker (make-marker) beg
))
1261 (m2 (move-marker (make-marker) end
)))
1262 (org-capture-put :begin-marker m1
)
1263 (org-capture-put :end-marker m2
)))
1265 (defun org-capture-position-for-last-stored (where)
1266 "Memorize the position that should later become the position of last capture."
1269 (org-capture-put :position-for-last-stored
1270 (move-marker (make-marker) where
1271 (or (buffer-base-buffer (current-buffer))
1272 (current-buffer)))))
1273 ((eq where
'table-line
)
1274 (org-capture-put :position-for-last-stored
1276 (org-table-current-dline))))
1277 (t (error "This should not happen"))))
1279 (defun org-capture-bookmark-last-stored-position ()
1280 "Bookmark the last-captured position."
1281 (let* ((where (org-capture-get :position-for-last-stored
'local
))
1284 (prog1 (marker-position where
)
1285 (move-marker where nil
)))
1286 ((and (listp where
) (eq (car where
) 'table-line
))
1287 (if (org-at-table-p)
1289 (org-table-goto-line (nth 1 where
))
1292 (with-current-buffer (buffer-base-buffer (current-buffer))
1297 (let ((bookmark-name (plist-get org-bookmark-names-plist
1300 (with-demoted-errors
1301 (bookmark-set bookmark-name
))))
1302 (move-marker org-capture-last-stored-marker
(point)))))))
1304 (defun org-capture-narrow (beg end
)
1305 "Narrow, unless configuration says not to narrow."
1306 (unless (org-capture-get :unnarrowed
)
1307 (narrow-to-region beg end
)
1310 (defun org-capture-empty-lines-before (&optional n
)
1311 "Set the correct number of empty lines before the insertion point.
1312 Point will be after the empty lines, so insertion can directly be done."
1313 (setq n
(or n
(org-capture-get :empty-lines-before
)
1314 (org-capture-get :empty-lines
) 0))
1315 (let ((pos (point)))
1316 (org-back-over-empty-lines)
1317 (delete-region (point) pos
)
1318 (if (> n
0) (newline n
))))
1320 (defun org-capture-empty-lines-after (&optional n
)
1321 "Set the correct number of empty lines after the inserted string.
1322 Point will remain at the first line after the inserted text."
1323 (setq n
(or n
(org-capture-get :empty-lines-after
)
1324 (org-capture-get :empty-lines
) 0))
1325 (org-back-over-empty-lines)
1326 (while (looking-at "[ \t]*\n") (replace-match ""))
1327 (let ((pos (point)))
1328 (if (> n
0) (newline n
))
1331 (defvar org-clock-marker
) ; Defined in org.el
1333 (defun org-capture-insert-template-here ()
1334 "Insert the capture template at point."
1335 (let* ((template (org-capture-get :template
))
1336 (type (org-capture-get :type
))
1338 (or (bolp) (newline))
1341 ((and (eq type
'entry
) (derived-mode-p 'org-mode
))
1342 (org-capture-verify-tree (org-capture-get :template
))
1343 (org-paste-subtree nil template t
))
1344 ((and (memq type
'(item checkitem
))
1345 (derived-mode-p 'org-mode
)
1346 (save-excursion (skip-chars-backward " \t\n")
1351 (skip-chars-backward " ")
1352 (skip-chars-backward "-+*0123456789).")
1353 (delete-region (point) (point-at-eol))
1355 (org-remove-indentation template
)
1357 (org-capture-empty-lines-after)
1362 (t (insert template
)))
1365 (if (re-search-forward "%\\?" end t
)
1366 (replace-match ""))))
1368 (defun org-capture-set-plist (entry)
1369 "Initialize the property list from the template definition."
1370 (setq org-capture-plist
(copy-sequence (nthcdr 5 entry
)))
1371 (org-capture-put :key
(car entry
) :description
(nth 1 entry
)
1372 :target
(nth 3 entry
))
1373 (let ((txt (nth 4 entry
)) (type (or (nth 2 entry
) 'entry
)))
1374 (when (or (not txt
) (and (stringp txt
) (not (string-match "\\S-" txt
))))
1375 ;; The template may be empty or omitted for special types.
1376 ;; Here we insert the default templates for such cases.
1378 ((eq type
'item
) (setq txt
"- %?"))
1379 ((eq type
'checkitem
) (setq txt
"- [ ] %?"))
1380 ((eq type
'table-line
) (setq txt
"| %? |"))
1381 ((member type
'(nil entry
)) (setq txt
"* %?\n %a"))))
1382 (org-capture-put :template txt
:type type
)))
1384 (defun org-capture-goto-target (&optional template-key
)
1385 "Go to the target location of a capture template.
1386 The user is queried for the template."
1388 (let* (org-select-template-temp-major-mode
1389 (entry (org-capture-select-template template-key
)))
1391 (error "No capture template selected"))
1392 (org-capture-set-plist entry
)
1393 (org-capture-set-target-location)
1394 (org-pop-to-buffer-same-window (org-capture-get :buffer
))
1395 (goto-char (org-capture-get :pos
))))
1397 (defun org-capture-get-indirect-buffer (&optional buffer prefix
)
1398 "Make an indirect buffer for a capture process.
1399 Use PREFIX as a prefix for the name of the indirect buffer."
1400 (setq buffer
(or buffer
(current-buffer)))
1401 (let ((n 1) (base (buffer-name buffer
)) bname
)
1402 (setq bname
(concat prefix
"-" base
))
1403 (while (buffer-live-p (get-buffer bname
))
1404 (setq bname
(concat prefix
"-" (number-to-string (incf n
)) "-" base
)))
1406 (make-indirect-buffer buffer bname
'clone
)
1408 (let ((buf (make-indirect-buffer buffer bname
)))
1409 (with-current-buffer buf
(org-mode))
1412 (defun org-capture-verify-tree (tree)
1413 "Throw error if TREE is not a valid tree."
1414 (unless (org-kill-is-subtree-p tree
)
1415 (error "Template is not a valid Org entry or tree")))
1417 (defun org-mks (table title
&optional prompt specials
)
1418 "Select a member of an alist with multiple keys.
1419 TABLE is the alist which should contain entries where the car is a string.
1420 There should be two types of entries.
1422 1. prefix descriptions like (\"a\" \"Description\")
1423 This indicates that `a' is a prefix key for multi-letter selection, and
1424 that there are entries following with keys like \"ab\", \"ax\"...
1426 2. Selectable members must have more than two elements, with the first
1427 being the string of keys that lead to selecting it, and the second a
1428 short description string of the item.
1430 The command will then make a temporary buffer listing all entries
1431 that can be selected with a single key, and all the single key
1432 prefixes. When you press the key for a single-letter entry, it is selected.
1433 When you press a prefix key, the commands (and maybe further prefixes)
1434 under this key will be shown and offered for selection.
1436 TITLE will be placed over the selection in the temporary buffer,
1437 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1438 also (\"key\" \"description\") entries. When one of these is selection,
1439 only the bare key is returned."
1440 (setq prompt
(or prompt
"Select: "))
1441 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1442 current prefix rtn re pressed buffer
(inhibit-quit t
))
1443 (save-window-excursion
1444 (setq buffer
(org-switch-to-buffer-other-window "*Org Select*"))
1445 (setq orig-table table
)
1449 (insert title
"\n\n")
1454 (setq prefix
(if current
(concat current
" ") ""))
1457 ((and (= 2 (length (car tbl
))) (= (length (caar tbl
)) 1))
1458 ;; This is a description on this level
1459 (setq dkey
(caar tbl
) ddesc
(cadar tbl
))
1461 (push dkey des-keys
)
1462 (push dkey allowed-keys
)
1463 (insert prefix
"[" dkey
"]" "..." " " ddesc
"..." "\n")
1464 ;; Skip keys which are below this prefix
1465 (setq re
(concat "\\`" (regexp-quote dkey
)))
1466 (let (case-fold-search)
1467 (while (and tbl
(string-match re
(caar tbl
))) (pop tbl
))))
1468 ((= 2 (length (car tbl
)))
1469 ;; Not yet a usable description, skip it
1472 ;; usable entry on this level
1473 (insert prefix
"[" (caar tbl
) "]" " " (nth 1 (car tbl
)) "\n")
1474 (push (caar tbl
) allowed-keys
)
1477 (insert "-------------------------------------------------------------------------------\n")
1478 (let ((sp specials
))
1480 (insert (format "[%s] %s\n"
1481 (caar sp
) (nth 1 (car sp
))))
1482 (push (caar sp
) allowed-keys
)
1484 (push "\C-g" allowed-keys
)
1485 (goto-char (point-min))
1486 (if (not (pos-visible-in-window-p (point-max)))
1487 (org-fit-window-to-buffer))
1489 (setq pressed
(char-to-string (read-char-exclusive)))
1490 (while (not (member pressed allowed-keys
))
1491 (message "Invalid key `%s'" pressed
) (sit-for 1)
1493 (setq pressed
(char-to-string (read-char-exclusive))))
1494 (when (equal pressed
"\C-g")
1495 (kill-buffer buffer
)
1497 (when (and (not (assoc pressed table
))
1498 (not (member pressed des-keys
))
1499 (assoc pressed specials
))
1500 (throw 'exit
(setq rtn pressed
)))
1501 (unless (member pressed des-keys
)
1502 (throw 'exit
(setq rtn
(rassoc (cdr (assoc pressed table
))
1504 (setq current
(concat current pressed
))
1507 (if (and (> (length (car x
)) 1)
1508 (equal (substring (car x
) 0 1) pressed
))
1509 (cons (substring (car x
) 1) (cdr x
))
1512 (setq table
(remove nil table
)))))
1513 (when buffer
(kill-buffer buffer
))
1516 ;;; The template code
1517 (defun org-capture-select-template (&optional keys
)
1518 "Select a capture template.
1519 Lisp programs can force the template by setting KEYS to a string."
1520 (let ((org-capture-templates
1521 (or (org-contextualize-keys
1522 org-capture-templates org-capture-templates-contexts
)
1523 '(("t" "Task" entry
(file+headline
"" "Tasks")
1524 "* TODO %?\n %u\n %a")))))
1526 (or (assoc keys org-capture-templates
)
1527 (error "No capture template referred to by \"%s\" keys" keys
))
1528 (org-mks org-capture-templates
1529 "Select a capture template\n========================="
1531 '(("C" "Customize org-capture-templates")
1534 (defun org-capture-fill-template (&optional template initial annotation
)
1535 "Fill a template and return the filled template as a string.
1536 The template may still contain \"%?\" for cursor positioning."
1537 (setq template
(or template
(org-capture-get :template
)))
1538 (when (stringp initial
)
1539 (setq initial
(org-no-properties initial
)))
1540 (let* ((buffer (org-capture-get :buffer
))
1541 (file (buffer-file-name (or (buffer-base-buffer buffer
) buffer
)))
1542 (ct (org-capture-get :default-time
))
1543 (dct (decode-time ct
))
1545 (if (< (nth 2 dct
) org-extend-today-until
)
1546 (encode-time 0 59 23 (1- (nth 3 dct
)) (nth 4 dct
) (nth 5 dct
))
1548 (plist-p org-store-link-plist
)
1549 (v-c (and (> (length kill-ring
) 0) (current-kill 0)))
1550 (v-x (or (org-get-x-clipboard 'PRIMARY
)
1551 (org-get-x-clipboard 'CLIPBOARD
)
1552 (org-get-x-clipboard 'SECONDARY
)))
1553 (v-t (format-time-string (car org-time-stamp-formats
) ct1
))
1554 (v-T (format-time-string (cdr org-time-stamp-formats
) ct1
))
1555 (v-u (concat "[" (substring v-t
1 -
1) "]"))
1556 (v-U (concat "[" (substring v-T
1 -
1) "]"))
1557 ;; `initial' and `annotation' might have been passed. But if
1558 ;; the property list has them, we prefer those values.
1559 (v-i (or (plist-get org-store-link-plist
:initial
)
1561 (org-capture-get :initial
)
1563 (v-a (or (plist-get org-store-link-plist
:annotation
)
1565 (org-capture-get :annotation
)
1567 ;; Is the link empty? Then we do not want it...
1568 (v-a (if (equal v-a
"[[]]") "" v-a
))
1569 (clipboards (remq nil
(list v-i
1570 (org-get-x-clipboard 'PRIMARY
)
1571 (org-get-x-clipboard 'CLIPBOARD
)
1572 (org-get-x-clipboard 'SECONDARY
)
1574 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1575 (v-A (if (and v-a
(string-match l-re v-a
))
1576 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a
)
1578 (v-l (if (and v-a
(string-match l-re v-a
))
1579 (replace-match "\\1" nil nil v-a
)
1581 (v-n user-full-name
)
1582 (v-k (and (marker-buffer org-clock-marker
)
1583 (org-no-properties org-clock-heading
)))
1584 (v-K (if (marker-buffer org-clock-marker
)
1585 (org-make-link-string
1586 (buffer-file-name (marker-buffer org-clock-marker
))
1587 org-clock-heading
)))
1588 (v-f (or (org-capture-get :original-file-nondirectory
) ""))
1589 (v-F (or (org-capture-get :original-file
) ""))
1590 (org-startup-folded nil
)
1591 (org-inhibit-startup t
)
1594 (setq org-store-link-plist
(plist-put org-store-link-plist
:annotation v-a
))
1595 (setq org-store-link-plist
(plist-put org-store-link-plist
:initial v-i
))
1600 (message "no template") (ding)
1602 (save-window-excursion
1603 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1605 ;; Turn on org-mode in temp buffer, set local variables. This
1606 ;; is to support completion in interactive prompts
1607 (let ((org-inhibit-startup t
)) (org-mode))
1609 (goto-char (point-min))
1610 (org-capture-steal-local-variables buffer
)
1611 (setq buffer-file-name nil
)
1612 (setq mark-active nil
)
1614 ;; %[] insert contents of a file.
1616 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t
)
1617 (unless (org-capture-escaped-%
)
1618 (let ((start (match-beginning 0))
1620 (filename (expand-file-name (match-string 1))))
1622 (delete-region start end
)
1623 (condition-case error
1624 (insert-file-contents filename
)
1625 (error (insert (format "%%![couldn not insert %s: %s]"
1629 ;; Mark %() embedded elisp for later evaluation.
1630 (org-capture--expand-embedded-elisp 'mark
)
1632 (let ((regexp "%\\(:[-a-za-z]+\\|<\\([^>\n]+\\)>\\|[aAcfFikKlntTuUx]\\|\
1633 \\^\\(?:{\\([^}]*\\)}\\)?\\([CgGLptTuU]\\)?\\)")
1635 (while (re-search-forward regexp nil t
)
1636 (let ((pos (copy-marker (match-beginning 0)))
1637 (end (copy-marker (match-end 0)))
1638 (value (match-string 1))
1639 (time-string (match-string 2))
1640 (prompt (match-string-no-properties 3))
1641 (key (match-string 4)))
1642 (unless (org-capture-escaped-%
)
1644 (delete-region pos end
)
1645 (pcase (string-to-char value
)
1647 ;; The current time.
1648 (insert (format-time-string time-string
)))
1650 ;; From the property list.
1651 (insert (or (plist-get org-store-link-plist
(intern value
))
1653 (?i
(let ((lead (buffer-substring-no-properties
1654 (line-beginning-position) pos
)))
1655 (insert (mapconcat #'identity
1656 (split-string initial
"\n")
1657 (concat "\n" lead
)))))
1659 ;; Interactive template entries.
1660 (let ((completions nil
)
1664 (setq completions
(org-split-string prompt
"|"))
1665 (setq prompt
(pop completions
))
1666 (setq default
(car completions
))
1669 "org-capture-template-prompt-history::"
1671 (setq completions
(mapcar #'list completions
)))
1674 (let* ((org-last-tags-completion-table
1675 (org-global-tags-completion-table
1676 (cond ((equal key
"G") (org-agenda-files))
1679 (org-add-colon-after-tag-completion t
)
1684 (if prompt
(concat prompt
": ") "Tags: ")
1685 'org-tags-completion-function nil nil nil
1687 "[^[:alnum:]_@#%]+")
1689 (when (org-string-nw-p ins
)
1690 (unless (eq (char-before) ?
:) (insert ":"))
1692 (unless (eq (char-after) ?
:) (insert ":"))
1693 (and (org-at-heading-p)
1694 (let ((org-ignore-region t
))
1695 (org-set-tags nil
'align
))))))
1698 ((= (length clipboards
) 1) (insert (car clipboards
)))
1699 ((> (length clipboards
) 1)
1700 (insert (read-string "Clipboard/kill value: "
1703 (car clipboards
))))))
1705 (cond ((= (length clipboards
) 1)
1706 (org-insert-link 0 (car clipboards
)))
1707 ((> (length clipboards
) 1)
1710 (read-string "Clipboard/kill value: "
1713 (car clipboards
))))))
1714 (`"p" (org-set-property prompt nil
))
1716 ;; These are the date/time related ones.
1717 (let* ((upcase?
(equal (upcase key
) key
))
1718 (org-time-was-given upcase?
)
1719 (org-end-time-was-given)
1720 (time (org-read-date upcase? t nil prompt
)))
1721 (org-insert-time-stamp
1722 time org-time-was-given
1723 (member key
'("u" "U"))
1724 nil nil
(list org-end-time-was-given
))))
1726 (push (completing-read
1727 (concat (or prompt
"Enter string")
1728 (and default
(format " [%s]" default
))
1730 completions nil nil nil histvar default
)
1732 (insert (car strings
))))))
1746 (set-marker pos nil
)
1747 (set-marker end nil
)))))
1749 ;; Replace %n escapes with nth %^{...} string.
1750 (setq strings
(nreverse strings
))
1752 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t
)
1753 (unless (org-capture-escaped-%
)
1755 (nth (1- (string-to-number (match-string 1))) strings
)
1758 ;; Expand %() embedded Elisp. Limit to Sexp originally marked.
1759 (org-capture--expand-embedded-elisp)
1761 ;; Make sure there are no empty lines before the text, and that
1762 ;; it ends with a newline character.
1763 (goto-char (point-min))
1764 (while (looking-at "[ \t]*\n") (replace-match ""))
1765 (when (re-search-forward "[ \t\n]*\\'" nil t
) (replace-match "\n"))
1766 ;; Return the expanded template and kill the temporary buffer.
1767 (untabify (point-min) (point-max))
1768 (set-buffer-modified-p nil
)
1769 (prog1 (buffer-substring-no-properties (point-min) (point-max))
1770 (kill-buffer (current-buffer))))))
1772 (defun org-capture-escaped-%
()
1773 "Non-nil if % was escaped.
1774 If yes, unescape it now. Assume match-data contains the
1775 placeholder to check."
1777 (goto-char (match-beginning 0))
1778 (let ((n (abs (skip-chars-backward "\\\\"))))
1779 (delete-char (/ (1+ n
) 2))
1782 (defun org-capture--expand-embedded-elisp (&optional mark
)
1783 "Evaluate embedded elisp %(sexp) and replace with the result.
1784 When optional MARK argument is non-nil, mark Sexp with a text
1785 property (`org-embedded-elisp') for later evaluation. Only
1786 marked Sexp are evaluated when this argument is nil."
1788 (goto-char (point-min))
1789 (while (re-search-forward "%(" nil t
)
1790 (unless (org-capture-escaped-%
)
1793 (match-beginning 0) (match-end 0) 'org-embedded-elisp t
)
1794 (when (get-text-property (match-beginning 0) 'org-embedded-elisp
)
1795 (goto-char (match-beginning 0))
1796 (let ((template-start (point)))
1798 (let* ((sexp (read (current-buffer)))
1800 (org-capture--expand-keyword-in-embedded-elisp
1802 (delete-region template-start
(point))
1804 (if (stringp result
)
1807 "Capture template sexp `%s' must evaluate to string or nil"
1810 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1811 "Recursively replace capture link keywords in ATTR sexp.
1812 Such keywords are prefixed with \"%:\". See
1813 `org-capture-template' for more information."
1815 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr
))
1817 (let* ((attr-symbol (symbol-name attr
))
1818 (key (and (string-match "%\\(:.*\\)" attr-symbol
)
1819 (intern (match-string 1 attr-symbol
)))))
1820 (or (plist-get org-store-link-plist key
)
1824 (defun org-capture-inside-embedded-elisp-p ()
1825 "Return non-nil if point is inside of embedded elisp %(sexp)."
1827 (with-syntax-table emacs-lisp-mode-syntax-table
1829 ;; `looking-at' and `search-backward' below do not match the "%(" if
1830 ;; point is in its middle
1831 (when (equal (char-before) ?%
)
1834 (when (or (looking-at "%(") (search-backward "%(" nil t
))
1836 (setq end
(progn (forward-char) (forward-sexp) (1- (point)))))))
1838 (and (<= (point) end
) (>= (point) beg
))))))
1841 (defun org-capture-import-remember-templates ()
1842 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1844 (when (and (yes-or-no-p
1845 "Import old remember templates into org-capture-templates? ")
1847 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1848 (require 'org-remember
)
1849 (setq org-capture-templates
1852 (let ((desc (car entry
))
1853 (key (char-to-string (nth 1 entry
)))
1854 (template (nth 2 entry
))
1855 (file (or (nth 3 entry
) org-default-notes-file
))
1856 (position (or (nth 4 entry
) org-remember-default-headline
))
1858 (prepend org-reverse-note-order
)
1859 immediate target jump-to-captured
)
1861 ((member position
'(top bottom
))
1862 (setq target
(list 'file file
)
1863 prepend
(eq position
'top
)))
1864 ((eq position
'date-tree
)
1865 (setq target
(list 'file
+datetree file
)
1867 (t (setq target
(list 'file
+headline file position
))))
1869 (when (string-match "%!" template
)
1870 (setq template
(replace-match "" t t template
)
1873 (when (string-match "%&" template
)
1874 (setq jump-to-captured t
))
1876 (append (list key desc type target template
)
1877 (if prepend
'(:prepend t
))
1878 (if immediate
'(:immediate-finish t
))
1879 (if jump-to-captured
'(:jump-to-captured t
)))))
1881 org-remember-templates
))))
1883 (provide 'org-capture
)
1885 ;;; org-capture.el ends here