org-capture.el 71 KB

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