org-list.el 112 KB

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