org-element.el 166 KB

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