org-capture.el 57 KB

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