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