org-list.el 130 KB

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