org-exp.el 126 KB

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