org-element.el 212 KB

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