* org.el (org-archive-location): Minor docstring fix.
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-capture.el
bloba7dc92bbfe783602ccb9da573ecab01f4da6eec4
1 ;;; org-capture.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2010-2011 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 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains an alternative implementation of the same functionality
28 ;; that is also provided by org-remember.el. The implementation is more
29 ;; streamlined, can produce more target types (e.g. plain list items or
30 ;; table lines). Also, it does not use a temporary buffer for editing
31 ;; the captured entry - instead it uses an indirect buffer that visits
32 ;; the new entry already in the target buffer (this was an idea by Samuel
33 ;; Wales). John Wiegley's excellent `remember.el' is not needed for this
34 ;; implementation, even though we borrow heavily from its ideas.
36 ;; This implementation heavily draws on ideas by James TD Smith and
37 ;; Samuel Wales, and, of cause, uses John Wiegley's remember.el as inspiration.
39 ;;; TODO
41 ;; - find a clever way to not always insert an annotation maybe a
42 ;; predicate function that can check for conditions for %a to be
43 ;; used. This could be one of the properties.
45 ;; - Should there be plist members that arrange for properties to be
46 ;; asked for, like James proposed in his RFC?
48 ;;; Code:
50 (eval-when-compile
51 (require 'cl))
52 (require 'org)
53 (require 'org-mks)
55 (declare-function org-datetree-find-date-create "org-datetree"
56 (date &optional keep-restriction))
57 (declare-function org-table-get-specials "org-table" ())
58 (declare-function org-table-goto-line "org-table" (N))
59 (declare-function org-pop-to-buffer-same-window "org-compat"
60 (&optional buffer-or-name norecord label))
62 (defvar org-remember-default-headline)
63 (defvar org-remember-templates)
64 (defvar org-table-hlines)
65 (defvar dired-buffers)
67 (defvar org-capture-clock-was-started nil
68 "Internal flag, noting if the clock was started.")
70 (defvar org-capture-last-stored-marker (make-marker)
71 "Marker pointing to the entry most recently stored with `org-capture'.")
73 ;; The following variable is scoped dynamically by org-protocol
74 ;; to indicate that the link properties have already been stored
75 (defvar org-capture-link-is-already-stored nil)
77 (defgroup org-capture nil
78 "Options concerning capturing new entries."
79 :tag "Org Capture"
80 :group 'org)
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
101 selection.
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
106 a top-level entry.
107 item a plain list item, will be placed in the
108 first plain list at the target
109 location.
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
124 form.
126 Valid values are:
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
152 (clock)
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
157 file and location
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
167 from a function.
169 The rest of the entry is a property list of additional options. Recognized
170 properties are:
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
175 change that.
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
180 automatically.
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
184 other value is 1.
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
206 is finalized.
208 The template defines the text to be inserted. Often this is an
209 org-mode entry (so the first line should start with a star) that
210 will be filed as a child of the target headline. It can also be
211 freely formatted text. Furthermore, the following %-escapes will
212 be replaced with content and expanded in this order:
214 %[pathname] insert the contents of the file given by `pathname'.
215 %(sexp) evaluate elisp `(sexp)' and replace with the result.
216 %<...> the result of format-time-string on the ... format specification.
217 %t time stamp, date only.
218 %T time stamp with date and time.
219 %u, %U like the above, but inactive time stamps.
220 %a annotation, normally the link created with `org-store-link'.
221 %i initial content, copied from the active region. If %i is
222 indented, the entire inserted text will be indented as well.
223 %A like %a, but prompt for the description part.
224 %c current kill ring head.
225 %x content of the X clipboard.
226 %k title of currently clocked task.
227 %K link to currently clocked task.
228 %n user name (taken from `user-full-name').
229 %f file visited by current buffer when org-capture was called.
230 %F full path of the file or directory visited by current buffer.
231 %:keyword specific information for certain link types, see below.
232 %^g prompt for tags, with completion on tags in target file.
233 %^G prompt for tags, with completion on all tags in all agenda files.
234 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
235 You may define a prompt like %^{Please specify birthday.
236 %^C interactive selection of which kill or clip to use.
237 %^L like %^C, but insert as link.
238 %^{prop}p prompt the user for a value for property `prop'.
239 %^{prompt} prompt the user for a string and replace this sequence with it.
240 A default value and a completion table ca be specified like this:
241 %^{prompt|default|completion2|completion3|...}.
242 %? After completing the template, position cursor here.
244 Apart from these general escapes, you can access information specific to the
245 link type that is created. For example, calling `org-capture' in emails
246 or gnus will record the author and the subject of the message, which you
247 can access with \"%:from\" and \"%:subject\", respectively. Here is a
248 complete list of what is recorded for each link type.
250 Link type | Available information
251 ------------------------+------------------------------------------------------
252 bbdb | %:type %:name %:company
253 vm, wl, mh, mew, rmail | %:type %:subject %:message-id
254 | %:from %:fromname %:fromaddress
255 | %:to %:toname %:toaddress
256 | %:fromto (either \"to NAME\" or \"from NAME\")
257 | %:date
258 | %:date-timestamp (as active timestamp)
259 | %:date-timestamp-inactive (as inactive timestamp)
260 gnus | %:group, for messages also all email fields
261 w3, w3m | %:type %:url
262 info | %:type %:file %:node
263 calendar | %:type %:date"
264 :group 'org-capture
265 :type
266 '(repeat
267 (choice :value ("" "" entry (file "~/org/notes.org") "")
268 (list :tag "Multikey description"
269 (string :tag "Keys ")
270 (string :tag "Description"))
271 (list :tag "Template entry"
272 (string :tag "Keys ")
273 (string :tag "Description ")
274 (choice :tag "Capture Type " :value entry
275 (const :tag "Org entry" entry)
276 (const :tag "Plain list item" item)
277 (const :tag "Checkbox item" checkitem)
278 (const :tag "Plain text" plain)
279 (const :tag "Table line" table-line))
280 (choice :tag "Target location"
281 (list :tag "File"
282 (const :format "" file)
283 (file :tag " File"))
284 (list :tag "ID"
285 (const :format "" id)
286 (string :tag " ID"))
287 (list :tag "File & Headline"
288 (const :format "" file+headline)
289 (file :tag " File ")
290 (string :tag " Headline"))
291 (list :tag "File & Outline path"
292 (const :format "" file+olp)
293 (file :tag " File ")
294 (repeat :tag "Outline path" :inline t
295 (string :tag "Headline")))
296 (list :tag "File & Regexp"
297 (const :format "" file+regexp)
298 (file :tag " File ")
299 (regexp :tag " Regexp"))
300 (list :tag "File & Date tree"
301 (const :format "" file+datetree)
302 (file :tag " File"))
303 (list :tag "File & Date tree, prompt for date"
304 (const :format "" file+datetree+prompt)
305 (file :tag " File"))
306 (list :tag "File & function"
307 (const :format "" file+function)
308 (file :tag " File ")
309 (sexp :tag " Function"))
310 (list :tag "Current clocking task"
311 (const :format "" clock))
312 (list :tag "Function"
313 (const :format "" function)
314 (sexp :tag " Function")))
315 (choice :tag "Template"
316 (string)
317 (list :tag "File"
318 (const :format "" file)
319 (file :tag "Template file"))
320 (list :tag "Function"
321 (const :format "" function)
322 (function :tag "Template function")))
323 (plist :inline t
324 ;; Give the most common options as checkboxes
325 :options (((const :format "%v " :prepend) (const t))
326 ((const :format "%v " :immediate-finish) (const t))
327 ((const :format "%v " :empty-lines) (const 1))
328 ((const :format "%v " :clock-in) (const t))
329 ((const :format "%v " :clock-keep) (const t))
330 ((const :format "%v " :clock-resume) (const t))
331 ((const :format "%v " :unnarrowed) (const t))
332 ((const :format "%v " :kill-buffer) (const t))))))))
334 (defcustom org-capture-before-finalize-hook nil
335 "Hook that is run right before a capture process is finalized.
336 The capture buffer is still current when this hook runs."
337 :group 'org-capture
338 :type 'hook)
340 (defcustom org-capture-after-finalize-hook nil
341 "Hook that is run right after a capture process is finalized.
342 Suitable for window cleanup"
343 :group 'org-capture
344 :type 'hook)
346 ;;; The property list for keeping information about the capture process
348 (defvar org-capture-plist nil
349 "Plist for the current capture process, global, to avoid having to pass it.")
351 (defvar org-capture-current-plist nil
352 "Local variable holding the plist in a capture buffer.
353 This is used to store the plist for use when finishing a capture process
354 because another such process might have changed the global variable by then.
356 Each time a new capture buffer has been set up, the global `org-capture-plist'
357 is copied to this variable, which is local in the indirect buffer.")
359 (defvar org-capture-clock-keep nil
360 "Local variable to store the value of the :clock-keep parameter.
361 This is needed in case org-capture-finalize is called interactively.")
363 (defun org-capture-put (&rest stuff)
364 "Add properties to the capture property list `org-capture-plist'."
365 (while stuff
366 (setq org-capture-plist (plist-put org-capture-plist
367 (pop stuff) (pop stuff)))))
368 (defun org-capture-get (prop &optional local)
369 "Get properties from the capture property list `org-capture-plist'.
370 When LOCAL is set, use the local variable `org-capture-current-plist',
371 this is necessary after initialization of the capture process,
372 to avoid conflicts with other active capture processes."
373 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
375 (defun org-capture-member (prop &optional local)
376 "Is PROP a preperty in `org-capture-plist'.
377 When LOCAL is set, use the local variable `org-capture-current-plist',
378 this is necessary after initialization of the capture process,
379 to avoid conflicts with other active capture processes."
380 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
382 ;;; The minor mode
384 (defvar org-capture-mode-map (make-sparse-keymap)
385 "Keymap for `org-capture-mode', a minor mode.
386 Use this map to set additional keybindings for when Org-mode is used
387 for a capture buffer.")
389 (defvar org-capture-mode-hook nil
390 "Hook for the minor `org-capture-mode'.")
392 (define-minor-mode org-capture-mode
393 "Minor mode for special key bindings in a capture buffer."
394 nil " Rem" org-capture-mode-map
395 (org-set-local
396 'header-line-format
397 "Capture buffer. Finish `C-c C-c', refile `C-c C-w', abort `C-c C-k'.")
398 (run-hooks 'org-capture-mode-hook))
399 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
400 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
401 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
403 ;;; The main commands
405 ;;;###autoload
406 (defun org-capture (&optional goto keys)
407 "Capture something.
408 \\<org-capture-mode-map>
409 This will let you select a template from `org-capture-templates', and then
410 file the newly captured information. The text is immediately inserted
411 at the target location, and an indirect buffer is shown where you can
412 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
413 of Emacs, so that you can continue your work.
415 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
416 anything, just go to the file/headline where the selected template
417 stores its notes. With a double prefix argument \
418 \\[universal-argument] \\[universal-argument], go to the last note
419 stored.
421 When called with a `C-0' (zero) prefix, insert a template at point.
423 Lisp programs can set KEYS to a string associated with a template in
424 `org-capture-templates'. In this case, interactive selection will be
425 bypassed."
426 (interactive "P")
427 (cond
428 ((equal goto '(4)) (org-capture-goto-target))
429 ((equal goto '(16)) (org-capture-goto-last-stored))
431 ;; FIXME: Are these needed?
432 (let* ((orig-buf (current-buffer))
433 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
434 org-capture-link-is-already-stored)
435 (plist-get org-store-link-plist :annotation)
436 (ignore-errors (org-store-link nil))))
437 (initial (and (org-region-active-p)
438 (buffer-substring (point) (mark))))
439 (entry (org-capture-select-template keys)))
440 (when (stringp initial)
441 (remove-text-properties 0 (length initial) '(read-only t) initial))
442 (when (stringp annotation)
443 (remove-text-properties 0 (length annotation)
444 '(read-only t) annotation))
445 (cond
446 ((equal entry "C")
447 (customize-variable 'org-capture-templates))
448 ((equal entry "q")
449 (error "Abort"))
451 (org-capture-set-plist entry)
452 (org-capture-get-template)
453 (org-capture-put :original-buffer orig-buf
454 :original-file (or (buffer-file-name orig-buf)
455 (and (featurep 'dired)
456 (car (rassq orig-buf
457 dired-buffers))))
458 :original-file-nondirectory
459 (and (buffer-file-name orig-buf)
460 (file-name-nondirectory
461 (buffer-file-name orig-buf)))
462 :annotation annotation
463 :initial initial)
464 (org-capture-put :default-time
465 (or org-overriding-default-time
466 (org-current-time)))
467 (org-capture-set-target-location)
468 (condition-case error
469 (org-capture-put :template (org-capture-fill-template))
470 ((error quit)
471 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
472 (error "Capture abort: %s" error)))
474 (setq org-capture-clock-keep (org-capture-get :clock-keep))
475 (if (equal goto 0)
476 ;;insert at point
477 (org-capture-insert-template-here)
478 (condition-case error
479 (org-capture-place-template)
480 ((error quit)
481 (if (and (buffer-base-buffer (current-buffer))
482 (string-match "\\`CAPTURE-" (buffer-name)))
483 (kill-buffer (current-buffer)))
484 (set-window-configuration (org-capture-get :return-to-wconf))
485 (error "Capture template `%s': %s"
486 (org-capture-get :key)
487 (nth 1 error))))
488 (if (and (org-mode-p)
489 (org-capture-get :clock-in))
490 (condition-case nil
491 (progn
492 (if (org-clock-is-active)
493 (org-capture-put :interrupted-clock
494 (copy-marker org-clock-marker)))
495 (org-clock-in)
496 (org-set-local 'org-capture-clock-was-started t))
497 (error
498 "Could not start the clock in this capture buffer")))
499 (if (org-capture-get :immediate-finish)
500 (org-capture-finalize nil)))))))))
502 (defun org-capture-get-template ()
503 "Get the template from a file or a function if necessary."
504 (let ((txt (org-capture-get :template)) file)
505 (cond
506 ((and (listp txt) (eq (car txt) 'file))
507 (if (file-exists-p
508 (setq file (expand-file-name (nth 1 txt) org-directory)))
509 (setq txt (org-file-contents file))
510 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
511 ((and (listp txt) (eq (car txt) 'function))
512 (if (fboundp (nth 1 txt))
513 (setq txt (funcall (nth 1 txt)))
514 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
515 ((not txt) (setq txt ""))
516 ((stringp txt))
517 (t (setq txt "* Invalid capture template")))
518 (org-capture-put :template txt)))
520 (defun org-capture-finalize (&optional stay-with-capture)
521 "Finalize the capture process.
522 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
523 captured item after finalizing."
524 (interactive "P")
525 (unless (and org-capture-mode
526 (buffer-base-buffer (current-buffer)))
527 (error "This does not seem to be a capture buffer for Org-mode"))
529 ;; Did we start the clock in this capture buffer?
530 (when (and org-capture-clock-was-started
531 org-clock-marker (marker-buffer org-clock-marker)
532 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
533 (> org-clock-marker (point-min))
534 (< org-clock-marker (point-max)))
535 ;; Looks like the clock we started is still running. Clock out.
536 (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
537 (when (and (not org-capture-clock-keep)
538 (org-capture-get :clock-resume 'local)
539 (markerp (org-capture-get :interrupted-clock 'local))
540 (buffer-live-p (marker-buffer
541 (org-capture-get :interrupted-clock 'local))))
542 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
543 (org-with-point-at clock-in-task
544 (org-clock-in)))
545 (message "Interrupted clock has been resumed")))
547 (let ((beg (point-min))
548 (end (point-max))
549 (abort-note nil))
550 ;; Store the size of the capture buffer
551 (org-capture-put :captured-entry-size (- (point-max) (point-min)))
552 (widen)
553 ;; Store the insertion point in the target buffer
554 (org-capture-put :insertion-point (point))
556 (if org-note-abort
557 (let ((m1 (org-capture-get :begin-marker 'local))
558 (m2 (org-capture-get :end-marker 'local)))
559 (if (and m1 m2 (= m1 beg) (= m2 end))
560 (progn
561 (setq m2 (if (cdr (assoc 'heading org-blank-before-new-entry))
562 m2 (1+ m2))
563 m2 (if (< (point-max) m2) (point-max) m2))
564 (setq abort-note 'clean)
565 (kill-region m1 m2))
566 (setq abort-note 'dirty)))
568 ;; Make sure that the empty lines after are correct
569 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
570 (member (org-capture-get :type 'local)
571 '(entry item checkitem plain)))
572 (save-excursion
573 (goto-char end)
574 (or (bolp) (newline))
575 (org-capture-empty-lines-after
576 (or (org-capture-get :empty-lines 'local) 0))))
577 ;; Postprocessing: Update Statistics cookies, do the sorting
578 (when (org-mode-p)
579 (save-excursion
580 (when (ignore-errors (org-back-to-heading))
581 (org-update-parent-todo-statistics)
582 (org-update-checkbox-count)))
583 ;; FIXME Here we should do the sorting
584 ;; If we have added a table line, maybe recompute?
585 (when (and (eq (org-capture-get :type 'local) 'table-line)
586 (org-at-table-p))
587 (if (org-table-get-stored-formulas)
588 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
589 (org-table-align))))
590 ;; Store this place as the last one where we stored something
591 ;; Do the marking in the base buffer, so that it makes sense after
592 ;; the indirect buffer has been killed.
593 (org-capture-bookmark-last-stored-position)
595 ;; Run the hook
596 (run-hooks 'org-capture-before-finalize-hook))
598 ;; Kill the indirect buffer
599 (save-buffer)
600 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
601 (new-buffer (org-capture-get :new-buffer 'local))
602 (kill-buffer (org-capture-get :kill-buffer 'local))
603 (base-buffer (buffer-base-buffer (current-buffer))))
605 ;; Kill the indirect buffer
606 (kill-buffer (current-buffer))
608 ;; Narrow back the target buffer to its previous state
609 (with-current-buffer (org-capture-get :buffer)
610 (let ((reg (org-capture-get :initial-target-region))
611 (pos (org-capture-get :initial-target-position))
612 (ipt (org-capture-get :insertion-point))
613 (size (org-capture-get :captured-entry-size)))
614 (when reg
615 (cond ((< ipt (car reg))
616 ;; insertion point is before the narrowed region
617 (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
618 ((> ipt (cdr reg))
619 ;; insertion point is after the narrowed region
620 (narrow-to-region (car reg) (cdr reg)))
622 ;; insertion point is within the narrowed region
623 (narrow-to-region (car reg) (+ size (cdr reg)))))
624 ;; now place back the point at its original position
625 (if (< ipt (car reg))
626 (goto-char (+ size pos))
627 (goto-char (if (< ipt pos) (+ size pos) pos))))))
629 ;; Kill the target buffer if that is desired
630 (when (and base-buffer new-buffer kill-buffer)
631 (with-current-buffer base-buffer (save-buffer))
632 (kill-buffer base-buffer))
634 ;; Restore the window configuration before capture
635 (set-window-configuration return-wconf))
637 (run-hooks 'org-capture-after-finalize-hook)
638 ;; Special cases
639 (cond
640 (abort-note
641 (cond
642 ((equal abort-note 'clean)
643 (message "Capture process aborted and target buffer cleaned up"))
644 ((equal abort-note 'dirty)
645 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
646 (stay-with-capture
647 (org-capture-goto-last-stored)))
648 ;; Return if we did store something
649 (not abort-note)))
651 (defun org-capture-refile ()
652 "Finalize the current capture and then refile the entry.
653 Refiling is done from the base buffer, because the indirect buffer is then
654 already gone. Any prefix argument will be passed to the refile command."
655 (interactive)
656 (unless (eq (org-capture-get :type 'local) 'entry)
657 (error
658 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
659 (let ((pos (point))
660 (base (buffer-base-buffer (current-buffer)))
661 (org-refile-for-capture t))
662 (org-capture-finalize)
663 (save-window-excursion
664 (with-current-buffer (or base (current-buffer))
665 (save-excursion
666 (save-restriction
667 (widen)
668 (goto-char pos)
669 (call-interactively 'org-refile)))))))
671 (defun org-capture-kill ()
672 "Abort the current capture process."
673 (interactive)
674 ;; FIXME: This does not do the right thing, we need to remove the new stuff
675 ;; By hand it is easy: undo, then kill the buffer
676 (let ((org-note-abort t)
677 (org-capture-before-finalize-hook nil))
678 (org-capture-finalize)))
680 (defun org-capture-goto-last-stored ()
681 "Go to the location where the last capture note was stored."
682 (interactive)
683 (org-goto-marker-or-bmk org-capture-last-stored-marker
684 "org-capture-last-stored")
685 (message "This is the last note stored by a capture process"))
687 ;;; Supporting functions for handling the process
689 (defun org-capture-put-target-region-and-position ()
690 "Store the initial region with `org-capture-put'."
691 (org-capture-put
692 :initial-target-region
693 ;; Check if the buffer is currently narrowed
694 (when (/= (buffer-size) (- (point-max) (point-min)))
695 (cons (point-min) (point-max))))
696 ;; store the current point
697 (org-capture-put :initial-target-position (point)))
699 (defun org-capture-set-target-location (&optional target)
700 "Find target buffer and position and store then in the property list."
701 (let ((target-entry-p t))
702 (setq target (or target (org-capture-get :target)))
703 (save-excursion
704 (cond
705 ((eq (car target) 'file)
706 (set-buffer (org-capture-target-buffer (nth 1 target)))
707 (org-capture-put-target-region-and-position)
708 (widen)
709 (setq target-entry-p nil))
711 ((eq (car target) 'id)
712 (let ((loc (org-id-find (nth 1 target))))
713 (if (not loc)
714 (error "Cannot find target ID \"%s\"" (nth 1 target))
715 (set-buffer (org-capture-target-buffer (car loc)))
716 (widen)
717 (org-capture-put-target-region-and-position)
718 (goto-char (cdr loc)))))
720 ((eq (car target) 'file+headline)
721 (set-buffer (org-capture-target-buffer (nth 1 target)))
722 (org-capture-put-target-region-and-position)
723 (widen)
724 (let ((hd (nth 2 target)))
725 (goto-char (point-min))
726 (unless (org-mode-p)
727 (error
728 "Target buffer \"%s\" for file+headline should be in Org mode"
729 (current-buffer)))
730 (if (re-search-forward
731 (format org-complex-heading-regexp-format (regexp-quote hd))
732 nil t)
733 (goto-char (point-at-bol))
734 (goto-char (point-max))
735 (or (bolp) (insert "\n"))
736 (insert "* " hd "\n")
737 (beginning-of-line 0))))
739 ((eq (car target) 'file+olp)
740 (let ((m (org-find-olp
741 (cons (org-capture-expand-file (nth 1 target))
742 (cddr target)))))
743 (set-buffer (marker-buffer m))
744 (org-capture-put-target-region-and-position)
745 (widen)
746 (goto-char m)))
748 ((eq (car target) 'file+regexp)
749 (set-buffer (org-capture-target-buffer (nth 1 target)))
750 (org-capture-put-target-region-and-position)
751 (widen)
752 (goto-char (point-min))
753 (if (re-search-forward (nth 2 target) nil t)
754 (progn
755 (goto-char (if (org-capture-get :prepend)
756 (match-beginning 0) (match-end 0)))
757 (org-capture-put :exact-position (point))
758 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
759 (error "No match for target regexp in file %s" (nth 1 target))))
761 ((memq (car target) '(file+datetree file+datetree+prompt))
762 (require 'org-datetree)
763 (set-buffer (org-capture-target-buffer (nth 1 target)))
764 (org-capture-put-target-region-and-position)
765 (widen)
766 ;; Make a date tree entry, with the current date (or yesterday,
767 ;; if we are extending dates for a couple of hours)
768 (org-datetree-find-date-create
769 (calendar-gregorian-from-absolute
770 (cond
771 (org-overriding-default-time
772 ;; use the overriding default time
773 (time-to-days org-overriding-default-time))
775 ((eq (car target) 'file+datetree+prompt)
776 ;; prompt for date
777 (let ((prompt-time (org-read-date
778 nil t nil "Date for tree entry:"
779 (current-time))))
780 (org-capture-put :prompt-time prompt-time)
781 (time-to-days prompt-time)))
783 ;; current date, possible corrected for late night workers
784 (org-today))))))
786 ((eq (car target) 'file+function)
787 (set-buffer (org-capture-target-buffer (nth 1 target)))
788 (org-capture-put-target-region-and-position)
789 (widen)
790 (funcall (nth 2 target))
791 (org-capture-put :exact-position (point))
792 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
794 ((eq (car target) 'function)
795 (funcall (nth 1 target))
796 (org-capture-put :exact-position (point))
797 (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
799 ((eq (car target) 'clock)
800 (if (and (markerp org-clock-hd-marker)
801 (marker-buffer org-clock-hd-marker))
802 (progn (set-buffer (marker-buffer org-clock-hd-marker))
803 (org-capture-put-target-region-and-position)
804 (widen)
805 (goto-char org-clock-hd-marker))
806 (error "No running clock that could be used as capture target")))
808 (t (error "Invalid capture target specification")))
810 (org-capture-put :buffer (current-buffer) :pos (point)
811 :target-entry-p target-entry-p))))
813 (defun org-capture-expand-file (file)
814 "Expand functions and symbols for FILE.
815 When FILE is a function, call it. When it is a form, evaluate
816 it. When it is a variable, retrieve the value. Return whatever we get."
817 (cond
818 ((org-string-nw-p file) file)
819 ((functionp file) (funcall file))
820 ((and (symbolp file) (boundp file)) (symbol-value file))
821 ((and file (consp file)) (eval file))
822 (t file)))
824 (defun org-capture-target-buffer (file)
825 "Get a buffer for FILE."
826 (setq file (org-capture-expand-file file))
827 (setq file (or (org-string-nw-p file)
828 org-default-notes-file
829 (error "No notes file specified, and no default available")))
830 (or (org-find-base-buffer-visiting file)
831 (progn (org-capture-put :new-buffer t)
832 (find-file-noselect (expand-file-name file org-directory)))))
834 (defun org-capture-steal-local-variables (buffer)
835 "Install Org-mode local variables."
836 (mapc (lambda (v)
837 (ignore-errors (org-set-local (car v) (cdr v))))
838 (buffer-local-variables buffer)))
840 (defun org-capture-place-template ()
841 "Insert the template at the target location, and display the buffer."
842 (org-capture-put :return-to-wconf (current-window-configuration))
843 (delete-other-windows)
844 (org-switch-to-buffer-other-window
845 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
846 (widen)
847 (show-all)
848 (goto-char (org-capture-get :pos))
849 (org-set-local 'org-capture-target-marker
850 (move-marker (make-marker) (point)))
851 (let* ((template (org-capture-get :template))
852 (type (org-capture-get :type)))
853 (case type
854 ((nil entry) (org-capture-place-entry))
855 (table-line (org-capture-place-table-line))
856 (plain (org-capture-place-plain-text))
857 (item (org-capture-place-item))
858 (checkitem (org-capture-place-item))))
859 (org-capture-mode 1)
860 (org-set-local 'org-capture-current-plist org-capture-plist))
862 (defun org-capture-place-entry ()
863 "Place the template as a new Org entry."
864 (let* ((txt (org-capture-get :template))
865 (reversed (org-capture-get :prepend))
866 (target-entry-p (org-capture-get :target-entry-p))
867 level beg end file)
869 (cond
870 ((org-capture-get :exact-position)
871 (goto-char (org-capture-get :exact-position)))
872 ((not target-entry-p)
873 ;; Insert as top-level entry, either at beginning or at end of file
874 (setq level 1)
875 (if reversed
876 (progn (goto-char (point-min))
877 (or (org-at-heading-p)
878 (outline-next-heading)))
879 (goto-char (point-max))
880 (or (bolp) (insert "\n"))))
882 ;; Insert as a child of the current entry
883 (and (looking-at "\\*+")
884 (setq level (- (match-end 0) (match-beginning 0))))
885 (setq level (org-get-valid-level (or level 1) 1))
886 (if reversed
887 (progn
888 (outline-next-heading)
889 (or (bolp) (insert "\n")))
890 (org-end-of-subtree t t)
891 (or (bolp) (insert "\n")))))
892 (org-capture-empty-lines-before)
893 (setq beg (point))
894 (org-capture-verify-tree txt)
895 (org-paste-subtree level txt 'for-yank)
896 (org-capture-empty-lines-after 1)
897 (org-capture-position-for-last-stored beg)
898 (outline-next-heading)
899 (setq end (point))
900 (org-capture-mark-kill-region beg (1- end))
901 (org-capture-narrow beg (1- end))
902 (goto-char beg)
903 (if (re-search-forward "%\\?" end t) (replace-match ""))))
905 (defun org-capture-place-item ()
906 "Place the template as a new plain list item."
907 (let* ((txt (org-capture-get :template))
908 (target-entry-p (org-capture-get :target-entry-p))
909 (ind 0)
910 beg end)
911 (cond
912 ((org-capture-get :exact-position)
913 (goto-char (org-capture-get :exact-position)))
914 ((not target-entry-p)
915 ;; Insert as top-level entry, either at beginning or at end of file
916 (setq beg (point-min) end (point-max)))
918 (setq beg (1+ (point-at-eol))
919 end (save-excursion (outline-next-heading) (point)))))
920 (if (org-capture-get :prepend)
921 (progn
922 (goto-char beg)
923 (if (org-list-search-forward (org-item-beginning-re) end t)
924 (progn
925 (goto-char (match-beginning 0))
926 (setq ind (org-get-indentation)))
927 (goto-char end)
928 (setq ind 0)))
929 (goto-char end)
930 (if (org-list-search-backward (org-item-beginning-re) beg t)
931 (progn
932 (setq ind (org-get-indentation))
933 (org-end-of-item))
934 (setq ind 0)))
935 ;; Remove common indentation
936 (setq txt (org-remove-indentation txt))
937 ;; Make sure this is indeed an item
938 (unless (string-match (concat "\\`" (org-item-re)) txt)
939 (setq txt (concat "- "
940 (mapconcat 'identity (split-string txt "\n")
941 "\n "))))
942 ;; Set the correct indentation, depending on context
943 (setq ind (make-string ind ?\ ))
944 (setq txt (concat ind
945 (mapconcat 'identity (split-string txt "\n")
946 (concat "\n" ind))
947 "\n"))
948 ;; Insert, with surrounding empty lines
949 (org-capture-empty-lines-before)
950 (setq beg (point))
951 (insert txt)
952 (or (bolp) (insert "\n"))
953 (org-capture-empty-lines-after 1)
954 (org-capture-position-for-last-stored beg)
955 (forward-char 1)
956 (setq end (point))
957 (org-capture-mark-kill-region beg (1- end))
958 (org-capture-narrow beg (1- end))
959 (if (re-search-forward "%\\?" end t) (replace-match ""))))
961 (defun org-capture-place-table-line ()
962 "Place the template as a table line."
963 (require 'org-table)
964 (let* ((txt (org-capture-get :template))
965 (target-entry-p (org-capture-get :target-entry-p))
966 (table-line-pos (org-capture-get :table-line-pos))
967 ind beg end)
968 (cond
969 ((org-capture-get :exact-position)
970 (goto-char (org-capture-get :exact-position)))
971 ((not target-entry-p)
972 ;; Table is not necessarily under a heading
973 (setq beg (point-min) end (point-max)))
975 ;; WE are at a heading, limit search to the body
976 (setq beg (1+ (point-at-eol))
977 end (save-excursion (outline-next-heading) (point)))))
978 (if (re-search-forward org-table-dataline-regexp end t)
979 (let ((b (org-table-begin)) (e (org-table-end)))
980 (goto-char e)
981 (if (looking-at "[ \t]*#\\+TBLFM:")
982 (forward-line 1))
983 (narrow-to-region b (point)))
984 (goto-char end)
985 (insert "\n| |\n|----|\n| |\n")
986 (narrow-to-region (1+ end) (point)))
987 ;; We are narrowed to the table, or to an empty line if there was no table
989 ;; Check if the template is good
990 (if (not (string-match org-table-dataline-regexp txt))
991 (setq txt "| %?Bad template |\n"))
992 (cond
993 ((and table-line-pos
994 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
995 ;; we have a complex line specification
996 (goto-char (point-min))
997 (let ((nh (- (match-end 1) (match-beginning 1)))
998 (delta (string-to-number (match-string 2 table-line-pos)))
1000 ;; The user wants a special position in the table
1001 (org-table-get-specials)
1002 (setq ll (ignore-errors (aref org-table-hlines nh)))
1003 (unless ll (error "Invalid table line specification \"%s\""
1004 table-line-pos))
1005 (setq ll (+ ll delta (if (< delta 0) 0 -1)))
1006 (org-goto-line ll)
1007 (org-table-insert-row 'below)
1008 (beginning-of-line 1)
1009 (delete-region (point) (1+ (point-at-eol)))
1010 (setq beg (point))
1011 (insert txt)
1012 (setq end (point))))
1013 ((org-capture-get :prepend)
1014 (goto-char (point-min))
1015 (re-search-forward org-table-hline-regexp nil t)
1016 (beginning-of-line 1)
1017 (re-search-forward org-table-dataline-regexp nil t)
1018 (beginning-of-line 1)
1019 (setq beg (point))
1020 (org-table-insert-row)
1021 (beginning-of-line 1)
1022 (delete-region (point) (1+ (point-at-eol)))
1023 (insert txt)
1024 (setq end (point)))
1026 (goto-char (point-max))
1027 (re-search-backward org-table-dataline-regexp nil t)
1028 (beginning-of-line 1)
1029 (org-table-insert-row 'below)
1030 (beginning-of-line 1)
1031 (delete-region (point) (1+ (point-at-eol)))
1032 (setq beg (point))
1033 (insert txt)
1034 (setq end (point))))
1035 (goto-char beg)
1036 (org-capture-position-for-last-stored 'table-line)
1037 (if (re-search-forward "%\\?" end t) (replace-match ""))
1038 (org-table-align)))
1040 (defun org-capture-place-plain-text ()
1041 "Place the template plainly.
1042 If the target locator points at an Org node, place the template into
1043 the text of the entry, before the first child. If not, place the
1044 template at the beginning or end of the file.
1045 Of course, if exact position has been required, just put it there."
1046 (let* ((txt (org-capture-get :template))
1047 beg end)
1048 (cond
1049 ((org-capture-get :exact-position)
1050 (goto-char (org-capture-get :exact-position)))
1051 ((and (org-capture-get :target-entry-p)
1052 (bolp)
1053 (looking-at org-outline-regexp))
1054 ;; we should place the text into this entry
1055 (if (org-capture-get :prepend)
1056 ;; Skip meta data and drawers
1057 (org-end-of-meta-data-and-drawers)
1058 ;; go to ent of the entry text, before the next headline
1059 (outline-next-heading)))
1061 ;; beginning or end of file
1062 (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
1063 (or (bolp) (newline))
1064 (org-capture-empty-lines-before)
1065 (setq beg (point))
1066 (insert txt)
1067 (org-capture-empty-lines-after 1)
1068 (org-capture-position-for-last-stored beg)
1069 (setq end (point))
1070 (org-capture-mark-kill-region beg (1- end))
1071 (org-capture-narrow beg (1- end))
1072 (if (re-search-forward "%\\?" end t) (replace-match ""))))
1074 (defun org-capture-mark-kill-region (beg end)
1075 "Mark the region that will have to be killed when aborting capture."
1076 (let ((m1 (move-marker (make-marker) beg))
1077 (m2 (move-marker (make-marker) end)))
1078 (org-capture-put :begin-marker m1)
1079 (org-capture-put :end-marker m2)))
1081 (defun org-capture-position-for-last-stored (where)
1082 "Memorize the position that should later become the position of last capture."
1083 (cond
1084 ((integerp where)
1085 (org-capture-put :position-for-last-stored
1086 (move-marker (make-marker) where
1087 (or (buffer-base-buffer (current-buffer))
1088 (current-buffer)))))
1089 ((eq where 'table-line)
1090 (org-capture-put :position-for-last-stored
1091 (list 'table-line
1092 (org-table-current-dline))))
1093 (t (error "This should not happen"))))
1095 (defun org-capture-bookmark-last-stored-position ()
1096 "Bookmark the last-captured position."
1097 (let* ((where (org-capture-get :position-for-last-stored 'local))
1098 (pos (cond
1099 ((markerp where)
1100 (prog1 (marker-position where)
1101 (move-marker where nil)))
1102 ((and (listp where) (eq (car where) 'table-line))
1103 (if (org-at-table-p)
1104 (save-excursion
1105 (org-table-goto-line (nth 1 where))
1106 (point-at-bol))
1107 (point))))))
1108 (with-current-buffer (buffer-base-buffer (current-buffer))
1109 (save-excursion
1110 (save-restriction
1111 (widen)
1112 (goto-char pos)
1113 (bookmark-set "org-capture-last-stored")
1114 (move-marker org-capture-last-stored-marker (point)))))))
1116 (defun org-capture-narrow (beg end)
1117 "Narrow, unless configuration says not to narrow."
1118 (unless (org-capture-get :unnarrowed)
1119 (narrow-to-region beg end)
1120 (goto-char beg)))
1122 (defun org-capture-empty-lines-before (&optional n)
1123 "Arrange for the correct number of empty lines before the insertion point.
1124 Point will be after the empty lines, so insertion can directly be done."
1125 (setq n (or n (org-capture-get :empty-lines) 0))
1126 (let ((pos (point)))
1127 (org-back-over-empty-lines)
1128 (delete-region (point) pos)
1129 (if (> n 0) (newline n))))
1131 (defun org-capture-empty-lines-after (&optional n)
1132 "Arrange for the correct number of empty lines after the inserted string.
1133 Point will remain at the first line after the inserted text."
1134 (setq n (or n (org-capture-get :empty-lines) 0))
1135 (org-back-over-empty-lines)
1136 (while (looking-at "[ \t]*\n") (replace-match ""))
1137 (let ((pos (point)))
1138 (if (> n 0) (newline n))
1139 (goto-char pos)))
1141 (defvar org-clock-marker) ; Defined in org.el
1142 ;;;###autoload
1143 (defun org-capture-insert-template-here ()
1144 (let* ((template (org-capture-get :template))
1145 (type (org-capture-get :type))
1146 beg end pp)
1147 (or (bolp) (newline))
1148 (setq beg (point))
1149 (cond
1150 ((and (eq type 'entry) (org-mode-p))
1151 (org-capture-verify-tree (org-capture-get :template))
1152 (org-paste-subtree nil template t))
1153 ((and (memq type '(item checkitem))
1154 (org-mode-p)
1155 (save-excursion (skip-chars-backward " \t\n")
1156 (setq pp (point))
1157 (org-in-item-p)))
1158 (goto-char pp)
1159 (org-insert-item)
1160 (skip-chars-backward " ")
1161 (skip-chars-backward "-+*0123456789).")
1162 (delete-region (point) (point-at-eol))
1163 (setq beg (point))
1164 (org-remove-indentation template)
1165 (insert template)
1166 (org-capture-empty-lines-after)
1167 (goto-char beg)
1168 (org-list-repair)
1169 (org-end-of-item)
1170 (setq end (point)))
1171 (t (insert template)))
1172 (setq end (point))
1173 (goto-char beg)
1174 (if (re-search-forward "%\\?" end t)
1175 (replace-match ""))))
1177 (defun org-capture-set-plist (entry)
1178 "Initialize the property list from the template definition."
1179 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1180 (org-capture-put :key (car entry) :description (nth 1 entry)
1181 :target (nth 3 entry))
1182 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1183 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1184 ;; The template may be empty or omitted for special types.
1185 ;; Here we insert the default templates for such cases.
1186 (cond
1187 ((eq type 'item) (setq txt "- %?"))
1188 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1189 ((eq type 'table-line) (setq txt "| %? |"))
1190 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1191 (org-capture-put :template txt :type type)))
1193 (defun org-capture-goto-target (&optional template-key)
1194 "Go to the target location of a capture template.
1195 The user is queried for the template."
1196 (interactive)
1197 (let* (org-select-template-temp-major-mode
1198 (entry (org-capture-select-template template-key)))
1199 (unless entry
1200 (error "No capture template selected"))
1201 (org-capture-set-plist entry)
1202 (org-capture-set-target-location)
1203 (org-pop-to-buffer-same-window (org-capture-get :buffer))
1204 (goto-char (org-capture-get :pos))))
1206 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1207 "Make an indirect buffer for a capture process.
1208 Use PREFIX as a prefix for the name of the indirect buffer."
1209 (setq buffer (or buffer (current-buffer)))
1210 (let ((n 1) (base (buffer-name buffer)) bname)
1211 (setq bname (concat prefix "-" base))
1212 (while (buffer-live-p (get-buffer bname))
1213 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1214 (condition-case nil
1215 (make-indirect-buffer buffer bname 'clone)
1216 (error (make-indirect-buffer buffer bname)))))
1219 (defun org-capture-verify-tree (tree)
1220 "Throw error if TREE is not a valid tree"
1221 (unless (org-kill-is-subtree-p tree)
1222 (error "Template is not a valid Org entry or tree")))
1224 ;;; The template code
1226 (defun org-capture-select-template (&optional keys)
1227 "Select a capture template.
1228 Lisp programs can force the template by setting KEYS to a string."
1229 (let ((org-capture-templates
1230 (or org-capture-templates
1231 '(("t" "Task" entry (file+headline "" "Tasks")
1232 "* TODO %?\n %u\n %a")))))
1233 (if keys
1234 (or (assoc keys org-capture-templates)
1235 (error "No capture template referred to by \"%s\" keys" keys))
1236 (org-mks org-capture-templates
1237 "Select a capture template\n========================="
1238 "Template key: "
1239 '(("C" "Customize org-capture-templates")
1240 ("q" "Abort"))))))
1242 (defun org-capture-fill-template (&optional template initial annotation)
1243 "Fill a template and return the filled template as a string.
1244 The template may still contain \"%?\" for cursor positioning."
1245 (setq template (or template (org-capture-get :template)))
1246 (when (stringp initial)
1247 (setq initial (org-no-properties initial))
1248 (remove-text-properties 0 (length initial) '(read-only t) initial))
1249 (let* ((buffer (org-capture-get :buffer))
1250 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1251 (ct (org-capture-get :default-time))
1252 (dct (decode-time ct))
1253 (ct1
1254 (if (< (nth 2 dct) org-extend-today-until)
1255 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
1256 ct))
1257 (plist-p (if org-store-link-plist t nil))
1258 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
1259 (v-x (or (org-get-x-clipboard 'PRIMARY)
1260 (org-get-x-clipboard 'CLIPBOARD)
1261 (org-get-x-clipboard 'SECONDARY)))
1262 (v-t (format-time-string (car org-time-stamp-formats) ct))
1263 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
1264 (v-u (concat "[" (substring v-t 1 -1) "]"))
1265 (v-U (concat "[" (substring v-T 1 -1) "]"))
1266 ;; `initial' and `annotation' might habe been passed.
1267 ;; But if the property list has them, we prefer those values
1268 (v-i (or (plist-get org-store-link-plist :initial)
1269 initial
1270 (org-capture-get :initial)
1271 ""))
1272 (v-a (or (plist-get org-store-link-plist :annotation)
1273 annotation
1274 (org-capture-get :annotation)
1275 ""))
1276 ;; Is the link empty? Then we do not want it...
1277 (v-a (if (equal v-a "[[]]") "" v-a))
1278 (clipboards (remove nil (list v-i
1279 (org-get-x-clipboard 'PRIMARY)
1280 (org-get-x-clipboard 'CLIPBOARD)
1281 (org-get-x-clipboard 'SECONDARY)
1282 v-c)))
1283 (v-A (if (and v-a
1284 (string-match
1285 "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
1286 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
1287 v-a))
1288 (v-n user-full-name)
1289 (v-k (if (marker-buffer org-clock-marker)
1290 (org-substring-no-properties org-clock-heading)))
1291 (v-K (if (marker-buffer org-clock-marker)
1292 (org-make-link-string
1293 (buffer-file-name (marker-buffer org-clock-marker))
1294 org-clock-heading)))
1295 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1296 (v-F (or (org-capture-get :original-file) ""))
1298 (org-startup-folded nil)
1299 (org-inhibit-startup t)
1300 org-time-was-given org-end-time-was-given x
1301 prompt completions char time pos default histvar)
1303 (setq org-store-link-plist
1304 (plist-put org-store-link-plist :annotation v-a)
1305 org-store-link-plist
1306 (plist-put org-store-link-plist :initial v-i))
1307 (setq initial v-i)
1309 (unless template (setq template "") (message "No template") (ding)
1310 (sit-for 1))
1311 (save-window-excursion
1312 (delete-other-windows)
1313 (org-pop-to-buffer-same-window (get-buffer-create "*Capture*"))
1314 (erase-buffer)
1315 (insert template)
1316 (goto-char (point-min))
1317 (org-capture-steal-local-variables buffer)
1318 (setq buffer-file-name nil)
1320 ;; %[] Insert contents of a file.
1321 (goto-char (point-min))
1322 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1323 (unless (org-capture-escaped-%)
1324 (let ((start (match-beginning 0))
1325 (end (match-end 0))
1326 (filename (expand-file-name (match-string 1))))
1327 (goto-char start)
1328 (delete-region start end)
1329 (condition-case error
1330 (insert-file-contents filename)
1331 (error (insert (format "%%![Couldn't insert %s: %s]"
1332 filename error)))))))
1333 ;; %() embedded elisp
1334 (goto-char (point-min))
1335 (while (re-search-forward "%\\((.+)\\)" nil t)
1336 (unless (org-capture-escaped-%)
1337 (goto-char (match-beginning 0))
1338 (let ((template-start (point)))
1339 (forward-char 1)
1340 (let ((result (org-eval (read (current-buffer)))))
1341 (delete-region template-start (point))
1342 (insert result)))))
1344 ;; The current time
1345 (goto-char (point-min))
1346 (while (re-search-forward "%<\\([^>\n]+\\)>" nil t)
1347 (replace-match (format-time-string (match-string 1)) t t))
1349 ;; Simple %-escapes
1350 (goto-char (point-min))
1351 (while (re-search-forward "%\\([tTuUaiAcxkKInfF]\\)" nil t)
1352 (unless (org-capture-escaped-%)
1353 (when (and initial (equal (match-string 0) "%i"))
1354 (save-match-data
1355 (let* ((lead (buffer-substring
1356 (point-at-bol) (match-beginning 0))))
1357 (setq v-i (mapconcat 'identity
1358 (org-split-string initial "\n")
1359 (concat "\n" lead))))))
1360 (replace-match
1361 (or (eval (intern (concat "v-" (match-string 1)))) "")
1362 t t)))
1364 ;; From the property list
1365 (when plist-p
1366 (goto-char (point-min))
1367 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
1368 (unless (org-capture-escaped-%)
1369 (and (setq x (or (plist-get org-store-link-plist
1370 (intern (match-string 1))) ""))
1371 (replace-match x t t)))))
1373 ;; Turn on org-mode in temp buffer, set local variables
1374 ;; This is to support completion in interactive prompts
1375 (let ((org-inhibit-startup t)) (org-mode))
1376 ;; Interactive template entries
1377 (goto-char (point-min))
1378 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
1379 nil t)
1380 (unless (org-capture-escaped-%)
1381 (setq char (if (match-end 3) (match-string-no-properties 3))
1382 prompt (if (match-end 2) (match-string-no-properties 2)))
1383 (goto-char (match-beginning 0))
1384 (replace-match "")
1385 (setq completions nil default nil)
1386 (when prompt
1387 (setq completions (org-split-string prompt "|")
1388 prompt (pop completions)
1389 default (car completions)
1390 histvar (intern (concat
1391 "org-capture-template-prompt-history::"
1392 (or prompt "")))
1393 completions (mapcar 'list completions)))
1394 (unless (boundp histvar) (set histvar nil))
1395 (cond
1396 ((member char '("G" "g"))
1397 (let* ((org-last-tags-completion-table
1398 (org-global-tags-completion-table
1399 (if (equal char "G")
1400 (org-agenda-files)
1401 (and file (list file)))))
1402 (org-add-colon-after-tag-completion t)
1403 (ins (org-icompleting-read
1404 (if prompt (concat prompt ": ") "Tags: ")
1405 'org-tags-completion-function nil nil nil
1406 'org-tags-history)))
1407 (setq ins (mapconcat 'identity
1408 (org-split-string
1409 ins (org-re "[^[:alnum:]_@#%]+"))
1410 ":"))
1411 (when (string-match "\\S-" ins)
1412 (or (equal (char-before) ?:) (insert ":"))
1413 (insert ins)
1414 (or (equal (char-after) ?:) (insert ":"))
1415 (and (org-on-heading-p) (org-set-tags nil 'align)))))
1416 ((equal char "C")
1417 (cond ((= (length clipboards) 1) (insert (car clipboards)))
1418 ((> (length clipboards) 1)
1419 (insert (read-string "Clipboard/kill value: "
1420 (car clipboards) '(clipboards . 1)
1421 (car clipboards))))))
1422 ((equal char "L")
1423 (cond ((= (length clipboards) 1)
1424 (org-insert-link 0 (car clipboards)))
1425 ((> (length clipboards) 1)
1426 (org-insert-link 0 (read-string "Clipboard/kill value: "
1427 (car clipboards)
1428 '(clipboards . 1)
1429 (car clipboards))))))
1430 ((equal char "p")
1431 (org-set-property (org-substring-no-properties prompt) nil))
1432 (char
1433 ;; These are the date/time related ones
1434 (setq org-time-was-given (equal (upcase char) char))
1435 (setq time (org-read-date (equal (upcase char) char) t nil
1436 prompt))
1437 (if (equal (upcase char) char) (setq org-time-was-given t))
1438 (org-insert-time-stamp time org-time-was-given
1439 (member char '("u" "U"))
1440 nil nil (list org-end-time-was-given)))
1442 (let (org-completion-use-ido)
1443 (insert (org-completing-read-no-i
1444 (concat (if prompt prompt "Enter string")
1445 (if default (concat " [" default "]"))
1446 ": ")
1447 completions nil nil nil histvar default)))))))
1448 ;; Make sure there are no empty lines before the text, and that
1449 ;; it ends with a newline character
1450 (goto-char (point-min))
1451 (while (looking-at "[ \t]*\n") (replace-match ""))
1452 (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
1453 ;; Return the expanded tempate and kill the temporary buffer
1454 (untabify (point-min) (point-max))
1455 (set-buffer-modified-p nil)
1456 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1458 (defun org-capture-escaped-% ()
1459 "Check if % was escaped - if yes, unescape it now."
1460 (if (equal (char-before (match-beginning 0)) ?\\)
1461 (progn
1462 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1464 nil))
1466 ;;;###autoload
1467 (defun org-capture-import-remember-templates ()
1468 "Set org-capture-templates to be similar to `org-remember-templates'."
1469 (interactive)
1470 (when (and (yes-or-no-p
1471 "Import old remember templates into org-capture-templates? ")
1472 (yes-or-no-p
1473 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1474 (require 'org-remember)
1475 (setq org-capture-templates
1476 (mapcar
1477 (lambda (entry)
1478 (let ((desc (car entry))
1479 (key (char-to-string (nth 1 entry)))
1480 (template (nth 2 entry))
1481 (file (or (nth 3 entry) org-default-notes-file))
1482 (position (or (nth 4 entry) org-remember-default-headline))
1483 (type 'entry)
1484 (prepend org-reverse-note-order)
1485 immediate target)
1486 (cond
1487 ((member position '(top bottom))
1488 (setq target (list 'file file)
1489 prepend (eq position 'top)))
1490 ((eq position 'date-tree)
1491 (setq target (list 'file+datetree file)
1492 prepend nil))
1493 (t (setq target (list 'file+headline file position))))
1495 (when (string-match "%!" template)
1496 (setq template (replace-match "" t t template)
1497 immediate t))
1499 (append (list key desc type target template)
1500 (if prepend '(:prepend t))
1501 (if immediate '(:immediate-finish t)))))
1503 org-remember-templates))))
1505 (provide 'org-capture)
1507 ;;; org-capture.el ends here