org-element.el 157 KB

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