org-element.el 171 KB

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