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