1 ;;; org-capture.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains an alternaive implementation of the same functionality
29 ;; that is also provided by org-remember.el. The implementation is more
30 ;; streamlined, can produce more target types (e.g. plain list items or
31 ;; table lines). Also, it does not use a temporary buffer for editing
32 ;; the captured entry - instead it uses an indirect buffer that visits
33 ;; the new entry already in the target buffer (this was an idea by Samuel
34 ;; Wales). John Wiegley's excellent `remember.el' is not needed for this
35 ;; implementation, even though we borrow heavily from its ideas.
37 ;; This implementation heavily draws on ideas by James TD Smith and
38 ;; Samuel Wales, and, of cause, uses John Wiegley's remember.el as inspiration.
42 ;; - find a clever way to not always insert an annotation maybe a
43 ;; predicate function that can check for conditions for %a to be
44 ;; used. This could be one of the properties.
46 ;; - Should there be plist members that arrange for properties to be
47 ;; asked for, like James proposed in his RFC?
56 (declare-function org-datetree-find-date-create
"org-datetree"
57 (DATE &optional KEEP-RESTRICTION
))
58 (declare-function org-table-get-specials
"org-table" ())
59 (declare-function org-table-goto-line
"org-table" (N))
60 (defvar org-remember-default-headline
)
61 (defvar org-remember-templates
)
62 (defvar org-table-hlines
)
64 (defvar org-capture-clock-was-started nil
65 "Internal flag, noting if the clock was started.")
67 (defvar org-capture-last-stored-marker
(make-marker)
68 "Marker pointing to the entry most recently stored with `org-capture'.")
70 ;; The following variable is scoped dynamically by org-protocol
71 ;; to indicate that the link properties have already been stored
72 (defvar org-capture-link-is-already-stored nil
)
74 (defgroup org-capture nil
75 "Options concerning capturing new entries."
79 (defcustom org-capture-templates nil
80 "Templates for the creation of new entries.
82 Each entry is a list with the following items:
84 keys The keys that will select the template, as a string, characters
85 only, for example \"a\" for a template to be selected with a
86 single key, or \"bt\" for selection with two keys. When using
87 several keys, keys using the same prefix key must be together
88 in the list and preceded by a 2-element entry explaining the
89 prefix key, for example
91 (\"b\" \"Templates for marking stuff to buy\")
93 The \"C\" key is used by default for quick access to the
94 customization of the template variable. But if you want to use
95 that key for a template, you can.
97 description A short string describing the template, will be shown during
100 type The type of entry. Valid are:
101 entry an Org-mode node, with a headline. Will be
102 filed as the child of the target entry or as
104 item a plain list item, placed in the first plain
105 list a the target location.
106 checkitem a checkbox item. This only differs from the
107 plain lis item by the default template.
108 table-line a new line in the first table at target location.
109 plain text to be inserted as it is.
111 target Specification of where the captured item should be placed.
112 In Org-mode files, targets usually define a node. Entries will
113 become children of this node, other types will be added to the
114 table or list in the body of this node.
118 (file \"path/to/file\")
119 Text will be placed at the beginning or end of that file
121 (id \"id of existing org entry\")
122 Filing as child of this entry, or in the body of the entry
124 (file+headline \"path/to/file\" \"node headline\")
125 Fast configuration if the target heading is unique in the file
127 (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
128 For non-unique headings, the full path is safer
130 (file+regexp \"path/to/file\" \"regexp to find location\")
132 (file+datetree \"path/to/file\")
133 Will create a heading in a date tree.
135 (file+function \"path/to/file\" function-finding-location)
136 A function to find the right location in the file.
139 File to the entry that is currently being clocked.
141 (function function-finding-location)
142 Most general way, write your own function to find both
146 template The template for creating the capture item. If you leave this
147 empty, an appropriate default template will be used. See below
150 The rest of the entry is a property list of additional options. Recognized
153 :prepend Normally new captured information will be appended at
154 the target location (last child, last table line,
155 last list item...). Setting this property will
158 :immediate-finish When set, do not offer to edit the information, just
159 file it away immediately. This makes sense if the
160 template only needs information that can be added
163 :empty-lines Set this to the number of lines the should be inserted
164 before and after the new item. Default 0, only common
167 :clock-in Start the clock in this item.
169 :clock-resume Start the interrupted clock when finishing the capture.
171 :unnarrowed Do not narrow the target buffer, simply show the
172 full buffer. Default is to narrow it so that you
173 only see the new stuff.
175 :table-line-pos Specification of the location in the table where the
176 new line should be inserted. It looks like \"II-3\"
177 which means that the new line should become the third
178 line before the second horizontal separaor line.
180 The template defined the text to be inserted. Often then this is an org-mode
181 entry (so the first line should start with a star) that will be filed as a
182 child of the target headline. It can also be freely formatted text.
183 Furthermore, the following %-escapes will be replaced with content:
185 %^{prompt} Prompt the user for a string and replace this sequence with it.
186 A default value and a completion table ca be specified like this:
187 %^{prompt|default|completion2|completion3|...}
188 %t time stamp, date only
189 %T time stamp with date and time
190 %u, %U like the above, but inactive time stamps
191 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
192 You may define a prompt like %^{Please specify birthday
193 %n user name (taken from `user-full-name')
194 %a annotation, normally the link created with `org-store-link'
195 %i initial content, copied from the active region. If %i is
196 indented, the entire inserted text will be indented as well.
197 %c current kill ring head
198 %x content of the X clipboard
199 %^C Interactive selection of which kill or clip to use
200 %^L Like %^C, but insert as link
201 %k title of currently clocked task
202 %K link to currently clocked task
203 %^g prompt for tags, with completion on tags in target file
204 %^G prompt for tags, with completion all tags in all agenda files
205 %^{prop}p Prompt the user for a value for property `prop'
206 %:keyword specific information for certain link types, see below
207 %[pathname] insert the contents of the file given by `pathname'
208 %(sexp) evaluate elisp `(sexp)' and replace with the result
210 %? After completing the template, position cursor here.
212 Apart from these general escapes, you can access information specific to the
213 link type that is created. For example, calling `org-capture' in emails
214 or gnus will record the author and the subject of the message, which you
215 can access with \"%:author\" and \"%:subject\", respectively. Here is a
216 complete list of what is recorded for each link type.
218 Link type | Available information
219 -------------------+------------------------------------------------------
220 bbdb | %:type %:name %:company
221 vm, wl, mh, rmail | %:type %:subject %:message-id
222 | %:from %:fromname %:fromaddress
223 | %:to %:toname %:toaddress
224 | %:fromto (either \"to NAME\" or \"from NAME\")
225 gnus | %:group, for messages also all email fields
226 w3, w3m | %:type %:url
227 info | %:type %:file %:node
228 calendar | %:type %:date"
232 (choice :value
("" "" entry
(file "~/org/notes.org") "")
233 (list :tag
"Multikey description"
234 (string :tag
"Keys ")
235 (string :tag
"Description"))
236 (list :tag
"Template entry"
237 (string :tag
"Keys ")
238 (string :tag
"Description ")
239 (choice :tag
"Capture Type " :value entry
240 (const :tag
"Org entry" entry
)
241 (const :tag
"Plain list item" item
)
242 (const :tag
"Checkbox item" checkitem
)
243 (const :tag
"Plain text" plain
)
244 (const :tag
"Table line" table-line
))
245 (choice :tag
"Target location"
247 (const :format
"" file
)
250 (const :format
"" id
)
252 (list :tag
"File & Headline"
253 (const :format
"" file
+headline
)
255 (string :tag
" Headline"))
256 (list :tag
"File & Outline path"
257 (const :format
"" file
+olp
)
259 (repeat :tag
"Outline path" :inline t
260 (string :tag
"Headline")))
261 (list :tag
"File & Regexp"
262 (const :format
"" file
+regexp
)
264 (regexp :tag
" Regexp"))
265 (list :tag
"File & Date tree"
266 (const :format
"" file
+datetree
)
268 (list :tag
"File & function"
269 (const :format
"" file
+function
)
271 (sexp :tag
" Function"))
272 (list :tag
"Current clocking task"
273 (const :format
"" clock
))
274 (list :tag
"Function"
275 (const :format
"" function
)
276 (sexp :tag
" Function")))
277 (string :tag
"Template (opt) ")
279 ;; Give the most common options as checkboxes
280 :options
(((const :format
"%v " :prepend
) (const t
))
281 ((const :format
"%v " :immediate-finish
) (const t
))
282 ((const :format
"%v " :empty-lines
) (const 1))
283 ((const :format
"%v " :clock-in
) (const t
))
284 ((const :format
"%v " :clock-resume
) (const t
))
285 ((const :format
"%v " :unnarrowed
) (const t
))))))))
287 (defcustom org-capture-before-finalize-hook nil
288 "Hook that is run right before a remember process is finalized.
289 The remember buffer is still current when this hook runs."
293 ;;; The property list for keeping information about the capture process
295 (defvar org-capture-plist nil
296 "Plist for the current capture process, global, to avoid having to pass it.")
297 (defvar org-capture-current-plist nil
298 "Local variable holding the plist in a capture buffer.
299 This is used to store the plist for use when finishing a capture process.
300 Another such process might have changed the global varaible by then.")
302 (defun org-capture-put (&rest stuff
)
304 (setq org-capture-plist
(plist-put org-capture-plist
305 (pop stuff
) (pop stuff
)))))
306 (defun org-capture-get (prop &optional local
)
307 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
309 (defun org-capture-member (prop)
310 (plist-get org-capture-plist prop
))
314 (defvar org-capture-mode-map
(make-sparse-keymap)
315 "Keymap for org-capture-mode, a minor mode.
316 Use this map to set additional keybindings for when Org-mode is used
317 for a Remember buffer.")
319 (defvar org-capture-mode-hook nil
320 "Hook for the minor `org-capture-mode'.")
322 (define-minor-mode org-capture-mode
323 "Minor mode for special key bindings in a remember buffer."
324 nil
" Rem" org-capture-mode-map
327 "Capture buffer. Finish `C-c C-c', refile `C-c C-w', abort `C-c C-k'.")
328 (run-hooks 'org-capture-mode-hook
))
329 (define-key org-capture-mode-map
"\C-c\C-c" 'org-capture-finalize
)
330 (define-key org-capture-mode-map
"\C-c\C-k" 'org-capture-kill
)
331 (define-key org-capture-mode-map
"\C-c\C-w" 'org-capture-refile
)
333 ;;; The main commands
336 (defun org-capture (&optional goto keys
)
339 This will let you select a template from org-capture-templates, and then
340 file new captured information. The text is immediately inserted at the
341 target location, and an indirect buffer is shown where you can edit it.
342 Pressing `C-c C-c' brings you back to the previous state of Emacs,
343 so that you can continue your work.
345 When called interactively with a `C-u' prefix argument GOTO, don't capture
346 anything, just go to the file/headline where the selected template
347 stores its notes. With a double prefix arg `C-u C-u', go to the last
350 When called with a `C-0' (zero) prefix, insert a template at point.
352 Lisp programs can set KEYS to a string associated with a template in
353 `org-capture-templates'. In this case, interactive selection will be
357 ((equal goto
'(4)) (org-capture-goto-target))
358 ((equal goto
'(16)) (org-capture-goto-last-stored))
360 ;; FIXME: Are these needed?
361 (let* ((orig-buf (current-buffer))
362 (annotation (if org-capture-link-is-already-stored
363 (plist-get org-store-link-plist
:annotation
)
364 (org-store-link nil
)))
365 (initial (and (org-region-active-p)
366 (buffer-substring (point) (mark))))
367 (entry (org-capture-select-template keys
)))
370 (customize-variable 'org-capture-templates
))
374 (org-capture-set-plist entry
)
375 (org-capture-put :original-buffer orig-buf
:annotation annotation
377 (org-capture-put :default-time
378 (or org-overriding-default-time
380 (org-capture-set-target-location)
381 (condition-case error
382 (org-capture-put :template
(org-capture-fill-template))
384 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
385 (error "Capture abort: %s" error
)))
389 (org-capture-insert-template-here)
390 (condition-case error
391 (org-capture-place-template)
393 (if (and (buffer-base-buffer (current-buffer))
394 (string-match "\\`CAPTURE-" (buffer-name)))
395 (kill-buffer (current-buffer)))
396 (set-window-configuration (org-capture-get :return-to-wconf
))
397 (error "Capture template `%s': %s"
398 (org-capture-get :key
)
400 (if (org-capture-get :immediate-finish
)
401 (org-capture-finalize)
402 (if (and (org-mode-p)
403 (org-capture-get :clock-in
))
406 (if (org-clock-is-active)
407 (org-capture-put :interrupted-clock
408 (copy-marker org-clock-marker
)))
410 (org-set-local 'org-capture-clock-was-started t
))
412 "Could not start the clock in this capture buffer")))))))))))
414 (defun org-capture-finalize ()
415 "Finalize the capture process."
417 (unless (and org-capture-mode
418 (buffer-base-buffer (current-buffer)))
419 (error "This does not seem to be a capture buffer for Org-mode"))
421 ;; Did we start the clock in this capture buffer?
422 (when (and org-capture-clock-was-started
423 org-clock-marker
(marker-buffer org-clock-marker
)
424 (equal (marker-buffer org-clock-marker
) (buffer-base-buffer))
425 (> org-clock-marker
(point-min))
426 (< org-clock-marker
(point-max)))
427 ;; Looks like the clock we started is still running. Clock out.
428 (let (org-log-note-clock-out) (org-clock-out))
429 (when (and (org-capture-get :clock-resume
'local
)
430 (markerp (org-capture-get :interrupted-clock
'local
))
431 (buffer-live-p (marker-buffer
432 (org-capture-get :interrupted-clock
'local
))))
433 (org-with-point-at (org-capture-get :interrupted-clock
'local
)
435 (message "Interrupted clock has been resumed")))
437 (let ((beg (point-min))
443 (let ((m1 (org-capture-get :begin-marker
'local
))
444 (m2 (org-capture-get :end-marker
'local
)))
445 (if (and m1 m2
(= m1 beg
) (= m2 end
))
447 (setq abort-note
'clean
)
449 (setq abort-note
'dirty
)))
451 ;; Make sure that the empty lines after are correct
452 (when (and (> (point-max) end
) ; indeed, the buffer was still narrowed
453 (member (org-capture-get :type
'local
)
454 '(entry item checkitem plain
)))
457 (or (bolp) (newline))
458 (org-capture-empty-lines-after
459 (or (org-capture-get :empty-lines
'local
) 0))))
460 ;; Postprocessing: Update Statistics cookies, do the sorting
463 (when (ignore-errors (org-back-to-heading))
464 (org-update-parent-todo-statistics)
465 (org-update-checkbox-count)))
466 ;; FIXME Here we should do the sorting
467 ;; If we have added a table line, maybe recompute?
468 (when (and (eq (org-capture-get :type
'local
) 'table-line
)
470 (if (org-table-get-stored-formulas)
471 (org-table-recalculate 'all
) ;; FIXME: Should we iterate???
474 ;; Store this place as the last one where we stored something
475 ;; Do the marking in the base buffer, so that it makes sense after
476 ;; the indirect buffer has been killed.
477 (org-capture-bookmark-last-stored-position)
480 (run-hooks 'org-capture-before-finalize-hook
)
483 ;; Kill the indirect buffer
485 (let ((return-wconf (org-capture-get :return-to-wconf
'local
)))
486 (kill-buffer (current-buffer))
487 ;; Restore the window configuration before capture
488 (set-window-configuration return-wconf
))
491 ((equal abort-note
'clean
)
492 (message "Capture process aborted and target file cleaned up"))
493 ((equal abort-note
'dirty
)
494 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))))
496 (defun org-capture-refile ()
497 "Finalize the current capture and then refile the entry.
498 Refiling is done from the base buffer, because the indirect buffer is then
501 (unless (eq (org-capture-get :type
'local
) 'entry
)
503 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
504 (let ((pos (point)) (base (buffer-base-buffer (current-buffer))))
505 (org-capture-finalize)
506 (save-window-excursion
507 (with-current-buffer (or base
(current-buffer))
512 (call-interactively 'org-refile
)))))))
514 (defun org-capture-kill ()
515 "Abort the current capture process."
517 ;; FIXME: This does not do the right thing, we need to remove the new stuff
518 ;; By hand it is easy: undo, then kill the buffer
519 (let ((org-note-abort t
) (org-capture-before-finalize-hook nil
))
520 (org-capture-finalize)))
522 (defun org-capture-goto-last-stored ()
523 "Go to the location where the last remember note was stored."
525 (org-goto-marker-or-bmk org-capture-last-stored-marker
526 "org-capture-last-stored")
527 (message "This is the last note stored by a capture process"))
529 ;;; Supporting functions for handling the process
531 (defun org-capture-set-target-location (&optional target
)
532 "Find target buffer and position and store then in the property list."
533 (let ((target-entry-p t
))
534 (setq target
(or target
(org-capture-get :target
)))
537 ((eq (car target
) 'file
)
538 (set-buffer (org-capture-target-buffer (nth 1 target
)))
539 (setq target-entry-p nil
))
541 ((eq (car target
) 'id
)
542 (let ((loc (org-id-find (nth 1 target
))))
544 (error "Cannot find target ID \"%s\"" (nth 1 target
))
545 (set-buffer (org-capture-target-buffer (car loc
)))
546 (goto-char (cdr loc
)))))
548 ((eq (car target
) 'file
+headline
)
549 (set-buffer (org-capture-target-buffer (nth 1 target
)))
550 (let ((hd (nth 2 target
)))
551 (goto-char (point-min))
552 (if (re-search-forward
553 (format org-complex-heading-regexp-format
(regexp-quote hd
))
555 (goto-char (point-at-bol))
556 (goto-char (point-max))
557 (or (bolp) (insert "\n"))
558 (insert "* " hd
"\n")
559 (beginning-of-line 0))))
561 ((eq (car target
) 'file
+olp
)
562 (let ((m (org-find-olp (cdr target
))))
563 (set-buffer (marker-buffer m
))
566 ((eq (car target
) 'file
+regexp
)
567 (set-buffer (org-capture-target-buffer (nth 1 target
)))
568 (goto-char (point-min))
569 (if (re-search-forward (nth 2 target
) nil t
)
571 (goto-char (if (org-capture-get :prepend
)
572 (match-beginning 0) (match-end 0)))
573 (org-capture-put :exact-position
(point))
574 (setq target-entry-p
(and (org-mode-p) (org-at-heading-p))))
575 (error "No match for target regexp in file %s" (nth 1 target
))))
577 ((eq (car target
) 'file
+datetree
)
578 (require 'org-datetree
)
579 (set-buffer (org-capture-target-buffer (nth 1 target
)))
580 ;; Make a date tree entry, with the current date (or yesterday,
581 ;; if we are extending dates for a couple of hours)
582 (org-datetree-find-date-create
583 (calendar-gregorian-from-absolute
584 (if org-overriding-default-time
585 (time-to-days org-overriding-default-time
)
587 (time-subtract (current-time)
588 (list 0 (* 3600 org-extend-today-until
) 0)))))))
590 ((eq (car target
) 'file
+function
)
591 (set-buffer (org-capture-target-buffer (nth 1 target
)))
592 (funcall (nth 2 target
))
593 (org-capture-put :exact-position
(point))
594 (setq target-entry-p
(and (org-mode-p) (org-at-heading-p))))
596 ((eq (car target
) 'clock
)
597 (if (and (markerp org-clock-hd-marker
)
598 (marker-buffer org-clock-hd-marker
))
599 (progn (set-buffer (marker-buffer org-clock-hd-marker
))
600 (goto-char org-clock-hd-marker
))
601 (error "No running clock that could be used as capture target")))
603 (t (error "Invalid capture target specification")))
605 (org-capture-put :buffer
(current-buffer) :pos
(point)
606 :target-entry-p target-entry-p
))))
608 (defun org-capture-target-buffer (file)
609 "Get a buffer for FILE."
610 (or (org-find-base-buffer-visiting file
)
611 (find-file-noselect (expand-file-name file org-directory
))))
613 (defun org-capture-steal-local-variables (buffer)
614 "Install Org-mode local variables"
616 (ignore-errors (org-set-local (car v
) (cdr v
))))
617 (buffer-local-variables buffer
)))
619 (defun org-capture-place-template ()
620 "Insert the template at the target location, and display the buffer."
621 (org-capture-put :return-to-wconf
(current-window-configuration))
622 (delete-other-windows)
623 (org-switch-to-buffer-other-window
624 (org-capture-get-indirect-buffer (org-capture-get :buffer
) "CAPTURE"))
626 (goto-char (org-capture-get :pos
))
627 (org-set-local 'org-capture-target-marker
628 (move-marker (make-marker) (point)))
629 (let* ((template (org-capture-get :template
))
630 (type (org-capture-get :type
)))
632 ((nil entry
) (org-capture-place-entry))
633 (table-line (org-capture-place-table-line))
634 (plain (org-capture-place-plain-text))
635 (item (org-capture-place-item))))
637 (org-set-local 'org-capture-current-plist org-capture-plist
))
639 (defun org-capture-place-entry ()
640 "Place the template as a new Org entry."
641 (let* ((txt (org-capture-get :template
))
642 (reversed (org-capture-get :prepend
))
643 (target-entry-p (org-capture-get :target-entry-p
))
646 ((org-capture-get :exact-position
)
647 (goto-char (org-capture-get :exact-position
)))
648 ((not target-entry-p
)
649 ;; Insert as top-level entry, either at beginning or at end of file
652 (progn (goto-char (point-min))
653 (outline-next-heading))
654 (goto-char (point-max))
655 (or (bolp) (insert "\n"))))
657 ;; Insert as a child of the current entry
658 (and (looking-at "\\*+")
659 (setq level
(- (match-end 0) (match-beginning 0))))
660 (setq level
(org-get-valid-level (or level
1) 1))
663 (outline-next-heading)
664 (or (bolp) (insert "\n")))
665 (org-end-of-subtree t t
)
666 (or (bolp) (insert "\n")))))
667 (org-capture-empty-lines-before)
669 (org-paste-subtree level txt
'for-yank
)
670 (org-capture-empty-lines-after 1)
671 (org-capture-position-for-last-stored beg
)
672 (outline-next-heading)
674 (org-capture-mark-kill-region beg
(1- end
))
675 (org-capture-narrow beg
(1- end
))
676 (if (re-search-forward "%\\?" end t
) (replace-match ""))))
678 (defun org-capture-place-item ()
679 "Place the template as a new plain list item."
680 (let* ((txt (org-capture-get :template
))
681 (target-entry-p (org-capture-get :target-entry-p
))
685 ((org-capture-get :exact-position
)
686 (goto-char (org-capture-get :exact-position
)))
687 ((not target-entry-p
)
688 ;; Insert as top-level entry, either at beginning or at end of file
689 (setq beg
(point-min) end
(point-max)))
691 (setq beg
(1+ (point-at-eol))
692 end
(save-excursion (outline-next-heading) (point)))))
693 (if (org-capture-get :prepend
)
696 (if (re-search-forward (concat "^" (org-item-re)) nil t
)
698 (goto-char (match-beginning 0))
699 (setq ind
(org-get-indentation)))
703 (if (re-search-backward (concat "^" (org-item-re)) nil t
)
705 (setq ind
(org-get-indentation))
708 ;; Remove common indentation
709 (setq txt
(org-remove-indentation txt
))
710 ;; Make sure this is indeed an item
711 (unless (string-match (concat "\\`" (org-item-re)) txt
)
712 (setq txt
(concat "- "
713 (mapconcat 'identity
(split-string txt
"\n")
715 ;; Set the correct indentation, depending on context
716 (setq ind
(make-string ind ?\
))
717 (setq txt
(concat ind
718 (mapconcat 'identity
(split-string txt
"\n")
721 ;; Insert, with surrounding empty lines
722 (org-capture-empty-lines-before)
725 (or (bolp) (insert "\n"))
726 (org-capture-empty-lines-after 1)
727 (org-capture-position-for-last-stored beg
)
730 (org-capture-mark-kill-region beg
(1- end
))
731 (org-capture-narrow beg
(1- end
))
732 (if (re-search-forward "%\\?" end t
) (replace-match ""))))
734 (defun org-capture-place-table-line ()
735 "Place the template as a table line."
737 (let* ((txt (org-capture-get :template
))
738 (target-entry-p (org-capture-get :target-entry-p
))
739 (table-line-pos (org-capture-get :table-line-pos
))
742 ((org-capture-get :exact-position
)
743 (goto-char (org-capture-get :exact-position
)))
744 ((not target-entry-p
)
745 ;; Table is not necessarily under a heading
746 (setq beg
(point-min) end
(point-max)))
748 ;; WE are at a heading, limit search to the body
749 (setq beg
(1+ (point-at-eol))
750 end
(save-excursion (outline-next-heading) (point)))))
751 (if (re-search-forward org-table-dataline-regexp end t
)
752 (let ((b (org-table-begin)) (e (org-table-end)))
754 (if (looking-at "[ \t]*#\\+TBLFM:")
756 (narrow-to-region b
(point)))
758 (insert "\n| |\n|----|\n| |\n")
759 (narrow-to-region (1+ end
) (point)))
760 ;; We are narrowed to the table, or to an empty line if there was no table
762 ;; Check if the template is good
763 (if (not (string-match org-table-dataline-regexp txt
))
764 (setq txt
"| %?Bad template |\n"))
767 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos
))
768 ;; we have a complex line specification
769 (goto-char (point-min))
770 (let ((nh (- (match-end 1) (match-beginning 1)))
771 (delta (string-to-number (match-string 2 table-line-pos
)))
773 ;; The user wants a special position in the table
774 (org-table-get-specials)
775 (setq ll
(ignore-errors (aref org-table-hlines nh
)))
776 (unless ll
(error "Invalid table line specification \"%s\""
778 (setq ll
(+ ll delta
(if (< delta
0) 0 -
1)))
780 (org-table-insert-row 'below
)
781 (beginning-of-line 1)
782 (delete-region (point) (1+ (point-at-eol)))
786 ((org-capture-get :prepend
)
787 (goto-char (point-min))
788 (re-search-forward org-table-hline-regexp nil t
)
789 (beginning-of-line 1)
790 (re-search-forward org-table-dataline-regexp nil t
)
791 (beginning-of-line 1)
793 (org-table-insert-row)
794 (beginning-of-line 1)
795 (delete-region (point) (1+ (point-at-eol)))
799 (goto-char (point-max))
800 (re-search-backward org-table-dataline-regexp nil t
)
801 (beginning-of-line 1)
802 (org-table-insert-row 'below
)
803 (beginning-of-line 1)
804 (delete-region (point) (1+ (point-at-eol)))
809 (org-capture-position-for-last-stored 'table-line
)
810 (if (re-search-forward "%\\?" end t
) (replace-match ""))
813 (defun org-capture-place-plain-text ()
814 "Place the template plainly."
815 (let* ((txt (org-capture-get :template
))
818 ((org-capture-get :exact-position
))
819 ((org-capture-get :prepend
) (point-min))
821 (or (bolp) (newline))
822 (org-capture-empty-lines-before)
825 (org-capture-empty-lines-after 1)
826 (org-capture-position-for-last-stored beg
)
828 (org-capture-mark-kill-region beg
(1- end
))
829 (org-capture-narrow beg
(1- end
))
830 (if (re-search-forward "%\\?" end t
) (replace-match ""))))
832 (defun org-capture-mark-kill-region (beg end
)
833 "Mark the region that will have to be killed when aborting capture."
834 (let ((m1 (move-marker (make-marker) beg
))
835 (m2 (move-marker (make-marker) end
)))
836 (org-capture-put :begin-marker m1
)
837 (org-capture-put :end-marker m2
)))
839 (defun org-capture-position-for-last-stored (where)
840 "Memorize the position that should later become the position of last capture."
843 (org-capture-put :position-for-last-stored
844 (move-marker (make-marker) where
845 (or (buffer-base-buffer (current-buffer))
847 ((eq where
'table-line
)
848 (org-capture-put :position-for-last-stored
850 (org-table-current-dline))))
851 (t (error "This should not happen"))))
853 (defun org-capture-bookmark-last-stored-position ()
854 "Bookmark the last-captured position."
855 (let* ((where (org-capture-get :position-for-last-stored
'local
))
858 (prog1 (marker-position where
)
859 (move-marker where nil
)))
860 ((and (listp where
) (eq (car where
) 'table-line
))
863 (org-table-goto-line (nth 1 where
))
866 (with-current-buffer (buffer-base-buffer (current-buffer))
871 (bookmark-set "org-capture-last-stored")
872 (move-marker org-capture-last-stored-marker
(point)))))))
874 (defun org-capture-narrow (beg end
)
875 "Narrow, unless configuraion says not to narrow."
876 (unless (org-capture-get :unnarrowed
)
877 (narrow-to-region beg end
)
880 (defun org-capture-empty-lines-before (&optional n
)
881 "Arrange for the correct number of empty lines before the insertion point.
882 Point will be after the empty lines, so insertion can direcetly be done."
883 (setq n
(or n
(org-capture-get :empty-lines
) 0))
885 (org-back-over-empty-lines)
886 (delete-region (point) pos
)
889 (defun org-capture-empty-lines-after (&optional n
)
890 "Arrange for the correct number of empty lines after the inserted string.
891 Point will remain at the first line after the inserted text."
892 (setq n
(or n
(org-capture-get :empty-lines
) 0))
893 (org-back-over-empty-lines)
894 (while (looking-at "[ \t]*\n") (replace-match ""))
899 (defvar org-clock-marker
) ; Defined in org.el
901 (defun org-capture-insert-template-here ()
902 (let* ((template (org-capture-get :template
))
903 (type (org-capture-get :type
))
905 (or (bolp) (newline))
908 ((and (eq type
'entry
) (org-mode-p))
909 (org-paste-subtree nil template t
))
910 ((and (memq type
'(item checkitem
))
912 (save-excursion (skip-chars-backward " \t\n")
917 (skip-chars-backward " ")
918 (skip-chars-backward "-+*0123456789).")
919 (delete-region (point) (point-at-eol))
921 (org-remove-indentation template
)
923 (org-capture-empty-lines-after)
925 (org-maybe-renumber-ordered-list)
928 (t (insert template
)))
931 (if (re-search-forward "%\\?" end t
)
932 (replace-match ""))))
934 (defun org-capture-set-plist (entry)
935 "Initialize the property list from the template definition."
936 (setq org-capture-plist
(copy-sequence (nthcdr 5 entry
)))
937 (org-capture-put :key
(car entry
) :description
(nth 1 entry
)
938 :target
(nth 3 entry
))
939 (let ((txt (nth 4 entry
)) (type (or (nth 2 entry
) 'entry
)))
940 (when (or (not txt
) (not (string-match "\\S-" txt
)))
941 ;; The template may be empty or omitted for special types.
942 ;; Here we insert the default templates for such cases.
944 ((eq type
'item
) (setq txt
"- %?"))
945 ((eq type
'checkitem
) (setq txt
"- [ ] %?"))
946 ((eq type
'table-line
) (setq txt
"| %? |"))
947 ((member type
'(nil entry
)) (setq txt
"* %?\n %a"))))
948 (org-capture-put :template txt
:type type
)))
950 (defun org-capture-goto-target (&optional template-key
)
951 "Go to the target location of a capture template.
952 The user is queried for the template."
954 (let* (org-select-template-temp-major-mode
955 (entry (org-capture-select-template template-key
)))
957 (error "No capture emplate selected"))
958 (org-capture-set-plist entry
)
959 (org-capture-set-target-location)
960 (switch-to-buffer (org-capture-get :buffer
))
961 (goto-char (org-capture-get :pos
))))
963 (defun org-capture-get-indirect-buffer (&optional buffer prefix
)
964 "Make an indirect buffer for a capture process.
965 Use PREFIX as a prefix for the name of the indirect buffer."
966 (setq buffer
(or buffer
(current-buffer)))
967 (let ((n 1) (base (buffer-name buffer
)) bname
)
968 (setq bname
(concat prefix
"-" base
))
969 (while (buffer-live-p (get-buffer bname
))
970 (setq bname
(concat prefix
"-" (number-to-string (incf n
)) "-" base
)))
972 (make-indirect-buffer buffer bname
'clone
)
973 (error (make-indirect-buffer buffer bname
)))))
976 ;;; The template code
978 (defun org-capture-select-template (&optional keys
)
979 "Select a capture template.
980 Lisp programs can force the template by setting KEYS to a string."
981 (when org-capture-templates
983 (or (assoc keys org-capture-templates
)
984 (error "No capture template referred to by \"%s\" keys" keys
))
985 (if (= 1 (length org-capture-templates
))
986 (car org-capture-templates
)
987 (org-mks org-capture-templates
988 "Select a capture template\n========================="
990 '(("C" "Customize org-capture-templates")
993 (defun org-capture-fill-template (&optional template initial annotation
)
994 "Fill a template and return the filled template as a string.
995 The template may still contain \"%?\" for cursor positioning."
996 (setq template
(or template
(org-capture-get :template
)))
997 (when (stringp initial
)
998 (setq initial
(org-no-properties initial
))
999 (remove-text-properties 0 (length initial
) '(read-only t
) initial
))
1000 (let* ((buffer (org-capture-get :buffer
))
1001 (file (buffer-file-name (or (buffer-base-buffer buffer
) buffer
)))
1002 (ct (org-capture-get :default-time
))
1003 (dct (decode-time ct
))
1005 (if (< (nth 2 dct
) org-extend-today-until
)
1006 (encode-time 0 59 23 (1- (nth 3 dct
)) (nth 4 dct
) (nth 5 dct
))
1008 (plist-p (if org-store-link-plist t nil
))
1009 (v-c (and (> (length kill-ring
) 0) (current-kill 0)))
1010 (v-x (or (org-get-x-clipboard 'PRIMARY
)
1011 (org-get-x-clipboard 'CLIPBOARD
)
1012 (org-get-x-clipboard 'SECONDARY
)))
1013 (v-t (format-time-string (car org-time-stamp-formats
) ct
))
1014 (v-T (format-time-string (cdr org-time-stamp-formats
) ct
))
1015 (v-u (concat "[" (substring v-t
1 -
1) "]"))
1016 (v-U (concat "[" (substring v-T
1 -
1) "]"))
1017 ;; `initial' and `annotation' might habe been passed.
1018 ;; But if the property list has them, we prefer those values
1019 (v-i (or (plist-get org-store-link-plist
:initial
)
1021 (org-capture-get :initial
)
1023 (v-a (or (plist-get org-store-link-plist
:annotation
)
1025 (org-capture-get :annotation
)
1027 ;; Is the link empty? Then we do not want it...
1028 (v-a (if (equal v-a
"[[]]") "" v-a
))
1029 (clipboards (remove nil
(list v-i
1030 (org-get-x-clipboard 'PRIMARY
)
1031 (org-get-x-clipboard 'CLIPBOARD
)
1032 (org-get-x-clipboard 'SECONDARY
)
1036 "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a
))
1037 (replace-match "[\\1[%^{Link description}]]" nil nil v-a
)
1039 (v-n user-full-name
)
1040 (v-k (if (marker-buffer org-clock-marker
)
1041 (org-substring-no-properties org-clock-heading
)))
1042 (v-K (if (marker-buffer org-clock-marker
)
1043 (org-make-link-string
1044 (buffer-file-name (marker-buffer org-clock-marker
))
1045 org-clock-heading
)))
1047 (org-startup-folded nil
)
1048 (org-inhibit-startup t
)
1049 org-time-was-given org-end-time-was-given x
1050 prompt completions char time pos default histvar
)
1052 (setq org-store-link-plist
1053 (plist-put org-store-link-plist
:annotation v-a
)
1054 org-store-link-plist
1055 (plist-put org-store-link-plist
:initial v-i
))
1057 (unless template
(setq template
"") (message "No template") (ding)
1059 (save-window-excursion
1060 (delete-other-windows)
1061 (switch-to-buffer (get-buffer-create "*Capture*"))
1064 (goto-char (point-min))
1065 (org-capture-steal-local-variables buffer
)
1066 (setq buffer-file-name nil
)
1068 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t
)
1069 (unless (org-capture-escaped-%
)
1070 (when (and initial
(equal (match-string 0) "%i"))
1072 (let* ((lead (buffer-substring
1073 (point-at-bol) (match-beginning 0))))
1074 (setq v-i
(mapconcat 'identity
1075 (org-split-string initial
"\n")
1076 (concat "\n" lead
))))))
1078 (or (eval (intern (concat "v-" (match-string 1)))) "")
1081 ;; %[] Insert contents of a file.
1082 (goto-char (point-min))
1083 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t
)
1084 (unless (org-capture-escaped-%
)
1085 (let ((start (match-beginning 0))
1087 (filename (expand-file-name (match-string 1))))
1089 (delete-region start end
)
1090 (condition-case error
1091 (insert-file-contents filename
)
1092 (error (insert (format "%%![Couldn't insert %s: %s]"
1093 filename error
)))))))
1094 ;; %() embedded elisp
1095 (goto-char (point-min))
1096 (while (re-search-forward "%\\((.+)\\)" nil t
)
1097 (unless (org-capture-escaped-%
)
1098 (goto-char (match-beginning 0))
1099 (let ((template-start (point)))
1102 (condition-case error
1103 (eval (read (current-buffer)))
1104 (error (format "%%![Error: %s]" error
)))))
1105 (delete-region template-start
(point))
1108 ;; From the property list
1110 (goto-char (point-min))
1111 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t
)
1112 (unless (org-capture-escaped-%
)
1113 (and (setq x
(or (plist-get org-store-link-plist
1114 (intern (match-string 1))) ""))
1115 (replace-match x t t
)))))
1117 ;; Turn on org-mode in temp buffer, set local variables
1118 ;; This is to support completion in interactive prompts
1119 (let ((org-inhibit-startup t
)) (org-mode))
1120 ;; Interactive template entries
1121 (goto-char (point-min))
1122 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
1124 (unless (org-capture-escaped-%
)
1125 (setq char
(if (match-end 3) (match-string 3))
1126 prompt
(if (match-end 2) (match-string 2)))
1127 (goto-char (match-beginning 0))
1129 (setq completions nil default nil
)
1131 (setq completions
(org-split-string prompt
"|")
1132 prompt
(pop completions
)
1133 default
(car completions
)
1134 histvar
(intern (concat
1135 "org-capture-template-prompt-history::"
1137 completions
(mapcar 'list completions
)))
1139 ((member char
'("G" "g"))
1140 (let* ((org-last-tags-completion-table
1141 (org-global-tags-completion-table
1142 (if (equal char
"G")
1144 (and file
(list file
)))))
1145 (org-add-colon-after-tag-completion t
)
1146 (ins (org-icompleting-read
1147 (if prompt
(concat prompt
": ") "Tags: ")
1148 'org-tags-completion-function nil nil nil
1149 'org-tags-history
)))
1150 (setq ins
(mapconcat 'identity
1152 ins
(org-re "[^[:alnum:]_@]+"))
1154 (when (string-match "\\S-" ins
)
1155 (or (equal (char-before) ?
:) (insert ":"))
1157 (or (equal (char-after) ?
:) (insert ":")))))
1159 (cond ((= (length clipboards
) 1) (insert (car clipboards
)))
1160 ((> (length clipboards
) 1)
1161 (insert (read-string "Clipboard/kill value: "
1162 (car clipboards
) '(clipboards .
1)
1163 (car clipboards
))))))
1165 (cond ((= (length clipboards
) 1)
1166 (org-insert-link 0 (car clipboards
)))
1167 ((> (length clipboards
) 1)
1168 (org-insert-link 0 (read-string "Clipboard/kill value: "
1171 (car clipboards
))))))
1174 ((prop (org-substring-no-properties prompt
))
1175 (pall (concat prop
"_ALL"))
1177 (with-current-buffer
1178 (get-buffer (file-name-nondirectory file
))
1179 (or (cdr (assoc pall org-file-properties
))
1180 (cdr (assoc pall org-global-properties
))
1181 (cdr (assoc pall org-global-properties-fixed
)))))
1182 (existing (with-current-buffer
1183 (get-buffer (file-name-nondirectory file
))
1184 (mapcar 'list
(org-property-values prop
))))
1185 (propprompt (concat "Value for " prop
": "))
1187 (org-completing-read
1189 (mapcar 'list
(org-split-string allowed
1192 (org-completing-read-no-i propprompt
1195 (org-set-property prop val
)))
1197 ;; These are the date/time related ones
1198 (setq org-time-was-given
(equal (upcase char
) char
))
1199 (setq time
(org-read-date (equal (upcase char
) char
) t nil
1201 (if (equal (upcase char
) char
) (setq org-time-was-given t
))
1202 (org-insert-time-stamp time org-time-was-given
1203 (member char
'("u" "U"))
1204 nil nil
(list org-end-time-was-given
)))
1206 (let (org-completion-use-ido)
1207 (insert (org-completing-read-no-i
1208 (concat (if prompt prompt
"Enter string")
1209 (if default
(concat " [" default
"]"))
1211 completions nil nil nil histvar default
)))))))
1212 ;; Make sure there are no empty lines before the text, and that
1213 ;; it ends with a newline character
1214 (goto-char (point-min))
1215 (while (looking-at "[ \t]*\n") (replace-match ""))
1216 (if (re-search-forward "[ \t\n]*\\'" nil t
) (replace-match "\n"))
1217 ;; Return the expanded tempate and kill the temporary buffer
1218 (untabify (point-min) (point-max))
1219 (set-buffer-modified-p nil
)
1220 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1222 (defun org-capture-escaped-%
()
1223 "Check if % was escaped - if yes, unescape it now."
1224 (if (equal (char-before (match-beginning 0)) ?
\\)
1226 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1231 (defun org-capture-import-remember-templates ()
1232 "Set org-capture-templates to be similar to `org-remember-templates'."
1234 (when (and (yes-or-no-p
1235 "Import old remember templates into org-capture-templates? ")
1237 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1238 (require 'org-remember
)
1239 (setq org-capture-templates
1242 (let ((desc (car entry
))
1243 (key (char-to-string (nth 1 entry
)))
1244 (template (nth 2 entry
))
1245 (file (or (nth 3 entry
) org-default-notes-file
))
1246 (position (or (nth 4 entry
) org-remember-default-headline
))
1248 (prepend org-reverse-note-order
)
1251 ((member position
'(top bottom
))
1252 (setq target
(list 'file file
)
1253 prepend
(eq position
'top
)))
1254 ((eq position
'date-tree
)
1255 (setq target
(list 'file
+datetree file
)
1257 (t (setq target
(list 'file
+headline file position
))))
1259 (when (string-match "%!" template
)
1260 (setq template
(replace-match "" t t template
)
1263 (append (list key desc type target template
)
1264 (if prepend
'(:prepend t
))
1265 (if immediate
'(:immediate-finish t
)))))
1267 org-remember-templates
))))
1269 (provide 'org-capture
)
1271 ;; arch-tag: 986bf41b-8ada-4e28-bf20-e8388a7205a0
1273 ;;; org-capture.el ends here