org-element.el 147 KB

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