org-element.el 200 KB

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