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