org-element.el 147 KB

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