org-element.el 137 KB

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