org-element.el 148 KB

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