org-element.el 150 KB

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