org-element.el 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522
  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 ((case-fold-search t)
  1584. (begin (car affiliated))
  1585. (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
  1586. (upcase (org-match-string-no-properties 1))))
  1587. (value (org-trim (buffer-substring-no-properties
  1588. (match-end 0) (point-at-eol))))
  1589. (pos-before-blank (progn (forward-line) (point)))
  1590. (end (progn (skip-chars-forward " \r\t\n" limit)
  1591. (skip-chars-backward " \t")
  1592. (if (bolp) (point) (line-end-position)))))
  1593. (list 'keyword
  1594. (nconc
  1595. (list :key key
  1596. :value value
  1597. :begin begin
  1598. :end end
  1599. :post-blank (count-lines pos-before-blank end))
  1600. (cdr affiliated))))))
  1601. (defun org-element-keyword-interpreter (keyword contents)
  1602. "Interpret KEYWORD element as Org syntax.
  1603. CONTENTS is nil."
  1604. (format "#+%s: %s"
  1605. (org-element-property :key keyword)
  1606. (org-element-property :value keyword)))
  1607. ;;;; Latex Environment
  1608. (defun org-element-latex-environment-parser (limit affiliated)
  1609. "Parse a LaTeX environment.
  1610. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1611. the buffer position at the beginning of the first affiliated
  1612. keyword and CDR is a plist of affiliated keywords along with
  1613. their value.
  1614. Return a list whose CAR is `latex-environment' and CDR is a plist
  1615. containing `:begin', `:end', `:value' and `:post-blank'
  1616. keywords.
  1617. Assume point is at the beginning of the latex environment."
  1618. (save-excursion
  1619. (let* ((case-fold-search t)
  1620. (code-begin (point))
  1621. (begin (car affiliated))
  1622. (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
  1623. (regexp-quote (match-string 1))))
  1624. (code-end
  1625. (progn (re-search-forward
  1626. (format "^[ \t]*\\\\end{%s}[ \t]*$" env) limit t)
  1627. (forward-line)
  1628. (point)))
  1629. (value (buffer-substring-no-properties code-begin code-end))
  1630. (end (progn (skip-chars-forward " \r\t\n" limit)
  1631. (skip-chars-backward " \t")
  1632. (if (bolp) (point) (line-end-position)))))
  1633. (list 'latex-environment
  1634. (nconc
  1635. (list :begin begin
  1636. :end end
  1637. :value value
  1638. :post-blank (count-lines code-end end))
  1639. (cdr affiliated))))))
  1640. (defun org-element-latex-environment-interpreter (latex-environment contents)
  1641. "Interpret LATEX-ENVIRONMENT element as Org syntax.
  1642. CONTENTS is nil."
  1643. (org-element-property :value latex-environment))
  1644. ;;;; Node Property
  1645. (defun org-element-node-property-parser (limit)
  1646. "Parse a node-property at point.
  1647. LIMIT bounds the search.
  1648. Return a list whose CAR is `node-property' and CDR is a plist
  1649. containing `:key', `:value', `:begin', `:end' and `:post-blank'
  1650. keywords."
  1651. (save-excursion
  1652. (let ((case-fold-search t)
  1653. (begin (point))
  1654. (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
  1655. (org-match-string-no-properties 1)))
  1656. (value (org-match-string-no-properties 2))
  1657. (pos-before-blank (progn (forward-line) (point)))
  1658. (end (progn (skip-chars-forward " \r\t\n" limit)
  1659. (if (eobp) (point) (point-at-bol)))))
  1660. (list 'node-property
  1661. (list :key key
  1662. :value value
  1663. :begin begin
  1664. :end end
  1665. :post-blank (count-lines pos-before-blank end))))))
  1666. (defun org-element-node-property-interpreter (node-property contents)
  1667. "Interpret NODE-PROPERTY element as Org syntax.
  1668. CONTENTS is nil."
  1669. (format org-property-format
  1670. (format ":%s:" (org-element-property :key node-property))
  1671. (org-element-property :value node-property)))
  1672. ;;;; Paragraph
  1673. (defun org-element-paragraph-parser (limit affiliated)
  1674. "Parse a paragraph.
  1675. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1676. the buffer position at the beginning of the first affiliated
  1677. keyword and CDR is a plist of affiliated keywords along with
  1678. their value.
  1679. Return a list whose CAR is `paragraph' and CDR is a plist
  1680. containing `:begin', `:end', `:contents-begin' and
  1681. `:contents-end' and `:post-blank' keywords.
  1682. Assume point is at the beginning of the paragraph."
  1683. (save-excursion
  1684. (let* ((begin (car affiliated))
  1685. (contents-begin (point))
  1686. (before-blank
  1687. (let ((case-fold-search t))
  1688. (end-of-line)
  1689. (if (not (re-search-forward
  1690. org-element-paragraph-separate limit 'm))
  1691. limit
  1692. ;; A matching `org-element-paragraph-separate' is not
  1693. ;; necessarily the end of the paragraph. In
  1694. ;; particular, lines starting with # or : as a first
  1695. ;; non-space character are ambiguous. We have check
  1696. ;; if they are valid Org syntax (i.e. not an
  1697. ;; incomplete keyword).
  1698. (beginning-of-line)
  1699. (while (not
  1700. (or
  1701. ;; There's no ambiguity for other symbols or
  1702. ;; empty lines: stop here.
  1703. (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
  1704. ;; Stop at valid fixed-width areas.
  1705. (looking-at "[ \t]*:\\(?: \\|$\\)")
  1706. ;; Stop at drawers.
  1707. (and (looking-at org-drawer-regexp)
  1708. (save-excursion
  1709. (re-search-forward
  1710. "^[ \t]*:END:[ \t]*$" limit t)))
  1711. ;; Stop at valid comments.
  1712. (looking-at "[ \t]*#\\(?: \\|$\\)")
  1713. ;; Stop at valid dynamic blocks.
  1714. (and (looking-at org-dblock-start-re)
  1715. (save-excursion
  1716. (re-search-forward
  1717. "^[ \t]*#\\+END:?[ \t]*$" limit t)))
  1718. ;; Stop at valid blocks.
  1719. (and (looking-at
  1720. "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1721. (save-excursion
  1722. (re-search-forward
  1723. (format "^[ \t]*#\\+END_%s[ \t]*$"
  1724. (match-string 1))
  1725. limit t)))
  1726. ;; Stop at valid latex environments.
  1727. (and (looking-at
  1728. "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
  1729. (save-excursion
  1730. (re-search-forward
  1731. (format "^[ \t]*\\\\end{%s}[ \t]*$"
  1732. (match-string 1))
  1733. limit t)))
  1734. ;; Stop at valid keywords.
  1735. (looking-at "[ \t]*#\\+\\S-+:")
  1736. ;; Skip everything else.
  1737. (not
  1738. (progn
  1739. (end-of-line)
  1740. (re-search-forward org-element-paragraph-separate
  1741. limit 'm)))))
  1742. (beginning-of-line)))
  1743. (if (= (point) limit) limit
  1744. (goto-char (line-beginning-position)))))
  1745. (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
  1746. (forward-line)
  1747. (point)))
  1748. (end (progn (skip-chars-forward " \r\t\n" limit)
  1749. (skip-chars-backward " \t")
  1750. (if (bolp) (point) (line-end-position)))))
  1751. (list 'paragraph
  1752. (nconc
  1753. (list :begin begin
  1754. :end end
  1755. :contents-begin contents-begin
  1756. :contents-end contents-end
  1757. :post-blank (count-lines before-blank end))
  1758. (cdr affiliated))))))
  1759. (defun org-element-paragraph-interpreter (paragraph contents)
  1760. "Interpret PARAGRAPH element as Org syntax.
  1761. CONTENTS is the contents of the element."
  1762. contents)
  1763. ;;;; Planning
  1764. (defun org-element-planning-parser (limit)
  1765. "Parse a planning.
  1766. LIMIT bounds the search.
  1767. Return a list whose CAR is `planning' and CDR is a plist
  1768. containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
  1769. and `:post-blank' keywords."
  1770. (save-excursion
  1771. (let* ((case-fold-search nil)
  1772. (begin (point))
  1773. (post-blank (let ((before-blank (progn (forward-line) (point))))
  1774. (skip-chars-forward " \r\t\n" limit)
  1775. (skip-chars-backward " \t")
  1776. (unless (bolp) (end-of-line))
  1777. (count-lines before-blank (point))))
  1778. (end (point))
  1779. closed deadline scheduled)
  1780. (goto-char begin)
  1781. (while (re-search-forward org-keyword-time-not-clock-regexp
  1782. (line-end-position) t)
  1783. (goto-char (match-end 1))
  1784. (org-skip-whitespace)
  1785. (let ((time (buffer-substring-no-properties
  1786. (1+ (point)) (1- (match-end 0))))
  1787. (keyword (match-string 1)))
  1788. (cond ((equal keyword org-closed-string) (setq closed time))
  1789. ((equal keyword org-deadline-string) (setq deadline time))
  1790. (t (setq scheduled time)))))
  1791. (list 'planning
  1792. (list :closed closed
  1793. :deadline deadline
  1794. :scheduled scheduled
  1795. :begin begin
  1796. :end end
  1797. :post-blank post-blank)))))
  1798. (defun org-element-planning-interpreter (planning contents)
  1799. "Interpret PLANNING element as Org syntax.
  1800. CONTENTS is nil."
  1801. (mapconcat
  1802. 'identity
  1803. (delq nil
  1804. (list (let ((closed (org-element-property :closed planning)))
  1805. (when closed (concat org-closed-string " [" closed "]")))
  1806. (let ((deadline (org-element-property :deadline planning)))
  1807. (when deadline (concat org-deadline-string " <" deadline ">")))
  1808. (let ((scheduled (org-element-property :scheduled planning)))
  1809. (when scheduled
  1810. (concat org-scheduled-string " <" scheduled ">")))))
  1811. " "))
  1812. ;;;; Quote Section
  1813. (defun org-element-quote-section-parser (limit)
  1814. "Parse a quote section.
  1815. LIMIT bounds the search.
  1816. Return a list whose CAR is `quote-section' and CDR is a plist
  1817. containing `:begin', `:end', `:value' and `:post-blank' keywords.
  1818. Assume point is at beginning of the section."
  1819. (save-excursion
  1820. (let* ((begin (point))
  1821. (end (progn (org-with-limited-levels (outline-next-heading))
  1822. (point)))
  1823. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1824. (forward-line)
  1825. (point)))
  1826. (value (buffer-substring-no-properties begin pos-before-blank)))
  1827. (list 'quote-section
  1828. (list :begin begin
  1829. :end end
  1830. :value value
  1831. :post-blank (count-lines pos-before-blank end))))))
  1832. (defun org-element-quote-section-interpreter (quote-section contents)
  1833. "Interpret QUOTE-SECTION element as Org syntax.
  1834. CONTENTS is nil."
  1835. (org-element-property :value quote-section))
  1836. ;;;; Src Block
  1837. (defun org-element-src-block-parser (limit affiliated)
  1838. "Parse a src block.
  1839. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1840. the buffer position at the beginning of the first affiliated
  1841. keyword and CDR is a plist of affiliated keywords along with
  1842. their value.
  1843. Return a list whose CAR is `src-block' and CDR is a plist
  1844. containing `:language', `:switches', `:parameters', `:begin',
  1845. `:end', `:hiddenp', `:number-lines', `:retain-labels',
  1846. `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
  1847. `:post-blank' keywords.
  1848. Assume point is at the beginning of the block."
  1849. (let ((case-fold-search t))
  1850. (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
  1851. limit t)))
  1852. ;; Incomplete block: parse it as a paragraph.
  1853. (org-element-paragraph-parser limit affiliated)
  1854. (let ((contents-end (match-beginning 0)))
  1855. (save-excursion
  1856. (let* ((begin (car affiliated))
  1857. ;; Get language as a string.
  1858. (language
  1859. (progn
  1860. (looking-at
  1861. (concat "^[ \t]*#\\+BEGIN_SRC"
  1862. "\\(?: +\\(\\S-+\\)\\)?"
  1863. "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
  1864. "\\(.*\\)[ \t]*$"))
  1865. (org-match-string-no-properties 1)))
  1866. ;; Get switches.
  1867. (switches (org-match-string-no-properties 2))
  1868. ;; Get parameters.
  1869. (parameters (org-match-string-no-properties 3))
  1870. ;; Switches analysis
  1871. (number-lines (cond ((not switches) nil)
  1872. ((string-match "-n\\>" switches) 'new)
  1873. ((string-match "+n\\>" switches) 'continued)))
  1874. (preserve-indent (and switches (string-match "-i\\>" switches)))
  1875. (label-fmt (and switches
  1876. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1877. (match-string 1 switches)))
  1878. ;; Should labels be retained in (or stripped from)
  1879. ;; src blocks?
  1880. (retain-labels
  1881. (or (not switches)
  1882. (not (string-match "-r\\>" switches))
  1883. (and number-lines (string-match "-k\\>" switches))))
  1884. ;; What should code-references use - labels or
  1885. ;; line-numbers?
  1886. (use-labels
  1887. (or (not switches)
  1888. (and retain-labels (not (string-match "-k\\>" switches)))))
  1889. ;; Get visibility status.
  1890. (hidden (progn (forward-line) (org-invisible-p2)))
  1891. ;; Retrieve code.
  1892. (value (org-unescape-code-in-string
  1893. (buffer-substring-no-properties (point) contents-end)))
  1894. (pos-before-blank (progn (goto-char contents-end)
  1895. (forward-line)
  1896. (point)))
  1897. ;; Get position after ending blank lines.
  1898. (end (progn (skip-chars-forward " \r\t\n" limit)
  1899. (skip-chars-backward " \t")
  1900. (if (bolp) (point) (line-end-position)))))
  1901. (list 'src-block
  1902. (nconc
  1903. (list :language language
  1904. :switches (and (org-string-nw-p switches)
  1905. (org-trim switches))
  1906. :parameters (and (org-string-nw-p parameters)
  1907. (org-trim parameters))
  1908. :begin begin
  1909. :end end
  1910. :number-lines number-lines
  1911. :preserve-indent preserve-indent
  1912. :retain-labels retain-labels
  1913. :use-labels use-labels
  1914. :label-fmt label-fmt
  1915. :hiddenp hidden
  1916. :value value
  1917. :post-blank (count-lines pos-before-blank end))
  1918. (cdr affiliated)))))))))
  1919. (defun org-element-src-block-interpreter (src-block contents)
  1920. "Interpret SRC-BLOCK element as Org syntax.
  1921. CONTENTS is nil."
  1922. (let ((lang (org-element-property :language src-block))
  1923. (switches (org-element-property :switches src-block))
  1924. (params (org-element-property :parameters src-block))
  1925. (value (let ((val (org-element-property :value src-block)))
  1926. (cond
  1927. (org-src-preserve-indentation val)
  1928. ((zerop org-edit-src-content-indentation)
  1929. (org-remove-indentation val))
  1930. (t
  1931. (let ((ind (make-string
  1932. org-edit-src-content-indentation 32)))
  1933. (replace-regexp-in-string
  1934. "\\(^\\)[ \t]*\\S-" ind
  1935. (org-remove-indentation val) nil nil 1)))))))
  1936. (concat (format "#+BEGIN_SRC%s\n"
  1937. (concat (and lang (concat " " lang))
  1938. (and switches (concat " " switches))
  1939. (and params (concat " " params))))
  1940. (org-escape-code-in-string value)
  1941. "#+END_SRC")))
  1942. ;;;; Table
  1943. (defun org-element-table-parser (limit affiliated)
  1944. "Parse a table at point.
  1945. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1946. the buffer position at the beginning of the first affiliated
  1947. keyword and CDR is a plist of affiliated keywords along with
  1948. their value.
  1949. Return a list whose CAR is `table' and CDR is a plist containing
  1950. `:begin', `:end', `:tblfm', `:type', `:contents-begin',
  1951. `:contents-end', `:value' and `:post-blank' keywords.
  1952. Assume point is at the beginning of the table."
  1953. (save-excursion
  1954. (let* ((case-fold-search t)
  1955. (table-begin (point))
  1956. (type (if (org-at-table.el-p) 'table.el 'org))
  1957. (begin (car affiliated))
  1958. (table-end
  1959. (if (re-search-forward org-table-any-border-regexp limit 'm)
  1960. (goto-char (match-beginning 0))
  1961. (point)))
  1962. (tblfm (let (acc)
  1963. (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
  1964. (push (org-match-string-no-properties 1) acc)
  1965. (forward-line))
  1966. acc))
  1967. (pos-before-blank (point))
  1968. (end (progn (skip-chars-forward " \r\t\n" limit)
  1969. (skip-chars-backward " \t")
  1970. (if (bolp) (point) (line-end-position)))))
  1971. (list 'table
  1972. (nconc
  1973. (list :begin begin
  1974. :end end
  1975. :type type
  1976. :tblfm tblfm
  1977. ;; Only `org' tables have contents. `table.el' tables
  1978. ;; use a `:value' property to store raw table as
  1979. ;; a string.
  1980. :contents-begin (and (eq type 'org) table-begin)
  1981. :contents-end (and (eq type 'org) table-end)
  1982. :value (and (eq type 'table.el)
  1983. (buffer-substring-no-properties
  1984. table-begin table-end))
  1985. :post-blank (count-lines pos-before-blank end))
  1986. (cdr affiliated))))))
  1987. (defun org-element-table-interpreter (table contents)
  1988. "Interpret TABLE element as Org syntax.
  1989. CONTENTS is nil."
  1990. (if (eq (org-element-property :type table) 'table.el)
  1991. (org-remove-indentation (org-element-property :value table))
  1992. (concat (with-temp-buffer (insert contents)
  1993. (org-table-align)
  1994. (buffer-string))
  1995. (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
  1996. (reverse (org-element-property :tblfm table))
  1997. "\n"))))
  1998. ;;;; Table Row
  1999. (defun org-element-table-row-parser (limit)
  2000. "Parse table row at point.
  2001. LIMIT bounds the search.
  2002. Return a list whose CAR is `table-row' and CDR is a plist
  2003. containing `:begin', `:end', `:contents-begin', `:contents-end',
  2004. `:type' and `:post-blank' keywords."
  2005. (save-excursion
  2006. (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
  2007. (begin (point))
  2008. ;; A table rule has no contents. In that case, ensure
  2009. ;; CONTENTS-BEGIN matches CONTENTS-END.
  2010. (contents-begin (and (eq type 'standard)
  2011. (search-forward "|")
  2012. (point)))
  2013. (contents-end (and (eq type 'standard)
  2014. (progn
  2015. (end-of-line)
  2016. (skip-chars-backward " \t")
  2017. (point))))
  2018. (end (progn (forward-line) (point))))
  2019. (list 'table-row
  2020. (list :type type
  2021. :begin begin
  2022. :end end
  2023. :contents-begin contents-begin
  2024. :contents-end contents-end
  2025. :post-blank 0)))))
  2026. (defun org-element-table-row-interpreter (table-row contents)
  2027. "Interpret TABLE-ROW element as Org syntax.
  2028. CONTENTS is the contents of the table row."
  2029. (if (eq (org-element-property :type table-row) 'rule) "|-"
  2030. (concat "| " contents)))
  2031. ;;;; Verse Block
  2032. (defun org-element-verse-block-parser (limit affiliated)
  2033. "Parse a verse block.
  2034. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2035. the buffer position at the beginning of the first affiliated
  2036. keyword and CDR is a plist of affiliated keywords along with
  2037. their value.
  2038. Return a list whose CAR is `verse-block' and CDR is a plist
  2039. containing `:begin', `:end', `:contents-begin', `:contents-end',
  2040. `:hiddenp' and `:post-blank' keywords.
  2041. Assume point is at beginning of the block."
  2042. (let ((case-fold-search t))
  2043. (if (not (save-excursion
  2044. (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
  2045. ;; Incomplete block: parse it as a paragraph.
  2046. (org-element-paragraph-parser limit affiliated)
  2047. (let ((contents-end (match-beginning 0)))
  2048. (save-excursion
  2049. (let* ((begin (car affiliated))
  2050. (hidden (progn (forward-line) (org-invisible-p2)))
  2051. (contents-begin (point))
  2052. (pos-before-blank (progn (goto-char contents-end)
  2053. (forward-line)
  2054. (point)))
  2055. (end (progn (skip-chars-forward " \r\t\n" limit)
  2056. (skip-chars-backward " \t")
  2057. (if (bolp) (point) (line-end-position)))))
  2058. (list 'verse-block
  2059. (nconc
  2060. (list :begin begin
  2061. :end end
  2062. :contents-begin contents-begin
  2063. :contents-end contents-end
  2064. :hiddenp hidden
  2065. :post-blank (count-lines pos-before-blank end))
  2066. (cdr affiliated)))))))))
  2067. (defun org-element-verse-block-interpreter (verse-block contents)
  2068. "Interpret VERSE-BLOCK element as Org syntax.
  2069. CONTENTS is verse block contents."
  2070. (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
  2071. ;;; Objects
  2072. ;;
  2073. ;; Unlike to elements, interstices can be found between objects.
  2074. ;; That's why, along with the parser, successor functions are provided
  2075. ;; for each object. Some objects share the same successor (i.e. `code'
  2076. ;; and `verbatim' objects).
  2077. ;;
  2078. ;; A successor must accept a single argument bounding the search. It
  2079. ;; will return either a cons cell whose CAR is the object's type, as
  2080. ;; a symbol, and CDR the position of its next occurrence, or nil.
  2081. ;;
  2082. ;; Successors follow the naming convention:
  2083. ;; org-element-NAME-successor, where NAME is the name of the
  2084. ;; successor, as defined in `org-element-all-successors'.
  2085. ;;
  2086. ;; Some object types (i.e. `italic') are recursive. Restrictions on
  2087. ;; object types they can contain will be specified in
  2088. ;; `org-element-object-restrictions'.
  2089. ;;
  2090. ;; Adding a new type of object is simple. Implement a successor,
  2091. ;; a parser, and an interpreter for it, all following the naming
  2092. ;; convention. Register type in `org-element-all-objects' and
  2093. ;; successor in `org-element-all-successors'. Maybe tweak
  2094. ;; restrictions about it, and that's it.
  2095. ;;;; Bold
  2096. (defun org-element-bold-parser ()
  2097. "Parse bold object at point.
  2098. Return a list whose CAR is `bold' and CDR is a plist with
  2099. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2100. `:post-blank' keywords.
  2101. Assume point is at the first star marker."
  2102. (save-excursion
  2103. (unless (bolp) (backward-char 1))
  2104. (looking-at org-emph-re)
  2105. (let ((begin (match-beginning 2))
  2106. (contents-begin (match-beginning 4))
  2107. (contents-end (match-end 4))
  2108. (post-blank (progn (goto-char (match-end 2))
  2109. (skip-chars-forward " \t")))
  2110. (end (point)))
  2111. (list 'bold
  2112. (list :begin begin
  2113. :end end
  2114. :contents-begin contents-begin
  2115. :contents-end contents-end
  2116. :post-blank post-blank)))))
  2117. (defun org-element-bold-interpreter (bold contents)
  2118. "Interpret BOLD object as Org syntax.
  2119. CONTENTS is the contents of the object."
  2120. (format "*%s*" contents))
  2121. (defun org-element-text-markup-successor (limit)
  2122. "Search for the next text-markup object.
  2123. LIMIT bounds the search.
  2124. Return value is a cons cell whose CAR is a symbol among `bold',
  2125. `italic', `underline', `strike-through', `code' and `verbatim'
  2126. and CDR is beginning position."
  2127. (save-excursion
  2128. (unless (bolp) (backward-char))
  2129. (when (re-search-forward org-emph-re limit t)
  2130. (let ((marker (match-string 3)))
  2131. (cons (cond
  2132. ((equal marker "*") 'bold)
  2133. ((equal marker "/") 'italic)
  2134. ((equal marker "_") 'underline)
  2135. ((equal marker "+") 'strike-through)
  2136. ((equal marker "~") 'code)
  2137. ((equal marker "=") 'verbatim)
  2138. (t (error "Unknown marker at %d" (match-beginning 3))))
  2139. (match-beginning 2))))))
  2140. ;;;; Code
  2141. (defun org-element-code-parser ()
  2142. "Parse code object at point.
  2143. Return a list whose CAR is `code' and CDR is a plist with
  2144. `:value', `:begin', `:end' and `:post-blank' keywords.
  2145. Assume point is at the first tilde marker."
  2146. (save-excursion
  2147. (unless (bolp) (backward-char 1))
  2148. (looking-at org-emph-re)
  2149. (let ((begin (match-beginning 2))
  2150. (value (org-match-string-no-properties 4))
  2151. (post-blank (progn (goto-char (match-end 2))
  2152. (skip-chars-forward " \t")))
  2153. (end (point)))
  2154. (list 'code
  2155. (list :value value
  2156. :begin begin
  2157. :end end
  2158. :post-blank post-blank)))))
  2159. (defun org-element-code-interpreter (code contents)
  2160. "Interpret CODE object as Org syntax.
  2161. CONTENTS is nil."
  2162. (format "~%s~" (org-element-property :value code)))
  2163. ;;;; Entity
  2164. (defun org-element-entity-parser ()
  2165. "Parse entity at point.
  2166. Return a list whose CAR is `entity' and CDR a plist with
  2167. `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
  2168. `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
  2169. keywords.
  2170. Assume point is at the beginning of the entity."
  2171. (save-excursion
  2172. (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
  2173. (let* ((value (org-entity-get (match-string 1)))
  2174. (begin (match-beginning 0))
  2175. (bracketsp (string= (match-string 2) "{}"))
  2176. (post-blank (progn (goto-char (match-end 1))
  2177. (when bracketsp (forward-char 2))
  2178. (skip-chars-forward " \t")))
  2179. (end (point)))
  2180. (list 'entity
  2181. (list :name (car value)
  2182. :latex (nth 1 value)
  2183. :latex-math-p (nth 2 value)
  2184. :html (nth 3 value)
  2185. :ascii (nth 4 value)
  2186. :latin1 (nth 5 value)
  2187. :utf-8 (nth 6 value)
  2188. :begin begin
  2189. :end end
  2190. :use-brackets-p bracketsp
  2191. :post-blank post-blank)))))
  2192. (defun org-element-entity-interpreter (entity contents)
  2193. "Interpret ENTITY object as Org syntax.
  2194. CONTENTS is nil."
  2195. (concat "\\"
  2196. (org-element-property :name entity)
  2197. (when (org-element-property :use-brackets-p entity) "{}")))
  2198. (defun org-element-latex-or-entity-successor (limit)
  2199. "Search for the next latex-fragment or entity object.
  2200. LIMIT bounds the search.
  2201. Return value is a cons cell whose CAR is `entity' or
  2202. `latex-fragment' and CDR is beginning position."
  2203. (save-excursion
  2204. (unless (bolp) (backward-char))
  2205. (let ((matchers
  2206. (remove "begin" (plist-get org-format-latex-options :matchers)))
  2207. ;; ENTITY-RE matches both LaTeX commands and Org entities.
  2208. (entity-re
  2209. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
  2210. (when (re-search-forward
  2211. (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
  2212. matchers "\\|")
  2213. "\\|" entity-re)
  2214. limit t)
  2215. (goto-char (match-beginning 0))
  2216. (if (looking-at entity-re)
  2217. ;; Determine if it's a real entity or a LaTeX command.
  2218. (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
  2219. (match-beginning 0))
  2220. ;; No entity nor command: point is at a LaTeX fragment.
  2221. ;; Determine its type to get the correct beginning position.
  2222. (cons 'latex-fragment
  2223. (catch 'return
  2224. (mapc (lambda (e)
  2225. (when (looking-at (nth 1 (assoc e org-latex-regexps)))
  2226. (throw 'return
  2227. (match-beginning
  2228. (nth 2 (assoc e org-latex-regexps))))))
  2229. matchers)
  2230. (point))))))))
  2231. ;;;; Export Snippet
  2232. (defun org-element-export-snippet-parser ()
  2233. "Parse export snippet at point.
  2234. Return a list whose CAR is `export-snippet' and CDR a plist with
  2235. `:begin', `:end', `:back-end', `:value' and `:post-blank' as
  2236. keywords.
  2237. Assume point is at the beginning of the snippet."
  2238. (save-excursion
  2239. (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
  2240. (let* ((begin (match-beginning 0))
  2241. (back-end (org-match-string-no-properties 1))
  2242. (value (buffer-substring-no-properties
  2243. (point)
  2244. (progn (re-search-forward "@@" nil t) (match-beginning 0))))
  2245. (post-blank (skip-chars-forward " \t"))
  2246. (end (point)))
  2247. (list 'export-snippet
  2248. (list :back-end back-end
  2249. :value value
  2250. :begin begin
  2251. :end end
  2252. :post-blank post-blank)))))
  2253. (defun org-element-export-snippet-interpreter (export-snippet contents)
  2254. "Interpret EXPORT-SNIPPET object as Org syntax.
  2255. CONTENTS is nil."
  2256. (format "@@%s:%s@@"
  2257. (org-element-property :back-end export-snippet)
  2258. (org-element-property :value export-snippet)))
  2259. (defun org-element-export-snippet-successor (limit)
  2260. "Search for the next export-snippet object.
  2261. LIMIT bounds the search.
  2262. Return value is a cons cell whose CAR is `export-snippet' and CDR
  2263. its beginning position."
  2264. (save-excursion
  2265. (let (beg)
  2266. (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
  2267. (setq beg (match-beginning 0))
  2268. (search-forward "@@" limit t))
  2269. (cons 'export-snippet beg)))))
  2270. ;;;; Footnote Reference
  2271. (defun org-element-footnote-reference-parser ()
  2272. "Parse footnote reference at point.
  2273. Return a list whose CAR is `footnote-reference' and CDR a plist
  2274. with `:label', `:type', `:inline-definition', `:begin', `:end'
  2275. and `:post-blank' as keywords."
  2276. (save-excursion
  2277. (looking-at org-footnote-re)
  2278. (let* ((begin (point))
  2279. (label (or (org-match-string-no-properties 2)
  2280. (org-match-string-no-properties 3)
  2281. (and (match-string 1)
  2282. (concat "fn:" (org-match-string-no-properties 1)))))
  2283. (type (if (or (not label) (match-string 1)) 'inline 'standard))
  2284. (inner-begin (match-end 0))
  2285. (inner-end
  2286. (let ((count 1))
  2287. (forward-char)
  2288. (while (and (> count 0) (re-search-forward "[][]" nil t))
  2289. (if (equal (match-string 0) "[") (incf count) (decf count)))
  2290. (1- (point))))
  2291. (post-blank (progn (goto-char (1+ inner-end))
  2292. (skip-chars-forward " \t")))
  2293. (end (point))
  2294. (footnote-reference
  2295. (list 'footnote-reference
  2296. (list :label label
  2297. :type type
  2298. :begin begin
  2299. :end end
  2300. :post-blank post-blank))))
  2301. (org-element-put-property
  2302. footnote-reference :inline-definition
  2303. (and (eq type 'inline)
  2304. (org-element-parse-secondary-string
  2305. (buffer-substring inner-begin inner-end)
  2306. (org-element-restriction 'footnote-reference)
  2307. footnote-reference))))))
  2308. (defun org-element-footnote-reference-interpreter (footnote-reference contents)
  2309. "Interpret FOOTNOTE-REFERENCE object as Org syntax.
  2310. CONTENTS is nil."
  2311. (let ((label (or (org-element-property :label footnote-reference) "fn:"))
  2312. (def
  2313. (let ((inline-def
  2314. (org-element-property :inline-definition footnote-reference)))
  2315. (if (not inline-def) ""
  2316. (concat ":" (org-element-interpret-data inline-def))))))
  2317. (format "[%s]" (concat label def))))
  2318. (defun org-element-footnote-reference-successor (limit)
  2319. "Search for the next footnote-reference object.
  2320. LIMIT bounds the search.
  2321. Return value is a cons cell whose CAR is `footnote-reference' and
  2322. CDR is beginning position."
  2323. (save-excursion
  2324. (catch 'exit
  2325. (while (re-search-forward org-footnote-re limit t)
  2326. (save-excursion
  2327. (let ((beg (match-beginning 0))
  2328. (count 1))
  2329. (backward-char)
  2330. (while (re-search-forward "[][]" limit t)
  2331. (if (equal (match-string 0) "[") (incf count) (decf count))
  2332. (when (zerop count)
  2333. (throw 'exit (cons 'footnote-reference beg))))))))))
  2334. ;;;; Inline Babel Call
  2335. (defun org-element-inline-babel-call-parser ()
  2336. "Parse inline babel call at point.
  2337. Return a list whose CAR is `inline-babel-call' and CDR a plist
  2338. with `:begin', `:end', `:info' and `:post-blank' as keywords.
  2339. Assume point is at the beginning of the babel call."
  2340. (save-excursion
  2341. (unless (bolp) (backward-char))
  2342. (looking-at org-babel-inline-lob-one-liner-regexp)
  2343. (let ((info (save-match-data (org-babel-lob-get-info)))
  2344. (begin (match-end 1))
  2345. (post-blank (progn (goto-char (match-end 0))
  2346. (skip-chars-forward " \t")))
  2347. (end (point)))
  2348. (list 'inline-babel-call
  2349. (list :begin begin
  2350. :end end
  2351. :info info
  2352. :post-blank post-blank)))))
  2353. (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
  2354. "Interpret INLINE-BABEL-CALL object as Org syntax.
  2355. CONTENTS is nil."
  2356. (let* ((babel-info (org-element-property :info inline-babel-call))
  2357. (main-source (car babel-info))
  2358. (post-options (nth 1 babel-info)))
  2359. (concat "call_"
  2360. (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
  2361. ;; Remove redundant square brackets.
  2362. (replace-match
  2363. (match-string 1 main-source) nil nil main-source)
  2364. main-source)
  2365. (and post-options (format "[%s]" post-options)))))
  2366. (defun org-element-inline-babel-call-successor (limit)
  2367. "Search for the next inline-babel-call object.
  2368. LIMIT bounds the search.
  2369. Return value is a cons cell whose CAR is `inline-babel-call' and
  2370. CDR is beginning position."
  2371. (save-excursion
  2372. ;; Use a simplified version of
  2373. ;; `org-babel-inline-lob-one-liner-regexp'.
  2374. (when (re-search-forward
  2375. "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
  2376. limit t)
  2377. (cons 'inline-babel-call (match-beginning 0)))))
  2378. ;;;; Inline Src Block
  2379. (defun org-element-inline-src-block-parser ()
  2380. "Parse inline source block at point.
  2381. LIMIT bounds the search.
  2382. Return a list whose CAR is `inline-src-block' and CDR a plist
  2383. with `:begin', `:end', `:language', `:value', `:parameters' and
  2384. `:post-blank' as keywords.
  2385. Assume point is at the beginning of the inline src block."
  2386. (save-excursion
  2387. (unless (bolp) (backward-char))
  2388. (looking-at org-babel-inline-src-block-regexp)
  2389. (let ((begin (match-beginning 1))
  2390. (language (org-match-string-no-properties 2))
  2391. (parameters (org-match-string-no-properties 4))
  2392. (value (org-match-string-no-properties 5))
  2393. (post-blank (progn (goto-char (match-end 0))
  2394. (skip-chars-forward " \t")))
  2395. (end (point)))
  2396. (list 'inline-src-block
  2397. (list :language language
  2398. :value value
  2399. :parameters parameters
  2400. :begin begin
  2401. :end end
  2402. :post-blank post-blank)))))
  2403. (defun org-element-inline-src-block-interpreter (inline-src-block contents)
  2404. "Interpret INLINE-SRC-BLOCK object as Org syntax.
  2405. CONTENTS is nil."
  2406. (let ((language (org-element-property :language inline-src-block))
  2407. (arguments (org-element-property :parameters inline-src-block))
  2408. (body (org-element-property :value inline-src-block)))
  2409. (format "src_%s%s{%s}"
  2410. language
  2411. (if arguments (format "[%s]" arguments) "")
  2412. body)))
  2413. (defun org-element-inline-src-block-successor (limit)
  2414. "Search for the next inline-babel-call element.
  2415. LIMIT bounds the search.
  2416. Return value is a cons cell whose CAR is `inline-babel-call' and
  2417. CDR is beginning position."
  2418. (save-excursion
  2419. (unless (bolp) (backward-char))
  2420. (when (re-search-forward org-babel-inline-src-block-regexp limit t)
  2421. (cons 'inline-src-block (match-beginning 1)))))
  2422. ;;;; Italic
  2423. (defun org-element-italic-parser ()
  2424. "Parse italic object at point.
  2425. Return a list whose CAR is `italic' and CDR is a plist with
  2426. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2427. `:post-blank' keywords.
  2428. Assume point is at the first slash marker."
  2429. (save-excursion
  2430. (unless (bolp) (backward-char 1))
  2431. (looking-at org-emph-re)
  2432. (let ((begin (match-beginning 2))
  2433. (contents-begin (match-beginning 4))
  2434. (contents-end (match-end 4))
  2435. (post-blank (progn (goto-char (match-end 2))
  2436. (skip-chars-forward " \t")))
  2437. (end (point)))
  2438. (list 'italic
  2439. (list :begin begin
  2440. :end end
  2441. :contents-begin contents-begin
  2442. :contents-end contents-end
  2443. :post-blank post-blank)))))
  2444. (defun org-element-italic-interpreter (italic contents)
  2445. "Interpret ITALIC object as Org syntax.
  2446. CONTENTS is the contents of the object."
  2447. (format "/%s/" contents))
  2448. ;;;; Latex Fragment
  2449. (defun org-element-latex-fragment-parser ()
  2450. "Parse latex fragment at point.
  2451. Return a list whose CAR is `latex-fragment' and CDR a plist with
  2452. `:value', `:begin', `:end', and `:post-blank' as keywords.
  2453. Assume point is at the beginning of the latex fragment."
  2454. (save-excursion
  2455. (let* ((begin (point))
  2456. (substring-match
  2457. (catch 'exit
  2458. (mapc (lambda (e)
  2459. (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
  2460. (when (or (looking-at latex-regexp)
  2461. (and (not (bobp))
  2462. (save-excursion
  2463. (backward-char)
  2464. (looking-at latex-regexp))))
  2465. (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
  2466. (plist-get org-format-latex-options :matchers))
  2467. ;; None found: it's a macro.
  2468. (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
  2469. 0))
  2470. (value (match-string-no-properties substring-match))
  2471. (post-blank (progn (goto-char (match-end substring-match))
  2472. (skip-chars-forward " \t")))
  2473. (end (point)))
  2474. (list 'latex-fragment
  2475. (list :value value
  2476. :begin begin
  2477. :end end
  2478. :post-blank post-blank)))))
  2479. (defun org-element-latex-fragment-interpreter (latex-fragment contents)
  2480. "Interpret LATEX-FRAGMENT object as Org syntax.
  2481. CONTENTS is nil."
  2482. (org-element-property :value latex-fragment))
  2483. ;;;; Line Break
  2484. (defun org-element-line-break-parser ()
  2485. "Parse line break at point.
  2486. Return a list whose CAR is `line-break', and CDR a plist with
  2487. `:begin', `:end' and `:post-blank' keywords.
  2488. Assume point is at the beginning of the line break."
  2489. (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
  2490. (defun org-element-line-break-interpreter (line-break contents)
  2491. "Interpret LINE-BREAK object as Org syntax.
  2492. CONTENTS is nil."
  2493. "\\\\")
  2494. (defun org-element-line-break-successor (limit)
  2495. "Search for the next line-break object.
  2496. LIMIT bounds the search.
  2497. Return value is a cons cell whose CAR is `line-break' and CDR is
  2498. beginning position."
  2499. (save-excursion
  2500. (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
  2501. (goto-char (match-beginning 1)))))
  2502. ;; A line break can only happen on a non-empty line.
  2503. (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
  2504. (cons 'line-break beg)))))
  2505. ;;;; Link
  2506. (defun org-element-link-parser ()
  2507. "Parse link at point.
  2508. Return a list whose CAR is `link' and CDR a plist with `:type',
  2509. `:path', `:raw-link', `:application', `:search-option', `:begin',
  2510. `:end', `:contents-begin', `:contents-end' and `:post-blank' as
  2511. keywords.
  2512. Assume point is at the beginning of the link."
  2513. (save-excursion
  2514. (let ((begin (point))
  2515. end contents-begin contents-end link-end post-blank path type
  2516. raw-link link search-option application)
  2517. (cond
  2518. ;; Type 1: Text targeted from a radio target.
  2519. ((and org-target-link-regexp (looking-at org-target-link-regexp))
  2520. (setq type "radio"
  2521. link-end (match-end 0)
  2522. path (org-match-string-no-properties 0)))
  2523. ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
  2524. ((looking-at org-bracket-link-regexp)
  2525. (setq contents-begin (match-beginning 3)
  2526. contents-end (match-end 3)
  2527. link-end (match-end 0)
  2528. ;; RAW-LINK is the original link.
  2529. raw-link (org-match-string-no-properties 1)
  2530. link (org-translate-link
  2531. (org-link-expand-abbrev
  2532. (org-link-unescape raw-link))))
  2533. ;; Determine TYPE of link and set PATH accordingly.
  2534. (cond
  2535. ;; File type.
  2536. ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
  2537. (setq type "file" path link))
  2538. ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
  2539. ((string-match org-link-re-with-space3 link)
  2540. (setq type (match-string 1 link) path (match-string 2 link)))
  2541. ;; Id type: PATH is the id.
  2542. ((string-match "^id:\\([-a-f0-9]+\\)" link)
  2543. (setq type "id" path (match-string 1 link)))
  2544. ;; Code-ref type: PATH is the name of the reference.
  2545. ((string-match "^(\\(.*\\))$" link)
  2546. (setq type "coderef" path (match-string 1 link)))
  2547. ;; Custom-id type: PATH is the name of the custom id.
  2548. ((= (aref link 0) ?#)
  2549. (setq type "custom-id" path (substring link 1)))
  2550. ;; Fuzzy type: Internal link either matches a target, an
  2551. ;; headline name or nothing. PATH is the target or
  2552. ;; headline's name.
  2553. (t (setq type "fuzzy" path link))))
  2554. ;; Type 3: Plain link, i.e. http://orgmode.org
  2555. ((looking-at org-plain-link-re)
  2556. (setq raw-link (org-match-string-no-properties 0)
  2557. type (org-match-string-no-properties 1)
  2558. path (org-match-string-no-properties 2)
  2559. link-end (match-end 0)))
  2560. ;; Type 4: Angular link, i.e. <http://orgmode.org>
  2561. ((looking-at org-angle-link-re)
  2562. (setq raw-link (buffer-substring-no-properties
  2563. (match-beginning 1) (match-end 2))
  2564. type (org-match-string-no-properties 1)
  2565. path (org-match-string-no-properties 2)
  2566. link-end (match-end 0))))
  2567. ;; In any case, deduce end point after trailing white space from
  2568. ;; LINK-END variable.
  2569. (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
  2570. end (point))
  2571. ;; Extract search option and opening application out of
  2572. ;; "file"-type links.
  2573. (when (member type org-element-link-type-is-file)
  2574. ;; Application.
  2575. (cond ((string-match "^file\\+\\(.*\\)$" type)
  2576. (setq application (match-string 1 type)))
  2577. ((not (string-match "^file" type))
  2578. (setq application type)))
  2579. ;; Extract search option from PATH.
  2580. (when (string-match "::\\(.*\\)$" path)
  2581. (setq search-option (match-string 1 path)
  2582. path (replace-match "" nil nil path)))
  2583. ;; Make sure TYPE always report "file".
  2584. (setq type "file"))
  2585. (list 'link
  2586. (list :type type
  2587. :path path
  2588. :raw-link (or raw-link path)
  2589. :application application
  2590. :search-option search-option
  2591. :begin begin
  2592. :end end
  2593. :contents-begin contents-begin
  2594. :contents-end contents-end
  2595. :post-blank post-blank)))))
  2596. (defun org-element-link-interpreter (link contents)
  2597. "Interpret LINK object as Org syntax.
  2598. CONTENTS is the contents of the object, or nil."
  2599. (let ((type (org-element-property :type link))
  2600. (raw-link (org-element-property :raw-link link)))
  2601. (if (string= type "radio") raw-link
  2602. (format "[[%s]%s]"
  2603. raw-link
  2604. (if contents (format "[%s]" contents) "")))))
  2605. (defun org-element-link-successor (limit)
  2606. "Search for the next link object.
  2607. LIMIT bounds the search.
  2608. Return value is a cons cell whose CAR is `link' and CDR is
  2609. beginning position."
  2610. (save-excursion
  2611. (let ((link-regexp
  2612. (if (not org-target-link-regexp) org-any-link-re
  2613. (concat org-any-link-re "\\|" org-target-link-regexp))))
  2614. (when (re-search-forward link-regexp limit t)
  2615. (cons 'link (match-beginning 0))))))
  2616. ;;;; Macro
  2617. (defun org-element-macro-parser ()
  2618. "Parse macro at point.
  2619. Return a list whose CAR is `macro' and CDR a plist with `:key',
  2620. `:args', `:begin', `:end', `:value' and `:post-blank' as
  2621. keywords.
  2622. Assume point is at the macro."
  2623. (save-excursion
  2624. (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
  2625. (let ((begin (point))
  2626. (key (downcase (org-match-string-no-properties 1)))
  2627. (value (org-match-string-no-properties 0))
  2628. (post-blank (progn (goto-char (match-end 0))
  2629. (skip-chars-forward " \t")))
  2630. (end (point))
  2631. (args (let ((args (org-match-string-no-properties 3)) args2)
  2632. (when args
  2633. (setq args (org-split-string args ","))
  2634. (while args
  2635. (while (string-match "\\\\\\'" (car args))
  2636. ;; Repair bad splits.
  2637. (setcar (cdr args) (concat (substring (car args) 0 -1)
  2638. "," (nth 1 args)))
  2639. (pop args))
  2640. (push (pop args) args2))
  2641. (mapcar 'org-trim (nreverse args2))))))
  2642. (list 'macro
  2643. (list :key key
  2644. :value value
  2645. :args args
  2646. :begin begin
  2647. :end end
  2648. :post-blank post-blank)))))
  2649. (defun org-element-macro-interpreter (macro contents)
  2650. "Interpret MACRO object as Org syntax.
  2651. CONTENTS is nil."
  2652. (org-element-property :value macro))
  2653. (defun org-element-macro-successor (limit)
  2654. "Search for the next macro object.
  2655. LIMIT bounds the search.
  2656. Return value is cons cell whose CAR is `macro' and CDR is
  2657. beginning position."
  2658. (save-excursion
  2659. (when (re-search-forward
  2660. "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
  2661. limit t)
  2662. (cons 'macro (match-beginning 0)))))
  2663. ;;;; Radio-target
  2664. (defun org-element-radio-target-parser ()
  2665. "Parse radio target at point.
  2666. Return a list whose CAR is `radio-target' and CDR a plist with
  2667. `:begin', `:end', `:contents-begin', `:contents-end', `:value'
  2668. and `:post-blank' as keywords.
  2669. Assume point is at the radio target."
  2670. (save-excursion
  2671. (looking-at org-radio-target-regexp)
  2672. (let ((begin (point))
  2673. (contents-begin (match-beginning 1))
  2674. (contents-end (match-end 1))
  2675. (value (org-match-string-no-properties 1))
  2676. (post-blank (progn (goto-char (match-end 0))
  2677. (skip-chars-forward " \t")))
  2678. (end (point)))
  2679. (list 'radio-target
  2680. (list :begin begin
  2681. :end end
  2682. :contents-begin contents-begin
  2683. :contents-end contents-end
  2684. :post-blank post-blank
  2685. :value value)))))
  2686. (defun org-element-radio-target-interpreter (target contents)
  2687. "Interpret TARGET object as Org syntax.
  2688. CONTENTS is the contents of the object."
  2689. (concat "<<<" contents ">>>"))
  2690. (defun org-element-radio-target-successor (limit)
  2691. "Search for the next radio-target object.
  2692. LIMIT bounds the search.
  2693. Return value is a cons cell whose CAR is `radio-target' and CDR
  2694. is beginning position."
  2695. (save-excursion
  2696. (when (re-search-forward org-radio-target-regexp limit t)
  2697. (cons 'radio-target (match-beginning 0)))))
  2698. ;;;; Statistics Cookie
  2699. (defun org-element-statistics-cookie-parser ()
  2700. "Parse statistics cookie at point.
  2701. Return a list whose CAR is `statistics-cookie', and CDR a plist
  2702. with `:begin', `:end', `:value' and `:post-blank' keywords.
  2703. Assume point is at the beginning of the statistics-cookie."
  2704. (save-excursion
  2705. (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
  2706. (let* ((begin (point))
  2707. (value (buffer-substring-no-properties
  2708. (match-beginning 0) (match-end 0)))
  2709. (post-blank (progn (goto-char (match-end 0))
  2710. (skip-chars-forward " \t")))
  2711. (end (point)))
  2712. (list 'statistics-cookie
  2713. (list :begin begin
  2714. :end end
  2715. :value value
  2716. :post-blank post-blank)))))
  2717. (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
  2718. "Interpret STATISTICS-COOKIE object as Org syntax.
  2719. CONTENTS is nil."
  2720. (org-element-property :value statistics-cookie))
  2721. (defun org-element-statistics-cookie-successor (limit)
  2722. "Search for the next statistics cookie object.
  2723. LIMIT bounds the search.
  2724. Return value is a cons cell whose CAR is `statistics-cookie' and
  2725. CDR is beginning position."
  2726. (save-excursion
  2727. (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
  2728. (cons 'statistics-cookie (match-beginning 0)))))
  2729. ;;;; Strike-Through
  2730. (defun org-element-strike-through-parser ()
  2731. "Parse strike-through object at point.
  2732. Return a list whose CAR is `strike-through' and CDR is a plist
  2733. with `:begin', `:end', `:contents-begin' and `:contents-end' and
  2734. `:post-blank' keywords.
  2735. Assume point is at the first plus sign marker."
  2736. (save-excursion
  2737. (unless (bolp) (backward-char 1))
  2738. (looking-at org-emph-re)
  2739. (let ((begin (match-beginning 2))
  2740. (contents-begin (match-beginning 4))
  2741. (contents-end (match-end 4))
  2742. (post-blank (progn (goto-char (match-end 2))
  2743. (skip-chars-forward " \t")))
  2744. (end (point)))
  2745. (list 'strike-through
  2746. (list :begin begin
  2747. :end end
  2748. :contents-begin contents-begin
  2749. :contents-end contents-end
  2750. :post-blank post-blank)))))
  2751. (defun org-element-strike-through-interpreter (strike-through contents)
  2752. "Interpret STRIKE-THROUGH object as Org syntax.
  2753. CONTENTS is the contents of the object."
  2754. (format "+%s+" contents))
  2755. ;;;; Subscript
  2756. (defun org-element-subscript-parser ()
  2757. "Parse subscript at point.
  2758. Return a list whose CAR is `subscript' and CDR a plist with
  2759. `:begin', `:end', `:contents-begin', `:contents-end',
  2760. `:use-brackets-p' and `:post-blank' as keywords.
  2761. Assume point is at the underscore."
  2762. (save-excursion
  2763. (unless (bolp) (backward-char))
  2764. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
  2765. t
  2766. (not (looking-at org-match-substring-regexp))))
  2767. (begin (match-beginning 2))
  2768. (contents-begin (or (match-beginning 5)
  2769. (match-beginning 3)))
  2770. (contents-end (or (match-end 5) (match-end 3)))
  2771. (post-blank (progn (goto-char (match-end 0))
  2772. (skip-chars-forward " \t")))
  2773. (end (point)))
  2774. (list 'subscript
  2775. (list :begin begin
  2776. :end end
  2777. :use-brackets-p bracketsp
  2778. :contents-begin contents-begin
  2779. :contents-end contents-end
  2780. :post-blank post-blank)))))
  2781. (defun org-element-subscript-interpreter (subscript contents)
  2782. "Interpret SUBSCRIPT object as Org syntax.
  2783. CONTENTS is the contents of the object."
  2784. (format
  2785. (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
  2786. contents))
  2787. (defun org-element-sub/superscript-successor (limit)
  2788. "Search for the next sub/superscript object.
  2789. LIMIT bounds the search.
  2790. Return value is a cons cell whose CAR is either `subscript' or
  2791. `superscript' and CDR is beginning position."
  2792. (save-excursion
  2793. (unless (bolp) (backward-char))
  2794. (when (re-search-forward org-match-substring-regexp limit t)
  2795. (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
  2796. (match-beginning 2)))))
  2797. ;;;; Superscript
  2798. (defun org-element-superscript-parser ()
  2799. "Parse superscript at point.
  2800. Return a list whose CAR is `superscript' and CDR a plist with
  2801. `:begin', `:end', `:contents-begin', `:contents-end',
  2802. `:use-brackets-p' and `:post-blank' as keywords.
  2803. Assume point is at the caret."
  2804. (save-excursion
  2805. (unless (bolp) (backward-char))
  2806. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
  2807. (not (looking-at org-match-substring-regexp))))
  2808. (begin (match-beginning 2))
  2809. (contents-begin (or (match-beginning 5)
  2810. (match-beginning 3)))
  2811. (contents-end (or (match-end 5) (match-end 3)))
  2812. (post-blank (progn (goto-char (match-end 0))
  2813. (skip-chars-forward " \t")))
  2814. (end (point)))
  2815. (list 'superscript
  2816. (list :begin begin
  2817. :end end
  2818. :use-brackets-p bracketsp
  2819. :contents-begin contents-begin
  2820. :contents-end contents-end
  2821. :post-blank post-blank)))))
  2822. (defun org-element-superscript-interpreter (superscript contents)
  2823. "Interpret SUPERSCRIPT object as Org syntax.
  2824. CONTENTS is the contents of the object."
  2825. (format
  2826. (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
  2827. contents))
  2828. ;;;; Table Cell
  2829. (defun org-element-table-cell-parser ()
  2830. "Parse table cell at point.
  2831. Return a list whose CAR is `table-cell' and CDR is a plist
  2832. containing `:begin', `:end', `:contents-begin', `:contents-end'
  2833. and `:post-blank' keywords."
  2834. (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
  2835. (let* ((begin (match-beginning 0))
  2836. (end (match-end 0))
  2837. (contents-begin (match-beginning 1))
  2838. (contents-end (match-end 1)))
  2839. (list 'table-cell
  2840. (list :begin begin
  2841. :end end
  2842. :contents-begin contents-begin
  2843. :contents-end contents-end
  2844. :post-blank 0))))
  2845. (defun org-element-table-cell-interpreter (table-cell contents)
  2846. "Interpret TABLE-CELL element as Org syntax.
  2847. CONTENTS is the contents of the cell, or nil."
  2848. (concat " " contents " |"))
  2849. (defun org-element-table-cell-successor (limit)
  2850. "Search for the next table-cell object.
  2851. LIMIT bounds the search.
  2852. Return value is a cons cell whose CAR is `table-cell' and CDR is
  2853. beginning position."
  2854. (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
  2855. ;;;; Target
  2856. (defun org-element-target-parser ()
  2857. "Parse target at point.
  2858. Return a list whose CAR is `target' and CDR a plist with
  2859. `:begin', `:end', `:value' and `:post-blank' as keywords.
  2860. Assume point is at the target."
  2861. (save-excursion
  2862. (looking-at org-target-regexp)
  2863. (let ((begin (point))
  2864. (value (org-match-string-no-properties 1))
  2865. (post-blank (progn (goto-char (match-end 0))
  2866. (skip-chars-forward " \t")))
  2867. (end (point)))
  2868. (list 'target
  2869. (list :begin begin
  2870. :end end
  2871. :value value
  2872. :post-blank post-blank)))))
  2873. (defun org-element-target-interpreter (target contents)
  2874. "Interpret TARGET object as Org syntax.
  2875. CONTENTS is nil."
  2876. (format "<<%s>>" (org-element-property :value target)))
  2877. (defun org-element-target-successor (limit)
  2878. "Search for the next target object.
  2879. LIMIT bounds the search.
  2880. Return value is a cons cell whose CAR is `target' and CDR is
  2881. beginning position."
  2882. (save-excursion
  2883. (when (re-search-forward org-target-regexp limit t)
  2884. (cons 'target (match-beginning 0)))))
  2885. ;;;; Timestamp
  2886. (defun org-element-timestamp-parser ()
  2887. "Parse time stamp at point.
  2888. Return a list whose CAR is `timestamp', and CDR a plist with
  2889. `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
  2890. Assume point is at the beginning of the timestamp."
  2891. (save-excursion
  2892. (let* ((begin (point))
  2893. (activep (eq (char-after) ?<))
  2894. (main-value
  2895. (progn
  2896. (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
  2897. (match-string-no-properties 1)))
  2898. (range-end (match-string-no-properties 3))
  2899. (type (cond ((match-string 2) 'diary)
  2900. ((and activep range-end) 'active-range)
  2901. (activep 'active)
  2902. (range-end 'inactive-range)
  2903. (t 'inactive)))
  2904. (post-blank (progn (goto-char (match-end 0))
  2905. (skip-chars-forward " \t")))
  2906. (end (point)))
  2907. (list 'timestamp
  2908. (list :type type
  2909. :value main-value
  2910. :range-end range-end
  2911. :begin begin
  2912. :end end
  2913. :post-blank post-blank)))))
  2914. (defun org-element-timestamp-interpreter (timestamp contents)
  2915. "Interpret TIMESTAMP object as Org syntax.
  2916. CONTENTS is nil."
  2917. (let ((type (org-element-property :type timestamp) ))
  2918. (concat
  2919. (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
  2920. (org-element-property :value timestamp))
  2921. (let ((range-end (org-element-property :range-end timestamp)))
  2922. (when range-end
  2923. (concat "--"
  2924. (format (if (eq type 'inactive-range) "[%s]" "<%s>")
  2925. range-end)))))))
  2926. (defun org-element-timestamp-successor (limit)
  2927. "Search for the next timestamp object.
  2928. LIMIT bounds the search.
  2929. Return value is a cons cell whose CAR is `timestamp' and CDR is
  2930. beginning position."
  2931. (save-excursion
  2932. (when (re-search-forward
  2933. (concat org-ts-regexp-both
  2934. "\\|"
  2935. "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  2936. "\\|"
  2937. "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
  2938. limit t)
  2939. (cons 'timestamp (match-beginning 0)))))
  2940. ;;;; Underline
  2941. (defun org-element-underline-parser ()
  2942. "Parse underline object at point.
  2943. Return a list whose CAR is `underline' and CDR is a plist with
  2944. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2945. `:post-blank' keywords.
  2946. Assume point is at the first underscore marker."
  2947. (save-excursion
  2948. (unless (bolp) (backward-char 1))
  2949. (looking-at org-emph-re)
  2950. (let ((begin (match-beginning 2))
  2951. (contents-begin (match-beginning 4))
  2952. (contents-end (match-end 4))
  2953. (post-blank (progn (goto-char (match-end 2))
  2954. (skip-chars-forward " \t")))
  2955. (end (point)))
  2956. (list 'underline
  2957. (list :begin begin
  2958. :end end
  2959. :contents-begin contents-begin
  2960. :contents-end contents-end
  2961. :post-blank post-blank)))))
  2962. (defun org-element-underline-interpreter (underline contents)
  2963. "Interpret UNDERLINE object as Org syntax.
  2964. CONTENTS is the contents of the object."
  2965. (format "_%s_" contents))
  2966. ;;;; Verbatim
  2967. (defun org-element-verbatim-parser ()
  2968. "Parse verbatim object at point.
  2969. Return a list whose CAR is `verbatim' and CDR is a plist with
  2970. `:value', `:begin', `:end' and `:post-blank' keywords.
  2971. Assume point is at the first equal sign marker."
  2972. (save-excursion
  2973. (unless (bolp) (backward-char 1))
  2974. (looking-at org-emph-re)
  2975. (let ((begin (match-beginning 2))
  2976. (value (org-match-string-no-properties 4))
  2977. (post-blank (progn (goto-char (match-end 2))
  2978. (skip-chars-forward " \t")))
  2979. (end (point)))
  2980. (list 'verbatim
  2981. (list :value value
  2982. :begin begin
  2983. :end end
  2984. :post-blank post-blank)))))
  2985. (defun org-element-verbatim-interpreter (verbatim contents)
  2986. "Interpret VERBATIM object as Org syntax.
  2987. CONTENTS is nil."
  2988. (format "=%s=" (org-element-property :value verbatim)))
  2989. ;;; Parsing Element Starting At Point
  2990. ;;
  2991. ;; `org-element--current-element' is the core function of this section.
  2992. ;; It returns the Lisp representation of the element starting at
  2993. ;; point.
  2994. ;;
  2995. ;; `org-element--current-element' makes use of special modes. They
  2996. ;; are activated for fixed element chaining (i.e. `plain-list' >
  2997. ;; `item') or fixed conditional element chaining (i.e. `headline' >
  2998. ;; `section'). Special modes are: `first-section', `item',
  2999. ;; `node-property', `quote-section', `section' and `table-row'.
  3000. (defun org-element--current-element
  3001. (limit &optional granularity special structure)
  3002. "Parse the element starting at point.
  3003. LIMIT bounds the search.
  3004. Return value is a list like (TYPE PROPS) where TYPE is the type
  3005. of the element and PROPS a plist of properties associated to the
  3006. element.
  3007. Possible types are defined in `org-element-all-elements'.
  3008. Optional argument GRANULARITY determines the depth of the
  3009. recursion. Allowed values are `headline', `greater-element',
  3010. `element', `object' or nil. When it is broader than `object' (or
  3011. nil), secondary values will not be parsed, since they only
  3012. contain objects.
  3013. Optional argument SPECIAL, when non-nil, can be either
  3014. `first-section', `item', `node-property', `quote-section',
  3015. `section', and `table-row'.
  3016. If STRUCTURE isn't provided but SPECIAL is set to `item', it will
  3017. be computed.
  3018. This function assumes point is always at the beginning of the
  3019. element it has to parse."
  3020. (save-excursion
  3021. (let ((case-fold-search t)
  3022. ;; Determine if parsing depth allows for secondary strings
  3023. ;; parsing. It only applies to elements referenced in
  3024. ;; `org-element-secondary-value-alist'.
  3025. (raw-secondary-p (and granularity (not (eq granularity 'object)))))
  3026. (cond
  3027. ;; Item.
  3028. ((eq special 'item)
  3029. (org-element-item-parser limit structure raw-secondary-p))
  3030. ;; Table Row.
  3031. ((eq special 'table-row) (org-element-table-row-parser limit))
  3032. ;; Node Property.
  3033. ((eq special 'node-property) (org-element-node-property-parser limit))
  3034. ;; Headline.
  3035. ((org-with-limited-levels (org-at-heading-p))
  3036. (org-element-headline-parser limit raw-secondary-p))
  3037. ;; Sections (must be checked after headline).
  3038. ((eq special 'section) (org-element-section-parser limit))
  3039. ((eq special 'quote-section) (org-element-quote-section-parser limit))
  3040. ((eq special 'first-section)
  3041. (org-element-section-parser
  3042. (or (save-excursion (org-with-limited-levels (outline-next-heading)))
  3043. limit)))
  3044. ;; When not at bol, point is at the beginning of an item or
  3045. ;; a footnote definition: next item is always a paragraph.
  3046. ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
  3047. ;; Planning and Clock.
  3048. ((and (looking-at org-planning-or-clock-line-re))
  3049. (if (equal (match-string 1) org-clock-string)
  3050. (org-element-clock-parser limit)
  3051. (org-element-planning-parser limit)))
  3052. ;; Inlinetask.
  3053. ((org-at-heading-p)
  3054. (org-element-inlinetask-parser limit raw-secondary-p))
  3055. ;; From there, elements can have affiliated keywords.
  3056. (t (let ((affiliated (org-element--collect-affiliated-keywords)))
  3057. (cond
  3058. ;; LaTeX Environment.
  3059. ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
  3060. (if (save-excursion
  3061. (re-search-forward
  3062. (format "^[ \t]*\\\\end{%s}[ \t]*$"
  3063. (regexp-quote (match-string 1)))
  3064. nil t))
  3065. (org-element-latex-environment-parser limit affiliated)
  3066. (org-element-paragraph-parser limit affiliated)))
  3067. ;; Drawer and Property Drawer.
  3068. ((looking-at org-drawer-regexp)
  3069. (let ((name (match-string 1)))
  3070. (cond
  3071. ((not (save-excursion
  3072. (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
  3073. (org-element-paragraph-parser limit affiliated))
  3074. ((equal "PROPERTIES" name)
  3075. (org-element-property-drawer-parser limit affiliated))
  3076. (t (org-element-drawer-parser limit affiliated)))))
  3077. ;; Fixed Width
  3078. ((looking-at "[ \t]*:\\( \\|$\\)")
  3079. (org-element-fixed-width-parser limit affiliated))
  3080. ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
  3081. ;; Keywords.
  3082. ((looking-at "[ \t]*#")
  3083. (goto-char (match-end 0))
  3084. (cond ((looking-at "\\(?: \\|$\\)")
  3085. (beginning-of-line)
  3086. (org-element-comment-parser limit affiliated))
  3087. ((looking-at "\\+BEGIN_\\(\\S-+\\)")
  3088. (beginning-of-line)
  3089. (let ((parser (assoc (upcase (match-string 1))
  3090. org-element-block-name-alist)))
  3091. (if parser (funcall (cdr parser) limit affiliated)
  3092. (org-element-special-block-parser limit affiliated))))
  3093. ((looking-at "\\+CALL:")
  3094. (beginning-of-line)
  3095. (org-element-babel-call-parser limit affiliated))
  3096. ((looking-at "\\+BEGIN:? ")
  3097. (beginning-of-line)
  3098. (org-element-dynamic-block-parser limit affiliated))
  3099. ((looking-at "\\+\\S-+:")
  3100. (beginning-of-line)
  3101. (org-element-keyword-parser limit affiliated))
  3102. (t
  3103. (beginning-of-line)
  3104. (org-element-paragraph-parser limit affiliated))))
  3105. ;; Footnote Definition.
  3106. ((looking-at org-footnote-definition-re)
  3107. (org-element-footnote-definition-parser limit affiliated))
  3108. ;; Horizontal Rule.
  3109. ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
  3110. (org-element-horizontal-rule-parser limit affiliated))
  3111. ;; Table.
  3112. ((org-at-table-p t) (org-element-table-parser limit affiliated))
  3113. ;; List.
  3114. ((looking-at (org-item-re))
  3115. (org-element-plain-list-parser
  3116. limit affiliated (or structure (org-list-struct))))
  3117. ;; Default element: Paragraph.
  3118. (t (org-element-paragraph-parser limit affiliated)))))))))
  3119. ;; Most elements can have affiliated keywords. When looking for an
  3120. ;; element beginning, we want to move before them, as they belong to
  3121. ;; that element, and, in the meantime, collect information they give
  3122. ;; into appropriate properties. Hence the following function.
  3123. (defun org-element--collect-affiliated-keywords ()
  3124. "Collect affiliated keywords from point.
  3125. Return a list whose CAR is the position at the first of them and
  3126. CDR a plist of keywords and values and move point to the
  3127. beginning of the first line after them.
  3128. As a special case, if element doesn't start at the beginning of
  3129. the line (i.e. a paragraph starting an item), CAR is current
  3130. position of point and CDR is nil."
  3131. (if (not (bolp)) (list (point))
  3132. (let ((case-fold-search t)
  3133. (origin (point))
  3134. ;; RESTRICT is the list of objects allowed in parsed
  3135. ;; keywords value.
  3136. (restrict (org-element-restriction 'keyword))
  3137. output)
  3138. (while (and (not (eobp)) (looking-at org-element--affiliated-re))
  3139. (let* ((raw-kwd (upcase (match-string 1)))
  3140. ;; Apply translation to RAW-KWD. From there, KWD is
  3141. ;; the official keyword.
  3142. (kwd (or (cdr (assoc raw-kwd
  3143. org-element-keyword-translation-alist))
  3144. raw-kwd))
  3145. ;; Find main value for any keyword.
  3146. (value
  3147. (save-match-data
  3148. (org-trim
  3149. (buffer-substring-no-properties
  3150. (match-end 0) (point-at-eol)))))
  3151. ;; PARSEDP is non-nil when keyword should have its
  3152. ;; value parsed.
  3153. (parsedp (member kwd org-element-parsed-keywords))
  3154. ;; If KWD is a dual keyword, find its secondary
  3155. ;; value. Maybe parse it.
  3156. (dualp (member kwd org-element-dual-keywords))
  3157. (dual-value
  3158. (and dualp
  3159. (let ((sec (org-match-string-no-properties 2)))
  3160. (if (or (not sec) (not parsedp)) sec
  3161. (org-element-parse-secondary-string sec restrict)))))
  3162. ;; Attribute a property name to KWD.
  3163. (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
  3164. ;; Now set final shape for VALUE.
  3165. (when parsedp
  3166. (setq value (org-element-parse-secondary-string value restrict)))
  3167. (when dualp (setq value (and value (cons value dual-value))))
  3168. (when (or (member kwd org-element-multiple-keywords)
  3169. ;; Attributes can always appear on multiple lines.
  3170. (string-match "^ATTR_" kwd))
  3171. (setq value (cons value (plist-get output kwd-sym))))
  3172. ;; Eventually store the new value in OUTPUT.
  3173. (setq output (plist-put output kwd-sym value))
  3174. ;; Move to next keyword.
  3175. (forward-line)))
  3176. ;; If affiliated keywords are orphaned: move back to first one.
  3177. ;; They will be parsed as a paragraph.
  3178. (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
  3179. ;; Return value.
  3180. (cons origin output))))
  3181. ;;; The Org Parser
  3182. ;;
  3183. ;; The two major functions here are `org-element-parse-buffer', which
  3184. ;; parses Org syntax inside the current buffer, taking into account
  3185. ;; region, narrowing, or even visibility if specified, and
  3186. ;; `org-element-parse-secondary-string', which parses objects within
  3187. ;; a given string.
  3188. ;;
  3189. ;; The (almost) almighty `org-element-map' allows to apply a function
  3190. ;; on elements or objects matching some type, and accumulate the
  3191. ;; resulting values. In an export situation, it also skips unneeded
  3192. ;; parts of the parse tree.
  3193. (defun org-element-parse-buffer (&optional granularity visible-only)
  3194. "Recursively parse the buffer and return structure.
  3195. If narrowing is in effect, only parse the visible part of the
  3196. buffer.
  3197. Optional argument GRANULARITY determines the depth of the
  3198. recursion. It can be set to the following symbols:
  3199. `headline' Only parse headlines.
  3200. `greater-element' Don't recurse into greater elements excepted
  3201. headlines and sections. Thus, elements
  3202. parsed are the top-level ones.
  3203. `element' Parse everything but objects and plain text.
  3204. `object' Parse the complete buffer (default).
  3205. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3206. elements.
  3207. Assume buffer is in Org mode."
  3208. (save-excursion
  3209. (goto-char (point-min))
  3210. (org-skip-whitespace)
  3211. (org-element--parse-elements
  3212. (point-at-bol) (point-max)
  3213. ;; Start in `first-section' mode so text before the first
  3214. ;; headline belongs to a section.
  3215. 'first-section nil granularity visible-only (list 'org-data nil))))
  3216. (defun org-element-parse-secondary-string (string restriction &optional parent)
  3217. "Recursively parse objects in STRING and return structure.
  3218. RESTRICTION is a symbol limiting the object types that will be
  3219. looked after.
  3220. Optional argument PARENT, when non-nil, is the element or object
  3221. containing the secondary string. It is used to set correctly
  3222. `:parent' property within the string."
  3223. (with-temp-buffer
  3224. (insert string)
  3225. (let ((secondary (org-element--parse-objects
  3226. (point-min) (point-max) nil restriction)))
  3227. (mapc (lambda (obj) (org-element-put-property obj :parent parent))
  3228. secondary))))
  3229. (defun org-element-map
  3230. (data types fun &optional info first-match no-recursion with-affiliated)
  3231. "Map a function on selected elements or objects.
  3232. DATA is the parsed tree, as returned by, i.e,
  3233. `org-element-parse-buffer'. TYPES is a symbol or list of symbols
  3234. of elements or objects types. FUN is the function called on the
  3235. matching element or object. It must accept one arguments: the
  3236. element or object itself.
  3237. When optional argument INFO is non-nil, it should be a plist
  3238. holding export options. In that case, parts of the parse tree
  3239. not exportable according to that property list will be skipped.
  3240. When optional argument FIRST-MATCH is non-nil, stop at the first
  3241. match for which FUN doesn't return nil, and return that value.
  3242. Optional argument NO-RECURSION is a symbol or a list of symbols
  3243. representing elements or objects types. `org-element-map' won't
  3244. enter any recursive element or object whose type belongs to that
  3245. list. Though, FUN can still be applied on them.
  3246. When optional argument WITH-AFFILIATED is non-nil, also move into
  3247. affiliated keywords to find objects.
  3248. Nil values returned from FUN do not appear in the results."
  3249. ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
  3250. (unless (listp types) (setq types (list types)))
  3251. (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
  3252. ;; Recursion depth is determined by --CATEGORY.
  3253. (let* ((--category
  3254. (catch 'found
  3255. (let ((category 'greater-elements))
  3256. (mapc (lambda (type)
  3257. (cond ((or (memq type org-element-all-objects)
  3258. (eq type 'plain-text))
  3259. ;; If one object is found, the function
  3260. ;; has to recurse into every object.
  3261. (throw 'found 'objects))
  3262. ((not (memq type org-element-greater-elements))
  3263. ;; If one regular element is found, the
  3264. ;; function has to recurse, at least,
  3265. ;; into every element it encounters.
  3266. (and (not (eq category 'elements))
  3267. (setq category 'elements)))))
  3268. types)
  3269. category)))
  3270. ;; Compute properties for affiliated keywords if necessary.
  3271. (--affiliated-alist
  3272. (and with-affiliated
  3273. (mapcar (lambda (kwd)
  3274. (cons kwd (intern (concat ":" (downcase kwd)))))
  3275. org-element-affiliated-keywords)))
  3276. --acc
  3277. --walk-tree
  3278. (--walk-tree
  3279. (function
  3280. (lambda (--data)
  3281. ;; Recursively walk DATA. INFO, if non-nil, is a plist
  3282. ;; holding contextual information.
  3283. (let ((--type (org-element-type --data)))
  3284. (cond
  3285. ((not --data))
  3286. ;; Ignored element in an export context.
  3287. ((and info (memq --data (plist-get info :ignore-list))))
  3288. ;; Secondary string: only objects can be found there.
  3289. ((not --type)
  3290. (when (eq --category 'objects) (mapc --walk-tree --data)))
  3291. ;; Unconditionally enter parse trees.
  3292. ((eq --type 'org-data)
  3293. (mapc --walk-tree (org-element-contents --data)))
  3294. (t
  3295. ;; Check if TYPE is matching among TYPES. If so,
  3296. ;; apply FUN to --DATA and accumulate return value
  3297. ;; into --ACC (or exit if FIRST-MATCH is non-nil).
  3298. (when (memq --type types)
  3299. (let ((result (funcall fun --data)))
  3300. (cond ((not result))
  3301. (first-match (throw '--map-first-match result))
  3302. (t (push result --acc)))))
  3303. ;; If --DATA has a secondary string that can contain
  3304. ;; objects with their type among TYPES, look into it.
  3305. (when (and (eq --category 'objects) (not (stringp --data)))
  3306. (let ((sec-prop
  3307. (assq --type org-element-secondary-value-alist)))
  3308. (when sec-prop
  3309. (funcall --walk-tree
  3310. (org-element-property (cdr sec-prop) --data)))))
  3311. ;; If --DATA has any affiliated keywords and
  3312. ;; WITH-AFFILIATED is non-nil, look for objects in
  3313. ;; them.
  3314. (when (and with-affiliated
  3315. (eq --category 'objects)
  3316. (memq --type org-element-all-elements))
  3317. (mapc (lambda (kwd-pair)
  3318. (let ((kwd (car kwd-pair))
  3319. (value (org-element-property
  3320. (cdr kwd-pair) --data)))
  3321. ;; Pay attention to the type of value.
  3322. ;; Preserve order for multiple keywords.
  3323. (cond
  3324. ((not value))
  3325. ((and (member kwd org-element-multiple-keywords)
  3326. (member kwd org-element-dual-keywords))
  3327. (mapc (lambda (line)
  3328. (funcall --walk-tree (cdr line))
  3329. (funcall --walk-tree (car line)))
  3330. (reverse value)))
  3331. ((member kwd org-element-multiple-keywords)
  3332. (mapc (lambda (line) (funcall --walk-tree line))
  3333. (reverse value)))
  3334. ((member kwd org-element-dual-keywords)
  3335. (funcall --walk-tree (cdr value))
  3336. (funcall --walk-tree (car value)))
  3337. (t (funcall --walk-tree value)))))
  3338. --affiliated-alist))
  3339. ;; Determine if a recursion into --DATA is possible.
  3340. (cond
  3341. ;; --TYPE is explicitly removed from recursion.
  3342. ((memq --type no-recursion))
  3343. ;; --DATA has no contents.
  3344. ((not (org-element-contents --data)))
  3345. ;; Looking for greater elements but --DATA is simply
  3346. ;; an element or an object.
  3347. ((and (eq --category 'greater-elements)
  3348. (not (memq --type org-element-greater-elements))))
  3349. ;; Looking for elements but --DATA is an object.
  3350. ((and (eq --category 'elements)
  3351. (memq --type org-element-all-objects)))
  3352. ;; In any other case, map contents.
  3353. (t (mapc --walk-tree (org-element-contents --data)))))))))))
  3354. (catch '--map-first-match
  3355. (funcall --walk-tree data)
  3356. ;; Return value in a proper order.
  3357. (nreverse --acc))))
  3358. ;; The following functions are internal parts of the parser.
  3359. ;;
  3360. ;; The first one, `org-element--parse-elements' acts at the element's
  3361. ;; level.
  3362. ;;
  3363. ;; The second one, `org-element--parse-objects' applies on all objects
  3364. ;; of a paragraph or a secondary string. It uses
  3365. ;; `org-element--get-next-object-candidates' to optimize the search of
  3366. ;; the next object in the buffer.
  3367. ;;
  3368. ;; More precisely, that function looks for every allowed object type
  3369. ;; first. Then, it discards failed searches, keeps further matches,
  3370. ;; and searches again types matched behind point, for subsequent
  3371. ;; calls. Thus, searching for a given type fails only once, and every
  3372. ;; object is searched only once at top level (but sometimes more for
  3373. ;; nested types).
  3374. (defun org-element--parse-elements
  3375. (beg end special structure granularity visible-only acc)
  3376. "Parse elements between BEG and END positions.
  3377. SPECIAL prioritize some elements over the others. It can be set
  3378. to `first-section', `quote-section', `section' `item' or
  3379. `table-row'.
  3380. When value is `item', STRUCTURE will be used as the current list
  3381. structure.
  3382. GRANULARITY determines the depth of the recursion. See
  3383. `org-element-parse-buffer' for more information.
  3384. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3385. elements.
  3386. Elements are accumulated into ACC."
  3387. (save-excursion
  3388. (goto-char beg)
  3389. ;; When parsing only headlines, skip any text before first one.
  3390. (when (and (eq granularity 'headline) (not (org-at-heading-p)))
  3391. (org-with-limited-levels (outline-next-heading)))
  3392. ;; Main loop start.
  3393. (while (< (point) end)
  3394. ;; Find current element's type and parse it accordingly to
  3395. ;; its category.
  3396. (let* ((element (org-element--current-element
  3397. end granularity special structure))
  3398. (type (org-element-type element))
  3399. (cbeg (org-element-property :contents-begin element)))
  3400. (goto-char (org-element-property :end element))
  3401. ;; Fill ELEMENT contents by side-effect.
  3402. (cond
  3403. ;; If VISIBLE-ONLY is true and element is hidden or if it has
  3404. ;; no contents, don't modify it.
  3405. ((or (and visible-only (org-element-property :hiddenp element))
  3406. (not cbeg)))
  3407. ;; Greater element: parse it between `contents-begin' and
  3408. ;; `contents-end'. Make sure GRANULARITY allows the
  3409. ;; recursion, or ELEMENT is an headline, in which case going
  3410. ;; inside is mandatory, in order to get sub-level headings.
  3411. ((and (memq type org-element-greater-elements)
  3412. (or (memq granularity '(element object nil))
  3413. (and (eq granularity 'greater-element)
  3414. (eq type 'section))
  3415. (eq type 'headline)))
  3416. (org-element--parse-elements
  3417. cbeg (org-element-property :contents-end element)
  3418. ;; Possibly switch to a special mode.
  3419. (case type
  3420. (headline
  3421. (if (org-element-property :quotedp element) 'quote-section
  3422. 'section))
  3423. (plain-list 'item)
  3424. (property-drawer 'node-property)
  3425. (table 'table-row))
  3426. (org-element-property :structure element)
  3427. granularity visible-only element))
  3428. ;; ELEMENT has contents. Parse objects inside, if
  3429. ;; GRANULARITY allows it.
  3430. ((memq granularity '(object nil))
  3431. (org-element--parse-objects
  3432. cbeg (org-element-property :contents-end element) element
  3433. (org-element-restriction type))))
  3434. (org-element-adopt-elements acc element)))
  3435. ;; Return result.
  3436. acc))
  3437. (defun org-element--parse-objects (beg end acc restriction)
  3438. "Parse objects between BEG and END and return recursive structure.
  3439. Objects are accumulated in ACC.
  3440. RESTRICTION is a list of object types which are allowed in the
  3441. current object."
  3442. (let (candidates)
  3443. (save-excursion
  3444. (goto-char beg)
  3445. (while (and (< (point) end)
  3446. (setq candidates (org-element--get-next-object-candidates
  3447. end restriction candidates)))
  3448. (let ((next-object
  3449. (let ((pos (apply 'min (mapcar 'cdr candidates))))
  3450. (save-excursion
  3451. (goto-char pos)
  3452. (funcall (intern (format "org-element-%s-parser"
  3453. (car (rassq pos candidates)))))))))
  3454. ;; 1. Text before any object. Untabify it.
  3455. (let ((obj-beg (org-element-property :begin next-object)))
  3456. (unless (= (point) obj-beg)
  3457. (setq acc
  3458. (org-element-adopt-elements
  3459. acc
  3460. (replace-regexp-in-string
  3461. "\t" (make-string tab-width ? )
  3462. (buffer-substring-no-properties (point) obj-beg))))))
  3463. ;; 2. Object...
  3464. (let ((obj-end (org-element-property :end next-object))
  3465. (cont-beg (org-element-property :contents-begin next-object)))
  3466. ;; Fill contents of NEXT-OBJECT by side-effect, if it has
  3467. ;; a recursive type.
  3468. (when (and cont-beg
  3469. (memq (car next-object) org-element-recursive-objects))
  3470. (save-restriction
  3471. (narrow-to-region
  3472. cont-beg
  3473. (org-element-property :contents-end next-object))
  3474. (org-element--parse-objects
  3475. (point-min) (point-max) next-object
  3476. (org-element-restriction next-object))))
  3477. (setq acc (org-element-adopt-elements acc next-object))
  3478. (goto-char obj-end))))
  3479. ;; 3. Text after last object. Untabify it.
  3480. (unless (= (point) end)
  3481. (setq acc
  3482. (org-element-adopt-elements
  3483. acc
  3484. (replace-regexp-in-string
  3485. "\t" (make-string tab-width ? )
  3486. (buffer-substring-no-properties (point) end)))))
  3487. ;; Result.
  3488. acc)))
  3489. (defun org-element--get-next-object-candidates (limit restriction objects)
  3490. "Return an alist of candidates for the next object.
  3491. LIMIT bounds the search, and RESTRICTION narrows candidates to
  3492. some object types.
  3493. Return value is an alist whose CAR is position and CDR the object
  3494. type, as a symbol.
  3495. OBJECTS is the previous candidates alist."
  3496. ;; Filter out any object found but not belonging to RESTRICTION.
  3497. (setq objects
  3498. (org-remove-if-not
  3499. (lambda (obj)
  3500. (let ((type (car obj)))
  3501. (memq (or (cdr (assq type org-element-object-successor-alist))
  3502. type)
  3503. restriction)))
  3504. objects))
  3505. (let (next-candidates types-to-search)
  3506. ;; If no previous result, search every object type in RESTRICTION.
  3507. ;; Otherwise, keep potential candidates (old objects located after
  3508. ;; point) and ask to search again those which had matched before.
  3509. (if (not objects) (setq types-to-search restriction)
  3510. (mapc (lambda (obj)
  3511. (if (< (cdr obj) (point)) (push (car obj) types-to-search)
  3512. (push obj next-candidates)))
  3513. objects))
  3514. ;; Call the appropriate successor function for each type to search
  3515. ;; and accumulate matches.
  3516. (mapc
  3517. (lambda (type)
  3518. (let* ((successor-fun
  3519. (intern
  3520. (format "org-element-%s-successor"
  3521. (or (cdr (assq type org-element-object-successor-alist))
  3522. type))))
  3523. (obj (funcall successor-fun limit)))
  3524. (and obj (push obj next-candidates))))
  3525. types-to-search)
  3526. ;; Return alist.
  3527. next-candidates))
  3528. ;;; Towards A Bijective Process
  3529. ;;
  3530. ;; The parse tree obtained with `org-element-parse-buffer' is really
  3531. ;; a snapshot of the corresponding Org buffer. Therefore, it can be
  3532. ;; interpreted and expanded into a string with canonical Org syntax.
  3533. ;; Hence `org-element-interpret-data'.
  3534. ;;
  3535. ;; The function relies internally on
  3536. ;; `org-element--interpret-affiliated-keywords'.
  3537. ;;;###autoload
  3538. (defun org-element-interpret-data (data &optional parent)
  3539. "Interpret DATA as Org syntax.
  3540. DATA is a parse tree, an element, an object or a secondary string
  3541. to interpret.
  3542. Optional argument PARENT is used for recursive calls. It contains
  3543. the element or object containing data, or nil.
  3544. Return Org syntax as a string."
  3545. (let* ((type (org-element-type data))
  3546. (results
  3547. (cond
  3548. ;; Secondary string.
  3549. ((not type)
  3550. (mapconcat
  3551. (lambda (obj) (org-element-interpret-data obj parent))
  3552. data ""))
  3553. ;; Full Org document.
  3554. ((eq type 'org-data)
  3555. (mapconcat
  3556. (lambda (obj) (org-element-interpret-data obj parent))
  3557. (org-element-contents data) ""))
  3558. ;; Plain text.
  3559. ((stringp data) data)
  3560. ;; Element/Object without contents.
  3561. ((not (org-element-contents data))
  3562. (funcall (intern (format "org-element-%s-interpreter" type))
  3563. data nil))
  3564. ;; Element/Object with contents.
  3565. (t
  3566. (let* ((greaterp (memq type org-element-greater-elements))
  3567. (objectp (and (not greaterp)
  3568. (memq type org-element-recursive-objects)))
  3569. (contents
  3570. (mapconcat
  3571. (lambda (obj) (org-element-interpret-data obj data))
  3572. (org-element-contents
  3573. (if (or greaterp objectp) data
  3574. ;; Elements directly containing objects must
  3575. ;; have their indentation normalized first.
  3576. (org-element-normalize-contents
  3577. data
  3578. ;; When normalizing first paragraph of an
  3579. ;; item or a footnote-definition, ignore
  3580. ;; first line's indentation.
  3581. (and (eq type 'paragraph)
  3582. (equal data (car (org-element-contents parent)))
  3583. (memq (org-element-type parent)
  3584. '(footnote-definition item))))))
  3585. "")))
  3586. (funcall (intern (format "org-element-%s-interpreter" type))
  3587. data
  3588. (if greaterp (org-element-normalize-contents contents)
  3589. contents)))))))
  3590. (if (memq type '(org-data plain-text nil)) results
  3591. ;; Build white spaces. If no `:post-blank' property is
  3592. ;; specified, assume its value is 0.
  3593. (let ((post-blank (or (org-element-property :post-blank data) 0)))
  3594. (if (memq type org-element-all-objects)
  3595. (concat results (make-string post-blank 32))
  3596. (concat
  3597. (org-element--interpret-affiliated-keywords data)
  3598. (org-element-normalize-string results)
  3599. (make-string post-blank 10)))))))
  3600. (defun org-element--interpret-affiliated-keywords (element)
  3601. "Return ELEMENT's affiliated keywords as Org syntax.
  3602. If there is no affiliated keyword, return the empty string."
  3603. (let ((keyword-to-org
  3604. (function
  3605. (lambda (key value)
  3606. (let (dual)
  3607. (when (member key org-element-dual-keywords)
  3608. (setq dual (cdr value) value (car value)))
  3609. (concat "#+" key
  3610. (and dual
  3611. (format "[%s]" (org-element-interpret-data dual)))
  3612. ": "
  3613. (if (member key org-element-parsed-keywords)
  3614. (org-element-interpret-data value)
  3615. value)
  3616. "\n"))))))
  3617. (mapconcat
  3618. (lambda (prop)
  3619. (let ((value (org-element-property prop element))
  3620. (keyword (upcase (substring (symbol-name prop) 1))))
  3621. (when value
  3622. (if (or (member keyword org-element-multiple-keywords)
  3623. ;; All attribute keywords can have multiple lines.
  3624. (string-match "^ATTR_" keyword))
  3625. (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
  3626. (reverse value)
  3627. "")
  3628. (funcall keyword-to-org keyword value)))))
  3629. ;; List all ELEMENT's properties matching an attribute line or an
  3630. ;; affiliated keyword, but ignore translated keywords since they
  3631. ;; cannot belong to the property list.
  3632. (loop for prop in (nth 1 element) by 'cddr
  3633. when (let ((keyword (upcase (substring (symbol-name prop) 1))))
  3634. (or (string-match "^ATTR_" keyword)
  3635. (and
  3636. (member keyword org-element-affiliated-keywords)
  3637. (not (assoc keyword
  3638. org-element-keyword-translation-alist)))))
  3639. collect prop)
  3640. "")))
  3641. ;; Because interpretation of the parse tree must return the same
  3642. ;; number of blank lines between elements and the same number of white
  3643. ;; space after objects, some special care must be given to white
  3644. ;; spaces.
  3645. ;;
  3646. ;; The first function, `org-element-normalize-string', ensures any
  3647. ;; string different from the empty string will end with a single
  3648. ;; newline character.
  3649. ;;
  3650. ;; The second function, `org-element-normalize-contents', removes
  3651. ;; global indentation from the contents of the current element.
  3652. (defun org-element-normalize-string (s)
  3653. "Ensure string S ends with a single newline character.
  3654. If S isn't a string return it unchanged. If S is the empty
  3655. string, return it. Otherwise, return a new string with a single
  3656. newline character at its end."
  3657. (cond
  3658. ((not (stringp s)) s)
  3659. ((string= "" s) "")
  3660. (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
  3661. (replace-match "\n" nil nil s)))))
  3662. (defun org-element-normalize-contents (element &optional ignore-first)
  3663. "Normalize plain text in ELEMENT's contents.
  3664. ELEMENT must only contain plain text and objects.
  3665. If optional argument IGNORE-FIRST is non-nil, ignore first line's
  3666. indentation to compute maximal common indentation.
  3667. Return the normalized element that is element with global
  3668. indentation removed from its contents. The function assumes that
  3669. indentation is not done with TAB characters."
  3670. (let* (ind-list ; for byte-compiler
  3671. collect-inds ; for byte-compiler
  3672. (collect-inds
  3673. (function
  3674. ;; Return list of indentations within BLOB. This is done by
  3675. ;; walking recursively BLOB and updating IND-LIST along the
  3676. ;; way. FIRST-FLAG is non-nil when the first string hasn't
  3677. ;; been seen yet. It is required as this string is the only
  3678. ;; one whose indentation doesn't happen after a newline
  3679. ;; character.
  3680. (lambda (blob first-flag)
  3681. (mapc
  3682. (lambda (object)
  3683. (when (and first-flag (stringp object))
  3684. (setq first-flag nil)
  3685. (string-match "\\`\\( *\\)" object)
  3686. (let ((len (length (match-string 1 object))))
  3687. ;; An indentation of zero means no string will be
  3688. ;; modified. Quit the process.
  3689. (if (zerop len) (throw 'zero (setq ind-list nil))
  3690. (push len ind-list))))
  3691. (cond
  3692. ((stringp object)
  3693. (let ((start 0))
  3694. ;; Avoid matching blank or empty lines.
  3695. (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
  3696. (not (equal (match-string 2 object) " ")))
  3697. (setq start (match-end 0))
  3698. (push (length (match-string 1 object)) ind-list))))
  3699. ((memq (org-element-type object) org-element-recursive-objects)
  3700. (funcall collect-inds object first-flag))))
  3701. (org-element-contents blob))))))
  3702. ;; Collect indentation list in ELEMENT. Possibly remove first
  3703. ;; value if IGNORE-FIRST is non-nil.
  3704. (catch 'zero (funcall collect-inds element (not ignore-first)))
  3705. (if (not ind-list) element
  3706. ;; Build ELEMENT back, replacing each string with the same
  3707. ;; string minus common indentation.
  3708. (let* (build ; For byte compiler.
  3709. (build
  3710. (function
  3711. (lambda (blob mci first-flag)
  3712. ;; Return BLOB with all its strings indentation
  3713. ;; shortened from MCI white spaces. FIRST-FLAG is
  3714. ;; non-nil when the first string hasn't been seen
  3715. ;; yet.
  3716. (setcdr (cdr blob)
  3717. (mapcar
  3718. (lambda (object)
  3719. (when (and first-flag (stringp object))
  3720. (setq first-flag nil)
  3721. (setq object
  3722. (replace-regexp-in-string
  3723. (format "\\` \\{%d\\}" mci) "" object)))
  3724. (cond
  3725. ((stringp object)
  3726. (replace-regexp-in-string
  3727. (format "\n \\{%d\\}" mci) "\n" object))
  3728. ((memq (org-element-type object)
  3729. org-element-recursive-objects)
  3730. (funcall build object mci first-flag))
  3731. (t object)))
  3732. (org-element-contents blob)))
  3733. blob))))
  3734. (funcall build element (apply 'min ind-list) (not ignore-first))))))
  3735. ;;; The Toolbox
  3736. ;;
  3737. ;; The first move is to implement a way to obtain the smallest element
  3738. ;; containing point. This is the job of `org-element-at-point'. It
  3739. ;; basically jumps back to the beginning of section containing point
  3740. ;; and moves, element after element, with
  3741. ;; `org-element--current-element' until the container is found. Note:
  3742. ;; When using `org-element-at-point', secondary values are never
  3743. ;; parsed since the function focuses on elements, not on objects.
  3744. ;;
  3745. ;; At a deeper level, `org-element-context' lists all elements and
  3746. ;; objects containing point.
  3747. ;;
  3748. ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
  3749. ;; internally by navigation and manipulation tools.
  3750. ;;;###autoload
  3751. (defun org-element-at-point (&optional keep-trail)
  3752. "Determine closest element around point.
  3753. Return value is a list like (TYPE PROPS) where TYPE is the type
  3754. of the element and PROPS a plist of properties associated to the
  3755. element.
  3756. Possible types are defined in `org-element-all-elements'.
  3757. Properties depend on element or object type, but always
  3758. include :begin, :end, :parent and :post-blank properties.
  3759. As a special case, if point is at the very beginning of a list or
  3760. sub-list, returned element will be that list instead of the first
  3761. item. In the same way, if point is at the beginning of the first
  3762. row of a table, returned element will be the table instead of the
  3763. first row.
  3764. If optional argument KEEP-TRAIL is non-nil, the function returns
  3765. a list of of elements leading to element at point. The list's
  3766. CAR is always the element at point. Following positions contain
  3767. element's siblings, then parents, siblings of parents, until the
  3768. first element of current section."
  3769. (org-with-wide-buffer
  3770. ;; If at an headline, parse it. It is the sole element that
  3771. ;; doesn't require to know about context. Be sure to disallow
  3772. ;; secondary string parsing, though.
  3773. (if (org-with-limited-levels (org-at-heading-p))
  3774. (progn
  3775. (beginning-of-line)
  3776. (if (not keep-trail) (org-element-headline-parser (point-max) t)
  3777. (list (org-element-headline-parser (point-max) t))))
  3778. ;; Otherwise move at the beginning of the section containing
  3779. ;; point.
  3780. (let ((origin (point))
  3781. (end (save-excursion
  3782. (org-with-limited-levels (outline-next-heading)) (point)))
  3783. element type special-flag trail struct prevs parent)
  3784. (org-with-limited-levels
  3785. (if (org-with-limited-levels (org-before-first-heading-p))
  3786. (goto-char (point-min))
  3787. (org-back-to-heading)
  3788. (forward-line)))
  3789. (org-skip-whitespace)
  3790. (beginning-of-line)
  3791. ;; Parse successively each element, skipping those ending
  3792. ;; before original position.
  3793. (catch 'exit
  3794. (while t
  3795. (setq element
  3796. (org-element--current-element end 'element special-flag struct)
  3797. type (car element))
  3798. (org-element-put-property element :parent parent)
  3799. (when keep-trail (push element trail))
  3800. (cond
  3801. ;; 1. Skip any element ending before point. Also skip
  3802. ;; element ending at point when we're sure that another
  3803. ;; element has started.
  3804. ((let ((elem-end (org-element-property :end element)))
  3805. (when (or (< elem-end origin)
  3806. (and (= elem-end origin) (/= elem-end end)))
  3807. (goto-char elem-end))))
  3808. ;; 2. An element containing point is always the element at
  3809. ;; point.
  3810. ((not (memq type org-element-greater-elements))
  3811. (throw 'exit (if keep-trail trail element)))
  3812. ;; 3. At any other greater element type, if point is
  3813. ;; within contents, move into it.
  3814. (t
  3815. (let ((cbeg (org-element-property :contents-begin element))
  3816. (cend (org-element-property :contents-end element)))
  3817. (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
  3818. ;; Create an anchor for tables and plain lists:
  3819. ;; when point is at the very beginning of these
  3820. ;; elements, ignoring affiliated keywords,
  3821. ;; target them instead of their contents.
  3822. (and (= cbeg origin) (memq type '(plain-list table)))
  3823. ;; When point is at contents end, do not move
  3824. ;; into elements with an explicit ending, but
  3825. ;; return that element instead.
  3826. (and (= cend origin)
  3827. (memq type
  3828. '(center-block
  3829. drawer dynamic-block inlinetask item
  3830. plain-list property-drawer quote-block
  3831. special-block))))
  3832. (throw 'exit (if keep-trail trail element))
  3833. (setq parent element)
  3834. (case type
  3835. (plain-list
  3836. (setq special-flag 'item
  3837. struct (org-element-property :structure element)))
  3838. (property-drawer (setq special-flag 'node-property))
  3839. (table (setq special-flag 'table-row))
  3840. (otherwise (setq special-flag nil)))
  3841. (setq end cend)
  3842. (goto-char cbeg)))))))))))
  3843. ;;;###autoload
  3844. (defun org-element-context ()
  3845. "Return closest element or object around point.
  3846. Return value is a list like (TYPE PROPS) where TYPE is the type
  3847. of the element or object and PROPS a plist of properties
  3848. associated to it.
  3849. Possible types are defined in `org-element-all-elements' and
  3850. `org-element-all-objects'. Properties depend on element or
  3851. object type, but always include :begin, :end, :parent
  3852. and :post-blank properties."
  3853. (org-with-wide-buffer
  3854. (let* ((origin (point))
  3855. (element (org-element-at-point))
  3856. (type (car element))
  3857. end)
  3858. ;; Check if point is inside an element containing objects or at
  3859. ;; a secondary string. In that case, move to beginning of the
  3860. ;; element or secondary string and set END to the other side.
  3861. (if (not (or (and (eq type 'item)
  3862. (let ((tag (org-element-property :tag element)))
  3863. (and tag
  3864. (progn
  3865. (beginning-of-line)
  3866. (search-forward tag (point-at-eol))
  3867. (goto-char (match-beginning 0))
  3868. (and (>= origin (point))
  3869. (<= origin
  3870. ;; `1+' is required so some
  3871. ;; successors can match
  3872. ;; properly their object.
  3873. (setq end (1+ (match-end 0)))))))))
  3874. (and (memq type '(headline inlinetask))
  3875. (progn (beginning-of-line)
  3876. (skip-chars-forward "* ")
  3877. (setq end (point-at-eol))))
  3878. (and (memq type '(paragraph table-row verse-block))
  3879. (let ((cbeg (org-element-property
  3880. :contents-begin element))
  3881. (cend (org-element-property
  3882. :contents-end element)))
  3883. (and (>= origin cbeg)
  3884. (<= origin cend)
  3885. (progn (goto-char cbeg) (setq end cend)))))))
  3886. element
  3887. (let ((restriction (org-element-restriction element))
  3888. (parent element)
  3889. candidates)
  3890. (catch 'exit
  3891. (while (setq candidates (org-element--get-next-object-candidates
  3892. end restriction candidates))
  3893. (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
  3894. candidates)))
  3895. ;; If ORIGIN is before next object in element, there's
  3896. ;; no point in looking further.
  3897. (if (> (cdr closest-cand) origin) (throw 'exit parent)
  3898. (let* ((object
  3899. (progn (goto-char (cdr closest-cand))
  3900. (funcall (intern (format "org-element-%s-parser"
  3901. (car closest-cand))))))
  3902. (cbeg (org-element-property :contents-begin object))
  3903. (cend (org-element-property :contents-end object)))
  3904. (cond
  3905. ;; ORIGIN is after OBJECT, so skip it.
  3906. ((< (org-element-property :end object) origin)
  3907. (goto-char (org-element-property :end object)))
  3908. ;; ORIGIN is within a non-recursive object or at an
  3909. ;; object boundaries: Return that object.
  3910. ((or (not cbeg) (> cbeg origin) (< cend origin))
  3911. (throw 'exit
  3912. (org-element-put-property object :parent parent)))
  3913. ;; Otherwise, move within current object and restrict
  3914. ;; search to the end of its contents.
  3915. (t (goto-char cbeg)
  3916. (org-element-put-property object :parent parent)
  3917. (setq parent object
  3918. restriction (org-element-restriction object)
  3919. end cend)))))))
  3920. parent))))))
  3921. (defsubst org-element-nested-p (elem-A elem-B)
  3922. "Non-nil when elements ELEM-A and ELEM-B are nested."
  3923. (let ((beg-A (org-element-property :begin elem-A))
  3924. (beg-B (org-element-property :begin elem-B))
  3925. (end-A (org-element-property :end elem-A))
  3926. (end-B (org-element-property :end elem-B)))
  3927. (or (and (>= beg-A beg-B) (<= end-A end-B))
  3928. (and (>= beg-B beg-A) (<= end-B end-A)))))
  3929. (defun org-element-swap-A-B (elem-A elem-B)
  3930. "Swap elements ELEM-A and ELEM-B.
  3931. Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
  3932. end of ELEM-A."
  3933. (goto-char (org-element-property :begin elem-A))
  3934. ;; There are two special cases when an element doesn't start at bol:
  3935. ;; the first paragraph in an item or in a footnote definition.
  3936. (let ((specialp (not (bolp))))
  3937. ;; Only a paragraph without any affiliated keyword can be moved at
  3938. ;; ELEM-A position in such a situation. Note that the case of
  3939. ;; a footnote definition is impossible: it cannot contain two
  3940. ;; paragraphs in a row because it cannot contain a blank line.
  3941. (if (and specialp
  3942. (or (not (eq (org-element-type elem-B) 'paragraph))
  3943. (/= (org-element-property :begin elem-B)
  3944. (org-element-property :contents-begin elem-B))))
  3945. (error "Cannot swap elements"))
  3946. ;; In a special situation, ELEM-A will have no indentation. We'll
  3947. ;; give it ELEM-B's (which will in, in turn, have no indentation).
  3948. (let* ((ind-B (when specialp
  3949. (goto-char (org-element-property :begin elem-B))
  3950. (org-get-indentation)))
  3951. (beg-A (org-element-property :begin elem-A))
  3952. (end-A (save-excursion
  3953. (goto-char (org-element-property :end elem-A))
  3954. (skip-chars-backward " \r\t\n")
  3955. (point-at-eol)))
  3956. (beg-B (org-element-property :begin elem-B))
  3957. (end-B (save-excursion
  3958. (goto-char (org-element-property :end elem-B))
  3959. (skip-chars-backward " \r\t\n")
  3960. (point-at-eol)))
  3961. ;; Store overlays responsible for visibility status. We
  3962. ;; also need to store their boundaries as they will be
  3963. ;; removed from buffer.
  3964. (overlays
  3965. (cons
  3966. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  3967. (overlays-in beg-A end-A))
  3968. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  3969. (overlays-in beg-B end-B))))
  3970. ;; Get contents.
  3971. (body-A (buffer-substring beg-A end-A))
  3972. (body-B (delete-and-extract-region beg-B end-B)))
  3973. (goto-char beg-B)
  3974. (when specialp
  3975. (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
  3976. (org-indent-to-column ind-B))
  3977. (insert body-A)
  3978. ;; Restore ex ELEM-A overlays.
  3979. (let ((offset (- beg-B beg-A)))
  3980. (mapc (lambda (ov)
  3981. (move-overlay
  3982. (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
  3983. (car overlays))
  3984. (goto-char beg-A)
  3985. (delete-region beg-A end-A)
  3986. (insert body-B)
  3987. ;; Restore ex ELEM-B overlays.
  3988. (mapc (lambda (ov)
  3989. (move-overlay
  3990. (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
  3991. (cdr overlays)))
  3992. (goto-char (org-element-property :end elem-B)))))
  3993. (provide 'org-element)
  3994. ;; Local variables:
  3995. ;; generated-autoload-file: "org-loaddefs.el"
  3996. ;; End:
  3997. ;;; org-element.el ends here