org-list.el 119 KB

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