org-element.el 142 KB

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