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 in this order:
227 %[pathname] Insert the contents of the file given by `pathname'.
228 %(sexp) Evaluate elisp `(sexp)' and replace it with the results.
229 For convenience, %:keyword (see below) placeholders within
230 the expression will be expanded prior to this.
231 %<...> The result of format-time-string on the ... format specification.
232 %t Time stamp, date only.
233 %T Time stamp with date and time.
234 %u, %U Like the above, but inactive time stamps.
235 %i Initial content, copied from the active region. If %i is
236 indented, the entire inserted text will be indented as well.
237 %a Annotation, normally the link created with `org-store-link'.
238 %A Like %a, but prompt for the description part.
239 %l Like %a, but only insert the literal link.
240 %c Current kill ring head.
241 %x Content of the X clipboard.
242 %k Title of currently clocked task.
243 %K Link to currently clocked task.
244 %n User name (taken from the variable `user-full-name').
245 %f File visited by current buffer when org-capture was called.
246 %F Full path of the file or directory visited by current buffer.
247 %:keyword Specific information for certain link types, see below.
248 %^g Prompt for tags, with completion on tags in target file.
249 %^G Prompt for tags, with completion on all tags in all agenda files.
250 %^t Like %t, but prompt for date. Similarly %^T, %^u, %^U.
251 You may define a prompt like: %^{Please specify birthday}t
252 %^C Interactive selection of which kill or clip to use.
253 %^L Like %^C, but insert as link.
254 %^{prop}p Prompt the user for a value for property `prop'.
255 %^{prompt} Prompt the user for a string and replace this sequence with it.
256 A default value and a completion table ca be specified like this:
257 %^{prompt|default|completion2|completion3|...}.
258 %? After completing the template, position cursor here.
259 %\\n Insert the text entered at the nth %^{prompt}, where `n' is
260 a number, starting from 1.
262 Apart from these general escapes, you can access information specific to
263 the link type that is created. For example, calling `org-capture' in emails
264 or in Gnus will record the author and the subject of the message, which you
265 can access with \"%:from\" and \"%:subject\", respectively. Here is a
266 complete list of what is recorded for each link type.
268 Link type | Available information
269 ------------------------+------------------------------------------------------
270 bbdb | %:type %:name %:company
271 vm, wl, mh, mew, rmail, | %:type %:subject %:message-id
272 gnus | %:from %:fromname %:fromaddress
273 | %:to %:toname %:toaddress
274 | %:fromto (either \"to NAME\" or \"from NAME\")
275 | %:date %:date-timestamp (as active timestamp)
276 | %:date-timestamp-inactive (as inactive timestamp)
277 gnus | %:group, for messages also all email fields
278 w3, w3m | %:type %:url
279 info | %:type %:file %:node
280 calendar | %:type %:date"
285 (choice :value
("" "" entry
(file "~/org/notes.org") "")
286 (list :tag
"Multikey description"
287 (string :tag
"Keys ")
288 (string :tag
"Description"))
289 (list :tag
"Template entry"
290 (string :tag
"Keys ")
291 (string :tag
"Description ")
292 (choice :tag
"Capture Type " :value entry
293 (const :tag
"Org entry" entry
)
294 (const :tag
"Plain list item" item
)
295 (const :tag
"Checkbox item" checkitem
)
296 (const :tag
"Plain text" plain
)
297 (const :tag
"Table line" table-line
))
298 (choice :tag
"Target location"
300 (const :format
"" file
)
303 (const :format
"" id
)
305 (list :tag
"File & Headline"
306 (const :format
"" file
+headline
)
308 (string :tag
" Headline"))
309 (list :tag
"File & Outline path"
310 (const :format
"" file
+olp
)
312 (repeat :tag
"Outline path" :inline t
313 (string :tag
"Headline")))
314 (list :tag
"File & Regexp"
315 (const :format
"" file
+regexp
)
317 (regexp :tag
" Regexp"))
318 (list :tag
"File & Date tree"
319 (const :format
"" file
+datetree
)
321 (list :tag
"File & Date tree, prompt for date"
322 (const :format
"" file
+datetree
+prompt
)
324 (list :tag
"File & function"
325 (const :format
"" file
+function
)
327 (sexp :tag
" Function"))
328 (list :tag
"Current clocking task"
329 (const :format
"" clock
))
330 (list :tag
"Function"
331 (const :format
"" function
)
332 (sexp :tag
" Function")))
333 (choice :tag
"Template"
336 (const :format
"" file
)
337 (file :tag
"Template file"))
338 (list :tag
"Function"
339 (const :format
"" function
)
340 (function :tag
"Template function")))
342 ;; Give the most common options as checkboxes
343 :options
(((const :format
"%v " :prepend
) (const t
))
344 ((const :format
"%v " :immediate-finish
) (const t
))
345 ((const :format
"%v " :jump-to-captured
) (const t
))
346 ((const :format
"%v " :empty-lines
) (const 1))
347 ((const :format
"%v " :empty-lines-before
) (const 1))
348 ((const :format
"%v " :empty-lines-after
) (const 1))
349 ((const :format
"%v " :clock-in
) (const t
))
350 ((const :format
"%v " :clock-keep
) (const t
))
351 ((const :format
"%v " :clock-resume
) (const t
))
352 ((const :format
"%v " :unnarrowed
) (const t
))
353 ((const :format
"%v " :table-line-pos
) (const t
))
354 ((const :format
"%v " :kill-buffer
) (const t
))))))))
356 (defcustom org-capture-before-finalize-hook nil
357 "Hook that is run right before a capture process is finalized.
358 The capture buffer is still current when this hook runs and it is
359 widened to the entire buffer."
364 (defcustom org-capture-after-finalize-hook nil
365 "Hook that is run right after a capture process is finalized.
366 Suitable for window cleanup."
371 (defcustom org-capture-prepare-finalize-hook nil
372 "Hook that is run before the finalization starts.
373 The capture buffer is current and still narrowed."
378 (defcustom org-capture-bookmark t
379 "When non-nil, add a bookmark pointing at the last stored
380 position when capturing."
385 ;;; The property list for keeping information about the capture process
387 (defvar org-capture-plist nil
388 "Plist for the current capture process, global, to avoid having to pass it.")
390 (defvar org-capture-current-plist nil
391 "Local variable holding the plist in a capture buffer.
392 This is used to store the plist for use when finishing a capture process
393 because another such process might have changed the global variable by then.
395 Each time a new capture buffer has been set up, the global `org-capture-plist'
396 is copied to this variable, which is local in the indirect buffer.")
398 (defvar org-capture-clock-keep nil
399 "Local variable to store the value of the :clock-keep parameter.
400 This is needed in case org-capture-finalize is called interactively.")
402 (defun org-capture-put (&rest stuff
)
403 "Add properties to the capture property list `org-capture-plist'."
405 (setq org-capture-plist
(plist-put org-capture-plist
406 (pop stuff
) (pop stuff
)))))
407 (defun org-capture-get (prop &optional local
)
408 "Get properties from the capture property list `org-capture-plist'.
409 When LOCAL is set, use the local variable `org-capture-current-plist',
410 this is necessary after initialization of the capture process,
411 to avoid conflicts with other active capture processes."
412 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
414 (defun org-capture-member (prop &optional local
)
415 "Is PROP a property in `org-capture-plist'.
416 When LOCAL is set, use the local variable `org-capture-current-plist',
417 this is necessary after initialization of the capture process,
418 to avoid conflicts with other active capture processes."
419 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
423 (defvar org-capture-mode-map
(make-sparse-keymap)
424 "Keymap for `org-capture-mode', a minor mode.
425 Use this map to set additional keybindings for when Org-mode is used
426 for a capture buffer.")
428 (defvar org-capture-mode-hook nil
429 "Hook for the minor `org-capture-mode'.")
431 (define-minor-mode org-capture-mode
432 "Minor mode for special key bindings in a capture buffer.
434 Turning on this mode runs the normal hook `org-capture-mode-hook'."
435 nil
" Rem" org-capture-mode-map
438 (substitute-command-keys
439 "\\<org-capture-mode-map>Capture buffer. Finish \\[org-capture-finalize], \
440 refile \\[org-capture-refile], abort \\[org-capture-kill].")))
441 (define-key org-capture-mode-map
"\C-c\C-c" 'org-capture-finalize
)
442 (define-key org-capture-mode-map
"\C-c\C-k" 'org-capture-kill
)
443 (define-key org-capture-mode-map
"\C-c\C-w" 'org-capture-refile
)
445 ;;; The main commands
447 (defvar org-capture-initial nil
)
448 (defvar org-capture-entry nil
)
451 (defun org-capture-string (string &optional keys
)
452 "Capture STRING with the template selected by KEYS."
453 (interactive "sInitial text: \n")
454 (let ((org-capture-initial string
)
455 (org-capture-entry (org-capture-select-template keys
)))
458 (defcustom org-capture-templates-contexts nil
459 "Alist of capture templates and valid contexts.
461 For example, if you have a capture template \"c\" and you want
462 this template to be accessible only from `message-mode' buffers,
465 \\='((\"c\" ((in-mode . \"message-mode\"))))
467 Here are the available contexts definitions:
469 in-file: command displayed only in matching files
470 in-mode: command displayed only in matching modes
471 not-in-file: command not displayed in matching files
472 not-in-mode: command not displayed in matching modes
473 in-buffer: command displayed only in matching buffers
474 not-in-buffer: command not displayed in matching buffers
475 [function]: a custom function taking no argument
477 If you define several checks, the agenda command will be
478 accessible if there is at least one valid check.
480 You can also bind a key to another agenda custom command
481 depending on contextual rules.
483 \\='((\"c\" \"d\" ((in-mode . \"message-mode\"))))
485 Here it means: in `message-mode buffers', use \"c\" as the
486 key for the capture template otherwise associated with \"d\".
487 \(The template originally associated with \"d\" is not displayed
488 to avoid duplicates.)"
491 :type
'(repeat (list :tag
"Rule"
492 (string :tag
" Capture key")
493 (string :tag
"Replace by template")
494 (repeat :tag
"Available when"
496 (cons :tag
"Condition"
498 (const :tag
"In file" in-file
)
499 (const :tag
"Not in file" not-in-file
)
500 (const :tag
"In buffer" in-buffer
)
501 (const :tag
"Not in buffer" not-in-buffer
)
502 (const :tag
"In mode" in-mode
)
503 (const :tag
"Not in mode" not-in-mode
))
505 (function :tag
"Custom function"))))))
507 (defcustom org-capture-use-agenda-date nil
508 "Non-nil means use the date at point when capturing from agendas.
509 When nil, you can still capture using the date at point with \\[org-agenda-capture]."
515 (defun org-capture (&optional goto keys
)
517 \\<org-capture-mode-map>
518 This will let you select a template from `org-capture-templates', and then
519 file the newly captured information. The text is immediately inserted
520 at the target location, and an indirect buffer is shown where you can
521 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
522 of Emacs, so that you can continue your work.
524 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
525 anything, just go to the file/headline where the selected template
526 stores its notes. With a double prefix argument \
527 \\[universal-argument] \\[universal-argument], go to the last note
530 When called with a `C-0' (zero) prefix, insert a template at point.
532 ELisp programs can set KEYS to a string associated with a template
533 in `org-capture-templates'. In this case, interactive selection
536 If `org-capture-use-agenda-date' is non-nil, capturing from the
537 agenda will use the date at point as the default date. Then, a
538 `C-1' prefix will tell the capture process to use the HH:MM time
539 of the day at point (if any) or the current HH:MM time."
541 (when (and org-capture-use-agenda-date
542 (eq major-mode
'org-agenda-mode
))
543 (setq org-overriding-default-time
544 (org-get-cursor-date (equal goto
1))))
546 ((equal goto
'(4)) (org-capture-goto-target))
547 ((equal goto
'(16)) (org-capture-goto-last-stored))
549 ;; FIXME: Are these needed?
550 (let* ((orig-buf (current-buffer))
551 (annotation (if (and (boundp 'org-capture-link-is-already-stored
)
552 org-capture-link-is-already-stored
)
553 (plist-get org-store-link-plist
:annotation
)
554 (ignore-errors (org-store-link nil
))))
555 (entry (or org-capture-entry
(org-capture-select-template keys
)))
557 (setq initial
(or org-capture-initial
558 (and (org-region-active-p)
559 (buffer-substring (point) (mark)))))
560 (when (stringp initial
)
561 (remove-text-properties 0 (length initial
) '(read-only t
) initial
))
562 (when (stringp annotation
)
563 (remove-text-properties 0 (length annotation
)
564 '(read-only t
) annotation
))
567 (customize-variable 'org-capture-templates
))
571 (org-capture-set-plist entry
)
572 (org-capture-get-template)
573 (org-capture-put :original-buffer orig-buf
574 :original-file
(or (buffer-file-name orig-buf
)
575 (and (featurep 'dired
)
578 :original-file-nondirectory
579 (and (buffer-file-name orig-buf
)
580 (file-name-nondirectory
581 (buffer-file-name orig-buf
)))
582 :annotation annotation
584 :return-to-wconf
(current-window-configuration)
586 (or org-overriding-default-time
588 (org-capture-set-target-location)
589 (condition-case error
590 (org-capture-put :template
(org-capture-fill-template))
592 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
593 (error "Capture abort: %s" error
)))
595 (setq org-capture-clock-keep
(org-capture-get :clock-keep
))
598 (org-capture-insert-template-here)
599 (condition-case error
600 (org-capture-place-template
601 (equal (car (org-capture-get :target
)) 'function
))
603 (if (and (buffer-base-buffer (current-buffer))
604 (string-match "\\`CAPTURE-" (buffer-name)))
605 (kill-buffer (current-buffer)))
606 (set-window-configuration (org-capture-get :return-to-wconf
))
607 (error "Capture template `%s': %s"
608 (org-capture-get :key
)
610 (if (and (derived-mode-p 'org-mode
)
611 (org-capture-get :clock-in
))
614 (if (org-clock-is-active)
615 (org-capture-put :interrupted-clock
616 (copy-marker org-clock-marker
)))
618 (org-set-local 'org-capture-clock-was-started t
))
620 "Could not start the clock in this capture buffer")))
621 (if (org-capture-get :immediate-finish
)
622 (org-capture-finalize)))))))))
624 (defun org-capture-get-template ()
625 "Get the template from a file or a function if necessary."
626 (let ((txt (org-capture-get :template
)) file
)
628 ((and (listp txt
) (eq (car txt
) 'file
))
630 (setq file
(expand-file-name (nth 1 txt
) org-directory
)))
631 (setq txt
(org-file-contents file
))
632 (setq txt
(format "* Template file %s not found" (nth 1 txt
)))))
633 ((and (listp txt
) (eq (car txt
) 'function
))
634 (if (fboundp (nth 1 txt
))
635 (setq txt
(funcall (nth 1 txt
)))
636 (setq txt
(format "* Template function %s not found" (nth 1 txt
)))))
637 ((not txt
) (setq txt
""))
639 (t (setq txt
"* Invalid capture template")))
640 (org-capture-put :template txt
)))
642 (defun org-capture-finalize (&optional stay-with-capture
)
643 "Finalize the capture process.
644 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
645 captured item after finalizing."
647 (when (org-capture-get :jump-to-captured
)
648 (setq stay-with-capture t
))
649 (unless (and org-capture-mode
650 (buffer-base-buffer (current-buffer)))
651 (error "This does not seem to be a capture buffer for Org-mode"))
653 (run-hooks 'org-capture-prepare-finalize-hook
)
655 ;; Did we start the clock in this capture buffer?
656 (when (and org-capture-clock-was-started
657 org-clock-marker
(marker-buffer org-clock-marker
)
658 (equal (marker-buffer org-clock-marker
) (buffer-base-buffer))
659 (> org-clock-marker
(point-min))
660 (< org-clock-marker
(point-max)))
661 ;; Looks like the clock we started is still running. Clock out.
662 (when (not org-capture-clock-keep
) (let (org-log-note-clock-out) (org-clock-out)))
663 (when (and (not org-capture-clock-keep
)
664 (org-capture-get :clock-resume
'local
)
665 (markerp (org-capture-get :interrupted-clock
'local
))
666 (buffer-live-p (marker-buffer
667 (org-capture-get :interrupted-clock
'local
))))
668 (let ((clock-in-task (org-capture-get :interrupted-clock
'local
)))
669 (org-with-point-at clock-in-task
671 (message "Interrupted clock has been resumed")))
673 (let ((beg (point-min))
676 ;; Store the size of the capture buffer
677 (org-capture-put :captured-entry-size
(- (point-max) (point-min)))
679 ;; Store the insertion point in the target buffer
680 (org-capture-put :insertion-point
(point))
683 (let ((m1 (org-capture-get :begin-marker
'local
))
684 (m2 (org-capture-get :end-marker
'local
)))
685 (if (and m1 m2
(= m1 beg
) (= m2 end
))
687 (setq m2
(if (cdr (assoc 'heading org-blank-before-new-entry
))
689 m2
(if (< (point-max) m2
) (point-max) m2
))
690 (setq abort-note
'clean
)
692 (setq abort-note
'dirty
)))
694 ;; Make sure that the empty lines after are correct
695 (when (and (> (point-max) end
) ; indeed, the buffer was still narrowed
696 (member (org-capture-get :type
'local
)
697 '(entry item checkitem plain
)))
700 (or (bolp) (newline))
701 (org-capture-empty-lines-after
702 (or (org-capture-get :empty-lines-after
'local
)
703 (org-capture-get :empty-lines
'local
) 0))))
704 ;; Postprocessing: Update Statistics cookies, do the sorting
705 (when (derived-mode-p 'org-mode
)
707 (when (ignore-errors (org-back-to-heading))
708 (org-update-parent-todo-statistics)
709 (org-update-checkbox-count)))
710 ;; FIXME Here we should do the sorting
711 ;; If we have added a table line, maybe recompute?
712 (when (and (eq (org-capture-get :type
'local
) 'table-line
)
714 (if (org-table-get-stored-formulas)
715 (org-table-recalculate 'all
) ;; FIXME: Should we iterate???
717 ;; Store this place as the last one where we stored something
718 ;; Do the marking in the base buffer, so that it makes sense after
719 ;; the indirect buffer has been killed.
720 (when org-capture-bookmark
721 (org-capture-bookmark-last-stored-position))
724 (run-hooks 'org-capture-before-finalize-hook
))
726 (when (org-capture-get :decrypted
)
728 (goto-char (org-capture-get :decrypted
))
729 (org-encrypt-entry)))
731 ;; Kill the indirect buffer
733 (let ((return-wconf (org-capture-get :return-to-wconf
'local
))
734 (new-buffer (org-capture-get :new-buffer
'local
))
735 (kill-buffer (org-capture-get :kill-buffer
'local
))
736 (base-buffer (buffer-base-buffer (current-buffer))))
738 ;; Kill the indirect buffer
739 (kill-buffer (current-buffer))
741 ;; Narrow back the target buffer to its previous state
742 (with-current-buffer (org-capture-get :buffer
)
743 (let ((reg (org-capture-get :initial-target-region
))
744 (pos (org-capture-get :initial-target-position
))
745 (ipt (org-capture-get :insertion-point
))
746 (size (org-capture-get :captured-entry-size
)))
749 (cond ((< ipt
(car reg
))
750 ;; insertion point is before the narrowed region
751 (narrow-to-region (+ size
(car reg
)) (+ size
(cdr reg
))))
753 ;; insertion point is after the narrowed region
754 (narrow-to-region (car reg
) (cdr reg
)))
756 ;; insertion point is within the narrowed region
757 (narrow-to-region (car reg
) (+ size
(cdr reg
)))))
758 ;; now place back the point at its original position
759 (if (< ipt
(car reg
))
760 (goto-char (+ size pos
))
761 (goto-char (if (< ipt pos
) (+ size pos
) pos
))))))
763 ;; Kill the target buffer if that is desired
764 (when (and base-buffer new-buffer kill-buffer
)
765 (with-current-buffer base-buffer
(save-buffer))
766 (kill-buffer base-buffer
))
768 ;; Restore the window configuration before capture
769 (set-window-configuration return-wconf
))
771 (run-hooks 'org-capture-after-finalize-hook
)
776 ((equal abort-note
'clean
)
777 (message "Capture process aborted and target buffer cleaned up"))
778 ((equal abort-note
'dirty
)
779 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
781 (org-capture-goto-last-stored)))
782 ;; Return if we did store something
785 (defun org-capture-refile ()
786 "Finalize the current capture and then refile the entry.
787 Refiling is done from the base buffer, because the indirect buffer is then
788 already gone. Any prefix argument will be passed to the refile command."
790 (unless (eq (org-capture-get :type
'local
) 'entry
)
792 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
794 (base (buffer-base-buffer (current-buffer)))
795 (org-refile-for-capture t
))
796 (save-window-excursion
797 (with-current-buffer (or base
(current-buffer))
802 (call-interactively 'org-refile
)))))
803 (org-capture-finalize)))
805 (defun org-capture-kill ()
806 "Abort the current capture process."
808 ;; FIXME: This does not do the right thing, we need to remove the
809 ;; new stuff by hand it is easy: undo, then kill the buffer
810 (let ((org-note-abort t
)
811 (org-capture-before-finalize-hook nil
))
812 (org-capture-finalize)))
814 (defun org-capture-goto-last-stored ()
815 "Go to the location where the last capture note was stored."
817 (org-goto-marker-or-bmk org-capture-last-stored-marker
818 (plist-get org-bookmark-names-plist
820 (message "This is the last note stored by a capture process"))
822 ;;; Supporting functions for handling the process
824 (defun org-capture-put-target-region-and-position ()
825 "Store the initial region with `org-capture-put'."
827 :initial-target-region
828 ;; Check if the buffer is currently narrowed
829 (when (org-buffer-narrowed-p)
830 (cons (point-min) (point-max))))
831 ;; store the current point
832 (org-capture-put :initial-target-position
(point)))
834 (defvar org-time-was-given
) ; dynamically scoped parameter
835 (defun org-capture-set-target-location (&optional target
)
836 "Find TARGET buffer and position.
837 Store them in the capture property list."
838 (let ((target-entry-p t
) decrypted-hl-pos
)
839 (setq target
(or target
(org-capture-get :target
)))
842 ((eq (car target
) 'file
)
843 (set-buffer (org-capture-target-buffer (nth 1 target
)))
844 (org-capture-put-target-region-and-position)
846 (setq target-entry-p nil
))
848 ((eq (car target
) 'id
)
849 (let ((loc (org-id-find (nth 1 target
))))
851 (error "Cannot find target ID \"%s\"" (nth 1 target
))
852 (set-buffer (org-capture-target-buffer (car loc
)))
854 (org-capture-put-target-region-and-position)
855 (goto-char (cdr loc
)))))
857 ((eq (car target
) 'file
+headline
)
858 (set-buffer (org-capture-target-buffer (nth 1 target
)))
859 (org-capture-put-target-region-and-position)
861 (let ((hd (nth 2 target
)))
862 (goto-char (point-min))
863 (unless (derived-mode-p 'org-mode
)
865 "Target buffer \"%s\" for file+headline should be in Org mode"
867 (if (re-search-forward
868 (format org-complex-heading-regexp-format
(regexp-quote hd
))
870 (goto-char (point-at-bol))
871 (goto-char (point-max))
872 (or (bolp) (insert "\n"))
873 (insert "* " hd
"\n")
874 (beginning-of-line 0))))
876 ((eq (car target
) 'file
+olp
)
877 (let ((m (org-find-olp
878 (cons (org-capture-expand-file (nth 1 target
))
880 (set-buffer (marker-buffer m
))
881 (org-capture-put-target-region-and-position)
885 ((eq (car target
) 'file
+regexp
)
886 (set-buffer (org-capture-target-buffer (nth 1 target
)))
887 (org-capture-put-target-region-and-position)
889 (goto-char (point-min))
890 (if (re-search-forward (nth 2 target
) nil t
)
892 (goto-char (if (org-capture-get :prepend
)
893 (match-beginning 0) (match-end 0)))
894 (org-capture-put :exact-position
(point))
895 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
896 (error "No match for target regexp in file %s" (nth 1 target
))))
898 ((memq (car target
) '(file+datetree file
+datetree
+prompt
))
899 (require 'org-datetree
)
900 (set-buffer (org-capture-target-buffer (nth 1 target
)))
901 (org-capture-put-target-region-and-position)
903 ;; Make a date tree entry, with the current date (or yesterday,
904 ;; if we are extending dates for a couple of hours)
905 (org-datetree-find-date-create
906 (calendar-gregorian-from-absolute
908 (org-overriding-default-time
909 ;; use the overriding default time
910 (time-to-days org-overriding-default-time
))
912 ((eq (car target
) 'file
+datetree
+prompt
)
914 (let ((prompt-time (org-read-date
915 nil t nil
"Date for tree entry:"
919 (cond ((and (or (not (boundp 'org-time-was-given
))
920 (not org-time-was-given
))
921 (not (= (time-to-days prompt-time
) (org-today))))
922 ;; Use 00:00 when no time is given for another date than today?
923 (apply 'encode-time
(append '(0 0 0) (cdddr (decode-time prompt-time
)))))
924 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer
)
925 ;; Replace any time range by its start
927 (org-read-date-analyze
928 (replace-match "\\1 \\2" nil nil org-read-date-final-answer
)
929 prompt-time
(decode-time prompt-time
))))
931 (time-to-days prompt-time
)))
933 ;; current date, possibly corrected for late night workers
936 ((eq (car target
) 'file
+function
)
937 (set-buffer (org-capture-target-buffer (nth 1 target
)))
938 (org-capture-put-target-region-and-position)
940 (funcall (nth 2 target
))
941 (org-capture-put :exact-position
(point))
942 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
944 ((eq (car target
) 'function
)
945 (funcall (nth 1 target
))
946 (org-capture-put :exact-position
(point))
947 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
949 ((eq (car target
) 'clock
)
950 (if (and (markerp org-clock-hd-marker
)
951 (marker-buffer org-clock-hd-marker
))
952 (progn (set-buffer (marker-buffer org-clock-hd-marker
))
953 (org-capture-put-target-region-and-position)
955 (goto-char org-clock-hd-marker
))
956 (error "No running clock that could be used as capture target")))
958 (t (error "Invalid capture target specification")))
960 (when (and (featurep 'org-crypt
) (org-at-encrypted-entry-p))
962 (setq decrypted-hl-pos
963 (save-excursion (and (org-back-to-heading t
) (point)))))
965 (org-capture-put :buffer
(current-buffer) :pos
(point)
966 :target-entry-p target-entry-p
967 :decrypted decrypted-hl-pos
))))
969 (defun org-capture-expand-file (file)
970 "Expand functions and symbols for FILE.
971 When FILE is a function, call it. When it is a form, evaluate
972 it. When it is a variable, retrieve the value. When it is
973 a string, return it. However, if it is the empty string, return
974 `org-default-notes-file' instead."
976 ((equal file
"") org-default-notes-file
)
977 ((org-string-nw-p file
) file
)
978 ((functionp file
) (funcall file
))
979 ((and (symbolp file
) (boundp file
)) (symbol-value file
))
980 ((consp file
) (eval file
))
983 (defun org-capture-target-buffer (file)
984 "Get a buffer for FILE."
985 (setq file
(org-capture-expand-file file
))
986 (setq file
(or (org-string-nw-p file
)
987 org-default-notes-file
988 (error "No notes file specified, and no default available")))
989 (or (org-find-base-buffer-visiting file
)
990 (progn (org-capture-put :new-buffer t
)
991 (find-file-noselect (expand-file-name file org-directory
)))))
993 (defun org-capture-steal-local-variables (buffer)
994 "Install Org-mode local variables of BUFFER."
996 (ignore-errors (org-set-local (car v
) (cdr v
))))
997 (buffer-local-variables buffer
)))
999 (defun org-capture-place-template (&optional inhibit-wconf-store
)
1000 "Insert the template at the target location, and display the buffer.
1001 When `inhibit-wconf-store', don't store the window configuration, as it
1002 may have been stored before."
1003 (unless inhibit-wconf-store
1004 (org-capture-put :return-to-wconf
(current-window-configuration)))
1005 (delete-other-windows)
1006 (org-switch-to-buffer-other-window
1007 (org-capture-get-indirect-buffer (org-capture-get :buffer
) "CAPTURE"))
1010 (goto-char (org-capture-get :pos
))
1011 (org-set-local 'org-capture-target-marker
1013 (org-set-local 'outline-level
'org-outline-level
)
1014 (let* ((template (org-capture-get :template
))
1015 (type (org-capture-get :type
)))
1017 ((nil entry
) (org-capture-place-entry))
1018 (table-line (org-capture-place-table-line))
1019 (plain (org-capture-place-plain-text))
1020 (item (org-capture-place-item))
1021 (checkitem (org-capture-place-item))))
1022 (org-capture-mode 1)
1023 (org-set-local 'org-capture-current-plist org-capture-plist
))
1025 (defun org-capture-place-entry ()
1026 "Place the template as a new Org entry."
1027 (let* ((txt (org-capture-get :template
))
1028 (reversed (org-capture-get :prepend
))
1029 (target-entry-p (org-capture-get :target-entry-p
))
1032 (and (org-capture-get :exact-position
)
1033 (goto-char (org-capture-get :exact-position
)))
1035 ((not target-entry-p
)
1036 ;; Insert as top-level entry, either at beginning or at end of file
1039 (progn (goto-char (point-min))
1040 (or (org-at-heading-p)
1041 (outline-next-heading)))
1042 (goto-char (point-max))
1043 (or (bolp) (insert "\n"))))
1045 ;; Insert as a child of the current entry
1046 (and (looking-at "\\*+")
1047 (setq level
(- (match-end 0) (match-beginning 0))))
1048 (setq level
(org-get-valid-level (or level
1) 1))
1051 (outline-next-heading)
1052 (or (bolp) (insert "\n")))
1053 (org-end-of-subtree t nil
)
1054 (or (bolp) (insert "\n")))))
1055 (org-capture-empty-lines-before)
1057 (org-capture-verify-tree txt
)
1058 (org-paste-subtree level txt
'for-yank
)
1059 (org-capture-empty-lines-after 1)
1060 (org-capture-position-for-last-stored beg
)
1061 (outline-next-heading)
1063 (org-capture-mark-kill-region beg
(1- end
))
1064 (org-capture-narrow beg
(1- end
))
1065 (if (or (re-search-backward "%\\?" beg t
)
1066 (re-search-forward "%\\?" end t
))
1067 (replace-match ""))))
1069 (defun org-capture-place-item ()
1070 "Place the template as a new plain list item."
1071 (let* ((txt (org-capture-get :template
))
1072 (target-entry-p (org-capture-get :target-entry-p
))
1075 (if (org-capture-get :exact-position
)
1076 (goto-char (org-capture-get :exact-position
))
1078 ((not target-entry-p
)
1079 ;; Insert as top-level entry, either at beginning or at end of file
1080 (setq beg
(point-min) end
(point-max)))
1082 (setq beg
(1+ (point-at-eol))
1083 end
(save-excursion (outline-next-heading) (point)))))
1085 (if (org-capture-get :prepend
)
1088 (when (org-list-search-forward (org-item-beginning-re) end t
)
1089 (goto-char (match-beginning 0))
1090 (setq ind
(org-get-indentation))))
1092 (when (org-list-search-backward (org-item-beginning-re) beg t
)
1093 (setq ind
(org-get-indentation))
1095 (unless ind
(goto-char end
)))
1096 ;; Remove common indentation
1097 (setq txt
(org-remove-indentation txt
))
1098 ;; Make sure this is indeed an item
1099 (unless (string-match (concat "\\`" (org-item-re)) txt
)
1100 (setq txt
(concat "- "
1101 (mapconcat 'identity
(split-string txt
"\n")
1103 ;; Prepare surrounding empty lines.
1104 (org-capture-empty-lines-before)
1106 (unless (eolp) (save-excursion (insert "\n")))
1109 (setq ind
(org-get-indentation))
1110 (delete-region beg
(point)))
1111 ;; Set the correct indentation, depending on context
1112 (setq ind
(make-string ind ?\
))
1113 (setq txt
(concat ind
1114 (mapconcat 'identity
(split-string txt
"\n")
1119 (org-capture-empty-lines-after 1)
1120 (org-capture-position-for-last-stored beg
)
1123 (org-capture-mark-kill-region beg
(1- end
))
1124 (org-capture-narrow beg
(1- end
))
1125 (if (or (re-search-backward "%\\?" beg t
)
1126 (re-search-forward "%\\?" end t
))
1127 (replace-match ""))))
1129 (defun org-capture-place-table-line ()
1130 "Place the template as a table line."
1131 (require 'org-table
)
1132 (let* ((txt (org-capture-get :template
))
1133 (target-entry-p (org-capture-get :target-entry-p
))
1134 (table-line-pos (org-capture-get :table-line-pos
))
1137 ((org-capture-get :exact-position
)
1138 (goto-char (org-capture-get :exact-position
)))
1139 ((not target-entry-p
)
1140 ;; Table is not necessarily under a heading
1141 (setq beg
(point-min) end
(point-max)))
1143 ;; WE are at a heading, limit search to the body
1144 (setq beg
(1+ (point-at-eol))
1145 end
(save-excursion (outline-next-heading) (point)))))
1146 (if (re-search-forward org-table-dataline-regexp end t
)
1147 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t
))
1149 (if (looking-at "[ \t]*#\\+tblfm:")
1151 (narrow-to-region b
(point)))
1153 (insert "\n| |\n|----|\n| |\n")
1154 (narrow-to-region (1+ end
) (point)))
1155 ;; We are narrowed to the table, or to an empty line if there was no table
1157 ;; Check if the template is good
1158 (if (not (string-match org-table-dataline-regexp txt
))
1159 (setq txt
"| %?Bad template |\n"))
1160 (if (functionp table-line-pos
)
1161 (setq table-line-pos
(funcall table-line-pos
))
1162 (setq table-line-pos
(eval table-line-pos
)))
1164 ((and table-line-pos
1165 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos
))
1166 ;; we have a complex line specification
1167 (let ((ll (ignore-errors
1168 (save-match-data (org-table-analyze))
1169 (aref org-table-hlines
1170 (- (match-end 1) (match-beginning 1)))))
1171 (delta (string-to-number (match-string 2 table-line-pos
))))
1172 ;; The user wants a special position in the table
1174 (error "Invalid table line specification \"%s\"" table-line-pos
))
1175 (goto-char org-table-current-begin-pos
)
1176 (forward-line (+ ll delta
(if (< delta
0) 0 -
1)))
1177 (org-table-insert-row 'below
)
1178 (beginning-of-line 1)
1179 (delete-region (point) (1+ (point-at-eol)))
1182 (setq end
(point))))
1183 ((org-capture-get :prepend
)
1184 (goto-char (point-min))
1185 (re-search-forward org-table-hline-regexp nil t
)
1186 (beginning-of-line 1)
1187 (re-search-forward org-table-dataline-regexp nil t
)
1188 (beginning-of-line 1)
1190 (org-table-insert-row)
1191 (beginning-of-line 1)
1192 (delete-region (point) (1+ (point-at-eol)))
1196 (goto-char (point-max))
1197 (re-search-backward org-table-dataline-regexp nil t
)
1198 (beginning-of-line 1)
1199 (org-table-insert-row 'below
)
1200 (beginning-of-line 1)
1201 (delete-region (point) (1+ (point-at-eol)))
1204 (setq end
(point))))
1206 (org-capture-position-for-last-stored 'table-line
)
1207 (if (or (re-search-backward "%\\?" beg t
)
1208 (re-search-forward "%\\?" end t
))
1212 (defun org-capture-place-plain-text ()
1213 "Place the template plainly.
1214 If the target locator points at an Org node, place the template into
1215 the text of the entry, before the first child. If not, place the
1216 template at the beginning or end of the file.
1217 Of course, if exact position has been required, just put it there."
1218 (let* ((txt (org-capture-get :template
))
1221 ((org-capture-get :exact-position
)
1222 (goto-char (org-capture-get :exact-position
)))
1223 ((and (org-capture-get :target-entry-p
)
1225 (looking-at org-outline-regexp
))
1226 ;; we should place the text into this entry
1227 (if (org-capture-get :prepend
)
1228 ;; Skip meta data and drawers
1229 (org-end-of-meta-data t
)
1230 ;; go to ent of the entry text, before the next headline
1231 (outline-next-heading)))
1233 ;; beginning or end of file
1234 (goto-char (if (org-capture-get :prepend
) (point-min) (point-max)))))
1235 (or (bolp) (newline))
1236 (org-capture-empty-lines-before)
1239 (org-capture-empty-lines-after 1)
1240 (org-capture-position-for-last-stored beg
)
1242 (org-capture-mark-kill-region beg
(1- end
))
1243 (org-capture-narrow beg
(1- end
))
1244 (if (or (re-search-backward "%\\?" beg t
)
1245 (re-search-forward "%\\?" end t
))
1246 (replace-match ""))))
1248 (defun org-capture-mark-kill-region (beg end
)
1249 "Mark the region that will have to be killed when aborting capture."
1250 (let ((m1 (move-marker (make-marker) beg
))
1251 (m2 (move-marker (make-marker) end
)))
1252 (org-capture-put :begin-marker m1
)
1253 (org-capture-put :end-marker m2
)))
1255 (defun org-capture-position-for-last-stored (where)
1256 "Memorize the position that should later become the position of last capture."
1259 (org-capture-put :position-for-last-stored
1260 (move-marker (make-marker) where
1261 (or (buffer-base-buffer (current-buffer))
1262 (current-buffer)))))
1263 ((eq where
'table-line
)
1264 (org-capture-put :position-for-last-stored
1266 (org-table-current-dline))))
1267 (t (error "This should not happen"))))
1269 (defun org-capture-bookmark-last-stored-position ()
1270 "Bookmark the last-captured position."
1271 (let* ((where (org-capture-get :position-for-last-stored
'local
))
1274 (prog1 (marker-position where
)
1275 (move-marker where nil
)))
1276 ((and (listp where
) (eq (car where
) 'table-line
))
1277 (if (org-at-table-p)
1279 (org-table-goto-line (nth 1 where
))
1282 (with-current-buffer (buffer-base-buffer (current-buffer))
1287 (let ((bookmark-name (plist-get org-bookmark-names-plist
1290 (with-demoted-errors
1291 (bookmark-set bookmark-name
))))
1292 (move-marker org-capture-last-stored-marker
(point)))))))
1294 (defun org-capture-narrow (beg end
)
1295 "Narrow, unless configuration says not to narrow."
1296 (unless (org-capture-get :unnarrowed
)
1297 (narrow-to-region beg end
)
1300 (defun org-capture-empty-lines-before (&optional n
)
1301 "Set the correct number of empty lines before the insertion point.
1302 Point will be after the empty lines, so insertion can directly be done."
1303 (setq n
(or n
(org-capture-get :empty-lines-before
)
1304 (org-capture-get :empty-lines
) 0))
1305 (let ((pos (point)))
1306 (org-back-over-empty-lines)
1307 (delete-region (point) pos
)
1308 (if (> n
0) (newline n
))))
1310 (defun org-capture-empty-lines-after (&optional n
)
1311 "Set the correct number of empty lines after the inserted string.
1312 Point will remain at the first line after the inserted text."
1313 (setq n
(or n
(org-capture-get :empty-lines-after
)
1314 (org-capture-get :empty-lines
) 0))
1315 (org-back-over-empty-lines)
1316 (while (looking-at "[ \t]*\n") (replace-match ""))
1317 (let ((pos (point)))
1318 (if (> n
0) (newline n
))
1321 (defvar org-clock-marker
) ; Defined in org.el
1323 (defun org-capture-insert-template-here ()
1324 "Insert the capture template at point."
1325 (let* ((template (org-capture-get :template
))
1326 (type (org-capture-get :type
))
1328 (or (bolp) (newline))
1331 ((and (eq type
'entry
) (derived-mode-p 'org-mode
))
1332 (org-capture-verify-tree (org-capture-get :template
))
1333 (org-paste-subtree nil template t
))
1334 ((and (memq type
'(item checkitem
))
1335 (derived-mode-p 'org-mode
)
1336 (save-excursion (skip-chars-backward " \t\n")
1341 (skip-chars-backward " ")
1342 (skip-chars-backward "-+*0123456789).")
1343 (delete-region (point) (point-at-eol))
1345 (org-remove-indentation template
)
1347 (org-capture-empty-lines-after)
1352 (t (insert template
)))
1355 (if (re-search-forward "%\\?" end t
)
1356 (replace-match ""))))
1358 (defun org-capture-set-plist (entry)
1359 "Initialize the property list from the template definition."
1360 (setq org-capture-plist
(copy-sequence (nthcdr 5 entry
)))
1361 (org-capture-put :key
(car entry
) :description
(nth 1 entry
)
1362 :target
(nth 3 entry
))
1363 (let ((txt (nth 4 entry
)) (type (or (nth 2 entry
) 'entry
)))
1364 (when (or (not txt
) (and (stringp txt
) (not (string-match "\\S-" txt
))))
1365 ;; The template may be empty or omitted for special types.
1366 ;; Here we insert the default templates for such cases.
1368 ((eq type
'item
) (setq txt
"- %?"))
1369 ((eq type
'checkitem
) (setq txt
"- [ ] %?"))
1370 ((eq type
'table-line
) (setq txt
"| %? |"))
1371 ((member type
'(nil entry
)) (setq txt
"* %?\n %a"))))
1372 (org-capture-put :template txt
:type type
)))
1374 (defun org-capture-goto-target (&optional template-key
)
1375 "Go to the target location of a capture template.
1376 The user is queried for the template."
1378 (let* (org-select-template-temp-major-mode
1379 (entry (org-capture-select-template template-key
)))
1381 (error "No capture template selected"))
1382 (org-capture-set-plist entry
)
1383 (org-capture-set-target-location)
1384 (org-pop-to-buffer-same-window (org-capture-get :buffer
))
1385 (goto-char (org-capture-get :pos
))))
1387 (defun org-capture-get-indirect-buffer (&optional buffer prefix
)
1388 "Make an indirect buffer for a capture process.
1389 Use PREFIX as a prefix for the name of the indirect buffer."
1390 (setq buffer
(or buffer
(current-buffer)))
1391 (let ((n 1) (base (buffer-name buffer
)) bname
)
1392 (setq bname
(concat prefix
"-" base
))
1393 (while (buffer-live-p (get-buffer bname
))
1394 (setq bname
(concat prefix
"-" (number-to-string (incf n
)) "-" base
)))
1396 (make-indirect-buffer buffer bname
'clone
)
1398 (let ((buf (make-indirect-buffer buffer bname
)))
1399 (with-current-buffer buf
(org-mode))
1402 (defun org-capture-verify-tree (tree)
1403 "Throw error if TREE is not a valid tree."
1404 (unless (org-kill-is-subtree-p tree
)
1405 (error "Template is not a valid Org entry or tree")))
1407 (defun org-mks (table title
&optional prompt specials
)
1408 "Select a member of an alist with multiple keys.
1409 TABLE is the alist which should contain entries where the car is a string.
1410 There should be two types of entries.
1412 1. prefix descriptions like (\"a\" \"Description\")
1413 This indicates that `a' is a prefix key for multi-letter selection, and
1414 that there are entries following with keys like \"ab\", \"ax\"...
1416 2. Selectable members must have more than two elements, with the first
1417 being the string of keys that lead to selecting it, and the second a
1418 short description string of the item.
1420 The command will then make a temporary buffer listing all entries
1421 that can be selected with a single key, and all the single key
1422 prefixes. When you press the key for a single-letter entry, it is selected.
1423 When you press a prefix key, the commands (and maybe further prefixes)
1424 under this key will be shown and offered for selection.
1426 TITLE will be placed over the selection in the temporary buffer,
1427 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1428 also (\"key\" \"description\") entries. When one of these is selection,
1429 only the bare key is returned."
1430 (setq prompt
(or prompt
"Select: "))
1431 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1432 current prefix rtn re pressed buffer
(inhibit-quit t
))
1433 (save-window-excursion
1434 (setq buffer
(org-switch-to-buffer-other-window "*Org Select*"))
1435 (setq orig-table table
)
1439 (insert title
"\n\n")
1444 (setq prefix
(if current
(concat current
" ") ""))
1447 ((and (= 2 (length (car tbl
))) (= (length (caar tbl
)) 1))
1448 ;; This is a description on this level
1449 (setq dkey
(caar tbl
) ddesc
(cadar tbl
))
1451 (push dkey des-keys
)
1452 (push dkey allowed-keys
)
1453 (insert prefix
"[" dkey
"]" "..." " " ddesc
"..." "\n")
1454 ;; Skip keys which are below this prefix
1455 (setq re
(concat "\\`" (regexp-quote dkey
)))
1456 (let (case-fold-search)
1457 (while (and tbl
(string-match re
(caar tbl
))) (pop tbl
))))
1458 ((= 2 (length (car tbl
)))
1459 ;; Not yet a usable description, skip it
1462 ;; usable entry on this level
1463 (insert prefix
"[" (caar tbl
) "]" " " (nth 1 (car tbl
)) "\n")
1464 (push (caar tbl
) allowed-keys
)
1467 (insert "-------------------------------------------------------------------------------\n")
1468 (let ((sp specials
))
1470 (insert (format "[%s] %s\n"
1471 (caar sp
) (nth 1 (car sp
))))
1472 (push (caar sp
) allowed-keys
)
1474 (push "\C-g" allowed-keys
)
1475 (goto-char (point-min))
1476 (if (not (pos-visible-in-window-p (point-max)))
1477 (org-fit-window-to-buffer))
1479 (setq pressed
(char-to-string (read-char-exclusive)))
1480 (while (not (member pressed allowed-keys
))
1481 (message "Invalid key `%s'" pressed
) (sit-for 1)
1483 (setq pressed
(char-to-string (read-char-exclusive))))
1484 (when (equal pressed
"\C-g")
1485 (kill-buffer buffer
)
1487 (when (and (not (assoc pressed table
))
1488 (not (member pressed des-keys
))
1489 (assoc pressed specials
))
1490 (throw 'exit
(setq rtn pressed
)))
1491 (unless (member pressed des-keys
)
1492 (throw 'exit
(setq rtn
(rassoc (cdr (assoc pressed table
))
1494 (setq current
(concat current pressed
))
1497 (if (and (> (length (car x
)) 1)
1498 (equal (substring (car x
) 0 1) pressed
))
1499 (cons (substring (car x
) 1) (cdr x
))
1502 (setq table
(remove nil table
)))))
1503 (when buffer
(kill-buffer buffer
))
1506 ;;; The template code
1507 (defun org-capture-select-template (&optional keys
)
1508 "Select a capture template.
1509 Lisp programs can force the template by setting KEYS to a string."
1510 (let ((org-capture-templates
1511 (or (org-contextualize-keys
1512 org-capture-templates org-capture-templates-contexts
)
1513 '(("t" "Task" entry
(file+headline
"" "Tasks")
1514 "* TODO %?\n %u\n %a")))))
1516 (or (assoc keys org-capture-templates
)
1517 (error "No capture template referred to by \"%s\" keys" keys
))
1518 (org-mks org-capture-templates
1519 "Select a capture template\n========================="
1521 '(("C" "Customize org-capture-templates")
1524 (defun org-capture-fill-template (&optional template initial annotation
)
1525 "Fill a template and return the filled template as a string.
1526 The template may still contain \"%?\" for cursor positioning."
1527 (setq template
(or template
(org-capture-get :template
)))
1528 (when (stringp initial
)
1529 (setq initial
(org-no-properties initial
)))
1530 (let* ((buffer (org-capture-get :buffer
))
1531 (file (buffer-file-name (or (buffer-base-buffer buffer
) buffer
)))
1532 (ct (org-capture-get :default-time
))
1533 (dct (decode-time ct
))
1535 (if (< (nth 2 dct
) org-extend-today-until
)
1536 (encode-time 0 59 23 (1- (nth 3 dct
)) (nth 4 dct
) (nth 5 dct
))
1538 (plist-p (if org-store-link-plist t nil
))
1539 (v-c (and (> (length kill-ring
) 0) (current-kill 0)))
1540 (v-x (or (org-get-x-clipboard 'PRIMARY
)
1541 (org-get-x-clipboard 'CLIPBOARD
)
1542 (org-get-x-clipboard 'SECONDARY
)))
1543 (v-t (format-time-string (car org-time-stamp-formats
) ct1
))
1544 (v-T (format-time-string (cdr org-time-stamp-formats
) ct1
))
1545 (v-u (concat "[" (substring v-t
1 -
1) "]"))
1546 (v-U (concat "[" (substring v-T
1 -
1) "]"))
1547 ;; `initial' and `annotation' might habe been passed.
1548 ;; But if the property list has them, we prefer those values
1549 (v-i (or (plist-get org-store-link-plist
:initial
)
1551 (org-capture-get :initial
)
1553 (v-a (or (plist-get org-store-link-plist
:annotation
)
1555 (org-capture-get :annotation
)
1557 ;; Is the link empty? Then we do not want it...
1558 (v-a (if (equal v-a
"[[]]") "" v-a
))
1559 (clipboards (remove nil
(list v-i
1560 (org-get-x-clipboard 'PRIMARY
)
1561 (org-get-x-clipboard 'CLIPBOARD
)
1562 (org-get-x-clipboard 'SECONDARY
)
1564 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1565 (v-A (if (and v-a
(string-match l-re v-a
))
1566 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a
)
1568 (v-l (if (and v-a
(string-match l-re v-a
))
1569 (replace-match "\\1" nil nil v-a
)
1571 (v-n user-full-name
)
1572 (v-k (if (marker-buffer org-clock-marker
)
1573 (org-no-properties org-clock-heading
)))
1574 (v-K (if (marker-buffer org-clock-marker
)
1575 (org-make-link-string
1576 (buffer-file-name (marker-buffer org-clock-marker
))
1577 org-clock-heading
)))
1578 (v-f (or (org-capture-get :original-file-nondirectory
) ""))
1579 (v-F (or (org-capture-get :original-file
) ""))
1581 (org-startup-folded nil
)
1582 (org-inhibit-startup t
)
1583 org-time-was-given org-end-time-was-given x
1584 prompt completions char time pos default histvar strings
)
1586 (setq org-store-link-plist
1587 (plist-put org-store-link-plist
:annotation v-a
)
1588 org-store-link-plist
1589 (plist-put org-store-link-plist
:initial v-i
))
1592 (unless template
(setq template
"") (message "No template") (ding)
1594 (save-window-excursion
1595 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1598 (goto-char (point-min))
1599 (org-capture-steal-local-variables buffer
)
1600 (setq buffer-file-name nil mark-active nil
)
1602 ;; %[] Insert contents of a file.
1603 (goto-char (point-min))
1604 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t
)
1605 (unless (org-capture-escaped-%
)
1606 (let ((start (match-beginning 0))
1608 (filename (expand-file-name (match-string 1))))
1610 (delete-region start end
)
1611 (condition-case error
1612 (insert-file-contents filename
)
1613 (error (insert (format "%%![Couldn not insert %s: %s]"
1614 filename error
)))))))
1617 (goto-char (point-min))
1618 (while (re-search-forward "%<\\([^>\n]+\\)>" nil t
)
1619 (replace-match (format-time-string (match-string 1)) t t
))
1622 (goto-char (point-min))
1623 (while (re-search-forward "%\\([tTuUaliAcxkKInfF]\\)" nil t
)
1624 (unless (org-capture-escaped-%
)
1625 (when (and initial
(equal (match-string 0) "%i"))
1627 (let* ((lead (buffer-substring
1628 (point-at-bol) (match-beginning 0))))
1629 (setq v-i
(mapconcat 'identity
1630 (org-split-string initial
"\n")
1631 (concat "\n" lead
))))))
1632 (replace-match (or (eval (intern (concat "v-" (match-string 1)))) "")
1635 ;; From the property list
1637 (goto-char (point-min))
1638 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t
)
1639 (unless (org-capture-escaped-%
)
1640 (and (setq x
(or (plist-get org-store-link-plist
1641 (intern (match-string 1))) ""))
1642 (replace-match x t t
)))))
1644 ;; %() embedded elisp
1645 (goto-char (point-min))
1646 (org-capture-expand-embedded-elisp)
1648 ;; Turn on org-mode in temp buffer, set local variables
1649 ;; This is to support completion in interactive prompts
1650 (let ((org-inhibit-startup t
)) (org-mode))
1651 ;; Interactive template entries
1652 (goto-char (point-min))
1653 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t
)
1654 (unless (org-capture-escaped-%
)
1655 (setq char
(if (match-end 3) (match-string-no-properties 3))
1656 prompt
(if (match-end 2) (match-string-no-properties 2)))
1657 (goto-char (match-beginning 0))
1659 (setq completions nil default nil
)
1661 (setq completions
(org-split-string prompt
"|")
1662 prompt
(pop completions
)
1663 default
(car completions
)
1664 histvar
(intern (concat
1665 "org-capture-template-prompt-history::"
1667 completions
(mapcar 'list completions
)))
1668 (unless (boundp histvar
) (set histvar nil
))
1670 ((member char
'("G" "g"))
1671 (let* ((org-last-tags-completion-table
1672 (org-global-tags-completion-table
1673 (if (equal char
"G")
1675 (and file
(list file
)))))
1676 (org-add-colon-after-tag-completion t
)
1677 (ins (org-icompleting-read
1678 (if prompt
(concat prompt
": ") "Tags: ")
1679 'org-tags-completion-function nil nil nil
1680 'org-tags-history
)))
1681 (setq ins
(mapconcat 'identity
1683 ins
(org-re "[^[:alnum:]_@#%]+"))
1685 (when (string-match "\\S-" ins
)
1686 (or (equal (char-before) ?
:) (insert ":"))
1688 (or (equal (char-after) ?
:) (insert ":"))
1689 (and (org-at-heading-p)
1690 (let ((org-ignore-region t
))
1691 (org-set-tags nil
'align
))))))
1693 (cond ((= (length clipboards
) 1) (insert (car clipboards
)))
1694 ((> (length clipboards
) 1)
1695 (insert (read-string "Clipboard/kill value: "
1696 (car clipboards
) '(clipboards .
1)
1697 (car clipboards
))))))
1699 (cond ((= (length clipboards
) 1)
1700 (org-insert-link 0 (car clipboards
)))
1701 ((> (length clipboards
) 1)
1702 (org-insert-link 0 (read-string "Clipboard/kill value: "
1705 (car clipboards
))))))
1707 (org-set-property (org-no-properties prompt
) nil
))
1709 ;; These are the date/time related ones
1710 (setq org-time-was-given
(equal (upcase char
) char
))
1711 (setq time
(org-read-date (equal (upcase char
) char
) t nil
1713 (if (equal (upcase char
) char
) (setq org-time-was-given t
))
1714 (org-insert-time-stamp time org-time-was-given
1715 (member char
'("u" "U"))
1716 nil nil
(list org-end-time-was-given
)))
1718 (let (org-completion-use-ido)
1719 (push (org-completing-read-no-i
1720 (concat (if prompt prompt
"Enter string")
1721 (if default
(concat " [" default
"]"))
1723 completions nil nil nil histvar default
)
1725 (insert (car strings
)))))))
1726 ;; Replace %n escapes with nth %^{...} string
1727 (setq strings
(nreverse strings
))
1728 (goto-char (point-min))
1729 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t
)
1730 (unless (org-capture-escaped-%
)
1732 (nth (1- (string-to-number (match-string 1))) strings
)
1734 ;; Make sure there are no empty lines before the text, and that
1735 ;; it ends with a newline character
1736 (goto-char (point-min))
1737 (while (looking-at "[ \t]*\n") (replace-match ""))
1738 (if (re-search-forward "[ \t\n]*\\'" nil t
) (replace-match "\n"))
1739 ;; Return the expanded template and kill the temporary buffer
1740 (untabify (point-min) (point-max))
1741 (set-buffer-modified-p nil
)
1742 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1744 (defun org-capture-escaped-%
()
1745 "Check if % was escaped - if yes, unescape it now."
1746 (if (equal (char-before (match-beginning 0)) ?
\\)
1748 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1752 (defun org-capture-expand-embedded-elisp ()
1753 "Evaluate embedded elisp %(sexp) and replace with the result."
1754 (goto-char (point-min))
1755 (while (re-search-forward "%(" nil t
)
1756 (unless (org-capture-escaped-%
)
1757 (goto-char (match-beginning 0))
1758 (let ((template-start (point)))
1760 (let* ((sexp (read (current-buffer)))
1762 (org-capture--expand-keyword-in-embedded-elisp sexp
))))
1763 (delete-region template-start
(point))
1765 (if (stringp result
)
1767 (error "Capture template sexp `%s' must evaluate to string or nil"
1770 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1771 "Recursively replace capture link keywords in ATTR sexp.
1772 Such keywords are prefixed with \"%:\". See
1773 `org-capture-template' for more information."
1775 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr
))
1777 (let* ((attr-symbol (symbol-name attr
))
1778 (key (and (string-match "%\\(:.*\\)" attr-symbol
)
1779 (intern (match-string 1 attr-symbol
)))))
1780 (or (plist-get org-store-link-plist key
)
1784 (defun org-capture-inside-embedded-elisp-p ()
1785 "Return non-nil if point is inside of embedded elisp %(sexp)."
1787 (with-syntax-table emacs-lisp-mode-syntax-table
1789 ;; `looking-at' and `search-backward' below do not match the "%(" if
1790 ;; point is in its middle
1791 (when (equal (char-before) ?%
)
1794 (when (or (looking-at "%(") (search-backward "%(" nil t
))
1796 (setq end
(progn (forward-char) (forward-sexp) (1- (point)))))))
1798 (and (<= (point) end
) (>= (point) beg
))))))
1801 (defun org-capture-import-remember-templates ()
1802 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1804 (when (and (yes-or-no-p
1805 "Import old remember templates into org-capture-templates? ")
1807 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1808 (require 'org-remember
)
1809 (setq org-capture-templates
1812 (let ((desc (car entry
))
1813 (key (char-to-string (nth 1 entry
)))
1814 (template (nth 2 entry
))
1815 (file (or (nth 3 entry
) org-default-notes-file
))
1816 (position (or (nth 4 entry
) org-remember-default-headline
))
1818 (prepend org-reverse-note-order
)
1819 immediate target jump-to-captured
)
1821 ((member position
'(top bottom
))
1822 (setq target
(list 'file file
)
1823 prepend
(eq position
'top
)))
1824 ((eq position
'date-tree
)
1825 (setq target
(list 'file
+datetree file
)
1827 (t (setq target
(list 'file
+headline file position
))))
1829 (when (string-match "%!" template
)
1830 (setq template
(replace-match "" t t template
)
1833 (when (string-match "%&" template
)
1834 (setq jump-to-captured t
))
1836 (append (list key desc type target template
)
1837 (if prepend
'(:prepend t
))
1838 (if immediate
'(:immediate-finish t
))
1839 (if jump-to-captured
'(:jump-to-captured t
)))))
1841 org-remember-templates
))))
1843 (provide 'org-capture
)
1845 ;;; org-capture.el ends here