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 alternative 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 (declare-function org-pop-to-buffer-same-window "org-compat"
61 (&optional buffer-or-name norecord label))
63 (defvar org-remember-default-headline)
64 (defvar org-remember-templates)
65 (defvar org-table-hlines)
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 org-mode
209 entry (so the first line should start with a star) that will be filed as a
210 child of the target headline. It can also be freely formatted text.
211 Furthermore, the following %-escapes will be replaced with content:
213 %^{prompt} prompt the user for a string and replace this sequence with it.
214 A default value and a completion table ca be specified like this:
215 %^{prompt|default|completion2|completion3|...}.
216 %t time stamp, date only.
217 %T time stamp with date and time.
218 %u, %U like the above, but inactive time stamps.
219 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
220 You may define a prompt like %^{Please specify birthday.
221 %<...> the result of format-time-string on the ... format specification.
222 %n user name (taken from `user-full-name').
223 %a annotation, normally the link created with `org-store-link'.
224 %i initial content, copied from the active region. If %i is
225 indented, the entire inserted text will be indented as well.
226 %c current kill ring head.
227 %x content of the X clipboard.
228 %^C interactive selection of which kill or clip to use.
229 %^L like %^C, but insert as link.
230 %k title of currently clocked task.
231 %K link to currently clocked task.
232 %f file visited by current buffer when org-capture was called.
233 %F full path of the file or directory visited by current buffer.
234 %^g prompt for tags, with completion on tags in target file.
235 %^G prompt for tags, with completion on all tags in all agenda files.
236 %^{prop}p prompt the user for a value for property `prop'.
237 %:keyword specific information for certain link types, see below.
238 %[pathname] insert the contents of the file given by `pathname'.
239 %(sexp) evaluate elisp `(sexp)' and replace with the result.
241 %? After completing the template, position cursor here.
243 Apart from these general escapes, you can access information specific to the
244 link type that is created. For example, calling `org-capture' in emails
245 or gnus will record the author and the subject of the message, which you
246 can access with \"%:from\" and \"%:subject\", respectively. Here is a
247 complete list of what is recorded for each link type.
249 Link type | Available information
250 ------------------------+------------------------------------------------------
251 bbdb | %:type %:name %:company
252 vm, wl, mh, mew, rmail | %:type %:subject %:message-id
253 | %:from %:fromname %:fromaddress
254 | %:to %:toname %:toaddress
255 | %:fromto (either \"to NAME\" or \"from NAME\")
257 | %:date-timestamp (as active timestamp)
258 | %:date-timestamp-inactive (as inactive timestamp)
259 gnus | %:group, for messages also all email fields
260 w3, w3m | %:type %:url
261 info | %:type %:file %:node
262 calendar | %:type %:date"
266 (choice :value ("" "" entry (file "~/org/notes.org") "")
267 (list :tag "Multikey description"
268 (string :tag "Keys ")
269 (string :tag "Description"))
270 (list :tag "Template entry"
271 (string :tag "Keys ")
272 (string :tag "Description ")
273 (choice :tag "Capture Type " :value entry
274 (const :tag "Org entry" entry)
275 (const :tag "Plain list item" item)
276 (const :tag "Checkbox item" checkitem)
277 (const :tag "Plain text" plain)
278 (const :tag "Table line" table-line))
279 (choice :tag "Target location"
281 (const :format "" file)
284 (const :format "" id)
286 (list :tag "File & Headline"
287 (const :format "" file+headline)
289 (string :tag " Headline"))
290 (list :tag "File & Outline path"
291 (const :format "" file+olp)
293 (repeat :tag "Outline path" :inline t
294 (string :tag "Headline")))
295 (list :tag "File & Regexp"
296 (const :format "" file+regexp)
298 (regexp :tag " Regexp"))
299 (list :tag "File & Date tree"
300 (const :format "" file+datetree)
302 (list :tag "File & Date tree, prompt for date"
303 (const :format "" file+datetree+prompt)
305 (list :tag "File & function"
306 (const :format "" file+function)
308 (sexp :tag " Function"))
309 (list :tag "Current clocking task"
310 (const :format "" clock))
311 (list :tag "Function"
312 (const :format "" function)
313 (sexp :tag " Function")))
314 (choice :tag "Template"
317 (const :format "" file)
318 (file :tag "Template file"))
319 (list :tag "Function"
320 (const :format "" function)
321 (function :tag "Template function")))
323 ;; Give the most common options as checkboxes
324 :options (((const :format "%v " :prepend) (const t))
325 ((const :format "%v " :immediate-finish) (const t))
326 ((const :format "%v " :empty-lines) (const 1))
327 ((const :format "%v " :clock-in) (const t))
328 ((const :format "%v " :clock-keep) (const t))
329 ((const :format "%v " :clock-resume) (const t))
330 ((const :format "%v " :unnarrowed) (const t))
331 ((const :format "%v " :kill-buffer) (const t))))))))
333 (defcustom org-capture-before-finalize-hook nil
334 "Hook that is run right before a capture process is finalized.
335 The capture buffer is still current when this hook runs."
339 (defcustom org-capture-after-finalize-hook nil
340 "Hook that is run right after a capture process is finalized.
341 Suitable for window cleanup"
345 ;;; The property list for keeping information about the capture process
347 (defvar org-capture-plist nil
348 "Plist for the current capture process, global, to avoid having to pass it.")
350 (defvar org-capture-current-plist nil
351 "Local variable holding the plist in a capture buffer.
352 This is used to store the plist for use when finishing a capture process
353 because another such process might have changed the global variable by then.
355 Each time a new capture buffer has been set up, the global `org-capture-plist'
356 is copied to this variable, which is local in the indirect buffer.")
358 (defvar org-capture-clock-keep nil
359 "Local variable to store the value of the :clock-keep parameter.
360 This is needed in case org-capture-finalize is called interactively.")
362 (defun org-capture-put (&rest stuff)
363 "Add properties to the capture property list `org-capture-plist'."
365 (setq org-capture-plist (plist-put org-capture-plist
366 (pop stuff) (pop stuff)))))
367 (defun org-capture-get (prop &optional local)
368 "Get properties from the capture property list `org-capture-plist'.
369 When LOCAL is set, use the local variable `org-capture-current-plist',
370 this is necessary after initialization of the capture process,
371 to avoid conflicts with other active capture processes."
372 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
374 (defun org-capture-member (prop &optional local)
375 "Is PROP a preperty in `org-capture-plist'.
376 When LOCAL is set, use the local variable `org-capture-current-plist',
377 this is necessary after initialization of the capture process,
378 to avoid conflicts with other active capture processes."
379 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
383 (defvar org-capture-mode-map (make-sparse-keymap)
384 "Keymap for `org-capture-mode', a minor mode.
385 Use this map to set additional keybindings for when Org-mode is used
386 for a capture buffer.")
388 (defvar org-capture-mode-hook nil
389 "Hook for the minor `org-capture-mode'.")
391 (define-minor-mode org-capture-mode
392 "Minor mode for special key bindings in a capture buffer."
393 nil " Rem" org-capture-mode-map
396 "Capture buffer. Finish `C-c C-c', refile `C-c C-w', abort `C-c C-k'.")
397 (run-hooks 'org-capture-mode-hook))
398 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
399 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
400 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
402 ;;; The main commands
405 (defun org-capture (&optional goto keys)
407 \\<org-capture-mode-map>
408 This will let you select a template from `org-capture-templates', and then
409 file the newly captured information. The text is immediately inserted
410 at the target location, and an indirect buffer is shown where you can
411 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
412 of Emacs, so that you can continue your work.
414 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
415 anything, just go to the file/headline where the selected template
416 stores its notes. With a double prefix argument \
417 \\[universal-argument] \\[universal-argument], go to the last note
420 When called with a `C-0' (zero) prefix, insert a template at point.
422 Lisp programs can set KEYS to a string associated with a template in
423 `org-capture-templates'. In this case, interactive selection will be
427 ((equal goto '(4)) (org-capture-goto-target))
428 ((equal goto '(16)) (org-capture-goto-last-stored))
430 ;; FIXME: Are these needed?
431 (let* ((orig-buf (current-buffer))
432 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
433 org-capture-link-is-already-stored)
434 (plist-get org-store-link-plist :annotation)
435 (ignore-errors (org-store-link nil))))
436 (initial (and (org-region-active-p)
437 (buffer-substring (point) (mark))))
438 (entry (org-capture-select-template keys)))
439 (when (stringp initial)
440 (remove-text-properties 0 (length initial) '(read-only t) initial))
441 (when (stringp annotation)
442 (remove-text-properties 0 (length annotation)
443 '(read-only t) annotation))
446 (customize-variable 'org-capture-templates))
450 (org-capture-set-plist entry)
451 (org-capture-get-template)
452 (org-capture-put :original-buffer orig-buf
453 :original-file (or (buffer-file-name orig-buf)
454 (car (rassq orig-buf dired-buffers)))
455 :original-file-nondirectory
456 (and (buffer-file-name orig-buf)
457 (file-name-nondirectory
458 (buffer-file-name orig-buf)))
459 :annotation annotation
461 (org-capture-put :default-time
462 (or org-overriding-default-time
464 (org-capture-set-target-location)
465 (condition-case error
466 (org-capture-put :template (org-capture-fill-template))
468 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
469 (error "Capture abort: %s" error)))
471 (setq org-capture-clock-keep (org-capture-get :clock-keep))
474 (org-capture-insert-template-here)
475 (condition-case error
476 (org-capture-place-template)
478 (if (and (buffer-base-buffer (current-buffer))
479 (string-match "\\`CAPTURE-" (buffer-name)))
480 (kill-buffer (current-buffer)))
481 (set-window-configuration (org-capture-get :return-to-wconf))
482 (error "Capture template `%s': %s"
483 (org-capture-get :key)
485 (if (and (org-mode-p)
486 (org-capture-get :clock-in))
489 (if (org-clock-is-active)
490 (org-capture-put :interrupted-clock
491 (copy-marker org-clock-marker)))
493 (org-set-local 'org-capture-clock-was-started t))
495 "Could not start the clock in this capture buffer")))
496 (if (org-capture-get :immediate-finish)
497 (org-capture-finalize nil)))))))))
499 (defun org-capture-get-template ()
500 "Get the template from a file or a function if necessary."
501 (let ((txt (org-capture-get :template)) file)
503 ((and (listp txt) (eq (car txt) 'file))
505 (setq file (expand-file-name (nth 1 txt) org-directory)))
506 (setq txt (org-file-contents file))
507 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
508 ((and (listp txt) (eq (car txt) 'function))
509 (if (fboundp (nth 1 txt))
510 (setq txt (funcall (nth 1 txt)))
511 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
512 ((not txt) (setq txt ""))
514 (t (setq txt "* Invalid capture template")))
515 (org-capture-put :template txt)))
517 (defun org-capture-finalize (&optional stay-with-capture)
518 "Finalize the capture process.
519 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
520 captured item after finalizing."
522 (unless (and org-capture-mode
523 (buffer-base-buffer (current-buffer)))
524 (error "This does not seem to be a capture buffer for Org-mode"))
526 ;; Did we start the clock in this capture buffer?
527 (when (and org-capture-clock-was-started
528 org-clock-marker (marker-buffer org-clock-marker)
529 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
530 (> org-clock-marker (point-min))
531 (< org-clock-marker (point-max)))
532 ;; Looks like the clock we started is still running. Clock out.
533 (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
534 (when (and (not org-capture-clock-keep)
535 (org-capture-get :clock-resume 'local)
536 (markerp (org-capture-get :interrupted-clock 'local))
537 (buffer-live-p (marker-buffer
538 (org-capture-get :interrupted-clock 'local))))
539 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
540 (org-with-point-at clock-in-task
542 (message "Interrupted clock has been resumed")))
544 (let ((beg (point-min))
547 ;; Store the size of the capture buffer
548 (org-capture-put :captured-entry-size (- (point-max) (point-min)))
550 ;; Store the insertion point in the target buffer
551 (org-capture-put :insertion-point (point))
554 (let ((m1 (org-capture-get :begin-marker 'local))
555 (m2 (org-capture-get :end-marker 'local)))
556 (if (and m1 m2 (= m1 beg) (= m2 end))
558 (setq m2 (if (cdr (assoc 'heading org-blank-before-new-entry))
560 m2 (if (< (point-max) m2) (point-max) m2))
561 (setq abort-note 'clean)
563 (setq abort-note 'dirty)))
565 ;; Make sure that the empty lines after are correct
566 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
567 (member (org-capture-get :type 'local)
568 '(entry item checkitem plain)))
571 (or (bolp) (newline))
572 (org-capture-empty-lines-after
573 (or (org-capture-get :empty-lines 'local) 0))))
574 ;; Postprocessing: Update Statistics cookies, do the sorting
577 (when (ignore-errors (org-back-to-heading))
578 (org-update-parent-todo-statistics)
579 (org-update-checkbox-count)))
580 ;; FIXME Here we should do the sorting
581 ;; If we have added a table line, maybe recompute?
582 (when (and (eq (org-capture-get :type 'local) 'table-line)
584 (if (org-table-get-stored-formulas)
585 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
587 ;; Store this place as the last one where we stored something
588 ;; Do the marking in the base buffer, so that it makes sense after
589 ;; the indirect buffer has been killed.
590 (org-capture-bookmark-last-stored-position)
593 (run-hooks 'org-capture-before-finalize-hook))
595 ;; Kill the indirect buffer
597 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
598 (new-buffer (org-capture-get :new-buffer 'local))
599 (kill-buffer (org-capture-get :kill-buffer 'local))
600 (base-buffer (buffer-base-buffer (current-buffer))))
602 ;; Kill the indirect buffer
603 (kill-buffer (current-buffer))
605 ;; Narrow back the target buffer to its previous state
606 (with-current-buffer (org-capture-get :buffer)
607 (let ((reg (org-capture-get :initial-target-region))
608 (pos (org-capture-get :initial-target-position))
609 (ipt (org-capture-get :insertion-point))
610 (size (org-capture-get :captured-entry-size)))
612 (cond ((< ipt (car reg))
613 ;; insertion point is before the narrowed region
614 (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
616 ;; insertion point is after the narrowed region
617 (narrow-to-region (car reg) (cdr reg)))
619 ;; insertion point is within the narrowed region
620 (narrow-to-region (car reg) (+ size (cdr reg)))))
621 ;; now place back the point at its original position
622 (if (< ipt (car reg))
623 (goto-char (+ size pos))
624 (goto-char (if (< ipt pos) (+ size pos) pos))))))
626 ;; Kill the target buffer if that is desired
627 (when (and base-buffer new-buffer kill-buffer)
628 (with-current-buffer base-buffer (save-buffer))
629 (kill-buffer base-buffer))
631 ;; Restore the window configuration before capture
632 (set-window-configuration return-wconf))
634 (run-hooks 'org-capture-after-finalize-hook)
639 ((equal abort-note 'clean)
640 (message "Capture process aborted and target buffer cleaned up"))
641 ((equal abort-note 'dirty)
642 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
644 (org-capture-goto-last-stored)))
645 ;; Return if we did store something
648 (defun org-capture-refile ()
649 "Finalize the current capture and then refile the entry.
650 Refiling is done from the base buffer, because the indirect buffer is then
651 already gone. Any prefix argument will be passed to the refile command."
653 (unless (eq (org-capture-get :type 'local) 'entry)
655 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
657 (base (buffer-base-buffer (current-buffer)))
658 (org-refile-for-capture t))
659 (org-capture-finalize)
660 (save-window-excursion
661 (with-current-buffer (or base (current-buffer))
666 (call-interactively 'org-refile)))))))
668 (defun org-capture-kill ()
669 "Abort the current capture process."
671 ;; FIXME: This does not do the right thing, we need to remove the new stuff
672 ;; By hand it is easy: undo, then kill the buffer
673 (let ((org-note-abort t)
674 (org-capture-before-finalize-hook nil))
675 (org-capture-finalize)))
677 (defun org-capture-goto-last-stored ()
678 "Go to the location where the last capture note was stored."
680 (org-goto-marker-or-bmk org-capture-last-stored-marker
681 "org-capture-last-stored")
682 (message "This is the last note stored by a capture process"))
684 ;;; Supporting functions for handling the process
686 (defun org-capture-put-target-region-and-position ()
687 "Store the initial region with `org-capture-put'."
689 :initial-target-region
690 ;; Check if the buffer is currently narrowed
691 (when (/= (buffer-size) (- (point-max) (point-min)))
692 (cons (point-min) (point-max))))
693 ;; store the current point
694 (org-capture-put :initial-target-position (point)))
696 (defun org-capture-set-target-location (&optional target)
697 "Find target buffer and position and store then in the property list."
698 (let ((target-entry-p t))
699 (setq target (or target (org-capture-get :target)))
702 ((eq (car target) 'file)
703 (set-buffer (org-capture-target-buffer (nth 1 target)))
704 (org-capture-put-target-region-and-position)
706 (setq target-entry-p nil))
708 ((eq (car target) 'id)
709 (let ((loc (org-id-find (nth 1 target))))
711 (error "Cannot find target ID \"%s\"" (nth 1 target))
712 (set-buffer (org-capture-target-buffer (car loc)))
714 (org-capture-put-target-region-and-position)
715 (goto-char (cdr loc)))))
717 ((eq (car target) 'file+headline)
718 (set-buffer (org-capture-target-buffer (nth 1 target)))
719 (org-capture-put-target-region-and-position)
721 (let ((hd (nth 2 target)))
722 (goto-char (point-min))
725 "Target buffer \"%s\" for file+headline should be in Org mode"
727 (if (re-search-forward
728 (format org-complex-heading-regexp-format (regexp-quote hd))
730 (goto-char (point-at-bol))
731 (goto-char (point-max))
732 (or (bolp) (insert "\n"))
733 (insert "* " hd "\n")
734 (beginning-of-line 0))))
736 ((eq (car target) 'file+olp)
737 (let ((m (org-find-olp
738 (cons (org-capture-expand-file (nth 1 target))
740 (set-buffer (marker-buffer m))
741 (org-capture-put-target-region-and-position)
745 ((eq (car target) 'file+regexp)
746 (set-buffer (org-capture-target-buffer (nth 1 target)))
747 (org-capture-put-target-region-and-position)
749 (goto-char (point-min))
750 (if (re-search-forward (nth 2 target) nil t)
752 (goto-char (if (org-capture-get :prepend)
753 (match-beginning 0) (match-end 0)))
754 (org-capture-put :exact-position (point))
755 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
756 (error "No match for target regexp in file %s" (nth 1 target))))
758 ((memq (car target) '(file+datetree file+datetree+prompt))
759 (require 'org-datetree)
760 (set-buffer (org-capture-target-buffer (nth 1 target)))
761 (org-capture-put-target-region-and-position)
763 ;; Make a date tree entry, with the current date (or yesterday,
764 ;; if we are extending dates for a couple of hours)
765 (org-datetree-find-date-create
766 (calendar-gregorian-from-absolute
768 (org-overriding-default-time
769 ;; use the overriding default time
770 (time-to-days org-overriding-default-time))
772 ((eq (car target) 'file+datetree+prompt)
774 (let ((prompt-time (org-read-date
775 nil t nil "Date for tree entry:"
777 (org-capture-put :prompt-time prompt-time)
778 (time-to-days prompt-time)))
780 ;; current date, possible corrected for late night workers
783 ((eq (car target) 'file+function)
784 (set-buffer (org-capture-target-buffer (nth 1 target)))
785 (org-capture-put-target-region-and-position)
787 (funcall (nth 2 target))
788 (org-capture-put :exact-position (point))
789 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
791 ((eq (car target) 'function)
792 (funcall (nth 1 target))
793 (org-capture-put :exact-position (point))
794 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
796 ((eq (car target) 'clock)
797 (if (and (markerp org-clock-hd-marker)
798 (marker-buffer org-clock-hd-marker))
799 (progn (set-buffer (marker-buffer org-clock-hd-marker))
800 (org-capture-put-target-region-and-position)
802 (goto-char org-clock-hd-marker))
803 (error "No running clock that could be used as capture target")))
805 (t (error "Invalid capture target specification")))
807 (org-capture-put :buffer (current-buffer) :pos (point)
808 :target-entry-p target-entry-p))))
810 (defun org-capture-expand-file (file)
811 "Expand functions and symbols for FILE.
812 When FILE is a function, call it. When it is a form, evaluate
813 it. When it is a variable, retrieve the value. Return whatever we get."
815 ((org-string-nw-p file) file)
816 ((functionp file) (funcall file))
817 ((and (symbolp file) (boundp file)) (symbol-value file))
818 ((and file (consp file)) (eval file))
821 (defun org-capture-target-buffer (file)
822 "Get a buffer for FILE."
823 (setq file (org-capture-expand-file file))
824 (setq file (or (org-string-nw-p file)
825 org-default-notes-file
826 (error "No notes file specified, and no default available")))
827 (or (org-find-base-buffer-visiting file)
828 (progn (org-capture-put :new-buffer t)
829 (find-file-noselect (expand-file-name file org-directory)))))
831 (defun org-capture-steal-local-variables (buffer)
832 "Install Org-mode local variables."
834 (ignore-errors (org-set-local (car v) (cdr v))))
835 (buffer-local-variables buffer)))
837 (defun org-capture-place-template ()
838 "Insert the template at the target location, and display the buffer."
839 (org-capture-put :return-to-wconf (current-window-configuration))
840 (delete-other-windows)
841 (org-switch-to-buffer-other-window
842 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
845 (goto-char (org-capture-get :pos))
846 (org-set-local 'org-capture-target-marker
847 (move-marker (make-marker) (point)))
848 (let* ((template (org-capture-get :template))
849 (type (org-capture-get :type)))
851 ((nil entry) (org-capture-place-entry))
852 (table-line (org-capture-place-table-line))
853 (plain (org-capture-place-plain-text))
854 (item (org-capture-place-item))
855 (checkitem (org-capture-place-item))))
857 (org-set-local 'org-capture-current-plist org-capture-plist))
859 (defun org-capture-place-entry ()
860 "Place the template as a new Org entry."
861 (let* ((txt (org-capture-get :template))
862 (reversed (org-capture-get :prepend))
863 (target-entry-p (org-capture-get :target-entry-p))
867 ((org-capture-get :exact-position)
868 (goto-char (org-capture-get :exact-position)))
869 ((not target-entry-p)
870 ;; Insert as top-level entry, either at beginning or at end of file
873 (progn (goto-char (point-min))
874 (or (org-at-heading-p)
875 (outline-next-heading)))
876 (goto-char (point-max))
877 (or (bolp) (insert "\n"))))
879 ;; Insert as a child of the current entry
880 (and (looking-at "\\*+")
881 (setq level (- (match-end 0) (match-beginning 0))))
882 (setq level (org-get-valid-level (or level 1) 1))
885 (outline-next-heading)
886 (or (bolp) (insert "\n")))
887 (org-end-of-subtree t t)
888 (or (bolp) (insert "\n")))))
889 (org-capture-empty-lines-before)
891 (org-capture-verify-tree txt)
892 (org-paste-subtree level txt 'for-yank)
893 (org-capture-empty-lines-after 1)
894 (org-capture-position-for-last-stored beg)
895 (outline-next-heading)
897 (org-capture-mark-kill-region beg (1- end))
898 (org-capture-narrow beg (1- end))
900 (if (re-search-forward "%\\?" end t) (replace-match ""))))
902 (defun org-capture-place-item ()
903 "Place the template as a new plain list item."
904 (let* ((txt (org-capture-get :template))
905 (target-entry-p (org-capture-get :target-entry-p))
909 ((org-capture-get :exact-position)
910 (goto-char (org-capture-get :exact-position)))
911 ((not target-entry-p)
912 ;; Insert as top-level entry, either at beginning or at end of file
913 (setq beg (point-min) end (point-max)))
915 (setq beg (1+ (point-at-eol))
916 end (save-excursion (outline-next-heading) (point)))))
917 (if (org-capture-get :prepend)
920 (if (org-list-search-forward (org-item-beginning-re) end t)
922 (goto-char (match-beginning 0))
923 (setq ind (org-get-indentation)))
927 (if (org-list-search-backward (org-item-beginning-re) beg t)
929 (setq ind (org-get-indentation))
932 ;; Remove common indentation
933 (setq txt (org-remove-indentation txt))
934 ;; Make sure this is indeed an item
935 (unless (string-match (concat "\\`" (org-item-re)) txt)
936 (setq txt (concat "- "
937 (mapconcat 'identity (split-string txt "\n")
939 ;; Set the correct indentation, depending on context
940 (setq ind (make-string ind ?\ ))
941 (setq txt (concat ind
942 (mapconcat 'identity (split-string txt "\n")
945 ;; Insert, with surrounding empty lines
946 (org-capture-empty-lines-before)
949 (or (bolp) (insert "\n"))
950 (org-capture-empty-lines-after 1)
951 (org-capture-position-for-last-stored beg)
954 (org-capture-mark-kill-region beg (1- end))
955 (org-capture-narrow beg (1- end))
956 (if (re-search-forward "%\\?" end t) (replace-match ""))))
958 (defun org-capture-place-table-line ()
959 "Place the template as a table line."
961 (let* ((txt (org-capture-get :template))
962 (target-entry-p (org-capture-get :target-entry-p))
963 (table-line-pos (org-capture-get :table-line-pos))
966 ((org-capture-get :exact-position)
967 (goto-char (org-capture-get :exact-position)))
968 ((not target-entry-p)
969 ;; Table is not necessarily under a heading
970 (setq beg (point-min) end (point-max)))
972 ;; WE are at a heading, limit search to the body
973 (setq beg (1+ (point-at-eol))
974 end (save-excursion (outline-next-heading) (point)))))
975 (if (re-search-forward org-table-dataline-regexp end t)
976 (let ((b (org-table-begin)) (e (org-table-end)))
978 (if (looking-at "[ \t]*#\\+TBLFM:")
980 (narrow-to-region b (point)))
982 (insert "\n| |\n|----|\n| |\n")
983 (narrow-to-region (1+ end) (point)))
984 ;; We are narrowed to the table, or to an empty line if there was no table
986 ;; Check if the template is good
987 (if (not (string-match org-table-dataline-regexp txt))
988 (setq txt "| %?Bad template |\n"))
991 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
992 ;; we have a complex line specification
993 (goto-char (point-min))
994 (let ((nh (- (match-end 1) (match-beginning 1)))
995 (delta (string-to-number (match-string 2 table-line-pos)))
997 ;; The user wants a special position in the table
998 (org-table-get-specials)
999 (setq ll (ignore-errors (aref org-table-hlines nh)))
1000 (unless ll (error "Invalid table line specification \"%s\""
1002 (setq ll (+ ll delta (if (< delta 0) 0 -1)))
1004 (org-table-insert-row 'below)
1005 (beginning-of-line 1)
1006 (delete-region (point) (1+ (point-at-eol)))
1009 (setq end (point))))
1010 ((org-capture-get :prepend)
1011 (goto-char (point-min))
1012 (re-search-forward org-table-hline-regexp nil t)
1013 (beginning-of-line 1)
1014 (re-search-forward org-table-dataline-regexp nil t)
1015 (beginning-of-line 1)
1017 (org-table-insert-row)
1018 (beginning-of-line 1)
1019 (delete-region (point) (1+ (point-at-eol)))
1023 (goto-char (point-max))
1024 (re-search-backward org-table-dataline-regexp nil t)
1025 (beginning-of-line 1)
1026 (org-table-insert-row 'below)
1027 (beginning-of-line 1)
1028 (delete-region (point) (1+ (point-at-eol)))
1031 (setq end (point))))
1033 (org-capture-position-for-last-stored 'table-line)
1034 (if (re-search-forward "%\\?" end t) (replace-match ""))
1037 (defun org-capture-place-plain-text ()
1038 "Place the template plainly.
1039 If the target locator points at an Org node, place the template into
1040 the text of the entry, before the first child. If not, place the
1041 template at the beginning or end of the file.
1042 Of course, if exact position has been required, just put it there."
1043 (let* ((txt (org-capture-get :template))
1046 ((org-capture-get :exact-position)
1047 (goto-char (org-capture-get :exact-position)))
1048 ((and (org-capture-get :target-entry-p)
1050 (looking-at org-outline-regexp))
1051 ;; we should place the text into this entry
1052 (if (org-capture-get :prepend)
1053 ;; Skip meta data and drawers
1054 (org-end-of-meta-data-and-drawers)
1055 ;; go to ent of the entry text, before the next headline
1056 (outline-next-heading)))
1058 ;; beginning or end of file
1059 (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
1060 (or (bolp) (newline))
1061 (org-capture-empty-lines-before)
1064 (org-capture-empty-lines-after 1)
1065 (org-capture-position-for-last-stored beg)
1067 (org-capture-mark-kill-region beg (1- end))
1068 (org-capture-narrow beg (1- end))
1069 (if (re-search-forward "%\\?" end t) (replace-match ""))))
1071 (defun org-capture-mark-kill-region (beg end)
1072 "Mark the region that will have to be killed when aborting capture."
1073 (let ((m1 (move-marker (make-marker) beg))
1074 (m2 (move-marker (make-marker) end)))
1075 (org-capture-put :begin-marker m1)
1076 (org-capture-put :end-marker m2)))
1078 (defun org-capture-position-for-last-stored (where)
1079 "Memorize the position that should later become the position of last capture."
1082 (org-capture-put :position-for-last-stored
1083 (move-marker (make-marker) where
1084 (or (buffer-base-buffer (current-buffer))
1085 (current-buffer)))))
1086 ((eq where 'table-line)
1087 (org-capture-put :position-for-last-stored
1089 (org-table-current-dline))))
1090 (t (error "This should not happen"))))
1092 (defun org-capture-bookmark-last-stored-position ()
1093 "Bookmark the last-captured position."
1094 (let* ((where (org-capture-get :position-for-last-stored 'local))
1097 (prog1 (marker-position where)
1098 (move-marker where nil)))
1099 ((and (listp where) (eq (car where) 'table-line))
1100 (if (org-at-table-p)
1102 (org-table-goto-line (nth 1 where))
1105 (with-current-buffer (buffer-base-buffer (current-buffer))
1110 (bookmark-set "org-capture-last-stored")
1111 (move-marker org-capture-last-stored-marker (point)))))))
1113 (defun org-capture-narrow (beg end)
1114 "Narrow, unless configuration says not to narrow."
1115 (unless (org-capture-get :unnarrowed)
1116 (narrow-to-region beg end)
1119 (defun org-capture-empty-lines-before (&optional n)
1120 "Arrange for the correct number of empty lines before the insertion point.
1121 Point will be after the empty lines, so insertion can directly be done."
1122 (setq n (or n (org-capture-get :empty-lines) 0))
1123 (let ((pos (point)))
1124 (org-back-over-empty-lines)
1125 (delete-region (point) pos)
1126 (if (> n 0) (newline n))))
1128 (defun org-capture-empty-lines-after (&optional n)
1129 "Arrange for the correct number of empty lines after the inserted string.
1130 Point will remain at the first line after the inserted text."
1131 (setq n (or n (org-capture-get :empty-lines) 0))
1132 (org-back-over-empty-lines)
1133 (while (looking-at "[ \t]*\n") (replace-match ""))
1134 (let ((pos (point)))
1135 (if (> n 0) (newline n))
1138 (defvar org-clock-marker) ; Defined in org.el
1140 (defun org-capture-insert-template-here ()
1141 (let* ((template (org-capture-get :template))
1142 (type (org-capture-get :type))
1144 (or (bolp) (newline))
1147 ((and (eq type 'entry) (org-mode-p))
1148 (org-capture-verify-tree (org-capture-get :template))
1149 (org-paste-subtree nil template t))
1150 ((and (memq type '(item checkitem))
1152 (save-excursion (skip-chars-backward " \t\n")
1157 (skip-chars-backward " ")
1158 (skip-chars-backward "-+*0123456789).")
1159 (delete-region (point) (point-at-eol))
1161 (org-remove-indentation template)
1163 (org-capture-empty-lines-after)
1168 (t (insert template)))
1171 (if (re-search-forward "%\\?" end t)
1172 (replace-match ""))))
1174 (defun org-capture-set-plist (entry)
1175 "Initialize the property list from the template definition."
1176 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1177 (org-capture-put :key (car entry) :description (nth 1 entry)
1178 :target (nth 3 entry))
1179 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1180 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1181 ;; The template may be empty or omitted for special types.
1182 ;; Here we insert the default templates for such cases.
1184 ((eq type 'item) (setq txt "- %?"))
1185 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1186 ((eq type 'table-line) (setq txt "| %? |"))
1187 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1188 (org-capture-put :template txt :type type)))
1190 (defun org-capture-goto-target (&optional template-key)
1191 "Go to the target location of a capture template.
1192 The user is queried for the template."
1194 (let* (org-select-template-temp-major-mode
1195 (entry (org-capture-select-template template-key)))
1197 (error "No capture template selected"))
1198 (org-capture-set-plist entry)
1199 (org-capture-set-target-location)
1200 (org-pop-to-buffer-same-window (org-capture-get :buffer))
1201 (goto-char (org-capture-get :pos))))
1203 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1204 "Make an indirect buffer for a capture process.
1205 Use PREFIX as a prefix for the name of the indirect buffer."
1206 (setq buffer (or buffer (current-buffer)))
1207 (let ((n 1) (base (buffer-name buffer)) bname)
1208 (setq bname (concat prefix "-" base))
1209 (while (buffer-live-p (get-buffer bname))
1210 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1212 (make-indirect-buffer buffer bname 'clone)
1213 (error (make-indirect-buffer buffer bname)))))
1216 (defun org-capture-verify-tree (tree)
1217 "Throw error if TREE is not a valid tree"
1218 (unless (org-kill-is-subtree-p tree)
1219 (error "Template is not a valid Org entry or tree")))
1221 ;;; The template code
1223 (defun org-capture-select-template (&optional keys)
1224 "Select a capture template.
1225 Lisp programs can force the template by setting KEYS to a string."
1226 (let ((org-capture-templates
1227 (or org-capture-templates
1228 '(("t" "Task" entry (file+headline "" "Tasks")
1229 "* TODO %?\n %u\n %a")))))
1231 (or (assoc keys org-capture-templates)
1232 (error "No capture template referred to by \"%s\" keys" keys))
1233 (org-mks org-capture-templates
1234 "Select a capture template\n========================="
1236 '(("C" "Customize org-capture-templates")
1239 (defun org-capture-fill-template (&optional template initial annotation)
1240 "Fill a template and return the filled template as a string.
1241 The template may still contain \"%?\" for cursor positioning."
1242 (setq template (or template (org-capture-get :template)))
1243 (when (stringp initial)
1244 (setq initial (org-no-properties initial))
1245 (remove-text-properties 0 (length initial) '(read-only t) initial))
1246 (let* ((buffer (org-capture-get :buffer))
1247 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1248 (ct (org-capture-get :default-time))
1249 (dct (decode-time ct))
1251 (if (< (nth 2 dct) org-extend-today-until)
1252 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
1254 (plist-p (if org-store-link-plist t nil))
1255 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
1256 (v-x (or (org-get-x-clipboard 'PRIMARY)
1257 (org-get-x-clipboard 'CLIPBOARD)
1258 (org-get-x-clipboard 'SECONDARY)))
1259 (v-t (format-time-string (car org-time-stamp-formats) ct))
1260 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
1261 (v-u (concat "[" (substring v-t 1 -1) "]"))
1262 (v-U (concat "[" (substring v-T 1 -1) "]"))
1263 ;; `initial' and `annotation' might habe been passed.
1264 ;; But if the property list has them, we prefer those values
1265 (v-i (or (plist-get org-store-link-plist :initial)
1267 (org-capture-get :initial)
1269 (v-a (or (plist-get org-store-link-plist :annotation)
1271 (org-capture-get :annotation)
1273 ;; Is the link empty? Then we do not want it...
1274 (v-a (if (equal v-a "[[]]") "" v-a))
1275 (clipboards (remove nil (list v-i
1276 (org-get-x-clipboard 'PRIMARY)
1277 (org-get-x-clipboard 'CLIPBOARD)
1278 (org-get-x-clipboard 'SECONDARY)
1282 "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
1283 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
1285 (v-n user-full-name)
1286 (v-k (if (marker-buffer org-clock-marker)
1287 (org-substring-no-properties org-clock-heading)))
1288 (v-K (if (marker-buffer org-clock-marker)
1289 (org-make-link-string
1290 (buffer-file-name (marker-buffer org-clock-marker))
1291 org-clock-heading)))
1292 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1293 (v-F (or (org-capture-get :original-file) ""))
1295 (org-startup-folded nil)
1296 (org-inhibit-startup t)
1297 org-time-was-given org-end-time-was-given x
1298 prompt completions char time pos default histvar)
1300 (setq org-store-link-plist
1301 (plist-put org-store-link-plist :annotation v-a)
1302 org-store-link-plist
1303 (plist-put org-store-link-plist :initial v-i))
1306 (unless template (setq template "") (message "No template") (ding)
1308 (save-window-excursion
1309 (delete-other-windows)
1310 (org-pop-to-buffer-same-window (get-buffer-create "*Capture*"))
1313 (goto-char (point-min))
1314 (org-capture-steal-local-variables buffer)
1315 (setq buffer-file-name nil)
1317 ;; %[] Insert contents of a file.
1318 (goto-char (point-min))
1319 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1320 (unless (org-capture-escaped-%)
1321 (let ((start (match-beginning 0))
1323 (filename (expand-file-name (match-string 1))))
1325 (delete-region start end)
1326 (condition-case error
1327 (insert-file-contents filename)
1328 (error (insert (format "%%![Couldn't insert %s: %s]"
1329 filename error)))))))
1330 ;; %() embedded elisp
1331 (goto-char (point-min))
1332 (while (re-search-forward "%\\((.+)\\)" nil t)
1333 (unless (org-capture-escaped-%)
1334 (goto-char (match-beginning 0))
1335 (let ((template-start (point)))
1337 (let ((result (org-eval (read (current-buffer)))))
1338 (delete-region template-start (point))
1342 (goto-char (point-min))
1343 (while (re-search-forward "%<\\([^>\n]+\\)>" nil t)
1344 (replace-match (format-time-string (match-string 1)) t t))
1347 (goto-char (point-min))
1348 (while (re-search-forward "%\\([tTuUaiAcxkKInfF]\\)" nil t)
1349 (unless (org-capture-escaped-%)
1350 (when (and initial (equal (match-string 0) "%i"))
1352 (let* ((lead (buffer-substring
1353 (point-at-bol) (match-beginning 0))))
1354 (setq v-i (mapconcat 'identity
1355 (org-split-string initial "\n")
1356 (concat "\n" lead))))))
1358 (or (eval (intern (concat "v-" (match-string 1)))) "")
1361 ;; From the property list
1363 (goto-char (point-min))
1364 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
1365 (unless (org-capture-escaped-%)
1366 (and (setq x (or (plist-get org-store-link-plist
1367 (intern (match-string 1))) ""))
1368 (replace-match x t t)))))
1370 ;; Turn on org-mode in temp buffer, set local variables
1371 ;; This is to support completion in interactive prompts
1372 (let ((org-inhibit-startup t)) (org-mode))
1373 ;; Interactive template entries
1374 (goto-char (point-min))
1375 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
1377 (unless (org-capture-escaped-%)
1378 (setq char (if (match-end 3) (match-string-no-properties 3))
1379 prompt (if (match-end 2) (match-string-no-properties 2)))
1380 (goto-char (match-beginning 0))
1382 (setq completions nil default nil)
1384 (setq completions (org-split-string prompt "|")
1385 prompt (pop completions)
1386 default (car completions)
1387 histvar (intern (concat
1388 "org-capture-template-prompt-history::"
1390 completions (mapcar 'list completions)))
1391 (unless (boundp histvar) (set histvar nil))
1393 ((member char '("G" "g"))
1394 (let* ((org-last-tags-completion-table
1395 (org-global-tags-completion-table
1396 (if (equal char "G")
1398 (and file (list file)))))
1399 (org-add-colon-after-tag-completion t)
1400 (ins (org-icompleting-read
1401 (if prompt (concat prompt ": ") "Tags: ")
1402 'org-tags-completion-function nil nil nil
1403 'org-tags-history)))
1404 (setq ins (mapconcat 'identity
1406 ins (org-re "[^[:alnum:]_@#%]+"))
1408 (when (string-match "\\S-" ins)
1409 (or (equal (char-before) ?:) (insert ":"))
1411 (or (equal (char-after) ?:) (insert ":"))
1412 (and (org-on-heading-p) (org-set-tags nil 'align)))))
1414 (cond ((= (length clipboards) 1) (insert (car clipboards)))
1415 ((> (length clipboards) 1)
1416 (insert (read-string "Clipboard/kill value: "
1417 (car clipboards) '(clipboards . 1)
1418 (car clipboards))))))
1420 (cond ((= (length clipboards) 1)
1421 (org-insert-link 0 (car clipboards)))
1422 ((> (length clipboards) 1)
1423 (org-insert-link 0 (read-string "Clipboard/kill value: "
1426 (car clipboards))))))
1428 (org-set-property (org-substring-no-properties prompt) nil))
1430 ;; These are the date/time related ones
1431 (setq org-time-was-given (equal (upcase char) char))
1432 (setq time (org-read-date (equal (upcase char) char) t nil
1434 (if (equal (upcase char) char) (setq org-time-was-given t))
1435 (org-insert-time-stamp time org-time-was-given
1436 (member char '("u" "U"))
1437 nil nil (list org-end-time-was-given)))
1439 (let (org-completion-use-ido)
1440 (insert (org-completing-read-no-i
1441 (concat (if prompt prompt "Enter string")
1442 (if default (concat " [" default "]"))
1444 completions nil nil nil histvar default)))))))
1445 ;; Make sure there are no empty lines before the text, and that
1446 ;; it ends with a newline character
1447 (goto-char (point-min))
1448 (while (looking-at "[ \t]*\n") (replace-match ""))
1449 (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
1450 ;; Return the expanded tempate and kill the temporary buffer
1451 (untabify (point-min) (point-max))
1452 (set-buffer-modified-p nil)
1453 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1455 (defun org-capture-escaped-% ()
1456 "Check if % was escaped - if yes, unescape it now."
1457 (if (equal (char-before (match-beginning 0)) ?\\)
1459 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1464 (defun org-capture-import-remember-templates ()
1465 "Set org-capture-templates to be similar to `org-remember-templates'."
1467 (when (and (yes-or-no-p
1468 "Import old remember templates into org-capture-templates? ")
1470 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1471 (require 'org-remember)
1472 (setq org-capture-templates
1475 (let ((desc (car entry))
1476 (key (char-to-string (nth 1 entry)))
1477 (template (nth 2 entry))
1478 (file (or (nth 3 entry) org-default-notes-file))
1479 (position (or (nth 4 entry) org-remember-default-headline))
1481 (prepend org-reverse-note-order)
1484 ((member position '(top bottom))
1485 (setq target (list 'file file)
1486 prepend (eq position 'top)))
1487 ((eq position 'date-tree)
1488 (setq target (list 'file+datetree file)
1490 (t (setq target (list 'file+headline file position))))
1492 (when (string-match "%!" template)
1493 (setq template (replace-match "" t t template)
1496 (append (list key desc type target template)
1497 (if prepend '(:prepend t))
1498 (if immediate '(:immediate-finish t)))))
1500 org-remember-templates))))
1502 (provide 'org-capture)
1504 ;; arch-tag: 986bf41b-8ada-4e28-bf20-e8388a7205a0
1506 ;;; org-capture.el ends here