org-element.el 216 KB

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