org-element.el 175 KB

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