org-list.el 127 KB

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