org-element.el 135 KB

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