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