org-element.el 171 KB

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