org-element.el 142 KB

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