org-element.el 150 KB

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