org-list.el 105 KB

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