org-list.el 109 KB

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