org-element.el 212 KB

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