org-element.el 138 KB

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