org-list.el 116 KB

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