org-list.el 107 KB

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