org-capture.el 49 KB

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