org-list.el 130 KB

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