org-exp.el 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364
  1. ;;; org-exp.el --- Export internals for Org-mode
  2. ;; Copyright (C) 2004-2013 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. ;;; Code:
  23. (require 'org)
  24. (require 'org-macs)
  25. (require 'org-agenda)
  26. (require 'ob-exp)
  27. (require 'org-src)
  28. (eval-when-compile
  29. (require 'cl))
  30. (declare-function org-export-latex-preprocess "org-latex" (parameters))
  31. (declare-function org-export-ascii-preprocess "org-ascii" (parameters))
  32. (declare-function org-export-html-preprocess "org-html" (parameters))
  33. (declare-function org-export-docbook-preprocess "org-docbook" (parameters))
  34. (declare-function org-infojs-options-inbuffer-template "org-jsinfo" ())
  35. (declare-function org-export-htmlize-region-for-paste "org-html" (beg end))
  36. (declare-function htmlize-buffer "ext:htmlize" (&optional buffer))
  37. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  38. (declare-function org-table-cookie-line-p "org-table" (line))
  39. (declare-function org-table-colgroup-line-p "org-table" (line))
  40. (declare-function org-pop-to-buffer-same-window "org-compat"
  41. (&optional buffer-or-name norecord label))
  42. (declare-function org-unescape-code-in-region "org-src" (beg end))
  43. (autoload 'org-export-generic "org-export-generic" "Export using the generic exporter" t)
  44. (autoload 'org-export-as-odt "org-odt"
  45. "Export the outline to a OpenDocument Text file." t)
  46. (autoload 'org-export-as-odt-and-open "org-odt"
  47. "Export the outline to a OpenDocument Text file and open it." t)
  48. (defgroup org-export nil
  49. "Options for exporting org-listings."
  50. :tag "Org Export"
  51. :group 'org)
  52. (defgroup org-export-general nil
  53. "General options for exporting Org-mode files."
  54. :tag "Org Export General"
  55. :group 'org-export)
  56. (defcustom org-export-allow-BIND 'confirm
  57. "Non-nil means allow #+BIND to define local variable values for export.
  58. This is a potential security risk, which is why the user must confirm the
  59. use of these lines."
  60. :group 'org-export-general
  61. :type '(choice
  62. (const :tag "Never" nil)
  63. (const :tag "Always" t)
  64. (const :tag "Make the user confirm for each file" confirm)))
  65. ;; FIXME
  66. (defvar org-export-publishing-directory nil)
  67. (defcustom org-export-show-temporary-export-buffer t
  68. "Non-nil means show buffer after exporting to temp buffer.
  69. When Org exports to a file, the buffer visiting that file is ever
  70. shown, but remains buried. However, when exporting to a temporary
  71. buffer, that buffer is popped up in a second window. When this variable
  72. is nil, the buffer remains buried also in these cases."
  73. :group 'org-export-general
  74. :type 'boolean)
  75. (defcustom org-export-copy-to-kill-ring 'if-interactive
  76. "Should we push exported content to the kill ring?"
  77. :group 'org-export-general
  78. :version "24.3"
  79. :type '(choice
  80. (const :tag "Always" t)
  81. (const :tag "When export is done interactively" if-interactive)
  82. (const :tag "Never" nil)))
  83. (defcustom org-export-kill-product-buffer-when-displayed nil
  84. "Non-nil means kill the product buffer if it is displayed immediately.
  85. This applied to the commands `org-export-as-html-and-open' and
  86. `org-export-as-pdf-and-open'."
  87. :group 'org-export-general
  88. :version "24.1"
  89. :type 'boolean)
  90. (defcustom org-export-run-in-background nil
  91. "Non-nil means export and publishing commands will run in background.
  92. This works by starting up a separate Emacs process visiting the same file
  93. and doing the export from there.
  94. Not all export commands are affected by this - only the ones which
  95. actually write to a file, and that do not depend on the buffer state.
  96. \\<org-mode-map>
  97. If this option is nil, you can still get background export by calling
  98. `org-export' with a double prefix arg: \
  99. \\[universal-argument] \\[universal-argument] \\[org-export].
  100. If this option is t, the double prefix can be used to exceptionally
  101. force an export command into the current process."
  102. :group 'org-export-general
  103. :type 'boolean)
  104. (defcustom org-export-initial-scope 'buffer
  105. "The initial scope when exporting with `org-export'.
  106. This variable can be either set to 'buffer or 'subtree."
  107. :group 'org-export-general
  108. :version "24.1"
  109. :type '(choice
  110. (const :tag "Export current buffer" 'buffer)
  111. (const :tag "Export current subtree" 'subtree)))
  112. (defcustom org-export-select-tags '("export")
  113. "Tags that select a tree for export.
  114. If any such tag is found in a buffer, all trees that do not carry one
  115. of these tags will be deleted before export.
  116. Inside trees that are selected like this, you can still deselect a
  117. subtree by tagging it with one of the `org-export-exclude-tags'."
  118. :group 'org-export-general
  119. :type '(repeat (string :tag "Tag")))
  120. (defcustom org-export-exclude-tags '("noexport")
  121. "Tags that exclude a tree from export.
  122. All trees carrying any of these tags will be excluded from export.
  123. This is without condition, so even subtrees inside that carry one of the
  124. `org-export-select-tags' will be removed."
  125. :group 'org-export-general
  126. :type '(repeat (string :tag "Tag")))
  127. ;; FIXME: rename, this is a general variable
  128. (defcustom org-export-html-expand t
  129. "Non-nil means for HTML export, treat @<...> as HTML tag.
  130. When nil, these tags will be exported as plain text and therefore
  131. not be interpreted by a browser.
  132. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  133. :group 'org-export-html
  134. :group 'org-export-general
  135. :type 'boolean)
  136. (defcustom org-export-with-special-strings t
  137. "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
  138. When this option is turned on, these strings will be exported as:
  139. Org HTML LaTeX
  140. -----+----------+--------
  141. \\- &shy; \\-
  142. -- &ndash; --
  143. --- &mdash; ---
  144. ... &hellip; \ldots
  145. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  146. :group 'org-export-translation
  147. :type 'boolean)
  148. (defcustom org-export-html-link-up ""
  149. "Where should the \"UP\" link of exported HTML pages lead?"
  150. :group 'org-export-html
  151. :group 'org-export-general
  152. :type '(string :tag "File or URL"))
  153. (defcustom org-export-html-link-home ""
  154. "Where should the \"HOME\" link of exported HTML pages lead?"
  155. :group 'org-export-html
  156. :group 'org-export-general
  157. :type '(string :tag "File or URL"))
  158. (defcustom org-export-language-setup
  159. '(("en" "Author" "Date" "Table of Contents" "Footnotes")
  160. ("ca" "Autor" "Data" "&Iacute;ndex" "Peus de p&agrave;gina")
  161. ("cs" "Autor" "Datum" "Obsah" "Pozn\xe1mky pod carou")
  162. ("da" "Ophavsmand" "Dato" "Indhold" "Fodnoter")
  163. ("de" "Autor" "Datum" "Inhaltsverzeichnis" "Fu&szlig;noten")
  164. ("eo" "A&#365;toro" "Dato" "Enhavo" "Piednotoj")
  165. ("es" "Autor" "Fecha" "&Iacute;ndice" "Pies de p&aacute;gina")
  166. ("fi" "Tekij&auml;" "P&auml;iv&auml;m&auml;&auml;r&auml;" "Sis&auml;llysluettelo" "Alaviitteet")
  167. ("fr" "Auteur" "Date" "Sommaire" "Notes de bas de page")
  168. ("hu" "Szerz&otilde;" "D&aacute;tum" "Tartalomjegyz&eacute;k" "L&aacute;bjegyzet")
  169. ("is" "H&ouml;fundur" "Dagsetning" "Efnisyfirlit" "Aftanm&aacute;lsgreinar")
  170. ("it" "Autore" "Data" "Indice" "Note a pi&egrave; di pagina")
  171. ;; Use numeric character entities for proper rendering of non-UTF8 documents
  172. ;; ("ja" "著者" "日付" "目次" "脚注")
  173. ("ja" "&#33879;&#32773;" "&#26085;&#20184;" "&#30446;&#27425;" "&#33050;&#27880;")
  174. ("nl" "Auteur" "Datum" "Inhoudsopgave" "Voetnoten")
  175. ("no" "Forfatter" "Dato" "Innhold" "Fotnoter")
  176. ("nb" "Forfatter" "Dato" "Innhold" "Fotnoter") ;; nb = Norsk (bokm.l)
  177. ("nn" "Forfattar" "Dato" "Innhald" "Fotnotar") ;; nn = Norsk (nynorsk)
  178. ("pl" "Autor" "Data" "Spis tre&#x015b;ci" "Przypis")
  179. ;; Use numeric character entities for proper rendering of non-UTF8 documents
  180. ;; ("ru" "Автор" "Дата" "Содержание" "Сноски")
  181. ("ru" "&#1040;&#1074;&#1090;&#1086;&#1088;" "&#1044;&#1072;&#1090;&#1072;" "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;" "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;")
  182. ("sv" "F&ouml;rfattare" "Datum" "Inneh&aring;ll" "Fotnoter")
  183. ;; Use numeric character entities for proper rendering of non-UTF8 documents
  184. ;; ("uk" "Автор" "Дата" "Зміст" "Примітки")
  185. ("uk" "&#1040;&#1074;&#1090;&#1086;&#1088;" "&#1044;&#1072;&#1090;&#1072;" "&#1047;&#1084;&#1110;&#1089;&#1090;" "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;")
  186. ;; Use numeric character entities for proper rendering of non-UTF8 documents
  187. ;; ("zh-CN" "作者" "日期" "目录" "脚注")
  188. ("zh-CN" "&#20316;&#32773;" "&#26085;&#26399;" "&#30446;&#24405;" "&#33050;&#27880;")
  189. ;; Use numeric character entities for proper rendering of non-UTF8 documents
  190. ;; ("zh-TW" "作者" "日期" "目錄" "腳註")
  191. ("zh-TW" "&#20316;&#32773;" "&#26085;&#26399;" "&#30446;&#37636;" "&#33139;&#35387;"))
  192. "Terms used in export text, translated to different languages.
  193. Use the variable `org-export-default-language' to set the language,
  194. or use the +OPTION lines for a per-file setting."
  195. :group 'org-export-general
  196. :type '(repeat
  197. (list
  198. (string :tag "HTML language tag")
  199. (string :tag "Author")
  200. (string :tag "Date")
  201. (string :tag "Table of Contents")
  202. (string :tag "Footnotes"))))
  203. (defcustom org-export-default-language "en"
  204. "The default language for export and clocktable translations, as a string.
  205. This should have an association in `org-export-language-setup'
  206. and in `org-clock-clocktable-language-setup'."
  207. :group 'org-export-general
  208. :type 'string)
  209. (defcustom org-export-date-timestamp-format "%Y-%m-%d"
  210. "Time string format for Org timestamps in the #+DATE option."
  211. :group 'org-export-general
  212. :version "24.1"
  213. :type 'string)
  214. (defvar org-export-page-description ""
  215. "The page description, for the XHTML meta tag.
  216. This is best set with the #+DESCRIPTION line in a file, it does not make
  217. sense to set this globally.")
  218. (defvar org-export-page-keywords ""
  219. "The page description, for the XHTML meta tag.
  220. This is best set with the #+KEYWORDS line in a file, it does not make
  221. sense to set this globally.")
  222. (defcustom org-export-skip-text-before-1st-heading nil
  223. "Non-nil means skip all text before the first headline when exporting.
  224. When nil, that text is exported as well."
  225. :group 'org-export-general
  226. :type 'boolean)
  227. (defcustom org-export-headline-levels 3
  228. "The last level which is still exported as a headline.
  229. Inferior levels will produce itemize lists when exported.
  230. Note that a numeric prefix argument to an exporter function overrides
  231. this setting.
  232. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  233. :group 'org-export-general
  234. :type 'integer)
  235. (defcustom org-export-with-section-numbers t
  236. "Non-nil means add section numbers to headlines when exporting.
  237. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  238. :group 'org-export-general
  239. :type 'boolean)
  240. (defcustom org-export-section-number-format '((("1" ".")) . "")
  241. "Format of section numbers for export.
  242. The variable has two components.
  243. 1. A list of lists, each indicating a counter type and a separator.
  244. The counter type can be any of \"1\", \"A\", \"a\", \"I\", or \"i\".
  245. It causes causes numeric, alphabetic, or roman counters, respectively.
  246. The separator is only used if another counter for a subsection is being
  247. added.
  248. If there are more numbered section levels than entries in this lists,
  249. then the last entry will be reused.
  250. 2. A terminator string that will be added after the entire
  251. section number."
  252. :group 'org-export-general
  253. :type '(cons
  254. (repeat
  255. (list
  256. (string :tag "Counter Type")
  257. (string :tag "Separator ")))
  258. (string :tag "Terminator")))
  259. (defcustom org-export-with-toc t
  260. "Non-nil means create a table of contents in exported files.
  261. The TOC contains headlines with levels up to`org-export-headline-levels'.
  262. When an integer, include levels up to N in the toc, this may then be
  263. different from `org-export-headline-levels', but it will not be allowed
  264. to be larger than the number of headline levels.
  265. When nil, no table of contents is made.
  266. Headlines which contain any TODO items will be marked with \"(*)\" in
  267. ASCII export, and with red color in HTML output, if the option
  268. `org-export-mark-todo-in-toc' is set.
  269. In HTML output, the TOC will be clickable.
  270. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  271. or \"toc:3\"."
  272. :group 'org-export-general
  273. :type '(choice
  274. (const :tag "No Table of Contents" nil)
  275. (const :tag "Full Table of Contents" t)
  276. (integer :tag "TOC to level")))
  277. (defcustom org-export-mark-todo-in-toc nil
  278. "Non-nil means mark TOC lines that contain any open TODO items."
  279. :group 'org-export-general
  280. :type 'boolean)
  281. (defcustom org-export-with-todo-keywords t
  282. "Non-nil means include TODO keywords in export.
  283. When nil, remove all these keywords from the export."
  284. :group 'org-export-general
  285. :type 'boolean)
  286. (defcustom org-export-with-tasks t
  287. "Non-nil means include TODO items for export.
  288. This may have the following values:
  289. t include tasks independent of state.
  290. todo include only tasks that are not yet done.
  291. done include only tasks that are already done.
  292. nil remove all tasks before export
  293. list of TODO kwds keep only tasks with these keywords"
  294. :group 'org-export-general
  295. :version "24.1"
  296. :type '(choice
  297. (const :tag "All tasks" t)
  298. (const :tag "No tasks" nil)
  299. (const :tag "Not-done tasks" todo)
  300. (const :tag "Only done tasks" done)
  301. (repeat :tag "Specific TODO keywords"
  302. (string :tag "Keyword"))))
  303. (defcustom org-export-with-priority nil
  304. "Non-nil means include priority cookies in export.
  305. When nil, remove priority cookies for export."
  306. :group 'org-export-general
  307. :type 'boolean)
  308. (defcustom org-export-preserve-breaks nil
  309. "Non-nil means preserve all line breaks when exporting.
  310. Normally, in HTML output paragraphs will be reformatted. In ASCII
  311. export, line breaks will always be preserved, regardless of this variable.
  312. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  313. :group 'org-export-general
  314. :type 'boolean)
  315. (defcustom org-export-with-archived-trees 'headline
  316. "Whether subtrees with the ARCHIVE tag should be exported.
  317. This can have three different values
  318. nil Do not export, pretend this tree is not present
  319. t Do export the entire tree
  320. headline Only export the headline, but skip the tree below it."
  321. :group 'org-export-general
  322. :group 'org-archive
  323. :type '(choice
  324. (const :tag "not at all" nil)
  325. (const :tag "headline only" 'headline)
  326. (const :tag "entirely" t)))
  327. (defcustom org-export-author-info t
  328. "Non-nil means insert author name and email into the exported file.
  329. This option can also be set with the +OPTIONS line,
  330. e.g. \"author:nil\"."
  331. :group 'org-export-general
  332. :type 'boolean)
  333. (defcustom org-export-email-info nil
  334. "Non-nil means insert author name and email into the exported file.
  335. This option can also be set with the +OPTIONS line,
  336. e.g. \"email:t\"."
  337. :group 'org-export-general
  338. :version "24.1"
  339. :type 'boolean)
  340. (defcustom org-export-creator-info t
  341. "Non-nil means the postamble should contain a creator sentence.
  342. This sentence is \"HTML generated by org-mode XX in emacs XXX\"."
  343. :group 'org-export-general
  344. :type 'boolean)
  345. (defcustom org-export-time-stamp-file t
  346. "Non-nil means insert a time stamp into the exported file.
  347. The time stamp shows when the file was created.
  348. This option can also be set with the +OPTIONS line,
  349. e.g. \"timestamp:nil\"."
  350. :group 'org-export-general
  351. :type 'boolean)
  352. (defcustom org-export-with-timestamps t
  353. "If nil, do not export time stamps and associated keywords."
  354. :group 'org-export-general
  355. :type 'boolean)
  356. (defcustom org-export-remove-timestamps-from-toc t
  357. "If t, remove timestamps from the table of contents entries."
  358. :group 'org-export-general
  359. :type 'boolean)
  360. (defcustom org-export-with-tags 'not-in-toc
  361. "If nil, do not export tags, just remove them from headlines.
  362. If this is the symbol `not-in-toc', tags will be removed from table of
  363. contents entries, but still be shown in the headlines of the document.
  364. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  365. :group 'org-export-general
  366. :type '(choice
  367. (const :tag "Off" nil)
  368. (const :tag "Not in TOC" not-in-toc)
  369. (const :tag "On" t)))
  370. (defcustom org-export-with-drawers nil
  371. "Non-nil means export with drawers like the property drawer.
  372. When t, all drawers are exported. This may also be a list of
  373. drawer names to export."
  374. :group 'org-export-general
  375. :type '(choice
  376. (const :tag "All drawers" t)
  377. (const :tag "None" nil)
  378. (repeat :tag "Selected drawers"
  379. (string :tag "Drawer name"))))
  380. (defvar org-export-first-hook nil
  381. "Hook called as the first thing in each exporter.
  382. Point will be still in the original buffer.
  383. Good for general initialization")
  384. (defvar org-export-preprocess-hook nil
  385. "Hook for preprocessing an export buffer.
  386. Pretty much the first thing when exporting is running this hook.
  387. Point will be in a temporary buffer that contains a copy of
  388. the original buffer, or of the section that is being exported.
  389. All the other hooks in the org-export-preprocess... category
  390. also work in that temporary buffer, already modified by various
  391. stages of the processing.")
  392. (defvar org-export-preprocess-after-include-files-hook nil
  393. "Hook for preprocessing an export buffer.
  394. This is run after the contents of included files have been inserted.")
  395. (defvar org-export-preprocess-after-tree-selection-hook nil
  396. "Hook for preprocessing an export buffer.
  397. This is run after selection of trees to be exported has happened.
  398. This selection includes tags-based selection, as well as removal
  399. of commented and archived trees.")
  400. (defvar org-export-preprocess-after-headline-targets-hook nil
  401. "Hook for preprocessing export buffer.
  402. This is run just after the headline targets have been defined and
  403. the target-alist has been set up.")
  404. (defvar org-export-preprocess-before-selecting-backend-code-hook nil
  405. "Hook for preprocessing an export buffer.
  406. This is run just before backend-specific blocks get selected.")
  407. (defvar org-export-preprocess-after-blockquote-hook nil
  408. "Hook for preprocessing an export buffer.
  409. This is run after blockquote/quote/verse/center have been marked
  410. with cookies.")
  411. (defvar org-export-preprocess-after-radio-targets-hook nil
  412. "Hook for preprocessing an export buffer.
  413. This is run after radio target processing.")
  414. (defvar org-export-preprocess-before-normalizing-links-hook nil
  415. "Hook for preprocessing an export buffer.
  416. This hook is run before links are normalized.")
  417. (defvar org-export-preprocess-before-backend-specifics-hook nil
  418. "Hook run before backend-specific functions are called during preprocessing.")
  419. (defvar org-export-preprocess-final-hook nil
  420. "Hook for preprocessing an export buffer.
  421. This is run as the last thing in the preprocessing buffer, just before
  422. returning the buffer string to the backend.")
  423. (defgroup org-export-translation nil
  424. "Options for translating special ascii sequences for the export backends."
  425. :tag "Org Export Translation"
  426. :group 'org-export)
  427. (defcustom org-export-with-emphasize t
  428. "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
  429. If the export target supports emphasizing text, the word will be
  430. typeset in bold, italic, or underlined, respectively. Works only for
  431. single words, but you can say: I *really* *mean* *this*.
  432. Not all export backends support this.
  433. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  434. :group 'org-export-translation
  435. :type 'boolean)
  436. (defcustom org-export-with-footnotes t
  437. "If nil, export [1] as a footnote marker.
  438. Lines starting with [1] will be formatted as footnotes.
  439. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  440. :group 'org-export-translation
  441. :type 'boolean)
  442. (defcustom org-export-with-TeX-macros t
  443. "Non-nil means interpret simple TeX-like macros when exporting.
  444. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  445. Not only real TeX macros will work here, but the standard HTML entities
  446. for math can be used as macro names as well. For a list of supported
  447. names in HTML export, see the constant `org-entities' and the user option
  448. `org-entities-user'.
  449. Not all export backends support this.
  450. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  451. :group 'org-export-translation
  452. :group 'org-export-latex
  453. :type 'boolean)
  454. (defcustom org-export-with-LaTeX-fragments t
  455. "Non-nil means process LaTeX math fragments for HTML display.
  456. When set, the exporter will find and process LaTeX environments if the
  457. \\begin line is the first non-white thing on a line. It will also find
  458. and process the math delimiters like $a=b$ and \\( a=b \\) for inline math,
  459. $$a=b$$ and \\=\\[ a=b \\] for display math.
  460. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\".
  461. Allowed values are:
  462. nil Don't do anything.
  463. verbatim Keep everything in verbatim
  464. dvipng Process the LaTeX fragments to images.
  465. This will also include processing of non-math environments.
  466. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  467. to convert pdf files to png files.
  468. t Do MathJax preprocessing if there is at least on math snippet,
  469. and arrange for MathJax.js to be loaded.
  470. The default is nil, because this option needs the `dvipng' program which
  471. is not available on all systems."
  472. :group 'org-export-translation
  473. :group 'org-export-latex
  474. :type '(choice
  475. (const :tag "Do not process math in any way" nil)
  476. (const :tag "Obsolete, use dvipng setting" t)
  477. (const :tag "Use dvipng to make images" dvipng)
  478. (const :tag "Use imagemagick to make images" imagemagick)
  479. (const :tag "Use MathJax to display math" mathjax)
  480. (const :tag "Leave math verbatim" verbatim)))
  481. (defcustom org-export-with-fixed-width t
  482. "Non-nil means lines starting with \":\" will be in fixed width font.
  483. This can be used to have pre-formatted text, fragments of code etc. For
  484. example:
  485. : ;; Some Lisp examples
  486. : (while (defc cnt)
  487. : (ding))
  488. will be looking just like this in also HTML. See also the QUOTE keyword.
  489. Not all export backends support this.
  490. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  491. :group 'org-export-translation
  492. :type 'boolean)
  493. (defgroup org-export-tables nil
  494. "Options for exporting tables in Org-mode."
  495. :tag "Org Export Tables"
  496. :group 'org-export)
  497. (defcustom org-export-with-tables t
  498. "If non-nil, lines starting with \"|\" define a table.
  499. For example:
  500. | Name | Address | Birthday |
  501. |-------------+----------+-----------|
  502. | Arthur Dent | England | 29.2.2100 |
  503. Not all export backends support this.
  504. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  505. :group 'org-export-tables
  506. :type 'boolean)
  507. (defcustom org-export-highlight-first-table-line t
  508. "Non-nil means highlight the first table line.
  509. In HTML export, this means use <th> instead of <td>.
  510. In tables created with table.el, this applies to the first table line.
  511. In Org-mode tables, all lines before the first horizontal separator
  512. line will be formatted with <th> tags."
  513. :group 'org-export-tables
  514. :type 'boolean)
  515. (defcustom org-export-table-remove-special-lines t
  516. "Remove special lines and marking characters in calculating tables.
  517. This removes the special marking character column from tables that are set
  518. up for spreadsheet calculations. It also removes the entire lines
  519. marked with `!', `_', or `^'. The lines with `$' are kept, because
  520. the values of constants may be useful to have."
  521. :group 'org-export-tables
  522. :type 'boolean)
  523. (defcustom org-export-table-remove-empty-lines t
  524. "Remove empty lines when exporting tables.
  525. This is the global equivalent of the :remove-nil-lines option
  526. when locally sending a table with #+ORGTBL."
  527. :group 'org-export-tables
  528. :version "24.1"
  529. :type 'boolean)
  530. (defcustom org-export-prefer-native-exporter-for-tables nil
  531. "Non-nil means always export tables created with table.el natively.
  532. Natively means use the HTML code generator in table.el.
  533. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  534. the table does not use row- or column-spanning). This has the
  535. advantage, that the automatic HTML conversions for math symbols and
  536. sub/superscripts can be applied. Org-mode's HTML generator is also
  537. much faster. The LaTeX exporter always use the native exporter for
  538. table.el tables."
  539. :group 'org-export-tables
  540. :type 'boolean)
  541. ;;;; Exporting
  542. ;;; Variables, constants, and parameter plists
  543. (defconst org-level-max 20)
  544. (defvar org-export-current-backend nil
  545. "During export, this will be bound to a symbol such as 'html,
  546. 'latex, 'docbook, 'ascii, etc, indicating which of the export
  547. backends is in use. Otherwise it has the value nil. Users
  548. should not attempt to change the value of this variable
  549. directly, but it can be used in code to test whether export is
  550. in progress, and if so, what the backend is.")
  551. (defvar org-current-export-file nil) ; dynamically scoped parameter
  552. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  553. (defvar org-export-opt-plist nil
  554. "Contains the current option plist.")
  555. (defvar org-last-level nil) ; dynamically scoped variable
  556. (defvar org-min-level nil) ; dynamically scoped variable
  557. (defvar org-levels-open nil) ; dynamically scoped parameter
  558. (defvar org-export-footnotes-data nil
  559. "Alist of labels used in buffers, along with their definition.")
  560. (defvar org-export-footnotes-seen nil
  561. "Alist of labels encountered so far by the exporter, along with their definition.")
  562. (defconst org-export-plist-vars
  563. '((:link-up nil org-export-html-link-up)
  564. (:link-home nil org-export-html-link-home)
  565. (:language nil org-export-default-language)
  566. (:keywords nil org-export-page-keywords)
  567. (:description nil org-export-page-description)
  568. (:customtime nil org-display-custom-times)
  569. (:headline-levels "H" org-export-headline-levels)
  570. (:section-numbers "num" org-export-with-section-numbers)
  571. (:section-number-format nil org-export-section-number-format)
  572. (:table-of-contents "toc" org-export-with-toc)
  573. (:preserve-breaks "\\n" org-export-preserve-breaks)
  574. (:archived-trees nil org-export-with-archived-trees)
  575. (:emphasize "*" org-export-with-emphasize)
  576. (:sub-superscript "^" org-export-with-sub-superscripts)
  577. (:special-strings "-" org-export-with-special-strings)
  578. (:footnotes "f" org-export-with-footnotes)
  579. (:drawers "d" org-export-with-drawers)
  580. (:tags "tags" org-export-with-tags)
  581. (:todo-keywords "todo" org-export-with-todo-keywords)
  582. (:tasks "tasks" org-export-with-tasks)
  583. (:priority "pri" org-export-with-priority)
  584. (:TeX-macros "TeX" org-export-with-TeX-macros)
  585. (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments)
  586. (:latex-listings nil org-export-latex-listings)
  587. (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading)
  588. (:fixed-width ":" org-export-with-fixed-width)
  589. (:timestamps "<" org-export-with-timestamps)
  590. (:author nil user-full-name)
  591. (:email nil user-mail-address)
  592. (:author-info "author" org-export-author-info)
  593. (:email-info "email" org-export-email-info)
  594. (:creator-info "creator" org-export-creator-info)
  595. (:time-stamp-file "timestamp" org-export-time-stamp-file)
  596. (:tables "|" org-export-with-tables)
  597. (:table-auto-headline nil org-export-highlight-first-table-line)
  598. (:style-include-default nil org-export-html-style-include-default)
  599. (:style-include-scripts nil org-export-html-style-include-scripts)
  600. (:style nil org-export-html-style)
  601. (:style-extra nil org-export-html-style-extra)
  602. (:agenda-style nil org-agenda-export-html-style)
  603. (:convert-org-links nil org-export-html-link-org-files-as-html)
  604. (:html-inline-images nil org-export-html-inline-images)
  605. (:latex-inline-images nil org-export-latex-inline-images)
  606. (:odt-inline-images nil org-export-odt-inline-images)
  607. (:docbook-inline-images nil org-export-docbook-inline-images)
  608. (:html-extension nil org-export-html-extension)
  609. (:html-preamble nil org-export-html-preamble)
  610. (:html-postamble nil org-export-html-postamble)
  611. (:xml-declaration nil org-export-html-xml-declaration)
  612. (:html-table-tag nil org-export-html-table-tag)
  613. (:expand-quoted-html "@" org-export-html-expand)
  614. (:timestamp nil org-export-html-with-timestamp)
  615. (:publishing-directory nil org-export-publishing-directory)
  616. (:select-tags nil org-export-select-tags)
  617. (:exclude-tags nil org-export-exclude-tags)
  618. (:latex-image-options nil org-export-latex-image-default-option))
  619. "List of properties that represent export/publishing variables.
  620. Each element is a list of 3 items:
  621. 1. The property that is used internally, and also for org-publish-project-alist
  622. 2. The string that can be used in the OPTION lines to set this option,
  623. or nil if this option cannot be changed in this way
  624. 3. The customization variable that sets the default for this option."
  625. )
  626. (defun org-default-export-plist ()
  627. "Return the property list with default settings for the export variables."
  628. (let* ((infile (org-infile-export-plist))
  629. (letbind (plist-get infile :let-bind))
  630. (l org-export-plist-vars) rtn e s v)
  631. (while (setq e (pop l))
  632. (setq s (nth 2 e)
  633. v (cond
  634. ((assq s letbind) (nth 1 (assq s letbind)))
  635. ((boundp s) (symbol-value s)))
  636. rtn (cons (car e) (cons v rtn))))
  637. rtn))
  638. (defvar org-export-inbuffer-options-extra nil
  639. "List of additional in-buffer options that should be detected.
  640. Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
  641. etc. Extensions can add to this list to get their options detected, and they
  642. can then add a function to `org-export-options-filters' to process these
  643. options.
  644. Each element in this list must be a list, with the in-buffer keyword as car,
  645. and a property (a symbol) as the next element. All occurrences of the
  646. keyword will be found, the values concatenated with a space character
  647. in between, and the result stored in the export options property list.")
  648. (defvar org-export-options-filters nil
  649. "Functions to be called to finalize the export/publishing options.
  650. All these options are stored in a property list, and each of the functions
  651. in this hook gets a chance to modify this property list. Each function
  652. must accept the property list as an argument, and must return the (possibly
  653. modified) list.")
  654. ;; FIXME: should we fold case here?
  655. (defun org-infile-export-plist ()
  656. "Return the property list with file-local settings for export."
  657. (save-excursion
  658. (save-restriction
  659. (widen)
  660. (goto-char (point-min))
  661. (let ((re (org-make-options-regexp
  662. (append
  663. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
  664. "MATHJAX"
  665. "LINK_UP" "LINK_HOME" "SETUPFILE" "STYLE"
  666. "LATEX_HEADER" "LATEX_CLASS" "LATEX_CLASS_OPTIONS"
  667. "EXPORT_SELECT_TAGS" "EXPORT_EXCLUDE_TAGS"
  668. "KEYWORDS" "DESCRIPTION" "MACRO" "BIND" "XSLT")
  669. (mapcar 'car org-export-inbuffer-options-extra))))
  670. (case-fold-search t)
  671. p key val text options mathjax a pr style
  672. latex-header latex-class latex-class-options macros letbind
  673. ext-setup-or-nil setup-file setup-dir setup-contents (start 0))
  674. (while (or (and ext-setup-or-nil
  675. (string-match re ext-setup-or-nil start)
  676. (setq start (match-end 0)))
  677. (and (setq ext-setup-or-nil nil start 0)
  678. (re-search-forward re nil t)))
  679. (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
  680. val (org-match-string-no-properties 2 ext-setup-or-nil))
  681. (cond
  682. ((setq a (assoc key org-export-inbuffer-options-extra))
  683. (setq pr (nth 1 a))
  684. (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
  685. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  686. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  687. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  688. ((string-equal key "DATE")
  689. ;; If date is an Org timestamp, convert it to a time
  690. ;; string using `org-export-date-timestamp-format'
  691. (when (string-match org-ts-regexp3 val)
  692. (setq val (format-time-string
  693. org-export-date-timestamp-format
  694. (apply 'encode-time (org-parse-time-string
  695. (match-string 0 val))))))
  696. (setq p (plist-put p :date val)))
  697. ((string-equal key "KEYWORDS") (setq p (plist-put p :keywords val)))
  698. ((string-equal key "DESCRIPTION")
  699. (setq p (plist-put p :description val)))
  700. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  701. ((string-equal key "STYLE")
  702. (setq style (concat style "\n" val)))
  703. ((string-equal key "LATEX_HEADER")
  704. (setq latex-header (concat latex-header "\n" val)))
  705. ((string-equal key "LATEX_CLASS")
  706. (setq latex-class val))
  707. ((string-equal key "LATEX_CLASS_OPTIONS")
  708. (setq latex-class-options val))
  709. ((string-equal key "TEXT")
  710. (setq text (if text (concat text "\n" val) val)))
  711. ((string-equal key "OPTIONS")
  712. (setq options (concat val " " options)))
  713. ((string-equal key "MATHJAX")
  714. (setq mathjax (concat val " " mathjax)))
  715. ((string-equal key "BIND")
  716. (push (read (concat "(" val ")")) letbind))
  717. ((string-equal key "XSLT")
  718. (setq p (plist-put p :xslt val)))
  719. ((string-equal key "LINK_UP")
  720. (setq p (plist-put p :link-up val)))
  721. ((string-equal key "LINK_HOME")
  722. (setq p (plist-put p :link-home val)))
  723. ((string-equal key "EXPORT_SELECT_TAGS")
  724. (setq p (plist-put p :select-tags (org-split-string val))))
  725. ((string-equal key "EXPORT_EXCLUDE_TAGS")
  726. (setq p (plist-put p :exclude-tags (org-split-string val))))
  727. ((string-equal key "MACRO")
  728. (push val macros))
  729. ((equal key "SETUPFILE")
  730. (setq setup-file (org-remove-double-quotes (org-trim val))
  731. ;; take care of recursive inclusion of setupfiles
  732. setup-file (if (or (file-name-absolute-p val) (not setup-dir))
  733. (expand-file-name setup-file)
  734. (let ((default-directory setup-dir))
  735. (expand-file-name setup-file))))
  736. (setq setup-dir (file-name-directory setup-file))
  737. (setq setup-contents (org-file-contents setup-file 'noerror))
  738. (if (not ext-setup-or-nil)
  739. (setq ext-setup-or-nil setup-contents start 0)
  740. (setq ext-setup-or-nil
  741. (concat (substring ext-setup-or-nil 0 start)
  742. "\n" setup-contents "\n"
  743. (substring ext-setup-or-nil start)))))))
  744. (setq p (plist-put p :text text))
  745. (when (and letbind (org-export-confirm-letbind))
  746. (setq p (plist-put p :let-bind letbind)))
  747. (when style (setq p (plist-put p :style-extra style)))
  748. (when latex-header
  749. (setq p (plist-put p :latex-header-extra (substring latex-header 1))))
  750. (when latex-class
  751. (setq p (plist-put p :latex-class latex-class)))
  752. (when latex-class-options
  753. (setq p (plist-put p :latex-class-options latex-class-options)))
  754. (when options
  755. (setq p (org-export-add-options-to-plist p options)))
  756. (when mathjax
  757. (setq p (plist-put p :mathjax mathjax)))
  758. ;; Add macro definitions
  759. (setq p (plist-put p :macro-date "(eval (format-time-string \"$1\"))"))
  760. (setq p (plist-put p :macro-time "(eval (format-time-string \"$1\"))"))
  761. (setq p (plist-put p :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"))
  762. (setq p (plist-put
  763. p :macro-modification-time
  764. (and (buffer-file-name)
  765. (file-exists-p (buffer-file-name))
  766. (concat
  767. "(eval (format-time-string \"$1\" '"
  768. (prin1-to-string (nth 5 (file-attributes
  769. (buffer-file-name))))
  770. "))"))))
  771. (setq p (plist-put p :macro-input-file (and (buffer-file-name)
  772. (file-name-nondirectory
  773. (buffer-file-name)))))
  774. (while (setq val (pop macros))
  775. (when (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)" val)
  776. (setq p (plist-put
  777. p (intern
  778. (concat ":macro-" (downcase (match-string 1 val))))
  779. (org-export-interpolate-newlines (match-string 2 val))))))
  780. p))))
  781. (defun org-export-interpolate-newlines (s)
  782. (while (string-match "\\\\n" s)
  783. (setq s (replace-match "\n" t t s)))
  784. s)
  785. (defvar org-export-allow-BIND-local nil)
  786. (defun org-export-confirm-letbind ()
  787. "Can we use #+BIND values during export?
  788. By default this will ask for confirmation by the user, to divert possible
  789. security risks."
  790. (cond
  791. ((not org-export-allow-BIND) nil)
  792. ((eq org-export-allow-BIND t) t)
  793. ((local-variable-p 'org-export-allow-BIND-local (current-buffer))
  794. org-export-allow-BIND-local)
  795. (t (org-set-local 'org-export-allow-BIND-local
  796. (yes-or-no-p "Allow BIND values in this buffer? ")))))
  797. (defun org-install-letbind ()
  798. "Install the values from #+BIND lines as local variables."
  799. (let ((letbind (plist-get org-export-opt-plist :let-bind))
  800. pair)
  801. (while (setq pair (pop letbind))
  802. (org-set-local (car pair) (nth 1 pair)))))
  803. (defun org-export-add-options-to-plist (p options)
  804. "Parse an OPTIONS line and set values in the property list P."
  805. (let (o)
  806. (when options
  807. (let ((op org-export-plist-vars))
  808. (while (setq o (pop op))
  809. (if (and (nth 1 o)
  810. (string-match (concat "\\(\\`\\|[ \t]\\)"
  811. (regexp-quote (nth 1 o))
  812. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  813. options))
  814. (setq p (plist-put p (car o)
  815. (car (read-from-string
  816. (match-string 2 options))))))))))
  817. p)
  818. (defun org-export-add-subtree-options (p pos)
  819. "Add options in subtree at position POS to property list P."
  820. (save-excursion
  821. (goto-char pos)
  822. (when (org-at-heading-p)
  823. (let (a)
  824. ;; This is actually read in `org-export-get-title-from-subtree'
  825. ;; (when (setq a (org-entry-get pos "EXPORT_TITLE"))
  826. ;; (setq p (plist-put p :title a)))
  827. (when (setq a (org-entry-get pos "EXPORT_TEXT"))
  828. (setq p (plist-put p :text a)))
  829. (when (setq a (org-entry-get pos "EXPORT_AUTHOR"))
  830. (setq p (plist-put p :author a)))
  831. (when (setq a (org-entry-get pos "EXPORT_DATE"))
  832. (setq p (plist-put p :date a)))
  833. (when (setq a (org-entry-get pos "EXPORT_OPTIONS"))
  834. (setq p (org-export-add-options-to-plist p a)))))
  835. p))
  836. (defun org-export-directory (type plist)
  837. (let* ((val (plist-get plist :publishing-directory))
  838. (dir (if (listp val)
  839. (or (cdr (assoc type val)) ".")
  840. val)))
  841. dir))
  842. (defun org-export-process-option-filters (plist)
  843. (let ((functions org-export-options-filters) f)
  844. (while (setq f (pop functions))
  845. (setq plist (funcall f plist))))
  846. plist)
  847. ;;;###autoload
  848. (defun org-export (&optional arg)
  849. "Export dispatcher for Org-mode.
  850. When `org-export-run-in-background' is non-nil, try to run the command
  851. in the background. This will be done only for commands that write
  852. to a file. For details see the docstring of `org-export-run-in-background'.
  853. The prefix argument ARG will be passed to the exporter. However, if
  854. ARG is a double universal prefix \\[universal-argument] \\[universal-argument], \
  855. that means to inverse the
  856. value of `org-export-run-in-background'.
  857. If `org-export-initial-scope' is set to 'subtree, try to export
  858. the current subtree, otherwise try to export the whole buffer.
  859. Pressing `1' will switch between these two options."
  860. (interactive "P")
  861. (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
  862. (subtree-p (or (org-region-active-p)
  863. (eq org-export-initial-scope 'subtree)))
  864. (regb (and (org-region-active-p) (region-beginning)))
  865. (rege (and (org-region-active-p) (region-end)))
  866. (help "[t] insert the export option template
  867. \[v] limit export to visible part of outline tree
  868. \[1] switch buffer/subtree export
  869. \[SPC] publish enclosing subtree (with LaTeX_CLASS or EXPORT_FILE_NAME prop)
  870. \[a/n/u] export as ASCII/Latin-1/UTF-8 [A/N/U] to temporary buffer
  871. \[h] export as HTML [H] to temporary buffer [R] export region
  872. \[b] export as HTML and open in browser
  873. \[l] export as LaTeX [L] to temporary buffer
  874. \[p] export as LaTeX and process to PDF [d] ... and open PDF file
  875. \[D] export as DocBook [V] export as DocBook, process to PDF, and open
  876. \[o] export as OpenDocument Text [O] ... and open
  877. \[j] export as TaskJuggler [J] ... and open
  878. \[m] export as Freemind mind map
  879. \[x] export as XOXO
  880. \[g] export using Wes Hardaker's generic exporter
  881. \[i] export current file as iCalendar file
  882. \[I] export all agenda files as iCalendar files [c] ...as one combined file
  883. \[F] publish current file [P] publish current project
  884. \[X] publish a project... [E] publish every projects")
  885. (cmds
  886. '((?t org-insert-export-options-template nil)
  887. (?v org-export-visible nil)
  888. (?a org-export-as-ascii t)
  889. (?A org-export-as-ascii-to-buffer t)
  890. (?n org-export-as-latin1 t)
  891. (?N org-export-as-latin1-to-buffer t)
  892. (?u org-export-as-utf8 t)
  893. (?U org-export-as-utf8-to-buffer t)
  894. (?h org-export-as-html t)
  895. (?b org-export-as-html-and-open t)
  896. (?H org-export-as-html-to-buffer nil)
  897. (?R org-export-region-as-html nil)
  898. (?x org-export-as-xoxo t)
  899. (?g org-export-generic t)
  900. (?D org-export-as-docbook t)
  901. (?V org-export-as-docbook-pdf-and-open t)
  902. (?o org-export-as-odt t)
  903. (?O org-export-as-odt-and-open t)
  904. (?j org-export-as-taskjuggler t)
  905. (?J org-export-as-taskjuggler-and-open t)
  906. (?m org-export-as-freemind t)
  907. (?l org-export-as-latex t)
  908. (?p org-export-as-pdf t)
  909. (?d org-export-as-pdf-and-open t)
  910. (?L org-export-as-latex-to-buffer nil)
  911. (?i org-export-icalendar-this-file t)
  912. (?I org-export-icalendar-all-agenda-files t)
  913. (?c org-export-icalendar-combine-agenda-files t)
  914. (?F org-publish-current-file t)
  915. (?P org-publish-current-project t)
  916. (?X org-publish t)
  917. (?E org-publish-all t)))
  918. r1 r2 ass
  919. (cpos (point)) (cbuf (current-buffer)) bpos)
  920. (save-excursion
  921. (save-window-excursion
  922. (if subtree-p
  923. (message "Export subtree: ")
  924. (message "Export buffer: "))
  925. (delete-other-windows)
  926. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  927. (princ help))
  928. (org-fit-window-to-buffer (get-buffer-window
  929. "*Org Export/Publishing Help*"))
  930. (while (eq (setq r1 (read-char-exclusive)) ?1)
  931. (cond (subtree-p
  932. (setq subtree-p nil)
  933. (message "Export buffer: "))
  934. ((not subtree-p)
  935. (setq subtree-p t)
  936. (setq bpos (point))
  937. (org-mark-subtree)
  938. (org-activate-mark)
  939. (setq regb (and (org-region-active-p) (region-beginning)))
  940. (setq rege (and (org-region-active-p) (region-end)))
  941. (message "Export subtree: "))))
  942. (when (eq r1 ?\ )
  943. (let ((case-fold-search t)
  944. (end (save-excursion (while (org-up-heading-safe)) (point))))
  945. (outline-next-heading)
  946. (if (re-search-backward
  947. "^[ \t]+\\(:latex_class:\\|:export_title:\\|:export_file_name:\\)[ \t]+\\S-"
  948. end t)
  949. (progn
  950. (org-back-to-heading t)
  951. (setq subtree-p t)
  952. (setq bpos (point))
  953. (message "Select command (for subtree): ")
  954. (setq r1 (read-char-exclusive)))
  955. (error "No enclosing node with LaTeX_CLASS or EXPORT_TITLE or EXPORT_FILE_NAME")
  956. )))))
  957. (if (fboundp 'redisplay) (redisplay)) ;; XEmacs does not have/need (redisplay)
  958. (and bpos (goto-char bpos))
  959. (setq r2 (if (< r1 27) (+ r1 96) r1))
  960. (unless (setq ass (assq r2 cmds))
  961. (error "No command associated with key %c" r1))
  962. (if (and bg (nth 2 ass)
  963. (not (buffer-base-buffer))
  964. (not (org-region-active-p)))
  965. ;; execute in background
  966. (let ((p (start-process
  967. (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
  968. "*Org Processes*"
  969. (expand-file-name invocation-name invocation-directory)
  970. "-batch"
  971. "-l" user-init-file
  972. "--eval" "(require 'org-exp)"
  973. "--eval" "(setq org-wait .2)"
  974. (buffer-file-name)
  975. "-f" (symbol-name (nth 1 ass)))))
  976. (set-process-sentinel p 'org-export-process-sentinel)
  977. (message "Background process \"%s\": started" p))
  978. ;; set the mark correctly when exporting a subtree
  979. (if subtree-p (let (deactivate-mark) (push-mark rege t t) (goto-char regb)))
  980. (call-interactively (nth 1 ass))
  981. (when (and bpos (get-buffer-window cbuf))
  982. (let ((cw (selected-window)))
  983. (select-window (get-buffer-window cbuf))
  984. (goto-char cpos)
  985. (deactivate-mark)
  986. (select-window cw))))))
  987. (defun org-export-process-sentinel (process status)
  988. (if (string-match "\n+\\'" status)
  989. (setq status (substring status 0 -1)))
  990. (message "Background process \"%s\": %s" process status))
  991. ;;; General functions for all backends
  992. (defvar org-export-target-aliases nil
  993. "Alist of targets with invisible aliases.")
  994. (defvar org-export-preferred-target-alist nil
  995. "Alist of section id's with preferred aliases.")
  996. (defvar org-export-id-target-alist nil
  997. "Alist of section id's with preferred aliases.")
  998. (defvar org-export-code-refs nil
  999. "Alist of code references and line numbers.")
  1000. (defun org-export-preprocess-string (string &rest parameters)
  1001. "Cleanup STRING so that the true exported has a more consistent source.
  1002. This function takes STRING, which should be a buffer-string of an org-file
  1003. to export. It then creates a temporary buffer where it does its job.
  1004. The result is then again returned as a string, and the exporter works
  1005. on this string to produce the exported version."
  1006. (interactive)
  1007. (let* ((org-export-current-backend (or (plist-get parameters :for-backend)
  1008. org-export-current-backend))
  1009. (archived-trees (plist-get parameters :archived-trees))
  1010. (inhibit-read-only t)
  1011. (drawers org-drawers)
  1012. (source-buffer (current-buffer))
  1013. target-alist rtn)
  1014. (setq org-export-target-aliases nil
  1015. org-export-preferred-target-alist nil
  1016. org-export-id-target-alist nil
  1017. org-export-code-refs nil)
  1018. (with-temp-buffer
  1019. (erase-buffer)
  1020. (insert string)
  1021. (setq case-fold-search t)
  1022. (let ((inhibit-read-only t))
  1023. (remove-text-properties (point-min) (point-max)
  1024. '(read-only t)))
  1025. ;; Remove license-to-kill stuff
  1026. ;; The caller marks some stuff for killing, stuff that has been
  1027. ;; used to create the page title, for example.
  1028. (org-export-kill-licensed-text)
  1029. (let ((org-inhibit-startup t)) (org-mode))
  1030. (setq case-fold-search t)
  1031. (org-clone-local-variables source-buffer "^\\(org-\\|orgtbl-\\)")
  1032. (org-install-letbind)
  1033. ;; Call the hook
  1034. (run-hooks 'org-export-preprocess-hook)
  1035. (untabify (point-min) (point-max))
  1036. ;; Handle include files, and call a hook
  1037. (org-export-handle-include-files-recurse)
  1038. (run-hooks 'org-export-preprocess-after-include-files-hook)
  1039. ;; Get rid of archived trees
  1040. (org-export-remove-archived-trees archived-trees)
  1041. ;; Remove comment environment and comment subtrees
  1042. (org-export-remove-comment-blocks-and-subtrees)
  1043. ;; Get rid of excluded trees, and call a hook
  1044. (org-export-handle-export-tags (plist-get parameters :select-tags)
  1045. (plist-get parameters :exclude-tags))
  1046. (run-hooks 'org-export-preprocess-after-tree-selection-hook)
  1047. ;; Get rid of tasks, depending on configuration
  1048. (org-export-remove-tasks (plist-get parameters :tasks))
  1049. ;; Prepare footnotes for export. During that process, footnotes
  1050. ;; actually included in the exported part of the buffer go
  1051. ;; though some transformations:
  1052. ;; 1. They have their label normalized (like "[N]");
  1053. ;; 2. They get moved at the same place in the buffer (usually at
  1054. ;; its end, but backends may define another place via
  1055. ;; `org-footnote-insert-pos-for-preprocessor');
  1056. ;; 3. The are stored in `org-export-footnotes-seen', while
  1057. ;; `org-export-preprocess-string' is applied to their
  1058. ;; definition.
  1059. ;; Line-wise exporters ignore `org-export-footnotes-seen', as
  1060. ;; they interpret footnotes at the moment they see them in the
  1061. ;; buffer. Context-wise exporters grab all the info needed in
  1062. ;; that variable and delete moved definitions (as described in
  1063. ;; 2nd step).
  1064. (when (plist-get parameters :footnotes)
  1065. (org-footnote-normalize nil parameters))
  1066. ;; Change lists ending. Other parts of export may insert blank
  1067. ;; lines and lists' structure could be altered.
  1068. (org-export-mark-list-end)
  1069. ;; Process the macros
  1070. (org-export-preprocess-apply-macros)
  1071. (run-hooks 'org-export-preprocess-after-macros-hook)
  1072. ;; Export code blocks
  1073. (org-export-blocks-preprocess)
  1074. ;; Mark lists with properties
  1075. (org-export-mark-list-properties)
  1076. ;; Handle source code snippets
  1077. (org-export-replace-src-segments-and-examples)
  1078. ;; Protect short examples marked by a leading colon
  1079. (org-export-protect-colon-examples)
  1080. ;; Protected spaces
  1081. (org-export-convert-protected-spaces)
  1082. ;; Find all headings and compute the targets for them
  1083. (setq target-alist (org-export-define-heading-targets target-alist))
  1084. (run-hooks 'org-export-preprocess-after-headline-targets-hook)
  1085. ;; Find HTML special classes for headlines
  1086. (org-export-remember-html-container-classes)
  1087. ;; Get rid of drawers
  1088. (org-export-remove-or-extract-drawers
  1089. drawers (plist-get parameters :drawers))
  1090. ;; Get the correct stuff before the first headline
  1091. (when (plist-get parameters :skip-before-1st-heading)
  1092. (goto-char (point-min))
  1093. (when (re-search-forward "^\\(#.*\n\\)?\\*+[ \t]" nil t)
  1094. (delete-region (point-min) (match-beginning 0))
  1095. (goto-char (point-min))
  1096. (insert "\n")))
  1097. (when (plist-get parameters :add-text)
  1098. (goto-char (point-min))
  1099. (insert (plist-get parameters :add-text) "\n"))
  1100. ;; Remove todo-keywords before exporting, if the user has requested so
  1101. (org-export-remove-headline-metadata parameters)
  1102. ;; Find targets in comments and move them out of comments,
  1103. ;; but mark them as targets that should be invisible
  1104. (setq target-alist (org-export-handle-invisible-targets target-alist))
  1105. ;; Select and protect backend specific stuff, throw away stuff
  1106. ;; that is specific for other backends
  1107. (run-hooks 'org-export-preprocess-before-selecting-backend-code-hook)
  1108. (org-export-select-backend-specific-text)
  1109. ;; Protect quoted subtrees
  1110. (org-export-protect-quoted-subtrees)
  1111. ;; Remove clock lines
  1112. (org-export-remove-clock-lines)
  1113. ;; Protect verbatim elements
  1114. (org-export-protect-verbatim)
  1115. ;; Blockquotes, verse, and center
  1116. (org-export-mark-blockquote-verse-center)
  1117. (run-hooks 'org-export-preprocess-after-blockquote-hook)
  1118. ;; Remove timestamps, if the user has requested so
  1119. (unless (plist-get parameters :timestamps)
  1120. (org-export-remove-timestamps))
  1121. ;; Attach captions to the correct object
  1122. (setq target-alist (org-export-attach-captions-and-attributes target-alist))
  1123. ;; Find matches for radio targets and turn them into internal links
  1124. (org-export-mark-radio-links)
  1125. (run-hooks 'org-export-preprocess-after-radio-targets-hook)
  1126. ;; Find all links that contain a newline and put them into a single line
  1127. (org-export-concatenate-multiline-links)
  1128. ;; Normalize links: Convert angle and plain links into bracket links
  1129. ;; and expand link abbreviations
  1130. (run-hooks 'org-export-preprocess-before-normalizing-links-hook)
  1131. (org-export-normalize-links)
  1132. ;; Find all internal links. If they have a fuzzy match (i.e. not
  1133. ;; a *dedicated* target match, let the link point to the
  1134. ;; corresponding section.
  1135. (org-export-target-internal-links target-alist)
  1136. ;; Find multiline emphasis and put them into single line
  1137. (when (plist-get parameters :emph-multiline)
  1138. (org-export-concatenate-multiline-emphasis))
  1139. ;; Remove special table lines, and store alignment information
  1140. (org-store-forced-table-alignment)
  1141. (when org-export-table-remove-special-lines
  1142. (org-export-remove-special-table-lines))
  1143. ;; Another hook
  1144. (run-hooks 'org-export-preprocess-before-backend-specifics-hook)
  1145. ;; Backend-specific preprocessing
  1146. (let* ((backend-name (symbol-name org-export-current-backend))
  1147. (f (intern (format "org-export-%s-preprocess" backend-name))))
  1148. (require (intern (concat "org-" backend-name)) nil)
  1149. (funcall f parameters))
  1150. ;; Remove or replace comments
  1151. (org-export-handle-comments (plist-get parameters :comments))
  1152. ;; Remove #+TBLFM #+TBLNAME #+NAME #+RESULTS lines
  1153. (org-export-handle-metalines)
  1154. ;; Run the final hook
  1155. (run-hooks 'org-export-preprocess-final-hook)
  1156. (setq rtn (buffer-string)))
  1157. rtn))
  1158. (defun org-export-kill-licensed-text ()
  1159. "Remove all text that is marked with a :org-license-to-kill property."
  1160. (let (p)
  1161. (while (setq p (text-property-any (point-min) (point-max)
  1162. :org-license-to-kill t))
  1163. (delete-region
  1164. p (or (next-single-property-change p :org-license-to-kill)
  1165. (point-max))))))
  1166. (defvar org-export-define-heading-targets-headline-hook nil
  1167. "Hook that is run when a headline was matched during target search.
  1168. This is part of the preprocessing for export.")
  1169. (defun org-export-define-heading-targets (target-alist)
  1170. "Find all headings and define the targets for them.
  1171. The new targets are added to TARGET-ALIST, which is also returned.
  1172. Also find all ID and CUSTOM_ID properties and store them."
  1173. (goto-char (point-min))
  1174. (org-init-section-numbers)
  1175. (let ((re (concat "^" org-outline-regexp
  1176. "\\|"
  1177. "^[ \t]*:\\(ID\\|CUSTOM_ID\\):[ \t]*\\([^ \t\r\n]+\\)"))
  1178. level target last-section-target a id)
  1179. (while (re-search-forward re nil t)
  1180. (org-if-unprotected-at (match-beginning 0)
  1181. (if (match-end 2)
  1182. (progn
  1183. (setq id (org-match-string-no-properties 2))
  1184. (push (cons id target) target-alist)
  1185. (setq a (or (assoc last-section-target org-export-target-aliases)
  1186. (progn
  1187. (push (list last-section-target)
  1188. org-export-target-aliases)
  1189. (car org-export-target-aliases))))
  1190. (push (caar target-alist) (cdr a))
  1191. (when (equal (match-string 1) "CUSTOM_ID")
  1192. (if (not (assoc last-section-target
  1193. org-export-preferred-target-alist))
  1194. (push (cons last-section-target id)
  1195. org-export-preferred-target-alist)))
  1196. (when (equal (match-string 1) "ID")
  1197. (if (not (assoc last-section-target
  1198. org-export-id-target-alist))
  1199. (push (cons last-section-target (concat "ID-" id))
  1200. org-export-id-target-alist))))
  1201. (setq level (org-reduced-level
  1202. (save-excursion (goto-char (point-at-bol))
  1203. (org-outline-level))))
  1204. (setq target (org-solidify-link-text
  1205. (format "sec-%s" (replace-regexp-in-string
  1206. "\\." "-"
  1207. (org-section-number level)))))
  1208. (setq last-section-target target)
  1209. (push (cons target target) target-alist)
  1210. (add-text-properties
  1211. (point-at-bol) (point-at-eol)
  1212. (list 'target target))
  1213. (run-hooks 'org-export-define-heading-targets-headline-hook)))))
  1214. target-alist)
  1215. (defun org-export-handle-invisible-targets (target-alist)
  1216. "Find targets in comments and move them out of comments.
  1217. Mark them as invisible targets."
  1218. (let (target tmp a)
  1219. (goto-char (point-min))
  1220. (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
  1221. ;; Check if the line before or after is a headline with a target
  1222. (if (setq target (or (get-text-property (point-at-bol 0) 'target)
  1223. (get-text-property (point-at-bol 2) 'target)))
  1224. (progn
  1225. ;; use the existing target in a neighboring line
  1226. (setq tmp (match-string 2))
  1227. (replace-match "")
  1228. (and (looking-at "\n") (delete-char 1))
  1229. (push (cons (setq tmp (org-solidify-link-text tmp)) target)
  1230. target-alist)
  1231. (setq a (or (assoc target org-export-target-aliases)
  1232. (progn
  1233. (push (list target) org-export-target-aliases)
  1234. (car org-export-target-aliases))))
  1235. (push tmp (cdr a)))
  1236. ;; Make an invisible target
  1237. (replace-match "\\1(INVISIBLE)"))))
  1238. target-alist)
  1239. (defun org-export-target-internal-links (target-alist)
  1240. "Find all internal links and assign targets to them.
  1241. If a link has a fuzzy match (i.e. not a *dedicated* target match),
  1242. let the link point to the corresponding section.
  1243. This function also handles the id links, if they have a match in
  1244. the current file."
  1245. (goto-char (point-min))
  1246. (while (re-search-forward org-bracket-link-regexp nil t)
  1247. (org-if-unprotected-at (1+ (match-beginning 0))
  1248. (let* ((org-link-search-must-match-exact-headline t)
  1249. (md (match-data))
  1250. (desc (match-end 2))
  1251. (link (org-link-unescape (match-string 1)))
  1252. (slink (org-solidify-link-text link))
  1253. found props pos cref
  1254. (target
  1255. (cond
  1256. ((= (string-to-char link) ?#)
  1257. ;; user wants exactly this link
  1258. link)
  1259. ((cdr (assoc slink target-alist))
  1260. (or (cdr (assoc (assoc slink target-alist)
  1261. org-export-preferred-target-alist))
  1262. (cdr (assoc slink target-alist))))
  1263. ((and (string-match "^id:" link)
  1264. (cdr (assoc (substring link 3) target-alist))))
  1265. ((string-match "^(\\(.*\\))$" link)
  1266. (setq cref (match-string 1 link))
  1267. (concat "coderef:" cref))
  1268. ((string-match org-link-types-re link) nil)
  1269. ((or (file-name-absolute-p link)
  1270. (string-match "^\\." link))
  1271. nil)
  1272. (t
  1273. (let ((org-link-search-inhibit-query t))
  1274. (save-excursion
  1275. (setq found (condition-case nil (org-link-search link)
  1276. (error nil)))
  1277. (when (and found
  1278. (or (org-at-heading-p)
  1279. (not (eq found 'dedicated))))
  1280. (or (get-text-property (point) 'target)
  1281. (get-text-property
  1282. (max (point-min)
  1283. (1- (or (previous-single-property-change
  1284. (point) 'target) 0)))
  1285. 'target)))))))))
  1286. (when target
  1287. (set-match-data md)
  1288. (goto-char (match-beginning 1))
  1289. (setq props (text-properties-at (point)))
  1290. (delete-region (match-beginning 1) (match-end 1))
  1291. (setq pos (point))
  1292. (insert target)
  1293. (unless desc (insert "][" link))
  1294. (add-text-properties pos (point) props))))))
  1295. (defun org-export-remember-html-container-classes ()
  1296. "Store the HTML_CONTAINER_CLASS properties in a text property."
  1297. (goto-char (point-min))
  1298. (let (class)
  1299. (while (re-search-forward
  1300. "^[ \t]*:HTML_CONTAINER_CLASS:[ \t]+\\(.+\\)$" nil t)
  1301. (setq class (match-string 1))
  1302. (save-excursion
  1303. (when (re-search-backward "^\\*" (point-min) t)
  1304. (org-back-to-heading t)
  1305. (put-text-property (point-at-bol) (point-at-eol)
  1306. 'html-container-class class))))))
  1307. (defvar org-export-format-drawer-function nil
  1308. "Function to be called to format the contents of a drawer.
  1309. The function must accept two parameters:
  1310. NAME the drawer name, like \"PROPERTIES\"
  1311. CONTENT the content of the drawer.
  1312. You can check the export backend through `org-export-current-backend'.
  1313. The function should return the text to be inserted into the buffer.
  1314. If this is nil, `org-export-format-drawer' is used as a default.")
  1315. (defun org-export-remove-or-extract-drawers (all-drawers exp-drawers)
  1316. "Remove drawers, or extract and format the content.
  1317. ALL-DRAWERS is a list of all drawer names valid in the current buffer.
  1318. EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
  1319. whose content to keep. Any drawers that are in ALL-DRAWERS but not in
  1320. EXP-DRAWERS will be removed."
  1321. (goto-char (point-min))
  1322. (let ((re (concat "^[ \t]*:\\("
  1323. (mapconcat 'identity all-drawers "\\|")
  1324. "\\):[ \t]*$"))
  1325. name beg beg-content eol content)
  1326. (while (re-search-forward re nil t)
  1327. (org-if-unprotected
  1328. (setq name (match-string 1))
  1329. (setq beg (match-beginning 0)
  1330. beg-content (1+ (point-at-eol))
  1331. eol (point-at-eol))
  1332. (if (not (and (re-search-forward
  1333. "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]" nil t)
  1334. (match-end 1)))
  1335. (goto-char eol)
  1336. (goto-char (match-beginning 0))
  1337. (and (looking-at ".*\n?") (replace-match ""))
  1338. (setq content (buffer-substring beg-content (point)))
  1339. (delete-region beg (point))
  1340. (when (or (eq exp-drawers t)
  1341. (member name exp-drawers))
  1342. (setq content (funcall (or org-export-format-drawer-function
  1343. 'org-export-format-drawer)
  1344. name content))
  1345. (insert content)))))))
  1346. (defun org-export-format-drawer (name content)
  1347. "Format the content of a drawer as a colon example."
  1348. (if (string-match "[ \t]+\\'" content)
  1349. (setq content (substring content (match-beginning 0))))
  1350. (while (string-match "\\`[ \t]*\n" content)
  1351. (setq content (substring content (match-end 0))))
  1352. (setq content (org-remove-indentation content))
  1353. (setq content (concat ": " (mapconcat 'identity
  1354. (org-split-string content "\n")
  1355. "\n: ")
  1356. "\n"))
  1357. (setq content (concat " : " (upcase name) "\n" content))
  1358. (org-add-props content nil 'org-protected t))
  1359. (defun org-export-handle-export-tags (select-tags exclude-tags)
  1360. "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.
  1361. Both arguments are lists of tags.
  1362. If any of SELECT-TAGS is found, all trees not marked by a SELECT-TAG
  1363. will be removed.
  1364. After that, all subtrees that are marked by EXCLUDE-TAGS will be
  1365. removed as well."
  1366. (remove-text-properties (point-min) (point-max) '(:org-delete t))
  1367. (let* ((re-sel (concat ":\\(" (mapconcat 'regexp-quote
  1368. select-tags "\\|")
  1369. "\\):"))
  1370. (re-excl (concat ":\\(" (mapconcat 'regexp-quote
  1371. exclude-tags "\\|")
  1372. "\\):"))
  1373. beg end cont)
  1374. (goto-char (point-min))
  1375. (when (and select-tags
  1376. (re-search-forward
  1377. (concat "^\\*+[ \t].*" re-sel "[^ \t\n]*[ \t]*$") nil t))
  1378. ;; At least one tree is marked for export, this means
  1379. ;; all the unmarked stuff needs to go.
  1380. ;; Dig out the trees that should be exported
  1381. (goto-char (point-min))
  1382. (outline-next-heading)
  1383. (setq beg (point))
  1384. (put-text-property beg (point-max) :org-delete t)
  1385. (while (re-search-forward re-sel nil t)
  1386. (when (org-at-heading-p)
  1387. (org-back-to-heading)
  1388. (remove-text-properties
  1389. (max (1- (point)) (point-min))
  1390. (setq cont (save-excursion (org-end-of-subtree t t)))
  1391. '(:org-delete t))
  1392. (while (and (org-up-heading-safe)
  1393. (get-text-property (point) :org-delete))
  1394. (remove-text-properties (max (1- (point)) (point-min))
  1395. (point-at-eol) '(:org-delete t)))
  1396. (goto-char cont))))
  1397. ;; Remove the trees explicitly marked for noexport
  1398. (when exclude-tags
  1399. (goto-char (point-min))
  1400. (while (re-search-forward re-excl nil t)
  1401. (when (org-at-heading-p)
  1402. (org-back-to-heading t)
  1403. (setq beg (point))
  1404. (org-end-of-subtree t t)
  1405. (delete-region beg (point))
  1406. (when (featurep 'org-inlinetask)
  1407. (org-inlinetask-remove-END-maybe)))))
  1408. ;; Remove everything that is now still marked for deletion
  1409. (goto-char (point-min))
  1410. (while (setq beg (text-property-any (point-min) (point-max) :org-delete t))
  1411. (setq end (or (next-single-property-change beg :org-delete)
  1412. (point-max)))
  1413. (delete-region beg end))))
  1414. (defun org-export-remove-tasks (keep)
  1415. "Remove tasks depending on configuration.
  1416. When KEEP is nil, remove all tasks.
  1417. When KEEP is `todo', remove the tasks that are DONE.
  1418. When KEEP is `done', remove the tasks that are not yet done.
  1419. When it is a list of strings, keep only tasks with these TODO keywords."
  1420. (when (or (listp keep) (memq keep '(todo done nil)))
  1421. (let ((re (concat "^\\*+[ \t]+\\("
  1422. (mapconcat
  1423. 'regexp-quote
  1424. (cond ((not keep) org-todo-keywords-1)
  1425. ((eq keep 'todo) org-done-keywords)
  1426. ((eq keep 'done) org-not-done-keywords)
  1427. ((listp keep)
  1428. (org-delete-all keep (copy-sequence
  1429. org-todo-keywords-1))))
  1430. "\\|")
  1431. "\\)\\($\\|[ \t]\\)"))
  1432. (case-fold-search nil)
  1433. beg)
  1434. (goto-char (point-min))
  1435. (while (re-search-forward re nil t)
  1436. (org-if-unprotected
  1437. (setq beg (match-beginning 0))
  1438. (org-end-of-subtree t t)
  1439. (if (looking-at "^\\*+[ \t]+END[ \t]*$")
  1440. ;; Kill the END line of the inline task
  1441. (goto-char (min (point-max) (1+ (match-end 0)))))
  1442. (delete-region beg (point)))))))
  1443. (defun org-export-remove-archived-trees (export-archived-trees)
  1444. "Remove archived trees.
  1445. When EXPORT-ARCHIVED-TREES is `headline;, only the headline will be exported.
  1446. When it is t, the entire archived tree will be exported.
  1447. When it is nil the entire tree including the headline will be removed
  1448. from the buffer."
  1449. (let ((re-archive (concat ":" org-archive-tag ":"))
  1450. a b)
  1451. (when (not (eq export-archived-trees t))
  1452. (goto-char (point-min))
  1453. (while (re-search-forward re-archive nil t)
  1454. (if (not (org-at-heading-p t))
  1455. (goto-char (point-at-eol))
  1456. (beginning-of-line 1)
  1457. (setq a (if export-archived-trees
  1458. (1+ (point-at-eol)) (point))
  1459. b (org-end-of-subtree t))
  1460. (if (> b a) (delete-region a b)))))))
  1461. (defun org-export-remove-headline-metadata (opts)
  1462. "Remove meta data from the headline, according to user options."
  1463. (let ((re org-complex-heading-regexp)
  1464. (todo (plist-get opts :todo-keywords))
  1465. (tags (plist-get opts :tags))
  1466. (pri (plist-get opts :priority))
  1467. (elts '(1 2 3 4 5))
  1468. (case-fold-search nil)
  1469. rpl)
  1470. (setq elts (delq nil (list 1 (if todo 2) (if pri 3) 4 (if tags 5))))
  1471. (when (or (not todo) (not tags) (not pri))
  1472. (goto-char (point-min))
  1473. (while (re-search-forward re nil t)
  1474. (org-if-unprotected
  1475. (setq rpl (mapconcat (lambda (i) (if (match-end i) (match-string i) ""))
  1476. elts " "))
  1477. (replace-match rpl t t))))))
  1478. (defun org-export-remove-timestamps ()
  1479. "Remove timestamps and keywords for export."
  1480. (goto-char (point-min))
  1481. (while (re-search-forward org-maybe-keyword-time-regexp nil t)
  1482. (backward-char 1)
  1483. (org-if-unprotected
  1484. (unless (save-match-data (org-at-table-p))
  1485. (replace-match "")
  1486. (beginning-of-line 1)
  1487. (if (looking-at "[- \t]*\\(=>[- \t0-9:]*\\)?[ \t]*\n")
  1488. (replace-match ""))))))
  1489. (defun org-export-remove-clock-lines ()
  1490. "Remove clock lines for export."
  1491. (goto-char (point-min))
  1492. (let ((re (concat "^[ \t]*" org-clock-string ".*\n?")))
  1493. (while (re-search-forward re nil t)
  1494. (org-if-unprotected
  1495. (replace-match "")))))
  1496. (defvar org-heading-keyword-regexp-format) ; defined in org.el
  1497. (defun org-export-protect-quoted-subtrees ()
  1498. "Mark quoted subtrees with the protection property."
  1499. (let ((org-re-quote (format org-heading-keyword-regexp-format
  1500. org-quote-string)))
  1501. (goto-char (point-min))
  1502. (while (re-search-forward org-re-quote nil t)
  1503. (goto-char (match-beginning 0))
  1504. (end-of-line 1)
  1505. (add-text-properties (point) (org-end-of-subtree t)
  1506. '(org-protected t)))))
  1507. (defun org-export-convert-protected-spaces ()
  1508. "Convert strings like \\____ to protected spaces in all backends."
  1509. (goto-char (point-min))
  1510. (while (re-search-forward "\\\\__+" nil t)
  1511. (org-if-unprotected-1
  1512. (replace-match
  1513. (org-add-props
  1514. (cond
  1515. ((eq org-export-current-backend 'latex)
  1516. (format "\\hspace{%dex}" (- (match-end 0) (match-beginning 0))))
  1517. ((eq org-export-current-backend 'html)
  1518. (org-add-props (match-string 0) nil
  1519. 'org-whitespace (- (match-end 0) (match-beginning 0))))
  1520. ;; ((eq org-export-current-backend 'docbook))
  1521. ((eq org-export-current-backend 'ascii)
  1522. (org-add-props (match-string 0) '(org-whitespace t)))
  1523. (t (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  1524. '(org-protected t))
  1525. t t))))
  1526. (defun org-export-protect-verbatim ()
  1527. "Mark verbatim snippets with the protection property."
  1528. (goto-char (point-min))
  1529. (while (re-search-forward org-verbatim-re nil t)
  1530. (org-if-unprotected
  1531. (add-text-properties (match-beginning 4) (match-end 4)
  1532. '(org-protected t org-verbatim-emph t))
  1533. (goto-char (1+ (match-end 4))))))
  1534. (defun org-export-protect-colon-examples ()
  1535. "Protect lines starting with a colon."
  1536. (goto-char (point-min))
  1537. (let ((re "^[ \t]*:\\([ \t]\\|$\\)") beg)
  1538. (while (re-search-forward re nil t)
  1539. (beginning-of-line 1)
  1540. (setq beg (point))
  1541. (while (looking-at re)
  1542. (end-of-line 1)
  1543. (or (eobp) (forward-char 1)))
  1544. (add-text-properties beg (if (bolp) (1- (point)) (point))
  1545. '(org-protected t)))))
  1546. (defvar org-export-backends
  1547. '(docbook html beamer ascii latex)
  1548. "List of Org supported export backends.")
  1549. (defun org-export-select-backend-specific-text ()
  1550. (let ((formatters org-export-backends)
  1551. (case-fold-search t)
  1552. backend backend-name beg beg-content end end-content ind)
  1553. (while formatters
  1554. (setq backend (pop formatters)
  1555. backend-name (symbol-name backend))
  1556. ;; Handle #+BACKEND: stuff
  1557. (goto-char (point-min))
  1558. (while (re-search-forward (concat "^\\([ \t]*\\)#\\+" backend-name
  1559. ":[ \t]*\\(.*\\)") nil t)
  1560. (if (not (eq backend org-export-current-backend))
  1561. (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  1562. (let ((ind (get-text-property (point-at-bol) 'original-indentation)))
  1563. (replace-match "\\1\\2" t)
  1564. (add-text-properties
  1565. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  1566. `(org-protected t original-indentation ,ind org-native-text t)))))
  1567. ;; Delete #+ATTR_BACKEND: stuff of another backend. Those
  1568. ;; matching the current backend will be taken care of by
  1569. ;; `org-export-attach-captions-and-attributes'
  1570. (goto-char (point-min))
  1571. (while (re-search-forward (concat "^\\([ \t]*\\)#\\+ATTR_" backend-name
  1572. ":[ \t]*\\(.*\\)") nil t)
  1573. (setq ind (org-get-indentation))
  1574. (when (not (eq backend org-export-current-backend))
  1575. (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
  1576. ;; Handle #+BEGIN_BACKEND and #+END_BACKEND stuff
  1577. (goto-char (point-min))
  1578. (while (re-search-forward (concat "^[ \t]*#\\+BEGIN_" backend-name "\\>.*\n?")
  1579. nil t)
  1580. (setq beg (match-beginning 0) beg-content (match-end 0))
  1581. (setq ind (or (get-text-property beg 'original-indentation)
  1582. (save-excursion (goto-char beg) (org-get-indentation))))
  1583. (when (re-search-forward (concat "^[ \t]*#\\+END_" backend-name "\\>.*\n?")
  1584. nil t)
  1585. (setq end (match-end 0) end-content (match-beginning 0))
  1586. (if (eq backend org-export-current-backend)
  1587. ;; yes, keep this
  1588. (progn
  1589. (add-text-properties
  1590. beg-content end-content
  1591. `(org-protected t original-indentation ,ind org-native-text t))
  1592. ;; strip protective commas
  1593. (org-unescape-code-in-region beg-content end-content)
  1594. (delete-region (match-beginning 0) (match-end 0))
  1595. (save-excursion
  1596. (goto-char beg)
  1597. (delete-region (point) (1+ (point-at-eol)))))
  1598. ;; No, this is for a different backend, kill it
  1599. (delete-region beg end)))))))
  1600. (defun org-export-mark-blockquote-verse-center ()
  1601. "Mark block quote and verse environments with special cookies.
  1602. These special cookies will later be interpreted by the backend."
  1603. ;; Blockquotes
  1604. (let (type t1 ind beg end beg1 end1 content)
  1605. (goto-char (point-min))
  1606. (while (re-search-forward
  1607. "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)"
  1608. nil t)
  1609. (setq ind (length (match-string 1))
  1610. type (downcase (match-string 3))
  1611. t1 (if (equal type "quote") "blockquote" type))
  1612. (setq beg (match-beginning 0)
  1613. beg1 (1+ (match-end 0)))
  1614. (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t)
  1615. (setq end1 (1- (match-beginning 0))
  1616. end (+ (point-at-eol) (if (looking-at "\n$") 1 0)))
  1617. (setq content (org-remove-indentation (buffer-substring beg1 end1)))
  1618. (setq content (concat "ORG-" (upcase t1) "-START\n"
  1619. content "\n"
  1620. "ORG-" (upcase t1) "-END\n"))
  1621. (delete-region beg end)
  1622. (insert (org-add-props content nil 'original-indentation ind))))))
  1623. (defun org-export-mark-list-end ()
  1624. "Mark all list endings with a special string."
  1625. (unless (eq org-export-current-backend 'ascii)
  1626. (mapc
  1627. (lambda (e)
  1628. ;; For each type allowing list export, find every list, remove
  1629. ;; ending regexp if needed, and insert org-list-end.
  1630. (goto-char (point-min))
  1631. (while (re-search-forward (org-item-beginning-re) nil t)
  1632. (when (eq (nth 2 (org-list-context)) e)
  1633. (let* ((struct (org-list-struct))
  1634. (bottom (org-list-get-bottom-point struct))
  1635. (top (point-at-bol))
  1636. (top-ind (org-list-get-ind top struct)))
  1637. (goto-char bottom)
  1638. (when (and (not (looking-at "[ \t]*$"))
  1639. (looking-at org-list-end-re))
  1640. (replace-match ""))
  1641. (unless (bolp) (insert "\n"))
  1642. ;; As org-list-end is inserted at column 0, it would end
  1643. ;; by indentation any list. It can be problematic when
  1644. ;; there are lists within lists: the inner list end would
  1645. ;; also become the outer list end. To avoid this, text
  1646. ;; property `original-indentation' is added, as
  1647. ;; `org-list-struct' pays attention to it when reading a
  1648. ;; list.
  1649. (insert (org-add-props
  1650. "ORG-LIST-END-MARKER\n"
  1651. (list 'original-indentation top-ind)))))))
  1652. (cons nil org-list-export-context))))
  1653. (defun org-export-mark-list-properties ()
  1654. "Mark list with special properties.
  1655. These special properties will later be interpreted by the backend."
  1656. (let ((mark-list
  1657. (function
  1658. ;; Mark a list with 3 properties: `list-item' which is
  1659. ;; position at beginning of line, `list-struct' which is
  1660. ;; list structure, and `list-prevs' which is the alist of
  1661. ;; item and its predecessor. Leave point at list ending.
  1662. (lambda (ctxt)
  1663. (let* ((struct (org-list-struct))
  1664. (top (org-list-get-top-point struct))
  1665. (bottom (org-list-get-bottom-point struct))
  1666. (prevs (org-list-prevs-alist struct))
  1667. poi)
  1668. ;; Get every item and ending position, without dups and
  1669. ;; without bottom point of list.
  1670. (mapc (lambda (e)
  1671. (let ((pos (car e))
  1672. (end (nth 6 e)))
  1673. (unless (memq pos poi)
  1674. (push pos poi))
  1675. (unless (or (= end bottom) (memq end poi))
  1676. (push end poi))))
  1677. struct)
  1678. (setq poi (sort poi '<))
  1679. ;; For every point of interest, mark the whole line with
  1680. ;; its position in list.
  1681. (mapc
  1682. (lambda (e)
  1683. (goto-char e)
  1684. (add-text-properties (point-at-bol) (point-at-eol)
  1685. (list 'list-item (point-at-bol)
  1686. 'list-struct struct
  1687. 'list-prevs prevs)))
  1688. poi)
  1689. ;; Take care of bottom point. As babel may have inserted
  1690. ;; a new list in buffer, list ending isn't always
  1691. ;; marked. Now mark every list ending and add properties
  1692. ;; useful to line processing exporters.
  1693. (goto-char bottom)
  1694. (when (or (looking-at "^ORG-LIST-END-MARKER\n")
  1695. (and (not (looking-at "[ \t]*$"))
  1696. (looking-at org-list-end-re)))
  1697. (replace-match ""))
  1698. (unless (bolp) (insert "\n"))
  1699. (insert
  1700. (org-add-props "ORG-LIST-END-MARKER\n" (list 'list-item bottom
  1701. 'list-struct struct
  1702. 'list-prevs prevs)))
  1703. ;; Following property is used by LaTeX exporter.
  1704. (add-text-properties top (point) (list 'list-context ctxt)))))))
  1705. ;; Mark lists except for backends not interpreting them.
  1706. (unless (eq org-export-current-backend 'ascii)
  1707. (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
  1708. (mapc
  1709. (lambda (e)
  1710. (goto-char (point-min))
  1711. (while (re-search-forward (org-item-beginning-re) nil t)
  1712. (let ((context (nth 2 (org-list-context))))
  1713. (if (eq context e)
  1714. (funcall mark-list e)
  1715. (put-text-property (point-at-bol) (point-at-eol)
  1716. 'list-context context)))))
  1717. (cons nil org-list-export-context))))))
  1718. (defun org-export-attach-captions-and-attributes (target-alist)
  1719. "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
  1720. If the next thing following is a table, add the text properties to the first
  1721. table line. If it is a link, add it to the line containing the link."
  1722. (goto-char (point-min))
  1723. (remove-text-properties (point-min) (point-max)
  1724. '(org-caption nil org-attributes nil))
  1725. (let ((case-fold-search t)
  1726. (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
  1727. "\\|"
  1728. "^[ \t]*#\\+attr_" (symbol-name org-export-current-backend) ":[ \t]+\\(.*\\)"
  1729. "\\|"
  1730. "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
  1731. "\\|"
  1732. "^[ \t]*\\(|[^-]\\)"
  1733. "\\|"
  1734. "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
  1735. cap shortn attr label end)
  1736. (while (re-search-forward re nil t)
  1737. (cond
  1738. ;; there is a caption
  1739. ((match-end 1)
  1740. (progn
  1741. (setq cap (concat cap (if cap " " "") (org-trim (match-string 1))))
  1742. (when (string-match "\\[\\(.*\\)\\]{\\(.*\\)}" cap)
  1743. (setq shortn (match-string 1 cap)
  1744. cap (match-string 2 cap)))
  1745. (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
  1746. ;; there is an attribute
  1747. ((match-end 2)
  1748. (progn
  1749. (setq attr (concat attr (if attr " " "") (org-trim (match-string 2))))
  1750. (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
  1751. ;; there is a label
  1752. ((match-end 3)
  1753. (progn
  1754. (setq label (org-trim (match-string 3)))
  1755. (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
  1756. (t
  1757. (setq end (if (match-end 4)
  1758. (let ((ee (org-table-end)))
  1759. (prog1 (1- (marker-position ee)) (move-marker ee nil)))
  1760. (point-at-eol)))
  1761. (add-text-properties (point-at-bol) end
  1762. (list 'org-caption cap
  1763. 'org-caption-shortn shortn
  1764. 'org-attributes attr
  1765. 'org-label label))
  1766. (if label (push (cons label label) target-alist))
  1767. (goto-char end)
  1768. (setq cap nil shortn nil attr nil label nil)))))
  1769. target-alist)
  1770. (defun org-export-remove-comment-blocks-and-subtrees ()
  1771. "Remove the comment environment, and also commented subtrees."
  1772. (let ((re-commented (format org-heading-keyword-regexp-format
  1773. org-comment-string))
  1774. case-fold-search)
  1775. ;; Remove comment environment
  1776. (goto-char (point-min))
  1777. (setq case-fold-search t)
  1778. (while (re-search-forward
  1779. "^#\\+begin_comment[ \t]*\n[^\000]*?\n#\\+end_comment\\>.*" nil t)
  1780. (replace-match "" t t))
  1781. ;; Remove subtrees that are commented
  1782. (goto-char (point-min))
  1783. (setq case-fold-search nil)
  1784. (while (re-search-forward re-commented nil t)
  1785. (goto-char (match-beginning 0))
  1786. (delete-region (point) (org-end-of-subtree t)))))
  1787. (defun org-export-handle-comments (org-commentsp)
  1788. "Remove comments, or convert to backend-specific format.
  1789. ORG-COMMENTSP can be a format string for publishing comments.
  1790. When it is nil, all comments will be removed."
  1791. (let ((re "^[ \t]*#\\( \\|$\\)"))
  1792. (goto-char (point-min))
  1793. (while (re-search-forward re nil t)
  1794. (let ((pos (match-beginning 0))
  1795. (end (progn (forward-line) (point))))
  1796. (if (get-text-property pos 'org-protected)
  1797. (forward-line)
  1798. (if (not org-commentsp) (delete-region pos end)
  1799. (add-text-properties pos end '(org-protected t))
  1800. (replace-match
  1801. (org-add-props
  1802. (format org-commentsp (buffer-substring (match-end 0) end))
  1803. nil 'org-protected t)
  1804. t t)))))
  1805. ;; Hack attack: previous implementation also removed keywords at
  1806. ;; column 0. Brainlessly do it again.
  1807. (goto-char (point-min))
  1808. (while (re-search-forward "^#\\+" nil t)
  1809. (unless (get-text-property (point-at-bol) 'org-protected)
  1810. (delete-region (point-at-bol) (progn (forward-line) (point)))))))
  1811. (defun org-export-handle-metalines ()
  1812. "Remove tables and source blocks metalines.
  1813. This function should only be called after all block processing
  1814. has taken place."
  1815. (let ((re "^[ \t]*#\\+\\(tbl\\(?:name\\|fm\\)\\|results\\(?:\\[[a-z0-9]+\\]\\)?\\|name\\):\\(.*\n?\\)")
  1816. (case-fold-search t)
  1817. pos)
  1818. (goto-char (point-min))
  1819. (while (or (looking-at re)
  1820. (re-search-forward re nil t))
  1821. (setq pos (match-beginning 0))
  1822. (if (get-text-property (match-beginning 1) 'org-protected)
  1823. (goto-char (1+ pos))
  1824. (goto-char (1+ pos))
  1825. (replace-match "")
  1826. (goto-char (max (point-min) (1- pos)))))))
  1827. (defun org-export-mark-radio-links ()
  1828. "Find all matches for radio targets and turn them into internal links."
  1829. (let ((re-radio (and org-target-link-regexp
  1830. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
  1831. (goto-char (point-min))
  1832. (when re-radio
  1833. (while (re-search-forward re-radio nil t)
  1834. (unless
  1835. (save-match-data
  1836. (or (org-in-regexp org-bracket-link-regexp)
  1837. (org-in-regexp org-plain-link-re)
  1838. (org-in-regexp "<<[^<>]+>>")))
  1839. (org-if-unprotected
  1840. (replace-match "\\1[[\\2]]")))))))
  1841. (defun org-store-forced-table-alignment ()
  1842. "Find table lines which force alignment, store the results in properties."
  1843. (let (line cnt cookies)
  1844. (goto-char (point-min))
  1845. (while (re-search-forward "|[ \t]*<\\([lrc]?[0-9]+\\|[lrc]\\)>[ \t]*|"
  1846. nil t)
  1847. ;; OK, this looks like a table line with an alignment cookie
  1848. (org-if-unprotected
  1849. (setq line (buffer-substring (point-at-bol) (point-at-eol)))
  1850. (when (and (org-at-table-p)
  1851. (org-table-cookie-line-p line))
  1852. (setq cnt 0 cookies nil)
  1853. (mapc
  1854. (lambda (x)
  1855. (setq cnt (1+ cnt))
  1856. (when (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'" x)
  1857. (let ((align (and (match-end 1)
  1858. (downcase (match-string 1 x))))
  1859. (width (and (match-end 2)
  1860. (string-to-number (match-string 2 x)))))
  1861. (push (cons cnt (list align width)) cookies))))
  1862. (org-split-string line "[ \t]*|[ \t]*"))
  1863. (add-text-properties (org-table-begin) (org-table-end)
  1864. (list 'org-col-cookies cookies))))
  1865. (goto-char (point-at-eol)))))
  1866. (defun org-export-remove-special-table-lines ()
  1867. "Remove tables lines that are used for internal purposes.
  1868. Also, store forced alignment information found in such lines."
  1869. (goto-char (point-min))
  1870. (while (re-search-forward "^[ \t]*|" nil t)
  1871. (org-if-unprotected-at (1- (point))
  1872. (beginning-of-line 1)
  1873. (if (or (looking-at "[ \t]*| *[!_^] *|")
  1874. (not
  1875. (memq
  1876. nil
  1877. (mapcar
  1878. (lambda (f)
  1879. (or (and org-export-table-remove-empty-lines (= (length f) 0))
  1880. (string-match
  1881. "\\`<\\([0-9]\\|[lrc]\\|[lrc][0-9]+\\)>\\'" f)))
  1882. (org-split-string ;; FIXME, can't we do without splitting???
  1883. (buffer-substring (point-at-bol) (point-at-eol))
  1884. "[ \t]*|[ \t]*")))))
  1885. (delete-region (max (point-min) (1- (point-at-bol)))
  1886. (point-at-eol))
  1887. (end-of-line 1)))))
  1888. (defun org-export-protect-sub-super (s)
  1889. (save-match-data
  1890. (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
  1891. (setq s (replace-match "\\1\\\\\\2" nil nil s)))
  1892. s))
  1893. (defun org-export-normalize-links ()
  1894. "Convert all links to bracket links, and expand link abbreviations."
  1895. (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  1896. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  1897. nodesc)
  1898. (goto-char (point-min))
  1899. (while (re-search-forward org-bracket-link-regexp nil t)
  1900. (put-text-property (match-beginning 0) (match-end 0) 'org-normalized-link t))
  1901. (goto-char (point-min))
  1902. (while (re-search-forward re-plain-link nil t)
  1903. (unless (or (get-text-property (match-beginning 0) 'org-normalized-link)
  1904. (assoc :tags (org-context)))
  1905. (goto-char (1- (match-end 0)))
  1906. (org-if-unprotected-at (1+ (match-beginning 0))
  1907. (let* ((s (concat (match-string 1)
  1908. "[[" (match-string 2) ":" (match-string 3)
  1909. "][" (match-string 2) ":" (org-export-protect-sub-super
  1910. (match-string 3))
  1911. "]]")))
  1912. ;; added 'org-link face to links
  1913. (put-text-property 0 (length s) 'face 'org-link s)
  1914. (replace-match s t t)))))
  1915. (goto-char (point-min))
  1916. (while (re-search-forward re-angle-link nil t)
  1917. (goto-char (1- (match-end 0)))
  1918. (org-if-unprotected
  1919. (let* ((s (concat (match-string 1)
  1920. "[[" (match-string 2) ":" (match-string 3)
  1921. "][" (match-string 2) ":" (org-export-protect-sub-super
  1922. (match-string 3))
  1923. "]]")))
  1924. (put-text-property 0 (length s) 'face 'org-link s)
  1925. (replace-match s t t))))
  1926. (goto-char (point-min))
  1927. (while (re-search-forward org-bracket-link-regexp nil t)
  1928. (goto-char (1- (match-end 0)))
  1929. (setq nodesc (not (match-end 3)))
  1930. (org-if-unprotected
  1931. (let* ((xx (save-match-data
  1932. (org-translate-link
  1933. (org-link-expand-abbrev (match-string 1)))))
  1934. (s (concat
  1935. "[[" (org-add-props (copy-sequence xx)
  1936. nil 'org-protected t 'org-no-description nodesc)
  1937. "]"
  1938. (if (match-end 3)
  1939. (match-string 2)
  1940. (concat "[" (copy-sequence xx)
  1941. "]"))
  1942. "]")))
  1943. (put-text-property 0 (length s) 'face 'org-link s)
  1944. (replace-match s t t))))))
  1945. (defun org-export-concatenate-multiline-links ()
  1946. "Find multi-line links and put it all into a single line.
  1947. This is to make sure that the line-processing export backends
  1948. can work correctly."
  1949. (goto-char (point-min))
  1950. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  1951. (org-if-unprotected-at (match-beginning 1)
  1952. (replace-match "\\1 \\3")
  1953. (goto-char (match-beginning 0)))))
  1954. (defun org-export-concatenate-multiline-emphasis ()
  1955. "Find multi-line emphasis and put it all into a single line.
  1956. This is to make sure that the line-processing export backends
  1957. can work correctly."
  1958. (goto-char (point-min))
  1959. (while (re-search-forward org-emph-re nil t)
  1960. (if (and (not (= (char-after (match-beginning 3))
  1961. (char-after (match-beginning 4))))
  1962. (save-excursion (goto-char (match-beginning 0))
  1963. (save-match-data
  1964. (and (not (org-at-table-p))
  1965. (not (org-at-heading-p))))))
  1966. (org-if-unprotected
  1967. (subst-char-in-region (match-beginning 0) (match-end 0)
  1968. ?\n ?\ t)
  1969. (goto-char (1- (match-end 0))))
  1970. (goto-char (1+ (match-beginning 0))))))
  1971. (defun org-export-grab-title-from-buffer ()
  1972. "Get a title for the current document, from looking at the buffer."
  1973. (let ((inhibit-read-only t))
  1974. (save-excursion
  1975. (goto-char (point-min))
  1976. (let ((end (if (looking-at org-outline-regexp)
  1977. (point)
  1978. (save-excursion (outline-next-heading) (point)))))
  1979. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  1980. ;; Mark the line so that it will not be exported as normal text.
  1981. (unless (org-in-block-p org-list-forbidden-blocks)
  1982. (org-unmodified
  1983. (add-text-properties (match-beginning 0) (match-end 0)
  1984. (list :org-license-to-kill t))))
  1985. ;; Return the title string
  1986. (org-trim (match-string 0)))))))
  1987. (defun org-export-get-title-from-subtree ()
  1988. "Return subtree title and exclude it from export."
  1989. (let ((rbeg (region-beginning)) (rend (region-end))
  1990. (inhibit-read-only t)
  1991. (tags (plist-get (org-infile-export-plist) :tags))
  1992. title)
  1993. (save-excursion
  1994. (goto-char rbeg)
  1995. (when (and (org-at-heading-p)
  1996. (>= (org-end-of-subtree t t) rend))
  1997. (when (plist-member org-export-opt-plist :tags)
  1998. (setq tags (or (plist-get org-export-opt-plist :tags) tags)))
  1999. ;; This is a subtree, we take the title from the first heading
  2000. (goto-char rbeg)
  2001. (looking-at org-todo-line-tags-regexp)
  2002. (setq title (if (and (eq tags t) (match-string 4))
  2003. (format "%s\t%s" (match-string 3) (match-string 4))
  2004. (match-string 3)))
  2005. (org-unmodified
  2006. (add-text-properties (point) (1+ (point-at-eol))
  2007. (list :org-license-to-kill t)))
  2008. (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
  2009. title))
  2010. (defun org-solidify-link-text (s &optional alist)
  2011. "Take link text and make a safe target out of it."
  2012. (save-match-data
  2013. (let* ((rtn
  2014. (mapconcat
  2015. 'identity
  2016. (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-"))
  2017. (a (assoc rtn alist)))
  2018. (or (cdr a) rtn))))
  2019. (defun org-get-min-level (lines &optional offset)
  2020. "Get the minimum level in LINES."
  2021. (let ((re "^\\(\\*+\\) ") l)
  2022. (catch 'exit
  2023. (while (setq l (pop lines))
  2024. (if (string-match re l)
  2025. (throw 'exit (org-tr-level (- (length (match-string 1 l))
  2026. (or offset 0))))))
  2027. 1)))
  2028. ;; Variable holding the vector with section numbers
  2029. (defvar org-section-numbers (make-vector org-level-max 0))
  2030. (defun org-init-section-numbers ()
  2031. "Initialize the vector for the section numbers."
  2032. (let* ((level -1)
  2033. (numbers (nreverse (org-split-string "" "\\.")))
  2034. (depth (1- (length org-section-numbers)))
  2035. (i depth) number-string)
  2036. (while (>= i 0)
  2037. (if (> i level)
  2038. (aset org-section-numbers i 0)
  2039. (setq number-string (or (car numbers) "0"))
  2040. (if (string-match "\\`[A-Z]\\'" number-string)
  2041. (aset org-section-numbers i
  2042. (- (string-to-char number-string) ?A -1))
  2043. (aset org-section-numbers i (string-to-number number-string)))
  2044. (pop numbers))
  2045. (setq i (1- i)))))
  2046. (defun org-section-number (&optional level)
  2047. "Return a string with the current section number.
  2048. When LEVEL is non-nil, increase section numbers on that level."
  2049. (let* ((depth (1- (length org-section-numbers)))
  2050. (string "")
  2051. (fmts (car org-export-section-number-format))
  2052. (term (cdr org-export-section-number-format))
  2053. (sep "")
  2054. ctype fmt idx n)
  2055. (when level
  2056. (when (> level -1)
  2057. (aset org-section-numbers
  2058. level (1+ (aref org-section-numbers level))))
  2059. (setq idx (1+ level))
  2060. (while (<= idx depth)
  2061. (if (not (= idx 1))
  2062. (aset org-section-numbers idx 0))
  2063. (setq idx (1+ idx))))
  2064. (setq idx 0)
  2065. (while (<= idx depth)
  2066. (when (> (aref org-section-numbers idx) 0)
  2067. (setq fmt (or (pop fmts) fmt)
  2068. ctype (car fmt)
  2069. n (aref org-section-numbers idx)
  2070. string (if (> n 0)
  2071. (concat string sep (org-number-to-counter n ctype))
  2072. (concat string ".0"))
  2073. sep (nth 1 fmt)))
  2074. (setq idx (1+ idx)))
  2075. (save-match-data
  2076. (if (string-match "\\`\\([@0]\\.\\)+" string)
  2077. (setq string (replace-match "" t nil string)))
  2078. (if (string-match "\\(\\.0\\)+\\'" string)
  2079. (setq string (replace-match "" t nil string))))
  2080. (concat string term)))
  2081. (defun org-number-to-counter (n type)
  2082. "Concert number N to a string counter, according to TYPE.
  2083. TYPE must be a string, any of:
  2084. 1 number
  2085. A A,B,....
  2086. a a,b,....
  2087. I upper case roman numeral
  2088. i lower case roman numeral"
  2089. (cond
  2090. ((equal type "1") (number-to-string n))
  2091. ((equal type "A") (char-to-string (+ ?A n -1)))
  2092. ((equal type "a") (char-to-string (+ ?a n -1)))
  2093. ((equal type "I") (org-number-to-roman n))
  2094. ((equal type "i") (downcase (org-number-to-roman n)))
  2095. (t (error "Invalid counter type `%s'" type))))
  2096. (defun org-number-to-roman (n)
  2097. "Convert integer N into a roman numeral."
  2098. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  2099. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  2100. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  2101. ( 1 . "I")))
  2102. (res ""))
  2103. (if (<= n 0)
  2104. (number-to-string n)
  2105. (while roman
  2106. (if (>= n (caar roman))
  2107. (setq n (- n (caar roman))
  2108. res (concat res (cdar roman)))
  2109. (pop roman)))
  2110. res)))
  2111. ;;; Macros
  2112. (defun org-export-preprocess-apply-macros ()
  2113. "Replace macro references."
  2114. (goto-char (point-min))
  2115. (let (sy val key args args2 ind-str s n)
  2116. (while (re-search-forward
  2117. "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
  2118. nil t)
  2119. (unless (save-match-data (save-excursion
  2120. (goto-char (point-at-bol))
  2121. (looking-at "[ \t]*#\\+macro")))
  2122. ;; Get macro name (KEY), arguments (ARGS), and indentation of
  2123. ;; current line (IND-STR) as strings.
  2124. (setq key (downcase (match-string 1))
  2125. args (match-string 3)
  2126. ind-str (save-match-data (save-excursion
  2127. (beginning-of-line)
  2128. (looking-at "^\\([ \t]*\\).*")
  2129. (match-string 1))))
  2130. ;; When macro is defined, retrieve replacement text in VAL,
  2131. ;; and proceed with expansion.
  2132. (when (setq val (or (plist-get org-export-opt-plist
  2133. (intern (concat ":macro-" key)))
  2134. (plist-get org-export-opt-plist
  2135. (intern (concat ":" key)))))
  2136. (save-match-data
  2137. ;; If arguments are provided, first retrieve them properly
  2138. ;; (in ARGS, as a list), then replace them in VAL.
  2139. (when args
  2140. (setq args (org-split-string args ",") args2 nil)
  2141. (while args
  2142. (while (string-match "\\\\\\'" (car args))
  2143. ;; Repair bad splits.
  2144. (setcar (cdr args) (concat (substring (car args) 0 -1)
  2145. "," (nth 1 args)))
  2146. (pop args))
  2147. (push (pop args) args2))
  2148. (setq args (mapcar 'org-trim (nreverse args2)))
  2149. (setq s 0)
  2150. (while (string-match "\\$\\([0-9]+\\)" val s)
  2151. (setq s (1+ (match-beginning 0))
  2152. n (string-to-number (match-string 1 val)))
  2153. (and (>= (length args) n)
  2154. (setq val (replace-match (nth (1- n) args) t t val)))))
  2155. ;; VAL starts with "(eval": it is a sexp, `eval' it.
  2156. (when (string-match "\\`(eval\\>" val)
  2157. (setq val (eval (read val))))
  2158. ;; Ensure VAL is a string (or nil) and that each new line
  2159. ;; is indented as the first one.
  2160. (setq val (and val
  2161. (mapconcat 'identity
  2162. (org-split-string
  2163. (if (stringp val) val (format "%s" val))
  2164. "\n")
  2165. (concat "\n" ind-str)))))
  2166. ;; Eventually do the replacement, if VAL isn't nil. Move
  2167. ;; point at beginning of macro for recursive expansions.
  2168. (when val
  2169. (replace-match val t t)
  2170. (goto-char (match-beginning 0))))))))
  2171. (defun org-export-apply-macros-in-string (s)
  2172. "Apply the macros in string S."
  2173. (when s
  2174. (with-temp-buffer
  2175. (insert s)
  2176. (org-export-preprocess-apply-macros)
  2177. (buffer-string))))
  2178. ;;; Include files
  2179. (defun org-export-handle-include-files ()
  2180. "Include the contents of include files, with proper formatting."
  2181. (let ((case-fold-search t)
  2182. params file markup lang start end prefix prefix1 switches all minlevel currentlevel addlevel lines)
  2183. (goto-char (point-min))
  2184. (while (re-search-forward "^#\\+include:[ \t]+\\(.*\\)" nil t)
  2185. (setq params (read (concat "(" (match-string 1) ")"))
  2186. prefix (org-get-and-remove-property 'params :prefix)
  2187. prefix1 (org-get-and-remove-property 'params :prefix1)
  2188. minlevel (org-get-and-remove-property 'params :minlevel)
  2189. addlevel (org-get-and-remove-property 'params :addlevel)
  2190. lines (org-get-and-remove-property 'params :lines)
  2191. file (org-symname-or-string (pop params))
  2192. markup (org-symname-or-string (pop params))
  2193. lang (and (member markup '("src" "SRC"))
  2194. (org-symname-or-string (pop params)))
  2195. switches (mapconcat #'(lambda (x) (format "%s" x)) params " ")
  2196. start nil end nil)
  2197. (delete-region (match-beginning 0) (match-end 0))
  2198. (setq currentlevel (or (org-current-level) 0))
  2199. (if (or (not file)
  2200. (not (file-exists-p file))
  2201. (not (file-readable-p file)))
  2202. (insert (format "CANNOT INCLUDE FILE %s" file))
  2203. (setq all (cons file all))
  2204. (when markup
  2205. (if (equal (downcase markup) "src")
  2206. (setq start (format "#+begin_src %s %s\n"
  2207. (or lang "fundamental")
  2208. (or switches ""))
  2209. end "#+end_src")
  2210. (setq start (format "#+begin_%s %s\n" markup switches)
  2211. end (format "#+end_%s" markup))))
  2212. (insert (or start ""))
  2213. (insert (org-get-file-contents (expand-file-name file)
  2214. prefix prefix1 markup currentlevel minlevel addlevel lines))
  2215. (or (bolp) (newline))
  2216. (insert (or end ""))))
  2217. all))
  2218. (defun org-export-handle-include-files-recurse ()
  2219. "Recursively include files aborting on circular inclusion."
  2220. (let ((now (list org-current-export-file)) all)
  2221. (while now
  2222. (setq all (append now all))
  2223. (setq now (org-export-handle-include-files))
  2224. (let ((intersection
  2225. (delq nil
  2226. (mapcar (lambda (el) (when (member el all) el)) now))))
  2227. (when intersection
  2228. (error "Recursive #+INCLUDE: %S" intersection))))))
  2229. (defun org-get-file-contents (file &optional prefix prefix1 markup minlevel parentlevel addlevel lines)
  2230. "Get the contents of FILE and return them as a string.
  2231. If PREFIX is a string, prepend it to each line. If PREFIX1
  2232. is a string, prepend it to the first line instead of PREFIX.
  2233. If MARKUP, don't protect org-like lines, the exporter will
  2234. take care of the block they are in. If ADDLEVEL is a number,
  2235. demote included file to current heading level+ADDLEVEL.
  2236. If LINES is a string specifying a range of lines,
  2237. include only those lines."
  2238. (if (stringp markup) (setq markup (downcase markup)))
  2239. (with-temp-buffer
  2240. (insert-file-contents file)
  2241. (when lines
  2242. (let* ((lines (split-string lines "-"))
  2243. (lbeg (string-to-number (car lines)))
  2244. (lend (string-to-number (cadr lines)))
  2245. (beg (if (zerop lbeg) (point-min)
  2246. (goto-char (point-min))
  2247. (forward-line (1- lbeg))
  2248. (point)))
  2249. (end (if (zerop lend) (point-max)
  2250. (goto-char (point-min))
  2251. (forward-line (1- lend))
  2252. (point))))
  2253. (narrow-to-region beg end)))
  2254. (when (or prefix prefix1)
  2255. (goto-char (point-min))
  2256. (while (not (eobp))
  2257. (insert (or prefix1 prefix))
  2258. (setq prefix1 "")
  2259. (beginning-of-line 2)))
  2260. (buffer-string)
  2261. (when (member markup '("src" "example"))
  2262. (goto-char (point-min))
  2263. (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
  2264. (goto-char (match-beginning 0))
  2265. (insert ",")
  2266. (end-of-line 1)))
  2267. (when minlevel
  2268. (dotimes (lvl minlevel)
  2269. (org-map-region 'org-demote (point-min) (point-max))))
  2270. (when addlevel
  2271. (let ((inclevel (or (if (org-before-first-heading-p)
  2272. (1- (and (outline-next-heading)
  2273. (org-current-level)))
  2274. (1- (org-current-level)))
  2275. 0)))
  2276. (dotimes (level (- (+ parentlevel addlevel) inclevel))
  2277. (org-map-region 'org-demote (point-min) (point-max)))))
  2278. (buffer-string)))
  2279. (defun org-get-and-remove-property (listvar prop)
  2280. "Check if the value of LISTVAR contains PROP as a property.
  2281. If yes, return the value of that property (i.e. the element following
  2282. in the list) and remove property and value from the list in LISTVAR."
  2283. (let ((list (symbol-value listvar)) m v)
  2284. (when (setq m (member prop list))
  2285. (setq v (nth 1 m))
  2286. (if (equal (car list) prop)
  2287. (set listvar (cddr list))
  2288. (setcdr (nthcdr (- (length list) (length m) 1) list)
  2289. (cddr m))
  2290. (set listvar list)))
  2291. v))
  2292. (defun org-symname-or-string (s)
  2293. (if (symbolp s)
  2294. (if s (symbol-name s) s)
  2295. s))
  2296. ;;; Fontification and line numbers for code examples
  2297. (defvar org-export-last-code-line-counter-value 0)
  2298. (defun org-export-replace-src-segments-and-examples ()
  2299. "Replace source code segments with special code for export."
  2300. (setq org-export-last-code-line-counter-value 0)
  2301. (let ((case-fold-search t)
  2302. lang code trans opts indent caption)
  2303. (goto-char (point-min))
  2304. (while (re-search-forward
  2305. "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)"
  2306. nil t)
  2307. (if (match-end 1)
  2308. (if (not (match-string 4))
  2309. (error "Source block missing language specification: %s"
  2310. (let* ((body (match-string 6))
  2311. (nothing (message "body:%s" body))
  2312. (preview (or (and (string-match
  2313. "^[ \t]*\\([^\n\r]*\\)" body)
  2314. (match-string 1 body)) body)))
  2315. (if (> (length preview) 35)
  2316. (concat (substring preview 0 32) "...")
  2317. preview)))
  2318. ;; src segments
  2319. (setq lang (match-string 4)
  2320. opts (match-string 5)
  2321. code (match-string 6)
  2322. indent (length (match-string 2))
  2323. caption (get-text-property 0 'org-caption (match-string 0))))
  2324. (setq lang nil
  2325. opts (match-string 9)
  2326. code (match-string 10)
  2327. indent (length (match-string 8))
  2328. caption (get-text-property 0 'org-caption (match-string 0))))
  2329. (setq trans (org-export-format-source-code-or-example
  2330. lang code opts indent caption))
  2331. (replace-match trans t t))))
  2332. (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
  2333. (defvar org-export-latex-listings) ;; defined in org-latex.el
  2334. (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
  2335. (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el
  2336. (defvar org-export-latex-minted-langs) ;; defined in org-latex.el
  2337. (defvar org-export-latex-custom-lang-environments) ;; defined in org-latex.el
  2338. (defvar org-export-latex-listings-options) ;; defined in org-latex.el
  2339. (defvar org-export-latex-minted-options) ;; defined in org-latex.el
  2340. (defun org-remove-formatting-on-newlines-in-region (beg end)
  2341. "Remove formatting on newline characters."
  2342. (interactive "r")
  2343. (save-excursion
  2344. (goto-char beg)
  2345. (while (progn (end-of-line) (< (point) end))
  2346. (put-text-property (point) (1+ (point)) 'face nil)
  2347. (forward-char 1))))
  2348. (defun org-export-format-source-code-or-example
  2349. (lang code &optional opts indent caption)
  2350. "Format CODE from language LANG and return it formatted for export.
  2351. The CODE is marked up in `org-export-current-backend' format.
  2352. Check if a function by name
  2353. \"org-<backend>-format-source-code-or-example\" is bound. If yes,
  2354. use it as the custom formatter. Otherwise, use the default
  2355. formatter. Default formatters are provided for docbook, html,
  2356. latex and ascii backends. For example, use
  2357. `org-html-format-source-code-or-example' to provide a custom
  2358. formatter for export to \"html\".
  2359. If LANG is nil, do not add any fontification.
  2360. OPTS contains formatting options, like `-n' for triggering numbering lines,
  2361. and `+n' for continuing previous numbering.
  2362. Code formatting according to language currently only works for HTML.
  2363. Numbering lines works for all three major backends (html, latex, and ascii).
  2364. INDENT was the original indentation of the block."
  2365. (save-match-data
  2366. (let* ((backend-name (symbol-name org-export-current-backend))
  2367. (backend-formatter
  2368. (intern (format "org-%s-format-source-code-or-example"
  2369. backend-name)))
  2370. (backend-feature (intern (concat "org-" backend-name)))
  2371. (backend-formatter
  2372. (and (require (intern (concat "org-" backend-name)) nil)
  2373. (fboundp backend-formatter) backend-formatter))
  2374. num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
  2375. (setq opts (or opts "")
  2376. num (string-match "[-+]n\\>" opts)
  2377. cont (string-match "\\+n\\>" opts)
  2378. rpllbl (string-match "-r\\>" opts)
  2379. keepp (string-match "-k\\>" opts)
  2380. textareap (string-match "-t\\>" opts)
  2381. preserve-indentp (or org-src-preserve-indentation
  2382. (string-match "-i\\>" opts))
  2383. cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
  2384. (string-to-number (match-string 1 opts))
  2385. 80)
  2386. rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
  2387. (string-to-number (match-string 1 opts))
  2388. (org-count-lines code))
  2389. fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
  2390. (match-string 1 opts)))
  2391. (when (and textareap (eq org-export-current-backend 'html))
  2392. ;; we cannot use numbering or highlighting.
  2393. (setq num nil cont nil lang nil))
  2394. (if keepp (setq rpllbl 'keep))
  2395. (setq rtn (if preserve-indentp code (org-remove-indentation code)))
  2396. (when (string-match "^," rtn)
  2397. (setq rtn (with-temp-buffer
  2398. (insert rtn)
  2399. ;; Free up the protected lines
  2400. (goto-char (point-min))
  2401. (while (re-search-forward "^," nil t)
  2402. (if (or (equal lang "org")
  2403. (save-match-data
  2404. (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
  2405. (replace-match ""))
  2406. (end-of-line 1))
  2407. (buffer-string))))
  2408. ;; Now backend-specific coding
  2409. (setq rtn
  2410. (cond
  2411. (backend-formatter
  2412. (funcall backend-formatter rtn lang caption textareap cols rows num
  2413. cont rpllbl fmt))
  2414. ((eq org-export-current-backend 'docbook)
  2415. (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
  2416. (concat "<programlisting><![CDATA["
  2417. rtn
  2418. "]]></programlisting>\n"))
  2419. ((eq org-export-current-backend 'html)
  2420. ;; We are exporting to HTML
  2421. (when lang
  2422. (if (featurep 'xemacs)
  2423. (require 'htmlize)
  2424. (require 'htmlize nil t))
  2425. (when (not (fboundp 'htmlize-region-for-paste))
  2426. ;; we do not have htmlize.el, or an old version of it
  2427. (setq lang nil)
  2428. (message
  2429. "htmlize.el 1.34 or later is needed for source code formatting")))
  2430. (if lang
  2431. (let* ((lang-m (when lang
  2432. (or (cdr (assoc lang org-src-lang-modes))
  2433. lang)))
  2434. (mode (and lang-m (intern
  2435. (concat
  2436. (if (symbolp lang-m)
  2437. (symbol-name lang-m)
  2438. lang-m)
  2439. "-mode"))))
  2440. (org-inhibit-startup t)
  2441. (org-startup-folded nil))
  2442. (setq rtn
  2443. (with-temp-buffer
  2444. (insert rtn)
  2445. (if (functionp mode)
  2446. (funcall mode)
  2447. (fundamental-mode))
  2448. (font-lock-fontify-buffer)
  2449. ;; markup each line separately
  2450. (org-remove-formatting-on-newlines-in-region (point-min) (point-max))
  2451. (org-src-mode)
  2452. (set-buffer-modified-p nil)
  2453. (org-export-htmlize-region-for-paste
  2454. (point-min) (point-max))))
  2455. (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
  2456. (setq rtn
  2457. (concat
  2458. (if caption
  2459. (concat
  2460. "<div class=\"org-src-container\">"
  2461. (format
  2462. "<label class=\"org-src-name\">%s</label>"
  2463. caption))
  2464. "")
  2465. (replace-match
  2466. (format "<pre class=\"src src-%s\">\n" lang)
  2467. t t rtn)
  2468. (if caption "</div>" "")))))
  2469. (if textareap
  2470. (setq rtn (concat
  2471. (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
  2472. cols rows)
  2473. rtn "</textarea>\n</p>\n"))
  2474. (with-temp-buffer
  2475. (insert rtn)
  2476. (goto-char (point-min))
  2477. (while (re-search-forward "[<>&]" nil t)
  2478. (replace-match (cdr (assq (char-before)
  2479. '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
  2480. t t))
  2481. (setq rtn (buffer-string)))
  2482. (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
  2483. (unless textareap
  2484. (setq rtn (org-export-number-lines rtn 1 1 num cont rpllbl fmt)))
  2485. (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
  2486. (setq rtn (replace-match "\\1" t nil rtn)))
  2487. rtn)
  2488. ((eq org-export-current-backend 'latex)
  2489. (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
  2490. (cond
  2491. ((and lang org-export-latex-listings)
  2492. (let* ((make-option-string
  2493. (lambda (pair)
  2494. (concat (first pair)
  2495. (if (> (length (second pair)) 0)
  2496. (concat "=" (second pair))))))
  2497. (lang-sym (intern lang))
  2498. (minted-p (eq org-export-latex-listings 'minted))
  2499. (listings-p (not minted-p))
  2500. (backend-lang
  2501. (or (cadr
  2502. (assq
  2503. lang-sym
  2504. (cond
  2505. (minted-p org-export-latex-minted-langs)
  2506. (listings-p org-export-latex-listings-langs))))
  2507. lang))
  2508. (custom-environment
  2509. (cadr
  2510. (assq
  2511. lang-sym
  2512. org-export-latex-custom-lang-environments))))
  2513. (concat
  2514. (when (and listings-p (not custom-environment))
  2515. (format
  2516. "\\lstset{%s}\n"
  2517. (mapconcat
  2518. make-option-string
  2519. (append org-export-latex-listings-options
  2520. `(("language" ,backend-lang))) ",")))
  2521. (when (and caption org-export-latex-listings-w-names)
  2522. (format
  2523. "\n%s $\\equiv$ \n"
  2524. (replace-regexp-in-string "_" "\\\\_" caption)))
  2525. (cond
  2526. (custom-environment
  2527. (format "\\begin{%s}\n%s\\end{%s}\n"
  2528. custom-environment rtn custom-environment))
  2529. (listings-p
  2530. (format "\\begin{%s}\n%s\\end{%s}"
  2531. "lstlisting" rtn "lstlisting"))
  2532. (minted-p
  2533. (format
  2534. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  2535. (mapconcat make-option-string
  2536. org-export-latex-minted-options ",")
  2537. backend-lang rtn))))))
  2538. (t (concat (car org-export-latex-verbatim-wrap)
  2539. rtn (cdr org-export-latex-verbatim-wrap)))))
  2540. ((eq org-export-current-backend 'ascii)
  2541. ;; This is not HTML or LaTeX, so just make it an example.
  2542. (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
  2543. (concat caption "\n"
  2544. (concat
  2545. (mapconcat
  2546. (lambda (l) (concat " " l))
  2547. (org-split-string rtn "\n")
  2548. "\n")
  2549. "\n")))
  2550. (t
  2551. (error "Don't know how to markup source or example block in %s"
  2552. (upcase backend-name)))))
  2553. (setq rtn
  2554. (concat
  2555. "\n#+BEGIN_" backend-name "\n"
  2556. (org-add-props rtn
  2557. '(org-protected t org-example t org-native-text t))
  2558. "\n#+END_" backend-name "\n"))
  2559. (org-add-props rtn nil 'original-indentation indent))))
  2560. (defun org-export-number-lines (text &optional skip1 skip2 number cont
  2561. replace-labels label-format preprocess)
  2562. "Apply line numbers to literal examples and handle code references.
  2563. Handle user-specified options under info node `(org)Literal
  2564. examples' and return the modified source block.
  2565. TEXT contains the source or example block.
  2566. SKIP1 and SKIP2 are the number of lines that are to be skipped at
  2567. the beginning and end of TEXT. Use these to skip over
  2568. backend-specific lines pre-pended or appended to the original
  2569. source block.
  2570. NUMBER is non-nil if the literal example specifies \"+n\" or
  2571. \"-n\" switch. If NUMBER is non-nil add line numbers.
  2572. CONT is non-nil if the literal example specifies \"+n\" switch.
  2573. If CONT is nil, start numbering this block from 1. Otherwise
  2574. continue numbering from the last numbered block.
  2575. REPLACE-LABELS is dual-purpose.
  2576. 1. It controls the retention of labels in the exported block.
  2577. 2. It specifies in what manner the links (or references) to a
  2578. labeled line be formatted.
  2579. REPLACE-LABELS is the symbol `keep' if the literal example
  2580. specifies \"-k\" option, is numeric if the literal example
  2581. specifies \"-r\" option and is nil otherwise.
  2582. Handle REPLACE-LABELS as below:
  2583. - If nil, retain labels in the exported block and use
  2584. user-provided labels for referencing the labeled lines.
  2585. - If it is a number, remove labels in the exported block and use
  2586. one of line numbers or labels for referencing labeled lines based
  2587. on NUMBER option.
  2588. - If it is a keep, retain labels in the exported block and use
  2589. one of line numbers or labels for referencing labeled lines
  2590. based on NUMBER option.
  2591. LABEL-FORMAT is the value of \"-l\" switch associated with
  2592. literal example. See `org-coderef-label-format'.
  2593. PREPROCESS is intended for backend-agnostic handling of source
  2594. block numbering. When non-nil do the following:
  2595. - do not number the lines
  2596. - always strip the labels from exported block
  2597. - do not make the labeled line a target of an incoming link.
  2598. Instead mark the labeled line with `org-coderef' property and
  2599. store the label in it."
  2600. (setq skip1 (or skip1 0) skip2 (or skip2 0))
  2601. (if (and number (not cont)) (setq org-export-last-code-line-counter-value 0))
  2602. (with-temp-buffer
  2603. (insert text)
  2604. (goto-char (point-max))
  2605. (skip-chars-backward " \t\n\r")
  2606. (delete-region (point) (point-max))
  2607. (beginning-of-line (- 1 skip2))
  2608. (let* ((last (org-current-line))
  2609. (n org-export-last-code-line-counter-value)
  2610. (nmax (+ n (- last skip1)))
  2611. (fmt (format "%%%dd: " (length (number-to-string nmax))))
  2612. (fm
  2613. (cond
  2614. ((eq org-export-current-backend 'html) (format "<span class=\"linenr\">%s</span>"
  2615. fmt))
  2616. ((eq org-export-current-backend 'ascii) fmt)
  2617. ((eq org-export-current-backend 'latex) fmt)
  2618. ((eq org-export-current-backend 'docbook) fmt)
  2619. (t "")))
  2620. (label-format (or label-format org-coderef-label-format))
  2621. (label-pre (if (string-match "%s" label-format)
  2622. (substring label-format 0 (match-beginning 0))
  2623. label-format))
  2624. (label-post (if (string-match "%s" label-format)
  2625. (substring label-format (match-end 0))
  2626. ""))
  2627. (lbl-re
  2628. (concat
  2629. ".*?\\S-.*?\\([ \t]*\\("
  2630. (regexp-quote label-pre)
  2631. "\\([-a-zA-Z0-9_ ]+\\)"
  2632. (regexp-quote label-post)
  2633. "\\)\\)"))
  2634. ref)
  2635. (org-goto-line (1+ skip1))
  2636. (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
  2637. (when number (incf n))
  2638. (if (or preprocess (not number))
  2639. (forward-char 1)
  2640. (insert (format fm n)))
  2641. (when (looking-at lbl-re)
  2642. (setq ref (match-string 3))
  2643. (cond ((numberp replace-labels)
  2644. ;; remove labels; use numbers for references when lines
  2645. ;; are numbered, use labels otherwise
  2646. (delete-region (match-beginning 1) (match-end 1))
  2647. (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
  2648. ((eq replace-labels 'keep)
  2649. ;; don't remove labels; use numbers for references when
  2650. ;; lines are numbered, use labels otherwise
  2651. (goto-char (match-beginning 2))
  2652. (delete-region (match-beginning 2) (match-end 2))
  2653. (unless preprocess
  2654. (insert "(" ref ")"))
  2655. (push (cons ref (if (> n 0) n (concat "(" ref ")")))
  2656. org-export-code-refs))
  2657. (t
  2658. ;; don't remove labels and don't use numbers for
  2659. ;; references
  2660. (goto-char (match-beginning 2))
  2661. (delete-region (match-beginning 2) (match-end 2))
  2662. (unless preprocess
  2663. (insert "(" ref ")"))
  2664. (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
  2665. (when (and (eq org-export-current-backend 'html) (not preprocess))
  2666. (save-excursion
  2667. (beginning-of-line 1)
  2668. (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
  2669. ref))
  2670. (end-of-line 1)
  2671. (insert "</span>")))
  2672. (when preprocess
  2673. (add-text-properties
  2674. (point-at-bol) (point-at-eol) (list 'org-coderef ref)))))
  2675. (setq org-export-last-code-line-counter-value n)
  2676. (goto-char (point-max))
  2677. (newline)
  2678. (buffer-string))))
  2679. (defun org-search-todo-below (line lines level)
  2680. "Search the subtree below LINE for any TODO entries."
  2681. (let ((rest (cdr (memq line lines)))
  2682. (re org-todo-line-regexp)
  2683. line lv todo)
  2684. (catch 'exit
  2685. (while (setq line (pop rest))
  2686. (if (string-match re line)
  2687. (progn
  2688. (setq lv (- (match-end 1) (match-beginning 1))
  2689. todo (and (match-beginning 2)
  2690. (not (member (match-string 2 line)
  2691. org-done-keywords))))
  2692. ; TODO, not DONE
  2693. (if (<= lv level) (throw 'exit nil))
  2694. (if todo (throw 'exit t))))))))
  2695. ;;;###autoload
  2696. (defun org-export-visible (type arg)
  2697. "Create a copy of the visible part of the current buffer, and export it.
  2698. The copy is created in a temporary buffer and removed after use.
  2699. TYPE is the final key (as a string) that also selects the export command in
  2700. the \\<org-mode-map>\\[org-export] export dispatcher.
  2701. As a special case, if the you type SPC at the prompt, the temporary
  2702. org-mode file will not be removed but presented to you so that you can
  2703. continue to use it. The prefix arg ARG is passed through to the exporting
  2704. command."
  2705. (interactive
  2706. (list (progn
  2707. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]buffer with HTML [D]ocBook [l]atex [p]df [d]view pdf [L]atex buffer [x]OXO [ ]keep buffer")
  2708. (read-char-exclusive))
  2709. current-prefix-arg))
  2710. (if (not (member type '(?a ?n ?u ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L ?H ?R)))
  2711. (error "Invalid export key"))
  2712. (let* ((binding (cdr (assoc type
  2713. '(
  2714. (?a . org-export-as-ascii)
  2715. (?A . org-export-as-ascii-to-buffer)
  2716. (?n . org-export-as-latin1)
  2717. (?N . org-export-as-latin1-to-buffer)
  2718. (?u . org-export-as-utf8)
  2719. (?U . org-export-as-utf8-to-buffer)
  2720. (?\C-a . org-export-as-ascii)
  2721. (?b . org-export-as-html-and-open)
  2722. (?\C-b . org-export-as-html-and-open)
  2723. (?h . org-export-as-html)
  2724. (?H . org-export-as-html-to-buffer)
  2725. (?R . org-export-region-as-html)
  2726. (?D . org-export-as-docbook)
  2727. (?l . org-export-as-latex)
  2728. (?p . org-export-as-pdf)
  2729. (?d . org-export-as-pdf-and-open)
  2730. (?L . org-export-as-latex-to-buffer)
  2731. (?x . org-export-as-xoxo)))))
  2732. (keepp (equal type ?\ ))
  2733. (file buffer-file-name)
  2734. (buffer (get-buffer-create "*Org Export Visible*"))
  2735. s e)
  2736. ;; Need to hack the drawers here.
  2737. (save-excursion
  2738. (goto-char (point-min))
  2739. (while (re-search-forward org-drawer-regexp nil t)
  2740. (goto-char (match-beginning 1))
  2741. (or (outline-invisible-p) (org-flag-drawer nil))))
  2742. (with-current-buffer buffer (erase-buffer))
  2743. (save-excursion
  2744. (setq s (goto-char (point-min)))
  2745. (while (not (= (point) (point-max)))
  2746. (goto-char (org-find-invisible))
  2747. (append-to-buffer buffer s (point))
  2748. (setq s (goto-char (org-find-visible))))
  2749. (org-cycle-hide-drawers 'all)
  2750. (goto-char (point-min))
  2751. (unless keepp
  2752. ;; Copy all comment lines to the end, to make sure #+ settings are
  2753. ;; still available for the second export step. Kind of a hack, but
  2754. ;; does do the trick.
  2755. (if (looking-at "#[^\r\n]*")
  2756. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  2757. (when (re-search-forward "^\\*+[ \t]+" nil t)
  2758. (while (re-search-backward "[\n\r]#[^\n\r]*" nil t)
  2759. (append-to-buffer buffer (1+ (match-beginning 0))
  2760. (min (point-max) (1+ (match-end 0)))))))
  2761. (set-buffer buffer)
  2762. (let ((buffer-file-name file)
  2763. (org-inhibit-startup t))
  2764. (org-mode)
  2765. (show-all)
  2766. (unless keepp (funcall binding arg))))
  2767. (if (not keepp)
  2768. (kill-buffer buffer)
  2769. (switch-to-buffer-other-window buffer)
  2770. (goto-char (point-min)))))
  2771. (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
  2772. (defun org-export-string (string fmt &optional dir)
  2773. "Export STRING to FMT using existing export facilities.
  2774. During export STRING is saved to a temporary file whose location
  2775. could vary. Optional argument DIR can be used to force the
  2776. directory in which the temporary file is created during export
  2777. which can be useful for resolving relative paths. Dir defaults
  2778. to the value of `temporary-file-directory'."
  2779. (let ((temporary-file-directory (or dir temporary-file-directory))
  2780. (tmp-file (make-temp-file "org-")))
  2781. (unwind-protect
  2782. (with-temp-buffer
  2783. (insert string)
  2784. (write-file tmp-file)
  2785. (org-load-modules-maybe)
  2786. (unless org-local-vars
  2787. (setq org-local-vars (org-get-local-variables)))
  2788. (eval ;; convert to fmt -- mimicking `org-run-like-in-org-mode'
  2789. (list 'let org-local-vars
  2790. (list (intern (format "org-export-as-%s" fmt))
  2791. nil nil ''string t dir))))
  2792. (delete-file tmp-file))))
  2793. ;;;###autoload
  2794. (defun org-export-as-org (arg &optional ext-plist to-buffer body-only pub-dir)
  2795. "Make a copy with not-exporting stuff removed.
  2796. The purpose of this function is to provide a way to export the source
  2797. Org file of a webpage in Org format, but with sensitive and/or irrelevant
  2798. stuff removed. This command will remove the following:
  2799. - archived trees (if the variable `org-export-with-archived-trees' is nil)
  2800. - comment blocks and trees starting with the COMMENT keyword
  2801. - only trees that are consistent with `org-export-select-tags'
  2802. and `org-export-exclude-tags'.
  2803. The only arguments that will be used are EXT-PLIST and PUB-DIR,
  2804. all the others will be ignored (but are present so that the general
  2805. mechanism to call publishing functions will work).
  2806. EXT-PLIST is a property list with external parameters overriding
  2807. org-mode's default settings, but still inferior to file-local
  2808. settings. When PUB-DIR is set, use this as the publishing
  2809. directory."
  2810. (interactive "P")
  2811. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  2812. ext-plist
  2813. (org-infile-export-plist)))
  2814. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  2815. (filename (concat (file-name-as-directory
  2816. (or pub-dir
  2817. (org-export-directory :org opt-plist)))
  2818. (file-name-sans-extension
  2819. (file-name-nondirectory bfname))
  2820. ".org"))
  2821. (filename (and filename
  2822. (if (equal (file-truename filename)
  2823. (file-truename bfname))
  2824. (concat (file-name-sans-extension filename)
  2825. "-source."
  2826. (file-name-extension filename))
  2827. filename)))
  2828. (backup-inhibited t)
  2829. (buffer (find-file-noselect filename))
  2830. (region (buffer-string))
  2831. str-ret)
  2832. (save-excursion
  2833. (org-pop-to-buffer-same-window buffer)
  2834. (erase-buffer)
  2835. (insert region)
  2836. (let ((org-inhibit-startup t)) (org-mode))
  2837. (org-install-letbind)
  2838. ;; Get rid of archived trees
  2839. (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
  2840. ;; Remove comment environment and comment subtrees
  2841. (org-export-remove-comment-blocks-and-subtrees)
  2842. ;; Get rid of excluded trees
  2843. (org-export-handle-export-tags (plist-get opt-plist :select-tags)
  2844. (plist-get opt-plist :exclude-tags))
  2845. (when (or (plist-get opt-plist :plain-source)
  2846. (not (or (plist-get opt-plist :plain-source)
  2847. (plist-get opt-plist :htmlized-source))))
  2848. ;; Either nothing special is requested (default call)
  2849. ;; or the plain source is explicitly requested
  2850. ;; so: save it
  2851. (save-buffer))
  2852. (when (plist-get opt-plist :htmlized-source)
  2853. ;; Make the htmlized version
  2854. (require 'htmlize)
  2855. (require 'org-html)
  2856. (font-lock-fontify-buffer)
  2857. (let* ((htmlize-output-type 'css)
  2858. (newbuf (htmlize-buffer)))
  2859. (with-current-buffer newbuf
  2860. (when org-export-htmlized-org-css-url
  2861. (goto-char (point-min))
  2862. (and (re-search-forward
  2863. "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
  2864. nil t)
  2865. (replace-match
  2866. (format
  2867. "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
  2868. org-export-htmlized-org-css-url)
  2869. t t)))
  2870. (write-file (concat filename ".html")))
  2871. (kill-buffer newbuf)))
  2872. (set-buffer-modified-p nil)
  2873. (if (equal to-buffer 'string)
  2874. (progn (setq str-ret (buffer-string))
  2875. (kill-buffer (current-buffer))
  2876. str-ret)
  2877. (kill-buffer (current-buffer))))))
  2878. (defvar org-archive-location) ;; gets loaded with the org-archive require.
  2879. (defun org-get-current-options ()
  2880. "Return a string with current options as keyword options.
  2881. Does include HTML export options as well as TODO and CATEGORY stuff."
  2882. (require 'org-archive)
  2883. (format
  2884. "#+TITLE: %s
  2885. #+AUTHOR: %s
  2886. #+EMAIL: %s
  2887. #+DATE: %s
  2888. #+DESCRIPTION:
  2889. #+KEYWORDS:
  2890. #+LANGUAGE: %s
  2891. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s
  2892. #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s
  2893. %s
  2894. #+EXPORT_SELECT_TAGS: %s
  2895. #+EXPORT_EXCLUDE_TAGS: %s
  2896. #+LINK_UP: %s
  2897. #+LINK_HOME: %s
  2898. #+XSLT:
  2899. #+CATEGORY: %s
  2900. #+SEQ_TODO: %s
  2901. #+TYP_TODO: %s
  2902. #+PRIORITIES: %c %c %c
  2903. #+DRAWERS: %s
  2904. #+STARTUP: %s %s %s %s %s
  2905. #+TAGS: %s
  2906. #+FILETAGS: %s
  2907. #+ARCHIVE: %s
  2908. #+LINK: %s
  2909. "
  2910. (buffer-name) (user-full-name) user-mail-address
  2911. (format-time-string (substring (car org-time-stamp-formats) 1 -1))
  2912. org-export-default-language
  2913. org-export-headline-levels
  2914. org-export-with-section-numbers
  2915. org-export-with-toc
  2916. org-export-preserve-breaks
  2917. org-export-html-expand
  2918. org-export-with-fixed-width
  2919. org-export-with-tables
  2920. org-export-with-sub-superscripts
  2921. org-export-with-special-strings
  2922. org-export-with-footnotes
  2923. org-export-with-emphasize
  2924. org-export-with-timestamps
  2925. org-export-with-TeX-macros
  2926. org-export-with-LaTeX-fragments
  2927. org-export-skip-text-before-1st-heading
  2928. org-export-with-drawers
  2929. org-export-with-todo-keywords
  2930. org-export-with-priority
  2931. org-export-with-tags
  2932. (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
  2933. (mapconcat 'identity org-export-select-tags " ")
  2934. (mapconcat 'identity org-export-exclude-tags " ")
  2935. org-export-html-link-up
  2936. org-export-html-link-home
  2937. (or (ignore-errors
  2938. (file-name-sans-extension
  2939. (file-name-nondirectory (buffer-file-name (buffer-base-buffer)))))
  2940. "NOFILENAME")
  2941. "TODO FEEDBACK VERIFY DONE"
  2942. "Me Jason Marie DONE"
  2943. org-highest-priority org-lowest-priority org-default-priority
  2944. (mapconcat 'identity org-drawers " ")
  2945. (cdr (assoc org-startup-folded
  2946. '((nil . "showall") (t . "overview") (content . "content"))))
  2947. (if org-odd-levels-only "odd" "oddeven")
  2948. (if org-hide-leading-stars "hidestars" "showstars")
  2949. (if org-startup-align-all-tables "align" "noalign")
  2950. (cond ((eq org-log-done t) "logdone")
  2951. ((equal org-log-done 'note) "lognotedone")
  2952. ((not org-log-done) "nologdone"))
  2953. (or (mapconcat (lambda (x)
  2954. (cond
  2955. ((equal :startgroup (car x)) "{")
  2956. ((equal :endgroup (car x)) "}")
  2957. ((equal :newline (car x)) "")
  2958. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  2959. (t (car x))))
  2960. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  2961. (mapconcat 'identity org-file-tags " ")
  2962. org-archive-location
  2963. "org file:~/org/%s.org"))
  2964. (defun org-insert-export-options-template ()
  2965. "Insert into the buffer a template with information for exporting."
  2966. (interactive)
  2967. (if (not (bolp)) (newline))
  2968. (let ((s (org-get-current-options)))
  2969. (and (string-match "#\\+CATEGORY" s)
  2970. (setq s (substring s 0 (match-beginning 0))))
  2971. (insert s)))
  2972. (defvar org-table-colgroup-info nil)
  2973. (defun org-table-clean-before-export (lines &optional maybe-quoted)
  2974. "Check if the table has a marking column.
  2975. If yes remove the column and the special lines."
  2976. (setq org-table-colgroup-info nil)
  2977. (if (memq nil
  2978. (mapcar
  2979. (lambda (x) (or (string-match "^[ \t]*|-" x)
  2980. (string-match
  2981. (if maybe-quoted
  2982. "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|"
  2983. "^[ \t]*| *\\([\#!$*_^ /]\\) *|")
  2984. x)))
  2985. lines))
  2986. ;; No special marking column
  2987. (progn
  2988. (setq org-table-clean-did-remove-column nil)
  2989. (delq nil
  2990. (mapcar
  2991. (lambda (x)
  2992. (cond
  2993. ((org-table-colgroup-line-p x)
  2994. ;; This line contains colgroup info, extract it
  2995. ;; and then discard the line
  2996. (setq org-table-colgroup-info
  2997. (mapcar (lambda (x)
  2998. (cond ((member x '("<" "&lt;")) :start)
  2999. ((member x '(">" "&gt;")) :end)
  3000. ((member x '("<>" "&lt;&gt;")) :startend)))
  3001. (org-split-string x "[ \t]*|[ \t]*")))
  3002. nil)
  3003. ((org-table-cookie-line-p x)
  3004. ;; This line contains formatting cookies, discard it
  3005. nil)
  3006. (t x)))
  3007. lines)))
  3008. ;; there is a special marking column
  3009. (setq org-table-clean-did-remove-column t)
  3010. (delq nil
  3011. (mapcar
  3012. (lambda (x)
  3013. (cond
  3014. ((org-table-colgroup-line-p x)
  3015. ;; This line contains colgroup info, extract it
  3016. ;; and then discard the line
  3017. (setq org-table-colgroup-info
  3018. (mapcar (lambda (x)
  3019. (cond ((member x '("<" "&lt;")) :start)
  3020. ((member x '(">" "&gt;")) :end)
  3021. ((member x '("<>" "&lt;&gt;")) :startend)))
  3022. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  3023. nil)
  3024. ((org-table-cookie-line-p x)
  3025. ;; This line contains formatting cookies, discard it
  3026. nil)
  3027. ((string-match "^[ \t]*| *\\([!_^/$]\\|\\\\\\$\\) *|" x)
  3028. ;; ignore this line
  3029. nil)
  3030. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  3031. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  3032. ;; remove the first column
  3033. (replace-match "\\1|" t nil x))))
  3034. lines))))
  3035. (defun org-export-cleanup-toc-line (s)
  3036. "Remove tags and timestamps from lines going into the toc."
  3037. (if (not s)
  3038. "" ; Return a string when argument is nil
  3039. (when (memq org-export-with-tags '(not-in-toc nil))
  3040. (if (string-match (org-re " +:[[:alnum:]_@#%:]+: *$") s)
  3041. (setq s (replace-match "" t t s))))
  3042. (when org-export-remove-timestamps-from-toc
  3043. (while (string-match org-maybe-keyword-time-regexp s)
  3044. (setq s (replace-match "" t t s))))
  3045. (while (string-match org-bracket-link-regexp s)
  3046. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  3047. t t s)))
  3048. (while (string-match "\\[\\([0-9]\\|fn:[^]]*\\)\\]" s)
  3049. (setq s (replace-match "" t t s)))
  3050. s))
  3051. (defun org-get-text-property-any (pos prop &optional object)
  3052. (or (get-text-property pos prop object)
  3053. (and (setq pos (next-single-property-change pos prop object))
  3054. (get-text-property pos prop object))))
  3055. (defun org-export-get-coderef-format (path desc)
  3056. (save-match-data
  3057. (if (and desc (string-match
  3058. (regexp-quote (concat "(" path ")"))
  3059. desc))
  3060. (replace-match "%s" t t desc)
  3061. (or desc "%s"))))
  3062. (defun org-export-push-to-kill-ring (format)
  3063. "Push buffer content to kill ring.
  3064. The depends on the variable `org-export-copy-to-kill-ring'."
  3065. (when (or (and (eq org-export-copy-to-kill-ring 'if-interactive)
  3066. (not (or executing-kbd-macro noninteractive)))
  3067. (eq org-export-copy-to-kill-ring t))
  3068. (org-kill-new (buffer-string))
  3069. (when (fboundp 'x-set-selection)
  3070. (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
  3071. (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
  3072. (message "%s export done, pushed to kill ring and clipboard" format)))
  3073. (provide 'org-exp)
  3074. ;; Local variables:
  3075. ;; generated-autoload-file: "org-loaddefs.el"
  3076. ;; End:
  3077. ;;; org-exp.el ends here