org-capture.el 74 KB

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