org-element.el 147 KB

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