org-element.el 150 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635
  1. ;;; org-element.el --- Parser And Applications for Org syntax
  2. ;; Copyright (C) 2011 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. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Org syntax can be divided into three categories: "Greater
  17. ;; elements", "Elements" and "Objects".
  18. ;; An object can be defined anywhere on a line. It may span over more
  19. ;; than a line but never contains a blank one. Objects belong to the
  20. ;; following types: `emphasis', `entity', `export-snippet',
  21. ;; `footnote-reference', `inline-babel-call', `inline-src-block',
  22. ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
  23. ;; `statistics-cookie', `subscript', `superscript', `target',
  24. ;; `time-stamp' and `verbatim'.
  25. ;; An element always starts and ends at the beginning of a line. The
  26. ;; only element's type containing objects is called a `paragraph'.
  27. ;; Other types are: `comment', `comment-block', `example-block',
  28. ;; `export-block', `fixed-width', `horizontal-rule', `keyword',
  29. ;; `latex-environment', `babel-call', `property-drawer',
  30. ;; `quote-section', `src-block', `table' and `verse-block'.
  31. ;; Elements containing paragraphs are called greater elements.
  32. ;; Concerned types are: `center-block', `drawer', `dynamic-block',
  33. ;; `footnote-definition', `headline', `inlinetask', `item',
  34. ;; `plain-list', `quote-block' and `special-block'.
  35. ;; Greater elements (excepted `headline' and `item' types) and
  36. ;; elements (excepted `keyword', `babel-call', and `property-drawer'
  37. ;; types) can have a fixed set of keywords as attributes. Those are
  38. ;; called "affiliated keywords", to distinguish them from others
  39. ;; keywords, which are full-fledged elements. In particular, the
  40. ;; "name" affiliated keyword allows to label almost any element in an
  41. ;; Org buffer.
  42. ;; Notwithstanding affiliated keywords, each greater element, element
  43. ;; and object has a fixed set of properties attached to it. Among
  44. ;; them, three are shared by all types: `:begin' and `:end', which
  45. ;; refer to the beginning and ending buffer positions of the
  46. ;; considered element or object, and `:post-blank', which holds the
  47. ;; number of blank lines, or white spaces, at its end.
  48. ;; Some elements also have special properties whose value can hold
  49. ;; objects themselves (i.e. an item tag, an headline name, a table
  50. ;; cell). Such values are called "secondary strings".
  51. ;; Lisp-wise, an element or an object can be represented as a list.
  52. ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
  53. ;; TYPE is a symbol describing the Org element or object.
  54. ;; PROPERTIES is the property list attached to it. See docstring of
  55. ;; appropriate parsing function to get an exhaustive
  56. ;; list.
  57. ;; CONTENTS is a list of elements, objects or raw strings contained
  58. ;; in the current element or object, when applicable.
  59. ;; An Org buffer is a nested list of such elements and objects, whose
  60. ;; type is `org-data' and properties is nil.
  61. ;; The first part of this file implements a parser and an interpreter
  62. ;; for each type of Org syntax.
  63. ;; The next two parts introduce two accessors and a function
  64. ;; retrieving the smallest element containing point (respectively
  65. ;; `org-element-get-property', `org-element-get-contents' and
  66. ;; `org-element-at-point').
  67. ;; The following part creates a fully recursive buffer parser. It
  68. ;; also provides a tool to map a function to elements or objects
  69. ;; matching some criteria in the parse tree. Functions of interest
  70. ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
  71. ;; extent, `org-element-parse-secondary-string'.
  72. ;; The penultimate part is the cradle of an interpreter for the
  73. ;; obtained parse tree: `org-element-interpret-data' (and its
  74. ;; relative, `org-element-interpret-secondary').
  75. ;; The library ends by furnishing a set of interactive tools for
  76. ;; element's navigation and manipulation.
  77. ;;; Code:
  78. (eval-when-compile (require 'cl))
  79. (require 'org)
  80. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  81. ;;; Greater elements
  82. ;; For each greater element type, we define a parser and an
  83. ;; interpreter.
  84. ;; A parser (`item''s excepted) accepts no argument and represents the
  85. ;; element or object as the list described above. An interpreter
  86. ;; accepts two arguments: the list representation of the element or
  87. ;; object, and its contents. The latter may be nil, depending on the
  88. ;; element or object considered. It returns the appropriate Org
  89. ;; syntax, as a string.
  90. ;; Parsing functions must follow the naming convention:
  91. ;; org-element-TYPE-parser, where TYPE is greater element's type, as
  92. ;; defined in `org-element-greater-elements'.
  93. ;;
  94. ;; Similarly, interpreting functions must follow the naming
  95. ;; convention: org-element-TYPE-interpreter.
  96. ;; With the exception of `headline' and `item' types, greater elements
  97. ;; cannot contain other greater elements of their own type.
  98. ;; Beside implementing a parser and an interpreter, adding a new
  99. ;; greater element requires to tweak `org-element-guess-type'.
  100. ;; Moreover, the newly defined type must be added to both
  101. ;; `org-element-all-elements' and `org-element-greater-elements'.
  102. ;;;; Center Block
  103. (defun org-element-center-block-parser ()
  104. "Parse a center block.
  105. Return a list whose car is `center-block' and cdr is a plist
  106. containing `:begin', `:end', `:hiddenp', `:contents-begin',
  107. `:contents-end' and `:post-blank' keywords.
  108. Assume point is at beginning or end of the block."
  109. (save-excursion
  110. (let* ((case-fold-search t)
  111. (keywords (progn
  112. (end-of-line)
  113. (re-search-backward
  114. (concat "^[ \t]*#\\+begin_center") nil t)
  115. (org-element-collect-affiliated-keywords)))
  116. (begin (car keywords))
  117. (contents-begin (progn (forward-line) (point)))
  118. (hidden (org-truely-invisible-p))
  119. (contents-end (progn (re-search-forward
  120. (concat "^[ \t]*#\\+end_center") nil t)
  121. (point-at-bol)))
  122. (pos-before-blank (progn (forward-line) (point)))
  123. (end (progn (org-skip-whitespace)
  124. (if (eobp) (point) (point-at-bol)))))
  125. (list 'center-block
  126. `(:begin ,begin
  127. :end ,end
  128. :hiddenp ,hidden
  129. :contents-begin ,contents-begin
  130. :contents-end ,contents-end
  131. :post-blank ,(count-lines pos-before-blank end)
  132. ,@(cadr keywords))))))
  133. (defun org-element-center-block-interpreter (center-block contents)
  134. "Interpret CENTER-BLOCK element as Org syntax.
  135. CONTENTS is the contents of the element."
  136. (format "#+begin_center\n%s#+end_center" contents))
  137. ;;;; Drawer
  138. (defun org-element-drawer-parser ()
  139. "Parse a drawer.
  140. Return a list whose car is `drawer' and cdr is a plist containing
  141. `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
  142. `:contents-end' and `:post-blank' keywords.
  143. Assume point is at beginning of drawer."
  144. (save-excursion
  145. (let* ((case-fold-search t)
  146. (name (progn (looking-at org-drawer-regexp)
  147. (org-match-string-no-properties 1)))
  148. (keywords (org-element-collect-affiliated-keywords))
  149. (begin (car keywords))
  150. (contents-begin (progn (forward-line) (point)))
  151. (hidden (org-truely-invisible-p))
  152. (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t)
  153. (point-at-bol)))
  154. (pos-before-blank (progn (forward-line) (point)))
  155. (end (progn (org-skip-whitespace)
  156. (if (eobp) (point) (point-at-bol)))))
  157. (list 'drawer
  158. `(:begin ,begin
  159. :end ,end
  160. :drawer-name ,name
  161. :hiddenp ,hidden
  162. :contents-begin ,contents-begin
  163. :contents-end ,contents-end
  164. :post-blank ,(count-lines pos-before-blank end)
  165. ,@(cadr keywords))))))
  166. (defun org-element-drawer-interpreter (drawer contents)
  167. "Interpret DRAWER element as Org syntax.
  168. CONTENTS is the contents of the element."
  169. (format ":%s:\n%s:END:"
  170. (org-element-get-property :drawer-name drawer)
  171. contents))
  172. ;;;; Dynamic Block
  173. (defun org-element-dynamic-block-parser ()
  174. "Parse a dynamic block.
  175. Return a list whose car is `dynamic-block' and cdr is a plist
  176. containing `:block-name', `:begin', `:end', `:hiddenp',
  177. `:contents-begin', `:contents-end', `:arguments' and
  178. `:post-blank' keywords.
  179. Assume point is at beginning of dynamic block."
  180. (save-excursion
  181. (let* ((case-fold-search t)
  182. (name (progn (looking-at org-dblock-start-re)
  183. (org-match-string-no-properties 1)))
  184. (arguments (org-match-string-no-properties 3))
  185. (keywords (org-element-collect-affiliated-keywords))
  186. (begin (car keywords))
  187. (contents-begin (progn (forward-line) (point)))
  188. (hidden (org-truely-invisible-p))
  189. (contents-end (progn (re-search-forward org-dblock-end-re nil t)
  190. (point-at-bol)))
  191. (pos-before-blank (progn (forward-line) (point)))
  192. (end (progn (org-skip-whitespace)
  193. (if (eobp) (point) (point-at-bol)))))
  194. (list 'dynamic-block
  195. `(:begin ,begin
  196. :end ,end
  197. :block-name ,name
  198. :arguments ,arguments
  199. :hiddenp ,hidden
  200. :contents-begin ,contents-begin
  201. :contents-end ,contents-end
  202. :post-blank ,(count-lines pos-before-blank end)
  203. ,@(cadr keywords))))))
  204. (defun org-element-dynamic-block-interpreter (dynamic-block contents)
  205. "Interpret DYNAMIC-BLOCK element as Org syntax.
  206. CONTENTS is the contents of the element."
  207. (format "#+BEGIN: %s%s\n%s#+END:"
  208. (org-element-get-property :block-name dynamic-block)
  209. (let ((args (org-element-get-property :arguments dynamic-block)))
  210. (and arg (concat " " args)))
  211. contents))
  212. ;;;; Footnote Definition
  213. (defun org-element-footnote-definition-parser ()
  214. "Parse a footnote definition.
  215. Return a list whose car is `footnote-definition' and cdr is
  216. a plist containing `:label', `:begin' `:end', `:contents-begin',
  217. `contents-end' and `:post-blank' keywords."
  218. (save-excursion
  219. (let* ((f-def (org-footnote-at-definition-p))
  220. (label (car f-def))
  221. (keywords (progn (goto-char (nth 1 f-def))
  222. (org-element-collect-affiliated-keywords)))
  223. (begin (car keywords))
  224. (contents-begin (progn (looking-at (concat "\\[" label "\\]"))
  225. (goto-char (match-end 0))
  226. (org-skip-whitespace)
  227. (point)))
  228. (end (goto-char (nth 2 f-def)))
  229. (contents-end (progn (skip-chars-backward " \r\t\n")
  230. (forward-line)
  231. (point))))
  232. (list 'footnote-definition
  233. `(:label ,label
  234. :begin ,begin
  235. :end ,end
  236. :contents-begin ,contents-begin
  237. :contents-end ,contents-end
  238. :post-blank ,(count-lines contents-end end)
  239. ,@(cadr keywords))))))
  240. (defun org-element-footnote-definition-interpreter (footnote-definition contents)
  241. "Interpret FOOTNOTE-DEFINITION element as Org syntax.
  242. CONTENTS is the contents of the footnote-definition."
  243. (concat (format "[%s]" (org-element-get-property :label footnote-definition))
  244. " "
  245. contents))
  246. ;;;; Headline
  247. (defun org-element-headline-parser ()
  248. "Parse an headline.
  249. Return a list whose car is `headline' and cdr is a plist
  250. containing `:raw-value', `:title', `:begin', `:end',
  251. `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
  252. `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
  253. `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
  254. `:custom-id', `:id', `:quotedp', `:archivedp', `:commentedp',
  255. `:last-sibling-p' and `:footnote-section-p' keywords.
  256. The plist also contains any property set in the property drawer,
  257. with its name in lowercase, the underscores replaced with hyphens
  258. and colons at the beginning (i.e. `:custom-id').
  259. Assume point is at beginning of the headline."
  260. (save-excursion
  261. (let* ((components (org-heading-components))
  262. (level (nth 1 components))
  263. (todo (nth 2 components))
  264. (todo-type (and todo
  265. (if (member todo org-done-keywords) 'done 'todo)))
  266. (tags (nth 5 components))
  267. (raw-value (nth 4 components))
  268. (quotedp (string-match (format "^%s +" org-quote-string) raw-value))
  269. (commentedp (string-match
  270. (format "^%s +" org-comment-string) raw-value))
  271. (archivedp (and tags
  272. (string-match (format ":%s:" org-archive-tag) tags)))
  273. (footnote-section-p (and org-footnote-section
  274. (string= org-footnote-section raw-value)))
  275. (standard-props (let (plist)
  276. (mapc
  277. (lambda (p)
  278. (let ((p-name (downcase (car p))))
  279. (while (string-match "_" p-name)
  280. (setq p-name
  281. (replace-match "-" nil nil p-name)))
  282. (setq p-name (intern (concat ":" p-name)))
  283. (setq plist
  284. (plist-put plist p-name (cdr p)))))
  285. (org-entry-properties nil 'standard))
  286. plist))
  287. (time-props (org-entry-properties nil 'special "CLOCK"))
  288. (scheduled (cdr (assoc "SCHEDULED" time-props)))
  289. (deadline (cdr (assoc "DEADLINE" time-props)))
  290. (clock (cdr (assoc "CLOCK" time-props)))
  291. (timestamp (cdr (assoc "TIMESTAMP" time-props)))
  292. (begin (point))
  293. (pos-after-head (save-excursion (forward-line) (point)))
  294. (contents-begin (save-excursion (forward-line)
  295. (org-skip-whitespace)
  296. (if (eobp) (point) (point-at-bol))))
  297. (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
  298. (end (progn (goto-char (org-end-of-subtree t t))))
  299. (contents-end (progn (skip-chars-backward " \r\t\n")
  300. (forward-line)
  301. (point)))
  302. title)
  303. ;; Clean RAW-VALUE from any quote or comment string.
  304. (when (or quotedp commentedp)
  305. (setq raw-value
  306. (replace-regexp-in-string
  307. (concat "\\(" org-quote-string "\\|" org-comment-string "\\) +")
  308. ""
  309. raw-value)))
  310. ;; Clean TAGS from archive tag, if any.
  311. (when archivedp
  312. (setq tags
  313. (and (not (string= tags (format ":%s:" org-archive-tag)))
  314. (replace-regexp-in-string
  315. (concat org-archive-tag ":") "" tags)))
  316. (when (string= tags ":") (setq tags nil)))
  317. ;; Then get TITLE.
  318. (setq title (org-element-parse-secondary-string
  319. raw-value
  320. (cdr (assq 'headline org-element-string-restrictions))))
  321. (list 'headline
  322. `(:raw-value ,raw-value
  323. :title ,title
  324. :begin ,begin
  325. :end ,end
  326. :pre-blank ,(count-lines pos-after-head contents-begin)
  327. :hiddenp ,hidden
  328. :contents-begin ,contents-begin
  329. :contents-end ,contents-end
  330. :level ,level
  331. :priority ,(nth 3 components)
  332. :tags ,tags
  333. :todo-keyword ,todo
  334. :todo-type ,todo-type
  335. :scheduled ,scheduled
  336. :deadline ,deadline
  337. :timestamp ,timestamp
  338. :clock ,clock
  339. :post-blank ,(count-lines contents-end end)
  340. :footnote-section-p ,footnote-section-p
  341. :archivedp ,archivedp
  342. :commentedp ,commentedp
  343. :quotedp ,quotedp
  344. ,@standard-props)))))
  345. (defun org-element-headline-interpreter (headline contents)
  346. "Interpret HEADLINE element as Org syntax.
  347. CONTENTS is the contents of the element."
  348. (let* ((level (org-element-get-property :level headline))
  349. (todo (org-element-get-property :todo-keyword headline))
  350. (priority (org-element-get-property :priority headline))
  351. (title (org-element-get-property :raw-value headline))
  352. (tags (let ((tag-string (org-element-get-property :tags headline))
  353. (archivedp (org-element-get-property :archivedp headline)))
  354. (cond
  355. ((and (not tag-string) archivedp)
  356. (format ":%s:" org-archive-tag))
  357. (archivedp (concat ":" org-archive-tag tag-string))
  358. (t tag-string))))
  359. (commentedp (org-element-get-property :commentedp headline))
  360. (quotedp (org-element-get-property :quotedp headline))
  361. (pre-blank (org-element-get-property :pre-blank headline))
  362. (heading (concat (make-string level ?*)
  363. (and todo (concat " " todo))
  364. (and quotedp (concat " " org-quote-string))
  365. (and commentedp (concat " " org-comment-string))
  366. (and priority (concat " " priority))
  367. (cond ((and org-footnote-section
  368. (org-element-get-property
  369. :footnote-section-p headline))
  370. (concat " " org-footnote-section))
  371. (title (concat " " title)))))
  372. ;; Align tags.
  373. (tags-fmt (when tags
  374. (let ((tags-len (length tags)))
  375. (format "%% %ds"
  376. (cond
  377. ((zerop org-tags-column) (1+ tags-len))
  378. ((< org-tags-column 0)
  379. (max (- (+ org-tags-column (length heading)))
  380. (1+ tags-len)))
  381. (t (max (+ (- org-tags-column (length heading))
  382. tags-len)
  383. (1+ tags-len)))))))))
  384. (concat heading (and tags (format tags-fmt tags))
  385. (make-string (1+ pre-blank) 10)
  386. contents)))
  387. ;;;; Inlinetask
  388. (defun org-element-inlinetask-parser ()
  389. "Parse an inline task.
  390. Return a list whose car is `inlinetask' and cdr is a plist
  391. containing `:raw-value', `:title', `:begin', `:end', `:hiddenp',
  392. `:contents-begin' and `:contents-end', `:level', `:with-priority',
  393. `tags:', `todo-keyword', `todo-type', and `:post-blank' keywords.
  394. Assume point is at beginning of the inline task."
  395. (save-excursion
  396. (let* ((keywords (org-element-collect-affiliated-keywords))
  397. (begin (car keywords))
  398. (components (org-heading-components))
  399. (todo (nth 2 components))
  400. (todo-type (and todo
  401. (if (member todo org-done-keywords) 'done 'todo)))
  402. (raw-value (nth 4 components))
  403. (title (org-element-parse-secondary-string
  404. raw-value
  405. (cdr (assq 'inlinetask org-element-string-restrictions))))
  406. (contents-begin (save-excursion (forward-line) (point)))
  407. (hidden (org-truely-invisible-p))
  408. (pos-before-blank (org-inlinetask-goto-end))
  409. ;; In the case of a single line task, CONTENTS-BEGIN and
  410. ;; CONTENTS-END might overlap.
  411. (contents-end (max contents-begin
  412. (save-excursion (forward-line -1) (point))))
  413. (end (progn (org-skip-whitespace)
  414. (if (eobp) (point) (point-at-bol)))))
  415. (list 'inlinetask
  416. `(:raw-value ,raw-value
  417. :title ,title
  418. :begin ,begin
  419. :end ,end
  420. :hiddenp ,(and (> contents-end contents-begin) hidden)
  421. :contents-begin ,contents-begin
  422. :contents-end ,contents-end
  423. :level ,(nth 1 components)
  424. :with-priority ,(nth 3 components)
  425. :with-tags ,(nth 5 components)
  426. :todo-keyword ,todo
  427. :todo-type ,todo-type
  428. :post-blank ,(count-lines pos-before-blank end)
  429. ,@(cadr keywords))))))
  430. (defun org-element-inlinetask-interpreter (inlinetask contents)
  431. "Interpret INLINETASK element as Org syntax.
  432. CONTENTS is the contents of inlinetask."
  433. (let* ((level (org-element-get-property :level inlinetask))
  434. (todo (org-element-get-property :todo-keyword inlinetask))
  435. (priority (org-element-get-property :priority inlinetask))
  436. (title (org-element-get-property :raw-value inlinetask))
  437. (tags (org-element-get-property :tags inlinetask))
  438. (task (concat (make-string level ?*)
  439. (and todo (concat " " todo))
  440. (and priority (concat " " priority))
  441. (and title (concat " " title))))
  442. ;; Align tags.
  443. (tags-fmt (when tags
  444. (format "%% %ds"
  445. (cond
  446. ((zerop org-tags-column) 1)
  447. ((< 0 org-tags-column)
  448. (max (+ org-tags-column
  449. (length inlinetask)
  450. (length tags))
  451. 1))
  452. (t (max (- org-tags-column (length inlinetask))
  453. 1)))))))
  454. (concat inlinetask (and tags (format tags-fmt tags) "\n" contents))))
  455. ;;;; Item
  456. (defun org-element-item-parser (struct)
  457. "Parse an item.
  458. STRUCT is the structure of the plain list.
  459. Return a list whose car is `item' and cdr is a plist containing
  460. `:begin', `:end', `:contents-begin', `:contents-end',
  461. `:checkbox', `:counter', `:tag' and `:hiddenp'.
  462. Assume point is at the beginning of the item."
  463. (save-excursion
  464. (beginning-of-line)
  465. (let* ((begin (point))
  466. (bullet (org-list-get-bullet (point) struct))
  467. (checkbox (let ((box (org-list-get-checkbox begin struct)))
  468. (cond ((equal "[ ]" box) 'off)
  469. ((equal "[X]" box) 'on)
  470. ((equal "[-]" box) 'trans))))
  471. (counter (let ((c (org-list-get-counter begin struct)))
  472. (cond
  473. ((not c) nil)
  474. ((string-match "[A-Za-z]" c)
  475. (- (string-to-char (upcase (match-string 0 c)))
  476. 64))
  477. ((string-match "[0-9]+" c)
  478. (string-to-number (match-string 0 c))))))
  479. (raw-tag (org-list-get-tag begin struct))
  480. (tag (and raw-tag
  481. (org-element-parse-secondary-string
  482. raw-tag
  483. (cdr (assq 'item org-element-string-restrictions)))))
  484. (end (org-list-get-item-end begin struct))
  485. (contents-begin (progn (looking-at org-list-full-item-re)
  486. (goto-char (match-end 0))
  487. (org-skip-whitespace)
  488. (if (>= (point) end)
  489. (point-at-bol)
  490. (point))))
  491. (hidden (progn (forward-line)
  492. (and (not (= (point) end))
  493. (org-truely-invisible-p))))
  494. (contents-end (progn (goto-char end)
  495. (skip-chars-backward " \r\t\n")
  496. (forward-line)
  497. (point))))
  498. ;; Note: CONTENTS-BEGIN and CONTENTS-END can be mixed up in the
  499. ;; case of an empty item separated from the next by a blank
  500. ;; line.
  501. (list 'item
  502. `(:bullet ,bullet
  503. :begin ,begin
  504. :end ,end
  505. :contents-begin ,(min contents-begin contents-end)
  506. :contents-end ,(max contents-begin contents-end)
  507. :checkbox ,checkbox
  508. :counter ,counter
  509. :raw-tag ,raw-tag
  510. :tag ,tag
  511. :hiddenp ,hidden
  512. :structure ,struct
  513. :post-blank ,(count-lines contents-end end))))))
  514. (defun org-element-item-interpreter (item contents)
  515. "Interpret ITEM element as Org syntax.
  516. CONTENTS is the contents of the element."
  517. (let* ((bullet (org-element-get-property :bullet item))
  518. (checkbox (org-element-get-property :checkbox item))
  519. (counter (org-element-get-property :counter item))
  520. (tag (org-element-get-property :raw-tag item))
  521. ;; Compute indentation.
  522. (ind (make-string (length bullet) 32)))
  523. ;; Indent contents.
  524. (concat
  525. bullet
  526. (when (and org-list-two-spaces-after-bullet-regexp
  527. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  528. " ")
  529. (and counter (format "[@%d] " counter))
  530. (cond
  531. ((eq checkbox 'on) "[X] ")
  532. ((eq checkbox 'off) "[ ] ")
  533. ((eq checkbox 'trans) "[-] "))
  534. (and tag (format "%s :: " tag))
  535. (org-trim
  536. (replace-regexp-in-string
  537. "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))))
  538. ;;;; Plain List
  539. (defun org-element-plain-list-parser (&optional structure)
  540. "Parse a plain list.
  541. Return a list whose car is `plain-list' and cdr is a plist
  542. containing `:type', `:begin', `:end', `:contents-begin' and
  543. `:contents-end', `:level', `:structure' and `:post-blank'
  544. keywords.
  545. Assume point is at one of the list items."
  546. (save-excursion
  547. (let* ((struct (or structure (org-list-struct)))
  548. (prevs (org-list-prevs-alist struct))
  549. (parents (org-list-parents-alist struct))
  550. (type (org-list-get-list-type (point) struct prevs))
  551. (contents-begin (goto-char
  552. (org-list-get-list-begin (point) struct prevs)))
  553. (keywords (org-element-collect-affiliated-keywords))
  554. (begin (car keywords))
  555. (contents-end (goto-char
  556. (org-list-get-list-end (point) struct prevs)))
  557. (end (save-excursion (org-skip-whitespace)
  558. (if (eobp) (point) (point-at-bol))))
  559. (level 0))
  560. ;; Get list level.
  561. (let ((item contents-begin))
  562. (while (setq item
  563. (org-list-get-parent
  564. (org-list-get-list-begin item struct prevs)
  565. struct parents))
  566. (incf level)))
  567. ;; Blank lines below list belong to the top-level list only.
  568. (when (> level 0)
  569. (setq end (min (org-list-get-bottom-point struct)
  570. (progn (org-skip-whitespace)
  571. (if (eobp) (point) (point-at-bol))))))
  572. ;; Return value.
  573. (list 'plain-list
  574. `(:type ,type
  575. :begin ,begin
  576. :end ,end
  577. :contents-begin ,contents-begin
  578. :contents-end ,contents-end
  579. :level ,level
  580. :structure ,struct
  581. :post-blank ,(count-lines contents-end end)
  582. ,@(cadr keywords))))))
  583. (defun org-element-plain-list-interpreter (plain-list contents)
  584. "Interpret PLAIN-LIST element as Org syntax.
  585. CONTENTS is the contents of the element."
  586. contents)
  587. ;;;; Quote Block
  588. (defun org-element-quote-block-parser ()
  589. "Parse a quote block.
  590. Return a list whose car is `quote-block' and cdr is a plist
  591. containing `:begin', `:end', `:hiddenp', `:contents-begin',
  592. `:contents-end' and `:post-blank' keywords.
  593. Assume point is at beginning or end of the block."
  594. (save-excursion
  595. (let* ((case-fold-search t)
  596. (keywords (progn
  597. (end-of-line)
  598. (re-search-backward
  599. (concat "^[ \t]*#\\+begin_quote") nil t)
  600. (org-element-collect-affiliated-keywords)))
  601. (begin (car keywords))
  602. (contents-begin (progn (forward-line) (point)))
  603. (hidden (org-truely-invisible-p))
  604. (contents-end (progn (re-search-forward
  605. (concat "^[ \t]*#\\+end_quote") nil t)
  606. (point-at-bol)))
  607. (pos-before-blank (progn (forward-line) (point)))
  608. (end (progn (org-skip-whitespace)
  609. (if (eobp) (point) (point-at-bol)))))
  610. (list 'quote-block
  611. `(:begin ,begin
  612. :end ,end
  613. :hiddenp ,hidden
  614. :contents-begin ,contents-begin
  615. :contents-end ,contents-end
  616. :post-blank ,(count-lines pos-before-blank end)
  617. ,@(cadr keywords))))))
  618. (defun org-element-quote-block-interpreter (quote-block contents)
  619. "Interpret QUOTE-BLOCK element as Org syntax.
  620. CONTENTS is the contents of the element."
  621. (format "#+begin_quote\n%s#+end_quote" contents))
  622. ;;;; Special Block
  623. (defun org-element-special-block-parser ()
  624. "Parse a special block.
  625. Return a list whose car is `special-block' and cdr is a plist
  626. containing `:type', `:begin', `:end', `:hiddenp',
  627. `:contents-begin', `:contents-end' and `:post-blank' keywords.
  628. Assume point is at beginning or end of the block."
  629. (save-excursion
  630. (let* ((case-fold-search t)
  631. (type (progn (looking-at
  632. "[ \t]*#\\+\\(?:begin\\|end\\)_\\([-A-Za-z0-9]+\\)")
  633. (org-match-string-no-properties 1)))
  634. (keywords (progn
  635. (end-of-line)
  636. (re-search-backward
  637. (concat "^[ \t]*#\\+begin_" type) nil t)
  638. (org-element-collect-affiliated-keywords)))
  639. (begin (car keywords))
  640. (contents-begin (progn (forward-line) (point)))
  641. (hidden (org-truely-invisible-p))
  642. (contents-end (progn (re-search-forward
  643. (concat "^[ \t]*#\\+end_" type) nil t)
  644. (point-at-bol)))
  645. (pos-before-blank (progn (forward-line) (point)))
  646. (end (progn (org-skip-whitespace)
  647. (if (eobp) (point) (point-at-bol)))))
  648. (list 'special-block
  649. `(:type ,type
  650. :begin ,begin
  651. :end ,end
  652. :hiddenp ,hidden
  653. :contents-begin ,contents-begin
  654. :contents-end ,contents-end
  655. :post-blank ,(count-lines pos-before-blank end)
  656. ,@(cadr keywords))))))
  657. (defun org-element-special-block-interpreter (special-block contents)
  658. "Interpret SPECIAL-BLOCK element as Org syntax.
  659. CONTENTS is the contents of the element."
  660. (let ((block-type (org-element-get-property :type special-block)))
  661. (format "#+begin_%s\n%s#+end_%s" block-type contents block-type)))
  662. ;;; Elements
  663. ;; For each element, a parser and an interpreter are also defined.
  664. ;; Both follow the same naming convention used for greater elements.
  665. ;; Also, as for greater elements, adding a new element type is done
  666. ;; through the following steps: implement a parser and an interpreter,
  667. ;; tweak `org-element-guess-type' so that it recognizes the new type
  668. ;; and add that new type to `org-element-all-elements'.
  669. ;; As a special case, when the newly defined type is a block type,
  670. ;; `org-element-non-recursive-block-alist' has to be modified
  671. ;; accordingly.
  672. ;;;; Babel Call
  673. (defun org-element-babel-call-parser ()
  674. "Parse a babel call.
  675. Return a list whose car is `babel-call' and cdr is a plist
  676. containing `:begin', `:end', `:info' and `:post-blank' as
  677. keywords."
  678. (save-excursion
  679. (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp)
  680. (org-babel-lob-get-info)))
  681. (beg (point-at-bol))
  682. (pos-before-blank (progn (forward-line) (point)))
  683. (end (progn (org-skip-whitespace)
  684. (if (eobp) (point) (point-at-bol)))))
  685. (list 'babel-call
  686. `(:beg ,beg
  687. :end ,end
  688. :info ,info
  689. :post-blank ,(count-lines pos-before-blank end))))))
  690. (defun org-element-babel-call-interpreter (inline-babel-call contents)
  691. "Interpret INLINE-BABEL-CALL object as Org syntax.
  692. CONTENTS is nil."
  693. (let* ((babel-info (org-element-get-property :info inline-babel-call))
  694. (main-source (car babel-info))
  695. (post-options (nth 1 babel-info)))
  696. (concat "#+call: "
  697. (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
  698. ;; Remove redundant square brackets.
  699. (replace-match
  700. (match-string 1 main-source) nil nil main-source)
  701. main-source)
  702. (and post-options (format "[%s]" post-options)))))
  703. ;;;; Comment
  704. (defun org-element-comment-parser ()
  705. "Parse a comment.
  706. Return a list whose car is `comment' and cdr is a plist
  707. containing `:begin', `:end', `:value' and `:post-blank'
  708. keywords."
  709. (let ((comment-re "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)")
  710. beg-coms begin end value pos-before-blank keywords)
  711. (save-excursion
  712. ;; Move to the beginning of comments.
  713. (unless (bobp)
  714. (while (and (not (bobp)) (looking-at comment-re))
  715. (forward-line -1))
  716. (unless (looking-at comment-re) (forward-line 1)))
  717. (setq beg-coms (point))
  718. ;; Get affiliated keywords, if any.
  719. (setq keywords (org-element-collect-affiliated-keywords))
  720. ;; Store true beginning of element.
  721. (setq begin (car keywords))
  722. ;; Get ending of comments. If point is in a list, ensure to not
  723. ;; get outside of it.
  724. (let* ((itemp (org-in-item-p))
  725. (max-pos (if itemp
  726. (org-list-get-bottom-point
  727. (save-excursion (goto-char itemp) (org-list-struct)))
  728. (point-max))))
  729. (while (and (looking-at comment-re) (< (point) max-pos))
  730. (forward-line)))
  731. (setq pos-before-blank (point))
  732. ;; Find position after blank.
  733. (org-skip-whitespace)
  734. (setq end (if (eobp) (point) (point-at-bol)))
  735. ;; Extract value.
  736. (setq value (buffer-substring-no-properties beg-coms pos-before-blank)))
  737. (list 'comment
  738. `(:begin ,begin
  739. :end ,end
  740. :value ,value
  741. :post-blank ,(count-lines pos-before-blank end)
  742. ,@(cadr keywords)))))
  743. (defun org-element-comment-interpreter (comment contents)
  744. "Interpret COMMENT element as Org syntax.
  745. CONTENTS is nil."
  746. (org-element-get-property :value comment))
  747. ;;;; Comment Block
  748. (defun org-element-comment-block-parser ()
  749. "Parse an export block.
  750. Return a list whose car is `comment-block' and cdr is a plist
  751. containing `:begin', `:end', `:hiddenp', `:value' and
  752. `:post-blank' keywords."
  753. (save-excursion
  754. (end-of-line)
  755. (let* ((case-fold-search t)
  756. (keywords (progn
  757. (re-search-backward "^[ \t]*#\\+begin_comment" nil t)
  758. (org-element-collect-affiliated-keywords)))
  759. (begin (car keywords))
  760. (contents-begin (progn (forward-line) (point)))
  761. (hidden (org-truely-invisible-p))
  762. (contents-end (progn (re-search-forward
  763. "^[ \t]*#\\+end_comment" nil t)
  764. (point-at-bol)))
  765. (pos-before-blank (progn (forward-line) (point)))
  766. (end (progn (org-skip-whitespace)
  767. (if (eobp) (point) (point-at-bol))))
  768. (value (buffer-substring-no-properties contents-begin contents-end)))
  769. (list 'comment-block
  770. `(:begin ,begin
  771. :end ,end
  772. :value ,value
  773. :hiddenp ,hidden
  774. :post-blank ,(count-lines pos-before-blank end)
  775. ,@(cadr keywords))))))
  776. (defun org-element-comment-block-interpreter (comment-block contents)
  777. "Interpret COMMENT-BLOCK element as Org syntax.
  778. CONTENTS is nil."
  779. (concat "#+begin_comment\n"
  780. (org-remove-indentation
  781. (org-element-get-property :value comment-block))
  782. "#+begin_comment"))
  783. ;;;; Example Block
  784. (defun org-element-example-block-parser ()
  785. "Parse an example block.
  786. Return a list whose car is `example' and cdr is a plist
  787. containing `:begin', `:end', `:options', `:hiddenp', `:value' and
  788. `:post-blank' keywords."
  789. (save-excursion
  790. (end-of-line)
  791. (let* ((case-fold-search t)
  792. (options (progn
  793. (re-search-backward
  794. "^[ \t]*#\\+begin_example\\(?: +\\(.*\\)\\)?" nil t)
  795. (org-match-string-no-properties 1)))
  796. (keywords (org-element-collect-affiliated-keywords))
  797. (begin (car keywords))
  798. (contents-begin (progn (forward-line) (point)))
  799. (hidden (org-truely-invisible-p))
  800. (contents-end (progn
  801. (re-search-forward "^[ \t]*#\\+end_example" nil t)
  802. (point-at-bol)))
  803. (value (buffer-substring-no-properties contents-begin contents-end))
  804. (pos-before-blank (progn (forward-line) (point)))
  805. (end (progn (org-skip-whitespace)
  806. (if (eobp) (point) (point-at-bol)))))
  807. (list 'example-block
  808. `(:begin ,begin
  809. :end ,end
  810. :value ,value
  811. :options ,options
  812. :hiddenp ,hidden
  813. :post-blank ,(count-lines pos-before-blank end)
  814. ,@(cadr keywords))))))
  815. (defun org-element-example-block-interpreter (example-block contents)
  816. "Interpret EXAMPLE-BLOCK element as Org syntax.
  817. CONTENTS is nil."
  818. (let ((options (org-element-get-property :options example-block)))
  819. (concat "#+begin_example" (and options (concat " " options)) "\n"
  820. (org-remove-indentation
  821. (org-element-get-property :value example-block))
  822. "#+end_example")))
  823. ;;;; Export Block
  824. (defun org-element-export-block-parser ()
  825. "Parse an export block.
  826. Return a list whose car is `export-block' and cdr is a plist
  827. containing `:begin', `:end', `:type', `:hiddenp', `:value' and
  828. `:post-blank' keywords."
  829. (save-excursion
  830. (end-of-line)
  831. (let* ((case-fold-search t)
  832. (contents)
  833. (type (progn (re-search-backward
  834. (concat "[ \t]*#\\+begin_"
  835. (org-re "\\([[:alnum:]]+\\)")))
  836. (downcase (org-match-string-no-properties 1))))
  837. (keywords (org-element-collect-affiliated-keywords))
  838. (begin (car keywords))
  839. (contents-begin (progn (forward-line) (point)))
  840. (hidden (org-truely-invisible-p))
  841. (contents-end (progn (re-search-forward
  842. (concat "^[ \t]*#\\+end_" type) nil t)
  843. (point-at-bol)))
  844. (pos-before-blank (progn (forward-line) (point)))
  845. (end (progn (org-skip-whitespace)
  846. (if (eobp) (point) (point-at-bol))))
  847. (value (buffer-substring-no-properties contents-begin contents-end)))
  848. (list 'export-block
  849. `(:begin ,begin
  850. :end ,end
  851. :type ,type
  852. :value ,value
  853. :hiddenp ,hidden
  854. :post-blank ,(count-lines pos-before-blank end)
  855. ,@(cadr keywords))))))
  856. (defun org-element-export-block-interpreter (export-block contents)
  857. "Interpret EXPORT-BLOCK element as Org syntax.
  858. CONTENTS is nil."
  859. (let ((type (org-element-get-property :type export-block)))
  860. (concat (format "#+begin_%s\n" type)
  861. (org-element-get-property :value export-block)
  862. (format "#+end_%s" type))))
  863. ;;;; Fixed-width
  864. (defun org-element-fixed-width-parser ()
  865. "Parse a fixed-width section.
  866. Return a list whose car is `fixed-width' and cdr is a plist
  867. containing `:begin', `:end', `:value' and `:post-blank'
  868. keywords."
  869. (let ((fixed-re "[ \t]*:\\( \\|$\\)")
  870. beg-area begin end value pos-before-blank keywords)
  871. (save-excursion
  872. ;; Move to the beginning of the fixed-width area.
  873. (unless (bobp)
  874. (while (and (not (bobp)) (looking-at fixed-re))
  875. (forward-line -1))
  876. (unless (looking-at fixed-re) (forward-line 1)))
  877. (setq beg-area (point))
  878. ;; Get affiliated keywords, if any.
  879. (setq keywords (org-element-collect-affiliated-keywords))
  880. ;; Store true beginning of element.
  881. (setq begin (car keywords))
  882. ;; Get ending of fixed-width area. If point is in a list,
  883. ;; ensure to not get outside of it.
  884. (let* ((itemp (org-in-item-p))
  885. (max-pos (if itemp
  886. (org-list-get-bottom-point
  887. (save-excursion (goto-char itemp) (org-list-struct)))
  888. (point-max))))
  889. (while (and (looking-at fixed-re) (< (point) max-pos))
  890. (forward-line)))
  891. (setq pos-before-blank (point))
  892. ;; Find position after blank
  893. (org-skip-whitespace)
  894. (setq end (if (eobp) (point) (point-at-bol)))
  895. ;; Extract value.
  896. (setq value (buffer-substring-no-properties beg-area pos-before-blank)))
  897. (list 'fixed-width
  898. `(:begin ,begin
  899. :end ,end
  900. :value ,value
  901. :post-blank ,(count-lines pos-before-blank end)
  902. ,@(cadr keywords)))))
  903. (defun org-element-fixed-width-interpreter (fixed-width contents)
  904. "Interpret FIXED-WIDTH element as Org syntax.
  905. CONTENTS is nil."
  906. (org-remove-indentation (org-element-get-property :value fixed-width)))
  907. ;;;; Horizontal Rule
  908. (defun org-element-horizontal-rule-parser ()
  909. "Parse an horizontal rule.
  910. Return a list whose car is `horizontal-rule' and cdr is
  911. a plist containing `:begin', `:end' and `:post-blank'
  912. keywords."
  913. (save-excursion
  914. (let* ((keywords (org-element-collect-affiliated-keywords))
  915. (begin (car keywords))
  916. (post-hr (progn (forward-line) (point)))
  917. (end (progn (org-skip-whitespace)
  918. (if (eobp) (point) (point-at-bol)))))
  919. (list 'horizontal-rule
  920. `(:begin ,begin
  921. :end ,end
  922. :post-blank ,(count-lines post-hr end)
  923. ,@(cadr keywords))))))
  924. (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
  925. "Interpret HORIZONTAL-RULE element as Org syntax.
  926. CONTENTS is nil."
  927. "-----")
  928. ;;;; Keyword
  929. (defun org-element-keyword-parser ()
  930. "Parse a keyword at point.
  931. Return a list whose car is `keyword' and cdr is a plist
  932. containing `:key', `:value', `:begin', `:end' and `:post-blank'
  933. keywords."
  934. (save-excursion
  935. (let* ((begin (point))
  936. (key (progn (looking-at
  937. "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
  938. (org-match-string-no-properties 1)))
  939. (value (org-trim (buffer-substring-no-properties
  940. (match-end 0) (point-at-eol))))
  941. (pos-before-blank (progn (forward-line) (point)))
  942. (end (progn (org-skip-whitespace)
  943. (if (eobp) (point) (point-at-bol)))))
  944. (list 'keyword
  945. `(:key ,key
  946. :value ,value
  947. :begin ,begin
  948. :end ,end
  949. :post-blank ,(count-lines pos-before-blank end))))))
  950. (defun org-element-keyword-interpreter (keyword contents)
  951. "Interpret KEYWORD element as Org syntax.
  952. CONTENTS is nil."
  953. (format "#+%s: %s"
  954. (org-element-get-property :key keyword)
  955. (org-element-get-property :value keyword)))
  956. ;;;; Latex Environment
  957. (defun org-element-latex-environment-parser ()
  958. "Parse a LaTeX environment.
  959. Return a list whose car is `latex-environment' and cdr is a plist
  960. containing `:begin', `:end', `:value' and `:post-blank' keywords."
  961. (save-excursion
  962. (end-of-line)
  963. (let* ((case-fold-search t)
  964. (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t))
  965. (keywords (org-element-collect-affiliated-keywords))
  966. (begin (car keywords))
  967. (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
  968. (forward-line)
  969. (point)))
  970. (value (buffer-substring-no-properties contents-begin contents-end))
  971. (end (progn (org-skip-whitespace)
  972. (if (eobp) (point) (point-at-bol)))))
  973. (list 'latex-environment
  974. `(:begin ,begin
  975. :end ,end
  976. :value ,value
  977. :post-blank ,(count-lines contents-end end)
  978. ,@(cadr keywords))))))
  979. (defun org-element-latex-environment-interpreter (latex-environment contents)
  980. "Interpret LATEX-ENVIRONMENT element as Org syntax.
  981. CONTENTS is nil."
  982. (org-element-get-property :value latex-environment))
  983. ;;;; Paragraph
  984. (defun org-element-paragraph-parser ()
  985. "Parse a paragraph.
  986. Return a list whose car is `paragraph' and cdr is a plist
  987. containing `:begin', `:end', `:contents-begin' and
  988. `:contents-end' and `:post-blank' keywords.
  989. Assume point is at the beginning of the paragraph."
  990. (save-excursion
  991. (let* ((contents-begin (point))
  992. (keywords (org-element-collect-affiliated-keywords))
  993. (begin (car keywords))
  994. (contents-end (progn
  995. (end-of-line)
  996. (if (re-search-forward
  997. org-element-paragraph-separate nil 'm)
  998. (progn (forward-line -1) (end-of-line) (point))
  999. (point))))
  1000. (pos-before-blank (progn (forward-line) (point)))
  1001. (end (progn (org-skip-whitespace)
  1002. (if (eobp) (point) (point-at-bol)))))
  1003. (list 'paragraph
  1004. `(:begin ,begin
  1005. :end ,end
  1006. :contents-begin ,contents-begin
  1007. :contents-end ,contents-end
  1008. :post-blank ,(count-lines pos-before-blank end)
  1009. ,@(cadr keywords))))))
  1010. (defun org-element-paragraph-interpreter (paragraph contents)
  1011. "Interpret PARAGRAPH element as Org syntax.
  1012. CONTENTS is the contents of the element."
  1013. contents)
  1014. ;;;; Property Drawer
  1015. (defun org-element-property-drawer-parser ()
  1016. "Parse a property drawer.
  1017. Return a list whose car is `property-drawer' and cdr is a plist
  1018. containing `:begin', `:end', `:hiddenp', `:contents-begin',
  1019. `:contents-end', `:properties' and `:post-blank' keywords."
  1020. (save-excursion
  1021. (let ((case-fold-search t)
  1022. (begin (progn (end-of-line)
  1023. (re-search-backward org-property-start-re)
  1024. (match-beginning 0)))
  1025. (contents-begin (progn (forward-line) (point)))
  1026. (hidden (org-truely-invisible-p))
  1027. (properties (let (val)
  1028. (while (not (looking-at "^[ \t]*:END:"))
  1029. (when (looking-at
  1030. (org-re
  1031. "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
  1032. (push (cons (match-string 1)
  1033. (org-trim
  1034. (buffer-substring
  1035. (match-end 0) (point-at-eol))))
  1036. val))
  1037. (forward-line))
  1038. val))
  1039. (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t)
  1040. (point-at-bol)))
  1041. (pos-before-blank (progn (forward-line) (point)))
  1042. (end (progn (org-skip-whitespace)
  1043. (if (eobp) (point) (point-at-bol)))))
  1044. (list 'property-drawer
  1045. `(:begin ,begin
  1046. :end ,end
  1047. :hiddenp ,hidden
  1048. :properties ,properties
  1049. :post-blank ,(count-lines pos-before-blank end))))))
  1050. (defun org-element-property-drawer-interpreter (property-drawer contents)
  1051. "Interpret PROPERTY-DRAWER element as Org syntax.
  1052. CONTENTS is nil."
  1053. (let ((props (org-element-get-property :properties property-drawer)))
  1054. (concat
  1055. ":PROPERTIES:\n"
  1056. (mapconcat (lambda (p)
  1057. (format org-property-format (format ":%s:" (car p)) (cdr p)))
  1058. (nreverse props) "\n")
  1059. "\n:END:")))
  1060. ;;;; Quote Section
  1061. (defun org-element-quote-section-parser ()
  1062. "Parse a quote section.
  1063. Return a list whose car is `quote-section' and cdr is a plist
  1064. containing `:begin', `:end', `:value' and `:post-blank'
  1065. keywords."
  1066. (save-excursion
  1067. (let* ((begin (progn (org-back-to-heading t)
  1068. (forward-line)
  1069. (org-skip-whitespace)
  1070. (point-at-bol)))
  1071. (end (progn (org-with-limited-levels (outline-next-heading))
  1072. (point)))
  1073. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1074. (forward-line)
  1075. (point)))
  1076. (value (unless (= begin end)
  1077. (buffer-substring-no-properties begin pos-before-blank))))
  1078. (list 'quote-section
  1079. `(:begin ,begin
  1080. :end ,end
  1081. :value ,value
  1082. :post-blank ,(if value
  1083. (count-lines pos-before-blank end)
  1084. 0))))))
  1085. (defun org-element-quote-section-interpreter (quote-section contents)
  1086. "Interpret QUOTE-SECTION element as Org syntax.
  1087. CONTENTS is nil."
  1088. (org-element-get-property :value quote-section))
  1089. ;;;; Src Block
  1090. (defun org-element-src-block-parser ()
  1091. "Parse a src block.
  1092. Return a list whose car is `src-block' and cdr is a plist
  1093. containing `:language', `:switches', `:parameters', `:begin',
  1094. `:end', `:hiddenp', `:contents-begin', `:contents-end', `:value'
  1095. and `:post-blank' keywords."
  1096. (save-excursion
  1097. (end-of-line)
  1098. (let* ((case-fold-search t)
  1099. ;; Get position at beginning of block.
  1100. (contents-begin
  1101. (re-search-backward
  1102. (concat "^[ \t]*#\\+begin_src"
  1103. "\\(?: +\\(\\S-+\\)\\)?" ; language
  1104. "\\(\\(?: +[-+][A-Za-z]\\)*\\)" ; switches
  1105. "\\(.*\\)[ \t]*$") ; arguments
  1106. nil t))
  1107. ;; Get language as a string.
  1108. (language (org-match-string-no-properties 1))
  1109. ;; Get switches.
  1110. (switches (org-match-string-no-properties 2))
  1111. ;; Get parameters.
  1112. (parameters (org-trim (org-match-string-no-properties 3)))
  1113. ;; Get affiliated keywords.
  1114. (keywords (org-element-collect-affiliated-keywords))
  1115. ;; Get beginning position.
  1116. (begin (car keywords))
  1117. ;; Get position at end of block.
  1118. (contents-end (progn (re-search-forward "^[ \t]*#\\+end_src" nil t)
  1119. (forward-line)
  1120. (point)))
  1121. ;; Retrieve code.
  1122. (value (buffer-substring-no-properties
  1123. (save-excursion (goto-char contents-begin)
  1124. (forward-line)
  1125. (point))
  1126. (match-beginning 0)))
  1127. ;; Get position after ending blank lines.
  1128. (end (progn (org-skip-whitespace)
  1129. (if (eobp) (point) (point-at-bol))))
  1130. ;; Get visibility status.
  1131. (hidden (progn (goto-char contents-begin)
  1132. (forward-line)
  1133. (org-truely-invisible-p))))
  1134. (list 'src-block
  1135. `(:language ,language
  1136. :switches ,switches
  1137. :parameters ,parameters
  1138. :begin ,begin
  1139. :end ,end
  1140. :hiddenp ,hidden
  1141. :value ,value
  1142. :post-blank ,(count-lines contents-end end)
  1143. ,@(cadr keywords))))))
  1144. (defun org-element-src-block-interpreter (src-block contents)
  1145. "Interpret SRC-BLOCK element as Org syntax.
  1146. CONTENTS is nil."
  1147. (let ((lang (org-element-get-property :language src-block))
  1148. (switches (org-element-get-property :switches src-block))
  1149. (params (org-element-get-property :parameters src-block))
  1150. (value (let ((val (org-element-get-property :value src-block)))
  1151. (cond
  1152. (org-src-preserve-indentation val)
  1153. ((zerop org-edit-src-content-indentation)
  1154. (org-remove-indentation val))
  1155. (t
  1156. (let ((ind (make-string
  1157. org-edit-src-content-indentation 32)))
  1158. (replace-regexp-in-string
  1159. "\\(^\\)[ \t]*\\S-" ind
  1160. (org-remove-indentation val) nil nil 1)))))))
  1161. (concat (format "#+begin_src%s\n"
  1162. (concat (and lang (concat " " lang))
  1163. (and switches (concat " " switches))
  1164. (and params (concat " " params))))
  1165. value
  1166. "#+end_src")))
  1167. ;;;; Table
  1168. (defun org-element-table-parser ()
  1169. "Parse a table at point.
  1170. Return a list whose car is `table' and cdr is a plist containing
  1171. `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
  1172. `:type', `:raw-table' and `:post-blank' keywords."
  1173. (save-excursion
  1174. (let* ((table-begin (goto-char (org-table-begin t)))
  1175. (type (if (org-at-table.el-p) 'table.el 'org))
  1176. (keywords (org-element-collect-affiliated-keywords))
  1177. (begin (car keywords))
  1178. (table-end (goto-char (marker-position (org-table-end t))))
  1179. (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
  1180. (prog1 (org-match-string-no-properties 1)
  1181. (forward-line))))
  1182. (pos-before-blank (point))
  1183. (end (progn (org-skip-whitespace)
  1184. (if (eobp) (point) (point-at-bol))))
  1185. (raw-table (org-remove-indentation
  1186. (buffer-substring-no-properties table-begin table-end))))
  1187. (list 'table
  1188. `(:begin ,begin
  1189. :end ,end
  1190. :type ,type
  1191. :raw-table ,raw-table
  1192. :tblfm ,tblfm
  1193. :post-blank ,(count-lines pos-before-blank end)
  1194. ,@(cadr keywords))))))
  1195. (defun org-element-table-interpreter (table contents)
  1196. "Interpret TABLE element as Org syntax.
  1197. CONTENTS is nil."
  1198. (org-element-get-property :raw-table table))
  1199. ;;;; Verse Block
  1200. (defun org-element-verse-block-parser ()
  1201. "Parse a verse block.
  1202. Return a list whose car is `verse-block' and cdr is a plist
  1203. containing `:begin', `:end', `:hiddenp', `:raw-value', `:value'
  1204. and `:post-blank' keywords.
  1205. Assume point is at beginning or end of the block."
  1206. (save-excursion
  1207. (let* ((case-fold-search t)
  1208. (keywords (progn
  1209. (end-of-line)
  1210. (re-search-backward
  1211. (concat "^[ \t]*#\\+begin_verse") nil t)
  1212. (org-element-collect-affiliated-keywords)))
  1213. (begin (car keywords))
  1214. (hidden (progn (forward-line) (org-truely-invisible-p)))
  1215. (raw-val (buffer-substring-no-properties
  1216. (point)
  1217. (progn
  1218. (re-search-forward (concat "^[ \t]*#\\+end_verse") nil t)
  1219. (point-at-bol))))
  1220. (pos-before-blank (progn (forward-line) (point)))
  1221. (end (progn (org-skip-whitespace)
  1222. (if (eobp) (point) (point-at-bol))))
  1223. (value (org-element-parse-secondary-string
  1224. (org-remove-indentation raw-val)
  1225. (cdr (assq 'verse org-element-string-restrictions)))))
  1226. (list 'verse-block
  1227. `(:begin ,begin
  1228. :end ,end
  1229. :hiddenp ,hidden
  1230. :raw-value ,raw-val
  1231. :value ,value
  1232. :post-blank ,(count-lines pos-before-blank end)
  1233. ,@(cadr keywords))))))
  1234. (defun org-element-verse-block-interpreter (verse-block contents)
  1235. "Interpret VERSE-BLOCK element as Org syntax.
  1236. CONTENTS is nil."
  1237. (format "#+begin_verse\n%s#+end_verse"
  1238. (org-remove-indentation
  1239. (org-element-get-property :raw-value verse-block))))
  1240. ;;; Objects
  1241. ;; Unlike to elements, interstices can be found between objects.
  1242. ;; That's why, along with the parser, successor functions are provided
  1243. ;; for each object. Some objects share the same successor
  1244. ;; (i.e. `emphasis' and `verbatim' objects).
  1245. ;; A successor must accept a single argument bounding the search. It
  1246. ;; will return either a cons cell whose car is the object's type, as
  1247. ;; a symbol, and cdr the position of its next occurrence, or nil.
  1248. ;; Successors follow the naming convention:
  1249. ;; org-element-NAME-successor, where NAME is the name of the
  1250. ;; successor, as defined in `org-element-all-successors'.
  1251. ;; Some object types (i.e `emphasis') are recursive. Restrictions on
  1252. ;; object types they can contain will be specified in
  1253. ;; `org-element-object-restrictions'.
  1254. ;; Adding a new type of object is simple. Implement a successor,
  1255. ;; a parser, and an interpreter for it, all following the naming
  1256. ;; convention. Register successor in `org-element-all-successors',
  1257. ;; maybe tweak restrictions about it, and that's it.
  1258. ;;;; Emphasis
  1259. (defun org-element-emphasis-parser ()
  1260. "Parse text markup object at point.
  1261. Return a list whose car is `emphasis' and cdr is a plist with
  1262. `:marker', `:begin', `:end', `:contents-begin' and
  1263. `:contents-end' and `:post-blank' keywords.
  1264. Assume point is at the first emphasis marker."
  1265. (save-excursion
  1266. (unless (bolp) (backward-char 1))
  1267. (looking-at org-emph-re)
  1268. (let ((begin (match-beginning 2))
  1269. (marker (org-match-string-no-properties 3))
  1270. (contents-begin (match-beginning 4))
  1271. (contents-end (match-end 4))
  1272. (post-blank (progn (goto-char (match-end 2))
  1273. (skip-chars-forward " \t")))
  1274. (end (point)))
  1275. (list 'emphasis
  1276. `(:marker ,marker
  1277. :begin ,begin
  1278. :end ,end
  1279. :contents-begin ,contents-begin
  1280. :contents-end ,contents-end
  1281. :post-blank ,post-blank)))))
  1282. (defun org-element-emphasis-interpreter (emphasis contents)
  1283. "Interpret EMPHASIS object as Org syntax.
  1284. CONTENTS is the contents of the object."
  1285. (let ((marker (org-element-get-property :marker emphasis)))
  1286. (concat marker contents marker)))
  1287. (defun org-element-text-markup-successor (limit)
  1288. "Search for the next emphasis or verbatim and return position.
  1289. LIMIT bounds the search.
  1290. Return value is a cons cell whose car is `emphasis' or
  1291. `verbatim' and cdr is beginning position."
  1292. (save-excursion
  1293. (unless (bolp) (backward-char))
  1294. (when (re-search-forward org-emph-re limit t)
  1295. (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist))
  1296. 'verbatim
  1297. 'emphasis)
  1298. (match-beginning 2)))))
  1299. ;;;; Entity
  1300. (defun org-element-entity-parser ()
  1301. "Parse entity at point.
  1302. Return a list whose car is `entity' and cdr a plist with
  1303. `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
  1304. `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
  1305. keywords.
  1306. Assume point is at the beginning of the entity."
  1307. (save-excursion
  1308. (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
  1309. (let* ((value (org-entity-get (match-string 1)))
  1310. (begin (match-beginning 0))
  1311. (bracketsp (string= (match-string 2) "{}"))
  1312. (post-blank (progn (goto-char (match-end 1))
  1313. (when bracketsp (forward-char 2))
  1314. (skip-chars-forward " \t")))
  1315. (end (point)))
  1316. (list 'entity
  1317. `(:name ,(car value)
  1318. :latex ,(nth 1 value)
  1319. :latex-math-p ,(nth 2 value)
  1320. :html ,(nth 3 value)
  1321. :ascii ,(nth 4 value)
  1322. :latin1 ,(nth 5 value)
  1323. :utf-8 ,(nth 6 value)
  1324. :begin ,begin
  1325. :end ,end
  1326. :use-brackets-p ,bracketsp
  1327. :post-blank ,post-blank)))))
  1328. (defun org-element-entity-interpreter (entity contents)
  1329. "Interpret ENTITY object as Org syntax.
  1330. CONTENTS is nil."
  1331. (concat "\\"
  1332. (org-element-get-property :name entity)
  1333. (when (org-element-get-property :use-brackets-p entity) "{}")))
  1334. (defun org-element-latex-or-entity-successor (limit)
  1335. "Search for the next latex-fragment or entity object.
  1336. LIMIT bounds the search.
  1337. Return value is a cons cell whose car is `entity' or
  1338. `latex-fragment' and cdr is beginning position."
  1339. (save-excursion
  1340. (let ((matchers (plist-get org-format-latex-options :matchers))
  1341. ;; ENTITY-RE matches both LaTeX commands and Org entities.
  1342. (entity-re
  1343. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
  1344. (when (re-search-forward
  1345. (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
  1346. matchers "\\|")
  1347. "\\|" entity-re)
  1348. limit t)
  1349. (goto-char (match-beginning 0))
  1350. (if (looking-at entity-re)
  1351. ;; Determine if it's a real entity or a LaTeX command.
  1352. (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
  1353. (match-beginning 0))
  1354. ;; No entity nor command: point is at a LaTeX fragment.
  1355. ;; Determine its type to get the correct beginning position.
  1356. (cons 'latex-fragment
  1357. (catch 'return
  1358. (mapc (lambda (e)
  1359. (when (looking-at (nth 1 (assoc e org-latex-regexps)))
  1360. (throw 'return
  1361. (match-beginning
  1362. (nth 2 (assoc e org-latex-regexps))))))
  1363. matchers)
  1364. (point))))))))
  1365. ;;;; Export Snippet
  1366. (defun org-element-export-snippet-parser ()
  1367. "Parse export snippet at point.
  1368. Return a list whose car is `export-snippet' and cdr a plist with
  1369. `:begin', `:end', `:back-end', `:value' and `:post-blank' as
  1370. keywords.
  1371. Assume point is at the beginning of the snippet."
  1372. (save-excursion
  1373. (looking-at "@\\([-A-Za-z0-9]+\\){")
  1374. (let* ((begin (point))
  1375. (back-end (org-match-string-no-properties 1))
  1376. (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
  1377. (value (buffer-substring-no-properties
  1378. (match-end 0) (1- before-blank)))
  1379. (post-blank (skip-chars-forward " \t"))
  1380. (end (point)))
  1381. (list 'export-snippet
  1382. `(:back-end ,back-end
  1383. :value ,value
  1384. :begin ,begin
  1385. :end ,end
  1386. :post-blank ,post-blank)))))
  1387. (defun org-element-export-snippet-interpreter (export-snippet contents)
  1388. "Interpret EXPORT-SNIPPET object as Org syntax.
  1389. CONTENTS is nil."
  1390. (format "@%s{%s}"
  1391. (org-element-get-property :back-end export-snippet)
  1392. (org-element-get-property :value export-snippet)))
  1393. (defun org-element-export-snippet-successor (limit)
  1394. "Search for the next export-snippet object.
  1395. LIMIT bounds the search.
  1396. Return value is a cons cell whose car is `export-snippet' cdr is
  1397. its beginning position."
  1398. (save-excursion
  1399. (catch 'exit
  1400. (while (re-search-forward "@[-A-Za-z0-9]+{" limit t)
  1401. (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
  1402. (and end (eq (char-before end) ?})))
  1403. (throw 'exit (cons 'export-snippet (match-beginning 0))))))))
  1404. ;;;; Footnote Reference
  1405. (defun org-element-footnote-reference-parser ()
  1406. "Parse footnote reference at point.
  1407. Return a list whose car is `footnote-reference' and cdr a plist
  1408. with `:label', `:type', `:definition', `:begin', `:end' and
  1409. `:post-blank' as keywords."
  1410. (save-excursion
  1411. (let* ((ref (org-footnote-at-reference-p))
  1412. (label (car ref))
  1413. (raw-def (nth 3 ref))
  1414. (inline-def (and raw-def
  1415. (org-element-parse-secondary-string raw-def nil)))
  1416. (type (if (nth 3 ref) 'inline 'standard))
  1417. (begin (nth 1 ref))
  1418. (post-blank (progn (goto-char (nth 2 ref))
  1419. (skip-chars-forward " \t")))
  1420. (end (point)))
  1421. (list 'footnote-reference
  1422. `(:label ,label
  1423. :type ,type
  1424. :inline-definition ,inline-def
  1425. :begin ,begin
  1426. :end ,end
  1427. :post-blank ,post-blank
  1428. :raw-definition ,raw-def)))))
  1429. (defun org-element-footnote-reference-interpreter (footnote-reference contents)
  1430. "Interpret FOOTNOTE-REFERENCE object as Org syntax.
  1431. CONTENTS is nil."
  1432. (let ((label (or (org-element-get-property :label footnote-reference)
  1433. "fn:"))
  1434. (def (let ((raw (org-element-get-property
  1435. :raw-definition footnote-reference)))
  1436. (if raw (concat ":" raw) ""))))
  1437. (format "[%s]" (concat label def))))
  1438. (defun org-element-footnote-reference-successor (limit)
  1439. "Search for the next footnote-reference and return beginning
  1440. position.
  1441. LIMIT bounds the search.
  1442. Return value is a cons cell whose car is `footnote-reference' and
  1443. cdr is beginning position."
  1444. (let (fn-ref)
  1445. (when (setq fn-ref (org-footnote-get-next-reference nil nil limit))
  1446. (cons 'footnote-reference (nth 1 fn-ref)))))
  1447. ;;;; Inline Babel Call
  1448. (defun org-element-inline-babel-call-parser ()
  1449. "Parse inline babel call at point.
  1450. Return a list whose car is `inline-babel-call' and cdr a plist with
  1451. `:begin', `:end', `:info' and `:post-blank' as keywords.
  1452. Assume point is at the beginning of the babel call."
  1453. (save-excursion
  1454. (unless (bolp) (backward-char))
  1455. (looking-at org-babel-inline-lob-one-liner-regexp)
  1456. (let ((info (save-match-data (org-babel-lob-get-info)))
  1457. (begin (match-end 1))
  1458. (post-blank (progn (goto-char (match-end 0))
  1459. (skip-chars-forward " \t")))
  1460. (end (point)))
  1461. (list 'inline-babel-call
  1462. `(:begin ,begin
  1463. :end ,end
  1464. :info ,info
  1465. :post-blank ,post-blank)))))
  1466. (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
  1467. "Interpret INLINE-BABEL-CALL object as Org syntax.
  1468. CONTENTS is nil."
  1469. (let* ((babel-info (org-element-get-property :info inline-babel-call))
  1470. (main-source (car babel-info))
  1471. (post-options (nth 1 babel-info)))
  1472. (concat "call_"
  1473. (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
  1474. ;; Remove redundant square brackets.
  1475. (replace-match
  1476. (match-string 1 main-source) nil nil main-source)
  1477. main-source)
  1478. (and post-options (format "[%s]" post-options)))))
  1479. (defun org-element-inline-babel-call-successor (limit)
  1480. "Search for the next inline-babel-call and return beginning
  1481. position.
  1482. LIMIT bounds the search.
  1483. Return value is a cons cell whose car is `inline-babel-call' and
  1484. cdr is beginning position."
  1485. (save-excursion
  1486. ;; Use a simplified version of
  1487. ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
  1488. (when (re-search-forward
  1489. "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
  1490. limit t)
  1491. (cons 'inline-babel-call (match-beginning 0)))))
  1492. ;;;; Inline Src Block
  1493. (defun org-element-inline-src-block-parser ()
  1494. "Parse inline source block at point.
  1495. Return a list whose car is `inline-src-block' and cdr a plist
  1496. with `:begin', `:end', `:language', `:value', `:parameters' and
  1497. `:post-blank' as keywords.
  1498. Assume point is at the beginning of the inline src block."
  1499. (save-excursion
  1500. (unless (bolp) (backward-char))
  1501. (looking-at org-babel-inline-src-block-regexp)
  1502. (let ((begin (match-beginning 1))
  1503. (language (org-match-string-no-properties 2))
  1504. (parameters (org-match-string-no-properties 4))
  1505. (value (org-match-string-no-properties 5))
  1506. (post-blank (progn (goto-char (match-end 0))
  1507. (skip-chars-forward " \t")))
  1508. (end (point)))
  1509. (list 'inline-src-block
  1510. `(:language ,language
  1511. :value ,value
  1512. :parameters ,parameters
  1513. :begin ,begin
  1514. :end ,end
  1515. :post-blank ,post-blank)))))
  1516. (defun org-element-inline-src-block-successor (limit)
  1517. "Search for the next inline-babel-call and return beginning position.
  1518. LIMIT bounds the search.
  1519. Return value is a cons cell whose car is `inline-babel-call' and
  1520. cdr is beginning position."
  1521. (save-excursion
  1522. (when (re-search-forward org-babel-inline-src-block-regexp limit t)
  1523. (cons 'inline-src-block (match-beginning 1)))))
  1524. ;;;; Latex Fragment
  1525. (defun org-element-latex-fragment-parser ()
  1526. "Parse latex fragment at point.
  1527. Return a list whose car is `latex-fragment' and cdr a plist with
  1528. `:value', `:begin', `:end', and `:post-blank' as keywords.
  1529. Assume point is at the beginning of the latex fragment."
  1530. (save-excursion
  1531. (let* ((begin (point))
  1532. (substring-match
  1533. (catch 'exit
  1534. (mapc (lambda (e)
  1535. (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
  1536. (when (or (looking-at latex-regexp)
  1537. (and (not (bobp))
  1538. (save-excursion
  1539. (backward-char)
  1540. (looking-at latex-regexp))))
  1541. (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
  1542. (plist-get org-format-latex-options :matchers))
  1543. ;; None found: it's a macro.
  1544. (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
  1545. 0))
  1546. (value (match-string-no-properties substring-match))
  1547. (post-blank (progn (goto-char (match-end substring-match))
  1548. (skip-chars-forward " \t")))
  1549. (end (point)))
  1550. (list 'latex-fragment
  1551. `(:value ,value
  1552. :begin ,begin
  1553. :end ,end
  1554. :post-blank ,post-blank)))))
  1555. (defun org-element-latex-fragment-interpreter (latex-fragment contents)
  1556. "Interpret LATEX-FRAGMENT object as Org syntax.
  1557. CONTENTS is nil."
  1558. (org-element-get-property :value latex-fragment))
  1559. ;;;; Line Break
  1560. (defun org-element-line-break-parser ()
  1561. "Parse line break at point.
  1562. Return a list whose car is `line-break', and cdr a plist with
  1563. `:begin', `:end' and `:post-blank' keywords.
  1564. Assume point is at the beginning of the line break."
  1565. (save-excursion
  1566. (let* ((begin (point))
  1567. (end (progn (end-of-line) (point)))
  1568. (post-blank (- (skip-chars-backward " \t")))
  1569. (end (point)))
  1570. (list 'line-break
  1571. `(:begin ,begin
  1572. :end ,end
  1573. :post-blank ,post-blank)))))
  1574. (defun org-element-line-break-interpreter (line-break contents)
  1575. "Interpret LINE-BREAK object as Org syntax.
  1576. CONTENTS is nil."
  1577. (org-element-get-property :value line-break))
  1578. (defun org-element-line-break-successor (limit)
  1579. "Search for the next statistics cookie and return position.
  1580. LIMIT bounds the search.
  1581. Return value is a cons cell whose car is `line-break' and cdr is
  1582. beginning position."
  1583. (save-excursion
  1584. (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
  1585. (goto-char (match-beginning 1)))))
  1586. ;; A line break can only happen on a non-empty line.
  1587. (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
  1588. (cons 'line-break beg)))))
  1589. ;;;; Link
  1590. (defun org-element-link-parser ()
  1591. "Parse link at point.
  1592. Return a list whose car is `link' and cdr a plist with `:type',
  1593. `:path', `:raw-link', `:begin', `:end', `:contents-begin',
  1594. `:contents-end' and `:post-blank' as keywords.
  1595. Assume point is at the beginning of the link."
  1596. (save-excursion
  1597. (let ((begin (point))
  1598. end contents-begin contents-end link-end post-blank path type
  1599. raw-link link)
  1600. (cond
  1601. ;; Type 1: text targeted from a radio target.
  1602. ((and org-target-link-regexp (looking-at org-target-link-regexp))
  1603. (setq type "radio"
  1604. path (org-match-string-no-properties 0)
  1605. contents-begin (match-beginning 0)
  1606. contents-end (match-end 0)
  1607. link-end (match-end 0)))
  1608. ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
  1609. ((looking-at org-bracket-link-regexp)
  1610. (setq contents-begin (match-beginning 3)
  1611. contents-end (match-end 3)
  1612. link-end (match-end 0)
  1613. ;; RAW-LINK is the original link.
  1614. raw-link (org-match-string-no-properties 1)
  1615. link (org-link-expand-abbrev
  1616. (replace-regexp-in-string
  1617. " *\n *" " " (org-link-unescape raw-link) t t)))
  1618. ;; Determine TYPE of link and set PATH accordingly.
  1619. (cond
  1620. ;; File type.
  1621. ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
  1622. (setq type "file" path link))
  1623. ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
  1624. ((string-match org-link-re-with-space3 link)
  1625. (setq type (match-string 1 link) path (match-string 2 link)))
  1626. ;; Id type: PATH is the id.
  1627. ((string-match "^id:\\([-a-f0-9]+\\)" link)
  1628. (setq type "id" path (match-string 1 link)))
  1629. ;; Code-ref type: PATH is the name of the reference.
  1630. ((string-match "^(\\(.*\\))$" link)
  1631. (setq type "coderef" path (match-string 1 link)))
  1632. ;; Custom-id type: PATH is the name of the custom id.
  1633. ((= (aref link 0) ?#)
  1634. (setq type "custom-id" path (substring link 1)))
  1635. ;; Fuzzy type: Internal link either matches a target, an
  1636. ;; headline name or nothing. PATH is the target or headline's
  1637. ;; name.
  1638. (t (setq type "fuzzy" path link))))
  1639. ;; Type 3: Plain link, i.e. http://orgmode.org
  1640. ((looking-at org-plain-link-re)
  1641. (setq raw-link (org-match-string-no-properties 0)
  1642. type (org-match-string-no-properties 1)
  1643. path (org-match-string-no-properties 2)
  1644. link-end (match-end 0)))
  1645. ;; Type 4: Angular link, i.e. <http://orgmode.org>
  1646. ((looking-at org-angle-link-re)
  1647. (setq raw-link (buffer-substring-no-properties
  1648. (match-beginning 1) (match-end 2))
  1649. type (org-match-string-no-properties 1)
  1650. path (org-match-string-no-properties 2)
  1651. link-end (match-end 0))))
  1652. ;; In any case, deduce end point after trailing white space from
  1653. ;; LINK-END variable.
  1654. (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
  1655. end (point))
  1656. (list 'link
  1657. `(:type ,type
  1658. :path ,path
  1659. :raw-link ,(or raw-link path)
  1660. :begin ,begin
  1661. :end ,end
  1662. :contents-begin ,contents-begin
  1663. :contents-end ,contents-end
  1664. :post-blank ,post-blank)))))
  1665. (defun org-element-link-interpreter (link contents)
  1666. "Interpret LINK object as Org syntax.
  1667. CONTENTS is the contents of the object."
  1668. (let ((type (org-element-get-property :type link))
  1669. (raw-link (org-element-get-property :raw-link link)))
  1670. (cond
  1671. ((string= type "radio") raw-link)
  1672. (t (format "[[%s]%s]"
  1673. raw-link
  1674. (if (string= contents "") "" (format "[%s]" contents)))))))
  1675. (defun org-element-link-successor (limit)
  1676. "Search for the next link and return position.
  1677. LIMIT bounds the search.
  1678. Return value is a cons cell whose car is `link' and cdr is
  1679. beginning position."
  1680. (save-excursion
  1681. (let ((link-regexp
  1682. (if org-target-link-regexp
  1683. (concat org-any-link-re "\\|" org-target-link-regexp)
  1684. org-any-link-re)))
  1685. (when (re-search-forward link-regexp limit t)
  1686. (cons 'link (match-beginning 0))))))
  1687. ;;;; Macro
  1688. (defun org-element-macro-parser ()
  1689. "Parse macro at point.
  1690. Return a list whose car is `macro' and cdr a plist with `:key',
  1691. `:args', `:begin', `:end', `:value' and `:post-blank' as
  1692. keywords.
  1693. Assume point is at the macro."
  1694. (save-excursion
  1695. (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
  1696. (let ((begin (point))
  1697. (key (downcase (org-match-string-no-properties 1)))
  1698. (value (org-match-string-no-properties 0))
  1699. (post-blank (progn (goto-char (match-end 0))
  1700. (skip-chars-forward " \t")))
  1701. (end (point))
  1702. (args (let ((args (org-match-string-no-properties 3)) args2)
  1703. (when args
  1704. (setq args (org-split-string args ","))
  1705. (while args
  1706. (while (string-match "\\\\\\'" (car args))
  1707. ;; Repair bad splits.
  1708. (setcar (cdr args) (concat (substring (car args) 0 -1)
  1709. "," (nth 1 args)))
  1710. (pop args))
  1711. (push (pop args) args2))
  1712. (mapcar 'org-trim (nreverse args2))))))
  1713. (list 'macro
  1714. `(:key ,key
  1715. :value ,value
  1716. :args ,args
  1717. :begin ,begin
  1718. :end ,end
  1719. :post-blank ,post-blank)))))
  1720. (defun org-element-macro-interpreter (macro contents)
  1721. "Interpret MACRO object as Org syntax.
  1722. CONTENTS is nil."
  1723. (org-element-get-property :value macro))
  1724. (defun org-element-macro-successor (limit)
  1725. "Search for the next macro and return position.
  1726. LIMIT bounds the search.
  1727. Return value is cons cell whose car is `macro' and cdr is
  1728. beginning position."
  1729. (save-excursion
  1730. (when (re-search-forward
  1731. "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
  1732. limit t)
  1733. (cons 'macro (match-beginning 0)))))
  1734. ;;;; Radio-target
  1735. (defun org-element-radio-target-parser ()
  1736. "Parse radio target at point.
  1737. Return a list whose car is `radio-target' and cdr a plist with
  1738. `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
  1739. and `:post-blank' as keywords.
  1740. Assume point is at the radio target."
  1741. (save-excursion
  1742. (looking-at org-radio-target-regexp)
  1743. (let ((begin (point))
  1744. (contents-begin (match-beginning 1))
  1745. (contents-end (match-end 1))
  1746. (raw-value (org-match-string-no-properties 1))
  1747. (post-blank (progn (goto-char (match-end 0))
  1748. (skip-chars-forward " \t")))
  1749. (end (point)))
  1750. (list 'radio-target
  1751. `(:begin ,begin
  1752. :end ,end
  1753. :contents-begin ,contents-begin
  1754. :contents-end ,contents-end
  1755. :raw-value ,raw-value
  1756. :post-blank ,post-blank)))))
  1757. (defun org-element-radio-target-interpreter (target contents)
  1758. "Interpret TARGET object as Org syntax.
  1759. CONTENTS is the contents of the object."
  1760. (concat ">"))
  1761. (defun org-element-radio-target-successor (limit)
  1762. "Search for the next radio-target and return position.
  1763. LIMIT bounds the search.
  1764. Return value is a cons cell whose car is `radio-target' and cdr
  1765. is beginning position."
  1766. (save-excursion
  1767. (when (re-search-forward org-radio-target-regexp limit t)
  1768. (cons 'radio-target (match-beginning 0)))))
  1769. ;;;; Statistics Cookie
  1770. (defun org-element-statistics-cookie-parser ()
  1771. "Parse statistics cookie at point.
  1772. Return a list whose car is `statistics-cookie', and cdr a plist
  1773. with `:begin', `:end', `:value' and `:post-blank' keywords.
  1774. Assume point is at the beginning of the statistics-cookie."
  1775. (save-excursion
  1776. (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
  1777. (let* ((begin (point))
  1778. (value (buffer-substring-no-properties
  1779. (match-beginning 0) (match-end 0)))
  1780. (post-blank (progn (goto-char (match-end 0))
  1781. (skip-chars-forward " \t")))
  1782. (end (point)))
  1783. (list 'statistics-cookie
  1784. `(:begin ,begin
  1785. :end ,end
  1786. :value ,value
  1787. :post-blank ,post-blank)))))
  1788. (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
  1789. "Interpret STATISTICS-COOKIE object as Org syntax.
  1790. CONTENTS is nil."
  1791. (org-element-get-property :value statistics-cookie))
  1792. (defun org-element-statistics-cookie-successor (limit)
  1793. "Search for the next statistics cookie and return position.
  1794. LIMIT bounds the search.
  1795. Return value is a cons cell whose car is `statistics-cookie' and
  1796. cdr is beginning position."
  1797. (save-excursion
  1798. (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
  1799. (cons 'statistics-cookie (match-beginning 0)))))
  1800. ;;;; Subscript
  1801. (defun org-element-subscript-parser ()
  1802. "Parse subscript at point.
  1803. Return a list whose car is `subscript' and cdr a plist with
  1804. `:begin', `:end', `:contents-begin', `:contents-end',
  1805. `:use-brackets-p' and `:post-blank' as keywords.
  1806. Assume point is at the underscore."
  1807. (save-excursion
  1808. (unless (bolp) (backward-char))
  1809. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
  1810. t
  1811. (not (looking-at org-match-substring-regexp))))
  1812. (begin (match-beginning 2))
  1813. (contents-begin (or (match-beginning 5)
  1814. (match-beginning 3)))
  1815. (contents-end (or (match-end 5) (match-end 3)))
  1816. (post-blank (progn (goto-char (match-end 0))
  1817. (skip-chars-forward " \t")))
  1818. (end (point)))
  1819. (list 'subscript
  1820. `(:begin ,begin
  1821. :end ,end
  1822. :use-brackets-p ,bracketsp
  1823. :contents-begin ,contents-begin
  1824. :contents-end ,contents-end
  1825. :post-blank ,post-blank)))))
  1826. (defun org-element-subscript-interpreter (subscript contents)
  1827. "Interpret SUBSCRIPT object as Org syntax.
  1828. CONTENTS is the contents of the object."
  1829. (format
  1830. (if (org-element-get-property :use-brackets-p subscript) "_{%s}" "_%s")
  1831. contents))
  1832. (defun org-element-sub/superscript-successor (limit)
  1833. "Search for the next sub/superscript and return beginning
  1834. position.
  1835. LIMIT bounds the search.
  1836. Return value is a cons cell whose car is either `subscript' or
  1837. `superscript' and cdr is beginning position."
  1838. (save-excursion
  1839. (when (re-search-forward org-match-substring-regexp limit t)
  1840. (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
  1841. (match-beginning 2)))))
  1842. ;;;; Superscript
  1843. (defun org-element-superscript-parser ()
  1844. "Parse superscript at point.
  1845. Return a list whose car is `superscript' and cdr a plist with
  1846. `:begin', `:end', `:contents-begin', `:contents-end',
  1847. `:use-brackets-p' and `:post-blank' as keywords.
  1848. Assume point is at the caret."
  1849. (save-excursion
  1850. (unless (bolp) (backward-char))
  1851. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
  1852. t
  1853. (not (looking-at org-match-substring-regexp))))
  1854. (begin (match-beginning 2))
  1855. (contents-begin (or (match-beginning 5)
  1856. (match-beginning 3)))
  1857. (contents-end (or (match-end 5) (match-end 3)))
  1858. (post-blank (progn (goto-char (match-end 0))
  1859. (skip-chars-forward " \t")))
  1860. (end (point)))
  1861. (list 'superscript
  1862. `(:begin ,begin
  1863. :end ,end
  1864. :use-brackets-p ,bracketsp
  1865. :contents-begin ,contents-begin
  1866. :contents-end ,contents-end
  1867. :post-blank ,post-blank)))))
  1868. (defun org-element-superscript-interpreter (superscript contents)
  1869. "Interpret SUPERSCRIPT object as Org syntax.
  1870. CONTENTS is the contents of the object."
  1871. (format
  1872. (if (org-element-get-property :use-brackets-p superscript) "^{%s}" "^%s")
  1873. contents))
  1874. ;;;; Target
  1875. (defun org-element-target-parser ()
  1876. "Parse target at point.
  1877. Return a list whose car is `target' and cdr a plist with
  1878. `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
  1879. and `:post-blank' as keywords.
  1880. Assume point is at the target."
  1881. (save-excursion
  1882. (looking-at org-target-regexp)
  1883. (let ((begin (point))
  1884. (contents-begin (match-beginning 1))
  1885. (contents-end (match-end 1))
  1886. (raw-value (org-match-string-no-properties 1))
  1887. (post-blank (progn (goto-char (match-end 0))
  1888. (skip-chars-forward " \t")))
  1889. (end (point)))
  1890. (list 'target
  1891. `(:begin ,begin
  1892. :end ,end
  1893. :contents-begin ,contents-begin
  1894. :contents-end ,contents-end
  1895. :raw-value ,raw-value
  1896. :post-blank ,post-blank)))))
  1897. (defun org-element-target-interpreter (target contents)
  1898. "Interpret TARGET object as Org syntax.
  1899. CONTENTS is the contents of target."
  1900. (concat ""))
  1901. (defun org-element-target-successor (limit)
  1902. "Search for the next target and return position.
  1903. LIMIT bounds the search.
  1904. Return value is a cons cell whose car is `target' and cdr is
  1905. beginning position."
  1906. (save-excursion
  1907. (when (re-search-forward org-target-regexp limit t)
  1908. (cons 'target (match-beginning 0)))))
  1909. ;;;; Time-stamp
  1910. (defun org-element-time-stamp-parser ()
  1911. "Parse time stamp at point.
  1912. Return a list whose car is `time-stamp', and cdr a plist with
  1913. `:appt-type', `:type', `:begin', `:end', `:value' and
  1914. `:post-blank' keywords.
  1915. Assume point is at the beginning of the time-stamp."
  1916. (save-excursion
  1917. (let* ((appt-type (cond
  1918. ((looking-at (concat org-deadline-string " +"))
  1919. (goto-char (match-end 0))
  1920. 'deadline)
  1921. ((looking-at (concat org-scheduled-string " +"))
  1922. (goto-char (match-end 0))
  1923. 'scheduled)
  1924. ((looking-at (concat org-closed-string " +"))
  1925. (goto-char (match-end 0))
  1926. 'closed)))
  1927. (begin (and appt-type (match-beginning 0)))
  1928. (type (cond
  1929. ((looking-at org-tsr-regexp)
  1930. (if (match-string 2) 'active-range 'active))
  1931. ((looking-at org-tsr-regexp-both)
  1932. (if (match-string 2) 'inactive-range 'inactive))
  1933. ((looking-at (concat
  1934. "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  1935. "\\|"
  1936. "\\(<%%\\(([^>\n]+)\\)>\\)"))
  1937. 'diary)))
  1938. (begin (or begin (match-beginning 0)))
  1939. (value (buffer-substring-no-properties
  1940. (match-beginning 0) (match-end 0)))
  1941. (post-blank (progn (goto-char (match-end 0))
  1942. (skip-chars-forward " \t")))
  1943. (end (point)))
  1944. (list 'time-stamp
  1945. `(:appt-type ,appt-type
  1946. :type ,type
  1947. :value ,value
  1948. :begin ,begin
  1949. :end ,end
  1950. :post-blank ,post-blank)))))
  1951. (defun org-element-time-stamp-interpreter (time-stamp contents)
  1952. "Interpret TIME-STAMP object as Org syntax.
  1953. CONTENTS is nil."
  1954. (concat
  1955. (case (org-element-get-property :appt-type time-stamp)
  1956. (closed (concat org-closed-string " "))
  1957. (deadline (concat org-deadline-string " "))
  1958. (scheduled (concat org-scheduled-string " ")))
  1959. (org-element-get-property :value time-stamp)))
  1960. (defun org-element-time-stamp-successor (limit)
  1961. "Search for the next time-stamp and return position.
  1962. LIMIT bounds the search.
  1963. Return value is a cons cell whose car is `time-stamp' and cdr is
  1964. beginning position."
  1965. (save-excursion
  1966. (when (re-search-forward
  1967. (concat "\\(?:" org-scheduled-string " +\\|"
  1968. org-deadline-string " +\\|" org-closed-string " +\\)?"
  1969. org-ts-regexp-both
  1970. "\\|"
  1971. "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  1972. "\\|"
  1973. "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
  1974. limit t)
  1975. (cons 'time-stamp (match-beginning 0)))))
  1976. ;;;; Verbatim
  1977. (defun org-element-verbatim-parser ()
  1978. "Parse verbatim object at point.
  1979. Return a list whose car is `verbatim' and cdr is a plist with
  1980. `:marker', `:begin', `:end' and `:post-blank' keywords.
  1981. Assume point is at the first verbatim marker."
  1982. (save-excursion
  1983. (unless (bolp) (backward-char 1))
  1984. (looking-at org-emph-re)
  1985. (let ((begin (match-beginning 2))
  1986. (marker (org-match-string-no-properties 3))
  1987. (value (org-match-string-no-properties 4))
  1988. (post-blank (progn (goto-char (match-end 2))
  1989. (skip-chars-forward " \t")))
  1990. (end (point)))
  1991. (list 'verbatim
  1992. `(:marker ,marker
  1993. :begin ,begin
  1994. :end ,end
  1995. :value ,value
  1996. :post-blank ,post-blank)))))
  1997. (defun org-element-verbatim-interpreter (verbatim contents)
  1998. "Interpret VERBATIM object as Org syntax.
  1999. CONTENTS is nil."
  2000. (let ((marker (org-element-get-property :marker verbatim))
  2001. (value (org-element-get-property :value verbatim)))
  2002. (concat marker value marker)))
  2003. ;;; Definitions And Rules
  2004. ;; Define elements, greater elements and specify recursive objects,
  2005. ;; along with the affiliated keywords recognized. Also set up
  2006. ;; restrictions on recursive objects combinations.
  2007. ;; These variables really act as a control center for the parsing
  2008. ;; process.
  2009. (defconst org-element-paragraph-separate
  2010. (concat "\f" "\\|" "^[ \t]*$" "\\|"
  2011. ;; Headlines and inlinetasks.
  2012. org-outline-regexp-bol "\\|"
  2013. ;; Comments, blocks (any type), keywords and babel calls.
  2014. "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
  2015. ;; Lists.
  2016. (org-item-beginning-re) "\\|"
  2017. ;; Fixed-width, drawers (any type) and tables.
  2018. "^[ \t]*[:|]" "\\|"
  2019. ;; Footnote definitions.
  2020. org-footnote-definition-re "\\|"
  2021. ;; Horizontal rules.
  2022. "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
  2023. ;; LaTeX environments.
  2024. "^[ \t]*\\\\\\(begin\\|end\\)")
  2025. "Regexp to separate paragraphs in an Org buffer.")
  2026. (defconst org-element-all-elements
  2027. '(center-block comment comment-block drawer dynamic-block example-block
  2028. export-block fixed-width footnote-definition headline
  2029. horizontal-rule inlinetask item keyword latex-environment
  2030. babel-call paragraph plain-list property-drawer quote-block
  2031. quote-section special-block src-block table verse-block)
  2032. "Complete list of elements.")
  2033. (defconst org-element-greater-elements
  2034. '(center-block drawer dynamic-block footnote-definition headline inlinetask
  2035. item plain-list quote-block special-block)
  2036. "List of recursive element types aka Greater Elements.")
  2037. (defconst org-element-all-successors
  2038. '(export-snippet footnote-reference inline-babel-call inline-src-block
  2039. latex-or-entity line-break link macro radio-target
  2040. statistics-cookie sub/superscript target text-markup
  2041. time-stamp)
  2042. "Complete list of successors.")
  2043. (defconst org-element-object-successor-alist
  2044. '((subscript . sub/superscript) (superscript . sub/superscript)
  2045. (emphasis . text-markup) (verbatim . text-markup)
  2046. (entity . latex-or-entity) (latex-fragment . latex-or-entity))
  2047. "Alist of translations between object type and successor name.
  2048. Sharing the same successor comes handy when, for example, the
  2049. regexp matching one object can also match the other object.")
  2050. (defconst org-element-recursive-objects
  2051. '(emphasis link subscript superscript target radio-target)
  2052. "List of recursive object types.")
  2053. (defconst org-element-non-recursive-block-alist
  2054. '(("ascii" . export-block)
  2055. ("comment" . comment-block)
  2056. ("docbook" . export-block)
  2057. ("example" . example-block)
  2058. ("html" . export-block)
  2059. ("latex" . latex-block)
  2060. ("odt" . export-block)
  2061. ("src" . src-block)
  2062. ("verse" . verse-block))
  2063. "Alist between non-recursive block name and their element type.")
  2064. (defconst org-element-affiliated-keywords
  2065. '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
  2066. "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
  2067. "source" "srcname" "tblname")
  2068. "List of affiliated keywords as strings.")
  2069. (defconst org-element-keyword-translation-alist
  2070. '(("tblname" . "name") ("srcname" . "name") ("resname" . "name")
  2071. ("source" . "name") ("result" . "results") ("headers" . "header")
  2072. ("label" . "name"))
  2073. "Alist of usual translations for keywords.
  2074. The key is the old name and the value the new one. The property
  2075. holding their value will be named after the translated name.")
  2076. (defconst org-element-multiple-keywords
  2077. '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
  2078. "List of affiliated keywords that can occur more that once in an element.
  2079. Their value will be consed into a list of strings, which will be
  2080. returned as the value of the property.
  2081. This list is checked after translations have been applied. See
  2082. `org-element-keyword-translation-alist'.")
  2083. (defconst org-element-parsed-keywords '("author" "caption" "title")
  2084. "List of keywords whose value can be parsed.
  2085. Their value will be stored as a secondary string: a list of
  2086. strings and objects.
  2087. This list is checked after translations have been applied. See
  2088. `org-element-keyword-translation-alist'.")
  2089. (defconst org-element-dual-keywords '("results")
  2090. "List of keywords which can have a secondary value.
  2091. In Org syntax, they can be written with optional square brackets
  2092. before the colons. For example, results keyword can be
  2093. associated to a hash value with the following:
  2094. #+results[hash-string]: some-source
  2095. This list is checked after translations have been applied. See
  2096. `org-element-keyword-translation-alist'.")
  2097. (defconst org-element-object-restrictions
  2098. '((emphasis entity export-snippet inline-babel-call inline-src-block
  2099. radio-target sub/superscript target text-markup time-stamp)
  2100. (link entity export-snippet inline-babel-call inline-src-block
  2101. latex-fragment sub/superscript text-markup)
  2102. (radio-target entity export-snippet latex-fragment sub/superscript)
  2103. (subscript entity export-snippet inline-babel-call inline-src-block
  2104. latex-fragment sub/superscript text-markup)
  2105. (superscript entity export-snippet inline-babel-call inline-src-block
  2106. latex-fragment sub/superscript text-markup)
  2107. (target entity export-snippet latex-fragment sub/superscript text-markup))
  2108. "Alist of recursive objects restrictions.
  2109. Car is a recursive object type and cdr is a list of successors
  2110. that will be called within an object of such type.
  2111. For example, in a `radio-target' object, one can only find
  2112. entities, export snippets, latex-fragments, subscript and
  2113. superscript.")
  2114. (defconst org-element-string-restrictions
  2115. '((headline entity inline-babel-call latex-fragment link macro radio-target
  2116. statistics-cookie sub/superscript text-markup time-stamp)
  2117. (inlinetask entity inline-babel-call latex-fragment link macro radio-target
  2118. sub/superscript text-markup time-stamp)
  2119. (item entity inline-babel-call latex-fragment macro radio-target
  2120. sub/superscript target verbatim)
  2121. (keyword entity latex-fragment macro sub/superscript text-markup)
  2122. (table entity latex-fragment macro text-markup)
  2123. (verse entity footnote-reference inline-babel-call inline-src-block
  2124. latex-fragment line-break link macro radio-target sub/superscript
  2125. target text-markup time-stamp))
  2126. "Alist of secondary strings restrictions.
  2127. When parsed, some elements have a secondary string which could
  2128. contain various objects (i.e. headline's name, or table's cells).
  2129. For association, the car is the element type, and the cdr a list
  2130. of successors that will be called in that secondary string.
  2131. Note: `keyword' secondary string type only applies to keywords
  2132. matching `org-element-parsed-keywords'.")
  2133. ;;; Accessors
  2134. ;;
  2135. ;; Provide two accessors: `org-element-get-property' and
  2136. ;; `org-element-get-contents'.
  2137. (defun org-element-get-property (property element)
  2138. "Extract the value from the PROPERTY of an ELEMENT."
  2139. (plist-get (nth 1 element) property))
  2140. (defun org-element-get-contents (element)
  2141. "Extract contents from an ELEMENT."
  2142. (nthcdr 2 element))
  2143. ;; Obtaining The Smallest Element Containing Point
  2144. ;; `org-element-at-point' is the core function of this section. It
  2145. ;; returns the Lisp representation of the element at point. It uses
  2146. ;; `org-element-guess-type' and `org-element-skip-keywords' as helper
  2147. ;; functions.
  2148. ;; When point is at an item, there is no automatic way to determine if
  2149. ;; the function should return the `plain-list' element, or the
  2150. ;; corresponding `item' element. By default, `org-element-at-point'
  2151. ;; works at the `plain-list' level. But, by providing an optional
  2152. ;; argument, one can make it switch to the `item' level.
  2153. (defconst org-element--affiliated-re
  2154. (format "[ \t]*#\\+\\(%s\\):"
  2155. (mapconcat
  2156. (lambda (keyword)
  2157. (if (member keyword org-element-dual-keywords)
  2158. (format "\\(%s\\)\\(?:\\[\\(.*?\\)\\]\\)?"
  2159. (regexp-quote keyword))
  2160. (regexp-quote keyword)))
  2161. org-element-affiliated-keywords "\\|"))
  2162. "Regexp matching any affiliated keyword.
  2163. Keyword name is put in match group 1. Moreover, if keyword
  2164. belongs to `org-element-dual-keywords', put the dual value in
  2165. match group 2.
  2166. Don't modify it, set `org-element--affiliated-keywords' instead.")
  2167. (defun org-element-at-point (&optional toggle-item structure)
  2168. "Determine closest element around point.
  2169. Return value is a list \(TYPE PROPS\) where TYPE is the type of
  2170. the element and PROPS a plist of properties associated to the
  2171. element.
  2172. Possible types are defined in `org-element-all-elements'.
  2173. If optional argument TOGGLE-ITEM is non-nil, parse item wise
  2174. instead of plain-list wise, using STRUCTURE as the current list
  2175. structure.
  2176. If STRUCTURE isn't provided but TOGGLE-ITEM is non-nil, it will
  2177. be computed."
  2178. (save-excursion
  2179. (beginning-of-line)
  2180. ;; Move before any blank line.
  2181. (when (looking-at "[ \t]*$")
  2182. (skip-chars-backward " \r\t\n")
  2183. (beginning-of-line))
  2184. (let ((case-fold-search t))
  2185. ;; Check if point is at an affiliated keyword. In that case,
  2186. ;; try moving to the beginning of the associated element. If
  2187. ;; the keyword is orphaned, treat it as plain text.
  2188. (when (looking-at org-element--affiliated-re)
  2189. (let ((opoint (point)))
  2190. (while (looking-at org-element--affiliated-re) (forward-line))
  2191. (when (looking-at "[ \t]*$") (goto-char opoint))))
  2192. (let ((type (org-element-guess-type)))
  2193. (cond
  2194. ;; Guessing element type on the current line is impossible:
  2195. ;; try to find the beginning of the current element to get
  2196. ;; more information.
  2197. ((not type)
  2198. (let ((search-origin (point))
  2199. (opoint-in-item-p (org-in-item-p))
  2200. (par-found-p
  2201. (progn
  2202. (end-of-line)
  2203. (re-search-backward org-element-paragraph-separate nil 'm))))
  2204. (cond
  2205. ;; Unable to find a paragraph delimiter above: we're at
  2206. ;; bob and looking at a paragraph.
  2207. ((not par-found-p) (org-element-paragraph-parser))
  2208. ;; Trying to find element's beginning set point back to
  2209. ;; its original position. There's something peculiar on
  2210. ;; this line that prevents parsing, probably an
  2211. ;; ill-formed keyword or an undefined drawer name. Parse
  2212. ;; it as plain text anyway.
  2213. ((< search-origin (point-at-eol)) (org-element-paragraph-parser))
  2214. ;; Original point wasn't in a list but previous paragraph
  2215. ;; is. It means that either point was inside some block,
  2216. ;; or current list was ended without using a blank line.
  2217. ;; In the last case, paragraph really starts at list end.
  2218. ((let (item)
  2219. (and (not opoint-in-item-p)
  2220. (not (looking-at "[ \t]*#\\+begin"))
  2221. (setq item (org-in-item-p))
  2222. (let ((struct (save-excursion (goto-char item)
  2223. (org-list-struct))))
  2224. (goto-char (org-list-get-bottom-point struct))
  2225. (org-skip-whitespace)
  2226. (beginning-of-line)
  2227. (org-element-paragraph-parser)))))
  2228. ((org-footnote-at-definition-p)
  2229. (org-element-footnote-definition-parser))
  2230. ((and opoint-in-item-p (org-at-item-p) (= opoint-in-item-p (point)))
  2231. (if toggle-item
  2232. (org-element-item-parser (or structure (org-list-struct)))
  2233. (org-element-plain-list-parser (or structure (org-list-struct)))))
  2234. ;; In any other case, the paragraph started the line
  2235. ;; below.
  2236. (t (forward-line) (org-element-paragraph-parser)))))
  2237. ((eq type 'plain-list)
  2238. (if toggle-item
  2239. (org-element-item-parser (or structure (org-list-struct)))
  2240. (org-element-plain-list-parser (or structure (org-list-struct)))))
  2241. ;; Straightforward case: call the appropriate parser.
  2242. (t (funcall (intern (format "org-element-%s-parser" type)))))))))
  2243. ;; It is obvious to tell if point is in most elements, either by
  2244. ;; looking for a specific regexp in the current line, or by using
  2245. ;; already implemented functions. This is the goal of
  2246. ;; `org-element-guess-type'.
  2247. (defconst org-element--element-block-types
  2248. (mapcar 'car org-element-non-recursive-block-alist)
  2249. "List of non-recursive block types, as strings.
  2250. Used internally by `org-element-guess-type'. Do not modify it
  2251. directly, set `org-element-non-recursive-block-alist' instead.")
  2252. (defun org-element-guess-type ()
  2253. "Return the type of element at point, or nil if undetermined.
  2254. This function may move point to an appropriate position for
  2255. parsing. Used internally by `org-element-at-point'."
  2256. ;; Beware: Order matters for some cases in that function.
  2257. (beginning-of-line)
  2258. (let ((case-fold-search t))
  2259. (cond
  2260. ((org-with-limited-levels (org-at-heading-p)) 'headline)
  2261. ((let ((headline (ignore-errors (nth 4 (org-heading-components)))))
  2262. (and headline
  2263. (let (case-fold-search)
  2264. (string-match (format "^%s\\(?: \\|$\\)" org-quote-string)
  2265. headline))))
  2266. 'quote-section)
  2267. ;; Non-recursive block.
  2268. ((let ((type (org-in-block-p org-element--element-block-types)))
  2269. (and type (cdr (assoc type org-element-non-recursive-block-alist)))))
  2270. ((org-at-heading-p) 'inlinetask)
  2271. ((org-between-regexps-p
  2272. "^[ \t]*\\\\begin{" "^[ \t]*\\\\end{[^}]*}[ \t]*") 'latex-environment)
  2273. ;; Property drawer. Almost `org-at-property-p', but allow drawer
  2274. ;; boundaries.
  2275. ((org-with-wide-buffer
  2276. (and (not (org-before-first-heading-p))
  2277. (let ((pblock (org-get-property-block)))
  2278. (and pblock
  2279. (<= (point) (cdr pblock))
  2280. (>= (point-at-eol) (1- (car pblock)))))))
  2281. 'property-drawer)
  2282. ;; Recursive block. If the block isn't complete, parse the
  2283. ;; current part as a paragraph.
  2284. ((looking-at "[ \t]*#\\+\\(begin\\|end\\)_\\([-A-Za-z0-9]+\\)\\(?:$\\|\\s-\\)")
  2285. (let ((type (downcase (match-string 2))))
  2286. (cond
  2287. ((not (org-in-block-p (list type))) 'paragraph)
  2288. ((string= type "center") 'center-block)
  2289. ((string= type "quote") 'quote-block)
  2290. (t 'special-block))))
  2291. ;; Regular drawers must be tested after property drawer as both
  2292. ;; elements share the same ending regexp.
  2293. ((or (looking-at org-drawer-regexp) (looking-at "[ \t]*:END:[ \t]*$"))
  2294. (let ((completep (org-between-regexps-p
  2295. org-drawer-regexp "^[ \t]*:END:[ \t]*$")))
  2296. (if (not completep)
  2297. 'paragraph
  2298. (goto-char (car completep)) 'drawer)))
  2299. ((looking-at "[ \t]*:\\( \\|$\\)") 'fixed-width)
  2300. ;; Babel calls must be tested before general keywords as they are
  2301. ;; a subset of them.
  2302. ((looking-at org-babel-block-lob-one-liner-regexp) 'babel-call)
  2303. ((looking-at org-footnote-definition-re) 'footnote-definition)
  2304. ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
  2305. (if (member (downcase (match-string 1)) org-element-affiliated-keywords)
  2306. 'paragraph
  2307. 'keyword))
  2308. ;; Dynamic block: simplify regexp used for match. If it isn't
  2309. ;; complete, parse the current part as a paragraph.
  2310. ((looking-at "[ \t]*#\\+\\(begin\\end\\):\\(?:\\s-\\|$\\)")
  2311. (let ((completep (org-between-regexps-p
  2312. "^[ \t]*#\\+begin:\\(?:\\s-\\|$\\)"
  2313. "^[ \t]*#\\+end:\\(?:\\s-\\|$\\)")))
  2314. (if (not completep)
  2315. 'paragraph
  2316. (goto-char (car completep)) 'dynamic-block)))
  2317. ((looking-at "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)") 'comment)
  2318. ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") 'horizontal-rule)
  2319. ((org-at-table-p t) 'table)
  2320. ((looking-at "[ \t]*#\\+tblfm:")
  2321. (forward-line -1)
  2322. ;; A TBLFM line separated from any table is just plain text.
  2323. (if (org-at-table-p)
  2324. 'table
  2325. (forward-line) 'paragraph))
  2326. ((looking-at (org-item-re)) 'plain-list))))
  2327. ;; Most elements can have affiliated keywords. When looking for an
  2328. ;; element beginning, we want to move before them, as they belong to
  2329. ;; that element, and, in the meantime, collect information they give
  2330. ;; into appropriate properties. Hence the following function.
  2331. ;; Usage of optional arguments may not be obvious at first glance:
  2332. ;; - TRANS-LIST is used to polish keywords names that have evolved
  2333. ;; during Org history. In example, even though =result= and
  2334. ;; =results= coexist, we want to have them under the same =result=
  2335. ;; property. It's also true for "srcname" and "name", where the
  2336. ;; latter seems to be preferred nowadays (thus the "name" property).
  2337. ;; - CONSED allows to regroup multi-lines keywords under the same
  2338. ;; property, while preserving their own identity. This is mostly
  2339. ;; used for "attr_latex" and al.
  2340. ;; - PARSED prepares a keyword value for export. This is useful for
  2341. ;; "caption". Objects restrictions for such keywords are defined in
  2342. ;; `org-element-string-restrictions'.
  2343. ;; - DUALS is used to take care of keywords accepting a main and an
  2344. ;; optional secondary values. For example "results" has its
  2345. ;; source's name as the main value, and may have an hash string in
  2346. ;; optional square brackets as the secondary one.
  2347. ;; A keyword may belong to more than one category.
  2348. (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
  2349. consed parsed duals)
  2350. "Collect affiliated keywords before point.
  2351. Optional argument KEY-RE is a regexp matching keywords, which
  2352. puts matched keyword in group 1. It defaults to
  2353. `org-element--affiliated-re'.
  2354. TRANS-LIST is an alist where key is the keyword and value the
  2355. property name it should be translated to, without the colons. It
  2356. defaults to `org-element-keyword-translation-alist'.
  2357. CONSED is a list of strings. Any keyword belonging to that list
  2358. will have its value consed. The check is done after keyword
  2359. translation. It defaults to `org-element-multiple-keywords'.
  2360. PARSED is a list of strings. Any keyword member of this list
  2361. will have its value parsed. The check is done after keyword
  2362. translation. If a keyword is a member of both CONSED and PARSED,
  2363. it's value will be a list of parsed strings. It defaults to
  2364. `org-element-parsed-keywords'.
  2365. DUALS is a list of strings. Any keyword member of this list can
  2366. have two parts: one mandatory and one optional. Its value is
  2367. a cons cell whose car is the former, and the cdr the latter. If
  2368. a keyword is a member of both PARSED and DUALS, only the primary
  2369. part will be parsed. It defaults to `org-element-dual-keywords'.
  2370. Return a list whose car is the position at the first of them and
  2371. cdr a plist of keywords and values."
  2372. (save-excursion
  2373. (let ((case-fold-search t)
  2374. (key-re (or key-re org-element--affiliated-re))
  2375. (trans-list (or trans-list org-element-keyword-translation-alist))
  2376. (consed (or consed org-element-multiple-keywords))
  2377. (parsed (or parsed org-element-parsed-keywords))
  2378. (duals (or duals org-element-dual-keywords))
  2379. output)
  2380. (unless (bobp)
  2381. (while (and (not (bobp))
  2382. (progn (forward-line -1) (looking-at key-re)))
  2383. (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
  2384. ;; Apply translation to RAW-KWD. From there, KWD is
  2385. ;; the official keyword.
  2386. (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
  2387. ;; If KWD is a dual keyword, find it secondary value.
  2388. (dual-value (and (member kwd duals)
  2389. (org-match-string-no-properties 3)))
  2390. ;; Find main value for any keyword.
  2391. (value (org-trim (buffer-substring-no-properties
  2392. (match-end 0) (point-at-eol))))
  2393. ;; Attribute a property name to KWD.
  2394. (kwd-sym (and kwd (intern (concat ":" kwd)))))
  2395. ;; Now set final shape for VALUE.
  2396. (when (member kwd parsed)
  2397. (setq value
  2398. (org-element-parse-secondary-string
  2399. value
  2400. (cdr (assq 'keyword org-element-string-restrictions)))))
  2401. (when (member kwd duals) (setq value (cons value dual-value)))
  2402. (when (member kwd consed)
  2403. (setq value (cons value (plist-get output kwd-sym))))
  2404. ;; Eventually store the new value in OUTPUT.
  2405. (setq output (plist-put output kwd-sym value))))
  2406. (unless (looking-at key-re) (forward-line 1)))
  2407. (list (point) output))))
  2408. ;;; The Org Parser
  2409. ;; The two major functions here are `org-element-parse-buffer', which
  2410. ;; parses Org syntax inside the current buffer, taking into account
  2411. ;; region, narrowing, or even visibility if specified, and
  2412. ;; `org-element-parse-secondary-string', which parses objects within
  2413. ;; a given string.
  2414. ;; The (almost) almighty `org-element-map' allows to apply a function
  2415. ;; on elements or objects matching some type, and accumulate the
  2416. ;; resulting values. In an export situation, it also skips unneeded
  2417. ;; parts of the parse tree, transparently walks into included files,
  2418. ;; and maintain a list of local properties (i.e. those inherited from
  2419. ;; parent headlines) for function's consumption.
  2420. (defun org-element-parse-buffer (&optional granularity visible-only)
  2421. "Recursively parse the buffer and return structure.
  2422. If narrowing is in effect, only parse the visible part of the
  2423. buffer.
  2424. Optional argument GRANULARITY determines the depth of the
  2425. recursion. It can be set to the following symbols:
  2426. `headline' Only parse headlines.
  2427. `greater-element' Don't recurse into greater elements. Thus,
  2428. elements parsed are the top-level ones.
  2429. `element' Parse everything but objects and plain text.
  2430. `object' Parse the complete buffer (default).
  2431. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  2432. elements.
  2433. Assume buffer is in Org mode."
  2434. (save-excursion
  2435. (goto-char (point-min))
  2436. (org-skip-whitespace)
  2437. (nconc (list 'org-data nil)
  2438. (org-element-parse-elements
  2439. (point-at-bol) (point-max)
  2440. nil nil granularity visible-only nil))))
  2441. (defun org-element-parse-secondary-string (string restriction &optional buffer)
  2442. "Recursively parse objects in STRING and return structure.
  2443. RESTRICTION, when non-nil, is a symbol limiting the object types
  2444. that will be looked after.
  2445. Optional argument BUFFER indicates the buffer from where the
  2446. secondary string was extracted. It is used to determine where to
  2447. get extraneous information for an object \(i.e. when resolving
  2448. a link or looking for a footnote definition\). It defaults to
  2449. the current buffer."
  2450. (with-temp-buffer
  2451. (insert string)
  2452. (org-element-parse-objects (point-min) (point-max) nil restriction)))
  2453. (defun org-element-map (data types fun &optional options first-match)
  2454. "Map a function on selected elements or objects.
  2455. DATA is the parsed tree, as returned by, i.e,
  2456. `org-element-parse-buffer'. TYPES is a symbol or list of symbols
  2457. of elements or objects types. FUN is the function called on the
  2458. matching element or object. It must accept two arguments: the
  2459. element or object itself and a plist holding contextual
  2460. information.
  2461. When optional argument OPTIONS is non-nil, it should be a plist
  2462. holding export options. In that case, parts of the parse tree
  2463. not exportable according to that property list will be skipped
  2464. and files included through a keyword will be visited.
  2465. When optional argument FIRST-MATCH is non-nil, stop at the first
  2466. match for which FUN doesn't return nil, and return that value.
  2467. Nil values returned from FUN are ignored in the result."
  2468. ;; Ensure TYPES is a list, even of one element.
  2469. (unless (listp types) (setq types (list types)))
  2470. ;; Recursion depth is determined by TYPE-CATEGORY, to avoid
  2471. ;; unnecessary steps.
  2472. (let* ((type-category
  2473. (cond
  2474. ((loop for type in types
  2475. always (memq type org-element-greater-elements))
  2476. 'greater-elements)
  2477. ((loop for type in types
  2478. always (memq type org-element-all-elements))
  2479. 'elements)
  2480. (t 'objects)))
  2481. walk-tree ; For byte-compiler
  2482. acc ; Accumulate results into ACC.
  2483. (accumulate-maybe
  2484. (function
  2485. ;; Check if TYPE is matching among TYPES. If so, apply FUN
  2486. ;; to BLOB and accumulate return value into ACC. INFO is
  2487. ;; the communication channel.
  2488. (lambda (type types fun blob info)
  2489. (when (memq type types)
  2490. (let ((result (funcall fun blob info)))
  2491. (cond
  2492. ((not result))
  2493. (first-match (throw 'first-match result))
  2494. (t (push result acc))))))))
  2495. (walk-tree
  2496. (function
  2497. ;; Recursively walk DATA. INFO, if non-nil, is a plist
  2498. ;; holding contextual information.
  2499. (lambda (data info)
  2500. (mapc
  2501. (lambda (blob)
  2502. (let ((type (if (stringp blob) 'plain-text (car blob))))
  2503. ;; Determine if a recursion into BLOB is possible
  2504. ;; and allowed.
  2505. (cond
  2506. ;; Element or object not exportable.
  2507. ((and info (org-export-skip-p blob info)))
  2508. ;; Archived headline: skip it.
  2509. ((and info
  2510. (eq type 'headline)
  2511. (and (eq (plist-get info :with-archived-trees)
  2512. 'headline)
  2513. (org-element-get-property :archivedp blob)))
  2514. (funcall accumulate-maybe type types fun blob info))
  2515. ;; At an include keyword: apply mapping to its
  2516. ;; contents.
  2517. ((and info
  2518. (eq type 'keyword)
  2519. (string=
  2520. (downcase (org-element-get-property :key blob))
  2521. "include"))
  2522. (funcall accumulate-maybe type types fun blob info)
  2523. (let* ((data (org-export-parse-included-file blob info))
  2524. (value (org-element-get-property :value blob))
  2525. (file (and (string-match "^\"\\(\\S-+\\)\"" value)
  2526. (match-string 1 value))))
  2527. (funcall
  2528. walk-tree
  2529. data
  2530. (org-combine-plists
  2531. info
  2532. ;; Store full path of already included files
  2533. ;; to avoid recursive file inclusion.
  2534. `(:included-files
  2535. ,(cons (expand-file-name file)
  2536. (plist-get info :included-files))
  2537. ;; Ensure that a top-level headline in the
  2538. ;; included file becomes a direct child of
  2539. ;; the current headline in the buffer.
  2540. :headline-offset
  2541. ,(- (+ (plist-get
  2542. (plist-get info :inherited-properties) :level)
  2543. (or (plist-get info :headline-offset) 0))
  2544. (1- (org-export-get-min-level data info))))))))
  2545. ;; Limiting recursion to greater elements, and BLOB
  2546. ;; isn't one.
  2547. ((and (eq type-category 'greater-elements)
  2548. (not (memq type org-element-greater-elements)))
  2549. (funcall accumulate-maybe type types fun blob info))
  2550. ;; Limiting recursion to elements, and BLOB only
  2551. ;; contains objects.
  2552. ((and (eq type-category 'elements) (eq type 'paragraph)))
  2553. ;; No limitation on recursion, but BLOB hasn't got
  2554. ;; a recursive type.
  2555. ((and (eq type-category 'objects)
  2556. (not (or (eq type 'paragraph)
  2557. (memq type org-element-greater-elements)
  2558. (memq type org-element-recursive-objects))))
  2559. (funcall accumulate-maybe type types fun blob info))
  2560. ;; Recursion is possible and allowed: Update local
  2561. ;; informations and move into BLOB.
  2562. (t (funcall accumulate-maybe type types fun blob info)
  2563. (funcall
  2564. walk-tree
  2565. blob
  2566. (and options (org-export-update-info blob info t)))))))
  2567. (org-element-get-contents data))))))
  2568. (catch 'first-match
  2569. (funcall walk-tree data options)
  2570. ;; Return value in a proper order.
  2571. (reverse acc))))
  2572. ;; The following functions are internal parts of the parser. The
  2573. ;; first one, `org-element-parse-elements' acts at the element's
  2574. ;; level. The second one, `org-element-parse-objects' applies on all
  2575. ;; objects of a paragraph or a secondary string. It uses
  2576. ;; `org-element-get-candidates' to optimize the search of the next
  2577. ;; object in the buffer.
  2578. ;;
  2579. ;; More precisely, that function looks for every allowed object type
  2580. ;; first. Then, it discards failed searches, keeps further matches,
  2581. ;; and searches again types matched behind point, for subsequent
  2582. ;; calls. Thus, searching for a given type fails only once, and every
  2583. ;; object is searched only once at top level (but sometimes more for
  2584. ;; nested types).
  2585. (defun org-element-parse-elements (beg end item structure granularity visible-only acc)
  2586. "Parse ELEMENT with point at its beginning.
  2587. If ITEM is non-nil, parse item wise instead of plain-list wise,
  2588. using STRUCTURE as the current list structure.
  2589. GRANULARITY determines the depth of the recursion. It can be set
  2590. to the following symbols:
  2591. `headline' Only parse headlines.
  2592. `greater-element' Don't recurse into greater elements. Thus,
  2593. elements parsed are the top-level ones.
  2594. `element' Parse everything but objects and plain text.
  2595. `object' or nil Parse the complete buffer.
  2596. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  2597. greater elements.
  2598. Elements are accumulated into ACC."
  2599. (save-excursion
  2600. (goto-char beg)
  2601. ;; Shortcut when parsing only headlines.
  2602. (when (and (eq granularity 'headline) (not (org-at-heading-p)))
  2603. (org-with-limited-levels (outline-next-heading)))
  2604. ;; Main loop start.
  2605. (while (and (< (point) end) (not (eobp)))
  2606. (push
  2607. ;; 1. If ITEM is toggled, point is at an item. Knowing that,
  2608. ;; there's no need to go through `org-element-at-point'.
  2609. (if item
  2610. (let* ((element (org-element-item-parser structure))
  2611. (cbeg (org-element-get-property :contents-begin element))
  2612. (cend (org-element-get-property :contents-end element)))
  2613. (goto-char (org-element-get-property :end element))
  2614. ;; Narrow region to contents, so that item bullet don't
  2615. ;; interfere with paragraph parsing.
  2616. (save-restriction
  2617. (narrow-to-region cbeg cend)
  2618. (org-element-parse-elements
  2619. cbeg cend nil structure granularity visible-only
  2620. (reverse element))))
  2621. ;; 2. When ITEM is nil, find current element's type and parse
  2622. ;; it accordingly to its category.
  2623. (let ((element (org-element-at-point nil structure)))
  2624. (goto-char (org-element-get-property :end element))
  2625. (cond
  2626. ;; Case 1: ELEMENT is a footnote-definition. If
  2627. ;; GRANURALITY allows parsing, use narrowing so that
  2628. ;; footnote label don't interfere with paragraph
  2629. ;; recognition.
  2630. ((and (eq (car element) 'footnote-definition)
  2631. (not (memq granularity '(headline greater-element))))
  2632. (let ((cbeg (org-element-get-property :contents-begin element))
  2633. (cend (org-element-get-property :contents-end element)))
  2634. (save-restriction
  2635. (narrow-to-region cbeg cend)
  2636. (org-element-parse-elements
  2637. cbeg cend nil structure granularity visible-only
  2638. (reverse element)))))
  2639. ;; Case 1: ELEMENT is a paragraph. Parse objects inside,
  2640. ;; if GRANULARITY allows it.
  2641. ((and (eq (car element) 'paragraph)
  2642. (or (not granularity) (eq granularity 'object)))
  2643. (org-element-parse-objects
  2644. (org-element-get-property :contents-begin element)
  2645. (org-element-get-property :contents-end element)
  2646. (reverse element)
  2647. nil))
  2648. ;; Case 2: ELEMENT is recursive: parse it between
  2649. ;; `contents-begin' and `contents-end'. If it's
  2650. ;; a plain list, also switch to item mode. Make
  2651. ;; sure GRANULARITY allows the recursion, or
  2652. ;; ELEMENT is an headline, in which case going
  2653. ;; inside is mandatory, in order to get sub-level
  2654. ;; headings. If VISIBLE-ONLY is true and element
  2655. ;; is hidden, do not recurse into it.
  2656. ((and (memq (car element) org-element-greater-elements)
  2657. (or (not granularity)
  2658. (memq granularity '(element object))
  2659. (eq (car element) 'headline))
  2660. (not (and visible-only
  2661. (org-element-get-property :hiddenp element))))
  2662. (org-element-parse-elements
  2663. (org-element-get-property :contents-begin element)
  2664. (org-element-get-property :contents-end element)
  2665. (eq (car element) 'plain-list)
  2666. (org-element-get-property :structure element)
  2667. granularity
  2668. visible-only
  2669. (reverse element)))
  2670. ;; Case 3: Else, just accumulate ELEMENT, unless
  2671. ;; GRANULARITY is set to `headline'.
  2672. ((not (eq granularity 'headline)) element))))
  2673. acc)
  2674. (org-skip-whitespace))
  2675. ;; Return result.
  2676. (nreverse acc)))
  2677. (defun org-element-parse-objects (beg end acc restriction)
  2678. "Parse objects between BEG and END and return recursive structure.
  2679. Objects are accumulated in ACC.
  2680. RESTRICTION, when non-nil, is a list of object types which are
  2681. allowed in the current object."
  2682. (let ((get-next-object
  2683. (function
  2684. (lambda (cand)
  2685. ;; Return the parsing function associated to the nearest
  2686. ;; object among list of candidates CAND.
  2687. (let ((pos (apply #'min (mapcar #'cdr cand))))
  2688. (save-excursion
  2689. (goto-char pos)
  2690. (funcall
  2691. (intern
  2692. (format "org-element-%s-parser" (car (rassq pos cand))))))))))
  2693. next-object candidates)
  2694. (save-excursion
  2695. (goto-char beg)
  2696. (while (setq candidates (org-element-get-next-object-candidates
  2697. end restriction candidates))
  2698. (setq next-object (funcall get-next-object candidates))
  2699. ;; 1. Text before any object.
  2700. (let ((obj-beg (org-element-get-property :begin next-object)))
  2701. (unless (= beg obj-beg)
  2702. (push (buffer-substring-no-properties (point) obj-beg) acc)))
  2703. ;; 2. Object...
  2704. (let ((obj-end (org-element-get-property :end next-object))
  2705. (cont-beg (org-element-get-property :contents-begin next-object)))
  2706. (push (if (and (memq (car next-object) org-element-recursive-objects)
  2707. cont-beg)
  2708. ;; ... recursive. The CONT-BEG check is for
  2709. ;; links, as some of them might not be recursive
  2710. ;; (i.e. plain links).
  2711. (save-restriction
  2712. (narrow-to-region
  2713. cont-beg
  2714. (org-element-get-property :contents-end next-object))
  2715. (org-element-parse-objects
  2716. (point-min) (point-max) (reverse next-object)
  2717. ;; Restrict allowed objects. This is the
  2718. ;; intersection of current restriction and next
  2719. ;; object's restriction.
  2720. (let ((new-restr
  2721. (cdr (assq (car next-object)
  2722. org-element-object-restrictions))))
  2723. (if (not restriction)
  2724. new-restr
  2725. (delq nil
  2726. (mapcar (lambda (e)
  2727. (and (memq e restriction) e))
  2728. new-restr))))))
  2729. ;; ... not recursive.
  2730. next-object)
  2731. acc)
  2732. (goto-char obj-end)))
  2733. ;; 3. Text after last object.
  2734. (unless (= (point) end)
  2735. (push (buffer-substring-no-properties (point) end) acc))
  2736. ;; Result.
  2737. (nreverse acc))))
  2738. (defun org-element-get-next-object-candidates (limit restriction objects)
  2739. "Return an alist of candidates for the next object.
  2740. LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
  2741. the possible object types.
  2742. Return value is an alist whose car is position and cdr the object
  2743. type, as a string. There is an association for the closest
  2744. object of each type within RESTRICTION when non-nil, or for every
  2745. type otherwise.
  2746. OBJECTS is the previous candidates alist."
  2747. (let ((restriction (or restriction org-element-all-successors))
  2748. next-candidates types-to-search)
  2749. ;; If no previous result, search every object type in RESTRICTION.
  2750. ;; Otherwise, keep potential candidates (old objects located after
  2751. ;; point) and ask to search again those which had matched before.
  2752. (if objects
  2753. (mapc (lambda (obj)
  2754. (if (< (cdr obj) (point))
  2755. (push (car obj) types-to-search)
  2756. (push obj next-candidates)))
  2757. objects)
  2758. (setq types-to-search restriction))
  2759. ;; Call the appropriate "get-next" function for each type to
  2760. ;; search and accumulate matches.
  2761. (mapc
  2762. (lambda (type)
  2763. (let* ((successor-fun
  2764. (intern
  2765. (format "org-element-%s-successor"
  2766. (or (cdr (assq type org-element-object-successor-alist))
  2767. type))))
  2768. (obj (funcall successor-fun limit)))
  2769. (and obj (push obj next-candidates))))
  2770. types-to-search)
  2771. ;; Return alist.
  2772. next-candidates))
  2773. ;;; Towards A Bijective Process
  2774. ;; The parse tree obtained with `org-element-parse-buffer' is really
  2775. ;; a snapshot of the corresponding Org buffer. Therefore, it can be
  2776. ;; interpreted and expanded into a string with canonical Org
  2777. ;; syntax. Hence `org-element-interpret-data'.
  2778. ;;
  2779. ;; Data parsed from secondary strings, whose shape is slightly
  2780. ;; different than the standard parse tree, is expanded with the
  2781. ;; equivalent function `org-element-interpret-secondary'.
  2782. ;;
  2783. ;; Both functions rely internally on
  2784. ;; `org-element-interpret--affiliated-keywords'.
  2785. (defun org-element-interpret-data (data &optional genealogy previous)
  2786. "Interpret a parse tree representing Org data.
  2787. DATA is the parse tree to interpret.
  2788. Optional arguments GENEALOGY and PREVIOUS are used for recursive
  2789. calls:
  2790. GENEALOGY is the list of its parents types.
  2791. PREVIOUS is the type of the element or object at the same level
  2792. interpreted before.
  2793. Return Org syntax as a string."
  2794. (mapconcat
  2795. (lambda (blob)
  2796. ;; BLOB can be an element, an object, a string, or nil.
  2797. (cond
  2798. ((not blob) nil)
  2799. ((equal blob "") nil)
  2800. ((stringp blob) blob)
  2801. (t
  2802. (let* ((type (car blob))
  2803. (interpreter
  2804. (if (eq type 'org-data)
  2805. 'identity
  2806. (intern (format "org-element-%s-interpreter" type))))
  2807. (contents
  2808. (cond
  2809. ;; Full Org document.
  2810. ((eq type 'org-data)
  2811. (org-element-interpret-data blob genealogy previous))
  2812. ;; Recursive objects.
  2813. ((memq type org-element-recursive-objects)
  2814. (org-element-interpret-data
  2815. blob (cons type genealogy) nil))
  2816. ;; Recursive elements.
  2817. ((memq type org-element-greater-elements)
  2818. (org-element-normalize-string
  2819. (org-element-interpret-data
  2820. blob (cons type genealogy) nil)))
  2821. ;; Paragraphs.
  2822. ((eq type 'paragraph)
  2823. (let ((paragraph
  2824. (org-element-normalize-contents
  2825. blob
  2826. ;; When normalizing contents of an item,
  2827. ;; ignore first line's indentation.
  2828. (and (not previous)
  2829. (memq (car genealogy)
  2830. '(footnote-definiton item))))))
  2831. (org-element-interpret-data
  2832. paragraph (cons type genealogy) nil)))))
  2833. (results (funcall interpreter blob contents)))
  2834. ;; Update PREVIOUS.
  2835. (setq previous type)
  2836. ;; Build white spaces.
  2837. (cond
  2838. ((eq type 'org-data) results)
  2839. ((memq type org-element-all-elements)
  2840. (concat
  2841. (org-element-interpret--affiliated-keywords blob)
  2842. (org-element-normalize-string results)
  2843. (make-string (org-element-get-property :post-blank blob) 10)))
  2844. (t (concat
  2845. results
  2846. (make-string
  2847. (org-element-get-property :post-blank blob) 32))))))))
  2848. (org-element-get-contents data) ""))
  2849. (defun org-element-interpret-secondary (secondary)
  2850. "Interpret SECONDARY string as Org syntax.
  2851. SECONDARY-STRING is a nested list as returned by
  2852. `org-element-parse-secondary-string'.
  2853. Return interpreted string."
  2854. ;; Make SECONDARY acceptable for `org-element-interpret-data'.
  2855. (let ((s (if (listp secondary) secondary (list secondary))))
  2856. (org-element-interpret-data `(org-data nil ,@s) nil nil)))
  2857. ;; Both functions internally use `org-element--affiliated-keywords'.
  2858. (defun org-element-interpret--affiliated-keywords (element)
  2859. "Return ELEMENT's affiliated keywords as Org syntax.
  2860. If there is no affiliated keyword, return the empty string."
  2861. (let ((keyword-to-org
  2862. (function
  2863. (lambda (key value)
  2864. (let (dual)
  2865. (when (member key org-element-dual-keywords)
  2866. (setq dual (cdr value) value (car value)))
  2867. (concat "#+" key (and dual (format "[%s]" dual)) ": "
  2868. (if (member key org-element-parsed-keywords)
  2869. (org-element-interpret-secondary value)
  2870. value)
  2871. "\n"))))))
  2872. (mapconcat
  2873. (lambda (key)
  2874. (let ((value (org-element-get-property (intern (concat ":" key)) element)))
  2875. (when value
  2876. (if (member key org-element-multiple-keywords)
  2877. (mapconcat (lambda (line)
  2878. (funcall keyword-to-org key line))
  2879. value "")
  2880. (funcall keyword-to-org key value)))))
  2881. ;; Remove translated keywords.
  2882. (delq nil
  2883. (mapcar
  2884. (lambda (key)
  2885. (and (not (assoc key org-element-keyword-translation-alist)) key))
  2886. org-element-affiliated-keywords))
  2887. "")))
  2888. ;; Because interpretation of the parse tree must return the same
  2889. ;; number of blank lines between elements and the same number of white
  2890. ;; space after objects, some special care must be given to white
  2891. ;; spaces.
  2892. ;;
  2893. ;; The first function, `org-element-normalize-string', ensures any
  2894. ;; string different from the empty string will end with a single
  2895. ;; newline character.
  2896. ;;
  2897. ;; The second function, `org-element-normalize-contents', removes
  2898. ;; global indentation from the contents of the current element.
  2899. (defun org-element-normalize-string (s)
  2900. "Ensure string S ends with a single newline character.
  2901. If S isn't a string return it unchanged. If S is the empty
  2902. string, return it. Otherwise, return a new string with a single
  2903. newline character at its end."
  2904. (cond
  2905. ((not (stringp s)) s)
  2906. ((string= "" s) "")
  2907. (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
  2908. (replace-match "\n" nil nil s)))))
  2909. (defun org-element-normalize-contents (element &optional ignore-first)
  2910. "Normalize plain text in ELEMENT's contents.
  2911. ELEMENT must only contain plain text and objects.
  2912. The following changes are applied to plain text:
  2913. - Remove global indentation, preserving relative one.
  2914. - Untabify it.
  2915. If optional argument IGNORE-FIRST is non-nil, ignore first line's
  2916. indentation to compute maximal common indentation.
  2917. Return the normalized element."
  2918. (nconc
  2919. (list (car element) (nth 1 element))
  2920. (let ((contents (org-element-get-contents element)))
  2921. (cond
  2922. ((and (not ignore-first) (not (stringp (car contents)))) contents)
  2923. (t
  2924. (catch 'exit
  2925. ;; 1. Remove tabs from each string in CONTENTS. Get maximal
  2926. ;; common indentation (MCI) along the way.
  2927. (let* ((ind-list (unless ignore-first
  2928. (list (org-get-string-indentation (car contents)))))
  2929. (contents
  2930. (mapcar (lambda (object)
  2931. (if (not (stringp object))
  2932. object
  2933. (let ((start 0)
  2934. (object (org-remove-tabs object)))
  2935. (while (string-match "\n\\( *\\)" object start)
  2936. (setq start (match-end 0))
  2937. (push (length (match-string 1 object))
  2938. ind-list))
  2939. object)))
  2940. contents))
  2941. (mci (if ind-list
  2942. (apply 'min ind-list)
  2943. (throw 'exit contents))))
  2944. ;; 2. Remove that indentation from CONTENTS. First string
  2945. ;; must be treated differently because it's the only one
  2946. ;; whose indentation doesn't happen after a newline
  2947. ;; character.
  2948. (let ((first-obj (car contents)))
  2949. (unless (or (not (stringp first-obj)) ignore-first)
  2950. (setq contents
  2951. (cons (replace-regexp-in-string
  2952. (format "\\` \\{%d\\}" mci) "" first-obj)
  2953. (cdr contents)))))
  2954. (mapcar (lambda (object)
  2955. (if (not (stringp object))
  2956. object
  2957. (replace-regexp-in-string
  2958. (format "\n \\{%d\\}" mci) "\n" object)))
  2959. contents))))))))
  2960. ;;; The Toolbox
  2961. ;; Once the structure of an Org file is well understood, it's easy to
  2962. ;; implement some replacements for `forward-paragraph'
  2963. ;; `backward-paragraph', namely `org-element-forward' and
  2964. ;; `org-element-backward'.
  2965. ;; Also, `org-transpose-elements' mimics the behaviour of
  2966. ;; `transpose-words', at the element's level, whereas
  2967. ;; `org-element-drag-forward', `org-element-drag-backward', and
  2968. ;; `org-element-up' generalize, respectively, functions
  2969. ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
  2970. ;; `org-element-unindent-buffer' will, as its name almost suggests,
  2971. ;; smartly remove global indentation from buffer, making it possible
  2972. ;; to use Org indent mode on a file created with hard indentation.
  2973. ;; `org-element-nested-p' and `org-element-swap-A-B' are used
  2974. ;; internally by some of the previously cited tools.
  2975. (defsubst org-element-nested-p (elem-A elem-B)
  2976. "Non-nil when elements ELEM-A and ELEM-B are nested."
  2977. (let ((beg-A (org-element-get-property :begin elem-A))
  2978. (beg-B (org-element-get-property :begin elem-B))
  2979. (end-A (org-element-get-property :end elem-A))
  2980. (end-B (org-element-get-property :end elem-B)))
  2981. (or (and (>= beg-A beg-B) (<= end-A end-B))
  2982. (and (>= beg-B beg-A) (<= end-B end-A)))))
  2983. (defun org-element-swap-A-B (elem-A elem-B)
  2984. "Swap elements ELEM-A and ELEM-B.
  2985. Leave point at the end of ELEM-A.
  2986. Assume ELEM-A is before ELEM-B and that they are not nested."
  2987. (goto-char (org-element-get-property :begin elem-A))
  2988. (let* ((beg-B (org-element-get-property :begin elem-B))
  2989. (end-B-no-blank (save-excursion
  2990. (goto-char (org-element-get-property :end elem-B))
  2991. (skip-chars-backward " \r\t\n")
  2992. (forward-line)
  2993. (point)))
  2994. (beg-A (org-element-get-property :begin elem-A))
  2995. (end-A-no-blank (save-excursion
  2996. (goto-char (org-element-get-property :end elem-A))
  2997. (skip-chars-backward " \r\t\n")
  2998. (forward-line)
  2999. (point)))
  3000. (body-A (buffer-substring beg-A end-A-no-blank))
  3001. (body-B (buffer-substring beg-B end-B-no-blank))
  3002. (between-A-B (buffer-substring end-A-no-blank beg-B)))
  3003. (delete-region beg-A end-B-no-blank)
  3004. (insert body-B between-A-B body-A)
  3005. (goto-char (org-element-get-property :end elem-B))))
  3006. (defun org-element-backward ()
  3007. "Move backward by one element."
  3008. (interactive)
  3009. (let* ((opoint (point))
  3010. (element (org-element-at-point))
  3011. (start-el-beg (org-element-get-property :begin element)))
  3012. ;; At an headline. The previous element is the previous sibling,
  3013. ;; or the parent if any.
  3014. (cond
  3015. ;; Already at the beginning of the current element: move to the
  3016. ;; beginning of the previous one.
  3017. ((= opoint start-el-beg)
  3018. (forward-line -1)
  3019. (skip-chars-backward " \r\t\n")
  3020. (let* ((prev-element (org-element-at-point))
  3021. (itemp (org-in-item-p))
  3022. (struct (and itemp
  3023. (save-excursion (goto-char itemp)
  3024. (org-list-struct)))))
  3025. ;; When moving into a new list, go directly at the
  3026. ;; beginning of the top list structure.
  3027. (if (and itemp (<= (org-list-get-bottom-point struct) opoint))
  3028. (progn
  3029. (goto-char (org-list-get-top-point struct))
  3030. (goto-char (org-element-get-property
  3031. :begin (org-element-at-point))))
  3032. (goto-char (org-element-get-property :begin prev-element))))
  3033. (while (org-truely-invisible-p) (org-element-up)))
  3034. ;; Else, move at the element beginning. One exception: if point
  3035. ;; was in the blank lines after the end of a list, move directly
  3036. ;; to the top item.
  3037. (t
  3038. (let (struct itemp)
  3039. (if (and (setq itemp (org-in-item-p))
  3040. (<= (org-list-get-bottom-point
  3041. (save-excursion (goto-char itemp)
  3042. (setq struct (org-list-struct))))
  3043. opoint))
  3044. (progn (goto-char (org-list-get-top-point struct))
  3045. (goto-char (org-element-get-property
  3046. :begin (org-element-at-point))))
  3047. (goto-char start-el-beg)))))))
  3048. (defun org-element-drag-backward ()
  3049. "Drag backward element at point."
  3050. (interactive)
  3051. (let* ((pos (point))
  3052. (elem (org-element-at-point)))
  3053. (when (= (progn (goto-char (point-min))
  3054. (org-skip-whitespace)
  3055. (point-at-bol))
  3056. (org-element-get-property :end elem))
  3057. (error "Cannot drag element backward"))
  3058. (goto-char (org-element-get-property :begin elem))
  3059. (org-element-backward)
  3060. (let ((prev-elem (org-element-at-point)))
  3061. (when (or (org-element-nested-p elem prev-elem)
  3062. (and (eq (car elem) 'headline)
  3063. (not (eq (car prev-elem) 'headline))))
  3064. (goto-char pos)
  3065. (error "Cannot drag element backward"))
  3066. ;; Compute new position of point: it's shifted by PREV-ELEM
  3067. ;; body's length.
  3068. (let ((size-prev (- (org-element-get-property :end prev-elem)
  3069. (org-element-get-property :begin prev-elem))))
  3070. (org-element-swap-A-B prev-elem elem)
  3071. (goto-char (- pos size-prev))))))
  3072. (defun org-element-drag-forward ()
  3073. "Move forward element at point."
  3074. (interactive)
  3075. (let* ((pos (point))
  3076. (elem (org-element-at-point)))
  3077. (when (= (point-max) (org-element-get-property :end elem))
  3078. (error "Cannot drag element forward"))
  3079. (goto-char (org-element-get-property :end elem))
  3080. (let ((next-elem (org-element-at-point)))
  3081. (when (or (org-element-nested-p elem next-elem)
  3082. (and (eq (car next-elem) 'headline)
  3083. (not (eq (car elem) 'headline))))
  3084. (goto-char pos)
  3085. (error "Cannot drag element forward"))
  3086. ;; Compute new position of point: it's shifted by NEXT-ELEM
  3087. ;; body's length (without final blanks) and by the length of
  3088. ;; blanks between ELEM and NEXT-ELEM.
  3089. (let ((size-next (- (save-excursion
  3090. (goto-char (org-element-get-property :end next-elem))
  3091. (skip-chars-backward " \r\t\n")
  3092. (forward-line)
  3093. (point))
  3094. (org-element-get-property :begin next-elem)))
  3095. (size-blank (- (org-element-get-property :end elem)
  3096. (save-excursion
  3097. (goto-char (org-element-get-property :end elem))
  3098. (skip-chars-backward " \r\t\n")
  3099. (forward-line)
  3100. (point)))))
  3101. (org-element-swap-A-B elem next-elem)
  3102. (goto-char (+ pos size-next size-blank))))))
  3103. (defun org-element-forward ()
  3104. "Move forward by one element."
  3105. (interactive)
  3106. (beginning-of-line)
  3107. (cond ((eobp) (error "Cannot move further down"))
  3108. ((looking-at "[ \t]*$")
  3109. (org-skip-whitespace)
  3110. (goto-char (if (eobp) (point) (point-at-bol))))
  3111. (t
  3112. (let ((element (org-element-at-point t))
  3113. (origin (point)))
  3114. (cond
  3115. ;; At an item: Either move to the next element inside, or
  3116. ;; to its end if it's hidden.
  3117. ((eq (car element) 'item)
  3118. (if (org-element-get-property :hiddenp element)
  3119. (goto-char (org-element-get-property :end element))
  3120. (end-of-line)
  3121. (re-search-forward org-element-paragraph-separate nil t)
  3122. (org-skip-whitespace)
  3123. (beginning-of-line)))
  3124. ;; At a recursive element: Either move inside, or if it's
  3125. ;; hidden, move to its end.
  3126. ((memq (car element) org-element-greater-elements)
  3127. (let ((cbeg (org-element-get-property :contents-begin element)))
  3128. (goto-char
  3129. (if (or (org-element-get-property :hiddenp element)
  3130. (> origin cbeg))
  3131. (org-element-get-property :end element)
  3132. cbeg))))
  3133. ;; Else: move to the current element's end.
  3134. (t (goto-char (org-element-get-property :end element))))))))
  3135. (defun org-element-mark-element ()
  3136. "Put point at beginning of this element, mark at end.
  3137. Interactively, if this command is repeated or (in Transient Mark
  3138. mode) if the mark is active, it marks the next element after the
  3139. ones already marked."
  3140. (interactive)
  3141. (let (deactivate-mark)
  3142. (if (or (and (eq last-command this-command) (mark t))
  3143. (and transient-mark-mode mark-active))
  3144. (set-mark
  3145. (save-excursion
  3146. (goto-char (mark))
  3147. (goto-char (org-element-get-property :end (org-element-at-point)))))
  3148. (let ((element (org-element-at-point)))
  3149. (end-of-line)
  3150. (push-mark (org-element-get-property :end element) t t)
  3151. (goto-char (org-element-get-property :begin element))))))
  3152. (defun org-narrow-to-element ()
  3153. "Narrow buffer to current element."
  3154. (interactive)
  3155. (let ((elem (org-element-at-point)))
  3156. (cond
  3157. ((eq (car elem) 'headline)
  3158. (narrow-to-region
  3159. (org-element-get-property :begin elem)
  3160. (org-element-get-property :end elem)))
  3161. ((memq (car elem) org-element-greater-elements)
  3162. (narrow-to-region
  3163. (org-element-get-property :contents-begin elem)
  3164. (org-element-get-property :contents-end elem)))
  3165. (t
  3166. (narrow-to-region
  3167. (org-element-get-property :begin elem)
  3168. (org-element-get-property :end elem))))))
  3169. (defun org-transpose-elements ()
  3170. "Transpose current and previous elements, keeping blank lines between.
  3171. Point is moved after both elements."
  3172. (interactive)
  3173. (org-skip-whitespace)
  3174. (let ((pos (point))
  3175. (cur (org-element-at-point)))
  3176. (when (= (save-excursion (goto-char (point-min))
  3177. (org-skip-whitespace)
  3178. (point-at-bol))
  3179. (org-element-get-property :begin cur))
  3180. (error "No previous element"))
  3181. (goto-char (org-element-get-property :begin cur))
  3182. (forward-line -1)
  3183. (let ((prev (org-element-at-point)))
  3184. (when (org-element-nested-p cur prev)
  3185. (goto-char pos)
  3186. (error "Cannot transpose nested elements"))
  3187. (org-element-swap-A-B prev cur))))
  3188. (defun org-element-unindent-buffer ()
  3189. "Un-indent the visible part of the buffer.
  3190. Relative indentation \(between items, inside blocks, etc.\) isn't
  3191. modified."
  3192. (interactive)
  3193. (unless (eq major-mode 'org-mode)
  3194. (error "Cannot un-indent a buffer not in Org mode"))
  3195. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  3196. unindent-tree ; For byte-compiler.
  3197. (unindent-tree
  3198. (function
  3199. (lambda (contents)
  3200. (mapc (lambda (element)
  3201. (if (eq (car element) 'headline)
  3202. (funcall unindent-tree
  3203. (org-element-get-contents element))
  3204. (save-excursion
  3205. (save-restriction
  3206. (narrow-to-region
  3207. (org-element-get-property :begin element)
  3208. (org-element-get-property :end element))
  3209. (org-do-remove-indentation)))))
  3210. (reverse contents))))))
  3211. (funcall unindent-tree (org-element-get-contents parse-tree))))
  3212. (defun org-element-up ()
  3213. "Move to upper element.
  3214. Return position at the beginning of the upper element."
  3215. (interactive)
  3216. (let ((opoint (point)) elem)
  3217. (cond
  3218. ((bobp) (error "No surrounding element"))
  3219. ((org-with-limited-levels (org-at-heading-p))
  3220. (or (org-up-heading-safe) (error "No surronding element")))
  3221. ((and (org-at-item-p)
  3222. (setq elem (org-element-at-point))
  3223. (let* ((top-list-p (zerop (org-element-get-property :level elem))))
  3224. (unless top-list-p
  3225. ;; If parent is bound to be in the same list as the
  3226. ;; original point, move to that parent.
  3227. (let ((struct (org-element-get-property :structure elem)))
  3228. (goto-char
  3229. (org-list-get-parent
  3230. (point-at-bol) struct (org-list-parents-alist struct))))))))
  3231. (t
  3232. (let* ((elem (or elem (org-element-at-point)))
  3233. (end (save-excursion
  3234. (goto-char (org-element-get-property :end elem))
  3235. (skip-chars-backward " \r\t\n")
  3236. (forward-line)
  3237. (point)))
  3238. prev-elem)
  3239. (goto-char (org-element-get-property :begin elem))
  3240. (forward-line -1)
  3241. (while (and (< (org-element-get-property
  3242. :end (setq prev-elem (org-element-at-point)))
  3243. end)
  3244. (not (bobp)))
  3245. (goto-char (org-element-get-property :begin prev-elem))
  3246. (forward-line -1))
  3247. (if (and (bobp) (< (org-element-get-property :end prev-elem) end))
  3248. (progn (goto-char opoint)
  3249. (error "No surrounding element"))
  3250. (goto-char (org-element-get-property :begin prev-elem))))))))
  3251. (provide 'org-element)
  3252. ;;; org-element.el ends here