org-element.el 154 KB

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