org-element.el 130 KB

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