org-element.el 174 KB

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