org-element.el 146 KB

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