org-list.el 127 KB

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