org-element.el 140 KB

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