org-element.el 175 KB

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