org-element.el 139 KB

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