org-element.el 200 KB

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