org-element.el 141 KB

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