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