org-exp.el 126 KB

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