org-element.el 163 KB

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