org-element.el 156 KB

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