org-element.el 212 KB

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