org-element.el 137 KB

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