org-element.el 150 KB

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