ox-latex: Use lexical binding
[org-mode.git] / lisp / org-capture.el
blob58b578bc48b6351a7e2f7da9373fbaff8635d36a
1 ;;; org-capture.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains an alternative implementation of the functionality
28 ;; that used to be provided by org-remember.el. The implementation is more
29 ;; streamlined, can produce more target types (e.g. plain list items or
30 ;; table lines). Also, it does not use a temporary buffer for editing
31 ;; the captured entry - instead it uses an indirect buffer that visits
32 ;; the new entry already in the target buffer (this was an idea by Samuel
33 ;; Wales). John Wiegley's excellent `remember.el' is not needed anymore
34 ;; for this implementation, even though we borrow heavily from its ideas.
36 ;; This implementation heavily draws on ideas by James TD Smith and
37 ;; Samuel Wales, and, of cause, uses John Wiegley's remember.el as inspiration.
39 ;;; TODO
41 ;; - find a clever way to not always insert an annotation maybe a
42 ;; predicate function that can check for conditions for %a to be
43 ;; used. This could be one of the properties.
45 ;; - Should there be plist members that arrange for properties to be
46 ;; asked for, like James proposed in his RFC?
48 ;;; Code:
50 (eval-when-compile
51 (require 'cl))
52 (require 'org)
54 (declare-function org-datetree-find-date-create "org-datetree"
55 (date &optional keep-restriction))
56 (declare-function org-table-analyze "org-table" ())
57 (declare-function org-table-goto-line "org-table" (N))
58 (declare-function org-pop-to-buffer-same-window "org-compat"
59 (&optional buffer-or-name norecord label))
60 (declare-function org-at-encrypted-entry-p "org-crypt" ())
61 (declare-function org-encrypt-entry "org-crypt" ())
62 (declare-function org-decrypt-entry "org-crypt" ())
64 (defvar org-remember-default-headline)
65 (defvar org-remember-templates)
66 (defvar org-table-hlines)
67 (defvar org-table-current-begin-pos)
68 (defvar dired-buffers)
70 (defvar org-capture-clock-was-started nil
71 "Internal flag, noting if the clock was started.")
73 (defvar org-capture-last-stored-marker (make-marker)
74 "Marker pointing to the entry most recently stored with `org-capture'.")
76 ;; The following variable is scoped dynamically by org-protocol
77 ;; to indicate that the link properties have already been stored
78 (defvar org-capture-link-is-already-stored nil)
80 (defgroup org-capture nil
81 "Options concerning capturing new entries."
82 :tag "Org Capture"
83 :group 'org)
85 (defcustom org-capture-templates nil
86 "Templates for the creation of new entries.
88 Each entry is a list with the following items:
90 keys The keys that will select the template, as a string, characters
91 only, for example \"a\" for a template to be selected with a
92 single key, or \"bt\" for selection with two keys. When using
93 several keys, keys using the same prefix key must be together
94 in the list and preceded by a 2-element entry explaining the
95 prefix key, for example
97 (\"b\" \"Templates for marking stuff to buy\")
99 The \"C\" key is used by default for quick access to the
100 customization of the template variable. But if you want to use
101 that key for a template, you can.
103 description A short string describing the template, will be shown during
104 selection.
106 type The type of entry. Valid types are:
107 entry an Org-mode node, with a headline. Will be
108 filed as the child of the target entry or as
109 a top-level entry.
110 item a plain list item, will be placed in the
111 first plain list at the target
112 location.
113 checkitem a checkbox item. This differs from the
114 plain list item only is so far as it uses a
115 different default template.
116 table-line a new line in the first table at target location.
117 plain text to be inserted as it is.
119 target Specification of where the captured item should be placed.
120 In Org-mode files, targets usually define a node. Entries will
121 become children of this node, other types will be added to the
122 table or list in the body of this node.
124 Most target specifications contain a file name. If that file
125 name is the empty string, it defaults to `org-default-notes-file'.
126 A file can also be given as a variable, function, or Emacs Lisp
127 form.
129 Valid values are:
131 (file \"path/to/file\")
132 Text will be placed at the beginning or end of that file
134 (id \"id of existing org entry\")
135 File as child of this entry, or in the body of the entry
137 (file+headline \"path/to/file\" \"node headline\")
138 Fast configuration if the target heading is unique in the file
140 (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
141 For non-unique headings, the full path is safer
143 (file+regexp \"path/to/file\" \"regexp to find location\")
144 File to the entry matching regexp
146 (file+datetree \"path/to/file\")
147 Will create a heading in a date tree for today's date
149 (file+datetree+prompt \"path/to/file\")
150 Will create a heading in a date tree, prompts for date
152 (file+function \"path/to/file\" function-finding-location)
153 A function to find the right location in the file
155 (clock)
156 File to the entry that is currently being clocked
158 (function function-finding-location)
159 Most general way, write your own function to find both
160 file and location
162 template The template for creating the capture item. If you leave this
163 empty, an appropriate default template will be used. See below
164 for more details. Instead of a string, this may also be one of
166 (file \"/path/to/template-file\")
167 (function function-returning-the-template)
169 in order to get a template from a file, or dynamically
170 from a function.
172 The rest of the entry is a property list of additional options. Recognized
173 properties are:
175 :prepend Normally newly captured information will be appended at
176 the target location (last child, last table line,
177 last list item...). Setting this property will
178 change that.
180 :immediate-finish When set, do not offer to edit the information, just
181 file it away immediately. This makes sense if the
182 template only needs information that can be added
183 automatically.
185 :jump-to-captured When set, jump to the captured entry when finished.
187 :empty-lines Set this to the number of lines the should be inserted
188 before and after the new item. Default 0, only common
189 other value is 1.
191 :empty-lines-before Set this to the number of lines the should be inserted
192 before the new item. Overrides :empty-lines for the
193 number lines inserted before.
195 :empty-lines-after Set this to the number of lines the should be inserted
196 after the new item. Overrides :empty-lines for the
197 number of lines inserted after.
199 :clock-in Start the clock in this item.
201 :clock-keep Keep the clock running when filing the captured entry.
203 :clock-resume Start the interrupted clock when finishing the capture.
204 Note that :clock-keep has precedence over :clock-resume.
205 When setting both to t, the current clock will run and
206 the previous one will not be resumed.
208 :unnarrowed Do not narrow the target buffer, simply show the
209 full buffer. Default is to narrow it so that you
210 only see the new stuff.
212 :table-line-pos Specification of the location in the table where the
213 new line should be inserted. It should be a string like
214 \"II-3\", meaning that the new line should become the
215 third line before the second horizontal separator line.
217 :kill-buffer If the target file was not yet visited by a buffer when
218 capture was invoked, kill the buffer again after capture
219 is finalized.
221 The template defines the text to be inserted. Often this is an
222 org-mode entry (so the first line should start with a star) that
223 will be filed as a child of the target headline. It can also be
224 freely formatted text. Furthermore, the following %-escapes will
225 be replaced with content and expanded in this order:
227 %[pathname] Insert the contents of the file given by `pathname'.
228 %(sexp) Evaluate elisp `(sexp)' and replace it with the results.
229 For convenience, %:keyword (see below) placeholders within
230 the expression will be expanded prior to this.
231 %<...> The result of format-time-string on the ... format specification.
232 %t Time stamp, date only.
233 %T Time stamp with date and time.
234 %u, %U Like the above, but inactive time stamps.
235 %i Initial content, copied from the active region. If %i is
236 indented, the entire inserted text will be indented as well.
237 %a Annotation, normally the link created with `org-store-link'.
238 %A Like %a, but prompt for the description part.
239 %l Like %a, but only insert the literal link.
240 %c Current kill ring head.
241 %x Content of the X clipboard.
242 %k Title of currently clocked task.
243 %K Link to currently clocked task.
244 %n User name (taken from the variable `user-full-name').
245 %f File visited by current buffer when org-capture was called.
246 %F Full path of the file or directory visited by current buffer.
247 %:keyword Specific information for certain link types, see below.
248 %^g Prompt for tags, with completion on tags in target file.
249 %^G Prompt for tags, with completion on all tags in all agenda files.
250 %^t Like %t, but prompt for date. Similarly %^T, %^u, %^U.
251 You may define a prompt like: %^{Please specify birthday}t
252 %^C Interactive selection of which kill or clip to use.
253 %^L Like %^C, but insert as link.
254 %^{prop}p Prompt the user for a value for property `prop'.
255 %^{prompt} Prompt the user for a string and replace this sequence with it.
256 A default value and a completion table ca be specified like this:
257 %^{prompt|default|completion2|completion3|...}.
258 %? After completing the template, position cursor here.
259 %\\n Insert the text entered at the nth %^{prompt}, where `n' is
260 a number, starting from 1.
262 Apart from these general escapes, you can access information specific to
263 the link type that is created. For example, calling `org-capture' in emails
264 or in Gnus will record the author and the subject of the message, which you
265 can access with \"%:from\" and \"%:subject\", respectively. Here is a
266 complete list of what is recorded for each link type.
268 Link type | Available information
269 ------------------------+------------------------------------------------------
270 bbdb | %:type %:name %:company
271 vm, wl, mh, mew, rmail, | %:type %:subject %:message-id
272 gnus | %:from %:fromname %:fromaddress
273 | %:to %:toname %:toaddress
274 | %:fromto (either \"to NAME\" or \"from NAME\")
275 | %:date %:date-timestamp (as active timestamp)
276 | %:date-timestamp-inactive (as inactive timestamp)
277 gnus | %:group, for messages also all email fields
278 w3, w3m | %:type %:url
279 info | %:type %:file %:node
280 calendar | %:type %:date"
281 :group 'org-capture
282 :version "24.1"
283 :type
284 '(repeat
285 (choice :value ("" "" entry (file "~/org/notes.org") "")
286 (list :tag "Multikey description"
287 (string :tag "Keys ")
288 (string :tag "Description"))
289 (list :tag "Template entry"
290 (string :tag "Keys ")
291 (string :tag "Description ")
292 (choice :tag "Capture Type " :value entry
293 (const :tag "Org entry" entry)
294 (const :tag "Plain list item" item)
295 (const :tag "Checkbox item" checkitem)
296 (const :tag "Plain text" plain)
297 (const :tag "Table line" table-line))
298 (choice :tag "Target location"
299 (list :tag "File"
300 (const :format "" file)
301 (file :tag " File"))
302 (list :tag "ID"
303 (const :format "" id)
304 (string :tag " ID"))
305 (list :tag "File & Headline"
306 (const :format "" file+headline)
307 (file :tag " File ")
308 (string :tag " Headline"))
309 (list :tag "File & Outline path"
310 (const :format "" file+olp)
311 (file :tag " File ")
312 (repeat :tag "Outline path" :inline t
313 (string :tag "Headline")))
314 (list :tag "File & Regexp"
315 (const :format "" file+regexp)
316 (file :tag " File ")
317 (regexp :tag " Regexp"))
318 (list :tag "File & Date tree"
319 (const :format "" file+datetree)
320 (file :tag " File"))
321 (list :tag "File & Date tree, prompt for date"
322 (const :format "" file+datetree+prompt)
323 (file :tag " File"))
324 (list :tag "File & function"
325 (const :format "" file+function)
326 (file :tag " File ")
327 (sexp :tag " Function"))
328 (list :tag "Current clocking task"
329 (const :format "" clock))
330 (list :tag "Function"
331 (const :format "" function)
332 (sexp :tag " Function")))
333 (choice :tag "Template"
334 (string)
335 (list :tag "File"
336 (const :format "" file)
337 (file :tag "Template file"))
338 (list :tag "Function"
339 (const :format "" function)
340 (function :tag "Template function")))
341 (plist :inline t
342 ;; Give the most common options as checkboxes
343 :options (((const :format "%v " :prepend) (const t))
344 ((const :format "%v " :immediate-finish) (const t))
345 ((const :format "%v " :jump-to-captured) (const t))
346 ((const :format "%v " :empty-lines) (const 1))
347 ((const :format "%v " :empty-lines-before) (const 1))
348 ((const :format "%v " :empty-lines-after) (const 1))
349 ((const :format "%v " :clock-in) (const t))
350 ((const :format "%v " :clock-keep) (const t))
351 ((const :format "%v " :clock-resume) (const t))
352 ((const :format "%v " :unnarrowed) (const t))
353 ((const :format "%v " :table-line-pos) (const t))
354 ((const :format "%v " :kill-buffer) (const t))))))))
356 (defcustom org-capture-before-finalize-hook nil
357 "Hook that is run right before a capture process is finalized.
358 The capture buffer is still current when this hook runs and it is
359 widened to the entire buffer."
360 :group 'org-capture
361 :version "24.1"
362 :type 'hook)
364 (defcustom org-capture-after-finalize-hook nil
365 "Hook that is run right after a capture process is finalized.
366 Suitable for window cleanup."
367 :group 'org-capture
368 :version "24.1"
369 :type 'hook)
371 (defcustom org-capture-prepare-finalize-hook nil
372 "Hook that is run before the finalization starts.
373 The capture buffer is current and still narrowed."
374 :group 'org-capture
375 :version "24.1"
376 :type 'hook)
378 (defcustom org-capture-bookmark t
379 "When non-nil, add a bookmark pointing at the last stored
380 position when capturing."
381 :group 'org-capture
382 :version "24.3"
383 :type 'boolean)
385 ;;; The property list for keeping information about the capture process
387 (defvar org-capture-plist nil
388 "Plist for the current capture process, global, to avoid having to pass it.")
390 (defvar org-capture-current-plist nil
391 "Local variable holding the plist in a capture buffer.
392 This is used to store the plist for use when finishing a capture process
393 because another such process might have changed the global variable by then.
395 Each time a new capture buffer has been set up, the global `org-capture-plist'
396 is copied to this variable, which is local in the indirect buffer.")
398 (defvar org-capture-clock-keep nil
399 "Local variable to store the value of the :clock-keep parameter.
400 This is needed in case org-capture-finalize is called interactively.")
402 (defun org-capture-put (&rest stuff)
403 "Add properties to the capture property list `org-capture-plist'."
404 (while stuff
405 (setq org-capture-plist (plist-put org-capture-plist
406 (pop stuff) (pop stuff)))))
407 (defun org-capture-get (prop &optional local)
408 "Get properties from the capture property list `org-capture-plist'.
409 When LOCAL is set, use the local variable `org-capture-current-plist',
410 this is necessary after initialization of the capture process,
411 to avoid conflicts with other active capture processes."
412 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
414 (defun org-capture-member (prop &optional local)
415 "Is PROP a property in `org-capture-plist'.
416 When LOCAL is set, use the local variable `org-capture-current-plist',
417 this is necessary after initialization of the capture process,
418 to avoid conflicts with other active capture processes."
419 (plist-get (if local org-capture-current-plist org-capture-plist) prop))
421 ;;; The minor mode
423 (defvar org-capture-mode-map (make-sparse-keymap)
424 "Keymap for `org-capture-mode', a minor mode.
425 Use this map to set additional keybindings for when Org-mode is used
426 for a capture buffer.")
428 (defvar org-capture-mode-hook nil
429 "Hook for the minor `org-capture-mode'.")
431 (define-minor-mode org-capture-mode
432 "Minor mode for special key bindings in a capture buffer.
434 Turning on this mode runs the normal hook `org-capture-mode-hook'."
435 nil " Rem" org-capture-mode-map
436 (org-set-local
437 'header-line-format
438 (substitute-command-keys
439 "\\<org-capture-mode-map>Capture buffer. Finish \\[org-capture-finalize], \
440 refile \\[org-capture-refile], abort \\[org-capture-kill].")))
441 (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
442 (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
443 (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
445 ;;; The main commands
447 (defvar org-capture-initial nil)
448 (defvar org-capture-entry nil)
450 ;;;###autoload
451 (defun org-capture-string (string &optional keys)
452 "Capture STRING with the template selected by KEYS."
453 (interactive "sInitial text: \n")
454 (let ((org-capture-initial string)
455 (org-capture-entry (org-capture-select-template keys)))
456 (org-capture)))
458 (defcustom org-capture-templates-contexts nil
459 "Alist of capture templates and valid contexts.
461 For example, if you have a capture template \"c\" and you want
462 this template to be accessible only from `message-mode' buffers,
463 use this:
465 \\='((\"c\" ((in-mode . \"message-mode\"))))
467 Here are the available contexts definitions:
469 in-file: command displayed only in matching files
470 in-mode: command displayed only in matching modes
471 not-in-file: command not displayed in matching files
472 not-in-mode: command not displayed in matching modes
473 in-buffer: command displayed only in matching buffers
474 not-in-buffer: command not displayed in matching buffers
475 [function]: a custom function taking no argument
477 If you define several checks, the agenda command will be
478 accessible if there is at least one valid check.
480 You can also bind a key to another agenda custom command
481 depending on contextual rules.
483 \\='((\"c\" \"d\" ((in-mode . \"message-mode\"))))
485 Here it means: in `message-mode buffers', use \"c\" as the
486 key for the capture template otherwise associated with \"d\".
487 \(The template originally associated with \"d\" is not displayed
488 to avoid duplicates.)"
489 :version "24.3"
490 :group 'org-capture
491 :type '(repeat (list :tag "Rule"
492 (string :tag " Capture key")
493 (string :tag "Replace by template")
494 (repeat :tag "Available when"
495 (choice
496 (cons :tag "Condition"
497 (choice
498 (const :tag "In file" in-file)
499 (const :tag "Not in file" not-in-file)
500 (const :tag "In buffer" in-buffer)
501 (const :tag "Not in buffer" not-in-buffer)
502 (const :tag "In mode" in-mode)
503 (const :tag "Not in mode" not-in-mode))
504 (regexp))
505 (function :tag "Custom function"))))))
507 (defcustom org-capture-use-agenda-date nil
508 "Non-nil means use the date at point when capturing from agendas.
509 When nil, you can still capture using the date at point with \\[org-agenda-capture]."
510 :group 'org-capture
511 :version "24.3"
512 :type 'boolean)
514 ;;;###autoload
515 (defun org-capture (&optional goto keys)
516 "Capture something.
517 \\<org-capture-mode-map>
518 This will let you select a template from `org-capture-templates', and then
519 file the newly captured information. The text is immediately inserted
520 at the target location, and an indirect buffer is shown where you can
521 edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
522 of Emacs, so that you can continue your work.
524 When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
525 anything, just go to the file/headline where the selected template
526 stores its notes. With a double prefix argument \
527 \\[universal-argument] \\[universal-argument], go to the last note
528 stored.
530 When called with a `C-0' (zero) prefix, insert a template at point.
532 ELisp programs can set KEYS to a string associated with a template
533 in `org-capture-templates'. In this case, interactive selection
534 will be bypassed.
536 If `org-capture-use-agenda-date' is non-nil, capturing from the
537 agenda will use the date at point as the default date. Then, a
538 `C-1' prefix will tell the capture process to use the HH:MM time
539 of the day at point (if any) or the current HH:MM time."
540 (interactive "P")
541 (when (and org-capture-use-agenda-date
542 (eq major-mode 'org-agenda-mode))
543 (setq org-overriding-default-time
544 (org-get-cursor-date (equal goto 1))))
545 (cond
546 ((equal goto '(4)) (org-capture-goto-target))
547 ((equal goto '(16)) (org-capture-goto-last-stored))
549 ;; FIXME: Are these needed?
550 (let* ((orig-buf (current-buffer))
551 (annotation (if (and (boundp 'org-capture-link-is-already-stored)
552 org-capture-link-is-already-stored)
553 (plist-get org-store-link-plist :annotation)
554 (ignore-errors (org-store-link nil))))
555 (entry (or org-capture-entry (org-capture-select-template keys)))
556 initial)
557 (setq initial (or org-capture-initial
558 (and (org-region-active-p)
559 (buffer-substring (point) (mark)))))
560 (when (stringp initial)
561 (remove-text-properties 0 (length initial) '(read-only t) initial))
562 (when (stringp annotation)
563 (remove-text-properties 0 (length annotation)
564 '(read-only t) annotation))
565 (cond
566 ((equal entry "C")
567 (customize-variable 'org-capture-templates))
568 ((equal entry "q")
569 (error "Abort"))
571 (org-capture-set-plist entry)
572 (org-capture-get-template)
573 (org-capture-put :original-buffer orig-buf
574 :original-file (or (buffer-file-name orig-buf)
575 (and (featurep 'dired)
576 (car (rassq orig-buf
577 dired-buffers))))
578 :original-file-nondirectory
579 (and (buffer-file-name orig-buf)
580 (file-name-nondirectory
581 (buffer-file-name orig-buf)))
582 :annotation annotation
583 :initial initial
584 :return-to-wconf (current-window-configuration)
585 :default-time
586 (or org-overriding-default-time
587 (org-current-time)))
588 (org-capture-set-target-location)
589 (condition-case error
590 (org-capture-put :template (org-capture-fill-template))
591 ((error quit)
592 (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
593 (error "Capture abort: %s" error)))
595 (setq org-capture-clock-keep (org-capture-get :clock-keep))
596 (if (equal goto 0)
597 ;;insert at point
598 (org-capture-insert-template-here)
599 (condition-case error
600 (org-capture-place-template
601 (equal (car (org-capture-get :target)) 'function))
602 ((error quit)
603 (if (and (buffer-base-buffer (current-buffer))
604 (string-match "\\`CAPTURE-" (buffer-name)))
605 (kill-buffer (current-buffer)))
606 (set-window-configuration (org-capture-get :return-to-wconf))
607 (error "Capture template `%s': %s"
608 (org-capture-get :key)
609 (nth 1 error))))
610 (if (and (derived-mode-p 'org-mode)
611 (org-capture-get :clock-in))
612 (condition-case nil
613 (progn
614 (if (org-clock-is-active)
615 (org-capture-put :interrupted-clock
616 (copy-marker org-clock-marker)))
617 (org-clock-in)
618 (org-set-local 'org-capture-clock-was-started t))
619 (error
620 "Could not start the clock in this capture buffer")))
621 (if (org-capture-get :immediate-finish)
622 (org-capture-finalize)))))))))
624 (defun org-capture-get-template ()
625 "Get the template from a file or a function if necessary."
626 (let ((txt (org-capture-get :template)) file)
627 (cond
628 ((and (listp txt) (eq (car txt) 'file))
629 (if (file-exists-p
630 (setq file (expand-file-name (nth 1 txt) org-directory)))
631 (setq txt (org-file-contents file))
632 (setq txt (format "* Template file %s not found" (nth 1 txt)))))
633 ((and (listp txt) (eq (car txt) 'function))
634 (if (fboundp (nth 1 txt))
635 (setq txt (funcall (nth 1 txt)))
636 (setq txt (format "* Template function %s not found" (nth 1 txt)))))
637 ((not txt) (setq txt ""))
638 ((stringp txt))
639 (t (setq txt "* Invalid capture template")))
640 (org-capture-put :template txt)))
642 (defun org-capture-finalize (&optional stay-with-capture)
643 "Finalize the capture process.
644 With prefix argument STAY-WITH-CAPTURE, jump to the location of the
645 captured item after finalizing."
646 (interactive "P")
647 (when (org-capture-get :jump-to-captured)
648 (setq stay-with-capture t))
649 (unless (and org-capture-mode
650 (buffer-base-buffer (current-buffer)))
651 (error "This does not seem to be a capture buffer for Org-mode"))
653 (run-hooks 'org-capture-prepare-finalize-hook)
655 ;; Did we start the clock in this capture buffer?
656 (when (and org-capture-clock-was-started
657 org-clock-marker (marker-buffer org-clock-marker)
658 (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
659 (> org-clock-marker (point-min))
660 (< org-clock-marker (point-max)))
661 ;; Looks like the clock we started is still running. Clock out.
662 (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
663 (when (and (not org-capture-clock-keep)
664 (org-capture-get :clock-resume 'local)
665 (markerp (org-capture-get :interrupted-clock 'local))
666 (buffer-live-p (marker-buffer
667 (org-capture-get :interrupted-clock 'local))))
668 (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
669 (org-with-point-at clock-in-task
670 (org-clock-in)))
671 (message "Interrupted clock has been resumed")))
673 (let ((beg (point-min))
674 (end (point-max))
675 (abort-note nil))
676 ;; Store the size of the capture buffer
677 (org-capture-put :captured-entry-size (- (point-max) (point-min)))
678 (widen)
679 ;; Store the insertion point in the target buffer
680 (org-capture-put :insertion-point (point))
682 (if org-note-abort
683 (let ((m1 (org-capture-get :begin-marker 'local))
684 (m2 (org-capture-get :end-marker 'local)))
685 (if (and m1 m2 (= m1 beg) (= m2 end))
686 (progn
687 (setq m2 (if (cdr (assoc 'heading org-blank-before-new-entry))
688 m2 (1+ m2))
689 m2 (if (< (point-max) m2) (point-max) m2))
690 (setq abort-note 'clean)
691 (kill-region m1 m2))
692 (setq abort-note 'dirty)))
694 ;; Make sure that the empty lines after are correct
695 (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
696 (member (org-capture-get :type 'local)
697 '(entry item checkitem plain)))
698 (save-excursion
699 (goto-char end)
700 (or (bolp) (newline))
701 (org-capture-empty-lines-after
702 (or (org-capture-get :empty-lines-after 'local)
703 (org-capture-get :empty-lines 'local) 0))))
704 ;; Postprocessing: Update Statistics cookies, do the sorting
705 (when (derived-mode-p 'org-mode)
706 (save-excursion
707 (when (ignore-errors (org-back-to-heading))
708 (org-update-parent-todo-statistics)
709 (org-update-checkbox-count)))
710 ;; FIXME Here we should do the sorting
711 ;; If we have added a table line, maybe recompute?
712 (when (and (eq (org-capture-get :type 'local) 'table-line)
713 (org-at-table-p))
714 (if (org-table-get-stored-formulas)
715 (org-table-recalculate 'all) ;; FIXME: Should we iterate???
716 (org-table-align))))
717 ;; Store this place as the last one where we stored something
718 ;; Do the marking in the base buffer, so that it makes sense after
719 ;; the indirect buffer has been killed.
720 (when org-capture-bookmark
721 (org-capture-bookmark-last-stored-position))
723 ;; Run the hook
724 (run-hooks 'org-capture-before-finalize-hook))
726 (when (org-capture-get :decrypted)
727 (save-excursion
728 (goto-char (org-capture-get :decrypted))
729 (org-encrypt-entry)))
731 ;; Kill the indirect buffer
732 (save-buffer)
733 (let ((return-wconf (org-capture-get :return-to-wconf 'local))
734 (new-buffer (org-capture-get :new-buffer 'local))
735 (kill-buffer (org-capture-get :kill-buffer 'local))
736 (base-buffer (buffer-base-buffer (current-buffer))))
738 ;; Kill the indirect buffer
739 (kill-buffer (current-buffer))
741 ;; Narrow back the target buffer to its previous state
742 (with-current-buffer (org-capture-get :buffer)
743 (let ((reg (org-capture-get :initial-target-region))
744 (pos (org-capture-get :initial-target-position))
745 (ipt (org-capture-get :insertion-point))
746 (size (org-capture-get :captured-entry-size)))
747 (if (not reg)
748 (widen)
749 (cond ((< ipt (car reg))
750 ;; insertion point is before the narrowed region
751 (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
752 ((> ipt (cdr reg))
753 ;; insertion point is after the narrowed region
754 (narrow-to-region (car reg) (cdr reg)))
756 ;; insertion point is within the narrowed region
757 (narrow-to-region (car reg) (+ size (cdr reg)))))
758 ;; now place back the point at its original position
759 (if (< ipt (car reg))
760 (goto-char (+ size pos))
761 (goto-char (if (< ipt pos) (+ size pos) pos))))))
763 ;; Kill the target buffer if that is desired
764 (when (and base-buffer new-buffer kill-buffer)
765 (with-current-buffer base-buffer (save-buffer))
766 (kill-buffer base-buffer))
768 ;; Restore the window configuration before capture
769 (set-window-configuration return-wconf))
771 (run-hooks 'org-capture-after-finalize-hook)
772 ;; Special cases
773 (cond
774 (abort-note
775 (cond
776 ((equal abort-note 'clean)
777 (message "Capture process aborted and target buffer cleaned up"))
778 ((equal abort-note 'dirty)
779 (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
780 (stay-with-capture
781 (org-capture-goto-last-stored)))
782 ;; Return if we did store something
783 (not abort-note)))
785 (defun org-capture-refile ()
786 "Finalize the current capture and then refile the entry.
787 Refiling is done from the base buffer, because the indirect buffer is then
788 already gone. Any prefix argument will be passed to the refile command."
789 (interactive)
790 (unless (eq (org-capture-get :type 'local) 'entry)
791 (error
792 "Refiling from a capture buffer makes only sense for `entry'-type templates"))
793 (let ((pos (point))
794 (base (buffer-base-buffer (current-buffer)))
795 (org-refile-for-capture t)
796 (kill-buffer (org-capture-get :kill-buffer 'local)))
797 (org-capture-put :kill-buffer nil)
798 (org-capture-finalize)
799 (save-window-excursion
800 (with-current-buffer (or base (current-buffer))
801 (save-excursion
802 (save-restriction
803 (widen)
804 (goto-char pos)
805 (call-interactively 'org-refile)))))
806 (when kill-buffer (kill-buffer base))))
808 (defun org-capture-kill ()
809 "Abort the current capture process."
810 (interactive)
811 ;; FIXME: This does not do the right thing, we need to remove the
812 ;; new stuff by hand it is easy: undo, then kill the buffer
813 (let ((org-note-abort t)
814 (org-capture-before-finalize-hook nil))
815 (org-capture-finalize)))
817 (defun org-capture-goto-last-stored ()
818 "Go to the location where the last capture note was stored."
819 (interactive)
820 (org-goto-marker-or-bmk org-capture-last-stored-marker
821 (plist-get org-bookmark-names-plist
822 :last-capture))
823 (message "This is the last note stored by a capture process"))
825 ;;; Supporting functions for handling the process
827 (defun org-capture-put-target-region-and-position ()
828 "Store the initial region with `org-capture-put'."
829 (org-capture-put
830 :initial-target-region
831 ;; Check if the buffer is currently narrowed
832 (when (org-buffer-narrowed-p)
833 (cons (point-min) (point-max))))
834 ;; store the current point
835 (org-capture-put :initial-target-position (point)))
837 (defvar org-time-was-given) ; dynamically scoped parameter
838 (defun org-capture-set-target-location (&optional target)
839 "Find TARGET buffer and position.
840 Store them in the capture property list."
841 (let ((target-entry-p t) decrypted-hl-pos)
842 (setq target (or target (org-capture-get :target)))
843 (save-excursion
844 (cond
845 ((eq (car target) 'file)
846 (set-buffer (org-capture-target-buffer (nth 1 target)))
847 (org-capture-put-target-region-and-position)
848 (widen)
849 (setq target-entry-p nil))
851 ((eq (car target) 'id)
852 (let ((loc (org-id-find (nth 1 target))))
853 (if (not loc)
854 (error "Cannot find target ID \"%s\"" (nth 1 target))
855 (set-buffer (org-capture-target-buffer (car loc)))
856 (widen)
857 (org-capture-put-target-region-and-position)
858 (goto-char (cdr loc)))))
860 ((eq (car target) 'file+headline)
861 (set-buffer (org-capture-target-buffer (nth 1 target)))
862 (org-capture-put-target-region-and-position)
863 (widen)
864 (let ((hd (nth 2 target)))
865 (goto-char (point-min))
866 (unless (derived-mode-p 'org-mode)
867 (error
868 "Target buffer \"%s\" for file+headline should be in Org mode"
869 (current-buffer)))
870 (if (re-search-forward
871 (format org-complex-heading-regexp-format (regexp-quote hd))
872 nil t)
873 (goto-char (point-at-bol))
874 (goto-char (point-max))
875 (or (bolp) (insert "\n"))
876 (insert "* " hd "\n")
877 (beginning-of-line 0))))
879 ((eq (car target) 'file+olp)
880 (let ((m (org-find-olp
881 (cons (org-capture-expand-file (nth 1 target))
882 (cddr target)))))
883 (set-buffer (marker-buffer m))
884 (org-capture-put-target-region-and-position)
885 (widen)
886 (goto-char m)))
888 ((eq (car target) 'file+regexp)
889 (set-buffer (org-capture-target-buffer (nth 1 target)))
890 (org-capture-put-target-region-and-position)
891 (widen)
892 (goto-char (point-min))
893 (if (re-search-forward (nth 2 target) nil t)
894 (progn
895 (goto-char (if (org-capture-get :prepend)
896 (match-beginning 0) (match-end 0)))
897 (org-capture-put :exact-position (point))
898 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
899 (error "No match for target regexp in file %s" (nth 1 target))))
901 ((memq (car target) '(file+datetree file+datetree+prompt))
902 (require 'org-datetree)
903 (set-buffer (org-capture-target-buffer (nth 1 target)))
904 (org-capture-put-target-region-and-position)
905 (widen)
906 ;; Make a date tree entry, with the current date (or yesterday,
907 ;; if we are extending dates for a couple of hours)
908 (org-datetree-find-date-create
909 (calendar-gregorian-from-absolute
910 (cond
911 (org-overriding-default-time
912 ;; use the overriding default time
913 (time-to-days org-overriding-default-time))
915 ((eq (car target) 'file+datetree+prompt)
916 ;; prompt for date
917 (let ((prompt-time (org-read-date
918 nil t nil "Date for tree entry:"
919 (current-time))))
920 (org-capture-put
921 :default-time
922 (cond ((and (or (not (boundp 'org-time-was-given))
923 (not org-time-was-given))
924 (not (= (time-to-days prompt-time) (org-today))))
925 ;; Use 00:00 when no time is given for another date than today?
926 (apply 'encode-time (append '(0 0 0) (cdddr (decode-time prompt-time)))))
927 ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer)
928 ;; Replace any time range by its start
929 (apply 'encode-time
930 (org-read-date-analyze
931 (replace-match "\\1 \\2" nil nil org-read-date-final-answer)
932 prompt-time (decode-time prompt-time))))
933 (t prompt-time)))
934 (time-to-days prompt-time)))
936 ;; current date, possibly corrected for late night workers
937 (org-today))))))
939 ((eq (car target) 'file+function)
940 (set-buffer (org-capture-target-buffer (nth 1 target)))
941 (org-capture-put-target-region-and-position)
942 (widen)
943 (funcall (nth 2 target))
944 (org-capture-put :exact-position (point))
945 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
947 ((eq (car target) 'function)
948 (funcall (nth 1 target))
949 (org-capture-put :exact-position (point))
950 (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
952 ((eq (car target) 'clock)
953 (if (and (markerp org-clock-hd-marker)
954 (marker-buffer org-clock-hd-marker))
955 (progn (set-buffer (marker-buffer org-clock-hd-marker))
956 (org-capture-put-target-region-and-position)
957 (widen)
958 (goto-char org-clock-hd-marker))
959 (error "No running clock that could be used as capture target")))
961 (t (error "Invalid capture target specification")))
963 (when (and (featurep 'org-crypt) (org-at-encrypted-entry-p))
964 (org-decrypt-entry)
965 (setq decrypted-hl-pos
966 (save-excursion (and (org-back-to-heading t) (point)))))
968 (org-capture-put :buffer (current-buffer) :pos (point)
969 :target-entry-p target-entry-p
970 :decrypted decrypted-hl-pos))))
972 (defun org-capture-expand-file (file)
973 "Expand functions and symbols for FILE.
974 When FILE is a function, call it. When it is a form, evaluate
975 it. When it is a variable, retrieve the value. When it is
976 a string, return it. However, if it is the empty string, return
977 `org-default-notes-file' instead."
978 (cond
979 ((equal file "") org-default-notes-file)
980 ((org-string-nw-p file) file)
981 ((functionp file) (funcall file))
982 ((and (symbolp file) (boundp file)) (symbol-value file))
983 ((consp file) (eval file))
984 (t file)))
986 (defun org-capture-target-buffer (file)
987 "Get a buffer for FILE."
988 (setq file (org-capture-expand-file file))
989 (setq file (or (org-string-nw-p file)
990 org-default-notes-file
991 (error "No notes file specified, and no default available")))
992 (or (org-find-base-buffer-visiting file)
993 (progn (org-capture-put :new-buffer t)
994 (find-file-noselect (expand-file-name file org-directory)))))
996 (defun org-capture-steal-local-variables (buffer)
997 "Install Org-mode local variables of BUFFER."
998 (mapc (lambda (v)
999 (ignore-errors (org-set-local (car v) (cdr v))))
1000 (buffer-local-variables buffer)))
1002 (defun org-capture-place-template (&optional inhibit-wconf-store)
1003 "Insert the template at the target location, and display the buffer.
1004 When `inhibit-wconf-store', don't store the window configuration, as it
1005 may have been stored before."
1006 (unless inhibit-wconf-store
1007 (org-capture-put :return-to-wconf (current-window-configuration)))
1008 (delete-other-windows)
1009 (org-switch-to-buffer-other-window
1010 (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
1011 (widen)
1012 (outline-show-all)
1013 (goto-char (org-capture-get :pos))
1014 (org-set-local 'org-capture-target-marker
1015 (point-marker))
1016 (org-set-local 'outline-level 'org-outline-level)
1017 (let* ((template (org-capture-get :template))
1018 (type (org-capture-get :type)))
1019 (case type
1020 ((nil entry) (org-capture-place-entry))
1021 (table-line (org-capture-place-table-line))
1022 (plain (org-capture-place-plain-text))
1023 (item (org-capture-place-item))
1024 (checkitem (org-capture-place-item))))
1025 (org-capture-mode 1)
1026 (org-set-local 'org-capture-current-plist org-capture-plist))
1028 (defun org-capture-place-entry ()
1029 "Place the template as a new Org entry."
1030 (let* ((txt (org-capture-get :template))
1031 (reversed (org-capture-get :prepend))
1032 (target-entry-p (org-capture-get :target-entry-p))
1033 level beg end file)
1035 (and (org-capture-get :exact-position)
1036 (goto-char (org-capture-get :exact-position)))
1037 (cond
1038 ((not target-entry-p)
1039 ;; Insert as top-level entry, either at beginning or at end of file
1040 (setq level 1)
1041 (if reversed
1042 (progn (goto-char (point-min))
1043 (or (org-at-heading-p)
1044 (outline-next-heading)))
1045 (goto-char (point-max))
1046 (or (bolp) (insert "\n"))))
1048 ;; Insert as a child of the current entry
1049 (and (looking-at "\\*+")
1050 (setq level (- (match-end 0) (match-beginning 0))))
1051 (setq level (org-get-valid-level (or level 1) 1))
1052 (if reversed
1053 (progn
1054 (outline-next-heading)
1055 (or (bolp) (insert "\n")))
1056 (org-end-of-subtree t nil)
1057 (or (bolp) (insert "\n")))))
1058 (org-capture-empty-lines-before)
1059 (setq beg (point))
1060 (org-capture-verify-tree txt)
1061 (org-paste-subtree level txt 'for-yank)
1062 (org-capture-empty-lines-after 1)
1063 (org-capture-position-for-last-stored beg)
1064 (outline-next-heading)
1065 (setq end (point))
1066 (org-capture-mark-kill-region beg (1- end))
1067 (org-capture-narrow beg (1- end))
1068 (if (or (re-search-backward "%\\?" beg t)
1069 (re-search-forward "%\\?" end t))
1070 (replace-match ""))))
1072 (defun org-capture-place-item ()
1073 "Place the template as a new plain list item."
1074 (let* ((txt (org-capture-get :template))
1075 (target-entry-p (org-capture-get :target-entry-p))
1076 (ind 0)
1077 beg end)
1078 (if (org-capture-get :exact-position)
1079 (goto-char (org-capture-get :exact-position))
1080 (cond
1081 ((not target-entry-p)
1082 ;; Insert as top-level entry, either at beginning or at end of file
1083 (setq beg (point-min) end (point-max)))
1085 (setq beg (1+ (point-at-eol))
1086 end (save-excursion (outline-next-heading) (point)))))
1087 (setq ind nil)
1088 (if (org-capture-get :prepend)
1089 (progn
1090 (goto-char beg)
1091 (when (org-list-search-forward (org-item-beginning-re) end t)
1092 (goto-char (match-beginning 0))
1093 (setq ind (org-get-indentation))))
1094 (goto-char end)
1095 (when (org-list-search-backward (org-item-beginning-re) beg t)
1096 (setq ind (org-get-indentation))
1097 (org-end-of-item)))
1098 (unless ind (goto-char end)))
1099 ;; Remove common indentation
1100 (setq txt (org-remove-indentation txt))
1101 ;; Make sure this is indeed an item
1102 (unless (string-match (concat "\\`" (org-item-re)) txt)
1103 (setq txt (concat "- "
1104 (mapconcat 'identity (split-string txt "\n")
1105 "\n "))))
1106 ;; Prepare surrounding empty lines.
1107 (org-capture-empty-lines-before)
1108 (setq beg (point))
1109 (unless (eolp) (save-excursion (insert "\n")))
1110 (unless ind
1111 (org-indent-line)
1112 (setq ind (org-get-indentation))
1113 (delete-region beg (point)))
1114 ;; Set the correct indentation, depending on context
1115 (setq ind (make-string ind ?\ ))
1116 (setq txt (concat ind
1117 (mapconcat 'identity (split-string txt "\n")
1118 (concat "\n" ind))
1119 "\n"))
1120 ;; Insert item.
1121 (insert txt)
1122 (org-capture-empty-lines-after 1)
1123 (org-capture-position-for-last-stored beg)
1124 (forward-char 1)
1125 (setq end (point))
1126 (org-capture-mark-kill-region beg (1- end))
1127 (org-capture-narrow beg (1- end))
1128 (if (or (re-search-backward "%\\?" beg t)
1129 (re-search-forward "%\\?" end t))
1130 (replace-match ""))))
1132 (defun org-capture-place-table-line ()
1133 "Place the template as a table line."
1134 (require 'org-table)
1135 (let* ((txt (org-capture-get :template))
1136 (target-entry-p (org-capture-get :target-entry-p))
1137 (table-line-pos (org-capture-get :table-line-pos))
1138 ind beg end)
1139 (cond
1140 ((org-capture-get :exact-position)
1141 (goto-char (org-capture-get :exact-position)))
1142 ((not target-entry-p)
1143 ;; Table is not necessarily under a heading
1144 (setq beg (point-min) end (point-max)))
1146 ;; WE are at a heading, limit search to the body
1147 (setq beg (1+ (point-at-eol))
1148 end (save-excursion (outline-next-heading) (point)))))
1149 (if (re-search-forward org-table-dataline-regexp end t)
1150 (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t))
1151 (goto-char e)
1152 (if (looking-at "[ \t]*#\\+tblfm:")
1153 (forward-line 1))
1154 (narrow-to-region b (point)))
1155 (goto-char end)
1156 (insert "\n| |\n|----|\n| |\n")
1157 (narrow-to-region (1+ end) (point)))
1158 ;; We are narrowed to the table, or to an empty line if there was no table
1160 ;; Check if the template is good
1161 (if (not (string-match org-table-dataline-regexp txt))
1162 (setq txt "| %?Bad template |\n"))
1163 (if (functionp table-line-pos)
1164 (setq table-line-pos (funcall table-line-pos))
1165 (setq table-line-pos (eval table-line-pos)))
1166 (cond
1167 ((and table-line-pos
1168 (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
1169 ;; we have a complex line specification
1170 (let ((ll (ignore-errors
1171 (save-match-data (org-table-analyze))
1172 (aref org-table-hlines
1173 (- (match-end 1) (match-beginning 1)))))
1174 (delta (string-to-number (match-string 2 table-line-pos))))
1175 ;; The user wants a special position in the table
1176 (unless ll
1177 (error "Invalid table line specification \"%s\"" table-line-pos))
1178 (goto-char org-table-current-begin-pos)
1179 (forward-line (+ ll delta (if (< delta 0) 0 -1)))
1180 (org-table-insert-row 'below)
1181 (beginning-of-line 1)
1182 (delete-region (point) (1+ (point-at-eol)))
1183 (setq beg (point))
1184 (insert txt)
1185 (setq end (point))))
1186 ((org-capture-get :prepend)
1187 (goto-char (point-min))
1188 (re-search-forward org-table-hline-regexp nil t)
1189 (beginning-of-line 1)
1190 (re-search-forward org-table-dataline-regexp nil t)
1191 (beginning-of-line 1)
1192 (setq beg (point))
1193 (org-table-insert-row)
1194 (beginning-of-line 1)
1195 (delete-region (point) (1+ (point-at-eol)))
1196 (insert txt)
1197 (setq end (point)))
1199 (goto-char (point-max))
1200 (re-search-backward org-table-dataline-regexp nil t)
1201 (beginning-of-line 1)
1202 (org-table-insert-row 'below)
1203 (beginning-of-line 1)
1204 (delete-region (point) (1+ (point-at-eol)))
1205 (setq beg (point))
1206 (insert txt)
1207 (setq end (point))))
1208 (goto-char beg)
1209 (org-capture-position-for-last-stored 'table-line)
1210 (if (or (re-search-backward "%\\?" beg t)
1211 (re-search-forward "%\\?" end t))
1212 (replace-match ""))
1213 (org-table-align)))
1215 (defun org-capture-place-plain-text ()
1216 "Place the template plainly.
1217 If the target locator points at an Org node, place the template into
1218 the text of the entry, before the first child. If not, place the
1219 template at the beginning or end of the file.
1220 Of course, if exact position has been required, just put it there."
1221 (let* ((txt (org-capture-get :template))
1222 beg end)
1223 (cond
1224 ((org-capture-get :exact-position)
1225 (goto-char (org-capture-get :exact-position)))
1226 ((and (org-capture-get :target-entry-p)
1227 (bolp)
1228 (looking-at org-outline-regexp))
1229 ;; we should place the text into this entry
1230 (if (org-capture-get :prepend)
1231 ;; Skip meta data and drawers
1232 (org-end-of-meta-data t)
1233 ;; go to ent of the entry text, before the next headline
1234 (outline-next-heading)))
1236 ;; beginning or end of file
1237 (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
1238 (or (bolp) (newline))
1239 (org-capture-empty-lines-before)
1240 (setq beg (point))
1241 (insert txt)
1242 (org-capture-empty-lines-after 1)
1243 (org-capture-position-for-last-stored beg)
1244 (setq end (point))
1245 (org-capture-mark-kill-region beg (1- end))
1246 (org-capture-narrow beg (1- end))
1247 (if (or (re-search-backward "%\\?" beg t)
1248 (re-search-forward "%\\?" end t))
1249 (replace-match ""))))
1251 (defun org-capture-mark-kill-region (beg end)
1252 "Mark the region that will have to be killed when aborting capture."
1253 (let ((m1 (move-marker (make-marker) beg))
1254 (m2 (move-marker (make-marker) end)))
1255 (org-capture-put :begin-marker m1)
1256 (org-capture-put :end-marker m2)))
1258 (defun org-capture-position-for-last-stored (where)
1259 "Memorize the position that should later become the position of last capture."
1260 (cond
1261 ((integerp where)
1262 (org-capture-put :position-for-last-stored
1263 (move-marker (make-marker) where
1264 (or (buffer-base-buffer (current-buffer))
1265 (current-buffer)))))
1266 ((eq where 'table-line)
1267 (org-capture-put :position-for-last-stored
1268 (list 'table-line
1269 (org-table-current-dline))))
1270 (t (error "This should not happen"))))
1272 (defun org-capture-bookmark-last-stored-position ()
1273 "Bookmark the last-captured position."
1274 (let* ((where (org-capture-get :position-for-last-stored 'local))
1275 (pos (cond
1276 ((markerp where)
1277 (prog1 (marker-position where)
1278 (move-marker where nil)))
1279 ((and (listp where) (eq (car where) 'table-line))
1280 (if (org-at-table-p)
1281 (save-excursion
1282 (org-table-goto-line (nth 1 where))
1283 (point-at-bol))
1284 (point))))))
1285 (with-current-buffer (buffer-base-buffer (current-buffer))
1286 (save-excursion
1287 (save-restriction
1288 (widen)
1289 (goto-char pos)
1290 (let ((bookmark-name (plist-get org-bookmark-names-plist
1291 :last-capture)))
1292 (when bookmark-name
1293 (with-demoted-errors
1294 (bookmark-set bookmark-name))))
1295 (move-marker org-capture-last-stored-marker (point)))))))
1297 (defun org-capture-narrow (beg end)
1298 "Narrow, unless configuration says not to narrow."
1299 (unless (org-capture-get :unnarrowed)
1300 (narrow-to-region beg end)
1301 (goto-char beg)))
1303 (defun org-capture-empty-lines-before (&optional n)
1304 "Set the correct number of empty lines before the insertion point.
1305 Point will be after the empty lines, so insertion can directly be done."
1306 (setq n (or n (org-capture-get :empty-lines-before)
1307 (org-capture-get :empty-lines) 0))
1308 (let ((pos (point)))
1309 (org-back-over-empty-lines)
1310 (delete-region (point) pos)
1311 (if (> n 0) (newline n))))
1313 (defun org-capture-empty-lines-after (&optional n)
1314 "Set the correct number of empty lines after the inserted string.
1315 Point will remain at the first line after the inserted text."
1316 (setq n (or n (org-capture-get :empty-lines-after)
1317 (org-capture-get :empty-lines) 0))
1318 (org-back-over-empty-lines)
1319 (while (looking-at "[ \t]*\n") (replace-match ""))
1320 (let ((pos (point)))
1321 (if (> n 0) (newline n))
1322 (goto-char pos)))
1324 (defvar org-clock-marker) ; Defined in org.el
1326 (defun org-capture-insert-template-here ()
1327 "Insert the capture template at point."
1328 (let* ((template (org-capture-get :template))
1329 (type (org-capture-get :type))
1330 beg end pp)
1331 (or (bolp) (newline))
1332 (setq beg (point))
1333 (cond
1334 ((and (eq type 'entry) (derived-mode-p 'org-mode))
1335 (org-capture-verify-tree (org-capture-get :template))
1336 (org-paste-subtree nil template t))
1337 ((and (memq type '(item checkitem))
1338 (derived-mode-p 'org-mode)
1339 (save-excursion (skip-chars-backward " \t\n")
1340 (setq pp (point))
1341 (org-in-item-p)))
1342 (goto-char pp)
1343 (org-insert-item)
1344 (skip-chars-backward " ")
1345 (skip-chars-backward "-+*0123456789).")
1346 (delete-region (point) (point-at-eol))
1347 (setq beg (point))
1348 (org-remove-indentation template)
1349 (insert template)
1350 (org-capture-empty-lines-after)
1351 (goto-char beg)
1352 (org-list-repair)
1353 (org-end-of-item)
1354 (setq end (point)))
1355 (t (insert template)))
1356 (setq end (point))
1357 (goto-char beg)
1358 (if (re-search-forward "%\\?" end t)
1359 (replace-match ""))))
1361 (defun org-capture-set-plist (entry)
1362 "Initialize the property list from the template definition."
1363 (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
1364 (org-capture-put :key (car entry) :description (nth 1 entry)
1365 :target (nth 3 entry))
1366 (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
1367 (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
1368 ;; The template may be empty or omitted for special types.
1369 ;; Here we insert the default templates for such cases.
1370 (cond
1371 ((eq type 'item) (setq txt "- %?"))
1372 ((eq type 'checkitem) (setq txt "- [ ] %?"))
1373 ((eq type 'table-line) (setq txt "| %? |"))
1374 ((member type '(nil entry)) (setq txt "* %?\n %a"))))
1375 (org-capture-put :template txt :type type)))
1377 (defun org-capture-goto-target (&optional template-key)
1378 "Go to the target location of a capture template.
1379 The user is queried for the template."
1380 (interactive)
1381 (let* (org-select-template-temp-major-mode
1382 (entry (org-capture-select-template template-key)))
1383 (unless entry
1384 (error "No capture template selected"))
1385 (org-capture-set-plist entry)
1386 (org-capture-set-target-location)
1387 (org-pop-to-buffer-same-window (org-capture-get :buffer))
1388 (goto-char (org-capture-get :pos))))
1390 (defun org-capture-get-indirect-buffer (&optional buffer prefix)
1391 "Make an indirect buffer for a capture process.
1392 Use PREFIX as a prefix for the name of the indirect buffer."
1393 (setq buffer (or buffer (current-buffer)))
1394 (let ((n 1) (base (buffer-name buffer)) bname)
1395 (setq bname (concat prefix "-" base))
1396 (while (buffer-live-p (get-buffer bname))
1397 (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
1398 (condition-case nil
1399 (make-indirect-buffer buffer bname 'clone)
1400 (error
1401 (let ((buf (make-indirect-buffer buffer bname)))
1402 (with-current-buffer buf (org-mode))
1403 buf)))))
1405 (defun org-capture-verify-tree (tree)
1406 "Throw error if TREE is not a valid tree."
1407 (unless (org-kill-is-subtree-p tree)
1408 (error "Template is not a valid Org entry or tree")))
1410 (defun org-mks (table title &optional prompt specials)
1411 "Select a member of an alist with multiple keys.
1412 TABLE is the alist which should contain entries where the car is a string.
1413 There should be two types of entries.
1415 1. prefix descriptions like (\"a\" \"Description\")
1416 This indicates that `a' is a prefix key for multi-letter selection, and
1417 that there are entries following with keys like \"ab\", \"ax\"...
1419 2. Selectable members must have more than two elements, with the first
1420 being the string of keys that lead to selecting it, and the second a
1421 short description string of the item.
1423 The command will then make a temporary buffer listing all entries
1424 that can be selected with a single key, and all the single key
1425 prefixes. When you press the key for a single-letter entry, it is selected.
1426 When you press a prefix key, the commands (and maybe further prefixes)
1427 under this key will be shown and offered for selection.
1429 TITLE will be placed over the selection in the temporary buffer,
1430 PROMPT will be used when prompting for a key. SPECIAL is an alist with
1431 also (\"key\" \"description\") entries. When one of these is selection,
1432 only the bare key is returned."
1433 (setq prompt (or prompt "Select: "))
1434 (let (tbl orig-table dkey ddesc des-keys allowed-keys
1435 current prefix rtn re pressed buffer (inhibit-quit t))
1436 (save-window-excursion
1437 (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
1438 (setq orig-table table)
1439 (catch 'exit
1440 (while t
1441 (erase-buffer)
1442 (insert title "\n\n")
1443 (setq tbl table
1444 des-keys nil
1445 allowed-keys nil
1446 cursor-type nil)
1447 (setq prefix (if current (concat current " ") ""))
1448 (while tbl
1449 (cond
1450 ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
1451 ;; This is a description on this level
1452 (setq dkey (caar tbl) ddesc (cadar tbl))
1453 (pop tbl)
1454 (push dkey des-keys)
1455 (push dkey allowed-keys)
1456 (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
1457 ;; Skip keys which are below this prefix
1458 (setq re (concat "\\`" (regexp-quote dkey)))
1459 (let (case-fold-search)
1460 (while (and tbl (string-match re (caar tbl))) (pop tbl))))
1461 ((= 2 (length (car tbl)))
1462 ;; Not yet a usable description, skip it
1465 ;; usable entry on this level
1466 (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
1467 (push (caar tbl) allowed-keys)
1468 (pop tbl))))
1469 (when specials
1470 (insert "-------------------------------------------------------------------------------\n")
1471 (let ((sp specials))
1472 (while sp
1473 (insert (format "[%s] %s\n"
1474 (caar sp) (nth 1 (car sp))))
1475 (push (caar sp) allowed-keys)
1476 (pop sp))))
1477 (push "\C-g" allowed-keys)
1478 (goto-char (point-min))
1479 (if (not (pos-visible-in-window-p (point-max)))
1480 (org-fit-window-to-buffer))
1481 (message prompt)
1482 (setq pressed (char-to-string (read-char-exclusive)))
1483 (while (not (member pressed allowed-keys))
1484 (message "Invalid key `%s'" pressed) (sit-for 1)
1485 (message prompt)
1486 (setq pressed (char-to-string (read-char-exclusive))))
1487 (when (equal pressed "\C-g")
1488 (kill-buffer buffer)
1489 (error "Abort"))
1490 (when (and (not (assoc pressed table))
1491 (not (member pressed des-keys))
1492 (assoc pressed specials))
1493 (throw 'exit (setq rtn pressed)))
1494 (unless (member pressed des-keys)
1495 (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
1496 orig-table))))
1497 (setq current (concat current pressed))
1498 (setq table (mapcar
1499 (lambda (x)
1500 (if (and (> (length (car x)) 1)
1501 (equal (substring (car x) 0 1) pressed))
1502 (cons (substring (car x) 1) (cdr x))
1503 nil))
1504 table))
1505 (setq table (remove nil table)))))
1506 (when buffer (kill-buffer buffer))
1507 rtn))
1509 ;;; The template code
1510 (defun org-capture-select-template (&optional keys)
1511 "Select a capture template.
1512 Lisp programs can force the template by setting KEYS to a string."
1513 (let ((org-capture-templates
1514 (or (org-contextualize-keys
1515 org-capture-templates org-capture-templates-contexts)
1516 '(("t" "Task" entry (file+headline "" "Tasks")
1517 "* TODO %?\n %u\n %a")))))
1518 (if keys
1519 (or (assoc keys org-capture-templates)
1520 (error "No capture template referred to by \"%s\" keys" keys))
1521 (org-mks org-capture-templates
1522 "Select a capture template\n========================="
1523 "Template key: "
1524 '(("C" "Customize org-capture-templates")
1525 ("q" "Abort"))))))
1527 (defun org-capture-fill-template (&optional template initial annotation)
1528 "Fill a template and return the filled template as a string.
1529 The template may still contain \"%?\" for cursor positioning."
1530 (setq template (or template (org-capture-get :template)))
1531 (when (stringp initial)
1532 (setq initial (org-no-properties initial)))
1533 (let* ((buffer (org-capture-get :buffer))
1534 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
1535 (ct (org-capture-get :default-time))
1536 (dct (decode-time ct))
1537 (ct1
1538 (if (< (nth 2 dct) org-extend-today-until)
1539 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
1540 ct))
1541 (plist-p (if org-store-link-plist t nil))
1542 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
1543 (v-x (or (org-get-x-clipboard 'PRIMARY)
1544 (org-get-x-clipboard 'CLIPBOARD)
1545 (org-get-x-clipboard 'SECONDARY)))
1546 (v-t (format-time-string (car org-time-stamp-formats) ct1))
1547 (v-T (format-time-string (cdr org-time-stamp-formats) ct1))
1548 (v-u (concat "[" (substring v-t 1 -1) "]"))
1549 (v-U (concat "[" (substring v-T 1 -1) "]"))
1550 ;; `initial' and `annotation' might habe been passed.
1551 ;; But if the property list has them, we prefer those values
1552 (v-i (or (plist-get org-store-link-plist :initial)
1553 initial
1554 (org-capture-get :initial)
1555 ""))
1556 (v-a (or (plist-get org-store-link-plist :annotation)
1557 annotation
1558 (org-capture-get :annotation)
1559 ""))
1560 ;; Is the link empty? Then we do not want it...
1561 (v-a (if (equal v-a "[[]]") "" v-a))
1562 (clipboards (remove nil (list v-i
1563 (org-get-x-clipboard 'PRIMARY)
1564 (org-get-x-clipboard 'CLIPBOARD)
1565 (org-get-x-clipboard 'SECONDARY)
1566 v-c)))
1567 (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
1568 (v-A (if (and v-a (string-match l-re v-a))
1569 (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
1570 v-a))
1571 (v-l (if (and v-a (string-match l-re v-a))
1572 (replace-match "\\1" nil nil v-a)
1573 v-a))
1574 (v-n user-full-name)
1575 (v-k (if (marker-buffer org-clock-marker)
1576 (org-no-properties org-clock-heading)))
1577 (v-K (if (marker-buffer org-clock-marker)
1578 (org-make-link-string
1579 (buffer-file-name (marker-buffer org-clock-marker))
1580 org-clock-heading)))
1581 (v-f (or (org-capture-get :original-file-nondirectory) ""))
1582 (v-F (or (org-capture-get :original-file) ""))
1584 (org-startup-folded nil)
1585 (org-inhibit-startup t)
1586 org-time-was-given org-end-time-was-given x
1587 prompt completions char time pos default histvar strings)
1589 (setq org-store-link-plist
1590 (plist-put org-store-link-plist :annotation v-a)
1591 org-store-link-plist
1592 (plist-put org-store-link-plist :initial v-i))
1593 (setq initial v-i)
1595 (unless template (setq template "") (message "No template") (ding)
1596 (sit-for 1))
1597 (save-window-excursion
1598 (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
1599 (erase-buffer)
1600 (insert template)
1601 (goto-char (point-min))
1602 (org-capture-steal-local-variables buffer)
1603 (setq buffer-file-name nil mark-active nil)
1605 ;; %[] Insert contents of a file.
1606 (goto-char (point-min))
1607 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
1608 (unless (org-capture-escaped-%)
1609 (let ((start (match-beginning 0))
1610 (end (match-end 0))
1611 (filename (expand-file-name (match-string 1))))
1612 (goto-char start)
1613 (delete-region start end)
1614 (condition-case error
1615 (insert-file-contents filename)
1616 (error (insert (format "%%![Couldn not insert %s: %s]"
1617 filename error)))))))
1619 ;; The current time
1620 (goto-char (point-min))
1621 (while (re-search-forward "%<\\([^>\n]+\\)>" nil t)
1622 (replace-match (format-time-string (match-string 1)) t t))
1624 ;; Simple %-escapes
1625 (goto-char (point-min))
1626 (while (re-search-forward "%\\([tTuUaliAcxkKInfF]\\)" nil t)
1627 (unless (org-capture-escaped-%)
1628 (when (and initial (equal (match-string 0) "%i"))
1629 (save-match-data
1630 (let* ((lead (buffer-substring
1631 (point-at-bol) (match-beginning 0))))
1632 (setq v-i (mapconcat 'identity
1633 (org-split-string initial "\n")
1634 (concat "\n" lead))))))
1635 (replace-match (or (eval (intern (concat "v-" (match-string 1)))) "")
1636 t t)))
1638 ;; From the property list
1639 (when plist-p
1640 (goto-char (point-min))
1641 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
1642 (unless (org-capture-escaped-%)
1643 (and (setq x (or (plist-get org-store-link-plist
1644 (intern (match-string 1))) ""))
1645 (replace-match x t t)))))
1647 ;; %() embedded elisp
1648 (goto-char (point-min))
1649 (org-capture-expand-embedded-elisp)
1651 ;; Turn on org-mode in temp buffer, set local variables
1652 ;; This is to support completion in interactive prompts
1653 (let ((org-inhibit-startup t)) (org-mode))
1654 ;; Interactive template entries
1655 (goto-char (point-min))
1656 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
1657 (unless (org-capture-escaped-%)
1658 (setq char (if (match-end 3) (match-string-no-properties 3))
1659 prompt (if (match-end 2) (match-string-no-properties 2)))
1660 (goto-char (match-beginning 0))
1661 (replace-match "")
1662 (setq completions nil default nil)
1663 (when prompt
1664 (setq completions (org-split-string prompt "|")
1665 prompt (pop completions)
1666 default (car completions)
1667 histvar (intern (concat
1668 "org-capture-template-prompt-history::"
1669 (or prompt "")))
1670 completions (mapcar 'list completions)))
1671 (unless (boundp histvar) (set histvar nil))
1672 (cond
1673 ((member char '("G" "g"))
1674 (let* ((org-last-tags-completion-table
1675 (org-global-tags-completion-table
1676 (if (equal char "G")
1677 (org-agenda-files)
1678 (and file (list file)))))
1679 (org-add-colon-after-tag-completion t)
1680 (ins (org-icompleting-read
1681 (if prompt (concat prompt ": ") "Tags: ")
1682 'org-tags-completion-function nil nil nil
1683 'org-tags-history)))
1684 (setq ins (mapconcat 'identity
1685 (org-split-string
1686 ins (org-re "[^[:alnum:]_@#%]+"))
1687 ":"))
1688 (when (string-match "\\S-" ins)
1689 (or (equal (char-before) ?:) (insert ":"))
1690 (insert ins)
1691 (or (equal (char-after) ?:) (insert ":"))
1692 (and (org-at-heading-p)
1693 (let ((org-ignore-region t))
1694 (org-set-tags nil 'align))))))
1695 ((equal char "C")
1696 (cond ((= (length clipboards) 1) (insert (car clipboards)))
1697 ((> (length clipboards) 1)
1698 (insert (read-string "Clipboard/kill value: "
1699 (car clipboards) '(clipboards . 1)
1700 (car clipboards))))))
1701 ((equal char "L")
1702 (cond ((= (length clipboards) 1)
1703 (org-insert-link 0 (car clipboards)))
1704 ((> (length clipboards) 1)
1705 (org-insert-link 0 (read-string "Clipboard/kill value: "
1706 (car clipboards)
1707 '(clipboards . 1)
1708 (car clipboards))))))
1709 ((equal char "p")
1710 (org-set-property (org-no-properties prompt) nil))
1711 (char
1712 ;; These are the date/time related ones
1713 (setq org-time-was-given (equal (upcase char) char))
1714 (setq time (org-read-date (equal (upcase char) char) t nil
1715 prompt))
1716 (if (equal (upcase char) char) (setq org-time-was-given t))
1717 (org-insert-time-stamp time org-time-was-given
1718 (member char '("u" "U"))
1719 nil nil (list org-end-time-was-given)))
1721 (let (org-completion-use-ido)
1722 (push (org-completing-read-no-i
1723 (concat (if prompt prompt "Enter string")
1724 (if default (concat " [" default "]"))
1725 ": ")
1726 completions nil nil nil histvar default)
1727 strings)
1728 (insert (car strings)))))))
1729 ;; Replace %n escapes with nth %^{...} string
1730 (setq strings (nreverse strings))
1731 (goto-char (point-min))
1732 (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
1733 (unless (org-capture-escaped-%)
1734 (replace-match
1735 (nth (1- (string-to-number (match-string 1))) strings)
1736 nil t)))
1737 ;; Make sure there are no empty lines before the text, and that
1738 ;; it ends with a newline character
1739 (goto-char (point-min))
1740 (while (looking-at "[ \t]*\n") (replace-match ""))
1741 (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
1742 ;; Return the expanded template and kill the temporary buffer
1743 (untabify (point-min) (point-max))
1744 (set-buffer-modified-p nil)
1745 (prog1 (buffer-string) (kill-buffer (current-buffer))))))
1747 (defun org-capture-escaped-% ()
1748 "Check if % was escaped - if yes, unescape it now."
1749 (if (equal (char-before (match-beginning 0)) ?\\)
1750 (progn
1751 (delete-region (1- (match-beginning 0)) (match-beginning 0))
1753 nil))
1755 (defun org-capture-expand-embedded-elisp ()
1756 "Evaluate embedded elisp %(sexp) and replace with the result."
1757 (goto-char (point-min))
1758 (while (re-search-forward "%(" nil t)
1759 (unless (org-capture-escaped-%)
1760 (goto-char (match-beginning 0))
1761 (let ((template-start (point)))
1762 (forward-char 1)
1763 (let* ((sexp (read (current-buffer)))
1764 (result (org-eval
1765 (org-capture--expand-keyword-in-embedded-elisp sexp))))
1766 (delete-region template-start (point))
1767 (when result
1768 (if (stringp result)
1769 (insert result)
1770 (error "Capture template sexp `%s' must evaluate to string or nil"
1771 sexp))))))))
1773 (defun org-capture--expand-keyword-in-embedded-elisp (attr)
1774 "Recursively replace capture link keywords in ATTR sexp.
1775 Such keywords are prefixed with \"%:\". See
1776 `org-capture-template' for more information."
1777 (cond ((consp attr)
1778 (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr))
1779 ((symbolp attr)
1780 (let* ((attr-symbol (symbol-name attr))
1781 (key (and (string-match "%\\(:.*\\)" attr-symbol)
1782 (intern (match-string 1 attr-symbol)))))
1783 (or (plist-get org-store-link-plist key)
1784 attr)))
1785 (t attr)))
1787 (defun org-capture-inside-embedded-elisp-p ()
1788 "Return non-nil if point is inside of embedded elisp %(sexp)."
1789 (let (beg end)
1790 (with-syntax-table emacs-lisp-mode-syntax-table
1791 (save-excursion
1792 ;; `looking-at' and `search-backward' below do not match the "%(" if
1793 ;; point is in its middle
1794 (when (equal (char-before) ?%)
1795 (backward-char))
1796 (save-match-data
1797 (when (or (looking-at "%(") (search-backward "%(" nil t))
1798 (setq beg (point))
1799 (setq end (progn (forward-char) (forward-sexp) (1- (point)))))))
1800 (when (and beg end)
1801 (and (<= (point) end) (>= (point) beg))))))
1803 ;;;###autoload
1804 (defun org-capture-import-remember-templates ()
1805 "Set `org-capture-templates' to be similar to `org-remember-templates'."
1806 (interactive)
1807 (when (and (yes-or-no-p
1808 "Import old remember templates into org-capture-templates? ")
1809 (yes-or-no-p
1810 "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
1811 (require 'org-remember)
1812 (setq org-capture-templates
1813 (mapcar
1814 (lambda (entry)
1815 (let ((desc (car entry))
1816 (key (char-to-string (nth 1 entry)))
1817 (template (nth 2 entry))
1818 (file (or (nth 3 entry) org-default-notes-file))
1819 (position (or (nth 4 entry) org-remember-default-headline))
1820 (type 'entry)
1821 (prepend org-reverse-note-order)
1822 immediate target jump-to-captured)
1823 (cond
1824 ((member position '(top bottom))
1825 (setq target (list 'file file)
1826 prepend (eq position 'top)))
1827 ((eq position 'date-tree)
1828 (setq target (list 'file+datetree file)
1829 prepend nil))
1830 (t (setq target (list 'file+headline file position))))
1832 (when (string-match "%!" template)
1833 (setq template (replace-match "" t t template)
1834 immediate t))
1836 (when (string-match "%&" template)
1837 (setq jump-to-captured t))
1839 (append (list key desc type target template)
1840 (if prepend '(:prepend t))
1841 (if immediate '(:immediate-finish t))
1842 (if jump-to-captured '(:jump-to-captured t)))))
1844 org-remember-templates))))
1846 (provide 'org-capture)
1848 ;;; org-capture.el ends here