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