org-element.el 199 KB

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