org-element.el 170 KB

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