org-element.el 195 KB

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