org-list.el 130 KB

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