org-list.el 130 KB

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