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