org-element.el 214 KB

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