org-list.el 111 KB

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