org-element.el 149 KB

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