org-capture.el 71 KB

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