org-capture.el 71 KB

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