org-element.el 166 KB

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