org-element.el 156 KB

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