org-element.el 169 KB

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