org-element.el 146 KB

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