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