org-list.el 107 KB

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