org-exp.el 126 KB

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