org-agenda: fix start/end time in timerange
[org-mode.git] / lisp / org-capture.el
bloba28c3b881f14c4370dce4a25b9f9d0ea7c84dec0
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
8 ;; Version: 7.4
9 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
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.
40 ;;; TODO
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?
49 ;;; Code:
51 (eval-when-compile
52 (require 'cl))
53 (require 'org)
54 (require 'org-mks)
56 (declare-function org-datetree-find-date-create "org-datetree"
57 (DATE &optional KEEP-RESTRICTION))
58 (declare-function org-table-get-specials "org-table" ())
59 (declare-function org-table-goto-line "org-table" (N))
60 (defvar org-remember-default-headline)
61 (defvar org-remember-templates)
62 (defvar org-table-hlines)
64 (defvar org-capture-clock-was-started nil
65 "Internal flag, noting if the clock was started.")
67 (defvar org-capture-last-stored-marker (make-marker)
68 "Marker pointing to the entry most recently stored with `org-capture'.")
70 ;; The following variable is scoped dynamically by org-protocol
71 ;; to indicate that the link properties have already been stored
72 (defvar org-capture-link-is-already-stored nil)
74 (defgroup org-capture nil
75 "Options concerning capturing new entries."
76 :tag "Org Capture"
77 :group 'org)
79 (defcustom org-capture-templates nil
80 "Templates for the creation of new entries.
82 Each entry is a list with the following items:
84 keys The keys that will select the template, as a string, characters
85 only, for example \"a\" for a template to be selected with a
86 single key, or \"bt\" for selection with two keys. When using
87 several keys, keys using the same prefix key must be together
88 in the list and preceded by a 2-element entry explaining the
89 prefix key, for example
91 (\"b\" \"Templates for marking stuff to buy\")
93 The \"C\" key is used by default for quick access to the
94 customization of the template variable. But if you want to use
95 that key for a template, you can.
97 description A short string describing the template, will be shown during
98 selection.
100 type The type of entry. Valid types are:
101 entry an Org-mode node, with a headline. Will be
102 filed as the child of the target entry or as
103 a top-level entry.
104 item a plain list item, will be placed in the
105 first plain list at the target
106 location.
107 checkitem a checkbox item. This differs from the
108 plain list item only is so far as it uses a
109 different default template.
110 table-line a new line in the first table at target location.
111 plain text to be inserted as it is.
113 target Specification of where the captured item should be placed.
114 In Org-mode files, targets usually define a node. Entries will
115 become children of this node, other types will be added to the
116 table or list in the body of this node.
118 Valid values are:
120 (file \"path/to/file\")
121 Text will be placed at the beginning or end of that file
123 (id \"id of existing org entry\")
124 File as child of this entry, or in the body of the entry
126 (file+headline \"path/to/file\" \"node headline\")
127 Fast configuration if the target heading is unique in the file
129 (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
130 For non-unique headings, the full path is safer
132 (file+regexp \"path/to/file\" \"regexp to find location\")
133 File to the entry matching regexp
135 (file+datetree \"path/to/file\")
136 Will create a heading in a date tree for today's date
138 (file+datetree+prompt \"path/to/file\")
139 Will create a heading in a date tree, promts for date
141 (file+function \"path/to/file\" function-finding-location)
142 A function to find the right location in the file
144 (clock)
145 File to the entry that is currently being clocked
147 (function function-finding-location)
148 Most general way, write your own function to find both
149 file and location
151 template The template for creating the capture item. If you leave this
152 empty, an appropriate default template will be used. See below
153 for more details. Instead of a string, this may also be one of
155 (file \"/path/to/template-file\")
156 (function function-returning-the-template)
158 in order to get a template from a file, or dynamically
159 from a function.
161 The rest of the entry is a property list of additional options. Recognized
162 properties are:
164 :prepend Normally newly captured information will be appended at
165 the target location (last child, last table line,
166 last list item...). Setting this property will
167 change that.
169 :immediate-finish When set, do not offer to edit the information, just
170 file it away immediately. This makes sense if the
171 template only needs information that can be added
172 automatically.
174 :empty-lines Set this to the number of lines the should be inserted
175 before and after the new item. Default 0, only common
176 other value is 1.
178 :clock-in Start the clock in this item.
180 :clock-resume Start the interrupted clock when finishing the capture.
182 :unnarrowed Do not narrow the target buffer, simply show the
183 full buffer. Default is to narrow it so that you
184 only see the new stuff.
186 :table-line-pos Specification of the location in the table where the
187 new line should be inserted. It looks like \"II-3\"
188 which means that the new line should become the third
189 line before the second horizontal separator line.
191 :kill-buffer If the target file was not yet visited by a buffer when
192 capture was invoked, kill the buffer again after capture
193 is finalized.
195 The template defines the text to be inserted. Often this is an org-mode
196 entry (so the first line should start with a star) that will be filed as a
197 child of the target headline. It can also be freely formatted text.
198 Furthermore, the following %-escapes will be replaced with content:
200 %^{prompt} prompt the user for a string and replace this sequence with it.
201 A default value and a completion table ca be specified like this:
202 %^{prompt|default|completion2|completion3|...}
203 %t time stamp, date only
204 %T time stamp with date and time
205 %u, %U like the above, but inactive time stamps
206 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
207 You may define a prompt like %^{Please specify birthday
208 %n user name (taken from `user-full-name')
209 %a annotation, normally the link created with `org-store-link'
210 %i initial content, copied from the active region. If %i is
211 indented, the entire inserted text will be indented as well.
212 %c current kill ring head
213 %x content of the X clipboard
214 %^C interactive selection of which kill or clip to use
215 %^L like %^C, but insert as link
216 %k title of currently clocked task
217 %K link to currently clocked task
218 %f file visited by current buffer when org-capture was called
219 %F like @code{%f}, but include full path
220 %^g prompt for tags, with completion on tags in target file
221 %^G prompt for tags, with completion on all tags in all agenda files
222 %^{prop}p prompt the user for a value for property `prop'
223 %:keyword specific information for certain link types, see below
224 %[pathname] insert the contents of the file given by `pathname'
225 %(sexp) evaluate elisp `(sexp)' and replace with the result
227 %? After completing the template, position cursor here.
229 Apart from these general escapes, you can access information specific to the
230 link type that is created. For example, calling `org-capture' in emails
231 or gnus will record the author and the subject of the message, which you
232 can access with \"%:from\" and \"%:subject\", respectively. Here is a
233 complete list of what is recorded for each link type.
235 Link type | Available information
236 ------------------------+------------------------------------------------------
237 bbdb | %:type %:name %:company
238 vm, wl, mh, mew, rmail | %:type %:subject %:message-id
239 | %:from %:fromname %:fromaddress
240 | %:to %:toname %:toaddress
241 | %:fromto (either \"to NAME\" or \"from NAME\")
242 | %:date
243 | %:date-timestamp (as active timestamp)
244 | %:date-timestamp-inactive (as inactive timestamp)
245 gnus | %:group, for messages also all email fields
246 w3, w3m | %:type %:url
247 info | %:type %:file %:node
248 calendar | %:type %:date"
249 :group 'org-capture
250 :type
251 '(repeat
252 (choice :value ("" "" entry (file "~/org/notes.org") "")
253 (list :tag "Multikey description"
254 (string :tag "Keys ")
255 (string :tag "Description"))
256 (list :tag "Template entry"
257 (string :tag "Keys ")
258 (string :tag "Description ")
259 (choice :tag "Capture Type " :value entry
260 (const :tag "Org entry" entry)
261 (const :tag "Plain list item" item)
262 (const :tag "Checkbox item" checkitem)
263 (const :tag "Plain text" plain)
264 (const :tag "Table line" table-line))
265 (choice :tag "Target location"
266 (list :tag "File"
267 (const :format "" file)
268 (file :tag " File"))
269 (list :tag "ID"
270 (const :format "" id)
271 (string :tag " ID"))
272 (list :tag "File & Headline"
273 (const :format "" file+headline)
274 (file :tag " File ")
275 (string :tag " Headline"))
276 (list :tag "File & Outline path"
277 (const :format "" file+olp)
278 (file :tag " File ")
279 (repeat :tag "Outline path" :inline t
280 (string :tag "Headline")))
281 (list :tag "File & Regexp"
282 (const :format "" file+regexp)
283 (file :tag " File ")
284 (regexp :tag " Regexp"))
285 (list :tag "File & Date tree"
286 (const :format "" file+datetree)
287 (file :tag " File"))
288 (list :tag "File & Date tree, prompt for date"
289 (const :format "" file+datetree+prompt)
290 (file :tag " File"))
291 (list :tag "File & function"
292 (const :format "" file+function)
293 (file :tag " File ")
294 (sexp :tag " Function"))
295 (list :tag "Current clocking task"
296 (const :format "" clock))
297 (list :tag "Function"
298 (const :format "" function)
299 (sexp :tag " Function")))
300 (choice :tag "Template"
301 (string)
302 (list :tag "File"
303 (const :format "" file)
304 (file :tag "Template file"))
305 (list :tag "Function"
306 (const :format "" function)
307 (function :tag "Template function")))
308 (plist :inline t
309 ;; Give the most common options as checkboxes
310 :options (((const :format "%v " :prepend) (const t))
311 ((const :format "%v " :immediate-finish) (const t))
312 ((const :format "%v " :empty-lines) (const 1))
313 ((const :format "%v " :clock-in) (const t))
314 ((const :format "%v " :clock-resume) (const t))
315 ((const :format "%v " :unnarrowed) (const t))
316 ((const :format "%v " :kill-buffer) (const t))))))))
318 (defcustom org-capture-before-finalize-hook nil
319 "Hook that is run right before a remember process is finalized.
320 The remember buffer is still current when this hook runs."
321 :group 'org-capture
322 :type 'hook)
324 (defcustom org-capture-after-finalize-hook nil
325 "Hook that is run right after a capture process is finalized.
326 Suitable for window cleanup"
327 :group 'org-capture
328 :type 'hook)
330 ;;; The property list for keeping information about the capture process
332 (defvar org-capture-plist nil
333 "Plist for the current capture process, global, to avoid having to pass it.")
334 (defvar org-capture-current-plist nil
335 "Local variable holding the plist in a capture buffer.
336 This is used to store the plist for use when finishing a capture process.
337 Another such process might have changed the global variable by then.")
339 (defun org-capture-put (&rest stuff)
340 (while stuff
341 (setq org-capture-plist (plist-put org-capture-plist
342 (pop stuff) (pop stuff)))))
343 (defun org-capture-get (prop &optional local)
344 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
346 (defun org-capture-member (prop)
347 (plist-get org-capture-plist prop))
349 ;;; The minor mode
351 (defvar org-capture-mode-map (make-sparse-keymap)
352 "Keymap for `org-capture-mode', a minor mode.
353 Use this map to set additional keybindings for when Org-mode is used
354 for a Remember buffer.")
356 (defvar org-capture-mode-hook nil
357 "Hook for the minor `org-capture-mode'.")
359 (define-minor-mode org-capture-mode
360 "Minor mode for special key bindings in a remember buffer."
361 nil " Rem" org-capture-mode-map
362 (org-set-local
363 'header-line-format
364 "Capture buffer. Finish `C-c C-c', refile `C-c C-w', abort `C-c C-k'.")
365 (run-hooks 'org-capture-mode-hook))
366 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
367 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
368 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
370 ;;; The main commands
372 ;;;###autoload
373 (defun org-capture (&optional goto keys)
374 "Capture something.
375 \\<org-capture-mode-map>
376 This will let you select a template from `org-capture-templates', and then
377 file the newly captured information. The text is immediately inserted
378 at the target location, and an indirect buffer is shown where you can
379 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
380 of Emacs, so that you can continue your work.
382 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
383 anything, just go to the file/headline where the selected template
384 stores its notes. With a double prefix argument \
385 \\[universal-argument] \\[universal-argument], go to the last note
386 stored.
388 When called with a `C-0' (zero) prefix, insert a template at point.
390 Lisp programs can set KEYS to a string associated with a template in
391 `org-capture-templates'. In this case, interactive selection will be
392 bypassed."
393 (interactive "P")
394 (cond
395 ((equal goto '(4)) (org-capture-goto-target))
396 ((equal goto '(16)) (org-capture-goto-last-stored))
398 ;; FIXME: Are these needed?
399 (let* ((orig-buf (current-buffer))
400 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
401 org-capture-link-is-already-stored)
402 (plist-get org-store-link-plist :annotation)
403 (org-store-link nil)))
404 (initial (and (org-region-active-p)
405 (buffer-substring (point) (mark))))
406 (entry (org-capture-select-template keys)))
407 (when (stringp initial)
408 (remove-text-properties 0 (length initial) '(read-only t) initial))
409 (when (stringp annotation)
410 (remove-text-properties 0 (length annotation)
411 '(read-only t) annotation))
412 (cond
413 ((equal entry "C")
414 (customize-variable 'org-capture-templates))
415 ((equal entry "q")
416 (error "Abort"))
418 (org-capture-set-plist entry)
419 (org-capture-get-template)
420 (org-capture-put :original-buffer orig-buf
421 :original-file (buffer-file-name orig-buf)
422 :original-file-nondirectory
423 (and (buffer-file-name orig-buf)
424 (file-name-nondirectory
425 (buffer-file-name orig-buf)))
426 :annotation annotation
427 :initial initial)
428 (org-capture-put :default-time
429 (or org-overriding-default-time
430 (org-current-time)))
431 (org-capture-set-target-location)
432 (condition-case error
433 (org-capture-put :template (org-capture-fill-template))
434 ((error quit)
435 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
436 (error "Capture abort: %s" error)))
438 (if (equal goto 0)
439 ;;insert at point
440 (org-capture-insert-template-here)
441 (condition-case error
442 (org-capture-place-template)
443 ((error quit)
444 (if (and (buffer-base-buffer (current-buffer))
445 (string-match "\\`CAPTURE-" (buffer-name)))
446 (kill-buffer (current-buffer)))
447 (set-window-configuration (org-capture-get :return-to-wconf))
448 (error "Capture template `%s': %s"
449 (org-capture-get :key)
450 (nth 1 error))))
451 (if (org-capture-get :immediate-finish)
452 (org-capture-finalize)
453 (if (and (org-mode-p)
454 (org-capture-get :clock-in))
455 (condition-case nil
456 (progn
457 (if (org-clock-is-active)
458 (org-capture-put :interrupted-clock
459 (copy-marker org-clock-marker)))
460 (org-clock-in)
461 (org-set-local 'org-capture-clock-was-started t))
462 (error
463 "Could not start the clock in this capture buffer")))))))))))
466 (defun org-capture-get-template ()
467 "Get the template from a file or a function if necessary."
468 (let ((txt (org-capture-get :template)) file)
469 (cond
470 ((and (listp txt) (eq (car txt) 'file))
471 (if (file-exists-p
472 (setq file (expand-file-name (nth 1 txt) org-directory)))
473 (setq txt (org-file-contents file))
474 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
475 ((and (listp txt) (eq (car txt) 'function))
476 (if (fboundp (nth 1 txt))
477 (setq txt (funcall (nth 1 txt)))
478 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
479 ((not txt) (setq txt ""))
480 ((stringp txt))
481 (t (setq txt "* Invalid capture template")))
482 (org-capture-put :template txt)))
484 (defun org-capture-finalize (&optional stay-with-capture)
485 "Finalize the capture process.
486 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
487 captured item after finalizing."
488 (interactive "P")
489 (unless (and org-capture-mode
490 (buffer-base-buffer (current-buffer)))
491 (error "This does not seem to be a capture buffer for Org-mode"))
493 ;; Did we start the clock in this capture buffer?
494 (when (and org-capture-clock-was-started
495 org-clock-marker (marker-buffer org-clock-marker)
496 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
497 (> org-clock-marker (point-min))
498 (< org-clock-marker (point-max)))
499 ;; Looks like the clock we started is still running. Clock out.
500 (let (org-log-note-clock-out) (org-clock-out))
501 (when (and (org-capture-get :clock-resume 'local)
502 (markerp (org-capture-get :interrupted-clock 'local))
503 (buffer-live-p (marker-buffer
504 (org-capture-get :interrupted-clock 'local))))
505 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
506 (org-with-point-at clock-in-task
507 (org-clock-in)))
508 (message "Interrupted clock has been resumed")))
510 (let ((beg (point-min))
511 (end (point-max))
512 (abort-note nil))
513 (widen)
515 (if org-note-abort
516 (let ((m1 (org-capture-get :begin-marker 'local))
517 (m2 (org-capture-get :end-marker 'local)))
518 (if (and m1 m2 (= m1 beg) (= m2 end))
519 (progn
520 (setq abort-note 'clean)
521 (kill-region m1 m2))
522 (setq abort-note 'dirty)))
524 ;; Make sure that the empty lines after are correct
525 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
526 (member (org-capture-get :type 'local)
527 '(entry item checkitem plain)))
528 (save-excursion
529 (goto-char end)
530 (or (bolp) (newline))
531 (org-capture-empty-lines-after
532 (or (org-capture-get :empty-lines 'local) 0))))
533 ;; Postprocessing: Update Statistics cookies, do the sorting
534 (when (org-mode-p)
535 (save-excursion
536 (when (ignore-errors (org-back-to-heading))
537 (org-update-parent-todo-statistics)
538 (org-update-checkbox-count)))
539 ;; FIXME Here we should do the sorting
540 ;; If we have added a table line, maybe recompute?
541 (when (and (eq (org-capture-get :type 'local) 'table-line)
542 (org-at-table-p))
543 (if (org-table-get-stored-formulas)
544 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
545 (org-table-align)))
547 ;; Store this place as the last one where we stored something
548 ;; Do the marking in the base buffer, so that it makes sense after
549 ;; the indirect buffer has been killed.
550 (org-capture-bookmark-last-stored-position)
552 ;; Run the hook
553 (run-hooks 'org-capture-before-finalize-hook)
556 ;; Kill the indirect buffer
557 (save-buffer)
558 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
559 (new-buffer (org-capture-get :new-buffer 'local))
560 (kill-buffer (org-capture-get :kill-buffer 'local))
561 (base-buffer (buffer-base-buffer (current-buffer))))
563 ;; Kill the indiret buffer
564 (kill-buffer (current-buffer))
566 ;; Kill the target buffer if that is desired
567 (when (and base-buffer new-buffer kill-buffer)
568 (with-current-buffer base-buffer (save-buffer))
569 (kill-buffer base-buffer))
571 ;; Restore the window configuration before capture
572 (set-window-configuration return-wconf))
574 (run-hooks 'org-capture-after-finalize-hook)
575 ;; Special cases
576 (cond
577 (abort-note
578 (cond
579 ((equal abort-note 'clean)
580 (message "Capture process aborted and target buffer cleaned up"))
581 ((equal abort-note 'dirty)
582 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
583 (stay-with-capture
584 (org-capture-goto-last-stored)))
585 ;; Return if we did store something
586 (not abort-note)))
588 (defun org-capture-refile ()
589 "Finalize the current capture and then refile the entry.
590 Refiling is done from the base buffer, because the indirect buffer is then
591 already gone. Any prefix argument will be passed to the refile comand."
592 (interactive)
593 (unless (eq (org-capture-get :type 'local) 'entry)
594 (error
595 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
596 (let ((pos (point))
597 (base (buffer-base-buffer (current-buffer)))
598 (org-refile-for-capture t))
599 (org-capture-finalize)
600 (save-window-excursion
601 (with-current-buffer (or base (current-buffer))
602 (save-excursion
603 (save-restriction
604 (widen)
605 (goto-char pos)
606 (call-interactively 'org-refile)))))))
608 (defun org-capture-kill ()
609 "Abort the current capture process."
610 (interactive)
611 ;; FIXME: This does not do the right thing, we need to remove the new stuff
612 ;; By hand it is easy: undo, then kill the buffer
613 (let ((org-note-abort t) (org-capture-before-finalize-hook nil))
614 (org-capture-finalize)))
616 (defun org-capture-goto-last-stored ()
617 "Go to the location where the last remember note was stored."
618 (interactive)
619 (org-goto-marker-or-bmk org-capture-last-stored-marker
620 "org-capture-last-stored")
621 (message "This is the last note stored by a capture process"))
623 ;;; Supporting functions for handling the process
625 (defun org-capture-set-target-location (&optional target)
626 "Find target buffer and position and store then in the property list."
627 (let ((target-entry-p t))
628 (setq target (or target (org-capture-get :target)))
629 (save-excursion
630 (cond
631 ((eq (car target) 'file)
632 (set-buffer (org-capture-target-buffer (nth 1 target)))
633 (setq target-entry-p nil))
635 ((eq (car target) 'id)
636 (let ((loc (org-id-find (nth 1 target))))
637 (if (not loc)
638 (error "Cannot find target ID \"%s\"" (nth 1 target))
639 (set-buffer (org-capture-target-buffer (car loc)))
640 (goto-char (cdr loc)))))
642 ((eq (car target) 'file+headline)
643 (set-buffer (org-capture-target-buffer (nth 1 target)))
644 (let ((hd (nth 2 target)))
645 (goto-char (point-min))
646 (unless (org-mode-p)
647 (error
648 "Target buffer \"%s\" for file+headline should be in Org mode"
649 (current-buffer)))
650 (if (re-search-forward
651 (format org-complex-heading-regexp-format (regexp-quote hd))
652 nil t)
653 (goto-char (point-at-bol))
654 (goto-char (point-max))
655 (or (bolp) (insert "\n"))
656 (insert "* " hd "\n")
657 (beginning-of-line 0))))
659 ((eq (car target) 'file+olp)
660 (let ((m (org-find-olp (cdr target))))
661 (set-buffer (marker-buffer m))
662 (goto-char m)))
664 ((eq (car target) 'file+regexp)
665 (set-buffer (org-capture-target-buffer (nth 1 target)))
666 (goto-char (point-min))
667 (if (re-search-forward (nth 2 target) nil t)
668 (progn
669 (goto-char (if (org-capture-get :prepend)
670 (match-beginning 0) (match-end 0)))
671 (org-capture-put :exact-position (point))
672 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
673 (error "No match for target regexp in file %s" (nth 1 target))))
675 ((memq (car target) '(file+datetree file+datetree+prompt))
676 (require 'org-datetree)
677 (set-buffer (org-capture-target-buffer (nth 1 target)))
678 ;; Make a date tree entry, with the current date (or yesterday,
679 ;; if we are extending dates for a couple of hours)
680 (org-datetree-find-date-create
681 (calendar-gregorian-from-absolute
682 (cond
684 (org-overriding-default-time
685 ;; use the overriding default time
686 (time-to-days org-overriding-default-time))
688 ((eq (car target) 'file+datetree+prompt)
689 ;; prompt for date
690 (time-to-days (org-read-date
691 nil t nil "Date for tree entry:"
692 (current-time))))
694 ;; current date, possible corrected for late night workers
695 (org-today))))))
697 ((eq (car target) 'file+function)
698 (set-buffer (org-capture-target-buffer (nth 1 target)))
699 (funcall (nth 2 target))
700 (org-capture-put :exact-position (point))
701 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
703 ((eq (car target) 'function)
704 (funcall (nth 1 target))
705 (org-capture-put :exact-position (point))
706 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
708 ((eq (car target) 'clock)
709 (if (and (markerp org-clock-hd-marker)
710 (marker-buffer org-clock-hd-marker))
711 (progn (set-buffer (marker-buffer org-clock-hd-marker))
712 (goto-char org-clock-hd-marker))
713 (error "No running clock that could be used as capture target")))
715 (t (error "Invalid capture target specification")))
717 (org-capture-put :buffer (current-buffer) :pos (point)
718 :target-entry-p target-entry-p))))
720 (defun org-capture-target-buffer (file)
721 "Get a buffer for FILE."
722 (setq file (or (org-string-nw-p file)
723 org-default-notes-file
724 (error "No notes file specified, and no default available")))
725 (or (org-find-base-buffer-visiting file)
726 (progn (org-capture-put :new-buffer t)
727 (find-file-noselect (expand-file-name file org-directory)))))
729 (defun org-capture-steal-local-variables (buffer)
730 "Install Org-mode local variables."
731 (mapc (lambda (v)
732 (ignore-errors (org-set-local (car v) (cdr v))))
733 (buffer-local-variables buffer)))
735 (defun org-capture-place-template ()
736 "Insert the template at the target location, and display the buffer."
737 (org-capture-put :return-to-wconf (current-window-configuration))
738 (delete-other-windows)
739 (org-switch-to-buffer-other-window
740 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
741 (widen)
742 (show-all)
743 (goto-char (org-capture-get :pos))
744 (org-set-local 'org-capture-target-marker
745 (move-marker (make-marker) (point)))
746 (let* ((template (org-capture-get :template))
747 (type (org-capture-get :type)))
748 (case type
749 ((nil entry) (org-capture-place-entry))
750 (table-line (org-capture-place-table-line))
751 (plain (org-capture-place-plain-text))
752 (item (org-capture-place-item))
753 (checkitem (org-capture-place-item))))
754 (org-capture-mode 1)
755 (org-set-local 'org-capture-current-plist org-capture-plist))
757 (defun org-capture-place-entry ()
758 "Place the template as a new Org entry."
759 (let* ((txt (org-capture-get :template))
760 (reversed (org-capture-get :prepend))
761 (target-entry-p (org-capture-get :target-entry-p))
762 level beg end file)
764 (cond
765 ((org-capture-get :exact-position)
766 (goto-char (org-capture-get :exact-position)))
767 ((not target-entry-p)
768 ;; Insert as top-level entry, either at beginning or at end of file
769 (setq level 1)
770 (if reversed
771 (progn (goto-char (point-min))
772 (or (org-at-heading-p)
773 (outline-next-heading)))
774 (goto-char (point-max))
775 (or (bolp) (insert "\n"))))
777 ;; Insert as a child of the current entry
778 (and (looking-at "\\*+")
779 (setq level (- (match-end 0) (match-beginning 0))))
780 (setq level (org-get-valid-level (or level 1) 1))
781 (if reversed
782 (progn
783 (outline-next-heading)
784 (or (bolp) (insert "\n")))
785 (org-end-of-subtree t t)
786 (or (bolp) (insert "\n")))))
787 (org-capture-empty-lines-before)
788 (setq beg (point))
789 (org-capture-verify-tree txt)
790 (org-paste-subtree level txt 'for-yank)
791 (org-capture-empty-lines-after 1)
792 (org-capture-position-for-last-stored beg)
793 (outline-next-heading)
794 (setq end (point))
795 (org-capture-mark-kill-region beg (1- end))
796 (org-capture-narrow beg (1- end))
797 (goto-char beg)
798 (if (re-search-forward "%\\?" end t) (replace-match ""))))
800 (defun org-capture-place-item ()
801 "Place the template as a new plain list item."
802 (let* ((txt (org-capture-get :template))
803 (target-entry-p (org-capture-get :target-entry-p))
804 (ind 0)
805 beg end)
806 (cond
807 ((org-capture-get :exact-position)
808 (goto-char (org-capture-get :exact-position)))
809 ((not target-entry-p)
810 ;; Insert as top-level entry, either at beginning or at end of file
811 (setq beg (point-min) end (point-max)))
813 (setq beg (1+ (point-at-eol))
814 end (save-excursion (outline-next-heading) (point)))))
815 (if (org-capture-get :prepend)
816 (progn
817 (goto-char beg)
818 (if (org-search-forward-unenclosed org-item-beginning-re end t)
819 (progn
820 (goto-char (match-beginning 0))
821 (setq ind (org-get-indentation)))
822 (goto-char end)
823 (setq ind 0)))
824 (goto-char end)
825 (if (org-search-backward-unenclosed org-item-beginning-re beg t)
826 (progn
827 (setq ind (org-get-indentation))
828 (org-end-of-item))
829 (setq ind 0)))
830 ;; Remove common indentation
831 (setq txt (org-remove-indentation txt))
832 ;; Make sure this is indeed an item
833 (unless (string-match (concat "\\`" (org-item-re)) txt)
834 (setq txt (concat "- "
835 (mapconcat 'identity (split-string txt "\n")
836 "\n "))))
837 ;; Set the correct indentation, depending on context
838 (setq ind (make-string ind ?\ ))
839 (setq txt (concat ind
840 (mapconcat 'identity (split-string txt "\n")
841 (concat "\n" ind))
842 "\n"))
843 ;; Insert, with surrounding empty lines
844 (org-capture-empty-lines-before)
845 (setq beg (point))
846 (insert txt)
847 (or (bolp) (insert "\n"))
848 (org-capture-empty-lines-after 1)
849 (org-capture-position-for-last-stored beg)
850 (forward-char 1)
851 (setq end (point))
852 (org-capture-mark-kill-region beg (1- end))
853 (org-capture-narrow beg (1- end))
854 (if (re-search-forward "%\\?" end t) (replace-match ""))))
856 (defun org-capture-place-table-line ()
857 "Place the template as a table line."
858 (require 'org-table)
859 (let* ((txt (org-capture-get :template))
860 (target-entry-p (org-capture-get :target-entry-p))
861 (table-line-pos (org-capture-get :table-line-pos))
862 ind beg end)
863 (cond
864 ((org-capture-get :exact-position)
865 (goto-char (org-capture-get :exact-position)))
866 ((not target-entry-p)
867 ;; Table is not necessarily under a heading
868 (setq beg (point-min) end (point-max)))
870 ;; WE are at a heading, limit search to the body
871 (setq beg (1+ (point-at-eol))
872 end (save-excursion (outline-next-heading) (point)))))
873 (if (re-search-forward org-table-dataline-regexp end t)
874 (let ((b (org-table-begin)) (e (org-table-end)))
875 (goto-char e)
876 (if (looking-at "[ \t]*#\\+TBLFM:")
877 (forward-line 1))
878 (narrow-to-region b (point)))
879 (goto-char end)
880 (insert "\n| |\n|----|\n| |\n")
881 (narrow-to-region (1+ end) (point)))
882 ;; We are narrowed to the table, or to an empty line if there was no table
884 ;; Check if the template is good
885 (if (not (string-match org-table-dataline-regexp txt))
886 (setq txt "| %?Bad template |\n"))
887 (cond
888 ((and table-line-pos
889 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
890 ;; we have a complex line specification
891 (goto-char (point-min))
892 (let ((nh (- (match-end 1) (match-beginning 1)))
893 (delta (string-to-number (match-string 2 table-line-pos)))
895 ;; The user wants a special position in the table
896 (org-table-get-specials)
897 (setq ll (ignore-errors (aref org-table-hlines nh)))
898 (unless ll (error "Invalid table line specification \"%s\""
899 table-line-pos))
900 (setq ll (+ ll delta (if (< delta 0) 0 -1)))
901 (org-goto-line ll)
902 (org-table-insert-row 'below)
903 (beginning-of-line 1)
904 (delete-region (point) (1+ (point-at-eol)))
905 (setq beg (point))
906 (insert txt)
907 (setq end (point))))
908 ((org-capture-get :prepend)
909 (goto-char (point-min))
910 (re-search-forward org-table-hline-regexp nil t)
911 (beginning-of-line 1)
912 (re-search-forward org-table-dataline-regexp nil t)
913 (beginning-of-line 1)
914 (setq beg (point))
915 (org-table-insert-row)
916 (beginning-of-line 1)
917 (delete-region (point) (1+ (point-at-eol)))
918 (insert txt)
919 (setq end (point)))
921 (goto-char (point-max))
922 (re-search-backward org-table-dataline-regexp nil t)
923 (beginning-of-line 1)
924 (org-table-insert-row 'below)
925 (beginning-of-line 1)
926 (delete-region (point) (1+ (point-at-eol)))
927 (setq beg (point))
928 (insert txt)
929 (setq end (point))))
930 (goto-char beg)
931 (org-capture-position-for-last-stored 'table-line)
932 (if (re-search-forward "%\\?" end t) (replace-match ""))
933 (org-table-align)))
935 (defun org-capture-place-plain-text ()
936 "Place the template plainly."
937 (let* ((txt (org-capture-get :template))
938 beg end)
939 (goto-char (cond
940 ((org-capture-get :exact-position))
941 ((org-capture-get :prepend) (point-min))
942 (t (point-max))))
943 (or (bolp) (newline))
944 (org-capture-empty-lines-before)
945 (setq beg (point))
946 (insert txt)
947 (org-capture-empty-lines-after 1)
948 (org-capture-position-for-last-stored beg)
949 (setq end (point))
950 (org-capture-mark-kill-region beg (1- end))
951 (org-capture-narrow beg (1- end))
952 (if (re-search-forward "%\\?" end t) (replace-match ""))))
954 (defun org-capture-mark-kill-region (beg end)
955 "Mark the region that will have to be killed when aborting capture."
956 (let ((m1 (move-marker (make-marker) beg))
957 (m2 (move-marker (make-marker) end)))
958 (org-capture-put :begin-marker m1)
959 (org-capture-put :end-marker m2)))
961 (defun org-capture-position-for-last-stored (where)
962 "Memorize the position that should later become the position of last capture."
963 (cond
964 ((integerp where)
965 (org-capture-put :position-for-last-stored
966 (move-marker (make-marker) where
967 (or (buffer-base-buffer (current-buffer))
968 (current-buffer)))))
969 ((eq where 'table-line)
970 (org-capture-put :position-for-last-stored
971 (list 'table-line
972 (org-table-current-dline))))
973 (t (error "This should not happen"))))
975 (defun org-capture-bookmark-last-stored-position ()
976 "Bookmark the last-captured position."
977 (let* ((where (org-capture-get :position-for-last-stored 'local))
978 (pos (cond
979 ((markerp where)
980 (prog1 (marker-position where)
981 (move-marker where nil)))
982 ((and (listp where) (eq (car where) 'table-line))
983 (if (org-at-table-p)
984 (save-excursion
985 (org-table-goto-line (nth 1 where))
986 (point-at-bol))
987 (point))))))
988 (with-current-buffer (buffer-base-buffer (current-buffer))
989 (save-excursion
990 (save-restriction
991 (widen)
992 (goto-char pos)
993 (bookmark-set "org-capture-last-stored")
994 (move-marker org-capture-last-stored-marker (point)))))))
996 (defun org-capture-narrow (beg end)
997 "Narrow, unless configuration says not to narrow."
998 (unless (org-capture-get :unnarrowed)
999 (narrow-to-region beg end)
1000 (goto-char beg)))
1002 (defun org-capture-empty-lines-before (&optional n)
1003 "Arrange for the correct number of empty lines before the insertion point.
1004 Point will be after the empty lines, so insertion can directly be done."
1005 (setq n (or n (org-capture-get :empty-lines) 0))
1006 (let ((pos (point)))
1007 (org-back-over-empty-lines)
1008 (delete-region (point) pos)
1009 (if (> n 0) (newline n))))
1011 (defun org-capture-empty-lines-after (&optional n)
1012 "Arrange for the correct number of empty lines after the inserted string.
1013 Point will remain at the first line after the inserted text."
1014 (setq n (or n (org-capture-get :empty-lines) 0))
1015 (org-back-over-empty-lines)
1016 (while (looking-at "[ \t]*\n") (replace-match ""))
1017 (let ((pos (point)))
1018 (if (> n 0) (newline n))
1019 (goto-char pos)))
1021 (defvar org-clock-marker) ; Defined in org.el
1022 ;;;###autoload
1023 (defun org-capture-insert-template-here ()
1024 (let* ((template (org-capture-get :template))
1025 (type (org-capture-get :type))
1026 beg end pp)
1027 (or (bolp) (newline))
1028 (setq beg (point))
1029 (cond
1030 ((and (eq type 'entry) (org-mode-p))
1031 (org-capture-verify-tree txt)
1032 (org-paste-subtree nil template t))
1033 ((and (memq type '(item checkitem))
1034 (org-mode-p)
1035 (save-excursion (skip-chars-backward " \t\n")
1036 (setq pp (point))
1037 (org-in-item-p)))
1038 (goto-char pp)
1039 (org-insert-item)
1040 (skip-chars-backward " ")
1041 (skip-chars-backward "-+*0123456789).")
1042 (delete-region (point) (point-at-eol))
1043 (setq beg (point))
1044 (org-remove-indentation template)
1045 (insert template)
1046 (org-capture-empty-lines-after)
1047 (goto-char beg)
1048 (org-list-repair)
1049 (org-end-of-item)
1050 (setq end (point)))
1051 (t (insert template)))
1052 (setq end (point))
1053 (goto-char beg)
1054 (if (re-search-forward "%\\?" end t)
1055 (replace-match ""))))
1057 (defun org-capture-set-plist (entry)
1058 "Initialize the property list from the template definition."
1059 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1060 (org-capture-put :key (car entry) :description (nth 1 entry)
1061 :target (nth 3 entry))
1062 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1063 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1064 ;; The template may be empty or omitted for special types.
1065 ;; Here we insert the default templates for such cases.
1066 (cond
1067 ((eq type 'item) (setq txt "- %?"))
1068 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1069 ((eq type 'table-line) (setq txt "| %? |"))
1070 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1071 (org-capture-put :template txt :type type)))
1073 (defun org-capture-goto-target (&optional template-key)
1074 "Go to the target location of a capture template.
1075 The user is queried for the template."
1076 (interactive)
1077 (let* (org-select-template-temp-major-mode
1078 (entry (org-capture-select-template template-key)))
1079 (unless entry
1080 (error "No capture template selected"))
1081 (org-capture-set-plist entry)
1082 (org-capture-set-target-location)
1083 (switch-to-buffer (org-capture-get :buffer))
1084 (goto-char (org-capture-get :pos))))
1086 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1087 "Make an indirect buffer for a capture process.
1088 Use PREFIX as a prefix for the name of the indirect buffer."
1089 (setq buffer (or buffer (current-buffer)))
1090 (let ((n 1) (base (buffer-name buffer)) bname)
1091 (setq bname (concat prefix "-" base))
1092 (while (buffer-live-p (get-buffer bname))
1093 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1094 (condition-case nil
1095 (make-indirect-buffer buffer bname 'clone)
1096 (error (make-indirect-buffer buffer bname)))))
1099 (defun org-capture-verify-tree (tree)
1100 "Throw error if TREE is not a valid tree"
1101 (unless (org-kill-is-subtree-p tree)
1102 (error "Template is not a valid Org entry or tree")))
1104 ;;; The template code
1106 (defun org-capture-select-template (&optional keys)
1107 "Select a capture template.
1108 Lisp programs can force the template by setting KEYS to a string."
1109 (let ((org-capture-templates
1110 (or org-capture-templates
1111 '(("t" "Task" entry (file+headline "" "Tasks")
1112 "* TODO %?\n %u\n %a")))))
1113 (if keys
1114 (or (assoc keys org-capture-templates)
1115 (error "No capture template referred to by \"%s\" keys" keys))
1116 (org-mks org-capture-templates
1117 "Select a capture template\n========================="
1118 "Template key: "
1119 '(("C" "Customize org-capture-templates")
1120 ("q" "Abort"))))))
1122 (defun org-capture-fill-template (&optional template initial annotation)
1123 "Fill a template and return the filled template as a string.
1124 The template may still contain \"%?\" for cursor positioning."
1125 (setq template (or template (org-capture-get :template)))
1126 (when (stringp initial)
1127 (setq initial (org-no-properties initial))
1128 (remove-text-properties 0 (length initial) '(read-only t) initial))
1129 (let* ((buffer (org-capture-get :buffer))
1130 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1131 (ct (org-capture-get :default-time))
1132 (dct (decode-time ct))
1133 (ct1
1134 (if (< (nth 2 dct) org-extend-today-until)
1135 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
1136 ct))
1137 (plist-p (if org-store-link-plist t nil))
1138 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
1139 (v-x (or (org-get-x-clipboard 'PRIMARY)
1140 (org-get-x-clipboard 'CLIPBOARD)
1141 (org-get-x-clipboard 'SECONDARY)))
1142 (v-t (format-time-string (car org-time-stamp-formats) ct))
1143 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
1144 (v-u (concat "[" (substring v-t 1 -1) "]"))
1145 (v-U (concat "[" (substring v-T 1 -1) "]"))
1146 ;; `initial' and `annotation' might habe been passed.
1147 ;; But if the property list has them, we prefer those values
1148 (v-i (or (plist-get org-store-link-plist :initial)
1149 initial
1150 (org-capture-get :initial)
1151 ""))
1152 (v-a (or (plist-get org-store-link-plist :annotation)
1153 annotation
1154 (org-capture-get :annotation)
1155 ""))
1156 ;; Is the link empty? Then we do not want it...
1157 (v-a (if (equal v-a "[[]]") "" v-a))
1158 (clipboards (remove nil (list v-i
1159 (org-get-x-clipboard 'PRIMARY)
1160 (org-get-x-clipboard 'CLIPBOARD)
1161 (org-get-x-clipboard 'SECONDARY)
1162 v-c)))
1163 (v-A (if (and v-a
1164 (string-match
1165 "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
1166 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
1167 v-a))
1168 (v-n user-full-name)
1169 (v-k (if (marker-buffer org-clock-marker)
1170 (org-substring-no-properties org-clock-heading)))
1171 (v-K (if (marker-buffer org-clock-marker)
1172 (org-make-link-string
1173 (buffer-file-name (marker-buffer org-clock-marker))
1174 org-clock-heading)))
1175 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1176 (v-F (or (org-capture-get :original-file) ""))
1178 (org-startup-folded nil)
1179 (org-inhibit-startup t)
1180 org-time-was-given org-end-time-was-given x
1181 prompt completions char time pos default histvar)
1183 (setq org-store-link-plist
1184 (plist-put org-store-link-plist :annotation v-a)
1185 org-store-link-plist
1186 (plist-put org-store-link-plist :initial v-i))
1187 (setq initial v-i)
1189 (unless template (setq template "") (message "No template") (ding)
1190 (sit-for 1))
1191 (save-window-excursion
1192 (delete-other-windows)
1193 (switch-to-buffer (get-buffer-create "*Capture*"))
1194 (erase-buffer)
1195 (insert template)
1196 (goto-char (point-min))
1197 (org-capture-steal-local-variables buffer)
1198 (setq buffer-file-name nil)
1200 ;; %[] Insert contents of a file.
1201 (goto-char (point-min))
1202 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1203 (unless (org-capture-escaped-%)
1204 (let ((start (match-beginning 0))
1205 (end (match-end 0))
1206 (filename (expand-file-name (match-string 1))))
1207 (goto-char start)
1208 (delete-region start end)
1209 (condition-case error
1210 (insert-file-contents filename)
1211 (error (insert (format "%%![Couldn't insert %s: %s]"
1212 filename error)))))))
1213 ;; %() embedded elisp
1214 (goto-char (point-min))
1215 (while (re-search-forward "%\\((.+)\\)" nil t)
1216 (unless (org-capture-escaped-%)
1217 (goto-char (match-beginning 0))
1218 (let ((template-start (point)))
1219 (forward-char 1)
1220 (let ((result (org-eval (read (current-buffer)))))
1221 (delete-region template-start (point))
1222 (insert result)))))
1224 ;; Simple %-escapes
1225 (goto-char (point-min))
1226 (while (re-search-forward "%\\([tTuUaiAcxkKInfF]\\)" nil t)
1227 (unless (org-capture-escaped-%)
1228 (when (and initial (equal (match-string 0) "%i"))
1229 (save-match-data
1230 (let* ((lead (buffer-substring
1231 (point-at-bol) (match-beginning 0))))
1232 (setq v-i (mapconcat 'identity
1233 (org-split-string initial "\n")
1234 (concat "\n" lead))))))
1235 (replace-match
1236 (or (eval (intern (concat "v-" (match-string 1)))) "")
1237 t t)))
1239 ;; From the property list
1240 (when plist-p
1241 (goto-char (point-min))
1242 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
1243 (unless (org-capture-escaped-%)
1244 (and (setq x (or (plist-get org-store-link-plist
1245 (intern (match-string 1))) ""))
1246 (replace-match x t t)))))
1248 ;; Turn on org-mode in temp buffer, set local variables
1249 ;; This is to support completion in interactive prompts
1250 (let ((org-inhibit-startup t)) (org-mode))
1251 ;; Interactive template entries
1252 (goto-char (point-min))
1253 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
1254 nil t)
1255 (unless (org-capture-escaped-%)
1256 (setq char (if (match-end 3) (match-string 3))
1257 prompt (if (match-end 2) (match-string 2)))
1258 (goto-char (match-beginning 0))
1259 (replace-match "")
1260 (setq completions nil default nil)
1261 (when prompt
1262 (setq completions (org-split-string prompt "|")
1263 prompt (pop completions)
1264 default (car completions)
1265 histvar (intern (concat
1266 "org-capture-template-prompt-history::"
1267 (or prompt "")))
1268 completions (mapcar 'list completions)))
1269 (unless (boundp histvar) (set histvar nil))
1270 (cond
1271 ((member char '("G" "g"))
1272 (let* ((org-last-tags-completion-table
1273 (org-global-tags-completion-table
1274 (if (equal char "G")
1275 (org-agenda-files)
1276 (and file (list file)))))
1277 (org-add-colon-after-tag-completion t)
1278 (ins (org-icompleting-read
1279 (if prompt (concat prompt ": ") "Tags: ")
1280 'org-tags-completion-function nil nil nil
1281 'org-tags-history)))
1282 (setq ins (mapconcat 'identity
1283 (org-split-string
1284 ins (org-re "[^[:alnum:]_@#%]+"))
1285 ":"))
1286 (when (string-match "\\S-" ins)
1287 (or (equal (char-before) ?:) (insert ":"))
1288 (insert ins)
1289 (or (equal (char-after) ?:) (insert ":"))
1290 (and (org-on-heading-p) (org-set-tags nil 'align)))))
1291 ((equal char "C")
1292 (cond ((= (length clipboards) 1) (insert (car clipboards)))
1293 ((> (length clipboards) 1)
1294 (insert (read-string "Clipboard/kill value: "
1295 (car clipboards) '(clipboards . 1)
1296 (car clipboards))))))
1297 ((equal char "L")
1298 (cond ((= (length clipboards) 1)
1299 (org-insert-link 0 (car clipboards)))
1300 ((> (length clipboards) 1)
1301 (org-insert-link 0 (read-string "Clipboard/kill value: "
1302 (car clipboards)
1303 '(clipboards . 1)
1304 (car clipboards))))))
1305 ((equal char "p")
1306 (org-set-property (org-substring-no-properties prompt) nil))
1307 (char
1308 ;; These are the date/time related ones
1309 (setq org-time-was-given (equal (upcase char) char))
1310 (setq time (org-read-date (equal (upcase char) char) t nil
1311 prompt))
1312 (if (equal (upcase char) char) (setq org-time-was-given t))
1313 (org-insert-time-stamp time org-time-was-given
1314 (member char '("u" "U"))
1315 nil nil (list org-end-time-was-given)))
1317 (let (org-completion-use-ido)
1318 (insert (org-completing-read-no-i
1319 (concat (if prompt prompt "Enter string")
1320 (if default (concat " [" default "]"))
1321 ": ")
1322 completions nil nil nil histvar default)))))))
1323 ;; Make sure there are no empty lines before the text, and that
1324 ;; it ends with a newline character
1325 (goto-char (point-min))
1326 (while (looking-at "[ \t]*\n") (replace-match ""))
1327 (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
1328 ;; Return the expanded tempate and kill the temporary buffer
1329 (untabify (point-min) (point-max))
1330 (set-buffer-modified-p nil)
1331 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1333 (defun org-capture-escaped-% ()
1334 "Check if % was escaped - if yes, unescape it now."
1335 (if (equal (char-before (match-beginning 0)) ?\\)
1336 (progn
1337 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1339 nil))
1341 ;;;###autoload
1342 (defun org-capture-import-remember-templates ()
1343 "Set org-capture-templates to be similar to `org-remember-templates'."
1344 (interactive)
1345 (when (and (yes-or-no-p
1346 "Import old remember templates into org-capture-templates? ")
1347 (yes-or-no-p
1348 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1349 (require 'org-remember)
1350 (setq org-capture-templates
1351 (mapcar
1352 (lambda (entry)
1353 (let ((desc (car entry))
1354 (key (char-to-string (nth 1 entry)))
1355 (template (nth 2 entry))
1356 (file (or (nth 3 entry) org-default-notes-file))
1357 (position (or (nth 4 entry) org-remember-default-headline))
1358 (type 'entry)
1359 (prepend org-reverse-note-order)
1360 immediate target)
1361 (cond
1362 ((member position '(top bottom))
1363 (setq target (list 'file file)
1364 prepend (eq position 'top)))
1365 ((eq position 'date-tree)
1366 (setq target (list 'file+datetree file)
1367 prepend nil))
1368 (t (setq target (list 'file+headline file position))))
1370 (when (string-match "%!" template)
1371 (setq template (replace-match "" t t template)
1372 immediate t))
1374 (append (list key desc type target template)
1375 (if prepend '(:prepend t))
1376 (if immediate '(:immediate-finish t)))))
1378 org-remember-templates))))
1380 (provide 'org-capture)
1382 ;; arch-tag: 986bf41b-8ada-4e28-bf20-e8388a7205a0
1384 ;;; org-capture.el ends here