org-element.el 146 KB

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