org-element.el 175 KB

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