org-element.el 148 KB

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