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