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