org-list.el 128 KB

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