org-element.el 199 KB

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