org-element.el 215 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999
  1. ;;; org-element.el --- Parser And Applications for Org syntax
  2. ;; Copyright (C) 2012-2015 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. 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 (`clock', `headline', `inlinetask', `item',
  27. ;; `planning', `property-drawer', `node-property', `section' and
  28. ;; `table-row' types), it can also accept a fixed set of keywords as
  29. ;; attributes. Those are called "affiliated keywords" to distinguish
  30. ;; them from other keywords, which are full-fledged elements. Almost
  31. ;; all affiliated keywords are referenced in
  32. ;; `org-element-affiliated-keywords'; the others are export attributes
  33. ;; and start with "ATTR_" prefix.
  34. ;;
  35. ;; Element containing other elements (and only elements) are called
  36. ;; greater elements. Concerned types are: `center-block', `drawer',
  37. ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
  38. ;; `item', `plain-list', `property-drawer', `quote-block', `section'
  39. ;; and `special-block'.
  40. ;;
  41. ;; Other element types are: `babel-call', `clock', `comment',
  42. ;; `comment-block', `diary-sexp', `example-block', `export-block',
  43. ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
  44. ;; `node-property', `paragraph', `planning', `src-block', `table',
  45. ;; `table-row' and `verse-block'. Among them, `paragraph' and
  46. ;; `verse-block' types can contain Org objects and plain text.
  47. ;;
  48. ;; Objects are related to document's contents. Some of them are
  49. ;; recursive. Associated types are of the following: `bold', `code',
  50. ;; `entity', `export-snippet', `footnote-reference',
  51. ;; `inline-babel-call', `inline-src-block', `italic',
  52. ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
  53. ;; `statistics-cookie', `strike-through', `subscript', `superscript',
  54. ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
  55. ;;
  56. ;; Some elements also have special properties whose value can hold
  57. ;; objects themselves (e.g. an item tag or a headline name). Such
  58. ;; values are called "secondary strings". Any object belongs to
  59. ;; either an element or a secondary string.
  60. ;;
  61. ;; Notwithstanding affiliated keywords, each greater element, element
  62. ;; and object has a fixed set of properties attached to it. Among
  63. ;; them, four are shared by all types: `:begin' and `:end', which
  64. ;; refer to the beginning and ending buffer positions of the
  65. ;; considered element or object, `:post-blank', which holds the number
  66. ;; of blank lines, or white spaces, at its end and `:parent' which
  67. ;; refers to the element or object containing it. Greater elements,
  68. ;; elements and objects containing objects will also have
  69. ;; `:contents-begin' and `:contents-end' properties to delimit
  70. ;; contents. Eventually, All elements have a `:post-affiliated'
  71. ;; property referring to the buffer position after all affiliated
  72. ;; keywords, if any, or to their beginning position otherwise.
  73. ;;
  74. ;; At the lowest level, a `:parent' property is also attached to any
  75. ;; string, as a text property.
  76. ;;
  77. ;; Lisp-wise, an element or an object can be represented as a list.
  78. ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
  79. ;; TYPE is a symbol describing the Org element or object.
  80. ;; PROPERTIES is the property list attached to it. See docstring of
  81. ;; appropriate parsing function to get an exhaustive
  82. ;; list.
  83. ;; CONTENTS is a list of elements, objects or raw strings contained
  84. ;; in the current element or object, when applicable.
  85. ;;
  86. ;; An Org buffer is a nested list of such elements and objects, whose
  87. ;; type is `org-data' and properties is nil.
  88. ;;
  89. ;; The first part of this file defines Org syntax, while the second
  90. ;; one provide accessors and setters functions.
  91. ;;
  92. ;; The next part implements a parser and an interpreter for each
  93. ;; element and object type in Org syntax.
  94. ;;
  95. ;; The following part creates a fully recursive buffer parser. It
  96. ;; also provides a tool to map a function to elements or objects
  97. ;; matching some criteria in the parse tree. Functions of interest
  98. ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
  99. ;; extent, `org-element-parse-secondary-string'.
  100. ;;
  101. ;; The penultimate part is the cradle of an interpreter for the
  102. ;; obtained parse tree: `org-element-interpret-data'.
  103. ;;
  104. ;; The library ends by furnishing `org-element-at-point' function, and
  105. ;; a way to give information about document structure around point
  106. ;; with `org-element-context'. A cache mechanism is also provided for
  107. ;; these functions.
  108. ;;; Code:
  109. (eval-when-compile (require 'cl))
  110. (require 'org)
  111. (require 'avl-tree)
  112. ;;; Definitions And Rules
  113. ;;
  114. ;; Define elements, greater elements and specify recursive objects,
  115. ;; along with the affiliated keywords recognized. Also set up
  116. ;; restrictions on recursive objects combinations.
  117. ;;
  118. ;; `org-element-update-syntax' builds proper syntax regexps according
  119. ;; to current setup.
  120. (defvar org-element-paragraph-separate nil
  121. "Regexp to separate paragraphs in an Org buffer.
  122. In the case of lines starting with \"#\" and \":\", this regexp
  123. is not sufficient to know if point is at a paragraph ending. See
  124. `org-element-paragraph-parser' for more information.")
  125. (defvar org-element--object-regexp nil
  126. "Regexp possibly matching the beginning of an object.
  127. This regexp allows false positives. Dedicated parser (e.g.,
  128. `org-export-bold-parser') will take care of further filtering.
  129. Radio links are not matched by this regexp, as they are treated
  130. specially in `org-element--object-lex'.")
  131. (defun org-element--set-regexps ()
  132. "Build variable syntax regexps."
  133. (setq org-element-paragraph-separate
  134. (concat "^\\(?:"
  135. ;; Headlines, inlinetasks.
  136. org-outline-regexp "\\|"
  137. ;; Footnote definitions.
  138. "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
  139. ;; Diary sexps.
  140. "%%(" "\\|"
  141. "[ \t]*\\(?:"
  142. ;; Empty lines.
  143. "$" "\\|"
  144. ;; Tables (any type).
  145. "\\(?:|\\|\\+-[-+]\\)" "\\|"
  146. ;; Blocks (any type), Babel calls and keywords. This
  147. ;; is only an indication and need some thorough check.
  148. "#\\(?:[+ ]\\|$\\)" "\\|"
  149. ;; Drawers (any type) and fixed-width areas. This is
  150. ;; also only an indication.
  151. ":" "\\|"
  152. ;; Horizontal rules.
  153. "-\\{5,\\}[ \t]*$" "\\|"
  154. ;; LaTeX environments.
  155. "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
  156. ;; Clock lines.
  157. (regexp-quote org-clock-string) "\\|"
  158. ;; Lists.
  159. (let ((term (case org-plain-list-ordered-item-terminator
  160. (?\) ")") (?. "\\.") (otherwise "[.)]")))
  161. (alpha (and org-list-allow-alphabetical "\\|[A-Za-z]")))
  162. (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
  163. "\\(?:[ \t]\\|$\\)"))
  164. "\\)\\)")
  165. org-element--object-regexp
  166. (mapconcat #'identity
  167. (let ((link-types (regexp-opt org-link-types)))
  168. (list
  169. ;; Sub/superscript.
  170. "\\(?:[_^][-{(*+.,[:alnum:]]\\)"
  171. ;; Bold, code, italic, strike-through, underline
  172. ;; and verbatim.
  173. (concat "[*~=+_/]"
  174. (format "[^%s]"
  175. (nth 2 org-emphasis-regexp-components)))
  176. ;; Plain links.
  177. (concat "\\<" link-types ":")
  178. ;; Objects starting with "[": regular link,
  179. ;; footnote reference, statistics cookie,
  180. ;; timestamp (inactive).
  181. "\\[\\(?:fn:\\|\\(?:[0-9]\\|\\(?:%\\|/[0-9]*\\)\\]\\)\\|\\[\\)"
  182. ;; Objects starting with "@": export snippets.
  183. "@@"
  184. ;; Objects starting with "{": macro.
  185. "{{{"
  186. ;; Objects starting with "<" : timestamp
  187. ;; (active, diary), target, radio target and
  188. ;; angular links.
  189. (concat "<\\(?:%%\\|<\\|[0-9]\\|" link-types "\\)")
  190. ;; Objects starting with "$": latex fragment.
  191. "\\$"
  192. ;; Objects starting with "\": line break,
  193. ;; entity, latex fragment.
  194. "\\\\\\(?:[a-zA-Z[(]\\|\\\\[ \t]*$\\|_ +\\)"
  195. ;; Objects starting with raw text: inline Babel
  196. ;; source block, inline Babel call.
  197. "\\(?:call\\|src\\)_"))
  198. "\\|")))
  199. (org-element--set-regexps)
  200. ;;;###autoload
  201. (defun org-element-update-syntax ()
  202. "Update parser internals."
  203. (interactive)
  204. (org-element--set-regexps)
  205. (org-element-cache-reset 'all))
  206. (defconst org-element-all-elements
  207. '(babel-call center-block clock comment comment-block diary-sexp drawer
  208. dynamic-block example-block export-block fixed-width
  209. footnote-definition headline horizontal-rule inlinetask item
  210. keyword latex-environment node-property paragraph plain-list
  211. planning property-drawer quote-block section
  212. special-block src-block table table-row verse-block)
  213. "Complete list of element types.")
  214. (defconst org-element-greater-elements
  215. '(center-block drawer dynamic-block footnote-definition headline inlinetask
  216. item plain-list property-drawer quote-block section
  217. special-block table)
  218. "List of recursive element types aka Greater Elements.")
  219. (defconst org-element-all-objects
  220. '(bold code entity export-snippet footnote-reference inline-babel-call
  221. inline-src-block italic line-break latex-fragment link macro
  222. radio-target statistics-cookie strike-through subscript superscript
  223. table-cell target timestamp underline verbatim)
  224. "Complete list of object types.")
  225. (defconst org-element-recursive-objects
  226. '(bold footnote-reference italic link subscript radio-target strike-through
  227. superscript table-cell underline)
  228. "List of recursive object types.")
  229. (defconst org-element-object-containers
  230. (append org-element-recursive-objects '(paragraph table-row verse-block))
  231. "List of object or element types that can directly contain objects.")
  232. (defvar org-element-block-name-alist
  233. '(("CENTER" . org-element-center-block-parser)
  234. ("COMMENT" . org-element-comment-block-parser)
  235. ("EXAMPLE" . org-element-example-block-parser)
  236. ("QUOTE" . org-element-quote-block-parser)
  237. ("SRC" . org-element-src-block-parser)
  238. ("VERSE" . org-element-verse-block-parser))
  239. "Alist between block names and the associated parsing function.
  240. Names must be uppercase. Any block whose name has no association
  241. is parsed with `org-element-special-block-parser'.")
  242. (defconst org-element-affiliated-keywords
  243. '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
  244. "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
  245. "List of affiliated keywords as strings.
  246. By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
  247. are affiliated keywords and need not to be in this list.")
  248. (defconst org-element-keyword-translation-alist
  249. '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
  250. ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
  251. ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
  252. "Alist of usual translations for keywords.
  253. The key is the old name and the value the new one. The property
  254. holding their value will be named after the translated name.")
  255. (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
  256. "List of affiliated keywords that can occur more than once in an element.
  257. Their value will be consed into a list of strings, which will be
  258. returned as the value of the property.
  259. This list is checked after translations have been applied. See
  260. `org-element-keyword-translation-alist'.
  261. By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
  262. allow multiple occurrences and need not to be in this list.")
  263. (defconst org-element-parsed-keywords '("CAPTION")
  264. "List of affiliated keywords whose value can be parsed.
  265. Their value will be stored as a secondary string: a list of
  266. strings and objects.
  267. This list is checked after translations have been applied. See
  268. `org-element-keyword-translation-alist'.")
  269. (defconst org-element--parsed-properties-alist
  270. (mapcar (lambda (k) (cons k (intern (concat ":" (downcase k)))))
  271. org-element-parsed-keywords)
  272. "Alist of parsed keywords and associated properties.
  273. This is generated from `org-element-parsed-keywords', which
  274. see.")
  275. (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
  276. "List of affiliated keywords which can have a secondary value.
  277. In Org syntax, they can be written with optional square brackets
  278. before the colons. For example, RESULTS keyword can be
  279. associated to a hash value with the following:
  280. #+RESULTS[hash-string]: some-source
  281. This list is checked after translations have been applied. See
  282. `org-element-keyword-translation-alist'.")
  283. (defconst org-element--affiliated-re
  284. (format "[ \t]*#\\+\\(?:%s\\):\\(?: \\|$\\)"
  285. (concat
  286. ;; Dual affiliated keywords.
  287. (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
  288. (regexp-opt org-element-dual-keywords))
  289. "\\|"
  290. ;; Regular affiliated keywords.
  291. (format "\\(?1:%s\\)"
  292. (regexp-opt
  293. (org-remove-if
  294. #'(lambda (keyword)
  295. (member keyword org-element-dual-keywords))
  296. org-element-affiliated-keywords)))
  297. "\\|"
  298. ;; Export attributes.
  299. "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
  300. "Regexp matching any affiliated keyword.
  301. Keyword name is put in match group 1. Moreover, if keyword
  302. belongs to `org-element-dual-keywords', put the dual value in
  303. match group 2.
  304. Don't modify it, set `org-element-affiliated-keywords' instead.")
  305. (defconst org-element-object-restrictions
  306. (let* ((standard-set (remq 'table-cell org-element-all-objects))
  307. (standard-set-no-line-break (remq 'line-break standard-set)))
  308. `((bold ,@standard-set)
  309. (footnote-reference ,@standard-set)
  310. (headline ,@standard-set-no-line-break)
  311. (inlinetask ,@standard-set-no-line-break)
  312. (italic ,@standard-set)
  313. (item ,@standard-set-no-line-break)
  314. (keyword ,@(remq 'footnote-reference standard-set))
  315. ;; Ignore all links excepted plain links in a link description.
  316. ;; Also ignore radio-targets and line breaks.
  317. (link bold code entity export-snippet inline-babel-call inline-src-block
  318. italic latex-fragment macro plain-link statistics-cookie
  319. strike-through subscript superscript underline verbatim)
  320. (paragraph ,@standard-set)
  321. ;; Remove any variable object from radio target as it would
  322. ;; prevent it from being properly recognized.
  323. (radio-target bold code entity italic latex-fragment strike-through
  324. subscript superscript underline superscript)
  325. (strike-through ,@standard-set)
  326. (subscript ,@standard-set)
  327. (superscript ,@standard-set)
  328. ;; Ignore inline babel call and inline src block as formulas are
  329. ;; possible. Also ignore line breaks and statistics cookies.
  330. (table-cell bold code entity export-snippet footnote-reference italic
  331. latex-fragment link macro radio-target strike-through
  332. subscript superscript target timestamp underline verbatim)
  333. (table-row table-cell)
  334. (underline ,@standard-set)
  335. (verse-block ,@standard-set)))
  336. "Alist of objects restrictions.
  337. key is an element or object type containing objects and value is
  338. a list of types that can be contained within an element or object
  339. of such type.
  340. For example, in a `radio-target' object, one can only find
  341. entities, latex-fragments, subscript, superscript and text
  342. markup.
  343. This alist also applies to secondary string. For example, an
  344. `headline' type element doesn't directly contain objects, but
  345. still has an entry since one of its properties (`:title') does.")
  346. (defconst org-element-secondary-value-alist
  347. '((headline :title)
  348. (inlinetask :title)
  349. (item :tag))
  350. "Alist between element types and locations of secondary values.")
  351. (defconst org-element--pair-square-table
  352. (let ((table (make-syntax-table)))
  353. (modify-syntax-entry ?\[ "(]" table)
  354. (modify-syntax-entry ?\] ")[" table)
  355. (dolist (char '(?\{ ?\} ?\( ?\) ?\< ?\>) table)
  356. (modify-syntax-entry char " " table)))
  357. "Table used internally to pair only square brackets.
  358. Other brackets are treated as spaces.")
  359. ;;; Accessors and Setters
  360. ;;
  361. ;; Provide four accessors: `org-element-type', `org-element-property'
  362. ;; `org-element-contents' and `org-element-restriction'.
  363. ;;
  364. ;; Setter functions allow to modify elements by side effect. There is
  365. ;; `org-element-put-property', `org-element-set-contents'. These
  366. ;; low-level functions are useful to build a parse tree.
  367. ;;
  368. ;; `org-element-adopt-element', `org-element-set-element',
  369. ;; `org-element-extract-element' and `org-element-insert-before' are
  370. ;; high-level functions useful to modify a parse tree.
  371. ;;
  372. ;; `org-element-secondary-p' is a predicate used to know if a given
  373. ;; object belongs to a secondary string. `org-element-copy' returns
  374. ;; an element or object, stripping its parent property in the process.
  375. (defsubst org-element-type (element)
  376. "Return type of ELEMENT.
  377. The function returns the type of the element or object provided.
  378. It can also return the following special value:
  379. `plain-text' for a string
  380. `org-data' for a complete document
  381. nil in any other case."
  382. (cond
  383. ((not (consp element)) (and (stringp element) 'plain-text))
  384. ((symbolp (car element)) (car element))))
  385. (defsubst org-element-property (property element)
  386. "Extract the value from the PROPERTY of an ELEMENT."
  387. (if (stringp element) (get-text-property 0 property element)
  388. (plist-get (nth 1 element) property)))
  389. (defsubst org-element-contents (element)
  390. "Extract contents from an ELEMENT."
  391. (cond ((not (consp element)) nil)
  392. ((symbolp (car element)) (nthcdr 2 element))
  393. (t element)))
  394. (defsubst org-element-restriction (element)
  395. "Return restriction associated to ELEMENT.
  396. ELEMENT can be an element, an object or a symbol representing an
  397. element or object type."
  398. (cdr (assq (if (symbolp element) element (org-element-type element))
  399. org-element-object-restrictions)))
  400. (defsubst org-element-put-property (element property value)
  401. "In ELEMENT set PROPERTY to VALUE.
  402. Return modified element."
  403. (if (stringp element) (org-add-props element nil property value)
  404. (setcar (cdr element) (plist-put (nth 1 element) property value))
  405. element))
  406. (defsubst org-element-set-contents (element &rest contents)
  407. "Set ELEMENT contents to CONTENTS."
  408. (cond ((not element) (list contents))
  409. ((not (symbolp (car element))) contents)
  410. ((cdr element) (setcdr (cdr element) contents))
  411. (t (nconc element contents))))
  412. (defun org-element-secondary-p (object)
  413. "Non-nil when OBJECT directly belongs to a secondary string.
  414. Return value is the property name, as a keyword, or nil."
  415. (let* ((parent (org-element-property :parent object))
  416. (properties (cdr (assq (org-element-type parent)
  417. org-element-secondary-value-alist))))
  418. (catch 'exit
  419. (dolist (p properties)
  420. (and (memq object (org-element-property p parent))
  421. (throw 'exit p))))))
  422. (defsubst org-element-adopt-elements (parent &rest children)
  423. "Append elements to the contents of another element.
  424. PARENT is an element or object. CHILDREN can be elements,
  425. objects, or a strings.
  426. The function takes care of setting `:parent' property for CHILD.
  427. Return parent element."
  428. ;; Link every child to PARENT. If PARENT is nil, it is a secondary
  429. ;; string: parent is the list itself.
  430. (mapc (lambda (child)
  431. (org-element-put-property child :parent (or parent children)))
  432. children)
  433. ;; Add CHILDREN at the end of PARENT contents.
  434. (when parent
  435. (apply 'org-element-set-contents
  436. parent
  437. (nconc (org-element-contents parent) children)))
  438. ;; Return modified PARENT element.
  439. (or parent children))
  440. (defun org-element-extract-element (element)
  441. "Extract ELEMENT from parse tree.
  442. Remove element from the parse tree by side-effect, and return it
  443. with its `:parent' property stripped out."
  444. (let ((parent (org-element-property :parent element))
  445. (secondary (org-element-secondary-p element)))
  446. (if secondary
  447. (org-element-put-property
  448. parent secondary
  449. (delq element (org-element-property secondary parent)))
  450. (apply #'org-element-set-contents
  451. parent
  452. (delq element (org-element-contents parent))))
  453. ;; Return ELEMENT with its :parent removed.
  454. (org-element-put-property element :parent nil)))
  455. (defun org-element-insert-before (element location)
  456. "Insert ELEMENT before LOCATION in parse tree.
  457. LOCATION is an element, object or string within the parse tree.
  458. Parse tree is modified by side effect."
  459. (let* ((parent (org-element-property :parent location))
  460. (property (org-element-secondary-p location))
  461. (siblings (if property (org-element-property property parent)
  462. (org-element-contents parent)))
  463. ;; Special case: LOCATION is the first element of an
  464. ;; independent secondary string (e.g. :title property). Add
  465. ;; ELEMENT in-place.
  466. (specialp (and (not property)
  467. (eq siblings parent)
  468. (eq (car parent) location))))
  469. ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
  470. (cond (specialp)
  471. ((or (null siblings) (eq (car siblings) location))
  472. (push element siblings))
  473. ((null location) (nconc siblings (list element)))
  474. (t (let ((previous (cadr (memq location (reverse siblings)))))
  475. (if (not previous)
  476. (error "No location found to insert element")
  477. (let ((next (memq previous siblings)))
  478. (setcdr next (cons element (cdr next))))))))
  479. ;; Store SIBLINGS at appropriate place in parse tree.
  480. (cond
  481. (specialp (setcdr parent (copy-sequence parent)) (setcar parent element))
  482. (property (org-element-put-property parent property siblings))
  483. (t (apply #'org-element-set-contents parent siblings)))
  484. ;; Set appropriate :parent property.
  485. (org-element-put-property element :parent parent)))
  486. (defun org-element-set-element (old new)
  487. "Replace element or object OLD with element or object NEW.
  488. The function takes care of setting `:parent' property for NEW."
  489. ;; Ensure OLD and NEW have the same parent.
  490. (org-element-put-property new :parent (org-element-property :parent old))
  491. (if (or (memq (org-element-type old) '(plain-text nil))
  492. (memq (org-element-type new) '(plain-text nil)))
  493. ;; We cannot replace OLD with NEW since one of them is not an
  494. ;; object or element. We take the long path.
  495. (progn (org-element-insert-before new old)
  496. (org-element-extract-element old))
  497. ;; Since OLD is going to be changed into NEW by side-effect, first
  498. ;; make sure that every element or object within NEW has OLD as
  499. ;; parent.
  500. (dolist (blob (org-element-contents new))
  501. (org-element-put-property blob :parent old))
  502. ;; Transfer contents.
  503. (apply #'org-element-set-contents old (org-element-contents new))
  504. ;; Overwrite OLD's properties with NEW's.
  505. (setcar (cdr old) (nth 1 new))
  506. ;; Transfer type.
  507. (setcar old (car new))))
  508. (defun org-element-copy (datum)
  509. "Return a copy of DATUM.
  510. DATUM is an element, object, string or nil. `:parent' property
  511. is cleared and contents are removed in the process."
  512. (when datum
  513. (let ((type (org-element-type datum)))
  514. (case type
  515. (org-data (list 'org-data nil))
  516. (plain-text (substring-no-properties datum))
  517. ((nil) (copy-sequence datum))
  518. (otherwise
  519. (list type (plist-put (copy-sequence (nth 1 datum)) :parent nil)))))))
  520. ;;; Greater elements
  521. ;;
  522. ;; For each greater element type, we define a parser and an
  523. ;; interpreter.
  524. ;;
  525. ;; A parser returns the element or object as the list described above.
  526. ;; Most of them accepts no argument. Though, exceptions exist. Hence
  527. ;; every element containing a secondary string (see
  528. ;; `org-element-secondary-value-alist') will accept an optional
  529. ;; argument to toggle parsing of these secondary strings. Moreover,
  530. ;; `item' parser requires current list's structure as its first
  531. ;; element.
  532. ;;
  533. ;; An interpreter accepts two arguments: the list representation of
  534. ;; the element or object, and its contents. The latter may be nil,
  535. ;; depending on the element or object considered. It returns the
  536. ;; appropriate Org syntax, as a string.
  537. ;;
  538. ;; Parsing functions must follow the naming convention:
  539. ;; org-element-TYPE-parser, where TYPE is greater element's type, as
  540. ;; defined in `org-element-greater-elements'.
  541. ;;
  542. ;; Similarly, interpreting functions must follow the naming
  543. ;; convention: org-element-TYPE-interpreter.
  544. ;;
  545. ;; With the exception of `headline' and `item' types, greater elements
  546. ;; cannot contain other greater elements of their own type.
  547. ;;
  548. ;; Beside implementing a parser and an interpreter, adding a new
  549. ;; greater element requires to tweak `org-element--current-element'.
  550. ;; Moreover, the newly defined type must be added to both
  551. ;; `org-element-all-elements' and `org-element-greater-elements'.
  552. ;;;; Center Block
  553. (defun org-element-center-block-parser (limit affiliated)
  554. "Parse a center block.
  555. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  556. the buffer position at the beginning of the first affiliated
  557. keyword and CDR is a plist of affiliated keywords along with
  558. their value.
  559. Return a list whose CAR is `center-block' and CDR is a plist
  560. containing `:begin', `:end', `:contents-begin', `:contents-end',
  561. `:post-blank' and `:post-affiliated' keywords.
  562. Assume point is at the beginning of the block."
  563. (let ((case-fold-search t))
  564. (if (not (save-excursion
  565. (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
  566. ;; Incomplete block: parse it as a paragraph.
  567. (org-element-paragraph-parser limit affiliated)
  568. (let ((block-end-line (match-beginning 0)))
  569. (let* ((begin (car affiliated))
  570. (post-affiliated (point))
  571. ;; Empty blocks have no contents.
  572. (contents-begin (progn (forward-line)
  573. (and (< (point) block-end-line)
  574. (point))))
  575. (contents-end (and contents-begin block-end-line))
  576. (pos-before-blank (progn (goto-char block-end-line)
  577. (forward-line)
  578. (point)))
  579. (end (save-excursion
  580. (skip-chars-forward " \r\t\n" limit)
  581. (if (eobp) (point) (line-beginning-position)))))
  582. (list 'center-block
  583. (nconc
  584. (list :begin begin
  585. :end end
  586. :contents-begin contents-begin
  587. :contents-end contents-end
  588. :post-blank (count-lines pos-before-blank end)
  589. :post-affiliated post-affiliated)
  590. (cdr affiliated))))))))
  591. (defun org-element-center-block-interpreter (center-block contents)
  592. "Interpret CENTER-BLOCK element as Org syntax.
  593. CONTENTS is the contents of the element."
  594. (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
  595. ;;;; Drawer
  596. (defun org-element-drawer-parser (limit affiliated)
  597. "Parse a drawer.
  598. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  599. the buffer position at the beginning of the first affiliated
  600. keyword and CDR is a plist of affiliated keywords along with
  601. their value.
  602. Return a list whose CAR is `drawer' and CDR is a plist containing
  603. `:drawer-name', `:begin', `:end', `:contents-begin',
  604. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  605. Assume point is at beginning of drawer."
  606. (let ((case-fold-search t))
  607. (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
  608. ;; Incomplete drawer: parse it as a paragraph.
  609. (org-element-paragraph-parser limit affiliated)
  610. (save-excursion
  611. (let* ((drawer-end-line (match-beginning 0))
  612. (name (progn (looking-at org-drawer-regexp)
  613. (org-match-string-no-properties 1)))
  614. (begin (car affiliated))
  615. (post-affiliated (point))
  616. ;; Empty drawers have no contents.
  617. (contents-begin (progn (forward-line)
  618. (and (< (point) drawer-end-line)
  619. (point))))
  620. (contents-end (and contents-begin drawer-end-line))
  621. (pos-before-blank (progn (goto-char drawer-end-line)
  622. (forward-line)
  623. (point)))
  624. (end (progn (skip-chars-forward " \r\t\n" limit)
  625. (if (eobp) (point) (line-beginning-position)))))
  626. (list 'drawer
  627. (nconc
  628. (list :begin begin
  629. :end end
  630. :drawer-name name
  631. :contents-begin contents-begin
  632. :contents-end contents-end
  633. :post-blank (count-lines pos-before-blank end)
  634. :post-affiliated post-affiliated)
  635. (cdr affiliated))))))))
  636. (defun org-element-drawer-interpreter (drawer contents)
  637. "Interpret DRAWER element as Org syntax.
  638. CONTENTS is the contents of the element."
  639. (format ":%s:\n%s:END:"
  640. (org-element-property :drawer-name drawer)
  641. contents))
  642. ;;;; Dynamic Block
  643. (defun org-element-dynamic-block-parser (limit affiliated)
  644. "Parse a dynamic block.
  645. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  646. the buffer position at the beginning of the first affiliated
  647. keyword and CDR is a plist of affiliated keywords along with
  648. their value.
  649. Return a list whose CAR is `dynamic-block' and CDR is a plist
  650. containing `:block-name', `:begin', `:end', `:contents-begin',
  651. `:contents-end', `:arguments', `:post-blank' and
  652. `:post-affiliated' keywords.
  653. Assume point is at beginning of dynamic block."
  654. (let ((case-fold-search t))
  655. (if (not (save-excursion
  656. (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
  657. ;; Incomplete block: parse it as a paragraph.
  658. (org-element-paragraph-parser limit affiliated)
  659. (let ((block-end-line (match-beginning 0)))
  660. (save-excursion
  661. (let* ((name (progn (looking-at org-dblock-start-re)
  662. (org-match-string-no-properties 1)))
  663. (arguments (org-match-string-no-properties 3))
  664. (begin (car affiliated))
  665. (post-affiliated (point))
  666. ;; Empty blocks have no contents.
  667. (contents-begin (progn (forward-line)
  668. (and (< (point) block-end-line)
  669. (point))))
  670. (contents-end (and contents-begin block-end-line))
  671. (pos-before-blank (progn (goto-char block-end-line)
  672. (forward-line)
  673. (point)))
  674. (end (progn (skip-chars-forward " \r\t\n" limit)
  675. (if (eobp) (point) (line-beginning-position)))))
  676. (list 'dynamic-block
  677. (nconc
  678. (list :begin begin
  679. :end end
  680. :block-name name
  681. :arguments arguments
  682. :contents-begin contents-begin
  683. :contents-end contents-end
  684. :post-blank (count-lines pos-before-blank end)
  685. :post-affiliated post-affiliated)
  686. (cdr affiliated)))))))))
  687. (defun org-element-dynamic-block-interpreter (dynamic-block contents)
  688. "Interpret DYNAMIC-BLOCK element as Org syntax.
  689. CONTENTS is the contents of the element."
  690. (format "#+BEGIN: %s%s\n%s#+END:"
  691. (org-element-property :block-name dynamic-block)
  692. (let ((args (org-element-property :arguments dynamic-block)))
  693. (and args (concat " " args)))
  694. contents))
  695. ;;;; Footnote Definition
  696. (defun org-element-footnote-definition-parser (limit affiliated)
  697. "Parse a footnote definition.
  698. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  699. the buffer position at the beginning of the first affiliated
  700. keyword and CDR is a plist of affiliated keywords along with
  701. their value.
  702. Return a list whose CAR is `footnote-definition' and CDR is
  703. a plist containing `:label', `:begin' `:end', `:contents-begin',
  704. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  705. Assume point is at the beginning of the footnote definition."
  706. (save-excursion
  707. (let* ((label (progn (looking-at org-footnote-definition-re)
  708. (org-match-string-no-properties 1)))
  709. (begin (car affiliated))
  710. (post-affiliated (point))
  711. (ending (save-excursion
  712. (if (progn
  713. (end-of-line)
  714. (re-search-forward
  715. (concat org-outline-regexp-bol "\\|"
  716. org-footnote-definition-re "\\|"
  717. "^\\([ \t]*\n\\)\\{2,\\}") limit 'move))
  718. (match-beginning 0)
  719. (point))))
  720. (contents-begin (progn
  721. (search-forward "]")
  722. (skip-chars-forward " \r\t\n" ending)
  723. (cond ((= (point) ending) nil)
  724. ((= (line-beginning-position) begin) (point))
  725. (t (line-beginning-position)))))
  726. (contents-end (and contents-begin ending))
  727. (end (progn (goto-char ending)
  728. (skip-chars-forward " \r\t\n" limit)
  729. (if (eobp) (point) (line-beginning-position)))))
  730. (list 'footnote-definition
  731. (nconc
  732. (list :label label
  733. :begin begin
  734. :end end
  735. :contents-begin contents-begin
  736. :contents-end contents-end
  737. :post-blank (count-lines ending end)
  738. :post-affiliated post-affiliated)
  739. (cdr affiliated))))))
  740. (defun org-element-footnote-definition-interpreter (footnote-definition contents)
  741. "Interpret FOOTNOTE-DEFINITION element as Org syntax.
  742. CONTENTS is the contents of the footnote-definition."
  743. (concat (format "[%s]" (org-element-property :label footnote-definition))
  744. " "
  745. contents))
  746. ;;;; Headline
  747. (defun org-element--get-node-properties ()
  748. "Return node properties associated to headline at point.
  749. Upcase property names. It avoids confusion between properties
  750. obtained through property drawer and default properties from the
  751. parser (e.g. `:end' and :END:). Return value is a plist."
  752. (save-excursion
  753. (forward-line)
  754. (when (org-looking-at-p org-planning-line-re) (forward-line))
  755. (when (looking-at org-property-drawer-re)
  756. (forward-line)
  757. (let ((end (match-end 0)) properties)
  758. (while (< (line-end-position) end)
  759. (looking-at org-property-re)
  760. (push (org-match-string-no-properties 3) properties)
  761. (push (intern (concat ":" (upcase (match-string 2)))) properties)
  762. (forward-line))
  763. properties))))
  764. (defun org-element--get-time-properties ()
  765. "Return time properties associated to headline at point.
  766. Return value is a plist."
  767. (save-excursion
  768. (when (progn (forward-line) (looking-at org-planning-line-re))
  769. (let ((end (line-end-position)) plist)
  770. (while (re-search-forward org-keyword-time-not-clock-regexp end t)
  771. (goto-char (match-end 1))
  772. (skip-chars-forward " \t")
  773. (let ((keyword (match-string 1))
  774. (time (org-element-timestamp-parser)))
  775. (cond ((equal keyword org-scheduled-string)
  776. (setq plist (plist-put plist :scheduled time)))
  777. ((equal keyword org-deadline-string)
  778. (setq plist (plist-put plist :deadline time)))
  779. (t (setq plist (plist-put plist :closed time))))))
  780. plist))))
  781. (defun org-element-headline-parser (limit &optional raw-secondary-p)
  782. "Parse a headline.
  783. Return a list whose CAR is `headline' and CDR is a plist
  784. containing `:raw-value', `:title', `:begin', `:end',
  785. `:pre-blank', `:contents-begin' and `:contents-end', `:level',
  786. `:priority', `:tags', `:todo-keyword',`:todo-type', `:scheduled',
  787. `:deadline', `:closed', `:archivedp', `:commentedp'
  788. `:footnote-section-p', `:post-blank' and `:post-affiliated'
  789. keywords.
  790. The plist also contains any property set in the property drawer,
  791. with its name in upper cases and colons added at the
  792. beginning (e.g., `:CUSTOM_ID').
  793. LIMIT is a buffer position bounding the search.
  794. When RAW-SECONDARY-P is non-nil, headline's title will not be
  795. parsed as a secondary string, but as a plain string instead.
  796. Assume point is at beginning of the headline."
  797. (save-excursion
  798. (let* ((begin (point))
  799. (level (prog1 (org-reduced-level (skip-chars-forward "*"))
  800. (skip-chars-forward " \t")))
  801. (todo (and org-todo-regexp
  802. (let (case-fold-search) (looking-at org-todo-regexp))
  803. (progn (goto-char (match-end 0))
  804. (skip-chars-forward " \t")
  805. (match-string 0))))
  806. (todo-type
  807. (and todo (if (member todo org-done-keywords) 'done 'todo)))
  808. (priority (and (looking-at "\\[#.\\][ \t]*")
  809. (progn (goto-char (match-end 0))
  810. (aref (match-string 0) 2))))
  811. (commentedp
  812. (and (let (case-fold-search) (looking-at org-comment-string))
  813. (goto-char (match-end 0))))
  814. (title-start (point))
  815. (tags (when (re-search-forward
  816. (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
  817. (line-end-position)
  818. 'move)
  819. (goto-char (match-beginning 0))
  820. (org-split-string (match-string 1) ":")))
  821. (title-end (point))
  822. (raw-value (org-trim
  823. (buffer-substring-no-properties title-start title-end)))
  824. (archivedp (member org-archive-tag tags))
  825. (footnote-section-p (and org-footnote-section
  826. (string= org-footnote-section raw-value)))
  827. (standard-props (org-element--get-node-properties))
  828. (time-props (org-element--get-time-properties))
  829. (end (min (save-excursion (org-end-of-subtree t t)) limit))
  830. (pos-after-head (progn (forward-line) (point)))
  831. (contents-begin (save-excursion
  832. (skip-chars-forward " \r\t\n" end)
  833. (and (/= (point) end) (line-beginning-position))))
  834. (contents-end (and contents-begin
  835. (progn (goto-char end)
  836. (skip-chars-backward " \r\t\n")
  837. (forward-line)
  838. (point)))))
  839. (let ((headline
  840. (list 'headline
  841. (nconc
  842. (list :raw-value raw-value
  843. :begin begin
  844. :end end
  845. :pre-blank
  846. (if (not contents-begin) 0
  847. (count-lines pos-after-head contents-begin))
  848. :contents-begin contents-begin
  849. :contents-end contents-end
  850. :level level
  851. :priority priority
  852. :tags tags
  853. :todo-keyword todo
  854. :todo-type todo-type
  855. :post-blank (count-lines
  856. (or contents-end pos-after-head)
  857. end)
  858. :footnote-section-p footnote-section-p
  859. :archivedp archivedp
  860. :commentedp commentedp
  861. :post-affiliated begin)
  862. time-props
  863. standard-props))))
  864. (org-element-put-property
  865. headline :title
  866. (if raw-secondary-p raw-value
  867. (let ((title (org-element--parse-objects
  868. (progn (goto-char title-start)
  869. (skip-chars-forward " \t")
  870. (point))
  871. (progn (goto-char title-end)
  872. (skip-chars-backward " \t")
  873. (point))
  874. nil
  875. (org-element-restriction 'headline))))
  876. (dolist (datum title title)
  877. (org-element-put-property datum :parent headline)))))))))
  878. (defun org-element-headline-interpreter (headline contents)
  879. "Interpret HEADLINE element as Org syntax.
  880. CONTENTS is the contents of the element."
  881. (let* ((level (org-element-property :level headline))
  882. (todo (org-element-property :todo-keyword headline))
  883. (priority (org-element-property :priority headline))
  884. (title (org-element-interpret-data
  885. (org-element-property :title headline)))
  886. (tags (let ((tag-list (org-element-property :tags headline)))
  887. (and tag-list
  888. (format ":%s:" (mapconcat #'identity tag-list ":")))))
  889. (commentedp (org-element-property :commentedp headline))
  890. (pre-blank (or (org-element-property :pre-blank headline) 0))
  891. (heading
  892. (concat (make-string (if org-odd-levels-only (1- (* level 2)) level)
  893. ?*)
  894. (and todo (concat " " todo))
  895. (and commentedp (concat " " org-comment-string))
  896. (and priority (format " [#%c]" priority))
  897. " "
  898. (if (and org-footnote-section
  899. (org-element-property :footnote-section-p headline))
  900. org-footnote-section
  901. title))))
  902. (concat
  903. heading
  904. ;; Align tags.
  905. (when tags
  906. (cond
  907. ((zerop org-tags-column) (format " %s" tags))
  908. ((< org-tags-column 0)
  909. (concat
  910. (make-string
  911. (max (- (+ org-tags-column (length heading) (length tags))) 1)
  912. ?\s)
  913. tags))
  914. (t
  915. (concat
  916. (make-string (max (- org-tags-column (length heading)) 1) ?\s)
  917. tags))))
  918. (make-string (1+ pre-blank) ?\n)
  919. contents)))
  920. ;;;; Inlinetask
  921. (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
  922. "Parse an inline task.
  923. Return a list whose CAR is `inlinetask' and CDR is a plist
  924. containing `:title', `:begin', `:end', `:contents-begin' and
  925. `:contents-end', `:level', `:priority', `:raw-value', `:tags',
  926. `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
  927. `:closed', `:post-blank' and `:post-affiliated' keywords.
  928. The plist also contains any property set in the property drawer,
  929. with its name in upper cases and colons added at the
  930. beginning (e.g., `:CUSTOM_ID').
  931. When optional argument RAW-SECONDARY-P is non-nil, inline-task's
  932. title will not be parsed as a secondary string, but as a plain
  933. string instead.
  934. Assume point is at beginning of the inline task."
  935. (save-excursion
  936. (let* ((begin (point))
  937. (level (prog1 (org-reduced-level (skip-chars-forward "*"))
  938. (skip-chars-forward " \t")))
  939. (todo (and org-todo-regexp
  940. (let (case-fold-search) (looking-at org-todo-regexp))
  941. (progn (goto-char (match-end 0))
  942. (skip-chars-forward " \t")
  943. (match-string 0))))
  944. (todo-type (and todo
  945. (if (member todo org-done-keywords) 'done 'todo)))
  946. (priority (and (looking-at "\\[#.\\][ \t]*")
  947. (progn (goto-char (match-end 0))
  948. (aref (match-string 0) 2))))
  949. (title-start (point))
  950. (tags (when (re-search-forward
  951. (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
  952. (line-end-position)
  953. 'move)
  954. (goto-char (match-beginning 0))
  955. (org-split-string (match-string 1) ":")))
  956. (title-end (point))
  957. (raw-value (org-trim
  958. (buffer-substring-no-properties title-start title-end)))
  959. (task-end (save-excursion
  960. (end-of-line)
  961. (and (re-search-forward org-outline-regexp-bol limit t)
  962. (org-looking-at-p "END[ \t]*$")
  963. (line-beginning-position))))
  964. (standard-props (and task-end (org-element--get-node-properties)))
  965. (time-props (and task-end (org-element--get-time-properties)))
  966. (contents-begin (progn (forward-line)
  967. (and task-end (< (point) task-end) (point))))
  968. (contents-end (and contents-begin task-end))
  969. (before-blank (if (not task-end) (point)
  970. (goto-char task-end)
  971. (forward-line)
  972. (point)))
  973. (end (progn (skip-chars-forward " \r\t\n" limit)
  974. (if (eobp) (point) (line-beginning-position))))
  975. (inlinetask
  976. (list 'inlinetask
  977. (nconc
  978. (list :raw-value raw-value
  979. :begin begin
  980. :end end
  981. :contents-begin contents-begin
  982. :contents-end contents-end
  983. :level level
  984. :priority priority
  985. :tags tags
  986. :todo-keyword todo
  987. :todo-type todo-type
  988. :post-blank (count-lines before-blank end)
  989. :post-affiliated begin)
  990. time-props
  991. standard-props))))
  992. (org-element-put-property
  993. inlinetask :title
  994. (if raw-secondary-p raw-value
  995. (let ((title (org-element--parse-objects
  996. (progn (goto-char title-start)
  997. (skip-chars-forward " \t")
  998. (point))
  999. (progn (goto-char title-end)
  1000. (skip-chars-backward " \t")
  1001. (point))
  1002. nil
  1003. (org-element-restriction 'inlinetask))))
  1004. (dolist (datum title title)
  1005. (org-element-put-property datum :parent inlinetask))))))))
  1006. (defun org-element-inlinetask-interpreter (inlinetask contents)
  1007. "Interpret INLINETASK element as Org syntax.
  1008. CONTENTS is the contents of inlinetask."
  1009. (let* ((level (org-element-property :level inlinetask))
  1010. (todo (org-element-property :todo-keyword inlinetask))
  1011. (priority (org-element-property :priority inlinetask))
  1012. (title (org-element-interpret-data
  1013. (org-element-property :title inlinetask)))
  1014. (tags (let ((tag-list (org-element-property :tags inlinetask)))
  1015. (and tag-list
  1016. (format ":%s:" (mapconcat 'identity tag-list ":")))))
  1017. (task (concat (make-string level ?*)
  1018. (and todo (concat " " todo))
  1019. (and priority (format " [#%c]" priority))
  1020. (and title (concat " " title)))))
  1021. (concat task
  1022. ;; Align tags.
  1023. (when tags
  1024. (cond
  1025. ((zerop org-tags-column) (format " %s" tags))
  1026. ((< org-tags-column 0)
  1027. (concat
  1028. (make-string
  1029. (max (- (+ org-tags-column (length task) (length tags))) 1)
  1030. ? )
  1031. tags))
  1032. (t
  1033. (concat
  1034. (make-string (max (- org-tags-column (length task)) 1) ? )
  1035. tags))))
  1036. ;; Prefer degenerate inlinetasks when there are no
  1037. ;; contents.
  1038. (when contents
  1039. (concat "\n"
  1040. contents
  1041. (make-string level ?*) " END")))))
  1042. ;;;; Item
  1043. (defun org-element-item-parser (limit struct &optional raw-secondary-p)
  1044. "Parse an item.
  1045. STRUCT is the structure of the plain list.
  1046. Return a list whose CAR is `item' and CDR is a plist containing
  1047. `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
  1048. `:checkbox', `:counter', `:tag', `:structure', `:post-blank' and
  1049. `:post-affiliated' keywords.
  1050. When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
  1051. any, will not be parsed as a secondary string, but as a plain
  1052. string instead.
  1053. Assume point is at the beginning of the item."
  1054. (save-excursion
  1055. (beginning-of-line)
  1056. (looking-at org-list-full-item-re)
  1057. (let* ((begin (point))
  1058. (bullet (org-match-string-no-properties 1))
  1059. (checkbox (let ((box (org-match-string-no-properties 3)))
  1060. (cond ((equal "[ ]" box) 'off)
  1061. ((equal "[X]" box) 'on)
  1062. ((equal "[-]" box) 'trans))))
  1063. (counter (let ((c (org-match-string-no-properties 2)))
  1064. (save-match-data
  1065. (cond
  1066. ((not c) nil)
  1067. ((string-match "[A-Za-z]" c)
  1068. (- (string-to-char (upcase (match-string 0 c)))
  1069. 64))
  1070. ((string-match "[0-9]+" c)
  1071. (string-to-number (match-string 0 c)))))))
  1072. (end (progn (goto-char (nth 6 (assq (point) struct)))
  1073. (unless (bolp) (forward-line))
  1074. (point)))
  1075. (contents-begin
  1076. (progn (goto-char
  1077. ;; Ignore tags in un-ordered lists: they are just
  1078. ;; a part of item's body.
  1079. (if (and (match-beginning 4)
  1080. (save-match-data (string-match "[.)]" bullet)))
  1081. (match-beginning 4)
  1082. (match-end 0)))
  1083. (skip-chars-forward " \r\t\n" limit)
  1084. ;; If first line isn't empty, contents really start
  1085. ;; at the text after item's meta-data.
  1086. (if (= (point-at-bol) begin) (point) (point-at-bol))))
  1087. (contents-end (progn (goto-char end)
  1088. (skip-chars-backward " \r\t\n")
  1089. (forward-line)
  1090. (point)))
  1091. (item
  1092. (list 'item
  1093. (list :bullet bullet
  1094. :begin begin
  1095. :end end
  1096. ;; CONTENTS-BEGIN and CONTENTS-END may be
  1097. ;; mixed up in the case of an empty item
  1098. ;; separated from the next by a blank line.
  1099. ;; Thus ensure the former is always the
  1100. ;; smallest.
  1101. :contents-begin (min contents-begin contents-end)
  1102. :contents-end (max contents-begin contents-end)
  1103. :checkbox checkbox
  1104. :counter counter
  1105. :structure struct
  1106. :post-blank (count-lines contents-end end)
  1107. :post-affiliated begin))))
  1108. (org-element-put-property
  1109. item :tag
  1110. (let ((raw (org-list-get-tag begin struct)))
  1111. (when raw
  1112. (if raw-secondary-p raw
  1113. (let ((tag (org-element--parse-objects
  1114. (match-beginning 4) (match-end 4) nil
  1115. (org-element-restriction 'item))))
  1116. (dolist (datum tag tag)
  1117. (org-element-put-property datum :parent item))))))))))
  1118. (defun org-element-item-interpreter (item contents)
  1119. "Interpret ITEM element as Org syntax.
  1120. CONTENTS is the contents of the element."
  1121. (let* ((bullet (let ((bullet (org-element-property :bullet item)))
  1122. (org-list-bullet-string
  1123. (cond ((not (string-match "[0-9a-zA-Z]" bullet)) "- ")
  1124. ((eq org-plain-list-ordered-item-terminator ?\)) "1)")
  1125. (t "1.")))))
  1126. (checkbox (org-element-property :checkbox item))
  1127. (counter (org-element-property :counter item))
  1128. (tag (let ((tag (org-element-property :tag item)))
  1129. (and tag (org-element-interpret-data tag))))
  1130. ;; Compute indentation.
  1131. (ind (make-string (length bullet) 32))
  1132. (item-starts-with-par-p
  1133. (eq (org-element-type (car (org-element-contents item)))
  1134. 'paragraph)))
  1135. ;; Indent contents.
  1136. (concat
  1137. bullet
  1138. (and counter (format "[@%d] " counter))
  1139. (case checkbox
  1140. (on "[X] ")
  1141. (off "[ ] ")
  1142. (trans "[-] "))
  1143. (and tag (format "%s :: " tag))
  1144. (when contents
  1145. (let ((contents (replace-regexp-in-string
  1146. "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
  1147. (if item-starts-with-par-p (org-trim contents)
  1148. (concat "\n" contents)))))))
  1149. ;;;; Plain List
  1150. (defun org-element--list-struct (limit)
  1151. ;; Return structure of list at point. Internal function. See
  1152. ;; `org-list-struct' for details.
  1153. (let ((case-fold-search t)
  1154. (top-ind limit)
  1155. (item-re (org-item-re))
  1156. (inlinetask-re (and (featurep 'org-inlinetask) "^\\*+ "))
  1157. items struct)
  1158. (save-excursion
  1159. (catch 'exit
  1160. (while t
  1161. (cond
  1162. ;; At limit: end all items.
  1163. ((>= (point) limit)
  1164. (throw 'exit
  1165. (let ((end (progn (skip-chars-backward " \r\t\n")
  1166. (forward-line)
  1167. (point))))
  1168. (dolist (item items (sort (nconc items struct)
  1169. 'car-less-than-car))
  1170. (setcar (nthcdr 6 item) end)))))
  1171. ;; At list end: end all items.
  1172. ((looking-at org-list-end-re)
  1173. (throw 'exit (dolist (item items (sort (nconc items struct)
  1174. 'car-less-than-car))
  1175. (setcar (nthcdr 6 item) (point)))))
  1176. ;; At a new item: end previous sibling.
  1177. ((looking-at item-re)
  1178. (let ((ind (save-excursion (skip-chars-forward " \t")
  1179. (current-column))))
  1180. (setq top-ind (min top-ind ind))
  1181. (while (and items (<= ind (nth 1 (car items))))
  1182. (let ((item (pop items)))
  1183. (setcar (nthcdr 6 item) (point))
  1184. (push item struct)))
  1185. (push (progn (looking-at org-list-full-item-re)
  1186. (let ((bullet (match-string-no-properties 1)))
  1187. (list (point)
  1188. ind
  1189. bullet
  1190. (match-string-no-properties 2) ; counter
  1191. (match-string-no-properties 3) ; checkbox
  1192. ;; Description tag.
  1193. (and (save-match-data
  1194. (string-match "[-+*]" bullet))
  1195. (match-string-no-properties 4))
  1196. ;; Ending position, unknown so far.
  1197. nil)))
  1198. items))
  1199. (forward-line 1))
  1200. ;; Skip empty lines.
  1201. ((looking-at "^[ \t]*$") (forward-line))
  1202. ;; Skip inline tasks and blank lines along the way.
  1203. ((and inlinetask-re (looking-at inlinetask-re))
  1204. (forward-line)
  1205. (let ((origin (point)))
  1206. (when (re-search-forward inlinetask-re limit t)
  1207. (if (org-looking-at-p "END[ \t]*$") (forward-line)
  1208. (goto-char origin)))))
  1209. ;; At some text line. Check if it ends any previous item.
  1210. (t
  1211. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  1212. (when (<= ind top-ind)
  1213. (skip-chars-backward " \r\t\n")
  1214. (forward-line))
  1215. (while (<= ind (nth 1 (car items)))
  1216. (let ((item (pop items)))
  1217. (setcar (nthcdr 6 item) (line-beginning-position))
  1218. (push item struct)
  1219. (unless items
  1220. (throw 'exit (sort struct 'car-less-than-car))))))
  1221. ;; Skip blocks (any type) and drawers contents.
  1222. (cond
  1223. ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
  1224. (re-search-forward
  1225. (format "^[ \t]*#\\+END%s[ \t]*$" (match-string 1))
  1226. limit t)))
  1227. ((and (looking-at org-drawer-regexp)
  1228. (re-search-forward "^[ \t]*:END:[ \t]*$" limit t))))
  1229. (forward-line))))))))
  1230. (defun org-element-plain-list-parser (limit affiliated structure)
  1231. "Parse a plain list.
  1232. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1233. the buffer position at the beginning of the first affiliated
  1234. keyword and CDR is a plist of affiliated keywords along with
  1235. their value. STRUCTURE is the structure of the plain list being
  1236. parsed.
  1237. Return a list whose CAR is `plain-list' and CDR is a plist
  1238. containing `:type', `:begin', `:end', `:contents-begin' and
  1239. `:contents-end', `:structure', `:post-blank' and
  1240. `:post-affiliated' keywords.
  1241. Assume point is at the beginning of the list."
  1242. (save-excursion
  1243. (let* ((struct (or structure (org-element--list-struct limit)))
  1244. (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered)
  1245. ((nth 5 (assq (point) struct)) 'descriptive)
  1246. (t 'unordered)))
  1247. (contents-begin (point))
  1248. (begin (car affiliated))
  1249. (contents-end (let* ((item (assq contents-begin struct))
  1250. (ind (nth 1 item))
  1251. (pos (nth 6 item)))
  1252. (while (and (setq item (assq pos struct))
  1253. (= (nth 1 item) ind))
  1254. (setq pos (nth 6 item)))
  1255. pos))
  1256. (end (progn (goto-char contents-end)
  1257. (skip-chars-forward " \r\t\n" limit)
  1258. (if (= (point) limit) limit (line-beginning-position)))))
  1259. ;; Return value.
  1260. (list 'plain-list
  1261. (nconc
  1262. (list :type type
  1263. :begin begin
  1264. :end end
  1265. :contents-begin contents-begin
  1266. :contents-end contents-end
  1267. :structure struct
  1268. :post-blank (count-lines contents-end end)
  1269. :post-affiliated contents-begin)
  1270. (cdr affiliated))))))
  1271. (defun org-element-plain-list-interpreter (plain-list contents)
  1272. "Interpret PLAIN-LIST element as Org syntax.
  1273. CONTENTS is the contents of the element."
  1274. (with-temp-buffer
  1275. (insert contents)
  1276. (goto-char (point-min))
  1277. (org-list-repair)
  1278. (buffer-string)))
  1279. ;;;; Property Drawer
  1280. (defun org-element-property-drawer-parser (limit)
  1281. "Parse a property drawer.
  1282. LIMIT bounds the search.
  1283. Return a list whose car is `property-drawer' and cdr is a plist
  1284. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1285. `:post-blank' and `:post-affiliated' keywords.
  1286. Assume point is at the beginning of the property drawer."
  1287. (save-excursion
  1288. (let ((case-fold-search t)
  1289. (begin (point))
  1290. (contents-begin (line-beginning-position 2)))
  1291. (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)
  1292. (let ((contents-end (and (> (match-beginning 0) contents-begin)
  1293. (match-beginning 0)))
  1294. (before-blank (progn (forward-line) (point)))
  1295. (end (progn (skip-chars-forward " \r\t\n" limit)
  1296. (if (eobp) (point) (line-beginning-position)))))
  1297. (list 'property-drawer
  1298. (list :begin begin
  1299. :end end
  1300. :contents-begin (and contents-end contents-begin)
  1301. :contents-end contents-end
  1302. :post-blank (count-lines before-blank end)
  1303. :post-affiliated begin))))))
  1304. (defun org-element-property-drawer-interpreter (property-drawer contents)
  1305. "Interpret PROPERTY-DRAWER element as Org syntax.
  1306. CONTENTS is the properties within the drawer."
  1307. (format ":PROPERTIES:\n%s:END:" contents))
  1308. ;;;; Quote Block
  1309. (defun org-element-quote-block-parser (limit affiliated)
  1310. "Parse a quote block.
  1311. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1312. the buffer position at the beginning of the first affiliated
  1313. keyword and CDR is a plist of affiliated keywords along with
  1314. their value.
  1315. Return a list whose CAR is `quote-block' and CDR is a plist
  1316. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1317. `:post-blank' and `:post-affiliated' keywords.
  1318. Assume point is at the beginning of the block."
  1319. (let ((case-fold-search t))
  1320. (if (not (save-excursion
  1321. (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
  1322. ;; Incomplete block: parse it as a paragraph.
  1323. (org-element-paragraph-parser limit affiliated)
  1324. (let ((block-end-line (match-beginning 0)))
  1325. (save-excursion
  1326. (let* ((begin (car affiliated))
  1327. (post-affiliated (point))
  1328. ;; Empty blocks have no contents.
  1329. (contents-begin (progn (forward-line)
  1330. (and (< (point) block-end-line)
  1331. (point))))
  1332. (contents-end (and contents-begin block-end-line))
  1333. (pos-before-blank (progn (goto-char block-end-line)
  1334. (forward-line)
  1335. (point)))
  1336. (end (progn (skip-chars-forward " \r\t\n" limit)
  1337. (if (eobp) (point) (line-beginning-position)))))
  1338. (list 'quote-block
  1339. (nconc
  1340. (list :begin begin
  1341. :end end
  1342. :contents-begin contents-begin
  1343. :contents-end contents-end
  1344. :post-blank (count-lines pos-before-blank end)
  1345. :post-affiliated post-affiliated)
  1346. (cdr affiliated)))))))))
  1347. (defun org-element-quote-block-interpreter (quote-block contents)
  1348. "Interpret QUOTE-BLOCK element as Org syntax.
  1349. CONTENTS is the contents of the element."
  1350. (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
  1351. ;;;; Section
  1352. (defun org-element-section-parser (limit)
  1353. "Parse a section.
  1354. LIMIT bounds the search.
  1355. Return a list whose CAR is `section' and CDR is a plist
  1356. containing `:begin', `:end', `:contents-begin', `contents-end',
  1357. `:post-blank' and `:post-affiliated' keywords."
  1358. (save-excursion
  1359. ;; Beginning of section is the beginning of the first non-blank
  1360. ;; line after previous headline.
  1361. (let ((begin (point))
  1362. (end (progn (org-with-limited-levels (outline-next-heading))
  1363. (point)))
  1364. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1365. (forward-line)
  1366. (point))))
  1367. (list 'section
  1368. (list :begin begin
  1369. :end end
  1370. :contents-begin begin
  1371. :contents-end pos-before-blank
  1372. :post-blank (count-lines pos-before-blank end)
  1373. :post-affiliated begin)))))
  1374. (defun org-element-section-interpreter (section contents)
  1375. "Interpret SECTION element as Org syntax.
  1376. CONTENTS is the contents of the element."
  1377. contents)
  1378. ;;;; Special Block
  1379. (defun org-element-special-block-parser (limit affiliated)
  1380. "Parse a special block.
  1381. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1382. the buffer position at the beginning of the first affiliated
  1383. keyword and CDR is a plist of affiliated keywords along with
  1384. their value.
  1385. Return a list whose CAR is `special-block' and CDR is a plist
  1386. containing `:type', `:begin', `:end', `:contents-begin',
  1387. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  1388. Assume point is at the beginning of the block."
  1389. (let* ((case-fold-search t)
  1390. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1391. (match-string-no-properties 1))))
  1392. (if (not (save-excursion
  1393. (re-search-forward
  1394. (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
  1395. limit t)))
  1396. ;; Incomplete block: parse it as a paragraph.
  1397. (org-element-paragraph-parser limit affiliated)
  1398. (let ((block-end-line (match-beginning 0)))
  1399. (save-excursion
  1400. (let* ((begin (car affiliated))
  1401. (post-affiliated (point))
  1402. ;; Empty blocks have no contents.
  1403. (contents-begin (progn (forward-line)
  1404. (and (< (point) block-end-line)
  1405. (point))))
  1406. (contents-end (and contents-begin block-end-line))
  1407. (pos-before-blank (progn (goto-char block-end-line)
  1408. (forward-line)
  1409. (point)))
  1410. (end (progn (skip-chars-forward " \r\t\n" limit)
  1411. (if (eobp) (point) (line-beginning-position)))))
  1412. (list 'special-block
  1413. (nconc
  1414. (list :type type
  1415. :begin begin
  1416. :end end
  1417. :contents-begin contents-begin
  1418. :contents-end contents-end
  1419. :post-blank (count-lines pos-before-blank end)
  1420. :post-affiliated post-affiliated)
  1421. (cdr affiliated)))))))))
  1422. (defun org-element-special-block-interpreter (special-block contents)
  1423. "Interpret SPECIAL-BLOCK element as Org syntax.
  1424. CONTENTS is the contents of the element."
  1425. (let ((block-type (org-element-property :type special-block)))
  1426. (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
  1427. ;;; Elements
  1428. ;;
  1429. ;; For each element, a parser and an interpreter are also defined.
  1430. ;; Both follow the same naming convention used for greater elements.
  1431. ;;
  1432. ;; Also, as for greater elements, adding a new element type is done
  1433. ;; through the following steps: implement a parser and an interpreter,
  1434. ;; tweak `org-element--current-element' so that it recognizes the new
  1435. ;; type and add that new type to `org-element-all-elements'.
  1436. ;;
  1437. ;; As a special case, when the newly defined type is a block type,
  1438. ;; `org-element-block-name-alist' has to be modified accordingly.
  1439. ;;;; Babel Call
  1440. (defun org-element-babel-call-parser (limit affiliated)
  1441. "Parse a babel call.
  1442. LIMIT bounds the search. AFFILIATED is a list of which car is
  1443. the buffer position at the beginning of the first affiliated
  1444. keyword and cdr is a plist of affiliated keywords along with
  1445. their value.
  1446. Return a list whose car is `babel-call' and cdr is a plist
  1447. containing `:call', `:inside-header', `:arguments',
  1448. `:end-header', `:begin', `:end', `:value', `:post-blank' and
  1449. `:post-affiliated' as keywords."
  1450. (save-excursion
  1451. (let* ((begin (car affiliated))
  1452. (post-affiliated (point))
  1453. (value (progn (search-forward ":" nil t)
  1454. (org-trim
  1455. (buffer-substring-no-properties
  1456. (point) (line-end-position)))))
  1457. (pos-before-blank (progn (forward-line) (point)))
  1458. (end (progn (skip-chars-forward " \r\t\n" limit)
  1459. (if (eobp) (point) (line-beginning-position))))
  1460. (valid-value
  1461. (string-match
  1462. "\\([^()\n]+?\\)\\(?:\\[\\(.*?\\)\\]\\)?(\\(.*?\\))[ \t]*\\(.*\\)"
  1463. value)))
  1464. (list 'babel-call
  1465. (nconc
  1466. (list :call (and valid-value (match-string 1 value))
  1467. :inside-header (and valid-value
  1468. (org-string-nw-p (match-string 2 value)))
  1469. :arguments (and valid-value
  1470. (org-string-nw-p (match-string 3 value)))
  1471. :end-header (and valid-value
  1472. (org-string-nw-p (match-string 4 value)))
  1473. :begin begin
  1474. :end end
  1475. :value value
  1476. :post-blank (count-lines pos-before-blank end)
  1477. :post-affiliated post-affiliated)
  1478. (cdr affiliated))))))
  1479. (defun org-element-babel-call-interpreter (babel-call contents)
  1480. "Interpret BABEL-CALL element as Org syntax.
  1481. CONTENTS is nil."
  1482. (concat "#+CALL: "
  1483. (org-element-property :call babel-call)
  1484. (let ((h (org-element-property :inside-header babel-call)))
  1485. (and h (format "[%s]" h)))
  1486. (concat "(" (org-element-property :arguments babel-call) ")")
  1487. (let ((h (org-element-property :end-header babel-call)))
  1488. (and h (concat " " h)))))
  1489. ;;;; Clock
  1490. (defun org-element-clock-parser (limit)
  1491. "Parse a clock.
  1492. LIMIT bounds the search.
  1493. Return a list whose CAR is `clock' and CDR is a plist containing
  1494. `:status', `:value', `:time', `:begin', `:end', `:post-blank' and
  1495. `:post-affiliated' as keywords."
  1496. (save-excursion
  1497. (let* ((case-fold-search nil)
  1498. (begin (point))
  1499. (value (progn (search-forward org-clock-string (line-end-position) t)
  1500. (skip-chars-forward " \t")
  1501. (org-element-timestamp-parser)))
  1502. (duration (and (search-forward " => " (line-end-position) t)
  1503. (progn (skip-chars-forward " \t")
  1504. (looking-at "\\(\\S-+\\)[ \t]*$"))
  1505. (org-match-string-no-properties 1)))
  1506. (status (if duration 'closed 'running))
  1507. (post-blank (let ((before-blank (progn (forward-line) (point))))
  1508. (skip-chars-forward " \r\t\n" limit)
  1509. (skip-chars-backward " \t")
  1510. (unless (bolp) (end-of-line))
  1511. (count-lines before-blank (point))))
  1512. (end (point)))
  1513. (list 'clock
  1514. (list :status status
  1515. :value value
  1516. :duration duration
  1517. :begin begin
  1518. :end end
  1519. :post-blank post-blank
  1520. :post-affiliated begin)))))
  1521. (defun org-element-clock-interpreter (clock contents)
  1522. "Interpret CLOCK element as Org syntax.
  1523. CONTENTS is nil."
  1524. (concat org-clock-string " "
  1525. (org-element-timestamp-interpreter
  1526. (org-element-property :value clock) nil)
  1527. (let ((duration (org-element-property :duration clock)))
  1528. (and duration
  1529. (concat " => "
  1530. (apply 'format
  1531. "%2s:%02s"
  1532. (org-split-string duration ":")))))))
  1533. ;;;; Comment
  1534. (defun org-element-comment-parser (limit affiliated)
  1535. "Parse a comment.
  1536. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1537. the buffer position at the beginning of the first affiliated
  1538. keyword and CDR is a plist of affiliated keywords along with
  1539. their value.
  1540. Return a list whose CAR is `comment' and CDR is a plist
  1541. containing `:begin', `:end', `:value', `:post-blank',
  1542. `:post-affiliated' keywords.
  1543. Assume point is at comment beginning."
  1544. (save-excursion
  1545. (let* ((begin (car affiliated))
  1546. (post-affiliated (point))
  1547. (value (prog2 (looking-at "[ \t]*# ?")
  1548. (buffer-substring-no-properties
  1549. (match-end 0) (line-end-position))
  1550. (forward-line)))
  1551. (com-end
  1552. ;; Get comments ending.
  1553. (progn
  1554. (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
  1555. ;; Accumulate lines without leading hash and first
  1556. ;; whitespace.
  1557. (setq value
  1558. (concat value
  1559. "\n"
  1560. (buffer-substring-no-properties
  1561. (match-end 0) (line-end-position))))
  1562. (forward-line))
  1563. (point)))
  1564. (end (progn (goto-char com-end)
  1565. (skip-chars-forward " \r\t\n" limit)
  1566. (if (eobp) (point) (line-beginning-position)))))
  1567. (list 'comment
  1568. (nconc
  1569. (list :begin begin
  1570. :end end
  1571. :value value
  1572. :post-blank (count-lines com-end end)
  1573. :post-affiliated post-affiliated)
  1574. (cdr affiliated))))))
  1575. (defun org-element-comment-interpreter (comment contents)
  1576. "Interpret COMMENT element as Org syntax.
  1577. CONTENTS is nil."
  1578. (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
  1579. ;;;; Comment Block
  1580. (defun org-element-comment-block-parser (limit affiliated)
  1581. "Parse an export block.
  1582. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1583. the buffer position at the beginning of the first affiliated
  1584. keyword and CDR is a plist of affiliated keywords along with
  1585. their value.
  1586. Return a list whose CAR is `comment-block' and CDR is a plist
  1587. containing `:begin', `:end', `:value', `:post-blank' and
  1588. `:post-affiliated' keywords.
  1589. Assume point is at comment block beginning."
  1590. (let ((case-fold-search t))
  1591. (if (not (save-excursion
  1592. (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
  1593. ;; Incomplete block: parse it as a paragraph.
  1594. (org-element-paragraph-parser limit affiliated)
  1595. (let ((contents-end (match-beginning 0)))
  1596. (save-excursion
  1597. (let* ((begin (car affiliated))
  1598. (post-affiliated (point))
  1599. (contents-begin (progn (forward-line) (point)))
  1600. (pos-before-blank (progn (goto-char contents-end)
  1601. (forward-line)
  1602. (point)))
  1603. (end (progn (skip-chars-forward " \r\t\n" limit)
  1604. (if (eobp) (point) (line-beginning-position))))
  1605. (value (buffer-substring-no-properties
  1606. contents-begin contents-end)))
  1607. (list 'comment-block
  1608. (nconc
  1609. (list :begin begin
  1610. :end end
  1611. :value value
  1612. :post-blank (count-lines pos-before-blank end)
  1613. :post-affiliated post-affiliated)
  1614. (cdr affiliated)))))))))
  1615. (defun org-element-comment-block-interpreter (comment-block contents)
  1616. "Interpret COMMENT-BLOCK element as Org syntax.
  1617. CONTENTS is nil."
  1618. (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
  1619. (org-element-normalize-string
  1620. (org-remove-indentation
  1621. (org-element-property :value comment-block)))))
  1622. ;;;; Diary Sexp
  1623. (defun org-element-diary-sexp-parser (limit affiliated)
  1624. "Parse a diary sexp.
  1625. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1626. the buffer position at the beginning of the first affiliated
  1627. keyword and CDR is a plist of affiliated keywords along with
  1628. their value.
  1629. Return a list whose CAR is `diary-sexp' and CDR is a plist
  1630. containing `:begin', `:end', `:value', `:post-blank' and
  1631. `:post-affiliated' keywords."
  1632. (save-excursion
  1633. (let ((begin (car affiliated))
  1634. (post-affiliated (point))
  1635. (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
  1636. (org-match-string-no-properties 1)))
  1637. (pos-before-blank (progn (forward-line) (point)))
  1638. (end (progn (skip-chars-forward " \r\t\n" limit)
  1639. (if (eobp) (point) (line-beginning-position)))))
  1640. (list 'diary-sexp
  1641. (nconc
  1642. (list :value value
  1643. :begin begin
  1644. :end end
  1645. :post-blank (count-lines pos-before-blank end)
  1646. :post-affiliated post-affiliated)
  1647. (cdr affiliated))))))
  1648. (defun org-element-diary-sexp-interpreter (diary-sexp contents)
  1649. "Interpret DIARY-SEXP as Org syntax.
  1650. CONTENTS is nil."
  1651. (org-element-property :value diary-sexp))
  1652. ;;;; Example Block
  1653. (defun org-element-example-block-parser (limit affiliated)
  1654. "Parse an example block.
  1655. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1656. the buffer position at the beginning of the first affiliated
  1657. keyword and CDR is a plist of affiliated keywords along with
  1658. their value.
  1659. Return a list whose CAR is `example-block' and CDR is a plist
  1660. containing `:begin', `:end', `:number-lines', `:preserve-indent',
  1661. `:retain-labels', `:use-labels', `:label-fmt', `:switches',
  1662. `:value', `:post-blank' and `:post-affiliated' keywords."
  1663. (let ((case-fold-search t))
  1664. (if (not (save-excursion
  1665. (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
  1666. ;; Incomplete block: parse it as a paragraph.
  1667. (org-element-paragraph-parser limit affiliated)
  1668. (let ((contents-end (match-beginning 0)))
  1669. (save-excursion
  1670. (let* ((switches
  1671. (progn
  1672. (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
  1673. (org-match-string-no-properties 1)))
  1674. ;; Switches analysis
  1675. (number-lines
  1676. (cond ((not switches) nil)
  1677. ((string-match "-n\\>" switches) 'new)
  1678. ((string-match "+n\\>" switches) 'continued)))
  1679. (preserve-indent
  1680. (and switches (string-match "-i\\>" switches)))
  1681. ;; Should labels be retained in (or stripped from) example
  1682. ;; blocks?
  1683. (retain-labels
  1684. (or (not switches)
  1685. (not (string-match "-r\\>" switches))
  1686. (and number-lines (string-match "-k\\>" switches))))
  1687. ;; What should code-references use - labels or
  1688. ;; line-numbers?
  1689. (use-labels
  1690. (or (not switches)
  1691. (and retain-labels
  1692. (not (string-match "-k\\>" switches)))))
  1693. (label-fmt
  1694. (and switches
  1695. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1696. (match-string 1 switches)))
  1697. ;; Standard block parsing.
  1698. (begin (car affiliated))
  1699. (post-affiliated (point))
  1700. (block-ind (progn (skip-chars-forward " \t") (current-column)))
  1701. (contents-begin (progn (forward-line) (point)))
  1702. (value (org-element-remove-indentation
  1703. (org-unescape-code-in-string
  1704. (buffer-substring-no-properties
  1705. contents-begin contents-end))
  1706. block-ind))
  1707. (pos-before-blank (progn (goto-char contents-end)
  1708. (forward-line)
  1709. (point)))
  1710. (end (progn (skip-chars-forward " \r\t\n" limit)
  1711. (if (eobp) (point) (line-beginning-position)))))
  1712. (list 'example-block
  1713. (nconc
  1714. (list :begin begin
  1715. :end end
  1716. :value value
  1717. :switches switches
  1718. :number-lines number-lines
  1719. :preserve-indent preserve-indent
  1720. :retain-labels retain-labels
  1721. :use-labels use-labels
  1722. :label-fmt label-fmt
  1723. :post-blank (count-lines pos-before-blank end)
  1724. :post-affiliated post-affiliated)
  1725. (cdr affiliated)))))))))
  1726. (defun org-element-example-block-interpreter (example-block contents)
  1727. "Interpret EXAMPLE-BLOCK element as Org syntax.
  1728. CONTENTS is nil."
  1729. (let ((switches (org-element-property :switches example-block))
  1730. (value (org-element-property :value example-block)))
  1731. (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
  1732. (org-element-normalize-string
  1733. (org-escape-code-in-string
  1734. (if (or org-src-preserve-indentation
  1735. (org-element-property :preserve-indent example-block))
  1736. value
  1737. (org-element-remove-indentation value))))
  1738. "#+END_EXAMPLE")))
  1739. ;;;; Export Block
  1740. (defun org-element-export-block-parser (limit affiliated)
  1741. "Parse an export block.
  1742. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1743. the buffer position at the beginning of the first affiliated
  1744. keyword and CDR is a plist of affiliated keywords along with
  1745. their value.
  1746. Return a list whose CAR is `export-block' and CDR is a plist
  1747. containing `:begin', `:end', `:type', `:value', `:post-blank' and
  1748. `:post-affiliated' keywords.
  1749. Assume point is at export-block beginning."
  1750. (let* ((case-fold-search t)
  1751. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1752. (upcase (org-match-string-no-properties 1)))))
  1753. (if (not (save-excursion
  1754. (re-search-forward
  1755. (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
  1756. ;; Incomplete block: parse it as a paragraph.
  1757. (org-element-paragraph-parser limit affiliated)
  1758. (let ((contents-end (match-beginning 0)))
  1759. (save-excursion
  1760. (let* ((begin (car affiliated))
  1761. (post-affiliated (point))
  1762. (contents-begin (progn (forward-line) (point)))
  1763. (pos-before-blank (progn (goto-char contents-end)
  1764. (forward-line)
  1765. (point)))
  1766. (end (progn (skip-chars-forward " \r\t\n" limit)
  1767. (if (eobp) (point) (line-beginning-position))))
  1768. (value (buffer-substring-no-properties contents-begin
  1769. contents-end)))
  1770. (list 'export-block
  1771. (nconc
  1772. (list :begin begin
  1773. :end end
  1774. :type type
  1775. :value value
  1776. :post-blank (count-lines pos-before-blank end)
  1777. :post-affiliated post-affiliated)
  1778. (cdr affiliated)))))))))
  1779. (defun org-element-export-block-interpreter (export-block contents)
  1780. "Interpret EXPORT-BLOCK element as Org syntax.
  1781. CONTENTS is nil."
  1782. (let ((type (org-element-property :type export-block)))
  1783. (concat (format "#+BEGIN_%s\n" type)
  1784. (org-element-property :value export-block)
  1785. (format "#+END_%s" type))))
  1786. ;;;; Fixed-width
  1787. (defun org-element-fixed-width-parser (limit affiliated)
  1788. "Parse a fixed-width section.
  1789. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1790. the buffer position at the beginning of the first affiliated
  1791. keyword and CDR is a plist of affiliated keywords along with
  1792. their value.
  1793. Return a list whose CAR is `fixed-width' and CDR is a plist
  1794. containing `:begin', `:end', `:value', `:post-blank' and
  1795. `:post-affiliated' keywords.
  1796. Assume point is at the beginning of the fixed-width area."
  1797. (save-excursion
  1798. (let* ((begin (car affiliated))
  1799. (post-affiliated (point))
  1800. value
  1801. (end-area
  1802. (progn
  1803. (while (and (< (point) limit)
  1804. (looking-at "[ \t]*:\\( \\|$\\)"))
  1805. ;; Accumulate text without starting colons.
  1806. (setq value
  1807. (concat value
  1808. (buffer-substring-no-properties
  1809. (match-end 0) (point-at-eol))
  1810. "\n"))
  1811. (forward-line))
  1812. (point)))
  1813. (end (progn (skip-chars-forward " \r\t\n" limit)
  1814. (if (eobp) (point) (line-beginning-position)))))
  1815. (list 'fixed-width
  1816. (nconc
  1817. (list :begin begin
  1818. :end end
  1819. :value value
  1820. :post-blank (count-lines end-area end)
  1821. :post-affiliated post-affiliated)
  1822. (cdr affiliated))))))
  1823. (defun org-element-fixed-width-interpreter (fixed-width contents)
  1824. "Interpret FIXED-WIDTH element as Org syntax.
  1825. CONTENTS is nil."
  1826. (let ((value (org-element-property :value fixed-width)))
  1827. (and value
  1828. (replace-regexp-in-string
  1829. "^" ": "
  1830. (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
  1831. ;;;; Horizontal Rule
  1832. (defun org-element-horizontal-rule-parser (limit affiliated)
  1833. "Parse an horizontal rule.
  1834. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1835. the buffer position at the beginning of the first affiliated
  1836. keyword and CDR is a plist of affiliated keywords along with
  1837. their value.
  1838. Return a list whose CAR is `horizontal-rule' and CDR is a plist
  1839. containing `:begin', `:end', `:post-blank' and `:post-affiliated'
  1840. keywords."
  1841. (save-excursion
  1842. (let ((begin (car affiliated))
  1843. (post-affiliated (point))
  1844. (post-hr (progn (forward-line) (point)))
  1845. (end (progn (skip-chars-forward " \r\t\n" limit)
  1846. (if (eobp) (point) (line-beginning-position)))))
  1847. (list 'horizontal-rule
  1848. (nconc
  1849. (list :begin begin
  1850. :end end
  1851. :post-blank (count-lines post-hr end)
  1852. :post-affiliated post-affiliated)
  1853. (cdr affiliated))))))
  1854. (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
  1855. "Interpret HORIZONTAL-RULE element as Org syntax.
  1856. CONTENTS is nil."
  1857. "-----")
  1858. ;;;; Keyword
  1859. (defun org-element-keyword-parser (limit affiliated)
  1860. "Parse a keyword at point.
  1861. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1862. the buffer position at the beginning of the first affiliated
  1863. keyword and CDR is a plist of affiliated keywords along with
  1864. their value.
  1865. Return a list whose CAR is `keyword' and CDR is a plist
  1866. containing `:key', `:value', `:begin', `:end', `:post-blank' and
  1867. `:post-affiliated' keywords."
  1868. (save-excursion
  1869. ;; An orphaned affiliated keyword is considered as a regular
  1870. ;; keyword. In this case AFFILIATED is nil, so we take care of
  1871. ;; this corner case.
  1872. (let ((begin (or (car affiliated) (point)))
  1873. (post-affiliated (point))
  1874. (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
  1875. (upcase (org-match-string-no-properties 1))))
  1876. (value (org-trim (buffer-substring-no-properties
  1877. (match-end 0) (point-at-eol))))
  1878. (pos-before-blank (progn (forward-line) (point)))
  1879. (end (progn (skip-chars-forward " \r\t\n" limit)
  1880. (if (eobp) (point) (line-beginning-position)))))
  1881. (list 'keyword
  1882. (nconc
  1883. (list :key key
  1884. :value value
  1885. :begin begin
  1886. :end end
  1887. :post-blank (count-lines pos-before-blank end)
  1888. :post-affiliated post-affiliated)
  1889. (cdr affiliated))))))
  1890. (defun org-element-keyword-interpreter (keyword contents)
  1891. "Interpret KEYWORD element as Org syntax.
  1892. CONTENTS is nil."
  1893. (format "#+%s: %s"
  1894. (org-element-property :key keyword)
  1895. (org-element-property :value keyword)))
  1896. ;;;; Latex Environment
  1897. (defconst org-element--latex-begin-environment
  1898. "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
  1899. "Regexp matching the beginning of a LaTeX environment.
  1900. The environment is captured by the first group.
  1901. See also `org-element--latex-end-environment'.")
  1902. (defconst org-element--latex-end-environment
  1903. "\\\\end{%s}[ \t]*$"
  1904. "Format string matching the ending of a LaTeX environment.
  1905. See also `org-element--latex-begin-environment'.")
  1906. (defun org-element-latex-environment-parser (limit affiliated)
  1907. "Parse a LaTeX environment.
  1908. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1909. the buffer position at the beginning of the first affiliated
  1910. keyword and CDR is a plist of affiliated keywords along with
  1911. their value.
  1912. Return a list whose CAR is `latex-environment' and CDR is a plist
  1913. containing `:begin', `:end', `:value', `:post-blank' and
  1914. `:post-affiliated' keywords.
  1915. Assume point is at the beginning of the latex environment."
  1916. (save-excursion
  1917. (let ((case-fold-search t)
  1918. (code-begin (point)))
  1919. (looking-at org-element--latex-begin-environment)
  1920. (if (not (re-search-forward (format org-element--latex-end-environment
  1921. (regexp-quote (match-string 1)))
  1922. limit t))
  1923. ;; Incomplete latex environment: parse it as a paragraph.
  1924. (org-element-paragraph-parser limit affiliated)
  1925. (let* ((code-end (progn (forward-line) (point)))
  1926. (begin (car affiliated))
  1927. (value (buffer-substring-no-properties code-begin code-end))
  1928. (end (progn (skip-chars-forward " \r\t\n" limit)
  1929. (if (eobp) (point) (line-beginning-position)))))
  1930. (list 'latex-environment
  1931. (nconc
  1932. (list :begin begin
  1933. :end end
  1934. :value value
  1935. :post-blank (count-lines code-end end)
  1936. :post-affiliated code-begin)
  1937. (cdr affiliated))))))))
  1938. (defun org-element-latex-environment-interpreter (latex-environment contents)
  1939. "Interpret LATEX-ENVIRONMENT element as Org syntax.
  1940. CONTENTS is nil."
  1941. (org-element-property :value latex-environment))
  1942. ;;;; Node Property
  1943. (defun org-element-node-property-parser (limit)
  1944. "Parse a node-property at point.
  1945. LIMIT bounds the search.
  1946. Return a list whose CAR is `node-property' and CDR is a plist
  1947. containing `:key', `:value', `:begin', `:end', `:post-blank' and
  1948. `:post-affiliated' keywords."
  1949. (looking-at org-property-re)
  1950. (let ((case-fold-search t)
  1951. (begin (point))
  1952. (key (org-match-string-no-properties 2))
  1953. (value (org-match-string-no-properties 3))
  1954. (end (save-excursion
  1955. (end-of-line)
  1956. (if (re-search-forward org-property-re limit t)
  1957. (line-beginning-position)
  1958. limit))))
  1959. (list 'node-property
  1960. (list :key key
  1961. :value value
  1962. :begin begin
  1963. :end end
  1964. :post-blank 0
  1965. :post-affiliated begin))))
  1966. (defun org-element-node-property-interpreter (node-property contents)
  1967. "Interpret NODE-PROPERTY element as Org syntax.
  1968. CONTENTS is nil."
  1969. (format org-property-format
  1970. (format ":%s:" (org-element-property :key node-property))
  1971. (or (org-element-property :value node-property) "")))
  1972. ;;;; Paragraph
  1973. (defun org-element-paragraph-parser (limit affiliated)
  1974. "Parse a paragraph.
  1975. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1976. the buffer position at the beginning of the first affiliated
  1977. keyword and CDR is a plist of affiliated keywords along with
  1978. their value.
  1979. Return a list whose CAR is `paragraph' and CDR is a plist
  1980. containing `:begin', `:end', `:contents-begin' and
  1981. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  1982. Assume point is at the beginning of the paragraph."
  1983. (save-excursion
  1984. (let* ((begin (car affiliated))
  1985. (contents-begin (point))
  1986. (before-blank
  1987. (let ((case-fold-search t))
  1988. (end-of-line)
  1989. (if (not (re-search-forward
  1990. org-element-paragraph-separate limit 'm))
  1991. limit
  1992. ;; A matching `org-element-paragraph-separate' is not
  1993. ;; necessarily the end of the paragraph. In
  1994. ;; particular, lines starting with # or : as a first
  1995. ;; non-space character are ambiguous. We have to
  1996. ;; check if they are valid Org syntax (e.g., not an
  1997. ;; incomplete keyword).
  1998. (beginning-of-line)
  1999. (while (not
  2000. (or
  2001. ;; There's no ambiguity for other symbols or
  2002. ;; empty lines: stop here.
  2003. (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
  2004. ;; Stop at valid fixed-width areas.
  2005. (looking-at "[ \t]*:\\(?: \\|$\\)")
  2006. ;; Stop at drawers.
  2007. (and (looking-at org-drawer-regexp)
  2008. (save-excursion
  2009. (re-search-forward
  2010. "^[ \t]*:END:[ \t]*$" limit t)))
  2011. ;; Stop at valid comments.
  2012. (looking-at "[ \t]*#\\(?: \\|$\\)")
  2013. ;; Stop at valid dynamic blocks.
  2014. (and (looking-at org-dblock-start-re)
  2015. (save-excursion
  2016. (re-search-forward
  2017. "^[ \t]*#\\+END:?[ \t]*$" limit t)))
  2018. ;; Stop at valid blocks.
  2019. (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  2020. (save-excursion
  2021. (re-search-forward
  2022. (format "^[ \t]*#\\+END_%s[ \t]*$"
  2023. (regexp-quote
  2024. (org-match-string-no-properties 1)))
  2025. limit t)))
  2026. ;; Stop at valid latex environments.
  2027. (and (looking-at org-element--latex-begin-environment)
  2028. (save-excursion
  2029. (re-search-forward
  2030. (format org-element--latex-end-environment
  2031. (regexp-quote
  2032. (org-match-string-no-properties 1)))
  2033. limit t)))
  2034. ;; Stop at valid keywords.
  2035. (looking-at "[ \t]*#\\+\\S-+:")
  2036. ;; Skip everything else.
  2037. (not
  2038. (progn
  2039. (end-of-line)
  2040. (re-search-forward org-element-paragraph-separate
  2041. limit 'm)))))
  2042. (beginning-of-line)))
  2043. (if (= (point) limit) limit
  2044. (goto-char (line-beginning-position)))))
  2045. (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
  2046. (forward-line)
  2047. (point)))
  2048. (end (progn (skip-chars-forward " \r\t\n" limit)
  2049. (if (eobp) (point) (line-beginning-position)))))
  2050. (list 'paragraph
  2051. (nconc
  2052. (list :begin begin
  2053. :end end
  2054. :contents-begin contents-begin
  2055. :contents-end contents-end
  2056. :post-blank (count-lines before-blank end)
  2057. :post-affiliated contents-begin)
  2058. (cdr affiliated))))))
  2059. (defun org-element-paragraph-interpreter (paragraph contents)
  2060. "Interpret PARAGRAPH element as Org syntax.
  2061. CONTENTS is the contents of the element."
  2062. contents)
  2063. ;;;; Planning
  2064. (defun org-element-planning-parser (limit)
  2065. "Parse a planning.
  2066. LIMIT bounds the search.
  2067. Return a list whose CAR is `planning' and CDR is a plist
  2068. containing `:closed', `:deadline', `:scheduled', `:begin',
  2069. `:end', `:post-blank' and `:post-affiliated' keywords."
  2070. (save-excursion
  2071. (let* ((case-fold-search nil)
  2072. (begin (point))
  2073. (post-blank (let ((before-blank (progn (forward-line) (point))))
  2074. (skip-chars-forward " \r\t\n" limit)
  2075. (skip-chars-backward " \t")
  2076. (unless (bolp) (end-of-line))
  2077. (count-lines before-blank (point))))
  2078. (end (point))
  2079. closed deadline scheduled)
  2080. (goto-char begin)
  2081. (while (re-search-forward org-keyword-time-not-clock-regexp end t)
  2082. (goto-char (match-end 1))
  2083. (skip-chars-forward " \t" end)
  2084. (let ((keyword (match-string 1))
  2085. (time (org-element-timestamp-parser)))
  2086. (cond ((equal keyword org-closed-string) (setq closed time))
  2087. ((equal keyword org-deadline-string) (setq deadline time))
  2088. (t (setq scheduled time)))))
  2089. (list 'planning
  2090. (list :closed closed
  2091. :deadline deadline
  2092. :scheduled scheduled
  2093. :begin begin
  2094. :end end
  2095. :post-blank post-blank
  2096. :post-affiliated begin)))))
  2097. (defun org-element-planning-interpreter (planning contents)
  2098. "Interpret PLANNING element as Org syntax.
  2099. CONTENTS is nil."
  2100. (mapconcat
  2101. 'identity
  2102. (delq nil
  2103. (list (let ((deadline (org-element-property :deadline planning)))
  2104. (when deadline
  2105. (concat org-deadline-string " "
  2106. (org-element-timestamp-interpreter deadline nil))))
  2107. (let ((scheduled (org-element-property :scheduled planning)))
  2108. (when scheduled
  2109. (concat org-scheduled-string " "
  2110. (org-element-timestamp-interpreter scheduled nil))))
  2111. (let ((closed (org-element-property :closed planning)))
  2112. (when closed
  2113. (concat org-closed-string " "
  2114. (org-element-timestamp-interpreter closed nil))))))
  2115. " "))
  2116. ;;;; Src Block
  2117. (defun org-element-src-block-parser (limit affiliated)
  2118. "Parse a src block.
  2119. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2120. the buffer position at the beginning of the first affiliated
  2121. keyword and CDR is a plist of affiliated keywords along with
  2122. their value.
  2123. Return a list whose CAR is `src-block' and CDR is a plist
  2124. containing `:language', `:switches', `:parameters', `:begin',
  2125. `:end', `:number-lines', `:retain-labels', `:use-labels',
  2126. `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
  2127. `:post-affiliated' keywords.
  2128. Assume point is at the beginning of the block."
  2129. (let ((case-fold-search t))
  2130. (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
  2131. limit t)))
  2132. ;; Incomplete block: parse it as a paragraph.
  2133. (org-element-paragraph-parser limit affiliated)
  2134. (let ((contents-end (match-beginning 0)))
  2135. (save-excursion
  2136. (let* ((begin (car affiliated))
  2137. (post-affiliated (point))
  2138. ;; Get language as a string.
  2139. (language
  2140. (progn
  2141. (looking-at
  2142. (concat "^[ \t]*#\\+BEGIN_SRC"
  2143. "\\(?: +\\(\\S-+\\)\\)?"
  2144. "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
  2145. "\\(.*\\)[ \t]*$"))
  2146. (org-match-string-no-properties 1)))
  2147. ;; Get switches.
  2148. (switches (org-match-string-no-properties 2))
  2149. ;; Get parameters.
  2150. (parameters (org-match-string-no-properties 3))
  2151. ;; Switches analysis
  2152. (number-lines
  2153. (cond ((not switches) nil)
  2154. ((string-match "-n\\>" switches) 'new)
  2155. ((string-match "+n\\>" switches) 'continued)))
  2156. (preserve-indent (and switches
  2157. (string-match "-i\\>" switches)))
  2158. (label-fmt
  2159. (and switches
  2160. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2161. (match-string 1 switches)))
  2162. ;; Should labels be retained in (or stripped from)
  2163. ;; src blocks?
  2164. (retain-labels
  2165. (or (not switches)
  2166. (not (string-match "-r\\>" switches))
  2167. (and number-lines (string-match "-k\\>" switches))))
  2168. ;; What should code-references use - labels or
  2169. ;; line-numbers?
  2170. (use-labels
  2171. (or (not switches)
  2172. (and retain-labels
  2173. (not (string-match "-k\\>" switches)))))
  2174. ;; Indentation.
  2175. (block-ind (progn (skip-chars-forward " \t") (current-column)))
  2176. ;; Retrieve code.
  2177. (value (org-element-remove-indentation
  2178. (org-unescape-code-in-string
  2179. (buffer-substring-no-properties
  2180. (progn (forward-line) (point)) contents-end))
  2181. block-ind))
  2182. (pos-before-blank (progn (goto-char contents-end)
  2183. (forward-line)
  2184. (point)))
  2185. ;; Get position after ending blank lines.
  2186. (end (progn (skip-chars-forward " \r\t\n" limit)
  2187. (if (eobp) (point) (line-beginning-position)))))
  2188. (list 'src-block
  2189. (nconc
  2190. (list :language language
  2191. :switches (and (org-string-nw-p switches)
  2192. (org-trim switches))
  2193. :parameters (and (org-string-nw-p parameters)
  2194. (org-trim parameters))
  2195. :begin begin
  2196. :end end
  2197. :number-lines number-lines
  2198. :preserve-indent preserve-indent
  2199. :retain-labels retain-labels
  2200. :use-labels use-labels
  2201. :label-fmt label-fmt
  2202. :value value
  2203. :post-blank (count-lines pos-before-blank end)
  2204. :post-affiliated post-affiliated)
  2205. (cdr affiliated)))))))))
  2206. (defun org-element-src-block-interpreter (src-block contents)
  2207. "Interpret SRC-BLOCK element as Org syntax.
  2208. CONTENTS is nil."
  2209. (let ((lang (org-element-property :language src-block))
  2210. (switches (org-element-property :switches src-block))
  2211. (params (org-element-property :parameters src-block))
  2212. (value
  2213. (let ((val (org-element-property :value src-block)))
  2214. (cond
  2215. ((or org-src-preserve-indentation
  2216. (org-element-property :preserve-indent src-block))
  2217. val)
  2218. ((zerop org-edit-src-content-indentation) val)
  2219. (t
  2220. (let ((ind (make-string org-edit-src-content-indentation ?\s)))
  2221. (replace-regexp-in-string
  2222. "\\(^\\)[ \t]*\\S-" ind val nil nil 1)))))))
  2223. (concat (format "#+BEGIN_SRC%s\n"
  2224. (concat (and lang (concat " " lang))
  2225. (and switches (concat " " switches))
  2226. (and params (concat " " params))))
  2227. (org-element-normalize-string (org-escape-code-in-string value))
  2228. "#+END_SRC")))
  2229. ;;;; Table
  2230. (defun org-element-table-parser (limit affiliated)
  2231. "Parse a table at point.
  2232. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2233. the buffer position at the beginning of the first affiliated
  2234. keyword and CDR is a plist of affiliated keywords along with
  2235. their value.
  2236. Return a list whose CAR is `table' and CDR is a plist containing
  2237. `:begin', `:end', `:tblfm', `:type', `:contents-begin',
  2238. `:contents-end', `:value', `:post-blank' and `:post-affiliated'
  2239. keywords.
  2240. Assume point is at the beginning of the table."
  2241. (save-excursion
  2242. (let* ((case-fold-search t)
  2243. (table-begin (point))
  2244. (type (if (org-at-table.el-p) 'table.el 'org))
  2245. (begin (car affiliated))
  2246. (table-end
  2247. (if (re-search-forward org-table-any-border-regexp limit 'm)
  2248. (goto-char (match-beginning 0))
  2249. (point)))
  2250. (tblfm (let (acc)
  2251. (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
  2252. (push (org-match-string-no-properties 1) acc)
  2253. (forward-line))
  2254. acc))
  2255. (pos-before-blank (point))
  2256. (end (progn (skip-chars-forward " \r\t\n" limit)
  2257. (if (eobp) (point) (line-beginning-position)))))
  2258. (list 'table
  2259. (nconc
  2260. (list :begin begin
  2261. :end end
  2262. :type type
  2263. :tblfm tblfm
  2264. ;; Only `org' tables have contents. `table.el' tables
  2265. ;; use a `:value' property to store raw table as
  2266. ;; a string.
  2267. :contents-begin (and (eq type 'org) table-begin)
  2268. :contents-end (and (eq type 'org) table-end)
  2269. :value (and (eq type 'table.el)
  2270. (buffer-substring-no-properties
  2271. table-begin table-end))
  2272. :post-blank (count-lines pos-before-blank end)
  2273. :post-affiliated table-begin)
  2274. (cdr affiliated))))))
  2275. (defun org-element-table-interpreter (table contents)
  2276. "Interpret TABLE element as Org syntax.
  2277. CONTENTS is a string, if table's type is `org', or nil."
  2278. (if (eq (org-element-property :type table) 'table.el)
  2279. (org-remove-indentation (org-element-property :value table))
  2280. (concat (with-temp-buffer (insert contents)
  2281. (org-table-align)
  2282. (buffer-string))
  2283. (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
  2284. (reverse (org-element-property :tblfm table))
  2285. "\n"))))
  2286. ;;;; Table Row
  2287. (defun org-element-table-row-parser (limit)
  2288. "Parse table row at point.
  2289. LIMIT bounds the search.
  2290. Return a list whose CAR is `table-row' and CDR is a plist
  2291. containing `:begin', `:end', `:contents-begin', `:contents-end',
  2292. `:type', `:post-blank' and `:post-affiliated' keywords."
  2293. (save-excursion
  2294. (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
  2295. (begin (point))
  2296. ;; A table rule has no contents. In that case, ensure
  2297. ;; CONTENTS-BEGIN matches CONTENTS-END.
  2298. (contents-begin (and (eq type 'standard)
  2299. (search-forward "|")
  2300. (point)))
  2301. (contents-end (and (eq type 'standard)
  2302. (progn
  2303. (end-of-line)
  2304. (skip-chars-backward " \t")
  2305. (point))))
  2306. (end (progn (forward-line) (point))))
  2307. (list 'table-row
  2308. (list :type type
  2309. :begin begin
  2310. :end end
  2311. :contents-begin contents-begin
  2312. :contents-end contents-end
  2313. :post-blank 0
  2314. :post-affiliated begin)))))
  2315. (defun org-element-table-row-interpreter (table-row contents)
  2316. "Interpret TABLE-ROW element as Org syntax.
  2317. CONTENTS is the contents of the table row."
  2318. (if (eq (org-element-property :type table-row) 'rule) "|-"
  2319. (concat "| " contents)))
  2320. ;;;; Verse Block
  2321. (defun org-element-verse-block-parser (limit affiliated)
  2322. "Parse a verse block.
  2323. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2324. the buffer position at the beginning of the first affiliated
  2325. keyword and CDR is a plist of affiliated keywords along with
  2326. their value.
  2327. Return a list whose CAR is `verse-block' and CDR is a plist
  2328. containing `:begin', `:end', `:contents-begin', `:contents-end',
  2329. `:post-blank' and `:post-affiliated' keywords.
  2330. Assume point is at beginning of the block."
  2331. (let ((case-fold-search t))
  2332. (if (not (save-excursion
  2333. (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
  2334. ;; Incomplete block: parse it as a paragraph.
  2335. (org-element-paragraph-parser limit affiliated)
  2336. (let ((contents-end (match-beginning 0)))
  2337. (save-excursion
  2338. (let* ((begin (car affiliated))
  2339. (post-affiliated (point))
  2340. (contents-begin (progn (forward-line) (point)))
  2341. (pos-before-blank (progn (goto-char contents-end)
  2342. (forward-line)
  2343. (point)))
  2344. (end (progn (skip-chars-forward " \r\t\n" limit)
  2345. (if (eobp) (point) (line-beginning-position)))))
  2346. (list 'verse-block
  2347. (nconc
  2348. (list :begin begin
  2349. :end end
  2350. :contents-begin contents-begin
  2351. :contents-end contents-end
  2352. :post-blank (count-lines pos-before-blank end)
  2353. :post-affiliated post-affiliated)
  2354. (cdr affiliated)))))))))
  2355. (defun org-element-verse-block-interpreter (verse-block contents)
  2356. "Interpret VERSE-BLOCK element as Org syntax.
  2357. CONTENTS is verse block contents."
  2358. (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
  2359. ;;; Objects
  2360. ;;
  2361. ;; Unlike to elements, raw text can be found between objects. Hence,
  2362. ;; `org-element--object-lex' is provided to find the next object in
  2363. ;; buffer.
  2364. ;;
  2365. ;; Some object types (e.g., `italic') are recursive. Restrictions on
  2366. ;; object types they can contain will be specified in
  2367. ;; `org-element-object-restrictions'.
  2368. ;;
  2369. ;; Creating a new type of object requires to alter
  2370. ;; `org-element--object-regexp' and `org-element--object-lex', add the
  2371. ;; new type in `org-element-all-objects', and possibly add
  2372. ;; restrictions in `org-element-object-restrictions'.
  2373. ;;;; Bold
  2374. (defun org-element-bold-parser ()
  2375. "Parse bold object at point, if any.
  2376. When at a bold object, return a list whose car is `bold' and cdr
  2377. is a plist with `:begin', `:end', `:contents-begin' and
  2378. `:contents-end' and `:post-blank' keywords. Otherwise, return
  2379. nil.
  2380. Assume point is at the first star marker."
  2381. (save-excursion
  2382. (unless (bolp) (backward-char 1))
  2383. (when (looking-at org-emph-re)
  2384. (let ((begin (match-beginning 2))
  2385. (contents-begin (match-beginning 4))
  2386. (contents-end (match-end 4))
  2387. (post-blank (progn (goto-char (match-end 2))
  2388. (skip-chars-forward " \t")))
  2389. (end (point)))
  2390. (list 'bold
  2391. (list :begin begin
  2392. :end end
  2393. :contents-begin contents-begin
  2394. :contents-end contents-end
  2395. :post-blank post-blank))))))
  2396. (defun org-element-bold-interpreter (bold contents)
  2397. "Interpret BOLD object as Org syntax.
  2398. CONTENTS is the contents of the object."
  2399. (format "*%s*" contents))
  2400. ;;;; Code
  2401. (defun org-element-code-parser ()
  2402. "Parse code object at point, if any.
  2403. When at a code object, return a list whose car is `code' and cdr
  2404. is a plist with `:value', `:begin', `:end' and `:post-blank'
  2405. keywords. Otherwise, return nil.
  2406. Assume point is at the first tilde marker."
  2407. (save-excursion
  2408. (unless (bolp) (backward-char 1))
  2409. (when (looking-at org-emph-re)
  2410. (let ((begin (match-beginning 2))
  2411. (value (org-match-string-no-properties 4))
  2412. (post-blank (progn (goto-char (match-end 2))
  2413. (skip-chars-forward " \t")))
  2414. (end (point)))
  2415. (list 'code
  2416. (list :value value
  2417. :begin begin
  2418. :end end
  2419. :post-blank post-blank))))))
  2420. (defun org-element-code-interpreter (code contents)
  2421. "Interpret CODE object as Org syntax.
  2422. CONTENTS is nil."
  2423. (format "~%s~" (org-element-property :value code)))
  2424. ;;;; Entity
  2425. (defun org-element-entity-parser ()
  2426. "Parse entity at point, if any.
  2427. When at an entity, return a list whose car is `entity' and cdr
  2428. a plist with `:begin', `:end', `:latex', `:latex-math-p',
  2429. `:html', `:latin1', `:utf-8', `:ascii', `:use-brackets-p' and
  2430. `:post-blank' as keywords. Otherwise, return nil.
  2431. Assume point is at the beginning of the entity."
  2432. (catch 'no-object
  2433. (when (looking-at "\\\\\\(?:\\(?1:_ +\\)\\|\\(?1:there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\(?2:$\\|{}\\|[^[:alpha:]]\\)\\)")
  2434. (save-excursion
  2435. (let* ((value (or (org-entity-get (match-string 1))
  2436. (throw 'no-object nil)))
  2437. (begin (match-beginning 0))
  2438. (bracketsp (string= (match-string 2) "{}"))
  2439. (post-blank (progn (goto-char (match-end 1))
  2440. (when bracketsp (forward-char 2))
  2441. (skip-chars-forward " \t")))
  2442. (end (point)))
  2443. (list 'entity
  2444. (list :name (car value)
  2445. :latex (nth 1 value)
  2446. :latex-math-p (nth 2 value)
  2447. :html (nth 3 value)
  2448. :ascii (nth 4 value)
  2449. :latin1 (nth 5 value)
  2450. :utf-8 (nth 6 value)
  2451. :begin begin
  2452. :end end
  2453. :use-brackets-p bracketsp
  2454. :post-blank post-blank)))))))
  2455. (defun org-element-entity-interpreter (entity contents)
  2456. "Interpret ENTITY object as Org syntax.
  2457. CONTENTS is nil."
  2458. (concat "\\"
  2459. (org-element-property :name entity)
  2460. (when (org-element-property :use-brackets-p entity) "{}")))
  2461. ;;;; Export Snippet
  2462. (defun org-element-export-snippet-parser ()
  2463. "Parse export snippet at point.
  2464. When at an export snippet, return a list whose car is
  2465. `export-snippet' and cdr a plist with `:begin', `:end',
  2466. `:back-end', `:value' and `:post-blank' as keywords. Otherwise,
  2467. return nil.
  2468. Assume point is at the beginning of the snippet."
  2469. (save-excursion
  2470. (let (contents-end)
  2471. (when (and (looking-at "@@\\([-A-Za-z0-9]+\\):")
  2472. (setq contents-end
  2473. (save-match-data (goto-char (match-end 0))
  2474. (re-search-forward "@@" nil t)
  2475. (match-beginning 0))))
  2476. (let* ((begin (match-beginning 0))
  2477. (back-end (org-match-string-no-properties 1))
  2478. (value (buffer-substring-no-properties
  2479. (match-end 0) contents-end))
  2480. (post-blank (skip-chars-forward " \t"))
  2481. (end (point)))
  2482. (list 'export-snippet
  2483. (list :back-end back-end
  2484. :value value
  2485. :begin begin
  2486. :end end
  2487. :post-blank post-blank)))))))
  2488. (defun org-element-export-snippet-interpreter (export-snippet contents)
  2489. "Interpret EXPORT-SNIPPET object as Org syntax.
  2490. CONTENTS is nil."
  2491. (format "@@%s:%s@@"
  2492. (org-element-property :back-end export-snippet)
  2493. (org-element-property :value export-snippet)))
  2494. ;;;; Footnote Reference
  2495. (defun org-element-footnote-reference-parser ()
  2496. "Parse footnote reference at point, if any.
  2497. When at a footnote reference, return a list whose car is
  2498. `footnote-reference' and cdr a plist with `:label', `:type',
  2499. `:begin', `:end', `:content-begin', `:contents-end' and
  2500. `:post-blank' as keywords. Otherwise, return nil."
  2501. (when (looking-at org-footnote-re)
  2502. (let ((closing (with-syntax-table org-element--pair-square-table
  2503. (ignore-errors (scan-lists (point) 1 0)))))
  2504. (when closing
  2505. (save-excursion
  2506. (let* ((begin (point))
  2507. (label
  2508. (or (org-match-string-no-properties 2)
  2509. (org-match-string-no-properties 3)
  2510. (and (match-string 1)
  2511. (concat "fn:" (org-match-string-no-properties 1)))))
  2512. (type (if (or (not label) (match-string 1)) 'inline 'standard))
  2513. (inner-begin (match-end 0))
  2514. (inner-end (1- closing))
  2515. (post-blank (progn (goto-char closing)
  2516. (skip-chars-forward " \t")))
  2517. (end (point)))
  2518. (list 'footnote-reference
  2519. (list :label label
  2520. :type type
  2521. :begin begin
  2522. :end end
  2523. :contents-begin (and (eq type 'inline) inner-begin)
  2524. :contents-end (and (eq type 'inline) inner-end)
  2525. :post-blank post-blank))))))))
  2526. (defun org-element-footnote-reference-interpreter (footnote-reference contents)
  2527. "Interpret FOOTNOTE-REFERENCE object as Org syntax.
  2528. CONTENTS is its definition, when inline, or nil."
  2529. (format "[%s]"
  2530. (concat (or (org-element-property :label footnote-reference) "fn:")
  2531. (and contents (concat ":" contents)))))
  2532. ;;;; Inline Babel Call
  2533. (defun org-element-inline-babel-call-parser ()
  2534. "Parse inline babel call at point, if any.
  2535. When at an inline babel call, return a list whose car is
  2536. `inline-babel-call' and cdr a plist with `:call',
  2537. `:inside-header', `:arguments', `:end-header', `:begin', `:end',
  2538. `:value' and `:post-blank' as keywords. Otherwise, return nil.
  2539. Assume point is at the beginning of the babel call."
  2540. (save-excursion
  2541. (unless (bolp) (backward-char))
  2542. (when (let ((case-fold-search t))
  2543. (looking-at org-babel-inline-lob-one-liner-regexp))
  2544. (let ((begin (match-end 1))
  2545. (call (org-match-string-no-properties 2))
  2546. (inside-header (org-string-nw-p (org-match-string-no-properties 4)))
  2547. (arguments (org-string-nw-p (org-match-string-no-properties 6)))
  2548. (end-header (org-string-nw-p (org-match-string-no-properties 8)))
  2549. (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
  2550. (post-blank (progn (goto-char (match-end 0))
  2551. (skip-chars-forward " \t")))
  2552. (end (point)))
  2553. (list 'inline-babel-call
  2554. (list :call call
  2555. :inside-header inside-header
  2556. :arguments arguments
  2557. :end-header end-header
  2558. :begin begin
  2559. :end end
  2560. :value value
  2561. :post-blank post-blank))))))
  2562. (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
  2563. "Interpret INLINE-BABEL-CALL object as Org syntax.
  2564. CONTENTS is nil."
  2565. (concat "call_"
  2566. (org-element-property :call inline-babel-call)
  2567. (let ((h (org-element-property :inside-header inline-babel-call)))
  2568. (and h (format "[%s]" h)))
  2569. "(" (org-element-property :arguments inline-babel-call) ")"
  2570. (let ((h (org-element-property :end-header inline-babel-call)))
  2571. (and h (format "[%s]" h)))))
  2572. ;;;; Inline Src Block
  2573. (defun org-element-inline-src-block-parser ()
  2574. "Parse inline source block at point, if any.
  2575. When at an inline source block, return a list whose car is
  2576. `inline-src-block' and cdr a plist with `:begin', `:end',
  2577. `:language', `:value', `:parameters' and `:post-blank' as
  2578. keywords. Otherwise, return nil.
  2579. Assume point is at the beginning of the inline src block."
  2580. (save-excursion
  2581. (unless (bolp) (backward-char))
  2582. (when (looking-at org-babel-inline-src-block-regexp)
  2583. (let ((begin (match-beginning 1))
  2584. (language (org-match-string-no-properties 2))
  2585. (parameters (org-match-string-no-properties 4))
  2586. (value (org-match-string-no-properties 5))
  2587. (post-blank (progn (goto-char (match-end 0))
  2588. (skip-chars-forward " \t")))
  2589. (end (point)))
  2590. (list 'inline-src-block
  2591. (list :language language
  2592. :value value
  2593. :parameters parameters
  2594. :begin begin
  2595. :end end
  2596. :post-blank post-blank))))))
  2597. (defun org-element-inline-src-block-interpreter (inline-src-block contents)
  2598. "Interpret INLINE-SRC-BLOCK object as Org syntax.
  2599. CONTENTS is nil."
  2600. (let ((language (org-element-property :language inline-src-block))
  2601. (arguments (org-element-property :parameters inline-src-block))
  2602. (body (org-element-property :value inline-src-block)))
  2603. (format "src_%s%s{%s}"
  2604. language
  2605. (if arguments (format "[%s]" arguments) "")
  2606. body)))
  2607. ;;;; Italic
  2608. (defun org-element-italic-parser ()
  2609. "Parse italic object at point, if any.
  2610. When at an italic object, return a list whose car is `italic' and
  2611. cdr is a plist with `:begin', `:end', `:contents-begin' and
  2612. `:contents-end' and `:post-blank' keywords. Otherwise, return
  2613. nil.
  2614. Assume point is at the first slash marker."
  2615. (save-excursion
  2616. (unless (bolp) (backward-char 1))
  2617. (when (looking-at org-emph-re)
  2618. (let ((begin (match-beginning 2))
  2619. (contents-begin (match-beginning 4))
  2620. (contents-end (match-end 4))
  2621. (post-blank (progn (goto-char (match-end 2))
  2622. (skip-chars-forward " \t")))
  2623. (end (point)))
  2624. (list 'italic
  2625. (list :begin begin
  2626. :end end
  2627. :contents-begin contents-begin
  2628. :contents-end contents-end
  2629. :post-blank post-blank))))))
  2630. (defun org-element-italic-interpreter (italic contents)
  2631. "Interpret ITALIC object as Org syntax.
  2632. CONTENTS is the contents of the object."
  2633. (format "/%s/" contents))
  2634. ;;;; Latex Fragment
  2635. (defun org-element-latex-fragment-parser ()
  2636. "Parse LaTeX fragment at point, if any.
  2637. When at a LaTeX fragment, return a list whose car is
  2638. `latex-fragment' and cdr a plist with `:value', `:begin', `:end',
  2639. and `:post-blank' as keywords. Otherwise, return nil.
  2640. Assume point is at the beginning of the LaTeX fragment."
  2641. (catch 'no-object
  2642. (save-excursion
  2643. (let* ((begin (point))
  2644. (after-fragment
  2645. (if (eq (char-after) ?$)
  2646. (if (eq (char-after (1+ (point))) ?$)
  2647. (search-forward "$$" nil t 2)
  2648. (and (not (eq (char-before) ?$))
  2649. (search-forward "$" nil t 2)
  2650. (not (memq (char-before (match-beginning 0))
  2651. '(?\s ?\t ?\n ?, ?.)))
  2652. (looking-at "\\([- \t.,?;:'\"]\\|$\\)")
  2653. (point)))
  2654. (case (char-after (1+ (point)))
  2655. (?\( (search-forward "\\)" nil t))
  2656. (?\[ (search-forward "\\]" nil t))
  2657. (otherwise
  2658. ;; Macro.
  2659. (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
  2660. (match-end 0))))))
  2661. (post-blank (if (not after-fragment) (throw 'no-object nil)
  2662. (goto-char after-fragment)
  2663. (skip-chars-forward " \t")))
  2664. (end (point)))
  2665. (list 'latex-fragment
  2666. (list :value (buffer-substring-no-properties begin after-fragment)
  2667. :begin begin
  2668. :end end
  2669. :post-blank post-blank))))))
  2670. (defun org-element-latex-fragment-interpreter (latex-fragment contents)
  2671. "Interpret LATEX-FRAGMENT object as Org syntax.
  2672. CONTENTS is nil."
  2673. (org-element-property :value latex-fragment))
  2674. ;;;; Line Break
  2675. (defun org-element-line-break-parser ()
  2676. "Parse line break at point, if any.
  2677. When at a line break, return a list whose car is `line-break',
  2678. and cdr a plist with `:begin', `:end' and `:post-blank' keywords.
  2679. Otherwise, return nil.
  2680. Assume point is at the beginning of the line break."
  2681. (when (and (org-looking-at-p "\\\\\\\\[ \t]*$")
  2682. (not (eq (char-before) ?\\)))
  2683. (list 'line-break
  2684. (list :begin (point)
  2685. :end (line-beginning-position 2)
  2686. :post-blank 0))))
  2687. (defun org-element-line-break-interpreter (line-break contents)
  2688. "Interpret LINE-BREAK object as Org syntax.
  2689. CONTENTS is nil."
  2690. "\\\\\n")
  2691. ;;;; Link
  2692. (defun org-element-link-parser ()
  2693. "Parse link at point, if any.
  2694. When at a link, return a list whose car is `link' and cdr a plist
  2695. with `:type', `:path', `:raw-link', `:application',
  2696. `:search-option', `:begin', `:end', `:contents-begin',
  2697. `:contents-end' and `:post-blank' as keywords. Otherwise, return
  2698. nil.
  2699. Assume point is at the beginning of the link."
  2700. (catch 'no-object
  2701. (let ((begin (point))
  2702. end contents-begin contents-end link-end post-blank path type
  2703. raw-link link search-option application)
  2704. (cond
  2705. ;; Type 1: Text targeted from a radio target.
  2706. ((and org-target-link-regexp
  2707. (save-excursion (or (bolp) (backward-char))
  2708. (looking-at org-target-link-regexp)))
  2709. (setq type "radio"
  2710. link-end (match-end 1)
  2711. path (org-match-string-no-properties 1)
  2712. contents-begin (match-beginning 1)
  2713. contents-end (match-end 1)))
  2714. ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
  2715. ((looking-at org-bracket-link-regexp)
  2716. (setq contents-begin (match-beginning 3)
  2717. contents-end (match-end 3)
  2718. link-end (match-end 0)
  2719. ;; RAW-LINK is the original link. Expand any
  2720. ;; abbreviation in it.
  2721. raw-link (org-translate-link
  2722. (org-link-expand-abbrev
  2723. (org-match-string-no-properties 1))))
  2724. ;; Determine TYPE of link and set PATH accordingly.
  2725. (cond
  2726. ;; File type.
  2727. ((or (file-name-absolute-p raw-link)
  2728. (string-match "\\`\\.\\.?/" raw-link))
  2729. (setq type "file" path raw-link))
  2730. ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
  2731. ((string-match org-link-types-re raw-link)
  2732. (setq type (match-string 1 raw-link)
  2733. ;; According to RFC 3986, extra whitespace should be
  2734. ;; ignored when a URI is extracted.
  2735. path (replace-regexp-in-string
  2736. "[ \t]*\n[ \t]*" "" (substring raw-link (match-end 0)))))
  2737. ;; Id type: PATH is the id.
  2738. ((string-match "\\`id:\\([-a-f0-9]+\\)" raw-link)
  2739. (setq type "id" path (match-string 1 raw-link)))
  2740. ;; Code-ref type: PATH is the name of the reference.
  2741. ((string-match "\\`(\\(.*\\))\\'" raw-link)
  2742. (setq type "coderef" path (match-string 1 raw-link)))
  2743. ;; Custom-id type: PATH is the name of the custom id.
  2744. ((= (aref raw-link 0) ?#)
  2745. (setq type "custom-id" path (substring raw-link 1)))
  2746. ;; Fuzzy type: Internal link either matches a target, an
  2747. ;; headline name or nothing. PATH is the target or
  2748. ;; headline's name.
  2749. (t (setq type "fuzzy" path raw-link))))
  2750. ;; Type 3: Plain link, e.g., http://orgmode.org
  2751. ((looking-at org-plain-link-re)
  2752. (setq raw-link (org-match-string-no-properties 0)
  2753. type (org-match-string-no-properties 1)
  2754. link-end (match-end 0)
  2755. path (org-match-string-no-properties 2)))
  2756. ;; Type 4: Angular link, e.g., <http://orgmode.org>
  2757. ((looking-at org-angle-link-re)
  2758. (setq raw-link (buffer-substring-no-properties
  2759. (match-beginning 1) (match-end 2))
  2760. type (org-match-string-no-properties 1)
  2761. link-end (match-end 0)
  2762. path (org-match-string-no-properties 2)))
  2763. (t (throw 'no-object nil)))
  2764. ;; In any case, deduce end point after trailing white space from
  2765. ;; LINK-END variable.
  2766. (save-excursion
  2767. (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
  2768. end (point))
  2769. ;; Special "file" type link processing. Extract opening
  2770. ;; application and search option, if any. Also normalize URI.
  2771. (when (string-match "\\`file\\(?:\\+\\(.+\\)\\)?\\'" type)
  2772. (setq application (match-string 1 type) type "file")
  2773. (when (string-match "::\\(.*\\)\\'" path)
  2774. (setq search-option (match-string 1 path)
  2775. path (replace-match "" nil nil path)))
  2776. (setq path (replace-regexp-in-string "\\`/+" "/" path)))
  2777. (list 'link
  2778. (list :type type
  2779. :path path
  2780. :raw-link (or raw-link path)
  2781. :application application
  2782. :search-option search-option
  2783. :begin begin
  2784. :end end
  2785. :contents-begin contents-begin
  2786. :contents-end contents-end
  2787. :post-blank post-blank))))))
  2788. (defun org-element-link-interpreter (link contents)
  2789. "Interpret LINK object as Org syntax.
  2790. CONTENTS is the contents of the object, or nil."
  2791. (let ((type (org-element-property :type link))
  2792. (raw-link (org-element-property :raw-link link)))
  2793. (if (string= type "radio") raw-link
  2794. (format "[[%s]%s]"
  2795. raw-link
  2796. (if contents (format "[%s]" contents) "")))))
  2797. ;;;; Macro
  2798. (defun org-element-macro-parser ()
  2799. "Parse macro at point, if any.
  2800. When at a macro, return a list whose car is `macro' and cdr
  2801. a plist with `:key', `:args', `:begin', `:end', `:value' and
  2802. `:post-blank' as keywords. Otherwise, return nil.
  2803. Assume point is at the macro."
  2804. (save-excursion
  2805. (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
  2806. (let ((begin (point))
  2807. (key (downcase (org-match-string-no-properties 1)))
  2808. (value (org-match-string-no-properties 0))
  2809. (post-blank (progn (goto-char (match-end 0))
  2810. (skip-chars-forward " \t")))
  2811. (end (point))
  2812. (args (let ((args (org-match-string-no-properties 3)))
  2813. (and args (org-macro-extract-arguments args)))))
  2814. (list 'macro
  2815. (list :key key
  2816. :value value
  2817. :args args
  2818. :begin begin
  2819. :end end
  2820. :post-blank post-blank))))))
  2821. (defun org-element-macro-interpreter (macro contents)
  2822. "Interpret MACRO object as Org syntax.
  2823. CONTENTS is nil."
  2824. (org-element-property :value macro))
  2825. ;;;; Radio-target
  2826. (defun org-element-radio-target-parser ()
  2827. "Parse radio target at point, if any.
  2828. When at a radio target, return a list whose car is `radio-target'
  2829. and cdr a plist with `:begin', `:end', `:contents-begin',
  2830. `:contents-end', `:value' and `:post-blank' as keywords.
  2831. Otherwise, return nil.
  2832. Assume point is at the radio target."
  2833. (save-excursion
  2834. (when (looking-at org-radio-target-regexp)
  2835. (let ((begin (point))
  2836. (contents-begin (match-beginning 1))
  2837. (contents-end (match-end 1))
  2838. (value (org-match-string-no-properties 1))
  2839. (post-blank (progn (goto-char (match-end 0))
  2840. (skip-chars-forward " \t")))
  2841. (end (point)))
  2842. (list 'radio-target
  2843. (list :begin begin
  2844. :end end
  2845. :contents-begin contents-begin
  2846. :contents-end contents-end
  2847. :post-blank post-blank
  2848. :value value))))))
  2849. (defun org-element-radio-target-interpreter (target contents)
  2850. "Interpret TARGET object as Org syntax.
  2851. CONTENTS is the contents of the object."
  2852. (concat "<<<" contents ">>>"))
  2853. ;;;; Statistics Cookie
  2854. (defun org-element-statistics-cookie-parser ()
  2855. "Parse statistics cookie at point, if any.
  2856. When at a statistics cookie, return a list whose car is
  2857. `statistics-cookie', and cdr a plist with `:begin', `:end',
  2858. `:value' and `:post-blank' keywords. Otherwise, return nil.
  2859. Assume point is at the beginning of the statistics-cookie."
  2860. (save-excursion
  2861. (when (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
  2862. (let* ((begin (point))
  2863. (value (buffer-substring-no-properties
  2864. (match-beginning 0) (match-end 0)))
  2865. (post-blank (progn (goto-char (match-end 0))
  2866. (skip-chars-forward " \t")))
  2867. (end (point)))
  2868. (list 'statistics-cookie
  2869. (list :begin begin
  2870. :end end
  2871. :value value
  2872. :post-blank post-blank))))))
  2873. (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
  2874. "Interpret STATISTICS-COOKIE object as Org syntax.
  2875. CONTENTS is nil."
  2876. (org-element-property :value statistics-cookie))
  2877. ;;;; Strike-Through
  2878. (defun org-element-strike-through-parser ()
  2879. "Parse strike-through object at point, if any.
  2880. When at a strike-through object, return a list whose car is
  2881. `strike-through' and cdr is a plist with `:begin', `:end',
  2882. `:contents-begin' and `:contents-end' and `:post-blank' keywords.
  2883. Otherwise, return nil.
  2884. Assume point is at the first plus sign marker."
  2885. (save-excursion
  2886. (unless (bolp) (backward-char 1))
  2887. (when (looking-at org-emph-re)
  2888. (let ((begin (match-beginning 2))
  2889. (contents-begin (match-beginning 4))
  2890. (contents-end (match-end 4))
  2891. (post-blank (progn (goto-char (match-end 2))
  2892. (skip-chars-forward " \t")))
  2893. (end (point)))
  2894. (list 'strike-through
  2895. (list :begin begin
  2896. :end end
  2897. :contents-begin contents-begin
  2898. :contents-end contents-end
  2899. :post-blank post-blank))))))
  2900. (defun org-element-strike-through-interpreter (strike-through contents)
  2901. "Interpret STRIKE-THROUGH object as Org syntax.
  2902. CONTENTS is the contents of the object."
  2903. (format "+%s+" contents))
  2904. ;;;; Subscript
  2905. (defun org-element-subscript-parser ()
  2906. "Parse subscript at point, if any.
  2907. When at a subscript object, return a list whose car is
  2908. `subscript' and cdr a plist with `:begin', `:end',
  2909. `:contents-begin', `:contents-end', `:use-brackets-p' and
  2910. `:post-blank' as keywords. Otherwise, return nil.
  2911. Assume point is at the underscore."
  2912. (save-excursion
  2913. (unless (bolp) (backward-char))
  2914. (when (looking-at org-match-substring-regexp)
  2915. (let ((bracketsp (match-beginning 4))
  2916. (begin (match-beginning 2))
  2917. (contents-begin (or (match-beginning 4)
  2918. (match-beginning 3)))
  2919. (contents-end (or (match-end 4) (match-end 3)))
  2920. (post-blank (progn (goto-char (match-end 0))
  2921. (skip-chars-forward " \t")))
  2922. (end (point)))
  2923. (list 'subscript
  2924. (list :begin begin
  2925. :end end
  2926. :use-brackets-p bracketsp
  2927. :contents-begin contents-begin
  2928. :contents-end contents-end
  2929. :post-blank post-blank))))))
  2930. (defun org-element-subscript-interpreter (subscript contents)
  2931. "Interpret SUBSCRIPT object as Org syntax.
  2932. CONTENTS is the contents of the object."
  2933. (format
  2934. (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
  2935. contents))
  2936. ;;;; Superscript
  2937. (defun org-element-superscript-parser ()
  2938. "Parse superscript at point, if any.
  2939. When at a superscript object, return a list whose car is
  2940. `superscript' and cdr a plist with `:begin', `:end',
  2941. `:contents-begin', `:contents-end', `:use-brackets-p' and
  2942. `:post-blank' as keywords. Otherwise, return nil.
  2943. Assume point is at the caret."
  2944. (save-excursion
  2945. (unless (bolp) (backward-char))
  2946. (when (looking-at org-match-substring-regexp)
  2947. (let ((bracketsp (match-beginning 4))
  2948. (begin (match-beginning 2))
  2949. (contents-begin (or (match-beginning 4)
  2950. (match-beginning 3)))
  2951. (contents-end (or (match-end 4) (match-end 3)))
  2952. (post-blank (progn (goto-char (match-end 0))
  2953. (skip-chars-forward " \t")))
  2954. (end (point)))
  2955. (list 'superscript
  2956. (list :begin begin
  2957. :end end
  2958. :use-brackets-p bracketsp
  2959. :contents-begin contents-begin
  2960. :contents-end contents-end
  2961. :post-blank post-blank))))))
  2962. (defun org-element-superscript-interpreter (superscript contents)
  2963. "Interpret SUPERSCRIPT object as Org syntax.
  2964. CONTENTS is the contents of the object."
  2965. (format
  2966. (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
  2967. contents))
  2968. ;;;; Table Cell
  2969. (defun org-element-table-cell-parser ()
  2970. "Parse table cell at point.
  2971. Return a list whose car is `table-cell' and cdr is a plist
  2972. containing `:begin', `:end', `:contents-begin', `:contents-end'
  2973. and `:post-blank' keywords."
  2974. (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
  2975. (let* ((begin (match-beginning 0))
  2976. (end (match-end 0))
  2977. (contents-begin (match-beginning 1))
  2978. (contents-end (match-end 1)))
  2979. (list 'table-cell
  2980. (list :begin begin
  2981. :end end
  2982. :contents-begin contents-begin
  2983. :contents-end contents-end
  2984. :post-blank 0))))
  2985. (defun org-element-table-cell-interpreter (table-cell contents)
  2986. "Interpret TABLE-CELL element as Org syntax.
  2987. CONTENTS is the contents of the cell, or nil."
  2988. (concat " " contents " |"))
  2989. ;;;; Target
  2990. (defun org-element-target-parser ()
  2991. "Parse target at point, if any.
  2992. When at a target, return a list whose car is `target' and cdr
  2993. a plist with `:begin', `:end', `:value' and `:post-blank' as
  2994. keywords. Otherwise, return nil.
  2995. Assume point is at the target."
  2996. (save-excursion
  2997. (when (looking-at org-target-regexp)
  2998. (let ((begin (point))
  2999. (value (org-match-string-no-properties 1))
  3000. (post-blank (progn (goto-char (match-end 0))
  3001. (skip-chars-forward " \t")))
  3002. (end (point)))
  3003. (list 'target
  3004. (list :begin begin
  3005. :end end
  3006. :value value
  3007. :post-blank post-blank))))))
  3008. (defun org-element-target-interpreter (target contents)
  3009. "Interpret TARGET object as Org syntax.
  3010. CONTENTS is nil."
  3011. (format "<<%s>>" (org-element-property :value target)))
  3012. ;;;; Timestamp
  3013. (defconst org-element--timestamp-regexp
  3014. (concat org-ts-regexp-both
  3015. "\\|"
  3016. "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  3017. "\\|"
  3018. "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
  3019. "Regexp matching any timestamp type object.")
  3020. (defun org-element-timestamp-parser ()
  3021. "Parse time stamp at point, if any.
  3022. When at a time stamp, return a list whose car is `timestamp', and
  3023. cdr a plist with `:type', `:raw-value', `:year-start',
  3024. `:month-start', `:day-start', `:hour-start', `:minute-start',
  3025. `:year-end', `:month-end', `:day-end', `:hour-end',
  3026. `:minute-end', `:repeater-type', `:repeater-value',
  3027. `:repeater-unit', `:warning-type', `:warning-value',
  3028. `:warning-unit', `:begin', `:end' and `:post-blank' keywords.
  3029. Otherwise, return nil.
  3030. Assume point is at the beginning of the timestamp."
  3031. (when (org-looking-at-p org-element--timestamp-regexp)
  3032. (save-excursion
  3033. (let* ((begin (point))
  3034. (activep (eq (char-after) ?<))
  3035. (raw-value
  3036. (progn
  3037. (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
  3038. (match-string-no-properties 0)))
  3039. (date-start (match-string-no-properties 1))
  3040. (date-end (match-string 3))
  3041. (diaryp (match-beginning 2))
  3042. (post-blank (progn (goto-char (match-end 0))
  3043. (skip-chars-forward " \t")))
  3044. (end (point))
  3045. (time-range
  3046. (and (not diaryp)
  3047. (string-match
  3048. "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
  3049. date-start)
  3050. (cons (string-to-number (match-string 2 date-start))
  3051. (string-to-number (match-string 3 date-start)))))
  3052. (type (cond (diaryp 'diary)
  3053. ((and activep (or date-end time-range)) 'active-range)
  3054. (activep 'active)
  3055. ((or date-end time-range) 'inactive-range)
  3056. (t 'inactive)))
  3057. (repeater-props
  3058. (and (not diaryp)
  3059. (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
  3060. raw-value)
  3061. (list
  3062. :repeater-type
  3063. (let ((type (match-string 1 raw-value)))
  3064. (cond ((equal "++" type) 'catch-up)
  3065. ((equal ".+" type) 'restart)
  3066. (t 'cumulate)))
  3067. :repeater-value (string-to-number (match-string 2 raw-value))
  3068. :repeater-unit
  3069. (case (string-to-char (match-string 3 raw-value))
  3070. (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
  3071. (warning-props
  3072. (and (not diaryp)
  3073. (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
  3074. (list
  3075. :warning-type (if (match-string 1 raw-value) 'first 'all)
  3076. :warning-value (string-to-number (match-string 2 raw-value))
  3077. :warning-unit
  3078. (case (string-to-char (match-string 3 raw-value))
  3079. (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
  3080. year-start month-start day-start hour-start minute-start year-end
  3081. month-end day-end hour-end minute-end)
  3082. ;; Parse date-start.
  3083. (unless diaryp
  3084. (let ((date (org-parse-time-string date-start t)))
  3085. (setq year-start (nth 5 date)
  3086. month-start (nth 4 date)
  3087. day-start (nth 3 date)
  3088. hour-start (nth 2 date)
  3089. minute-start (nth 1 date))))
  3090. ;; Compute date-end. It can be provided directly in time-stamp,
  3091. ;; or extracted from time range. Otherwise, it defaults to the
  3092. ;; same values as date-start.
  3093. (unless diaryp
  3094. (let ((date (and date-end (org-parse-time-string date-end t))))
  3095. (setq year-end (or (nth 5 date) year-start)
  3096. month-end (or (nth 4 date) month-start)
  3097. day-end (or (nth 3 date) day-start)
  3098. hour-end (or (nth 2 date) (car time-range) hour-start)
  3099. minute-end (or (nth 1 date) (cdr time-range) minute-start))))
  3100. (list 'timestamp
  3101. (nconc (list :type type
  3102. :raw-value raw-value
  3103. :year-start year-start
  3104. :month-start month-start
  3105. :day-start day-start
  3106. :hour-start hour-start
  3107. :minute-start minute-start
  3108. :year-end year-end
  3109. :month-end month-end
  3110. :day-end day-end
  3111. :hour-end hour-end
  3112. :minute-end minute-end
  3113. :begin begin
  3114. :end end
  3115. :post-blank post-blank)
  3116. repeater-props
  3117. warning-props))))))
  3118. (defun org-element-timestamp-interpreter (timestamp contents)
  3119. "Interpret TIMESTAMP object as Org syntax.
  3120. CONTENTS is nil."
  3121. (let* ((repeat-string
  3122. (concat
  3123. (case (org-element-property :repeater-type timestamp)
  3124. (cumulate "+") (catch-up "++") (restart ".+"))
  3125. (let ((val (org-element-property :repeater-value timestamp)))
  3126. (and val (number-to-string val)))
  3127. (case (org-element-property :repeater-unit timestamp)
  3128. (hour "h") (day "d") (week "w") (month "m") (year "y"))))
  3129. (warning-string
  3130. (concat
  3131. (case (org-element-property :warning-type timestamp)
  3132. (first "--")
  3133. (all "-"))
  3134. (let ((val (org-element-property :warning-value timestamp)))
  3135. (and val (number-to-string val)))
  3136. (case (org-element-property :warning-unit timestamp)
  3137. (hour "h") (day "d") (week "w") (month "m") (year "y"))))
  3138. (build-ts-string
  3139. ;; Build an Org timestamp string from TIME. ACTIVEP is
  3140. ;; non-nil when time stamp is active. If WITH-TIME-P is
  3141. ;; non-nil, add a time part. HOUR-END and MINUTE-END
  3142. ;; specify a time range in the timestamp. REPEAT-STRING is
  3143. ;; the repeater string, if any.
  3144. (lambda (time activep &optional with-time-p hour-end minute-end)
  3145. (let ((ts (format-time-string
  3146. (funcall (if with-time-p 'cdr 'car)
  3147. org-time-stamp-formats)
  3148. time)))
  3149. (when (and hour-end minute-end)
  3150. (string-match "[012]?[0-9]:[0-5][0-9]" ts)
  3151. (setq ts
  3152. (replace-match
  3153. (format "\\&-%02d:%02d" hour-end minute-end)
  3154. nil nil ts)))
  3155. (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
  3156. (dolist (s (list repeat-string warning-string))
  3157. (when (org-string-nw-p s)
  3158. (setq ts (concat (substring ts 0 -1)
  3159. " "
  3160. s
  3161. (substring ts -1)))))
  3162. ;; Return value.
  3163. ts)))
  3164. (type (org-element-property :type timestamp)))
  3165. (case type
  3166. ((active inactive)
  3167. (let* ((minute-start (org-element-property :minute-start timestamp))
  3168. (minute-end (org-element-property :minute-end timestamp))
  3169. (hour-start (org-element-property :hour-start timestamp))
  3170. (hour-end (org-element-property :hour-end timestamp))
  3171. (time-range-p (and hour-start hour-end minute-start minute-end
  3172. (or (/= hour-start hour-end)
  3173. (/= minute-start minute-end)))))
  3174. (funcall
  3175. build-ts-string
  3176. (encode-time 0
  3177. (or minute-start 0)
  3178. (or hour-start 0)
  3179. (org-element-property :day-start timestamp)
  3180. (org-element-property :month-start timestamp)
  3181. (org-element-property :year-start timestamp))
  3182. (eq type 'active)
  3183. (and hour-start minute-start)
  3184. (and time-range-p hour-end)
  3185. (and time-range-p minute-end))))
  3186. ((active-range inactive-range)
  3187. (let ((minute-start (org-element-property :minute-start timestamp))
  3188. (minute-end (org-element-property :minute-end timestamp))
  3189. (hour-start (org-element-property :hour-start timestamp))
  3190. (hour-end (org-element-property :hour-end timestamp)))
  3191. (concat
  3192. (funcall
  3193. build-ts-string (encode-time
  3194. 0
  3195. (or minute-start 0)
  3196. (or hour-start 0)
  3197. (org-element-property :day-start timestamp)
  3198. (org-element-property :month-start timestamp)
  3199. (org-element-property :year-start timestamp))
  3200. (eq type 'active-range)
  3201. (and hour-start minute-start))
  3202. "--"
  3203. (funcall build-ts-string
  3204. (encode-time 0
  3205. (or minute-end 0)
  3206. (or hour-end 0)
  3207. (org-element-property :day-end timestamp)
  3208. (org-element-property :month-end timestamp)
  3209. (org-element-property :year-end timestamp))
  3210. (eq type 'active-range)
  3211. (and hour-end minute-end)))))
  3212. (otherwise (org-element-property :raw-value timestamp)))))
  3213. ;;;; Underline
  3214. (defun org-element-underline-parser ()
  3215. "Parse underline object at point, if any.
  3216. When at an underline object, return a list whose car is
  3217. `underline' and cdr is a plist with `:begin', `:end',
  3218. `:contents-begin' and `:contents-end' and `:post-blank' keywords.
  3219. Otherwise, return nil.
  3220. Assume point is at the first underscore marker."
  3221. (save-excursion
  3222. (unless (bolp) (backward-char 1))
  3223. (when (looking-at org-emph-re)
  3224. (let ((begin (match-beginning 2))
  3225. (contents-begin (match-beginning 4))
  3226. (contents-end (match-end 4))
  3227. (post-blank (progn (goto-char (match-end 2))
  3228. (skip-chars-forward " \t")))
  3229. (end (point)))
  3230. (list 'underline
  3231. (list :begin begin
  3232. :end end
  3233. :contents-begin contents-begin
  3234. :contents-end contents-end
  3235. :post-blank post-blank))))))
  3236. (defun org-element-underline-interpreter (underline contents)
  3237. "Interpret UNDERLINE object as Org syntax.
  3238. CONTENTS is the contents of the object."
  3239. (format "_%s_" contents))
  3240. ;;;; Verbatim
  3241. (defun org-element-verbatim-parser ()
  3242. "Parse verbatim object at point, if any.
  3243. When at a verbatim object, return a list whose car is `verbatim'
  3244. and cdr is a plist with `:value', `:begin', `:end' and
  3245. `:post-blank' keywords. Otherwise, return nil.
  3246. Assume point is at the first equal sign marker."
  3247. (save-excursion
  3248. (unless (bolp) (backward-char 1))
  3249. (when (looking-at org-emph-re)
  3250. (let ((begin (match-beginning 2))
  3251. (value (org-match-string-no-properties 4))
  3252. (post-blank (progn (goto-char (match-end 2))
  3253. (skip-chars-forward " \t")))
  3254. (end (point)))
  3255. (list 'verbatim
  3256. (list :value value
  3257. :begin begin
  3258. :end end
  3259. :post-blank post-blank))))))
  3260. (defun org-element-verbatim-interpreter (verbatim contents)
  3261. "Interpret VERBATIM object as Org syntax.
  3262. CONTENTS is nil."
  3263. (format "=%s=" (org-element-property :value verbatim)))
  3264. ;;; Parsing Element Starting At Point
  3265. ;;
  3266. ;; `org-element--current-element' is the core function of this section.
  3267. ;; It returns the Lisp representation of the element starting at
  3268. ;; point.
  3269. ;;
  3270. ;; `org-element--current-element' makes use of special modes. They
  3271. ;; are activated for fixed element chaining (e.g., `plain-list' >
  3272. ;; `item') or fixed conditional element chaining (e.g., `headline' >
  3273. ;; `section'). Special modes are: `first-section', `item',
  3274. ;; `node-property', `section' and `table-row'.
  3275. (defun org-element--current-element (limit &optional granularity mode structure)
  3276. "Parse the element starting at point.
  3277. Return value is a list like (TYPE PROPS) where TYPE is the type
  3278. of the element and PROPS a plist of properties associated to the
  3279. element.
  3280. Possible types are defined in `org-element-all-elements'.
  3281. LIMIT bounds the search.
  3282. Optional argument GRANULARITY determines the depth of the
  3283. recursion. Allowed values are `headline', `greater-element',
  3284. `element', `object' or nil. When it is broader than `object' (or
  3285. nil), secondary values will not be parsed, since they only
  3286. contain objects.
  3287. Optional argument MODE, when non-nil, can be either
  3288. `first-section', `section', `planning', `item', `node-property'
  3289. and `table-row'.
  3290. If STRUCTURE isn't provided but MODE is set to `item', it will be
  3291. computed.
  3292. This function assumes point is always at the beginning of the
  3293. element it has to parse."
  3294. (save-excursion
  3295. (let ((case-fold-search t)
  3296. ;; Determine if parsing depth allows for secondary strings
  3297. ;; parsing. It only applies to elements referenced in
  3298. ;; `org-element-secondary-value-alist'.
  3299. (raw-secondary-p (and granularity (not (eq granularity 'object)))))
  3300. (cond
  3301. ;; Item.
  3302. ((eq mode 'item)
  3303. (org-element-item-parser limit structure raw-secondary-p))
  3304. ;; Table Row.
  3305. ((eq mode 'table-row) (org-element-table-row-parser limit))
  3306. ;; Node Property.
  3307. ((eq mode 'node-property) (org-element-node-property-parser limit))
  3308. ;; Headline.
  3309. ((org-with-limited-levels (org-at-heading-p))
  3310. (org-element-headline-parser limit raw-secondary-p))
  3311. ;; Sections (must be checked after headline).
  3312. ((eq mode 'section) (org-element-section-parser limit))
  3313. ((eq mode 'first-section)
  3314. (org-element-section-parser
  3315. (or (save-excursion (org-with-limited-levels (outline-next-heading)))
  3316. limit)))
  3317. ;; Planning.
  3318. ((and (eq mode 'planning) (looking-at org-planning-line-re))
  3319. (org-element-planning-parser limit))
  3320. ;; Property drawer.
  3321. ((and (memq mode '(planning property-drawer))
  3322. (looking-at org-property-drawer-re))
  3323. (org-element-property-drawer-parser limit))
  3324. ;; When not at bol, point is at the beginning of an item or
  3325. ;; a footnote definition: next item is always a paragraph.
  3326. ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
  3327. ;; Clock.
  3328. ((looking-at org-clock-line-re) (org-element-clock-parser limit))
  3329. ;; Inlinetask.
  3330. ((org-at-heading-p)
  3331. (org-element-inlinetask-parser limit raw-secondary-p))
  3332. ;; From there, elements can have affiliated keywords.
  3333. (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
  3334. (cond
  3335. ;; Jumping over affiliated keywords put point off-limits.
  3336. ;; Parse them as regular keywords.
  3337. ((and (cdr affiliated) (>= (point) limit))
  3338. (goto-char (car affiliated))
  3339. (org-element-keyword-parser limit nil))
  3340. ;; LaTeX Environment.
  3341. ((looking-at org-element--latex-begin-environment)
  3342. (org-element-latex-environment-parser limit affiliated))
  3343. ;; Drawer and Property Drawer.
  3344. ((looking-at org-drawer-regexp)
  3345. (org-element-drawer-parser limit affiliated))
  3346. ;; Fixed Width
  3347. ((looking-at "[ \t]*:\\( \\|$\\)")
  3348. (org-element-fixed-width-parser limit affiliated))
  3349. ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
  3350. ;; Keywords.
  3351. ((looking-at "[ \t]*#")
  3352. (goto-char (match-end 0))
  3353. (cond ((looking-at "\\(?: \\|$\\)")
  3354. (beginning-of-line)
  3355. (org-element-comment-parser limit affiliated))
  3356. ((looking-at "\\+BEGIN_\\(\\S-+\\)")
  3357. (beginning-of-line)
  3358. (let ((parser (assoc (upcase (match-string 1))
  3359. org-element-block-name-alist)))
  3360. (if parser (funcall (cdr parser) limit affiliated)
  3361. (org-element-special-block-parser limit affiliated))))
  3362. ((looking-at "\\+CALL:")
  3363. (beginning-of-line)
  3364. (org-element-babel-call-parser limit affiliated))
  3365. ((looking-at "\\+BEGIN:? ")
  3366. (beginning-of-line)
  3367. (org-element-dynamic-block-parser limit affiliated))
  3368. ((looking-at "\\+\\S-+:")
  3369. (beginning-of-line)
  3370. (org-element-keyword-parser limit affiliated))
  3371. (t
  3372. (beginning-of-line)
  3373. (org-element-paragraph-parser limit affiliated))))
  3374. ;; Footnote Definition.
  3375. ((looking-at org-footnote-definition-re)
  3376. (org-element-footnote-definition-parser limit affiliated))
  3377. ;; Horizontal Rule.
  3378. ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
  3379. (org-element-horizontal-rule-parser limit affiliated))
  3380. ;; Diary Sexp.
  3381. ((looking-at "%%(")
  3382. (org-element-diary-sexp-parser limit affiliated))
  3383. ;; Table.
  3384. ((org-at-table-p t) (org-element-table-parser limit affiliated))
  3385. ;; List.
  3386. ((looking-at (org-item-re))
  3387. (org-element-plain-list-parser
  3388. limit affiliated
  3389. (or structure (org-element--list-struct limit))))
  3390. ;; Default element: Paragraph.
  3391. (t (org-element-paragraph-parser limit affiliated)))))))))
  3392. ;; Most elements can have affiliated keywords. When looking for an
  3393. ;; element beginning, we want to move before them, as they belong to
  3394. ;; that element, and, in the meantime, collect information they give
  3395. ;; into appropriate properties. Hence the following function.
  3396. (defun org-element--collect-affiliated-keywords (limit)
  3397. "Collect affiliated keywords from point down to LIMIT.
  3398. Return a list whose CAR is the position at the first of them and
  3399. CDR a plist of keywords and values and move point to the
  3400. beginning of the first line after them.
  3401. As a special case, if element doesn't start at the beginning of
  3402. the line (e.g., a paragraph starting an item), CAR is current
  3403. position of point and CDR is nil."
  3404. (if (not (bolp)) (list (point))
  3405. (let ((case-fold-search t)
  3406. (origin (point))
  3407. ;; RESTRICT is the list of objects allowed in parsed
  3408. ;; keywords value.
  3409. (restrict (org-element-restriction 'keyword))
  3410. output)
  3411. (while (and (< (point) limit) (looking-at org-element--affiliated-re))
  3412. (let* ((raw-kwd (upcase (match-string 1)))
  3413. ;; Apply translation to RAW-KWD. From there, KWD is
  3414. ;; the official keyword.
  3415. (kwd (or (cdr (assoc raw-kwd
  3416. org-element-keyword-translation-alist))
  3417. raw-kwd))
  3418. ;; Find main value for any keyword.
  3419. (value
  3420. (save-match-data
  3421. (org-trim
  3422. (buffer-substring-no-properties
  3423. (match-end 0) (line-end-position)))))
  3424. ;; PARSEDP is non-nil when keyword should have its
  3425. ;; value parsed.
  3426. (parsedp (member kwd org-element-parsed-keywords))
  3427. ;; If KWD is a dual keyword, find its secondary
  3428. ;; value. Maybe parse it.
  3429. (dualp (member kwd org-element-dual-keywords))
  3430. (dual-value
  3431. (and dualp
  3432. (let ((sec (org-match-string-no-properties 2)))
  3433. (if (or (not sec) (not parsedp)) sec
  3434. (org-element--parse-objects
  3435. (match-beginning 2) (match-end 2) nil restrict)))))
  3436. ;; Attribute a property name to KWD.
  3437. (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
  3438. ;; Now set final shape for VALUE.
  3439. (when parsedp
  3440. (setq value
  3441. (org-element--parse-objects
  3442. (match-end 0)
  3443. (progn (end-of-line) (skip-chars-backward " \t") (point))
  3444. nil restrict)))
  3445. (when dualp
  3446. (setq value (and (or value dual-value) (cons value dual-value))))
  3447. (when (or (member kwd org-element-multiple-keywords)
  3448. ;; Attributes can always appear on multiple lines.
  3449. (string-match "^ATTR_" kwd))
  3450. (setq value (cons value (plist-get output kwd-sym))))
  3451. ;; Eventually store the new value in OUTPUT.
  3452. (setq output (plist-put output kwd-sym value))
  3453. ;; Move to next keyword.
  3454. (forward-line)))
  3455. ;; If affiliated keywords are orphaned: move back to first one.
  3456. ;; They will be parsed as a paragraph.
  3457. (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
  3458. ;; Return value.
  3459. (cons origin output))))
  3460. ;;; The Org Parser
  3461. ;;
  3462. ;; The two major functions here are `org-element-parse-buffer', which
  3463. ;; parses Org syntax inside the current buffer, taking into account
  3464. ;; region, narrowing, or even visibility if specified, and
  3465. ;; `org-element-parse-secondary-string', which parses objects within
  3466. ;; a given string.
  3467. ;;
  3468. ;; The (almost) almighty `org-element-map' allows to apply a function
  3469. ;; on elements or objects matching some type, and accumulate the
  3470. ;; resulting values. In an export situation, it also skips unneeded
  3471. ;; parts of the parse tree.
  3472. (defun org-element-parse-buffer (&optional granularity visible-only)
  3473. "Recursively parse the buffer and return structure.
  3474. If narrowing is in effect, only parse the visible part of the
  3475. buffer.
  3476. Optional argument GRANULARITY determines the depth of the
  3477. recursion. It can be set to the following symbols:
  3478. `headline' Only parse headlines.
  3479. `greater-element' Don't recurse into greater elements excepted
  3480. headlines and sections. Thus, elements
  3481. parsed are the top-level ones.
  3482. `element' Parse everything but objects and plain text.
  3483. `object' Parse the complete buffer (default).
  3484. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3485. elements.
  3486. An element or an objects is represented as a list with the
  3487. pattern (TYPE PROPERTIES CONTENTS), where :
  3488. TYPE is a symbol describing the element or object. See
  3489. `org-element-all-elements' and `org-element-all-objects' for an
  3490. exhaustive list of such symbols. One can retrieve it with
  3491. `org-element-type' function.
  3492. PROPERTIES is the list of attributes attached to the element or
  3493. object, as a plist. Although most of them are specific to the
  3494. element or object type, all types share `:begin', `:end',
  3495. `:post-blank' and `:parent' properties, which respectively
  3496. refer to buffer position where the element or object starts,
  3497. ends, the number of white spaces or blank lines after it, and
  3498. the element or object containing it. Properties values can be
  3499. obtained by using `org-element-property' function.
  3500. CONTENTS is a list of elements, objects or raw strings
  3501. contained in the current element or object, when applicable.
  3502. One can access them with `org-element-contents' function.
  3503. The Org buffer has `org-data' as type and nil as properties.
  3504. `org-element-map' function can be used to find specific elements
  3505. or objects within the parse tree.
  3506. This function assumes that current major mode is `org-mode'."
  3507. (save-excursion
  3508. (goto-char (point-min))
  3509. (org-skip-whitespace)
  3510. (org-element--parse-elements
  3511. (point-at-bol) (point-max)
  3512. ;; Start in `first-section' mode so text before the first
  3513. ;; headline belongs to a section.
  3514. 'first-section nil granularity visible-only (list 'org-data nil))))
  3515. (defun org-element-parse-secondary-string (string restriction &optional parent)
  3516. "Recursively parse objects in STRING and return structure.
  3517. RESTRICTION is a symbol limiting the object types that will be
  3518. looked after.
  3519. Optional argument PARENT, when non-nil, is the element or object
  3520. containing the secondary string. It is used to set correctly
  3521. `:parent' property within the string.
  3522. If STRING is the empty string or nil, return nil."
  3523. (cond
  3524. ((not string) nil)
  3525. ((equal string "") nil)
  3526. (t (let ((local-variables (buffer-local-variables)))
  3527. (with-temp-buffer
  3528. (dolist (v local-variables)
  3529. (ignore-errors
  3530. (if (symbolp v) (makunbound v)
  3531. (org-set-local (car v) (cdr v)))))
  3532. (insert string)
  3533. (restore-buffer-modified-p nil)
  3534. (let ((data (org-element--parse-objects
  3535. (point-min) (point-max) nil restriction)))
  3536. (when parent
  3537. (dolist (o data) (org-element-put-property o :parent parent)))
  3538. data))))))
  3539. (defun org-element-map
  3540. (data types fun &optional info first-match no-recursion with-affiliated)
  3541. "Map a function on selected elements or objects.
  3542. DATA is a parse tree, an element, an object, a string, or a list
  3543. of such constructs. TYPES is a symbol or list of symbols of
  3544. elements or objects types (see `org-element-all-elements' and
  3545. `org-element-all-objects' for a complete list of types). FUN is
  3546. the function called on the matching element or object. It has to
  3547. accept one argument: the element or object itself.
  3548. When optional argument INFO is non-nil, it should be a plist
  3549. holding export options. In that case, parts of the parse tree
  3550. not exportable according to that property list will be skipped.
  3551. When optional argument FIRST-MATCH is non-nil, stop at the first
  3552. match for which FUN doesn't return nil, and return that value.
  3553. Optional argument NO-RECURSION is a symbol or a list of symbols
  3554. representing elements or objects types. `org-element-map' won't
  3555. enter any recursive element or object whose type belongs to that
  3556. list. Though, FUN can still be applied on them.
  3557. When optional argument WITH-AFFILIATED is non-nil, FUN will also
  3558. apply to matching objects within parsed affiliated keywords (see
  3559. `org-element-parsed-keywords').
  3560. Nil values returned from FUN do not appear in the results.
  3561. Examples:
  3562. ---------
  3563. Assuming TREE is a variable containing an Org buffer parse tree,
  3564. the following example will return a flat list of all `src-block'
  3565. and `example-block' elements in it:
  3566. \(org-element-map tree '(example-block src-block) #'identity)
  3567. The following snippet will find the first headline with a level
  3568. of 1 and a \"phone\" tag, and will return its beginning position:
  3569. \(org-element-map tree 'headline
  3570. \(lambda (hl)
  3571. \(and (= (org-element-property :level hl) 1)
  3572. \(member \"phone\" (org-element-property :tags hl))
  3573. \(org-element-property :begin hl)))
  3574. nil t)
  3575. The next example will return a flat list of all `plain-list' type
  3576. elements in TREE that are not a sub-list themselves:
  3577. \(org-element-map tree 'plain-list #'identity nil nil 'plain-list)
  3578. Eventually, this example will return a flat list of all `bold'
  3579. type objects containing a `latex-snippet' type object, even
  3580. looking into captions:
  3581. \(org-element-map tree 'bold
  3582. \(lambda (b)
  3583. \(and (org-element-map b 'latex-snippet #'identity nil t) b))
  3584. nil nil nil t)"
  3585. ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
  3586. (let* ((types (if (listp types) types (list types)))
  3587. (no-recursion (if (listp no-recursion) no-recursion
  3588. (list no-recursion)))
  3589. ;; Recursion depth is determined by --CATEGORY.
  3590. (--category
  3591. (catch 'found
  3592. (let ((category 'greater-elements)
  3593. (all-objects (cons 'plain-text org-element-all-objects)))
  3594. (dolist (type types category)
  3595. (cond ((memq type all-objects)
  3596. ;; If one object is found, the function has to
  3597. ;; recurse into every object.
  3598. (throw 'found 'objects))
  3599. ((not (memq type org-element-greater-elements))
  3600. ;; If one regular element is found, the
  3601. ;; function has to recurse, at least, into
  3602. ;; every element it encounters.
  3603. (and (not (eq category 'elements))
  3604. (setq category 'elements))))))))
  3605. --acc
  3606. --walk-tree
  3607. (--walk-tree
  3608. (lambda (--data)
  3609. ;; Recursively walk DATA. INFO, if non-nil, is a plist
  3610. ;; holding contextual information.
  3611. (let ((--type (org-element-type --data)))
  3612. (cond
  3613. ((not --data))
  3614. ;; Ignored element in an export context.
  3615. ((and info (memq --data (plist-get info :ignore-list))))
  3616. ;; List of elements or objects.
  3617. ((not --type) (mapc --walk-tree --data))
  3618. ;; Unconditionally enter parse trees.
  3619. ((eq --type 'org-data)
  3620. (mapc --walk-tree (org-element-contents --data)))
  3621. (t
  3622. ;; Check if TYPE is matching among TYPES. If so,
  3623. ;; apply FUN to --DATA and accumulate return value
  3624. ;; into --ACC (or exit if FIRST-MATCH is non-nil).
  3625. (when (memq --type types)
  3626. (let ((result (funcall fun --data)))
  3627. (cond ((not result))
  3628. (first-match (throw '--map-first-match result))
  3629. (t (push result --acc)))))
  3630. ;; If --DATA has a secondary string that can contain
  3631. ;; objects with their type among TYPES, look into it.
  3632. (when (and (eq --category 'objects) (not (stringp --data)))
  3633. (dolist (p (cdr (assq --type
  3634. org-element-secondary-value-alist)))
  3635. (funcall --walk-tree (org-element-property p --data))))
  3636. ;; If --DATA has any parsed affiliated keywords and
  3637. ;; WITH-AFFILIATED is non-nil, look for objects in
  3638. ;; them.
  3639. (when (and with-affiliated
  3640. (eq --category 'objects)
  3641. (memq --type org-element-all-elements))
  3642. (dolist (kwd-pair org-element--parsed-properties-alist)
  3643. (let ((kwd (car kwd-pair))
  3644. (value (org-element-property (cdr kwd-pair) --data)))
  3645. ;; Pay attention to the type of parsed keyword.
  3646. ;; In particular, preserve order for multiple
  3647. ;; keywords.
  3648. (cond
  3649. ((not value))
  3650. ((member kwd org-element-dual-keywords)
  3651. (if (member kwd org-element-multiple-keywords)
  3652. (dolist (line (reverse value))
  3653. (funcall --walk-tree (cdr line))
  3654. (funcall --walk-tree (car line)))
  3655. (funcall --walk-tree (cdr value))
  3656. (funcall --walk-tree (car value))))
  3657. ((member kwd org-element-multiple-keywords)
  3658. (mapc --walk-tree (reverse value)))
  3659. (t (funcall --walk-tree value))))))
  3660. ;; Determine if a recursion into --DATA is possible.
  3661. (cond
  3662. ;; --TYPE is explicitly removed from recursion.
  3663. ((memq --type no-recursion))
  3664. ;; --DATA has no contents.
  3665. ((not (org-element-contents --data)))
  3666. ;; Looking for greater elements but --DATA is simply
  3667. ;; an element or an object.
  3668. ((and (eq --category 'greater-elements)
  3669. (not (memq --type org-element-greater-elements))))
  3670. ;; Looking for elements but --DATA is an object.
  3671. ((and (eq --category 'elements)
  3672. (memq --type org-element-all-objects)))
  3673. ;; In any other case, map contents.
  3674. (t (mapc --walk-tree (org-element-contents --data))))))))))
  3675. (catch '--map-first-match
  3676. (funcall --walk-tree data)
  3677. ;; Return value in a proper order.
  3678. (nreverse --acc))))
  3679. (put 'org-element-map 'lisp-indent-function 2)
  3680. ;; The following functions are internal parts of the parser.
  3681. ;;
  3682. ;; The first one, `org-element--parse-elements' acts at the element's
  3683. ;; level.
  3684. ;;
  3685. ;; The second one, `org-element--parse-objects' applies on all objects
  3686. ;; of a paragraph or a secondary string. It calls
  3687. ;; `org-element--object-lex' to find the next object in the current
  3688. ;; container.
  3689. (defsubst org-element--next-mode (type parentp)
  3690. "Return next special mode according to TYPE, or nil.
  3691. TYPE is a symbol representing the type of an element or object
  3692. containing next element if PARENTP is non-nil, or before it
  3693. otherwise. Modes can be either `first-section', `item',
  3694. `node-property', `planning', `property-drawer', `section',
  3695. `table-row' or nil."
  3696. (if parentp
  3697. (case type
  3698. (headline 'section)
  3699. (plain-list 'item)
  3700. (property-drawer 'node-property)
  3701. (section 'planning)
  3702. (table 'table-row))
  3703. (case type
  3704. (item 'item)
  3705. (node-property 'node-property)
  3706. (planning 'property-drawer)
  3707. (table-row 'table-row))))
  3708. (defun org-element--parse-elements
  3709. (beg end mode structure granularity visible-only acc)
  3710. "Parse elements between BEG and END positions.
  3711. MODE prioritizes some elements over the others. It can be set to
  3712. `first-section', `section', `planning', `item', `node-property'
  3713. or `table-row'.
  3714. When value is `item', STRUCTURE will be used as the current list
  3715. structure.
  3716. GRANULARITY determines the depth of the recursion. See
  3717. `org-element-parse-buffer' for more information.
  3718. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3719. elements.
  3720. Elements are accumulated into ACC."
  3721. (save-excursion
  3722. (goto-char beg)
  3723. ;; Visible only: skip invisible parts at the beginning of the
  3724. ;; element.
  3725. (when (and visible-only (org-invisible-p2))
  3726. (goto-char (min (1+ (org-find-visible)) end)))
  3727. ;; When parsing only headlines, skip any text before first one.
  3728. (when (and (eq granularity 'headline) (not (org-at-heading-p)))
  3729. (org-with-limited-levels (outline-next-heading)))
  3730. ;; Main loop start.
  3731. (while (< (point) end)
  3732. ;; Find current element's type and parse it accordingly to
  3733. ;; its category.
  3734. (let* ((element (org-element--current-element
  3735. end granularity mode structure))
  3736. (type (org-element-type element))
  3737. (cbeg (org-element-property :contents-begin element)))
  3738. (goto-char (org-element-property :end element))
  3739. ;; Visible only: skip invisible parts between siblings.
  3740. (when (and visible-only (org-invisible-p2))
  3741. (goto-char (min (1+ (org-find-visible)) end)))
  3742. ;; Fill ELEMENT contents by side-effect.
  3743. (cond
  3744. ;; If element has no contents, don't modify it.
  3745. ((not cbeg))
  3746. ;; Greater element: parse it between `contents-begin' and
  3747. ;; `contents-end'. Make sure GRANULARITY allows the
  3748. ;; recursion, or ELEMENT is a headline, in which case going
  3749. ;; inside is mandatory, in order to get sub-level headings.
  3750. ((and (memq type org-element-greater-elements)
  3751. (or (memq granularity '(element object nil))
  3752. (and (eq granularity 'greater-element)
  3753. (eq type 'section))
  3754. (eq type 'headline)))
  3755. (org-element--parse-elements
  3756. cbeg (org-element-property :contents-end element)
  3757. ;; Possibly switch to a special mode.
  3758. (org-element--next-mode type t)
  3759. (and (memq type '(item plain-list))
  3760. (org-element-property :structure element))
  3761. granularity visible-only element))
  3762. ;; ELEMENT has contents. Parse objects inside, if
  3763. ;; GRANULARITY allows it.
  3764. ((memq granularity '(object nil))
  3765. (org-element--parse-objects
  3766. cbeg (org-element-property :contents-end element) element
  3767. (org-element-restriction type))))
  3768. (org-element-adopt-elements acc element)
  3769. ;; Update mode.
  3770. (setq mode (org-element--next-mode type nil))))
  3771. ;; Return result.
  3772. acc))
  3773. (defun org-element--object-lex (restriction)
  3774. "Return next object in current buffer or nil.
  3775. RESTRICTION is a list of object types, as symbols, that should be
  3776. looked after. This function assumes that the buffer is narrowed
  3777. to an appropriate container (e.g., a paragraph)."
  3778. (if (memq 'table-cell restriction) (org-element-table-cell-parser)
  3779. (save-excursion
  3780. (let ((limit (and org-target-link-regexp
  3781. (save-excursion
  3782. (or (bolp) (backward-char))
  3783. (re-search-forward org-target-link-regexp nil t))
  3784. (match-beginning 1)))
  3785. found)
  3786. (while (and (not found)
  3787. (re-search-forward org-element--object-regexp limit t))
  3788. (goto-char (match-beginning 0))
  3789. (let ((result (match-string 0)))
  3790. (setq found
  3791. (cond
  3792. ((eq (compare-strings result nil nil "call_" nil nil t) t)
  3793. (and (memq 'inline-babel-call restriction)
  3794. (org-element-inline-babel-call-parser)))
  3795. ((eq (compare-strings result nil nil "src_" nil nil t) t)
  3796. (and (memq 'inline-src-block restriction)
  3797. (org-element-inline-src-block-parser)))
  3798. (t
  3799. (case (char-after)
  3800. (?^ (and (memq 'superscript restriction)
  3801. (org-element-superscript-parser)))
  3802. (?_ (or (and (memq 'subscript restriction)
  3803. (org-element-subscript-parser))
  3804. (and (memq 'underline restriction)
  3805. (org-element-underline-parser))))
  3806. (?* (and (memq 'bold restriction)
  3807. (org-element-bold-parser)))
  3808. (?/ (and (memq 'italic restriction)
  3809. (org-element-italic-parser)))
  3810. (?~ (and (memq 'code restriction)
  3811. (org-element-code-parser)))
  3812. (?= (and (memq 'verbatim restriction)
  3813. (org-element-verbatim-parser)))
  3814. (?+ (and (memq 'strike-through restriction)
  3815. (org-element-strike-through-parser)))
  3816. (?@ (and (memq 'export-snippet restriction)
  3817. (org-element-export-snippet-parser)))
  3818. (?{ (and (memq 'macro restriction)
  3819. (org-element-macro-parser)))
  3820. (?$ (and (memq 'latex-fragment restriction)
  3821. (org-element-latex-fragment-parser)))
  3822. (?<
  3823. (if (eq (aref result 1) ?<)
  3824. (or (and (memq 'radio-target restriction)
  3825. (org-element-radio-target-parser))
  3826. (and (memq 'target restriction)
  3827. (org-element-target-parser)))
  3828. (or (and (memq 'timestamp restriction)
  3829. (org-element-timestamp-parser))
  3830. (and (memq 'link restriction)
  3831. (org-element-link-parser)))))
  3832. (?\\
  3833. (if (eq (aref result 1) ?\\)
  3834. (and (memq 'line-break restriction)
  3835. (org-element-line-break-parser))
  3836. (or (and (memq 'entity restriction)
  3837. (org-element-entity-parser))
  3838. (and (memq 'latex-fragment restriction)
  3839. (org-element-latex-fragment-parser)))))
  3840. (?\[
  3841. (if (eq (aref result 1) ?\[)
  3842. (and (memq 'link restriction)
  3843. (org-element-link-parser))
  3844. (or (and (memq 'footnote-reference restriction)
  3845. (org-element-footnote-reference-parser))
  3846. (and (memq 'timestamp restriction)
  3847. (org-element-timestamp-parser))
  3848. (and (memq 'statistics-cookie restriction)
  3849. (org-element-statistics-cookie-parser)))))
  3850. ;; This is probably a plain link.
  3851. (otherwise (and (or (memq 'link restriction)
  3852. (memq 'plain-link restriction))
  3853. (org-element-link-parser)))))))
  3854. (or (eobp) (forward-char))))
  3855. (cond (found)
  3856. ;; Radio link.
  3857. ((and limit (memq 'link restriction))
  3858. (goto-char limit) (org-element-link-parser)))))))
  3859. (defun org-element--parse-objects (beg end acc restriction)
  3860. "Parse objects between BEG and END and return recursive structure.
  3861. Objects are accumulated in ACC.
  3862. RESTRICTION is a list of object successors which are allowed in
  3863. the current object."
  3864. (save-excursion
  3865. (save-restriction
  3866. (narrow-to-region beg end)
  3867. (goto-char (point-min))
  3868. (let (next-object)
  3869. (while (and (not (eobp))
  3870. (setq next-object (org-element--object-lex restriction)))
  3871. ;; 1. Text before any object. Untabify it.
  3872. (let ((obj-beg (org-element-property :begin next-object)))
  3873. (unless (= (point) obj-beg)
  3874. (setq acc
  3875. (org-element-adopt-elements
  3876. acc
  3877. (replace-regexp-in-string
  3878. "\t" (make-string tab-width ? )
  3879. (buffer-substring-no-properties (point) obj-beg))))))
  3880. ;; 2. Object...
  3881. (let ((obj-end (org-element-property :end next-object))
  3882. (cont-beg (org-element-property :contents-begin next-object)))
  3883. ;; Fill contents of NEXT-OBJECT by side-effect, if it has
  3884. ;; a recursive type.
  3885. (when (and cont-beg
  3886. (memq (car next-object) org-element-recursive-objects))
  3887. (org-element--parse-objects
  3888. cont-beg (org-element-property :contents-end next-object)
  3889. next-object (org-element-restriction next-object)))
  3890. (setq acc (org-element-adopt-elements acc next-object))
  3891. (goto-char obj-end))))
  3892. ;; 3. Text after last object. Untabify it.
  3893. (unless (eobp)
  3894. (setq acc
  3895. (org-element-adopt-elements
  3896. acc
  3897. (replace-regexp-in-string
  3898. "\t" (make-string tab-width ? )
  3899. (buffer-substring-no-properties (point) end)))))
  3900. ;; Result.
  3901. acc)))
  3902. ;;; Towards A Bijective Process
  3903. ;;
  3904. ;; The parse tree obtained with `org-element-parse-buffer' is really
  3905. ;; a snapshot of the corresponding Org buffer. Therefore, it can be
  3906. ;; interpreted and expanded into a string with canonical Org syntax.
  3907. ;; Hence `org-element-interpret-data'.
  3908. ;;
  3909. ;; The function relies internally on
  3910. ;; `org-element--interpret-affiliated-keywords'.
  3911. ;;;###autoload
  3912. (defun org-element-interpret-data (data)
  3913. "Interpret DATA as Org syntax.
  3914. DATA is a parse tree, an element, an object or a secondary string
  3915. to interpret. Return Org syntax as a string."
  3916. (org-element--interpret-data-1 data nil))
  3917. (defun org-element--interpret-data-1 (data parent)
  3918. "Interpret DATA as Org syntax.
  3919. DATA is a parse tree, an element, an object or a secondary string
  3920. to interpret. PARENT is used for recursive calls. It contains
  3921. the element or object containing data, or nil.
  3922. Return Org syntax as a string."
  3923. (let* ((type (org-element-type data))
  3924. ;; Find interpreter for current object or element. If it
  3925. ;; doesn't exist (e.g. this is a pseudo object or element),
  3926. ;; return contents, if any.
  3927. (interpret
  3928. (let ((fun (intern (format "org-element-%s-interpreter" type))))
  3929. (if (fboundp fun) fun (lambda (data contents) contents))))
  3930. (results
  3931. (cond
  3932. ;; Secondary string.
  3933. ((not type)
  3934. (mapconcat
  3935. (lambda (obj) (org-element--interpret-data-1 obj parent)) data ""))
  3936. ;; Full Org document.
  3937. ((eq type 'org-data)
  3938. (mapconcat (lambda (obj) (org-element--interpret-data-1 obj parent))
  3939. (org-element-contents data) ""))
  3940. ;; Plain text: return it.
  3941. ((stringp data) data)
  3942. ;; Element or object without contents.
  3943. ((not (org-element-contents data)) (funcall interpret data nil))
  3944. ;; Element or object with contents.
  3945. (t
  3946. (funcall interpret data
  3947. ;; Recursively interpret contents.
  3948. (mapconcat
  3949. (lambda (obj) (org-element--interpret-data-1 obj data))
  3950. (org-element-contents
  3951. (if (not (memq type '(paragraph verse-block)))
  3952. data
  3953. ;; Fix indentation of elements containing
  3954. ;; objects. We ignore `table-row' elements
  3955. ;; as they are one line long anyway.
  3956. (org-element-normalize-contents
  3957. data
  3958. ;; When normalizing first paragraph of an
  3959. ;; item or a footnote-definition, ignore
  3960. ;; first line's indentation.
  3961. (and (eq type 'paragraph)
  3962. (equal data (car (org-element-contents parent)))
  3963. (memq (org-element-type parent)
  3964. '(footnote-definition item))))))
  3965. ""))))))
  3966. (if (memq type '(org-data plain-text nil)) results
  3967. ;; Build white spaces. If no `:post-blank' property is
  3968. ;; specified, assume its value is 0.
  3969. (let ((post-blank (or (org-element-property :post-blank data) 0)))
  3970. (if (or (memq type org-element-all-objects)
  3971. (and parent
  3972. (let ((type (org-element-type parent)))
  3973. (or (not type)
  3974. (memq type org-element-object-containers)))))
  3975. (concat results (make-string post-blank ?\s))
  3976. (concat
  3977. (org-element--interpret-affiliated-keywords data)
  3978. (org-element-normalize-string results)
  3979. (make-string post-blank ?\n)))))))
  3980. (defun org-element--interpret-affiliated-keywords (element)
  3981. "Return ELEMENT's affiliated keywords as Org syntax.
  3982. If there is no affiliated keyword, return the empty string."
  3983. (let ((keyword-to-org
  3984. (function
  3985. (lambda (key value)
  3986. (let (dual)
  3987. (when (member key org-element-dual-keywords)
  3988. (setq dual (cdr value) value (car value)))
  3989. (concat "#+" key
  3990. (and dual
  3991. (format "[%s]" (org-element-interpret-data dual)))
  3992. ": "
  3993. (if (member key org-element-parsed-keywords)
  3994. (org-element-interpret-data value)
  3995. value)
  3996. "\n"))))))
  3997. (mapconcat
  3998. (lambda (prop)
  3999. (let ((value (org-element-property prop element))
  4000. (keyword (upcase (substring (symbol-name prop) 1))))
  4001. (when value
  4002. (if (or (member keyword org-element-multiple-keywords)
  4003. ;; All attribute keywords can have multiple lines.
  4004. (string-match "^ATTR_" keyword))
  4005. (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
  4006. (reverse value)
  4007. "")
  4008. (funcall keyword-to-org keyword value)))))
  4009. ;; List all ELEMENT's properties matching an attribute line or an
  4010. ;; affiliated keyword, but ignore translated keywords since they
  4011. ;; cannot belong to the property list.
  4012. (loop for prop in (nth 1 element) by 'cddr
  4013. when (let ((keyword (upcase (substring (symbol-name prop) 1))))
  4014. (or (string-match "^ATTR_" keyword)
  4015. (and
  4016. (member keyword org-element-affiliated-keywords)
  4017. (not (assoc keyword
  4018. org-element-keyword-translation-alist)))))
  4019. collect prop)
  4020. "")))
  4021. ;; Because interpretation of the parse tree must return the same
  4022. ;; number of blank lines between elements and the same number of white
  4023. ;; space after objects, some special care must be given to white
  4024. ;; spaces.
  4025. ;;
  4026. ;; The first function, `org-element-normalize-string', ensures any
  4027. ;; string different from the empty string will end with a single
  4028. ;; newline character.
  4029. ;;
  4030. ;; The second function, `org-element-normalize-contents', removes
  4031. ;; global indentation from the contents of the current element.
  4032. (defun org-element-normalize-string (s)
  4033. "Ensure string S ends with a single newline character.
  4034. If S isn't a string return it unchanged. If S is the empty
  4035. string, return it. Otherwise, return a new string with a single
  4036. newline character at its end."
  4037. (cond
  4038. ((not (stringp s)) s)
  4039. ((string= "" s) "")
  4040. (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
  4041. (replace-match "\n" nil nil s)))))
  4042. (defun org-element-normalize-contents (element &optional ignore-first)
  4043. "Normalize plain text in ELEMENT's contents.
  4044. ELEMENT must only contain plain text and objects.
  4045. If optional argument IGNORE-FIRST is non-nil, ignore first line's
  4046. indentation to compute maximal common indentation.
  4047. Return the normalized element that is element with global
  4048. indentation removed from its contents. The function assumes that
  4049. indentation is not done with TAB characters."
  4050. (let* ((min-ind most-positive-fixnum)
  4051. find-min-ind ; For byte-compiler.
  4052. (find-min-ind
  4053. ;; Return minimal common indentation within BLOB. This is
  4054. ;; done by walking recursively BLOB and updating MIN-IND
  4055. ;; along the way. FIRST-FLAG is non-nil when the next
  4056. ;; object is expected to be a string that doesn't start with
  4057. ;; a newline character. It happens for strings at the
  4058. ;; beginnings of the contents or right after a line break.
  4059. (lambda (blob first-flag)
  4060. (dolist (object (org-element-contents blob))
  4061. (when first-flag
  4062. (setq first-flag nil)
  4063. ;; Objects cannot start with spaces: in this case,
  4064. ;; indentation is 0.
  4065. (if (not (stringp object)) (throw 'zero (setq min-ind 0))
  4066. (string-match "\\` *" object)
  4067. (let ((len (match-end 0)))
  4068. ;; An indentation of zero means no string will be
  4069. ;; modified. Quit the process.
  4070. (if (zerop len) (throw 'zero (setq min-ind 0))
  4071. (setq min-ind (min len min-ind))))))
  4072. (cond
  4073. ((stringp object)
  4074. (dolist (line (cdr (org-split-string object " *\n")))
  4075. (unless (string= line "")
  4076. (setq min-ind (min (org-get-indentation line) min-ind)))))
  4077. ((eq (org-element-type object) 'line-break) (setq first-flag t))
  4078. ((memq (org-element-type object) org-element-recursive-objects)
  4079. (funcall find-min-ind object first-flag)))))))
  4080. ;; Find minimal indentation in ELEMENT.
  4081. (catch 'zero (funcall find-min-ind element (not ignore-first)))
  4082. (if (or (zerop min-ind) (= min-ind most-positive-fixnum)) element
  4083. ;; Build ELEMENT back, replacing each string with the same
  4084. ;; string minus common indentation.
  4085. (let* (build ; For byte compiler.
  4086. (build
  4087. (lambda (blob first-flag)
  4088. ;; Return BLOB with all its strings indentation
  4089. ;; shortened from MIN-IND white spaces. FIRST-FLAG is
  4090. ;; non-nil when the next object is expected to be
  4091. ;; a string that doesn't start with a newline
  4092. ;; character.
  4093. (setcdr (cdr blob)
  4094. (mapcar
  4095. (lambda (object)
  4096. (when first-flag
  4097. (setq first-flag nil)
  4098. (when (stringp object)
  4099. (setq object
  4100. (replace-regexp-in-string
  4101. (format "\\` \\{%d\\}" min-ind)
  4102. "" object))))
  4103. (cond
  4104. ((stringp object)
  4105. (replace-regexp-in-string
  4106. (format "\n \\{%d\\}" min-ind) "\n" object))
  4107. ((memq (org-element-type object)
  4108. org-element-recursive-objects)
  4109. (funcall build object first-flag))
  4110. ((eq (org-element-type object) 'line-break)
  4111. (setq first-flag t)
  4112. object)
  4113. (t object)))
  4114. (org-element-contents blob)))
  4115. blob)))
  4116. (funcall build element (not ignore-first))))))
  4117. ;;; Cache
  4118. ;;
  4119. ;; Implement a caching mechanism for `org-element-at-point' and
  4120. ;; `org-element-context', which see.
  4121. ;;
  4122. ;; A single public function is provided: `org-element-cache-reset'.
  4123. ;;
  4124. ;; Cache is enabled by default, but can be disabled globally with
  4125. ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
  4126. ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
  4127. ;; can be tweaked to control caching behaviour.
  4128. ;;
  4129. ;; Internally, parsed elements are stored in an AVL tree,
  4130. ;; `org-element--cache'. This tree is updated lazily: whenever
  4131. ;; a change happens to the buffer, a synchronization request is
  4132. ;; registered in `org-element--cache-sync-requests' (see
  4133. ;; `org-element--cache-submit-request'). During idle time, requests
  4134. ;; are processed by `org-element--cache-sync'. Synchronization also
  4135. ;; happens when an element is required from the cache. In this case,
  4136. ;; the process stops as soon as the needed element is up-to-date.
  4137. ;;
  4138. ;; A synchronization request can only apply on a synchronized part of
  4139. ;; the cache. Therefore, the cache is updated at least to the
  4140. ;; location where the new request applies. Thus, requests are ordered
  4141. ;; from left to right and all elements starting before the first
  4142. ;; request are correct. This property is used by functions like
  4143. ;; `org-element--cache-find' to retrieve elements in the part of the
  4144. ;; cache that can be trusted.
  4145. ;;
  4146. ;; A request applies to every element, starting from its original
  4147. ;; location (or key, see below). When a request is processed, it
  4148. ;; moves forward and may collide the next one. In this case, both
  4149. ;; requests are merged into a new one that starts from that element.
  4150. ;; As a consequence, the whole synchronization complexity does not
  4151. ;; depend on the number of pending requests, but on the number of
  4152. ;; elements the very first request will be applied on.
  4153. ;;
  4154. ;; Elements cannot be accessed through their beginning position, which
  4155. ;; may or may not be up-to-date. Instead, each element in the tree is
  4156. ;; associated to a key, obtained with `org-element--cache-key'. This
  4157. ;; mechanism is robust enough to preserve total order among elements
  4158. ;; even when the tree is only partially synchronized.
  4159. ;;
  4160. ;; Objects contained in an element are stored in a hash table,
  4161. ;; `org-element--cache-objects'.
  4162. (defvar org-element-use-cache t
  4163. "Non nil when Org parser should cache its results.
  4164. This is mostly for debugging purpose.")
  4165. (defvar org-element-cache-sync-idle-time 0.6
  4166. "Length, in seconds, of idle time before syncing cache.")
  4167. (defvar org-element-cache-sync-duration (seconds-to-time 0.04)
  4168. "Maximum duration, as a time value, for a cache synchronization.
  4169. If the synchronization is not over after this delay, the process
  4170. pauses and resumes after `org-element-cache-sync-break'
  4171. seconds.")
  4172. (defvar org-element-cache-sync-break (seconds-to-time 0.3)
  4173. "Duration, as a time value, of the pause between synchronizations.
  4174. See `org-element-cache-sync-duration' for more information.")
  4175. ;;;; Data Structure
  4176. (defvar org-element--cache nil
  4177. "AVL tree used to cache elements.
  4178. Each node of the tree contains an element. Comparison is done
  4179. with `org-element--cache-compare'. This cache is used in
  4180. `org-element-at-point'.")
  4181. (defvar org-element--cache-objects nil
  4182. "Hash table used as to cache objects.
  4183. Key is an element, as returned by `org-element-at-point', and
  4184. value is an alist where each association is:
  4185. \(PARENT COMPLETEP . OBJECTS)
  4186. where PARENT is an element or object, COMPLETEP is a boolean,
  4187. non-nil when all direct children of parent are already cached and
  4188. OBJECTS is a list of such children, as objects, from farthest to
  4189. closest.
  4190. In the following example, \\alpha, bold object and \\beta are
  4191. contained within a paragraph
  4192. \\alpha *\\beta*
  4193. If the paragraph is completely parsed, OBJECTS-DATA will be
  4194. \((PARAGRAPH t BOLD-OBJECT ENTITY-OBJECT)
  4195. \(BOLD-OBJECT t ENTITY-OBJECT))
  4196. whereas in a partially parsed paragraph, it could be
  4197. \((PARAGRAPH nil ENTITY-OBJECT))
  4198. This cache is used in `org-element-context'.")
  4199. (defvar org-element--cache-sync-requests nil
  4200. "List of pending synchronization requests.
  4201. A request is a vector with the following pattern:
  4202. \[NEXT BEG END OFFSET OUTREACH PARENT PHASE]
  4203. Processing a synchronization request consists of three phases:
  4204. 0. Delete modified elements,
  4205. 1. Fill missing area in cache,
  4206. 2. Shift positions and re-parent elements after the changes.
  4207. During phase 0, NEXT is the key of the first element to be
  4208. removed, BEG and END is buffer position delimiting the
  4209. modifications. Elements starting between them (inclusive) are
  4210. removed and so are those contained within OUTREACH. PARENT, when
  4211. non-nil, is the parent of the first element to be removed.
  4212. During phase 1, NEXT is the key of the next known element in
  4213. cache and BEG its beginning position. Parse buffer between that
  4214. element and the one before it in order to determine the parent of
  4215. the next element. Set PARENT to the element containing NEXT.
  4216. During phase 2, NEXT is the key of the next element to shift in
  4217. the parse tree. All elements starting from this one have their
  4218. properties relatives to buffer positions shifted by integer
  4219. OFFSET and, if they belong to element PARENT, are adopted by it.
  4220. PHASE specifies the phase number, as an integer.")
  4221. (defvar org-element--cache-sync-timer nil
  4222. "Timer used for cache synchronization.")
  4223. (defvar org-element--cache-sync-keys nil
  4224. "Hash table used to store keys during synchronization.
  4225. See `org-element--cache-key' for more information.")
  4226. (defsubst org-element--cache-key (element)
  4227. "Return a unique key for ELEMENT in cache tree.
  4228. Keys are used to keep a total order among elements in the cache.
  4229. Comparison is done with `org-element--cache-key-less-p'.
  4230. When no synchronization is taking place, a key is simply the
  4231. beginning position of the element, or that position plus one in
  4232. the case of an first item (respectively row) in
  4233. a list (respectively a table).
  4234. During a synchronization, the key is the one the element had when
  4235. the cache was synchronized for the last time. Elements added to
  4236. cache during the synchronization get a new key generated with
  4237. `org-element--cache-generate-key'.
  4238. Such keys are stored in `org-element--cache-sync-keys'. The hash
  4239. table is cleared once the synchronization is complete."
  4240. (or (gethash element org-element--cache-sync-keys)
  4241. (let* ((begin (org-element-property :begin element))
  4242. ;; Increase beginning position of items (respectively
  4243. ;; table rows) by one, so the first item can get
  4244. ;; a different key from its parent list (respectively
  4245. ;; table).
  4246. (key (if (memq (org-element-type element) '(item table-row))
  4247. (1+ begin)
  4248. begin)))
  4249. (if org-element--cache-sync-requests
  4250. (puthash element key org-element--cache-sync-keys)
  4251. key))))
  4252. (defun org-element--cache-generate-key (lower upper)
  4253. "Generate a key between LOWER and UPPER.
  4254. LOWER and UPPER are integers or lists, possibly empty.
  4255. If LOWER and UPPER are equals, return LOWER. Otherwise, return
  4256. a unique key, as an integer or a list of integers, according to
  4257. the following rules:
  4258. - LOWER and UPPER are compared level-wise until values differ.
  4259. - If, at a given level, LOWER and UPPER differ from more than
  4260. 2, the new key shares all the levels above with LOWER and
  4261. gets a new level. Its value is the mean between LOWER and
  4262. UPPER:
  4263. \(1 2) + (1 4) --> (1 3)
  4264. - If LOWER has no value to compare with, it is assumed that its
  4265. value is `most-negative-fixnum'. E.g.,
  4266. \(1 1) + (1 1 2)
  4267. is equivalent to
  4268. \(1 1 m) + (1 1 2)
  4269. where m is `most-negative-fixnum'. Likewise, if UPPER is
  4270. short of levels, the current value is `most-positive-fixnum'.
  4271. - If they differ from only one, the new key inherits from
  4272. current LOWER level and fork it at the next level. E.g.,
  4273. \(2 1) + (3 3)
  4274. is equivalent to
  4275. \(2 1) + (2 M)
  4276. where M is `most-positive-fixnum'.
  4277. - If the key is only one level long, it is returned as an
  4278. integer:
  4279. \(1 2) + (3 2) --> 2
  4280. When they are not equals, the function assumes that LOWER is
  4281. lesser than UPPER, per `org-element--cache-key-less-p'."
  4282. (if (equal lower upper) lower
  4283. (let ((lower (if (integerp lower) (list lower) lower))
  4284. (upper (if (integerp upper) (list upper) upper))
  4285. skip-upper key)
  4286. (catch 'exit
  4287. (while t
  4288. (let ((min (or (car lower) most-negative-fixnum))
  4289. (max (cond (skip-upper most-positive-fixnum)
  4290. ((car upper))
  4291. (t most-positive-fixnum))))
  4292. (if (< (1+ min) max)
  4293. (let ((mean (+ (ash min -1) (ash max -1) (logand min max 1))))
  4294. (throw 'exit (if key (nreverse (cons mean key)) mean)))
  4295. (when (and (< min max) (not skip-upper))
  4296. ;; When at a given level, LOWER and UPPER differ from
  4297. ;; 1, ignore UPPER altogether. Instead create a key
  4298. ;; between LOWER and the greatest key with the same
  4299. ;; prefix as LOWER so far.
  4300. (setq skip-upper t))
  4301. (push min key)
  4302. (setq lower (cdr lower) upper (cdr upper)))))))))
  4303. (defsubst org-element--cache-key-less-p (a b)
  4304. "Non-nil if key A is less than key B.
  4305. A and B are either integers or lists of integers, as returned by
  4306. `org-element--cache-key'."
  4307. (if (integerp a) (if (integerp b) (< a b) (<= a (car b)))
  4308. (if (integerp b) (< (car a) b)
  4309. (catch 'exit
  4310. (while (and a b)
  4311. (cond ((car-less-than-car a b) (throw 'exit t))
  4312. ((car-less-than-car b a) (throw 'exit nil))
  4313. (t (setq a (cdr a) b (cdr b)))))
  4314. ;; If A is empty, either keys are equal (B is also empty) and
  4315. ;; we return nil, or A is lesser than B (B is longer) and we
  4316. ;; return a non-nil value.
  4317. ;;
  4318. ;; If A is not empty, B is necessarily empty and A is greater
  4319. ;; than B (A is longer). Therefore, return nil.
  4320. (and (null a) b)))))
  4321. (defun org-element--cache-compare (a b)
  4322. "Non-nil when element A is located before element B."
  4323. (org-element--cache-key-less-p (org-element--cache-key a)
  4324. (org-element--cache-key b)))
  4325. (defsubst org-element--cache-root ()
  4326. "Return root value in cache.
  4327. This function assumes `org-element--cache' is a valid AVL tree."
  4328. (avl-tree--node-left (avl-tree--dummyroot org-element--cache)))
  4329. ;;;; Tools
  4330. (defsubst org-element--cache-active-p ()
  4331. "Non-nil when cache is active in current buffer."
  4332. (and org-element-use-cache
  4333. (or (derived-mode-p 'org-mode) orgstruct-mode)))
  4334. (defun org-element--cache-find (pos &optional side)
  4335. "Find element in cache starting at POS or before.
  4336. POS refers to a buffer position.
  4337. When optional argument SIDE is non-nil, the function checks for
  4338. elements starting at or past POS instead. If SIDE is `both', the
  4339. function returns a cons cell where car is the first element
  4340. starting at or before POS and cdr the first element starting
  4341. after POS.
  4342. The function can only find elements in the synchronized part of
  4343. the cache."
  4344. (let ((limit (and org-element--cache-sync-requests
  4345. (aref (car org-element--cache-sync-requests) 0)))
  4346. (node (org-element--cache-root))
  4347. lower upper)
  4348. (while node
  4349. (let* ((element (avl-tree--node-data node))
  4350. (begin (org-element-property :begin element)))
  4351. (cond
  4352. ((and limit
  4353. (not (org-element--cache-key-less-p
  4354. (org-element--cache-key element) limit)))
  4355. (setq node (avl-tree--node-left node)))
  4356. ((> begin pos)
  4357. (setq upper element
  4358. node (avl-tree--node-left node)))
  4359. ((< begin pos)
  4360. (setq lower element
  4361. node (avl-tree--node-right node)))
  4362. ;; We found an element in cache starting at POS. If `side'
  4363. ;; is `both' we also want the next one in order to generate
  4364. ;; a key in-between.
  4365. ;;
  4366. ;; If the element is the first row or item in a table or
  4367. ;; a plain list, we always return the table or the plain
  4368. ;; list.
  4369. ;;
  4370. ;; In any other case, we return the element found.
  4371. ((eq side 'both)
  4372. (setq lower element)
  4373. (setq node (avl-tree--node-right node)))
  4374. ((and (memq (org-element-type element) '(item table-row))
  4375. (let ((parent (org-element-property :parent element)))
  4376. (and (= (org-element-property :begin element)
  4377. (org-element-property :contents-begin parent))
  4378. (setq node nil
  4379. lower parent
  4380. upper parent)))))
  4381. (t
  4382. (setq node nil
  4383. lower element
  4384. upper element)))))
  4385. (case side
  4386. (both (cons lower upper))
  4387. ((nil) lower)
  4388. (otherwise upper))))
  4389. (defun org-element--cache-put (element &optional data)
  4390. "Store ELEMENT in current buffer's cache, if allowed.
  4391. When optional argument DATA is non-nil, assume is it object data
  4392. relative to ELEMENT and store it in the objects cache."
  4393. (cond ((not (org-element--cache-active-p)) nil)
  4394. ((not data)
  4395. (when org-element--cache-sync-requests
  4396. ;; During synchronization, first build an appropriate key
  4397. ;; for the new element so `avl-tree-enter' can insert it at
  4398. ;; the right spot in the cache.
  4399. (let ((keys (org-element--cache-find
  4400. (org-element-property :begin element) 'both)))
  4401. (puthash element
  4402. (org-element--cache-generate-key
  4403. (and (car keys) (org-element--cache-key (car keys)))
  4404. (cond ((cdr keys) (org-element--cache-key (cdr keys)))
  4405. (org-element--cache-sync-requests
  4406. (aref (car org-element--cache-sync-requests) 0))))
  4407. org-element--cache-sync-keys)))
  4408. (avl-tree-enter org-element--cache element))
  4409. ;; Headlines are not stored in cache, so objects in titles are
  4410. ;; not stored either.
  4411. ((eq (org-element-type element) 'headline) nil)
  4412. (t (puthash element data org-element--cache-objects))))
  4413. (defsubst org-element--cache-remove (element)
  4414. "Remove ELEMENT from cache.
  4415. Assume ELEMENT belongs to cache and that a cache is active."
  4416. (avl-tree-delete org-element--cache element)
  4417. (remhash element org-element--cache-objects))
  4418. ;;;; Synchronization
  4419. (defsubst org-element--cache-set-timer (buffer)
  4420. "Set idle timer for cache synchronization in BUFFER."
  4421. (when org-element--cache-sync-timer
  4422. (cancel-timer org-element--cache-sync-timer))
  4423. (setq org-element--cache-sync-timer
  4424. (run-with-idle-timer
  4425. (let ((idle (current-idle-time)))
  4426. (if idle (time-add idle org-element-cache-sync-break)
  4427. org-element-cache-sync-idle-time))
  4428. nil
  4429. #'org-element--cache-sync
  4430. buffer)))
  4431. (defsubst org-element--cache-interrupt-p (time-limit)
  4432. "Non-nil when synchronization process should be interrupted.
  4433. TIME-LIMIT is a time value or nil."
  4434. (and time-limit
  4435. (or (input-pending-p)
  4436. (time-less-p time-limit (current-time)))))
  4437. (defsubst org-element--cache-shift-positions (element offset &optional props)
  4438. "Shift ELEMENT properties relative to buffer positions by OFFSET.
  4439. Properties containing buffer positions are `:begin', `:end',
  4440. `:contents-begin', `:contents-end' and `:structure'. When
  4441. optional argument PROPS is a list of keywords, only shift
  4442. properties provided in that list.
  4443. Properties are modified by side-effect."
  4444. (let ((properties (nth 1 element)))
  4445. ;; Shift `:structure' property for the first plain list only: it
  4446. ;; is the only one that really matters and it prevents from
  4447. ;; shifting it more than once.
  4448. (when (and (or (not props) (memq :structure props))
  4449. (eq (org-element-type element) 'plain-list)
  4450. (not (eq (org-element-type (plist-get properties :parent))
  4451. 'item)))
  4452. (dolist (item (plist-get properties :structure))
  4453. (incf (car item) offset)
  4454. (incf (nth 6 item) offset)))
  4455. (dolist (key '(:begin :contents-begin :contents-end :end :post-affiliated))
  4456. (let ((value (and (or (not props) (memq key props))
  4457. (plist-get properties key))))
  4458. (and value (plist-put properties key (+ offset value)))))))
  4459. (defun org-element--cache-sync (buffer &optional threshold future-change)
  4460. "Synchronize cache with recent modification in BUFFER.
  4461. When optional argument THRESHOLD is non-nil, do the
  4462. synchronization for all elements starting before or at threshold,
  4463. then exit. Otherwise, synchronize cache for as long as
  4464. `org-element-cache-sync-duration' or until Emacs leaves idle
  4465. state.
  4466. FUTURE-CHANGE, when non-nil, is a buffer position where changes
  4467. not registered yet in the cache are going to happen. It is used
  4468. in `org-element--cache-submit-request', where cache is partially
  4469. updated before current modification are actually submitted."
  4470. (when (buffer-live-p buffer)
  4471. (with-current-buffer buffer
  4472. (let ((inhibit-quit t) request next)
  4473. (when org-element--cache-sync-timer
  4474. (cancel-timer org-element--cache-sync-timer))
  4475. (catch 'interrupt
  4476. (while org-element--cache-sync-requests
  4477. (setq request (car org-element--cache-sync-requests)
  4478. next (nth 1 org-element--cache-sync-requests))
  4479. (org-element--cache-process-request
  4480. request
  4481. (and next (aref next 0))
  4482. threshold
  4483. (and (not threshold)
  4484. (time-add (current-time)
  4485. org-element-cache-sync-duration))
  4486. future-change)
  4487. ;; Request processed. Merge current and next offsets and
  4488. ;; transfer ending position.
  4489. (when next
  4490. (incf (aref next 3) (aref request 3))
  4491. (aset next 2 (aref request 2)))
  4492. (setq org-element--cache-sync-requests
  4493. (cdr org-element--cache-sync-requests))))
  4494. ;; If more requests are awaiting, set idle timer accordingly.
  4495. ;; Otherwise, reset keys.
  4496. (if org-element--cache-sync-requests
  4497. (org-element--cache-set-timer buffer)
  4498. (clrhash org-element--cache-sync-keys))))))
  4499. (defun org-element--cache-process-request
  4500. (request next threshold time-limit future-change)
  4501. "Process synchronization REQUEST for all entries before NEXT.
  4502. REQUEST is a vector, built by `org-element--cache-submit-request'.
  4503. NEXT is a cache key, as returned by `org-element--cache-key'.
  4504. When non-nil, THRESHOLD is a buffer position. Synchronization
  4505. stops as soon as a shifted element begins after it.
  4506. When non-nil, TIME-LIMIT is a time value. Synchronization stops
  4507. after this time or when Emacs exits idle state.
  4508. When non-nil, FUTURE-CHANGE is a buffer position where changes
  4509. not registered yet in the cache are going to happen. See
  4510. `org-element--cache-submit-request' for more information.
  4511. Throw `interrupt' if the process stops before completing the
  4512. request."
  4513. (catch 'quit
  4514. (when (= (aref request 6) 0)
  4515. ;; Phase 0.
  4516. ;;
  4517. ;; Delete all elements starting after BEG, but not after buffer
  4518. ;; position END or past element with key NEXT.
  4519. ;;
  4520. ;; At each iteration, we start again at tree root since
  4521. ;; a deletion modifies structure of the balanced tree.
  4522. (catch 'end-phase
  4523. (let ((beg (aref request 0))
  4524. (end (aref request 2))
  4525. (outreach (aref request 4)))
  4526. (while t
  4527. (when (org-element--cache-interrupt-p time-limit)
  4528. (throw 'interrupt nil))
  4529. ;; Find first element in cache with key BEG or after it.
  4530. (let ((node (org-element--cache-root)) data data-key)
  4531. (while node
  4532. (let* ((element (avl-tree--node-data node))
  4533. (key (org-element--cache-key element)))
  4534. (cond
  4535. ((org-element--cache-key-less-p key beg)
  4536. (setq node (avl-tree--node-right node)))
  4537. ((org-element--cache-key-less-p beg key)
  4538. (setq data element
  4539. data-key key
  4540. node (avl-tree--node-left node)))
  4541. (t (setq data element
  4542. data-key key
  4543. node nil)))))
  4544. (if data
  4545. (let ((pos (org-element-property :begin data)))
  4546. (if (if (or (not next)
  4547. (org-element--cache-key-less-p data-key next))
  4548. (<= pos end)
  4549. (let ((up data))
  4550. (while (and up (not (eq up outreach)))
  4551. (setq up (org-element-property :parent up)))
  4552. up))
  4553. (org-element--cache-remove data)
  4554. (aset request 0 data-key)
  4555. (aset request 1 pos)
  4556. (aset request 6 1)
  4557. (throw 'end-phase nil)))
  4558. ;; No element starting after modifications left in
  4559. ;; cache: further processing is futile.
  4560. (throw 'quit t)))))))
  4561. (when (= (aref request 6) 1)
  4562. ;; Phase 1.
  4563. ;;
  4564. ;; Phase 0 left a hole in the cache. Some elements after it
  4565. ;; could have parents within. For example, in the following
  4566. ;; buffer:
  4567. ;;
  4568. ;; - item
  4569. ;;
  4570. ;;
  4571. ;; Paragraph1
  4572. ;;
  4573. ;; Paragraph2
  4574. ;;
  4575. ;; if we remove a blank line between "item" and "Paragraph1",
  4576. ;; everything down to "Paragraph2" is removed from cache. But
  4577. ;; the paragraph now belongs to the list, and its `:parent'
  4578. ;; property no longer is accurate.
  4579. ;;
  4580. ;; Therefore we need to parse again elements in the hole, or at
  4581. ;; least in its last section, so that we can re-parent
  4582. ;; subsequent elements, during phase 2.
  4583. ;;
  4584. ;; Note that we only need to get the parent from the first
  4585. ;; element in cache after the hole.
  4586. ;;
  4587. ;; When next key is lesser or equal to the current one, delegate
  4588. ;; phase 1 processing to next request in order to preserve key
  4589. ;; order among requests.
  4590. (let ((key (aref request 0)))
  4591. (when (and next (not (org-element--cache-key-less-p key next)))
  4592. (let ((next-request (nth 1 org-element--cache-sync-requests)))
  4593. (aset next-request 0 key)
  4594. (aset next-request 1 (aref request 1))
  4595. (aset next-request 6 1))
  4596. (throw 'quit t)))
  4597. ;; Next element will start at its beginning position plus
  4598. ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
  4599. ;; contains the real beginning position of the first element to
  4600. ;; shift and re-parent.
  4601. (let ((limit (+ (aref request 1) (aref request 3))))
  4602. (cond ((and threshold (> limit threshold)) (throw 'interrupt nil))
  4603. ((and future-change (>= limit future-change))
  4604. ;; Changes are going to happen around this element and
  4605. ;; they will trigger another phase 1 request. Skip the
  4606. ;; current one.
  4607. (aset request 6 2))
  4608. (t
  4609. (let ((parent (org-element--parse-to limit t time-limit)))
  4610. (aset request 5 parent)
  4611. (aset request 6 2))))))
  4612. ;; Phase 2.
  4613. ;;
  4614. ;; Shift all elements starting from key START, but before NEXT, by
  4615. ;; OFFSET, and re-parent them when appropriate.
  4616. ;;
  4617. ;; Elements are modified by side-effect so the tree structure
  4618. ;; remains intact.
  4619. ;;
  4620. ;; Once THRESHOLD, if any, is reached, or once there is an input
  4621. ;; pending, exit. Before leaving, the current synchronization
  4622. ;; request is updated.
  4623. (let ((start (aref request 0))
  4624. (offset (aref request 3))
  4625. (parent (aref request 5))
  4626. (node (org-element--cache-root))
  4627. (stack (list nil))
  4628. (leftp t)
  4629. exit-flag)
  4630. ;; No re-parenting nor shifting planned: request is over.
  4631. (when (and (not parent) (zerop offset)) (throw 'quit t))
  4632. (while node
  4633. (let* ((data (avl-tree--node-data node))
  4634. (key (org-element--cache-key data)))
  4635. (if (and leftp (avl-tree--node-left node)
  4636. (not (org-element--cache-key-less-p key start)))
  4637. (progn (push node stack)
  4638. (setq node (avl-tree--node-left node)))
  4639. (unless (org-element--cache-key-less-p key start)
  4640. ;; We reached NEXT. Request is complete.
  4641. (when (equal key next) (throw 'quit t))
  4642. ;; Handle interruption request. Update current request.
  4643. (when (or exit-flag (org-element--cache-interrupt-p time-limit))
  4644. (aset request 0 key)
  4645. (aset request 5 parent)
  4646. (throw 'interrupt nil))
  4647. ;; Shift element.
  4648. (unless (zerop offset)
  4649. (org-element--cache-shift-positions data offset)
  4650. ;; Shift associated objects data, if any.
  4651. (dolist (object-data (gethash data org-element--cache-objects))
  4652. (dolist (object (cddr object-data))
  4653. (org-element--cache-shift-positions object offset))))
  4654. (let ((begin (org-element-property :begin data)))
  4655. ;; Update PARENT and re-parent DATA, only when
  4656. ;; necessary. Propagate new structures for lists.
  4657. (while (and parent
  4658. (<= (org-element-property :end parent) begin))
  4659. (setq parent (org-element-property :parent parent)))
  4660. (cond ((and (not parent) (zerop offset)) (throw 'quit nil))
  4661. ((and parent
  4662. (let ((p (org-element-property :parent data)))
  4663. (or (not p)
  4664. (< (org-element-property :begin p)
  4665. (org-element-property :begin parent)))))
  4666. (org-element-put-property data :parent parent)
  4667. (let ((s (org-element-property :structure parent)))
  4668. (when (and s (org-element-property :structure data))
  4669. (org-element-put-property data :structure s)))))
  4670. ;; Cache is up-to-date past THRESHOLD. Request
  4671. ;; interruption.
  4672. (when (and threshold (> begin threshold)) (setq exit-flag t))))
  4673. (setq node (if (setq leftp (avl-tree--node-right node))
  4674. (avl-tree--node-right node)
  4675. (pop stack))))))
  4676. ;; We reached end of tree: synchronization complete.
  4677. t)))
  4678. (defun org-element--parse-to (pos &optional syncp time-limit)
  4679. "Parse elements in current section, down to POS.
  4680. Start parsing from the closest between the last known element in
  4681. cache or headline above. Return the smallest element containing
  4682. POS.
  4683. When optional argument SYNCP is non-nil, return the parent of the
  4684. element containing POS instead. In that case, it is also
  4685. possible to provide TIME-LIMIT, which is a time value specifying
  4686. when the parsing should stop. The function throws `interrupt' if
  4687. the process stopped before finding the expected result."
  4688. (catch 'exit
  4689. (org-with-wide-buffer
  4690. (goto-char pos)
  4691. (let* ((cached (and (org-element--cache-active-p)
  4692. (org-element--cache-find pos nil)))
  4693. (begin (org-element-property :begin cached))
  4694. element next mode)
  4695. (cond
  4696. ;; Nothing in cache before point: start parsing from first
  4697. ;; element following headline above, or first element in
  4698. ;; buffer.
  4699. ((not cached)
  4700. (when (org-with-limited-levels (outline-previous-heading))
  4701. (setq mode 'planning)
  4702. (forward-line))
  4703. (skip-chars-forward " \r\t\n")
  4704. (beginning-of-line))
  4705. ;; Cache returned exact match: return it.
  4706. ((= pos begin)
  4707. (throw 'exit (if syncp (org-element-property :parent cached) cached)))
  4708. ;; There's a headline between cached value and POS: cached
  4709. ;; value is invalid. Start parsing from first element
  4710. ;; following the headline.
  4711. ((re-search-backward
  4712. (org-with-limited-levels org-outline-regexp-bol) begin t)
  4713. (forward-line)
  4714. (skip-chars-forward " \r\t\n")
  4715. (beginning-of-line)
  4716. (setq mode 'planning))
  4717. ;; Check if CACHED or any of its ancestors contain point.
  4718. ;;
  4719. ;; If there is such an element, we inspect it in order to know
  4720. ;; if we return it or if we need to parse its contents.
  4721. ;; Otherwise, we just start parsing from current location,
  4722. ;; which is right after the top-most element containing
  4723. ;; CACHED.
  4724. ;;
  4725. ;; As a special case, if POS is at the end of the buffer, we
  4726. ;; want to return the innermost element ending there.
  4727. ;;
  4728. ;; Also, if we find an ancestor and discover that we need to
  4729. ;; parse its contents, make sure we don't start from
  4730. ;; `:contents-begin', as we would otherwise go past CACHED
  4731. ;; again. Instead, in that situation, we will resume parsing
  4732. ;; from NEXT, which is located after CACHED or its higher
  4733. ;; ancestor not containing point.
  4734. (t
  4735. (let ((up cached)
  4736. (pos (if (= (point-max) pos) (1- pos) pos)))
  4737. (goto-char (or (org-element-property :contents-begin cached) begin))
  4738. (while (let ((end (org-element-property :end up)))
  4739. (and (<= end pos)
  4740. (goto-char end)
  4741. (setq up (org-element-property :parent up)))))
  4742. (cond ((not up))
  4743. ((eobp) (setq element up))
  4744. (t (setq element up next (point)))))))
  4745. ;; Parse successively each element until we reach POS.
  4746. (let ((end (or (org-element-property :end element)
  4747. (save-excursion
  4748. (org-with-limited-levels (outline-next-heading))
  4749. (point))))
  4750. (parent element))
  4751. (while t
  4752. (when syncp
  4753. (cond ((= (point) pos) (throw 'exit parent))
  4754. ((org-element--cache-interrupt-p time-limit)
  4755. (throw 'interrupt nil))))
  4756. (unless element
  4757. (setq element (org-element--current-element
  4758. end 'element mode
  4759. (org-element-property :structure parent)))
  4760. (org-element-put-property element :parent parent)
  4761. (org-element--cache-put element))
  4762. (let ((elem-end (org-element-property :end element))
  4763. (type (org-element-type element)))
  4764. (cond
  4765. ;; Skip any element ending before point. Also skip
  4766. ;; element ending at point (unless it is also the end of
  4767. ;; buffer) since we're sure that another element begins
  4768. ;; after it.
  4769. ((and (<= elem-end pos) (/= (point-max) elem-end))
  4770. (goto-char elem-end)
  4771. (setq mode (org-element--next-mode type nil)))
  4772. ;; A non-greater element contains point: return it.
  4773. ((not (memq type org-element-greater-elements))
  4774. (throw 'exit element))
  4775. ;; Otherwise, we have to decide if ELEMENT really
  4776. ;; contains POS. In that case we start parsing from
  4777. ;; contents' beginning.
  4778. ;;
  4779. ;; If POS is at contents' beginning but it is also at
  4780. ;; the beginning of the first item in a list or a table.
  4781. ;; In that case, we need to create an anchor for that
  4782. ;; list or table, so return it.
  4783. ;;
  4784. ;; Also, if POS is at the end of the buffer, no element
  4785. ;; can start after it, but more than one may end there.
  4786. ;; Arbitrarily, we choose to return the innermost of
  4787. ;; such elements.
  4788. ((let ((cbeg (org-element-property :contents-begin element))
  4789. (cend (org-element-property :contents-end element)))
  4790. (when (or syncp
  4791. (and cbeg cend
  4792. (or (< cbeg pos)
  4793. (and (= cbeg pos)
  4794. (not (memq type '(plain-list table)))))
  4795. (or (> cend pos)
  4796. (and (= cend pos) (= (point-max) pos)))))
  4797. (goto-char (or next cbeg))
  4798. (setq next nil
  4799. mode (org-element--next-mode type t)
  4800. parent element
  4801. end cend))))
  4802. ;; Otherwise, return ELEMENT as it is the smallest
  4803. ;; element containing POS.
  4804. (t (throw 'exit element))))
  4805. (setq element nil)))))))
  4806. ;;;; Staging Buffer Changes
  4807. (defconst org-element--cache-sensitive-re
  4808. (concat
  4809. org-outline-regexp-bol "\\|"
  4810. "\\\\end{[A-Za-z0-9*]+}[ \t]*$" "\\|"
  4811. "^[ \t]*\\(?:"
  4812. "#\\+\\(?:BEGIN[:_]\\|END\\(?:_\\|:?[ \t]*$\\)\\)" "\\|"
  4813. "\\\\begin{[A-Za-z0-9*]+}" "\\|"
  4814. ":\\(?:\\w\\|[-_]\\)+:[ \t]*$"
  4815. "\\)")
  4816. "Regexp matching a sensitive line, structure wise.
  4817. A sensitive line is a headline, inlinetask, block, drawer, or
  4818. latex-environment boundary. When such a line is modified,
  4819. structure changes in the document may propagate in the whole
  4820. section, possibly making cache invalid.")
  4821. (defvar org-element--cache-change-warning nil
  4822. "Non-nil when a sensitive line is about to be changed.
  4823. It is a symbol among nil, t and `headline'.")
  4824. (defun org-element--cache-before-change (beg end)
  4825. "Request extension of area going to be modified if needed.
  4826. BEG and END are the beginning and end of the range of changed
  4827. text. See `before-change-functions' for more information."
  4828. (when (org-element--cache-active-p)
  4829. (org-with-wide-buffer
  4830. (goto-char beg)
  4831. (beginning-of-line)
  4832. (let ((bottom (save-excursion (goto-char end) (line-end-position))))
  4833. (setq org-element--cache-change-warning
  4834. (save-match-data
  4835. (if (and (org-with-limited-levels (org-at-heading-p))
  4836. (= (line-end-position) bottom))
  4837. 'headline
  4838. (let ((case-fold-search t))
  4839. (re-search-forward
  4840. org-element--cache-sensitive-re bottom t)))))))))
  4841. (defun org-element--cache-after-change (beg end pre)
  4842. "Update buffer modifications for current buffer.
  4843. BEG and END are the beginning and end of the range of changed
  4844. text, and the length in bytes of the pre-change text replaced by
  4845. that range. See `after-change-functions' for more information."
  4846. (when (org-element--cache-active-p)
  4847. (org-with-wide-buffer
  4848. (goto-char beg)
  4849. (beginning-of-line)
  4850. (save-match-data
  4851. (let ((top (point))
  4852. (bottom (save-excursion (goto-char end) (line-end-position))))
  4853. ;; Determine if modified area needs to be extended, according
  4854. ;; to both previous and current state. We make a special
  4855. ;; case for headline editing: if a headline is modified but
  4856. ;; not removed, do not extend.
  4857. (when (case org-element--cache-change-warning
  4858. ((t) t)
  4859. (headline
  4860. (not (and (org-with-limited-levels (org-at-heading-p))
  4861. (= (line-end-position) bottom))))
  4862. (otherwise
  4863. (let ((case-fold-search t))
  4864. (re-search-forward
  4865. org-element--cache-sensitive-re bottom t))))
  4866. ;; Effectively extend modified area.
  4867. (org-with-limited-levels
  4868. (setq top (progn (goto-char top)
  4869. (when (outline-previous-heading) (forward-line))
  4870. (point)))
  4871. (setq bottom (progn (goto-char bottom)
  4872. (if (outline-next-heading) (1- (point))
  4873. (point))))))
  4874. ;; Store synchronization request.
  4875. (let ((offset (- end beg pre)))
  4876. (org-element--cache-submit-request top (- bottom offset) offset)))))
  4877. ;; Activate a timer to process the request during idle time.
  4878. (org-element--cache-set-timer (current-buffer))))
  4879. (defun org-element--cache-for-removal (beg end offset)
  4880. "Return first element to remove from cache.
  4881. BEG and END are buffer positions delimiting buffer modifications.
  4882. OFFSET is the size of the changes.
  4883. Returned element is usually the first element in cache containing
  4884. any position between BEG and END. As an exception, greater
  4885. elements around the changes that are robust to contents
  4886. modifications are preserved and updated according to the
  4887. changes."
  4888. (let* ((elements (org-element--cache-find (1- beg) 'both))
  4889. (before (car elements))
  4890. (after (cdr elements)))
  4891. (if (not before) after
  4892. (let ((up before)
  4893. (robust-flag t))
  4894. (while up
  4895. (if (let ((type (org-element-type up)))
  4896. (and (or (memq type '(center-block dynamic-block quote-block
  4897. special-block))
  4898. ;; Drawers named "PROPERTIES" are probably
  4899. ;; a properties drawer being edited. Force
  4900. ;; parsing to check if editing is over.
  4901. (and (eq type 'drawer)
  4902. (not (string=
  4903. (org-element-property :drawer-name up)
  4904. "PROPERTIES"))))
  4905. (let ((cbeg (org-element-property :contents-begin up)))
  4906. (and cbeg
  4907. (<= cbeg beg)
  4908. (> (org-element-property :contents-end up) end)))))
  4909. ;; UP is a robust greater element containing changes.
  4910. ;; We only need to extend its ending boundaries.
  4911. (org-element--cache-shift-positions
  4912. up offset '(:contents-end :end))
  4913. (setq before up)
  4914. (when robust-flag (setq robust-flag nil)))
  4915. (setq up (org-element-property :parent up)))
  4916. ;; We're at top level element containing ELEMENT: if it's
  4917. ;; altered by buffer modifications, it is first element in
  4918. ;; cache to be removed. Otherwise, that first element is the
  4919. ;; following one.
  4920. ;;
  4921. ;; As a special case, do not remove BEFORE if it is a robust
  4922. ;; container for current changes.
  4923. (if (or (< (org-element-property :end before) beg) robust-flag) after
  4924. before)))))
  4925. (defun org-element--cache-submit-request (beg end offset)
  4926. "Submit a new cache synchronization request for current buffer.
  4927. BEG and END are buffer positions delimiting the minimal area
  4928. where cache data should be removed. OFFSET is the size of the
  4929. change, as an integer."
  4930. (let ((next (car org-element--cache-sync-requests))
  4931. delete-to delete-from)
  4932. (if (and next
  4933. (zerop (aref next 6))
  4934. (> (setq delete-to (+ (aref next 2) (aref next 3))) end)
  4935. (<= (setq delete-from (aref next 1)) end))
  4936. ;; Current changes can be merged with first sync request: we
  4937. ;; can save a partial cache synchronization.
  4938. (progn
  4939. (incf (aref next 3) offset)
  4940. ;; If last change happened within area to be removed, extend
  4941. ;; boundaries of robust parents, if any. Otherwise, find
  4942. ;; first element to remove and update request accordingly.
  4943. (if (> beg delete-from)
  4944. (let ((up (aref next 5)))
  4945. (while up
  4946. (org-element--cache-shift-positions
  4947. up offset '(:contents-end :end))
  4948. (setq up (org-element-property :parent up))))
  4949. (let ((first (org-element--cache-for-removal beg delete-to offset)))
  4950. (when first
  4951. (aset next 0 (org-element--cache-key first))
  4952. (aset next 1 (org-element-property :begin first))
  4953. (aset next 5 (org-element-property :parent first))))))
  4954. ;; Ensure cache is correct up to END. Also make sure that NEXT,
  4955. ;; if any, is no longer a 0-phase request, thus ensuring that
  4956. ;; phases are properly ordered. We need to provide OFFSET as
  4957. ;; optional parameter since current modifications are not known
  4958. ;; yet to the otherwise correct part of the cache (i.e, before
  4959. ;; the first request).
  4960. (when next (org-element--cache-sync (current-buffer) end beg))
  4961. (let ((first (org-element--cache-for-removal beg end offset)))
  4962. (if first
  4963. (push (let ((beg (org-element-property :begin first))
  4964. (key (org-element--cache-key first)))
  4965. (cond
  4966. ;; When changes happen before the first known
  4967. ;; element, re-parent and shift the rest of the
  4968. ;; cache.
  4969. ((> beg end) (vector key beg nil offset nil nil 1))
  4970. ;; Otherwise, we find the first non robust
  4971. ;; element containing END. All elements between
  4972. ;; FIRST and this one are to be removed.
  4973. ;;
  4974. ;; Among them, some could be located outside the
  4975. ;; synchronized part of the cache, in which case
  4976. ;; comparing buffer positions to find them is
  4977. ;; useless. Instead, we store the element
  4978. ;; containing them in the request itself. All
  4979. ;; its children will be removed.
  4980. ((let ((first-end (org-element-property :end first)))
  4981. (and (> first-end end)
  4982. (vector key beg first-end offset first
  4983. (org-element-property :parent first) 0))))
  4984. (t
  4985. (let* ((element (org-element--cache-find end))
  4986. (end (org-element-property :end element))
  4987. (up element))
  4988. (while (and (setq up (org-element-property :parent up))
  4989. (>= (org-element-property :begin up) beg))
  4990. (setq end (org-element-property :end up)
  4991. element up))
  4992. (vector key beg end offset element
  4993. (org-element-property :parent first) 0)))))
  4994. org-element--cache-sync-requests)
  4995. ;; No element to remove. No need to re-parent either.
  4996. ;; Simply shift additional elements, if any, by OFFSET.
  4997. (when org-element--cache-sync-requests
  4998. (incf (aref (car org-element--cache-sync-requests) 3) offset)))))))
  4999. ;;;; Public Functions
  5000. ;;;###autoload
  5001. (defun org-element-cache-reset (&optional all)
  5002. "Reset cache in current buffer.
  5003. When optional argument ALL is non-nil, reset cache in all Org
  5004. buffers."
  5005. (interactive "P")
  5006. (dolist (buffer (if all (buffer-list) (list (current-buffer))))
  5007. (with-current-buffer buffer
  5008. (when (org-element--cache-active-p)
  5009. (org-set-local 'org-element--cache
  5010. (avl-tree-create #'org-element--cache-compare))
  5011. (org-set-local 'org-element--cache-objects (make-hash-table :test #'eq))
  5012. (org-set-local 'org-element--cache-sync-keys
  5013. (make-hash-table :weakness 'key :test #'eq))
  5014. (org-set-local 'org-element--cache-change-warning nil)
  5015. (org-set-local 'org-element--cache-sync-requests nil)
  5016. (org-set-local 'org-element--cache-sync-timer nil)
  5017. (add-hook 'before-change-functions
  5018. #'org-element--cache-before-change nil t)
  5019. (add-hook 'after-change-functions
  5020. #'org-element--cache-after-change nil t)))))
  5021. ;;;###autoload
  5022. (defun org-element-cache-refresh (pos)
  5023. "Refresh cache at position POS."
  5024. (when (org-element--cache-active-p)
  5025. (org-element--cache-sync (current-buffer) pos)
  5026. (org-element--cache-submit-request pos pos 0)
  5027. (org-element--cache-set-timer (current-buffer))))
  5028. ;;; The Toolbox
  5029. ;;
  5030. ;; The first move is to implement a way to obtain the smallest element
  5031. ;; containing point. This is the job of `org-element-at-point'. It
  5032. ;; basically jumps back to the beginning of section containing point
  5033. ;; and proceed, one element after the other, with
  5034. ;; `org-element--current-element' until the container is found. Note:
  5035. ;; When using `org-element-at-point', secondary values are never
  5036. ;; parsed since the function focuses on elements, not on objects.
  5037. ;;
  5038. ;; At a deeper level, `org-element-context' lists all elements and
  5039. ;; objects containing point.
  5040. ;;
  5041. ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
  5042. ;; internally by navigation and manipulation tools.
  5043. ;;;###autoload
  5044. (defun org-element-at-point ()
  5045. "Determine closest element around point.
  5046. Return value is a list like (TYPE PROPS) where TYPE is the type
  5047. of the element and PROPS a plist of properties associated to the
  5048. element.
  5049. Possible types are defined in `org-element-all-elements'.
  5050. Properties depend on element or object type, but always include
  5051. `:begin', `:end', `:parent' and `:post-blank' properties.
  5052. As a special case, if point is at the very beginning of the first
  5053. item in a list or sub-list, returned element will be that list
  5054. instead of the item. Likewise, if point is at the beginning of
  5055. the first row of a table, returned element will be the table
  5056. instead of the first row.
  5057. When point is at the end of the buffer, return the innermost
  5058. element ending there."
  5059. (org-with-wide-buffer
  5060. (let ((origin (point)))
  5061. (end-of-line)
  5062. (skip-chars-backward " \r\t\n")
  5063. (cond
  5064. ;; Within blank lines at the beginning of buffer, return nil.
  5065. ((bobp) nil)
  5066. ;; Within blank lines right after a headline, return that
  5067. ;; headline.
  5068. ((org-with-limited-levels (org-at-heading-p))
  5069. (beginning-of-line)
  5070. (org-element-headline-parser (point-max) t))
  5071. ;; Otherwise parse until we find element containing ORIGIN.
  5072. (t
  5073. (when (org-element--cache-active-p)
  5074. (if (not org-element--cache) (org-element-cache-reset)
  5075. (org-element--cache-sync (current-buffer) origin)))
  5076. (org-element--parse-to origin))))))
  5077. ;;;###autoload
  5078. (defun org-element-context (&optional element)
  5079. "Return smallest element or object around point.
  5080. Return value is a list like (TYPE PROPS) where TYPE is the type
  5081. of the element or object and PROPS a plist of properties
  5082. associated to it.
  5083. Possible types are defined in `org-element-all-elements' and
  5084. `org-element-all-objects'. Properties depend on element or
  5085. object type, but always include `:begin', `:end', `:parent' and
  5086. `:post-blank'.
  5087. As a special case, if point is right after an object and not at
  5088. the beginning of any other object, return that object.
  5089. Optional argument ELEMENT, when non-nil, is the closest element
  5090. containing point, as returned by `org-element-at-point'.
  5091. Providing it allows for quicker computation."
  5092. (catch 'objects-forbidden
  5093. (org-with-wide-buffer
  5094. (let* ((pos (point))
  5095. (element (or element (org-element-at-point)))
  5096. (type (org-element-type element)))
  5097. ;; If point is inside an element containing objects or
  5098. ;; a secondary string, narrow buffer to the container and
  5099. ;; proceed with parsing. Otherwise, return ELEMENT.
  5100. (cond
  5101. ;; At a parsed affiliated keyword, check if we're inside main
  5102. ;; or dual value.
  5103. ((let ((post (org-element-property :post-affiliated element)))
  5104. (and post (< pos post)))
  5105. (beginning-of-line)
  5106. (let ((case-fold-search t)) (looking-at org-element--affiliated-re))
  5107. (cond
  5108. ((not (member-ignore-case (match-string 1)
  5109. org-element-parsed-keywords))
  5110. (throw 'objects-forbidden element))
  5111. ((< (match-end 0) pos)
  5112. (narrow-to-region (match-end 0) (line-end-position)))
  5113. ((and (match-beginning 2)
  5114. (>= pos (match-beginning 2))
  5115. (< pos (match-end 2)))
  5116. (narrow-to-region (match-beginning 2) (match-end 2)))
  5117. (t (throw 'objects-forbidden element)))
  5118. ;; Also change type to retrieve correct restrictions.
  5119. (setq type 'keyword))
  5120. ;; At an item, objects can only be located within tag, if any.
  5121. ((eq type 'item)
  5122. (let ((tag (org-element-property :tag element)))
  5123. (if (not tag) (throw 'objects-forbidden element)
  5124. (beginning-of-line)
  5125. (search-forward tag (line-end-position))
  5126. (goto-char (match-beginning 0))
  5127. (if (and (>= pos (point)) (< pos (match-end 0)))
  5128. (narrow-to-region (point) (match-end 0))
  5129. (throw 'objects-forbidden element)))))
  5130. ;; At an headline or inlinetask, objects are in title.
  5131. ((memq type '(headline inlinetask))
  5132. (goto-char (org-element-property :begin element))
  5133. (skip-chars-forward "*")
  5134. (if (and (> pos (point)) (< pos (line-end-position)))
  5135. (narrow-to-region (point) (line-end-position))
  5136. (throw 'objects-forbidden element)))
  5137. ;; At a paragraph, a table-row or a verse block, objects are
  5138. ;; located within their contents.
  5139. ((memq type '(paragraph table-row verse-block))
  5140. (let ((cbeg (org-element-property :contents-begin element))
  5141. (cend (org-element-property :contents-end element)))
  5142. ;; CBEG is nil for table rules.
  5143. (if (and cbeg cend (>= pos cbeg)
  5144. (or (< pos cend) (and (= pos cend) (eobp))))
  5145. (narrow-to-region cbeg cend)
  5146. (throw 'objects-forbidden element))))
  5147. ;; At a planning line, if point is at a timestamp, return it,
  5148. ;; otherwise, return element.
  5149. ((eq type 'planning)
  5150. (dolist (p '(:closed :deadline :scheduled))
  5151. (let ((timestamp (org-element-property p element)))
  5152. (when (and timestamp
  5153. (<= (org-element-property :begin timestamp) pos)
  5154. (> (org-element-property :end timestamp) pos))
  5155. (throw 'objects-forbidden timestamp))))
  5156. ;; All other locations cannot contain objects: bail out.
  5157. (throw 'objects-forbidden element))
  5158. (t (throw 'objects-forbidden element)))
  5159. (goto-char (point-min))
  5160. (let ((restriction (org-element-restriction type))
  5161. (parent element)
  5162. (cache (cond ((not (org-element--cache-active-p)) nil)
  5163. (org-element--cache-objects
  5164. (gethash element org-element--cache-objects))
  5165. (t (org-element-cache-reset) nil)))
  5166. next object-data last)
  5167. (prog1
  5168. (catch 'exit
  5169. (while t
  5170. ;; When entering PARENT for the first time, get list
  5171. ;; of objects within known so far. Store it in
  5172. ;; OBJECT-DATA.
  5173. (unless next
  5174. (let ((data (assq parent cache)))
  5175. (if data (setq object-data data)
  5176. (push (setq object-data (list parent nil)) cache))))
  5177. ;; Find NEXT object for analysis.
  5178. (catch 'found
  5179. ;; If NEXT is non-nil, we already exhausted the
  5180. ;; cache so we can parse buffer to find the object
  5181. ;; after it.
  5182. (if next (setq next (org-element--object-lex restriction))
  5183. ;; Otherwise, check if cache can help us.
  5184. (let ((objects (cddr object-data))
  5185. (completep (nth 1 object-data)))
  5186. (cond
  5187. ((and (not objects) completep) (throw 'exit parent))
  5188. ((not objects)
  5189. (setq next (org-element--object-lex restriction)))
  5190. (t
  5191. (let ((cache-limit
  5192. (org-element-property :end (car objects))))
  5193. (if (>= cache-limit pos)
  5194. ;; Cache contains the information needed.
  5195. (dolist (object objects (throw 'exit parent))
  5196. (when (<= (org-element-property :begin object)
  5197. pos)
  5198. (if (>= (org-element-property :end object)
  5199. pos)
  5200. (throw 'found (setq next object))
  5201. (throw 'exit parent))))
  5202. (goto-char cache-limit)
  5203. (setq next
  5204. (org-element--object-lex restriction))))))))
  5205. ;; If we have a new object to analyze, store it in
  5206. ;; cache. Otherwise record that there is nothing
  5207. ;; more to parse in this element at this depth.
  5208. (if next
  5209. (progn (org-element-put-property next :parent parent)
  5210. (push next (cddr object-data)))
  5211. (setcar (cdr object-data) t)))
  5212. ;; Process NEXT, if any, in order to know if we need
  5213. ;; to skip it, return it or move into it.
  5214. (if (or (not next) (> (org-element-property :begin next) pos))
  5215. (throw 'exit (or last parent))
  5216. (let ((end (org-element-property :end next))
  5217. (cbeg (org-element-property :contents-begin next))
  5218. (cend (org-element-property :contents-end next)))
  5219. (cond
  5220. ;; Skip objects ending before point. Also skip
  5221. ;; objects ending at point unless it is also the
  5222. ;; end of buffer, since we want to return the
  5223. ;; innermost object.
  5224. ((and (<= end pos) (/= (point-max) end))
  5225. (goto-char end)
  5226. ;; For convenience, when object ends at POS,
  5227. ;; without any space, store it in LAST, as we
  5228. ;; will return it if no object starts here.
  5229. (when (and (= end pos)
  5230. (not (memq (char-before) '(?\s ?\t))))
  5231. (setq last next)))
  5232. ;; If POS is within a container object, move
  5233. ;; into that object.
  5234. ((and cbeg cend
  5235. (>= pos cbeg)
  5236. (or (< pos cend)
  5237. ;; At contents' end, if there is no
  5238. ;; space before point, also move into
  5239. ;; object, for consistency with
  5240. ;; convenience feature above.
  5241. (and (= pos cend)
  5242. (or (= (point-max) pos)
  5243. (not (memq (char-before pos)
  5244. '(?\s ?\t)))))))
  5245. (goto-char cbeg)
  5246. (narrow-to-region (point) cend)
  5247. (setq parent next
  5248. restriction (org-element-restriction next)
  5249. next nil
  5250. object-data nil))
  5251. ;; Otherwise, return NEXT.
  5252. (t (throw 'exit next)))))))
  5253. ;; Store results in cache, if applicable.
  5254. (org-element--cache-put element cache)))))))
  5255. (defun org-element-lineage (blob &optional types with-self)
  5256. "List all ancestors of a given element or object.
  5257. BLOB is an object or element.
  5258. When optional argument TYPES is a list of symbols, return the
  5259. first element or object in the lineage whose type belongs to that
  5260. list.
  5261. When optional argument WITH-SELF is non-nil, lineage includes
  5262. BLOB itself as the first element, and TYPES, if provided, also
  5263. apply to it.
  5264. When BLOB is obtained through `org-element-context' or
  5265. `org-element-at-point', only ancestors from its section can be
  5266. found. There is no such limitation when BLOB belongs to a full
  5267. parse tree."
  5268. (let ((up (if with-self blob (org-element-property :parent blob)))
  5269. ancestors)
  5270. (while (and up (not (memq (org-element-type up) types)))
  5271. (unless types (push up ancestors))
  5272. (setq up (org-element-property :parent up)))
  5273. (if types up (nreverse ancestors))))
  5274. (defun org-element-nested-p (elem-A elem-B)
  5275. "Non-nil when elements ELEM-A and ELEM-B are nested."
  5276. (let ((beg-A (org-element-property :begin elem-A))
  5277. (beg-B (org-element-property :begin elem-B))
  5278. (end-A (org-element-property :end elem-A))
  5279. (end-B (org-element-property :end elem-B)))
  5280. (or (and (>= beg-A beg-B) (<= end-A end-B))
  5281. (and (>= beg-B beg-A) (<= end-B end-A)))))
  5282. (defun org-element-swap-A-B (elem-A elem-B)
  5283. "Swap elements ELEM-A and ELEM-B.
  5284. Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
  5285. end of ELEM-A."
  5286. (goto-char (org-element-property :begin elem-A))
  5287. ;; There are two special cases when an element doesn't start at bol:
  5288. ;; the first paragraph in an item or in a footnote definition.
  5289. (let ((specialp (not (bolp))))
  5290. ;; Only a paragraph without any affiliated keyword can be moved at
  5291. ;; ELEM-A position in such a situation. Note that the case of
  5292. ;; a footnote definition is impossible: it cannot contain two
  5293. ;; paragraphs in a row because it cannot contain a blank line.
  5294. (if (and specialp
  5295. (or (not (eq (org-element-type elem-B) 'paragraph))
  5296. (/= (org-element-property :begin elem-B)
  5297. (org-element-property :contents-begin elem-B))))
  5298. (error "Cannot swap elements"))
  5299. ;; In a special situation, ELEM-A will have no indentation. We'll
  5300. ;; give it ELEM-B's (which will in, in turn, have no indentation).
  5301. (let* ((ind-B (when specialp
  5302. (goto-char (org-element-property :begin elem-B))
  5303. (org-get-indentation)))
  5304. (beg-A (org-element-property :begin elem-A))
  5305. (end-A (save-excursion
  5306. (goto-char (org-element-property :end elem-A))
  5307. (skip-chars-backward " \r\t\n")
  5308. (point-at-eol)))
  5309. (beg-B (org-element-property :begin elem-B))
  5310. (end-B (save-excursion
  5311. (goto-char (org-element-property :end elem-B))
  5312. (skip-chars-backward " \r\t\n")
  5313. (point-at-eol)))
  5314. ;; Store inner overlays responsible for visibility status.
  5315. ;; We also need to store their boundaries as they will be
  5316. ;; removed from buffer.
  5317. (overlays
  5318. (cons
  5319. (delq nil
  5320. (mapcar (lambda (o)
  5321. (and (>= (overlay-start o) beg-A)
  5322. (<= (overlay-end o) end-A)
  5323. (list o (overlay-start o) (overlay-end o))))
  5324. (overlays-in beg-A end-A)))
  5325. (delq nil
  5326. (mapcar (lambda (o)
  5327. (and (>= (overlay-start o) beg-B)
  5328. (<= (overlay-end o) end-B)
  5329. (list o (overlay-start o) (overlay-end o))))
  5330. (overlays-in beg-B end-B)))))
  5331. ;; Get contents.
  5332. (body-A (buffer-substring beg-A end-A))
  5333. (body-B (delete-and-extract-region beg-B end-B)))
  5334. (goto-char beg-B)
  5335. (when specialp
  5336. (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
  5337. (org-indent-to-column ind-B))
  5338. (insert body-A)
  5339. ;; Restore ex ELEM-A overlays.
  5340. (let ((offset (- beg-B beg-A)))
  5341. (dolist (o (car overlays))
  5342. (move-overlay (car o) (+ (nth 1 o) offset) (+ (nth 2 o) offset)))
  5343. (goto-char beg-A)
  5344. (delete-region beg-A end-A)
  5345. (insert body-B)
  5346. ;; Restore ex ELEM-B overlays.
  5347. (dolist (o (cdr overlays))
  5348. (move-overlay (car o) (- (nth 1 o) offset) (- (nth 2 o) offset))))
  5349. (goto-char (org-element-property :end elem-B)))))
  5350. (defun org-element-remove-indentation (s &optional n)
  5351. "Remove maximum common indentation in string S and return it.
  5352. When optional argument N is a positive integer, remove exactly
  5353. that much characters from indentation, if possible, or return
  5354. S as-is otherwise. Unlike to `org-remove-indentation', this
  5355. function doesn't call `untabify' on S."
  5356. (catch 'exit
  5357. (with-temp-buffer
  5358. (insert s)
  5359. (goto-char (point-min))
  5360. ;; Find maximum common indentation, if not specified.
  5361. (setq n (or n
  5362. (let ((min-ind (point-max)))
  5363. (save-excursion
  5364. (while (re-search-forward "^[ \t]*\\S-" nil t)
  5365. (let ((ind (1- (current-column))))
  5366. (if (zerop ind) (throw 'exit s)
  5367. (setq min-ind (min min-ind ind))))))
  5368. min-ind)))
  5369. (if (zerop n) s
  5370. ;; Remove exactly N indentation, but give up if not possible.
  5371. (while (not (eobp))
  5372. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  5373. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  5374. ((< ind n) (throw 'exit s))
  5375. (t (org-indent-line-to (- ind n))))
  5376. (forward-line)))
  5377. (buffer-string)))))
  5378. (provide 'org-element)
  5379. ;; Local variables:
  5380. ;; generated-autoload-file: "org-loaddefs.el"
  5381. ;; End:
  5382. ;;; org-element.el ends here