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