org-element.el 212 KB

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