org-capture.el 59 KB

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