org-element.el 215 KB

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