org-capture.el 69 KB

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