org-capture.el 57 KB

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