org-list.el 135 KB

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