org-exp.el 124 KB

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