org-list.el 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560
  1. ;;; org-list.el --- Plain lists for Org -*- lexical-binding: t; -*-
  2. ;;
  3. ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Bastien Guerry <bzg@gnu.org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file contains the code dealing with plain lists in Org-mode.
  26. ;; The core concept behind lists is their structure. A structure is
  27. ;; a snapshot of the list, in the shape of a data tree (see
  28. ;; `org-list-struct').
  29. ;; Once the list structure is stored, it is possible to make changes
  30. ;; on it that will be mirrored to the real list or to get information
  31. ;; about the list, using accessors and methods provided in the
  32. ;; library. Most of them require the use of one or two helper
  33. ;; functions, namely `org-list-parents-alist' and
  34. ;; `org-list-prevs-alist'.
  35. ;; Structure is eventually applied to the buffer with
  36. ;; `org-list-write-struct'. This function repairs (bullets,
  37. ;; indentation, checkboxes) the list in the process. It should be
  38. ;; called near the end of any function working on structures.
  39. ;; Thus, a function applying to lists should usually follow this
  40. ;; template:
  41. ;; 1. Verify point is in a list and grab item beginning (with the same
  42. ;; function `org-in-item-p'). If the function requires the cursor
  43. ;; to be at item's bullet, `org-at-item-p' is more selective. It
  44. ;; is also possible to move point to the closest item with
  45. ;; `org-list-search-backward', or `org-list-search-forward',
  46. ;; applied to the function `org-item-beginning-re'.
  47. ;; 2. Get list structure with `org-list-struct'.
  48. ;; 3. Compute one, or both, helper functions,
  49. ;; (`org-list-parents-alist', `org-list-prevs-alist') depending on
  50. ;; needed accessors.
  51. ;; 4. Proceed with the modifications, using methods and accessors.
  52. ;; 5. Verify and apply structure to buffer, using
  53. ;; `org-list-write-struct'.
  54. ;; 6. If changes made to the list might have modified check-boxes,
  55. ;; call `org-update-checkbox-count-maybe'.
  56. ;; Computing a structure can be a costly operation on huge lists (a
  57. ;; few thousand lines long). Thus, code should follow the rule:
  58. ;; "collect once, use many". As a corollary, it is usually a bad idea
  59. ;; to use directly an interactive function inside the code, as those,
  60. ;; being independent entities, read the whole list structure another
  61. ;; time.
  62. ;;; Code:
  63. (require 'cl-lib)
  64. (require 'org-macs)
  65. (require 'org-compat)
  66. (defvar org-M-RET-may-split-line)
  67. (defvar org-auto-align-tags)
  68. (defvar org-blank-before-new-entry)
  69. (defvar org-clock-string)
  70. (defvar org-closed-string)
  71. (defvar org-deadline-string)
  72. (defvar org-description-max-indent)
  73. (defvar org-done-keywords)
  74. (defvar org-drawer-regexp)
  75. (defvar org-element-all-objects)
  76. (defvar org-inhibit-startup)
  77. (defvar org-odd-levels-only)
  78. (defvar org-outline-regexp-bol)
  79. (defvar org-scheduled-string)
  80. (defvar org-todo-line-regexp)
  81. (defvar org-ts-regexp)
  82. (defvar org-ts-regexp-both)
  83. (declare-function org-at-heading-p "org" (&optional invisible-ok))
  84. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  85. (declare-function org-before-first-heading-p "org" ())
  86. (declare-function org-combine-plists "org" (&rest plists))
  87. (declare-function org-current-level "org" ())
  88. (declare-function org-element-at-point "org-element" ())
  89. (declare-function org-element-context "org-element" (&optional element))
  90. (declare-function org-element-interpret-data "org-element" (data))
  91. (declare-function
  92. org-element-lineage "org-element" (blob &optional types with-self))
  93. (declare-function org-element-macro-interpreter "org-element" (macro ##))
  94. (declare-function
  95. org-element-map "org-element"
  96. (data types fun &optional info first-match no-recursion with-affiliated))
  97. (declare-function org-element-normalize-string "org-element" (s))
  98. (declare-function org-element-parse-buffer "org-element"
  99. (&optional granularity visible-only))
  100. (declare-function org-element-property "org-element" (property element))
  101. (declare-function org-element-put-property "org-element"
  102. (element property value))
  103. (declare-function org-element-set-element "org-element" (old new))
  104. (declare-function org-element-type "org-element" (element))
  105. (declare-function org-element-update-syntax "org-element" ())
  106. (declare-function org-entry-get "org"
  107. (pom property &optional inherit literal-nil))
  108. (declare-function org-export-create-backend "ox" (&rest rest) t)
  109. (declare-function org-export-data-with-backend "ox" (data backend info))
  110. (declare-function org-export-get-backend "ox" (name))
  111. (declare-function org-export-get-environment "ox"
  112. (&optional backend subtreep ext-plist))
  113. (declare-function org-export-get-next-element "ox"
  114. (blob info &optional n))
  115. (declare-function org-export-with-backend "ox"
  116. (backend data &optional contents info))
  117. (declare-function org-fix-tags-on-the-fly "org" ())
  118. (declare-function org-get-indentation "org" (&optional line))
  119. (declare-function org-get-todo-state "org" ())
  120. (declare-function org-in-block-p "org" (names))
  121. (declare-function org-in-regexp "org" (re &optional nlines visually))
  122. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  123. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  124. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  125. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  126. (declare-function org-level-increment "org" ())
  127. (declare-function org-narrow-to-subtree "org" ())
  128. (declare-function org-outline-level "org" ())
  129. (declare-function org-previous-line-empty-p "org" ())
  130. (declare-function org-reduced-level "org" (L))
  131. (declare-function org-remove-indentation "org" (code &optional n))
  132. (declare-function org-show-subtree "org" ())
  133. (declare-function org-sort-remove-invisible "org" (S))
  134. (declare-function org-time-string-to-seconds "org" (s))
  135. (declare-function org-timer-hms-to-secs "org-timer" (hms))
  136. (declare-function org-timer-item "org-timer" (&optional arg))
  137. (declare-function org-trim "org" (s &optional keep-lead))
  138. (declare-function org-uniquify "org" (list))
  139. (declare-function outline-flag-region "outline" (from to flag))
  140. (declare-function outline-invisible-p "outline" (&optional pos))
  141. (declare-function outline-next-heading "outline" ())
  142. (declare-function outline-previous-heading "outline" ())
  143. ;;; Configuration variables
  144. (defgroup org-plain-lists nil
  145. "Options concerning plain lists in Org-mode."
  146. :tag "Org Plain lists"
  147. :group 'org-structure)
  148. (defcustom org-cycle-include-plain-lists t
  149. "When t, make TAB cycle visibility on plain list items.
  150. Cycling plain lists works only when the cursor is on a plain list
  151. item. When the cursor is on an outline heading, plain lists are
  152. treated as text. This is the most stable way of handling this,
  153. which is why it is the default.
  154. When this is the symbol `integrate', then integrate plain list
  155. items when cycling, as if they were children of outline headings.
  156. This setting can lead to strange effects when switching visibility
  157. to `children', because the first \"child\" in a subtree decides
  158. what children should be listed. If that first \"child\" is a
  159. plain list item with an implied large level number, all true
  160. children and grand children of the outline heading will be
  161. exposed in a children' view."
  162. :group 'org-plain-lists
  163. :group 'org-cycle
  164. :type '(choice
  165. (const :tag "Never" nil)
  166. (const :tag "With cursor in plain list (recommended)" t)
  167. (const :tag "As children of outline headings" integrate)))
  168. (defcustom org-list-demote-modify-bullet nil
  169. "Default bullet type installed when demoting an item.
  170. This is an association list, for each bullet type, this alist will point
  171. to the bullet that should be used when this item is demoted.
  172. For example,
  173. (setq org-list-demote-modify-bullet
  174. \\='((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
  175. will make
  176. + Movies
  177. + Silence of the Lambs
  178. + My Cousin Vinny
  179. + Books
  180. + The Hunt for Red October
  181. + The Road to Omaha
  182. into
  183. + Movies
  184. - Silence of the Lambs
  185. - My Cousin Vinny
  186. + Books
  187. - The Hunt for Red October
  188. - The Road to Omaha"
  189. :group 'org-plain-lists
  190. :type '(repeat
  191. (cons
  192. (choice :tag "If the current bullet is "
  193. (const "-")
  194. (const "+")
  195. (const "*")
  196. (const "1.")
  197. (const "1)"))
  198. (choice :tag "demotion will change it to"
  199. (const "-")
  200. (const "+")
  201. (const "*")
  202. (const "1.")
  203. (const "1)")))))
  204. (defcustom org-plain-list-ordered-item-terminator t
  205. "The character that makes a line with leading number an ordered list item.
  206. Valid values are ?. and ?\). To get both terminators, use t.
  207. This variable needs to be set before org.el is loaded. If you
  208. need to make a change while Emacs is running, use the customize
  209. interface or run the following code after updating it:
  210. \\[org-element-update-syntax]"
  211. :group 'org-plain-lists
  212. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  213. (const :tag "paren like in \"2)\"" ?\))
  214. (const :tag "both" t))
  215. :set (lambda (var val) (set var val)
  216. (when (featurep 'org-element) (org-element-update-syntax))))
  217. (defcustom org-list-allow-alphabetical nil
  218. "Non-nil means single character alphabetical bullets are allowed.
  219. Both uppercase and lowercase are handled. Lists with more than
  220. 26 items will fallback to standard numbering. Alphabetical
  221. counters like \"[@c]\" will be recognized.
  222. This variable needs to be set before org.el is loaded. If you
  223. need to make a change while Emacs is running, use the customize
  224. interface or run the following code after updating it:
  225. \\[org-element-update-syntax]"
  226. :group 'org-plain-lists
  227. :version "24.1"
  228. :type 'boolean
  229. :set (lambda (var val) (set var val)
  230. (when (featurep 'org-element) (org-element-update-syntax))))
  231. (defcustom org-list-two-spaces-after-bullet-regexp nil
  232. "A regular expression matching bullets that should have 2 spaces after them.
  233. When nil, no bullet will have two spaces after them. When
  234. a string, it will be used as a regular expression. When the
  235. bullet type of a list is changed, the new bullet type will be
  236. matched against this regexp. If it matches, there will be two
  237. spaces instead of one after the bullet in each item of the list."
  238. :group 'org-plain-lists
  239. :type '(choice
  240. (const :tag "never" nil)
  241. (regexp)))
  242. (defcustom org-list-automatic-rules '((checkbox . t)
  243. (indent . t))
  244. "Non-nil means apply set of rules when acting on lists.
  245. By default, automatic actions are taken when using
  246. \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
  247. \\[org-shiftmetaright], \\[org-shiftmetaleft],
  248. \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
  249. \\[org-insert-todo-heading]. You can disable individually these
  250. rules by setting them to nil. Valid rules are:
  251. checkbox when non-nil, checkbox statistics is updated each time
  252. you either insert a new checkbox or toggle a checkbox.
  253. indent when non-nil, indenting or outdenting list top-item
  254. with its subtree will move the whole list and
  255. outdenting a list whose bullet is * to column 0 will
  256. change that bullet to \"-\"."
  257. :group 'org-plain-lists
  258. :version "24.1"
  259. :type '(alist :tag "Sets of rules"
  260. :key-type
  261. (choice
  262. (const :tag "Checkbox" checkbox)
  263. (const :tag "Indent" indent))
  264. :value-type
  265. (boolean :tag "Activate" :value t)))
  266. (defcustom org-list-use-circular-motion nil
  267. "Non-nil means commands implying motion in lists should be cyclic.
  268. In that case, the item following the last item is the first one,
  269. and the item preceding the first item is the last one.
  270. This affects the behavior of \\[org-move-item-up],
  271. \\[org-move-item-down], \\[org-next-item] and
  272. \\[org-previous-item]."
  273. :group 'org-plain-lists
  274. :version "24.1"
  275. :type 'boolean)
  276. (defvar org-checkbox-statistics-hook nil
  277. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  278. This hook runs even if checkbox rule in
  279. `org-list-automatic-rules' does not apply, so it can be used to
  280. implement alternative ways of collecting statistics
  281. information.")
  282. (defcustom org-checkbox-hierarchical-statistics t
  283. "Non-nil means checkbox statistics counts only the state of direct children.
  284. When nil, all boxes below the cookie are counted.
  285. This can be set to nil on a per-node basis using a COOKIE_DATA property
  286. with the word \"recursive\" in the value."
  287. :group 'org-plain-lists
  288. :type 'boolean)
  289. (defcustom org-list-description-max-indent 20
  290. "Maximum indentation for the second line of a description list.
  291. When the indentation would be larger than this, it will become
  292. 5 characters instead."
  293. :group 'org-plain-lists
  294. :type 'integer)
  295. (defcustom org-list-indent-offset 0
  296. "Additional indentation for sub-items in a list.
  297. By setting this to a small number, usually 1 or 2, one can more
  298. clearly distinguish sub-items in a list."
  299. :group 'org-plain-lists
  300. :version "24.1"
  301. :type 'integer)
  302. (defcustom org-list-radio-list-templates
  303. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  304. % END RECEIVE ORGLST %n
  305. \\begin{comment}
  306. #+ORGLST: SEND %n org-list-to-latex
  307. -
  308. \\end{comment}\n")
  309. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  310. @c END RECEIVE ORGLST %n
  311. @ignore
  312. #+ORGLST: SEND %n org-list-to-texinfo
  313. -
  314. @end ignore\n")
  315. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  316. <!-- END RECEIVE ORGLST %n -->
  317. <!--
  318. #+ORGLST: SEND %n org-list-to-html
  319. -
  320. -->\n"))
  321. "Templates for radio lists in different major modes.
  322. All occurrences of %n in a template will be replaced with the name of the
  323. list, obtained by prompting the user."
  324. :group 'org-plain-lists
  325. :type '(repeat
  326. (list (symbol :tag "Major mode")
  327. (string :tag "Format"))))
  328. (defvar org-list-forbidden-blocks '("example" "verse" "src" "export")
  329. "Names of blocks where lists are not allowed.
  330. Names must be in lower case.")
  331. (defvar org-list-export-context '(block inlinetask)
  332. "Context types where lists will be interpreted during export.
  333. Valid types are `drawer', `inlinetask' and `block'. More
  334. specifically, type `block' is determined by the variable
  335. `org-list-forbidden-blocks'.")
  336. ;;; Predicates and regexps
  337. (defconst org-list-end-re "^[ \t]*\n[ \t]*\n"
  338. "Regex matching the end of a plain list.")
  339. (defconst org-list-full-item-re
  340. (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
  341. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  342. "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
  343. "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
  344. "Matches a list item and puts everything into groups:
  345. group 1: bullet
  346. group 2: counter
  347. group 3: checkbox
  348. group 4: description tag")
  349. (defun org-item-re ()
  350. "Return the correct regular expression for plain lists."
  351. (let ((term (cond
  352. ((eq org-plain-list-ordered-item-terminator t) "[.)]")
  353. ((= org-plain-list-ordered-item-terminator ?\)) ")")
  354. ((= org-plain-list-ordered-item-terminator ?.) "\\.")
  355. (t "[.)]")))
  356. (alpha (if org-list-allow-alphabetical "\\|[A-Za-z]" "")))
  357. (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha "\\)" term
  358. "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
  359. (defsubst org-item-beginning-re ()
  360. "Regexp matching the beginning of a plain list item."
  361. (concat "^" (org-item-re)))
  362. (defun org-list-at-regexp-after-bullet-p (regexp)
  363. "Is point at a list item with REGEXP after bullet?"
  364. (and (org-at-item-p)
  365. (save-excursion
  366. (goto-char (match-end 0))
  367. (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
  368. (if org-list-allow-alphabetical
  369. "\\([0-9]+\\|[A-Za-z]\\)"
  370. "[0-9]+")
  371. "\\][ \t]*\\)")))
  372. ;; Ignore counter if any
  373. (when (looking-at counter-re) (goto-char (match-end 0))))
  374. (looking-at regexp))))
  375. (defun org-list-in-valid-context-p ()
  376. "Is point in a context where lists are allowed?"
  377. (not (org-in-block-p org-list-forbidden-blocks)))
  378. (defun org-in-item-p ()
  379. "Return item beginning position when in a plain list, nil otherwise."
  380. (save-excursion
  381. (beginning-of-line)
  382. (let* ((case-fold-search t)
  383. (context (org-list-context))
  384. (lim-up (car context))
  385. (inlinetask-re (and (featurep 'org-inlinetask)
  386. (org-inlinetask-outline-regexp)))
  387. (item-re (org-item-re))
  388. ;; Indentation isn't meaningful when point starts at an empty
  389. ;; line or an inline task.
  390. (ind-ref (if (or (looking-at "^[ \t]*$")
  391. (and inlinetask-re (looking-at inlinetask-re)))
  392. 10000
  393. (org-get-indentation))))
  394. (cond
  395. ((eq (nth 2 context) 'invalid) nil)
  396. ((looking-at item-re) (point))
  397. (t
  398. ;; Detect if cursor in amidst `org-list-end-re'. First, count
  399. ;; number HL of hard lines it takes, then call `org-in-regexp'
  400. ;; to compute its boundaries END-BOUNDS. When point is
  401. ;; in-between, move cursor before regexp beginning.
  402. (let ((hl 0) (i -1) end-bounds)
  403. (when (and (progn
  404. (while (setq i (string-match
  405. "[\r\n]" org-list-end-re (1+ i)))
  406. (setq hl (1+ hl)))
  407. (setq end-bounds (org-in-regexp org-list-end-re hl)))
  408. (>= (point) (car end-bounds))
  409. (< (point) (cdr end-bounds)))
  410. (goto-char (car end-bounds))
  411. (forward-line -1)))
  412. ;; Look for an item, less indented that reference line.
  413. (catch 'exit
  414. (while t
  415. (let ((ind (org-get-indentation)))
  416. (cond
  417. ;; This is exactly what we want.
  418. ((and (looking-at item-re) (< ind ind-ref))
  419. (throw 'exit (point)))
  420. ;; At upper bound of search or looking at the end of a
  421. ;; previous list: search is over.
  422. ((<= (point) lim-up) (throw 'exit nil))
  423. ((looking-at org-list-end-re) (throw 'exit nil))
  424. ;; Skip blocks, drawers, inline-tasks, blank lines
  425. ((and (looking-at "^[ \t]*#\\+end_")
  426. (re-search-backward "^[ \t]*#\\+begin_" lim-up t)))
  427. ((and (looking-at "^[ \t]*:END:")
  428. (re-search-backward org-drawer-regexp lim-up t))
  429. (beginning-of-line))
  430. ((and inlinetask-re (looking-at inlinetask-re))
  431. (org-inlinetask-goto-beginning)
  432. (forward-line -1))
  433. ((looking-at "^[ \t]*$") (forward-line -1))
  434. ;; Text at column 0 cannot belong to a list: stop.
  435. ((zerop ind) (throw 'exit nil))
  436. ;; Normal text less indented than reference line, take
  437. ;; it as new reference.
  438. ((< ind ind-ref)
  439. (setq ind-ref ind)
  440. (forward-line -1))
  441. (t (forward-line -1)))))))))))
  442. (defun org-at-item-p ()
  443. "Is point in a line starting a hand-formatted item?"
  444. (save-excursion
  445. (beginning-of-line)
  446. (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
  447. (defun org-at-item-bullet-p ()
  448. "Is point at the bullet of a plain list item?"
  449. (and (org-at-item-p)
  450. (not (member (char-after) '(?\ ?\t)))
  451. (< (point) (match-end 0))))
  452. (defun org-at-item-timer-p ()
  453. "Is point at a line starting a plain list item with a timer?"
  454. (org-list-at-regexp-after-bullet-p
  455. "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  456. (defun org-at-item-description-p ()
  457. "Is point at a description list item?"
  458. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::\\([ \t]+\\|$\\)"))
  459. (defun org-at-item-checkbox-p ()
  460. "Is point at a line starting a plain-list item with a checklet?"
  461. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  462. (defun org-at-item-counter-p ()
  463. "Is point at a line starting a plain-list item with a counter?"
  464. (and (org-at-item-p)
  465. (looking-at org-list-full-item-re)
  466. (match-string 2)))
  467. ;;; Structures and helper functions
  468. (defun org-list-context ()
  469. "Determine context, and its boundaries, around point.
  470. Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
  471. are boundaries and CONTEXT is a symbol among `drawer', `block',
  472. `invalid', `inlinetask' and nil.
  473. Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
  474. (save-match-data
  475. (save-excursion
  476. (org-with-limited-levels
  477. (beginning-of-line)
  478. (let ((case-fold-search t) (pos (point)) beg end context-type
  479. ;; Get positions of surrounding headings. This is the
  480. ;; default context.
  481. (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t))
  482. (point)))
  483. (point-min)))
  484. (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
  485. ;; Is point inside a drawer?
  486. (let ((end-re "^[ \t]*:END:")
  487. (beg-re org-drawer-regexp))
  488. (when (save-excursion
  489. (and (not (looking-at beg-re))
  490. (not (looking-at end-re))
  491. (setq beg (and (re-search-backward beg-re lim-up t)
  492. (1+ (point-at-eol))))
  493. (setq end (or (and (re-search-forward end-re lim-down t)
  494. (1- (match-beginning 0)))
  495. lim-down))
  496. (>= end pos)))
  497. (setq lim-up beg lim-down end context-type 'drawer)))
  498. ;; Is point strictly in a block, and of which type?
  499. (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type)
  500. (when (save-excursion
  501. (and (not (looking-at block-re))
  502. (setq beg (and (re-search-backward block-re lim-up t)
  503. (1+ (point-at-eol))))
  504. (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
  505. (setq type (downcase (match-string 1)))
  506. (goto-char beg)
  507. (setq end (or (and (re-search-forward block-re lim-down t)
  508. (1- (point-at-bol)))
  509. lim-down))
  510. (>= end pos)
  511. (equal (downcase (match-string 1)) "end")))
  512. (setq lim-up beg lim-down end
  513. context-type (if (member type org-list-forbidden-blocks)
  514. 'invalid 'block))))
  515. ;; Is point in an inlinetask?
  516. (when (and (featurep 'org-inlinetask)
  517. (save-excursion
  518. (let* ((beg-re (org-inlinetask-outline-regexp))
  519. (end-re (concat beg-re "END[ \t]*$")))
  520. (and (not (looking-at "^\\*+"))
  521. (setq beg (and (re-search-backward beg-re lim-up t)
  522. (1+ (point-at-eol))))
  523. (not (looking-at end-re))
  524. (setq end (and (re-search-forward end-re lim-down t)
  525. (1- (match-beginning 0))))
  526. (> (point) pos)))))
  527. (setq lim-up beg lim-down end context-type 'inlinetask))
  528. ;; Return context boundaries and type.
  529. (list lim-up lim-down context-type))))))
  530. (defun org-list-struct ()
  531. "Return structure of list at point.
  532. A list structure is an alist where key is point at item, and
  533. values are:
  534. 1. indentation,
  535. 2. bullet with trailing whitespace,
  536. 3. bullet counter, if any,
  537. 4. checkbox, if any,
  538. 5. description tag, if any,
  539. 6. position at item end.
  540. Thus the following list, where numbers in parens are
  541. point-at-bol:
  542. - [X] first item (1)
  543. 1. sub-item 1 (18)
  544. 5. [@5] sub-item 2 (34)
  545. some other text belonging to first item (55)
  546. - last item (97)
  547. + tag :: description (109)
  548. (131)
  549. will get the following structure:
  550. ((1 0 \"- \" nil \"[X]\" nil 97)
  551. (18 2 \"1. \" nil nil nil 34)
  552. (34 2 \"5. \" \"5\" nil nil 55)
  553. (97 0 \"- \" nil nil nil 131)
  554. (109 2 \"+ \" nil nil \"tag\" 131))
  555. Assume point is at an item."
  556. (save-excursion
  557. (beginning-of-line)
  558. (let* ((case-fold-search t)
  559. (context (org-list-context))
  560. (lim-up (car context))
  561. (lim-down (nth 1 context))
  562. (text-min-ind 10000)
  563. (item-re (org-item-re))
  564. (inlinetask-re (and (featurep 'org-inlinetask)
  565. (org-inlinetask-outline-regexp)))
  566. (beg-cell (cons (point) (org-get-indentation)))
  567. itm-lst itm-lst-2 end-lst end-lst-2 struct
  568. (assoc-at-point
  569. (function
  570. ;; Return association at point.
  571. (lambda (ind)
  572. (looking-at org-list-full-item-re)
  573. (let ((bullet (match-string-no-properties 1)))
  574. (list (point)
  575. ind
  576. bullet
  577. (match-string-no-properties 2) ; counter
  578. (match-string-no-properties 3) ; checkbox
  579. ;; Description tag.
  580. (and (string-match-p "[-+*]" bullet)
  581. (match-string-no-properties 4)))))))
  582. (end-before-blank
  583. (function
  584. ;; Ensure list ends at the first blank line.
  585. (lambda ()
  586. (skip-chars-backward " \r\t\n")
  587. (min (1+ (point-at-eol)) lim-down)))))
  588. ;; 1. Read list from starting item to its beginning, and save
  589. ;; top item position and indentation in BEG-CELL. Also store
  590. ;; ending position of items in END-LST.
  591. (save-excursion
  592. (catch 'exit
  593. (while t
  594. (let ((ind (org-get-indentation)))
  595. (cond
  596. ((<= (point) lim-up)
  597. ;; At upward limit: if we ended at an item, store it,
  598. ;; else dismiss useless data recorded above BEG-CELL.
  599. ;; Jump to part 2.
  600. (throw 'exit
  601. (setq itm-lst
  602. (if (not (looking-at item-re))
  603. (memq (assq (car beg-cell) itm-lst) itm-lst)
  604. (setq beg-cell (cons (point) ind))
  605. (cons (funcall assoc-at-point ind) itm-lst)))))
  606. ;; Looking at a list ending regexp. Dismiss useless
  607. ;; data recorded above BEG-CELL. Jump to part 2.
  608. ((looking-at org-list-end-re)
  609. (throw 'exit
  610. (setq itm-lst
  611. (memq (assq (car beg-cell) itm-lst) itm-lst))))
  612. ;; Point is at an item. Add data to ITM-LST. It may
  613. ;; also end a previous item: save it in END-LST. If
  614. ;; ind is less or equal than BEG-CELL and there is no
  615. ;; end at this ind or lesser, this item becomes the new
  616. ;; BEG-CELL.
  617. ((looking-at item-re)
  618. (push (funcall assoc-at-point ind) itm-lst)
  619. (push (cons ind (point)) end-lst)
  620. (when (< ind text-min-ind) (setq beg-cell (cons (point) ind)))
  621. (forward-line -1))
  622. ;; Skip blocks, drawers, inline tasks, blank lines.
  623. ((and (looking-at "^[ \t]*#\\+end_")
  624. (re-search-backward "^[ \t]*#\\+begin_" lim-up t)))
  625. ((and (looking-at "^[ \t]*:END:")
  626. (re-search-backward org-drawer-regexp lim-up t))
  627. (beginning-of-line))
  628. ((and inlinetask-re (looking-at inlinetask-re))
  629. (org-inlinetask-goto-beginning)
  630. (forward-line -1))
  631. ((looking-at "^[ \t]*$")
  632. (forward-line -1))
  633. ;; From there, point is not at an item. Interpret
  634. ;; line's indentation:
  635. ;; - text at column 0 is necessarily out of any list.
  636. ;; Dismiss data recorded above BEG-CELL. Jump to
  637. ;; part 2.
  638. ;; - any other case may be an ending position for an
  639. ;; hypothetical item above. Store it and proceed.
  640. ((zerop ind)
  641. (throw 'exit
  642. (setq itm-lst
  643. (memq (assq (car beg-cell) itm-lst) itm-lst))))
  644. (t
  645. (when (< ind text-min-ind) (setq text-min-ind ind))
  646. (push (cons ind (point)) end-lst)
  647. (forward-line -1)))))))
  648. ;; 2. Read list from starting point to its end, that is until we
  649. ;; get out of context, or that a non-item line is less or
  650. ;; equally indented than BEG-CELL's cdr. Also, store ending
  651. ;; position of items in END-LST-2.
  652. (catch 'exit
  653. (while t
  654. (let ((ind (org-get-indentation)))
  655. (cond
  656. ((>= (point) lim-down)
  657. ;; At downward limit: this is de facto the end of the
  658. ;; list. Save point as an ending position, and jump to
  659. ;; part 3.
  660. (throw 'exit
  661. (push (cons 0 (funcall end-before-blank)) end-lst-2)))
  662. ;; Looking at a list ending regexp. Save point as an
  663. ;; ending position and jump to part 3.
  664. ((looking-at org-list-end-re)
  665. (throw 'exit (push (cons 0 (point)) end-lst-2)))
  666. ((looking-at item-re)
  667. ;; Point is at an item. Add data to ITM-LST-2. It may
  668. ;; also end a previous item, so save it in END-LST-2.
  669. (push (funcall assoc-at-point ind) itm-lst-2)
  670. (push (cons ind (point)) end-lst-2)
  671. (forward-line 1))
  672. ;; Skip inline tasks and blank lines along the way
  673. ((and inlinetask-re (looking-at inlinetask-re))
  674. (org-inlinetask-goto-end))
  675. ((looking-at "^[ \t]*$")
  676. (forward-line 1))
  677. ;; Ind is lesser or equal than BEG-CELL's. The list is
  678. ;; over: store point as an ending position and jump to
  679. ;; part 3.
  680. ((<= ind (cdr beg-cell))
  681. (throw 'exit
  682. (push (cons 0 (funcall end-before-blank)) end-lst-2)))
  683. ;; Else, if ind is lesser or equal than previous item's,
  684. ;; this is an ending position: store it. In any case,
  685. ;; skip block or drawer at point, and move to next line.
  686. (t
  687. (when (<= ind (nth 1 (car itm-lst-2)))
  688. (push (cons ind (point)) end-lst-2))
  689. (cond
  690. ((and (looking-at "^[ \t]*#\\+begin_")
  691. (re-search-forward "^[ \t]*#\\+end_" lim-down t)))
  692. ((and (looking-at org-drawer-regexp)
  693. (re-search-forward "^[ \t]*:END:" lim-down t))))
  694. (forward-line 1))))))
  695. (setq struct (append itm-lst (cdr (nreverse itm-lst-2)))
  696. end-lst (append end-lst (cdr (nreverse end-lst-2))))
  697. ;; 3. Associate each item to its end position.
  698. (org-list-struct-assoc-end struct end-lst)
  699. ;; 4. Return STRUCT
  700. struct)))
  701. (defun org-list-struct-assoc-end (struct end-list)
  702. "Associate proper ending point to items in STRUCT.
  703. END-LIST is a pseudo-alist where car is indentation and cdr is
  704. ending position.
  705. This function modifies STRUCT."
  706. (let ((endings end-list))
  707. (mapc
  708. (lambda (elt)
  709. (let ((pos (car elt))
  710. (ind (nth 1 elt)))
  711. ;; Remove end candidates behind current item.
  712. (while (or (<= (cdar endings) pos))
  713. (pop endings))
  714. ;; Add end position to item assoc.
  715. (let ((old-end (nthcdr 6 elt))
  716. (new-end (assoc-default ind endings '<=)))
  717. (if old-end
  718. (setcar old-end new-end)
  719. (setcdr elt (append (cdr elt) (list new-end)))))))
  720. struct)))
  721. (defun org-list-prevs-alist (struct)
  722. "Return alist between item and previous item in STRUCT."
  723. (let ((item-end-alist (mapcar (lambda (e) (cons (car e) (nth 6 e)))
  724. struct)))
  725. (mapcar (lambda (e)
  726. (let ((prev (car (rassq (car e) item-end-alist))))
  727. (cons (car e) prev)))
  728. struct)))
  729. (defun org-list-parents-alist (struct)
  730. "Return alist between item and parent in STRUCT."
  731. (let* ((ind-to-ori (list (list (nth 1 (car struct)))))
  732. (top-item (org-list-get-top-point struct))
  733. (prev-pos (list top-item)))
  734. (cons prev-pos
  735. (mapcar (lambda (item)
  736. (let ((pos (car item))
  737. (ind (nth 1 item))
  738. (prev-ind (caar ind-to-ori)))
  739. (push pos prev-pos)
  740. (cond
  741. ((> prev-ind ind)
  742. ;; A sub-list is over. Find the associated
  743. ;; origin in IND-TO-ORI. If it cannot be
  744. ;; found (ill-formed list), set its parent as
  745. ;; the first item less indented. If there is
  746. ;; none, make it a top-level item.
  747. (setq ind-to-ori
  748. (or (member (assq ind ind-to-ori) ind-to-ori)
  749. (catch 'exit
  750. (mapc
  751. (lambda (e)
  752. (when (< (car e) ind)
  753. (throw 'exit (member e ind-to-ori))))
  754. ind-to-ori)
  755. (list (list ind)))))
  756. (cons pos (cdar ind-to-ori)))
  757. ;; A sub-list starts. Every item at IND will
  758. ;; have previous item as its parent.
  759. ((< prev-ind ind)
  760. (let ((origin (nth 1 prev-pos)))
  761. (push (cons ind origin) ind-to-ori)
  762. (cons pos origin)))
  763. ;; Another item in the same sub-list: it shares
  764. ;; the same parent as the previous item.
  765. (t (cons pos (cdar ind-to-ori))))))
  766. (cdr struct)))))
  767. ;;; Accessors
  768. (defsubst org-list-get-nth (n key struct)
  769. "Return the Nth value of KEY in STRUCT."
  770. (nth n (assq key struct)))
  771. (defun org-list-set-nth (n key struct new)
  772. "Set the Nth value of KEY in STRUCT to NEW.
  773. \nThis function modifies STRUCT."
  774. (setcar (nthcdr n (assq key struct)) new))
  775. (defsubst org-list-get-ind (item struct)
  776. "Return indentation of ITEM in STRUCT."
  777. (org-list-get-nth 1 item struct))
  778. (defun org-list-set-ind (item struct ind)
  779. "Set indentation of ITEM in STRUCT to IND.
  780. \nThis function modifies STRUCT."
  781. (org-list-set-nth 1 item struct ind))
  782. (defsubst org-list-get-bullet (item struct)
  783. "Return bullet of ITEM in STRUCT."
  784. (org-list-get-nth 2 item struct))
  785. (defun org-list-set-bullet (item struct bullet)
  786. "Set bullet of ITEM in STRUCT to BULLET.
  787. \nThis function modifies STRUCT."
  788. (org-list-set-nth 2 item struct bullet))
  789. (defsubst org-list-get-counter (item struct)
  790. "Return counter of ITEM in STRUCT."
  791. (org-list-get-nth 3 item struct))
  792. (defsubst org-list-get-checkbox (item struct)
  793. "Return checkbox of ITEM in STRUCT or nil."
  794. (org-list-get-nth 4 item struct))
  795. (defun org-list-set-checkbox (item struct checkbox)
  796. "Set checkbox of ITEM in STRUCT to CHECKBOX.
  797. \nThis function modifies STRUCT."
  798. (org-list-set-nth 4 item struct checkbox))
  799. (defsubst org-list-get-tag (item struct)
  800. "Return end position of ITEM in STRUCT."
  801. (org-list-get-nth 5 item struct))
  802. (defun org-list-get-item-end (item struct)
  803. "Return end position of ITEM in STRUCT."
  804. (org-list-get-nth 6 item struct))
  805. (defun org-list-get-item-end-before-blank (item struct)
  806. "Return point at end of ITEM in STRUCT, before any blank line.
  807. Point returned is at end of line."
  808. (save-excursion
  809. (goto-char (org-list-get-item-end item struct))
  810. (skip-chars-backward " \r\t\n")
  811. (point-at-eol)))
  812. (defun org-list-get-parent (item struct parents)
  813. "Return parent of ITEM or nil.
  814. STRUCT is the list structure. PARENTS is the alist of parents,
  815. as returned by `org-list-parents-alist'."
  816. (let ((parents (or parents (org-list-parents-alist struct))))
  817. (cdr (assq item parents))))
  818. (defun org-list-has-child-p (item struct)
  819. "Non-nil if ITEM has a child.
  820. STRUCT is the list structure.
  821. Value returned is the position of the first child of ITEM."
  822. (let ((ind (org-list-get-ind item struct))
  823. (child-maybe (car (nth 1 (member (assq item struct) struct)))))
  824. (when (and child-maybe
  825. (< ind (org-list-get-ind child-maybe struct)))
  826. child-maybe)))
  827. (defun org-list-get-next-item (item _struct prevs)
  828. "Return next item in same sub-list as ITEM, or nil.
  829. STRUCT is the list structure. PREVS is the alist of previous
  830. items, as returned by `org-list-prevs-alist'."
  831. (car (rassq item prevs)))
  832. (defun org-list-get-prev-item (item _struct prevs)
  833. "Return previous item in same sub-list as ITEM, or nil.
  834. STRUCT is the list structure. PREVS is the alist of previous
  835. items, as returned by `org-list-prevs-alist'."
  836. (cdr (assq item prevs)))
  837. (defun org-list-get-subtree (item struct)
  838. "List all items having ITEM as a common ancestor, or nil.
  839. STRUCT is the list structure."
  840. (let* ((item-end (org-list-get-item-end item struct))
  841. (sub-struct (cdr (member (assq item struct) struct)))
  842. subtree)
  843. (catch 'exit
  844. (mapc (lambda (e)
  845. (let ((pos (car e)))
  846. (if (< pos item-end) (push pos subtree) (throw 'exit nil))))
  847. sub-struct))
  848. (nreverse subtree)))
  849. (defun org-list-get-all-items (item struct prevs)
  850. "List all items in the same sub-list as ITEM.
  851. STRUCT is the list structure. PREVS is the alist of previous
  852. items, as returned by `org-list-prevs-alist'."
  853. (let ((prev-item item)
  854. (next-item item)
  855. before-item after-item)
  856. (while (setq prev-item (org-list-get-prev-item prev-item struct prevs))
  857. (push prev-item before-item))
  858. (while (setq next-item (org-list-get-next-item next-item struct prevs))
  859. (push next-item after-item))
  860. (append before-item (list item) (nreverse after-item))))
  861. (defun org-list-get-children (item _struct parents)
  862. "List all children of ITEM, or nil.
  863. STRUCT is the list structure. PARENTS is the alist of parents,
  864. as returned by `org-list-parents-alist'."
  865. (let (all child)
  866. (while (setq child (car (rassq item parents)))
  867. (setq parents (cdr (member (assq child parents) parents)))
  868. (push child all))
  869. (nreverse all)))
  870. (defun org-list-get-top-point (struct)
  871. "Return point at beginning of list.
  872. STRUCT is the list structure."
  873. (caar struct))
  874. (defun org-list-get-bottom-point (struct)
  875. "Return point at bottom of list.
  876. STRUCT is the list structure."
  877. (apply #'max
  878. (mapcar (lambda (e) (org-list-get-item-end (car e) struct)) struct)))
  879. (defun org-list-get-list-begin (item struct prevs)
  880. "Return point at beginning of sub-list ITEM belongs.
  881. STRUCT is the list structure. PREVS is the alist of previous
  882. items, as returned by `org-list-prevs-alist'."
  883. (let ((first-item item) prev-item)
  884. (while (setq prev-item (org-list-get-prev-item first-item struct prevs))
  885. (setq first-item prev-item))
  886. first-item))
  887. (defalias 'org-list-get-first-item 'org-list-get-list-begin)
  888. (defun org-list-get-last-item (item struct prevs)
  889. "Return point at last item of sub-list ITEM belongs.
  890. STRUCT is the list structure. PREVS is the alist of previous
  891. items, as returned by `org-list-prevs-alist'."
  892. (let ((last-item item) next-item)
  893. (while (setq next-item (org-list-get-next-item last-item struct prevs))
  894. (setq last-item next-item))
  895. last-item))
  896. (defun org-list-get-list-end (item struct prevs)
  897. "Return point at end of sub-list ITEM belongs.
  898. STRUCT is the list structure. PREVS is the alist of previous
  899. items, as returned by `org-list-prevs-alist'."
  900. (org-list-get-item-end (org-list-get-last-item item struct prevs) struct))
  901. (defun org-list-get-list-type (item struct prevs)
  902. "Return the type of the list containing ITEM, as a symbol.
  903. STRUCT is the list structure. PREVS is the alist of previous
  904. items, as returned by `org-list-prevs-alist'.
  905. Possible types are `descriptive', `ordered' and `unordered'. The
  906. type is determined by the first item of the list."
  907. (let ((first (org-list-get-list-begin item struct prevs)))
  908. (cond
  909. ((string-match-p "[[:alnum:]]" (org-list-get-bullet first struct)) 'ordered)
  910. ((org-list-get-tag first struct) 'descriptive)
  911. (t 'unordered))))
  912. (defun org-list-get-item-number (item struct prevs parents)
  913. "Return ITEM's sequence number.
  914. STRUCT is the list structure. PREVS is the alist of previous
  915. items, as returned by `org-list-prevs-alist'. PARENTS is the
  916. alist of ancestors, as returned by `org-list-parents-alist'.
  917. Return value is a list of integers. Counters have an impact on
  918. that value."
  919. (let ((get-relative-number
  920. (function
  921. (lambda (item struct prevs)
  922. ;; Return relative sequence number of ITEM in the sub-list
  923. ;; it belongs. STRUCT is the list structure. PREVS is
  924. ;; the alist of previous items.
  925. (let ((seq 0) (pos item) counter)
  926. (while (and (not (setq counter (org-list-get-counter pos struct)))
  927. (setq pos (org-list-get-prev-item pos struct prevs)))
  928. (cl-incf seq))
  929. (if (not counter) (1+ seq)
  930. (cond
  931. ((string-match "[A-Za-z]" counter)
  932. (+ (- (string-to-char (upcase (match-string 0 counter))) 64)
  933. seq))
  934. ((string-match "[0-9]+" counter)
  935. (+ (string-to-number (match-string 0 counter)) seq))
  936. (t (1+ seq)))))))))
  937. ;; Cons each parent relative number into return value (OUT).
  938. (let ((out (list (funcall get-relative-number item struct prevs)))
  939. (parent item))
  940. (while (setq parent (org-list-get-parent parent struct parents))
  941. (push (funcall get-relative-number parent struct prevs) out))
  942. ;; Return value.
  943. out)))
  944. ;;; Searching
  945. (defun org-list-search-generic (search re bound noerr)
  946. "Search a string in valid contexts for lists.
  947. Arguments SEARCH, RE, BOUND and NOERR are similar to those used
  948. in `re-search-forward'."
  949. (catch 'exit
  950. (let ((origin (point)))
  951. (while t
  952. ;; 1. No match: return to origin or bound, depending on NOERR.
  953. (unless (funcall search re bound noerr)
  954. (throw 'exit (and (goto-char (if (memq noerr '(t nil)) origin bound))
  955. nil)))
  956. ;; 2. Match in valid context: return point. Else, continue
  957. ;; searching.
  958. (when (org-list-in-valid-context-p) (throw 'exit (point)))))))
  959. (defun org-list-search-backward (regexp &optional bound noerror)
  960. "Like `re-search-backward' but stop only where lists are recognized.
  961. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  962. `re-search-backward'."
  963. (org-list-search-generic #'re-search-backward
  964. regexp (or bound (point-min)) noerror))
  965. (defun org-list-search-forward (regexp &optional bound noerror)
  966. "Like `re-search-forward' but stop only where lists are recognized.
  967. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  968. `re-search-forward'."
  969. (org-list-search-generic #'re-search-forward
  970. regexp (or bound (point-max)) noerror))
  971. ;;; Methods on structures
  972. (defsubst org-list-bullet-string (bullet)
  973. "Return BULLET with the correct number of whitespaces.
  974. It determines the number of whitespaces to append by looking at
  975. `org-list-two-spaces-after-bullet-regexp'."
  976. (save-match-data
  977. (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
  978. (string-match
  979. org-list-two-spaces-after-bullet-regexp bullet))
  980. " "
  981. " ")))
  982. (if (string-match "\\S-+\\([ \t]*\\)" bullet)
  983. (replace-match spaces nil nil bullet 1)
  984. bullet))))
  985. (defun org-list-swap-items (beg-A beg-B struct)
  986. "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
  987. Blank lines at the end of items are left in place. Item
  988. visibility is preserved. Return the new structure after the
  989. changes.
  990. Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
  991. to the same sub-list.
  992. This function modifies STRUCT."
  993. (save-excursion
  994. (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct))
  995. (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct))
  996. (end-A (org-list-get-item-end beg-A struct))
  997. (end-B (org-list-get-item-end beg-B struct))
  998. (size-A (- end-A-no-blank beg-A))
  999. (size-B (- end-B-no-blank beg-B))
  1000. (body-A (buffer-substring beg-A end-A-no-blank))
  1001. (body-B (buffer-substring beg-B end-B-no-blank))
  1002. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B))
  1003. (sub-A (cons beg-A (org-list-get-subtree beg-A struct)))
  1004. (sub-B (cons beg-B (org-list-get-subtree beg-B struct)))
  1005. ;; Store overlays responsible for visibility status. We
  1006. ;; also need to store their boundaries as they will be
  1007. ;; removed from buffer.
  1008. (overlays
  1009. (cons
  1010. (delq nil
  1011. (mapcar (lambda (o)
  1012. (and (>= (overlay-start o) beg-A)
  1013. (<= (overlay-end o) end-A)
  1014. (list o (overlay-start o) (overlay-end o))))
  1015. (overlays-in beg-A end-A)))
  1016. (delq nil
  1017. (mapcar (lambda (o)
  1018. (and (>= (overlay-start o) beg-B)
  1019. (<= (overlay-end o) end-B)
  1020. (list o (overlay-start o) (overlay-end o))))
  1021. (overlays-in beg-B end-B))))))
  1022. ;; 1. Move effectively items in buffer.
  1023. (goto-char beg-A)
  1024. (delete-region beg-A end-B-no-blank)
  1025. (insert (concat body-B between-A-no-blank-and-B body-A))
  1026. ;; 2. Now modify struct. No need to re-read the list, the
  1027. ;; transformation is just a shift of positions. Some special
  1028. ;; attention is required for items ending at END-A and END-B
  1029. ;; as empty spaces are not moved there. In others words,
  1030. ;; item BEG-A will end with whitespaces that were at the end
  1031. ;; of BEG-B and the same applies to BEG-B.
  1032. (dolist (e struct)
  1033. (let ((pos (car e)))
  1034. (cond
  1035. ((< pos beg-A))
  1036. ((memq pos sub-A)
  1037. (let ((end-e (nth 6 e)))
  1038. (setcar e (+ pos (- end-B-no-blank end-A-no-blank)))
  1039. (setcar (nthcdr 6 e)
  1040. (+ end-e (- end-B-no-blank end-A-no-blank)))
  1041. (when (= end-e end-A) (setcar (nthcdr 6 e) end-B))))
  1042. ((memq pos sub-B)
  1043. (let ((end-e (nth 6 e)))
  1044. (setcar e (- (+ pos beg-A) beg-B))
  1045. (setcar (nthcdr 6 e) (+ end-e (- beg-A beg-B)))
  1046. (when (= end-e end-B)
  1047. (setcar (nthcdr 6 e)
  1048. (+ beg-A size-B (- end-A end-A-no-blank))))))
  1049. ((< pos beg-B)
  1050. (let ((end-e (nth 6 e)))
  1051. (setcar e (+ pos (- size-B size-A)))
  1052. (setcar (nthcdr 6 e) (+ end-e (- size-B size-A))))))))
  1053. (setq struct (sort struct #'car-less-than-car))
  1054. ;; Restore visibility status, by moving overlays to their new
  1055. ;; position.
  1056. (dolist (ov (car overlays))
  1057. (move-overlay
  1058. (car ov)
  1059. (+ (nth 1 ov) (- (+ beg-B (- size-B size-A)) beg-A))
  1060. (+ (nth 2 ov) (- (+ beg-B (- size-B size-A)) beg-A))))
  1061. (dolist (ov (cdr overlays))
  1062. (move-overlay (car ov)
  1063. (+ (nth 1 ov) (- beg-A beg-B))
  1064. (+ (nth 2 ov) (- beg-A beg-B))))
  1065. ;; Return structure.
  1066. struct)))
  1067. (defun org-list-separating-blank-lines-number (pos struct prevs)
  1068. "Return number of blank lines that should separate items in list.
  1069. POS is the position of point where `org-list-insert-item' was called.
  1070. STRUCT is the list structure. PREVS is the alist of previous
  1071. items, as returned by `org-list-prevs-alist'.
  1072. Assume point is at item's beginning. If the item is alone, apply
  1073. some heuristics to guess the result."
  1074. (save-excursion
  1075. (let ((item (point))
  1076. (insert-blank-p
  1077. (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  1078. usr-blank
  1079. (count-blanks
  1080. (function
  1081. (lambda ()
  1082. ;; Count blank lines above beginning of line.
  1083. (save-excursion
  1084. (count-lines (goto-char (point-at-bol))
  1085. (progn (skip-chars-backward " \r\t\n")
  1086. (forward-line)
  1087. (point))))))))
  1088. (cond
  1089. ;; Trivial cases where there should be none.
  1090. ((not insert-blank-p) 0)
  1091. ;; When `org-blank-before-new-entry' says so, it is 1.
  1092. ((eq insert-blank-p t) 1)
  1093. ;; `plain-list-item' is 'auto. Count blank lines separating
  1094. ;; neighbors' items in list.
  1095. (t (let ((next-p (org-list-get-next-item item struct prevs)))
  1096. (cond
  1097. ;; Is there a next item?
  1098. (next-p (goto-char next-p)
  1099. (funcall count-blanks))
  1100. ;; Is there a previous item?
  1101. ((org-list-get-prev-item item struct prevs)
  1102. (funcall count-blanks))
  1103. ;; User inserted blank lines, trust him.
  1104. ((and (> pos (org-list-get-item-end-before-blank item struct))
  1105. (> (save-excursion (goto-char pos)
  1106. (setq usr-blank (funcall count-blanks)))
  1107. 0))
  1108. usr-blank)
  1109. ;; Are there blank lines inside the list so far?
  1110. ((save-excursion
  1111. (goto-char (org-list-get-top-point struct))
  1112. ;; Do not use `org-list-search-forward' so blank lines
  1113. ;; in blocks can be counted in.
  1114. (re-search-forward
  1115. "^[ \t]*$" (org-list-get-item-end-before-blank item struct) t))
  1116. 1)
  1117. ;; Default choice: no blank line.
  1118. (t 0))))))))
  1119. (defun org-list-insert-item (pos struct prevs &optional checkbox after-bullet)
  1120. "Insert a new list item at POS and return the new structure.
  1121. If POS is before first character after bullet of the item, the
  1122. new item will be created before the current one.
  1123. STRUCT is the list structure. PREVS is the alist of previous
  1124. items, as returned by `org-list-prevs-alist'.
  1125. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  1126. after the bullet. Cursor will be after this text once the
  1127. function ends.
  1128. This function modifies STRUCT."
  1129. (let ((case-fold-search t))
  1130. ;; 1. Get information about list: position of point with regards
  1131. ;; to item start (BEFOREP), blank lines number separating items
  1132. ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
  1133. (let* ((item (progn (goto-char pos) (goto-char (org-list-get-item-begin))))
  1134. (item-end (org-list-get-item-end item struct))
  1135. (item-end-no-blank (org-list-get-item-end-before-blank item struct))
  1136. (beforep
  1137. (progn
  1138. (looking-at org-list-full-item-re)
  1139. (<= pos
  1140. (cond
  1141. ((not (match-beginning 4)) (match-end 0))
  1142. ;; Ignore tag in a non-descriptive list.
  1143. ((save-match-data (string-match "[.)]" (match-string 1)))
  1144. (match-beginning 4))
  1145. (t (save-excursion
  1146. (goto-char (match-end 4))
  1147. (skip-chars-forward " \t")
  1148. (point)))))))
  1149. (split-line-p (org-get-alist-option org-M-RET-may-split-line 'item))
  1150. (blank-nb (org-list-separating-blank-lines-number
  1151. pos struct prevs))
  1152. ;; 2. Build the new item to be created. Concatenate same
  1153. ;; bullet as item, checkbox, text AFTER-BULLET if
  1154. ;; provided, and text cut from point to end of item
  1155. ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
  1156. ;; BEFOREP and SPLIT-LINE-P. The difference of size
  1157. ;; between what was cut and what was inserted in buffer
  1158. ;; is stored in SIZE-OFFSET.
  1159. (ind (org-list-get-ind item struct))
  1160. (ind-size (if indent-tabs-mode
  1161. (+ (/ ind tab-width) (mod ind tab-width))
  1162. ind))
  1163. (bullet (org-list-bullet-string (org-list-get-bullet item struct)))
  1164. (box (when checkbox "[ ]"))
  1165. (text-cut
  1166. (and (not beforep) split-line-p
  1167. (progn
  1168. (goto-char pos)
  1169. ;; If POS is greater than ITEM-END, then point is
  1170. ;; in some white lines after the end of the list.
  1171. ;; Those must be removed, or they will be left,
  1172. ;; stacking up after the list.
  1173. (when (< item-end pos)
  1174. (delete-region (1- item-end) (point-at-eol)))
  1175. (skip-chars-backward " \r\t\n")
  1176. (setq pos (point))
  1177. (delete-and-extract-region pos item-end-no-blank))))
  1178. (body (concat bullet (when box (concat box " ")) after-bullet
  1179. (and text-cut
  1180. (if (string-match "\\`[ \t]+" text-cut)
  1181. (replace-match "" t t text-cut)
  1182. text-cut))))
  1183. (item-sep (make-string (1+ blank-nb) ?\n))
  1184. (item-size (+ ind-size (length body) (length item-sep)))
  1185. (size-offset (- item-size (length text-cut))))
  1186. ;; 4. Insert effectively item into buffer.
  1187. (goto-char item)
  1188. (indent-to-column ind)
  1189. (insert body item-sep)
  1190. ;; 5. Add new item to STRUCT.
  1191. (mapc (lambda (e)
  1192. (let ((p (car e)) (end (nth 6 e)))
  1193. (cond
  1194. ;; Before inserted item, positions don't change but
  1195. ;; an item ending after insertion has its end shifted
  1196. ;; by SIZE-OFFSET.
  1197. ((< p item)
  1198. (when (> end item) (setcar (nthcdr 6 e) (+ end size-offset))))
  1199. ;; Trivial cases where current item isn't split in
  1200. ;; two. Just shift every item after new one by
  1201. ;; ITEM-SIZE.
  1202. ((or beforep (not split-line-p))
  1203. (setcar e (+ p item-size))
  1204. (setcar (nthcdr 6 e) (+ end item-size)))
  1205. ;; Item is split in two: elements before POS are just
  1206. ;; shifted by ITEM-SIZE. In the case item would end
  1207. ;; after split POS, ending is only shifted by
  1208. ;; SIZE-OFFSET.
  1209. ((< p pos)
  1210. (setcar e (+ p item-size))
  1211. (if (< end pos)
  1212. (setcar (nthcdr 6 e) (+ end item-size))
  1213. (setcar (nthcdr 6 e) (+ end size-offset))))
  1214. ;; Elements after POS are moved into new item.
  1215. ;; Length of ITEM-SEP has to be removed as ITEM-SEP
  1216. ;; doesn't appear in buffer yet.
  1217. ((< p item-end)
  1218. (setcar e (+ p size-offset (- item pos (length item-sep))))
  1219. (if (= end item-end)
  1220. (setcar (nthcdr 6 e) (+ item item-size))
  1221. (setcar (nthcdr 6 e)
  1222. (+ end size-offset
  1223. (- item pos (length item-sep))))))
  1224. ;; Elements at ITEM-END or after are only shifted by
  1225. ;; SIZE-OFFSET.
  1226. (t (setcar e (+ p size-offset))
  1227. (setcar (nthcdr 6 e) (+ end size-offset))))))
  1228. struct)
  1229. (push (list item ind bullet nil box nil (+ item item-size)) struct)
  1230. (setq struct (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))
  1231. ;; 6. If not BEFOREP, new item must appear after ITEM, so
  1232. ;; exchange ITEM with the next item in list. Position cursor
  1233. ;; after bullet, counter, checkbox, and label.
  1234. (if beforep
  1235. (goto-char item)
  1236. (setq struct (org-list-swap-items item (+ item item-size) struct))
  1237. (goto-char (org-list-get-next-item
  1238. item struct (org-list-prevs-alist struct))))
  1239. struct)))
  1240. (defun org-list-delete-item (item struct)
  1241. "Remove ITEM from the list and return the new structure.
  1242. STRUCT is the list structure."
  1243. (let* ((end (org-list-get-item-end item struct))
  1244. (beg (if (= (org-list-get-bottom-point struct) end)
  1245. ;; If ITEM ends with the list, delete blank lines
  1246. ;; before it.
  1247. (save-excursion
  1248. (goto-char item)
  1249. (skip-chars-backward " \r\t\n")
  1250. (min (1+ (point-at-eol)) (point-max)))
  1251. item)))
  1252. ;; Remove item from buffer.
  1253. (delete-region beg end)
  1254. ;; Remove item from structure and shift others items accordingly.
  1255. ;; Don't forget to shift also ending position when appropriate.
  1256. (let ((size (- end beg)))
  1257. (delq nil (mapcar (lambda (e)
  1258. (let ((pos (car e)))
  1259. (cond
  1260. ((< pos item)
  1261. (let ((end-e (nth 6 e)))
  1262. (cond
  1263. ((< end-e item) e)
  1264. ((= end-e item)
  1265. (append (butlast e) (list beg)))
  1266. (t
  1267. (append (butlast e) (list (- end-e size)))))))
  1268. ((< pos end) nil)
  1269. (t
  1270. (cons (- pos size)
  1271. (append (butlast (cdr e))
  1272. (list (- (nth 6 e) size))))))))
  1273. struct)))))
  1274. (defun org-list-send-item (item dest struct)
  1275. "Send ITEM to destination DEST.
  1276. STRUCT is the list structure.
  1277. DEST can have various values.
  1278. If DEST is a buffer position, the function will assume it points
  1279. to another item in the same list as ITEM, and will move the
  1280. latter just before the former.
  1281. If DEST is `begin' (respectively `end'), ITEM will be moved at
  1282. the beginning (respectively end) of the list it belongs to.
  1283. If DEST is a string like \"N\", where N is an integer, ITEM will
  1284. be moved at the Nth position in the list.
  1285. If DEST is `kill', ITEM will be deleted and its body will be
  1286. added to the kill-ring.
  1287. If DEST is `delete', ITEM will be deleted.
  1288. Visibility of item is preserved.
  1289. This function returns, destructively, the new list structure."
  1290. (let* ((prevs (org-list-prevs-alist struct))
  1291. (item-end (org-list-get-item-end item struct))
  1292. ;; Grab full item body minus its bullet.
  1293. (body (org-trim
  1294. (buffer-substring
  1295. (save-excursion
  1296. (goto-char item)
  1297. (looking-at
  1298. (concat "[ \t]*"
  1299. (regexp-quote (org-list-get-bullet item struct))))
  1300. (match-end 0))
  1301. item-end)))
  1302. ;; Change DEST into a buffer position. A trick is needed
  1303. ;; when ITEM is meant to be sent at the end of the list.
  1304. ;; Indeed, by setting locally `org-M-RET-may-split-line' to
  1305. ;; nil and insertion point (INS-POINT) to the first line's
  1306. ;; end of the last item, we ensure the new item will be
  1307. ;; inserted after the last item, and not after any of its
  1308. ;; hypothetical sub-items.
  1309. (ins-point (cond
  1310. ((or (eq dest 'kill) (eq dest 'delete)))
  1311. ((eq dest 'begin)
  1312. (setq dest (org-list-get-list-begin item struct prevs)))
  1313. ((eq dest 'end)
  1314. (setq dest (org-list-get-list-end item struct prevs))
  1315. (save-excursion
  1316. (goto-char (org-list-get-last-item item struct prevs))
  1317. (point-at-eol)))
  1318. ((string-match-p "\\`[0-9]+\\'" dest)
  1319. (let* ((all (org-list-get-all-items item struct prevs))
  1320. (len (length all))
  1321. (index (mod (string-to-number dest) len)))
  1322. (if (not (zerop index))
  1323. (setq dest (nth (1- index) all))
  1324. ;; Send ITEM at the end of the list.
  1325. (setq dest (org-list-get-list-end item struct prevs))
  1326. (save-excursion
  1327. (goto-char
  1328. (org-list-get-last-item item struct prevs))
  1329. (point-at-eol)))))
  1330. (t dest)))
  1331. (org-M-RET-may-split-line nil)
  1332. ;; Store inner overlays (to preserve visibility).
  1333. (overlays (cl-remove-if (lambda (o) (or (< (overlay-start o) item)
  1334. (> (overlay-end o) item)))
  1335. (overlays-in item item-end))))
  1336. (cond
  1337. ((eq dest 'delete) (org-list-delete-item item struct))
  1338. ((eq dest 'kill)
  1339. (kill-new body)
  1340. (org-list-delete-item item struct))
  1341. ((and (integerp dest) (/= item ins-point))
  1342. (setq item (copy-marker item))
  1343. (setq struct (org-list-insert-item ins-point struct prevs nil body))
  1344. ;; 1. Structure returned by `org-list-insert-item' may not be
  1345. ;; accurate, as it cannot see sub-items included in BODY.
  1346. ;; Thus, first compute the real structure so far.
  1347. (let ((moved-items
  1348. (cons (marker-position item)
  1349. (org-list-get-subtree (marker-position item) struct)))
  1350. (new-end (org-list-get-item-end (point) struct))
  1351. (old-end (org-list-get-item-end (marker-position item) struct))
  1352. (new-item (point))
  1353. (shift (- (point) item)))
  1354. ;; 1.1. Remove the item just created in structure.
  1355. (setq struct (delete (assq new-item struct) struct))
  1356. ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
  1357. (setq struct (sort
  1358. (append
  1359. struct
  1360. (mapcar (lambda (e)
  1361. (let* ((cell (assq e struct))
  1362. (pos (car cell))
  1363. (end (nth 6 cell)))
  1364. (cons (+ pos shift)
  1365. (append (butlast (cdr cell))
  1366. (list (if (= end old-end)
  1367. new-end
  1368. (+ end shift)))))))
  1369. moved-items))
  1370. #'car-less-than-car)))
  1371. ;; 2. Restore inner overlays.
  1372. (dolist (o overlays)
  1373. (move-overlay o
  1374. (+ (overlay-start o) (- (point) item))
  1375. (+ (overlay-end o) (- (point) item))))
  1376. ;; 3. Eventually delete extra copy of the item and clean marker.
  1377. (prog1 (org-list-delete-item (marker-position item) struct)
  1378. (move-marker item nil)))
  1379. (t struct))))
  1380. (defun org-list-struct-outdent (start end struct parents)
  1381. "Outdent items between positions START and END.
  1382. STRUCT is the list structure. PARENTS is the alist of items'
  1383. parents, as returned by `org-list-parents-alist'.
  1384. START is included, END excluded."
  1385. (let* (acc
  1386. (out (lambda (cell)
  1387. (let* ((item (car cell))
  1388. (parent (cdr cell)))
  1389. (cond
  1390. ;; Item not yet in zone: keep association.
  1391. ((< item start) cell)
  1392. ;; Item out of zone: follow associations in ACC.
  1393. ((>= item end)
  1394. (let ((convert (and parent (assq parent acc))))
  1395. (if convert (cons item (cdr convert)) cell)))
  1396. ;; Item has no parent: error
  1397. ((not parent)
  1398. (error "Cannot outdent top-level items"))
  1399. ;; Parent is outdented: keep association.
  1400. ((>= parent start)
  1401. (push (cons parent item) acc) cell)
  1402. (t
  1403. ;; Parent isn't outdented: reparent to grand-parent.
  1404. (let ((grand-parent (org-list-get-parent
  1405. parent struct parents)))
  1406. (push (cons parent item) acc)
  1407. (cons item grand-parent))))))))
  1408. (mapcar out parents)))
  1409. (defun org-list-struct-indent (start end struct parents prevs)
  1410. "Indent items between positions START and END.
  1411. STRUCT is the list structure. PARENTS is the alist of parents
  1412. and PREVS is the alist of previous items, returned by,
  1413. respectively, `org-list-parents-alist' and
  1414. `org-list-prevs-alist'.
  1415. START is included and END excluded.
  1416. STRUCT may be modified if `org-list-demote-modify-bullet' matches
  1417. bullets between START and END."
  1418. (let* (acc
  1419. (set-assoc (lambda (cell) (push cell acc) cell))
  1420. (change-bullet-maybe
  1421. (function
  1422. (lambda (item)
  1423. (let ((new-bul-p
  1424. (cdr (assoc
  1425. ;; Normalize ordered bullets.
  1426. (let ((bul (org-trim
  1427. (org-list-get-bullet item struct))))
  1428. (cond ((string-match "[A-Z]\\." bul) "A.")
  1429. ((string-match "[A-Z])" bul) "A)")
  1430. ((string-match "[a-z]\\." bul) "a.")
  1431. ((string-match "[a-z])" bul) "a)")
  1432. ((string-match "[0-9]\\." bul) "1.")
  1433. ((string-match "[0-9])" bul) "1)")
  1434. (t bul)))
  1435. org-list-demote-modify-bullet))))
  1436. (when new-bul-p (org-list-set-bullet item struct new-bul-p))))))
  1437. (ind
  1438. (lambda (cell)
  1439. (let* ((item (car cell))
  1440. (parent (cdr cell)))
  1441. (cond
  1442. ;; Item not yet in zone: keep association.
  1443. ((< item start) cell)
  1444. ((>= item end)
  1445. ;; Item out of zone: follow associations in ACC.
  1446. (let ((convert (assq parent acc)))
  1447. (if convert (cons item (cdr convert)) cell)))
  1448. (t
  1449. ;; Item is in zone...
  1450. (let ((prev (org-list-get-prev-item item struct prevs)))
  1451. ;; Check if bullet needs to be changed.
  1452. (funcall change-bullet-maybe item)
  1453. (cond
  1454. ;; First item indented but not parent: error
  1455. ((and (not prev) (< parent start))
  1456. (error "Cannot indent the first item of a list"))
  1457. ;; First item and parent indented: keep same
  1458. ;; parent.
  1459. ((not prev) (funcall set-assoc cell))
  1460. ;; Previous item not indented: reparent to it.
  1461. ((< prev start) (funcall set-assoc (cons item prev)))
  1462. ;; Previous item indented: reparent like it.
  1463. (t
  1464. (funcall set-assoc
  1465. (cons item (cdr (assq prev acc)))))))))))))
  1466. (mapcar ind parents)))
  1467. ;;; Repairing structures
  1468. (defun org-list-use-alpha-bul-p (first struct prevs)
  1469. "Non-nil if list starting at FIRST can have alphabetical bullets.
  1470. STRUCT is list structure. PREVS is the alist of previous items,
  1471. as returned by `org-list-prevs-alist'."
  1472. (and org-list-allow-alphabetical
  1473. (catch 'exit
  1474. (let ((item first) (ascii 64) (case-fold-search nil))
  1475. ;; Pretend that bullets are uppercase and check if alphabet
  1476. ;; is sufficient, taking counters into account.
  1477. (while item
  1478. (let ((count (org-list-get-counter item struct)))
  1479. ;; Virtually determine current bullet
  1480. (if (and count (string-match-p "[a-zA-Z]" count))
  1481. ;; Counters are not case-sensitive.
  1482. (setq ascii (string-to-char (upcase count)))
  1483. (setq ascii (1+ ascii)))
  1484. ;; Test if bullet would be over z or Z.
  1485. (if (> ascii 90)
  1486. (throw 'exit nil)
  1487. (setq item (org-list-get-next-item item struct prevs)))))
  1488. ;; All items checked. All good.
  1489. t))))
  1490. (defun org-list-inc-bullet-maybe (bullet)
  1491. "Increment BULLET if applicable."
  1492. (let ((case-fold-search nil))
  1493. (cond
  1494. ;; Num bullet: increment it.
  1495. ((string-match "[0-9]+" bullet)
  1496. (replace-match
  1497. (number-to-string (1+ (string-to-number (match-string 0 bullet))))
  1498. nil nil bullet))
  1499. ;; Alpha bullet: increment it.
  1500. ((string-match "[A-Za-z]" bullet)
  1501. (replace-match
  1502. (char-to-string (1+ (string-to-char (match-string 0 bullet))))
  1503. nil nil bullet))
  1504. ;; Unordered bullet: leave it.
  1505. (t bullet))))
  1506. (defun org-list-struct-fix-bul (struct prevs)
  1507. "Verify and correct bullets in STRUCT.
  1508. PREVS is the alist of previous items, as returned by
  1509. `org-list-prevs-alist'.
  1510. This function modifies STRUCT."
  1511. (let ((case-fold-search nil)
  1512. (fix-bul
  1513. (function
  1514. ;; Set bullet of ITEM in STRUCT, depending on the type of
  1515. ;; first item of the list, the previous bullet and counter
  1516. ;; if any.
  1517. (lambda (item)
  1518. (let* ((prev (org-list-get-prev-item item struct prevs))
  1519. (prev-bul (and prev (org-list-get-bullet prev struct)))
  1520. (counter (org-list-get-counter item struct))
  1521. (bullet (org-list-get-bullet item struct))
  1522. (alphap (and (not prev)
  1523. (org-list-use-alpha-bul-p item struct prevs))))
  1524. (org-list-set-bullet
  1525. item struct
  1526. (org-list-bullet-string
  1527. (cond
  1528. ;; Alpha counter in alpha list: use counter.
  1529. ((and prev counter
  1530. (string-match "[a-zA-Z]" counter)
  1531. (string-match "[a-zA-Z]" prev-bul))
  1532. ;; Use cond to be sure `string-match' is used in
  1533. ;; both cases.
  1534. (let ((real-count
  1535. (cond
  1536. ((string-match "[a-z]" prev-bul) (downcase counter))
  1537. ((string-match "[A-Z]" prev-bul) (upcase counter)))))
  1538. (replace-match real-count nil nil prev-bul)))
  1539. ;; Num counter in a num list: use counter.
  1540. ((and prev counter
  1541. (string-match "[0-9]+" counter)
  1542. (string-match "[0-9]+" prev-bul))
  1543. (replace-match counter nil nil prev-bul))
  1544. ;; No counter: increase, if needed, previous bullet.
  1545. (prev
  1546. (org-list-inc-bullet-maybe (org-list-get-bullet prev struct)))
  1547. ;; Alpha counter at first item: use counter.
  1548. ((and counter (org-list-use-alpha-bul-p item struct prevs)
  1549. (string-match "[A-Za-z]" counter)
  1550. (string-match "[A-Za-z]" bullet))
  1551. (let ((real-count
  1552. (cond
  1553. ((string-match "[a-z]" bullet) (downcase counter))
  1554. ((string-match "[A-Z]" bullet) (upcase counter)))))
  1555. (replace-match real-count nil nil bullet)))
  1556. ;; Num counter at first item: use counter.
  1557. ((and counter
  1558. (string-match "[0-9]+" counter)
  1559. (string-match "[0-9]+" bullet))
  1560. (replace-match counter nil nil bullet))
  1561. ;; First bullet is alpha uppercase: use "A".
  1562. ((and alphap (string-match "[A-Z]" bullet))
  1563. (replace-match "A" nil nil bullet))
  1564. ;; First bullet is alpha lowercase: use "a".
  1565. ((and alphap (string-match "[a-z]" bullet))
  1566. (replace-match "a" nil nil bullet))
  1567. ;; First bullet is num: use "1".
  1568. ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet)
  1569. (replace-match "1" nil nil bullet))
  1570. ;; Not an ordered list: keep bullet.
  1571. (t bullet)))))))))
  1572. (mapc fix-bul (mapcar #'car struct))))
  1573. (defun org-list-struct-fix-ind (struct parents &optional bullet-size)
  1574. "Verify and correct indentation in STRUCT.
  1575. PARENTS is the alist of parents, as returned by
  1576. `org-list-parents-alist'.
  1577. If numeric optional argument BULLET-SIZE is set, assume all
  1578. bullets in list have this length to determine new indentation.
  1579. This function modifies STRUCT."
  1580. (let* ((ancestor (org-list-get-top-point struct))
  1581. (top-ind (org-list-get-ind ancestor struct))
  1582. (new-ind
  1583. (lambda (item)
  1584. (let ((parent (org-list-get-parent item struct parents)))
  1585. (if parent
  1586. ;; Indent like parent + length of parent's bullet +
  1587. ;; sub-list offset.
  1588. (org-list-set-ind
  1589. item struct (+ (or bullet-size
  1590. (length
  1591. (org-list-get-bullet parent struct)))
  1592. (org-list-get-ind parent struct)
  1593. org-list-indent-offset))
  1594. ;; If no parent, indent like top-point.
  1595. (org-list-set-ind item struct top-ind))))))
  1596. (mapc new-ind (mapcar #'car (cdr struct)))))
  1597. (defun org-list-struct-fix-box (struct parents prevs &optional ordered)
  1598. "Verify and correct checkboxes in STRUCT.
  1599. PARENTS is the alist of parents and PREVS is the alist of
  1600. previous items, as returned by, respectively,
  1601. `org-list-parents-alist' and `org-list-prevs-alist'.
  1602. If ORDERED is non-nil, a checkbox can only be checked when every
  1603. checkbox before it is checked too. If there was an attempt to
  1604. break this rule, the function will return the blocking item. In
  1605. all others cases, the return value will be nil.
  1606. This function modifies STRUCT."
  1607. (let ((all-items (mapcar #'car struct))
  1608. (set-parent-box
  1609. (function
  1610. (lambda (item)
  1611. (let* ((box-list
  1612. (mapcar (lambda (child)
  1613. (org-list-get-checkbox child struct))
  1614. (org-list-get-children item struct parents))))
  1615. (org-list-set-checkbox
  1616. item struct
  1617. (cond
  1618. ((and (member "[ ]" box-list) (member "[X]" box-list)) "[-]")
  1619. ((member "[-]" box-list) "[-]")
  1620. ((member "[X]" box-list) "[X]")
  1621. ((member "[ ]" box-list) "[ ]")
  1622. ;; Parent has no boxed child: leave box as-is.
  1623. (t (org-list-get-checkbox item struct))))))))
  1624. parent-list)
  1625. ;; 1. List all parents with a checkbox.
  1626. (mapc
  1627. (lambda (e)
  1628. (let* ((parent (org-list-get-parent e struct parents))
  1629. (parent-box-p (org-list-get-checkbox parent struct)))
  1630. (when (and parent-box-p (not (memq parent parent-list)))
  1631. (push parent parent-list))))
  1632. all-items)
  1633. ;; 2. Sort those parents by decreasing indentation.
  1634. (setq parent-list (sort parent-list
  1635. (lambda (e1 e2)
  1636. (> (org-list-get-ind e1 struct)
  1637. (org-list-get-ind e2 struct)))))
  1638. ;; 3. For each parent, get all children's checkboxes to determine
  1639. ;; and set its checkbox accordingly.
  1640. (mapc set-parent-box parent-list)
  1641. ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
  1642. (when ordered
  1643. (let* ((box-list
  1644. (mapcar (lambda (e) (org-list-get-checkbox e struct)) all-items))
  1645. (after-unchecked (member "[ ]" box-list)))
  1646. ;; There are boxes checked after an unchecked one: fix that.
  1647. (when (member "[X]" after-unchecked)
  1648. (let ((index (- (length struct) (length after-unchecked))))
  1649. (mapc (lambda (e)
  1650. (when (org-list-get-checkbox e struct)
  1651. (org-list-set-checkbox e struct "[ ]")))
  1652. (nthcdr index all-items))
  1653. ;; Verify once again the structure, without ORDERED.
  1654. (org-list-struct-fix-box struct parents prevs nil)
  1655. ;; Return blocking item.
  1656. (nth index all-items)))))))
  1657. (defun org-list-struct-fix-item-end (struct)
  1658. "Verify and correct each item end position in STRUCT.
  1659. This function modifies STRUCT."
  1660. (let (end-list acc-end)
  1661. (mapc (lambda (e)
  1662. (let* ((pos (car e))
  1663. (ind-pos (org-list-get-ind pos struct))
  1664. (end-pos (org-list-get-item-end pos struct)))
  1665. (unless (assq end-pos struct)
  1666. ;; To determine real ind of an ending position that is
  1667. ;; not at an item, we have to find the item it belongs
  1668. ;; to: it is the last item (ITEM-UP), whose ending is
  1669. ;; further than the position we're interested in.
  1670. (let ((item-up (assoc-default end-pos acc-end '>)))
  1671. (push (cons
  1672. ;; Else part is for the bottom point.
  1673. (if item-up (+ (org-list-get-ind item-up struct) 2) 0)
  1674. end-pos)
  1675. end-list)))
  1676. (push (cons ind-pos pos) end-list)
  1677. (push (cons end-pos pos) acc-end)))
  1678. struct)
  1679. (setq end-list (sort end-list (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
  1680. (org-list-struct-assoc-end struct end-list)))
  1681. (defun org-list-struct-apply-struct (struct old-struct)
  1682. "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
  1683. OLD-STRUCT is the structure before any modifications, and STRUCT
  1684. the structure to be applied. The function will only modify parts
  1685. of the list which have changed.
  1686. Initial position of cursor is restored after the changes."
  1687. (let* ((origin (point-marker))
  1688. (inlinetask-re (and (featurep 'org-inlinetask)
  1689. (org-inlinetask-outline-regexp)))
  1690. (item-re (org-item-re))
  1691. (shift-body-ind
  1692. (function
  1693. ;; Shift the indentation between END and BEG by DELTA.
  1694. ;; Start from the line before END.
  1695. (lambda (end beg delta)
  1696. (goto-char end)
  1697. (skip-chars-backward " \r\t\n")
  1698. (beginning-of-line)
  1699. (while (or (> (point) beg)
  1700. (and (= (point) beg)
  1701. (not (looking-at item-re))))
  1702. (cond
  1703. ;; Skip inline tasks.
  1704. ((and inlinetask-re (looking-at inlinetask-re))
  1705. (org-inlinetask-goto-beginning))
  1706. ;; Shift only non-empty lines.
  1707. ((looking-at-p "^[ \t]*\\S-")
  1708. (indent-line-to (+ (org-get-indentation) delta))))
  1709. (forward-line -1)))))
  1710. (modify-item
  1711. (function
  1712. ;; Replace ITEM first line elements with new elements from
  1713. ;; STRUCT, if appropriate.
  1714. (lambda (item)
  1715. (goto-char item)
  1716. (let* ((new-ind (org-list-get-ind item struct))
  1717. (old-ind (org-get-indentation))
  1718. (new-bul (org-list-bullet-string
  1719. (org-list-get-bullet item struct)))
  1720. (old-bul (org-list-get-bullet item old-struct))
  1721. (new-box (org-list-get-checkbox item struct)))
  1722. (looking-at org-list-full-item-re)
  1723. ;; a. Replace bullet
  1724. (unless (equal old-bul new-bul)
  1725. (replace-match new-bul nil nil nil 1))
  1726. ;; b. Replace checkbox.
  1727. (cond
  1728. ((equal (match-string 3) new-box))
  1729. ((and (match-string 3) new-box)
  1730. (replace-match new-box nil nil nil 3))
  1731. ((match-string 3)
  1732. (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
  1733. (replace-match "" nil nil nil 1))
  1734. (t (let ((counterp (match-end 2)))
  1735. (goto-char (if counterp (1+ counterp) (match-end 1)))
  1736. (insert (concat new-box (unless counterp " "))))))
  1737. ;; c. Indent item to appropriate column.
  1738. (unless (= new-ind old-ind)
  1739. (delete-region (goto-char (point-at-bol))
  1740. (progn (skip-chars-forward " \t") (point)))
  1741. (indent-to new-ind)))))))
  1742. ;; 1. First get list of items and position endings. We maintain
  1743. ;; two alists: ITM-SHIFT, determining indentation shift needed
  1744. ;; at item, and END-LIST, a pseudo-alist where key is ending
  1745. ;; position and value point.
  1746. (let (end-list acc-end itm-shift all-ends sliced-struct)
  1747. (dolist (e old-struct)
  1748. (let* ((pos (car e))
  1749. (ind-pos (org-list-get-ind pos struct))
  1750. (ind-old (org-list-get-ind pos old-struct))
  1751. (bul-pos (org-list-get-bullet pos struct))
  1752. (bul-old (org-list-get-bullet pos old-struct))
  1753. (ind-shift (- (+ ind-pos (length bul-pos))
  1754. (+ ind-old (length bul-old))))
  1755. (end-pos (org-list-get-item-end pos old-struct)))
  1756. (push (cons pos ind-shift) itm-shift)
  1757. (unless (assq end-pos old-struct)
  1758. ;; To determine real ind of an ending position that
  1759. ;; is not at an item, we have to find the item it
  1760. ;; belongs to: it is the last item (ITEM-UP), whose
  1761. ;; ending is further than the position we're
  1762. ;; interested in.
  1763. (let ((item-up (assoc-default end-pos acc-end #'>)))
  1764. (push (cons end-pos item-up) end-list)))
  1765. (push (cons end-pos pos) acc-end)))
  1766. ;; 2. Slice the items into parts that should be shifted by the
  1767. ;; same amount of indentation. Each slice follow the pattern
  1768. ;; (END BEG DELTA). Slices are returned in reverse order.
  1769. (setq all-ends (sort (append (mapcar #'car itm-shift)
  1770. (org-uniquify (mapcar #'car end-list)))
  1771. #'<)
  1772. acc-end (nreverse acc-end))
  1773. (while (cdr all-ends)
  1774. (let* ((up (pop all-ends))
  1775. (down (car all-ends))
  1776. (itemp (assq up struct))
  1777. (delta
  1778. (if itemp (cdr (assq up itm-shift))
  1779. ;; If we're not at an item, there's a child of the
  1780. ;; item point belongs to above. Make sure the less
  1781. ;; indented line in this slice has the same column
  1782. ;; as that child.
  1783. (let* ((child (cdr (assq up acc-end)))
  1784. (ind (org-list-get-ind child struct))
  1785. (min-ind most-positive-fixnum))
  1786. (save-excursion
  1787. (goto-char up)
  1788. (while (< (point) down)
  1789. ;; Ignore empty lines. Also ignore blocks and
  1790. ;; drawers contents.
  1791. (unless (looking-at-p "[ \t]*$")
  1792. (setq min-ind (min (org-get-indentation) min-ind))
  1793. (cond
  1794. ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
  1795. (re-search-forward
  1796. (format "^[ \t]*#\\+END%s[ \t]*$"
  1797. (match-string 1))
  1798. down t)))
  1799. ((and (looking-at org-drawer-regexp)
  1800. (re-search-forward "^[ \t]*:END:[ \t]*$"
  1801. down t)))))
  1802. (forward-line)))
  1803. (- ind min-ind)))))
  1804. (push (list down up delta) sliced-struct)))
  1805. ;; 3. Shift each slice in buffer, provided delta isn't 0, from
  1806. ;; end to beginning. Take a special action when beginning is
  1807. ;; at item bullet.
  1808. (dolist (e sliced-struct)
  1809. (unless (zerop (nth 2 e)) (apply shift-body-ind e))
  1810. (let* ((beg (nth 1 e))
  1811. (cell (assq beg struct)))
  1812. (unless (or (not cell) (equal cell (assq beg old-struct)))
  1813. (funcall modify-item beg)))))
  1814. ;; 4. Go back to initial position and clean marker.
  1815. (goto-char origin)
  1816. (move-marker origin nil)))
  1817. (defun org-list-write-struct (struct parents &optional old-struct)
  1818. "Correct bullets, checkboxes and indentation in list at point.
  1819. STRUCT is the list structure. PARENTS is the alist of parents,
  1820. as returned by `org-list-parents-alist'.
  1821. When non-nil, optional argument OLD-STRUCT is the reference
  1822. structure of the list. It should be provided whenever STRUCT
  1823. doesn't correspond anymore to the real list in buffer."
  1824. ;; Order of functions matters here: checkboxes and endings need
  1825. ;; correct indentation to be set, and indentation needs correct
  1826. ;; bullets.
  1827. ;;
  1828. ;; 0. Save a copy of structure before modifications
  1829. (let ((old-struct (or old-struct (copy-tree struct))))
  1830. ;; 1. Set a temporary, but coherent with PARENTS, indentation in
  1831. ;; order to get items endings and bullets properly
  1832. (org-list-struct-fix-ind struct parents 2)
  1833. ;; 2. Fix each item end to get correct prevs alist.
  1834. (org-list-struct-fix-item-end struct)
  1835. ;; 3. Get bullets right.
  1836. (let ((prevs (org-list-prevs-alist struct)))
  1837. (org-list-struct-fix-bul struct prevs)
  1838. ;; 4. Now get real indentation.
  1839. (org-list-struct-fix-ind struct parents)
  1840. ;; 5. Eventually fix checkboxes.
  1841. (org-list-struct-fix-box struct parents prevs))
  1842. ;; 6. Apply structure modifications to buffer.
  1843. (org-list-struct-apply-struct struct old-struct)))
  1844. ;;; Misc Tools
  1845. (defun org-apply-on-list (function init-value &rest args)
  1846. "Call FUNCTION on each item of the list at point.
  1847. FUNCTION must be called with at least one argument: INIT-VALUE,
  1848. that will contain the value returned by the function at the
  1849. previous item, plus ARGS extra arguments.
  1850. FUNCTION is applied on items in reverse order.
  1851. As an example, \(org-apply-on-list \(lambda \(result) \(1+ result)) 0)
  1852. will return the number of items in the current list.
  1853. Sublists of the list are skipped. Cursor is always at the
  1854. beginning of the item."
  1855. (let* ((struct (org-list-struct))
  1856. (prevs (org-list-prevs-alist struct))
  1857. (item (copy-marker (point-at-bol)))
  1858. (all (org-list-get-all-items (marker-position item) struct prevs))
  1859. (value init-value))
  1860. (mapc (lambda (e)
  1861. (goto-char e)
  1862. (setq value (apply function value args)))
  1863. (nreverse all))
  1864. (goto-char item)
  1865. (move-marker item nil)
  1866. value))
  1867. (defun org-list-set-item-visibility (item struct view)
  1868. "Set visibility of ITEM in STRUCT to VIEW.
  1869. Possible values are: `folded', `children' or `subtree'. See
  1870. `org-cycle' for more information."
  1871. (cond
  1872. ((eq view 'folded)
  1873. (let ((item-end (org-list-get-item-end-before-blank item struct)))
  1874. ;; Hide from eol
  1875. (outline-flag-region (save-excursion (goto-char item) (point-at-eol))
  1876. item-end t)))
  1877. ((eq view 'children)
  1878. ;; First show everything.
  1879. (org-list-set-item-visibility item struct 'subtree)
  1880. ;; Then fold every child.
  1881. (let* ((parents (org-list-parents-alist struct))
  1882. (children (org-list-get-children item struct parents)))
  1883. (mapc (lambda (e)
  1884. (org-list-set-item-visibility e struct 'folded))
  1885. children)))
  1886. ((eq view 'subtree)
  1887. ;; Show everything
  1888. (let ((item-end (org-list-get-item-end item struct)))
  1889. (outline-flag-region item item-end nil)))))
  1890. (defun org-list-item-body-column (item)
  1891. "Return column at which body of ITEM should start."
  1892. (save-excursion
  1893. (goto-char item)
  1894. (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?\\([ \t]+\\|$\\)")
  1895. (if (match-beginning 2)
  1896. (let ((start (1+ (match-end 2)))
  1897. (ind (org-get-indentation)))
  1898. (if (> start (+ ind org-list-description-max-indent)) (+ ind 5) start))
  1899. (+ (progn (goto-char (match-end 1)) (current-column))
  1900. (if (and org-list-two-spaces-after-bullet-regexp
  1901. (string-match-p org-list-two-spaces-after-bullet-regexp
  1902. (match-string 1)))
  1903. 2
  1904. 1)))))
  1905. ;;; Interactive functions
  1906. (defalias 'org-list-get-item-begin 'org-in-item-p)
  1907. (defun org-beginning-of-item ()
  1908. "Go to the beginning of the current item.
  1909. Throw an error when not in a list."
  1910. (interactive)
  1911. (let ((begin (org-in-item-p)))
  1912. (if begin (goto-char begin) (error "Not in an item"))))
  1913. (defun org-beginning-of-item-list ()
  1914. "Go to the beginning item of the current list or sublist.
  1915. Throw an error when not in a list."
  1916. (interactive)
  1917. (let ((begin (org-in-item-p)))
  1918. (if (not begin)
  1919. (error "Not in an item")
  1920. (goto-char begin)
  1921. (let* ((struct (org-list-struct))
  1922. (prevs (org-list-prevs-alist struct)))
  1923. (goto-char (org-list-get-list-begin begin struct prevs))))))
  1924. (defun org-end-of-item-list ()
  1925. "Go to the end of the current list or sublist.
  1926. Throw an error when not in a list."
  1927. (interactive)
  1928. (let ((begin (org-in-item-p)))
  1929. (if (not begin)
  1930. (error "Not in an item")
  1931. (goto-char begin)
  1932. (let* ((struct (org-list-struct))
  1933. (prevs (org-list-prevs-alist struct)))
  1934. (goto-char (org-list-get-list-end begin struct prevs))))))
  1935. (defun org-end-of-item ()
  1936. "Go to the end of the current item.
  1937. Throw an error when not in a list."
  1938. (interactive)
  1939. (let ((begin (org-in-item-p)))
  1940. (if (not begin)
  1941. (error "Not in an item")
  1942. (goto-char begin)
  1943. (let ((struct (org-list-struct)))
  1944. (goto-char (org-list-get-item-end begin struct))))))
  1945. (defun org-previous-item ()
  1946. "Move to the beginning of the previous item.
  1947. Throw an error when not in a list. Also throw an error when at
  1948. first item, unless `org-list-use-circular-motion' is non-nil."
  1949. (interactive)
  1950. (let ((item (org-in-item-p)))
  1951. (if (not item)
  1952. (error "Not in an item")
  1953. (goto-char item)
  1954. (let* ((struct (org-list-struct))
  1955. (prevs (org-list-prevs-alist struct))
  1956. (prevp (org-list-get-prev-item item struct prevs)))
  1957. (cond
  1958. (prevp (goto-char prevp))
  1959. (org-list-use-circular-motion
  1960. (goto-char (org-list-get-last-item item struct prevs)))
  1961. (t (error "On first item")))))))
  1962. (defun org-next-item ()
  1963. "Move to the beginning of the next item.
  1964. Throw an error when not in a list. Also throw an error when at
  1965. last item, unless `org-list-use-circular-motion' is non-nil."
  1966. (interactive)
  1967. (let ((item (org-in-item-p)))
  1968. (if (not item)
  1969. (error "Not in an item")
  1970. (goto-char item)
  1971. (let* ((struct (org-list-struct))
  1972. (prevs (org-list-prevs-alist struct))
  1973. (prevp (org-list-get-next-item item struct prevs)))
  1974. (cond
  1975. (prevp (goto-char prevp))
  1976. (org-list-use-circular-motion
  1977. (goto-char (org-list-get-first-item item struct prevs)))
  1978. (t (error "On last item")))))))
  1979. (defun org-move-item-down ()
  1980. "Move the item at point down, i.e. swap with following item.
  1981. Sub-items (items with larger indentation) are considered part of
  1982. the item, so this really moves item trees."
  1983. (interactive)
  1984. (unless (org-at-item-p) (error "Not at an item"))
  1985. (let* ((col (current-column))
  1986. (item (point-at-bol))
  1987. (struct (org-list-struct))
  1988. (prevs (org-list-prevs-alist struct))
  1989. (next-item (org-list-get-next-item (point-at-bol) struct prevs)))
  1990. (unless (or next-item org-list-use-circular-motion)
  1991. (user-error "Cannot move this item further down"))
  1992. (if (not next-item)
  1993. (setq struct (org-list-send-item item 'begin struct))
  1994. (setq struct (org-list-swap-items item next-item struct))
  1995. (goto-char
  1996. (org-list-get-next-item item struct (org-list-prevs-alist struct))))
  1997. (org-list-write-struct struct (org-list-parents-alist struct))
  1998. (org-move-to-column col)))
  1999. (defun org-move-item-up ()
  2000. "Move the item at point up, i.e. swap with previous item.
  2001. Sub-items (items with larger indentation) are considered part of
  2002. the item, so this really moves item trees."
  2003. (interactive)
  2004. (unless (org-at-item-p) (error "Not at an item"))
  2005. (let* ((col (current-column))
  2006. (item (point-at-bol))
  2007. (struct (org-list-struct))
  2008. (prevs (org-list-prevs-alist struct))
  2009. (prev-item (org-list-get-prev-item (point-at-bol) struct prevs)))
  2010. (unless (or prev-item org-list-use-circular-motion)
  2011. (user-error "Cannot move this item further up"))
  2012. (if (not prev-item)
  2013. (setq struct (org-list-send-item item 'end struct))
  2014. (setq struct (org-list-swap-items prev-item item struct)))
  2015. (org-list-write-struct struct (org-list-parents-alist struct))
  2016. (org-move-to-column col)))
  2017. (defun org-insert-item (&optional checkbox)
  2018. "Insert a new item at the current level.
  2019. If cursor is before first character after bullet of the item, the
  2020. new item will be created before the current one.
  2021. If CHECKBOX is non-nil, add a checkbox next to the bullet.
  2022. Return t when things worked, nil when we are not in an item, or
  2023. item is invisible."
  2024. (let ((itemp (org-in-item-p))
  2025. (pos (point)))
  2026. ;; If cursor isn't is a list or if list is invisible, return nil.
  2027. (unless (or (not itemp)
  2028. (save-excursion
  2029. (goto-char itemp)
  2030. (outline-invisible-p)))
  2031. (if (save-excursion
  2032. (goto-char itemp)
  2033. (org-at-item-timer-p))
  2034. ;; Timer list: delegate to `org-timer-item'.
  2035. (progn (org-timer-item) t)
  2036. (let* ((struct (save-excursion (goto-char itemp)
  2037. (org-list-struct)))
  2038. (prevs (org-list-prevs-alist struct))
  2039. ;; If we're in a description list, ask for the new term.
  2040. (desc (when (eq (org-list-get-list-type itemp struct prevs)
  2041. 'descriptive)
  2042. " :: ")))
  2043. (setq struct (org-list-insert-item pos struct prevs checkbox desc))
  2044. (org-list-write-struct struct (org-list-parents-alist struct))
  2045. (when checkbox (org-update-checkbox-count-maybe))
  2046. (looking-at org-list-full-item-re)
  2047. (goto-char (if (and (match-beginning 4)
  2048. (save-match-data
  2049. (string-match "[.)]" (match-string 1))))
  2050. (match-beginning 4)
  2051. (match-end 0)))
  2052. (if desc (backward-char 1))
  2053. t)))))
  2054. (defun org-list-repair ()
  2055. "Fix indentation, bullets and checkboxes in the list at point."
  2056. (interactive)
  2057. (unless (org-at-item-p) (error "This is not a list"))
  2058. (let* ((struct (org-list-struct))
  2059. (parents (org-list-parents-alist struct)))
  2060. (org-list-write-struct struct parents)))
  2061. (defun org-cycle-list-bullet (&optional which)
  2062. "Cycle through the different itemize/enumerate bullets.
  2063. This cycle the entire list level through the sequence:
  2064. `-' -> `+' -> `*' -> `1.' -> `1)'
  2065. If WHICH is a valid string, use that as the new bullet. If WHICH
  2066. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  2067. `previous', cycle backwards."
  2068. (interactive "P")
  2069. (unless (org-at-item-p) (error "Not at an item"))
  2070. (save-excursion
  2071. (beginning-of-line)
  2072. (let* ((struct (org-list-struct))
  2073. (parents (org-list-parents-alist struct))
  2074. (prevs (org-list-prevs-alist struct))
  2075. (list-beg (org-list-get-first-item (point) struct prevs))
  2076. (bullet (org-list-get-bullet list-beg struct))
  2077. (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs))
  2078. (case-fold-search nil)
  2079. (current (cond
  2080. ((string-match "[a-z]\\." bullet) "a.")
  2081. ((string-match "[a-z])" bullet) "a)")
  2082. ((string-match "[A-Z]\\." bullet) "A.")
  2083. ((string-match "[A-Z])" bullet) "A)")
  2084. ((string-match "\\." bullet) "1.")
  2085. ((string-match ")" bullet) "1)")
  2086. (t (org-trim bullet))))
  2087. ;; Compute list of possible bullets, depending on context.
  2088. (bullet-list
  2089. (append '("-" "+" )
  2090. ;; *-bullets are not allowed at column 0.
  2091. (unless (looking-at "\\S-") '("*"))
  2092. ;; Description items cannot be numbered.
  2093. (unless (or (eq org-plain-list-ordered-item-terminator ?\))
  2094. (org-at-item-description-p))
  2095. '("1."))
  2096. (unless (or (eq org-plain-list-ordered-item-terminator ?.)
  2097. (org-at-item-description-p))
  2098. '("1)"))
  2099. (unless (or (not alpha-p)
  2100. (eq org-plain-list-ordered-item-terminator ?\))
  2101. (org-at-item-description-p))
  2102. '("a." "A."))
  2103. (unless (or (not alpha-p)
  2104. (eq org-plain-list-ordered-item-terminator ?.)
  2105. (org-at-item-description-p))
  2106. '("a)" "A)"))))
  2107. (len (length bullet-list))
  2108. (item-index (- len (length (member current bullet-list))))
  2109. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  2110. (new (cond
  2111. ((member which bullet-list) which)
  2112. ((numberp which) (funcall get-value which))
  2113. ((eq 'previous which) (funcall get-value (1- item-index)))
  2114. (t (funcall get-value (1+ item-index))))))
  2115. ;; Use a short variation of `org-list-write-struct' as there's
  2116. ;; no need to go through all the steps.
  2117. (let ((old-struct (copy-tree struct)))
  2118. (org-list-set-bullet list-beg struct (org-list-bullet-string new))
  2119. (org-list-struct-fix-bul struct prevs)
  2120. (org-list-struct-fix-ind struct parents)
  2121. (org-list-struct-apply-struct struct old-struct)))))
  2122. (defun org-toggle-checkbox (&optional toggle-presence)
  2123. "Toggle the checkbox in the current line.
  2124. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  2125. double prefix, set checkbox to [-].
  2126. When there is an active region, toggle status or presence of the
  2127. first checkbox there, and make every item inside have the same
  2128. status or presence, respectively.
  2129. If the cursor is in a headline, apply this to all checkbox items
  2130. in the text below the heading, taking as reference the first item
  2131. in subtree, ignoring drawers."
  2132. (interactive "P")
  2133. (save-excursion
  2134. (let* (singlep
  2135. block-item
  2136. lim-up
  2137. lim-down
  2138. (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
  2139. "\\|" org-deadline-string
  2140. "\\|" org-closed-string
  2141. "\\|" org-clock-string "\\)"
  2142. " *[[<]\\([^]>]+\\)[]>]"))
  2143. (orderedp (org-entry-get nil "ORDERED"))
  2144. (_bounds
  2145. ;; In a region, start at first item in region.
  2146. (cond
  2147. ((org-region-active-p)
  2148. (let ((limit (region-end)))
  2149. (goto-char (region-beginning))
  2150. (if (org-list-search-forward (org-item-beginning-re) limit t)
  2151. (setq lim-up (point-at-bol))
  2152. (error "No item in region"))
  2153. (setq lim-down (copy-marker limit))))
  2154. ((org-at-heading-p)
  2155. ;; On an heading, start at first item after drawers and
  2156. ;; time-stamps (scheduled, etc.).
  2157. (let ((limit (save-excursion (outline-next-heading) (point))))
  2158. (forward-line 1)
  2159. (while (or (looking-at org-drawer-regexp)
  2160. (looking-at keyword-re))
  2161. (if (looking-at keyword-re)
  2162. (forward-line 1)
  2163. (re-search-forward "^[ \t]*:END:" limit nil)))
  2164. (if (org-list-search-forward (org-item-beginning-re) limit t)
  2165. (setq lim-up (point-at-bol))
  2166. (error "No item in subtree"))
  2167. (setq lim-down (copy-marker limit))))
  2168. ;; Just one item: set SINGLEP flag.
  2169. ((org-at-item-p)
  2170. (setq singlep t)
  2171. (setq lim-up (point-at-bol)
  2172. lim-down (copy-marker (point-at-eol))))
  2173. (t (error "Not at an item or heading, and no active region"))))
  2174. ;; Determine the checkbox going to be applied to all items
  2175. ;; within bounds.
  2176. (ref-checkbox
  2177. (progn
  2178. (goto-char lim-up)
  2179. (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
  2180. (cond
  2181. ((equal toggle-presence '(16)) "[-]")
  2182. ((equal toggle-presence '(4))
  2183. (unless cbox "[ ]"))
  2184. ((equal "[X]" cbox) "[ ]")
  2185. (t "[X]"))))))
  2186. ;; When an item is found within bounds, grab the full list at
  2187. ;; point structure, then: (1) set check-box of all its items
  2188. ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
  2189. ;; whole list, (3) move point after the list.
  2190. (goto-char lim-up)
  2191. (while (and (< (point) lim-down)
  2192. (org-list-search-forward (org-item-beginning-re)
  2193. lim-down 'move))
  2194. (let* ((struct (org-list-struct))
  2195. (struct-copy (copy-tree struct))
  2196. (parents (org-list-parents-alist struct))
  2197. (prevs (org-list-prevs-alist struct))
  2198. (bottom (copy-marker (org-list-get-bottom-point struct)))
  2199. (items-to-toggle (cl-remove-if
  2200. (lambda (e) (or (< e lim-up) (> e lim-down)))
  2201. (mapcar #'car struct))))
  2202. (mapc (lambda (e) (org-list-set-checkbox
  2203. e struct
  2204. ;; If there is no box at item, leave as-is
  2205. ;; unless function was called with C-u prefix.
  2206. (let ((cur-box (org-list-get-checkbox e struct)))
  2207. (if (or cur-box (equal toggle-presence '(4)))
  2208. ref-checkbox
  2209. cur-box))))
  2210. items-to-toggle)
  2211. (setq block-item (org-list-struct-fix-box
  2212. struct parents prevs orderedp))
  2213. ;; Report some problems due to ORDERED status of subtree.
  2214. ;; If only one box was being checked, throw an error, else,
  2215. ;; only signal problems.
  2216. (cond
  2217. ((and singlep block-item (> lim-up block-item))
  2218. (error
  2219. "Checkbox blocked because of unchecked box at line %d"
  2220. (org-current-line block-item)))
  2221. (block-item
  2222. (message
  2223. "Checkboxes were removed due to unchecked box at line %d"
  2224. (org-current-line block-item))))
  2225. (goto-char bottom)
  2226. (move-marker bottom nil)
  2227. (org-list-struct-apply-struct struct struct-copy)))
  2228. (move-marker lim-down nil)))
  2229. (org-update-checkbox-count-maybe))
  2230. (defun org-reset-checkbox-state-subtree ()
  2231. "Reset all checkboxes in an entry subtree."
  2232. (interactive "*")
  2233. (if (org-before-first-heading-p)
  2234. (error "Not inside a tree")
  2235. (save-restriction
  2236. (save-excursion
  2237. (org-narrow-to-subtree)
  2238. (org-show-subtree)
  2239. (goto-char (point-min))
  2240. (let ((end (point-max)))
  2241. (while (< (point) end)
  2242. (when (org-at-item-checkbox-p)
  2243. (replace-match "[ ]" t t nil 1))
  2244. (beginning-of-line 2)))
  2245. (org-update-checkbox-count-maybe 'all)))))
  2246. (defun org-update-checkbox-count (&optional all)
  2247. "Update the checkbox statistics in the current section.
  2248. This will find all statistic cookies like [57%] and [6/12] and
  2249. update them with the current numbers.
  2250. With optional prefix argument ALL, do this for the whole buffer."
  2251. (interactive "P")
  2252. (org-with-wide-buffer
  2253. (let* ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  2254. (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\
  2255. \\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
  2256. (recursivep
  2257. (or (not org-checkbox-hierarchical-statistics)
  2258. (string-match "\\<recursive\\>"
  2259. (or (org-entry-get nil "COOKIE_DATA") ""))))
  2260. (within-inlinetask (and (not all)
  2261. (featurep 'org-inlinetask)
  2262. (org-inlinetask-in-task-p)))
  2263. (end (cond (all (point-max))
  2264. (within-inlinetask
  2265. (save-excursion (outline-next-heading) (point)))
  2266. (t (save-excursion
  2267. (org-with-limited-levels (outline-next-heading))
  2268. (point)))))
  2269. (count-boxes
  2270. (lambda (item structs recursivep)
  2271. ;; Return number of checked boxes and boxes of all types
  2272. ;; in all structures in STRUCTS. If RECURSIVEP is
  2273. ;; non-nil, also count boxes in sub-lists. If ITEM is
  2274. ;; nil, count across the whole structure, else count only
  2275. ;; across subtree whose ancestor is ITEM.
  2276. (let ((c-on 0) (c-all 0))
  2277. (dolist (s structs (list c-on c-all))
  2278. (let* ((pre (org-list-prevs-alist s))
  2279. (par (org-list-parents-alist s))
  2280. (items
  2281. (cond
  2282. ((and recursivep item) (org-list-get-subtree item s))
  2283. (recursivep (mapcar #'car s))
  2284. (item (org-list-get-children item s par))
  2285. (t (org-list-get-all-items
  2286. (org-list-get-top-point s) s pre))))
  2287. (cookies (delq nil (mapcar
  2288. (lambda (e)
  2289. (org-list-get-checkbox e s))
  2290. items))))
  2291. (cl-incf c-all (length cookies))
  2292. (cl-incf c-on (cl-count "[X]" cookies :test #'equal)))))))
  2293. cookies-list cache)
  2294. ;; Move to start.
  2295. (cond (all (goto-char (point-min)))
  2296. (within-inlinetask (org-back-to-heading t))
  2297. (t (org-with-limited-levels (outline-previous-heading))))
  2298. ;; Build an alist for each cookie found. The key is the position
  2299. ;; at beginning of cookie and values ending position, format of
  2300. ;; cookie, number of checked boxes to report and total number of
  2301. ;; boxes.
  2302. (while (re-search-forward cookie-re end t)
  2303. (let ((context (save-excursion (backward-char)
  2304. (save-match-data (org-element-context)))))
  2305. (when (eq (org-element-type context) 'statistics-cookie)
  2306. (push
  2307. (append
  2308. (list (match-beginning 1) (match-end 1) (match-end 2))
  2309. (let* ((container
  2310. (org-element-lineage
  2311. context
  2312. '(drawer center-block dynamic-block inlinetask item
  2313. quote-block special-block verse-block)))
  2314. (beg (if container
  2315. (org-element-property :contents-begin container)
  2316. (save-excursion
  2317. (org-with-limited-levels
  2318. (outline-previous-heading))
  2319. (point)))))
  2320. (or (cdr (assq beg cache))
  2321. (save-excursion
  2322. (goto-char beg)
  2323. (let ((end
  2324. (if container
  2325. (org-element-property :contents-end container)
  2326. (save-excursion
  2327. (org-with-limited-levels (outline-next-heading))
  2328. (point))))
  2329. structs)
  2330. (while (re-search-forward box-re end t)
  2331. (let ((element (org-element-at-point)))
  2332. (when (eq (org-element-type element) 'item)
  2333. (push (org-element-property :structure element)
  2334. structs)
  2335. ;; Skip whole list since we have its
  2336. ;; structure anyway.
  2337. (while (setq element (org-element-lineage
  2338. element '(plain-list)))
  2339. (goto-char
  2340. (min (org-element-property :end element)
  2341. end))))))
  2342. ;; Cache count for cookies applying to the same
  2343. ;; area. Then return it.
  2344. (let ((count
  2345. (funcall count-boxes
  2346. (and (eq (org-element-type container)
  2347. 'item)
  2348. (org-element-property
  2349. :begin container))
  2350. structs
  2351. recursivep)))
  2352. (push (cons beg count) cache)
  2353. count))))))
  2354. cookies-list))))
  2355. ;; Apply alist to buffer.
  2356. (dolist (cookie cookies-list)
  2357. (let* ((beg (car cookie))
  2358. (end (nth 1 cookie))
  2359. (percent (nth 2 cookie))
  2360. (checked (nth 3 cookie))
  2361. (total (nth 4 cookie)))
  2362. (goto-char beg)
  2363. (insert
  2364. (if percent (format "[%d%%]" (floor (* 100.0 checked)
  2365. (max 1 total)))
  2366. (format "[%d/%d]" checked total)))
  2367. (delete-region (point) (+ (point) (- end beg)))
  2368. (when org-auto-align-tags (org-fix-tags-on-the-fly)))))))
  2369. (defun org-get-checkbox-statistics-face ()
  2370. "Select the face for checkbox statistics.
  2371. The face will be `org-done' when all relevant boxes are checked.
  2372. Otherwise it will be `org-todo'."
  2373. (if (match-end 1)
  2374. (if (equal (match-string 1) "100%")
  2375. 'org-checkbox-statistics-done
  2376. 'org-checkbox-statistics-todo)
  2377. (if (and (> (match-end 2) (match-beginning 2))
  2378. (equal (match-string 2) (match-string 3)))
  2379. 'org-checkbox-statistics-done
  2380. 'org-checkbox-statistics-todo)))
  2381. (defun org-update-checkbox-count-maybe (&optional all)
  2382. "Update checkbox statistics unless turned off by user.
  2383. With an optional argument ALL, update them in the whole buffer."
  2384. (when (cdr (assq 'checkbox org-list-automatic-rules))
  2385. (org-update-checkbox-count all))
  2386. (run-hooks 'org-checkbox-statistics-hook))
  2387. (defvar org-last-indent-begin-marker (make-marker))
  2388. (defvar org-last-indent-end-marker (make-marker))
  2389. (defun org-list-indent-item-generic (arg no-subtree struct)
  2390. "Indent a local list item including its children.
  2391. When number ARG is a negative, item will be outdented, otherwise
  2392. it will be indented.
  2393. If a region is active, all items inside will be moved.
  2394. If NO-SUBTREE is non-nil, only indent the item itself, not its
  2395. children.
  2396. STRUCT is the list structure.
  2397. Return t if successful."
  2398. (save-excursion
  2399. (let* ((regionp (org-region-active-p))
  2400. (rbeg (and regionp (region-beginning)))
  2401. (rend (and regionp (region-end)))
  2402. (top (org-list-get-top-point struct))
  2403. (parents (org-list-parents-alist struct))
  2404. (prevs (org-list-prevs-alist struct))
  2405. ;; Are we going to move the whole list?
  2406. (specialp
  2407. (and (not regionp)
  2408. (= top (point-at-bol))
  2409. (cdr (assq 'indent org-list-automatic-rules))
  2410. (if no-subtree
  2411. (error
  2412. "First item of list cannot move without its subtree")
  2413. t))))
  2414. ;; Determine begin and end points of zone to indent. If moving
  2415. ;; more than one item, save them for subsequent moves.
  2416. (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  2417. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  2418. (if regionp
  2419. (progn
  2420. (set-marker org-last-indent-begin-marker rbeg)
  2421. (set-marker org-last-indent-end-marker rend))
  2422. (set-marker org-last-indent-begin-marker (point-at-bol))
  2423. (set-marker org-last-indent-end-marker
  2424. (cond
  2425. (specialp (org-list-get-bottom-point struct))
  2426. (no-subtree (1+ (point-at-bol)))
  2427. (t (org-list-get-item-end (point-at-bol) struct))))))
  2428. (let* ((beg (marker-position org-last-indent-begin-marker))
  2429. (end (marker-position org-last-indent-end-marker)))
  2430. (cond
  2431. ;; Special case: moving top-item with indent rule.
  2432. (specialp
  2433. (let* ((level-skip (org-level-increment))
  2434. (offset (if (< arg 0) (- level-skip) level-skip))
  2435. (top-ind (org-list-get-ind beg struct))
  2436. (old-struct (copy-tree struct)))
  2437. (if (< (+ top-ind offset) 0)
  2438. (error "Cannot outdent beyond margin")
  2439. ;; Change bullet if necessary.
  2440. (when (and (= (+ top-ind offset) 0)
  2441. (string-match "*"
  2442. (org-list-get-bullet beg struct)))
  2443. (org-list-set-bullet beg struct
  2444. (org-list-bullet-string "-")))
  2445. ;; Shift every item by OFFSET and fix bullets. Then
  2446. ;; apply changes to buffer.
  2447. (mapc (lambda (e)
  2448. (let ((ind (org-list-get-ind (car e) struct)))
  2449. (org-list-set-ind (car e) struct (+ ind offset))))
  2450. struct)
  2451. (org-list-struct-fix-bul struct prevs)
  2452. (org-list-struct-apply-struct struct old-struct))))
  2453. ;; Forbidden move:
  2454. ((and (< arg 0)
  2455. ;; If only one item is moved, it mustn't have a child.
  2456. (or (and no-subtree
  2457. (not regionp)
  2458. (org-list-has-child-p beg struct))
  2459. ;; If a subtree or region is moved, the last item
  2460. ;; of the subtree mustn't have a child.
  2461. (let ((last-item (caar
  2462. (reverse
  2463. (cl-remove-if
  2464. (lambda (e) (>= (car e) end))
  2465. struct)))))
  2466. (org-list-has-child-p last-item struct))))
  2467. (error "Cannot outdent an item without its children"))
  2468. ;; Normal shifting
  2469. (t
  2470. (let* ((new-parents
  2471. (if (< arg 0)
  2472. (org-list-struct-outdent beg end struct parents)
  2473. (org-list-struct-indent beg end struct parents prevs))))
  2474. (org-list-write-struct struct new-parents))
  2475. (org-update-checkbox-count-maybe))))))
  2476. t)
  2477. (defun org-outdent-item ()
  2478. "Outdent a local list item, but not its children.
  2479. If a region is active, all items inside will be moved."
  2480. (interactive)
  2481. (let ((regionp (org-region-active-p)))
  2482. (cond
  2483. ((or (org-at-item-p)
  2484. (and regionp
  2485. (save-excursion (goto-char (region-beginning))
  2486. (org-at-item-p))))
  2487. (let ((struct (if (not regionp) (org-list-struct)
  2488. (save-excursion (goto-char (region-beginning))
  2489. (org-list-struct)))))
  2490. (org-list-indent-item-generic -1 t struct)))
  2491. (regionp (error "Region not starting at an item"))
  2492. (t (error "Not at an item")))))
  2493. (defun org-indent-item ()
  2494. "Indent a local list item, but not its children.
  2495. If a region is active, all items inside will be moved."
  2496. (interactive)
  2497. (let ((regionp (org-region-active-p)))
  2498. (cond
  2499. ((or (org-at-item-p)
  2500. (and regionp
  2501. (save-excursion (goto-char (region-beginning))
  2502. (org-at-item-p))))
  2503. (let ((struct (if (not regionp) (org-list-struct)
  2504. (save-excursion (goto-char (region-beginning))
  2505. (org-list-struct)))))
  2506. (org-list-indent-item-generic 1 t struct)))
  2507. (regionp (error "Region not starting at an item"))
  2508. (t (error "Not at an item")))))
  2509. (defun org-outdent-item-tree ()
  2510. "Outdent a local list item including its children.
  2511. If a region is active, all items inside will be moved."
  2512. (interactive)
  2513. (let ((regionp (org-region-active-p)))
  2514. (cond
  2515. ((or (org-at-item-p)
  2516. (and regionp
  2517. (save-excursion (goto-char (region-beginning))
  2518. (org-at-item-p))))
  2519. (let ((struct (if (not regionp) (org-list-struct)
  2520. (save-excursion (goto-char (region-beginning))
  2521. (org-list-struct)))))
  2522. (org-list-indent-item-generic -1 nil struct)))
  2523. (regionp (error "Region not starting at an item"))
  2524. (t (error "Not at an item")))))
  2525. (defun org-indent-item-tree ()
  2526. "Indent a local list item including its children.
  2527. If a region is active, all items inside will be moved."
  2528. (interactive)
  2529. (let ((regionp (org-region-active-p)))
  2530. (cond
  2531. ((or (org-at-item-p)
  2532. (and regionp
  2533. (save-excursion (goto-char (region-beginning))
  2534. (org-at-item-p))))
  2535. (let ((struct (if (not regionp) (org-list-struct)
  2536. (save-excursion (goto-char (region-beginning))
  2537. (org-list-struct)))))
  2538. (org-list-indent-item-generic 1 nil struct)))
  2539. (regionp (error "Region not starting at an item"))
  2540. (t (error "Not at an item")))))
  2541. (defvar org-tab-ind-state)
  2542. (defvar org-adapt-indentation)
  2543. (defun org-cycle-item-indentation ()
  2544. "Cycle levels of indentation of an empty item.
  2545. The first run indents the item, if applicable. Subsequent runs
  2546. outdent it at meaningful levels in the list. When done, item is
  2547. put back at its original position with its original bullet.
  2548. Return t at each successful move."
  2549. (when (org-at-item-p)
  2550. (let* ((org-adapt-indentation nil)
  2551. (struct (org-list-struct))
  2552. (ind (org-list-get-ind (point-at-bol) struct))
  2553. (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
  2554. ;; Accept empty items or if cycle has already started.
  2555. (when (or (eq last-command 'org-cycle-item-indentation)
  2556. (and (save-excursion
  2557. (beginning-of-line)
  2558. (looking-at org-list-full-item-re))
  2559. (>= (match-end 0) (save-excursion
  2560. (goto-char (org-list-get-item-end
  2561. (point-at-bol) struct))
  2562. (skip-chars-backward " \r\t\n")
  2563. (point)))))
  2564. (setq this-command 'org-cycle-item-indentation)
  2565. ;; When in the middle of the cycle, try to outdent first. If
  2566. ;; it fails, and point is still at initial position, indent.
  2567. ;; Else, re-create it at its original position.
  2568. (if (eq last-command 'org-cycle-item-indentation)
  2569. (cond
  2570. ((ignore-errors (org-list-indent-item-generic -1 t struct)))
  2571. ((and (= ind (car org-tab-ind-state))
  2572. (ignore-errors (org-list-indent-item-generic 1 t struct))))
  2573. (t (delete-region (point-at-bol) (point-at-eol))
  2574. (indent-to-column (car org-tab-ind-state))
  2575. (insert (cdr org-tab-ind-state) " ")
  2576. ;; Break cycle
  2577. (setq this-command 'identity)))
  2578. ;; If a cycle is starting, remember indentation and bullet,
  2579. ;; then try to indent. If it fails, try to outdent.
  2580. (setq org-tab-ind-state (cons ind bullet))
  2581. (cond
  2582. ((ignore-errors (org-list-indent-item-generic 1 t struct)))
  2583. ((ignore-errors (org-list-indent-item-generic -1 t struct)))
  2584. (t (user-error "Cannot move item"))))
  2585. t))))
  2586. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  2587. "Sort list items.
  2588. The cursor may be at any item of the list that should be sorted.
  2589. Sublists are not sorted. Checkboxes, if any, are ignored.
  2590. Sorting can be alphabetically, numerically, by date/time as given
  2591. by a time stamp, by a property or by priority.
  2592. Comparing entries ignores case by default. However, with an
  2593. optional argument WITH-CASE, the sorting considers case as well.
  2594. The command prompts for the sorting type unless it has been given
  2595. to the function through the SORTING-TYPE argument, which needs to
  2596. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F ?x ?X). Here is the
  2597. detailed meaning of each character:
  2598. n Numerically, by converting the beginning of the item to a number.
  2599. a Alphabetically. Only the first line of item is checked.
  2600. t By date/time, either the first active time stamp in the entry, if
  2601. any, or by the first inactive one. In a timer list, sort the timers.
  2602. x By \"checked\" status of a check list.
  2603. Capital letters will reverse the sort order.
  2604. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
  2605. a function to be called with point at the beginning of the
  2606. record. It must return either a string or a number that should
  2607. serve as the sorting key for that record. It will then use
  2608. COMPARE-FUNC to compare entries.
  2609. Sorting is done against the visible part of the headlines, it
  2610. ignores hidden links."
  2611. (interactive "P")
  2612. (let* ((case-func (if with-case 'identity 'downcase))
  2613. (struct (org-list-struct))
  2614. (prevs (org-list-prevs-alist struct))
  2615. (start (org-list-get-list-begin (point-at-bol) struct prevs))
  2616. (end (org-list-get-list-end (point-at-bol) struct prevs))
  2617. (sorting-type
  2618. (or sorting-type
  2619. (progn
  2620. (message
  2621. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc [x]checked A/N/T/F/X means reversed:")
  2622. (read-char-exclusive))))
  2623. (getkey-func
  2624. (or getkey-func
  2625. (and (= (downcase sorting-type) ?f)
  2626. (intern (completing-read "Sort using function: "
  2627. obarray 'fboundp t nil nil))))))
  2628. (message "Sorting items...")
  2629. (save-restriction
  2630. (narrow-to-region start end)
  2631. (goto-char (point-min))
  2632. (let* ((dcst (downcase sorting-type))
  2633. (case-fold-search nil)
  2634. (now (current-time))
  2635. (sort-func (cond
  2636. ((= dcst ?a) 'string<)
  2637. ((= dcst ?f) compare-func)
  2638. ((= dcst ?t) '<)
  2639. ((= dcst ?x) 'string<)))
  2640. (next-record (lambda ()
  2641. (skip-chars-forward " \r\t\n")
  2642. (or (eobp) (beginning-of-line))))
  2643. (end-record (lambda ()
  2644. (goto-char (org-list-get-item-end-before-blank
  2645. (point) struct))))
  2646. (value-to-sort
  2647. (lambda ()
  2648. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  2649. (cond
  2650. ((= dcst ?n)
  2651. (string-to-number
  2652. (org-sort-remove-invisible
  2653. (buffer-substring (match-end 0) (point-at-eol)))))
  2654. ((= dcst ?a)
  2655. (funcall case-func
  2656. (org-sort-remove-invisible
  2657. (buffer-substring
  2658. (match-end 0) (point-at-eol)))))
  2659. ((= dcst ?t)
  2660. (cond
  2661. ;; If it is a timer list, convert timer to seconds
  2662. ((org-at-item-timer-p)
  2663. (org-timer-hms-to-secs (match-string 1)))
  2664. ((or (save-excursion
  2665. (re-search-forward org-ts-regexp (point-at-eol) t))
  2666. (save-excursion (re-search-forward org-ts-regexp-both
  2667. (point-at-eol) t)))
  2668. (org-time-string-to-seconds (match-string 0)))
  2669. (t (float-time now))))
  2670. ((= dcst ?x) (or (and (stringp (match-string 1))
  2671. (match-string 1))
  2672. ""))
  2673. ((= dcst ?f)
  2674. (if getkey-func
  2675. (let ((value (funcall getkey-func)))
  2676. (if (stringp value)
  2677. (funcall case-func value)
  2678. value))
  2679. (error "Invalid key function `%s'" getkey-func)))
  2680. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  2681. (sort-subr (/= dcst sorting-type)
  2682. next-record
  2683. end-record
  2684. value-to-sort
  2685. nil
  2686. sort-func)
  2687. ;; Read and fix list again, as `sort-subr' probably destroyed
  2688. ;; its structure.
  2689. (org-list-repair)
  2690. (run-hooks 'org-after-sorting-entries-or-items-hook)
  2691. (message "Sorting items...done")))))
  2692. (defun org-toggle-item (arg)
  2693. "Convert headings or normal lines to items, items to normal lines.
  2694. If there is no active region, only the current line is considered.
  2695. If the first non blank line in the region is a headline, convert
  2696. all headlines to items, shifting text accordingly.
  2697. If it is an item, convert all items to normal lines.
  2698. If it is normal text, change region into a list of items.
  2699. With a prefix argument ARG, change the region in a single item."
  2700. (interactive "P")
  2701. (let ((shift-text
  2702. (lambda (ind end)
  2703. ;; Shift text in current section to IND, from point to END.
  2704. ;; The function leaves point to END line.
  2705. (let ((min-i 1000) (end (copy-marker end)))
  2706. ;; First determine the minimum indentation (MIN-I) of
  2707. ;; the text.
  2708. (save-excursion
  2709. (catch 'exit
  2710. (while (< (point) end)
  2711. (let ((i (org-get-indentation)))
  2712. (cond
  2713. ;; Skip blank lines and inline tasks.
  2714. ((looking-at "^[ \t]*$"))
  2715. ((looking-at org-outline-regexp-bol))
  2716. ;; We can't find less than 0 indentation.
  2717. ((zerop i) (throw 'exit (setq min-i 0)))
  2718. ((< i min-i) (setq min-i i))))
  2719. (forward-line))))
  2720. ;; Then indent each line so that a line indented to
  2721. ;; MIN-I becomes indented to IND. Ignore blank lines
  2722. ;; and inline tasks in the process.
  2723. (let ((delta (- ind min-i)))
  2724. (while (< (point) end)
  2725. (unless (or (looking-at "^[ \t]*$")
  2726. (looking-at org-outline-regexp-bol))
  2727. (indent-line-to (+ (org-get-indentation) delta)))
  2728. (forward-line))))))
  2729. (skip-blanks
  2730. (lambda (pos)
  2731. ;; Return beginning of first non-blank line, starting from
  2732. ;; line at POS.
  2733. (save-excursion
  2734. (goto-char pos)
  2735. (skip-chars-forward " \r\t\n")
  2736. (point-at-bol))))
  2737. beg end)
  2738. ;; Determine boundaries of changes.
  2739. (if (org-region-active-p)
  2740. (setq beg (funcall skip-blanks (region-beginning))
  2741. end (copy-marker (region-end)))
  2742. (setq beg (funcall skip-blanks (point-at-bol))
  2743. end (copy-marker (point-at-eol))))
  2744. ;; Depending on the starting line, choose an action on the text
  2745. ;; between BEG and END.
  2746. (org-with-limited-levels
  2747. (save-excursion
  2748. (goto-char beg)
  2749. (cond
  2750. ;; Case 1. Start at an item: de-itemize. Note that it only
  2751. ;; happens when a region is active: `org-ctrl-c-minus'
  2752. ;; would call `org-cycle-list-bullet' otherwise.
  2753. ((org-at-item-p)
  2754. (while (< (point) end)
  2755. (when (org-at-item-p)
  2756. (skip-chars-forward " \t")
  2757. (delete-region (point) (match-end 0)))
  2758. (forward-line)))
  2759. ;; Case 2. Start at an heading: convert to items.
  2760. ((org-at-heading-p)
  2761. (let* ((bul (org-list-bullet-string "-"))
  2762. (bul-len (length bul))
  2763. ;; Indentation of the first heading. It should be
  2764. ;; relative to the indentation of its parent, if any.
  2765. (start-ind (save-excursion
  2766. (cond
  2767. ((not org-adapt-indentation) 0)
  2768. ((not (outline-previous-heading)) 0)
  2769. (t (length (match-string 0))))))
  2770. ;; Level of first heading. Further headings will be
  2771. ;; compared to it to determine hierarchy in the list.
  2772. (ref-level (org-reduced-level (org-outline-level))))
  2773. (while (< (point) end)
  2774. (let* ((level (org-reduced-level (org-outline-level)))
  2775. (delta (max 0 (- level ref-level)))
  2776. (todo-state (org-get-todo-state)))
  2777. ;; If current headline is less indented than the first
  2778. ;; one, set it as reference, in order to preserve
  2779. ;; subtrees.
  2780. (when (< level ref-level) (setq ref-level level))
  2781. ;; Remove stars and TODO keyword.
  2782. (looking-at org-todo-line-regexp)
  2783. (delete-region (point) (or (match-beginning 3)
  2784. (line-end-position)))
  2785. (insert bul)
  2786. (indent-line-to (+ start-ind (* delta bul-len)))
  2787. ;; Turn TODO keyword into a check box.
  2788. (when todo-state
  2789. (let* ((struct (org-list-struct))
  2790. (old (copy-tree struct)))
  2791. (org-list-set-checkbox
  2792. (line-beginning-position)
  2793. struct
  2794. (if (member todo-state org-done-keywords)
  2795. "[X]"
  2796. "[ ]"))
  2797. (org-list-write-struct struct
  2798. (org-list-parents-alist struct)
  2799. old)))
  2800. ;; Ensure all text down to END (or SECTION-END) belongs
  2801. ;; to the newly created item.
  2802. (let ((section-end (save-excursion
  2803. (or (outline-next-heading) (point)))))
  2804. (forward-line)
  2805. (funcall shift-text
  2806. (+ start-ind (* (1+ delta) bul-len))
  2807. (min end section-end)))))))
  2808. ;; Case 3. Normal line with ARG: make the first line of region
  2809. ;; an item, and shift indentation of others lines to
  2810. ;; set them as item's body.
  2811. (arg (let* ((bul (org-list-bullet-string "-"))
  2812. (bul-len (length bul))
  2813. (ref-ind (org-get-indentation)))
  2814. (skip-chars-forward " \t")
  2815. (insert bul)
  2816. (forward-line)
  2817. (while (< (point) end)
  2818. ;; Ensure that lines less indented than first one
  2819. ;; still get included in item body.
  2820. (funcall shift-text
  2821. (+ ref-ind bul-len)
  2822. (min end (save-excursion (or (outline-next-heading)
  2823. (point)))))
  2824. (forward-line))))
  2825. ;; Case 4. Normal line without ARG: turn each non-item line
  2826. ;; into an item.
  2827. (t
  2828. (while (< (point) end)
  2829. (unless (or (org-at-heading-p) (org-at-item-p))
  2830. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  2831. (replace-match
  2832. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  2833. (forward-line))))))))
  2834. ;;; Send and receive lists
  2835. (defun org-list-to-lisp (&optional delete)
  2836. "Parse the list at point and maybe DELETE it.
  2837. Return a list whose car is a symbol of list type, among
  2838. `ordered', `unordered' and `descriptive'. Then, each item is
  2839. a list of strings and other sub-lists.
  2840. For example, the following list:
  2841. 1. first item
  2842. + sub-item one
  2843. + [X] sub-item two
  2844. more text in first item
  2845. 2. [@3] last item
  2846. is parsed as
  2847. \(ordered
  2848. \(\"first item\"
  2849. \(unordered
  2850. \(\"sub-item one\")
  2851. \(\"[X] sub-item two\"))
  2852. \"more text in first item\")
  2853. \(\"[@3] last item\"))
  2854. Point is left at list's end."
  2855. (letrec ((struct (org-list-struct))
  2856. (prevs (org-list-prevs-alist struct))
  2857. (parents (org-list-parents-alist struct))
  2858. (top (org-list-get-top-point struct))
  2859. (bottom (org-list-get-bottom-point struct))
  2860. (trim
  2861. (lambda (text)
  2862. ;; Remove indentation and final newline from TEXT.
  2863. (org-remove-indentation
  2864. (if (string-match-p "\n\\'" text)
  2865. (substring text 0 -1)
  2866. text))))
  2867. (parse-sublist
  2868. (lambda (e)
  2869. ;; Return a list whose car is list type and cdr a list
  2870. ;; of items' body.
  2871. (cons (org-list-get-list-type (car e) struct prevs)
  2872. (mapcar parse-item e))))
  2873. (parse-item
  2874. (lambda (e)
  2875. ;; Return a list containing counter of item, if any,
  2876. ;; text and any sublist inside it.
  2877. (let* ((end (org-list-get-item-end e struct))
  2878. (children (org-list-get-children e struct parents))
  2879. (body
  2880. (save-excursion
  2881. (goto-char e)
  2882. (looking-at "[ \t]*\\S-+[ \t]*")
  2883. (list
  2884. (funcall
  2885. trim
  2886. (concat
  2887. (make-string (string-width (match-string 0)) ?\s)
  2888. (buffer-substring-no-properties
  2889. (match-end 0) (or (car children) end))))))))
  2890. (while children
  2891. (let* ((child (car children))
  2892. (sub (org-list-get-all-items child struct prevs))
  2893. (last-in-sub (car (last sub))))
  2894. (push (funcall parse-sublist sub) body)
  2895. ;; Remove whole sub-list from children.
  2896. (setq children (cdr (memq last-in-sub children)))
  2897. ;; There is a chunk of text belonging to the item
  2898. ;; if last child doesn't end where next child
  2899. ;; starts or where item ends.
  2900. (let ((sub-end (org-list-get-item-end last-in-sub struct))
  2901. (next (or (car children) end)))
  2902. (when (/= sub-end next)
  2903. (push (funcall
  2904. trim
  2905. (buffer-substring-no-properties sub-end next))
  2906. body)))))
  2907. (nreverse body)))))
  2908. ;; Store output, take care of cursor position and deletion of
  2909. ;; list, then return output.
  2910. (prog1 (funcall parse-sublist (org-list-get-all-items top struct prevs))
  2911. (goto-char top)
  2912. (when delete
  2913. (delete-region top bottom)
  2914. (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re))
  2915. (replace-match ""))))))
  2916. (defun org-list-make-subtree ()
  2917. "Convert the plain list at point into a subtree."
  2918. (interactive)
  2919. (if (not (ignore-errors (goto-char (org-in-item-p))))
  2920. (error "Not in a list")
  2921. (let ((list (save-excursion (org-list-to-lisp t))))
  2922. (insert (org-list-to-subtree list)))))
  2923. (defun org-list-insert-radio-list ()
  2924. "Insert a radio list template appropriate for this major mode."
  2925. (interactive)
  2926. (let* ((e (assq major-mode org-list-radio-list-templates))
  2927. (txt (nth 1 e))
  2928. name pos)
  2929. (unless e (error "No radio list setup defined for %s" major-mode))
  2930. (setq name (read-string "List name: "))
  2931. (while (string-match "%n" txt)
  2932. (setq txt (replace-match name t t txt)))
  2933. (or (bolp) (insert "\n"))
  2934. (setq pos (point))
  2935. (insert txt)
  2936. (goto-char pos)))
  2937. (defun org-list-send-list (&optional maybe)
  2938. "Send a transformed version of this list to the receiver position.
  2939. With argument MAYBE, fail quietly if no transformation is defined
  2940. for this list."
  2941. (interactive)
  2942. (catch 'exit
  2943. (unless (org-at-item-p) (error "Not at a list item"))
  2944. (save-excursion
  2945. (let ((case-fold-search t))
  2946. (re-search-backward "^[ \t]*#\\+ORGLST:" nil t)
  2947. (unless (looking-at
  2948. "[ \t]*#\\+ORGLST:[ \t]+SEND[ \t]+\\(\\S-+\\)[ \t]+\\([^ \t\n]+\\)")
  2949. (if maybe (throw 'exit nil)
  2950. (error "Don't know how to transform this list")))))
  2951. (let* ((name (regexp-quote (match-string 1)))
  2952. (transform (intern (match-string 2)))
  2953. (bottom-point
  2954. (save-excursion
  2955. (re-search-forward
  2956. "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  2957. (match-beginning 0)))
  2958. (top-point
  2959. (progn
  2960. (re-search-backward "#\\+ORGLST" nil t)
  2961. (re-search-forward (org-item-beginning-re) bottom-point t)
  2962. (match-beginning 0)))
  2963. (plain-list (save-excursion
  2964. (goto-char top-point)
  2965. (org-list-to-lisp))))
  2966. (unless (fboundp transform)
  2967. (error "No such transformation function %s" transform))
  2968. (let ((txt (funcall transform plain-list)))
  2969. ;; Find the insertion(s) place(s).
  2970. (save-excursion
  2971. (goto-char (point-min))
  2972. (let ((receiver-count 0)
  2973. (begin-re (format "BEGIN +RECEIVE +ORGLST +%s\\([ \t]\\|$\\)"
  2974. name))
  2975. (end-re (format "END +RECEIVE +ORGLST +%s\\([ \t]\\|$\\)"
  2976. name)))
  2977. (while (re-search-forward begin-re nil t)
  2978. (cl-incf receiver-count)
  2979. (let ((beg (line-beginning-position 2)))
  2980. (unless (re-search-forward end-re nil t)
  2981. (user-error "Cannot find end of receiver location at %d" beg))
  2982. (beginning-of-line)
  2983. (delete-region beg (point))
  2984. (insert txt "\n")))
  2985. (cond
  2986. ((> receiver-count 1)
  2987. (message "List converted and installed at receiver locations"))
  2988. ((= receiver-count 1)
  2989. (message "List converted and installed at receiver location"))
  2990. (t (user-error "No valid receiver location found")))))))))
  2991. (defun org-list-to-generic (list params)
  2992. "Convert a LIST parsed through `org-list-to-lisp' to a custom format.
  2993. LIST is a list as returned by `org-list-to-lisp', which see.
  2994. PARAMS is a property list of parameters used to tweak the output
  2995. format.
  2996. Valid parameters are:
  2997. :backend, :raw
  2998. Export back-end used as a basis to transcode elements of the
  2999. list, when no specific parameter applies to it. It is also
  3000. used to translate its contents. You can prevent this by
  3001. setting :raw property to a non-nil value.
  3002. :splice
  3003. When non-nil, only export the contents of the top most plain
  3004. list, effectively ignoring its opening and closing lines.
  3005. :ustart, :uend
  3006. Strings to start and end an unordered list. They can also be
  3007. set to a function returning a string or nil, which will be
  3008. called with the depth of the list, counting from 1.
  3009. :ostart, :oend
  3010. Strings to start and end an ordered list. They can also be set
  3011. to a function returning a string or nil, which will be called
  3012. with the depth of the list, counting from 1.
  3013. :dstart, :dend
  3014. Strings to start and end a descriptive list. They can also be
  3015. set to a function returning a string or nil, which will be
  3016. called with the depth of the list, counting from 1.
  3017. :dtstart, :dtend, :ddstart, :ddend
  3018. Strings to start and end a descriptive term.
  3019. :istart, :iend
  3020. Strings to start or end a list item, and to start a list item
  3021. with a counter. They can also be set to a function returning
  3022. a string or nil, which will be called with the depth of the
  3023. item, counting from 1.
  3024. :icount
  3025. Strings to start a list item with a counter. It can also be
  3026. set to a function returning a string or nil, which will be
  3027. called with two arguments: the depth of the item, counting from
  3028. 1, and the counter. Its value, when non-nil, has precedence
  3029. over `:istart'.
  3030. :isep
  3031. String used to separate items. It can also be set to
  3032. a function returning a string or nil, which will be called with
  3033. the depth of the items, counting from 1. It always start on
  3034. a new line.
  3035. :cbon, :cboff, :cbtrans
  3036. String to insert, respectively, an un-checked check-box,
  3037. a checked check-box and a check-box in transitional state."
  3038. (require 'ox)
  3039. (let* ((backend (plist-get params :backend))
  3040. (custom-backend
  3041. (org-export-create-backend
  3042. :parent (or backend 'org)
  3043. :transcoders
  3044. `((plain-list . ,(org-list--to-generic-plain-list params))
  3045. (item . ,(org-list--to-generic-item params))
  3046. (macro . (lambda (m c i) (org-element-macro-interpreter m nil))))))
  3047. data info)
  3048. ;; Write LIST back into Org syntax and parse it.
  3049. (with-temp-buffer
  3050. (let ((org-inhibit-startup t)) (org-mode))
  3051. (letrec ((insert-list
  3052. (lambda (l)
  3053. (dolist (i (cdr l))
  3054. (funcall insert-item i (car l)))))
  3055. (insert-item
  3056. (lambda (i type)
  3057. (let ((start (point)))
  3058. (insert (if (eq type 'ordered) "1. " "- "))
  3059. (dolist (e i)
  3060. (if (consp e) (funcall insert-list e)
  3061. (insert e)
  3062. (insert "\n")))
  3063. (beginning-of-line)
  3064. (save-excursion
  3065. (let ((ind (if (eq type 'ordered) 3 2)))
  3066. (while (> (point) start)
  3067. (unless (looking-at-p "[ \t]*$")
  3068. (indent-to ind))
  3069. (forward-line -1))))))))
  3070. (funcall insert-list list))
  3071. (setf data
  3072. (org-element-map (org-element-parse-buffer) 'plain-list
  3073. #'identity nil t))
  3074. (setf info (org-export-get-environment backend nil params)))
  3075. (when (and backend (symbolp backend) (not (org-export-get-backend backend)))
  3076. (user-error "Unknown :backend value"))
  3077. (unless backend (require 'ox-org))
  3078. ;; When`:raw' property has a non-nil value, turn all objects back
  3079. ;; into Org syntax.
  3080. (when (and backend (plist-get params :raw))
  3081. (org-element-map data org-element-all-objects
  3082. (lambda (object)
  3083. (org-element-set-element
  3084. object (org-element-interpret-data object)))))
  3085. ;; We use a low-level mechanism to export DATA so as to skip all
  3086. ;; usual pre-processing and post-processing, i.e., hooks, filters,
  3087. ;; Babel code evaluation, include keywords and macro expansion,
  3088. ;; and filters.
  3089. (let ((output (org-export-data-with-backend data custom-backend info)))
  3090. ;; Remove final newline.
  3091. (if (org-string-nw-p output) (substring-no-properties output 0 -1) ""))))
  3092. (defun org-list--depth (element)
  3093. "Return the level of ELEMENT within current plain list.
  3094. ELEMENT is either an item or a plain list."
  3095. (cl-count-if (lambda (ancestor) (eq (org-element-type ancestor) 'plain-list))
  3096. (org-element-lineage element nil t)))
  3097. (defun org-list--trailing-newlines (string)
  3098. "Return the number of trailing newlines in STRING."
  3099. (with-temp-buffer
  3100. (insert string)
  3101. (skip-chars-backward " \t\n")
  3102. (count-lines (line-beginning-position 2) (point-max))))
  3103. (defun org-list--generic-eval (value &rest args)
  3104. "Evaluate VALUE according to its type.
  3105. VALUE is either nil, a string or a function. In the latter case,
  3106. it is called with arguments ARGS."
  3107. (cond ((null value) nil)
  3108. ((stringp value) value)
  3109. ((functionp value) (apply value args))
  3110. (t (error "Wrong value: %s" value))))
  3111. (defun org-list--to-generic-plain-list (params)
  3112. "Return a transcoder for `plain-list' elements.
  3113. PARAMS is a plist used to tweak the behavior of the transcoder."
  3114. (let ((ustart (plist-get params :ustart))
  3115. (uend (plist-get params :uend))
  3116. (ostart (plist-get params :ostart))
  3117. (oend (plist-get params :oend))
  3118. (dstart (plist-get params :dstart))
  3119. (dend (plist-get params :dend))
  3120. (splice (plist-get params :splice))
  3121. (backend (plist-get params :backend)))
  3122. (lambda (plain-list contents info)
  3123. (let* ((type (org-element-property :type plain-list))
  3124. (depth (org-list--depth plain-list))
  3125. (start (and (not splice)
  3126. (org-list--generic-eval
  3127. (pcase type
  3128. (`ordered ostart)
  3129. (`unordered ustart)
  3130. (_ dstart))
  3131. depth)))
  3132. (end (and (not splice)
  3133. (org-list--generic-eval
  3134. (pcase type
  3135. (`ordered oend)
  3136. (`unordered uend)
  3137. (_ dend))
  3138. depth))))
  3139. ;; Make sure trailing newlines in END appear in the output by
  3140. ;; setting `:post-blank' property to their number.
  3141. (when end
  3142. (org-element-put-property
  3143. plain-list :post-blank (org-list--trailing-newlines end)))
  3144. ;; Build output.
  3145. (concat (and start (concat start "\n"))
  3146. (if (or start end splice (not backend))
  3147. contents
  3148. (org-export-with-backend backend plain-list contents info))
  3149. end)))))
  3150. (defun org-list--to-generic-item (params)
  3151. "Return a transcoder for `item' elements.
  3152. PARAMS is a plist used to tweak the behavior of the transcoder."
  3153. (let ((backend (plist-get params :backend))
  3154. (istart (plist-get params :istart))
  3155. (iend (plist-get params :iend))
  3156. (isep (plist-get params :isep))
  3157. (icount (plist-get params :icount))
  3158. (cboff (plist-get params :cboff))
  3159. (cbon (plist-get params :cbon))
  3160. (cbtrans (plist-get params :cbtrans))
  3161. (dtstart (plist-get params :dtstart))
  3162. (dtend (plist-get params :dtend))
  3163. (ddstart (plist-get params :ddstart))
  3164. (ddend (plist-get params :ddend)))
  3165. (lambda (item contents info)
  3166. (let* ((type
  3167. (org-element-property :type (org-element-property :parent item)))
  3168. (tag (org-element-property :tag item))
  3169. (depth (org-list--depth item))
  3170. (separator (and (org-export-get-next-element item info)
  3171. (org-list--generic-eval isep depth)))
  3172. (closing (pcase (org-list--generic-eval iend depth)
  3173. ((or `nil `"") "\n")
  3174. ((and (guard separator) s)
  3175. (if (equal (substring s -1) "\n") s (concat s "\n")))
  3176. (s s))))
  3177. ;; When a closing line or a separator is provided, make sure
  3178. ;; its trailing newlines are taken into account when building
  3179. ;; output. This is done by setting `:post-blank' property to
  3180. ;; the number of such lines in the last line to be added.
  3181. (let ((last-string (or separator closing)))
  3182. (when last-string
  3183. (org-element-put-property
  3184. item
  3185. :post-blank
  3186. (max (1- (org-list--trailing-newlines last-string)) 0))))
  3187. ;; Build output.
  3188. (concat
  3189. (let ((c (org-element-property :counter item)))
  3190. (if c (org-list--generic-eval icount depth c)
  3191. (org-list--generic-eval istart depth)))
  3192. (let ((body
  3193. (if (or istart iend icount cbon cboff cbtrans (not backend)
  3194. (and (eq type 'descriptive)
  3195. (or dtstart dtend ddstart ddend)))
  3196. (concat
  3197. (pcase (org-element-property :checkbox item)
  3198. (`on cbon)
  3199. (`off cboff)
  3200. (`trans cbtrans))
  3201. (and tag
  3202. (concat dtstart
  3203. (if backend
  3204. (org-export-data-with-backend
  3205. tag backend info)
  3206. (org-element-interpret-data tag))
  3207. dtend))
  3208. (and tag ddstart)
  3209. (if (= (length contents) 0) "" (substring contents 0 -1))
  3210. (and tag ddend))
  3211. (org-export-with-backend backend item contents info))))
  3212. ;; Remove final newline.
  3213. (if (equal body "") ""
  3214. (substring (org-element-normalize-string body) 0 -1)))
  3215. closing
  3216. separator)))))
  3217. (defun org-list-to-latex (list &optional params)
  3218. "Convert LIST into a LaTeX list.
  3219. LIST is a parsed plain list, as returned by `org-list-to-lisp'.
  3220. Return converted list as a string."
  3221. (require 'ox-latex)
  3222. (org-list-to-generic list (org-combine-plists '(:backend latex) params)))
  3223. (defun org-list-to-html (list &optional params)
  3224. "Convert LIST into a HTML list.
  3225. LIST is a parsed plain list, as returned by `org-list-to-lisp'.
  3226. Return converted list as a string."
  3227. (require 'ox-html)
  3228. (org-list-to-generic list (org-combine-plists '(:backend html) params)))
  3229. (defun org-list-to-texinfo (list &optional params)
  3230. "Convert LIST into a Texinfo list.
  3231. LIST is a parsed plain list, as returned by `org-list-to-lisp'.
  3232. Return converted list as a string."
  3233. (require 'ox-texinfo)
  3234. (org-list-to-generic list (org-combine-plists '(:backend texinfo) params)))
  3235. (defun org-list-to-subtree (list &optional params)
  3236. "Convert LIST into an Org subtree.
  3237. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  3238. with overruling parameters for `org-list-to-generic'."
  3239. (let* ((blank (pcase (cdr (assq 'heading org-blank-before-new-entry))
  3240. (`t t)
  3241. (`auto (save-excursion
  3242. (org-with-limited-levels (outline-previous-heading))
  3243. (org-previous-line-empty-p)))))
  3244. (level (org-reduced-level (or (org-current-level) 0)))
  3245. (make-stars
  3246. (lambda (depth)
  3247. ;; Return the string for the heading, depending on DEPTH
  3248. ;; of current sub-list.
  3249. (let ((oddeven-level (+ level depth)))
  3250. (concat (make-string (if org-odd-levels-only
  3251. (1- (* 2 oddeven-level))
  3252. oddeven-level)
  3253. ?*)
  3254. " ")))))
  3255. (org-list-to-generic
  3256. list
  3257. (org-combine-plists
  3258. (list :splice t
  3259. :istart make-stars
  3260. :icount make-stars
  3261. :dtstart " " :dtend " "
  3262. :isep (if blank "\n\n" "\n")
  3263. :cbon "DONE " :cboff "TODO " :cbtrans "TODO ")
  3264. params))))
  3265. (provide 'org-list)
  3266. ;;; org-list.el ends here