org-capture.el 55 KB

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