org-element.el 156 KB

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