org-element.el 211 KB

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