org-list.el 111 KB

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