1 ;;; org-capture.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2010-2012 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
))
62 (defvar org-remember-default-headline
)
63 (defvar org-remember-templates
)
64 (defvar org-table-hlines
)
65 (defvar dired-buffers
)
67 (defvar org-capture-clock-was-started nil
68 "Internal flag, noting if the clock was started.")
70 (defvar org-capture-last-stored-marker
(make-marker)
71 "Marker pointing to the entry most recently stored with `org-capture'.")
73 ;; The following variable is scoped dynamically by org-protocol
74 ;; to indicate that the link properties have already been stored
75 (defvar org-capture-link-is-already-stored nil
)
77 (defgroup org-capture nil
78 "Options concerning capturing new entries."
82 (defcustom org-capture-templates nil
83 "Templates for the creation of new entries.
85 Each entry is a list with the following items:
87 keys The keys that will select the template, as a string, characters
88 only, for example \"a\" for a template to be selected with a
89 single key, or \"bt\" for selection with two keys. When using
90 several keys, keys using the same prefix key must be together
91 in the list and preceded by a 2-element entry explaining the
92 prefix key, for example
94 (\"b\" \"Templates for marking stuff to buy\")
96 The \"C\" key is used by default for quick access to the
97 customization of the template variable. But if you want to use
98 that key for a template, you can.
100 description A short string describing the template, will be shown during
103 type The type of entry. Valid types are:
104 entry an Org-mode node, with a headline. Will be
105 filed as the child of the target entry or as
107 item a plain list item, will be placed in the
108 first plain list at the target
110 checkitem a checkbox item. This differs from the
111 plain list item only is so far as it uses a
112 different default template.
113 table-line a new line in the first table at target location.
114 plain text to be inserted as it is.
116 target Specification of where the captured item should be placed.
117 In Org-mode files, targets usually define a node. Entries will
118 become children of this node, other types will be added to the
119 table or list in the body of this node.
121 Most target specifications contain a file name. If that file
122 name is the empty string, it defaults to `org-default-notes-file'.
123 A file can also be given as a variable, function, or Emacs Lisp
128 (file \"path/to/file\")
129 Text will be placed at the beginning or end of that file
131 (id \"id of existing org entry\")
132 File as child of this entry, or in the body of the entry
134 (file+headline \"path/to/file\" \"node headline\")
135 Fast configuration if the target heading is unique in the file
137 (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
138 For non-unique headings, the full path is safer
140 (file+regexp \"path/to/file\" \"regexp to find location\")
141 File to the entry matching regexp
143 (file+datetree \"path/to/file\")
144 Will create a heading in a date tree for today's date
146 (file+datetree+prompt \"path/to/file\")
147 Will create a heading in a date tree, prompts for date
149 (file+function \"path/to/file\" function-finding-location)
150 A function to find the right location in the file
153 File to the entry that is currently being clocked
155 (function function-finding-location)
156 Most general way, write your own function to find both
159 template The template for creating the capture item. If you leave this
160 empty, an appropriate default template will be used. See below
161 for more details. Instead of a string, this may also be one of
163 (file \"/path/to/template-file\")
164 (function function-returning-the-template)
166 in order to get a template from a file, or dynamically
169 The rest of the entry is a property list of additional options. Recognized
172 :prepend Normally newly captured information will be appended at
173 the target location (last child, last table line,
174 last list item...). Setting this property will
177 :immediate-finish When set, do not offer to edit the information, just
178 file it away immediately. This makes sense if the
179 template only needs information that can be added
182 :empty-lines Set this to the number of lines the should be inserted
183 before and after the new item. Default 0, only common
186 :clock-in Start the clock in this item.
188 :clock-keep Keep the clock running when filing the captured entry.
190 :clock-resume Start the interrupted clock when finishing the capture.
191 Note that :clock-keep has precedence over :clock-resume.
192 When setting both to `t', the current clock will run and
193 the previous one will not be resumed.
195 :unnarrowed Do not narrow the target buffer, simply show the
196 full buffer. Default is to narrow it so that you
197 only see the new stuff.
199 :table-line-pos Specification of the location in the table where the
200 new line should be inserted. It should be a string like
201 \"II-3\", meaning that the new line should become the
202 third line before the second horizontal separator line.
204 :kill-buffer If the target file was not yet visited by a buffer when
205 capture was invoked, kill the buffer again after capture
208 The template defines the text to be inserted. Often this is an
209 org-mode entry (so the first line should start with a star) that
210 will be filed as a child of the target headline. It can also be
211 freely formatted text. Furthermore, the following %-escapes will
212 be replaced with content and expanded in this order:
214 %[pathname] insert the contents of the file given by `pathname'.
215 %(sexp) evaluate elisp `(sexp)' and replace with the result.
216 %<...> the result of format-time-string on the ... format specification.
217 %t time stamp, date only.
218 %T time stamp with date and time.
219 %u, %U like the above, but inactive time stamps.
220 %a annotation, normally the link created with `org-store-link'.
221 %i initial content, copied from the active region. If %i is
222 indented, the entire inserted text will be indented as well.
223 %A like %a, but prompt for the description part.
224 %c current kill ring head.
225 %x content of the X clipboard.
226 %k title of currently clocked task.
227 %K link to currently clocked task.
228 %n user name (taken from `user-full-name').
229 %f file visited by current buffer when org-capture was called.
230 %F full path of the file or directory visited by current buffer.
231 %:keyword specific information for certain link types, see below.
232 %^g prompt for tags, with completion on tags in target file.
233 %^G prompt for tags, with completion on all tags in all agenda files.
234 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
235 You may define a prompt like %^{Please specify birthday.
236 %^C interactive selection of which kill or clip to use.
237 %^L like %^C, but insert as link.
238 %^{prop}p prompt the user for a value for property `prop'.
239 %^{prompt} prompt the user for a string and replace this sequence with it.
240 A default value and a completion table ca be specified like this:
241 %^{prompt|default|completion2|completion3|...}.
242 %? After completing the template, position cursor here.
244 Apart from these general escapes, you can access information specific to the
245 link type that is created. For example, calling `org-capture' in emails
246 or gnus will record the author and the subject of the message, which you
247 can access with \"%:from\" and \"%:subject\", respectively. Here is a
248 complete list of what is recorded for each link type.
250 Link type | Available information
251 ------------------------+------------------------------------------------------
252 bbdb | %:type %:name %:company
253 vm, wl, mh, mew, rmail | %:type %:subject %:message-id
254 | %:from %:fromname %:fromaddress
255 | %:to %:toname %:toaddress
256 | %:fromto (either \"to NAME\" or \"from NAME\")
258 | %:date-timestamp (as active timestamp)
259 | %:date-timestamp-inactive (as inactive timestamp)
260 gnus | %:group, for messages also all email fields
261 w3, w3m | %:type %:url
262 info | %:type %:file %:node
263 calendar | %:type %:date"
268 (choice :value
("" "" entry
(file "~/org/notes.org") "")
269 (list :tag
"Multikey description"
270 (string :tag
"Keys ")
271 (string :tag
"Description"))
272 (list :tag
"Template entry"
273 (string :tag
"Keys ")
274 (string :tag
"Description ")
275 (choice :tag
"Capture Type " :value entry
276 (const :tag
"Org entry" entry
)
277 (const :tag
"Plain list item" item
)
278 (const :tag
"Checkbox item" checkitem
)
279 (const :tag
"Plain text" plain
)
280 (const :tag
"Table line" table-line
))
281 (choice :tag
"Target location"
283 (const :format
"" file
)
286 (const :format
"" id
)
288 (list :tag
"File & Headline"
289 (const :format
"" file
+headline
)
291 (string :tag
" Headline"))
292 (list :tag
"File & Outline path"
293 (const :format
"" file
+olp
)
295 (repeat :tag
"Outline path" :inline t
296 (string :tag
"Headline")))
297 (list :tag
"File & Regexp"
298 (const :format
"" file
+regexp
)
300 (regexp :tag
" Regexp"))
301 (list :tag
"File & Date tree"
302 (const :format
"" file
+datetree
)
304 (list :tag
"File & Date tree, prompt for date"
305 (const :format
"" file
+datetree
+prompt
)
307 (list :tag
"File & function"
308 (const :format
"" file
+function
)
310 (sexp :tag
" Function"))
311 (list :tag
"Current clocking task"
312 (const :format
"" clock
))
313 (list :tag
"Function"
314 (const :format
"" function
)
315 (sexp :tag
" Function")))
316 (choice :tag
"Template"
319 (const :format
"" file
)
320 (file :tag
"Template file"))
321 (list :tag
"Function"
322 (const :format
"" function
)
323 (function :tag
"Template function")))
325 ;; Give the most common options as checkboxes
326 :options
(((const :format
"%v " :prepend
) (const t
))
327 ((const :format
"%v " :immediate-finish
) (const t
))
328 ((const :format
"%v " :empty-lines
) (const 1))
329 ((const :format
"%v " :clock-in
) (const t
))
330 ((const :format
"%v " :clock-keep
) (const t
))
331 ((const :format
"%v " :clock-resume
) (const t
))
332 ((const :format
"%v " :unnarrowed
) (const t
))
333 ((const :format
"%v " :kill-buffer
) (const t
))))))))
335 (defcustom org-capture-before-finalize-hook nil
336 "Hook that is run right before a capture process is finalized.
337 The capture buffer is still current when this hook runs and it is
338 widened to the entire buffer."
343 (defcustom org-capture-after-finalize-hook nil
344 "Hook that is run right after a capture process is finalized.
345 Suitable for window cleanup"
350 ;;; The property list for keeping information about the capture process
352 (defvar org-capture-plist nil
353 "Plist for the current capture process, global, to avoid having to pass it.")
355 (defvar org-capture-current-plist nil
356 "Local variable holding the plist in a capture buffer.
357 This is used to store the plist for use when finishing a capture process
358 because another such process might have changed the global variable by then.
360 Each time a new capture buffer has been set up, the global `org-capture-plist'
361 is copied to this variable, which is local in the indirect buffer.")
363 (defvar org-capture-clock-keep nil
364 "Local variable to store the value of the :clock-keep parameter.
365 This is needed in case org-capture-finalize is called interactively.")
367 (defun org-capture-put (&rest stuff
)
368 "Add properties to the capture property list `org-capture-plist'."
370 (setq org-capture-plist
(plist-put org-capture-plist
371 (pop stuff
) (pop stuff
)))))
372 (defun org-capture-get (prop &optional local
)
373 "Get properties from the capture property list `org-capture-plist'.
374 When LOCAL is set, use the local variable `org-capture-current-plist',
375 this is necessary after initialization of the capture process,
376 to avoid conflicts with other active capture processes."
377 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
379 (defun org-capture-member (prop &optional local
)
380 "Is PROP a property in `org-capture-plist'.
381 When LOCAL is set, use the local variable `org-capture-current-plist',
382 this is necessary after initialization of the capture process,
383 to avoid conflicts with other active capture processes."
384 (plist-get (if local org-capture-current-plist org-capture-plist
) prop
))
388 (defvar org-capture-mode-map
(make-sparse-keymap)
389 "Keymap for `org-capture-mode', a minor mode.
390 Use this map to set additional keybindings for when Org-mode is used
391 for a capture buffer.")
393 (defvar org-capture-mode-hook nil
394 "Hook for the minor `org-capture-mode'.")
396 (define-minor-mode org-capture-mode
397 "Minor mode for special key bindings in a capture buffer."
398 nil
" Rem" org-capture-mode-map
401 "Capture buffer. Finish `C-c C-c', refile `C-c C-w', abort `C-c C-k'.")
402 (run-hooks 'org-capture-mode-hook
))
403 (define-key org-capture-mode-map
"\C-c\C-c" 'org-capture-finalize
)
404 (define-key org-capture-mode-map
"\C-c\C-k" 'org-capture-kill
)
405 (define-key org-capture-mode-map
"\C-c\C-w" 'org-capture-refile
)
407 ;;; The main commands
410 (defun org-capture (&optional goto keys
)
412 \\<org-capture-mode-map>
413 This will let you select a template from `org-capture-templates', and then
414 file the newly captured information. The text is immediately inserted
415 at the target location, and an indirect buffer is shown where you can
416 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
417 of Emacs, so that you can continue your work.
419 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
420 anything, just go to the file/headline where the selected template
421 stores its notes. With a double prefix argument \
422 \\[universal-argument] \\[universal-argument], go to the last note
425 When called with a `C-0' (zero) prefix, insert a template at point.
427 Lisp programs can set KEYS to a string associated with a template in
428 `org-capture-templates'. In this case, interactive selection will be
432 ((equal goto
'(4)) (org-capture-goto-target))
433 ((equal goto
'(16)) (org-capture-goto-last-stored))
435 ;; FIXME: Are these needed?
436 (let* ((orig-buf (current-buffer))
437 (annotation (if (and (boundp 'org-capture-link-is-already-stored
)
438 org-capture-link-is-already-stored
)
439 (plist-get org-store-link-plist
:annotation
)
440 (ignore-errors (org-store-link nil
))))
441 (initial (and (org-region-active-p)
442 (buffer-substring (point) (mark))))
443 (entry (org-capture-select-template keys
)))
444 (when (stringp initial
)
445 (remove-text-properties 0 (length initial
) '(read-only t
) initial
))
446 (when (stringp annotation
)
447 (remove-text-properties 0 (length annotation
)
448 '(read-only t
) annotation
))
451 (customize-variable 'org-capture-templates
))
455 (org-capture-set-plist entry
)
456 (org-capture-get-template)
457 (org-capture-put :original-buffer orig-buf
458 :original-file
(or (buffer-file-name orig-buf
)
459 (and (featurep 'dired
)
462 :original-file-nondirectory
463 (and (buffer-file-name orig-buf
)
464 (file-name-nondirectory
465 (buffer-file-name orig-buf
)))
466 :annotation annotation
468 (org-capture-put :default-time
469 (or org-overriding-default-time
471 (org-capture-set-target-location)
472 (condition-case error
473 (org-capture-put :template
(org-capture-fill-template))
475 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
476 (error "Capture abort: %s" error
)))
478 (setq org-capture-clock-keep
(org-capture-get :clock-keep
))
481 (org-capture-insert-template-here)
482 (condition-case error
483 (org-capture-place-template)
485 (if (and (buffer-base-buffer (current-buffer))
486 (string-match "\\`CAPTURE-" (buffer-name)))
487 (kill-buffer (current-buffer)))
488 (set-window-configuration (org-capture-get :return-to-wconf
))
489 (error "Capture template `%s': %s"
490 (org-capture-get :key
)
492 (if (and (eq major-mode
'org-mode
)
493 (org-capture-get :clock-in
))
496 (if (org-clock-is-active)
497 (org-capture-put :interrupted-clock
498 (copy-marker org-clock-marker
)))
500 (org-set-local 'org-capture-clock-was-started t
))
502 "Could not start the clock in this capture buffer")))
503 (if (org-capture-get :immediate-finish
)
504 (org-capture-finalize nil
)))))))))
506 (defun org-capture-get-template ()
507 "Get the template from a file or a function if necessary."
508 (let ((txt (org-capture-get :template
)) file
)
510 ((and (listp txt
) (eq (car txt
) 'file
))
512 (setq file
(expand-file-name (nth 1 txt
) org-directory
)))
513 (setq txt
(org-file-contents file
))
514 (setq txt
(format "* Template file %s not found" (nth 1 txt
)))))
515 ((and (listp txt
) (eq (car txt
) 'function
))
516 (if (fboundp (nth 1 txt
))
517 (setq txt
(funcall (nth 1 txt
)))
518 (setq txt
(format "* Template function %s not found" (nth 1 txt
)))))
519 ((not txt
) (setq txt
""))
521 (t (setq txt
"* Invalid capture template")))
522 (org-capture-put :template txt
)))
524 (defun org-capture-finalize (&optional stay-with-capture
)
525 "Finalize the capture process.
526 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
527 captured item after finalizing."
529 (unless (and org-capture-mode
530 (buffer-base-buffer (current-buffer)))
531 (error "This does not seem to be a capture buffer for Org-mode"))
533 ;; Did we start the clock in this capture buffer?
534 (when (and org-capture-clock-was-started
535 org-clock-marker
(marker-buffer org-clock-marker
)
536 (equal (marker-buffer org-clock-marker
) (buffer-base-buffer))
537 (> org-clock-marker
(point-min))
538 (< org-clock-marker
(point-max)))
539 ;; Looks like the clock we started is still running. Clock out.
540 (when (not org-capture-clock-keep
) (let (org-log-note-clock-out) (org-clock-out)))
541 (when (and (not org-capture-clock-keep
)
542 (org-capture-get :clock-resume
'local
)
543 (markerp (org-capture-get :interrupted-clock
'local
))
544 (buffer-live-p (marker-buffer
545 (org-capture-get :interrupted-clock
'local
))))
546 (let ((clock-in-task (org-capture-get :interrupted-clock
'local
)))
547 (org-with-point-at clock-in-task
549 (message "Interrupted clock has been resumed")))
551 (let ((beg (point-min))
554 ;; Store the size of the capture buffer
555 (org-capture-put :captured-entry-size
(- (point-max) (point-min)))
557 ;; Store the insertion point in the target buffer
558 (org-capture-put :insertion-point
(point))
561 (let ((m1 (org-capture-get :begin-marker
'local
))
562 (m2 (org-capture-get :end-marker
'local
)))
563 (if (and m1 m2
(= m1 beg
) (= m2 end
))
565 (setq m2
(if (cdr (assoc 'heading org-blank-before-new-entry
))
567 m2
(if (< (point-max) m2
) (point-max) m2
))
568 (setq abort-note
'clean
)
570 (setq abort-note
'dirty
)))
572 ;; Make sure that the empty lines after are correct
573 (when (and (> (point-max) end
) ; indeed, the buffer was still narrowed
574 (member (org-capture-get :type
'local
)
575 '(entry item checkitem plain
)))
578 (or (bolp) (newline))
579 (org-capture-empty-lines-after
580 (or (org-capture-get :empty-lines
'local
) 0))))
581 ;; Postprocessing: Update Statistics cookies, do the sorting
582 (when (eq major-mode
'org-mode
)
584 (when (ignore-errors (org-back-to-heading))
585 (org-update-parent-todo-statistics)
586 (org-update-checkbox-count)))
587 ;; FIXME Here we should do the sorting
588 ;; If we have added a table line, maybe recompute?
589 (when (and (eq (org-capture-get :type
'local
) 'table-line
)
591 (if (org-table-get-stored-formulas)
592 (org-table-recalculate 'all
) ;; FIXME: Should we iterate???
594 ;; Store this place as the last one where we stored something
595 ;; Do the marking in the base buffer, so that it makes sense after
596 ;; the indirect buffer has been killed.
597 (org-capture-bookmark-last-stored-position)
600 (run-hooks 'org-capture-before-finalize-hook
))
602 ;; Kill the indirect buffer
604 (let ((return-wconf (org-capture-get :return-to-wconf
'local
))
605 (new-buffer (org-capture-get :new-buffer
'local
))
606 (kill-buffer (org-capture-get :kill-buffer
'local
))
607 (base-buffer (buffer-base-buffer (current-buffer))))
609 ;; Kill the indirect buffer
610 (kill-buffer (current-buffer))
612 ;; Narrow back the target buffer to its previous state
613 (with-current-buffer (org-capture-get :buffer
)
614 (let ((reg (org-capture-get :initial-target-region
))
615 (pos (org-capture-get :initial-target-position
))
616 (ipt (org-capture-get :insertion-point
))
617 (size (org-capture-get :captured-entry-size
)))
619 (cond ((< ipt
(car reg
))
620 ;; insertion point is before the narrowed region
621 (narrow-to-region (+ size
(car reg
)) (+ size
(cdr reg
))))
623 ;; insertion point is after the narrowed region
624 (narrow-to-region (car reg
) (cdr reg
)))
626 ;; insertion point is within the narrowed region
627 (narrow-to-region (car reg
) (+ size
(cdr reg
)))))
628 ;; now place back the point at its original position
629 (if (< ipt
(car reg
))
630 (goto-char (+ size pos
))
631 (goto-char (if (< ipt pos
) (+ size pos
) pos
))))))
633 ;; Kill the target buffer if that is desired
634 (when (and base-buffer new-buffer kill-buffer
)
635 (with-current-buffer base-buffer
(save-buffer))
636 (kill-buffer base-buffer
))
638 ;; Restore the window configuration before capture
639 (set-window-configuration return-wconf
))
641 (run-hooks 'org-capture-after-finalize-hook
)
646 ((equal abort-note
'clean
)
647 (message "Capture process aborted and target buffer cleaned up"))
648 ((equal abort-note
'dirty
)
649 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
651 (org-capture-goto-last-stored)))
652 ;; Return if we did store something
655 (defun org-capture-refile ()
656 "Finalize the current capture and then refile the entry.
657 Refiling is done from the base buffer, because the indirect buffer is then
658 already gone. Any prefix argument will be passed to the refile command."
660 (unless (eq (org-capture-get :type
'local
) 'entry
)
662 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
664 (base (buffer-base-buffer (current-buffer)))
665 (org-refile-for-capture t
))
666 (org-capture-finalize)
667 (save-window-excursion
668 (with-current-buffer (or base
(current-buffer))
673 (call-interactively 'org-refile
)))))))
675 (defun org-capture-kill ()
676 "Abort the current capture process."
678 ;; FIXME: This does not do the right thing, we need to remove the new stuff
679 ;; By hand it is easy: undo, then kill the buffer
680 (let ((org-note-abort t
)
681 (org-capture-before-finalize-hook nil
))
682 (org-capture-finalize)))
684 (defun org-capture-goto-last-stored ()
685 "Go to the location where the last capture note was stored."
687 (org-goto-marker-or-bmk org-capture-last-stored-marker
688 "org-capture-last-stored")
689 (message "This is the last note stored by a capture process"))
691 ;;; Supporting functions for handling the process
693 (defun org-capture-put-target-region-and-position ()
694 "Store the initial region with `org-capture-put'."
696 :initial-target-region
697 ;; Check if the buffer is currently narrowed
698 (when (/= (buffer-size) (- (point-max) (point-min)))
699 (cons (point-min) (point-max))))
700 ;; store the current point
701 (org-capture-put :initial-target-position
(point)))
703 (defun org-capture-set-target-location (&optional target
)
704 "Find target buffer and position and store then in the property list."
705 (let ((target-entry-p t
))
706 (setq target
(or target
(org-capture-get :target
)))
709 ((eq (car target
) 'file
)
710 (set-buffer (org-capture-target-buffer (nth 1 target
)))
711 (org-capture-put-target-region-and-position)
713 (setq target-entry-p nil
))
715 ((eq (car target
) 'id
)
716 (let ((loc (org-id-find (nth 1 target
))))
718 (error "Cannot find target ID \"%s\"" (nth 1 target
))
719 (set-buffer (org-capture-target-buffer (car loc
)))
721 (org-capture-put-target-region-and-position)
722 (goto-char (cdr loc
)))))
724 ((eq (car target
) 'file
+headline
)
725 (set-buffer (org-capture-target-buffer (nth 1 target
)))
726 (org-capture-put-target-region-and-position)
728 (let ((hd (nth 2 target
)))
729 (goto-char (point-min))
730 (unless (eq major-mode
'org-mode
)
732 "Target buffer \"%s\" for file+headline should be in Org mode"
734 (if (re-search-forward
735 (format org-complex-heading-regexp-format
(regexp-quote hd
))
737 (goto-char (point-at-bol))
738 (goto-char (point-max))
739 (or (bolp) (insert "\n"))
740 (insert "* " hd
"\n")
741 (beginning-of-line 0))))
743 ((eq (car target
) 'file
+olp
)
744 (let ((m (org-find-olp
745 (cons (org-capture-expand-file (nth 1 target
))
747 (set-buffer (marker-buffer m
))
748 (org-capture-put-target-region-and-position)
752 ((eq (car target
) 'file
+regexp
)
753 (set-buffer (org-capture-target-buffer (nth 1 target
)))
754 (org-capture-put-target-region-and-position)
756 (goto-char (point-min))
757 (if (re-search-forward (nth 2 target
) nil t
)
759 (goto-char (if (org-capture-get :prepend
)
760 (match-beginning 0) (match-end 0)))
761 (org-capture-put :exact-position
(point))
762 (setq target-entry-p
(and (eq major-mode
'org-mode
) (org-at-heading-p))))
763 (error "No match for target regexp in file %s" (nth 1 target
))))
765 ((memq (car target
) '(file+datetree file
+datetree
+prompt
))
766 (require 'org-datetree
)
767 (set-buffer (org-capture-target-buffer (nth 1 target
)))
768 (org-capture-put-target-region-and-position)
770 ;; Make a date tree entry, with the current date (or yesterday,
771 ;; if we are extending dates for a couple of hours)
772 (org-datetree-find-date-create
773 (calendar-gregorian-from-absolute
775 (org-overriding-default-time
776 ;; use the overriding default time
777 (time-to-days org-overriding-default-time
))
779 ((eq (car target
) 'file
+datetree
+prompt
)
781 (let ((prompt-time (org-read-date
782 nil t nil
"Date for tree entry:"
784 (org-capture-put :prompt-time prompt-time
785 :default-time prompt-time
)
786 (time-to-days prompt-time
)))
788 ;; current date, possible corrected for late night workers
791 ((eq (car target
) 'file
+function
)
792 (set-buffer (org-capture-target-buffer (nth 1 target
)))
793 (org-capture-put-target-region-and-position)
795 (funcall (nth 2 target
))
796 (org-capture-put :exact-position
(point))
797 (setq target-entry-p
(and (eq major-mode
'org-mode
) (org-at-heading-p))))
799 ((eq (car target
) 'function
)
800 (funcall (nth 1 target
))
801 (org-capture-put :exact-position
(point))
802 (setq target-entry-p
(and (eq major-mode
'org-mode
) (org-at-heading-p))))
804 ((eq (car target
) 'clock
)
805 (if (and (markerp org-clock-hd-marker
)
806 (marker-buffer org-clock-hd-marker
))
807 (progn (set-buffer (marker-buffer org-clock-hd-marker
))
808 (org-capture-put-target-region-and-position)
810 (goto-char org-clock-hd-marker
))
811 (error "No running clock that could be used as capture target")))
813 (t (error "Invalid capture target specification")))
815 (org-capture-put :buffer
(current-buffer) :pos
(point)
816 :target-entry-p target-entry-p
))))
818 (defun org-capture-expand-file (file)
819 "Expand functions and symbols for FILE.
820 When FILE is a function, call it. When it is a form, evaluate
821 it. When it is a variable, retrieve the value. Return whatever we get."
823 ((org-string-nw-p file
) file
)
824 ((functionp file
) (funcall file
))
825 ((and (symbolp file
) (boundp file
)) (symbol-value file
))
826 ((and file
(consp file
)) (eval file
))
829 (defun org-capture-target-buffer (file)
830 "Get a buffer for FILE."
831 (setq file
(org-capture-expand-file file
))
832 (setq file
(or (org-string-nw-p file
)
833 org-default-notes-file
834 (error "No notes file specified, and no default available")))
835 (or (org-find-base-buffer-visiting file
)
836 (progn (org-capture-put :new-buffer t
)
837 (find-file-noselect (expand-file-name file org-directory
)))))
839 (defun org-capture-steal-local-variables (buffer)
840 "Install Org-mode local variables."
842 (ignore-errors (org-set-local (car v
) (cdr v
))))
843 (buffer-local-variables buffer
)))
845 (defun org-capture-place-template ()
846 "Insert the template at the target location, and display the buffer."
847 (org-capture-put :return-to-wconf
(current-window-configuration))
848 (delete-other-windows)
849 (org-switch-to-buffer-other-window
850 (org-capture-get-indirect-buffer (org-capture-get :buffer
) "CAPTURE"))
853 (goto-char (org-capture-get :pos
))
854 (org-set-local 'org-capture-target-marker
855 (move-marker (make-marker) (point)))
856 (org-set-local 'outline-level
'org-outline-level
)
857 (let* ((template (org-capture-get :template
))
858 (type (org-capture-get :type
)))
860 ((nil entry
) (org-capture-place-entry))
861 (table-line (org-capture-place-table-line))
862 (plain (org-capture-place-plain-text))
863 (item (org-capture-place-item))
864 (checkitem (org-capture-place-item))))
866 (org-set-local 'org-capture-current-plist org-capture-plist
))
868 (defun org-capture-place-entry ()
869 "Place the template as a new Org entry."
870 (let* ((txt (org-capture-get :template
))
871 (reversed (org-capture-get :prepend
))
872 (target-entry-p (org-capture-get :target-entry-p
))
876 ((org-capture-get :exact-position
)
877 (goto-char (org-capture-get :exact-position
)))
878 ((not target-entry-p
)
879 ;; Insert as top-level entry, either at beginning or at end of file
882 (progn (goto-char (point-min))
883 (or (org-at-heading-p)
884 (outline-next-heading)))
885 (goto-char (point-max))
886 (or (bolp) (insert "\n"))))
888 ;; Insert as a child of the current entry
889 (and (looking-at "\\*+")
890 (setq level
(- (match-end 0) (match-beginning 0))))
891 (setq level
(org-get-valid-level (or level
1) 1))
894 (outline-next-heading)
895 (or (bolp) (insert "\n")))
896 (org-end-of-subtree t t
)
897 (or (bolp) (insert "\n")))))
898 (org-capture-empty-lines-before)
900 (org-capture-verify-tree txt
)
901 (org-paste-subtree level txt
'for-yank
)
902 (org-capture-empty-lines-after 1)
903 (org-capture-position-for-last-stored beg
)
904 (outline-next-heading)
906 (org-capture-mark-kill-region beg
(1- end
))
907 (org-capture-narrow beg
(1- end
))
909 (if (re-search-forward "%\\?" end t
) (replace-match ""))))
911 (defun org-capture-place-item ()
912 "Place the template as a new plain list item."
913 (let* ((txt (org-capture-get :template
))
914 (target-entry-p (org-capture-get :target-entry-p
))
917 (if (org-capture-get :exact-position
)
918 (goto-char (org-capture-get :exact-position
))
920 ((not target-entry-p
)
921 ;; Insert as top-level entry, either at beginning or at end of file
922 (setq beg
(point-min) end
(point-max)))
924 (setq beg
(1+ (point-at-eol))
925 end
(save-excursion (outline-next-heading) (point)))))
926 (if (org-capture-get :prepend
)
929 (if (org-list-search-forward (org-item-beginning-re) end t
)
931 (goto-char (match-beginning 0))
932 (setq ind
(org-get-indentation)))
936 (if (org-list-search-backward (org-item-beginning-re) beg t
)
938 (setq ind
(org-get-indentation))
941 ;; Remove common indentation
942 (setq txt
(org-remove-indentation txt
))
943 ;; Make sure this is indeed an item
944 (unless (string-match (concat "\\`" (org-item-re)) txt
)
945 (setq txt
(concat "- "
946 (mapconcat 'identity
(split-string txt
"\n")
948 ;; Set the correct indentation, depending on context
949 (setq ind
(make-string ind ?\
))
950 (setq txt
(concat ind
951 (mapconcat 'identity
(split-string txt
"\n")
954 ;; Insert, with surrounding empty lines
955 (org-capture-empty-lines-before)
958 (or (bolp) (insert "\n"))
959 (org-capture-empty-lines-after 1)
960 (org-capture-position-for-last-stored beg
)
963 (org-capture-mark-kill-region beg
(1- end
))
964 (org-capture-narrow beg
(1- end
))
965 (if (re-search-forward "%\\?" end t
) (replace-match ""))))
967 (defun org-capture-place-table-line ()
968 "Place the template as a table line."
970 (let* ((txt (org-capture-get :template
))
971 (target-entry-p (org-capture-get :target-entry-p
))
972 (table-line-pos (org-capture-get :table-line-pos
))
975 ((org-capture-get :exact-position
)
976 (goto-char (org-capture-get :exact-position
)))
977 ((not target-entry-p
)
978 ;; Table is not necessarily under a heading
979 (setq beg
(point-min) end
(point-max)))
981 ;; WE are at a heading, limit search to the body
982 (setq beg
(1+ (point-at-eol))
983 end
(save-excursion (outline-next-heading) (point)))))
984 (if (re-search-forward org-table-dataline-regexp end t
)
985 (let ((b (org-table-begin)) (e (org-table-end)))
987 (if (looking-at "[ \t]*#\\+TBLFM:")
989 (narrow-to-region b
(point)))
991 (insert "\n| |\n|----|\n| |\n")
992 (narrow-to-region (1+ end
) (point)))
993 ;; We are narrowed to the table, or to an empty line if there was no table
995 ;; Check if the template is good
996 (if (not (string-match org-table-dataline-regexp txt
))
997 (setq txt
"| %?Bad template |\n"))
1000 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos
))
1001 ;; we have a complex line specification
1002 (goto-char (point-min))
1003 (let ((nh (- (match-end 1) (match-beginning 1)))
1004 (delta (string-to-number (match-string 2 table-line-pos
)))
1006 ;; The user wants a special position in the table
1007 (org-table-get-specials)
1008 (setq ll
(ignore-errors (aref org-table-hlines nh
)))
1009 (unless ll
(error "Invalid table line specification \"%s\""
1011 (setq ll
(+ ll delta
(if (< delta
0) 0 -
1)))
1013 (org-table-insert-row 'below
)
1014 (beginning-of-line 1)
1015 (delete-region (point) (1+ (point-at-eol)))
1018 (setq end
(point))))
1019 ((org-capture-get :prepend
)
1020 (goto-char (point-min))
1021 (re-search-forward org-table-hline-regexp nil t
)
1022 (beginning-of-line 1)
1023 (re-search-forward org-table-dataline-regexp nil t
)
1024 (beginning-of-line 1)
1026 (org-table-insert-row)
1027 (beginning-of-line 1)
1028 (delete-region (point) (1+ (point-at-eol)))
1032 (goto-char (point-max))
1033 (re-search-backward org-table-dataline-regexp nil t
)
1034 (beginning-of-line 1)
1035 (org-table-insert-row 'below
)
1036 (beginning-of-line 1)
1037 (delete-region (point) (1+ (point-at-eol)))
1040 (setq end
(point))))
1042 (org-capture-position-for-last-stored 'table-line
)
1043 (if (re-search-forward "%\\?" end t
) (replace-match ""))
1046 (defun org-capture-place-plain-text ()
1047 "Place the template plainly.
1048 If the target locator points at an Org node, place the template into
1049 the text of the entry, before the first child. If not, place the
1050 template at the beginning or end of the file.
1051 Of course, if exact position has been required, just put it there."
1052 (let* ((txt (org-capture-get :template
))
1055 ((org-capture-get :exact-position
)
1056 (goto-char (org-capture-get :exact-position
)))
1057 ((and (org-capture-get :target-entry-p
)
1059 (looking-at org-outline-regexp
))
1060 ;; we should place the text into this entry
1061 (if (org-capture-get :prepend
)
1062 ;; Skip meta data and drawers
1063 (org-end-of-meta-data-and-drawers)
1064 ;; go to ent of the entry text, before the next headline
1065 (outline-next-heading)))
1067 ;; beginning or end of file
1068 (goto-char (if (org-capture-get :prepend
) (point-min) (point-max)))))
1069 (or (bolp) (newline))
1070 (org-capture-empty-lines-before)
1073 (org-capture-empty-lines-after 1)
1074 (org-capture-position-for-last-stored beg
)
1076 (org-capture-mark-kill-region beg
(1- end
))
1077 (org-capture-narrow beg
(1- end
))
1078 (if (re-search-forward "%\\?" end t
) (replace-match ""))))
1080 (defun org-capture-mark-kill-region (beg end
)
1081 "Mark the region that will have to be killed when aborting capture."
1082 (let ((m1 (move-marker (make-marker) beg
))
1083 (m2 (move-marker (make-marker) end
)))
1084 (org-capture-put :begin-marker m1
)
1085 (org-capture-put :end-marker m2
)))
1087 (defun org-capture-position-for-last-stored (where)
1088 "Memorize the position that should later become the position of last capture."
1091 (org-capture-put :position-for-last-stored
1092 (move-marker (make-marker) where
1093 (or (buffer-base-buffer (current-buffer))
1094 (current-buffer)))))
1095 ((eq where
'table-line
)
1096 (org-capture-put :position-for-last-stored
1098 (org-table-current-dline))))
1099 (t (error "This should not happen"))))
1101 (defun org-capture-bookmark-last-stored-position ()
1102 "Bookmark the last-captured position."
1103 (let* ((where (org-capture-get :position-for-last-stored
'local
))
1106 (prog1 (marker-position where
)
1107 (move-marker where nil
)))
1108 ((and (listp where
) (eq (car where
) 'table-line
))
1109 (if (org-at-table-p)
1111 (org-table-goto-line (nth 1 where
))
1114 (with-current-buffer (buffer-base-buffer (current-buffer))
1119 (bookmark-set "org-capture-last-stored")
1120 (move-marker org-capture-last-stored-marker
(point)))))))
1122 (defun org-capture-narrow (beg end
)
1123 "Narrow, unless configuration says not to narrow."
1124 (unless (org-capture-get :unnarrowed
)
1125 (narrow-to-region beg end
)
1128 (defun org-capture-empty-lines-before (&optional n
)
1129 "Arrange for the correct number of empty lines before the insertion point.
1130 Point will be after the empty lines, so insertion can directly be done."
1131 (setq n
(or n
(org-capture-get :empty-lines
) 0))
1132 (let ((pos (point)))
1133 (org-back-over-empty-lines)
1134 (delete-region (point) pos
)
1135 (if (> n
0) (newline n
))))
1137 (defun org-capture-empty-lines-after (&optional n
)
1138 "Arrange for the correct number of empty lines after the inserted string.
1139 Point will remain at the first line after the inserted text."
1140 (setq n
(or n
(org-capture-get :empty-lines
) 0))
1141 (org-back-over-empty-lines)
1142 (while (looking-at "[ \t]*\n") (replace-match ""))
1143 (let ((pos (point)))
1144 (if (> n
0) (newline n
))
1147 (defvar org-clock-marker
) ; Defined in org.el
1149 (defun org-capture-insert-template-here ()
1150 (let* ((template (org-capture-get :template
))
1151 (type (org-capture-get :type
))
1153 (or (bolp) (newline))
1156 ((and (eq type
'entry
) (eq major-mode
'org-mode
))
1157 (org-capture-verify-tree (org-capture-get :template
))
1158 (org-paste-subtree nil template t
))
1159 ((and (memq type
'(item checkitem
))
1160 (eq major-mode
'org-mode
)
1161 (save-excursion (skip-chars-backward " \t\n")
1166 (skip-chars-backward " ")
1167 (skip-chars-backward "-+*0123456789).")
1168 (delete-region (point) (point-at-eol))
1170 (org-remove-indentation template
)
1172 (org-capture-empty-lines-after)
1177 (t (insert template
)))
1180 (if (re-search-forward "%\\?" end t
)
1181 (replace-match ""))))
1183 (defun org-capture-set-plist (entry)
1184 "Initialize the property list from the template definition."
1185 (setq org-capture-plist
(copy-sequence (nthcdr 5 entry
)))
1186 (org-capture-put :key
(car entry
) :description
(nth 1 entry
)
1187 :target
(nth 3 entry
))
1188 (let ((txt (nth 4 entry
)) (type (or (nth 2 entry
) 'entry
)))
1189 (when (or (not txt
) (and (stringp txt
) (not (string-match "\\S-" txt
))))
1190 ;; The template may be empty or omitted for special types.
1191 ;; Here we insert the default templates for such cases.
1193 ((eq type
'item
) (setq txt
"- %?"))
1194 ((eq type
'checkitem
) (setq txt
"- [ ] %?"))
1195 ((eq type
'table-line
) (setq txt
"| %? |"))
1196 ((member type
'(nil entry
)) (setq txt
"* %?\n %a"))))
1197 (org-capture-put :template txt
:type type
)))
1199 (defun org-capture-goto-target (&optional template-key
)
1200 "Go to the target location of a capture template.
1201 The user is queried for the template."
1203 (let* (org-select-template-temp-major-mode
1204 (entry (org-capture-select-template template-key
)))
1206 (error "No capture template selected"))
1207 (org-capture-set-plist entry
)
1208 (org-capture-set-target-location)
1209 (org-pop-to-buffer-same-window (org-capture-get :buffer
))
1210 (goto-char (org-capture-get :pos
))))
1212 (defun org-capture-get-indirect-buffer (&optional buffer prefix
)
1213 "Make an indirect buffer for a capture process.
1214 Use PREFIX as a prefix for the name of the indirect buffer."
1215 (setq buffer
(or buffer
(current-buffer)))
1216 (let ((n 1) (base (buffer-name buffer
)) bname
)
1217 (setq bname
(concat prefix
"-" base
))
1218 (while (buffer-live-p (get-buffer bname
))
1219 (setq bname
(concat prefix
"-" (number-to-string (incf n
)) "-" base
)))
1221 (make-indirect-buffer buffer bname
'clone
)
1223 (let ((buf (make-indirect-buffer buffer bname
)))
1224 (with-current-buffer buf
(org-mode))
1227 (defun org-capture-verify-tree (tree)
1228 "Throw error if TREE is not a valid tree"
1229 (unless (org-kill-is-subtree-p tree
)
1230 (error "Template is not a valid Org entry or tree")))
1232 ;;; The template code
1234 (defun org-capture-select-template (&optional keys
)
1235 "Select a capture template.
1236 Lisp programs can force the template by setting KEYS to a string."
1237 (let ((org-capture-templates
1238 (or org-capture-templates
1239 '(("t" "Task" entry
(file+headline
"" "Tasks")
1240 "* TODO %?\n %u\n %a")))))
1242 (or (assoc keys org-capture-templates
)
1243 (error "No capture template referred to by \"%s\" keys" keys
))
1244 (org-mks org-capture-templates
1245 "Select a capture template\n========================="
1247 '(("C" "Customize org-capture-templates")
1250 (defun org-capture-fill-template (&optional template initial annotation
)
1251 "Fill a template and return the filled template as a string.
1252 The template may still contain \"%?\" for cursor positioning."
1253 (setq template
(or template
(org-capture-get :template
)))
1254 (when (stringp initial
)
1255 (setq initial
(org-no-properties initial
))
1256 (remove-text-properties 0 (length initial
) '(read-only t
) initial
))
1257 (let* ((buffer (org-capture-get :buffer
))
1258 (file (buffer-file-name (or (buffer-base-buffer buffer
) buffer
)))
1259 (ct (org-capture-get :default-time
))
1260 (dct (decode-time ct
))
1262 (if (< (nth 2 dct
) org-extend-today-until
)
1263 (encode-time 0 59 23 (1- (nth 3 dct
)) (nth 4 dct
) (nth 5 dct
))
1265 (plist-p (if org-store-link-plist t nil
))
1266 (v-c (and (> (length kill-ring
) 0) (current-kill 0)))
1267 (v-x (or (org-get-x-clipboard 'PRIMARY
)
1268 (org-get-x-clipboard 'CLIPBOARD
)
1269 (org-get-x-clipboard 'SECONDARY
)))
1270 (v-t (format-time-string (car org-time-stamp-formats
) ct
))
1271 (v-T (format-time-string (cdr org-time-stamp-formats
) ct
))
1272 (v-u (concat "[" (substring v-t
1 -
1) "]"))
1273 (v-U (concat "[" (substring v-T
1 -
1) "]"))
1274 ;; `initial' and `annotation' might habe been passed.
1275 ;; But if the property list has them, we prefer those values
1276 (v-i (or (plist-get org-store-link-plist
:initial
)
1278 (org-capture-get :initial
)
1280 (v-a (or (plist-get org-store-link-plist
:annotation
)
1282 (org-capture-get :annotation
)
1284 ;; Is the link empty? Then we do not want it...
1285 (v-a (if (equal v-a
"[[]]") "" v-a
))
1286 (clipboards (remove nil
(list v-i
1287 (org-get-x-clipboard 'PRIMARY
)
1288 (org-get-x-clipboard 'CLIPBOARD
)
1289 (org-get-x-clipboard 'SECONDARY
)
1293 "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a
))
1294 (replace-match "[\\1[%^{Link description}]]" nil nil v-a
)
1296 (v-n user-full-name
)
1297 (v-k (if (marker-buffer org-clock-marker
)
1298 (org-substring-no-properties org-clock-heading
)))
1299 (v-K (if (marker-buffer org-clock-marker
)
1300 (org-make-link-string
1301 (buffer-file-name (marker-buffer org-clock-marker
))
1302 org-clock-heading
)))
1303 (v-f (or (org-capture-get :original-file-nondirectory
) ""))
1304 (v-F (or (org-capture-get :original-file
) ""))
1306 (org-startup-folded nil
)
1307 (org-inhibit-startup t
)
1308 org-time-was-given org-end-time-was-given x
1309 prompt completions char time pos default histvar
)
1311 (setq org-store-link-plist
1312 (plist-put org-store-link-plist
:annotation v-a
)
1313 org-store-link-plist
1314 (plist-put org-store-link-plist
:initial v-i
))
1317 (unless template
(setq template
"") (message "No template") (ding)
1319 (save-window-excursion
1320 (delete-other-windows)
1321 (org-pop-to-buffer-same-window (get-buffer-create "*Capture*"))
1324 (goto-char (point-min))
1325 (org-capture-steal-local-variables buffer
)
1326 (setq buffer-file-name nil
)
1328 ;; %[] Insert contents of a file.
1329 (goto-char (point-min))
1330 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t
)
1331 (unless (org-capture-escaped-%
)
1332 (let ((start (match-beginning 0))
1334 (filename (expand-file-name (match-string 1))))
1336 (delete-region start end
)
1337 (condition-case error
1338 (insert-file-contents filename
)
1339 (error (insert (format "%%![Couldn't insert %s: %s]"
1340 filename error
)))))))
1341 ;; %() embedded elisp
1342 (goto-char (point-min))
1343 (while (re-search-forward "%\\((.+)\\)" nil t
)
1344 (unless (org-capture-escaped-%
)
1345 (goto-char (match-beginning 0))
1346 (let ((template-start (point)))
1348 (let ((result (org-eval (read (current-buffer)))))
1349 (delete-region template-start
(point))
1353 (goto-char (point-min))
1354 (while (re-search-forward "%<\\([^>\n]+\\)>" nil t
)
1355 (replace-match (format-time-string (match-string 1)) t t
))
1358 (goto-char (point-min))
1359 (while (re-search-forward "%\\([tTuUaiAcxkKInfF]\\)" nil t
)
1360 (unless (org-capture-escaped-%
)
1361 (when (and initial
(equal (match-string 0) "%i"))
1363 (let* ((lead (buffer-substring
1364 (point-at-bol) (match-beginning 0))))
1365 (setq v-i
(mapconcat 'identity
1366 (org-split-string initial
"\n")
1367 (concat "\n" lead
))))))
1369 (or (eval (intern (concat "v-" (match-string 1)))) "")
1372 ;; From the property list
1374 (goto-char (point-min))
1375 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t
)
1376 (unless (org-capture-escaped-%
)
1377 (and (setq x
(or (plist-get org-store-link-plist
1378 (intern (match-string 1))) ""))
1379 (replace-match x t t
)))))
1381 ;; Turn on org-mode in temp buffer, set local variables
1382 ;; This is to support completion in interactive prompts
1383 (let ((org-inhibit-startup t
)) (org-mode))
1384 ;; Interactive template entries
1385 (goto-char (point-min))
1386 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
1388 (unless (org-capture-escaped-%
)
1389 (setq char
(if (match-end 3) (match-string-no-properties 3))
1390 prompt
(if (match-end 2) (match-string-no-properties 2)))
1391 (goto-char (match-beginning 0))
1393 (setq completions nil default nil
)
1395 (setq completions
(org-split-string prompt
"|")
1396 prompt
(pop completions
)
1397 default
(car completions
)
1398 histvar
(intern (concat
1399 "org-capture-template-prompt-history::"
1401 completions
(mapcar 'list completions
)))
1402 (unless (boundp histvar
) (set histvar nil
))
1404 ((member char
'("G" "g"))
1405 (let* ((org-last-tags-completion-table
1406 (org-global-tags-completion-table
1407 (if (equal char
"G")
1409 (and file
(list file
)))))
1410 (org-add-colon-after-tag-completion t
)
1411 (ins (org-icompleting-read
1412 (if prompt
(concat prompt
": ") "Tags: ")
1413 'org-tags-completion-function nil nil nil
1414 'org-tags-history
)))
1415 (setq ins
(mapconcat 'identity
1417 ins
(org-re "[^[:alnum:]_@#%]+"))
1419 (when (string-match "\\S-" ins
)
1420 (or (equal (char-before) ?
:) (insert ":"))
1422 (or (equal (char-after) ?
:) (insert ":"))
1423 (and (org-at-heading-p) (org-set-tags nil
'align
)))))
1425 (cond ((= (length clipboards
) 1) (insert (car clipboards
)))
1426 ((> (length clipboards
) 1)
1427 (insert (read-string "Clipboard/kill value: "
1428 (car clipboards
) '(clipboards .
1)
1429 (car clipboards
))))))
1431 (cond ((= (length clipboards
) 1)
1432 (org-insert-link 0 (car clipboards
)))
1433 ((> (length clipboards
) 1)
1434 (org-insert-link 0 (read-string "Clipboard/kill value: "
1437 (car clipboards
))))))
1439 (org-set-property (org-substring-no-properties prompt
) nil
))
1441 ;; These are the date/time related ones
1442 (setq org-time-was-given
(equal (upcase char
) char
))
1443 (setq time
(org-read-date (equal (upcase char
) char
) t nil
1445 (if (equal (upcase char
) char
) (setq org-time-was-given t
))
1446 (org-insert-time-stamp time org-time-was-given
1447 (member char
'("u" "U"))
1448 nil nil
(list org-end-time-was-given
)))
1450 (let (org-completion-use-ido)
1451 (insert (org-completing-read-no-i
1452 (concat (if prompt prompt
"Enter string")
1453 (if default
(concat " [" default
"]"))
1455 completions nil nil nil histvar default
)))))))
1456 ;; Make sure there are no empty lines before the text, and that
1457 ;; it ends with a newline character
1458 (goto-char (point-min))
1459 (while (looking-at "[ \t]*\n") (replace-match ""))
1460 (if (re-search-forward "[ \t\n]*\\'" nil t
) (replace-match "\n"))
1461 ;; Return the expanded template and kill the temporary buffer
1462 (untabify (point-min) (point-max))
1463 (set-buffer-modified-p nil
)
1464 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1466 (defun org-capture-escaped-%
()
1467 "Check if % was escaped - if yes, unescape it now."
1468 (if (equal (char-before (match-beginning 0)) ?
\\)
1470 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1475 (defun org-capture-import-remember-templates ()
1476 "Set org-capture-templates to be similar to `org-remember-templates'."
1478 (when (and (yes-or-no-p
1479 "Import old remember templates into org-capture-templates? ")
1481 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1482 (require 'org-remember
)
1483 (setq org-capture-templates
1486 (let ((desc (car entry
))
1487 (key (char-to-string (nth 1 entry
)))
1488 (template (nth 2 entry
))
1489 (file (or (nth 3 entry
) org-default-notes-file
))
1490 (position (or (nth 4 entry
) org-remember-default-headline
))
1492 (prepend org-reverse-note-order
)
1495 ((member position
'(top bottom
))
1496 (setq target
(list 'file file
)
1497 prepend
(eq position
'top
)))
1498 ((eq position
'date-tree
)
1499 (setq target
(list 'file
+datetree file
)
1501 (t (setq target
(list 'file
+headline file position
))))
1503 (when (string-match "%!" template
)
1504 (setq template
(replace-match "" t t template
)
1507 (append (list key desc type target template
)
1508 (if prepend
'(:prepend t
))
1509 (if immediate
'(:immediate-finish t
)))))
1511 org-remember-templates
))))
1513 (provide 'org-capture
)
1515 ;;; org-capture.el ends here