org-list.el 127 KB

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