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