org-element.el 171 KB

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