org-element.el 176 KB

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