1 ;;; org-list.el --- Plain lists for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Bastien Guerry <bzg AT altern DOT org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; This file contains the code dealing with plain lists in Org-mode.
32 ;; The fundamental idea behind lists work is to use structures.
33 ;; A structure is a snapshot of the list, in the shape of data tree
34 ;; (see `org-list-struct').
36 ;; Once the list structure is stored, it is possible to make changes
37 ;; directly on it or get useful information about the list, with the
38 ;; two helper functions, namely `org-list-parents-alist' and
39 ;; `org-list-prevs-alist', and using accessors or methods.
41 ;; Structure is eventually applied to the buffer with
42 ;; `org-list-write-struct'. This function repairs (bullets,
43 ;; indentation, checkboxes) the structure before applying it. It
44 ;; should be called near the end of any function working on
47 ;; Thus, a function applying to lists should usually follow this
50 ;; 1. Verify point is in a list and grab item beginning (with the same
51 ;; function `org-in-item-p'). If the function requires the cursor
52 ;; to be at item's bullet, `org-at-item-p' is more selective. If
53 ;; the cursor is amidst the buffer, it is possible to find the
54 ;; closest item with `org-list-search-backward', or
55 ;; `org-list-search-forward', applied to `org-item-beginning-re'.
57 ;; 2. Get list structure with `org-list-struct'.
59 ;; 3. Compute one, or both, helper functions,
60 ;; (`org-list-parents-alist', `org-list-prevs-alist') depending on
63 ;; 4. Proceed with the modifications, using methods and accessors.
65 ;; 5. Verify and apply structure to buffer, using
66 ;; `org-list-write-struct'. Possibly use
67 ;; `org-update-checkbox-count-maybe' if checkboxes might have been
70 ;; Computing a list structure can be a costly operation on huge lists
71 ;; (a few thousand lines long). Thus, code should follow the rule :
72 ;; "collect once, use many". As a corollary, it is usally a bad idea
73 ;; to use directly an interactive function inside the code, as those,
74 ;; being independant entities, read the whole list structure another
84 (defvar org-M-RET-may-split-line
)
85 (defvar org-auto-align-tags
)
86 (defvar org-blank-before-new-entry
)
87 (defvar org-clock-string
)
88 (defvar org-closed-string
)
89 (defvar org-deadline-string
)
90 (defvar org-description-max-indent
)
92 (defvar org-odd-levels-only
)
93 (defvar org-scheduled-string
)
94 (defvar org-ts-regexp
)
95 (defvar org-ts-regexp-both
)
97 (declare-function org-at-heading-p
"org" (&optional ignored
))
98 (declare-function org-before-first-heading-p
"org" ())
99 (declare-function org-back-over-empty-lines
"org" ())
100 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
101 (declare-function org-combine-plists
"org" (&rest plists
))
102 (declare-function org-count
"org" (cl-item cl-seq
))
103 (declare-function org-current-level
"org" ())
104 (declare-function org-entry-get
"org"
105 (pom property
&optional inherit literal-nil
))
106 (declare-function org-fix-tags-on-the-fly
"org" ())
107 (declare-function org-get-indentation
"org" (&optional line
))
108 (declare-function org-icompleting-read
"org" (&rest args
))
109 (declare-function org-in-block-p
"org" (names))
110 (declare-function org-in-regexp
"org" (re &optional nlines visually
))
111 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
112 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
113 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
114 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
115 (declare-function org-level-increment
"org" ())
116 (declare-function org-narrow-to-subtree
"org" ())
117 (declare-function org-on-heading-p
"org" (&optional invisible-ok
))
118 (declare-function org-previous-line-empty-p
"org" ())
119 (declare-function org-remove-if
"org" (predicate seq
))
120 (declare-function org-reduced-level
"org" (L))
121 (declare-function org-show-subtree
"org" ())
122 (declare-function org-time-string-to-seconds
"org" (s))
123 (declare-function org-timer-hms-to-secs
"org-timer" (hms))
124 (declare-function org-timer-item
"org-timer" (&optional arg
))
125 (declare-function org-trim
"org" (s))
126 (declare-function org-uniquify
"org" (list))
127 (declare-function outline-invisible-p
"outline" (&optional pos
))
128 (declare-function outline-flag-region
"outline" (from to flag
))
129 (declare-function outline-next-heading
"outline" ())
130 (declare-function outline-previous-heading
"outline" ())
132 ;;; Configuration variables
134 (defgroup org-plain-lists nil
135 "Options concerning plain lists in Org-mode."
136 :tag
"Org Plain lists"
137 :group
'org-structure
)
139 (defcustom org-cycle-include-plain-lists t
140 "When t, make TAB cycle visibility on plain list items.
141 Cycling plain lists works only when the cursor is on a plain list
142 item. When the cursor is on an outline heading, plain lists are
143 treated as text. This is the most stable way of handling this,
144 which is why it is the default.
146 When this is the symbol `integrate', then during cycling, plain
147 list items will *temporarily* be interpreted as outline headlines
148 with a level given by 1000+i where i is the indentation of the
149 bullet. This setting can lead to strange effects when switching
150 visibility to `children', because the first \"child\" in a
151 subtree decides what children should be listed. If that first
152 \"child\" is a plain list item with an implied large level
153 number, all true children and grand children of the outline
154 heading will be exposed in a children' view."
155 :group
'org-plain-lists
157 (const :tag
"Never" nil
)
158 (const :tag
"With cursor in plain list (recommended)" t
)
159 (const :tag
"As children of outline headings" integrate
)))
161 (defcustom org-list-demote-modify-bullet nil
162 "Default bullet type installed when demoting an item.
163 This is an association list, for each bullet type, this alist will point
164 to the bullet that should be used when this item is demoted.
167 (setq org-list-demote-modify-bullet
168 '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
173 + Silence of the Lambs
176 + The Hunt for Red October
182 - Silence of the Lambs
185 - The Hunt for Red October
187 :group
'org-plain-lists
190 (choice :tag
"If the current bullet is "
196 (choice :tag
"demotion will change it to"
203 (defcustom org-plain-list-ordered-item-terminator t
204 "The character that makes a line with leading number an ordered list item.
205 Valid values are ?. and ?\). To get both terminators, use t."
206 :group
'org-plain-lists
207 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
208 (const :tag
"paren like in \"2)\"" ?\
))
209 (const :tab
"both" t
)))
211 (defcustom org-alphabetical-lists nil
212 "Non-nil means single character alphabetical bullets are allowed.
213 Both uppercase and lowercase are handled. Lists with more than
214 26 items will fallback to standard numbering. Alphabetical
215 counters like \"[@c]\" will be recognized."
216 :group
'org-plain-lists
219 (defcustom org-list-two-spaces-after-bullet-regexp nil
220 "A regular expression matching bullets that should have 2 spaces after them.
221 When nil, no bullet will have two spaces after them. When
222 a string, it will be used as a regular expression. When the
223 bullet type of a list is changed, the new bullet type will be
224 matched against this regexp. If it matches, there will be two
225 spaces instead of one after the bullet in each item of the list."
226 :group
'org-plain-lists
228 (const :tag
"never" nil
)
231 (defcustom org-list-ending-method
'both
232 "Determine where plain lists should end.
233 Valid values are: `regexp', `indent' or `both'.
235 When set to `regexp', Org will look into two variables,
236 `org-empty-line-terminates-plain-lists' and the more general
237 `org-list-end-regexp', to determine what will end lists.
239 When set to `indent', a list will end whenever a line following
240 an item, but not starting one, is less or equally indented than
241 the first item of the list.
243 When set to `both', each of the preceding methods is applied to
244 determine lists endings. This is the default method."
245 :group
'org-plain-lists
247 (const :tag
"With a regexp defining ending" regexp
)
248 (const :tag
"With indentation of regular (no bullet) text" indent
)
249 (const :tag
"With both methods" both
)))
251 (defcustom org-empty-line-terminates-plain-lists nil
252 "Non-nil means an empty line ends all plain list levels.
253 This variable only makes sense if `org-list-ending-method' is set
254 to `regexp' or `both'. This is then equivalent to set
255 `org-list-end-regexp' to \"^[ \\t]*$\"."
256 :group
'org-plain-lists
259 (defcustom org-list-end-regexp
"^[ \t]*\n[ \t]*\n"
260 "Regexp matching the end of all plain list levels.
261 It must start with \"^\" and end with \"\\n\". It defaults to 2
262 blank lines. `org-empty-line-terminates-plain-lists' has
264 :group
'org-plain-lists
267 (defcustom org-list-automatic-rules
'((bullet . t
)
270 "Non-nil means apply set of rules when acting on lists.
271 By default, automatic actions are taken when using
272 \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
273 \\[org-shiftmetaright], \\[org-shiftmetaleft],
274 \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
275 \\[org-insert-todo-heading]. You can disable individually these
276 rules by setting them to nil. Valid rules are:
278 bullet when non-nil, cycling bullet do not allow lists at
279 column 0 to have * as a bullet and descriptions lists
281 checkbox when non-nil, checkbox statistics is updated each time
282 you either insert a new checkbox or toggle a checkbox.
283 It also prevents from inserting a checkbox in a
285 indent when non-nil, indenting or outdenting list top-item
286 with its subtree will move the whole list and
287 outdenting a list whose bullet is * to column 0 will
288 change that bullet to \"-\"."
289 :group
'org-plain-lists
290 :type
'(alist :tag
"Sets of rules"
293 (const :tag
"Bullet" bullet
)
294 (const :tag
"Checkbox" checkbox
)
295 (const :tag
"Indent" indent
))
297 (boolean :tag
"Activate" :value t
)))
299 (defcustom org-list-use-circular-motion nil
300 "Non-nil means commands implying motion in lists should be cyclic.
302 In that case, the item following the last item is the first one,
303 and the item preceding the first item is the last one.
305 This affects the behavior of \\[org-move-item-up],
306 \\[org-move-item-down], \\[org-next-item] and
307 \\[org-previous-item]."
308 :group
'org-plain-lists
311 (defvar org-checkbox-statistics-hook nil
312 "Hook that is run whenever Org thinks checkbox statistics should be updated.
313 This hook runs even if checkbox rule in
314 `org-list-automatic-rules' does not apply, so it can be used to
315 implement alternative ways of collecting statistics
318 (defcustom org-hierarchical-checkbox-statistics t
319 "Non-nil means checkbox statistics counts only the state of direct children.
320 When nil, all boxes below the cookie are counted.
321 This can be set to nil on a per-node basis using a COOKIE_DATA property
322 with the word \"recursive\" in the value."
323 :group
'org-plain-lists
326 (defcustom org-description-max-indent
20
327 "Maximum indentation for the second line of a description list.
328 When the indentation would be larger than this, it will become
329 5 characters instead."
330 :group
'org-plain-lists
333 (defcustom org-list-indent-offset
0
334 "Additional indentation for sub-items in a list.
335 By setting this to a small number, usually 1 or 2, one can more
336 clearly distinguish sub-items in a list."
337 :group
'org-plain-lists
340 (defcustom org-list-radio-list-templates
341 '((latex-mode "% BEGIN RECEIVE ORGLST %n
342 % END RECEIVE ORGLST %n
344 #+ORGLST: SEND %n org-list-to-latex
347 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
348 @c END RECEIVE ORGLST %n
350 #+ORGLST: SEND %n org-list-to-texinfo
353 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
354 <!-- END RECEIVE ORGLST %n -->
356 #+ORGLST: SEND %n org-list-to-html
359 "Templates for radio lists in different major modes.
360 All occurrences of %n in a template will be replaced with the name of the
361 list, obtained by prompting the user."
362 :group
'org-plain-lists
364 (list (symbol :tag
"Major mode")
365 (string :tag
"Format"))))
367 (defvar org-list-forbidden-blocks
'("example" "verse" "src" "ascii" "beamer"
368 "docbook" "html" "latex" "odt")
369 "Names of blocks where lists are not allowed.
370 Names must be in lower case.")
372 (defvar org-list-export-context
'(block inlinetask
)
373 "Context types where lists will be interpreted during export.
375 Valid types are `drawer', `inlinetask' and `block'. More
376 specifically, type `block' is determined by the variable
377 `org-list-forbidden-blocks'.")
380 ;;; Predicates and regexps
382 (defconst org-list-end-re
(if org-empty-line-terminates-plain-lists
385 "Regex corresponding to the end of a list.
386 It depends on `org-empty-line-terminates-plain-lists'.")
388 (defconst org-list-full-item-re
389 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\)"
390 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
391 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
392 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
393 "Matches a list item and puts everything into groups:
397 group 4: description tag")
399 (defun org-item-re ()
400 "Return the correct regular expression for plain lists."
402 ((eq org-plain-list-ordered-item-terminator t
) "[.)]")
403 ((= org-plain-list-ordered-item-terminator ?\
)) ")")
404 ((= org-plain-list-ordered-item-terminator ?.
) "\\.")
406 (alpha (if org-alphabetical-lists
"\\|[A-Za-z]" "")))
407 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha
"\\)" term
408 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
410 (defsubst org-item-beginning-re
()
411 "Regexp matching the beginning of a plain list item."
412 (concat "^" (org-item-re)))
414 (defun org-list-at-regexp-after-bullet-p (regexp)
415 "Is point at a list item with REGEXP after bullet?"
418 (goto-char (match-end 0))
419 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
420 (if org-alphabetical-lists
421 "\\([0-9]+\\|[A-Za-z]\\)"
424 ;; Ignore counter if any
425 (when (looking-at counter-re
) (goto-char (match-end 0))))
426 (looking-at regexp
))))
428 (defun org-list-in-valid-context-p ()
429 "Is point in a context where lists are allowed?"
430 (not (org-in-block-p org-list-forbidden-blocks
)))
432 (defun org-in-item-p ()
433 "Return item beginning position when in a plain list, nil otherwise.
434 This checks `org-list-ending-method'."
437 (let* ((case-fold-search t
)
438 (context (org-list-context))
439 (lim-up (car context
))
440 (drawers-re (concat "^[ \t]*:\\("
441 (mapconcat 'regexp-quote org-drawers
"\\|")
443 (inlinetask-re (and (featurep 'org-inlinetask
)
444 (org-inlinetask-outline-regexp)))
445 (item-re (org-item-re))
446 ;; Indentation isn't meaningful when point starts at an empty
447 ;; line or an inline task.
448 (ind-ref (if (or (looking-at "^[ \t]*$")
449 (and inlinetask-re
(looking-at inlinetask-re
)))
451 (org-get-indentation))))
453 ((eq (nth 2 context
) 'invalid
) nil
)
454 ((looking-at item-re
) (point))
456 ;; Detect if cursor in amidst `org-list-end-re'. First, count
457 ;; number HL of hard lines it takes, then call `org-in-regexp'
458 ;; to compute its boundaries END-BOUNDS. When point is
459 ;; in-between, move cursor before regexp beginning.
460 (let ((hl 0) (i -
1) end-bounds
)
461 (when (and (not (eq org-list-ending-method
'indent
))
463 (while (setq i
(string-match
464 "[\r\n]" org-list-end-re
(1+ i
)))
466 (setq end-bounds
(org-in-regexp org-list-end-re hl
)))
467 (>= (point) (car end-bounds
))
468 (< (point) (cdr end-bounds
)))
469 (goto-char (car end-bounds
))
471 ;; Look for an item, less indented that reference line if
472 ;; `org-list-ending-method' isn't `regexp'.
475 (let ((ind (org-get-indentation)))
477 ;; This is exactly what we want.
478 ((and (looking-at item-re
)
480 (eq org-list-ending-method
'regexp
)))
481 (throw 'exit
(point)))
482 ;; At upper bound of search or looking at the end of a
483 ;; previous list: search is over.
484 ((<= (point) lim-up
) (throw 'exit nil
))
485 ((and (not (eq org-list-ending-method
'indent
))
486 (looking-at org-list-end-re
))
488 ;; Skip blocks, drawers, inline-tasks, blank lines
489 ((and (looking-at "^[ \t]*#\\+end_")
490 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
491 ((and (looking-at "^[ \t]*:END:")
492 (re-search-backward drawers-re lim-up t
))
494 ((and inlinetask-re
(looking-at inlinetask-re
))
495 (org-inlinetask-goto-beginning)
497 ((looking-at "^[ \t]*$") (forward-line -
1))
498 ;; Text at column 0 cannot belong to a list: stop.
499 ((zerop ind
) (throw 'exit nil
))
500 ;; Normal text less indented than reference line, take
501 ;; it as new reference.
505 (t (forward-line -
1)))))))))))
507 (defun org-at-item-p ()
508 "Is point in a line starting a hand-formatted item?"
511 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
513 (defun org-at-item-bullet-p ()
514 "Is point at the bullet of a plain list item?"
516 (not (member (char-after) '(?\ ?
\t)))
517 (< (point) (match-end 0))))
519 (defun org-at-item-timer-p ()
520 "Is point at a line starting a plain list item with a timer?"
521 (org-list-at-regexp-after-bullet-p
522 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
524 (defun org-at-item-description-p ()
525 "Is point at a description list item?"
526 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
528 (defun org-at-item-checkbox-p ()
529 "Is point at a line starting a plain-list item with a checklet?"
530 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
532 (defun org-at-item-counter-p ()
533 "Is point at a line starting a plain-list item with a counter?"
535 (looking-at org-list-full-item-re
)
539 ;;; Structures and helper functions
541 (defun org-list-context ()
542 "Determine context, and its boundaries, around point.
544 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
545 are boundaries and CONTEXT is a symbol among `drawer', `block',
546 `invalid', `inlinetask' and nil.
548 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
551 (org-with-limited-levels
553 (let ((case-fold-search t
) (pos (point)) beg end context-type
554 ;; Get positions of surrounding headings. This is the
556 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t
))
559 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
560 ;; Is point inside a drawer?
561 (let ((end-re "^[ \t]*:END:")
562 ;; Can't use org-drawers-regexp as this function might
563 ;; be called in buffers not in Org mode.
564 (beg-re (concat "^[ \t]*:\\("
565 (mapconcat 'regexp-quote org-drawers
"\\|")
567 (when (save-excursion
568 (and (not (looking-at beg-re
))
569 (not (looking-at end-re
))
570 (setq beg
(and (re-search-backward beg-re lim-up t
)
571 (1+ (point-at-eol))))
572 (setq end
(or (and (re-search-forward end-re lim-down t
)
573 (1- (match-beginning 0)))
576 (setq lim-up beg lim-down end context-type
'drawer
)))
577 ;; Is point strictly in a block, and of which type?
578 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type
)
579 (when (save-excursion
580 (and (not (looking-at block-re
))
581 (setq beg
(and (re-search-backward block-re lim-up t
)
582 (1+ (point-at-eol))))
583 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
584 (setq type
(downcase (match-string 1)))
586 (setq end
(or (and (re-search-forward block-re lim-down t
)
590 (equal (downcase (match-string 1)) "end")))
591 (setq lim-up beg lim-down end
592 context-type
(if (member type org-list-forbidden-blocks
)
594 ;; Is point in an inlinetask?
595 (when (and (featurep 'org-inlinetask
)
597 (let* ((beg-re (org-inlinetask-outline-regexp))
598 (end-re (concat beg-re
"END[ \t]*$")))
599 (and (not (looking-at "^\\*+"))
600 (setq beg
(and (re-search-backward beg-re lim-up t
)
601 (1+ (point-at-eol))))
602 (not (looking-at end-re
))
603 (setq end
(and (re-search-forward end-re lim-down t
)
604 (1- (match-beginning 0))))
606 (setq lim-up beg lim-down end context-type
'inlinetask
))
607 ;; Return context boundaries and type.
608 (list lim-up lim-down context-type
))))))
610 (defun org-list-struct ()
611 "Return structure of list at point.
613 A list structure is an alist where key is point at item, and
616 2. bullet with trailing whitespace,
617 3. bullet counter, if any,
619 5. description tag, if any,
620 6. position at item end.
622 Thus the following list, where numbers in parens are
627 5. [@5] sub-item 2 (34)
628 some other text belonging to first item (55)
630 + tag :: description (109)
633 will get the following structure:
635 \(\(1 0 \"- \" nil \"[X]\" nil 97\)
636 \(18 2 \"1. \" nil nil nil 34\)
637 \(34 2 \"5. \" \"5\" nil nil 55\)
638 \(97 0 \"- \" nil nil nil 131\)
639 \(109 2 \"+ \" nil nil \"tag\" 131\)
641 Assume point is at an item."
644 (let* ((case-fold-search t
)
645 (context (org-list-context))
646 (lim-up (car context
))
647 (lim-down (nth 1 context
))
649 (item-re (org-item-re))
650 (drawers-re (concat "^[ \t]*:\\("
651 (mapconcat 'regexp-quote org-drawers
"\\|")
653 (inlinetask-re (and (featurep 'org-inlinetask
)
654 (org-inlinetask-outline-regexp)))
655 (beg-cell (cons (point) (org-get-indentation)))
656 ind itm-lst itm-lst-2 end-lst end-lst-2 struct
659 ;; Return association at point.
661 (looking-at org-list-full-item-re
)
664 (match-string-no-properties 1) ; bullet
665 (match-string-no-properties 2) ; counter
666 (match-string-no-properties 3) ; checkbox
667 (match-string-no-properties 4))))) ; description tag
670 ;; Ensure list ends at the first blank line.
672 (skip-chars-backward " \r\t\n")
673 (min (1+ (point-at-eol)) lim-down
)))))
674 ;; 1. Read list from starting item to its beginning, and save
675 ;; top item position and indentation in BEG-CELL. Also store
676 ;; ending position of items in END-LST.
680 (let ((ind (+ (or (get-text-property (point) 'original-indentation
) 0)
681 (org-get-indentation))))
684 ;; At upward limit: if we ended at an item, store it,
685 ;; else dimiss useless data recorded above BEG-CELL.
689 (if (or (not (looking-at item-re
))
690 (get-text-property (point) 'org-example
))
691 (memq (assq (car beg-cell
) itm-lst
) itm-lst
)
692 (setq beg-cell
(cons (point) ind
))
693 (cons (funcall assoc-at-point ind
) itm-lst
)))))
694 ;; At a verbatim block, go before its beginning. Move
695 ;; from eol to ensure `previous-single-property-change'
696 ;; will return a value.
697 ((get-text-property (point) 'org-example
)
698 (goto-char (previous-single-property-change
699 (point-at-eol) 'org-example nil lim-up
))
701 ;; Looking at a list ending regexp. Dismiss useless
702 ;; data recorded above BEG-CELL. Jump to part 2.
703 ((and (not (eq org-list-ending-method
'indent
))
704 (looking-at org-list-end-re
))
707 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
708 ;; Point is at an item. Add data to ITM-LST. It may
709 ;; also end a previous item: save it in END-LST. If
710 ;; ind is less or equal than BEG-CELL and there is no
711 ;; end at this ind or lesser, this item becomes the new
713 ((looking-at item-re
)
714 (push (funcall assoc-at-point ind
) itm-lst
)
715 (push (cons ind
(point)) end-lst
)
716 (when (or (and (eq org-list-ending-method
'regexp
)
717 (<= ind
(cdr beg-cell
)))
718 (< ind text-min-ind
))
719 (setq beg-cell
(cons (point) ind
)))
721 ;; Skip blocks, drawers, inline tasks, blank lines.
722 ((and (looking-at "^[ \t]*#\\+end_")
723 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
724 ((and (looking-at "^[ \t]*:END:")
725 (re-search-backward drawers-re lim-up t
))
727 ((and inlinetask-re
(looking-at inlinetask-re
))
728 (org-inlinetask-goto-beginning)
730 ((looking-at "^[ \t]*$")
732 ;; From there, point is not at an item. Unless ending
733 ;; method is `regexp', interpret line's indentation:
734 ;; - text at column 0 is necessarily out of any list.
735 ;; Dismiss data recorded above BEG-CELL. Jump to
737 ;; - any other case may be an ending position for an
738 ;; hypothetical item above. Store it and proceed.
739 ((eq org-list-ending-method
'regexp
) (forward-line -
1))
743 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
745 (when (< ind text-min-ind
) (setq text-min-ind ind
))
746 (push (cons ind
(point)) end-lst
)
747 (forward-line -
1)))))))
748 ;; 2. Read list from starting point to its end, that is until we
749 ;; get out of context, or that a non-item line is less or
750 ;; equally indented than BEG-CELL's cdr. Also, store ending
751 ;; position of items in END-LST-2.
754 (let ((ind (+ (or (get-text-property (point) 'original-indentation
) 0)
755 (org-get-indentation))))
757 ((>= (point) lim-down
)
758 ;; At downward limit: this is de facto the end of the
759 ;; list. Save point as an ending position, and jump to
762 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
763 ;; At a verbatim block, move to its end. Point is at bol
764 ;; and 'org-example property is set by whole lines:
765 ;; `next-single-property-change' always return a value.
766 ((get-text-property (point) 'org-example
)
768 (next-single-property-change (point) 'org-example nil lim-down
)))
769 ;; Looking at a list ending regexp. Save point as an
770 ;; ending position and jump to part 3.
771 ((and (not (eq org-list-ending-method
'indent
))
772 (looking-at org-list-end-re
))
773 (throw 'exit
(push (cons 0 (point)) end-lst-2
)))
774 ((looking-at item-re
)
775 ;; Point is at an item. Add data to ITM-LST-2. It may
776 ;; also end a previous item, so save it in END-LST-2.
777 (push (funcall assoc-at-point ind
) itm-lst-2
)
778 (push (cons ind
(point)) end-lst-2
)
780 ;; Skip inline tasks and blank lines along the way
781 ((and inlinetask-re
(looking-at inlinetask-re
))
782 (org-inlinetask-goto-end))
783 ((looking-at "^[ \t]*$")
785 ;; Ind is lesser or equal than BEG-CELL's. The list is
786 ;; over: store point as an ending position and jump to
788 ((and (not (eq org-list-ending-method
'regexp
))
789 (<= ind
(cdr beg-cell
)))
791 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
792 ;; Else, if ind is lesser or equal than previous item's,
793 ;; this is an ending position: store it. In any case,
794 ;; skip block or drawer at point, and move to next line.
796 (when (and (not (eq org-list-ending-method
'regexp
))
797 (<= ind
(nth 1 (car itm-lst-2
))))
798 (push (cons ind
(point)) end-lst-2
))
800 ((and (looking-at "^[ \t]*#\\+begin_")
801 (re-search-forward "^[ \t]*#\\+end_" lim-down t
)))
802 ((and (looking-at drawers-re
)
803 (re-search-forward "^[ \t]*:END:" lim-down t
))))
804 (forward-line 1))))))
805 (setq struct
(append itm-lst
(cdr (nreverse itm-lst-2
)))
806 end-lst
(append end-lst
(cdr (nreverse end-lst-2
))))
807 ;; 3. Correct ill-formed lists by ensuring top item is the least
809 (let ((min-ind (nth 1 (car struct
))))
811 (let ((ind (nth 1 item
))
813 (when (< ind min-ind
)
814 (setcar (cdr item
) min-ind
)
815 ;; Trim bullet so item will be seen as different
816 ;; when compared with repaired version.
817 (setcar (nthcdr 2 item
) (org-trim bul
)))))
819 ;; 4. Associate each item to its end pos.
820 (org-list-struct-assoc-end struct end-lst
)
824 (defun org-list-struct-assoc-end (struct end-list
)
825 "Associate proper ending point to items in STRUCT.
827 END-LIST is a pseudo-alist where car is indentation and cdr is
830 This function modifies STRUCT."
831 (let ((endings end-list
))
834 (let ((pos (car elt
))
836 ;; Remove end candidates behind current item.
837 (while (or (<= (cdar endings
) pos
))
839 ;; Add end position to item assoc.
840 (let ((old-end (nthcdr 6 elt
))
841 (new-end (assoc-default ind endings
'<=)))
843 (setcar old-end new-end
)
844 (setcdr elt
(append (cdr elt
) (list new-end
)))))))
847 (defun org-list-prevs-alist (struct)
848 "Return alist between item and previous item in STRUCT."
849 (let ((item-end-alist (mapcar (lambda (e) (cons (car e
) (nth 6 e
)))
852 (let ((prev (car (rassq (car e
) item-end-alist
))))
853 (cons (car e
) prev
)))
856 (defun org-list-parents-alist (struct)
857 "Return alist between item and parent in STRUCT."
858 (let ((ind-to-ori (list (list (nth 1 (car struct
)))))
859 (prev-pos (list (caar struct
))))
861 (mapcar (lambda (item)
862 (let ((pos (car item
))
864 (prev-ind (caar ind-to-ori
)))
869 (member (assq ind ind-to-ori
) ind-to-ori
))
870 (cons pos
(cdar ind-to-ori
)))
872 (let ((origin (nth 1 prev-pos
)))
873 (push (cons ind origin
) ind-to-ori
)
875 (t (cons pos
(cdar ind-to-ori
))))))
881 (defsubst org-list-get-nth
(n key struct
)
882 "Return the Nth value of KEY in STRUCT."
883 (nth n
(assq key struct
)))
885 (defun org-list-set-nth (n key struct new
)
886 "Set the Nth value of KEY in STRUCT to NEW.
887 \nThis function modifies STRUCT."
888 (setcar (nthcdr n
(assq key struct
)) new
))
890 (defsubst org-list-get-ind
(item struct
)
891 "Return indentation of ITEM in STRUCT."
892 (org-list-get-nth 1 item struct
))
894 (defun org-list-set-ind (item struct ind
)
895 "Set indentation of ITEM in STRUCT to IND.
896 \nThis function modifies STRUCT."
897 (org-list-set-nth 1 item struct ind
))
899 (defsubst org-list-get-bullet
(item struct
)
900 "Return bullet of ITEM in STRUCT."
901 (org-list-get-nth 2 item struct
))
903 (defun org-list-set-bullet (item struct bullet
)
904 "Set bullet of ITEM in STRUCT to BULLET.
905 \nThis function modifies STRUCT."
906 (org-list-set-nth 2 item struct bullet
))
908 (defsubst org-list-get-counter
(item struct
)
909 "Return counter of ITEM in STRUCT."
910 (org-list-get-nth 3 item struct
))
912 (defsubst org-list-get-checkbox
(item struct
)
913 "Return checkbox of ITEM in STRUCT or nil."
914 (org-list-get-nth 4 item struct
))
916 (defun org-list-set-checkbox (item struct checkbox
)
917 "Set checkbox of ITEM in STRUCT to CHECKBOX.
918 \nThis function modifies STRUCT."
919 (org-list-set-nth 4 item struct checkbox
))
921 (defsubst org-list-get-tag
(item struct
)
922 "Return end position of ITEM in STRUCT."
923 (org-list-get-nth 5 item struct
))
925 (defun org-list-get-item-end (item struct
)
926 "Return end position of ITEM in STRUCT."
927 (org-list-get-nth 6 item struct
))
929 (defun org-list-get-item-end-before-blank (item struct
)
930 "Return point at end of ITEM in STRUCT, before any blank line.
931 Point returned is at end of line."
933 (goto-char (org-list-get-item-end item struct
))
934 (skip-chars-backward " \r\t\n")
937 (defun org-list-get-parent (item struct parents
)
938 "Return parent of ITEM or nil.
939 STRUCT is the list structure. PARENTS is the alist of parents,
940 as returned by `org-list-parents-alist'."
941 (let ((parents (or parents
(org-list-parents-alist struct
))))
942 (cdr (assq item parents
))))
944 (defun org-list-has-child-p (item struct
)
945 "Non-nil if ITEM has a child.
947 STRUCT is the list structure.
949 Value returned is the position of the first child of ITEM."
950 (let ((ind (org-list-get-ind item struct
))
951 (child-maybe (car (nth 1 (member (assq item struct
) struct
)))))
952 (when (and child-maybe
953 (< ind
(org-list-get-ind child-maybe struct
)))
956 (defun org-list-get-next-item (item struct prevs
)
957 "Return next item in same sub-list as ITEM, or nil.
958 STRUCT is the list structure. PREVS is the alist of previous
959 items, as returned by `org-list-prevs-alist'."
960 (car (rassq item prevs
)))
962 (defun org-list-get-prev-item (item struct prevs
)
963 "Return previous item in same sub-list as ITEM, or nil.
964 STRUCT is the list structure. PREVS is the alist of previous
965 items, as returned by `org-list-prevs-alist'."
966 (cdr (assq item prevs
)))
968 (defun org-list-get-subtree (item struct
)
969 "List all items having ITEM as a common ancestor, or nil.
970 STRUCT is the list structure."
971 (let* ((item-end (org-list-get-item-end item struct
))
972 (sub-struct (cdr (member (assq item struct
) struct
)))
977 (if (< pos item-end
) (push pos subtree
) (throw 'exit nil
))))
981 (defun org-list-get-all-items (item struct prevs
)
982 "List all items in the same sub-list as ITEM.
983 STRUCT is the list structure. PREVS is the alist of previous
984 items, as returned by `org-list-prevs-alist'."
985 (let ((prev-item item
)
987 before-item after-item
)
988 (while (setq prev-item
(org-list-get-prev-item prev-item struct prevs
))
989 (push prev-item before-item
))
990 (while (setq next-item
(org-list-get-next-item next-item struct prevs
))
991 (push next-item after-item
))
992 (append before-item
(list item
) (nreverse after-item
))))
994 (defun org-list-get-children (item struct parents
)
995 "List all children of ITEM, or nil.
996 STRUCT is the list structure. PARENTS is the alist of parents, as
997 returned by `org-list-parents-alist'."
999 (while (setq child
(car (rassq item parents
)))
1000 (setq parents
(cdr (member (assq child parents
) parents
)))
1004 (defun org-list-get-top-point (struct)
1005 "Return point at beginning of list.
1006 STRUCT is the list structure."
1009 (defun org-list-get-bottom-point (struct)
1010 "Return point at bottom of list.
1011 STRUCT is the list structure."
1013 (mapcar (lambda (e) (org-list-get-item-end (car e
) struct
)) struct
)))
1015 (defun org-list-get-list-begin (item struct prevs
)
1016 "Return point at beginning of sub-list ITEM belongs.
1017 STRUCT is the list structure. PREVS is the alist of previous
1018 items, as returned by `org-list-prevs-alist'."
1019 (let ((first-item item
) prev-item
)
1020 (while (setq prev-item
(org-list-get-prev-item first-item struct prevs
))
1021 (setq first-item prev-item
))
1024 (defalias 'org-list-get-first-item
'org-list-get-list-begin
)
1026 (defun org-list-get-last-item (item struct prevs
)
1027 "Return point at last item of sub-list ITEM belongs.
1028 STRUCT is the list structure. PREVS is the alist of previous
1029 items, as returned by `org-list-prevs-alist'."
1030 (let ((last-item item
) next-item
)
1031 (while (setq next-item
(org-list-get-next-item last-item struct prevs
))
1032 (setq last-item next-item
))
1035 (defun org-list-get-list-end (item struct prevs
)
1036 "Return point at end of sub-list ITEM belongs.
1037 STRUCT is the list structure. PREVS is the alist of previous
1038 items, as returned by `org-list-prevs-alist'."
1039 (org-list-get-item-end (org-list-get-last-item item struct prevs
) struct
))
1041 (defun org-list-get-list-type (item struct prevs
)
1042 "Return the type of the list containing ITEM, as a symbol.
1044 STRUCT is the list structure. PREVS is the alist of previous
1045 items, as returned by `org-list-prevs-alist'.
1047 Possible types are `descriptive', `ordered' and `unordered'. The
1048 type is determined by the first item of the list."
1049 (let ((first (org-list-get-list-begin item struct prevs
)))
1051 ((org-list-get-tag first struct
) 'descriptive
)
1052 ((string-match "[[:alnum:]]" (org-list-get-bullet first struct
)) 'ordered
)
1058 (defun org-list-search-generic (search re bound noerr
)
1059 "Search a string in valid contexts for lists.
1060 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1061 in `re-search-forward'."
1063 (let ((origin (point)))
1065 ;; 1. No match: return to origin or bound, depending on NOERR.
1066 (unless (funcall search re bound noerr
)
1067 (throw 'exit
(and (goto-char (if (memq noerr
'(t nil
)) origin bound
))
1069 ;; 2. Match in valid context: return point. Else, continue
1071 (when (org-list-in-valid-context-p) (throw 'exit
(point)))))))
1073 (defun org-list-search-backward (regexp &optional bound noerror
)
1074 "Like `re-search-backward' but stop only where lists are recognized.
1075 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1076 `re-search-backward'."
1077 (org-list-search-generic #'re-search-backward
1078 regexp
(or bound
(point-min)) noerror
))
1080 (defun org-list-search-forward (regexp &optional bound noerror
)
1081 "Like `re-search-forward' but stop only where lists are recognized.
1082 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1083 `re-search-forward'."
1084 (org-list-search-generic #'re-search-forward
1085 regexp
(or bound
(point-max)) noerror
))
1088 ;;; Methods on structures
1090 (defsubst org-list-bullet-string
(bullet)
1091 "Return BULLET with the correct number of whitespaces.
1092 It determines the number of whitespaces to append by looking at
1093 `org-list-two-spaces-after-bullet-regexp'."
1095 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1097 org-list-two-spaces-after-bullet-regexp bullet
))
1100 (string-match "\\S-+\\([ \t]*\\)" bullet
)
1101 (replace-match spaces nil nil bullet
1))))
1103 (defun org-list-swap-items (beg-A beg-B struct
)
1104 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1105 Blank lines at the end of items are left in place. Return the
1106 new structure after the changes.
1108 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1109 to the same sub-list.
1111 This function modifies STRUCT."
1113 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct
))
1114 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct
))
1115 (end-A (org-list-get-item-end beg-A struct
))
1116 (end-B (org-list-get-item-end beg-B struct
))
1117 (size-A (- end-A-no-blank beg-A
))
1118 (size-B (- end-B-no-blank beg-B
))
1119 (body-A (buffer-substring beg-A end-A-no-blank
))
1120 (body-B (buffer-substring beg-B end-B-no-blank
))
1121 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B
))
1122 (sub-A (cons beg-A
(org-list-get-subtree beg-A struct
)))
1123 (sub-B (cons beg-B
(org-list-get-subtree beg-B struct
))))
1124 ;; 1. Move effectively items in buffer.
1126 (delete-region beg-A end-B-no-blank
)
1127 (insert (concat body-B between-A-no-blank-and-B body-A
))
1128 ;; 2. Now modify struct. No need to re-read the list, the
1129 ;; transformation is just a shift of positions. Some special
1130 ;; attention is required for items ending at END-A and END-B
1131 ;; as empty spaces are not moved there. In others words,
1132 ;; item BEG-A will end with whitespaces that were at the end
1133 ;; of BEG-B and the same applies to BEG-B.
1135 (let ((pos (car e
)))
1139 (let ((end-e (nth 6 e
)))
1140 (setcar e
(+ pos
(- end-B-no-blank end-A-no-blank
)))
1141 (setcar (nthcdr 6 e
)
1142 (+ end-e
(- end-B-no-blank end-A-no-blank
)))
1143 (when (= end-e end-A
) (setcar (nthcdr 6 e
) end-B
))))
1145 (let ((end-e (nth 6 e
)))
1146 (setcar e
(- (+ pos beg-A
) beg-B
))
1147 (setcar (nthcdr 6 e
) (+ end-e
(- beg-A beg-B
)))
1148 (when (= end-e end-B
)
1149 (setcar (nthcdr 6 e
)
1150 (+ beg-A size-B
(- end-A end-A-no-blank
))))))
1152 (let ((end-e (nth 6 e
)))
1153 (setcar e
(+ pos
(- size-B size-A
)))
1154 (setcar (nthcdr 6 e
) (+ end-e
(- size-B size-A
))))))))
1156 (sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))))
1158 (defun org-list-separating-blank-lines-number (pos struct prevs
)
1159 "Return number of blank lines that should separate items in list.
1161 POS is the position of point where `org-list-insert-item' was called.
1163 STRUCT is the list structure. PREVS is the alist of previous
1164 items, as returned by `org-list-prevs-alist'.
1166 Assume point is at item's beginning. If the item is alone, apply
1167 some heuristics to guess the result."
1169 (let ((item (point))
1171 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
1174 ;; Trivial cases where there should be none.
1175 ((or (and (not (eq org-list-ending-method
'indent
))
1176 org-empty-line-terminates-plain-lists
)
1177 (not insert-blank-p
)) 0)
1178 ;; When `org-blank-before-new-entry' says so, it is 1.
1179 ((eq insert-blank-p t
) 1)
1180 ;; `plain-list-item' is 'auto. Count blank lines separating
1181 ;; neighbours items in list.
1182 (t (let ((next-p (org-list-get-next-item item struct prevs
)))
1184 ;; Is there a next item?
1185 (next-p (goto-char next-p
)
1186 (org-back-over-empty-lines))
1187 ;; Is there a previous item?
1188 ((org-list-get-prev-item item struct prevs
)
1189 (org-back-over-empty-lines))
1190 ;; User inserted blank lines, trust him.
1191 ((and (> pos
(org-list-get-item-end-before-blank item struct
))
1194 (skip-chars-backward " \t")
1195 (setq usr-blank
(org-back-over-empty-lines))) 0))
1197 ;; Are there blank lines inside the list so far?
1199 (goto-char (org-list-get-top-point struct
))
1200 (org-list-search-forward "^[ \t]*$" item t
))
1202 ;; Default choice: no blank line.
1205 (defun org-list-insert-item (pos struct prevs
&optional checkbox after-bullet
)
1206 "Insert a new list item at POS and return the new structure.
1207 If POS is before first character after bullet of the item, the
1208 new item will be created before the current one.
1210 STRUCT is the list structure. PREVS is the the alist of previous
1211 items, as returned by `org-list-prevs-alist'.
1213 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1214 after the bullet. Cursor will be after this text once the
1217 This function modifies STRUCT."
1218 (let ((case-fold-search t
))
1219 ;; 1. Get information about list: structure, usual helper
1220 ;; functions, position of point with regards to item start
1221 ;; (BEFOREP), blank lines number separating items (BLANK-NB),
1222 ;; position of split (POS) if we're allowed to (SPLIT-LINE-P).
1223 (let* ((item (progn (goto-char pos
) (goto-char (org-list-get-item-begin))))
1224 (item-end (org-list-get-item-end item struct
))
1225 (item-end-no-blank (org-list-get-item-end-before-blank item struct
))
1226 (beforep (and (looking-at org-list-full-item-re
)
1227 (<= pos
(match-end 0))))
1228 (split-line-p (org-get-alist-option org-M-RET-may-split-line
'item
))
1229 (blank-nb (org-list-separating-blank-lines-number
1231 ;; 2. Build the new item to be created. Concatenate same
1232 ;; bullet as item, checkbox, text AFTER-BULLET if
1233 ;; provided, and text cut from point to end of item
1234 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1235 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1236 ;; between what was cut and what was inserted in buffer
1237 ;; is stored in SIZE-OFFSET.
1238 (ind (org-list-get-ind item struct
))
1239 (ind-size (if indent-tabs-mode
1240 (+ (/ ind tab-width
) (mod ind tab-width
))
1242 (bullet (org-list-bullet-string (org-list-get-bullet item struct
)))
1243 (box (when checkbox
"[ ]"))
1245 (and (not beforep
) split-line-p
1248 (skip-chars-backward " \r\t\n")
1250 (delete-and-extract-region pos item-end-no-blank
))))
1251 (body (concat bullet
(when box
(concat box
" ")) after-bullet
1253 (if (string-match "\\`[ \t]+" text-cut
)
1254 (replace-match "" t t text-cut
)
1257 (item-sep (make-string (1+ blank-nb
) ?
\n))
1258 (item-size (+ ind-size
(length body
) (length item-sep
)))
1259 (size-offset (- item-size
(length text-cut
))))
1260 ;; 4. Insert effectively item into buffer.
1262 (org-indent-to-column ind
)
1263 (insert body item-sep
)
1264 ;; 5. Add new item to STRUCT.
1269 ;; Before inserted item, positions don't change but
1270 ;; an item ending after insertion has its end shifted
1273 (when (> end item
) (setcar (nthcdr 6 e
) (+ end size-offset
))))
1274 ;; Trivial cases where current item isn't split in
1275 ;; two. Just shift every item after new one by
1277 ((or beforep
(not split-line-p
))
1278 (setcar e
(+ p item-size
))
1279 (setcar (nthcdr 6 e
) (+ end item-size
)))
1280 ;; Item is split in two: elements before POS are just
1281 ;; shifted by ITEM-SIZE. In the case item would end
1282 ;; after split POS, ending is only shifted by
1285 (setcar e
(+ p item-size
))
1287 (setcar (nthcdr 6 e
) (+ end item-size
))
1288 (setcar (nthcdr 6 e
) (+ end size-offset
))))
1289 ;; Elements after POS are moved into new item.
1290 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1291 ;; doesn't appear in buffer yet.
1293 (setcar e
(+ p size-offset
(- item pos
(length item-sep
))))
1294 (if (= end item-end
)
1295 (setcar (nthcdr 6 e
) (+ item item-size
))
1296 (setcar (nthcdr 6 e
)
1298 (- item pos
(length item-sep
))))))
1299 ;; Elements at ITEM-END or after are only shifted by
1301 (t (setcar e
(+ p size-offset
))
1302 (setcar (nthcdr 6 e
) (+ end size-offset
))))))
1304 (push (list item ind bullet nil box nil
(+ item item-size
)) struct
)
1305 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1306 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1307 ;; exchange ITEM with the next item in list. Position cursor
1308 ;; after bullet, counter, checkbox, and label.
1311 (setq struct
(org-list-swap-items item
(+ item item-size
) struct
))
1312 (goto-char (org-list-get-next-item
1313 item struct
(org-list-prevs-alist struct
))))
1316 (defun org-list-delete-item (item struct
)
1317 "Remove ITEM from the list and return the new structure.
1319 STRUCT is the list structure."
1320 (let* ((end (org-list-get-item-end item struct
))
1321 (beg (if (= (org-list-get-bottom-point struct
) end
)
1322 ;; If ITEM ends with the list, delete blank lines
1326 (skip-chars-backward " \r\t\n")
1327 (min (1+ (point-at-eol)) (point-max)))
1329 ;; Remove item from buffer.
1330 (delete-region beg end
)
1331 ;; Remove item from structure and shift others items accordingly.
1332 ;; Don't forget to shift also ending position when appropriate.
1333 (let ((size (- end beg
)))
1334 (delq nil
(mapcar (lambda (e)
1335 (let ((pos (car e
)))
1338 (let ((end-e (nth 6 e
)))
1342 (append (butlast e
) (list beg
)))
1344 (append (butlast e
) (list (- end-e size
)))))))
1348 (append (butlast (cdr e
))
1349 (list (- (nth 6 e
) size
))))))))
1352 (defun org-list-send-item (item dest struct
)
1353 "Send ITEM to destination DEST.
1355 STRUCT is the list structure.
1357 DEST can have various values.
1359 If DEST is a buffer position, the function will assume it points
1360 to another item in the same list as ITEM, and will move the
1361 latter just before the former.
1363 If DEST is `begin' \(resp. `end'\), ITEM will be moved at the
1364 beginning \(resp. end\) of the list it belongs to.
1366 If DEST is a string like \"N\", where N is an integer, ITEM will
1367 be moved at the Nth position in the list.
1369 If DEST is `kill', ITEM will be deleted and its body will be
1370 added to the kill-ring.
1372 If DEST is `delete', ITEM will be deleted.
1374 This function returns, destructively, the new list structure."
1375 (let* ((prevs (org-list-prevs-alist struct
))
1376 (item-end (org-list-get-item-end item struct
))
1377 ;; Grab full item body minus its bullet.
1384 (regexp-quote (org-list-get-bullet item struct
))))
1387 ;; Change DEST into a buffer position. A trick is needed
1388 ;; when ITEM is meant to be sent at the end of the list.
1389 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1390 ;; nil and insertion point (INS-POINT) to the first line's
1391 ;; end of the last item, we ensure the new item will be
1392 ;; inserted after the last item, and not after any of its
1393 ;; hypothetical sub-items.
1395 ((or (eq dest
'kill
) (eq dest
'delete
)))
1397 (setq dest
(org-list-get-list-begin item struct prevs
)))
1399 (setq dest
(org-list-get-list-end item struct prevs
))
1401 (goto-char (org-list-get-last-item item struct prevs
))
1403 ((string-match "\\`[0-9]+\\'" dest
)
1404 (let* ((all (org-list-get-all-items item struct prevs
))
1406 (index (mod (string-to-number dest
) len
)))
1407 (if (not (zerop index
))
1408 (setq dest
(nth (1- index
) all
))
1409 ;; Send ITEM at the end of the list.
1410 (setq dest
(org-list-get-list-end item struct prevs
))
1413 (org-list-get-last-item item struct prevs
))
1416 (org-M-RET-may-split-line nil
))
1418 ((eq dest
'delete
) (org-list-delete-item item struct
))
1421 (org-list-delete-item item struct
))
1422 ((and (integerp dest
) (/= item ins-point
))
1423 (setq item
(copy-marker item
))
1424 (setq struct
(org-list-insert-item ins-point struct prevs nil body
))
1425 ;; 1. Structure returned by `org-list-insert-item' may not be
1426 ;; accurate, as it cannot see sub-items included in BODY.
1427 ;; Thus, first compute the real structure so far.
1429 (cons (marker-position item
)
1430 (org-list-get-subtree (marker-position item
) struct
)))
1431 (new-end (org-list-get-item-end (point) struct
))
1432 (old-end (org-list-get-item-end (marker-position item
) struct
))
1434 (shift (- (point) item
)))
1435 ;; 1.1. Remove the item just created in structure.
1436 (setq struct
(delete (assq new-item struct
) struct
))
1437 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1442 (let* ((cell (assq e struct
))
1446 (append (butlast (cdr cell
))
1447 (list (if (= end old-end
)
1451 (lambda (e1 e2
) (< (car e1
) (car e2
))))))
1452 ;; 2. Eventually delete the extra copy of the item and clean
1455 (org-list-delete-item (marker-position item
) struct
)
1456 (move-marker item nil
)))
1459 (defun org-list-struct-outdent (start end struct parents
)
1460 "Outdent items between positions START and END.
1462 STRUCT is the list structure. PARENTS is the alist of items'
1463 parents, as returned by `org-list-parents-alist'.
1465 START is included, END excluded."
1468 (let* ((item (car cell
))
1469 (parent (cdr cell
)))
1471 ;; Item not yet in zone: keep association.
1472 ((< item start
) cell
)
1473 ;; Item out of zone: follow associations in ACC.
1475 (let ((convert (and parent
(assq parent acc
))))
1476 (if convert
(cons item
(cdr convert
)) cell
)))
1477 ;; Item has no parent: error
1479 (error "Cannot outdent top-level items"))
1480 ;; Parent is outdented: keep association.
1482 (push (cons parent item
) acc
) cell
)
1484 ;; Parent isn't outdented: reparent to grand-parent.
1485 (let ((grand-parent (org-list-get-parent
1486 parent struct parents
)))
1487 (push (cons parent item
) acc
)
1488 (cons item grand-parent
))))))))
1489 (mapcar out parents
)))
1491 (defun org-list-struct-indent (start end struct parents prevs
)
1492 "Indent items between positions START and END.
1494 STRUCT is the list structure. PARENTS is the alist of parents
1495 and PREVS is the alist of previous items, returned by,
1496 respectively, `org-list-parents-alist' and
1497 `org-list-prevs-alist'.
1499 START is included and END excluded.
1501 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1502 bullets between START and END."
1504 (set-assoc (lambda (cell) (push cell acc
) cell
))
1505 (change-bullet-maybe
1508 (let* ((bul (org-trim (org-list-get-bullet item struct
)))
1509 (new-bul-p (cdr (assoc bul org-list-demote-modify-bullet
))))
1510 (when new-bul-p
(org-list-set-bullet item struct new-bul-p
))))))
1513 (let* ((item (car cell
))
1514 (parent (cdr cell
)))
1516 ;; Item not yet in zone: keep association.
1517 ((< item start
) cell
)
1519 ;; Item out of zone: follow associations in ACC.
1520 (let ((convert (assq parent acc
)))
1521 (if convert
(cons item
(cdr convert
)) cell
)))
1523 ;; Item is in zone...
1524 (let ((prev (org-list-get-prev-item item struct prevs
)))
1525 ;; Check if bullet needs to be changed.
1526 (funcall change-bullet-maybe item
)
1528 ;; First item indented but not parent: error
1529 ((and (not prev
) (< parent start
))
1530 (error "Cannot indent the first item of a list"))
1531 ;; First item and parent indented: keep same
1533 ((not prev
) (funcall set-assoc cell
))
1534 ;; Previous item not indented: reparent to it.
1535 ((< prev start
) (funcall set-assoc
(cons item prev
)))
1536 ;; Previous item indented: reparent like it.
1539 (cons item
(cdr (assq prev acc
)))))))))))))
1540 (mapcar ind parents
)))
1543 ;;; Repairing structures
1545 (defun org-list-use-alpha-bul-p (first struct prevs
)
1546 "Non-nil if list starting at FIRST can have alphabetical bullets.
1548 STRUCT is list structure. PREVS is the alist of previous items,
1549 as returned by `org-list-prevs-alist'."
1550 (and org-alphabetical-lists
1552 (let ((item first
) (ascii 64) (case-fold-search nil
))
1553 ;; Pretend that bullets are uppercase and check if alphabet
1554 ;; is sufficient, taking counters into account.
1556 (let ((bul (org-list-get-bullet item struct
))
1557 (count (org-list-get-counter item struct
)))
1558 ;; Virtually determine current bullet
1559 (if (and count
(string-match "[a-zA-Z]" count
))
1560 ;; Counters are not case-sensitive.
1561 (setq ascii
(string-to-char (upcase count
)))
1562 (setq ascii
(1+ ascii
)))
1563 ;; Test if bullet would be over z or Z.
1566 (setq item
(org-list-get-next-item item struct prevs
)))))
1567 ;; All items checked. All good.
1570 (defun org-list-inc-bullet-maybe (bullet)
1571 "Increment BULLET if applicable."
1572 (let ((case-fold-search nil
))
1574 ;; Num bullet: increment it.
1575 ((string-match "[0-9]+" bullet
)
1577 (number-to-string (1+ (string-to-number (match-string 0 bullet
))))
1579 ;; Alpha bullet: increment it.
1580 ((string-match "[A-Za-z]" bullet
)
1582 (char-to-string (1+ (string-to-char (match-string 0 bullet
))))
1584 ;; Unordered bullet: leave it.
1587 (defun org-list-struct-fix-bul (struct prevs
)
1588 "Verify and correct bullets in STRUCT.
1589 PREVS is the alist of previous items, as returned by
1590 `org-list-prevs-alist'.
1592 This function modifies STRUCT."
1593 (let ((case-fold-search nil
)
1596 ;; Set bullet of ITEM in STRUCT, depending on the type of
1597 ;; first item of the list, the previous bullet and counter
1600 (let* ((prev (org-list-get-prev-item item struct prevs
))
1601 (prev-bul (and prev
(org-list-get-bullet prev struct
)))
1602 (counter (org-list-get-counter item struct
))
1603 (bullet (org-list-get-bullet item struct
))
1604 (alphap (and (not prev
)
1605 (org-list-use-alpha-bul-p item struct prevs
))))
1606 (org-list-set-bullet
1608 (org-list-bullet-string
1610 ;; Alpha counter in alpha list: use counter.
1612 (string-match "[a-zA-Z]" counter
)
1613 (string-match "[a-zA-Z]" prev-bul
))
1614 ;; Use cond to be sure `string-match' is used in
1618 ((string-match "[a-z]" prev-bul
) (downcase counter
))
1619 ((string-match "[A-Z]" prev-bul
) (upcase counter
)))))
1620 (replace-match real-count nil nil prev-bul
)))
1621 ;; Num counter in a num list: use counter.
1623 (string-match "[0-9]+" counter
)
1624 (string-match "[0-9]+" prev-bul
))
1625 (replace-match counter nil nil prev-bul
))
1626 ;; No counter: increase, if needed, previous bullet.
1628 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct
)))
1629 ;; Alpha counter at first item: use counter.
1630 ((and counter
(org-list-use-alpha-bul-p item struct prevs
)
1631 (string-match "[A-Za-z]" counter
)
1632 (string-match "[A-Za-z]" bullet
))
1635 ((string-match "[a-z]" bullet
) (downcase counter
))
1636 ((string-match "[A-Z]" bullet
) (upcase counter
)))))
1637 (replace-match real-count nil nil bullet
)))
1638 ;; Num counter at first item: use counter.
1640 (string-match "[0-9]+" counter
)
1641 (string-match "[0-9]+" bullet
))
1642 (replace-match counter nil nil bullet
))
1643 ;; First bullet is alpha uppercase: use "A".
1644 ((and alphap
(string-match "[A-Z]" bullet
))
1645 (replace-match "A" nil nil bullet
))
1646 ;; First bullet is alpha lowercase: use "a".
1647 ((and alphap
(string-match "[a-z]" bullet
))
1648 (replace-match "a" nil nil bullet
))
1649 ;; First bullet is num: use "1".
1650 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet
)
1651 (replace-match "1" nil nil bullet
))
1652 ;; Not an ordered list: keep bullet.
1654 (mapc fix-bul
(mapcar 'car struct
))))
1656 (defun org-list-struct-fix-ind (struct parents
&optional bullet-size
)
1657 "Verify and correct indentation in STRUCT.
1659 PARENTS is the alist of parents, as returned by
1660 `org-list-parents-alist'.
1662 If numeric optional argument BULLET-SIZE is set, assume all
1663 bullets in list have this length to determine new indentation.
1665 This function modifies STRUCT."
1666 (let* ((ancestor (org-list-get-top-point struct
))
1667 (top-ind (org-list-get-ind ancestor struct
))
1670 (let ((parent (org-list-get-parent item struct parents
)))
1672 ;; Indent like parent + length of parent's bullet +
1675 item struct
(+ (or bullet-size
1677 (org-list-get-bullet parent struct
)))
1678 (org-list-get-ind parent struct
)
1679 org-list-indent-offset
))
1680 ;; If no parent, indent like top-point.
1681 (org-list-set-ind item struct top-ind
))))))
1682 (mapc new-ind
(mapcar 'car
(cdr struct
)))))
1684 (defun org-list-struct-fix-box (struct parents prevs
&optional ordered
)
1685 "Verify and correct checkboxes in STRUCT.
1687 PARENTS is the alist of parents and PREVS is the alist of
1688 previous items, as returned by, respectively,
1689 `org-list-parents-alist' and `org-list-prevs-alist'.
1691 If ORDERED is non-nil, a checkbox can only be checked when every
1692 checkbox before it is checked too. If there was an attempt to
1693 break this rule, the function will return the blocking item. In
1694 all others cases, the return value will be nil.
1696 This function modifies STRUCT."
1697 (let ((all-items (mapcar 'car struct
))
1702 (mapcar (lambda (child)
1703 (org-list-get-checkbox child struct
))
1704 (org-list-get-children item struct parents
))))
1705 (org-list-set-checkbox
1708 ((and (member "[ ]" box-list
) (member "[X]" box-list
)) "[-]")
1709 ((member "[-]" box-list
) "[-]")
1710 ((member "[X]" box-list
) "[X]")
1711 ((member "[ ]" box-list
) "[ ]")
1712 ;; Parent has no boxed child: leave box as-is.
1713 (t (org-list-get-checkbox item struct
))))))))
1715 ;; 1. List all parents with a checkbox.
1718 (let* ((parent (org-list-get-parent e struct parents
))
1719 (parent-box-p (org-list-get-checkbox parent struct
)))
1720 (when (and parent-box-p
(not (memq parent parent-list
)))
1721 (push parent parent-list
))))
1723 ;; 2. Sort those parents by decreasing indentation.
1724 (setq parent-list
(sort parent-list
1726 (> (org-list-get-ind e1 struct
)
1727 (org-list-get-ind e2 struct
)))))
1728 ;; 3. For each parent, get all children's checkboxes to determine
1729 ;; and set its checkbox accordingly.
1730 (mapc set-parent-box parent-list
)
1731 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1734 (mapcar (lambda (e) (org-list-get-checkbox e struct
)) all-items
))
1735 (after-unchecked (member "[ ]" box-list
)))
1736 ;; There are boxes checked after an unchecked one: fix that.
1737 (when (member "[X]" after-unchecked
)
1738 (let ((index (- (length struct
) (length after-unchecked
))))
1739 (mapc (lambda (e) (org-list-set-checkbox e struct
"[ ]"))
1740 (nthcdr index all-items
))
1741 ;; Verify once again the structure, without ORDERED.
1742 (org-list-struct-fix-box struct parents prevs nil
)
1743 ;; Return blocking item.
1744 (nth index all-items
)))))))
1746 (defun org-list-struct-apply-struct (struct old-struct
)
1747 "Apply set-difference between STRUCT and OLD-STRUCT to the buffer.
1749 OLD-STRUCT is the structure before any modifications, and STRUCT
1750 the structure to be applied. The function will only modify parts
1751 of the list which have changed.
1753 Initial position of cursor is restored after the changes."
1754 (let* ((pos (copy-marker (point)))
1755 (inlinetask-re (and (featurep 'org-inlinetask
)
1756 (org-inlinetask-outline-regexp)))
1757 (item-re (org-item-re))
1758 (box-rule-p (cdr (assq 'checkbox org-list-automatic-rules
)))
1761 ;; Shift the indentation between END and BEG by DELTA.
1762 ;; Start from the line before END.
1763 (lambda (end beg delta
)
1765 (skip-chars-backward " \r\t\n")
1767 (while (or (> (point) beg
)
1768 (and (= (point) beg
)
1769 (not (looking-at item-re
))))
1771 ;; Skip inline tasks.
1772 ((and inlinetask-re
(looking-at inlinetask-re
))
1773 (org-inlinetask-goto-beginning))
1774 ;; Shift only non-empty lines.
1775 ((org-looking-at-p "^[ \t]*\\S-")
1776 (let ((i (org-get-indentation)))
1777 (org-indent-line-to (+ i delta
)))))
1778 (forward-line -
1)))))
1781 ;; Replace ITEM first line elements with new elements from
1782 ;; STRUCT, if appropriate.
1785 (let* ((new-ind (org-list-get-ind item struct
))
1786 (old-ind (org-get-indentation))
1787 (new-bul (org-list-bullet-string
1788 (org-list-get-bullet item struct
)))
1789 (old-bul (org-list-get-bullet item old-struct
))
1790 (new-box (org-list-get-checkbox item struct
)))
1791 (looking-at org-list-full-item-re
)
1792 ;; a. Replace bullet
1793 (unless (equal old-bul new-bul
)
1794 (replace-match new-bul nil nil nil
1))
1795 ;; b. Replace checkbox.
1797 ((and new-box box-rule-p
1798 (save-match-data (org-at-item-description-p)))
1799 (message "Cannot add a checkbox to a description list item"))
1800 ((equal (match-string 3) new-box
))
1801 ((and (match-string 3) new-box
)
1802 (replace-match new-box nil nil nil
3))
1804 ;; (goto-char (or (match-end 2) (match-end 1)))
1805 ;; (skip-chars-backward " \t")
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
)
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
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
)))
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
)))
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
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
))))
1865 ;; 4. Go back to initial position.
1868 (defun org-list-write-struct (struct parents
)
1869 "Correct bullets, checkboxes and indentation in list at point.
1870 STRUCT is the list structure. PARENTS is the alist of parents,
1871 as returned by `org-list-parents-alist'."
1872 ;; Order of functions matters here: checkboxes and endings need
1873 ;; correct indentation to be set, and indentation needs correct
1876 ;; 0. Save a copy of structure before modifications
1877 (let ((old-struct (copy-tree struct
)))
1878 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
1879 ;; order to get items endings and bullets properly
1880 (org-list-struct-fix-ind struct parents
2)
1881 ;; 2. Get pseudo-alist of ending positions and sort it by position.
1882 ;; Then associate them to the structure.
1883 (let (end-list acc-end
)
1885 (let* ((pos (car e
))
1886 (ind-pos (org-list-get-ind pos struct
))
1887 (end-pos (org-list-get-item-end pos struct
)))
1888 (unless (assq end-pos struct
)
1889 ;; To determine real ind of an ending position that is
1890 ;; not at an item, we have to find the item it belongs
1891 ;; to: it is the last item (ITEM-UP), whose ending is
1892 ;; further than the position we're interested in.
1893 (let ((item-up (assoc-default end-pos acc-end
'>)))
1895 ;; Else part is for the bottom point.
1896 (if item-up
(+ (org-list-get-ind item-up struct
) 2) 0)
1899 (push (cons ind-pos pos
) end-list
)
1900 (push (cons end-pos pos
) acc-end
)))
1902 (setq end-list
(sort end-list
(lambda (e1 e2
) (< (cdr e1
) (cdr e2
)))))
1903 (org-list-struct-assoc-end struct end-list
))
1904 ;; 3. Get bullets right.
1905 (let ((prevs (org-list-prevs-alist struct
)))
1906 (org-list-struct-fix-bul struct prevs
)
1907 ;; 4. Now get real indentation.
1908 (org-list-struct-fix-ind struct parents
)
1909 ;; 5. Eventually fix checkboxes.
1910 (org-list-struct-fix-box struct parents prevs
))
1911 ;; 6. Apply structure modifications to buffer.
1912 (org-list-struct-apply-struct struct old-struct
)))
1917 (defun org-apply-on-list (function init-value
&rest args
)
1918 "Call FUNCTION on each item of the list at point.
1919 FUNCTION must be called with at least one argument: INIT-VALUE,
1920 that will contain the value returned by the function at the
1921 previous item, plus ARGS extra arguments.
1923 FUNCTION is applied on items in reverse order.
1925 As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
1926 will return the number of items in the current list.
1928 Sublists of the list are skipped. Cursor is always at the
1929 beginning of the item."
1930 (let* ((struct (org-list-struct))
1931 (prevs (org-list-prevs-alist struct
))
1932 (item (copy-marker (point-at-bol)))
1933 (all (org-list-get-all-items (marker-position item
) struct prevs
))
1937 (setq value
(apply function value args
)))
1942 (defun org-list-set-item-visibility (item struct view
)
1943 "Set visibility of ITEM in STRUCT to VIEW.
1945 Possible values are: `folded', `children' or `subtree'. See
1946 `org-cycle' for more information."
1949 (let ((item-end (org-list-get-item-end-before-blank item struct
)))
1951 (outline-flag-region (save-excursion (goto-char item
) (point-at-eol))
1953 ((eq view
'children
)
1954 ;; First show everything.
1955 (org-list-set-item-visibility item struct
'subtree
)
1956 ;; Then fold every child.
1957 (let* ((parents (org-list-parents-alist struct
))
1958 (children (org-list-get-children item struct parents
)))
1960 (org-list-set-item-visibility e struct
'folded
))
1964 (let ((item-end (org-list-get-item-end item struct
)))
1965 (outline-flag-region item item-end nil
)))))
1967 (defun org-list-item-body-column (item)
1968 "Return column at which body of ITEM should start."
1969 (let (bpos bcol tpos tcol
)
1972 (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?[ \t]+")
1973 (setq bpos
(match-beginning 1) tpos
(match-end 0)
1974 bcol
(progn (goto-char bpos
) (current-column))
1975 tcol
(progn (goto-char tpos
) (current-column)))
1976 (when (> tcol
(+ bcol org-description-max-indent
))
1977 (setq tcol
(+ bcol
5))))
1981 ;;; Interactive functions
1983 (defalias 'org-list-get-item-begin
'org-in-item-p
)
1985 (defun org-beginning-of-item ()
1986 "Go to the beginning of the current item.
1987 Throw an error when not in a list."
1989 (let ((begin (org-in-item-p)))
1990 (if begin
(goto-char begin
) (error "Not in an item"))))
1992 (defun org-beginning-of-item-list ()
1993 "Go to the beginning item of the current list or sublist.
1994 Throw an error when not in a list."
1996 (let ((begin (org-in-item-p)))
1998 (error "Not in an item")
2000 (let* ((struct (org-list-struct))
2001 (prevs (org-list-prevs-alist struct
)))
2002 (goto-char (org-list-get-list-begin begin struct prevs
))))))
2004 (defun org-end-of-item-list ()
2005 "Go to the end of the current list or sublist.
2006 Throw an error when not in a list."
2008 (let ((begin (org-in-item-p)))
2010 (error "Not in an item")
2012 (let* ((struct (org-list-struct))
2013 (prevs (org-list-prevs-alist struct
)))
2014 (goto-char (org-list-get-list-end begin struct prevs
))))))
2016 (defun org-end-of-item ()
2017 "Go to the end of the current item.
2018 Throw an error when not in a list."
2020 (let ((begin (org-in-item-p)))
2022 (error "Not in an item")
2024 (let ((struct (org-list-struct)))
2025 (goto-char (org-list-get-item-end begin struct
))))))
2027 (defun org-previous-item ()
2028 "Move to the beginning of the previous item.
2029 Throw an error when not in a list. Also throw an error when at
2030 first item, unless `org-list-use-circular-motion' is non-nil."
2032 (let ((item (org-in-item-p)))
2034 (error "Not in an item")
2036 (let* ((struct (org-list-struct))
2037 (prevs (org-list-prevs-alist struct
))
2038 (prevp (org-list-get-prev-item item struct prevs
)))
2040 (prevp (goto-char prevp
))
2041 (org-list-use-circular-motion
2042 (goto-char (org-list-get-last-item item struct prevs
)))
2043 (t (error "On first item")))))))
2045 (defun org-next-item ()
2046 "Move to the beginning of the next item.
2047 Throw an error when not in a list. Also throw an error when at
2048 last item, unless `org-list-use-circular-motion' is non-nil."
2050 (let ((item (org-in-item-p)))
2052 (error "Not in an item")
2054 (let* ((struct (org-list-struct))
2055 (prevs (org-list-prevs-alist struct
))
2056 (prevp (org-list-get-next-item item struct prevs
)))
2058 (prevp (goto-char prevp
))
2059 (org-list-use-circular-motion
2060 (goto-char (org-list-get-first-item item struct prevs
)))
2061 (t (error "On last item")))))))
2063 (defun org-move-item-down ()
2064 "Move the item at point down, i.e. swap with following item.
2065 Sub-items (items with larger indentation) are considered part of
2066 the item, so this really moves item trees."
2068 (unless (org-at-item-p) (error "Not at an item"))
2069 (let* ((col (current-column))
2070 (item (point-at-bol))
2071 (struct (org-list-struct))
2072 (prevs (org-list-prevs-alist struct
))
2073 (next-item (org-list-get-next-item (point-at-bol) struct prevs
)))
2074 (unless (or next-item org-list-use-circular-motion
)
2075 (error "Cannot move this item further down"))
2077 (setq struct
(org-list-send-item item
'begin struct
))
2078 (setq struct
(org-list-swap-items item next-item struct
))
2080 (org-list-get-next-item item struct
(org-list-prevs-alist struct
))))
2081 (org-list-write-struct struct
(org-list-parents-alist struct
))
2082 (org-move-to-column col
)))
2084 (defun org-move-item-up ()
2085 "Move the item at point up, i.e. swap with previous item.
2086 Sub-items (items with larger indentation) are considered part of
2087 the item, so this really moves item trees."
2089 (unless (org-at-item-p) (error "Not at an item"))
2090 (let* ((col (current-column))
2091 (item (point-at-bol))
2092 (struct (org-list-struct))
2093 (prevs (org-list-prevs-alist struct
))
2094 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs
)))
2095 (unless (or prev-item org-list-use-circular-motion
)
2096 (error "Cannot move this item further up"))
2098 (setq struct
(org-list-send-item item
'end struct
))
2099 (setq struct
(org-list-swap-items prev-item item struct
)))
2100 (org-list-write-struct struct
(org-list-parents-alist struct
))
2101 (org-move-to-column col
)))
2103 (defun org-insert-item (&optional checkbox
)
2104 "Insert a new item at the current level.
2105 If cursor is before first character after bullet of the item, the
2106 new item will be created before the current one.
2108 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2110 Return t when things worked, nil when we are not in an item, or
2112 (let ((itemp (org-in-item-p))
2114 ;; If cursor isn't is a list or if list is invisible, return nil.
2115 (unless (or (not itemp
)
2118 (outline-invisible-p)))
2121 (org-at-item-timer-p))
2122 ;; Timer list: delegate to `org-timer-item'.
2123 (progn (org-timer-item) t
)
2125 (let* ((struct (org-list-struct))
2126 (prevs (org-list-prevs-alist struct
))
2127 ;; If we're in a description list, ask for the new term.
2128 (desc (when (org-list-get-tag itemp struct
)
2129 (concat (read-string "Term: ") " :: ")))
2130 ;; Don't insert a checkbox if checkbox rule is applied
2131 ;; and it is a description item.
2132 (checkp (and checkbox
2134 (not (cdr (assq 'checkbox
2135 org-list-automatic-rules
)))))))
2137 (org-list-insert-item pos struct prevs checkp desc
))
2138 (org-list-write-struct struct
(org-list-parents-alist struct
))
2139 (when checkp
(org-update-checkbox-count-maybe))
2140 (looking-at org-list-full-item-re
)
2141 (goto-char (match-end 0))
2144 (defun org-list-repair ()
2145 "Fix indentation, bullets and checkboxes is the list at point."
2147 (unless (org-at-item-p) (error "This is not a list"))
2148 (let* ((struct (org-list-struct))
2149 (parents (org-list-parents-alist struct
)))
2150 (org-list-write-struct struct parents
)))
2152 (defun org-cycle-list-bullet (&optional which
)
2153 "Cycle through the different itemize/enumerate bullets.
2154 This cycle the entire list level through the sequence:
2156 `-' -> `+' -> `*' -> `1.' -> `1)'
2158 If WHICH is a valid string, use that as the new bullet. If WHICH
2159 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2160 `previous', cycle backwards."
2162 (unless (org-at-item-p) (error "Not at an item"))
2165 (let* ((struct (org-list-struct))
2166 (parents (org-list-parents-alist struct
))
2167 (prevs (org-list-prevs-alist struct
))
2168 (list-beg (org-list-get-first-item (point) struct prevs
))
2169 (bullet (org-list-get-bullet list-beg struct
))
2170 (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules
)))
2171 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs
))
2172 (case-fold-search nil
)
2174 ((string-match "[a-z]\\." bullet
) "a.")
2175 ((string-match "[a-z])" bullet
) "a)")
2176 ((string-match "[A-Z]\\." bullet
) "A.")
2177 ((string-match "[A-Z])" bullet
) "A)")
2178 ((string-match "\\." bullet
) "1.")
2179 ((string-match ")" bullet
) "1)")
2180 (t (org-trim bullet
))))
2181 ;; Compute list of possible bullets, depending on context.
2184 ;; *-bullets are not allowed at column 0.
2185 (unless (and bullet-rule-p
2186 (looking-at "\\S-")) '("*"))
2187 ;; Description items cannot be numbered.
2188 (unless (or (eq org-plain-list-ordered-item-terminator ?\
))
2189 (and bullet-rule-p
(org-at-item-description-p)))
2191 (unless (or (eq org-plain-list-ordered-item-terminator ?.
)
2192 (and bullet-rule-p
(org-at-item-description-p)))
2194 (unless (or (not alpha-p
)
2195 (eq org-plain-list-ordered-item-terminator ?\
))
2196 (and bullet-rule-p
(org-at-item-description-p)))
2198 (unless (or (not alpha-p
)
2199 (eq org-plain-list-ordered-item-terminator ?.
)
2200 (and bullet-rule-p
(org-at-item-description-p)))
2202 (len (length bullet-list
))
2203 (item-index (- len
(length (member current bullet-list
))))
2204 (get-value (lambda (index) (nth (mod index len
) bullet-list
)))
2206 ((member which bullet-list
) which
)
2207 ((numberp which
) (funcall get-value which
))
2208 ((eq 'previous which
) (funcall get-value
(1- item-index
)))
2209 (t (funcall get-value
(1+ item-index
))))))
2210 ;; Use a short variation of `org-list-write-struct' as there's
2211 ;; no need to go through all the steps.
2212 (let ((old-struct (copy-tree struct
)))
2213 (org-list-set-bullet list-beg struct
(org-list-bullet-string new
))
2214 (org-list-struct-fix-bul struct prevs
)
2215 (org-list-struct-fix-ind struct parents
)
2216 (org-list-struct-apply-struct struct old-struct
)))))
2218 (defun org-toggle-checkbox (&optional toggle-presence
)
2219 "Toggle the checkbox in the current line.
2220 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2221 double prefix, set checkbox to [-].
2223 When there is an active region, toggle status or presence of the
2224 first checkbox there, and make every item inside have the same
2225 status or presence, respectively.
2227 If the cursor is in a headline, apply this to all checkbox items
2228 in the text below the heading, taking as reference the first item
2229 in subtree, ignoring drawers."
2236 (drawer-re (concat "^[ \t]*:\\("
2237 (mapconcat 'regexp-quote org-drawers
"\\|")
2239 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2240 "\\|" org-deadline-string
2241 "\\|" org-closed-string
2242 "\\|" org-clock-string
"\\)"
2243 " *[[<]\\([^]>]+\\)[]>]"))
2244 (orderedp (org-entry-get nil
"ORDERED"))
2246 ;; In a region, start at first item in region.
2248 ((org-region-active-p)
2249 (let ((limit (region-end)))
2250 (goto-char (region-beginning))
2251 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2252 (setq lim-up
(point-at-bol))
2253 (error "No item in region"))
2254 (setq lim-down
(copy-marker limit
))))
2256 ;; On an heading, start at first item after drawers and
2257 ;; time-stamps (scheduled, etc.).
2258 (let ((limit (save-excursion (outline-next-heading) (point))))
2260 (while (or (looking-at drawer-re
) (looking-at keyword-re
))
2261 (if (looking-at keyword-re
)
2263 (re-search-forward "^[ \t]*:END:" limit nil
)))
2264 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2265 (setq lim-up
(point-at-bol))
2266 (error "No item in subtree"))
2267 (setq lim-down
(copy-marker limit
))))
2268 ;; Just one item: set SINGLEP flag.
2271 (setq lim-up
(point-at-bol)
2272 lim-down
(point-at-eol)))
2273 (t (error "Not at an item or heading, and no active region"))))
2274 ;; Determine the checkbox going to be applied to all items
2279 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2281 ((equal toggle-presence
'(16)) "[-]")
2282 ((equal toggle-presence
'(4))
2283 (unless cbox
"[ ]"))
2284 ((equal "[X]" cbox
) "[ ]")
2286 ;; When an item is found within bounds, grab the full list at
2287 ;; point structure, then:
2288 ;; 1. set check-box of all its items within bounds to
2290 ;; 2. fix check-boxes of the whole list; 3. move point after the
2293 (while (and (< (point) lim-down
)
2294 (org-list-search-forward (org-item-beginning-re)
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
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)))
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.
2319 ((and singlep block-item
(> lim-up block-item
))
2321 "Checkbox blocked because of unchecked box at line %d"
2322 (org-current-line block-item
)))
2325 "Checkboxes were removed due to unchecked box at line %d"
2326 (org-current-line block-item
))))
2328 (org-list-struct-apply-struct struct struct-copy
)))))
2329 (org-update-checkbox-count-maybe))
2331 (defun org-reset-checkbox-state-subtree ()
2332 "Reset all checkboxes in an entry subtree."
2334 (if (org-before-first-heading-p)
2335 (error "Not inside a tree")
2338 (org-narrow-to-subtree)
2340 (goto-char (point-min))
2341 (let ((end (point-max)))
2342 (while (< (point) end
)
2343 (when (org-at-item-checkbox-p)
2344 (replace-match "[ ]" t t nil
1))
2345 (beginning-of-line 2)))
2346 (org-update-checkbox-count-maybe 'all
)))))
2348 (defun org-update-checkbox-count (&optional all
)
2349 "Update the checkbox statistics in the current section.
2350 This will find all statistic cookies like [57%] and [6/12] and
2351 update them with the current numbers.
2353 With optional prefix argument ALL, do this for the whole buffer."
2356 (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2357 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2359 (or (not org-hierarchical-checkbox-statistics
)
2360 (string-match "\\<recursive\\>"
2361 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2363 (cons (point-min) (point-max))
2364 (cons (or (ignore-errors (org-back-to-heading t
) (point))
2366 (save-excursion (outline-next-heading) (point)))))
2369 ;; Return number of checked boxes and boxes of all types
2370 ;; in all structures in STRUCTS. If RECURSIVEP is
2371 ;; non-nil, also count boxes in sub-lists. If ITEM is
2372 ;; nil, count across the whole structure, else count only
2373 ;; across subtree whose ancestor is ITEM.
2374 (lambda (item structs recursivep
)
2375 (let ((c-on 0) (c-all 0))
2378 (let* ((pre (org-list-prevs-alist s
))
2379 (par (org-list-parents-alist s
))
2382 ((and recursivep item
) (org-list-get-subtree item s
))
2383 (recursivep (mapcar 'car s
))
2384 (item (org-list-get-children item s par
))
2385 (t (org-list-get-all-items
2386 (org-list-get-top-point s
) s pre
))))
2387 (cookies (delq nil
(mapcar
2389 (org-list-get-checkbox e s
))
2391 (setq c-all
(+ (length cookies
) c-all
)
2392 c-on
(+ (org-count "[X]" cookies
) c-on
))))
2394 (cons c-on c-all
)))))
2396 cookies-list structs-bak box-num
)
2397 (goto-char (car bounds
))
2398 ;; 1. Build an alist for each cookie found within BOUNDS. The
2399 ;; key will be position at beginning of cookie and values
2400 ;; ending position, format of cookie, and a cell whose car is
2401 ;; number of checked boxes to report, and cdr total number of
2403 (while (re-search-forward cookie-re
(cdr bounds
) t
)
2408 (match-beginning 1) ; cookie start
2409 (match-end 1) ; cookie end
2410 (match-string 2) ; percent?
2412 ;; Cookie is at an heading, but specifically for todo,
2413 ;; not for checkboxes: skip it.
2414 ((and (org-on-heading-p)
2415 (string-match "\\<todo\\>"
2417 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2419 ;; Cookie is at an heading, but all lists before next
2420 ;; heading already have been read. Use data collected
2421 ;; in STRUCTS-BAK. This should only happen when
2422 ;; heading has more than one cookie on it.
2423 ((and (org-on-heading-p)
2424 (<= (save-excursion (outline-next-heading) (point))
2426 (funcall count-boxes nil structs-bak recursivep
))
2427 ;; Cookie is at a fresh heading. Grab structure of
2428 ;; every list containing a checkbox between point and
2429 ;; next headline, and save them in STRUCTS-BAK.
2431 (setq backup-end
(save-excursion
2432 (outline-next-heading) (point))
2434 (while (org-list-search-forward box-re backup-end
'move
)
2435 (let* ((struct (org-list-struct))
2436 (bottom (org-list-get-bottom-point struct
)))
2437 (push struct structs-bak
)
2438 (goto-char bottom
)))
2439 (funcall count-boxes nil structs-bak recursivep
))
2440 ;; Cookie is at an item, and we already have list
2441 ;; structure stored in STRUCTS-BAK.
2442 ((and (org-at-item-p)
2443 (< (point-at-bol) backup-end
)
2444 ;; Only lists in no special context are stored.
2445 (not (nth 2 (org-list-context))))
2446 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2447 ;; Cookie is at an item, but we need to compute list
2450 (let ((struct (org-list-struct)))
2451 (setq backup-end
(org-list-get-bottom-point struct
)
2452 structs-bak
(list struct
)))
2453 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2454 ;; Else, cookie found is at a wrong place. Skip it.
2455 (t (throw 'skip nil
))))
2457 ;; 2. Apply alist to buffer, in reverse order so positions stay
2458 ;; unchanged after cookie modifications.
2459 (mapc (lambda (cookie)
2460 (let* ((beg (car cookie
))
2461 (end (nth 1 cookie
))
2462 (percentp (nth 2 cookie
))
2463 (checked (car (nth 3 cookie
)))
2464 (total (cdr (nth 3 cookie
)))
2466 (format "[%d%%]" (/ (* 100 checked
)
2468 (format "[%d/%d]" checked total
))))
2471 (delete-region (point) (+ (point) (- end beg
)))
2472 (when org-auto-align-tags
(org-fix-tags-on-the-fly))))
2475 (defun org-get-checkbox-statistics-face ()
2476 "Select the face for checkbox statistics.
2477 The face will be `org-done' when all relevant boxes are checked.
2478 Otherwise it will be `org-todo'."
2480 (if (equal (match-string 1) "100%")
2481 'org-checkbox-statistics-done
2482 'org-checkbox-statistics-todo
)
2483 (if (and (> (match-end 2) (match-beginning 2))
2484 (equal (match-string 2) (match-string 3)))
2485 'org-checkbox-statistics-done
2486 'org-checkbox-statistics-todo
)))
2488 (defun org-update-checkbox-count-maybe (&optional all
)
2489 "Update checkbox statistics unless turned off by user."
2490 (when (cdr (assq 'checkbox org-list-automatic-rules
))
2491 (org-update-checkbox-count all
))
2492 (run-hooks 'org-checkbox-statistics-hook
))
2494 (defvar org-last-indent-begin-marker
(make-marker))
2495 (defvar org-last-indent-end-marker
(make-marker))
2496 (defun org-list-indent-item-generic (arg no-subtree struct
)
2497 "Indent a local list item including its children.
2498 When number ARG is a negative, item will be outdented, otherwise
2499 it will be indented.
2501 If a region is active, all items inside will be moved.
2503 If NO-SUBTREE is non-nil, only indent the item itself, not its
2506 STRUCT is the list structure.
2508 Return t if successful."
2511 (let* ((regionp (org-region-active-p))
2512 (rbeg (and regionp
(region-beginning)))
2513 (rend (and regionp
(region-end)))
2514 (top (org-list-get-top-point struct
))
2515 (parents (org-list-parents-alist struct
))
2516 (prevs (org-list-prevs-alist struct
))
2517 ;; Are we going to move the whole list?
2519 (and (= top
(point))
2520 (cdr (assq 'indent org-list-automatic-rules
))
2523 "First item of list cannot move without its subtree")
2525 ;; Determine begin and end points of zone to indent. If moving
2526 ;; more than one item, save them for subsequent moves.
2527 (unless (and (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
2528 (memq this-command
'(org-shiftmetaright org-shiftmetaleft
)))
2531 (set-marker org-last-indent-begin-marker rbeg
)
2532 (set-marker org-last-indent-end-marker rend
))
2533 (set-marker org-last-indent-begin-marker
(point))
2534 (set-marker org-last-indent-end-marker
2536 (specialp (org-list-get-bottom-point struct
))
2537 (no-subtree (1+ (point)))
2538 (t (org-list-get-item-end (point) struct
))))))
2539 (let* ((beg (marker-position org-last-indent-begin-marker
))
2540 (end (marker-position org-last-indent-end-marker
)))
2542 ;; Special case: moving top-item with indent rule.
2544 (let* ((level-skip (org-level-increment))
2545 (offset (if (< arg
0) (- level-skip
) level-skip
))
2546 (top-ind (org-list-get-ind beg struct
))
2547 (old-struct (copy-tree struct
)))
2548 (if (< (+ top-ind offset
) 0)
2549 (error "Cannot outdent beyond margin")
2550 ;; Change bullet if necessary.
2551 (when (and (= (+ top-ind offset
) 0)
2553 (org-list-get-bullet beg struct
)))
2554 (org-list-set-bullet beg struct
2555 (org-list-bullet-string "-")))
2556 ;; Shift every item by OFFSET and fix bullets. Then
2557 ;; apply changes to buffer.
2559 (let ((ind (org-list-get-ind (car e
) struct
)))
2560 (org-list-set-ind (car e
) struct
(+ ind offset
))))
2562 (org-list-struct-fix-bul struct prevs
)
2563 (org-list-struct-apply-struct struct old-struct
))))
2566 ;; If only one item is moved, it mustn't have a child.
2569 (org-list-has-child-p beg struct
))
2570 ;; If a subtree or region is moved, the last item
2571 ;; of the subtree mustn't have a child.
2572 (let ((last-item (caar
2575 (lambda (e) (>= (car e
) end
))
2577 (org-list-has-child-p last-item struct
))))
2578 (error "Cannot outdent an item without its children"))
2583 (org-list-struct-outdent beg end struct parents
)
2584 (org-list-struct-indent beg end struct parents prevs
))))
2585 (org-list-write-struct struct new-parents
))
2586 (org-update-checkbox-count-maybe))))))
2589 (defun org-outdent-item ()
2590 "Outdent a local list item, but not its children.
2591 If a region is active, all items inside will be moved."
2594 (let ((struct (org-list-struct)))
2595 (org-list-indent-item-generic -
1 t struct
))
2596 (error "Not at an item")))
2598 (defun org-indent-item ()
2599 "Indent a local list item, but not its children.
2600 If a region is active, all items inside will be moved."
2603 (let ((struct (org-list-struct)))
2604 (org-list-indent-item-generic 1 t struct
))
2605 (error "Not at an item")))
2607 (defun org-outdent-item-tree ()
2608 "Outdent a local list item including its children.
2609 If a region is active, all items inside will be moved."
2611 (let ((regionp (org-region-active-p)))
2613 ((or (org-at-item-p)
2614 (and (org-region-active-p)
2615 (goto-char (region-beginning))
2617 (let ((struct (org-list-struct)))
2618 (org-list-indent-item-generic -
1 nil struct
)))
2619 (regionp (error "Region not starting at an item"))
2620 (t (error "Not at an item")))))
2622 (defun org-indent-item-tree ()
2623 "Indent a local list item including its children.
2624 If a region is active, all items inside will be moved."
2626 (let ((regionp (org-region-active-p)))
2628 ((or (org-at-item-p)
2629 (and (org-region-active-p)
2630 (goto-char (region-beginning))
2632 (let ((struct (org-list-struct)))
2633 (org-list-indent-item-generic 1 nil struct
)))
2634 (regionp (error "Region not starting at an item"))
2635 (t (error "Not at an item")))))
2637 (defvar org-tab-ind-state
)
2638 (defun org-cycle-item-indentation ()
2639 "Cycle levels of indentation of an empty item.
2640 The first run indents the item, if applicable. Subsequents runs
2641 outdent it at meaningful levels in the list. When done, item is
2642 put back at its original position with its original bullet.
2644 Return t at each successful move."
2645 (when (org-at-item-p)
2646 (let* ((org-adapt-indentation nil
)
2647 (struct (org-list-struct))
2648 (ind (org-list-get-ind (point-at-bol) struct
))
2649 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2650 ;; Accept empty items or if cycle has already started.
2651 (when (or (eq last-command
'org-cycle-item-indentation
)
2652 (and (save-excursion
2654 (looking-at org-list-full-item-re
))
2655 (>= (match-end 0) (save-excursion
2656 (goto-char (org-list-get-item-end
2657 (point-at-bol) struct
))
2658 (skip-chars-backward " \r\t\n")
2660 (setq this-command
'org-cycle-item-indentation
)
2661 ;; When in the middle of the cycle, try to outdent first. If
2662 ;; it fails, and point is still at initial position, indent.
2663 ;; Else, re-create it at its original position.
2664 (if (eq last-command
'org-cycle-item-indentation
)
2666 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2667 ((and (= ind
(car org-tab-ind-state
))
2668 (ignore-errors (org-list-indent-item-generic 1 t struct
))))
2669 (t (delete-region (point-at-bol) (point-at-eol))
2670 (org-indent-to-column (car org-tab-ind-state
))
2671 (insert (cdr org-tab-ind-state
) " ")
2673 (setq this-command
'identity
)))
2674 ;; If a cycle is starting, remember indentation and bullet,
2675 ;; then try to indent. If it fails, try to outdent.
2676 (setq org-tab-ind-state
(cons ind bullet
))
2678 ((ignore-errors (org-list-indent-item-generic 1 t struct
)))
2679 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2680 (t (error "Cannot move item"))))
2683 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func
)
2685 The cursor may be at any item of the list that should be sorted.
2686 Sublists are not sorted. Checkboxes, if any, are ignored.
2688 Sorting can be alphabetically, numerically, by date/time as given
2689 by a time stamp, by a property or by priority.
2691 Comparing entries ignores case by default. However, with an
2692 optional argument WITH-CASE, the sorting considers case as well.
2694 The command prompts for the sorting type unless it has been given
2695 to the function through the SORTING-TYPE argument, which needs to
2696 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
2697 meaning of each character:
2699 n Numerically, by converting the beginning of the item to a number.
2700 a Alphabetically. Only the first line of item is checked.
2701 t By date/time, either the first active time stamp in the entry, if
2702 any, or by the first inactive one. In a timer list, sort the timers.
2704 Capital letters will reverse the sort order.
2706 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2707 a function to be called with point at the beginning of the
2708 record. It must return either a string or a number that should
2709 serve as the sorting key for that record. It will then use
2710 COMPARE-FUNC to compare entries."
2712 (let* ((case-func (if with-case
'identity
'downcase
))
2713 (struct (org-list-struct))
2714 (prevs (org-list-prevs-alist struct
))
2715 (start (org-list-get-list-begin (point-at-bol) struct prevs
))
2716 (end (org-list-get-list-end (point-at-bol) struct prevs
))
2720 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
2721 (read-char-exclusive)))
2722 (getkey-func (and (= (downcase sorting-type
) ?f
)
2723 (intern (org-icompleting-read "Sort using function: "
2724 obarray
'fboundp t nil nil
)))))
2725 (message "Sorting items...")
2727 (narrow-to-region start end
)
2728 (goto-char (point-min))
2729 (let* ((dcst (downcase sorting-type
))
2730 (case-fold-search nil
)
2731 (now (current-time))
2733 ((= dcst ?a
) 'string
<)
2734 ((= dcst ?f
) compare-func
)
2737 (next-record (lambda ()
2738 (skip-chars-forward " \r\t\n")
2739 (beginning-of-line)))
2740 (end-record (lambda ()
2741 (goto-char (org-list-get-item-end-before-blank
2745 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2748 (string-to-number (buffer-substring (match-end 0)
2752 (buffer-substring (match-end 0) (point-at-eol))))
2755 ;; If it is a timer list, convert timer to seconds
2756 ((org-at-item-timer-p)
2757 (org-timer-hms-to-secs (match-string 1)))
2758 ((or (re-search-forward org-ts-regexp
(point-at-eol) t
)
2759 (re-search-forward org-ts-regexp-both
2761 (org-time-string-to-seconds (match-string 0)))
2762 (t (org-float-time now
))))
2765 (let ((value (funcall getkey-func
)))
2767 (funcall case-func value
)
2769 (error "Invalid key function `%s'" getkey-func
)))
2770 (t (error "Invalid sorting type `%c'" sorting-type
)))))))
2771 (sort-subr (/= dcst sorting-type
)
2777 ;; Read and fix list again, as `sort-subr' probably destroyed
2780 (run-hooks 'org-after-sorting-entries-or-items-hook
)
2781 (message "Sorting items...done")))))
2784 ;;; Send and receive lists
2786 (defun org-list-parse-list (&optional delete
)
2787 "Parse the list at point and maybe DELETE it.
2789 Return a list whose car is a symbol of list type, among
2790 `ordered', `unordered' and `descriptive'. Then, each item is
2791 a list whose car is counter, and cdr are strings and other
2792 sub-lists. Inside strings, check-boxes are replaced by
2793 \"[CBON]\", \"[CBOFF]\" and \"[CBTRANS]\".
2795 For example, the following list:
2800 more text in first item
2806 \(nil \"first item\"
2808 \(nil \"sub-item one\"\)
2809 \(nil \"[CBON] sub-item two\"\)\)
2810 \"more text in first item\"\)
2811 \(3 \"last item\"\)\)
2813 Point is left at list end."
2814 (let* ((struct (org-list-struct))
2815 (prevs (org-list-prevs-alist struct
))
2816 (parents (org-list-parents-alist struct
))
2817 (top (org-list-get-top-point struct
))
2818 (bottom (org-list-get-bottom-point struct
))
2820 parse-item
; for byte-compiler
2823 ;; Return text between BEG and END, trimmed, with
2824 ;; checkboxes replaced.
2826 (let ((text (org-trim (buffer-substring beg end
))))
2827 (if (string-match "\\`\\[\\([-X ]\\)\\]" text
)
2829 (let ((box (match-string 1 text
)))
2831 ((equal box
" ") "CBOFF")
2832 ((equal box
"-") "CBTRANS")
2838 ;; Return a list whose car is list type and cdr a list of
2841 (cons (org-list-get-list-type (car e
) struct prevs
)
2842 (mapcar parse-item e
)))))
2845 ;; Return a list containing counter of item, if any, text
2846 ;; and any sublist inside it.
2848 (let ((start (save-excursion
2850 (looking-at "[ \t]*\\S-+\\([ \t]+\\[@\\(start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\]\\)?[ \t]*")
2852 ;; Get counter number. For alphabetic counter, get
2853 ;; its position in the alphabet.
2854 (counter (let ((c (org-list-get-counter e struct
)))
2857 ((string-match "[A-Za-z]" c
)
2858 (- (string-to-char (upcase (match-string 0 c
)))
2860 ((string-match "[0-9]+" c
)
2861 (string-to-number (match-string 0 c
))))))
2862 (childp (org-list-has-child-p e struct
))
2863 (end (org-list-get-item-end e struct
)))
2864 ;; If item has a child, store text between bullet and
2865 ;; next child, then recursively parse all sublists. At
2866 ;; the end of each sublist, check for the presence of
2867 ;; text belonging to the original item.
2869 (let* ((children (org-list-get-children e struct parents
))
2870 (body (list (funcall get-text start childp
))))
2872 (let* ((first (car children
))
2873 (sub (org-list-get-all-items first struct prevs
))
2874 (last-c (car (last sub
)))
2875 (last-end (org-list-get-item-end last-c struct
)))
2876 (push (funcall parse-sublist sub
) body
)
2877 ;; Remove children from the list just parsed.
2878 (setq children
(cdr (member last-c children
)))
2879 ;; There is a chunk of text belonging to the
2880 ;; item if last child doesn't end where next
2881 ;; child starts or where item ends.
2882 (unless (= (or (car children
) end
) last-end
)
2883 (push (funcall get-text
2884 last-end
(or (car children
) end
))
2886 (cons counter
(nreverse body
)))
2887 (list counter
(funcall get-text start end
))))))))
2888 ;; Store output, take care of cursor position and deletion of
2889 ;; list, then return output.
2890 (setq out
(funcall parse-sublist
(org-list-get-all-items top struct prevs
)))
2893 (delete-region top bottom
)
2894 (when (and (not (eq org-list-ending-method
'indent
))
2895 (looking-at org-list-end-re
))
2896 (replace-match "\n")))
2899 (defun org-list-make-subtree ()
2900 "Convert the plain list at point into a subtree."
2902 (if (not (ignore-errors (goto-char (org-in-item-p))))
2903 (error "Not in a list")
2904 (let ((list (save-excursion (org-list-parse-list t
))))
2905 (insert (org-list-to-subtree list
)))))
2907 (defun org-list-insert-radio-list ()
2908 "Insert a radio list template appropriate for this major mode."
2910 (let* ((e (assq major-mode org-list-radio-list-templates
))
2913 (unless e
(error "No radio list setup defined for %s" major-mode
))
2914 (setq name
(read-string "List name: "))
2915 (while (string-match "%n" txt
)
2916 (setq txt
(replace-match name t t txt
)))
2917 (or (bolp) (insert "\n"))
2922 (defun org-list-send-list (&optional maybe
)
2923 "Send a transformed version of this list to the receiver position.
2924 With argument MAYBE, fail quietly if no transformation is defined
2928 (unless (org-at-item-p) (error "Not at a list item"))
2930 (re-search-backward "#\\+ORGLST" nil t
)
2931 (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
2934 (error "Don't know how to transform this list"))))
2935 (let* ((name (match-string 1))
2936 (transform (intern (match-string 2)))
2940 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t
)
2941 (match-beginning 0)))
2944 (re-search-backward "#\\+ORGLST" nil t
)
2945 (re-search-forward (org-item-beginning-re) bottom-point t
)
2946 (match-beginning 0)))
2947 (list (save-restriction
2948 (narrow-to-region top-point bottom-point
)
2949 (org-list-parse-list)))
2951 (unless (fboundp transform
)
2952 (error "No such transformation function %s" transform
))
2953 (let ((txt (funcall transform list
)))
2954 ;; Find the insertion place
2956 (goto-char (point-min))
2957 (unless (re-search-forward
2958 (concat "BEGIN RECEIVE ORGLST +"
2960 "\\([ \t]\\|$\\)") nil t
)
2961 (error "Don't know where to insert translated list"))
2962 (goto-char (match-beginning 0))
2963 (beginning-of-line 2)
2965 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
2966 (error "Cannot find end of insertion region"))
2967 (delete-region beg
(point-at-bol))
2970 (message "List converted and installed at receiver location"))))
2972 (defun org-list-to-generic (list params
)
2973 "Convert a LIST parsed through `org-list-parse-list' to other formats.
2974 Valid parameters PARAMS are:
2976 :ustart String to start an unordered list
2977 :uend String to end an unordered list
2979 :ostart String to start an ordered list
2980 :oend String to end an ordered list
2982 :dstart String to start a descriptive list
2983 :dend String to end a descriptive list
2984 :dtstart String to start a descriptive term
2985 :dtend String to end a descriptive term
2986 :ddstart String to start a description
2987 :ddend String to end a description
2989 :splice When set to t, return only list body lines, don't wrap
2990 them into :[u/o]start and :[u/o]end. Default is nil.
2992 :istart String to start a list item.
2993 :icount String to start an item with a counter.
2994 :iend String to end a list item
2995 :isep String to separate items
2996 :lsep String to separate sublists
2997 :csep String to separate text from a sub-list
2999 :cboff String to insert for an unchecked check-box
3000 :cbon String to insert for a checked check-box
3001 :cbtrans String to insert for a check-box in transitional state
3003 Alternatively, each parameter can also be a form returning
3004 a string. These sexp can use keywords `counter' and `depth',
3005 reprensenting respectively counter associated to the current
3006 item, and depth of the current sub-list, starting at 0.
3007 Obviously, `counter' is only available for parameters applying to
3011 (splicep (plist-get p
:splice
))
3012 (ostart (plist-get p
:ostart
))
3013 (oend (plist-get p
:oend
))
3014 (ustart (plist-get p
:ustart
))
3015 (uend (plist-get p
:uend
))
3016 (dstart (plist-get p
:dstart
))
3017 (dend (plist-get p
:dend
))
3018 (dtstart (plist-get p
:dtstart
))
3019 (dtend (plist-get p
:dtend
))
3020 (ddstart (plist-get p
:ddstart
))
3021 (ddend (plist-get p
:ddend
))
3022 (istart (plist-get p
:istart
))
3023 (icount (plist-get p
:icount
))
3024 (iend (plist-get p
:iend
))
3025 (isep (plist-get p
:isep
))
3026 (lsep (plist-get p
:lsep
))
3027 (csep (plist-get p
:csep
))
3028 (cbon (plist-get p
:cbon
))
3029 (cboff (plist-get p
:cboff
))
3030 (cbtrans (plist-get p
:cbtrans
))
3031 export-sublist
; for byte-compiler
3034 ;; Export an item ITEM of type TYPE, at DEPTH. First
3035 ;; string in item is treated in a special way as it can
3036 ;; bring extra information that needs to be processed.
3037 (lambda (item type depth
)
3038 (let* ((counter (pop item
))
3041 ((eq type
'descriptive
)
3042 ;; Stick DTSTART to ISTART by
3043 ;; left-trimming the latter.
3044 (concat (let ((s (eval istart
)))
3045 (or (and (string-match "[ \t\n\r]+\\'" s
)
3046 (replace-match "" t t s
))
3049 ((and counter
(eq type
'ordered
))
3050 (concat (eval icount
) "%s"))
3051 (t (concat (eval istart
) "%s")))
3054 ;; Replace checkbox if any is found.
3056 ((string-match "\\[CBON\\]" first
)
3057 (setq first
(replace-match cbon t t first
)))
3058 ((string-match "\\[CBOFF\\]" first
)
3059 (setq first
(replace-match cboff t t first
)))
3060 ((string-match "\\[CBTRANS\\]" first
)
3061 (setq first
(replace-match cbtrans t t first
))))
3062 ;; Insert descriptive term if TYPE is `descriptive'.
3063 (when (eq type
'descriptive
)
3064 (let* ((complete (string-match "^\\(.*\\)[ \t]+::" first
))
3067 (org-trim (match-string 1 first
)))
3070 (org-trim (substring first
(match-end 0)))
3072 (setq first
(concat (eval dtstart
) term
(eval dtend
)
3073 (eval ddstart
) desc
))))
3076 (mapconcat (lambda (e)
3078 (funcall export-sublist e
(1+ depth
))))
3079 item
(or (eval csep
) "")))))))
3082 ;; Export sublist SUB at DEPTH.
3084 (let* ((type (car sub
))
3089 (concat (eval ostart
) "%s" (eval oend
)))
3090 ((eq type
'descriptive
)
3091 (concat (eval dstart
) "%s" (eval dend
)))
3092 (t (concat (eval ustart
) "%s" (eval uend
))))
3094 (format fmt
(mapconcat (lambda (e)
3095 (funcall export-item e type depth
))
3096 items
(or (eval isep
) ""))))))))
3097 (concat (funcall export-sublist list
0) "\n")))
3099 (defun org-list-to-latex (list &optional params
)
3100 "Convert LIST into a LaTeX list.
3101 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3102 with overruling parameters for `org-list-to-generic'."
3103 (org-list-to-generic
3106 '(:splice nil
:ostart
"\\begin{enumerate}\n" :oend
"\\end{enumerate}"
3107 :ustart
"\\begin{itemize}\n" :uend
"\\end{itemize}"
3108 :dstart
"\\begin{description}\n" :dend
"\\end{description}"
3109 :dtstart
"[" :dtend
"] "
3110 :istart
"\\item " :iend
"\n"
3111 :icount
(let ((enum (nth depth
'("i" "ii" "iii" "iv"))))
3113 ;; LaTeX increments counter just before
3114 ;; using it, so set it to the desired
3115 ;; value, minus one.
3116 (format "\\setcounter{enum%s}{%s}\n\\item "
3120 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}"
3121 :cbtrans
"\\texttt{[-]}")
3124 (defun org-list-to-html (list &optional params
)
3125 "Convert LIST into a HTML list.
3126 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3127 with overruling parameters for `org-list-to-generic'."
3128 (org-list-to-generic
3131 '(:splice nil
:ostart
"<ol>\n" :oend
"\n</ol>"
3132 :ustart
"<ul>\n" :uend
"\n</ul>"
3133 :dstart
"<dl>\n" :dend
"\n</dl>"
3134 :dtstart
"<dt>" :dtend
"</dt>\n"
3135 :ddstart
"<dd>" :ddend
"</dd>"
3136 :istart
"<li>" :iend
"</li>"
3137 :icount
(format "<li value=\"%s\">" counter
)
3138 :isep
"\n" :lsep
"\n" :csep
"\n"
3139 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>"
3140 :cbtrans
"<code>[-]</code>")
3143 (defun org-list-to-texinfo (list &optional params
)
3144 "Convert LIST into a Texinfo list.
3145 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3146 with overruling parameters for `org-list-to-generic'."
3147 (org-list-to-generic
3150 '(:splice nil
:ostart
"@itemize @minus\n" :oend
"@end itemize"
3151 :ustart
"@enumerate\n" :uend
"@end enumerate"
3152 :dstart
"@table @asis\n" :dend
"@end table"
3153 :dtstart
" " :dtend
"\n"
3154 :istart
"@item\n" :iend
"\n"
3157 :cbon
"@code{[X]}" :cboff
"@code{[ ]}"
3158 :cbtrans
"@code{[-]}")
3161 (defun org-list-to-subtree (list &optional params
)
3162 "Convert LIST into an Org subtree.
3163 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3164 with overruling parameters for `org-list-to-generic'."
3165 (let* ((rule (cdr (assq 'heading org-blank-before-new-entry
)))
3166 (level (org-reduced-level (or (org-current-level) 0)))
3167 (blankp (or (eq rule t
)
3168 (and (eq rule
'auto
)
3170 (outline-previous-heading)
3171 (org-previous-line-empty-p)))))
3174 ;; Return the string for the heading, depending on depth D
3175 ;; of current sub-list.
3177 (let ((oddeven-level (+ level d
1)))
3178 (concat (make-string (if org-odd-levels-only
3179 (1- (* 2 oddeven-level
))
3183 (org-list-to-generic
3187 :dtstart
" " :dtend
" "
3188 :istart
(funcall get-stars depth
)
3189 :icount
(funcall get-stars depth
)
3190 :isep
(if blankp
"\n\n" "\n")
3191 :csep
(if blankp
"\n\n" "\n")
3192 :cbon
"DONE" :cboff
"TODO" :cbtrans
"TODO")
3197 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
3198 ;;; org-list.el ends here