org-capture.el 51 KB

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