org-element.el 153 KB

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