org-exp.el 126 KB

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