org-list.el 106 KB

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