org-element.el 170 KB

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