org-element.el 137 KB

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