1 ;;; org-capture.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2010-2013 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 same functionality
28 ;; that is also 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 for this
34 ;; 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?
55 (declare-function org-datetree-find-date-create
"org-datetree"
56 (date &optional keep-restriction
))
57 (declare-function org-table-get-specials
"org-table" ())
58 (declare-function org-table-goto-line
"org-table" (N))
59 (declare-function org-pop-to-buffer-same-window
"org-compat"
60 (&optional buffer-or-name norecord label
))
61 (declare-function org-at-encrypted-entry-p
"org-crypt" ())
62 (declare-function org-encrypt-entry
"org-crypt" ())
63 (declare-function org-decrypt-entry
"org-crypt" ())
65 (defvar org-remember-default-headline
)
66 (defvar org-remember-templates
)
67 (defvar org-table-hlines
)
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 :empty-lines Set this to the number of lines the should be inserted
186 before and after the new item. Default 0, only common
189 :empty-lines-before Set this to the number of lines the should be inserted
190 before the new item. Overrides :empty-lines for the
191 number lines inserted before.
193 :empty-lines-after Set this to the number of lines the should be inserted
194 after the new item. Overrides :empty-lines for the
195 number of lines inserted after.
197 :clock-in Start the clock in this item.
199 :clock-keep Keep the clock running when filing the captured entry.
201 :clock-resume Start the interrupted clock when finishing the capture.
202 Note that :clock-keep has precedence over :clock-resume.
203 When setting both to `t', the current clock will run and
204 the previous one will not be resumed.
206 :unnarrowed Do not narrow the target buffer, simply show the
207 full buffer. Default is to narrow it so that you
208 only see the new stuff.
210 :table-line-pos Specification of the location in the table where the
211 new line should be inserted. It should be a string like
212 \"II-3\", meaning that the new line should become the
213 third line before the second horizontal separator line.
215 :kill-buffer If the target file was not yet visited by a buffer when
216 capture was invoked, kill the buffer again after capture
219 The template defines the text to be inserted. Often this is an
220 org-mode entry (so the first line should start with a star) that
221 will be filed as a child of the target headline. It can also be
222 freely formatted text. Furthermore, the following %-escapes will
223 be replaced with content and expanded in this order:
225 %[pathname] Insert the contents of the file given by `pathname'.
226 %(sexp) Evaluate elisp `(sexp)' and replace it with the results.
227 For convenience, %:keyword (see below) placeholders within
228 the expression will be expanded prior to this.
229 %<...> The result of format-time-string on the ... format specification.
230 %t Time stamp, date only.
231 %T Time stamp with date and time.
232 %u, %U Like the above, but inactive time stamps.
233 %i Initial content, copied from the active region. If %i is
234 indented, the entire inserted text will be indented as well.
235 %a Annotation, normally the link created with `org-store-link'.
236 %A Like %a, but prompt for the description part.
237 %l Like %a, but only insert the literal link.
238 %c Current kill ring head.
239 %x Content of the X clipboard.
240 %k Title of currently clocked task.
241 %K Link to currently clocked task.
242 %n User name (taken from `user-full-name').
243 %f File visited by current buffer when org-capture was called.
244 %F Full path of the file or directory visited by current buffer.
245 %:keyword Specific information for certain link types, see below.
246 %^g Prompt for tags, with completion on tags in target file.
247 %^G Prompt for tags, with completion on all tags in all agenda files.
248 %^t Like %t, but prompt for date. Similarly %^T, %^u, %^U.
249 You may define a prompt like: %^{Please specify birthday}t
250 %^C Interactive selection of which kill or clip to use.
251 %^L Like %^C, but insert as link.
252 %^{prop}p Prompt the user for a value for property `prop'.
253 %^{prompt} Prompt the user for a string and replace this sequence with it.
254 A default value and a completion table ca be specified like this:
255 %^{prompt|default|completion2|completion3|...}.
256 %? After completing the template, position cursor here.
257 %\\n Insert the text entered at the nth %^{prompt}, where `n' is
258 a number, starting from 1.
260 Apart from these general escapes, you can access information specific to
261 the link type that is created. For example, calling `org-capture' in emails
262 or in Gnus will record the author and the subject of the message, which you
263 can access with \"%:from\" and \"%:subject\", respectively. Here is a
264 complete list of what is recorded for each link type.
266 Link type | Available information
267 ------------------------+------------------------------------------------------
268 bbdb | %:type %:name %:company
269 vm, wl, mh, mew, rmail, | %:type %:subject %:message-id
270 gnus | %:from %:fromname %:fromaddress
271 | %:to %:toname %:toaddress
272 | %:fromto (either \"to NAME\" or \"from NAME\")
273 | %:date %:date-timestamp (as active timestamp)
274 | %:date-timestamp-inactive (as inactive timestamp)
275 gnus | %:group, for messages also all email fields
276 w3, w3m | %:type %:url
277 info | %:type %:file %:node
278 calendar | %:type %:date"
283 (choice :value
("" "" entry
(file "~/org/notes.org") "")
284 (list :tag
"Multikey description"
285 (string :tag
"Keys ")
286 (string :tag
"Description"))
287 (list :tag
"Template entry"
288 (string :tag
"Keys ")
289 (string :tag
"Description ")
290 (choice :tag
"Capture Type " :value entry
291 (const :tag
"Org entry" entry
)
292 (const :tag
"Plain list item" item
)
293 (const :tag
"Checkbox item" checkitem
)
294 (const :tag
"Plain text" plain
)
295 (const :tag
"Table line" table-line
))
296 (choice :tag
"Target location"
298 (const :format
"" file
)
301 (const :format
"" id
)
303 (list :tag
"File & Headline"
304 (const :format
"" file
+headline
)
306 (string :tag
" Headline"))
307 (list :tag
"File & Outline path"
308 (const :format
"" file
+olp
)
310 (repeat :tag
"Outline path" :inline t
311 (string :tag
"Headline")))
312 (list :tag
"File & Regexp"
313 (const :format
"" file
+regexp
)
315 (regexp :tag
" Regexp"))
316 (list :tag
"File & Date tree"
317 (const :format
"" file
+datetree
)
319 (list :tag
"File & Date tree, prompt for date"
320 (const :format
"" file
+datetree
+prompt
)
322 (list :tag
"File & function"
323 (const :format
"" file
+function
)
325 (sexp :tag
" Function"))
326 (list :tag
"Current clocking task"
327 (const :format
"" clock
))
328 (list :tag
"Function"
329 (const :format
"" function
)
330 (sexp :tag
" Function")))
331 (choice :tag
"Template"
334 (const :format
"" file
)
335 (file :tag
"Template file"))
336 (list :tag
"Function"
337 (const :format
"" function
)
338 (function :tag
"Template function")))
340 ;; Give the most common options as checkboxes
341 :options
(((const :format
"%v " :prepend
) (const t
))
342 ((const :format
"%v " :immediate-finish
) (const t
))
343 ((const :format
"%v " :empty-lines
) (const 1))
344 ((const :format
"%v " :clock-in
) (const t
))
345 ((const :format
"%v " :clock-keep
) (const t
))
346 ((const :format
"%v " :clock-resume
) (const t
))
347 ((const :format
"%v " :unnarrowed
) (const t
))
348 ((const :format
"%v " :kill-buffer
) (const t
))))))))
350 (defcustom org-capture-before-finalize-hook nil
351 "Hook that is run right before a capture process is finalized.
352 The capture buffer is still current when this hook runs and it is
353 widened to the entire buffer."
358 (defcustom org-capture-after-finalize-hook nil
359 "Hook that is run right after a capture process is finalized.
360 Suitable for window cleanup."
365 (defcustom org-capture-prepare-finalize-hook nil
366 "Hook that is run before the finalization starts.
367 The capture buffer is current and still narrowed."
372 (defcustom org-capture-bookmark t
373 "When non-nil, add a bookmark pointing at the last stored
374 position when capturing."
379 ;;; The property list for keeping information about the capture process
381 (defvar org-capture-plist nil
382 "Plist for the current capture process, global, to avoid having to pass it.")
384 (defvar org-capture-current-plist nil
385 "Local variable holding the plist in a capture buffer.
386 This is used to store the plist for use when finishing a capture process
387 because another such process might have changed the global variable by then.
389 Each time a new capture buffer has been set up, the global `org-capture-plist'
390 is copied to this variable, which is local in the indirect buffer.")
392 (defvar org-capture-clock-keep nil
393 "Local variable to store the value of the :clock-keep parameter.
394 This is needed in case org-capture-finalize is called interactively.")
396 (defun org-capture-put (&rest stuff
)
397 "Add properties to the capture property list `org-capture-plist'."
399 (setq org-capture-plist
(plist-put org-capture-plist
400 (pop stuff
) (pop stuff
)))))
401 (defun org-capture-get (prop &optional local
)
402 "Get properties from the capture property list `org-capture-plist'.
403 When LOCAL is set, use the local variable `org-capture-current-plist',
404 this is necessary after initialization of the capture process,
405 to avoid conflicts with other active capture processes."
406 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
408 (defun org-capture-member (prop &optional local
)
409 "Is PROP a property in `org-capture-plist'.
410 When LOCAL is set, use the local variable `org-capture-current-plist',
411 this is necessary after initialization of the capture process,
412 to avoid conflicts with other active capture processes."
413 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
417 (defvar org-capture-mode-map
(make-sparse-keymap)
418 "Keymap for `org-capture-mode', a minor mode.
419 Use this map to set additional keybindings for when Org-mode is used
420 for a capture buffer.")
422 (defvar org-capture-mode-hook nil
423 "Hook for the minor `org-capture-mode'.")
425 (define-minor-mode org-capture-mode
426 "Minor mode for special key bindings in a capture buffer.
428 Turning on this mode runs the normal hook `org-capture-mode-hook'."
429 nil
" Rem" org-capture-mode-map
432 "Capture buffer. Finish `C-c C-c', refile `C-c C-w', abort `C-c C-k'."))
433 (define-key org-capture-mode-map
"\C-c\C-c" 'org-capture-finalize
)
434 (define-key org-capture-mode-map
"\C-c\C-k" 'org-capture-kill
)
435 (define-key org-capture-mode-map
"\C-c\C-w" 'org-capture-refile
)
437 ;;; The main commands
439 (defvar org-capture-initial nil
)
440 (defvar org-capture-entry nil
)
443 (defun org-capture-string (string &optional keys
)
444 (interactive "sInitial text: \n")
445 (let ((org-capture-initial string
)
446 (org-capture-entry (org-capture-select-template keys
)))
449 (defcustom org-capture-templates-contexts nil
450 "Alist of capture templates and valid contexts.
452 For example, if you have a capture template \"c\" and you want
453 this template to be accessible only from `message-mode' buffers,
456 '((\"c\" ((in-mode . \"message-mode\"))))
458 Here are the available contexts definitions:
460 in-file: command displayed only in matching files
461 in-mode: command displayed only in matching modes
462 not-in-file: command not displayed in matching files
463 not-in-mode: command not displayed in matching modes
464 in-buffer: command displayed only in matching buffers
465 not-in-buffer: command not displayed in matching buffers
466 [function]: a custom function taking no argument
468 If you define several checks, the agenda command will be
469 accessible if there is at least one valid check.
471 You can also bind a key to another agenda custom command
472 depending on contextual rules.
474 '((\"c\" \"d\" ((in-mode . \"message-mode\"))))
476 Here it means: in `message-mode buffers', use \"c\" as the
477 key for the capture template otherwise associated with \"d\".
478 \(The template originally associated with \"d\" is not displayed
479 to avoid duplicates.)"
482 :type
'(repeat (list :tag
"Rule"
483 (string :tag
" Capture key")
484 (string :tag
"Replace by template")
485 (repeat :tag
"Available when"
487 (cons :tag
"Condition"
489 (const :tag
"In file" in-file
)
490 (const :tag
"Not in file" not-in-file
)
491 (const :tag
"In buffer" in-buffer
)
492 (const :tag
"Not in buffer" not-in-buffer
)
493 (const :tag
"In mode" in-mode
)
494 (const :tag
"Not in mode" not-in-mode
))
496 (function :tag
"Custom function"))))))
498 (defcustom org-capture-use-agenda-date nil
499 "Non-nil means use the date at point when capturing from agendas.
500 When nil, you can still capturing using the date at point with \\[org-agenda-capture]]."
506 (defun org-capture (&optional goto keys
)
508 \\<org-capture-mode-map>
509 This will let you select a template from `org-capture-templates', and then
510 file the newly captured information. The text is immediately inserted
511 at the target location, and an indirect buffer is shown where you can
512 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
513 of Emacs, so that you can continue your work.
515 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
516 anything, just go to the file/headline where the selected template
517 stores its notes. With a double prefix argument \
518 \\[universal-argument] \\[universal-argument], go to the last note
521 When called with a `C-0' (zero) prefix, insert a template at point.
523 ELisp programs can set KEYS to a string associated with a template
524 in `org-capture-templates'. In this case, interactive selection
527 If `org-capture-use-agenda-date' is non-nil, capturing from the
528 agenda will use the date at point as the default date. Then, a
529 `C-1' prefix will tell the capture process to use the HH:MM time
530 of the day at point (if any) or the current HH:MM time."
532 (when (and org-capture-use-agenda-date
533 (eq major-mode
'org-agenda-mode
))
534 (setq org-overriding-default-time
535 (org-get-cursor-date (equal goto
1))))
537 ((equal goto
'(4)) (org-capture-goto-target))
538 ((equal goto
'(16)) (org-capture-goto-last-stored))
540 ;; FIXME: Are these needed?
541 (let* ((orig-buf (current-buffer))
542 (annotation (if (and (boundp 'org-capture-link-is-already-stored
)
543 org-capture-link-is-already-stored
)
544 (plist-get org-store-link-plist
:annotation
)
545 (ignore-errors (org-store-link nil
))))
546 (entry (or org-capture-entry
(org-capture-select-template keys
)))
548 (setq initial
(or org-capture-initial
549 (and (org-region-active-p)
550 (buffer-substring (point) (mark)))))
551 (when (stringp initial
)
552 (remove-text-properties 0 (length initial
) '(read-only t
) initial
))
553 (when (stringp annotation
)
554 (remove-text-properties 0 (length annotation
)
555 '(read-only t
) annotation
))
558 (customize-variable 'org-capture-templates
))
562 (org-capture-set-plist entry
)
563 (org-capture-get-template)
564 (org-capture-put :original-buffer orig-buf
565 :original-file
(or (buffer-file-name orig-buf
)
566 (and (featurep 'dired
)
569 :original-file-nondirectory
570 (and (buffer-file-name orig-buf
)
571 (file-name-nondirectory
572 (buffer-file-name orig-buf
)))
573 :annotation annotation
575 (org-capture-put :default-time
576 (or org-overriding-default-time
578 (org-capture-set-target-location)
579 (condition-case error
580 (org-capture-put :template
(org-capture-fill-template))
582 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
583 (error "Capture abort: %s" error
)))
585 (setq org-capture-clock-keep
(org-capture-get :clock-keep
))
588 (org-capture-insert-template-here)
589 (condition-case error
590 (org-capture-place-template)
592 (if (and (buffer-base-buffer (current-buffer))
593 (string-match "\\`CAPTURE-" (buffer-name)))
594 (kill-buffer (current-buffer)))
595 (set-window-configuration (org-capture-get :return-to-wconf
))
596 (error "Capture template `%s': %s"
597 (org-capture-get :key
)
599 (if (and (derived-mode-p 'org-mode
)
600 (org-capture-get :clock-in
))
603 (if (org-clock-is-active)
604 (org-capture-put :interrupted-clock
605 (copy-marker org-clock-marker
)))
607 (org-set-local 'org-capture-clock-was-started t
))
609 "Could not start the clock in this capture buffer")))
610 (if (org-capture-get :immediate-finish
)
611 (org-capture-finalize)))))))))
613 (defun org-capture-get-template ()
614 "Get the template from a file or a function if necessary."
615 (let ((txt (org-capture-get :template
)) file
)
617 ((and (listp txt
) (eq (car txt
) 'file
))
619 (setq file
(expand-file-name (nth 1 txt
) org-directory
)))
620 (setq txt
(org-file-contents file
))
621 (setq txt
(format "* Template file %s not found" (nth 1 txt
)))))
622 ((and (listp txt
) (eq (car txt
) 'function
))
623 (if (fboundp (nth 1 txt
))
624 (setq txt
(funcall (nth 1 txt
)))
625 (setq txt
(format "* Template function %s not found" (nth 1 txt
)))))
626 ((not txt
) (setq txt
""))
628 (t (setq txt
"* Invalid capture template")))
629 (org-capture-put :template txt
)))
631 (defun org-capture-finalize (&optional stay-with-capture
)
632 "Finalize the capture process.
633 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
634 captured item after finalizing."
636 (unless (and org-capture-mode
637 (buffer-base-buffer (current-buffer)))
638 (error "This does not seem to be a capture buffer for Org-mode"))
640 (run-hooks 'org-capture-prepare-finalize-hook
)
642 ;; Did we start the clock in this capture buffer?
643 (when (and org-capture-clock-was-started
644 org-clock-marker
(marker-buffer org-clock-marker
)
645 (equal (marker-buffer org-clock-marker
) (buffer-base-buffer))
646 (> org-clock-marker
(point-min))
647 (< org-clock-marker
(point-max)))
648 ;; Looks like the clock we started is still running. Clock out.
649 (when (not org-capture-clock-keep
) (let (org-log-note-clock-out) (org-clock-out)))
650 (when (and (not org-capture-clock-keep
)
651 (org-capture-get :clock-resume
'local
)
652 (markerp (org-capture-get :interrupted-clock
'local
))
653 (buffer-live-p (marker-buffer
654 (org-capture-get :interrupted-clock
'local
))))
655 (let ((clock-in-task (org-capture-get :interrupted-clock
'local
)))
656 (org-with-point-at clock-in-task
658 (message "Interrupted clock has been resumed")))
660 (let ((beg (point-min))
663 ;; Store the size of the capture buffer
664 (org-capture-put :captured-entry-size
(- (point-max) (point-min)))
666 ;; Store the insertion point in the target buffer
667 (org-capture-put :insertion-point
(point))
670 (let ((m1 (org-capture-get :begin-marker
'local
))
671 (m2 (org-capture-get :end-marker
'local
)))
672 (if (and m1 m2
(= m1 beg
) (= m2 end
))
674 (setq m2
(if (cdr (assoc 'heading org-blank-before-new-entry
))
676 m2
(if (< (point-max) m2
) (point-max) m2
))
677 (setq abort-note
'clean
)
679 (setq abort-note
'dirty
)))
681 ;; Make sure that the empty lines after are correct
682 (when (and (> (point-max) end
) ; indeed, the buffer was still narrowed
683 (member (org-capture-get :type
'local
)
684 '(entry item checkitem plain
)))
687 (or (bolp) (newline))
688 (org-capture-empty-lines-after
689 (or (org-capture-get :empty-lines-after
'local
)
690 (org-capture-get :empty-lines
'local
) 0))))
691 ;; Postprocessing: Update Statistics cookies, do the sorting
692 (when (derived-mode-p 'org-mode
)
694 (when (ignore-errors (org-back-to-heading))
695 (org-update-parent-todo-statistics)
696 (org-update-checkbox-count)))
697 ;; FIXME Here we should do the sorting
698 ;; If we have added a table line, maybe recompute?
699 (when (and (eq (org-capture-get :type
'local
) 'table-line
)
701 (if (org-table-get-stored-formulas)
702 (org-table-recalculate 'all
) ;; FIXME: Should we iterate???
704 ;; Store this place as the last one where we stored something
705 ;; Do the marking in the base buffer, so that it makes sense after
706 ;; the indirect buffer has been killed.
707 (when org-capture-bookmark
708 (org-capture-bookmark-last-stored-position))
711 (run-hooks 'org-capture-before-finalize-hook
))
713 (when (org-capture-get :decrypted
)
715 (goto-char (org-capture-get :decrypted
))
716 (org-encrypt-entry)))
718 ;; Kill the indirect buffer
720 (let ((return-wconf (org-capture-get :return-to-wconf
'local
))
721 (new-buffer (org-capture-get :new-buffer
'local
))
722 (kill-buffer (org-capture-get :kill-buffer
'local
))
723 (base-buffer (buffer-base-buffer (current-buffer))))
725 ;; Kill the indirect buffer
726 (kill-buffer (current-buffer))
728 ;; Narrow back the target buffer to its previous state
729 (with-current-buffer (org-capture-get :buffer
)
730 (let ((reg (org-capture-get :initial-target-region
))
731 (pos (org-capture-get :initial-target-position
))
732 (ipt (org-capture-get :insertion-point
))
733 (size (org-capture-get :captured-entry-size
)))
735 (cond ((< ipt
(car reg
))
736 ;; insertion point is before the narrowed region
737 (narrow-to-region (+ size
(car reg
)) (+ size
(cdr reg
))))
739 ;; insertion point is after the narrowed region
740 (narrow-to-region (car reg
) (cdr reg
)))
742 ;; insertion point is within the narrowed region
743 (narrow-to-region (car reg
) (+ size
(cdr reg
)))))
744 ;; now place back the point at its original position
745 (if (< ipt
(car reg
))
746 (goto-char (+ size pos
))
747 (goto-char (if (< ipt pos
) (+ size pos
) pos
))))))
749 ;; Kill the target buffer if that is desired
750 (when (and base-buffer new-buffer kill-buffer
)
751 (with-current-buffer base-buffer
(save-buffer))
752 (kill-buffer base-buffer
))
754 ;; Restore the window configuration before capture
755 (set-window-configuration return-wconf
))
757 (run-hooks 'org-capture-after-finalize-hook
)
762 ((equal abort-note
'clean
)
763 (message "Capture process aborted and target buffer cleaned up"))
764 ((equal abort-note
'dirty
)
765 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
767 (org-capture-goto-last-stored)))
768 ;; Return if we did store something
771 (defun org-capture-refile ()
772 "Finalize the current capture and then refile the entry.
773 Refiling is done from the base buffer, because the indirect buffer is then
774 already gone. Any prefix argument will be passed to the refile command."
776 (unless (eq (org-capture-get :type
'local
) 'entry
)
778 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
780 (base (buffer-base-buffer (current-buffer)))
781 (org-refile-for-capture t
))
782 (org-capture-finalize)
783 (save-window-excursion
784 (with-current-buffer (or base
(current-buffer))
789 (call-interactively 'org-refile
)))))))
791 (defun org-capture-kill ()
792 "Abort the current capture process."
794 ;; FIXME: This does not do the right thing, we need to remove the
795 ;; new stuff by hand it is easy: undo, then kill the buffer
796 (let ((org-note-abort t
)
797 (org-capture-before-finalize-hook nil
))
798 (org-capture-finalize)))
800 (defun org-capture-goto-last-stored ()
801 "Go to the location where the last capture note was stored."
803 (org-goto-marker-or-bmk org-capture-last-stored-marker
804 "org-capture-last-stored")
805 (message "This is the last note stored by a capture process"))
807 ;;; Supporting functions for handling the process
809 (defun org-capture-put-target-region-and-position ()
810 "Store the initial region with `org-capture-put'."
812 :initial-target-region
813 ;; Check if the buffer is currently narrowed
814 (when (/= (buffer-size) (- (point-max) (point-min)))
815 (cons (point-min) (point-max))))
816 ;; store the current point
817 (org-capture-put :initial-target-position
(point)))
819 (defvar org-time-was-given
) ; dynamically scoped parameter
820 (defun org-capture-set-target-location (&optional target
)
821 "Find TARGET buffer and position.
822 Store them in the capture property list."
823 (let ((target-entry-p t
) decrypted-hl-pos
)
824 (setq target
(or target
(org-capture-get :target
)))
827 ((eq (car target
) 'file
)
828 (set-buffer (org-capture-target-buffer (nth 1 target
)))
829 (org-capture-put-target-region-and-position)
831 (setq target-entry-p nil
))
833 ((eq (car target
) 'id
)
834 (let ((loc (org-id-find (nth 1 target
))))
836 (error "Cannot find target ID \"%s\"" (nth 1 target
))
837 (set-buffer (org-capture-target-buffer (car loc
)))
839 (org-capture-put-target-region-and-position)
840 (goto-char (cdr loc
)))))
842 ((eq (car target
) 'file
+headline
)
843 (set-buffer (org-capture-target-buffer (nth 1 target
)))
844 (org-capture-put-target-region-and-position)
846 (let ((hd (nth 2 target
)))
847 (goto-char (point-min))
848 (unless (derived-mode-p 'org-mode
)
850 "Target buffer \"%s\" for file+headline should be in Org mode"
852 (if (re-search-forward
853 (format org-complex-heading-regexp-format
(regexp-quote hd
))
855 (goto-char (point-at-bol))
856 (goto-char (point-max))
857 (or (bolp) (insert "\n"))
858 (insert "* " hd
"\n")
859 (beginning-of-line 0))))
861 ((eq (car target
) 'file
+olp
)
862 (let ((m (org-find-olp
863 (cons (org-capture-expand-file (nth 1 target
))
865 (set-buffer (marker-buffer m
))
866 (org-capture-put-target-region-and-position)
870 ((eq (car target
) 'file
+regexp
)
871 (set-buffer (org-capture-target-buffer (nth 1 target
)))
872 (org-capture-put-target-region-and-position)
874 (goto-char (point-min))
875 (if (re-search-forward (nth 2 target
) nil t
)
877 (goto-char (if (org-capture-get :prepend
)
878 (match-beginning 0) (match-end 0)))
879 (org-capture-put :exact-position
(point))
880 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
881 (error "No match for target regexp in file %s" (nth 1 target
))))
883 ((memq (car target
) '(file+datetree file
+datetree
+prompt
))
884 (require 'org-datetree
)
885 (set-buffer (org-capture-target-buffer (nth 1 target
)))
886 (org-capture-put-target-region-and-position)
888 ;; Make a date tree entry, with the current date (or yesterday,
889 ;; if we are extending dates for a couple of hours)
890 (org-datetree-find-date-create
891 (calendar-gregorian-from-absolute
893 (org-overriding-default-time
894 ;; use the overriding default time
895 (time-to-days org-overriding-default-time
))
897 ((eq (car target
) 'file
+datetree
+prompt
)
899 (let ((prompt-time (org-read-date
900 nil t nil
"Date for tree entry:"
904 (cond ((and (not org-time-was-given
)
905 (not (= (time-to-days prompt-time
) (org-today))))
906 ;; Use 00:00 when no time is given for another date than today?
907 (apply 'encode-time
(append '(0 0 0) (cdddr (decode-time prompt-time
)))))
908 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer
)
909 ;; Replace any time range by its start
911 (org-read-date-analyze
912 (replace-match "\\1 \\2" nil nil org-read-date-final-answer
)
913 prompt-time
(decode-time prompt-time
))))
915 (time-to-days prompt-time
)))
917 ;; current date, possibly corrected for late night workers
920 ((eq (car target
) 'file
+function
)
921 (set-buffer (org-capture-target-buffer (nth 1 target
)))
922 (org-capture-put-target-region-and-position)
924 (funcall (nth 2 target
))
925 (org-capture-put :exact-position
(point))
926 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
928 ((eq (car target
) 'function
)
929 (funcall (nth 1 target
))
930 (org-capture-put :exact-position
(point))
931 (setq target-entry-p
(and (derived-mode-p 'org-mode
) (org-at-heading-p))))
933 ((eq (car target
) 'clock
)
934 (if (and (markerp org-clock-hd-marker
)
935 (marker-buffer org-clock-hd-marker
))
936 (progn (set-buffer (marker-buffer org-clock-hd-marker
))
937 (org-capture-put-target-region-and-position)
939 (goto-char org-clock-hd-marker
))
940 (error "No running clock that could be used as capture target")))
942 (t (error "Invalid capture target specification")))
944 (when (and (featurep 'org-crypt
) (org-at-encrypted-entry-p))
946 (setq decrypted-hl-pos
947 (save-excursion (and (org-back-to-heading t
) (point)))))
949 (org-capture-put :buffer
(current-buffer) :pos
(point)
950 :target-entry-p target-entry-p
951 :decrypted decrypted-hl-pos
))))
953 (defun org-capture-expand-file (file)
954 "Expand functions and symbols for FILE.
955 When FILE is a function, call it. When it is a form, evaluate
956 it. When it is a variable, retrieve the value. Return whatever we get."
958 ((org-string-nw-p file
) file
)
959 ((functionp file
) (funcall file
))
960 ((and (symbolp file
) (boundp file
)) (symbol-value file
))
961 ((and file
(consp file
)) (eval file
))
964 (defun org-capture-target-buffer (file)
965 "Get a buffer for FILE."
966 (setq file
(org-capture-expand-file file
))
967 (setq file
(or (org-string-nw-p file
)
968 org-default-notes-file
969 (error "No notes file specified, and no default available")))
970 (or (org-find-base-buffer-visiting file
)
971 (progn (org-capture-put :new-buffer t
)
972 (find-file-noselect (expand-file-name file org-directory
)))))
974 (defun org-capture-steal-local-variables (buffer)
975 "Install Org-mode local variables."
977 (ignore-errors (org-set-local (car v
) (cdr v
))))
978 (buffer-local-variables buffer
)))
980 (defun org-capture-place-template ()
981 "Insert the template at the target location, and display the buffer."
982 (org-capture-put :return-to-wconf
(current-window-configuration))
983 (delete-other-windows)
984 (org-switch-to-buffer-other-window
985 (org-capture-get-indirect-buffer (org-capture-get :buffer
) "CAPTURE"))
988 (goto-char (org-capture-get :pos
))
989 (org-set-local 'org-capture-target-marker
991 (org-set-local 'outline-level
'org-outline-level
)
992 (let* ((template (org-capture-get :template
))
993 (type (org-capture-get :type
)))
995 ((nil entry
) (org-capture-place-entry))
996 (table-line (org-capture-place-table-line))
997 (plain (org-capture-place-plain-text))
998 (item (org-capture-place-item))
999 (checkitem (org-capture-place-item))))
1000 (org-capture-mode 1)
1001 (org-set-local 'org-capture-current-plist org-capture-plist
))
1003 (defun org-capture-place-entry ()
1004 "Place the template as a new Org entry."
1005 (let* ((txt (org-capture-get :template
))
1006 (reversed (org-capture-get :prepend
))
1007 (target-entry-p (org-capture-get :target-entry-p
))
1011 ((org-capture-get :exact-position
)
1012 (goto-char (org-capture-get :exact-position
)))
1013 ((not target-entry-p
)
1014 ;; Insert as top-level entry, either at beginning or at end of file
1017 (progn (goto-char (point-min))
1018 (or (org-at-heading-p)
1019 (outline-next-heading)))
1020 (goto-char (point-max))
1021 (or (bolp) (insert "\n"))))
1023 ;; Insert as a child of the current entry
1024 (and (looking-at "\\*+")
1025 (setq level
(- (match-end 0) (match-beginning 0))))
1026 (setq level
(org-get-valid-level (or level
1) 1))
1029 (outline-next-heading)
1030 (or (bolp) (insert "\n")))
1031 (org-end-of-subtree t nil
)
1032 (or (bolp) (insert "\n")))))
1033 (org-capture-empty-lines-before)
1035 (org-capture-verify-tree txt
)
1036 (org-paste-subtree level txt
'for-yank
)
1037 (org-capture-empty-lines-after 1)
1038 (org-capture-position-for-last-stored beg
)
1039 (outline-next-heading)
1041 (org-capture-mark-kill-region beg
(1- end
))
1042 (org-capture-narrow beg
(1- end
))
1043 (if (or (re-search-backward "%\\?" beg t
)
1044 (re-search-forward "%\\?" end t
))
1045 (replace-match ""))))
1047 (defun org-capture-place-item ()
1048 "Place the template as a new plain list item."
1049 (let* ((txt (org-capture-get :template
))
1050 (target-entry-p (org-capture-get :target-entry-p
))
1053 (if (org-capture-get :exact-position
)
1054 (goto-char (org-capture-get :exact-position
))
1056 ((not target-entry-p
)
1057 ;; Insert as top-level entry, either at beginning or at end of file
1058 (setq beg
(point-min) end
(point-max)))
1060 (setq beg
(1+ (point-at-eol))
1061 end
(save-excursion (outline-next-heading) (point)))))
1062 (if (org-capture-get :prepend
)
1065 (if (org-list-search-forward (org-item-beginning-re) end t
)
1067 (goto-char (match-beginning 0))
1068 (setq ind
(org-get-indentation)))
1072 (if (org-list-search-backward (org-item-beginning-re) beg t
)
1074 (setq ind
(org-get-indentation))
1077 ;; Remove common indentation
1078 (setq txt
(org-remove-indentation txt
))
1079 ;; Make sure this is indeed an item
1080 (unless (string-match (concat "\\`" (org-item-re)) txt
)
1081 (setq txt
(concat "- "
1082 (mapconcat 'identity
(split-string txt
"\n")
1084 ;; Set the correct indentation, depending on context
1085 (setq ind
(make-string ind ?\
))
1086 (setq txt
(concat ind
1087 (mapconcat 'identity
(split-string txt
"\n")
1090 ;; Insert, with surrounding empty lines
1091 (org-capture-empty-lines-before)
1094 (or (bolp) (insert "\n"))
1095 (org-capture-empty-lines-after 1)
1096 (org-capture-position-for-last-stored beg
)
1099 (org-capture-mark-kill-region beg
(1- end
))
1100 (org-capture-narrow beg
(1- end
))
1101 (if (or (re-search-backward "%\\?" beg t
)
1102 (re-search-forward "%\\?" end t
))
1103 (replace-match ""))))
1105 (defun org-capture-place-table-line ()
1106 "Place the template as a table line."
1107 (require 'org-table
)
1108 (let* ((txt (org-capture-get :template
))
1109 (target-entry-p (org-capture-get :target-entry-p
))
1110 (table-line-pos (org-capture-get :table-line-pos
))
1113 ((org-capture-get :exact-position
)
1114 (goto-char (org-capture-get :exact-position
)))
1115 ((not target-entry-p
)
1116 ;; Table is not necessarily under a heading
1117 (setq beg
(point-min) end
(point-max)))
1119 ;; WE are at a heading, limit search to the body
1120 (setq beg
(1+ (point-at-eol))
1121 end
(save-excursion (outline-next-heading) (point)))))
1122 (if (re-search-forward org-table-dataline-regexp end t
)
1123 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t
))
1125 (if (looking-at "[ \t]*#\\+tblfm:")
1127 (narrow-to-region b
(point)))
1129 (insert "\n| |\n|----|\n| |\n")
1130 (narrow-to-region (1+ end
) (point)))
1131 ;; We are narrowed to the table, or to an empty line if there was no table
1133 ;; Check if the template is good
1134 (if (not (string-match org-table-dataline-regexp txt
))
1135 (setq txt
"| %?Bad template |\n"))
1137 ((and table-line-pos
1138 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos
))
1139 ;; we have a complex line specification
1140 (goto-char (point-min))
1141 (let ((nh (- (match-end 1) (match-beginning 1)))
1142 (delta (string-to-number (match-string 2 table-line-pos
)))
1144 ;; The user wants a special position in the table
1145 (org-table-get-specials)
1146 (setq ll
(ignore-errors (aref org-table-hlines nh
)))
1147 (unless ll
(error "Invalid table line specification \"%s\""
1149 (setq ll
(+ ll delta
(if (< delta
0) 0 -
1)))
1151 (org-table-insert-row 'below
)
1152 (beginning-of-line 1)
1153 (delete-region (point) (1+ (point-at-eol)))
1156 (setq end
(point))))
1157 ((org-capture-get :prepend
)
1158 (goto-char (point-min))
1159 (re-search-forward org-table-hline-regexp nil t
)
1160 (beginning-of-line 1)
1161 (re-search-forward org-table-dataline-regexp nil t
)
1162 (beginning-of-line 1)
1164 (org-table-insert-row)
1165 (beginning-of-line 1)
1166 (delete-region (point) (1+ (point-at-eol)))
1170 (goto-char (point-max))
1171 (re-search-backward org-table-dataline-regexp nil t
)
1172 (beginning-of-line 1)
1173 (org-table-insert-row 'below
)
1174 (beginning-of-line 1)
1175 (delete-region (point) (1+ (point-at-eol)))
1178 (setq end
(point))))
1180 (org-capture-position-for-last-stored 'table-line
)
1181 (if (or (re-search-backward "%\\?" beg t
)
1182 (re-search-forward "%\\?" end t
))
1186 (defun org-capture-place-plain-text ()
1187 "Place the template plainly.
1188 If the target locator points at an Org node, place the template into
1189 the text of the entry, before the first child. If not, place the
1190 template at the beginning or end of the file.
1191 Of course, if exact position has been required, just put it there."
1192 (let* ((txt (org-capture-get :template
))
1195 ((org-capture-get :exact-position
)
1196 (goto-char (org-capture-get :exact-position
)))
1197 ((and (org-capture-get :target-entry-p
)
1199 (looking-at org-outline-regexp
))
1200 ;; we should place the text into this entry
1201 (if (org-capture-get :prepend
)
1202 ;; Skip meta data and drawers
1203 (org-end-of-meta-data-and-drawers)
1204 ;; go to ent of the entry text, before the next headline
1205 (outline-next-heading)))
1207 ;; beginning or end of file
1208 (goto-char (if (org-capture-get :prepend
) (point-min) (point-max)))))
1209 (or (bolp) (newline))
1210 (org-capture-empty-lines-before)
1213 (org-capture-empty-lines-after 1)
1214 (org-capture-position-for-last-stored beg
)
1216 (org-capture-mark-kill-region beg
(1- end
))
1217 (org-capture-narrow beg
(1- end
))
1218 (if (or (re-search-backward "%\\?" beg t
)
1219 (re-search-forward "%\\?" end t
))
1220 (replace-match ""))))
1222 (defun org-capture-mark-kill-region (beg end
)
1223 "Mark the region that will have to be killed when aborting capture."
1224 (let ((m1 (move-marker (make-marker) beg
))
1225 (m2 (move-marker (make-marker) end
)))
1226 (org-capture-put :begin-marker m1
)
1227 (org-capture-put :end-marker m2
)))
1229 (defun org-capture-position-for-last-stored (where)
1230 "Memorize the position that should later become the position of last capture."
1233 (org-capture-put :position-for-last-stored
1234 (move-marker (make-marker) where
1235 (or (buffer-base-buffer (current-buffer))
1236 (current-buffer)))))
1237 ((eq where
'table-line
)
1238 (org-capture-put :position-for-last-stored
1240 (org-table-current-dline))))
1241 (t (error "This should not happen"))))
1243 (defun org-capture-bookmark-last-stored-position ()
1244 "Bookmark the last-captured position."
1245 (let* ((where (org-capture-get :position-for-last-stored
'local
))
1248 (prog1 (marker-position where
)
1249 (move-marker where nil
)))
1250 ((and (listp where
) (eq (car where
) 'table-line
))
1251 (if (org-at-table-p)
1253 (org-table-goto-line (nth 1 where
))
1256 (with-current-buffer (buffer-base-buffer (current-buffer))
1261 (with-demoted-errors
1262 (bookmark-set "org-capture-last-stored"))
1263 (move-marker org-capture-last-stored-marker
(point)))))))
1265 (defun org-capture-narrow (beg end
)
1266 "Narrow, unless configuration says not to narrow."
1267 (unless (org-capture-get :unnarrowed
)
1268 (narrow-to-region beg end
)
1271 (defun org-capture-empty-lines-before (&optional n
)
1272 "Arrange for the correct number of empty lines before the insertion point.
1273 Point will be after the empty lines, so insertion can directly be done."
1274 (setq n
(or n
(org-capture-get :empty-lines-before
)
1275 (org-capture-get :empty-lines
) 0))
1276 (let ((pos (point)))
1277 (org-back-over-empty-lines)
1278 (delete-region (point) pos
)
1279 (if (> n
0) (newline n
))))
1281 (defun org-capture-empty-lines-after (&optional n
)
1282 "Arrange for the correct number of empty lines after the inserted string.
1283 Point will remain at the first line after the inserted text."
1284 (setq n
(or n
(org-capture-get :empty-lines-after
)
1285 (org-capture-get :empty-lines
) 0))
1286 (org-back-over-empty-lines)
1287 (while (looking-at "[ \t]*\n") (replace-match ""))
1288 (let ((pos (point)))
1289 (if (> n
0) (newline n
))
1292 (defvar org-clock-marker
) ; Defined in org.el
1294 (defun org-capture-insert-template-here ()
1295 (let* ((template (org-capture-get :template
))
1296 (type (org-capture-get :type
))
1298 (or (bolp) (newline))
1301 ((and (eq type
'entry
) (derived-mode-p 'org-mode
))
1302 (org-capture-verify-tree (org-capture-get :template
))
1303 (org-paste-subtree nil template t
))
1304 ((and (memq type
'(item checkitem
))
1305 (derived-mode-p 'org-mode
)
1306 (save-excursion (skip-chars-backward " \t\n")
1311 (skip-chars-backward " ")
1312 (skip-chars-backward "-+*0123456789).")
1313 (delete-region (point) (point-at-eol))
1315 (org-remove-indentation template
)
1317 (org-capture-empty-lines-after)
1322 (t (insert template
)))
1325 (if (re-search-forward "%\\?" end t
)
1326 (replace-match ""))))
1328 (defun org-capture-set-plist (entry)
1329 "Initialize the property list from the template definition."
1330 (setq org-capture-plist
(copy-sequence (nthcdr 5 entry
)))
1331 (org-capture-put :key
(car entry
) :description
(nth 1 entry
)
1332 :target
(nth 3 entry
))
1333 (let ((txt (nth 4 entry
)) (type (or (nth 2 entry
) 'entry
)))
1334 (when (or (not txt
) (and (stringp txt
) (not (string-match "\\S-" txt
))))
1335 ;; The template may be empty or omitted for special types.
1336 ;; Here we insert the default templates for such cases.
1338 ((eq type
'item
) (setq txt
"- %?"))
1339 ((eq type
'checkitem
) (setq txt
"- [ ] %?"))
1340 ((eq type
'table-line
) (setq txt
"| %? |"))
1341 ((member type
'(nil entry
)) (setq txt
"* %?\n %a"))))
1342 (org-capture-put :template txt
:type type
)))
1344 (defun org-capture-goto-target (&optional template-key
)
1345 "Go to the target location of a capture template.
1346 The user is queried for the template."
1348 (let* (org-select-template-temp-major-mode
1349 (entry (org-capture-select-template template-key
)))
1351 (error "No capture template selected"))
1352 (org-capture-set-plist entry
)
1353 (org-capture-set-target-location)
1354 (org-pop-to-buffer-same-window (org-capture-get :buffer
))
1355 (goto-char (org-capture-get :pos
))))
1357 (defun org-capture-get-indirect-buffer (&optional buffer prefix
)
1358 "Make an indirect buffer for a capture process.
1359 Use PREFIX as a prefix for the name of the indirect buffer."
1360 (setq buffer
(or buffer
(current-buffer)))
1361 (let ((n 1) (base (buffer-name buffer
)) bname
)
1362 (setq bname
(concat prefix
"-" base
))
1363 (while (buffer-live-p (get-buffer bname
))
1364 (setq bname
(concat prefix
"-" (number-to-string (incf n
)) "-" base
)))
1366 (make-indirect-buffer buffer bname
'clone
)
1368 (let ((buf (make-indirect-buffer buffer bname
)))
1369 (with-current-buffer buf
(org-mode))
1372 (defun org-capture-verify-tree (tree)
1373 "Throw error if TREE is not a valid tree."
1374 (unless (org-kill-is-subtree-p tree
)
1375 (error "Template is not a valid Org entry or tree")))
1377 ;;; The template code
1379 (defun org-capture-select-template (&optional keys
)
1380 "Select a capture template.
1381 Lisp programs can force the template by setting KEYS to a string."
1382 (let ((org-capture-templates
1383 (or (org-contextualize-keys
1384 org-capture-templates org-capture-templates-contexts
)
1385 '(("t" "Task" entry
(file+headline
"" "Tasks")
1386 "* TODO %?\n %u\n %a")))))
1388 (or (assoc keys org-capture-templates
)
1389 (error "No capture template referred to by \"%s\" keys" keys
))
1390 (org-mks org-capture-templates
1391 "Select a capture template\n========================="
1393 '(("C" "Customize org-capture-templates")
1396 (defun org-capture-fill-template (&optional template initial annotation
)
1397 "Fill a template and return the filled template as a string.
1398 The template may still contain \"%?\" for cursor positioning."
1399 (setq template
(or template
(org-capture-get :template
)))
1400 (when (stringp initial
)
1401 (setq initial
(org-no-properties initial
)))
1402 (let* ((buffer (org-capture-get :buffer
))
1403 (file (buffer-file-name (or (buffer-base-buffer buffer
) buffer
)))
1404 (ct (org-capture-get :default-time
))
1405 (dct (decode-time ct
))
1407 (if (< (nth 2 dct
) org-extend-today-until
)
1408 (encode-time 0 59 23 (1- (nth 3 dct
)) (nth 4 dct
) (nth 5 dct
))
1410 (plist-p (if org-store-link-plist t nil
))
1411 (v-c (and (> (length kill-ring
) 0) (current-kill 0)))
1412 (v-x (or (org-get-x-clipboard 'PRIMARY
)
1413 (org-get-x-clipboard 'CLIPBOARD
)
1414 (org-get-x-clipboard 'SECONDARY
)))
1415 (v-t (format-time-string (car org-time-stamp-formats
) ct
))
1416 (v-T (format-time-string (cdr org-time-stamp-formats
) ct
))
1417 (v-u (concat "[" (substring v-t
1 -
1) "]"))
1418 (v-U (concat "[" (substring v-T
1 -
1) "]"))
1419 ;; `initial' and `annotation' might habe been passed.
1420 ;; But if the property list has them, we prefer those values
1421 (v-i (or (plist-get org-store-link-plist
:initial
)
1423 (org-capture-get :initial
)
1425 (v-a (or (plist-get org-store-link-plist
:annotation
)
1427 (org-capture-get :annotation
)
1429 ;; Is the link empty? Then we do not want it...
1430 (v-a (if (equal v-a
"[[]]") "" v-a
))
1431 (clipboards (remove nil
(list v-i
1432 (org-get-x-clipboard 'PRIMARY
)
1433 (org-get-x-clipboard 'CLIPBOARD
)
1434 (org-get-x-clipboard 'SECONDARY
)
1436 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1437 (v-A (if (and v-a
(string-match l-re v-a
))
1438 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a
)
1440 (v-l (if (and v-a
(string-match l-re v-a
))
1441 (replace-match "\\1" nil nil v-a
)
1443 (v-n user-full-name
)
1444 (v-k (if (marker-buffer org-clock-marker
)
1445 (org-no-properties org-clock-heading
)))
1446 (v-K (if (marker-buffer org-clock-marker
)
1447 (org-make-link-string
1448 (buffer-file-name (marker-buffer org-clock-marker
))
1449 org-clock-heading
)))
1450 (v-f (or (org-capture-get :original-file-nondirectory
) ""))
1451 (v-F (or (org-capture-get :original-file
) ""))
1453 (org-startup-folded nil
)
1454 (org-inhibit-startup t
)
1455 org-time-was-given org-end-time-was-given x
1456 prompt completions char time pos default histvar strings
)
1458 (setq org-store-link-plist
1459 (plist-put org-store-link-plist
:annotation v-a
)
1460 org-store-link-plist
1461 (plist-put org-store-link-plist
:initial v-i
))
1464 (unless template
(setq template
"") (message "No template") (ding)
1466 (save-window-excursion
1467 (delete-other-windows)
1468 (org-pop-to-buffer-same-window (get-buffer-create "*Capture*"))
1471 (goto-char (point-min))
1472 (org-capture-steal-local-variables buffer
)
1473 (setq buffer-file-name nil
)
1475 ;; %[] Insert contents of a file.
1476 (goto-char (point-min))
1477 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t
)
1478 (unless (org-capture-escaped-%
)
1479 (let ((start (match-beginning 0))
1481 (filename (expand-file-name (match-string 1))))
1483 (delete-region start end
)
1484 (condition-case error
1485 (insert-file-contents filename
)
1486 (error (insert (format "%%![Couldn't insert %s: %s]"
1487 filename error
)))))))
1488 ;; %() embedded elisp
1489 (org-capture-expand-embedded-elisp)
1492 (goto-char (point-min))
1493 (while (re-search-forward "%<\\([^>\n]+\\)>" nil t
)
1494 (replace-match (format-time-string (match-string 1)) t t
))
1497 (goto-char (point-min))
1498 (while (re-search-forward "%\\([tTuUaliAcxkKInfF]\\)" nil t
)
1499 (unless (org-capture-escaped-%
)
1500 (when (and initial
(equal (match-string 0) "%i"))
1502 (let* ((lead (buffer-substring
1503 (point-at-bol) (match-beginning 0))))
1504 (setq v-i
(mapconcat 'identity
1505 (org-split-string initial
"\n")
1506 (concat "\n" lead
))))))
1507 (replace-match (or (eval (intern (concat "v-" (match-string 1)))) "")
1510 ;; From the property list
1512 (goto-char (point-min))
1513 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t
)
1514 (unless (org-capture-escaped-%
)
1515 (and (setq x
(or (plist-get org-store-link-plist
1516 (intern (match-string 1))) ""))
1517 (replace-match x t t
)))))
1519 ;; Turn on org-mode in temp buffer, set local variables
1520 ;; This is to support completion in interactive prompts
1521 (let ((org-inhibit-startup t
)) (org-mode))
1522 ;; Interactive template entries
1523 (goto-char (point-min))
1524 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t
)
1525 (unless (org-capture-escaped-%
)
1526 (setq char
(if (match-end 3) (match-string-no-properties 3))
1527 prompt
(if (match-end 2) (match-string-no-properties 2)))
1528 (goto-char (match-beginning 0))
1530 (setq completions nil default nil
)
1532 (setq completions
(org-split-string prompt
"|")
1533 prompt
(pop completions
)
1534 default
(car completions
)
1535 histvar
(intern (concat
1536 "org-capture-template-prompt-history::"
1538 completions
(mapcar 'list completions
)))
1539 (unless (boundp histvar
) (set histvar nil
))
1541 ((member char
'("G" "g"))
1542 (let* ((org-last-tags-completion-table
1543 (org-global-tags-completion-table
1544 (if (equal char
"G")
1546 (and file
(list file
)))))
1547 (org-add-colon-after-tag-completion t
)
1548 (ins (org-icompleting-read
1549 (if prompt
(concat prompt
": ") "Tags: ")
1550 'org-tags-completion-function nil nil nil
1551 'org-tags-history
)))
1552 (setq ins
(mapconcat 'identity
1554 ins
(org-re "[^[:alnum:]_@#%]+"))
1556 (when (string-match "\\S-" ins
)
1557 (or (equal (char-before) ?
:) (insert ":"))
1559 (or (equal (char-after) ?
:) (insert ":"))
1560 (and (org-at-heading-p) (org-set-tags nil
'align
)))))
1562 (cond ((= (length clipboards
) 1) (insert (car clipboards
)))
1563 ((> (length clipboards
) 1)
1564 (insert (read-string "Clipboard/kill value: "
1565 (car clipboards
) '(clipboards .
1)
1566 (car clipboards
))))))
1568 (cond ((= (length clipboards
) 1)
1569 (org-insert-link 0 (car clipboards
)))
1570 ((> (length clipboards
) 1)
1571 (org-insert-link 0 (read-string "Clipboard/kill value: "
1574 (car clipboards
))))))
1576 (org-set-property (org-no-properties prompt
) nil
))
1578 ;; These are the date/time related ones
1579 (setq org-time-was-given
(equal (upcase char
) char
))
1580 (setq time
(org-read-date (equal (upcase char
) char
) t nil
1582 (if (equal (upcase char
) char
) (setq org-time-was-given t
))
1583 (org-insert-time-stamp time org-time-was-given
1584 (member char
'("u" "U"))
1585 nil nil
(list org-end-time-was-given
)))
1587 (let (org-completion-use-ido)
1588 (push (org-completing-read-no-i
1589 (concat (if prompt prompt
"Enter string")
1590 (if default
(concat " [" default
"]"))
1592 completions nil nil nil histvar default
)
1594 (insert (car strings
)))))))
1595 ;; Replace %n escapes with nth %^{...} string
1596 (setq strings
(nreverse strings
))
1597 (goto-char (point-min))
1598 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t
)
1599 (unless (org-capture-escaped-%
)
1601 (nth (1- (string-to-number (match-string 1))) strings
)
1603 ;; Make sure there are no empty lines before the text, and that
1604 ;; it ends with a newline character
1605 (goto-char (point-min))
1606 (while (looking-at "[ \t]*\n") (replace-match ""))
1607 (if (re-search-forward "[ \t\n]*\\'" nil t
) (replace-match "\n"))
1608 ;; Return the expanded template and kill the temporary buffer
1609 (untabify (point-min) (point-max))
1610 (set-buffer-modified-p nil
)
1611 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1613 (defun org-capture-escaped-%
()
1614 "Check if % was escaped - if yes, unescape it now."
1615 (if (equal (char-before (match-beginning 0)) ?
\\)
1617 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1621 (defun org-capture-expand-embedded-elisp ()
1622 "Evaluate embedded elisp %(sexp) and replace with the result."
1623 (goto-char (point-min))
1624 (while (re-search-forward "%(" nil t
)
1625 (unless (org-capture-escaped-%
)
1626 (goto-char (match-beginning 0))
1627 (let ((template-start (point)))
1629 (let ((result (org-eval
1630 (org-capture--expand-keyword-in-embedded-elisp
1631 (read (current-buffer))))))
1632 (delete-region template-start
(point))
1633 (insert result
))))))
1635 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1636 "Recursively replace capture link keywords in ATTR sexp.
1637 Such keywords are prefixed with \"%:\". See
1638 `org-capture-template' for more information."
1640 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr
))
1642 (let* ((attr-symbol (symbol-name attr
))
1643 (key (and (string-match "%\\(:.*\\)" attr-symbol
)
1644 (intern (match-string 1 attr-symbol
)))))
1645 (or (plist-get org-store-link-plist key
)
1649 (defun org-capture-inside-embedded-elisp-p ()
1650 "Return non-nil if point is inside of embedded elisp %(sexp)."
1652 (with-syntax-table emacs-lisp-mode-syntax-table
1654 ;; `looking-at' and `search-backward' below do not match the "%(" if
1655 ;; point is in its middle
1656 (when (equal (char-before) ?%
)
1659 (when (or (looking-at "%(") (search-backward "%(" nil t
))
1661 (setq end
(progn (forward-char) (forward-sexp) (1- (point)))))))
1663 (and (<= (point) end
) (>= (point) beg
))))))
1666 (defun org-capture-import-remember-templates ()
1667 "Set org-capture-templates to be similar to `org-remember-templates'."
1669 (when (and (yes-or-no-p
1670 "Import old remember templates into org-capture-templates? ")
1672 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1673 (require 'org-remember
)
1674 (setq org-capture-templates
1677 (let ((desc (car entry
))
1678 (key (char-to-string (nth 1 entry
)))
1679 (template (nth 2 entry
))
1680 (file (or (nth 3 entry
) org-default-notes-file
))
1681 (position (or (nth 4 entry
) org-remember-default-headline
))
1683 (prepend org-reverse-note-order
)
1686 ((member position
'(top bottom
))
1687 (setq target
(list 'file file
)
1688 prepend
(eq position
'top
)))
1689 ((eq position
'date-tree
)
1690 (setq target
(list 'file
+datetree file
)
1692 (t (setq target
(list 'file
+headline file position
))))
1694 (when (string-match "%!" template
)
1695 (setq template
(replace-match "" t t template
)
1698 (append (list key desc type target template
)
1699 (if prepend
'(:prepend t
))
1700 (if immediate
'(:immediate-finish t
)))))
1702 org-remember-templates
))))
1704 (provide 'org-capture
)
1706 ;;; org-capture.el ends here