org-element.el 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478
  1. ;;; org-element.el --- Parser And Applications for Org syntax
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; This file is not part of GNU Emacs.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; Org syntax can be divided into three categories: "Greater
  19. ;; elements", "Elements" and "Objects".
  20. ;;
  21. ;; Elements are related to the structure of the document. Indeed, all
  22. ;; elements are a cover for the document: each position within belongs
  23. ;; to at least one element.
  24. ;;
  25. ;; An element always starts and ends at the beginning of a line. With
  26. ;; a few exceptions (namely `babel-call', `clock', `headline', `item',
  27. ;; `keyword', `planning', `property-drawer' and `section' types), it
  28. ;; can also accept a fixed set of keywords as attributes. Those are
  29. ;; called "affiliated keywords" to distinguish them from other
  30. ;; keywords, which are full-fledged elements. Almost all affiliated
  31. ;; keywords are referenced in `org-element-affiliated-keywords'; the
  32. ;; others are export attributes and start with "ATTR_" prefix.
  33. ;;
  34. ;; Element containing other elements (and only elements) are called
  35. ;; greater elements. Concerned types are: `center-block', `drawer',
  36. ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
  37. ;; `item', `plain-list', `quote-block', `section' and `special-block'.
  38. ;;
  39. ;; Other element types are: `babel-call', `clock', `comment',
  40. ;; `comment-block', `example-block', `export-block', `fixed-width',
  41. ;; `horizontal-rule', `keyword', `latex-environment', `paragraph',
  42. ;; `planning', `property-drawer', `quote-section', `src-block',
  43. ;; `table', `table-row' and `verse-block'. Among them, `paragraph'
  44. ;; and `verse-block' types can contain Org objects and plain text.
  45. ;;
  46. ;; Objects are related to document's contents. Some of them are
  47. ;; recursive. Associated types are of the following: `bold', `code',
  48. ;; `entity', `export-snippet', `footnote-reference',
  49. ;; `inline-babel-call', `inline-src-block', `italic',
  50. ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
  51. ;; `statistics-cookie', `strike-through', `subscript', `superscript',
  52. ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
  53. ;;
  54. ;; Some elements also have special properties whose value can hold
  55. ;; objects themselves (i.e. an item tag or an headline name). Such
  56. ;; values are called "secondary strings". Any object belongs to
  57. ;; either an element or a secondary string.
  58. ;;
  59. ;; Notwithstanding affiliated keywords, each greater element, element
  60. ;; and object has a fixed set of properties attached to it. Among
  61. ;; them, four are shared by all types: `:begin' and `:end', which
  62. ;; refer to the beginning and ending buffer positions of the
  63. ;; considered element or object, `:post-blank', which holds the number
  64. ;; of blank lines, or white spaces, at its end and `:parent' which
  65. ;; refers to the element or object containing it. Greater elements
  66. ;; and elements containing objects will also have `:contents-begin'
  67. ;; and `:contents-end' properties to delimit contents.
  68. ;;
  69. ;; Lisp-wise, an element or an object can be represented as a list.
  70. ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
  71. ;; TYPE is a symbol describing the Org element or object.
  72. ;; PROPERTIES is the property list attached to it. See docstring of
  73. ;; appropriate parsing function to get an exhaustive
  74. ;; list.
  75. ;; CONTENTS is a list of elements, objects or raw strings contained
  76. ;; in the current element or object, when applicable.
  77. ;;
  78. ;; An Org buffer is a nested list of such elements and objects, whose
  79. ;; type is `org-data' and properties is nil.
  80. ;;
  81. ;; The first part of this file defines Org syntax, while the second
  82. ;; one provide accessors and setters functions.
  83. ;;
  84. ;; The next part implements a parser and an interpreter for each
  85. ;; element and object type in Org syntax.
  86. ;;
  87. ;; The following part creates a fully recursive buffer parser. It
  88. ;; also provides a tool to map a function to elements or objects
  89. ;; matching some criteria in the parse tree. Functions of interest
  90. ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
  91. ;; extent, `org-element-parse-secondary-string'.
  92. ;;
  93. ;; The penultimate part is the cradle of an interpreter for the
  94. ;; obtained parse tree: `org-element-interpret-data'.
  95. ;;
  96. ;; The library ends by furnishing a set of interactive tools for
  97. ;; element's navigation and manipulation, mostly based on
  98. ;; `org-element-at-point' function, and a way to give information
  99. ;; about document structure around point with `org-element-context'.
  100. ;;; Code:
  101. (eval-when-compile
  102. (require 'cl))
  103. ;;; Definitions And Rules
  104. ;;
  105. ;; Define elements, greater elements and specify recursive objects,
  106. ;; along with the affiliated keywords recognized. Also set up
  107. ;; restrictions on recursive objects combinations.
  108. ;;
  109. ;; These variables really act as a control center for the parsing
  110. ;; process.
  111. (defconst org-element-paragraph-separate
  112. (concat "^[ \t]*$" "\\|"
  113. ;; Headlines and inlinetasks.
  114. org-outline-regexp-bol "\\|"
  115. ;; Comments, blocks (any type), keywords and babel calls.
  116. "^[ \t]*#\\+" "\\|" "^#\\(?: \\|$\\)" "\\|"
  117. ;; Lists.
  118. (org-item-beginning-re) "\\|"
  119. ;; Fixed-width, drawers (any type) and tables.
  120. "^[ \t]*[:|]" "\\|"
  121. ;; Footnote definitions.
  122. org-footnote-definition-re "\\|"
  123. ;; Horizontal rules.
  124. "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
  125. ;; LaTeX environments.
  126. "^[ \t]*\\\\\\(begin\\|end\\)" "\\|"
  127. ;; Planning and Clock lines.
  128. org-planning-or-clock-line-re)
  129. "Regexp to separate paragraphs in an Org buffer.")
  130. (defconst org-element-all-elements
  131. '(center-block clock comment comment-block drawer dynamic-block example-block
  132. export-block fixed-width footnote-definition headline
  133. horizontal-rule inlinetask item keyword latex-environment
  134. babel-call paragraph plain-list planning property-drawer
  135. quote-block quote-section section special-block src-block table
  136. table-row verse-block)
  137. "Complete list of element types.")
  138. (defconst org-element-greater-elements
  139. '(center-block drawer dynamic-block footnote-definition headline inlinetask
  140. item plain-list quote-block section special-block table)
  141. "List of recursive element types aka Greater Elements.")
  142. (defconst org-element-all-successors
  143. '(export-snippet footnote-reference inline-babel-call inline-src-block
  144. latex-or-entity line-break link macro radio-target
  145. statistics-cookie sub/superscript table-cell target
  146. text-markup timestamp)
  147. "Complete list of successors.")
  148. (defconst org-element-object-successor-alist
  149. '((subscript . sub/superscript) (superscript . sub/superscript)
  150. (bold . text-markup) (code . text-markup) (italic . text-markup)
  151. (strike-through . text-markup) (underline . text-markup)
  152. (verbatim . text-markup) (entity . latex-or-entity)
  153. (latex-fragment . latex-or-entity))
  154. "Alist of translations between object type and successor name.
  155. Sharing the same successor comes handy when, for example, the
  156. regexp matching one object can also match the other object.")
  157. (defconst org-element-all-objects
  158. '(bold code entity export-snippet footnote-reference inline-babel-call
  159. inline-src-block italic line-break latex-fragment link macro
  160. radio-target statistics-cookie strike-through subscript superscript
  161. table-cell target timestamp underline verbatim)
  162. "Complete list of object types.")
  163. (defconst org-element-recursive-objects
  164. '(bold italic link macro subscript radio-target strike-through superscript
  165. table-cell underline)
  166. "List of recursive object types.")
  167. (defconst org-element-block-name-alist
  168. '(("CENTER" . org-element-center-block-parser)
  169. ("COMMENT" . org-element-comment-block-parser)
  170. ("EXAMPLE" . org-element-example-block-parser)
  171. ("QUOTE" . org-element-quote-block-parser)
  172. ("SRC" . org-element-src-block-parser)
  173. ("VERSE" . org-element-verse-block-parser))
  174. "Alist between block names and the associated parsing function.
  175. Names must be uppercase. Any block whose name has no association
  176. is parsed with `org-element-special-block-parser'.")
  177. (defconst org-element-affiliated-keywords
  178. '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
  179. "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
  180. "List of affiliated keywords as strings.
  181. By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
  182. are affiliated keywords and need not to be in this list.")
  183. (defconst org-element--affiliated-re
  184. (format "[ \t]*#\\+%s:"
  185. ;; Regular affiliated keywords.
  186. (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
  187. (regexp-opt org-element-affiliated-keywords)))
  188. "Regexp matching any affiliated keyword.
  189. Keyword name is put in match group 1. Moreover, if keyword
  190. belongs to `org-element-dual-keywords', put the dual value in
  191. match group 2.
  192. Don't modify it, set `org-element-affiliated-keywords' instead.")
  193. (defconst org-element-keyword-translation-alist
  194. '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
  195. ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
  196. ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
  197. "Alist of usual translations for keywords.
  198. The key is the old name and the value the new one. The property
  199. holding their value will be named after the translated name.")
  200. (defconst org-element-multiple-keywords '("HEADER")
  201. "List of affiliated keywords that can occur more that once in an element.
  202. Their value will be consed into a list of strings, which will be
  203. returned as the value of the property.
  204. This list is checked after translations have been applied. See
  205. `org-element-keyword-translation-alist'.
  206. By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
  207. allow multiple occurrences and need not to be in this list.")
  208. (defconst org-element-parsed-keywords '("AUTHOR" "CAPTION" "DATE" "TITLE")
  209. "List of keywords whose value can be parsed.
  210. Their value will be stored as a secondary string: a list of
  211. strings and objects.
  212. This list is checked after translations have been applied. See
  213. `org-element-keyword-translation-alist'.")
  214. (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
  215. "List of keywords which can have a secondary value.
  216. In Org syntax, they can be written with optional square brackets
  217. before the colons. For example, results keyword can be
  218. associated to a hash value with the following:
  219. #+RESULTS[hash-string]: some-source
  220. This list is checked after translations have been applied. See
  221. `org-element-keyword-translation-alist'.")
  222. (defconst org-element-object-restrictions
  223. '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
  224. radio-target sub/superscript target text-markup timestamp)
  225. (footnote-reference export-snippet footnote-reference inline-babel-call
  226. inline-src-block latex-or-entity line-break link macro
  227. radio-target sub/superscript target text-markup
  228. timestamp)
  229. (headline inline-babel-call inline-src-block latex-or-entity link macro
  230. radio-target statistics-cookie sub/superscript target text-markup
  231. timestamp)
  232. (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
  233. radio-target sub/superscript target text-markup timestamp)
  234. (italic export-snippet inline-babel-call inline-src-block latex-or-entity
  235. link radio-target sub/superscript target text-markup timestamp)
  236. (item export-snippet footnote-reference inline-babel-call latex-or-entity
  237. link macro radio-target sub/superscript target text-markup)
  238. (keyword latex-or-entity macro sub/superscript text-markup)
  239. (link export-snippet inline-babel-call inline-src-block latex-or-entity link
  240. sub/superscript text-markup)
  241. (macro macro)
  242. (paragraph export-snippet footnote-reference inline-babel-call
  243. inline-src-block latex-or-entity line-break link macro
  244. radio-target statistics-cookie sub/superscript target text-markup
  245. timestamp)
  246. (radio-target export-snippet latex-or-entity sub/superscript)
  247. (strike-through export-snippet inline-babel-call inline-src-block
  248. latex-or-entity link radio-target sub/superscript target
  249. text-markup timestamp)
  250. (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
  251. sub/superscript target text-markup)
  252. (superscript export-snippet inline-babel-call inline-src-block
  253. latex-or-entity sub/superscript target text-markup)
  254. (table-cell export-snippet latex-or-entity link macro radio-target
  255. sub/superscript target text-markup timestamp)
  256. (table-row table-cell)
  257. (underline export-snippet inline-babel-call inline-src-block latex-or-entity
  258. link radio-target sub/superscript target text-markup timestamp)
  259. (verse-block footnote-reference inline-babel-call inline-src-block
  260. latex-or-entity line-break link macro radio-target
  261. sub/superscript target text-markup timestamp))
  262. "Alist of objects restrictions.
  263. CAR is an element or object type containing objects and CDR is
  264. a list of successors that will be called within an element or
  265. object of such type.
  266. For example, in a `radio-target' object, one can only find
  267. entities, export snippets, latex-fragments, subscript and
  268. superscript.
  269. This alist also applies to secondary string. For example, an
  270. `headline' type element doesn't directly contain objects, but
  271. still has an entry since one of its properties (`:title') does.")
  272. (defconst org-element-secondary-value-alist
  273. '((headline . :title)
  274. (inlinetask . :title)
  275. (item . :tag)
  276. (footnote-reference . :inline-definition))
  277. "Alist between element types and location of secondary value.")
  278. ;;; Accessors and Setters
  279. ;;
  280. ;; Provide four accessors: `org-element-type', `org-element-property'
  281. ;; `org-element-contents' and `org-element-restriction'.
  282. ;;
  283. ;; Setter functions allow to modify elements by side effect. There is
  284. ;; `org-element-put-property', `org-element-set-contents',
  285. ;; `org-element-set-element' and `org-element-adopt-element'. Note
  286. ;; that `org-element-set-element' and `org-element-adopt-element' are
  287. ;; higher level functions since also update `:parent' property.
  288. (defsubst org-element-type (element)
  289. "Return type of ELEMENT.
  290. The function returns the type of the element or object provided.
  291. It can also return the following special value:
  292. `plain-text' for a string
  293. `org-data' for a complete document
  294. nil in any other case."
  295. (cond
  296. ((not (consp element)) (and (stringp element) 'plain-text))
  297. ((symbolp (car element)) (car element))))
  298. (defsubst org-element-property (property element)
  299. "Extract the value from the PROPERTY of an ELEMENT."
  300. (plist-get (nth 1 element) property))
  301. (defsubst org-element-contents (element)
  302. "Extract contents from an ELEMENT."
  303. (and (consp element) (nthcdr 2 element)))
  304. (defsubst org-element-restriction (element)
  305. "Return restriction associated to ELEMENT.
  306. ELEMENT can be an element, an object or a symbol representing an
  307. element or object type."
  308. (cdr (assq (if (symbolp element) element (org-element-type element))
  309. org-element-object-restrictions)))
  310. (defsubst org-element-put-property (element property value)
  311. "In ELEMENT set PROPERTY to VALUE.
  312. Return modified element."
  313. (when (consp element)
  314. (setcar (cdr element) (plist-put (nth 1 element) property value)))
  315. element)
  316. (defsubst org-element-set-contents (element &rest contents)
  317. "Set ELEMENT contents to CONTENTS.
  318. Return modified element."
  319. (cond ((not element) (list contents))
  320. ((cdr element) (setcdr (cdr element) contents))
  321. (t (nconc element contents))))
  322. (defsubst org-element-set-element (old new)
  323. "Replace element or object OLD with element or object NEW.
  324. The function takes care of setting `:parent' property for NEW."
  325. ;; OLD can belong to the contents of PARENT or to its secondary
  326. ;; string.
  327. (let* ((parent (org-element-property :parent old))
  328. (sec-loc (cdr (assq (org-element-type parent)
  329. org-element-secondary-value-alist)))
  330. (sec-value (and sec-loc (org-element-property sec-loc parent)))
  331. (place (or (memq old sec-value) (memq old parent))))
  332. ;; Make sure NEW has correct `:parent' property.
  333. (org-element-put-property new :parent parent)
  334. ;; Replace OLD with NEW in PARENT.
  335. (setcar place new)))
  336. (defsubst org-element-adopt-element (parent child &optional append)
  337. "Add an element to the contents of another element.
  338. PARENT is an element or object. CHILD is an element, an object,
  339. or a string.
  340. CHILD is added at the beginning of PARENT contents, unless the
  341. optional argument APPEND is non-nil, in which case CHILD is added
  342. at the end.
  343. The function takes care of setting `:parent' property for CHILD.
  344. Return parent element."
  345. (if (not parent) (list child)
  346. (let ((contents (org-element-contents parent)))
  347. (apply 'org-element-set-contents
  348. parent
  349. (if append (append contents (list child)) (cons child contents))))
  350. ;; Link the CHILD element with PARENT.
  351. (when (consp child) (org-element-put-property child :parent parent))
  352. ;; Return the parent element.
  353. parent))
  354. ;;; Greater elements
  355. ;;
  356. ;; For each greater element type, we define a parser and an
  357. ;; interpreter.
  358. ;;
  359. ;; A parser returns the element or object as the list described above.
  360. ;; Most of them accepts no argument. Though, exceptions exist. Hence
  361. ;; every element containing a secondary string (see
  362. ;; `org-element-secondary-value-alist') will accept an optional
  363. ;; argument to toggle parsing of that secondary string. Moreover,
  364. ;; `item' parser requires current list's structure as its first
  365. ;; element.
  366. ;;
  367. ;; An interpreter accepts two arguments: the list representation of
  368. ;; the element or object, and its contents. The latter may be nil,
  369. ;; depending on the element or object considered. It returns the
  370. ;; appropriate Org syntax, as a string.
  371. ;;
  372. ;; Parsing functions must follow the naming convention:
  373. ;; org-element-TYPE-parser, where TYPE is greater element's type, as
  374. ;; defined in `org-element-greater-elements'.
  375. ;;
  376. ;; Similarly, interpreting functions must follow the naming
  377. ;; convention: org-element-TYPE-interpreter.
  378. ;;
  379. ;; With the exception of `headline' and `item' types, greater elements
  380. ;; cannot contain other greater elements of their own type.
  381. ;;
  382. ;; Beside implementing a parser and an interpreter, adding a new
  383. ;; greater element requires to tweak `org-element--current-element'.
  384. ;; Moreover, the newly defined type must be added to both
  385. ;; `org-element-all-elements' and `org-element-greater-elements'.
  386. ;;;; Center Block
  387. (defun org-element-center-block-parser (limit)
  388. "Parse a center block.
  389. LIMIT bounds the search.
  390. Return a list whose CAR is `center-block' and CDR is a plist
  391. containing `:begin', `:end', `:hiddenp', `:contents-begin',
  392. `:contents-end' and `:post-blank' keywords.
  393. Assume point is at the beginning of the block."
  394. (let ((case-fold-search t))
  395. (if (not (save-excursion
  396. (re-search-forward "^[ \t]*#\\+END_CENTER" limit t)))
  397. ;; Incomplete block: parse it as a comment.
  398. (org-element-comment-parser limit)
  399. (let ((block-end-line (match-beginning 0)))
  400. (let* ((keywords (org-element--collect-affiliated-keywords))
  401. (begin (car keywords))
  402. ;; Empty blocks have no contents.
  403. (contents-begin (progn (forward-line)
  404. (and (< (point) block-end-line)
  405. (point))))
  406. (contents-end (and contents-begin block-end-line))
  407. (hidden (org-invisible-p2))
  408. (pos-before-blank (progn (goto-char block-end-line)
  409. (forward-line)
  410. (point)))
  411. (end (save-excursion (skip-chars-forward " \r\t\n" limit)
  412. (if (eobp) (point) (point-at-bol)))))
  413. (list 'center-block
  414. (nconc
  415. (list :begin begin
  416. :end end
  417. :hiddenp hidden
  418. :contents-begin contents-begin
  419. :contents-end contents-end
  420. :post-blank (count-lines pos-before-blank end))
  421. (cadr keywords))))))))
  422. (defun org-element-center-block-interpreter (center-block contents)
  423. "Interpret CENTER-BLOCK element as Org syntax.
  424. CONTENTS is the contents of the element."
  425. (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
  426. ;;;; Drawer
  427. (defun org-element-drawer-parser (limit)
  428. "Parse a drawer.
  429. LIMIT bounds the search.
  430. Return a list whose CAR is `drawer' and CDR is a plist containing
  431. `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
  432. `:contents-end' and `:post-blank' keywords.
  433. Assume point is at beginning of drawer."
  434. (let ((case-fold-search t))
  435. (if (not (save-excursion (re-search-forward "^[ \t]*:END:" limit t)))
  436. ;; Incomplete drawer: parse it as a paragraph.
  437. (org-element-paragraph-parser limit)
  438. (let ((drawer-end-line (match-beginning 0)))
  439. (save-excursion
  440. (let* ((case-fold-search t)
  441. (name (progn (looking-at org-drawer-regexp)
  442. (org-match-string-no-properties 1)))
  443. (keywords (org-element--collect-affiliated-keywords))
  444. (begin (car keywords))
  445. ;; Empty drawers have no contents.
  446. (contents-begin (progn (forward-line)
  447. (and (< (point) drawer-end-line)
  448. (point))))
  449. (contents-end (and contents-begin drawer-end-line))
  450. (hidden (org-invisible-p2))
  451. (pos-before-blank (progn (goto-char drawer-end-line)
  452. (forward-line)
  453. (point)))
  454. (end (progn (skip-chars-forward " \r\t\n" limit)
  455. (if (eobp) (point) (point-at-bol)))))
  456. (list 'drawer
  457. (nconc
  458. (list :begin begin
  459. :end end
  460. :drawer-name name
  461. :hiddenp hidden
  462. :contents-begin contents-begin
  463. :contents-end contents-end
  464. :post-blank (count-lines pos-before-blank end))
  465. (cadr keywords)))))))))
  466. (defun org-element-drawer-interpreter (drawer contents)
  467. "Interpret DRAWER element as Org syntax.
  468. CONTENTS is the contents of the element."
  469. (format ":%s:\n%s:END:"
  470. (org-element-property :drawer-name drawer)
  471. contents))
  472. ;;;; Dynamic Block
  473. (defun org-element-dynamic-block-parser (limit)
  474. "Parse a dynamic block.
  475. LIMIT bounds the search.
  476. Return a list whose CAR is `dynamic-block' and CDR is a plist
  477. containing `:block-name', `:begin', `:end', `:hiddenp',
  478. `:contents-begin', `:contents-end', `:arguments' and
  479. `:post-blank' keywords.
  480. Assume point is at beginning of dynamic block."
  481. (let ((case-fold-search t))
  482. (if (not (save-excursion (re-search-forward org-dblock-end-re limit t)))
  483. ;; Incomplete block: parse it as a comment.
  484. (org-element-comment-parser limit)
  485. (let ((block-end-line (match-beginning 0)))
  486. (save-excursion
  487. (let* ((name (progn (looking-at org-dblock-start-re)
  488. (org-match-string-no-properties 1)))
  489. (arguments (org-match-string-no-properties 3))
  490. (keywords (org-element--collect-affiliated-keywords))
  491. (begin (car keywords))
  492. ;; Empty blocks have no contents.
  493. (contents-begin (progn (forward-line)
  494. (and (< (point) block-end-line)
  495. (point))))
  496. (contents-end (and contents-begin block-end-line))
  497. (hidden (org-invisible-p2))
  498. (pos-before-blank (progn (goto-char block-end-line)
  499. (forward-line)
  500. (point)))
  501. (end (progn (skip-chars-forward " \r\t\n" limit)
  502. (if (eobp) (point) (point-at-bol)))))
  503. (list 'dynamic-block
  504. (nconc
  505. (list :begin begin
  506. :end end
  507. :block-name name
  508. :arguments arguments
  509. :hiddenp hidden
  510. :contents-begin contents-begin
  511. :contents-end contents-end
  512. :post-blank (count-lines pos-before-blank end))
  513. (cadr keywords)))))))))
  514. (defun org-element-dynamic-block-interpreter (dynamic-block contents)
  515. "Interpret DYNAMIC-BLOCK element as Org syntax.
  516. CONTENTS is the contents of the element."
  517. (format "#+BEGIN: %s%s\n%s#+END:"
  518. (org-element-property :block-name dynamic-block)
  519. (let ((args (org-element-property :arguments dynamic-block)))
  520. (and args (concat " " args)))
  521. contents))
  522. ;;;; Footnote Definition
  523. (defun org-element-footnote-definition-parser (limit)
  524. "Parse a footnote definition.
  525. LIMIT bounds the search.
  526. Return a list whose CAR is `footnote-definition' and CDR is
  527. a plist containing `:label', `:begin' `:end', `:contents-begin',
  528. `:contents-end' and `:post-blank' keywords.
  529. Assume point is at the beginning of the footnote definition."
  530. (save-excursion
  531. (let* ((label (progn (looking-at org-footnote-definition-re)
  532. (org-match-string-no-properties 1)))
  533. (keywords (org-element--collect-affiliated-keywords))
  534. (begin (car keywords))
  535. (ending (save-excursion
  536. (if (progn
  537. (end-of-line)
  538. (re-search-forward
  539. (concat org-outline-regexp-bol "\\|"
  540. org-footnote-definition-re "\\|"
  541. "^[ \t]*$") limit 'move))
  542. (match-beginning 0)
  543. (point))))
  544. (contents-begin (progn (search-forward "]")
  545. (skip-chars-forward " \r\t\n" ending)
  546. (and (/= (point) ending) (point))))
  547. (contents-end (and contents-begin ending))
  548. (end (progn (goto-char ending)
  549. (skip-chars-forward " \r\t\n" limit)
  550. (if (eobp) (point) (point-at-bol)))))
  551. (list 'footnote-definition
  552. (nconc
  553. (list :label label
  554. :begin begin
  555. :end end
  556. :contents-begin contents-begin
  557. :contents-end contents-end
  558. :post-blank (count-lines ending end))
  559. (cadr keywords))))))
  560. (defun org-element-footnote-definition-interpreter (footnote-definition contents)
  561. "Interpret FOOTNOTE-DEFINITION element as Org syntax.
  562. CONTENTS is the contents of the footnote-definition."
  563. (concat (format "[%s]" (org-element-property :label footnote-definition))
  564. " "
  565. contents))
  566. ;;;; Headline
  567. (defun org-element-headline-parser (limit &optional raw-secondary-p)
  568. "Parse an headline.
  569. Return a list whose CAR is `headline' and CDR is a plist
  570. containing `:raw-value', `:title', `:begin', `:end',
  571. `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
  572. `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
  573. `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
  574. `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
  575. keywords.
  576. The plist also contains any property set in the property drawer,
  577. with its name in lowercase, the underscores replaced with hyphens
  578. and colons at the beginning (i.e. `:custom-id').
  579. When RAW-SECONDARY-P is non-nil, headline's title will not be
  580. parsed as a secondary string, but as a plain string instead.
  581. Assume point is at beginning of the headline."
  582. (save-excursion
  583. (let* ((components (org-heading-components))
  584. (level (nth 1 components))
  585. (todo (nth 2 components))
  586. (todo-type
  587. (and todo (if (member todo org-done-keywords) 'done 'todo)))
  588. (tags (let ((raw-tags (nth 5 components)))
  589. (and raw-tags (org-split-string raw-tags ":"))))
  590. (raw-value (nth 4 components))
  591. (quotedp
  592. (let ((case-fold-search nil))
  593. (string-match (format "^%s +" org-quote-string) raw-value)))
  594. (commentedp
  595. (let ((case-fold-search nil))
  596. (string-match (format "^%s +" org-comment-string) raw-value)))
  597. (archivedp (member org-archive-tag tags))
  598. (footnote-section-p (and org-footnote-section
  599. (string= org-footnote-section raw-value)))
  600. ;; Normalize property names: ":SOME_PROP:" becomes
  601. ;; ":some-prop".
  602. (standard-props (let (plist)
  603. (mapc
  604. (lambda (p)
  605. (let ((p-name (downcase (car p))))
  606. (while (string-match "_" p-name)
  607. (setq p-name
  608. (replace-match "-" nil nil p-name)))
  609. (setq p-name (intern (concat ":" p-name)))
  610. (setq plist
  611. (plist-put plist p-name (cdr p)))))
  612. (org-entry-properties nil 'standard))
  613. plist))
  614. (time-props (org-entry-properties nil 'special "CLOCK"))
  615. (scheduled (cdr (assoc "SCHEDULED" time-props)))
  616. (deadline (cdr (assoc "DEADLINE" time-props)))
  617. (clock (cdr (assoc "CLOCK" time-props)))
  618. (timestamp (cdr (assoc "TIMESTAMP" time-props)))
  619. (begin (point))
  620. (end (save-excursion (goto-char (org-end-of-subtree t t))))
  621. (pos-after-head (progn (forward-line) (point)))
  622. (contents-begin (save-excursion
  623. (skip-chars-forward " \r\t\n" end)
  624. (and (/= (point) end) (line-beginning-position))))
  625. (hidden (org-invisible-p2))
  626. (contents-end (and contents-begin
  627. (progn (goto-char end)
  628. (skip-chars-backward " \r\t\n")
  629. (forward-line)
  630. (point)))))
  631. ;; Clean RAW-VALUE from any quote or comment string.
  632. (when (or quotedp commentedp)
  633. (setq raw-value
  634. (replace-regexp-in-string
  635. (concat "\\(" org-quote-string "\\|" org-comment-string "\\) +")
  636. ""
  637. raw-value)))
  638. ;; Clean TAGS from archive tag, if any.
  639. (when archivedp (setq tags (delete org-archive-tag tags)))
  640. (let ((headline
  641. (list 'headline
  642. (nconc
  643. (list :raw-value raw-value
  644. :begin begin
  645. :end end
  646. :pre-blank
  647. (if (not contents-begin) 0
  648. (count-lines pos-after-head contents-begin))
  649. :hiddenp hidden
  650. :contents-begin contents-begin
  651. :contents-end contents-end
  652. :level level
  653. :priority (nth 3 components)
  654. :tags tags
  655. :todo-keyword todo
  656. :todo-type todo-type
  657. :scheduled scheduled
  658. :deadline deadline
  659. :timestamp timestamp
  660. :clock clock
  661. :post-blank (count-lines
  662. (if (not contents-end) pos-after-head
  663. (goto-char contents-end)
  664. (forward-line)
  665. (point))
  666. end)
  667. :footnote-section-p footnote-section-p
  668. :archivedp archivedp
  669. :commentedp commentedp
  670. :quotedp quotedp)
  671. standard-props))))
  672. (org-element-put-property
  673. headline :title
  674. (if raw-secondary-p raw-value
  675. (org-element-parse-secondary-string
  676. raw-value (org-element-restriction 'headline) headline)))))))
  677. (defun org-element-headline-interpreter (headline contents)
  678. "Interpret HEADLINE element as Org syntax.
  679. CONTENTS is the contents of the element."
  680. (let* ((level (org-element-property :level headline))
  681. (todo (org-element-property :todo-keyword headline))
  682. (priority (org-element-property :priority headline))
  683. (title (org-element-interpret-data
  684. (org-element-property :title headline)))
  685. (tags (let ((tag-list (if (org-element-property :archivedp headline)
  686. (cons org-archive-tag
  687. (org-element-property :tags headline))
  688. (org-element-property :tags headline))))
  689. (and tag-list
  690. (format ":%s:" (mapconcat 'identity tag-list ":")))))
  691. (commentedp (org-element-property :commentedp headline))
  692. (quotedp (org-element-property :quotedp headline))
  693. (pre-blank (or (org-element-property :pre-blank headline) 0))
  694. (heading (concat (make-string level ?*)
  695. (and todo (concat " " todo))
  696. (and quotedp (concat " " org-quote-string))
  697. (and commentedp (concat " " org-comment-string))
  698. (and priority
  699. (format " [#%s]" (char-to-string priority)))
  700. (cond ((and org-footnote-section
  701. (org-element-property
  702. :footnote-section-p headline))
  703. (concat " " org-footnote-section))
  704. (title (concat " " title))))))
  705. (concat heading
  706. ;; Align tags.
  707. (when tags
  708. (cond
  709. ((zerop org-tags-column) (format " %s" tags))
  710. ((< org-tags-column 0)
  711. (concat
  712. (make-string
  713. (max (- (+ org-tags-column (length heading) (length tags))) 1)
  714. ? )
  715. tags))
  716. (t
  717. (concat
  718. (make-string (max (- org-tags-column (length heading)) 1) ? )
  719. tags))))
  720. (make-string (1+ pre-blank) 10)
  721. contents)))
  722. ;;;; Inlinetask
  723. (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
  724. "Parse an inline task.
  725. Return a list whose CAR is `inlinetask' and CDR is a plist
  726. containing `:title', `:begin', `:end', `:hiddenp',
  727. `:contents-begin' and `:contents-end', `:level', `:priority',
  728. `:tags', `:todo-keyword', `:todo-type', `:scheduled',
  729. `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
  730. The plist also contains any property set in the property drawer,
  731. with its name in lowercase, the underscores replaced with hyphens
  732. and colons at the beginning (i.e. `:custom-id').
  733. When optional argument RAW-SECONDARY-P is non-nil, inline-task's
  734. title will not be parsed as a secondary string, but as a plain
  735. string instead.
  736. Assume point is at beginning of the inline task."
  737. (save-excursion
  738. (let* ((keywords (org-element--collect-affiliated-keywords))
  739. (begin (car keywords))
  740. (components (org-heading-components))
  741. (todo (nth 2 components))
  742. (todo-type (and todo
  743. (if (member todo org-done-keywords) 'done 'todo)))
  744. (tags (let ((raw-tags (nth 5 components)))
  745. (and raw-tags (org-split-string raw-tags ":"))))
  746. ;; Normalize property names: ":SOME_PROP:" becomes
  747. ;; ":some-prop".
  748. (standard-props (let (plist)
  749. (mapc
  750. (lambda (p)
  751. (let ((p-name (downcase (car p))))
  752. (while (string-match "_" p-name)
  753. (setq p-name
  754. (replace-match "-" nil nil p-name)))
  755. (setq p-name (intern (concat ":" p-name)))
  756. (setq plist
  757. (plist-put plist p-name (cdr p)))))
  758. (org-entry-properties nil 'standard))
  759. plist))
  760. (time-props (org-entry-properties nil 'special "CLOCK"))
  761. (scheduled (cdr (assoc "SCHEDULED" time-props)))
  762. (deadline (cdr (assoc "DEADLINE" time-props)))
  763. (clock (cdr (assoc "CLOCK" time-props)))
  764. (timestamp (cdr (assoc "TIMESTAMP" time-props)))
  765. (task-end (save-excursion
  766. (end-of-line)
  767. (and (re-search-forward "^\\*+ END" limit t)
  768. (match-beginning 0))))
  769. (contents-begin (progn (forward-line)
  770. (and task-end (< (point) task-end) (point))))
  771. (hidden (and contents-begin (org-invisible-p2)))
  772. (contents-end (and contents-begin task-end))
  773. (before-blank (if (not task-end) (point)
  774. (goto-char task-end)
  775. (forward-line)
  776. (point)))
  777. (end (progn (skip-chars-forward " \r\t\n" limit)
  778. (if (eobp) (point) (point-at-bol))))
  779. (inlinetask
  780. (list 'inlinetask
  781. (nconc
  782. (list :begin begin
  783. :end end
  784. :hiddenp hidden
  785. :contents-begin contents-begin
  786. :contents-end contents-end
  787. :level (nth 1 components)
  788. :priority (nth 3 components)
  789. :tags tags
  790. :todo-keyword todo
  791. :todo-type todo-type
  792. :scheduled scheduled
  793. :deadline deadline
  794. :timestamp timestamp
  795. :clock clock
  796. :post-blank (count-lines before-blank end))
  797. standard-props
  798. (cadr keywords)))))
  799. (org-element-put-property
  800. inlinetask :title
  801. (if raw-secondary-p (nth 4 components)
  802. (org-element-parse-secondary-string
  803. (nth 4 components)
  804. (org-element-restriction 'inlinetask)
  805. inlinetask))))))
  806. (defun org-element-inlinetask-interpreter (inlinetask contents)
  807. "Interpret INLINETASK element as Org syntax.
  808. CONTENTS is the contents of inlinetask."
  809. (let* ((level (org-element-property :level inlinetask))
  810. (todo (org-element-property :todo-keyword inlinetask))
  811. (priority (org-element-property :priority inlinetask))
  812. (title (org-element-interpret-data
  813. (org-element-property :title inlinetask)))
  814. (tags (let ((tag-list (org-element-property :tags inlinetask)))
  815. (and tag-list
  816. (format ":%s:" (mapconcat 'identity tag-list ":")))))
  817. (task (concat (make-string level ?*)
  818. (and todo (concat " " todo))
  819. (and priority
  820. (format " [#%s]" (char-to-string priority)))
  821. (and title (concat " " title)))))
  822. (concat task
  823. ;; Align tags.
  824. (when tags
  825. (cond
  826. ((zerop org-tags-column) (format " %s" tags))
  827. ((< org-tags-column 0)
  828. (concat
  829. (make-string
  830. (max (- (+ org-tags-column (length task) (length tags))) 1)
  831. ? )
  832. tags))
  833. (t
  834. (concat
  835. (make-string (max (- org-tags-column (length task)) 1) ? )
  836. tags))))
  837. ;; Prefer degenerate inlinetasks when there are no
  838. ;; contents.
  839. (when contents
  840. (concat "\n"
  841. contents
  842. (make-string level ?*) " END")))))
  843. ;;;; Item
  844. (defun org-element-item-parser (limit struct &optional raw-secondary-p)
  845. "Parse an item.
  846. STRUCT is the structure of the plain list.
  847. Return a list whose CAR is `item' and CDR is a plist containing
  848. `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
  849. `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
  850. `:post-blank' keywords.
  851. When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
  852. any, will not be parsed as a secondary string, but as a plain
  853. string instead.
  854. Assume point is at the beginning of the item."
  855. (save-excursion
  856. (beginning-of-line)
  857. (let* ((begin (point))
  858. (bullet (org-list-get-bullet (point) struct))
  859. (checkbox (let ((box (org-list-get-checkbox begin struct)))
  860. (cond ((equal "[ ]" box) 'off)
  861. ((equal "[X]" box) 'on)
  862. ((equal "[-]" box) 'trans))))
  863. (counter (let ((c (org-list-get-counter begin struct)))
  864. (cond
  865. ((not c) nil)
  866. ((string-match "[A-Za-z]" c)
  867. (- (string-to-char (upcase (match-string 0 c)))
  868. 64))
  869. ((string-match "[0-9]+" c)
  870. (string-to-number (match-string 0 c))))))
  871. (end (org-list-get-item-end begin struct))
  872. (contents-begin (progn (looking-at org-list-full-item-re)
  873. (goto-char (match-end 0))
  874. (org-skip-whitespace)
  875. ;; If first line isn't empty,
  876. ;; contents really start at the text
  877. ;; after item's meta-data.
  878. (if (= (point-at-bol) begin) (point)
  879. (point-at-bol))))
  880. (hidden (progn (forward-line)
  881. (and (not (= (point) end))
  882. (org-invisible-p2))))
  883. (contents-end (progn (goto-char end)
  884. (skip-chars-backward " \r\t\n")
  885. (forward-line)
  886. (point)))
  887. (item
  888. (list 'item
  889. (list :bullet bullet
  890. :begin begin
  891. :end end
  892. ;; CONTENTS-BEGIN and CONTENTS-END may be
  893. ;; mixed up in the case of an empty item
  894. ;; separated from the next by a blank line.
  895. ;; Thus ensure the former is always the
  896. ;; smallest.
  897. :contents-begin (min contents-begin contents-end)
  898. :contents-end (max contents-begin contents-end)
  899. :checkbox checkbox
  900. :counter counter
  901. :hiddenp hidden
  902. :structure struct
  903. :post-blank (count-lines contents-end end)))))
  904. (org-element-put-property
  905. item :tag
  906. (let ((raw-tag (org-list-get-tag begin struct)))
  907. (and raw-tag
  908. (if raw-secondary-p raw-tag
  909. (org-element-parse-secondary-string
  910. raw-tag (org-element-restriction 'item) item))))))))
  911. (defun org-element-item-interpreter (item contents)
  912. "Interpret ITEM element as Org syntax.
  913. CONTENTS is the contents of the element."
  914. (let* ((bullet (org-list-bullet-string (org-element-property :bullet item)))
  915. (checkbox (org-element-property :checkbox item))
  916. (counter (org-element-property :counter item))
  917. (tag (let ((tag (org-element-property :tag item)))
  918. (and tag (org-element-interpret-data tag))))
  919. ;; Compute indentation.
  920. (ind (make-string (length bullet) 32))
  921. (item-starts-with-par-p
  922. (eq (org-element-type (car (org-element-contents item)))
  923. 'paragraph)))
  924. ;; Indent contents.
  925. (concat
  926. bullet
  927. (and counter (format "[@%d] " counter))
  928. (cond
  929. ((eq checkbox 'on) "[X] ")
  930. ((eq checkbox 'off) "[ ] ")
  931. ((eq checkbox 'trans) "[-] "))
  932. (and tag (format "%s :: " tag))
  933. (let ((contents (replace-regexp-in-string
  934. "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
  935. (if item-starts-with-par-p (org-trim contents)
  936. (concat "\n" contents))))))
  937. ;;;; Plain List
  938. (defun org-element-plain-list-parser (limit &optional structure)
  939. "Parse a plain list.
  940. Optional argument STRUCTURE, when non-nil, is the structure of
  941. the plain list being parsed.
  942. Return a list whose CAR is `plain-list' and CDR is a plist
  943. containing `:type', `:begin', `:end', `:contents-begin' and
  944. `:contents-end', `:structure' and `:post-blank' keywords.
  945. Assume point is at the beginning of the list."
  946. (save-excursion
  947. (let* ((struct (or structure (org-list-struct)))
  948. (prevs (org-list-prevs-alist struct))
  949. (parents (org-list-parents-alist struct))
  950. (type (org-list-get-list-type (point) struct prevs))
  951. (contents-begin (point))
  952. (keywords (org-element--collect-affiliated-keywords))
  953. (begin (car keywords))
  954. (contents-end
  955. (goto-char (org-list-get-list-end (point) struct prevs)))
  956. (end (save-excursion (org-skip-whitespace)
  957. (if (eobp) (point) (point-at-bol)))))
  958. ;; Blank lines below list belong to the top-level list only.
  959. (unless (= (org-list-get-top-point struct) contents-begin)
  960. (setq end (min (org-list-get-bottom-point struct)
  961. (progn (skip-chars-forward " \r\t\n" limit)
  962. (if (eobp) (point) (point-at-bol))))))
  963. ;; Return value.
  964. (list 'plain-list
  965. (nconc
  966. (list :type type
  967. :begin begin
  968. :end end
  969. :contents-begin contents-begin
  970. :contents-end contents-end
  971. :structure struct
  972. :post-blank (count-lines contents-end end))
  973. (cadr keywords))))))
  974. (defun org-element-plain-list-interpreter (plain-list contents)
  975. "Interpret PLAIN-LIST element as Org syntax.
  976. CONTENTS is the contents of the element."
  977. (with-temp-buffer
  978. (insert contents)
  979. (goto-char (point-min))
  980. (org-list-repair)
  981. (buffer-string)))
  982. ;;;; Quote Block
  983. (defun org-element-quote-block-parser (limit)
  984. "Parse a quote block.
  985. LIMIT bounds the search.
  986. Return a list whose CAR is `quote-block' and CDR is a plist
  987. containing `:begin', `:end', `:hiddenp', `:contents-begin',
  988. `:contents-end' and `:post-blank' keywords.
  989. Assume point is at the beginning of the block."
  990. (let ((case-fold-search t))
  991. (if (not (save-excursion
  992. (re-search-forward "^[ \t]*#\\+END_QUOTE" limit t)))
  993. ;; Incomplete block: parse it as a comment.
  994. (org-element-comment-parser limit)
  995. (let ((block-end-line (match-beginning 0)))
  996. (save-excursion
  997. (let* ((keywords (org-element--collect-affiliated-keywords))
  998. (begin (car keywords))
  999. ;; Empty blocks have no contents.
  1000. (contents-begin (progn (forward-line)
  1001. (and (< (point) block-end-line)
  1002. (point))))
  1003. (contents-end (and contents-begin block-end-line))
  1004. (hidden (org-invisible-p2))
  1005. (pos-before-blank (progn (goto-char block-end-line)
  1006. (forward-line)
  1007. (point)))
  1008. (end (progn (skip-chars-forward " \r\t\n" limit)
  1009. (if (eobp) (point) (point-at-bol)))))
  1010. (list 'quote-block
  1011. (nconc
  1012. (list :begin begin
  1013. :end end
  1014. :hiddenp hidden
  1015. :contents-begin contents-begin
  1016. :contents-end contents-end
  1017. :post-blank (count-lines pos-before-blank end))
  1018. (cadr keywords)))))))))
  1019. (defun org-element-quote-block-interpreter (quote-block contents)
  1020. "Interpret QUOTE-BLOCK element as Org syntax.
  1021. CONTENTS is the contents of the element."
  1022. (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
  1023. ;;;; Section
  1024. (defun org-element-section-parser (limit)
  1025. "Parse a section.
  1026. LIMIT bounds the search.
  1027. Return a list whose CAR is `section' and CDR is a plist
  1028. containing `:begin', `:end', `:contents-begin', `contents-end'
  1029. and `:post-blank' keywords."
  1030. (save-excursion
  1031. ;; Beginning of section is the beginning of the first non-blank
  1032. ;; line after previous headline.
  1033. (org-with-limited-levels
  1034. (let ((begin (point))
  1035. (end (progn (goto-char limit) (point)))
  1036. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1037. (forward-line)
  1038. (point))))
  1039. (list 'section
  1040. (list :begin begin
  1041. :end end
  1042. :contents-begin begin
  1043. :contents-end pos-before-blank
  1044. :post-blank (count-lines pos-before-blank end)))))))
  1045. (defun org-element-section-interpreter (section contents)
  1046. "Interpret SECTION element as Org syntax.
  1047. CONTENTS is the contents of the element."
  1048. contents)
  1049. ;;;; Special Block
  1050. (defun org-element-special-block-parser (limit)
  1051. "Parse a special block.
  1052. LIMIT bounds the search.
  1053. Return a list whose CAR is `special-block' and CDR is a plist
  1054. containing `:type', `:begin', `:end', `:hiddenp',
  1055. `:contents-begin', `:contents-end' and `:post-blank' keywords.
  1056. Assume point is at the beginning of the block."
  1057. (let* ((case-fold-search t)
  1058. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
  1059. (upcase (match-string-no-properties 1)))))
  1060. (if (not (save-excursion
  1061. (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
  1062. ;; Incomplete block: parse it as a comment.
  1063. (org-element-comment-parser limit)
  1064. (let ((block-end-line (match-beginning 0)))
  1065. (save-excursion
  1066. (let* ((keywords (org-element--collect-affiliated-keywords))
  1067. (begin (car keywords))
  1068. ;; Empty blocks have no contents.
  1069. (contents-begin (progn (forward-line)
  1070. (and (< (point) block-end-line)
  1071. (point))))
  1072. (contents-end (and contents-begin block-end-line))
  1073. (hidden (org-invisible-p2))
  1074. (pos-before-blank (progn (goto-char block-end-line)
  1075. (forward-line)
  1076. (point)))
  1077. (end (progn (org-skip-whitespace)
  1078. (if (eobp) (point) (point-at-bol)))))
  1079. (list 'special-block
  1080. (nconc
  1081. (list :type type
  1082. :begin begin
  1083. :end end
  1084. :hiddenp hidden
  1085. :contents-begin contents-begin
  1086. :contents-end contents-end
  1087. :post-blank (count-lines pos-before-blank end))
  1088. (cadr keywords)))))))))
  1089. (defun org-element-special-block-interpreter (special-block contents)
  1090. "Interpret SPECIAL-BLOCK element as Org syntax.
  1091. CONTENTS is the contents of the element."
  1092. (let ((block-type (org-element-property :type special-block)))
  1093. (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
  1094. ;;; Elements
  1095. ;;
  1096. ;; For each element, a parser and an interpreter are also defined.
  1097. ;; Both follow the same naming convention used for greater elements.
  1098. ;;
  1099. ;; Also, as for greater elements, adding a new element type is done
  1100. ;; through the following steps: implement a parser and an interpreter,
  1101. ;; tweak `org-element--current-element' so that it recognizes the new
  1102. ;; type and add that new type to `org-element-all-elements'.
  1103. ;;
  1104. ;; As a special case, when the newly defined type is a block type,
  1105. ;; `org-element-block-name-alist' has to be modified accordingly.
  1106. ;;;; Babel Call
  1107. (defun org-element-babel-call-parser (limit)
  1108. "Parse a babel call.
  1109. LIMIT bounds the search.
  1110. Return a list whose CAR is `babel-call' and CDR is a plist
  1111. containing `:begin', `:end', `:info' and `:post-blank' as
  1112. keywords."
  1113. (save-excursion
  1114. (let ((case-fold-search t)
  1115. (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
  1116. (org-babel-lob-get-info)))
  1117. (begin (point-at-bol))
  1118. (pos-before-blank (progn (forward-line) (point)))
  1119. (end (progn (skip-chars-forward " \r\t\n" limit)
  1120. (if (eobp) (point) (point-at-bol)))))
  1121. (list 'babel-call
  1122. (list :begin begin
  1123. :end end
  1124. :info info
  1125. :post-blank (count-lines pos-before-blank end))))))
  1126. (defun org-element-babel-call-interpreter (babel-call contents)
  1127. "Interpret BABEL-CALL element as Org syntax.
  1128. CONTENTS is nil."
  1129. (let* ((babel-info (org-element-property :info babel-call))
  1130. (main (car babel-info))
  1131. (post-options (nth 1 babel-info)))
  1132. (concat "#+CALL: "
  1133. (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
  1134. ;; Remove redundant square brackets.
  1135. (replace-match (match-string 1 main) nil nil main))
  1136. (and post-options (format "[%s]" post-options)))))
  1137. ;;;; Clock
  1138. (defun org-element-clock-parser (limit)
  1139. "Parse a clock.
  1140. LIMIT bounds the search.
  1141. Return a list whose CAR is `clock' and CDR is a plist containing
  1142. `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
  1143. as keywords."
  1144. (save-excursion
  1145. (let* ((case-fold-search nil)
  1146. (begin (point))
  1147. (value (progn (search-forward org-clock-string (line-end-position) t)
  1148. (org-skip-whitespace)
  1149. (looking-at "\\[.*\\]")
  1150. (org-match-string-no-properties 0)))
  1151. (time (and (progn (goto-char (match-end 0))
  1152. (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
  1153. (org-match-string-no-properties 1)))
  1154. (status (if time 'closed 'running))
  1155. (post-blank (let ((before-blank (progn (forward-line) (point))))
  1156. (skip-chars-forward " \r\t\n" limit)
  1157. (unless (eobp) (beginning-of-line))
  1158. (count-lines before-blank (point))))
  1159. (end (point)))
  1160. (list 'clock
  1161. (list :status status
  1162. :value value
  1163. :time time
  1164. :begin begin
  1165. :end end
  1166. :post-blank post-blank)))))
  1167. (defun org-element-clock-interpreter (clock contents)
  1168. "Interpret CLOCK element as Org syntax.
  1169. CONTENTS is nil."
  1170. (concat org-clock-string " "
  1171. (org-element-property :value clock)
  1172. (let ((time (org-element-property :time clock)))
  1173. (and time
  1174. (concat " => "
  1175. (apply 'format
  1176. "%2s:%02s"
  1177. (org-split-string time ":")))))))
  1178. ;;;; Comment
  1179. (defun org-element-comment-parser (limit)
  1180. "Parse a comment.
  1181. LIMIT bounds the search.
  1182. Return a list whose CAR is `comment' and CDR is a plist
  1183. containing `:begin', `:end', `:value' and `:post-blank'
  1184. keywords.
  1185. Assume point is at comment beginning."
  1186. (save-excursion
  1187. (let* ((keywords (org-element--collect-affiliated-keywords))
  1188. (begin (car keywords))
  1189. ;; Match first line with a loose regexp since it might as
  1190. ;; well be an ill-defined keyword.
  1191. (value (prog2 (looking-at "[ \t]*# ?")
  1192. (buffer-substring-no-properties
  1193. (match-end 0) (line-end-position))
  1194. (forward-line)))
  1195. (com-end
  1196. ;; Get comments ending.
  1197. (progn
  1198. (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
  1199. ;; Accumulate lines without leading hash and first
  1200. ;; whitespace.
  1201. (setq value
  1202. (concat value
  1203. "\n"
  1204. (buffer-substring-no-properties
  1205. (match-end 0) (line-end-position))))
  1206. (forward-line))
  1207. (point)))
  1208. (end (progn (goto-char com-end)
  1209. (skip-chars-forward " \r\t\n" limit)
  1210. (if (eobp) (point) (point-at-bol)))))
  1211. (list 'comment
  1212. (nconc
  1213. (list :begin begin
  1214. :end end
  1215. :value value
  1216. :post-blank (count-lines com-end end))
  1217. (cadr keywords))))))
  1218. (defun org-element-comment-interpreter (comment contents)
  1219. "Interpret COMMENT element as Org syntax.
  1220. CONTENTS is nil."
  1221. (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
  1222. ;;;; Comment Block
  1223. (defun org-element-comment-block-parser (limit)
  1224. "Parse an export block.
  1225. LIMIT bounds the search.
  1226. Return a list whose CAR is `comment-block' and CDR is a plist
  1227. containing `:begin', `:end', `:hiddenp', `:value' and
  1228. `:post-blank' keywords.
  1229. Assume point is at comment block beginning."
  1230. (let ((case-fold-search t))
  1231. (if (not (save-excursion
  1232. (re-search-forward "^[ \t]*#\\+END_COMMENT" limit t)))
  1233. ;; Incomplete block: parse it as a comment.
  1234. (org-element-comment-parser limit)
  1235. (let ((contents-end (match-beginning 0)))
  1236. (save-excursion
  1237. (let* ((keywords (org-element--collect-affiliated-keywords))
  1238. (begin (car keywords))
  1239. (contents-begin (progn (forward-line) (point)))
  1240. (hidden (org-invisible-p2))
  1241. (pos-before-blank (progn (goto-char contents-end)
  1242. (forward-line)
  1243. (point)))
  1244. (end (progn (skip-chars-forward " \r\t\n" limit)
  1245. (if (eobp) (point) (point-at-bol))))
  1246. (value (buffer-substring-no-properties
  1247. contents-begin contents-end)))
  1248. (list 'comment-block
  1249. (nconc
  1250. (list :begin begin
  1251. :end end
  1252. :value value
  1253. :hiddenp hidden
  1254. :post-blank (count-lines pos-before-blank end))
  1255. (cadr keywords)))))))))
  1256. (defun org-element-comment-block-interpreter (comment-block contents)
  1257. "Interpret COMMENT-BLOCK element as Org syntax.
  1258. CONTENTS is nil."
  1259. (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
  1260. (org-remove-indentation (org-element-property :value comment-block))))
  1261. ;;;; Example Block
  1262. (defun org-element-example-block-parser (limit)
  1263. "Parse an example block.
  1264. LIMIT bounds the search.
  1265. Return a list whose CAR is `example-block' and CDR is a plist
  1266. containing `:begin', `:end', `:number-lines', `:preserve-indent',
  1267. `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
  1268. `:switches', `:value' and `:post-blank' keywords."
  1269. (let ((case-fold-search t))
  1270. (if (not (save-excursion
  1271. (re-search-forward "^[ \t]*#\\+END_EXAMPLE" limit t)))
  1272. ;; Incomplete block: parse it as a comment.
  1273. (org-element-comment-parser limit)
  1274. (let ((contents-end (match-beginning 0)))
  1275. (save-excursion
  1276. (let* ((switches
  1277. (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
  1278. (org-match-string-no-properties 1)))
  1279. ;; Switches analysis
  1280. (number-lines (cond ((not switches) nil)
  1281. ((string-match "-n\\>" switches) 'new)
  1282. ((string-match "+n\\>" switches) 'continued)))
  1283. (preserve-indent (and switches (string-match "-i\\>" switches)))
  1284. ;; Should labels be retained in (or stripped from) example
  1285. ;; blocks?
  1286. (retain-labels
  1287. (or (not switches)
  1288. (not (string-match "-r\\>" switches))
  1289. (and number-lines (string-match "-k\\>" switches))))
  1290. ;; What should code-references use - labels or
  1291. ;; line-numbers?
  1292. (use-labels
  1293. (or (not switches)
  1294. (and retain-labels (not (string-match "-k\\>" switches)))))
  1295. (label-fmt (and switches
  1296. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1297. (match-string 1 switches)))
  1298. ;; Standard block parsing.
  1299. (keywords (org-element--collect-affiliated-keywords))
  1300. (begin (car keywords))
  1301. (contents-begin (progn (forward-line) (point)))
  1302. (hidden (org-invisible-p2))
  1303. (value (buffer-substring-no-properties contents-begin contents-end))
  1304. (pos-before-blank (progn (goto-char contents-end)
  1305. (forward-line)
  1306. (point)))
  1307. (end (progn (skip-chars-forward " \r\t\n" limit)
  1308. (if (eobp) (point) (point-at-bol)))))
  1309. (list 'example-block
  1310. (nconc
  1311. (list :begin begin
  1312. :end end
  1313. :value value
  1314. :switches switches
  1315. :number-lines number-lines
  1316. :preserve-indent preserve-indent
  1317. :retain-labels retain-labels
  1318. :use-labels use-labels
  1319. :label-fmt label-fmt
  1320. :hiddenp hidden
  1321. :post-blank (count-lines pos-before-blank end))
  1322. (cadr keywords)))))))))
  1323. (defun org-element-example-block-interpreter (example-block contents)
  1324. "Interpret EXAMPLE-BLOCK element as Org syntax.
  1325. CONTENTS is nil."
  1326. (let ((switches (org-element-property :switches example-block)))
  1327. (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
  1328. (org-remove-indentation
  1329. (org-element-property :value example-block))
  1330. "#+END_EXAMPLE")))
  1331. ;;;; Export Block
  1332. (defun org-element-export-block-parser (limit)
  1333. "Parse an export block.
  1334. LIMIT bounds the search.
  1335. Return a list whose CAR is `export-block' and CDR is a plist
  1336. containing `:begin', `:end', `:type', `:hiddenp', `:value' and
  1337. `:post-blank' keywords.
  1338. Assume point is at export-block beginning."
  1339. (let* ((case-fold-search t)
  1340. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1341. (upcase (org-match-string-no-properties 1)))))
  1342. (if (not (save-excursion
  1343. (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
  1344. ;; Incomplete block: parse it as a comment.
  1345. (org-element-comment-parser limit)
  1346. (let ((contents-end (match-beginning 0)))
  1347. (save-excursion
  1348. (let* ((keywords (org-element--collect-affiliated-keywords))
  1349. (begin (car keywords))
  1350. (contents-begin (progn (forward-line) (point)))
  1351. (hidden (org-invisible-p2))
  1352. (pos-before-blank (progn (goto-char contents-end)
  1353. (forward-line)
  1354. (point)))
  1355. (end (progn (skip-chars-forward " \r\t\n" limit)
  1356. (if (eobp) (point) (point-at-bol))))
  1357. (value (buffer-substring-no-properties contents-begin
  1358. contents-end)))
  1359. (list 'export-block
  1360. (nconc
  1361. (list :begin begin
  1362. :end end
  1363. :type type
  1364. :value value
  1365. :hiddenp hidden
  1366. :post-blank (count-lines pos-before-blank end))
  1367. (cadr keywords)))))))))
  1368. (defun org-element-export-block-interpreter (export-block contents)
  1369. "Interpret EXPORT-BLOCK element as Org syntax.
  1370. CONTENTS is nil."
  1371. (let ((type (org-element-property :type export-block)))
  1372. (concat (format "#+BEGIN_%s\n" type)
  1373. (org-element-property :value export-block)
  1374. (format "#+END_%s" type))))
  1375. ;;;; Fixed-width
  1376. (defun org-element-fixed-width-parser (limit)
  1377. "Parse a fixed-width section.
  1378. LIMIT bounds the search.
  1379. Return a list whose CAR is `fixed-width' and CDR is a plist
  1380. containing `:begin', `:end', `:value' and `:post-blank' keywords.
  1381. Assume point is at the beginning of the fixed-width area."
  1382. (save-excursion
  1383. (let* ((keywords (org-element--collect-affiliated-keywords))
  1384. (begin (car keywords))
  1385. value
  1386. (end-area
  1387. (progn
  1388. (while (and (< (point) limit)
  1389. (looking-at "[ \t]*:\\( \\|$\\)"))
  1390. ;; Accumulate text without starting colons.
  1391. (setq value
  1392. (concat value
  1393. (buffer-substring-no-properties
  1394. (match-end 0) (point-at-eol))
  1395. "\n"))
  1396. (forward-line))
  1397. (point)))
  1398. (end (progn (skip-chars-forward " \r\t\n" limit)
  1399. (if (eobp) (point) (point-at-bol)))))
  1400. (list 'fixed-width
  1401. (nconc
  1402. (list :begin begin
  1403. :end end
  1404. :value value
  1405. :post-blank (count-lines end-area end))
  1406. (cadr keywords))))))
  1407. (defun org-element-fixed-width-interpreter (fixed-width contents)
  1408. "Interpret FIXED-WIDTH element as Org syntax.
  1409. CONTENTS is nil."
  1410. (replace-regexp-in-string
  1411. "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
  1412. ;;;; Horizontal Rule
  1413. (defun org-element-horizontal-rule-parser (limit)
  1414. "Parse an horizontal rule.
  1415. LIMIT bounds the search.
  1416. Return a list whose CAR is `horizontal-rule' and CDR is a plist
  1417. containing `:begin', `:end' and `:post-blank' keywords."
  1418. (save-excursion
  1419. (let* ((keywords (org-element--collect-affiliated-keywords))
  1420. (begin (car keywords))
  1421. (post-hr (progn (forward-line) (point)))
  1422. (end (progn (skip-chars-forward " \r\t\n" limit)
  1423. (if (eobp) (point) (point-at-bol)))))
  1424. (list 'horizontal-rule
  1425. (nconc
  1426. (list :begin begin
  1427. :end end
  1428. :post-blank (count-lines post-hr end))
  1429. (cadr keywords))))))
  1430. (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
  1431. "Interpret HORIZONTAL-RULE element as Org syntax.
  1432. CONTENTS is nil."
  1433. "-----")
  1434. ;;;; Keyword
  1435. (defun org-element-keyword-parser (limit)
  1436. "Parse a keyword at point.
  1437. LIMIT bounds the search.
  1438. Return a list whose CAR is `keyword' and CDR is a plist
  1439. containing `:key', `:value', `:begin', `:end' and `:post-blank'
  1440. keywords."
  1441. (save-excursion
  1442. (let* ((case-fold-search t)
  1443. (begin (point))
  1444. (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
  1445. (upcase (org-match-string-no-properties 1))))
  1446. (value (org-trim (buffer-substring-no-properties
  1447. (match-end 0) (point-at-eol))))
  1448. (pos-before-blank (progn (forward-line) (point)))
  1449. (end (progn (skip-chars-forward " \r\t\n" limit)
  1450. (if (eobp) (point) (point-at-bol)))))
  1451. (list 'keyword
  1452. (list :key key
  1453. :value value
  1454. :begin begin
  1455. :end end
  1456. :post-blank (count-lines pos-before-blank end))))))
  1457. (defun org-element-keyword-interpreter (keyword contents)
  1458. "Interpret KEYWORD element as Org syntax.
  1459. CONTENTS is nil."
  1460. (format "#+%s: %s"
  1461. (org-element-property :key keyword)
  1462. (org-element-property :value keyword)))
  1463. ;;;; Latex Environment
  1464. (defun org-element-latex-environment-parser (limit)
  1465. "Parse a LaTeX environment.
  1466. LIMIT bounds the search.
  1467. Return a list whose CAR is `latex-environment' and CDR is a plist
  1468. containing `:begin', `:end', `:value' and `:post-blank'
  1469. keywords.
  1470. Assume point is at the beginning of the latex environment."
  1471. (save-excursion
  1472. (let* ((case-fold-search t)
  1473. (code-begin (point))
  1474. (keywords (org-element--collect-affiliated-keywords))
  1475. (begin (car keywords))
  1476. (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
  1477. (regexp-quote (match-string 1))))
  1478. (code-end
  1479. (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env) limit t)
  1480. (forward-line)
  1481. (point)))
  1482. (value (buffer-substring-no-properties code-begin code-end))
  1483. (end (progn (skip-chars-forward " \r\t\n" limit)
  1484. (if (eobp) (point) (point-at-bol)))))
  1485. (list 'latex-environment
  1486. (nconc
  1487. (list :begin begin
  1488. :end end
  1489. :value value
  1490. :post-blank (count-lines code-end end))
  1491. (cadr keywords))))))
  1492. (defun org-element-latex-environment-interpreter (latex-environment contents)
  1493. "Interpret LATEX-ENVIRONMENT element as Org syntax.
  1494. CONTENTS is nil."
  1495. (org-element-property :value latex-environment))
  1496. ;;;; Paragraph
  1497. (defun org-element-paragraph-parser (limit)
  1498. "Parse a paragraph.
  1499. LIMIT bounds the search.
  1500. Return a list whose CAR is `paragraph' and CDR is a plist
  1501. containing `:begin', `:end', `:contents-begin' and
  1502. `:contents-end' and `:post-blank' keywords.
  1503. Assume point is at the beginning of the paragraph."
  1504. (save-excursion
  1505. (let* ((contents-begin (point))
  1506. (keywords (org-element--collect-affiliated-keywords))
  1507. (begin (car keywords))
  1508. (before-blank
  1509. (progn (end-of-line)
  1510. (if (re-search-forward org-element-paragraph-separate
  1511. limit
  1512. 'm)
  1513. (goto-char (match-beginning 0))
  1514. (point))))
  1515. (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
  1516. (forward-line)
  1517. (point)))
  1518. (end (progn (skip-chars-forward " \r\t\n" limit)
  1519. (if (eobp) (point) (point-at-bol)))))
  1520. (list 'paragraph
  1521. ;; If paragraph has no affiliated keywords, it may not begin
  1522. ;; at beginning of line if it starts an item.
  1523. (nconc
  1524. (list :begin (if (cadr keywords) begin contents-begin)
  1525. :end end
  1526. :contents-begin contents-begin
  1527. :contents-end contents-end
  1528. :post-blank (count-lines before-blank end))
  1529. (cadr keywords))))))
  1530. (defun org-element-paragraph-interpreter (paragraph contents)
  1531. "Interpret PARAGRAPH element as Org syntax.
  1532. CONTENTS is the contents of the element."
  1533. contents)
  1534. ;;;; Planning
  1535. (defun org-element-planning-parser (limit)
  1536. "Parse a planning.
  1537. LIMIT bounds the search.
  1538. Return a list whose CAR is `planning' and CDR is a plist
  1539. containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
  1540. and `:post-blank' keywords."
  1541. (save-excursion
  1542. (let* ((case-fold-search nil)
  1543. (begin (point))
  1544. (post-blank (let ((before-blank (progn (forward-line) (point))))
  1545. (skip-chars-forward " \r\t\n" limit)
  1546. (unless (eobp) (beginning-of-line))
  1547. (count-lines before-blank (point))))
  1548. (end (point))
  1549. closed deadline scheduled)
  1550. (goto-char begin)
  1551. (while (re-search-forward org-keyword-time-not-clock-regexp
  1552. (line-end-position) t)
  1553. (goto-char (match-end 1))
  1554. (org-skip-whitespace)
  1555. (let ((time (buffer-substring-no-properties
  1556. (1+ (point)) (1- (match-end 0))))
  1557. (keyword (match-string 1)))
  1558. (cond ((equal keyword org-closed-string) (setq closed time))
  1559. ((equal keyword org-deadline-string) (setq deadline time))
  1560. (t (setq scheduled time)))))
  1561. (list 'planning
  1562. (list :closed closed
  1563. :deadline deadline
  1564. :scheduled scheduled
  1565. :begin begin
  1566. :end end
  1567. :post-blank post-blank)))))
  1568. (defun org-element-planning-interpreter (planning contents)
  1569. "Interpret PLANNING element as Org syntax.
  1570. CONTENTS is nil."
  1571. (mapconcat
  1572. 'identity
  1573. (delq nil
  1574. (list (let ((closed (org-element-property :closed planning)))
  1575. (when closed (concat org-closed-string " [" closed "]")))
  1576. (let ((deadline (org-element-property :deadline planning)))
  1577. (when deadline (concat org-deadline-string " <" deadline ">")))
  1578. (let ((scheduled (org-element-property :scheduled planning)))
  1579. (when scheduled
  1580. (concat org-scheduled-string " <" scheduled ">")))))
  1581. " "))
  1582. ;;;; Property Drawer
  1583. (defun org-element-property-drawer-parser (limit)
  1584. "Parse a property drawer.
  1585. LIMIT bounds the search.
  1586. Return a list whose CAR is `property-drawer' and CDR is a plist
  1587. containing `:begin', `:end', `:hiddenp', `:contents-begin',
  1588. `:contents-end', `:properties' and `:post-blank' keywords.
  1589. Assume point is at the beginning of the property drawer."
  1590. (save-excursion
  1591. (let ((case-fold-search t)
  1592. (begin (point))
  1593. (prop-begin (progn (forward-line) (point)))
  1594. (hidden (org-invisible-p2))
  1595. (properties
  1596. (let (val)
  1597. (while (not (looking-at "^[ \t]*:END:"))
  1598. (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
  1599. (push (cons (org-match-string-no-properties 1)
  1600. (org-trim
  1601. (buffer-substring-no-properties
  1602. (match-end 0) (point-at-eol))))
  1603. val))
  1604. (forward-line))
  1605. val))
  1606. (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t)
  1607. (point-at-bol)))
  1608. (pos-before-blank (progn (forward-line) (point)))
  1609. (end (progn (skip-chars-forward " \r\t\n" limit)
  1610. (if (eobp) (point) (point-at-bol)))))
  1611. (list 'property-drawer
  1612. (list :begin begin
  1613. :end end
  1614. :hiddenp hidden
  1615. :properties properties
  1616. :post-blank (count-lines pos-before-blank end))))))
  1617. (defun org-element-property-drawer-interpreter (property-drawer contents)
  1618. "Interpret PROPERTY-DRAWER element as Org syntax.
  1619. CONTENTS is nil."
  1620. (let ((props (org-element-property :properties property-drawer)))
  1621. (concat
  1622. ":PROPERTIES:\n"
  1623. (mapconcat (lambda (p)
  1624. (format org-property-format (format ":%s:" (car p)) (cdr p)))
  1625. (nreverse props) "\n")
  1626. "\n:END:")))
  1627. ;;;; Quote Section
  1628. (defun org-element-quote-section-parser (limit)
  1629. "Parse a quote section.
  1630. LIMIT bounds the search.
  1631. Return a list whose CAR is `quote-section' and CDR is a plist
  1632. containing `:begin', `:end', `:value' and `:post-blank' keywords.
  1633. Assume point is at beginning of the section."
  1634. (save-excursion
  1635. (let* ((begin (point))
  1636. (end (progn (org-with-limited-levels (outline-next-heading))
  1637. (point)))
  1638. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1639. (forward-line)
  1640. (point)))
  1641. (value (buffer-substring-no-properties begin pos-before-blank)))
  1642. (list 'quote-section
  1643. (list :begin begin
  1644. :end end
  1645. :value value
  1646. :post-blank (count-lines pos-before-blank end))))))
  1647. (defun org-element-quote-section-interpreter (quote-section contents)
  1648. "Interpret QUOTE-SECTION element as Org syntax.
  1649. CONTENTS is nil."
  1650. (org-element-property :value quote-section))
  1651. ;;;; Src Block
  1652. (defun org-element-src-block-parser (limit)
  1653. "Parse a src block.
  1654. LIMIT bounds the search.
  1655. Return a list whose CAR is `src-block' and CDR is a plist
  1656. containing `:language', `:switches', `:parameters', `:begin',
  1657. `:end', `:hiddenp', `:number-lines', `:retain-labels',
  1658. `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
  1659. `:post-blank' keywords.
  1660. Assume point is at the beginning of the block."
  1661. (let ((case-fold-search t))
  1662. (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t)))
  1663. ;; Incomplete block: parse it as a comment.
  1664. (org-element-comment-parser limit)
  1665. (let ((contents-end (match-beginning 0)))
  1666. (save-excursion
  1667. (let* ((keywords (org-element--collect-affiliated-keywords))
  1668. ;; Get beginning position.
  1669. (begin (car keywords))
  1670. ;; Get language as a string.
  1671. (language
  1672. (progn
  1673. (looking-at
  1674. (concat "^[ \t]*#\\+BEGIN_SRC"
  1675. "\\(?: +\\(\\S-+\\)\\)?"
  1676. "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
  1677. "\\(.*\\)[ \t]*$"))
  1678. (org-match-string-no-properties 1)))
  1679. ;; Get switches.
  1680. (switches (org-match-string-no-properties 2))
  1681. ;; Get parameters.
  1682. (parameters (org-match-string-no-properties 3))
  1683. ;; Switches analysis
  1684. (number-lines (cond ((not switches) nil)
  1685. ((string-match "-n\\>" switches) 'new)
  1686. ((string-match "+n\\>" switches) 'continued)))
  1687. (preserve-indent (and switches (string-match "-i\\>" switches)))
  1688. (label-fmt (and switches
  1689. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1690. (match-string 1 switches)))
  1691. ;; Should labels be retained in (or stripped from)
  1692. ;; src blocks?
  1693. (retain-labels
  1694. (or (not switches)
  1695. (not (string-match "-r\\>" switches))
  1696. (and number-lines (string-match "-k\\>" switches))))
  1697. ;; What should code-references use - labels or
  1698. ;; line-numbers?
  1699. (use-labels
  1700. (or (not switches)
  1701. (and retain-labels (not (string-match "-k\\>" switches)))))
  1702. ;; Get visibility status.
  1703. (hidden (progn (forward-line) (org-invisible-p2)))
  1704. ;; Retrieve code.
  1705. (value (buffer-substring-no-properties (point) contents-end))
  1706. (pos-before-blank (progn (goto-char contents-end)
  1707. (forward-line)
  1708. (point)))
  1709. ;; Get position after ending blank lines.
  1710. (end (progn (skip-chars-forward " \r\t\n" limit)
  1711. (if (eobp) (point) (point-at-bol)))))
  1712. (list 'src-block
  1713. (nconc
  1714. (list :language language
  1715. :switches (and (org-string-nw-p switches)
  1716. (org-trim switches))
  1717. :parameters (and (org-string-nw-p parameters)
  1718. (org-trim parameters))
  1719. :begin begin
  1720. :end end
  1721. :number-lines number-lines
  1722. :preserve-indent preserve-indent
  1723. :retain-labels retain-labels
  1724. :use-labels use-labels
  1725. :label-fmt label-fmt
  1726. :hiddenp hidden
  1727. :value value
  1728. :post-blank (count-lines pos-before-blank end))
  1729. (cadr keywords)))))))))
  1730. (defun org-element-src-block-interpreter (src-block contents)
  1731. "Interpret SRC-BLOCK element as Org syntax.
  1732. CONTENTS is nil."
  1733. (let ((lang (org-element-property :language src-block))
  1734. (switches (org-element-property :switches src-block))
  1735. (params (org-element-property :parameters src-block))
  1736. (value (let ((val (org-element-property :value src-block)))
  1737. (cond
  1738. (org-src-preserve-indentation val)
  1739. ((zerop org-edit-src-content-indentation)
  1740. (org-remove-indentation val))
  1741. (t
  1742. (let ((ind (make-string
  1743. org-edit-src-content-indentation 32)))
  1744. (replace-regexp-in-string
  1745. "\\(^\\)[ \t]*\\S-" ind
  1746. (org-remove-indentation val) nil nil 1)))))))
  1747. (concat (format "#+BEGIN_SRC%s\n"
  1748. (concat (and lang (concat " " lang))
  1749. (and switches (concat " " switches))
  1750. (and params (concat " " params))))
  1751. value
  1752. "#+END_SRC")))
  1753. ;;;; Table
  1754. (defun org-element-table-parser (limit)
  1755. "Parse a table at point.
  1756. LIMIT bounds the search.
  1757. Return a list whose CAR is `table' and CDR is a plist containing
  1758. `:begin', `:end', `:tblfm', `:type', `:contents-begin',
  1759. `:contents-end', `:value' and `:post-blank' keywords.
  1760. Assume point is at the beginning of the table."
  1761. (save-excursion
  1762. (let* ((case-fold-search t)
  1763. (table-begin (point))
  1764. (type (if (org-at-table.el-p) 'table.el 'org))
  1765. (keywords (org-element--collect-affiliated-keywords))
  1766. (begin (car keywords))
  1767. (table-end (goto-char (marker-position (org-table-end t))))
  1768. (tblfm (let (acc)
  1769. (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
  1770. (push (org-match-string-no-properties 1) acc)
  1771. (forward-line))
  1772. acc))
  1773. (pos-before-blank (point))
  1774. (end (progn (skip-chars-forward " \r\t\n" limit)
  1775. (if (eobp) (point) (point-at-bol)))))
  1776. (list 'table
  1777. (nconc
  1778. (list :begin begin
  1779. :end end
  1780. :type type
  1781. :tblfm tblfm
  1782. ;; Only `org' tables have contents. `table.el' tables
  1783. ;; use a `:value' property to store raw table as
  1784. ;; a string.
  1785. :contents-begin (and (eq type 'org) table-begin)
  1786. :contents-end (and (eq type 'org) table-end)
  1787. :value (and (eq type 'table.el)
  1788. (buffer-substring-no-properties
  1789. table-begin table-end))
  1790. :post-blank (count-lines pos-before-blank end))
  1791. (cadr keywords))))))
  1792. (defun org-element-table-interpreter (table contents)
  1793. "Interpret TABLE element as Org syntax.
  1794. CONTENTS is nil."
  1795. (if (eq (org-element-property :type table) 'table.el)
  1796. (org-remove-indentation (org-element-property :value table))
  1797. (concat (with-temp-buffer (insert contents)
  1798. (org-table-align)
  1799. (buffer-string))
  1800. (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
  1801. (reverse (org-element-property :tblfm table))
  1802. "\n"))))
  1803. ;;;; Table Row
  1804. (defun org-element-table-row-parser (limit)
  1805. "Parse table row at point.
  1806. LIMIT bounds the search.
  1807. Return a list whose CAR is `table-row' and CDR is a plist
  1808. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1809. `:type' and `:post-blank' keywords."
  1810. (save-excursion
  1811. (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
  1812. (begin (point))
  1813. ;; A table rule has no contents. In that case, ensure
  1814. ;; CONTENTS-BEGIN matches CONTENTS-END.
  1815. (contents-begin (and (eq type 'standard)
  1816. (search-forward "|")
  1817. (point)))
  1818. (contents-end (and (eq type 'standard)
  1819. (progn
  1820. (end-of-line)
  1821. (skip-chars-backward " \t")
  1822. (point))))
  1823. (end (progn (forward-line) (point))))
  1824. (list 'table-row
  1825. (list :type type
  1826. :begin begin
  1827. :end end
  1828. :contents-begin contents-begin
  1829. :contents-end contents-end
  1830. :post-blank 0)))))
  1831. (defun org-element-table-row-interpreter (table-row contents)
  1832. "Interpret TABLE-ROW element as Org syntax.
  1833. CONTENTS is the contents of the table row."
  1834. (if (eq (org-element-property :type table-row) 'rule) "|-"
  1835. (concat "| " contents)))
  1836. ;;;; Verse Block
  1837. (defun org-element-verse-block-parser (limit)
  1838. "Parse a verse block.
  1839. LIMIT bounds the search.
  1840. Return a list whose CAR is `verse-block' and CDR is a plist
  1841. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1842. `:hiddenp' and `:post-blank' keywords.
  1843. Assume point is at beginning of the block."
  1844. (let ((case-fold-search t))
  1845. (if (not (save-excursion
  1846. (re-search-forward "^[ \t]*#\\+END_VERSE" limit t)))
  1847. ;; Incomplete block: parse it as a comment.
  1848. (org-element-comment-parser limit)
  1849. (let ((contents-end (match-beginning 0)))
  1850. (save-excursion
  1851. (let* ((keywords (org-element--collect-affiliated-keywords))
  1852. (begin (car keywords))
  1853. (hidden (progn (forward-line) (org-invisible-p2)))
  1854. (contents-begin (point))
  1855. (pos-before-blank (progn (goto-char contents-end)
  1856. (forward-line)
  1857. (point)))
  1858. (end (progn (skip-chars-forward " \r\t\n" limit)
  1859. (if (eobp) (point) (point-at-bol)))))
  1860. (list 'verse-block
  1861. (nconc
  1862. (list :begin begin
  1863. :end end
  1864. :contents-begin contents-begin
  1865. :contents-end contents-end
  1866. :hiddenp hidden
  1867. :post-blank (count-lines pos-before-blank end))
  1868. (cadr keywords)))))))))
  1869. (defun org-element-verse-block-interpreter (verse-block contents)
  1870. "Interpret VERSE-BLOCK element as Org syntax.
  1871. CONTENTS is verse block contents."
  1872. (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
  1873. ;;; Objects
  1874. ;;
  1875. ;; Unlike to elements, interstices can be found between objects.
  1876. ;; That's why, along with the parser, successor functions are provided
  1877. ;; for each object. Some objects share the same successor (i.e. `code'
  1878. ;; and `verbatim' objects).
  1879. ;;
  1880. ;; A successor must accept a single argument bounding the search. It
  1881. ;; will return either a cons cell whose CAR is the object's type, as
  1882. ;; a symbol, and CDR the position of its next occurrence, or nil.
  1883. ;;
  1884. ;; Successors follow the naming convention:
  1885. ;; org-element-NAME-successor, where NAME is the name of the
  1886. ;; successor, as defined in `org-element-all-successors'.
  1887. ;;
  1888. ;; Some object types (i.e. `italic') are recursive. Restrictions on
  1889. ;; object types they can contain will be specified in
  1890. ;; `org-element-object-restrictions'.
  1891. ;;
  1892. ;; Adding a new type of object is simple. Implement a successor,
  1893. ;; a parser, and an interpreter for it, all following the naming
  1894. ;; convention. Register type in `org-element-all-objects' and
  1895. ;; successor in `org-element-all-successors'. Maybe tweak
  1896. ;; restrictions about it, and that's it.
  1897. ;;;; Bold
  1898. (defun org-element-bold-parser ()
  1899. "Parse bold object at point.
  1900. Return a list whose CAR is `bold' and CDR is a plist with
  1901. `:begin', `:end', `:contents-begin' and `:contents-end' and
  1902. `:post-blank' keywords.
  1903. Assume point is at the first star marker."
  1904. (save-excursion
  1905. (unless (bolp) (backward-char 1))
  1906. (looking-at org-emph-re)
  1907. (let ((begin (match-beginning 2))
  1908. (contents-begin (match-beginning 4))
  1909. (contents-end (match-end 4))
  1910. (post-blank (progn (goto-char (match-end 2))
  1911. (skip-chars-forward " \t")))
  1912. (end (point)))
  1913. (list 'bold
  1914. (list :begin begin
  1915. :end end
  1916. :contents-begin contents-begin
  1917. :contents-end contents-end
  1918. :post-blank post-blank)))))
  1919. (defun org-element-bold-interpreter (bold contents)
  1920. "Interpret BOLD object as Org syntax.
  1921. CONTENTS is the contents of the object."
  1922. (format "*%s*" contents))
  1923. (defun org-element-text-markup-successor (limit)
  1924. "Search for the next text-markup object.
  1925. LIMIT bounds the search.
  1926. LIMIT bounds the search.
  1927. Return value is a cons cell whose CAR is a symbol among `bold',
  1928. `italic', `underline', `strike-through', `code' and `verbatim'
  1929. and CDR is beginning position."
  1930. (save-excursion
  1931. (unless (bolp) (backward-char))
  1932. (when (re-search-forward org-emph-re limit t)
  1933. (let ((marker (match-string 3)))
  1934. (cons (cond
  1935. ((equal marker "*") 'bold)
  1936. ((equal marker "/") 'italic)
  1937. ((equal marker "_") 'underline)
  1938. ((equal marker "+") 'strike-through)
  1939. ((equal marker "~") 'code)
  1940. ((equal marker "=") 'verbatim)
  1941. (t (error "Unknown marker at %d" (match-beginning 3))))
  1942. (match-beginning 2))))))
  1943. ;;;; Code
  1944. (defun org-element-code-parser ()
  1945. "Parse code object at point.
  1946. Return a list whose CAR is `code' and CDR is a plist with
  1947. `:value', `:begin', `:end' and `:post-blank' keywords.
  1948. Assume point is at the first tilde marker."
  1949. (save-excursion
  1950. (unless (bolp) (backward-char 1))
  1951. (looking-at org-emph-re)
  1952. (let ((begin (match-beginning 2))
  1953. (value (org-match-string-no-properties 4))
  1954. (post-blank (progn (goto-char (match-end 2))
  1955. (skip-chars-forward " \t")))
  1956. (end (point)))
  1957. (list 'code
  1958. (list :value value
  1959. :begin begin
  1960. :end end
  1961. :post-blank post-blank)))))
  1962. (defun org-element-code-interpreter (code contents)
  1963. "Interpret CODE object as Org syntax.
  1964. CONTENTS is nil."
  1965. (format "~%s~" (org-element-property :value code)))
  1966. ;;;; Entity
  1967. (defun org-element-entity-parser ()
  1968. "Parse entity at point.
  1969. Return a list whose CAR is `entity' and CDR a plist with
  1970. `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
  1971. `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
  1972. keywords.
  1973. Assume point is at the beginning of the entity."
  1974. (save-excursion
  1975. (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
  1976. (let* ((value (org-entity-get (match-string 1)))
  1977. (begin (match-beginning 0))
  1978. (bracketsp (string= (match-string 2) "{}"))
  1979. (post-blank (progn (goto-char (match-end 1))
  1980. (when bracketsp (forward-char 2))
  1981. (skip-chars-forward " \t")))
  1982. (end (point)))
  1983. (list 'entity
  1984. (list :name (car value)
  1985. :latex (nth 1 value)
  1986. :latex-math-p (nth 2 value)
  1987. :html (nth 3 value)
  1988. :ascii (nth 4 value)
  1989. :latin1 (nth 5 value)
  1990. :utf-8 (nth 6 value)
  1991. :begin begin
  1992. :end end
  1993. :use-brackets-p bracketsp
  1994. :post-blank post-blank)))))
  1995. (defun org-element-entity-interpreter (entity contents)
  1996. "Interpret ENTITY object as Org syntax.
  1997. CONTENTS is nil."
  1998. (concat "\\"
  1999. (org-element-property :name entity)
  2000. (when (org-element-property :use-brackets-p entity) "{}")))
  2001. (defun org-element-latex-or-entity-successor (limit)
  2002. "Search for the next latex-fragment or entity object.
  2003. LIMIT bounds the search.
  2004. Return value is a cons cell whose CAR is `entity' or
  2005. `latex-fragment' and CDR is beginning position."
  2006. (save-excursion
  2007. (let ((matchers (plist-get org-format-latex-options :matchers))
  2008. ;; ENTITY-RE matches both LaTeX commands and Org entities.
  2009. (entity-re
  2010. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
  2011. (when (re-search-forward
  2012. (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
  2013. matchers "\\|")
  2014. "\\|" entity-re)
  2015. limit t)
  2016. (goto-char (match-beginning 0))
  2017. (if (looking-at entity-re)
  2018. ;; Determine if it's a real entity or a LaTeX command.
  2019. (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
  2020. (match-beginning 0))
  2021. ;; No entity nor command: point is at a LaTeX fragment.
  2022. ;; Determine its type to get the correct beginning position.
  2023. (cons 'latex-fragment
  2024. (catch 'return
  2025. (mapc (lambda (e)
  2026. (when (looking-at (nth 1 (assoc e org-latex-regexps)))
  2027. (throw 'return
  2028. (match-beginning
  2029. (nth 2 (assoc e org-latex-regexps))))))
  2030. matchers)
  2031. (point))))))))
  2032. ;;;; Export Snippet
  2033. (defun org-element-export-snippet-parser ()
  2034. "Parse export snippet at point.
  2035. Return a list whose CAR is `export-snippet' and CDR a plist with
  2036. `:begin', `:end', `:back-end', `:value' and `:post-blank' as
  2037. keywords.
  2038. Assume point is at the beginning of the snippet."
  2039. (save-excursion
  2040. (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
  2041. (let* ((begin (match-beginning 0))
  2042. (back-end (org-match-string-no-properties 1))
  2043. (value (buffer-substring-no-properties
  2044. (point)
  2045. (progn (re-search-forward "@@" nil t) (match-beginning 0))))
  2046. (post-blank (skip-chars-forward " \t"))
  2047. (end (point)))
  2048. (list 'export-snippet
  2049. (list :back-end back-end
  2050. :value value
  2051. :begin begin
  2052. :end end
  2053. :post-blank post-blank)))))
  2054. (defun org-element-export-snippet-interpreter (export-snippet contents)
  2055. "Interpret EXPORT-SNIPPET object as Org syntax.
  2056. CONTENTS is nil."
  2057. (format "@@%s:%s@@"
  2058. (org-element-property :back-end export-snippet)
  2059. (org-element-property :value export-snippet)))
  2060. (defun org-element-export-snippet-successor (limit)
  2061. "Search for the next export-snippet object.
  2062. LIMIT bounds the search.
  2063. Return value is a cons cell whose CAR is `export-snippet' and CDR
  2064. its beginning position."
  2065. (save-excursion
  2066. (let (beg)
  2067. (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
  2068. (setq beg (match-beginning 0))
  2069. (re-search-forward "@@" limit t))
  2070. (cons 'export-snippet beg)))))
  2071. ;;;; Footnote Reference
  2072. (defun org-element-footnote-reference-parser ()
  2073. "Parse footnote reference at point.
  2074. Return a list whose CAR is `footnote-reference' and CDR a plist
  2075. with `:label', `:type', `:inline-definition', `:begin', `:end'
  2076. and `:post-blank' as keywords."
  2077. (save-excursion
  2078. (looking-at org-footnote-re)
  2079. (let* ((begin (point))
  2080. (label (or (org-match-string-no-properties 2)
  2081. (org-match-string-no-properties 3)
  2082. (and (match-string 1)
  2083. (concat "fn:" (org-match-string-no-properties 1)))))
  2084. (type (if (or (not label) (match-string 1)) 'inline 'standard))
  2085. (inner-begin (match-end 0))
  2086. (inner-end
  2087. (let ((count 1))
  2088. (forward-char)
  2089. (while (and (> count 0) (re-search-forward "[][]" nil t))
  2090. (if (equal (match-string 0) "[") (incf count) (decf count)))
  2091. (1- (point))))
  2092. (post-blank (progn (goto-char (1+ inner-end))
  2093. (skip-chars-forward " \t")))
  2094. (end (point))
  2095. (footnote-reference
  2096. (list 'footnote-reference
  2097. (list :label label
  2098. :type type
  2099. :begin begin
  2100. :end end
  2101. :post-blank post-blank))))
  2102. (org-element-put-property
  2103. footnote-reference :inline-definition
  2104. (and (eq type 'inline)
  2105. (org-element-parse-secondary-string
  2106. (buffer-substring inner-begin inner-end)
  2107. (org-element-restriction 'footnote-reference)
  2108. footnote-reference))))))
  2109. (defun org-element-footnote-reference-interpreter (footnote-reference contents)
  2110. "Interpret FOOTNOTE-REFERENCE object as Org syntax.
  2111. CONTENTS is nil."
  2112. (let ((label (or (org-element-property :label footnote-reference) "fn:"))
  2113. (def
  2114. (let ((inline-def
  2115. (org-element-property :inline-definition footnote-reference)))
  2116. (if (not inline-def) ""
  2117. (concat ":" (org-element-interpret-data inline-def))))))
  2118. (format "[%s]" (concat label def))))
  2119. (defun org-element-footnote-reference-successor (limit)
  2120. "Search for the next footnote-reference object.
  2121. LIMIT bounds the search.
  2122. Return value is a cons cell whose CAR is `footnote-reference' and
  2123. CDR is beginning position."
  2124. (save-excursion
  2125. (catch 'exit
  2126. (while (re-search-forward org-footnote-re limit t)
  2127. (save-excursion
  2128. (let ((beg (match-beginning 0))
  2129. (count 1))
  2130. (backward-char)
  2131. (while (re-search-forward "[][]" limit t)
  2132. (if (equal (match-string 0) "[") (incf count) (decf count))
  2133. (when (zerop count)
  2134. (throw 'exit (cons 'footnote-reference beg))))))))))
  2135. ;;;; Inline Babel Call
  2136. (defun org-element-inline-babel-call-parser ()
  2137. "Parse inline babel call at point.
  2138. Return a list whose CAR is `inline-babel-call' and CDR a plist
  2139. with `:begin', `:end', `:info' and `:post-blank' as keywords.
  2140. Assume point is at the beginning of the babel call."
  2141. (save-excursion
  2142. (unless (bolp) (backward-char))
  2143. (looking-at org-babel-inline-lob-one-liner-regexp)
  2144. (let ((info (save-match-data (org-babel-lob-get-info)))
  2145. (begin (match-end 1))
  2146. (post-blank (progn (goto-char (match-end 0))
  2147. (skip-chars-forward " \t")))
  2148. (end (point)))
  2149. (list 'inline-babel-call
  2150. (list :begin begin
  2151. :end end
  2152. :info info
  2153. :post-blank post-blank)))))
  2154. (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
  2155. "Interpret INLINE-BABEL-CALL object as Org syntax.
  2156. CONTENTS is nil."
  2157. (let* ((babel-info (org-element-property :info inline-babel-call))
  2158. (main-source (car babel-info))
  2159. (post-options (nth 1 babel-info)))
  2160. (concat "call_"
  2161. (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
  2162. ;; Remove redundant square brackets.
  2163. (replace-match
  2164. (match-string 1 main-source) nil nil main-source)
  2165. main-source)
  2166. (and post-options (format "[%s]" post-options)))))
  2167. (defun org-element-inline-babel-call-successor (limit)
  2168. "Search for the next inline-babel-call object.
  2169. LIMIT bounds the search.
  2170. Return value is a cons cell whose CAR is `inline-babel-call' and
  2171. CDR is beginning position."
  2172. (save-excursion
  2173. ;; Use a simplified version of
  2174. ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
  2175. (when (re-search-forward
  2176. "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
  2177. limit t)
  2178. (cons 'inline-babel-call (match-beginning 0)))))
  2179. ;;;; Inline Src Block
  2180. (defun org-element-inline-src-block-parser ()
  2181. "Parse inline source block at point.
  2182. LIMIT bounds the search.
  2183. Return a list whose CAR is `inline-src-block' and CDR a plist
  2184. with `:begin', `:end', `:language', `:value', `:parameters' and
  2185. `:post-blank' as keywords.
  2186. Assume point is at the beginning of the inline src block."
  2187. (save-excursion
  2188. (unless (bolp) (backward-char))
  2189. (looking-at org-babel-inline-src-block-regexp)
  2190. (let ((begin (match-beginning 1))
  2191. (language (org-match-string-no-properties 2))
  2192. (parameters (org-match-string-no-properties 4))
  2193. (value (org-match-string-no-properties 5))
  2194. (post-blank (progn (goto-char (match-end 0))
  2195. (skip-chars-forward " \t")))
  2196. (end (point)))
  2197. (list 'inline-src-block
  2198. (list :language language
  2199. :value value
  2200. :parameters parameters
  2201. :begin begin
  2202. :end end
  2203. :post-blank post-blank)))))
  2204. (defun org-element-inline-src-block-interpreter (inline-src-block contents)
  2205. "Interpret INLINE-SRC-BLOCK object as Org syntax.
  2206. CONTENTS is nil."
  2207. (let ((language (org-element-property :language inline-src-block))
  2208. (arguments (org-element-property :parameters inline-src-block))
  2209. (body (org-element-property :value inline-src-block)))
  2210. (format "src_%s%s{%s}"
  2211. language
  2212. (if arguments (format "[%s]" arguments) "")
  2213. body)))
  2214. (defun org-element-inline-src-block-successor (limit)
  2215. "Search for the next inline-babel-call element.
  2216. LIMIT bounds the search.
  2217. Return value is a cons cell whose CAR is `inline-babel-call' and
  2218. CDR is beginning position."
  2219. (save-excursion
  2220. (when (re-search-forward org-babel-inline-src-block-regexp limit t)
  2221. (cons 'inline-src-block (match-beginning 1)))))
  2222. ;;;; Italic
  2223. (defun org-element-italic-parser ()
  2224. "Parse italic object at point.
  2225. Return a list whose CAR is `italic' and CDR is a plist with
  2226. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2227. `:post-blank' keywords.
  2228. Assume point is at the first slash marker."
  2229. (save-excursion
  2230. (unless (bolp) (backward-char 1))
  2231. (looking-at org-emph-re)
  2232. (let ((begin (match-beginning 2))
  2233. (contents-begin (match-beginning 4))
  2234. (contents-end (match-end 4))
  2235. (post-blank (progn (goto-char (match-end 2))
  2236. (skip-chars-forward " \t")))
  2237. (end (point)))
  2238. (list 'italic
  2239. (list :begin begin
  2240. :end end
  2241. :contents-begin contents-begin
  2242. :contents-end contents-end
  2243. :post-blank post-blank)))))
  2244. (defun org-element-italic-interpreter (italic contents)
  2245. "Interpret ITALIC object as Org syntax.
  2246. CONTENTS is the contents of the object."
  2247. (format "/%s/" contents))
  2248. ;;;; Latex Fragment
  2249. (defun org-element-latex-fragment-parser ()
  2250. "Parse latex fragment at point.
  2251. Return a list whose CAR is `latex-fragment' and CDR a plist with
  2252. `:value', `:begin', `:end', and `:post-blank' as keywords.
  2253. Assume point is at the beginning of the latex fragment."
  2254. (save-excursion
  2255. (let* ((begin (point))
  2256. (substring-match
  2257. (catch 'exit
  2258. (mapc (lambda (e)
  2259. (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
  2260. (when (or (looking-at latex-regexp)
  2261. (and (not (bobp))
  2262. (save-excursion
  2263. (backward-char)
  2264. (looking-at latex-regexp))))
  2265. (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
  2266. (plist-get org-format-latex-options :matchers))
  2267. ;; None found: it's a macro.
  2268. (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
  2269. 0))
  2270. (value (match-string-no-properties substring-match))
  2271. (post-blank (progn (goto-char (match-end substring-match))
  2272. (skip-chars-forward " \t")))
  2273. (end (point)))
  2274. (list 'latex-fragment
  2275. (list :value value
  2276. :begin begin
  2277. :end end
  2278. :post-blank post-blank)))))
  2279. (defun org-element-latex-fragment-interpreter (latex-fragment contents)
  2280. "Interpret LATEX-FRAGMENT object as Org syntax.
  2281. CONTENTS is nil."
  2282. (org-element-property :value latex-fragment))
  2283. ;;;; Line Break
  2284. (defun org-element-line-break-parser ()
  2285. "Parse line break at point.
  2286. Return a list whose CAR is `line-break', and CDR a plist with
  2287. `:begin', `:end' and `:post-blank' keywords.
  2288. Assume point is at the beginning of the line break."
  2289. (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
  2290. (defun org-element-line-break-interpreter (line-break contents)
  2291. "Interpret LINE-BREAK object as Org syntax.
  2292. CONTENTS is nil."
  2293. "\\\\")
  2294. (defun org-element-line-break-successor (limit)
  2295. "Search for the next line-break object.
  2296. LIMIT bounds the search.
  2297. Return value is a cons cell whose CAR is `line-break' and CDR is
  2298. beginning position."
  2299. (save-excursion
  2300. (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
  2301. (goto-char (match-beginning 1)))))
  2302. ;; A line break can only happen on a non-empty line.
  2303. (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
  2304. (cons 'line-break beg)))))
  2305. ;;;; Link
  2306. (defun org-element-link-parser ()
  2307. "Parse link at point.
  2308. Return a list whose CAR is `link' and CDR a plist with `:type',
  2309. `:path', `:raw-link', `:begin', `:end', `:contents-begin',
  2310. `:contents-end' and `:post-blank' as keywords.
  2311. Assume point is at the beginning of the link."
  2312. (save-excursion
  2313. (let ((begin (point))
  2314. end contents-begin contents-end link-end post-blank path type
  2315. raw-link link)
  2316. (cond
  2317. ;; Type 1: Text targeted from a radio target.
  2318. ((and org-target-link-regexp (looking-at org-target-link-regexp))
  2319. (setq type "radio"
  2320. link-end (match-end 0)
  2321. path (org-match-string-no-properties 0)))
  2322. ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
  2323. ((looking-at org-bracket-link-regexp)
  2324. (setq contents-begin (match-beginning 3)
  2325. contents-end (match-end 3)
  2326. link-end (match-end 0)
  2327. ;; RAW-LINK is the original link.
  2328. raw-link (org-match-string-no-properties 1)
  2329. link (org-translate-link
  2330. (org-link-expand-abbrev
  2331. (org-link-unescape raw-link))))
  2332. ;; Determine TYPE of link and set PATH accordingly.
  2333. (cond
  2334. ;; File type.
  2335. ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
  2336. (setq type "file" path link))
  2337. ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
  2338. ((string-match org-link-re-with-space3 link)
  2339. (setq type (match-string 1 link) path (match-string 2 link)))
  2340. ;; Id type: PATH is the id.
  2341. ((string-match "^id:\\([-a-f0-9]+\\)" link)
  2342. (setq type "id" path (match-string 1 link)))
  2343. ;; Code-ref type: PATH is the name of the reference.
  2344. ((string-match "^(\\(.*\\))$" link)
  2345. (setq type "coderef" path (match-string 1 link)))
  2346. ;; Custom-id type: PATH is the name of the custom id.
  2347. ((= (aref link 0) ?#)
  2348. (setq type "custom-id" path (substring link 1)))
  2349. ;; Fuzzy type: Internal link either matches a target, an
  2350. ;; headline name or nothing. PATH is the target or
  2351. ;; headline's name.
  2352. (t (setq type "fuzzy" path link))))
  2353. ;; Type 3: Plain link, i.e. http://orgmode.org
  2354. ((looking-at org-plain-link-re)
  2355. (setq raw-link (org-match-string-no-properties 0)
  2356. type (org-match-string-no-properties 1)
  2357. path (org-match-string-no-properties 2)
  2358. link-end (match-end 0)))
  2359. ;; Type 4: Angular link, i.e. <http://orgmode.org>
  2360. ((looking-at org-angle-link-re)
  2361. (setq raw-link (buffer-substring-no-properties
  2362. (match-beginning 1) (match-end 2))
  2363. type (org-match-string-no-properties 1)
  2364. path (org-match-string-no-properties 2)
  2365. link-end (match-end 0))))
  2366. ;; In any case, deduce end point after trailing white space from
  2367. ;; LINK-END variable.
  2368. (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
  2369. end (point))
  2370. (list 'link
  2371. (list :type type
  2372. :path path
  2373. :raw-link (or raw-link path)
  2374. :begin begin
  2375. :end end
  2376. :contents-begin contents-begin
  2377. :contents-end contents-end
  2378. :post-blank post-blank)))))
  2379. (defun org-element-link-interpreter (link contents)
  2380. "Interpret LINK object as Org syntax.
  2381. CONTENTS is the contents of the object, or nil."
  2382. (let ((type (org-element-property :type link))
  2383. (raw-link (org-element-property :raw-link link)))
  2384. (if (string= type "radio") raw-link
  2385. (format "[[%s]%s]"
  2386. raw-link
  2387. (if contents (format "[%s]" contents) "")))))
  2388. (defun org-element-link-successor (limit)
  2389. "Search for the next link object.
  2390. LIMIT bounds the search.
  2391. Return value is a cons cell whose CAR is `link' and CDR is
  2392. beginning position."
  2393. (save-excursion
  2394. (let ((link-regexp
  2395. (if (not org-target-link-regexp) org-any-link-re
  2396. (concat org-any-link-re "\\|" org-target-link-regexp))))
  2397. (when (re-search-forward link-regexp limit t)
  2398. (cons 'link (match-beginning 0))))))
  2399. ;;;; Macro
  2400. (defun org-element-macro-parser ()
  2401. "Parse macro at point.
  2402. Return a list whose CAR is `macro' and CDR a plist with `:key',
  2403. `:args', `:begin', `:end', `:value' and `:post-blank' as
  2404. keywords.
  2405. Assume point is at the macro."
  2406. (save-excursion
  2407. (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
  2408. (let ((begin (point))
  2409. (key (downcase (org-match-string-no-properties 1)))
  2410. (value (org-match-string-no-properties 0))
  2411. (post-blank (progn (goto-char (match-end 0))
  2412. (skip-chars-forward " \t")))
  2413. (end (point))
  2414. (args (let ((args (org-match-string-no-properties 3)) args2)
  2415. (when args
  2416. (setq args (org-split-string args ","))
  2417. (while args
  2418. (while (string-match "\\\\\\'" (car args))
  2419. ;; Repair bad splits.
  2420. (setcar (cdr args) (concat (substring (car args) 0 -1)
  2421. "," (nth 1 args)))
  2422. (pop args))
  2423. (push (pop args) args2))
  2424. (mapcar 'org-trim (nreverse args2))))))
  2425. (list 'macro
  2426. (list :key key
  2427. :value value
  2428. :args args
  2429. :begin begin
  2430. :end end
  2431. :post-blank post-blank)))))
  2432. (defun org-element-macro-interpreter (macro contents)
  2433. "Interpret MACRO object as Org syntax.
  2434. CONTENTS is nil."
  2435. (org-element-property :value macro))
  2436. (defun org-element-macro-successor (limit)
  2437. "Search for the next macro object.
  2438. LIMIT bounds the search.
  2439. Return value is cons cell whose CAR is `macro' and CDR is
  2440. beginning position."
  2441. (save-excursion
  2442. (when (re-search-forward
  2443. "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
  2444. limit t)
  2445. (cons 'macro (match-beginning 0)))))
  2446. ;;;; Radio-target
  2447. (defun org-element-radio-target-parser ()
  2448. "Parse radio target at point.
  2449. Return a list whose CAR is `radio-target' and CDR a plist with
  2450. `:begin', `:end', `:contents-begin', `:contents-end', `:value'
  2451. and `:post-blank' as keywords.
  2452. Assume point is at the radio target."
  2453. (save-excursion
  2454. (looking-at org-radio-target-regexp)
  2455. (let ((begin (point))
  2456. (contents-begin (match-beginning 1))
  2457. (contents-end (match-end 1))
  2458. (value (org-match-string-no-properties 1))
  2459. (post-blank (progn (goto-char (match-end 0))
  2460. (skip-chars-forward " \t")))
  2461. (end (point)))
  2462. (list 'radio-target
  2463. (list :begin begin
  2464. :end end
  2465. :contents-begin contents-begin
  2466. :contents-end contents-end
  2467. :post-blank post-blank
  2468. :value value)))))
  2469. (defun org-element-radio-target-interpreter (target contents)
  2470. "Interpret TARGET object as Org syntax.
  2471. CONTENTS is the contents of the object."
  2472. (concat "<<<" contents ">>>"))
  2473. (defun org-element-radio-target-successor (limit)
  2474. "Search for the next radio-target object.
  2475. LIMIT bounds the search.
  2476. Return value is a cons cell whose CAR is `radio-target' and CDR
  2477. is beginning position."
  2478. (save-excursion
  2479. (when (re-search-forward org-radio-target-regexp limit t)
  2480. (cons 'radio-target (match-beginning 0)))))
  2481. ;;;; Statistics Cookie
  2482. (defun org-element-statistics-cookie-parser ()
  2483. "Parse statistics cookie at point.
  2484. Return a list whose CAR is `statistics-cookie', and CDR a plist
  2485. with `:begin', `:end', `:value' and `:post-blank' keywords.
  2486. Assume point is at the beginning of the statistics-cookie."
  2487. (save-excursion
  2488. (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
  2489. (let* ((begin (point))
  2490. (value (buffer-substring-no-properties
  2491. (match-beginning 0) (match-end 0)))
  2492. (post-blank (progn (goto-char (match-end 0))
  2493. (skip-chars-forward " \t")))
  2494. (end (point)))
  2495. (list 'statistics-cookie
  2496. (list :begin begin
  2497. :end end
  2498. :value value
  2499. :post-blank post-blank)))))
  2500. (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
  2501. "Interpret STATISTICS-COOKIE object as Org syntax.
  2502. CONTENTS is nil."
  2503. (org-element-property :value statistics-cookie))
  2504. (defun org-element-statistics-cookie-successor (limit)
  2505. "Search for the next statistics cookie object.
  2506. LIMIT bounds the search.
  2507. Return value is a cons cell whose CAR is `statistics-cookie' and
  2508. CDR is beginning position."
  2509. (save-excursion
  2510. (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
  2511. (cons 'statistics-cookie (match-beginning 0)))))
  2512. ;;;; Strike-Through
  2513. (defun org-element-strike-through-parser ()
  2514. "Parse strike-through object at point.
  2515. Return a list whose CAR is `strike-through' and CDR is a plist
  2516. with `:begin', `:end', `:contents-begin' and `:contents-end' and
  2517. `:post-blank' keywords.
  2518. Assume point is at the first plus sign marker."
  2519. (save-excursion
  2520. (unless (bolp) (backward-char 1))
  2521. (looking-at org-emph-re)
  2522. (let ((begin (match-beginning 2))
  2523. (contents-begin (match-beginning 4))
  2524. (contents-end (match-end 4))
  2525. (post-blank (progn (goto-char (match-end 2))
  2526. (skip-chars-forward " \t")))
  2527. (end (point)))
  2528. (list 'strike-through
  2529. (list :begin begin
  2530. :end end
  2531. :contents-begin contents-begin
  2532. :contents-end contents-end
  2533. :post-blank post-blank)))))
  2534. (defun org-element-strike-through-interpreter (strike-through contents)
  2535. "Interpret STRIKE-THROUGH object as Org syntax.
  2536. CONTENTS is the contents of the object."
  2537. (format "+%s+" contents))
  2538. ;;;; Subscript
  2539. (defun org-element-subscript-parser ()
  2540. "Parse subscript at point.
  2541. Return a list whose CAR is `subscript' and CDR a plist with
  2542. `:begin', `:end', `:contents-begin', `:contents-end',
  2543. `:use-brackets-p' and `:post-blank' as keywords.
  2544. Assume point is at the underscore."
  2545. (save-excursion
  2546. (unless (bolp) (backward-char))
  2547. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
  2548. t
  2549. (not (looking-at org-match-substring-regexp))))
  2550. (begin (match-beginning 2))
  2551. (contents-begin (or (match-beginning 5)
  2552. (match-beginning 3)))
  2553. (contents-end (or (match-end 5) (match-end 3)))
  2554. (post-blank (progn (goto-char (match-end 0))
  2555. (skip-chars-forward " \t")))
  2556. (end (point)))
  2557. (list 'subscript
  2558. (list :begin begin
  2559. :end end
  2560. :use-brackets-p bracketsp
  2561. :contents-begin contents-begin
  2562. :contents-end contents-end
  2563. :post-blank post-blank)))))
  2564. (defun org-element-subscript-interpreter (subscript contents)
  2565. "Interpret SUBSCRIPT object as Org syntax.
  2566. CONTENTS is the contents of the object."
  2567. (format
  2568. (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
  2569. contents))
  2570. (defun org-element-sub/superscript-successor (limit)
  2571. "Search for the next sub/superscript object.
  2572. LIMIT bounds the search.
  2573. Return value is a cons cell whose CAR is either `subscript' or
  2574. `superscript' and CDR is beginning position."
  2575. (save-excursion
  2576. (when (re-search-forward org-match-substring-regexp limit t)
  2577. (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
  2578. (match-beginning 2)))))
  2579. ;;;; Superscript
  2580. (defun org-element-superscript-parser ()
  2581. "Parse superscript at point.
  2582. Return a list whose CAR is `superscript' and CDR a plist with
  2583. `:begin', `:end', `:contents-begin', `:contents-end',
  2584. `:use-brackets-p' and `:post-blank' as keywords.
  2585. Assume point is at the caret."
  2586. (save-excursion
  2587. (unless (bolp) (backward-char))
  2588. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
  2589. (not (looking-at org-match-substring-regexp))))
  2590. (begin (match-beginning 2))
  2591. (contents-begin (or (match-beginning 5)
  2592. (match-beginning 3)))
  2593. (contents-end (or (match-end 5) (match-end 3)))
  2594. (post-blank (progn (goto-char (match-end 0))
  2595. (skip-chars-forward " \t")))
  2596. (end (point)))
  2597. (list 'superscript
  2598. (list :begin begin
  2599. :end end
  2600. :use-brackets-p bracketsp
  2601. :contents-begin contents-begin
  2602. :contents-end contents-end
  2603. :post-blank post-blank)))))
  2604. (defun org-element-superscript-interpreter (superscript contents)
  2605. "Interpret SUPERSCRIPT object as Org syntax.
  2606. CONTENTS is the contents of the object."
  2607. (format
  2608. (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
  2609. contents))
  2610. ;;;; Table Cell
  2611. (defun org-element-table-cell-parser ()
  2612. "Parse table cell at point.
  2613. Return a list whose CAR is `table-cell' and CDR is a plist
  2614. containing `:begin', `:end', `:contents-begin', `:contents-end'
  2615. and `:post-blank' keywords."
  2616. (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
  2617. (let* ((begin (match-beginning 0))
  2618. (end (match-end 0))
  2619. (contents-begin (match-beginning 1))
  2620. (contents-end (match-end 1)))
  2621. (list 'table-cell
  2622. (list :begin begin
  2623. :end end
  2624. :contents-begin contents-begin
  2625. :contents-end contents-end
  2626. :post-blank 0))))
  2627. (defun org-element-table-cell-interpreter (table-cell contents)
  2628. "Interpret TABLE-CELL element as Org syntax.
  2629. CONTENTS is the contents of the cell, or nil."
  2630. (concat " " contents " |"))
  2631. (defun org-element-table-cell-successor (limit)
  2632. "Search for the next table-cell object.
  2633. LIMIT bounds the search.
  2634. Return value is a cons cell whose CAR is `table-cell' and CDR is
  2635. beginning position."
  2636. (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
  2637. ;;;; Target
  2638. (defun org-element-target-parser ()
  2639. "Parse target at point.
  2640. Return a list whose CAR is `target' and CDR a plist with
  2641. `:begin', `:end', `:value' and `:post-blank' as keywords.
  2642. Assume point is at the target."
  2643. (save-excursion
  2644. (looking-at org-target-regexp)
  2645. (let ((begin (point))
  2646. (value (org-match-string-no-properties 1))
  2647. (post-blank (progn (goto-char (match-end 0))
  2648. (skip-chars-forward " \t")))
  2649. (end (point)))
  2650. (list 'target
  2651. (list :begin begin
  2652. :end end
  2653. :value value
  2654. :post-blank post-blank)))))
  2655. (defun org-element-target-interpreter (target contents)
  2656. "Interpret TARGET object as Org syntax.
  2657. CONTENTS is nil."
  2658. (format "<<%s>>" (org-element-property :value target)))
  2659. (defun org-element-target-successor (limit)
  2660. "Search for the next target object.
  2661. LIMIT bounds the search.
  2662. Return value is a cons cell whose CAR is `target' and CDR is
  2663. beginning position."
  2664. (save-excursion
  2665. (when (re-search-forward org-target-regexp limit t)
  2666. (cons 'target (match-beginning 0)))))
  2667. ;;;; Timestamp
  2668. (defun org-element-timestamp-parser ()
  2669. "Parse time stamp at point.
  2670. Return a list whose CAR is `timestamp', and CDR a plist with
  2671. `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
  2672. Assume point is at the beginning of the timestamp."
  2673. (save-excursion
  2674. (let* ((begin (point))
  2675. (activep (eq (char-after) ?<))
  2676. (main-value
  2677. (progn
  2678. (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
  2679. (match-string-no-properties 1)))
  2680. (range-end (match-string-no-properties 3))
  2681. (type (cond ((match-string 2) 'diary)
  2682. ((and activep range-end) 'active-range)
  2683. (activep 'active)
  2684. (range-end 'inactive-range)
  2685. (t 'inactive)))
  2686. (post-blank (progn (goto-char (match-end 0))
  2687. (skip-chars-forward " \t")))
  2688. (end (point)))
  2689. (list 'timestamp
  2690. (list :type type
  2691. :value main-value
  2692. :range-end range-end
  2693. :begin begin
  2694. :end end
  2695. :post-blank post-blank)))))
  2696. (defun org-element-timestamp-interpreter (timestamp contents)
  2697. "Interpret TIMESTAMP object as Org syntax.
  2698. CONTENTS is nil."
  2699. (let ((type (org-element-property :type timestamp) ))
  2700. (concat
  2701. (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
  2702. (org-element-property :value timestamp))
  2703. (let ((range-end (org-element-property :range-end timestamp)))
  2704. (when range-end
  2705. (concat "--"
  2706. (format (if (eq type 'inactive-range) "[%s]" "<%s>")
  2707. range-end)))))))
  2708. (defun org-element-timestamp-successor (limit)
  2709. "Search for the next timestamp object.
  2710. LIMIT bounds the search.
  2711. Return value is a cons cell whose CAR is `timestamp' and CDR is
  2712. beginning position."
  2713. (save-excursion
  2714. (when (re-search-forward
  2715. (concat org-ts-regexp-both
  2716. "\\|"
  2717. "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  2718. "\\|"
  2719. "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
  2720. limit t)
  2721. (cons 'timestamp (match-beginning 0)))))
  2722. ;;;; Underline
  2723. (defun org-element-underline-parser ()
  2724. "Parse underline object at point.
  2725. Return a list whose CAR is `underline' and CDR is a plist with
  2726. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2727. `:post-blank' keywords.
  2728. Assume point is at the first underscore marker."
  2729. (save-excursion
  2730. (unless (bolp) (backward-char 1))
  2731. (looking-at org-emph-re)
  2732. (let ((begin (match-beginning 2))
  2733. (contents-begin (match-beginning 4))
  2734. (contents-end (match-end 4))
  2735. (post-blank (progn (goto-char (match-end 2))
  2736. (skip-chars-forward " \t")))
  2737. (end (point)))
  2738. (list 'underline
  2739. (list :begin begin
  2740. :end end
  2741. :contents-begin contents-begin
  2742. :contents-end contents-end
  2743. :post-blank post-blank)))))
  2744. (defun org-element-underline-interpreter (underline contents)
  2745. "Interpret UNDERLINE object as Org syntax.
  2746. CONTENTS is the contents of the object."
  2747. (format "_%s_" contents))
  2748. ;;;; Verbatim
  2749. (defun org-element-verbatim-parser ()
  2750. "Parse verbatim object at point.
  2751. Return a list whose CAR is `verbatim' and CDR is a plist with
  2752. `:value', `:begin', `:end' and `:post-blank' keywords.
  2753. Assume point is at the first equal sign marker."
  2754. (save-excursion
  2755. (unless (bolp) (backward-char 1))
  2756. (looking-at org-emph-re)
  2757. (let ((begin (match-beginning 2))
  2758. (value (org-match-string-no-properties 4))
  2759. (post-blank (progn (goto-char (match-end 2))
  2760. (skip-chars-forward " \t")))
  2761. (end (point)))
  2762. (list 'verbatim
  2763. (list :value value
  2764. :begin begin
  2765. :end end
  2766. :post-blank post-blank)))))
  2767. (defun org-element-verbatim-interpreter (verbatim contents)
  2768. "Interpret VERBATIM object as Org syntax.
  2769. CONTENTS is nil."
  2770. (format "=%s=" (org-element-property :value verbatim)))
  2771. ;;; Parsing Element Starting At Point
  2772. ;;
  2773. ;; `org-element--current-element' is the core function of this section.
  2774. ;; It returns the Lisp representation of the element starting at
  2775. ;; point.
  2776. ;;
  2777. ;; `org-element--current-element' makes use of special modes. They
  2778. ;; are activated for fixed element chaining (i.e. `plain-list' >
  2779. ;; `item') or fixed conditional element chaining (i.e. `headline' >
  2780. ;; `section'). Special modes are: `first-section', `section',
  2781. ;; `quote-section', `item' and `table-row'.
  2782. (defun org-element--current-element
  2783. (limit &optional granularity special structure)
  2784. "Parse the element starting at point.
  2785. LIMIT bounds the search.
  2786. Return value is a list like (TYPE PROPS) where TYPE is the type
  2787. of the element and PROPS a plist of properties associated to the
  2788. element.
  2789. Possible types are defined in `org-element-all-elements'.
  2790. Optional argument GRANULARITY determines the depth of the
  2791. recursion. Allowed values are `headline', `greater-element',
  2792. `element', `object' or nil. When it is broader than `object' (or
  2793. nil), secondary values will not be parsed, since they only
  2794. contain objects.
  2795. Optional argument SPECIAL, when non-nil, can be either
  2796. `first-section', `section', `quote-section', `table-row' and
  2797. `item'.
  2798. If STRUCTURE isn't provided but SPECIAL is set to `item', it will
  2799. be computed.
  2800. This function assumes point is always at the beginning of the
  2801. element it has to parse."
  2802. (save-excursion
  2803. ;; If point is at an affiliated keyword, try moving to the
  2804. ;; beginning of the associated element. If none is found, the
  2805. ;; keyword is orphaned and will be treated as plain text.
  2806. (when (looking-at org-element--affiliated-re)
  2807. (let ((opoint (point)))
  2808. (while (looking-at org-element--affiliated-re) (forward-line))
  2809. (when (looking-at "[ \t]*$") (goto-char opoint))))
  2810. (let ((case-fold-search t)
  2811. ;; Determine if parsing depth allows for secondary strings
  2812. ;; parsing. It only applies to elements referenced in
  2813. ;; `org-element-secondary-value-alist'.
  2814. (raw-secondary-p (and granularity (not (eq granularity 'object)))))
  2815. (cond
  2816. ;; Item.
  2817. ((eq special 'item)
  2818. (org-element-item-parser limit structure raw-secondary-p))
  2819. ;; Quote Section.
  2820. ((eq special 'quote-section) (org-element-quote-section-parser limit))
  2821. ;; Table Row.
  2822. ((eq special 'table-row) (org-element-table-row-parser limit))
  2823. ;; Headline.
  2824. ((org-with-limited-levels (org-at-heading-p))
  2825. (org-element-headline-parser limit raw-secondary-p))
  2826. ;; Section (must be checked after headline).
  2827. ((eq special 'section) (org-element-section-parser limit))
  2828. ((eq special 'first-section)
  2829. (org-element-section-parser
  2830. (or (save-excursion (org-with-limited-levels (outline-next-heading)))
  2831. limit)))
  2832. ;; Planning and Clock.
  2833. ((and (looking-at org-planning-or-clock-line-re))
  2834. (if (equal (match-string 1) org-clock-string)
  2835. (org-element-clock-parser limit)
  2836. (org-element-planning-parser limit)))
  2837. ;; Inlinetask.
  2838. ((org-at-heading-p)
  2839. (org-element-inlinetask-parser limit raw-secondary-p))
  2840. ;; LaTeX Environment.
  2841. ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
  2842. (if (save-excursion
  2843. (re-search-forward
  2844. (format "[ \t]*\\\\end{%s}[ \t]*"
  2845. (regexp-quote (match-string 1)))
  2846. nil t))
  2847. (org-element-latex-environment-parser limit)
  2848. (org-element-paragraph-parser limit)))
  2849. ;; Drawer and Property Drawer.
  2850. ((looking-at org-drawer-regexp)
  2851. (let ((name (match-string 1)))
  2852. (cond
  2853. ((not (save-excursion
  2854. (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
  2855. (org-element-paragraph-parser limit))
  2856. ((equal "PROPERTIES" name)
  2857. (org-element-property-drawer-parser limit))
  2858. (t (org-element-drawer-parser limit)))))
  2859. ;; Fixed Width
  2860. ((looking-at "[ \t]*:\\( \\|$\\)")
  2861. (org-element-fixed-width-parser limit))
  2862. ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
  2863. ;; Keywords.
  2864. ((looking-at "[ \t]*#")
  2865. (goto-char (match-end 0))
  2866. (cond ((looking-at "\\+BEGIN_\\(\\S-+\\)")
  2867. (beginning-of-line)
  2868. (let ((parser (assoc (upcase (match-string 1))
  2869. org-element-block-name-alist)))
  2870. (if parser (funcall (cdr parser) limit)
  2871. (org-element-special-block-parser limit))))
  2872. ((looking-at "\\+CALL")
  2873. (beginning-of-line)
  2874. (org-element-babel-call-parser limit))
  2875. ((looking-at "\\+BEGIN:? ")
  2876. (beginning-of-line)
  2877. (org-element-dynamic-block-parser limit))
  2878. ((looking-at "\\+\\S-+:")
  2879. (beginning-of-line)
  2880. (org-element-keyword-parser limit))
  2881. (t
  2882. (beginning-of-line)
  2883. (org-element-comment-parser limit))))
  2884. ;; Footnote Definition.
  2885. ((looking-at org-footnote-definition-re)
  2886. (org-element-footnote-definition-parser limit))
  2887. ;; Horizontal Rule.
  2888. ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
  2889. (org-element-horizontal-rule-parser limit))
  2890. ;; Table.
  2891. ((org-at-table-p t) (org-element-table-parser limit))
  2892. ;; List.
  2893. ((looking-at (org-item-re))
  2894. (org-element-plain-list-parser limit (or structure (org-list-struct))))
  2895. ;; Default element: Paragraph.
  2896. (t (org-element-paragraph-parser limit))))))
  2897. ;; Most elements can have affiliated keywords. When looking for an
  2898. ;; element beginning, we want to move before them, as they belong to
  2899. ;; that element, and, in the meantime, collect information they give
  2900. ;; into appropriate properties. Hence the following function.
  2901. ;;
  2902. ;; Usage of optional arguments may not be obvious at first glance:
  2903. ;;
  2904. ;; - TRANS-LIST is used to polish keywords names that have evolved
  2905. ;; during Org history. In example, even though =result= and
  2906. ;; =results= coexist, we want to have them under the same =result=
  2907. ;; property. It's also true for "srcname" and "name", where the
  2908. ;; latter seems to be preferred nowadays (thus the "name" property).
  2909. ;;
  2910. ;; - CONSED allows to regroup multi-lines keywords under the same
  2911. ;; property, while preserving their own identity. This is mostly
  2912. ;; used for "attr_latex" and al.
  2913. ;;
  2914. ;; - PARSED prepares a keyword value for export. This is useful for
  2915. ;; "caption". Objects restrictions for such keywords are defined in
  2916. ;; `org-element-object-restrictions'.
  2917. ;;
  2918. ;; - DUALS is used to take care of keywords accepting a main and an
  2919. ;; optional secondary values. For example "results" has its
  2920. ;; source's name as the main value, and may have an hash string in
  2921. ;; optional square brackets as the secondary one.
  2922. ;;
  2923. ;; A keyword may belong to more than one category.
  2924. (defun org-element--collect-affiliated-keywords
  2925. (&optional key-re trans-list consed parsed duals)
  2926. "Collect affiliated keywords before point.
  2927. Optional argument KEY-RE is a regexp matching keywords, which
  2928. puts matched keyword in group 1. It defaults to
  2929. `org-element--affiliated-re'.
  2930. TRANS-LIST is an alist where key is the keyword and value the
  2931. property name it should be translated to, without the colons. It
  2932. defaults to `org-element-keyword-translation-alist'.
  2933. CONSED is a list of strings. Any keyword belonging to that list
  2934. will have its value consed. The check is done after keyword
  2935. translation. It defaults to `org-element-multiple-keywords'.
  2936. PARSED is a list of strings. Any keyword member of this list
  2937. will have its value parsed. The check is done after keyword
  2938. translation. If a keyword is a member of both CONSED and PARSED,
  2939. it's value will be a list of parsed strings. It defaults to
  2940. `org-element-parsed-keywords'.
  2941. DUALS is a list of strings. Any keyword member of this list can
  2942. have two parts: one mandatory and one optional. Its value is
  2943. a cons cell whose CAR is the former, and the CDR the latter. If
  2944. a keyword is a member of both PARSED and DUALS, both values will
  2945. be parsed. It defaults to `org-element-dual-keywords'.
  2946. Return a list whose CAR is the position at the first of them and
  2947. CDR a plist of keywords and values."
  2948. (save-excursion
  2949. (let ((case-fold-search t)
  2950. (key-re (or key-re org-element--affiliated-re))
  2951. (trans-list (or trans-list org-element-keyword-translation-alist))
  2952. (consed (or consed org-element-multiple-keywords))
  2953. (parsed (or parsed org-element-parsed-keywords))
  2954. (duals (or duals org-element-dual-keywords))
  2955. ;; RESTRICT is the list of objects allowed in parsed
  2956. ;; keywords value.
  2957. (restrict (org-element-restriction 'keyword))
  2958. output)
  2959. (unless (bobp)
  2960. (while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re)))
  2961. (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
  2962. ;; Apply translation to RAW-KWD. From there, KWD is
  2963. ;; the official keyword.
  2964. (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
  2965. ;; Find main value for any keyword.
  2966. (value
  2967. (save-match-data
  2968. (org-trim
  2969. (buffer-substring-no-properties
  2970. (match-end 0) (point-at-eol)))))
  2971. ;; If KWD is a dual keyword, find its secondary
  2972. ;; value. Maybe parse it.
  2973. (dual-value
  2974. (and (member kwd duals)
  2975. (let ((sec (org-match-string-no-properties 3)))
  2976. (if (or (not sec) (not (member kwd parsed))) sec
  2977. (org-element-parse-secondary-string sec restrict)))))
  2978. ;; Attribute a property name to KWD.
  2979. (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
  2980. ;; Now set final shape for VALUE.
  2981. (when (member kwd parsed)
  2982. (setq value (org-element-parse-secondary-string value restrict)))
  2983. (when (member kwd duals)
  2984. ;; VALUE is mandatory. Set it to nil if there is none.
  2985. (setq value (and value (cons value dual-value))))
  2986. ;; Attributes are always consed.
  2987. (when (or (member kwd consed) (string-match "^ATTR_" kwd))
  2988. (setq value (cons value (plist-get output kwd-sym))))
  2989. ;; Eventually store the new value in OUTPUT.
  2990. (setq output (plist-put output kwd-sym value))))
  2991. (unless (looking-at key-re) (forward-line 1)))
  2992. (list (point) output))))
  2993. ;;; The Org Parser
  2994. ;;
  2995. ;; The two major functions here are `org-element-parse-buffer', which
  2996. ;; parses Org syntax inside the current buffer, taking into account
  2997. ;; region, narrowing, or even visibility if specified, and
  2998. ;; `org-element-parse-secondary-string', which parses objects within
  2999. ;; a given string.
  3000. ;;
  3001. ;; The (almost) almighty `org-element-map' allows to apply a function
  3002. ;; on elements or objects matching some type, and accumulate the
  3003. ;; resulting values. In an export situation, it also skips unneeded
  3004. ;; parts of the parse tree.
  3005. (defun org-element-parse-buffer (&optional granularity visible-only)
  3006. "Recursively parse the buffer and return structure.
  3007. If narrowing is in effect, only parse the visible part of the
  3008. buffer.
  3009. Optional argument GRANULARITY determines the depth of the
  3010. recursion. It can be set to the following symbols:
  3011. `headline' Only parse headlines.
  3012. `greater-element' Don't recurse into greater elements excepted
  3013. headlines and sections. Thus, elements
  3014. parsed are the top-level ones.
  3015. `element' Parse everything but objects and plain text.
  3016. `object' Parse the complete buffer (default).
  3017. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3018. elements.
  3019. Assume buffer is in Org mode."
  3020. (save-excursion
  3021. (goto-char (point-min))
  3022. (org-skip-whitespace)
  3023. (org-element--parse-elements
  3024. (point-at-bol) (point-max)
  3025. ;; Start in `first-section' mode so text before the first
  3026. ;; headline belongs to a section.
  3027. 'first-section nil granularity visible-only (list 'org-data nil))))
  3028. (defun org-element-parse-secondary-string (string restriction &optional parent)
  3029. "Recursively parse objects in STRING and return structure.
  3030. RESTRICTION is a symbol limiting the object types that will be
  3031. looked after.
  3032. Optional argument PARENT, when non-nil, is the element or object
  3033. containing the secondary string. It is used to set correctly
  3034. `:parent' property within the string."
  3035. (with-temp-buffer
  3036. (insert string)
  3037. (let ((secondary (org-element--parse-objects
  3038. (point-min) (point-max) nil restriction)))
  3039. (mapc (lambda (obj) (org-element-put-property obj :parent parent))
  3040. secondary))))
  3041. (defun org-element-map (data types fun &optional info first-match no-recursion)
  3042. "Map a function on selected elements or objects.
  3043. DATA is the parsed tree, as returned by, i.e,
  3044. `org-element-parse-buffer'. TYPES is a symbol or list of symbols
  3045. of elements or objects types. FUN is the function called on the
  3046. matching element or object. It must accept one arguments: the
  3047. element or object itself.
  3048. When optional argument INFO is non-nil, it should be a plist
  3049. holding export options. In that case, parts of the parse tree
  3050. not exportable according to that property list will be skipped.
  3051. When optional argument FIRST-MATCH is non-nil, stop at the first
  3052. match for which FUN doesn't return nil, and return that value.
  3053. Optional argument NO-RECURSION is a symbol or a list of symbols
  3054. representing elements or objects types. `org-element-map' won't
  3055. enter any recursive element or object whose type belongs to that
  3056. list. Though, FUN can still be applied on them.
  3057. Nil values returned from FUN do not appear in the results."
  3058. ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
  3059. (unless (listp types) (setq types (list types)))
  3060. (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
  3061. ;; Recursion depth is determined by --CATEGORY.
  3062. (let* ((--category
  3063. (catch 'found
  3064. (let ((category 'greater-elements))
  3065. (mapc (lambda (type)
  3066. (cond ((or (memq type org-element-all-objects)
  3067. (eq type 'plain-text))
  3068. ;; If one object is found, the function
  3069. ;; has to recurse into every object.
  3070. (throw 'found 'objects))
  3071. ((not (memq type org-element-greater-elements))
  3072. ;; If one regular element is found, the
  3073. ;; function has to recurse, at lest, into
  3074. ;; every element it encounters.
  3075. (and (not (eq category 'elements))
  3076. (setq category 'elements)))))
  3077. types)
  3078. category)))
  3079. --acc
  3080. --walk-tree
  3081. (--walk-tree
  3082. (function
  3083. (lambda (--data)
  3084. ;; Recursively walk DATA. INFO, if non-nil, is a plist
  3085. ;; holding contextual information.
  3086. (let ((--type (org-element-type --data)))
  3087. (cond
  3088. ((not --data))
  3089. ;; Ignored element in an export context.
  3090. ((and info (memq --data (plist-get info :ignore-list))))
  3091. ;; Secondary string: only objects can be found there.
  3092. ((not --type)
  3093. (when (eq --category 'objects) (mapc --walk-tree --data)))
  3094. ;; Unconditionally enter parse trees.
  3095. ((eq --type 'org-data)
  3096. (mapc --walk-tree (org-element-contents --data)))
  3097. (t
  3098. ;; Check if TYPE is matching among TYPES. If so,
  3099. ;; apply FUN to --DATA and accumulate return value
  3100. ;; into --ACC (or exit if FIRST-MATCH is non-nil).
  3101. (when (memq --type types)
  3102. (let ((result (funcall fun --data)))
  3103. (cond ((not result))
  3104. (first-match (throw '--map-first-match result))
  3105. (t (push result --acc)))))
  3106. ;; If --DATA has a secondary string that can contain
  3107. ;; objects with their type among TYPES, look into it.
  3108. (when (eq --category 'objects)
  3109. (let ((sec-prop
  3110. (assq --type org-element-secondary-value-alist)))
  3111. (when sec-prop
  3112. (funcall --walk-tree
  3113. (org-element-property (cdr sec-prop) --data)))))
  3114. ;; Determine if a recursion into --DATA is possible.
  3115. (cond
  3116. ;; --TYPE is explicitly removed from recursion.
  3117. ((memq --type no-recursion))
  3118. ;; --DATA has no contents.
  3119. ((not (org-element-contents --data)))
  3120. ;; Looking for greater elements but --DATA is simply
  3121. ;; an element or an object.
  3122. ((and (eq --category 'greater-elements)
  3123. (not (memq --type org-element-greater-elements))))
  3124. ;; Looking for elements but --DATA is an object.
  3125. ((and (eq --category 'elements)
  3126. (memq --type org-element-all-objects)))
  3127. ;; In any other case, map contents.
  3128. (t (mapc --walk-tree (org-element-contents --data)))))))))))
  3129. (catch '--map-first-match
  3130. (funcall --walk-tree data)
  3131. ;; Return value in a proper order.
  3132. (nreverse --acc))))
  3133. ;; The following functions are internal parts of the parser.
  3134. ;;
  3135. ;; The first one, `org-element--parse-elements' acts at the element's
  3136. ;; level.
  3137. ;;
  3138. ;; The second one, `org-element--parse-objects' applies on all objects
  3139. ;; of a paragraph or a secondary string. It uses
  3140. ;; `org-element--get-next-object-candidates' to optimize the search of
  3141. ;; the next object in the buffer.
  3142. ;;
  3143. ;; More precisely, that function looks for every allowed object type
  3144. ;; first. Then, it discards failed searches, keeps further matches,
  3145. ;; and searches again types matched behind point, for subsequent
  3146. ;; calls. Thus, searching for a given type fails only once, and every
  3147. ;; object is searched only once at top level (but sometimes more for
  3148. ;; nested types).
  3149. (defun org-element--parse-elements
  3150. (beg end special structure granularity visible-only acc)
  3151. "Parse elements between BEG and END positions.
  3152. SPECIAL prioritize some elements over the others. It can be set
  3153. to `first-section', `quote-section', `section' `item' or
  3154. `table-row'.
  3155. When value is `item', STRUCTURE will be used as the current list
  3156. structure.
  3157. GRANULARITY determines the depth of the recursion. See
  3158. `org-element-parse-buffer' for more information.
  3159. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3160. elements.
  3161. Elements are accumulated into ACC."
  3162. (save-excursion
  3163. (goto-char beg)
  3164. ;; When parsing only headlines, skip any text before first one.
  3165. (when (and (eq granularity 'headline) (not (org-at-heading-p)))
  3166. (org-with-limited-levels (outline-next-heading)))
  3167. ;; Main loop start.
  3168. (while (< (point) end)
  3169. ;; Find current element's type and parse it accordingly to
  3170. ;; its category.
  3171. (let* ((element (org-element--current-element
  3172. end granularity special structure))
  3173. (type (org-element-type element))
  3174. (cbeg (org-element-property :contents-begin element)))
  3175. (goto-char (org-element-property :end element))
  3176. ;; Fill ELEMENT contents by side-effect.
  3177. (cond
  3178. ;; If VISIBLE-ONLY is true and element is hidden or if it has
  3179. ;; no contents, don't modify it.
  3180. ((or (and visible-only (org-element-property :hiddenp element))
  3181. (not cbeg)))
  3182. ;; Greater element: parse it between `contents-begin' and
  3183. ;; `contents-end'. Make sure GRANULARITY allows the
  3184. ;; recursion, or ELEMENT is an headline, in which case going
  3185. ;; inside is mandatory, in order to get sub-level headings.
  3186. ((and (memq type org-element-greater-elements)
  3187. (or (memq granularity '(element object nil))
  3188. (and (eq granularity 'greater-element)
  3189. (eq type 'section))
  3190. (eq type 'headline)))
  3191. (org-element--parse-elements
  3192. cbeg (org-element-property :contents-end element)
  3193. ;; Possibly switch to a special mode.
  3194. (case type
  3195. (headline
  3196. (if (org-element-property :quotedp element) 'quote-section
  3197. 'section))
  3198. (plain-list 'item)
  3199. (table 'table-row))
  3200. (org-element-property :structure element)
  3201. granularity visible-only element))
  3202. ;; ELEMENT has contents. Parse objects inside, if
  3203. ;; GRANULARITY allows it.
  3204. ((memq granularity '(object nil))
  3205. (org-element--parse-objects
  3206. cbeg (org-element-property :contents-end element) element
  3207. (org-element-restriction type))))
  3208. (org-element-adopt-element acc element t)))
  3209. ;; Return result.
  3210. acc))
  3211. (defun org-element--parse-objects (beg end acc restriction)
  3212. "Parse objects between BEG and END and return recursive structure.
  3213. Objects are accumulated in ACC.
  3214. RESTRICTION is a list of object types which are allowed in the
  3215. current object."
  3216. (let (candidates)
  3217. (save-excursion
  3218. (goto-char beg)
  3219. (while (and (< (point) end)
  3220. (setq candidates (org-element--get-next-object-candidates
  3221. end restriction candidates)))
  3222. (let ((next-object
  3223. (let ((pos (apply 'min (mapcar 'cdr candidates))))
  3224. (save-excursion
  3225. (goto-char pos)
  3226. (funcall (intern (format "org-element-%s-parser"
  3227. (car (rassq pos candidates)))))))))
  3228. ;; 1. Text before any object. Untabify it.
  3229. (let ((obj-beg (org-element-property :begin next-object)))
  3230. (unless (= (point) obj-beg)
  3231. (setq acc
  3232. (org-element-adopt-element
  3233. acc
  3234. (replace-regexp-in-string
  3235. "\t" (make-string tab-width ? )
  3236. (buffer-substring-no-properties (point) obj-beg)) t))))
  3237. ;; 2. Object...
  3238. (let ((obj-end (org-element-property :end next-object))
  3239. (cont-beg (org-element-property :contents-begin next-object)))
  3240. ;; Fill contents of NEXT-OBJECT by side-effect, if it has
  3241. ;; a recursive type.
  3242. (when (and cont-beg
  3243. (memq (car next-object) org-element-recursive-objects))
  3244. (save-restriction
  3245. (narrow-to-region
  3246. cont-beg
  3247. (org-element-property :contents-end next-object))
  3248. (org-element--parse-objects
  3249. (point-min) (point-max) next-object
  3250. (org-element-restriction next-object))))
  3251. (setq acc (org-element-adopt-element acc next-object t))
  3252. (goto-char obj-end))))
  3253. ;; 3. Text after last object. Untabify it.
  3254. (unless (= (point) end)
  3255. (setq acc
  3256. (org-element-adopt-element
  3257. acc
  3258. (replace-regexp-in-string
  3259. "\t" (make-string tab-width ? )
  3260. (buffer-substring-no-properties (point) end)) t)))
  3261. ;; Result.
  3262. acc)))
  3263. (defun org-element--get-next-object-candidates (limit restriction objects)
  3264. "Return an alist of candidates for the next object.
  3265. LIMIT bounds the search, and RESTRICTION narrows candidates to
  3266. some object types.
  3267. Return value is an alist whose CAR is position and CDR the object
  3268. type, as a symbol.
  3269. OBJECTS is the previous candidates alist."
  3270. (let (next-candidates types-to-search)
  3271. ;; If no previous result, search every object type in RESTRICTION.
  3272. ;; Otherwise, keep potential candidates (old objects located after
  3273. ;; point) and ask to search again those which had matched before.
  3274. (if (not objects) (setq types-to-search restriction)
  3275. (mapc (lambda (obj)
  3276. (if (< (cdr obj) (point)) (push (car obj) types-to-search)
  3277. (push obj next-candidates)))
  3278. objects))
  3279. ;; Call the appropriate successor function for each type to search
  3280. ;; and accumulate matches.
  3281. (mapc
  3282. (lambda (type)
  3283. (let* ((successor-fun
  3284. (intern
  3285. (format "org-element-%s-successor"
  3286. (or (cdr (assq type org-element-object-successor-alist))
  3287. type))))
  3288. (obj (funcall successor-fun limit)))
  3289. (and obj (push obj next-candidates))))
  3290. types-to-search)
  3291. ;; Return alist.
  3292. next-candidates))
  3293. ;;; Towards A Bijective Process
  3294. ;;
  3295. ;; The parse tree obtained with `org-element-parse-buffer' is really
  3296. ;; a snapshot of the corresponding Org buffer. Therefore, it can be
  3297. ;; interpreted and expanded into a string with canonical Org syntax.
  3298. ;; Hence `org-element-interpret-data'.
  3299. ;;
  3300. ;; The function relies internally on
  3301. ;; `org-element--interpret-affiliated-keywords'.
  3302. (defun org-element-interpret-data (data &optional parent)
  3303. "Interpret DATA as Org syntax.
  3304. DATA is a parse tree, an element, an object or a secondary string
  3305. to interpret.
  3306. Optional argument PARENT is used for recursive calls. It contains
  3307. the element or object containing data, or nil.
  3308. Return Org syntax as a string."
  3309. (let* ((type (org-element-type data))
  3310. (results
  3311. (cond
  3312. ;; Secondary string.
  3313. ((not type)
  3314. (mapconcat
  3315. (lambda (obj) (org-element-interpret-data obj parent))
  3316. data ""))
  3317. ;; Full Org document.
  3318. ((eq type 'org-data)
  3319. (mapconcat
  3320. (lambda (obj) (org-element-interpret-data obj parent))
  3321. (org-element-contents data) ""))
  3322. ;; Plain text.
  3323. ((stringp data) data)
  3324. ;; Element/Object without contents.
  3325. ((not (org-element-contents data))
  3326. (funcall (intern (format "org-element-%s-interpreter" type))
  3327. data nil))
  3328. ;; Element/Object with contents.
  3329. (t
  3330. (let* ((greaterp (memq type org-element-greater-elements))
  3331. (objectp (and (not greaterp)
  3332. (memq type org-element-recursive-objects)))
  3333. (contents
  3334. (mapconcat
  3335. (lambda (obj) (org-element-interpret-data obj data))
  3336. (org-element-contents
  3337. (if (or greaterp objectp) data
  3338. ;; Elements directly containing objects must
  3339. ;; have their indentation normalized first.
  3340. (org-element-normalize-contents
  3341. data
  3342. ;; When normalizing first paragraph of an
  3343. ;; item or a footnote-definition, ignore
  3344. ;; first line's indentation.
  3345. (and (eq type 'paragraph)
  3346. (equal data (car (org-element-contents parent)))
  3347. (memq (org-element-type parent)
  3348. '(footnote-definiton item))))))
  3349. "")))
  3350. (funcall (intern (format "org-element-%s-interpreter" type))
  3351. data
  3352. (if greaterp (org-element-normalize-contents contents)
  3353. contents)))))))
  3354. (if (memq type '(org-data plain-text nil)) results
  3355. ;; Build white spaces. If no `:post-blank' property is
  3356. ;; specified, assume its value is 0.
  3357. (let ((post-blank (or (org-element-property :post-blank data) 0)))
  3358. (if (memq type org-element-all-objects)
  3359. (concat results (make-string post-blank 32))
  3360. (concat
  3361. (org-element--interpret-affiliated-keywords data)
  3362. (org-element-normalize-string results)
  3363. (make-string post-blank 10)))))))
  3364. (defun org-element--interpret-affiliated-keywords (element)
  3365. "Return ELEMENT's affiliated keywords as Org syntax.
  3366. If there is no affiliated keyword, return the empty string."
  3367. (let ((keyword-to-org
  3368. (function
  3369. (lambda (key value)
  3370. (let (dual)
  3371. (when (member key org-element-dual-keywords)
  3372. (setq dual (cdr value) value (car value)))
  3373. (concat "#+" key
  3374. (and dual
  3375. (format "[%s]" (org-element-interpret-data dual)))
  3376. ": "
  3377. (if (member key org-element-parsed-keywords)
  3378. (org-element-interpret-data value)
  3379. value)
  3380. "\n"))))))
  3381. (mapconcat
  3382. (lambda (prop)
  3383. (let ((value (org-element-property prop element))
  3384. (keyword (upcase (substring (symbol-name prop) 1))))
  3385. (when value
  3386. (if (or (member keyword org-element-multiple-keywords)
  3387. ;; All attribute keywords can have multiple lines.
  3388. (string-match "^ATTR_" keyword))
  3389. (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
  3390. value
  3391. "")
  3392. (funcall keyword-to-org keyword value)))))
  3393. ;; List all ELEMENT's properties matching an attribute line or an
  3394. ;; affiliated keyword, but ignore translated keywords since they
  3395. ;; cannot belong to the property list.
  3396. (loop for prop in (nth 1 element) by 'cddr
  3397. when (let ((keyword (upcase (substring (symbol-name prop) 1))))
  3398. (or (string-match "^ATTR_" keyword)
  3399. (and
  3400. (member keyword org-element-affiliated-keywords)
  3401. (not (assoc keyword
  3402. org-element-keyword-translation-alist)))))
  3403. collect prop)
  3404. "")))
  3405. ;; Because interpretation of the parse tree must return the same
  3406. ;; number of blank lines between elements and the same number of white
  3407. ;; space after objects, some special care must be given to white
  3408. ;; spaces.
  3409. ;;
  3410. ;; The first function, `org-element-normalize-string', ensures any
  3411. ;; string different from the empty string will end with a single
  3412. ;; newline character.
  3413. ;;
  3414. ;; The second function, `org-element-normalize-contents', removes
  3415. ;; global indentation from the contents of the current element.
  3416. (defun org-element-normalize-string (s)
  3417. "Ensure string S ends with a single newline character.
  3418. If S isn't a string return it unchanged. If S is the empty
  3419. string, return it. Otherwise, return a new string with a single
  3420. newline character at its end."
  3421. (cond
  3422. ((not (stringp s)) s)
  3423. ((string= "" s) "")
  3424. (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
  3425. (replace-match "\n" nil nil s)))))
  3426. (defun org-element-normalize-contents (element &optional ignore-first)
  3427. "Normalize plain text in ELEMENT's contents.
  3428. ELEMENT must only contain plain text and objects.
  3429. If optional argument IGNORE-FIRST is non-nil, ignore first line's
  3430. indentation to compute maximal common indentation.
  3431. Return the normalized element that is element with global
  3432. indentation removed from its contents. The function assumes that
  3433. indentation is not done with TAB characters."
  3434. (let* (ind-list ; for byte-compiler
  3435. collect-inds ; for byte-compiler
  3436. (collect-inds
  3437. (function
  3438. ;; Return list of indentations within BLOB. This is done by
  3439. ;; walking recursively BLOB and updating IND-LIST along the
  3440. ;; way. FIRST-FLAG is non-nil when the first string hasn't
  3441. ;; been seen yet. It is required as this string is the only
  3442. ;; one whose indentation doesn't happen after a newline
  3443. ;; character.
  3444. (lambda (blob first-flag)
  3445. (mapc
  3446. (lambda (object)
  3447. (when (and first-flag (stringp object))
  3448. (setq first-flag nil)
  3449. (string-match "\\`\\( *\\)" object)
  3450. (let ((len (length (match-string 1 object))))
  3451. ;; An indentation of zero means no string will be
  3452. ;; modified. Quit the process.
  3453. (if (zerop len) (throw 'zero (setq ind-list nil))
  3454. (push len ind-list))))
  3455. (cond
  3456. ((stringp object)
  3457. (let ((start 0))
  3458. ;; Avoid matching blank or empty lines.
  3459. (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
  3460. (not (equal (match-string 2 object) " ")))
  3461. (setq start (match-end 0))
  3462. (push (length (match-string 1 object)) ind-list))))
  3463. ((memq (org-element-type object) org-element-recursive-objects)
  3464. (funcall collect-inds object first-flag))))
  3465. (org-element-contents blob))))))
  3466. ;; Collect indentation list in ELEMENT. Possibly remove first
  3467. ;; value if IGNORE-FIRST is non-nil.
  3468. (catch 'zero (funcall collect-inds element (not ignore-first)))
  3469. (if (not ind-list) element
  3470. ;; Build ELEMENT back, replacing each string with the same
  3471. ;; string minus common indentation.
  3472. (let* (build ; For byte compiler.
  3473. (build
  3474. (function
  3475. (lambda (blob mci first-flag)
  3476. ;; Return BLOB with all its strings indentation
  3477. ;; shortened from MCI white spaces. FIRST-FLAG is
  3478. ;; non-nil when the first string hasn't been seen
  3479. ;; yet.
  3480. (setcdr (cdr blob)
  3481. (mapcar
  3482. (lambda (object)
  3483. (when (and first-flag (stringp object))
  3484. (setq first-flag nil)
  3485. (setq object
  3486. (replace-regexp-in-string
  3487. (format "\\` \\{%d\\}" mci) "" object)))
  3488. (cond
  3489. ((stringp object)
  3490. (replace-regexp-in-string
  3491. (format "\n \\{%d\\}" mci) "\n" object))
  3492. ((memq (org-element-type object)
  3493. org-element-recursive-objects)
  3494. (funcall build object mci first-flag))
  3495. (t object)))
  3496. (org-element-contents blob)))
  3497. blob))))
  3498. (funcall build element (apply 'min ind-list) (not ignore-first))))))
  3499. ;;; The Toolbox
  3500. ;;
  3501. ;; The first move is to implement a way to obtain the smallest element
  3502. ;; containing point. This is the job of `org-element-at-point'. It
  3503. ;; basically jumps back to the beginning of section containing point
  3504. ;; and moves, element after element, with
  3505. ;; `org-element--current-element' until the container is found.
  3506. ;;
  3507. ;; At a deeper level, `org-element-context' lists all elements and
  3508. ;; objects containing point.
  3509. ;;
  3510. ;; Note: When using `org-element-at-point', secondary values are never
  3511. ;; parsed since the function focuses on elements, not on objects.
  3512. (defun org-element-at-point (&optional keep-trail)
  3513. "Determine closest element around point.
  3514. Return value is a list like (TYPE PROPS) where TYPE is the type
  3515. of the element and PROPS a plist of properties associated to the
  3516. element.
  3517. Possible types are defined in `org-element-all-elements'.
  3518. Properties depend on element or object type, but always
  3519. include :begin, :end, :parent and :post-blank properties.
  3520. As a special case, if point is at the very beginning of a list or
  3521. sub-list, returned element will be that list instead of the first
  3522. item. In the same way, if point is at the beginning of the first
  3523. row of a table, returned element will be the table instead of the
  3524. first row.
  3525. If optional argument KEEP-TRAIL is non-nil, the function returns
  3526. a list of of elements leading to element at point. The list's
  3527. CAR is always the element at point. Following positions contain
  3528. element's siblings, then parents, siblings of parents, until the
  3529. first element of current section."
  3530. (org-with-wide-buffer
  3531. ;; If at an headline, parse it. It is the sole element that
  3532. ;; doesn't require to know about context. Be sure to disallow
  3533. ;; secondary string parsing, though.
  3534. (if (org-with-limited-levels (org-at-heading-p))
  3535. (progn
  3536. (beginning-of-line)
  3537. (if (not keep-trail) (org-element-headline-parser (point-max) t)
  3538. (list (org-element-headline-parser (point-max) t))))
  3539. ;; Otherwise move at the beginning of the section containing
  3540. ;; point.
  3541. (let ((origin (point))
  3542. (end (save-excursion
  3543. (org-with-limited-levels (outline-next-heading)) (point)))
  3544. element type special-flag trail struct prevs parent)
  3545. (org-with-limited-levels
  3546. (if (org-with-limited-levels (org-before-first-heading-p))
  3547. (goto-char (point-min))
  3548. (org-back-to-heading)
  3549. (forward-line)))
  3550. (org-skip-whitespace)
  3551. (beginning-of-line)
  3552. ;; Parse successively each element, skipping those ending
  3553. ;; before original position.
  3554. (catch 'exit
  3555. (while t
  3556. (setq element
  3557. (org-element--current-element end 'element special-flag struct)
  3558. type (car element))
  3559. (org-element-put-property element :parent parent)
  3560. (when keep-trail (push element trail))
  3561. (cond
  3562. ;; 1. Skip any element ending before point or at point
  3563. ;; because the following element has started. On the
  3564. ;; other hand, if the element ends at point and that
  3565. ;; point is also the end of the buffer, do not skip it.
  3566. ((let ((end (org-element-property :end element)))
  3567. (when (or (< end origin)
  3568. (and (= end origin) (/= (point-max) end)))
  3569. (if (> (point-max) end) (goto-char end)
  3570. (throw 'exit (if keep-trail trail element))))))
  3571. ;; 2. An element containing point is always the element at
  3572. ;; point.
  3573. ((not (memq type org-element-greater-elements))
  3574. (throw 'exit (if keep-trail trail element)))
  3575. ;; 3. At any other greater element type, if point is
  3576. ;; within contents, move into it. Otherwise, return
  3577. ;; that element. As a special case, when ORIGIN is
  3578. ;; contents end and is also the end of the buffer, try
  3579. ;; to move inside the greater element to find the end
  3580. ;; of the innermost element.
  3581. (t
  3582. (let ((cbeg (org-element-property :contents-begin element))
  3583. (cend (org-element-property :contents-end element)))
  3584. (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
  3585. (and (= cend origin) (/= (point-max) origin))
  3586. (and (= cbeg origin) (memq type '(plain-list table))))
  3587. (throw 'exit (if keep-trail trail element))
  3588. (setq parent element)
  3589. (case type
  3590. (plain-list
  3591. (setq special-flag 'item
  3592. struct (org-element-property :structure element)))
  3593. (table (setq special-flag 'table-row))
  3594. (otherwise (setq special-flag nil)))
  3595. (setq end cend)
  3596. (goto-char cbeg)))))))))))
  3597. (defun org-element-context ()
  3598. "Return closest element or object around point.
  3599. Return value is a list like (TYPE PROPS) where TYPE is the type
  3600. of the element or object and PROPS a plist of properties
  3601. associated to it.
  3602. Possible types are defined in `org-element-all-elements' and
  3603. `org-element-all-objects'. Properties depend on element or
  3604. object type, but always include :begin, :end, :parent
  3605. and :post-blank properties."
  3606. (org-with-wide-buffer
  3607. (let* ((origin (point))
  3608. (element (org-element-at-point))
  3609. (type (car element))
  3610. end)
  3611. ;; Check if point is inside an element containing objects or at
  3612. ;; a secondary string. In that case, move to beginning of the
  3613. ;; element or secondary string and set END to the other side.
  3614. (if (not (or (and (eq type 'item)
  3615. (let ((tag (org-element-property :tag element)))
  3616. (and tag
  3617. (progn
  3618. (beginning-of-line)
  3619. (search-forward tag (point-at-eol))
  3620. (goto-char (match-beginning 0))
  3621. (and (>= origin (point))
  3622. (<= origin
  3623. ;; `1+' is required so some
  3624. ;; successors can match
  3625. ;; properly their object.
  3626. (setq end (1+ (match-end 0)))))))))
  3627. (and (memq type '(headline inlinetask))
  3628. (progn (beginning-of-line)
  3629. (skip-chars-forward "* ")
  3630. (setq end (point-at-eol))))
  3631. (and (memq type '(paragraph table-cell verse-block))
  3632. (let ((cbeg (org-element-property
  3633. :contents-begin element))
  3634. (cend (org-element-property
  3635. :contents-end element)))
  3636. (and (>= origin cbeg)
  3637. (<= origin cend)
  3638. (progn (goto-char cbeg) (setq end cend)))))))
  3639. element
  3640. (let ((restriction (org-element-restriction element))
  3641. (parent element)
  3642. candidates)
  3643. (catch 'exit
  3644. (while (setq candidates (org-element--get-next-object-candidates
  3645. end restriction candidates))
  3646. (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
  3647. candidates)))
  3648. ;; If ORIGIN is before next object in element, there's
  3649. ;; no point in looking further.
  3650. (if (> (cdr closest-cand) origin) (throw 'exit element)
  3651. (let* ((object
  3652. (progn (goto-char (cdr closest-cand))
  3653. (funcall (intern (format "org-element-%s-parser"
  3654. (car closest-cand))))))
  3655. (cbeg (org-element-property :contents-begin object))
  3656. (cend (org-element-property :contents-end object)))
  3657. (cond
  3658. ;; ORIGIN is after OBJECT, so skip it.
  3659. ((< (org-element-property :end object) origin)
  3660. (goto-char (org-element-property :end object)))
  3661. ;; ORIGIN is within a non-recursive object or at an
  3662. ;; object boundaries: Return that object.
  3663. ((or (not cbeg) (> cbeg origin) (< cend origin))
  3664. (throw 'exit
  3665. (org-element-put-property object :parent parent)))
  3666. ;; Otherwise, move within current object and restrict
  3667. ;; search to the end of its contents.
  3668. (t (goto-char cbeg)
  3669. (org-element-put-property object :parent parent)
  3670. (setq parent object end cend)))))))
  3671. parent))))))
  3672. ;; Once the local structure around point is well understood, it's easy
  3673. ;; to implement some replacements for `forward-paragraph'
  3674. ;; `backward-paragraph', namely `org-element-forward' and
  3675. ;; `org-element-backward'.
  3676. ;;
  3677. ;; Also, `org-transpose-elements' mimics the behaviour of
  3678. ;; `transpose-words', at the element's level, whereas
  3679. ;; `org-element-drag-forward', `org-element-drag-backward', and
  3680. ;; `org-element-up' generalize, respectively, functions
  3681. ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
  3682. ;;
  3683. ;; `org-element-unindent-buffer' will, as its name almost suggests,
  3684. ;; smartly remove global indentation from buffer, making it possible
  3685. ;; to use Org indent mode on a file created with hard indentation.
  3686. ;;
  3687. ;; `org-element-nested-p' and `org-element-swap-A-B' are used
  3688. ;; internally by some of the previously cited tools.
  3689. (defsubst org-element-nested-p (elem-A elem-B)
  3690. "Non-nil when elements ELEM-A and ELEM-B are nested."
  3691. (let ((beg-A (org-element-property :begin elem-A))
  3692. (beg-B (org-element-property :begin elem-B))
  3693. (end-A (org-element-property :end elem-A))
  3694. (end-B (org-element-property :end elem-B)))
  3695. (or (and (>= beg-A beg-B) (<= end-A end-B))
  3696. (and (>= beg-B beg-A) (<= end-B end-A)))))
  3697. (defun org-element-swap-A-B (elem-A elem-B)
  3698. "Swap elements ELEM-A and ELEM-B.
  3699. Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
  3700. end of ELEM-A."
  3701. (goto-char (org-element-property :begin elem-A))
  3702. ;; There are two special cases when an element doesn't start at bol:
  3703. ;; the first paragraph in an item or in a footnote definition.
  3704. (let ((specialp (not (bolp))))
  3705. ;; Only a paragraph without any affiliated keyword can be moved at
  3706. ;; ELEM-A position in such a situation. Note that the case of
  3707. ;; a footnote definition is impossible: it cannot contain two
  3708. ;; paragraphs in a row because it cannot contain a blank line.
  3709. (if (and specialp
  3710. (or (not (eq (org-element-type elem-B) 'paragraph))
  3711. (/= (org-element-property :begin elem-B)
  3712. (org-element-property :contents-begin elem-B))))
  3713. (error "Cannot swap elements"))
  3714. ;; In a special situation, ELEM-A will have no indentation. We'll
  3715. ;; give it ELEM-B's (which will in, in turn, have no indentation).
  3716. (let* ((ind-B (when specialp
  3717. (goto-char (org-element-property :begin elem-B))
  3718. (org-get-indentation)))
  3719. (beg-A (org-element-property :begin elem-A))
  3720. (end-A (save-excursion
  3721. (goto-char (org-element-property :end elem-A))
  3722. (skip-chars-backward " \r\t\n")
  3723. (point-at-eol)))
  3724. (beg-B (org-element-property :begin elem-B))
  3725. (end-B (save-excursion
  3726. (goto-char (org-element-property :end elem-B))
  3727. (skip-chars-backward " \r\t\n")
  3728. (point-at-eol)))
  3729. ;; Store overlays responsible for visibility status. We
  3730. ;; also need to store their boundaries as they will be
  3731. ;; removed from buffer.
  3732. (overlays
  3733. (cons
  3734. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  3735. (overlays-in beg-A end-A))
  3736. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  3737. (overlays-in beg-B end-B))))
  3738. ;; Get contents.
  3739. (body-A (buffer-substring beg-A end-A))
  3740. (body-B (delete-and-extract-region beg-B end-B)))
  3741. (goto-char beg-B)
  3742. (when specialp
  3743. (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
  3744. (org-indent-to-column ind-B))
  3745. (insert body-A)
  3746. ;; Restore ex ELEM-A overlays.
  3747. (mapc (lambda (ov)
  3748. (move-overlay
  3749. (car ov)
  3750. (+ (nth 1 ov) (- beg-B beg-A))
  3751. (+ (nth 2 ov) (- beg-B beg-A))))
  3752. (car overlays))
  3753. (goto-char beg-A)
  3754. (delete-region beg-A end-A)
  3755. (insert body-B)
  3756. ;; Restore ex ELEM-B overlays.
  3757. (mapc (lambda (ov)
  3758. (move-overlay (car ov)
  3759. (+ (nth 1 ov) (- beg-A beg-B))
  3760. (+ (nth 2 ov) (- beg-A beg-B))))
  3761. (cdr overlays))
  3762. (goto-char (org-element-property :end elem-B)))))
  3763. (defun org-element-forward ()
  3764. "Move forward by one element.
  3765. Move to the next element at the same level, when possible."
  3766. (interactive)
  3767. (cond ((eobp) (error "Cannot move further down"))
  3768. ((org-with-limited-levels (org-at-heading-p))
  3769. (let ((origin (point)))
  3770. (org-forward-same-level 1)
  3771. (unless (org-with-limited-levels (org-at-heading-p))
  3772. (goto-char origin)
  3773. (error "Cannot move further down"))))
  3774. (t
  3775. (let* ((elem (org-element-at-point))
  3776. (end (org-element-property :end elem))
  3777. (parent (org-element-property :parent elem)))
  3778. (if (and parent (= (org-element-property :contents-end parent) end))
  3779. (goto-char (org-element-property :end parent))
  3780. (goto-char end))))))
  3781. (defun org-element-backward ()
  3782. "Move backward by one element.
  3783. Move to the previous element at the same level, when possible."
  3784. (interactive)
  3785. (if (org-with-limited-levels (org-at-heading-p))
  3786. ;; At an headline, move to the previous one, if any, or stay
  3787. ;; here.
  3788. (let ((origin (point)))
  3789. (org-backward-same-level 1)
  3790. (unless (org-with-limited-levels (org-at-heading-p))
  3791. (goto-char origin)
  3792. (error "Cannot move further up")))
  3793. (let* ((trail (org-element-at-point 'keep-trail))
  3794. (elem (car trail))
  3795. (prev-elem (nth 1 trail))
  3796. (beg (org-element-property :begin elem)))
  3797. (cond
  3798. ;; Move to beginning of current element if point isn't there
  3799. ;; already.
  3800. ((/= (point) beg) (goto-char beg))
  3801. ((not prev-elem) (error "Cannot move further up"))
  3802. (t (goto-char (org-element-property :begin prev-elem)))))))
  3803. (defun org-element-up ()
  3804. "Move to upper element."
  3805. (interactive)
  3806. (if (org-with-limited-levels (org-at-heading-p))
  3807. (unless (org-up-heading-safe) (error "No surrounding element"))
  3808. (let* ((elem (org-element-at-point))
  3809. (parent (org-element-property :parent elem)))
  3810. (if parent (goto-char (org-element-property :begin parent))
  3811. (if (org-with-limited-levels (org-before-first-heading-p))
  3812. (error "No surrounding element")
  3813. (org-with-limited-levels (org-back-to-heading)))))))
  3814. (defun org-element-down ()
  3815. "Move to inner element."
  3816. (interactive)
  3817. (let ((element (org-element-at-point)))
  3818. (cond
  3819. ((memq (org-element-type element) '(plain-list table))
  3820. (goto-char (org-element-property :contents-begin element))
  3821. (forward-char))
  3822. ((memq (org-element-type element) org-element-greater-elements)
  3823. ;; If contents are hidden, first disclose them.
  3824. (when (org-element-property :hiddenp element) (org-cycle))
  3825. (goto-char (or (org-element-property :contents-begin element)
  3826. (error "No content for this element"))))
  3827. (t (error "No inner element")))))
  3828. (defun org-element-drag-backward ()
  3829. "Move backward element at point."
  3830. (interactive)
  3831. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  3832. (let* ((trail (org-element-at-point 'keep-trail))
  3833. (elem (car trail))
  3834. (prev-elem (nth 1 trail)))
  3835. ;; Error out if no previous element or previous element is
  3836. ;; a parent of the current one.
  3837. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  3838. (error "Cannot drag element backward")
  3839. (let ((pos (point)))
  3840. (org-element-swap-A-B prev-elem elem)
  3841. (goto-char (+ (org-element-property :begin prev-elem)
  3842. (- pos (org-element-property :begin elem)))))))))
  3843. (defun org-element-drag-forward ()
  3844. "Move forward element at point."
  3845. (interactive)
  3846. (let* ((pos (point))
  3847. (elem (org-element-at-point)))
  3848. (when (= (point-max) (org-element-property :end elem))
  3849. (error "Cannot drag element forward"))
  3850. (goto-char (org-element-property :end elem))
  3851. (let ((next-elem (org-element-at-point)))
  3852. (when (or (org-element-nested-p elem next-elem)
  3853. (and (eq (org-element-type next-elem) 'headline)
  3854. (not (eq (org-element-type elem) 'headline))))
  3855. (goto-char pos)
  3856. (error "Cannot drag element forward"))
  3857. ;; Compute new position of point: it's shifted by NEXT-ELEM
  3858. ;; body's length (without final blanks) and by the length of
  3859. ;; blanks between ELEM and NEXT-ELEM.
  3860. (let ((size-next (- (save-excursion
  3861. (goto-char (org-element-property :end next-elem))
  3862. (skip-chars-backward " \r\t\n")
  3863. (forward-line)
  3864. ;; Small correction if buffer doesn't end
  3865. ;; with a newline character.
  3866. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  3867. (org-element-property :begin next-elem)))
  3868. (size-blank (- (org-element-property :end elem)
  3869. (save-excursion
  3870. (goto-char (org-element-property :end elem))
  3871. (skip-chars-backward " \r\t\n")
  3872. (forward-line)
  3873. (point)))))
  3874. (org-element-swap-A-B elem next-elem)
  3875. (goto-char (+ pos size-next size-blank))))))
  3876. (defun org-element-mark-element ()
  3877. "Put point at beginning of this element, mark at end.
  3878. Interactively, if this command is repeated or (in Transient Mark
  3879. mode) if the mark is active, it marks the next element after the
  3880. ones already marked."
  3881. (interactive)
  3882. (let (deactivate-mark)
  3883. (if (or (and (eq last-command this-command) (mark t))
  3884. (and transient-mark-mode mark-active))
  3885. (set-mark
  3886. (save-excursion
  3887. (goto-char (mark))
  3888. (goto-char (org-element-property :end (org-element-at-point)))))
  3889. (let ((element (org-element-at-point)))
  3890. (end-of-line)
  3891. (push-mark (org-element-property :end element) t t)
  3892. (goto-char (org-element-property :begin element))))))
  3893. (defun org-narrow-to-element ()
  3894. "Narrow buffer to current element."
  3895. (interactive)
  3896. (let ((elem (org-element-at-point)))
  3897. (cond
  3898. ((eq (car elem) 'headline)
  3899. (narrow-to-region
  3900. (org-element-property :begin elem)
  3901. (org-element-property :end elem)))
  3902. ((memq (car elem) org-element-greater-elements)
  3903. (narrow-to-region
  3904. (org-element-property :contents-begin elem)
  3905. (org-element-property :contents-end elem)))
  3906. (t
  3907. (narrow-to-region
  3908. (org-element-property :begin elem)
  3909. (org-element-property :end elem))))))
  3910. (defun org-element-transpose ()
  3911. "Transpose current and previous elements, keeping blank lines between.
  3912. Point is moved after both elements."
  3913. (interactive)
  3914. (org-skip-whitespace)
  3915. (let ((end (org-element-property :end (org-element-at-point))))
  3916. (org-element-drag-backward)
  3917. (goto-char end)))
  3918. (defun org-element-unindent-buffer ()
  3919. "Un-indent the visible part of the buffer.
  3920. Relative indentation (between items, inside blocks, etc.) isn't
  3921. modified."
  3922. (interactive)
  3923. (unless (eq major-mode 'org-mode)
  3924. (error "Cannot un-indent a buffer not in Org mode"))
  3925. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  3926. unindent-tree ; For byte-compiler.
  3927. (unindent-tree
  3928. (function
  3929. (lambda (contents)
  3930. (mapc
  3931. (lambda (element)
  3932. (if (memq (org-element-type element) '(headline section))
  3933. (funcall unindent-tree (org-element-contents element))
  3934. (save-excursion
  3935. (save-restriction
  3936. (narrow-to-region
  3937. (org-element-property :begin element)
  3938. (org-element-property :end element))
  3939. (org-do-remove-indentation)))))
  3940. (reverse contents))))))
  3941. (funcall unindent-tree (org-element-contents parse-tree))))
  3942. (provide 'org-element)
  3943. ;;; org-element.el ends here