org-element.el 214 KB

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