org-element.el 149 KB

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