org-capture.el 72 KB

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