org-element.el 199 KB

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