org-element.el 159 KB

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