org-element.el 149 KB

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