org-element.el 151 KB

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