org-list.el 98 KB

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