org-capture.el 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  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. (plist-get org-bookmark-names-plist
  706. :last-capture))
  707. (message "This is the last note stored by a capture process"))
  708. ;;; Supporting functions for handling the process
  709. (defun org-capture-put-target-region-and-position ()
  710. "Store the initial region with `org-capture-put'."
  711. (org-capture-put
  712. :initial-target-region
  713. ;; Check if the buffer is currently narrowed
  714. (when (org-buffer-narrowed-p)
  715. (cons (point-min) (point-max))))
  716. ;; store the current point
  717. (org-capture-put :initial-target-position (point)))
  718. (defvar org-time-was-given) ; dynamically scoped parameter
  719. (defun org-capture-set-target-location (&optional target)
  720. "Find TARGET buffer and position.
  721. Store them in the capture property list."
  722. (let ((target-entry-p t) decrypted-hl-pos)
  723. (setq target (or target (org-capture-get :target)))
  724. (save-excursion
  725. (cond
  726. ((eq (car target) 'file)
  727. (set-buffer (org-capture-target-buffer (nth 1 target)))
  728. (org-capture-put-target-region-and-position)
  729. (widen)
  730. (setq target-entry-p nil))
  731. ((eq (car target) 'id)
  732. (let ((loc (org-id-find (nth 1 target))))
  733. (if (not loc)
  734. (error "Cannot find target ID \"%s\"" (nth 1 target))
  735. (set-buffer (org-capture-target-buffer (car loc)))
  736. (widen)
  737. (org-capture-put-target-region-and-position)
  738. (goto-char (cdr loc)))))
  739. ((eq (car target) 'file+headline)
  740. (set-buffer (org-capture-target-buffer (nth 1 target)))
  741. (org-capture-put-target-region-and-position)
  742. (widen)
  743. (let ((hd (nth 2 target)))
  744. (goto-char (point-min))
  745. (unless (derived-mode-p 'org-mode)
  746. (error
  747. "Target buffer \"%s\" for file+headline should be in Org mode"
  748. (current-buffer)))
  749. (if (re-search-forward
  750. (format org-complex-heading-regexp-format (regexp-quote hd))
  751. nil t)
  752. (goto-char (point-at-bol))
  753. (goto-char (point-max))
  754. (or (bolp) (insert "\n"))
  755. (insert "* " hd "\n")
  756. (beginning-of-line 0))))
  757. ((eq (car target) 'file+olp)
  758. (let ((m (org-find-olp
  759. (cons (org-capture-expand-file (nth 1 target))
  760. (cddr target)))))
  761. (set-buffer (marker-buffer m))
  762. (org-capture-put-target-region-and-position)
  763. (widen)
  764. (goto-char m)))
  765. ((eq (car target) 'file+regexp)
  766. (set-buffer (org-capture-target-buffer (nth 1 target)))
  767. (org-capture-put-target-region-and-position)
  768. (widen)
  769. (goto-char (point-min))
  770. (if (re-search-forward (nth 2 target) nil t)
  771. (progn
  772. (goto-char (if (org-capture-get :prepend)
  773. (match-beginning 0) (match-end 0)))
  774. (org-capture-put :exact-position (point))
  775. (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
  776. (error "No match for target regexp in file %s" (nth 1 target))))
  777. ((memq (car target) '(file+datetree file+datetree+prompt))
  778. (require 'org-datetree)
  779. (set-buffer (org-capture-target-buffer (nth 1 target)))
  780. (org-capture-put-target-region-and-position)
  781. (widen)
  782. ;; Make a date tree entry, with the current date (or yesterday,
  783. ;; if we are extending dates for a couple of hours)
  784. (org-datetree-find-date-create
  785. (calendar-gregorian-from-absolute
  786. (cond
  787. (org-overriding-default-time
  788. ;; use the overriding default time
  789. (time-to-days org-overriding-default-time))
  790. ((eq (car target) 'file+datetree+prompt)
  791. ;; prompt for date
  792. (let ((prompt-time (org-read-date
  793. nil t nil "Date for tree entry:"
  794. (current-time))))
  795. (org-capture-put
  796. :default-time
  797. (cond ((and (or (not (boundp 'org-time-was-given))
  798. (not org-time-was-given))
  799. (not (= (time-to-days prompt-time) (org-today))))
  800. ;; Use 00:00 when no time is given for another date than today?
  801. (apply 'encode-time (append '(0 0 0) (cdddr (decode-time prompt-time)))))
  802. ((string-match "\\([^ ]+\\)--?[^ ]+[ ]+\\(.*\\)" org-read-date-final-answer)
  803. ;; Replace any time range by its start
  804. (apply 'encode-time
  805. (org-read-date-analyze
  806. (replace-match "\\1 \\2" nil nil org-read-date-final-answer)
  807. prompt-time (decode-time prompt-time))))
  808. (t prompt-time)))
  809. (time-to-days prompt-time)))
  810. (t
  811. ;; current date, possibly corrected for late night workers
  812. (org-today))))))
  813. ((eq (car target) 'file+function)
  814. (set-buffer (org-capture-target-buffer (nth 1 target)))
  815. (org-capture-put-target-region-and-position)
  816. (widen)
  817. (funcall (nth 2 target))
  818. (org-capture-put :exact-position (point))
  819. (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
  820. ((eq (car target) 'function)
  821. (funcall (nth 1 target))
  822. (org-capture-put :exact-position (point))
  823. (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
  824. ((eq (car target) 'clock)
  825. (if (and (markerp org-clock-hd-marker)
  826. (marker-buffer org-clock-hd-marker))
  827. (progn (set-buffer (marker-buffer org-clock-hd-marker))
  828. (org-capture-put-target-region-and-position)
  829. (widen)
  830. (goto-char org-clock-hd-marker))
  831. (error "No running clock that could be used as capture target")))
  832. (t (error "Invalid capture target specification")))
  833. (when (and (featurep 'org-crypt) (org-at-encrypted-entry-p))
  834. (org-decrypt-entry)
  835. (setq decrypted-hl-pos
  836. (save-excursion (and (org-back-to-heading t) (point)))))
  837. (org-capture-put :buffer (current-buffer) :pos (point)
  838. :target-entry-p target-entry-p
  839. :decrypted decrypted-hl-pos))))
  840. (defun org-capture-expand-file (file)
  841. "Expand functions and symbols for FILE.
  842. When FILE is a function, call it. When it is a form, evaluate
  843. it. When it is a variable, retrieve the value. Return whatever we get."
  844. (cond
  845. ((org-string-nw-p file) file)
  846. ((functionp file) (funcall file))
  847. ((and (symbolp file) (boundp file)) (symbol-value file))
  848. ((and file (consp file)) (eval file))
  849. (t file)))
  850. (defun org-capture-target-buffer (file)
  851. "Get a buffer for FILE."
  852. (setq file (org-capture-expand-file file))
  853. (setq file (or (org-string-nw-p file)
  854. org-default-notes-file
  855. (error "No notes file specified, and no default available")))
  856. (or (org-find-base-buffer-visiting file)
  857. (progn (org-capture-put :new-buffer t)
  858. (find-file-noselect (expand-file-name file org-directory)))))
  859. (defun org-capture-steal-local-variables (buffer)
  860. "Install Org-mode local variables of BUFFER."
  861. (mapc (lambda (v)
  862. (ignore-errors (org-set-local (car v) (cdr v))))
  863. (buffer-local-variables buffer)))
  864. (defun org-capture-place-template (&optional inhibit-wconf-store)
  865. "Insert the template at the target location, and display the buffer.
  866. When `inhibit-wconf-store', don't store the window configuration, as it
  867. may have been stored before."
  868. (unless inhibit-wconf-store
  869. (org-capture-put :return-to-wconf (current-window-configuration)))
  870. (delete-other-windows)
  871. (org-switch-to-buffer-other-window
  872. (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
  873. (widen)
  874. (show-all)
  875. (goto-char (org-capture-get :pos))
  876. (org-set-local 'org-capture-target-marker
  877. (point-marker))
  878. (org-set-local 'outline-level 'org-outline-level)
  879. (let* ((template (org-capture-get :template))
  880. (type (org-capture-get :type)))
  881. (case type
  882. ((nil entry) (org-capture-place-entry))
  883. (table-line (org-capture-place-table-line))
  884. (plain (org-capture-place-plain-text))
  885. (item (org-capture-place-item))
  886. (checkitem (org-capture-place-item))))
  887. (org-capture-mode 1)
  888. (org-set-local 'org-capture-current-plist org-capture-plist))
  889. (defun org-capture-place-entry ()
  890. "Place the template as a new Org entry."
  891. (let* ((txt (org-capture-get :template))
  892. (reversed (org-capture-get :prepend))
  893. (target-entry-p (org-capture-get :target-entry-p))
  894. level beg end file)
  895. (and (org-capture-get :exact-position)
  896. (goto-char (org-capture-get :exact-position)))
  897. (cond
  898. ((not target-entry-p)
  899. ;; Insert as top-level entry, either at beginning or at end of file
  900. (setq level 1)
  901. (if reversed
  902. (progn (goto-char (point-min))
  903. (or (org-at-heading-p)
  904. (outline-next-heading)))
  905. (goto-char (point-max))
  906. (or (bolp) (insert "\n"))))
  907. (t
  908. ;; Insert as a child of the current entry
  909. (and (looking-at "\\*+")
  910. (setq level (- (match-end 0) (match-beginning 0))))
  911. (setq level (org-get-valid-level (or level 1) 1))
  912. (if reversed
  913. (progn
  914. (outline-next-heading)
  915. (or (bolp) (insert "\n")))
  916. (org-end-of-subtree t nil)
  917. (or (bolp) (insert "\n")))))
  918. (org-capture-empty-lines-before)
  919. (setq beg (point))
  920. (org-capture-verify-tree txt)
  921. (org-paste-subtree level txt 'for-yank)
  922. (org-capture-empty-lines-after 1)
  923. (org-capture-position-for-last-stored beg)
  924. (outline-next-heading)
  925. (setq end (point))
  926. (org-capture-mark-kill-region beg (1- end))
  927. (org-capture-narrow beg (1- end))
  928. (if (or (re-search-backward "%\\?" beg t)
  929. (re-search-forward "%\\?" end t))
  930. (replace-match ""))))
  931. (defun org-capture-place-item ()
  932. "Place the template as a new plain list item."
  933. (let* ((txt (org-capture-get :template))
  934. (target-entry-p (org-capture-get :target-entry-p))
  935. (ind 0)
  936. beg end)
  937. (if (org-capture-get :exact-position)
  938. (goto-char (org-capture-get :exact-position))
  939. (cond
  940. ((not target-entry-p)
  941. ;; Insert as top-level entry, either at beginning or at end of file
  942. (setq beg (point-min) end (point-max)))
  943. (t
  944. (setq beg (1+ (point-at-eol))
  945. end (save-excursion (outline-next-heading) (point)))))
  946. (if (org-capture-get :prepend)
  947. (progn
  948. (goto-char beg)
  949. (if (org-list-search-forward (org-item-beginning-re) end t)
  950. (progn
  951. (goto-char (match-beginning 0))
  952. (setq ind (org-get-indentation)))
  953. (goto-char end)
  954. (setq ind 0)))
  955. (goto-char end)
  956. (if (org-list-search-backward (org-item-beginning-re) beg t)
  957. (progn
  958. (setq ind (org-get-indentation))
  959. (org-end-of-item))
  960. (setq ind 0))))
  961. ;; Remove common indentation
  962. (setq txt (org-remove-indentation txt))
  963. ;; Make sure this is indeed an item
  964. (unless (string-match (concat "\\`" (org-item-re)) txt)
  965. (setq txt (concat "- "
  966. (mapconcat 'identity (split-string txt "\n")
  967. "\n "))))
  968. ;; Set the correct indentation, depending on context
  969. (setq ind (make-string ind ?\ ))
  970. (setq txt (concat ind
  971. (mapconcat 'identity (split-string txt "\n")
  972. (concat "\n" ind))
  973. "\n"))
  974. ;; Insert, with surrounding empty lines
  975. (org-capture-empty-lines-before)
  976. (setq beg (point))
  977. (insert txt)
  978. (or (bolp) (insert "\n"))
  979. (org-capture-empty-lines-after 1)
  980. (org-capture-position-for-last-stored beg)
  981. (forward-char 1)
  982. (setq end (point))
  983. (org-capture-mark-kill-region beg (1- end))
  984. (org-capture-narrow beg (1- end))
  985. (if (or (re-search-backward "%\\?" beg t)
  986. (re-search-forward "%\\?" end t))
  987. (replace-match ""))))
  988. (defun org-capture-place-table-line ()
  989. "Place the template as a table line."
  990. (require 'org-table)
  991. (let* ((txt (org-capture-get :template))
  992. (target-entry-p (org-capture-get :target-entry-p))
  993. (table-line-pos (org-capture-get :table-line-pos))
  994. ind beg end)
  995. (cond
  996. ((org-capture-get :exact-position)
  997. (goto-char (org-capture-get :exact-position)))
  998. ((not target-entry-p)
  999. ;; Table is not necessarily under a heading
  1000. (setq beg (point-min) end (point-max)))
  1001. (t
  1002. ;; WE are at a heading, limit search to the body
  1003. (setq beg (1+ (point-at-eol))
  1004. end (save-excursion (outline-next-heading) (point)))))
  1005. (if (re-search-forward org-table-dataline-regexp end t)
  1006. (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t))
  1007. (goto-char e)
  1008. (if (looking-at "[ \t]*#\\+tblfm:")
  1009. (forward-line 1))
  1010. (narrow-to-region b (point)))
  1011. (goto-char end)
  1012. (insert "\n| |\n|----|\n| |\n")
  1013. (narrow-to-region (1+ end) (point)))
  1014. ;; We are narrowed to the table, or to an empty line if there was no table
  1015. ;; Check if the template is good
  1016. (if (not (string-match org-table-dataline-regexp txt))
  1017. (setq txt "| %?Bad template |\n"))
  1018. (if (functionp table-line-pos)
  1019. (setq table-line-pos (funcall table-line-pos))
  1020. (setq table-line-pos (eval table-line-pos)))
  1021. (cond
  1022. ((and table-line-pos
  1023. (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
  1024. ;; we have a complex line specification
  1025. (goto-char (point-min))
  1026. (let ((nh (- (match-end 1) (match-beginning 1)))
  1027. (delta (string-to-number (match-string 2 table-line-pos)))
  1028. ll)
  1029. ;; The user wants a special position in the table
  1030. (org-table-get-specials)
  1031. (setq ll (ignore-errors (aref org-table-hlines nh)))
  1032. (unless ll (error "Invalid table line specification \"%s\""
  1033. table-line-pos))
  1034. (setq ll (+ ll delta (if (< delta 0) 0 -1)))
  1035. (org-goto-line ll)
  1036. (org-table-insert-row 'below)
  1037. (beginning-of-line 1)
  1038. (delete-region (point) (1+ (point-at-eol)))
  1039. (setq beg (point))
  1040. (insert txt)
  1041. (setq end (point))))
  1042. ((org-capture-get :prepend)
  1043. (goto-char (point-min))
  1044. (re-search-forward org-table-hline-regexp nil t)
  1045. (beginning-of-line 1)
  1046. (re-search-forward org-table-dataline-regexp nil t)
  1047. (beginning-of-line 1)
  1048. (setq beg (point))
  1049. (org-table-insert-row)
  1050. (beginning-of-line 1)
  1051. (delete-region (point) (1+ (point-at-eol)))
  1052. (insert txt)
  1053. (setq end (point)))
  1054. (t
  1055. (goto-char (point-max))
  1056. (re-search-backward org-table-dataline-regexp nil t)
  1057. (beginning-of-line 1)
  1058. (org-table-insert-row 'below)
  1059. (beginning-of-line 1)
  1060. (delete-region (point) (1+ (point-at-eol)))
  1061. (setq beg (point))
  1062. (insert txt)
  1063. (setq end (point))))
  1064. (goto-char beg)
  1065. (org-capture-position-for-last-stored 'table-line)
  1066. (if (or (re-search-backward "%\\?" beg t)
  1067. (re-search-forward "%\\?" end t))
  1068. (replace-match ""))
  1069. (org-table-align)))
  1070. (defun org-capture-place-plain-text ()
  1071. "Place the template plainly.
  1072. If the target locator points at an Org node, place the template into
  1073. the text of the entry, before the first child. If not, place the
  1074. template at the beginning or end of the file.
  1075. Of course, if exact position has been required, just put it there."
  1076. (let* ((txt (org-capture-get :template))
  1077. beg end)
  1078. (cond
  1079. ((org-capture-get :exact-position)
  1080. (goto-char (org-capture-get :exact-position)))
  1081. ((and (org-capture-get :target-entry-p)
  1082. (bolp)
  1083. (looking-at org-outline-regexp))
  1084. ;; we should place the text into this entry
  1085. (if (org-capture-get :prepend)
  1086. ;; Skip meta data and drawers
  1087. (org-end-of-meta-data-and-drawers)
  1088. ;; go to ent of the entry text, before the next headline
  1089. (outline-next-heading)))
  1090. (t
  1091. ;; beginning or end of file
  1092. (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
  1093. (or (bolp) (newline))
  1094. (org-capture-empty-lines-before)
  1095. (setq beg (point))
  1096. (insert txt)
  1097. (org-capture-empty-lines-after 1)
  1098. (org-capture-position-for-last-stored beg)
  1099. (setq end (point))
  1100. (org-capture-mark-kill-region beg (1- end))
  1101. (org-capture-narrow beg (1- end))
  1102. (if (or (re-search-backward "%\\?" beg t)
  1103. (re-search-forward "%\\?" end t))
  1104. (replace-match ""))))
  1105. (defun org-capture-mark-kill-region (beg end)
  1106. "Mark the region that will have to be killed when aborting capture."
  1107. (let ((m1 (move-marker (make-marker) beg))
  1108. (m2 (move-marker (make-marker) end)))
  1109. (org-capture-put :begin-marker m1)
  1110. (org-capture-put :end-marker m2)))
  1111. (defun org-capture-position-for-last-stored (where)
  1112. "Memorize the position that should later become the position of last capture."
  1113. (cond
  1114. ((integerp where)
  1115. (org-capture-put :position-for-last-stored
  1116. (move-marker (make-marker) where
  1117. (or (buffer-base-buffer (current-buffer))
  1118. (current-buffer)))))
  1119. ((eq where 'table-line)
  1120. (org-capture-put :position-for-last-stored
  1121. (list 'table-line
  1122. (org-table-current-dline))))
  1123. (t (error "This should not happen"))))
  1124. (defun org-capture-bookmark-last-stored-position ()
  1125. "Bookmark the last-captured position."
  1126. (let* ((where (org-capture-get :position-for-last-stored 'local))
  1127. (pos (cond
  1128. ((markerp where)
  1129. (prog1 (marker-position where)
  1130. (move-marker where nil)))
  1131. ((and (listp where) (eq (car where) 'table-line))
  1132. (if (org-at-table-p)
  1133. (save-excursion
  1134. (org-table-goto-line (nth 1 where))
  1135. (point-at-bol))
  1136. (point))))))
  1137. (with-current-buffer (buffer-base-buffer (current-buffer))
  1138. (save-excursion
  1139. (save-restriction
  1140. (widen)
  1141. (goto-char pos)
  1142. (let ((bookmark-name (plist-get org-bookmark-names-plist
  1143. :last-capture)))
  1144. (when bookmark-name
  1145. (with-demoted-errors
  1146. (bookmark-set bookmark-name))))
  1147. (move-marker org-capture-last-stored-marker (point)))))))
  1148. (defun org-capture-narrow (beg end)
  1149. "Narrow, unless configuration says not to narrow."
  1150. (unless (org-capture-get :unnarrowed)
  1151. (narrow-to-region beg end)
  1152. (goto-char beg)))
  1153. (defun org-capture-empty-lines-before (&optional n)
  1154. "Set the correct number of empty lines before the insertion point.
  1155. Point will be after the empty lines, so insertion can directly be done."
  1156. (setq n (or n (org-capture-get :empty-lines-before)
  1157. (org-capture-get :empty-lines) 0))
  1158. (let ((pos (point)))
  1159. (org-back-over-empty-lines)
  1160. (delete-region (point) pos)
  1161. (if (> n 0) (newline n))))
  1162. (defun org-capture-empty-lines-after (&optional n)
  1163. "Set the correct number of empty lines after the inserted string.
  1164. Point will remain at the first line after the inserted text."
  1165. (setq n (or n (org-capture-get :empty-lines-after)
  1166. (org-capture-get :empty-lines) 0))
  1167. (org-back-over-empty-lines)
  1168. (while (looking-at "[ \t]*\n") (replace-match ""))
  1169. (let ((pos (point)))
  1170. (if (> n 0) (newline n))
  1171. (goto-char pos)))
  1172. (defvar org-clock-marker) ; Defined in org.el
  1173. (defun org-capture-insert-template-here ()
  1174. "Insert the capture template at point."
  1175. (let* ((template (org-capture-get :template))
  1176. (type (org-capture-get :type))
  1177. beg end pp)
  1178. (or (bolp) (newline))
  1179. (setq beg (point))
  1180. (cond
  1181. ((and (eq type 'entry) (derived-mode-p 'org-mode))
  1182. (org-capture-verify-tree (org-capture-get :template))
  1183. (org-paste-subtree nil template t))
  1184. ((and (memq type '(item checkitem))
  1185. (derived-mode-p 'org-mode)
  1186. (save-excursion (skip-chars-backward " \t\n")
  1187. (setq pp (point))
  1188. (org-in-item-p)))
  1189. (goto-char pp)
  1190. (org-insert-item)
  1191. (skip-chars-backward " ")
  1192. (skip-chars-backward "-+*0123456789).")
  1193. (delete-region (point) (point-at-eol))
  1194. (setq beg (point))
  1195. (org-remove-indentation template)
  1196. (insert template)
  1197. (org-capture-empty-lines-after)
  1198. (goto-char beg)
  1199. (org-list-repair)
  1200. (org-end-of-item)
  1201. (setq end (point)))
  1202. (t (insert template)))
  1203. (setq end (point))
  1204. (goto-char beg)
  1205. (if (re-search-forward "%\\?" end t)
  1206. (replace-match ""))))
  1207. (defun org-capture-set-plist (entry)
  1208. "Initialize the property list from the template definition."
  1209. (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
  1210. (org-capture-put :key (car entry) :description (nth 1 entry)
  1211. :target (nth 3 entry))
  1212. (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
  1213. (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
  1214. ;; The template may be empty or omitted for special types.
  1215. ;; Here we insert the default templates for such cases.
  1216. (cond
  1217. ((eq type 'item) (setq txt "- %?"))
  1218. ((eq type 'checkitem) (setq txt "- [ ] %?"))
  1219. ((eq type 'table-line) (setq txt "| %? |"))
  1220. ((member type '(nil entry)) (setq txt "* %?\n %a"))))
  1221. (org-capture-put :template txt :type type)))
  1222. (defun org-capture-goto-target (&optional template-key)
  1223. "Go to the target location of a capture template.
  1224. The user is queried for the template."
  1225. (interactive)
  1226. (let* (org-select-template-temp-major-mode
  1227. (entry (org-capture-select-template template-key)))
  1228. (unless entry
  1229. (error "No capture template selected"))
  1230. (org-capture-set-plist entry)
  1231. (org-capture-set-target-location)
  1232. (org-pop-to-buffer-same-window (org-capture-get :buffer))
  1233. (goto-char (org-capture-get :pos))))
  1234. (defun org-capture-get-indirect-buffer (&optional buffer prefix)
  1235. "Make an indirect buffer for a capture process.
  1236. Use PREFIX as a prefix for the name of the indirect buffer."
  1237. (setq buffer (or buffer (current-buffer)))
  1238. (let ((n 1) (base (buffer-name buffer)) bname)
  1239. (setq bname (concat prefix "-" base))
  1240. (while (buffer-live-p (get-buffer bname))
  1241. (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
  1242. (condition-case nil
  1243. (make-indirect-buffer buffer bname 'clone)
  1244. (error
  1245. (let ((buf (make-indirect-buffer buffer bname)))
  1246. (with-current-buffer buf (org-mode))
  1247. buf)))))
  1248. (defun org-capture-verify-tree (tree)
  1249. "Throw error if TREE is not a valid tree."
  1250. (unless (org-kill-is-subtree-p tree)
  1251. (error "Template is not a valid Org entry or tree")))
  1252. (defun org-mks (table title &optional prompt specials)
  1253. "Select a member of an alist with multiple keys.
  1254. TABLE is the alist which should contain entries where the car is a string.
  1255. There should be two types of entries.
  1256. 1. prefix descriptions like (\"a\" \"Description\")
  1257. This indicates that `a' is a prefix key for multi-letter selection, and
  1258. that there are entries following with keys like \"ab\", \"ax\"...
  1259. 2. Selectable members must have more than two elements, with the first
  1260. being the string of keys that lead to selecting it, and the second a
  1261. short description string of the item.
  1262. The command will then make a temporary buffer listing all entries
  1263. that can be selected with a single key, and all the single key
  1264. prefixes. When you press the key for a single-letter entry, it is selected.
  1265. When you press a prefix key, the commands (and maybe further prefixes)
  1266. under this key will be shown and offered for selection.
  1267. TITLE will be placed over the selection in the temporary buffer,
  1268. PROMPT will be used when prompting for a key. SPECIAL is an alist with
  1269. also (\"key\" \"description\") entries. When one of these is selection,
  1270. only the bare key is returned."
  1271. (setq prompt (or prompt "Select: "))
  1272. (let (tbl orig-table dkey ddesc des-keys allowed-keys
  1273. current prefix rtn re pressed buffer (inhibit-quit t))
  1274. (save-window-excursion
  1275. (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
  1276. (setq orig-table table)
  1277. (catch 'exit
  1278. (while t
  1279. (erase-buffer)
  1280. (insert title "\n\n")
  1281. (setq tbl table
  1282. des-keys nil
  1283. allowed-keys nil
  1284. cursor-type nil)
  1285. (setq prefix (if current (concat current " ") ""))
  1286. (while tbl
  1287. (cond
  1288. ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
  1289. ;; This is a description on this level
  1290. (setq dkey (caar tbl) ddesc (cadar tbl))
  1291. (pop tbl)
  1292. (push dkey des-keys)
  1293. (push dkey allowed-keys)
  1294. (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
  1295. ;; Skip keys which are below this prefix
  1296. (setq re (concat "\\`" (regexp-quote dkey)))
  1297. (let (case-fold-search)
  1298. (while (and tbl (string-match re (caar tbl))) (pop tbl))))
  1299. ((= 2 (length (car tbl)))
  1300. ;; Not yet a usable description, skip it
  1301. )
  1302. (t
  1303. ;; usable entry on this level
  1304. (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
  1305. (push (caar tbl) allowed-keys)
  1306. (pop tbl))))
  1307. (when specials
  1308. (insert "-------------------------------------------------------------------------------\n")
  1309. (let ((sp specials))
  1310. (while sp
  1311. (insert (format "[%s] %s\n"
  1312. (caar sp) (nth 1 (car sp))))
  1313. (push (caar sp) allowed-keys)
  1314. (pop sp))))
  1315. (push "\C-g" allowed-keys)
  1316. (goto-char (point-min))
  1317. (if (not (pos-visible-in-window-p (point-max)))
  1318. (org-fit-window-to-buffer))
  1319. (message prompt)
  1320. (setq pressed (char-to-string (read-char-exclusive)))
  1321. (while (not (member pressed allowed-keys))
  1322. (message "Invalid key `%s'" pressed) (sit-for 1)
  1323. (message prompt)
  1324. (setq pressed (char-to-string (read-char-exclusive))))
  1325. (when (equal pressed "\C-g")
  1326. (kill-buffer buffer)
  1327. (error "Abort"))
  1328. (when (and (not (assoc pressed table))
  1329. (not (member pressed des-keys))
  1330. (assoc pressed specials))
  1331. (throw 'exit (setq rtn pressed)))
  1332. (unless (member pressed des-keys)
  1333. (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
  1334. orig-table))))
  1335. (setq current (concat current pressed))
  1336. (setq table (mapcar
  1337. (lambda (x)
  1338. (if (and (> (length (car x)) 1)
  1339. (equal (substring (car x) 0 1) pressed))
  1340. (cons (substring (car x) 1) (cdr x))
  1341. nil))
  1342. table))
  1343. (setq table (remove nil table)))))
  1344. (when buffer (kill-buffer buffer))
  1345. rtn))
  1346. ;;; The template code
  1347. (defun org-capture-select-template (&optional keys)
  1348. "Select a capture template.
  1349. Lisp programs can force the template by setting KEYS to a string."
  1350. (let ((org-capture-templates
  1351. (or (org-contextualize-keys
  1352. org-capture-templates org-capture-templates-contexts)
  1353. '(("t" "Task" entry (file+headline "" "Tasks")
  1354. "* TODO %?\n %u\n %a")))))
  1355. (if keys
  1356. (or (assoc keys org-capture-templates)
  1357. (error "No capture template referred to by \"%s\" keys" keys))
  1358. (org-mks org-capture-templates
  1359. "Select a capture template\n========================="
  1360. "Template key: "
  1361. '(("C" "Customize org-capture-templates")
  1362. ("q" "Abort"))))))
  1363. (defun org-capture-fill-template (&optional template initial annotation)
  1364. "Fill a template and return the filled template as a string.
  1365. The template may still contain \"%?\" for cursor positioning."
  1366. (setq template (or template (org-capture-get :template)))
  1367. (when (stringp initial)
  1368. (setq initial (org-no-properties initial)))
  1369. (let* ((buffer (org-capture-get :buffer))
  1370. (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
  1371. (ct (org-capture-get :default-time))
  1372. (dct (decode-time ct))
  1373. (ct1
  1374. (if (< (nth 2 dct) org-extend-today-until)
  1375. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  1376. ct))
  1377. (plist-p (if org-store-link-plist t nil))
  1378. (v-c (and (> (length kill-ring) 0) (current-kill 0)))
  1379. (v-x (or (org-get-x-clipboard 'PRIMARY)
  1380. (org-get-x-clipboard 'CLIPBOARD)
  1381. (org-get-x-clipboard 'SECONDARY)))
  1382. (v-t (format-time-string (car org-time-stamp-formats) ct))
  1383. (v-T (format-time-string (cdr org-time-stamp-formats) ct))
  1384. (v-u (concat "[" (substring v-t 1 -1) "]"))
  1385. (v-U (concat "[" (substring v-T 1 -1) "]"))
  1386. ;; `initial' and `annotation' might habe been passed.
  1387. ;; But if the property list has them, we prefer those values
  1388. (v-i (or (plist-get org-store-link-plist :initial)
  1389. initial
  1390. (org-capture-get :initial)
  1391. ""))
  1392. (v-a (or (plist-get org-store-link-plist :annotation)
  1393. annotation
  1394. (org-capture-get :annotation)
  1395. ""))
  1396. ;; Is the link empty? Then we do not want it...
  1397. (v-a (if (equal v-a "[[]]") "" v-a))
  1398. (clipboards (remove nil (list v-i
  1399. (org-get-x-clipboard 'PRIMARY)
  1400. (org-get-x-clipboard 'CLIPBOARD)
  1401. (org-get-x-clipboard 'SECONDARY)
  1402. v-c)))
  1403. (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
  1404. (v-A (if (and v-a (string-match l-re v-a))
  1405. (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
  1406. v-a))
  1407. (v-l (if (and v-a (string-match l-re v-a))
  1408. (replace-match "\\1" nil nil v-a)
  1409. v-a))
  1410. (v-n user-full-name)
  1411. (v-k (if (marker-buffer org-clock-marker)
  1412. (org-no-properties org-clock-heading)))
  1413. (v-K (if (marker-buffer org-clock-marker)
  1414. (org-make-link-string
  1415. (buffer-file-name (marker-buffer org-clock-marker))
  1416. org-clock-heading)))
  1417. (v-f (or (org-capture-get :original-file-nondirectory) ""))
  1418. (v-F (or (org-capture-get :original-file) ""))
  1419. v-I
  1420. (org-startup-folded nil)
  1421. (org-inhibit-startup t)
  1422. org-time-was-given org-end-time-was-given x
  1423. prompt completions char time pos default histvar strings)
  1424. (setq org-store-link-plist
  1425. (plist-put org-store-link-plist :annotation v-a)
  1426. org-store-link-plist
  1427. (plist-put org-store-link-plist :initial v-i))
  1428. (setq initial v-i)
  1429. (unless template (setq template "") (message "No template") (ding)
  1430. (sit-for 1))
  1431. (save-window-excursion
  1432. (delete-other-windows)
  1433. (org-pop-to-buffer-same-window (get-buffer-create "*Capture*"))
  1434. (erase-buffer)
  1435. (insert template)
  1436. (goto-char (point-min))
  1437. (org-capture-steal-local-variables buffer)
  1438. (setq buffer-file-name nil)
  1439. ;; %[] Insert contents of a file.
  1440. (goto-char (point-min))
  1441. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  1442. (unless (org-capture-escaped-%)
  1443. (let ((start (match-beginning 0))
  1444. (end (match-end 0))
  1445. (filename (expand-file-name (match-string 1))))
  1446. (goto-char start)
  1447. (delete-region start end)
  1448. (condition-case error
  1449. (insert-file-contents filename)
  1450. (error (insert (format "%%![Couldn't insert %s: %s]"
  1451. filename error)))))))
  1452. ;; %() embedded elisp
  1453. (org-capture-expand-embedded-elisp)
  1454. ;; The current time
  1455. (goto-char (point-min))
  1456. (while (re-search-forward "%<\\([^>\n]+\\)>" nil t)
  1457. (replace-match (format-time-string (match-string 1)) t t))
  1458. ;; Simple %-escapes
  1459. (goto-char (point-min))
  1460. (while (re-search-forward "%\\([tTuUaliAcxkKInfF]\\)" nil t)
  1461. (unless (org-capture-escaped-%)
  1462. (when (and initial (equal (match-string 0) "%i"))
  1463. (save-match-data
  1464. (let* ((lead (buffer-substring
  1465. (point-at-bol) (match-beginning 0))))
  1466. (setq v-i (mapconcat 'identity
  1467. (org-split-string initial "\n")
  1468. (concat "\n" lead))))))
  1469. (replace-match (or (eval (intern (concat "v-" (match-string 1)))) "")
  1470. t t)))
  1471. ;; From the property list
  1472. (when plist-p
  1473. (goto-char (point-min))
  1474. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  1475. (unless (org-capture-escaped-%)
  1476. (and (setq x (or (plist-get org-store-link-plist
  1477. (intern (match-string 1))) ""))
  1478. (replace-match x t t)))))
  1479. ;; Turn on org-mode in temp buffer, set local variables
  1480. ;; This is to support completion in interactive prompts
  1481. (let ((org-inhibit-startup t)) (org-mode))
  1482. ;; Interactive template entries
  1483. (goto-char (point-min))
  1484. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
  1485. (unless (org-capture-escaped-%)
  1486. (setq char (if (match-end 3) (match-string-no-properties 3))
  1487. prompt (if (match-end 2) (match-string-no-properties 2)))
  1488. (goto-char (match-beginning 0))
  1489. (replace-match "")
  1490. (setq completions nil default nil)
  1491. (when prompt
  1492. (setq completions (org-split-string prompt "|")
  1493. prompt (pop completions)
  1494. default (car completions)
  1495. histvar (intern (concat
  1496. "org-capture-template-prompt-history::"
  1497. (or prompt "")))
  1498. completions (mapcar 'list completions)))
  1499. (unless (boundp histvar) (set histvar nil))
  1500. (cond
  1501. ((member char '("G" "g"))
  1502. (let* ((org-last-tags-completion-table
  1503. (org-global-tags-completion-table
  1504. (if (equal char "G")
  1505. (org-agenda-files)
  1506. (and file (list file)))))
  1507. (org-add-colon-after-tag-completion t)
  1508. (ins (org-icompleting-read
  1509. (if prompt (concat prompt ": ") "Tags: ")
  1510. 'org-tags-completion-function nil nil nil
  1511. 'org-tags-history)))
  1512. (setq ins (mapconcat 'identity
  1513. (org-split-string
  1514. ins (org-re "[^[:alnum:]_@#%]+"))
  1515. ":"))
  1516. (when (string-match "\\S-" ins)
  1517. (or (equal (char-before) ?:) (insert ":"))
  1518. (insert ins)
  1519. (or (equal (char-after) ?:) (insert ":"))
  1520. (and (org-at-heading-p)
  1521. (let ((org-ignore-region t))
  1522. (org-set-tags nil 'align))))))
  1523. ((equal char "C")
  1524. (cond ((= (length clipboards) 1) (insert (car clipboards)))
  1525. ((> (length clipboards) 1)
  1526. (insert (read-string "Clipboard/kill value: "
  1527. (car clipboards) '(clipboards . 1)
  1528. (car clipboards))))))
  1529. ((equal char "L")
  1530. (cond ((= (length clipboards) 1)
  1531. (org-insert-link 0 (car clipboards)))
  1532. ((> (length clipboards) 1)
  1533. (org-insert-link 0 (read-string "Clipboard/kill value: "
  1534. (car clipboards)
  1535. '(clipboards . 1)
  1536. (car clipboards))))))
  1537. ((equal char "p")
  1538. (org-set-property (org-no-properties prompt) nil))
  1539. (char
  1540. ;; These are the date/time related ones
  1541. (setq org-time-was-given (equal (upcase char) char))
  1542. (setq time (org-read-date (equal (upcase char) char) t nil
  1543. prompt))
  1544. (if (equal (upcase char) char) (setq org-time-was-given t))
  1545. (org-insert-time-stamp time org-time-was-given
  1546. (member char '("u" "U"))
  1547. nil nil (list org-end-time-was-given)))
  1548. (t
  1549. (let (org-completion-use-ido)
  1550. (push (org-completing-read-no-i
  1551. (concat (if prompt prompt "Enter string")
  1552. (if default (concat " [" default "]"))
  1553. ": ")
  1554. completions nil nil nil histvar default)
  1555. strings)
  1556. (insert (car strings)))))))
  1557. ;; Replace %n escapes with nth %^{...} string
  1558. (setq strings (nreverse strings))
  1559. (goto-char (point-min))
  1560. (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
  1561. (unless (org-capture-escaped-%)
  1562. (replace-match
  1563. (nth (1- (string-to-number (match-string 1))) strings)
  1564. nil t)))
  1565. ;; Make sure there are no empty lines before the text, and that
  1566. ;; it ends with a newline character
  1567. (goto-char (point-min))
  1568. (while (looking-at "[ \t]*\n") (replace-match ""))
  1569. (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
  1570. ;; Return the expanded template and kill the temporary buffer
  1571. (untabify (point-min) (point-max))
  1572. (set-buffer-modified-p nil)
  1573. (prog1 (buffer-string) (kill-buffer (current-buffer))))))
  1574. (defun org-capture-escaped-% ()
  1575. "Check if % was escaped - if yes, unescape it now."
  1576. (if (equal (char-before (match-beginning 0)) ?\\)
  1577. (progn
  1578. (delete-region (1- (match-beginning 0)) (match-beginning 0))
  1579. t)
  1580. nil))
  1581. (defun org-capture-expand-embedded-elisp ()
  1582. "Evaluate embedded elisp %(sexp) and replace with the result."
  1583. (goto-char (point-min))
  1584. (while (re-search-forward "%(" nil t)
  1585. (unless (org-capture-escaped-%)
  1586. (goto-char (match-beginning 0))
  1587. (let ((template-start (point)))
  1588. (forward-char 1)
  1589. (let* ((sexp (read (current-buffer)))
  1590. (result (org-eval
  1591. (org-capture--expand-keyword-in-embedded-elisp sexp))))
  1592. (delete-region template-start (point))
  1593. (when result
  1594. (if (stringp result)
  1595. (insert result)
  1596. (error "Capture template sexp `%s' must evaluate to string or nil"
  1597. sexp))))))))
  1598. (defun org-capture--expand-keyword-in-embedded-elisp (attr)
  1599. "Recursively replace capture link keywords in ATTR sexp.
  1600. Such keywords are prefixed with \"%:\". See
  1601. `org-capture-template' for more information."
  1602. (cond ((consp attr)
  1603. (mapcar 'org-capture--expand-keyword-in-embedded-elisp attr))
  1604. ((symbolp attr)
  1605. (let* ((attr-symbol (symbol-name attr))
  1606. (key (and (string-match "%\\(:.*\\)" attr-symbol)
  1607. (intern (match-string 1 attr-symbol)))))
  1608. (or (plist-get org-store-link-plist key)
  1609. attr)))
  1610. (t attr)))
  1611. (defun org-capture-inside-embedded-elisp-p ()
  1612. "Return non-nil if point is inside of embedded elisp %(sexp)."
  1613. (let (beg end)
  1614. (with-syntax-table emacs-lisp-mode-syntax-table
  1615. (save-excursion
  1616. ;; `looking-at' and `search-backward' below do not match the "%(" if
  1617. ;; point is in its middle
  1618. (when (equal (char-before) ?%)
  1619. (backward-char))
  1620. (save-match-data
  1621. (when (or (looking-at "%(") (search-backward "%(" nil t))
  1622. (setq beg (point))
  1623. (setq end (progn (forward-char) (forward-sexp) (1- (point)))))))
  1624. (when (and beg end)
  1625. (and (<= (point) end) (>= (point) beg))))))
  1626. ;;;###autoload
  1627. (defun org-capture-import-remember-templates ()
  1628. "Set `org-capture-templates' to be similar to `org-remember-templates'."
  1629. (interactive)
  1630. (when (and (yes-or-no-p
  1631. "Import old remember templates into org-capture-templates? ")
  1632. (yes-or-no-p
  1633. "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
  1634. (require 'org-remember)
  1635. (setq org-capture-templates
  1636. (mapcar
  1637. (lambda (entry)
  1638. (let ((desc (car entry))
  1639. (key (char-to-string (nth 1 entry)))
  1640. (template (nth 2 entry))
  1641. (file (or (nth 3 entry) org-default-notes-file))
  1642. (position (or (nth 4 entry) org-remember-default-headline))
  1643. (type 'entry)
  1644. (prepend org-reverse-note-order)
  1645. immediate target jump-to-captured)
  1646. (cond
  1647. ((member position '(top bottom))
  1648. (setq target (list 'file file)
  1649. prepend (eq position 'top)))
  1650. ((eq position 'date-tree)
  1651. (setq target (list 'file+datetree file)
  1652. prepend nil))
  1653. (t (setq target (list 'file+headline file position))))
  1654. (when (string-match "%!" template)
  1655. (setq template (replace-match "" t t template)
  1656. immediate t))
  1657. (when (string-match "%&" template)
  1658. (setq jump-to-captured t))
  1659. (append (list key desc type target template)
  1660. (if prepend '(:prepend t))
  1661. (if immediate '(:immediate-finish t))
  1662. (if jump-to-captured '(:jump-to-captured t)))))
  1663. org-remember-templates))))
  1664. (provide 'org-capture)
  1665. ;;; org-capture.el ends here