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