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