org-element.el 146 KB

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