org-element.el 153 KB

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