org-capture.el 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. ;;; org-capture.el --- Fast note taking in Org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2016 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 functionality
  23. ;; that used to be 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 anymore
  29. ;; for this 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. (require 'cl-lib)
  40. (require 'org)
  41. (declare-function org-at-encrypted-entry-p "org-crypt" ())
  42. (declare-function org-datetree-find-date-create "org-datetree"
  43. (date &optional keep-restriction))
  44. (declare-function org-decrypt-entry "org-crypt" ())
  45. (declare-function org-encrypt-entry "org-crypt" ())
  46. (declare-function org-table-analyze "org-table" ())
  47. (declare-function org-table-goto-line "org-table" (N))
  48. (defvar org-end-time-was-given)
  49. (defvar org-remember-default-headline)
  50. (defvar org-remember-templates)
  51. (defvar org-table-hlines)
  52. (defvar org-table-current-begin-pos)
  53. (defvar dired-buffers)
  54. (defvar org-capture-clock-was-started nil
  55. "Internal flag, noting if the clock was started.")
  56. (defvar org-capture-last-stored-marker (make-marker)
  57. "Marker pointing to the entry most recently stored with `org-capture'.")
  58. ;; The following variable is scoped dynamically by org-protocol
  59. ;; to indicate that the link properties have already been stored
  60. (defvar org-capture-link-is-already-stored nil)
  61. (defvar org-capture-is-refiling nil
  62. "Non-nil when capture process is refiling an entry.")
  63. (defgroup org-capture nil
  64. "Options concerning capturing new entries."
  65. :tag "Org Capture"
  66. :group 'org)
  67. (defcustom org-capture-templates nil
  68. "Templates for the creation of new entries.
  69. Each entry is a list with the following items:
  70. keys The keys that will select the template, as a string, characters
  71. only, for example \"a\" for a template to be selected with a
  72. single key, or \"bt\" for selection with two keys. When using
  73. several keys, keys using the same prefix key must be together
  74. in the list and preceded by a 2-element entry explaining the
  75. prefix key, for example
  76. (\"b\" \"Templates for marking stuff to buy\")
  77. The \"C\" key is used by default for quick access to the
  78. customization of the template variable. But if you want to use
  79. that key for a template, you can.
  80. description A short string describing the template, will be shown during
  81. selection.
  82. type The type of entry. Valid types are:
  83. entry an Org node, with a headline. Will be filed
  84. as the child of the target entry or as a
  85. top-level entry.
  86. item a plain list item, will be placed in the
  87. first plain list at the target
  88. location.
  89. checkitem a checkbox item. This differs from the
  90. plain list item only is so far as it uses a
  91. different default template.
  92. table-line a new line in the first table at target location.
  93. plain text to be inserted as it is.
  94. target Specification of where the captured item should be placed.
  95. In Org files, targets usually define a node. Entries will
  96. become children of this node, other types will be added to the
  97. table or list in the body of this node.
  98. Most target specifications contain a file name. If that file
  99. name is the empty string, it defaults to `org-default-notes-file'.
  100. A file can also be given as a variable, function, or Emacs Lisp
  101. form. When an absolute path is not specified for a
  102. target, it is taken as relative to `org-directory'.
  103. Valid values are:
  104. (file \"path/to/file\")
  105. Text will be placed at the beginning or end of that file
  106. (id \"id of existing org entry\")
  107. File as child of this entry, or in the body of the entry
  108. (file+headline \"path/to/file\" \"node headline\")
  109. Fast configuration if the target heading is unique in the file
  110. (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
  111. For non-unique headings, the full path is safer
  112. (file+regexp \"path/to/file\" \"regexp to find location\")
  113. File to the entry matching regexp
  114. (file+datetree \"path/to/file\")
  115. Will create a heading in a date tree for today's date
  116. (file+datetree+prompt \"path/to/file\")
  117. Will create a heading in a date tree, prompts for date
  118. (file+weektree \"path/to/file\")
  119. Will create a heading in a week tree for today's date
  120. (file+weektree+prompt \"path/to/file\")
  121. Will create a heading in a week tree, prompts for date
  122. (file+function \"path/to/file\" function-finding-location)
  123. A function to find the right location in the file
  124. (clock)
  125. File to the entry that is currently being clocked
  126. (function function-finding-location)
  127. Most general way: write your own function which both visits
  128. the file and moves point to the right location
  129. template The template for creating the capture item. If you leave this
  130. empty, an appropriate default template will be used. See below
  131. for more details. Instead of a string, this may also be one of
  132. (file \"/path/to/template-file\")
  133. (function function-returning-the-template)
  134. in order to get a template from a file, or dynamically
  135. from a function.
  136. The rest of the entry is a property list of additional options. Recognized
  137. properties are:
  138. :prepend Normally newly captured information will be appended at
  139. the target location (last child, last table line,
  140. last list item...). Setting this property will
  141. change that.
  142. :immediate-finish When set, do not offer to edit the information, just
  143. file it away immediately. This makes sense if the
  144. template only needs information that can be added
  145. automatically.
  146. :jump-to-captured When set, jump to the captured entry when finished.
  147. :empty-lines Set this to the number of lines the should be inserted
  148. before and after the new item. Default 0, only common
  149. other value is 1.
  150. :empty-lines-before Set this to the number of lines the should be inserted
  151. before the new item. Overrides :empty-lines for the
  152. number lines inserted before.
  153. :empty-lines-after Set this to the number of lines the should be inserted
  154. after the new item. Overrides :empty-lines for the
  155. number of lines inserted after.
  156. :clock-in Start the clock in this item.
  157. :clock-keep Keep the clock running when filing the captured entry.
  158. :clock-resume Start the interrupted clock when finishing the capture.
  159. Note that :clock-keep has precedence over :clock-resume.
  160. When setting both to t, the current clock will run and
  161. the previous one will not be resumed.
  162. :unnarrowed Do not narrow the target buffer, simply show the
  163. full buffer. Default is to narrow it so that you
  164. only see the new stuff.
  165. :table-line-pos Specification of the location in the table where the
  166. new line should be inserted. It should be a string like
  167. \"II-3\", meaning that the new line should become the
  168. third line before the second horizontal separator line.
  169. :kill-buffer If the target file was not yet visited by a buffer when
  170. capture was invoked, kill the buffer again after capture
  171. is finalized.
  172. The template defines the text to be inserted. Often this is an
  173. Org mode entry (so the first line should start with a star) that
  174. will be filed as a child of the target headline. It can also be
  175. freely formatted text. Furthermore, the following %-escapes will
  176. be replaced with content and expanded:
  177. %[pathname] Insert the contents of the file given by
  178. `pathname'. These placeholders are expanded at the very
  179. beginning of the process so they can be used to extend the
  180. current template.
  181. %(sexp) Evaluate elisp `(sexp)' and replace it with the results.
  182. Only placeholders pre-existing within the template, or
  183. introduced with %[pathname] are expanded this way. Since this
  184. happens after expanding non-interactive %-escapes, those can
  185. be used to fill the expression.
  186. %<...> The result of format-time-string on the ... format specification.
  187. %t Time stamp, date only.
  188. %T Time stamp with date and time.
  189. %u, %U Like the above, but inactive time stamps.
  190. %i Initial content, copied from the active region. If %i is
  191. indented, the entire inserted text will be indented as well.
  192. %a Annotation, normally the link created with `org-store-link'.
  193. %A Like %a, but prompt for the description part.
  194. %l Like %a, but only insert the literal link.
  195. %c Current kill ring head.
  196. %x Content of the X clipboard.
  197. %k Title of currently clocked task.
  198. %K Link to currently clocked task.
  199. %n User name (taken from the variable `user-full-name').
  200. %f File visited by current buffer when org-capture was called.
  201. %F Full path of the file or directory visited by current buffer.
  202. %:keyword Specific information for certain link types, see below.
  203. %^g Prompt for tags, with completion on tags in target file.
  204. %^G Prompt for tags, with completion on all tags in all agenda files.
  205. %^t Like %t, but prompt for date. Similarly %^T, %^u, %^U.
  206. You may define a prompt like: %^{Please specify birthday}t
  207. %^C Interactive selection of which kill or clip to use.
  208. %^L Like %^C, but insert as link.
  209. %^{prop}p Prompt the user for a value for property `prop'.
  210. %^{prompt} Prompt the user for a string and replace this sequence with it.
  211. A default value and a completion table ca be specified like this:
  212. %^{prompt|default|completion2|completion3|...}.
  213. %? After completing the template, position cursor here.
  214. %\\N Insert the text entered at the nth %^{prompt}, where N
  215. is a number, starting from 1.
  216. Apart from these general escapes, you can access information specific to
  217. the link type that is created. For example, calling `org-capture' in emails
  218. or in Gnus will record the author and the subject of the message, which you
  219. can access with \"%:from\" and \"%:subject\", respectively. Here is a
  220. complete list of what is recorded for each link type.
  221. Link type | Available information
  222. ------------------------+------------------------------------------------------
  223. bbdb | %:type %:name %:company
  224. vm, wl, mh, mew, rmail, | %:type %:subject %:message-id
  225. gnus | %:from %:fromname %:fromaddress
  226. | %:to %:toname %:toaddress
  227. | %:fromto (either \"to NAME\" or \"from NAME\")
  228. | %:date %:date-timestamp (as active timestamp)
  229. | %:date-timestamp-inactive (as inactive timestamp)
  230. gnus | %:group, for messages also all email fields
  231. eww, w3, w3m | %:type %:url
  232. info | %:type %:file %:node
  233. calendar | %:type %:date
  234. When you need to insert a literal percent sign in the template,
  235. you can escape ambiguous cases with a backward slash, e.g., \\%i."
  236. :group 'org-capture
  237. :version "24.1"
  238. :type
  239. (let ((file-variants '(choice :tag "Filename "
  240. (file :tag "Literal")
  241. (function :tag "Function")
  242. (variable :tag "Variable")
  243. (sexp :tag "Form"))))
  244. `(repeat
  245. (choice :value ("" "" entry (file "~/org/notes.org") "")
  246. (list :tag "Multikey description"
  247. (string :tag "Keys ")
  248. (string :tag "Description"))
  249. (list :tag "Template entry"
  250. (string :tag "Keys ")
  251. (string :tag "Description ")
  252. (choice :tag "Capture Type " :value entry
  253. (const :tag "Org entry" entry)
  254. (const :tag "Plain list item" item)
  255. (const :tag "Checkbox item" checkitem)
  256. (const :tag "Plain text" plain)
  257. (const :tag "Table line" table-line))
  258. (choice :tag "Target location"
  259. (list :tag "File"
  260. (const :format "" file)
  261. ,file-variants)
  262. (list :tag "ID"
  263. (const :format "" id)
  264. (string :tag " ID"))
  265. (list :tag "File & Headline"
  266. (const :format "" file+headline)
  267. ,file-variants
  268. (string :tag " Headline"))
  269. (list :tag "File & Outline path"
  270. (const :format "" file+olp)
  271. ,file-variants
  272. (repeat :tag "Outline path" :inline t
  273. (string :tag "Headline")))
  274. (list :tag "File & Regexp"
  275. (const :format "" file+regexp)
  276. ,file-variants
  277. (regexp :tag " Regexp"))
  278. (list :tag "File & Date tree"
  279. (const :format "" file+datetree)
  280. ,file-variants)
  281. (list :tag "File & Date tree, prompt for date"
  282. (const :format "" file+datetree+prompt)
  283. ,file-variants)
  284. (list :tag "File & Week tree"
  285. (const :format "" file+weektree)
  286. ,file-variants)
  287. (list :tag "File & Week tree, prompt for date"
  288. (const :format "" file+weektree+prompt)
  289. ,file-variants)
  290. (list :tag "File & function"
  291. (const :format "" file+function)
  292. ,file-variants
  293. (sexp :tag " Function"))
  294. (list :tag "Current clocking task"
  295. (const :format "" clock))
  296. (list :tag "Function"
  297. (const :format "" function)
  298. (sexp :tag " Function")))
  299. (choice :tag "Template "
  300. (string)
  301. (list :tag "File"
  302. (const :format "" file)
  303. (file :tag "Template file"))
  304. (list :tag "Function"
  305. (const :format "" function)
  306. (function :tag "Template function")))
  307. (plist :inline t
  308. ;; Give the most common options as checkboxes
  309. :options (((const :format "%v " :prepend) (const t))
  310. ((const :format "%v " :immediate-finish) (const t))
  311. ((const :format "%v " :jump-to-captured) (const t))
  312. ((const :format "%v " :empty-lines) (const 1))
  313. ((const :format "%v " :empty-lines-before) (const 1))
  314. ((const :format "%v " :empty-lines-after) (const 1))
  315. ((const :format "%v " :clock-in) (const t))
  316. ((const :format "%v " :clock-keep) (const t))
  317. ((const :format "%v " :clock-resume) (const t))
  318. ((const :format "%v " :unnarrowed) (const t))
  319. ((const :format "%v " :table-line-pos) (const t))
  320. ((const :format "%v " :kill-buffer) (const t)))))))))
  321. (defcustom org-capture-before-finalize-hook nil
  322. "Hook that is run right before a capture process is finalized.
  323. The capture buffer is still current when this hook runs and it is
  324. widened to the entire buffer."
  325. :group 'org-capture
  326. :version "24.1"
  327. :type 'hook)
  328. (defcustom org-capture-after-finalize-hook nil
  329. "Hook that is run right after a capture process is finalized.
  330. Suitable for window cleanup."
  331. :group 'org-capture
  332. :version "24.1"
  333. :type 'hook)
  334. (defcustom org-capture-prepare-finalize-hook nil
  335. "Hook that is run before the finalization starts.
  336. The capture buffer is current and still narrowed."
  337. :group 'org-capture
  338. :version "24.1"
  339. :type 'hook)
  340. (defcustom org-capture-bookmark t
  341. "When non-nil, add a bookmark pointing at the last stored
  342. position when capturing."
  343. :group 'org-capture
  344. :version "24.3"
  345. :type 'boolean)
  346. ;;; The property list for keeping information about the capture process
  347. (defvar org-capture-plist nil
  348. "Plist for the current capture process, global, to avoid having to pass it.")
  349. (defvar org-capture-current-plist nil
  350. "Local variable holding the plist in a capture buffer.
  351. This is used to store the plist for use when finishing a capture process
  352. because another such process might have changed the global variable by then.
  353. Each time a new capture buffer has been set up, the global `org-capture-plist'
  354. is copied to this variable, which is local in the indirect buffer.")
  355. (defvar org-capture-clock-keep nil
  356. "Local variable to store the value of the :clock-keep parameter.
  357. This is needed in case org-capture-finalize is called interactively.")
  358. (defun org-capture-put (&rest stuff)
  359. "Add properties to the capture property list `org-capture-plist'."
  360. (while stuff
  361. (setq org-capture-plist (plist-put org-capture-plist
  362. (pop stuff) (pop stuff)))))
  363. (defun org-capture-get (prop &optional local)
  364. "Get properties from the capture property list `org-capture-plist'.
  365. When LOCAL is set, use the local variable `org-capture-current-plist',
  366. this is necessary after initialization of the capture process,
  367. to avoid conflicts with other active capture processes."
  368. (plist-get (if local org-capture-current-plist org-capture-plist) prop))
  369. (defun org-capture-member (prop &optional local)
  370. "Is PROP a property in `org-capture-plist'.
  371. When LOCAL is set, use the local variable `org-capture-current-plist',
  372. this is necessary after initialization of the capture process,
  373. to avoid conflicts with other active capture processes."
  374. (plist-get (if local org-capture-current-plist org-capture-plist) prop))
  375. ;;; The minor mode
  376. (defvar org-capture-mode-map (make-sparse-keymap)
  377. "Keymap for `org-capture-mode', a minor mode.
  378. Use this map to set additional keybindings for when Org mode is used
  379. for a capture buffer.")
  380. (defvar org-capture-mode-hook nil
  381. "Hook for the minor `org-capture-mode'.")
  382. (define-minor-mode org-capture-mode
  383. "Minor mode for special key bindings in a capture buffer.
  384. Turning on this mode runs the normal hook `org-capture-mode-hook'."
  385. nil " Rem" org-capture-mode-map
  386. (setq-local
  387. header-line-format
  388. (substitute-command-keys
  389. "\\<org-capture-mode-map>Capture buffer. Finish \
  390. `\\[org-capture-finalize]', refile `\\[org-capture-refile]', \
  391. abort `\\[org-capture-kill]'.")))
  392. (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
  393. (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
  394. (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
  395. ;;; The main commands
  396. (defvar org-capture-initial nil)
  397. (defvar org-capture-entry nil)
  398. ;;;###autoload
  399. (defun org-capture-string (string &optional keys)
  400. "Capture STRING with the template selected by KEYS."
  401. (interactive "sInitial text: \n")
  402. (let ((org-capture-initial string)
  403. (org-capture-entry (org-capture-select-template keys)))
  404. (org-capture)))
  405. (defcustom org-capture-templates-contexts nil
  406. "Alist of capture templates and valid contexts.
  407. For example, if you have a capture template \"c\" and you want
  408. this template to be accessible only from `message-mode' buffers,
  409. use this:
  410. \\='((\"c\" ((in-mode . \"message-mode\"))))
  411. Here are the available contexts definitions:
  412. in-file: command displayed only in matching files
  413. in-mode: command displayed only in matching modes
  414. not-in-file: command not displayed in matching files
  415. not-in-mode: command not displayed in matching modes
  416. in-buffer: command displayed only in matching buffers
  417. not-in-buffer: command not displayed in matching buffers
  418. [function]: a custom function taking no argument
  419. If you define several checks, the agenda command will be
  420. accessible if there is at least one valid check.
  421. You can also bind a key to another agenda custom command
  422. depending on contextual rules.
  423. \\='((\"c\" \"d\" ((in-mode . \"message-mode\"))))
  424. Here it means: in `message-mode buffers', use \"c\" as the
  425. key for the capture template otherwise associated with \"d\".
  426. \(The template originally associated with \"d\" is not displayed
  427. to avoid duplicates.)"
  428. :version "24.3"
  429. :group 'org-capture
  430. :type '(repeat (list :tag "Rule"
  431. (string :tag " Capture key")
  432. (string :tag "Replace by template")
  433. (repeat :tag "Available when"
  434. (choice
  435. (cons :tag "Condition"
  436. (choice
  437. (const :tag "In file" in-file)
  438. (const :tag "Not in file" not-in-file)
  439. (const :tag "In buffer" in-buffer)
  440. (const :tag "Not in buffer" not-in-buffer)
  441. (const :tag "In mode" in-mode)
  442. (const :tag "Not in mode" not-in-mode))
  443. (regexp))
  444. (function :tag "Custom function"))))))
  445. (defcustom org-capture-use-agenda-date nil
  446. "Non-nil means use the date at point when capturing from agendas.
  447. When nil, you can still capture using the date at point with
  448. `\\[org-agenda-capture]'."
  449. :group 'org-capture
  450. :version "24.3"
  451. :type 'boolean)
  452. ;;;###autoload
  453. (defun org-capture (&optional goto keys)
  454. "Capture something.
  455. \\<org-capture-mode-map>
  456. This will let you select a template from `org-capture-templates', and
  457. then file the newly captured information. The text is immediately
  458. inserted at the target location, and an indirect buffer is shown where
  459. you can edit it. Pressing `\\[org-capture-finalize]' brings you back to the \
  460. previous
  461. state of Emacs, so that you can continue your work.
  462. When called interactively with a `\\[universal-argument]' prefix argument \
  463. GOTO, don't
  464. capture anything, just go to the file/headline where the selected
  465. template stores its notes.
  466. With a `\\[universal-argument] \\[universal-argument]' prefix argument, go to \
  467. the last note stored.
  468. When called with a `C-0' (zero) prefix, insert a template at point.
  469. ELisp programs can set KEYS to a string associated with a template
  470. in `org-capture-templates'. In this case, interactive selection
  471. will be bypassed.
  472. If `org-capture-use-agenda-date' is non-nil, capturing from the
  473. agenda will use the date at point as the default date. Then, a
  474. `C-1' prefix will tell the capture process to use the HH:MM time
  475. of the day at point (if any) or the current HH:MM time."
  476. (interactive "P")
  477. (when (and org-capture-use-agenda-date
  478. (eq major-mode 'org-agenda-mode))
  479. (setq org-overriding-default-time
  480. (org-get-cursor-date (equal goto 1))))
  481. (cond
  482. ((equal goto '(4)) (org-capture-goto-target))
  483. ((equal goto '(16)) (org-capture-goto-last-stored))
  484. (t
  485. ;; FIXME: Are these needed?
  486. (let* ((orig-buf (current-buffer))
  487. (annotation (if (and (boundp 'org-capture-link-is-already-stored)
  488. org-capture-link-is-already-stored)
  489. (plist-get org-store-link-plist :annotation)
  490. (ignore-errors (org-store-link nil))))
  491. (entry (or org-capture-entry (org-capture-select-template keys)))
  492. initial)
  493. (setq initial (or org-capture-initial
  494. (and (org-region-active-p)
  495. (buffer-substring (point) (mark)))))
  496. (when (stringp initial)
  497. (remove-text-properties 0 (length initial) '(read-only t) initial))
  498. (when (stringp annotation)
  499. (remove-text-properties 0 (length annotation)
  500. '(read-only t) annotation))
  501. (cond
  502. ((equal entry "C")
  503. (customize-variable 'org-capture-templates))
  504. ((equal entry "q")
  505. (user-error "Abort"))
  506. (t
  507. (org-capture-set-plist entry)
  508. (org-capture-get-template)
  509. (org-capture-put :original-buffer orig-buf
  510. :original-file (or (buffer-file-name orig-buf)
  511. (and (featurep 'dired)
  512. (car (rassq orig-buf
  513. dired-buffers))))
  514. :original-file-nondirectory
  515. (and (buffer-file-name orig-buf)
  516. (file-name-nondirectory
  517. (buffer-file-name orig-buf)))
  518. :annotation annotation
  519. :initial initial
  520. :return-to-wconf (current-window-configuration)
  521. :default-time
  522. (or org-overriding-default-time
  523. (org-current-time)))
  524. (org-capture-set-target-location)
  525. (condition-case error
  526. (org-capture-put :template (org-capture-fill-template))
  527. ((error quit)
  528. (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
  529. (error "Capture abort: %s" error)))
  530. (setq org-capture-clock-keep (org-capture-get :clock-keep))
  531. (if (equal goto 0)
  532. ;;insert at point
  533. (org-capture-insert-template-here)
  534. (condition-case error
  535. (org-capture-place-template
  536. (eq (car (org-capture-get :target)) 'function))
  537. ((error quit)
  538. (if (and (buffer-base-buffer (current-buffer))
  539. (string-prefix-p "CAPTURE-" (buffer-name)))
  540. (kill-buffer (current-buffer)))
  541. (set-window-configuration (org-capture-get :return-to-wconf))
  542. (error "Capture template `%s': %s"
  543. (org-capture-get :key)
  544. (nth 1 error))))
  545. (if (and (derived-mode-p 'org-mode)
  546. (org-capture-get :clock-in))
  547. (condition-case nil
  548. (progn
  549. (if (org-clock-is-active)
  550. (org-capture-put :interrupted-clock
  551. (copy-marker org-clock-marker)))
  552. (org-clock-in)
  553. (setq-local org-capture-clock-was-started t))
  554. (error
  555. "Could not start the clock in this capture buffer")))
  556. (if (org-capture-get :immediate-finish)
  557. (org-capture-finalize)))))))))
  558. (defun org-capture-get-template ()
  559. "Get the template from a file or a function if necessary."
  560. (let ((txt (org-capture-get :template)) file)
  561. (cond
  562. ((and (listp txt) (eq (car txt) 'file))
  563. (if (file-exists-p
  564. (setq file (expand-file-name (nth 1 txt) org-directory)))
  565. (setq txt (org-file-contents file))
  566. (setq txt (format "* Template file %s not found" (nth 1 txt)))))
  567. ((and (listp txt) (eq (car txt) 'function))
  568. (if (fboundp (nth 1 txt))
  569. (setq txt (funcall (nth 1 txt)))
  570. (setq txt (format "* Template function %s not found" (nth 1 txt)))))
  571. ((not txt) (setq txt ""))
  572. ((stringp txt))
  573. (t (setq txt "* Invalid capture template")))
  574. (org-capture-put :template txt)))
  575. (defun org-capture-finalize (&optional stay-with-capture)
  576. "Finalize the capture process.
  577. With prefix argument STAY-WITH-CAPTURE, jump to the location of the
  578. captured item after finalizing."
  579. (interactive "P")
  580. (when (org-capture-get :jump-to-captured)
  581. (setq stay-with-capture t))
  582. (unless (and org-capture-mode
  583. (buffer-base-buffer (current-buffer)))
  584. (error "This does not seem to be a capture buffer for Org mode"))
  585. (run-hooks 'org-capture-prepare-finalize-hook)
  586. ;; Did we start the clock in this capture buffer?
  587. (when (and org-capture-clock-was-started
  588. org-clock-marker (marker-buffer org-clock-marker)
  589. (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
  590. (> org-clock-marker (point-min))
  591. (< org-clock-marker (point-max)))
  592. ;; Looks like the clock we started is still running. Clock out.
  593. (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
  594. (when (and (not org-capture-clock-keep)
  595. (org-capture-get :clock-resume 'local)
  596. (markerp (org-capture-get :interrupted-clock 'local))
  597. (buffer-live-p (marker-buffer
  598. (org-capture-get :interrupted-clock 'local))))
  599. (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
  600. (org-with-point-at clock-in-task
  601. (org-clock-in)))
  602. (message "Interrupted clock has been resumed")))
  603. (let ((beg (point-min))
  604. (end (point-max))
  605. (abort-note nil))
  606. ;; Store the size of the capture buffer
  607. (org-capture-put :captured-entry-size (- (point-max) (point-min)))
  608. (widen)
  609. ;; Store the insertion point in the target buffer
  610. (org-capture-put :insertion-point (point))
  611. (if org-note-abort
  612. (let ((m1 (org-capture-get :begin-marker 'local))
  613. (m2 (org-capture-get :end-marker 'local)))
  614. (if (and m1 m2 (= m1 beg) (= m2 end))
  615. (progn
  616. (setq m2 (if (cdr (assq 'heading org-blank-before-new-entry))
  617. m2 (1+ m2))
  618. m2 (if (< (point-max) m2) (point-max) m2))
  619. (setq abort-note 'clean)
  620. (kill-region m1 m2))
  621. (setq abort-note 'dirty)))
  622. ;; Make sure that the empty lines after are correct
  623. (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
  624. (member (org-capture-get :type 'local)
  625. '(entry item checkitem plain)))
  626. (save-excursion
  627. (goto-char end)
  628. (or (bolp) (newline))
  629. (org-capture-empty-lines-after
  630. (or (org-capture-get :empty-lines-after 'local)
  631. (org-capture-get :empty-lines 'local) 0))))
  632. ;; Postprocessing: Update Statistics cookies, do the sorting
  633. (when (derived-mode-p 'org-mode)
  634. (save-excursion
  635. (when (ignore-errors (org-back-to-heading))
  636. (org-update-parent-todo-statistics)
  637. (org-update-checkbox-count)))
  638. ;; FIXME Here we should do the sorting
  639. ;; If we have added a table line, maybe recompute?
  640. (when (and (eq (org-capture-get :type 'local) 'table-line)
  641. (org-at-table-p))
  642. (if (org-table-get-stored-formulas)
  643. (org-table-recalculate 'all) ;; FIXME: Should we iterate???
  644. (org-table-align))))
  645. ;; Store this place as the last one where we stored something
  646. ;; Do the marking in the base buffer, so that it makes sense after
  647. ;; the indirect buffer has been killed.
  648. (when org-capture-bookmark
  649. (org-capture-bookmark-last-stored-position))
  650. ;; Run the hook
  651. (run-hooks 'org-capture-before-finalize-hook))
  652. (when (org-capture-get :decrypted)
  653. (save-excursion
  654. (goto-char (org-capture-get :decrypted))
  655. (org-encrypt-entry)))
  656. ;; Kill the indirect buffer
  657. (save-buffer)
  658. (let ((return-wconf (org-capture-get :return-to-wconf 'local))
  659. (new-buffer (org-capture-get :new-buffer 'local))
  660. (kill-buffer (org-capture-get :kill-buffer 'local))
  661. (base-buffer (buffer-base-buffer (current-buffer))))
  662. ;; Kill the indirect buffer
  663. (kill-buffer (current-buffer))
  664. ;; Narrow back the target buffer to its previous state
  665. (with-current-buffer (org-capture-get :buffer)
  666. (let ((reg (org-capture-get :initial-target-region))
  667. (pos (org-capture-get :initial-target-position))
  668. (ipt (org-capture-get :insertion-point))
  669. (size (org-capture-get :captured-entry-size)))
  670. (if (not reg)
  671. (widen)
  672. (cond ((< ipt (car reg))
  673. ;; insertion point is before the narrowed region
  674. (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
  675. ((> ipt (cdr reg))
  676. ;; insertion point is after the narrowed region
  677. (narrow-to-region (car reg) (cdr reg)))
  678. (t
  679. ;; insertion point is within the narrowed region
  680. (narrow-to-region (car reg) (+ size (cdr reg)))))
  681. ;; now place back the point at its original position
  682. (if (< ipt (car reg))
  683. (goto-char (+ size pos))
  684. (goto-char (if (< ipt pos) (+ size pos) pos))))))
  685. ;; Kill the target buffer if that is desired
  686. (when (and base-buffer new-buffer kill-buffer)
  687. (with-current-buffer base-buffer (save-buffer))
  688. (kill-buffer base-buffer))
  689. ;; Restore the window configuration before capture
  690. (set-window-configuration return-wconf))
  691. (run-hooks 'org-capture-after-finalize-hook)
  692. ;; Special cases
  693. (cond
  694. (abort-note
  695. (cl-case abort-note
  696. (clean
  697. (message "Capture process aborted and target buffer cleaned up"))
  698. (dirty
  699. (error "Capture process aborted, but target buffer could not be \
  700. cleaned up correctly"))))
  701. (stay-with-capture
  702. (org-capture-goto-last-stored)))
  703. ;; Return if we did store something
  704. (not abort-note)))
  705. (defun org-capture-refile ()
  706. "Finalize the current capture and then refile the entry.
  707. Refiling is done from the base buffer, because the indirect buffer is then
  708. already gone. Any prefix argument will be passed to the refile command."
  709. (interactive)
  710. (unless (eq (org-capture-get :type 'local) 'entry)
  711. (error
  712. "Refiling from a capture buffer makes only sense for `entry'-type templates"))
  713. (let ((pos (point))
  714. (base (buffer-base-buffer (current-buffer)))
  715. (org-capture-is-refiling t)
  716. (kill-buffer (org-capture-get :kill-buffer 'local)))
  717. (org-capture-put :kill-buffer nil)
  718. (org-capture-finalize)
  719. (save-window-excursion
  720. (with-current-buffer (or base (current-buffer))
  721. (org-with-wide-buffer
  722. (goto-char pos)
  723. (call-interactively 'org-refile))))
  724. (when kill-buffer (kill-buffer base))))
  725. (defun org-capture-kill ()
  726. "Abort the current capture process."
  727. (interactive)
  728. ;; FIXME: This does not do the right thing, we need to remove the
  729. ;; new stuff by hand it is easy: undo, then kill the buffer
  730. (let ((org-note-abort t)
  731. (org-capture-before-finalize-hook nil))
  732. (org-capture-finalize)))
  733. (defun org-capture-goto-last-stored ()
  734. "Go to the location where the last capture note was stored."
  735. (interactive)
  736. (org-goto-marker-or-bmk org-capture-last-stored-marker
  737. (plist-get org-bookmark-names-plist
  738. :last-capture))
  739. (message "This is the last note stored by a capture process"))
  740. ;;; Supporting functions for handling the process
  741. (defun org-capture-put-target-region-and-position ()
  742. "Store the initial region with `org-capture-put'."
  743. (org-capture-put
  744. :initial-target-region
  745. ;; Check if the buffer is currently narrowed
  746. (when (org-buffer-narrowed-p)
  747. (cons (point-min) (point-max))))
  748. ;; store the current point
  749. (org-capture-put :initial-target-position (point)))
  750. (defvar org-time-was-given) ; dynamically scoped parameter
  751. (defun org-capture-set-target-location (&optional target)
  752. "Find TARGET buffer and position.
  753. Store them in the capture property list."
  754. (let ((target-entry-p t) decrypted-hl-pos)
  755. (setq target (or target (org-capture-get :target)))
  756. (save-excursion
  757. (cond
  758. ((eq (car target) 'file)
  759. (set-buffer (org-capture-target-buffer (nth 1 target)))
  760. (org-capture-put-target-region-and-position)
  761. (widen)
  762. (setq target-entry-p nil))
  763. ((eq (car target) 'id)
  764. (let ((loc (org-id-find (nth 1 target))))
  765. (if (not loc)
  766. (error "Cannot find target ID \"%s\"" (nth 1 target))
  767. (set-buffer (org-capture-target-buffer (car loc)))
  768. (widen)
  769. (org-capture-put-target-region-and-position)
  770. (goto-char (cdr loc)))))
  771. ((eq (car target) 'file+headline)
  772. (set-buffer (org-capture-target-buffer (nth 1 target)))
  773. (org-capture-put-target-region-and-position)
  774. (widen)
  775. (let ((hd (nth 2 target)))
  776. (goto-char (point-min))
  777. (unless (derived-mode-p 'org-mode)
  778. (error
  779. "Target buffer \"%s\" for file+headline should be in Org mode"
  780. (current-buffer)))
  781. (if (re-search-forward
  782. (format org-complex-heading-regexp-format (regexp-quote hd))
  783. nil t)
  784. (goto-char (point-at-bol))
  785. (goto-char (point-max))
  786. (or (bolp) (insert "\n"))
  787. (insert "* " hd "\n")
  788. (beginning-of-line 0))))
  789. ((eq (car target) 'file+olp)
  790. (let ((m (org-find-olp
  791. (cons (org-capture-expand-file (nth 1 target))
  792. (cddr target)))))
  793. (set-buffer (marker-buffer m))
  794. (org-capture-put-target-region-and-position)
  795. (widen)
  796. (goto-char m)))
  797. ((eq (car target) 'file+regexp)
  798. (set-buffer (org-capture-target-buffer (nth 1 target)))
  799. (org-capture-put-target-region-and-position)
  800. (widen)
  801. (goto-char (point-min))
  802. (if (re-search-forward (nth 2 target) nil t)
  803. (progn
  804. (goto-char (if (org-capture-get :prepend)
  805. (match-beginning 0) (match-end 0)))
  806. (org-capture-put :exact-position (point))
  807. (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
  808. (error "No match for target regexp in file %s" (nth 1 target))))
  809. ((memq (car target) '(file+datetree file+datetree+prompt file+weektree file+weektree+prompt))
  810. (require 'org-datetree)
  811. (set-buffer (org-capture-target-buffer (nth 1 target)))
  812. (org-capture-put-target-region-and-position)
  813. (widen)
  814. ;; Make a date/week tree entry, with the current date (or
  815. ;; yesterday, if we are extending dates for a couple of hours)
  816. (funcall
  817. (cond
  818. ((memq (car target) '(file+weektree file+weektree+prompt))
  819. #'org-datetree-find-iso-week-create)
  820. (t #'org-datetree-find-date-create))
  821. (calendar-gregorian-from-absolute
  822. (cond
  823. (org-overriding-default-time
  824. ;; use the overriding default time
  825. (time-to-days org-overriding-default-time))
  826. ((memq (car target) '(file+datetree+prompt file+weektree+prompt))
  827. ;; prompt for date
  828. (let ((prompt-time (org-read-date
  829. nil t nil "Date for tree entry:"
  830. (current-time))))
  831. (org-capture-put
  832. :default-time
  833. (cond ((and (or (not (boundp 'org-time-was-given))
  834. (not org-time-was-given))
  835. (not (= (time-to-days prompt-time) (org-today))))
  836. ;; Use 00:00 when no time is given for another date than today?
  837. (apply #'encode-time
  838. (append '(0 0 0)
  839. (cl-cdddr (decode-time prompt-time)))))
  840. ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer)
  841. ;; Replace any time range by its start
  842. (apply 'encode-time
  843. (org-read-date-analyze
  844. (replace-match "\\1 \\2" nil nil org-read-date-final-answer)
  845. prompt-time (decode-time prompt-time))))
  846. (t prompt-time)))
  847. (time-to-days prompt-time)))
  848. (t
  849. ;; current date, possibly corrected for late night workers
  850. (org-today))))))
  851. ((eq (car target) 'file+function)
  852. (set-buffer (org-capture-target-buffer (nth 1 target)))
  853. (org-capture-put-target-region-and-position)
  854. (widen)
  855. (funcall (nth 2 target))
  856. (org-capture-put :exact-position (point))
  857. (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
  858. ((eq (car target) 'function)
  859. (funcall (nth 1 target))
  860. (org-capture-put :exact-position (point))
  861. (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
  862. ((eq (car target) 'clock)
  863. (if (and (markerp org-clock-hd-marker)
  864. (marker-buffer org-clock-hd-marker))
  865. (progn (set-buffer (marker-buffer org-clock-hd-marker))
  866. (org-capture-put-target-region-and-position)
  867. (widen)
  868. (goto-char org-clock-hd-marker))
  869. (error "No running clock that could be used as capture target")))
  870. (t (error "Invalid capture target specification")))
  871. (when (and (featurep 'org-crypt) (org-at-encrypted-entry-p))
  872. (org-decrypt-entry)
  873. (setq decrypted-hl-pos
  874. (save-excursion (and (org-back-to-heading t) (point)))))
  875. (org-capture-put :buffer (current-buffer) :pos (point)
  876. :target-entry-p target-entry-p
  877. :decrypted decrypted-hl-pos))))
  878. (defun org-capture-expand-file (file)
  879. "Expand functions and symbols for FILE.
  880. When FILE is a function, call it. When it is a variable,
  881. retrieve its value. When it is the empty string, return
  882. `org-default-notes-file'. In any other case, return FILE as-is."
  883. (cond
  884. ((equal file "") org-default-notes-file)
  885. ((functionp file) (funcall file))
  886. ((and (symbolp file) (boundp file)) (symbol-value file))
  887. (t file)))
  888. (defun org-capture-target-buffer (file)
  889. "Get a buffer for FILE."
  890. (setq file (org-capture-expand-file file))
  891. (setq file (or (org-string-nw-p file)
  892. org-default-notes-file
  893. (error "No notes file specified, and no default available")))
  894. (or (org-find-base-buffer-visiting file)
  895. (progn (org-capture-put :new-buffer t)
  896. (find-file-noselect (expand-file-name file org-directory)))))
  897. (defun org-capture-place-template (&optional inhibit-wconf-store)
  898. "Insert the template at the target location, and display the buffer.
  899. When `inhibit-wconf-store', don't store the window configuration, as it
  900. may have been stored before."
  901. (unless inhibit-wconf-store
  902. (org-capture-put :return-to-wconf (current-window-configuration)))
  903. (delete-other-windows)
  904. (org-switch-to-buffer-other-window
  905. (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
  906. (widen)
  907. (outline-show-all)
  908. (goto-char (org-capture-get :pos))
  909. (setq-local outline-level 'org-outline-level)
  910. (pcase (org-capture-get :type)
  911. ((or `nil `entry) (org-capture-place-entry))
  912. (`table-line (org-capture-place-table-line))
  913. (`plain (org-capture-place-plain-text))
  914. (`item (org-capture-place-item))
  915. (`checkitem (org-capture-place-item)))
  916. (org-capture-mode 1)
  917. (setq-local org-capture-current-plist org-capture-plist))
  918. (defun org-capture-place-entry ()
  919. "Place the template as a new Org entry."
  920. (let* ((txt (org-capture-get :template))
  921. (reversed (org-capture-get :prepend))
  922. (target-entry-p (org-capture-get :target-entry-p))
  923. level beg end)
  924. (and (org-capture-get :exact-position)
  925. (goto-char (org-capture-get :exact-position)))
  926. (cond
  927. ((not target-entry-p)
  928. ;; Insert as top-level entry, either at beginning or at end of
  929. ;; file.
  930. (setq level 1)
  931. (if reversed
  932. (progn (goto-char (point-min))
  933. (or (org-at-heading-p)
  934. (outline-next-heading)))
  935. (goto-char (point-max))
  936. (or (bolp) (insert "\n"))))
  937. (t
  938. ;; Insert as a child of the current entry
  939. (and (looking-at "\\*+")
  940. (setq level (- (match-end 0) (match-beginning 0))))
  941. (setq level (org-get-valid-level (or level 1) 1))
  942. (if reversed
  943. (progn
  944. (outline-next-heading)
  945. (or (bolp) (insert "\n")))
  946. (org-end-of-subtree t nil)
  947. (or (bolp) (insert "\n")))))
  948. (org-capture-empty-lines-before)
  949. (setq beg (point))
  950. (org-capture-verify-tree txt)
  951. (org-paste-subtree level txt 'for-yank)
  952. (org-capture-empty-lines-after 1)
  953. (org-capture-position-for-last-stored beg)
  954. (outline-next-heading)
  955. (setq end (point))
  956. (org-capture-mark-kill-region beg (1- end))
  957. (org-capture-narrow beg (1- end))
  958. (if (or (re-search-backward "%\\?" beg t)
  959. (re-search-forward "%\\?" end t))
  960. (replace-match ""))))
  961. (defun org-capture-place-item ()
  962. "Place the template as a new plain list item."
  963. (let* ((txt (org-capture-get :template))
  964. (target-entry-p (org-capture-get :target-entry-p))
  965. (ind 0)
  966. beg end)
  967. (if (org-capture-get :exact-position)
  968. (goto-char (org-capture-get :exact-position))
  969. (cond
  970. ((not target-entry-p)
  971. ;; Insert as top-level entry, either at beginning or at end of file
  972. (setq beg (point-min) end (point-max)))
  973. (t
  974. (setq beg (1+ (point-at-eol))
  975. end (save-excursion (outline-next-heading) (point)))))
  976. (setq ind nil)
  977. (if (org-capture-get :prepend)
  978. (progn
  979. (goto-char beg)
  980. (when (org-list-search-forward (org-item-beginning-re) end t)
  981. (goto-char (match-beginning 0))
  982. (setq ind (org-get-indentation))))
  983. (goto-char end)
  984. (when (org-list-search-backward (org-item-beginning-re) beg t)
  985. (setq ind (org-get-indentation))
  986. (org-end-of-item)))
  987. (unless ind (goto-char end)))
  988. ;; Remove common indentation
  989. (setq txt (org-remove-indentation txt))
  990. ;; Make sure this is indeed an item
  991. (unless (string-match (concat "\\`" (org-item-re)) txt)
  992. (setq txt (concat "- "
  993. (mapconcat 'identity (split-string txt "\n")
  994. "\n "))))
  995. ;; Prepare surrounding empty lines.
  996. (org-capture-empty-lines-before)
  997. (setq beg (point))
  998. (unless (eolp) (save-excursion (insert "\n")))
  999. (unless ind
  1000. (org-indent-line)
  1001. (setq ind (org-get-indentation))
  1002. (delete-region beg (point)))
  1003. ;; Set the correct indentation, depending on context
  1004. (setq ind (make-string ind ?\ ))
  1005. (setq txt (concat ind
  1006. (mapconcat 'identity (split-string txt "\n")
  1007. (concat "\n" ind))
  1008. "\n"))
  1009. ;; Insert item.
  1010. (insert txt)
  1011. (org-capture-empty-lines-after 1)
  1012. (org-capture-position-for-last-stored beg)
  1013. (forward-char 1)
  1014. (setq end (point))
  1015. (org-capture-mark-kill-region beg (1- end))
  1016. (org-capture-narrow beg (1- end))
  1017. (if (or (re-search-backward "%\\?" beg t)
  1018. (re-search-forward "%\\?" end t))
  1019. (replace-match ""))))
  1020. (defun org-capture-place-table-line ()
  1021. "Place the template as a table line."
  1022. (require 'org-table)
  1023. (let* ((txt (org-capture-get :template))
  1024. (target-entry-p (org-capture-get :target-entry-p))
  1025. (table-line-pos (org-capture-get :table-line-pos))
  1026. beg end)
  1027. (cond
  1028. ((org-capture-get :exact-position)
  1029. (goto-char (org-capture-get :exact-position)))
  1030. ((not target-entry-p)
  1031. ;; Table is not necessarily under a heading
  1032. (setq beg (point-min) end (point-max)))
  1033. (t
  1034. ;; WE are at a heading, limit search to the body
  1035. (setq beg (1+ (point-at-eol))
  1036. end (save-excursion (outline-next-heading) (point)))))
  1037. (if (re-search-forward org-table-dataline-regexp end t)
  1038. (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t))
  1039. (goto-char e)
  1040. (if (looking-at "[ \t]*#\\+tblfm:")
  1041. (forward-line 1))
  1042. (narrow-to-region b (point)))
  1043. (goto-char end)
  1044. (insert "\n| |\n|----|\n| |\n")
  1045. (narrow-to-region (1+ end) (point)))
  1046. ;; We are narrowed to the table, or to an empty line if there was no table
  1047. ;; Check if the template is good
  1048. (if (not (string-match org-table-dataline-regexp txt))
  1049. (setq txt "| %?Bad template |\n"))
  1050. (if (functionp table-line-pos)
  1051. (setq table-line-pos (funcall table-line-pos))
  1052. (setq table-line-pos (eval table-line-pos)))
  1053. (cond
  1054. ((and table-line-pos
  1055. (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
  1056. (goto-char (point-min))
  1057. ;; we have a complex line specification
  1058. (let ((ll (ignore-errors
  1059. (save-match-data (org-table-analyze))
  1060. (aref org-table-hlines
  1061. (- (match-end 1) (match-beginning 1)))))
  1062. (delta (string-to-number (match-string 2 table-line-pos))))
  1063. ;; The user wants a special position in the table
  1064. (unless ll
  1065. (error "Invalid table line specification \"%s\"" table-line-pos))
  1066. (goto-char org-table-current-begin-pos)
  1067. (forward-line (+ ll delta (if (< delta 0) 0 -1)))
  1068. (org-table-insert-row 'below)
  1069. (beginning-of-line 1)
  1070. (delete-region (point) (1+ (point-at-eol)))
  1071. (setq beg (point))
  1072. (insert txt)
  1073. (setq end (point))))
  1074. ((org-capture-get :prepend)
  1075. (goto-char (point-min))
  1076. (re-search-forward org-table-hline-regexp nil t)
  1077. (beginning-of-line 1)
  1078. (re-search-forward org-table-dataline-regexp nil t)
  1079. (beginning-of-line 1)
  1080. (setq beg (point))
  1081. (org-table-insert-row)
  1082. (beginning-of-line 1)
  1083. (delete-region (point) (1+ (point-at-eol)))
  1084. (insert txt)
  1085. (setq end (point)))
  1086. (t
  1087. (goto-char (point-max))
  1088. (re-search-backward org-table-dataline-regexp nil t)
  1089. (beginning-of-line 1)
  1090. (org-table-insert-row 'below)
  1091. (beginning-of-line 1)
  1092. (delete-region (point) (1+ (point-at-eol)))
  1093. (setq beg (point))
  1094. (insert txt)
  1095. (setq end (point))))
  1096. (goto-char beg)
  1097. (org-capture-position-for-last-stored 'table-line)
  1098. (if (or (re-search-backward "%\\?" beg t)
  1099. (re-search-forward "%\\?" end t))
  1100. (replace-match ""))
  1101. (org-table-align)))
  1102. (defun org-capture-place-plain-text ()
  1103. "Place the template plainly.
  1104. If the target locator points at an Org node, place the template into
  1105. the text of the entry, before the first child. If not, place the
  1106. template at the beginning or end of the file.
  1107. Of course, if exact position has been required, just put it there."
  1108. (let* ((txt (org-capture-get :template))
  1109. beg end)
  1110. (cond
  1111. ((org-capture-get :exact-position)
  1112. (goto-char (org-capture-get :exact-position)))
  1113. ((and (org-capture-get :target-entry-p)
  1114. (bolp)
  1115. (looking-at org-outline-regexp))
  1116. ;; we should place the text into this entry
  1117. (if (org-capture-get :prepend)
  1118. ;; Skip meta data and drawers
  1119. (org-end-of-meta-data t)
  1120. ;; go to ent of the entry text, before the next headline
  1121. (outline-next-heading)))
  1122. (t
  1123. ;; beginning or end of file
  1124. (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
  1125. (or (bolp) (newline))
  1126. (org-capture-empty-lines-before)
  1127. (setq beg (point))
  1128. (insert txt)
  1129. (org-capture-empty-lines-after 1)
  1130. (org-capture-position-for-last-stored beg)
  1131. (setq end (point))
  1132. (org-capture-mark-kill-region beg (1- end))
  1133. (org-capture-narrow beg (1- end))
  1134. (if (or (re-search-backward "%\\?" beg t)
  1135. (re-search-forward "%\\?" end t))
  1136. (replace-match ""))))
  1137. (defun org-capture-mark-kill-region (beg end)
  1138. "Mark the region that will have to be killed when aborting capture."
  1139. (let ((m1 (move-marker (make-marker) beg))
  1140. (m2 (move-marker (make-marker) end)))
  1141. (org-capture-put :begin-marker m1)
  1142. (org-capture-put :end-marker m2)))
  1143. (defun org-capture-position-for-last-stored (where)
  1144. "Memorize the position that should later become the position of last capture."
  1145. (cond
  1146. ((integerp where)
  1147. (org-capture-put :position-for-last-stored
  1148. (move-marker (make-marker) where
  1149. (or (buffer-base-buffer (current-buffer))
  1150. (current-buffer)))))
  1151. ((eq where 'table-line)
  1152. (org-capture-put :position-for-last-stored
  1153. (list 'table-line
  1154. (org-table-current-dline))))
  1155. (t (error "This should not happen"))))
  1156. (defun org-capture-bookmark-last-stored-position ()
  1157. "Bookmark the last-captured position."
  1158. (let* ((where (org-capture-get :position-for-last-stored 'local))
  1159. (pos (cond
  1160. ((markerp where)
  1161. (prog1 (marker-position where)
  1162. (move-marker where nil)))
  1163. ((and (listp where) (eq (car where) 'table-line))
  1164. (if (org-at-table-p)
  1165. (save-excursion
  1166. (org-table-goto-line (nth 1 where))
  1167. (point-at-bol))
  1168. (point))))))
  1169. (with-current-buffer (buffer-base-buffer (current-buffer))
  1170. (org-with-wide-buffer
  1171. (goto-char pos)
  1172. (let ((bookmark-name (plist-get org-bookmark-names-plist
  1173. :last-capture)))
  1174. (when bookmark-name
  1175. (with-demoted-errors
  1176. (bookmark-set bookmark-name))))
  1177. (move-marker org-capture-last-stored-marker (point))))))
  1178. (defun org-capture-narrow (beg end)
  1179. "Narrow, unless configuration says not to narrow."
  1180. (unless (org-capture-get :unnarrowed)
  1181. (narrow-to-region beg end)
  1182. (goto-char beg)))
  1183. (defun org-capture-empty-lines-before (&optional n)
  1184. "Set the correct number of empty lines before the insertion point.
  1185. Point will be after the empty lines, so insertion can directly be done."
  1186. (setq n (or n (org-capture-get :empty-lines-before)
  1187. (org-capture-get :empty-lines) 0))
  1188. (let ((pos (point)))
  1189. (org-back-over-empty-lines)
  1190. (delete-region (point) pos)
  1191. (if (> n 0) (newline n))))
  1192. (defun org-capture-empty-lines-after (&optional n)
  1193. "Set the correct number of empty lines after the inserted string.
  1194. Point will remain at the first line after the inserted text."
  1195. (setq n (or n (org-capture-get :empty-lines-after)
  1196. (org-capture-get :empty-lines) 0))
  1197. (org-back-over-empty-lines)
  1198. (while (looking-at "[ \t]*\n") (replace-match ""))
  1199. (let ((pos (point)))
  1200. (if (> n 0) (newline n))
  1201. (goto-char pos)))
  1202. (defvar org-clock-marker) ; Defined in org.el
  1203. (defun org-capture-insert-template-here ()
  1204. "Insert the capture template at point."
  1205. (let* ((template (org-capture-get :template))
  1206. (type (org-capture-get :type))
  1207. beg end pp)
  1208. (or (bolp) (newline))
  1209. (setq beg (point))
  1210. (cond
  1211. ((and (eq type 'entry) (derived-mode-p 'org-mode))
  1212. (org-capture-verify-tree (org-capture-get :template))
  1213. (org-paste-subtree nil template t))
  1214. ((and (memq type '(item checkitem))
  1215. (derived-mode-p 'org-mode)
  1216. (save-excursion (skip-chars-backward " \t\n")
  1217. (setq pp (point))
  1218. (org-in-item-p)))
  1219. (goto-char pp)
  1220. (org-insert-item)
  1221. (skip-chars-backward " ")
  1222. (skip-chars-backward "-+*0123456789).")
  1223. (delete-region (point) (point-at-eol))
  1224. (setq beg (point))
  1225. (org-remove-indentation template)
  1226. (insert template)
  1227. (org-capture-empty-lines-after)
  1228. (goto-char beg)
  1229. (org-list-repair)
  1230. (org-end-of-item)
  1231. (setq end (point)))
  1232. (t (insert template)))
  1233. (setq end (point))
  1234. (goto-char beg)
  1235. (if (re-search-forward "%\\?" end t)
  1236. (replace-match ""))))
  1237. (defun org-capture-set-plist (entry)
  1238. "Initialize the property list from the template definition."
  1239. (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
  1240. (org-capture-put :key (car entry) :description (nth 1 entry)
  1241. :target (nth 3 entry))
  1242. (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
  1243. (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
  1244. ;; The template may be empty or omitted for special types.
  1245. ;; Here we insert the default templates for such cases.
  1246. (cond
  1247. ((eq type 'item) (setq txt "- %?"))
  1248. ((eq type 'checkitem) (setq txt "- [ ] %?"))
  1249. ((eq type 'table-line) (setq txt "| %? |"))
  1250. ((member type '(nil entry)) (setq txt "* %?\n %a"))))
  1251. (org-capture-put :template txt :type type)))
  1252. (defun org-capture-goto-target (&optional template-key)
  1253. "Go to the target location of a capture template.
  1254. The user is queried for the template."
  1255. (interactive)
  1256. (let ((entry (org-capture-select-template template-key)))
  1257. (unless entry (error "No capture template selected"))
  1258. (org-capture-set-plist entry)
  1259. (org-capture-set-target-location)
  1260. (pop-to-buffer-same-window (org-capture-get :buffer))
  1261. (goto-char (org-capture-get :pos))))
  1262. (defun org-capture-get-indirect-buffer (&optional buffer prefix)
  1263. "Make an indirect buffer for a capture process.
  1264. Use PREFIX as a prefix for the name of the indirect buffer."
  1265. (setq buffer (or buffer (current-buffer)))
  1266. (let ((n 1) (base (buffer-name buffer)) bname)
  1267. (setq bname (concat prefix "-" base))
  1268. (while (buffer-live-p (get-buffer bname))
  1269. (setq bname (concat prefix "-" (number-to-string (cl-incf n)) "-" base)))
  1270. (condition-case nil
  1271. (make-indirect-buffer buffer bname 'clone)
  1272. (error
  1273. (let ((buf (make-indirect-buffer buffer bname)))
  1274. (with-current-buffer buf (org-mode))
  1275. buf)))))
  1276. (defun org-capture-verify-tree (tree)
  1277. "Throw error if TREE is not a valid tree."
  1278. (unless (org-kill-is-subtree-p tree)
  1279. (error "Template is not a valid Org entry or tree")))
  1280. (defun org-mks (table title &optional prompt specials)
  1281. "Select a member of an alist with multiple keys.
  1282. TABLE is the alist which should contain entries where the car is a string.
  1283. There should be two types of entries.
  1284. 1. prefix descriptions like (\"a\" \"Description\")
  1285. This indicates that `a' is a prefix key for multi-letter selection, and
  1286. that there are entries following with keys like \"ab\", \"ax\"...
  1287. 2. Selectable members must have more than two elements, with the first
  1288. being the string of keys that lead to selecting it, and the second a
  1289. short description string of the item.
  1290. The command will then make a temporary buffer listing all entries
  1291. that can be selected with a single key, and all the single key
  1292. prefixes. When you press the key for a single-letter entry, it is selected.
  1293. When you press a prefix key, the commands (and maybe further prefixes)
  1294. under this key will be shown and offered for selection.
  1295. TITLE will be placed over the selection in the temporary buffer,
  1296. PROMPT will be used when prompting for a key. SPECIAL is an alist with
  1297. also (\"key\" \"description\") entries. When one of these is selection,
  1298. only the bare key is returned."
  1299. (setq prompt (or prompt "Select: "))
  1300. (let (tbl orig-table dkey ddesc des-keys allowed-keys
  1301. current prefix rtn re pressed buffer (inhibit-quit t))
  1302. (save-window-excursion
  1303. (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
  1304. (setq orig-table table)
  1305. (catch 'exit
  1306. (while t
  1307. (erase-buffer)
  1308. (insert title "\n\n")
  1309. (setq tbl table
  1310. des-keys nil
  1311. allowed-keys nil
  1312. cursor-type nil)
  1313. (setq prefix (if current (concat current " ") ""))
  1314. (while tbl
  1315. (cond
  1316. ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
  1317. ;; This is a description on this level
  1318. (setq dkey (caar tbl) ddesc (cl-cadar tbl))
  1319. (pop tbl)
  1320. (push dkey des-keys)
  1321. (push dkey allowed-keys)
  1322. (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
  1323. ;; Skip keys which are below this prefix
  1324. (setq re (concat "\\`" (regexp-quote dkey)))
  1325. (let (case-fold-search)
  1326. (while (and tbl (string-match re (caar tbl))) (pop tbl))))
  1327. ((= 2 (length (car tbl)))
  1328. ;; Not yet a usable description, skip it
  1329. )
  1330. (t
  1331. ;; usable entry on this level
  1332. (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
  1333. (push (caar tbl) allowed-keys)
  1334. (pop tbl))))
  1335. (when specials
  1336. (insert "-------------------------------------------------------------------------------\n")
  1337. (let ((sp specials))
  1338. (while sp
  1339. (insert (format "[%s] %s\n"
  1340. (caar sp) (nth 1 (car sp))))
  1341. (push (caar sp) allowed-keys)
  1342. (pop sp))))
  1343. (push "\C-g" allowed-keys)
  1344. (goto-char (point-min))
  1345. (if (not (pos-visible-in-window-p (point-max)))
  1346. (org-fit-window-to-buffer))
  1347. (message prompt)
  1348. (setq pressed (char-to-string (read-char-exclusive)))
  1349. (while (not (member pressed allowed-keys))
  1350. (message "Invalid key `%s'" pressed) (sit-for 1)
  1351. (message prompt)
  1352. (setq pressed (char-to-string (read-char-exclusive))))
  1353. (when (equal pressed "\C-g")
  1354. (kill-buffer buffer)
  1355. (user-error "Abort"))
  1356. (when (and (not (assoc pressed table))
  1357. (not (member pressed des-keys))
  1358. (assoc pressed specials))
  1359. (throw 'exit (setq rtn pressed)))
  1360. (unless (member pressed des-keys)
  1361. (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
  1362. orig-table))))
  1363. (setq current (concat current pressed))
  1364. (setq table (mapcar
  1365. (lambda (x)
  1366. (if (and (> (length (car x)) 1)
  1367. (equal (substring (car x) 0 1) pressed))
  1368. (cons (substring (car x) 1) (cdr x))
  1369. nil))
  1370. table))
  1371. (setq table (remove nil table)))))
  1372. (when buffer (kill-buffer buffer))
  1373. rtn))
  1374. ;;; The template code
  1375. (defun org-capture-select-template (&optional keys)
  1376. "Select a capture template.
  1377. Lisp programs can force the template by setting KEYS to a string."
  1378. (let ((org-capture-templates
  1379. (or (org-contextualize-keys
  1380. org-capture-templates org-capture-templates-contexts)
  1381. '(("t" "Task" entry (file+headline "" "Tasks")
  1382. "* TODO %?\n %u\n %a")))))
  1383. (if keys
  1384. (or (assoc keys org-capture-templates)
  1385. (error "No capture template referred to by \"%s\" keys" keys))
  1386. (org-mks org-capture-templates
  1387. "Select a capture template\n========================="
  1388. "Template key: "
  1389. '(("C" "Customize org-capture-templates")
  1390. ("q" "Abort"))))))
  1391. (defun org-capture-fill-template (&optional template initial annotation)
  1392. "Fill a template and return the filled template as a string.
  1393. The template may still contain \"%?\" for cursor positioning."
  1394. (let* ((template (or template (org-capture-get :template)))
  1395. (buffer (org-capture-get :buffer))
  1396. (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
  1397. (time (let* ((c (or (org-capture-get :default-time) (current-time)))
  1398. (d (decode-time c)))
  1399. (if (< (nth 2 d) org-extend-today-until)
  1400. (encode-time 0 59 23 (1- (nth 3 d)) (nth 4 d) (nth 5 d))
  1401. c)))
  1402. (v-t (format-time-string (org-time-stamp-format nil) time))
  1403. (v-T (format-time-string (org-time-stamp-format t) time))
  1404. (v-u (format-time-string (org-time-stamp-format nil t) time))
  1405. (v-U (format-time-string (org-time-stamp-format t t) time))
  1406. (v-c (and kill-ring (current-kill 0)))
  1407. (v-x (or (org-get-x-clipboard 'PRIMARY)
  1408. (org-get-x-clipboard 'CLIPBOARD)
  1409. (org-get-x-clipboard 'SECONDARY)))
  1410. ;; `initial' and `annotation' might have been passed. But if
  1411. ;; the property list has them, we prefer those values.
  1412. (v-i (or (plist-get org-store-link-plist :initial)
  1413. (and (stringp initial) (org-no-properties initial))
  1414. (org-capture-get :initial)
  1415. ""))
  1416. (v-a
  1417. (let ((a (or (plist-get org-store-link-plist :annotation)
  1418. annotation
  1419. (org-capture-get :annotation)
  1420. "")))
  1421. ;; Is the link empty? Then we do not want it...
  1422. (if (equal a "[[]]") "" a)))
  1423. (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
  1424. (v-A (if (and v-a (string-match l-re v-a))
  1425. (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
  1426. v-a))
  1427. (v-l (if (and v-a (string-match l-re v-a))
  1428. (replace-match "\\1" nil nil v-a)
  1429. v-a))
  1430. (v-n user-full-name)
  1431. (v-k (and (marker-buffer org-clock-marker)
  1432. (org-no-properties org-clock-heading)))
  1433. (v-K (if (marker-buffer org-clock-marker)
  1434. (org-make-link-string
  1435. (buffer-file-name (marker-buffer org-clock-marker))
  1436. org-clock-heading)))
  1437. (v-f (or (org-capture-get :original-file-nondirectory) ""))
  1438. (v-F (or (org-capture-get :original-file) ""))
  1439. (clipboards (delq nil
  1440. (list v-i
  1441. (org-get-x-clipboard 'PRIMARY)
  1442. (org-get-x-clipboard 'CLIPBOARD)
  1443. (org-get-x-clipboard 'SECONDARY)
  1444. v-c))))
  1445. (setq org-store-link-plist (plist-put org-store-link-plist :annotation v-a))
  1446. (setq org-store-link-plist (plist-put org-store-link-plist :initial v-i))
  1447. (unless template
  1448. (setq template "")
  1449. (message "no template") (ding)
  1450. (sit-for 1))
  1451. (save-window-excursion
  1452. (org-switch-to-buffer-other-window (get-buffer-create "*Capture*"))
  1453. (erase-buffer)
  1454. (setq buffer-file-name nil)
  1455. (setq mark-active nil)
  1456. (insert template)
  1457. (goto-char (point-min))
  1458. ;; %[] insert contents of a file.
  1459. (save-excursion
  1460. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  1461. (let ((filename (expand-file-name (match-string 1)))
  1462. (beg (copy-marker (match-beginning 0)))
  1463. (end (copy-marker (match-end 0))))
  1464. (unless (org-capture-escaped-%)
  1465. (delete-region beg end)
  1466. (set-marker beg nil)
  1467. (set-marker end nil)
  1468. (condition-case error
  1469. (insert-file-contents filename)
  1470. (error
  1471. (insert (format "%%![couldn not insert %s: %s]"
  1472. filename
  1473. error))))))))
  1474. ;; Mark %() embedded elisp for later evaluation.
  1475. (org-capture-expand-embedded-elisp 'mark)
  1476. ;; Expand non-interactive templates.
  1477. (let ((regexp "%\\(:[-a-za-z]+\\|<\\([^>\n]+\\)>\\|[aAcfFikKlntTuUx]\\)"))
  1478. (save-excursion
  1479. (while (re-search-forward regexp nil t)
  1480. ;; `org-capture-escaped-%' may modify buffer and cripple
  1481. ;; match-data. Use markers instead. Ditto for other
  1482. ;; templates.
  1483. (let ((pos (copy-marker (match-beginning 0)))
  1484. (end (copy-marker (match-end 0)))
  1485. (value (match-string 1))
  1486. (time-string (match-string 2)))
  1487. (unless (org-capture-escaped-%)
  1488. (delete-region pos end)
  1489. (set-marker pos nil)
  1490. (set-marker end nil)
  1491. (let ((replacement
  1492. (pcase (string-to-char value)
  1493. (?< (format-time-string time-string))
  1494. (?:
  1495. (or (plist-get org-store-link-plist (intern value))
  1496. ""))
  1497. (?i (let ((lead (buffer-substring-no-properties
  1498. (line-beginning-position) (point))))
  1499. (mapconcat #'identity
  1500. (split-string v-i "\n")
  1501. (concat "\n" lead))))
  1502. (?a v-a)
  1503. (?A v-A)
  1504. (?c v-c)
  1505. (?f v-f)
  1506. (?F v-F)
  1507. (?k v-k)
  1508. (?K v-K)
  1509. (?l v-l)
  1510. (?n v-n)
  1511. (?t v-t)
  1512. (?T v-T)
  1513. (?u v-u)
  1514. (?U v-U)
  1515. (?x v-x))))
  1516. (insert
  1517. (if (org-capture-inside-embedded-elisp-p)
  1518. (replace-regexp-in-string "\"" "\\\\\"" replacement)
  1519. replacement))))))))
  1520. ;; Expand %() embedded Elisp. Limit to Sexp originally marked.
  1521. (org-capture-expand-embedded-elisp)
  1522. ;; Expand interactive templates. This is the last step so that
  1523. ;; template is mostly expanded when prompting happens. Turn on
  1524. ;; Org mode and set local variables. This is to support
  1525. ;; completion in interactive prompts.
  1526. (let ((org-inhibit-startup t)) (org-mode))
  1527. (org-clone-local-variables buffer "\\`org-")
  1528. (let (strings) ; Stores interactive answers.
  1529. (save-excursion
  1530. (let ((regexp "%\\^\\(?:{\\([^}]*\\)}\\)?\\([CgGLptTuU]\\)?"))
  1531. (while (re-search-forward regexp nil t)
  1532. (let* ((items (and (match-end 1)
  1533. (save-match-data
  1534. (split-string (match-string-no-properties 1)
  1535. "|"))))
  1536. (key (match-string 2))
  1537. (beg (copy-marker (match-beginning 0)))
  1538. (end (copy-marker (match-end 0)))
  1539. (prompt (nth 0 items))
  1540. (default (nth 1 items))
  1541. (completions (nthcdr 2 items)))
  1542. (unless (org-capture-escaped-%)
  1543. (delete-region beg end)
  1544. (set-marker beg nil)
  1545. (set-marker end nil)
  1546. (pcase key
  1547. ((or "G" "g")
  1548. (let* ((org-last-tags-completion-table
  1549. (org-global-tags-completion-table
  1550. (cond ((equal key "G") (org-agenda-files))
  1551. (file (list file))
  1552. (t nil))))
  1553. (org-add-colon-after-tag-completion t)
  1554. (ins (mapconcat
  1555. #'identity
  1556. (org-split-string
  1557. (completing-read
  1558. (if prompt (concat prompt ": ") "Tags: ")
  1559. 'org-tags-completion-function nil nil nil
  1560. 'org-tags-history)
  1561. "[^[:alnum:]_@#%]+")
  1562. ":")))
  1563. (when (org-string-nw-p ins)
  1564. (unless (eq (char-before) ?:) (insert ":"))
  1565. (insert ins)
  1566. (unless (eq (char-after) ?:) (insert ":"))
  1567. (and (org-at-heading-p)
  1568. (let ((org-ignore-region t))
  1569. (org-set-tags nil 'align))))))
  1570. ("C"
  1571. (cond
  1572. ((= (length clipboards) 1) (insert (car clipboards)))
  1573. ((> (length clipboards) 1)
  1574. (insert (read-string "Clipboard/kill value: "
  1575. (car clipboards)
  1576. '(clipboards . 1)
  1577. (car clipboards))))))
  1578. ("L"
  1579. (cond ((= (length clipboards) 1)
  1580. (org-insert-link 0 (car clipboards)))
  1581. ((> (length clipboards) 1)
  1582. (org-insert-link
  1583. 0
  1584. (read-string "Clipboard/kill value: "
  1585. (car clipboards)
  1586. '(clipboards . 1)
  1587. (car clipboards))))))
  1588. ("p" (org-set-property prompt nil))
  1589. ((guard key)
  1590. ;; These are the date/time related ones.
  1591. (let* ((upcase? (equal (upcase key) key))
  1592. (org-time-was-given upcase?)
  1593. (org-end-time-was-given)
  1594. (time (org-read-date upcase? t nil prompt)))
  1595. (org-insert-time-stamp
  1596. time org-time-was-given
  1597. (member key '("u" "U"))
  1598. nil nil (list org-end-time-was-given))))
  1599. (_
  1600. (push (org-completing-read
  1601. (concat (or prompt "Enter string")
  1602. (and default (format " [%s]" default))
  1603. ": ")
  1604. completions nil nil nil nil default)
  1605. strings)
  1606. (insert (car strings)))))))))
  1607. ;; Replace %n escapes with nth %^{...} string.
  1608. (setq strings (nreverse strings))
  1609. (save-excursion
  1610. (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
  1611. (unless (org-capture-escaped-%)
  1612. (replace-match
  1613. (nth (1- (string-to-number (match-string 1))) strings)
  1614. nil t)))))
  1615. ;; Make sure there are no empty lines before the text, and that
  1616. ;; it ends with a newline character.
  1617. (skip-chars-forward " \t\n")
  1618. (delete-region (point-min) (line-beginning-position))
  1619. (goto-char (point-max))
  1620. (skip-chars-backward " \t\n")
  1621. (delete-region (point) (point-max))
  1622. (insert "\n")
  1623. ;; Return the expanded template and kill the capture buffer.
  1624. (untabify (point-min) (point-max))
  1625. (set-buffer-modified-p nil)
  1626. (prog1 (buffer-substring-no-properties (point-min) (point-max))
  1627. (kill-buffer (current-buffer))))))
  1628. (defun org-capture-escaped-% ()
  1629. "Non-nil if % was escaped.
  1630. If yes, unescape it now. Assume match-data contains the
  1631. placeholder to check."
  1632. (save-excursion
  1633. (goto-char (match-beginning 0))
  1634. (let ((n (abs (skip-chars-backward "\\\\"))))
  1635. (delete-char (/ (1+ n) 2))
  1636. (= (% n 2) 1))))
  1637. (defun org-capture-expand-embedded-elisp (&optional mark)
  1638. "Evaluate embedded elisp %(sexp) and replace with the result.
  1639. When optional MARK argument is non-nil, mark Sexp with a text
  1640. property (`org-embedded-elisp') for later evaluation. Only
  1641. marked Sexp are evaluated when this argument is nil."
  1642. (save-excursion
  1643. (goto-char (point-min))
  1644. (while (re-search-forward "%(" nil t)
  1645. (cond
  1646. ((get-text-property (match-beginning 0) 'org-embedded-elisp)
  1647. (goto-char (match-beginning 0))
  1648. (let ((template-start (point)))
  1649. (forward-char 1)
  1650. (let* ((sexp (read (current-buffer)))
  1651. (result (org-eval
  1652. (org-capture--expand-keyword-in-embedded-elisp
  1653. sexp))))
  1654. (delete-region template-start (point))
  1655. (cond
  1656. ((not result) nil)
  1657. ((stringp result) (insert result))
  1658. (t (error
  1659. "Capture template sexp `%s' must evaluate to string or nil"
  1660. sexp))))))
  1661. ((not mark) nil)
  1662. ;; Only mark valid and non-escaped sexp.
  1663. ((org-capture-escaped-%) nil)
  1664. (t
  1665. (let ((end (with-syntax-table emacs-lisp-mode-syntax-table
  1666. (ignore-errors (scan-sexps (1- (point)) 1)))))
  1667. (when end
  1668. (put-text-property (- (point) 2) end 'org-embedded-elisp t))))))))
  1669. (defun org-capture--expand-keyword-in-embedded-elisp (attr)
  1670. "Recursively replace capture link keywords in ATTR sexp.
  1671. Such keywords are prefixed with \"%:\". See
  1672. `org-capture-template' for more information."
  1673. (cond ((consp attr)
  1674. (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr))
  1675. ((symbolp attr)
  1676. (let* ((attr-symbol (symbol-name attr))
  1677. (key (and (string-match "%\\(:.*\\)" attr-symbol)
  1678. (intern (match-string 1 attr-symbol)))))
  1679. (or (plist-get org-store-link-plist key)
  1680. attr)))
  1681. (t attr)))
  1682. (defun org-capture-inside-embedded-elisp-p ()
  1683. "Non-nil if point is inside of embedded elisp %(sexp).
  1684. Assume sexps have been marked with
  1685. `org-capture-expand-embedded-elisp' beforehand."
  1686. (get-text-property (point) 'org-embedded-elisp))
  1687. ;;;###autoload
  1688. (defun org-capture-import-remember-templates ()
  1689. "Set `org-capture-templates' to be similar to `org-remember-templates'."
  1690. (interactive)
  1691. (when (and (yes-or-no-p
  1692. "Import old remember templates into org-capture-templates? ")
  1693. (yes-or-no-p
  1694. "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
  1695. (require 'org-remember)
  1696. (setq org-capture-templates
  1697. (mapcar
  1698. (lambda (entry)
  1699. (let ((desc (car entry))
  1700. (key (char-to-string (nth 1 entry)))
  1701. (template (nth 2 entry))
  1702. (file (or (nth 3 entry) org-default-notes-file))
  1703. (position (or (nth 4 entry) org-remember-default-headline))
  1704. (type 'entry)
  1705. (prepend org-reverse-note-order)
  1706. immediate target jump-to-captured)
  1707. (cond
  1708. ((member position '(top bottom))
  1709. (setq target (list 'file file)
  1710. prepend (eq position 'top)))
  1711. ((eq position 'date-tree)
  1712. (setq target (list 'file+datetree file)
  1713. prepend nil))
  1714. (t (setq target (list 'file+headline file position))))
  1715. (when (string-match "%!" template)
  1716. (setq template (replace-match "" t t template)
  1717. immediate t))
  1718. (when (string-match "%&" template)
  1719. (setq jump-to-captured t))
  1720. (append (list key desc type target template)
  1721. (if prepend '(:prepend t))
  1722. (if immediate '(:immediate-finish t))
  1723. (if jump-to-captured '(:jump-to-captured t)))))
  1724. org-remember-templates))))
  1725. ;;; The function was made obsolete by commit 65399674d5 of
  1726. ;;; 2013-02-22. This make-obsolete call was added 2016-09-01.
  1727. (make-obsolete 'org-capture-import-remember-templates "use the `org-capture-templates' variable instead." "Org 9.0")
  1728. (provide 'org-capture)
  1729. ;;; org-capture.el ends here