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