1 ;;; org-list.el --- Plain lists for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2019 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Bastien Guerry <bzg@gnu.org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: https://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains the code dealing with plain lists in Org mode.
30 ;; The core concept behind lists is their structure. A structure is
31 ;; a snapshot of the list, in the shape of a data tree (see
32 ;; `org-list-struct').
34 ;; Once the list structure is stored, it is possible to make changes
35 ;; on it that will be mirrored to the real list or to get information
36 ;; about the list, using accessors and methods provided in the
37 ;; library. Most of them require the use of one or two helper
38 ;; functions, namely `org-list-parents-alist' and
39 ;; `org-list-prevs-alist'.
41 ;; Structure is eventually applied to the buffer with
42 ;; `org-list-write-struct'. This function repairs (bullets,
43 ;; indentation, checkboxes) the list in the process. It should be
44 ;; called near the end of any function working on structures.
46 ;; Thus, a function applying to lists should usually follow this
49 ;; 1. Verify point is in a list and grab item beginning (with the same
50 ;; function `org-in-item-p'). If the function requires the cursor
51 ;; to be at item's bullet, `org-at-item-p' is more selective. It
52 ;; is also possible to move point to the closest item with
53 ;; `org-list-search-backward', or `org-list-search-forward',
54 ;; applied to the function `org-item-beginning-re'.
56 ;; 2. Get list structure with `org-list-struct'.
58 ;; 3. Compute one, or both, helper functions,
59 ;; (`org-list-parents-alist', `org-list-prevs-alist') depending on
62 ;; 4. Proceed with the modifications, using methods and accessors.
64 ;; 5. Verify and apply structure to buffer, using
65 ;; `org-list-write-struct'.
67 ;; 6. If changes made to the list might have modified check-boxes,
68 ;; call `org-update-checkbox-count-maybe'.
70 ;; Computing a structure can be a costly operation on huge lists (a
71 ;; few thousand lines long). Thus, code should follow the rule:
72 ;; "collect once, use many". As a corollary, it is usually a bad idea
73 ;; to use directly an interactive function inside the code, as those,
74 ;; being independent entities, read the whole list structure another
83 (defvar org-M-RET-may-split-line
)
84 (defvar org-auto-align-tags
)
85 (defvar org-blank-before-new-entry
)
86 (defvar org-clock-string
)
87 (defvar org-closed-string
)
88 (defvar org-deadline-string
)
89 (defvar org-description-max-indent
)
90 (defvar org-done-keywords
)
91 (defvar org-drawer-regexp
)
92 (defvar org-element-all-objects
)
93 (defvar org-inhibit-startup
)
94 (defvar org-loop-over-headlines-in-active-region
)
95 (defvar org-odd-levels-only
)
96 (defvar org-outline-regexp-bol
)
97 (defvar org-scheduled-string
)
98 (defvar org-todo-line-regexp
)
99 (defvar org-ts-regexp
)
100 (defvar org-ts-regexp-both
)
102 (declare-function org-at-heading-p
"org" (&optional invisible-ok
))
103 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
104 (declare-function org-before-first-heading-p
"org" ())
105 (declare-function org-current-level
"org" ())
106 (declare-function org-element-at-point
"org-element" ())
107 (declare-function org-element-context
"org-element" (&optional element
))
108 (declare-function org-element-interpret-data
"org-element" (data))
109 (declare-function org-element-lineage
"org-element" (blob &optional types with-self
))
110 (declare-function org-element-macro-interpreter
"org-element" (macro ##))
111 (declare-function org-element-map
"org-element" (data types fun
&optional info first-match no-recursion with-affiliated
))
112 (declare-function org-element-normalize-string
"org-element" (s))
113 (declare-function org-element-parse-buffer
"org-element" (&optional granularity visible-only
))
114 (declare-function org-element-property
"org-element" (property element
))
115 (declare-function org-element-put-property
"org-element" (element property value
))
116 (declare-function org-element-set-element
"org-element" (old new
))
117 (declare-function org-element-type
"org-element" (element))
118 (declare-function org-element-update-syntax
"org-element" ())
119 (declare-function org-end-of-meta-data
"org" (&optional full
))
120 (declare-function org-entry-get
"org" (pom property
&optional inherit literal-nil
))
121 (declare-function org-export-create-backend
"ox" (&rest rest
) t
)
122 (declare-function org-export-data-with-backend
"ox" (data backend info
))
123 (declare-function org-export-get-backend
"ox" (name))
124 (declare-function org-export-get-environment
"ox" (&optional backend subtreep ext-plist
))
125 (declare-function org-export-get-next-element
"ox" (blob info
&optional n
))
126 (declare-function org-export-with-backend
"ox" (backend data
&optional contents info
))
127 (declare-function org-fix-tags-on-the-fly
"org" ())
128 (declare-function org-get-todo-state
"org" ())
129 (declare-function org-in-block-p
"org" (names))
130 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
131 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
132 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
133 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
134 (declare-function org-level-increment
"org" ())
135 (declare-function org-narrow-to-subtree
"org" ())
136 (declare-function org-outline-level
"org" ())
137 (declare-function org-previous-line-empty-p
"org" ())
138 (declare-function org-reduced-level
"org" (L))
139 (declare-function org-set-tags
"org" (tags))
140 (declare-function org-show-subtree
"org" ())
141 (declare-function org-sort-remove-invisible
"org" (S))
142 (declare-function org-time-string-to-seconds
"org" (s))
143 (declare-function org-timer-hms-to-secs
"org-timer" (hms))
144 (declare-function org-timer-item
"org-timer" (&optional arg
))
145 (declare-function outline-next-heading
"outline" ())
146 (declare-function outline-previous-heading
"outline" ())
150 ;;; Configuration variables
152 (defgroup org-plain-lists nil
153 "Options concerning plain lists in Org mode."
154 :tag
"Org Plain lists"
155 :group
'org-structure
)
157 (defcustom org-cycle-include-plain-lists t
158 "When t, make TAB cycle visibility on plain list items.
159 Cycling plain lists works only when the cursor is on a plain list
160 item. When the cursor is on an outline heading, plain lists are
161 treated as text. This is the most stable way of handling this,
162 which is why it is the default.
164 When this is the symbol `integrate', then integrate plain list
165 items when cycling, as if they were children of outline headings.
167 This setting can lead to strange effects when switching visibility
168 to `children', because the first \"child\" in a subtree decides
169 what children should be listed. If that first \"child\" is a
170 plain list item with an implied large level number, all true
171 children and grand children of the outline heading will be
172 exposed in a children' view."
173 :group
'org-plain-lists
176 (const :tag
"Never" nil
)
177 (const :tag
"With cursor in plain list (recommended)" t
)
178 (const :tag
"As children of outline headings" integrate
)))
180 (defcustom org-list-demote-modify-bullet nil
181 "Default bullet type installed when demoting an item.
182 This is an association list, for each bullet type, this alist will point
183 to the bullet that should be used when this item is demoted.
186 (setq org-list-demote-modify-bullet
187 \\='((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
192 + Silence of the Lambs
195 + The Hunt for Red October
201 - Silence of the Lambs
204 - The Hunt for Red October
206 :group
'org-plain-lists
209 (choice :tag
"If the current bullet is "
215 (choice :tag
"demotion will change it to"
222 (defcustom org-plain-list-ordered-item-terminator t
223 "The character that makes a line with leading number an ordered list item.
224 Valid values are ?. and ?\). To get both terminators, use t.
226 This variable needs to be set before org.el is loaded. If you
227 need to make a change while Emacs is running, use the customize
228 interface or run the following code after updating it:
230 `\\[org-element-update-syntax]'"
231 :group
'org-plain-lists
232 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
233 (const :tag
"paren like in \"2)\"" ?\
))
234 (const :tag
"both" t
))
235 :set
(lambda (var val
) (set var val
)
236 (when (featurep 'org-element
) (org-element-update-syntax))))
238 (defcustom org-list-allow-alphabetical nil
239 "Non-nil means single character alphabetical bullets are allowed.
241 Both uppercase and lowercase are handled. Lists with more than
242 26 items will fallback to standard numbering. Alphabetical
243 counters like \"[@c]\" will be recognized.
245 This variable needs to be set before org.el is loaded. If you
246 need to make a change while Emacs is running, use the customize
247 interface or run the following code after updating it:
249 `\\[org-element-update-syntax]'"
250 :group
'org-plain-lists
253 :set
(lambda (var val
) (set var val
)
254 (when (featurep 'org-element
) (org-element-update-syntax))))
256 (defcustom org-list-two-spaces-after-bullet-regexp nil
257 "A regular expression matching bullets that should have 2 spaces after them.
258 When nil, no bullet will have two spaces after them. When
259 a string, it will be used as a regular expression. When the
260 bullet type of a list is changed, the new bullet type will be
261 matched against this regexp. If it matches, there will be two
262 spaces instead of one after the bullet in each item of the list."
263 :group
'org-plain-lists
265 (const :tag
"never" nil
)
268 (defcustom org-list-automatic-rules
'((checkbox . t
)
270 "Non-nil means apply set of rules when acting on lists.
272 By default, automatic actions are taken when using
273 `\\[org-meta-return]',
276 `\\[org-shiftmetaright]',
277 `\\[org-shiftmetaleft]',
278 `\\[org-ctrl-c-minus]',
279 `\\[org-toggle-checkbox]',
280 `\\[org-insert-todo-heading]'.
282 You can disable individually these rules by setting them to nil.
285 checkbox when non-nil, checkbox statistics is updated each time
286 you either insert a new checkbox or toggle a checkbox.
287 indent when non-nil, indenting or outdenting list top-item
288 with its subtree will move the whole list and
289 outdenting a list whose bullet is * to column 0 will
290 change that bullet to \"-\"."
291 :group
'org-plain-lists
293 :type
'(alist :tag
"Sets of rules"
296 (const :tag
"Checkbox" checkbox
)
297 (const :tag
"Indent" indent
))
299 (boolean :tag
"Activate" :value t
)))
301 (defcustom org-list-use-circular-motion nil
302 "Non-nil means commands implying motion in lists should be cyclic.
304 In that case, the item following the last item is the first one,
305 and the item preceding the first item is the last one.
307 This affects the behavior of
308 `\\[org-move-item-up]',
309 `\\[org-move-item-down]',
311 `\\[org-previous-item]'."
312 :group
'org-plain-lists
316 (defvar org-checkbox-statistics-hook nil
317 "Hook that is run whenever Org thinks checkbox statistics should be updated.
318 This hook runs even if checkbox rule in
319 `org-list-automatic-rules' does not apply, so it can be used to
320 implement alternative ways of collecting statistics
323 (defcustom org-checkbox-hierarchical-statistics t
324 "Non-nil means checkbox statistics counts only the state of direct children.
325 When nil, all boxes below the cookie are counted.
326 This can be set to nil on a per-node basis using a COOKIE_DATA property
327 with the word \"recursive\" in the value."
328 :group
'org-plain-lists
331 (defcustom org-list-description-max-indent
20
332 "Maximum indentation for the second line of a description list.
333 When the indentation would be larger than this, it will become
334 5 characters instead."
335 :group
'org-plain-lists
339 (defcustom org-list-indent-offset
0
340 "Additional indentation for sub-items in a list.
341 By setting this to a small number, usually 1 or 2, one can more
342 clearly distinguish sub-items in a list."
343 :group
'org-plain-lists
347 (defvar org-list-forbidden-blocks
'("example" "verse" "src" "export")
348 "Names of blocks where lists are not allowed.
349 Names must be in lower case.")
351 (defvar org-list-export-context
'(block inlinetask
)
352 "Context types where lists will be interpreted during export.
354 Valid types are `drawer', `inlinetask' and `block'. More
355 specifically, type `block' is determined by the variable
356 `org-list-forbidden-blocks'.")
360 ;;; Predicates and regexps
362 (defconst org-list-end-re
"^[ \t]*\n[ \t]*\n"
363 "Regex matching the end of a plain list.")
365 (defconst org-list-full-item-re
366 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
367 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
368 "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
369 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
370 "Matches a list item and puts everything into groups:
374 group 4: description tag")
376 (defun org-item-re ()
377 "Return the correct regular expression for plain lists."
379 ((eq org-plain-list-ordered-item-terminator t
) "[.)]")
380 ((= org-plain-list-ordered-item-terminator ?\
)) ")")
381 ((= org-plain-list-ordered-item-terminator ?.
) "\\.")
383 (alpha (if org-list-allow-alphabetical
"\\|[A-Za-z]" "")))
384 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha
"\\)" term
385 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
387 (defsubst org-item-beginning-re
()
388 "Regexp matching the beginning of a plain list item."
389 (concat "^" (org-item-re)))
391 (defun org-list-at-regexp-after-bullet-p (regexp)
392 "Is point at a list item with REGEXP after bullet?"
395 (goto-char (match-end 0))
396 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
397 (if org-list-allow-alphabetical
398 "\\([0-9]+\\|[A-Za-z]\\)"
401 ;; Ignore counter if any
402 (when (looking-at counter-re
) (goto-char (match-end 0))))
403 (looking-at regexp
))))
405 (defun org-list-in-valid-context-p ()
406 "Is point in a context where lists are allowed?"
407 (not (org-in-block-p org-list-forbidden-blocks
)))
409 (defun org-in-item-p ()
410 "Return item beginning position when in a plain list, nil otherwise."
413 (let* ((case-fold-search t
)
414 (context (org-list-context))
415 (lim-up (car context
))
416 (inlinetask-re (and (featurep 'org-inlinetask
)
417 (org-inlinetask-outline-regexp)))
418 (item-re (org-item-re))
419 ;; Indentation isn't meaningful when point starts at an empty
420 ;; line or an inline task.
421 (ind-ref (if (or (looking-at "^[ \t]*$")
422 (and inlinetask-re
(looking-at inlinetask-re
)))
424 (current-indentation))))
426 ((eq (nth 2 context
) 'invalid
) nil
)
427 ((looking-at item-re
) (point))
429 ;; Detect if cursor in amidst `org-list-end-re'. First, count
430 ;; number HL of hard lines it takes, then call `org-in-regexp'
431 ;; to compute its boundaries END-BOUNDS. When point is
432 ;; in-between, move cursor before regexp beginning.
433 (let ((hl 0) (i -
1) end-bounds
)
435 (while (setq i
(string-match
436 "[\r\n]" org-list-end-re
(1+ i
)))
438 (setq end-bounds
(org-in-regexp org-list-end-re hl
)))
439 (>= (point) (car end-bounds
))
440 (< (point) (cdr end-bounds
)))
441 (goto-char (car end-bounds
))
443 ;; Look for an item, less indented that reference line.
446 (let ((ind (current-indentation)))
448 ;; This is exactly what we want.
449 ((and (looking-at item-re
) (< ind ind-ref
))
450 (throw 'exit
(point)))
451 ;; At upper bound of search or looking at the end of a
452 ;; previous list: search is over.
453 ((<= (point) lim-up
) (throw 'exit nil
))
454 ((looking-at org-list-end-re
) (throw 'exit nil
))
455 ;; Skip blocks, drawers, inline-tasks, blank lines
456 ((and (looking-at "^[ \t]*#\\+end_")
457 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
458 ((and (looking-at "^[ \t]*:END:")
459 (re-search-backward org-drawer-regexp lim-up t
))
461 ((and inlinetask-re
(looking-at inlinetask-re
))
462 (org-inlinetask-goto-beginning)
464 ((looking-at "^[ \t]*$") (forward-line -
1))
465 ;; Text at column 0 cannot belong to a list: stop.
466 ((zerop ind
) (throw 'exit nil
))
467 ;; Normal text less indented than reference line, take
468 ;; it as new reference.
472 (t (forward-line -
1)))))))))))
474 (defun org-at-item-p ()
475 "Is point in a line starting a hand-formatted item?"
478 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
480 (defun org-at-item-bullet-p ()
481 "Is point at the bullet of a plain list item?"
483 (not (member (char-after) '(?\ ?
\t)))
484 (< (point) (match-end 0))))
486 (defun org-at-item-timer-p ()
487 "Is point at a line starting a plain list item with a timer?"
488 (org-list-at-regexp-after-bullet-p
489 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
491 (defun org-at-item-description-p ()
492 "Is point at a description list item?"
493 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::\\([ \t]+\\|$\\)"))
495 (defun org-at-item-checkbox-p ()
496 "Is point at a line starting a plain-list item with a checklet?"
497 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
499 (defun org-at-item-counter-p ()
500 "Is point at a line starting a plain-list item with a counter?"
502 (looking-at org-list-full-item-re
)
507 ;;; Structures and helper functions
509 (defun org-list-context ()
510 "Determine context, and its boundaries, around point.
512 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
513 are boundaries and CONTEXT is a symbol among `drawer', `block',
514 `invalid', `inlinetask' and nil.
516 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
519 (org-with-limited-levels
521 (let ((case-fold-search t
) (pos (point)) beg end context-type
522 ;; Get positions of surrounding headings. This is the
524 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t
))
527 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
528 ;; Is point inside a drawer?
529 (let ((end-re "^[ \t]*:END:")
530 (beg-re org-drawer-regexp
))
531 (when (save-excursion
532 (and (not (looking-at beg-re
))
533 (not (looking-at end-re
))
534 (setq beg
(and (re-search-backward beg-re lim-up t
)
535 (1+ (point-at-eol))))
536 (setq end
(or (and (re-search-forward end-re lim-down t
)
537 (1- (match-beginning 0)))
540 (setq lim-up beg lim-down end context-type
'drawer
)))
541 ;; Is point strictly in a block, and of which type?
542 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type
)
543 (when (save-excursion
544 (and (not (looking-at block-re
))
545 (setq beg
(and (re-search-backward block-re lim-up t
)
546 (1+ (point-at-eol))))
547 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
548 (setq type
(downcase (match-string 1)))
550 (setq end
(or (and (re-search-forward block-re lim-down t
)
554 (equal (downcase (match-string 1)) "end")))
555 (setq lim-up beg lim-down end
556 context-type
(if (member type org-list-forbidden-blocks
)
558 ;; Is point in an inlinetask?
559 (when (and (featurep 'org-inlinetask
)
561 (let* ((beg-re (org-inlinetask-outline-regexp))
562 (end-re (concat beg-re
"END[ \t]*$")))
563 (and (not (looking-at "^\\*+"))
564 (setq beg
(and (re-search-backward beg-re lim-up t
)
565 (1+ (point-at-eol))))
566 (not (looking-at end-re
))
567 (setq end
(and (re-search-forward end-re lim-down t
)
568 (1- (match-beginning 0))))
570 (setq lim-up beg lim-down end context-type
'inlinetask
))
571 ;; Return context boundaries and type.
572 (list lim-up lim-down context-type
))))))
574 (defun org-list-struct ()
575 "Return structure of list at point.
577 A list structure is an alist where key is point at item, and
580 2. bullet with trailing whitespace,
581 3. bullet counter, if any,
583 5. description tag, if any,
584 6. position at item end.
586 Thus the following list, where numbers in parens are
591 5. [@5] sub-item 2 (34)
592 some other text belonging to first item (55)
594 + tag :: description (109)
597 will get the following structure:
599 ((1 0 \"- \" nil \"[X]\" nil 97)
600 (18 2 \"1. \" nil nil nil 34)
601 (34 2 \"5. \" \"5\" nil nil 55)
602 (97 0 \"- \" nil nil nil 131)
603 (109 2 \"+ \" nil nil \"tag\" 131))
605 Assume point is at an item."
608 (let* ((case-fold-search t
)
609 (context (org-list-context))
610 (lim-up (car context
))
611 (lim-down (nth 1 context
))
613 (item-re (org-item-re))
614 (inlinetask-re (and (featurep 'org-inlinetask
)
615 (org-inlinetask-outline-regexp)))
616 (beg-cell (cons (point) (current-indentation)))
617 itm-lst itm-lst-2 end-lst end-lst-2 struct
620 ;; Return association at point.
622 (looking-at org-list-full-item-re
)
623 (let ((bullet (match-string-no-properties 1)))
627 (match-string-no-properties 2) ; counter
628 (match-string-no-properties 3) ; checkbox
630 (and (string-match-p "[-+*]" bullet
)
631 (match-string-no-properties 4)))))))
634 ;; Ensure list ends at the first blank line.
636 (skip-chars-backward " \r\t\n")
637 (min (1+ (point-at-eol)) lim-down
)))))
638 ;; 1. Read list from starting item to its beginning, and save
639 ;; top item position and indentation in BEG-CELL. Also store
640 ;; ending position of items in END-LST.
644 (let ((ind (current-indentation)))
647 ;; At upward limit: if we ended at an item, store it,
648 ;; else dismiss useless data recorded above BEG-CELL.
652 (if (not (looking-at item-re
))
653 (memq (assq (car beg-cell
) itm-lst
) itm-lst
)
654 (setq beg-cell
(cons (point) ind
))
655 (cons (funcall assoc-at-point ind
) itm-lst
)))))
656 ;; Looking at a list ending regexp. Dismiss useless
657 ;; data recorded above BEG-CELL. Jump to part 2.
658 ((looking-at org-list-end-re
)
661 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
662 ;; Point is at an item. Add data to ITM-LST. It may
663 ;; also end a previous item: save it in END-LST. If
664 ;; ind is less or equal than BEG-CELL and there is no
665 ;; end at this ind or lesser, this item becomes the new
667 ((looking-at item-re
)
668 (push (funcall assoc-at-point ind
) itm-lst
)
669 (push (cons ind
(point)) end-lst
)
670 (when (< ind text-min-ind
) (setq beg-cell
(cons (point) ind
)))
672 ;; Skip blocks, drawers, inline tasks, blank lines.
673 ((and (looking-at "^[ \t]*#\\+end_")
674 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
675 ((and (looking-at "^[ \t]*:END:")
676 (re-search-backward org-drawer-regexp lim-up t
))
678 ((and inlinetask-re
(looking-at inlinetask-re
))
679 (org-inlinetask-goto-beginning)
681 ((looking-at "^[ \t]*$")
683 ;; From there, point is not at an item. Interpret
684 ;; line's indentation:
685 ;; - text at column 0 is necessarily out of any list.
686 ;; Dismiss data recorded above BEG-CELL. Jump to
688 ;; - any other case may be an ending position for an
689 ;; hypothetical item above. Store it and proceed.
693 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
695 (when (< ind text-min-ind
) (setq text-min-ind ind
))
696 (push (cons ind
(point)) end-lst
)
697 (forward-line -
1)))))))
698 ;; 2. Read list from starting point to its end, that is until we
699 ;; get out of context, or that a non-item line is less or
700 ;; equally indented than BEG-CELL's cdr. Also, store ending
701 ;; position of items in END-LST-2.
704 (let ((ind (current-indentation)))
706 ((>= (point) lim-down
)
707 ;; At downward limit: this is de facto the end of the
708 ;; list. Save point as an ending position, and jump to
711 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
712 ;; Looking at a list ending regexp. Save point as an
713 ;; ending position and jump to part 3.
714 ((looking-at org-list-end-re
)
715 (throw 'exit
(push (cons 0 (point)) end-lst-2
)))
716 ((looking-at item-re
)
717 ;; Point is at an item. Add data to ITM-LST-2. It may
718 ;; also end a previous item, so save it in END-LST-2.
719 (push (funcall assoc-at-point ind
) itm-lst-2
)
720 (push (cons ind
(point)) end-lst-2
)
722 ;; Skip inline tasks and blank lines along the way
723 ((and inlinetask-re
(looking-at inlinetask-re
))
724 (org-inlinetask-goto-end))
725 ((looking-at "^[ \t]*$")
727 ;; Ind is lesser or equal than BEG-CELL's. The list is
728 ;; over: store point as an ending position and jump to
730 ((<= ind
(cdr beg-cell
))
732 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
733 ;; Else, if ind is lesser or equal than previous item's,
734 ;; this is an ending position: store it. In any case,
735 ;; skip block or drawer at point, and move to next line.
737 (when (<= ind
(nth 1 (car itm-lst-2
)))
738 (push (cons ind
(point)) end-lst-2
))
740 ((and (looking-at "^[ \t]*#\\+begin_")
741 (re-search-forward "^[ \t]*#\\+end_" lim-down t
)))
742 ((and (looking-at org-drawer-regexp
)
743 (re-search-forward "^[ \t]*:END:" lim-down t
))))
744 (forward-line 1))))))
745 (setq struct
(append itm-lst
(cdr (nreverse itm-lst-2
)))
746 end-lst
(append end-lst
(cdr (nreverse end-lst-2
))))
747 ;; 3. Associate each item to its end position.
748 (org-list-struct-assoc-end struct end-lst
)
752 (defun org-list-struct-assoc-end (struct end-list
)
753 "Associate proper ending point to items in STRUCT.
755 END-LIST is a pseudo-alist where car is indentation and cdr is
758 This function modifies STRUCT."
759 (let ((endings end-list
))
762 (let ((pos (car elt
))
764 ;; Remove end candidates behind current item.
765 (while (or (<= (cdar endings
) pos
))
767 ;; Add end position to item assoc.
768 (let ((old-end (nthcdr 6 elt
))
769 (new-end (assoc-default ind endings
'<=)))
771 (setcar old-end new-end
)
772 (setcdr elt
(append (cdr elt
) (list new-end
)))))))
775 (defun org-list-prevs-alist (struct)
776 "Return alist between item and previous item in STRUCT."
777 (let ((item-end-alist (mapcar (lambda (e) (cons (car e
) (nth 6 e
)))
780 (let ((prev (car (rassq (car e
) item-end-alist
))))
781 (cons (car e
) prev
)))
784 (defun org-list-parents-alist (struct)
785 "Return alist between item and parent in STRUCT."
786 (let* ((ind-to-ori (list (list (nth 1 (car struct
)))))
787 (top-item (org-list-get-top-point struct
))
788 (prev-pos (list top-item
)))
790 (mapcar (lambda (item)
791 (let ((pos (car item
))
793 (prev-ind (caar ind-to-ori
)))
797 ;; A sub-list is over. Find the associated
798 ;; origin in IND-TO-ORI. If it cannot be
799 ;; found (ill-formed list), set its parent as
800 ;; the first item less indented. If there is
801 ;; none, make it a top-level item.
803 (or (member (assq ind ind-to-ori
) ind-to-ori
)
807 (when (< (car e
) ind
)
808 (throw 'exit
(member e ind-to-ori
))))
811 (cons pos
(cdar ind-to-ori
)))
812 ;; A sub-list starts. Every item at IND will
813 ;; have previous item as its parent.
815 (let ((origin (nth 1 prev-pos
)))
816 (push (cons ind origin
) ind-to-ori
)
818 ;; Another item in the same sub-list: it shares
819 ;; the same parent as the previous item.
820 (t (cons pos
(cdar ind-to-ori
))))))
823 (defun org-list--delete-metadata ()
824 "Delete metadata from the heading at point.
825 Metadata are tags, planning information and properties drawers."
827 (org-with-wide-buffer
829 (delete-region (line-beginning-position 2)
831 (org-end-of-meta-data)
832 (org-skip-whitespace)
833 (if (eobp) (point) (line-beginning-position)))))))
838 (defsubst org-list-get-nth
(n key struct
)
839 "Return the Nth value of KEY in STRUCT."
840 (nth n
(assq key struct
)))
842 (defun org-list-set-nth (n key struct new
)
843 "Set the Nth value of KEY in STRUCT to NEW.
844 \nThis function modifies STRUCT."
845 (setcar (nthcdr n
(assq key struct
)) new
))
847 (defsubst org-list-get-ind
(item struct
)
848 "Return indentation of ITEM in STRUCT."
849 (org-list-get-nth 1 item struct
))
851 (defun org-list-set-ind (item struct ind
)
852 "Set indentation of ITEM in STRUCT to IND.
853 \nThis function modifies STRUCT."
854 (org-list-set-nth 1 item struct ind
))
856 (defsubst org-list-get-bullet
(item struct
)
857 "Return bullet of ITEM in STRUCT."
858 (org-list-get-nth 2 item struct
))
860 (defun org-list-set-bullet (item struct bullet
)
861 "Set bullet of ITEM in STRUCT to BULLET.
862 \nThis function modifies STRUCT."
863 (org-list-set-nth 2 item struct bullet
))
865 (defsubst org-list-get-counter
(item struct
)
866 "Return counter of ITEM in STRUCT."
867 (org-list-get-nth 3 item struct
))
869 (defsubst org-list-get-checkbox
(item struct
)
870 "Return checkbox of ITEM in STRUCT or nil."
871 (org-list-get-nth 4 item struct
))
873 (defun org-list-set-checkbox (item struct checkbox
)
874 "Set checkbox of ITEM in STRUCT to CHECKBOX.
875 \nThis function modifies STRUCT."
876 (org-list-set-nth 4 item struct checkbox
))
878 (defsubst org-list-get-tag
(item struct
)
879 "Return end position of ITEM in STRUCT."
880 (org-list-get-nth 5 item struct
))
882 (defun org-list-get-item-end (item struct
)
883 "Return end position of ITEM in STRUCT."
884 (org-list-get-nth 6 item struct
))
886 (defun org-list-get-item-end-before-blank (item struct
)
887 "Return point at end of ITEM in STRUCT, before any blank line.
888 Point returned is at end of line."
890 (goto-char (org-list-get-item-end item struct
))
891 (skip-chars-backward " \r\t\n")
894 (defun org-list-get-parent (item struct parents
)
895 "Return parent of ITEM or nil.
896 STRUCT is the list structure. PARENTS is the alist of parents,
897 as returned by `org-list-parents-alist'."
898 (let ((parents (or parents
(org-list-parents-alist struct
))))
899 (cdr (assq item parents
))))
901 (defun org-list-has-child-p (item struct
)
902 "Non-nil if ITEM has a child.
904 STRUCT is the list structure.
906 Value returned is the position of the first child of ITEM."
907 (let ((ind (org-list-get-ind item struct
))
908 (child-maybe (car (nth 1 (member (assq item struct
) struct
)))))
909 (when (and child-maybe
910 (< ind
(org-list-get-ind child-maybe struct
)))
913 (defun org-list-get-next-item (item _struct prevs
)
914 "Return next item in same sub-list as ITEM, or nil.
915 STRUCT is the list structure. PREVS is the alist of previous
916 items, as returned by `org-list-prevs-alist'."
917 (car (rassq item prevs
)))
919 (defun org-list-get-prev-item (item _struct prevs
)
920 "Return previous item in same sub-list as ITEM, or nil.
921 STRUCT is the list structure. PREVS is the alist of previous
922 items, as returned by `org-list-prevs-alist'."
923 (cdr (assq item prevs
)))
925 (defun org-list-get-subtree (item struct
)
926 "List all items having ITEM as a common ancestor, or nil.
927 STRUCT is the list structure."
928 (let* ((item-end (org-list-get-item-end item struct
))
929 (sub-struct (cdr (member (assq item struct
) struct
)))
934 (if (< pos item-end
) (push pos subtree
) (throw 'exit nil
))))
938 (defun org-list-get-all-items (item struct prevs
)
939 "List all items in the same sub-list as ITEM.
940 STRUCT is the list structure. PREVS is the alist of previous
941 items, as returned by `org-list-prevs-alist'."
942 (let ((prev-item item
)
944 before-item after-item
)
945 (while (setq prev-item
(org-list-get-prev-item prev-item struct prevs
))
946 (push prev-item before-item
))
947 (while (setq next-item
(org-list-get-next-item next-item struct prevs
))
948 (push next-item after-item
))
949 (append before-item
(list item
) (nreverse after-item
))))
951 (defun org-list-get-children (item _struct parents
)
952 "List all children of ITEM, or nil.
953 STRUCT is the list structure. PARENTS is the alist of parents,
954 as returned by `org-list-parents-alist'."
956 (while (setq child
(car (rassq item parents
)))
957 (setq parents
(cdr (member (assq child parents
) parents
)))
961 (defun org-list-get-top-point (struct)
962 "Return point at beginning of list.
963 STRUCT is the list structure."
966 (defun org-list-get-bottom-point (struct)
967 "Return point at bottom of list.
968 STRUCT is the list structure."
970 (mapcar (lambda (e) (org-list-get-item-end (car e
) struct
)) struct
)))
972 (defun org-list-get-list-begin (item struct prevs
)
973 "Return point at beginning of sub-list ITEM belongs.
974 STRUCT is the list structure. PREVS is the alist of previous
975 items, as returned by `org-list-prevs-alist'."
976 (let ((first-item item
) prev-item
)
977 (while (setq prev-item
(org-list-get-prev-item first-item struct prevs
))
978 (setq first-item prev-item
))
981 (defalias 'org-list-get-first-item
'org-list-get-list-begin
)
983 (defun org-list-get-last-item (item struct prevs
)
984 "Return point at last item of sub-list ITEM belongs.
985 STRUCT is the list structure. PREVS is the alist of previous
986 items, as returned by `org-list-prevs-alist'."
987 (let ((last-item item
) next-item
)
988 (while (setq next-item
(org-list-get-next-item last-item struct prevs
))
989 (setq last-item next-item
))
992 (defun org-list-get-list-end (item struct prevs
)
993 "Return point at end of sub-list ITEM belongs.
994 STRUCT is the list structure. PREVS is the alist of previous
995 items, as returned by `org-list-prevs-alist'."
996 (org-list-get-item-end (org-list-get-last-item item struct prevs
) struct
))
998 (defun org-list-get-list-type (item struct prevs
)
999 "Return the type of the list containing ITEM, as a symbol.
1001 STRUCT is the list structure. PREVS is the alist of previous
1002 items, as returned by `org-list-prevs-alist'.
1004 Possible types are `descriptive', `ordered' and `unordered'. The
1005 type is determined by the first item of the list."
1006 (let ((first (org-list-get-list-begin item struct prevs
)))
1008 ((string-match-p "[[:alnum:]]" (org-list-get-bullet first struct
)) 'ordered
)
1009 ((org-list-get-tag first struct
) 'descriptive
)
1012 (defun org-list-get-item-number (item struct prevs parents
)
1013 "Return ITEM's sequence number.
1015 STRUCT is the list structure. PREVS is the alist of previous
1016 items, as returned by `org-list-prevs-alist'. PARENTS is the
1017 alist of ancestors, as returned by `org-list-parents-alist'.
1019 Return value is a list of integers. Counters have an impact on
1021 (let ((get-relative-number
1023 (lambda (item struct prevs
)
1024 ;; Return relative sequence number of ITEM in the sub-list
1025 ;; it belongs. STRUCT is the list structure. PREVS is
1026 ;; the alist of previous items.
1027 (let ((seq 0) (pos item
) counter
)
1028 (while (and (not (setq counter
(org-list-get-counter pos struct
)))
1029 (setq pos
(org-list-get-prev-item pos struct prevs
)))
1031 (if (not counter
) (1+ seq
)
1033 ((string-match "[A-Za-z]" counter
)
1034 (+ (- (string-to-char (upcase (match-string 0 counter
))) 64)
1036 ((string-match "[0-9]+" counter
)
1037 (+ (string-to-number (match-string 0 counter
)) seq
))
1039 ;; Cons each parent relative number into return value (OUT).
1040 (let ((out (list (funcall get-relative-number item struct prevs
)))
1042 (while (setq parent
(org-list-get-parent parent struct parents
))
1043 (push (funcall get-relative-number parent struct prevs
) out
))
1051 (defun org-list-search-generic (search re bound noerr
)
1052 "Search a string in valid contexts for lists.
1053 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1054 in `re-search-forward'."
1056 (let ((origin (point)))
1058 ;; 1. No match: return to origin or bound, depending on NOERR.
1059 (unless (funcall search re bound noerr
)
1060 (throw 'exit
(and (goto-char (if (memq noerr
'(t nil
)) origin bound
))
1062 ;; 2. Match in valid context: return point. Else, continue
1064 (when (org-list-in-valid-context-p) (throw 'exit
(point)))))))
1066 (defun org-list-search-backward (regexp &optional bound noerror
)
1067 "Like `re-search-backward' but stop only where lists are recognized.
1068 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1069 `re-search-backward'."
1070 (org-list-search-generic #'re-search-backward
1071 regexp
(or bound
(point-min)) noerror
))
1073 (defun org-list-search-forward (regexp &optional bound noerror
)
1074 "Like `re-search-forward' but stop only where lists are recognized.
1075 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1076 `re-search-forward'."
1077 (org-list-search-generic #'re-search-forward
1078 regexp
(or bound
(point-max)) noerror
))
1082 ;;; Methods on structures
1084 (defsubst org-list-bullet-string
(bullet)
1085 "Return BULLET with the correct number of whitespaces.
1086 It determines the number of whitespaces to append by looking at
1087 `org-list-two-spaces-after-bullet-regexp'."
1089 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1091 org-list-two-spaces-after-bullet-regexp bullet
))
1094 (if (string-match "\\S-+\\([ \t]*\\)" bullet
)
1095 (replace-match spaces nil nil bullet
1)
1098 (defun org-list-swap-items (beg-A beg-B struct
)
1099 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1101 Blank lines at the end of items are left in place. Item
1102 visibility is preserved. Return the new structure after the
1105 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1106 to the same sub-list.
1108 This function modifies STRUCT."
1110 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct
))
1111 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct
))
1112 (end-A (org-list-get-item-end beg-A struct
))
1113 (end-B (org-list-get-item-end beg-B struct
))
1114 (size-A (- end-A-no-blank beg-A
))
1115 (size-B (- end-B-no-blank beg-B
))
1116 (body-A (buffer-substring beg-A end-A-no-blank
))
1117 (body-B (buffer-substring beg-B end-B-no-blank
))
1118 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B
))
1119 (sub-A (cons beg-A
(org-list-get-subtree beg-A struct
)))
1120 (sub-B (cons beg-B
(org-list-get-subtree beg-B struct
)))
1121 ;; Store overlays responsible for visibility status. We
1122 ;; also need to store their boundaries as they will be
1123 ;; removed from buffer.
1128 (and (>= (overlay-start o
) beg-A
)
1129 (<= (overlay-end o
) end-A
)
1130 (list o
(overlay-start o
) (overlay-end o
))))
1131 (overlays-in beg-A end-A
)))
1134 (and (>= (overlay-start o
) beg-B
)
1135 (<= (overlay-end o
) end-B
)
1136 (list o
(overlay-start o
) (overlay-end o
))))
1137 (overlays-in beg-B end-B
))))))
1138 ;; 1. Move effectively items in buffer.
1140 (delete-region beg-A end-B-no-blank
)
1141 (insert (concat body-B between-A-no-blank-and-B body-A
))
1142 ;; 2. Now modify struct. No need to re-read the list, the
1143 ;; transformation is just a shift of positions. Some special
1144 ;; attention is required for items ending at END-A and END-B
1145 ;; as empty spaces are not moved there. In others words,
1146 ;; item BEG-A will end with whitespaces that were at the end
1147 ;; of BEG-B and the same applies to BEG-B.
1149 (let ((pos (car e
)))
1153 (let ((end-e (nth 6 e
)))
1154 (setcar e
(+ pos
(- end-B-no-blank end-A-no-blank
)))
1155 (setcar (nthcdr 6 e
)
1156 (+ end-e
(- end-B-no-blank end-A-no-blank
)))
1157 (when (= end-e end-A
) (setcar (nthcdr 6 e
) end-B
))))
1159 (let ((end-e (nth 6 e
)))
1160 (setcar e
(- (+ pos beg-A
) beg-B
))
1161 (setcar (nthcdr 6 e
) (+ end-e
(- beg-A beg-B
)))
1162 (when (= end-e end-B
)
1163 (setcar (nthcdr 6 e
)
1164 (+ beg-A size-B
(- end-A end-A-no-blank
))))))
1166 (let ((end-e (nth 6 e
)))
1167 (setcar e
(+ pos
(- size-B size-A
)))
1168 (setcar (nthcdr 6 e
) (+ end-e
(- size-B size-A
))))))))
1169 (setq struct
(sort struct
#'car-less-than-car
))
1170 ;; Restore visibility status, by moving overlays to their new
1172 (dolist (ov (car overlays
))
1175 (+ (nth 1 ov
) (- (+ beg-B
(- size-B size-A
)) beg-A
))
1176 (+ (nth 2 ov
) (- (+ beg-B
(- size-B size-A
)) beg-A
))))
1177 (dolist (ov (cdr overlays
))
1178 (move-overlay (car ov
)
1179 (+ (nth 1 ov
) (- beg-A beg-B
))
1180 (+ (nth 2 ov
) (- beg-A beg-B
))))
1181 ;; Return structure.
1184 (defun org-list-separating-blank-lines-number (pos struct prevs
)
1185 "Return number of blank lines that should separate items in list.
1187 POS is the position of point where `org-list-insert-item' was called.
1189 STRUCT is the list structure. PREVS is the alist of previous
1190 items, as returned by `org-list-prevs-alist'.
1192 Assume point is at item's beginning. If the item is alone, apply
1193 some heuristics to guess the result."
1195 (let ((item (point))
1197 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
1202 ;; Count blank lines above beginning of line.
1204 (count-lines (goto-char (point-at-bol))
1205 (progn (skip-chars-backward " \r\t\n")
1209 ;; Trivial cases where there should be none.
1210 ((not insert-blank-p
) 0)
1211 ;; When `org-blank-before-new-entry' says so, it is 1.
1212 ((eq insert-blank-p t
) 1)
1213 ;; `plain-list-item' is 'auto. Count blank lines separating
1214 ;; neighbors' items in list.
1215 (t (let ((next-p (org-list-get-next-item item struct prevs
)))
1217 ;; Is there a next item?
1218 (next-p (goto-char next-p
)
1219 (funcall count-blanks
))
1220 ;; Is there a previous item?
1221 ((org-list-get-prev-item item struct prevs
)
1222 (funcall count-blanks
))
1223 ;; User inserted blank lines, trust him.
1224 ((and (> pos
(org-list-get-item-end-before-blank item struct
))
1225 (> (save-excursion (goto-char pos
)
1226 (setq usr-blank
(funcall count-blanks
)))
1229 ;; Are there blank lines inside the list so far?
1231 (goto-char (org-list-get-top-point struct
))
1232 ;; Do not use `org-list-search-forward' so blank lines
1233 ;; in blocks can be counted in.
1235 "^[ \t]*$" (org-list-get-item-end-before-blank item struct
) t
))
1237 ;; Default choice: no blank line.
1240 (defun org-list-insert-item (pos struct prevs
&optional checkbox after-bullet
)
1241 "Insert a new list item at POS and return the new structure.
1242 If POS is before first character after bullet of the item, the
1243 new item will be created before the current one.
1245 STRUCT is the list structure. PREVS is the alist of previous
1246 items, as returned by `org-list-prevs-alist'.
1248 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1249 after the bullet. Cursor will be after this text once the
1252 This function modifies STRUCT."
1253 (let ((case-fold-search t
))
1254 ;; 1. Get information about list: ITEM containing POS, position of
1255 ;; point with regards to item start (BEFOREP), blank lines
1256 ;; number separating items (BLANK-NB), if we're allowed to
1258 (let* ((item (goto-char (catch :exit
1259 (let ((inner-item 0))
1260 (pcase-dolist (`(,i .
,_
) struct
)
1262 ((= i pos
) (throw :exit i
))
1263 ((< i pos
) (setq inner-item i
))
1264 (t (throw :exit inner-item
))))
1266 (item-end (org-list-get-item-end item struct
))
1267 (item-end-no-blank (org-list-get-item-end-before-blank item struct
))
1270 (looking-at org-list-full-item-re
)
1273 ((not (match-beginning 4)) (match-end 0))
1274 ;; Ignore tag in a non-descriptive list.
1275 ((save-match-data (string-match "[.)]" (match-string 1)))
1276 (match-beginning 4))
1278 (goto-char (match-end 4))
1279 (skip-chars-forward " \t")
1281 (split-line-p (org-get-alist-option org-M-RET-may-split-line
'item
))
1282 (blank-nb (org-list-separating-blank-lines-number
1284 ;; 2. Build the new item to be created. Concatenate same
1285 ;; bullet as item, checkbox, text AFTER-BULLET if
1286 ;; provided, and text cut from point to end of item
1287 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1288 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1289 ;; between what was cut and what was inserted in buffer
1290 ;; is stored in SIZE-OFFSET.
1291 (ind (org-list-get-ind item struct
))
1292 (ind-size (if indent-tabs-mode
1293 (+ (/ ind tab-width
) (mod ind tab-width
))
1295 (bullet (org-list-bullet-string (org-list-get-bullet item struct
)))
1296 (box (when checkbox
"[ ]"))
1298 (and (not beforep
) split-line-p
1301 ;; If POS is greater than ITEM-END, then point is
1302 ;; in some white lines after the end of the list.
1303 ;; Those must be removed, or they will be left,
1304 ;; stacking up after the list.
1305 (when (< item-end pos
)
1306 (delete-region (1- item-end
) (point-at-eol)))
1307 (skip-chars-backward " \r\t\n")
1309 (delete-and-extract-region pos item-end-no-blank
))))
1310 (body (concat bullet
(when box
(concat box
" ")) after-bullet
1312 (if (string-match "\\`[ \t]+" text-cut
)
1313 (replace-match "" t t text-cut
)
1315 (item-sep (make-string (1+ blank-nb
) ?
\n))
1316 (item-size (+ ind-size
(length body
) (length item-sep
)))
1317 (size-offset (- item-size
(length text-cut
))))
1318 ;; 4. Insert effectively item into buffer.
1320 (indent-to-column ind
)
1321 (insert body item-sep
)
1322 ;; 5. Add new item to STRUCT.
1324 (let ((p (car e
)) (end (nth 6 e
)))
1326 ;; Before inserted item, positions don't change but
1327 ;; an item ending after insertion has its end shifted
1330 (when (> end item
) (setcar (nthcdr 6 e
) (+ end size-offset
))))
1331 ;; Trivial cases where current item isn't split in
1332 ;; two. Just shift every item after new one by
1334 ((or beforep
(not split-line-p
))
1335 (setcar e
(+ p item-size
))
1336 (setcar (nthcdr 6 e
) (+ end item-size
)))
1337 ;; Item is split in two: elements before POS are just
1338 ;; shifted by ITEM-SIZE. In the case item would end
1339 ;; after split POS, ending is only shifted by
1342 (setcar e
(+ p item-size
))
1344 (setcar (nthcdr 6 e
) (+ end item-size
))
1345 (setcar (nthcdr 6 e
) (+ end size-offset
))))
1346 ;; Elements after POS are moved into new item.
1347 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1348 ;; doesn't appear in buffer yet.
1350 (setcar e
(+ p size-offset
(- item pos
(length item-sep
))))
1351 (if (= end item-end
)
1352 (setcar (nthcdr 6 e
) (+ item item-size
))
1353 (setcar (nthcdr 6 e
)
1355 (- item pos
(length item-sep
))))))
1356 ;; Elements at ITEM-END or after are only shifted by
1358 (t (setcar e
(+ p size-offset
))
1359 (setcar (nthcdr 6 e
) (+ end size-offset
))))))
1361 (push (list item ind bullet nil box nil
(+ item item-size
)) struct
)
1362 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1363 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1364 ;; exchange ITEM with the next item in list. Position cursor
1365 ;; after bullet, counter, checkbox, and label.
1368 (setq struct
(org-list-swap-items item
(+ item item-size
) struct
))
1369 (goto-char (org-list-get-next-item
1370 item struct
(org-list-prevs-alist struct
))))
1373 (defun org-list-delete-item (item struct
)
1374 "Remove ITEM from the list and return the new structure.
1376 STRUCT is the list structure."
1377 (let* ((end (org-list-get-item-end item struct
))
1378 (beg (if (= (org-list-get-bottom-point struct
) end
)
1379 ;; If ITEM ends with the list, delete blank lines
1383 (skip-chars-backward " \r\t\n")
1384 (min (1+ (point-at-eol)) (point-max)))
1386 ;; Remove item from buffer.
1387 (delete-region beg end
)
1388 ;; Remove item from structure and shift others items accordingly.
1389 ;; Don't forget to shift also ending position when appropriate.
1390 (let ((size (- end beg
)))
1391 (delq nil
(mapcar (lambda (e)
1392 (let ((pos (car e
)))
1395 (let ((end-e (nth 6 e
)))
1399 (append (butlast e
) (list beg
)))
1401 (append (butlast e
) (list (- end-e size
)))))))
1405 (append (butlast (cdr e
))
1406 (list (- (nth 6 e
) size
))))))))
1409 (defun org-list-send-item (item dest struct
)
1410 "Send ITEM to destination DEST.
1412 STRUCT is the list structure.
1414 DEST can have various values.
1416 If DEST is a buffer position, the function will assume it points
1417 to another item in the same list as ITEM, and will move the
1418 latter just before the former.
1420 If DEST is `begin' (respectively `end'), ITEM will be moved at
1421 the beginning (respectively end) of the list it belongs to.
1423 If DEST is a string like \"N\", where N is an integer, ITEM will
1424 be moved at the Nth position in the list.
1426 If DEST is `kill', ITEM will be deleted and its body will be
1427 added to the kill-ring.
1429 If DEST is `delete', ITEM will be deleted.
1431 Visibility of item is preserved.
1433 This function returns, destructively, the new list structure."
1434 (let* ((prevs (org-list-prevs-alist struct
))
1435 (item-end (org-list-get-item-end item struct
))
1436 ;; Grab full item body minus its bullet.
1443 (regexp-quote (org-list-get-bullet item struct
))))
1446 ;; Change DEST into a buffer position. A trick is needed
1447 ;; when ITEM is meant to be sent at the end of the list.
1448 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1449 ;; nil and insertion point (INS-POINT) to the first line's
1450 ;; end of the last item, we ensure the new item will be
1451 ;; inserted after the last item, and not after any of its
1452 ;; hypothetical sub-items.
1454 ((or (eq dest
'kill
) (eq dest
'delete
)))
1456 (setq dest
(org-list-get-list-begin item struct prevs
)))
1458 (setq dest
(org-list-get-list-end item struct prevs
))
1460 (goto-char (org-list-get-last-item item struct prevs
))
1462 ((string-match-p "\\`[0-9]+\\'" dest
)
1463 (let* ((all (org-list-get-all-items item struct prevs
))
1465 (index (mod (string-to-number dest
) len
)))
1466 (if (not (zerop index
))
1467 (setq dest
(nth (1- index
) all
))
1468 ;; Send ITEM at the end of the list.
1469 (setq dest
(org-list-get-list-end item struct prevs
))
1472 (org-list-get-last-item item struct prevs
))
1475 (org-M-RET-may-split-line nil
)
1476 ;; Store inner overlays (to preserve visibility).
1477 (overlays (cl-remove-if (lambda (o) (or (< (overlay-start o
) item
)
1478 (> (overlay-end o
) item
)))
1479 (overlays-in item item-end
))))
1481 ((eq dest
'delete
) (org-list-delete-item item struct
))
1484 (org-list-delete-item item struct
))
1485 ((and (integerp dest
) (/= item ins-point
))
1486 (setq item
(copy-marker item
))
1487 (setq struct
(org-list-insert-item ins-point struct prevs nil body
))
1488 ;; 1. Structure returned by `org-list-insert-item' may not be
1489 ;; accurate, as it cannot see sub-items included in BODY.
1490 ;; Thus, first compute the real structure so far.
1492 (cons (marker-position item
)
1493 (org-list-get-subtree (marker-position item
) struct
)))
1494 (new-end (org-list-get-item-end (point) struct
))
1495 (old-end (org-list-get-item-end (marker-position item
) struct
))
1497 (shift (- (point) item
)))
1498 ;; 1.1. Remove the item just created in structure.
1499 (setq struct
(delete (assq new-item struct
) struct
))
1500 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1505 (let* ((cell (assq e struct
))
1509 (append (butlast (cdr cell
))
1510 (list (if (= end old-end
)
1514 #'car-less-than-car
)))
1515 ;; 2. Restore inner overlays.
1516 (dolist (o overlays
)
1518 (+ (overlay-start o
) (- (point) item
))
1519 (+ (overlay-end o
) (- (point) item
))))
1520 ;; 3. Eventually delete extra copy of the item and clean marker.
1521 (prog1 (org-list-delete-item (marker-position item
) struct
)
1522 (move-marker item nil
)))
1525 (defun org-list-struct-outdent (start end struct parents
)
1526 "Outdent items between positions START and END.
1528 STRUCT is the list structure. PARENTS is the alist of items'
1529 parents, as returned by `org-list-parents-alist'.
1531 START is included, END excluded."
1534 (let* ((item (car cell
))
1535 (parent (cdr cell
)))
1537 ;; Item not yet in zone: keep association.
1538 ((< item start
) cell
)
1539 ;; Item out of zone: follow associations in ACC.
1541 (let ((convert (and parent
(assq parent acc
))))
1542 (if convert
(cons item
(cdr convert
)) cell
)))
1543 ;; Item has no parent: error
1545 (error "Cannot outdent top-level items"))
1546 ;; Parent is outdented: keep association.
1548 (push (cons parent item
) acc
) cell
)
1550 ;; Parent isn't outdented: reparent to grand-parent.
1551 (let ((grand-parent (org-list-get-parent
1552 parent struct parents
)))
1553 (push (cons parent item
) acc
)
1554 (cons item grand-parent
))))))))
1555 (mapcar out parents
)))
1557 (defun org-list-struct-indent (start end struct parents prevs
)
1558 "Indent items between positions START and END.
1560 STRUCT is the list structure. PARENTS is the alist of parents
1561 and PREVS is the alist of previous items, returned by,
1562 respectively, `org-list-parents-alist' and
1563 `org-list-prevs-alist'.
1565 START is included and END excluded.
1567 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1568 bullets between START and END."
1570 (set-assoc (lambda (cell) (push cell acc
) cell
))
1571 (change-bullet-maybe
1576 ;; Normalize ordered bullets.
1577 (let ((bul (org-trim
1578 (org-list-get-bullet item struct
))))
1579 (cond ((string-match "[A-Z]\\." bul
) "A.")
1580 ((string-match "[A-Z])" bul
) "A)")
1581 ((string-match "[a-z]\\." bul
) "a.")
1582 ((string-match "[a-z])" bul
) "a)")
1583 ((string-match "[0-9]\\." bul
) "1.")
1584 ((string-match "[0-9])" bul
) "1)")
1586 org-list-demote-modify-bullet
))))
1587 (when new-bul-p
(org-list-set-bullet item struct new-bul-p
))))))
1590 (let* ((item (car cell
))
1591 (parent (cdr cell
)))
1593 ;; Item not yet in zone: keep association.
1594 ((< item start
) cell
)
1596 ;; Item out of zone: follow associations in ACC.
1597 (let ((convert (assq parent acc
)))
1598 (if convert
(cons item
(cdr convert
)) cell
)))
1600 ;; Item is in zone...
1601 (let ((prev (org-list-get-prev-item item struct prevs
)))
1602 ;; Check if bullet needs to be changed.
1603 (funcall change-bullet-maybe item
)
1605 ;; First item indented but not parent: error
1606 ((and (not prev
) (or (not parent
) (< parent start
)))
1607 (user-error "Cannot indent the first item of a list"))
1608 ;; First item and parent indented: keep same
1610 ((not prev
) (funcall set-assoc cell
))
1611 ;; Previous item not indented: reparent to it.
1612 ((< prev start
) (funcall set-assoc
(cons item prev
)))
1613 ;; Previous item indented: reparent like it.
1616 (cons item
(cdr (assq prev acc
)))))))))))))
1617 (mapcar ind parents
)))
1621 ;;; Repairing structures
1623 (defun org-list-use-alpha-bul-p (first struct prevs
)
1624 "Non-nil if list starting at FIRST can have alphabetical bullets.
1626 STRUCT is list structure. PREVS is the alist of previous items,
1627 as returned by `org-list-prevs-alist'."
1628 (and org-list-allow-alphabetical
1630 (let ((item first
) (ascii 64) (case-fold-search nil
))
1631 ;; Pretend that bullets are uppercase and check if alphabet
1632 ;; is sufficient, taking counters into account.
1634 (let ((count (org-list-get-counter item struct
)))
1635 ;; Virtually determine current bullet
1636 (if (and count
(string-match-p "[a-zA-Z]" count
))
1637 ;; Counters are not case-sensitive.
1638 (setq ascii
(string-to-char (upcase count
)))
1639 (setq ascii
(1+ ascii
)))
1640 ;; Test if bullet would be over z or Z.
1643 (setq item
(org-list-get-next-item item struct prevs
)))))
1644 ;; All items checked. All good.
1647 (defun org-list-inc-bullet-maybe (bullet)
1648 "Increment BULLET if applicable."
1649 (let ((case-fold-search nil
))
1651 ;; Num bullet: increment it.
1652 ((string-match "[0-9]+" bullet
)
1654 (number-to-string (1+ (string-to-number (match-string 0 bullet
))))
1656 ;; Alpha bullet: increment it.
1657 ((string-match "[A-Za-z]" bullet
)
1659 (char-to-string (1+ (string-to-char (match-string 0 bullet
))))
1661 ;; Unordered bullet: leave it.
1664 (defun org-list-struct-fix-bul (struct prevs
)
1665 "Verify and correct bullets in STRUCT.
1666 PREVS is the alist of previous items, as returned by
1667 `org-list-prevs-alist'.
1669 This function modifies STRUCT."
1670 (let ((case-fold-search nil
)
1673 ;; Set bullet of ITEM in STRUCT, depending on the type of
1674 ;; first item of the list, the previous bullet and counter
1677 (let* ((prev (org-list-get-prev-item item struct prevs
))
1678 (prev-bul (and prev
(org-list-get-bullet prev struct
)))
1679 (counter (org-list-get-counter item struct
))
1680 (bullet (org-list-get-bullet item struct
))
1681 (alphap (and (not prev
)
1682 (org-list-use-alpha-bul-p item struct prevs
))))
1683 (org-list-set-bullet
1685 (org-list-bullet-string
1687 ;; Alpha counter in alpha list: use counter.
1689 (string-match "[a-zA-Z]" counter
)
1690 (string-match "[a-zA-Z]" prev-bul
))
1691 ;; Use cond to be sure `string-match' is used in
1695 ((string-match "[a-z]" prev-bul
) (downcase counter
))
1696 ((string-match "[A-Z]" prev-bul
) (upcase counter
)))))
1697 (replace-match real-count nil nil prev-bul
)))
1698 ;; Num counter in a num list: use counter.
1700 (string-match "[0-9]+" counter
)
1701 (string-match "[0-9]+" prev-bul
))
1702 (replace-match counter nil nil prev-bul
))
1703 ;; No counter: increase, if needed, previous bullet.
1705 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct
)))
1706 ;; Alpha counter at first item: use counter.
1707 ((and counter
(org-list-use-alpha-bul-p item struct prevs
)
1708 (string-match "[A-Za-z]" counter
)
1709 (string-match "[A-Za-z]" bullet
))
1712 ((string-match "[a-z]" bullet
) (downcase counter
))
1713 ((string-match "[A-Z]" bullet
) (upcase counter
)))))
1714 (replace-match real-count nil nil bullet
)))
1715 ;; Num counter at first item: use counter.
1717 (string-match "[0-9]+" counter
)
1718 (string-match "[0-9]+" bullet
))
1719 (replace-match counter nil nil bullet
))
1720 ;; First bullet is alpha uppercase: use "A".
1721 ((and alphap
(string-match "[A-Z]" bullet
))
1722 (replace-match "A" nil nil bullet
))
1723 ;; First bullet is alpha lowercase: use "a".
1724 ((and alphap
(string-match "[a-z]" bullet
))
1725 (replace-match "a" nil nil bullet
))
1726 ;; First bullet is num: use "1".
1727 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet
)
1728 (replace-match "1" nil nil bullet
))
1729 ;; Not an ordered list: keep bullet.
1731 (mapc fix-bul
(mapcar #'car struct
))))
1733 (defun org-list-struct-fix-ind (struct parents
&optional bullet-size
)
1734 "Verify and correct indentation in STRUCT.
1736 PARENTS is the alist of parents, as returned by
1737 `org-list-parents-alist'.
1739 If numeric optional argument BULLET-SIZE is set, assume all
1740 bullets in list have this length to determine new indentation.
1742 This function modifies STRUCT."
1743 (let* ((ancestor (org-list-get-top-point struct
))
1744 (top-ind (org-list-get-ind ancestor struct
))
1747 (let ((parent (org-list-get-parent item struct parents
)))
1749 ;; Indent like parent + length of parent's bullet +
1752 item struct
(+ (or bullet-size
1754 (org-list-get-bullet parent struct
)))
1755 (org-list-get-ind parent struct
)
1756 org-list-indent-offset
))
1757 ;; If no parent, indent like top-point.
1758 (org-list-set-ind item struct top-ind
))))))
1759 (mapc new-ind
(mapcar #'car
(cdr struct
)))))
1761 (defun org-list-struct-fix-box (struct parents prevs
&optional ordered
)
1762 "Verify and correct checkboxes in STRUCT.
1764 PARENTS is the alist of parents and PREVS is the alist of
1765 previous items, as returned by, respectively,
1766 `org-list-parents-alist' and `org-list-prevs-alist'.
1768 If ORDERED is non-nil, a checkbox can only be checked when every
1769 checkbox before it is checked too. If there was an attempt to
1770 break this rule, the function will return the blocking item. In
1771 all others cases, the return value will be nil.
1773 This function modifies STRUCT."
1774 (let ((all-items (mapcar #'car struct
))
1779 (mapcar (lambda (child)
1780 (org-list-get-checkbox child struct
))
1781 (org-list-get-children item struct parents
))))
1782 (org-list-set-checkbox
1785 ((and (member "[ ]" box-list
) (member "[X]" box-list
)) "[-]")
1786 ((member "[-]" box-list
) "[-]")
1787 ((member "[X]" box-list
) "[X]")
1788 ((member "[ ]" box-list
) "[ ]")
1789 ;; Parent has no boxed child: leave box as-is.
1790 (t (org-list-get-checkbox item struct
))))))))
1792 ;; 1. List all parents with a checkbox.
1795 (let* ((parent (org-list-get-parent e struct parents
))
1796 (parent-box-p (org-list-get-checkbox parent struct
)))
1797 (when (and parent-box-p
(not (memq parent parent-list
)))
1798 (push parent parent-list
))))
1800 ;; 2. Sort those parents by decreasing indentation.
1801 (setq parent-list
(sort parent-list
1803 (> (org-list-get-ind e1 struct
)
1804 (org-list-get-ind e2 struct
)))))
1805 ;; 3. For each parent, get all children's checkboxes to determine
1806 ;; and set its checkbox accordingly.
1807 (mapc set-parent-box parent-list
)
1808 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1811 (mapcar (lambda (e) (org-list-get-checkbox e struct
)) all-items
))
1812 (after-unchecked (member "[ ]" box-list
)))
1813 ;; There are boxes checked after an unchecked one: fix that.
1814 (when (member "[X]" after-unchecked
)
1815 (let ((index (- (length struct
) (length after-unchecked
))))
1817 (when (org-list-get-checkbox e struct
)
1818 (org-list-set-checkbox e struct
"[ ]")))
1819 (nthcdr index all-items
))
1820 ;; Verify once again the structure, without ORDERED.
1821 (org-list-struct-fix-box struct parents prevs nil
)
1822 ;; Return blocking item.
1823 (nth index all-items
)))))))
1825 (defun org-list-struct-fix-item-end (struct)
1826 "Verify and correct each item end position in STRUCT.
1828 This function modifies STRUCT."
1829 (let (end-list acc-end
)
1831 (let* ((pos (car e
))
1832 (ind-pos (org-list-get-ind pos struct
))
1833 (end-pos (org-list-get-item-end pos struct
)))
1834 (unless (assq end-pos struct
)
1835 ;; To determine real ind of an ending position that is
1836 ;; not at an item, we have to find the item it belongs
1837 ;; to: it is the last item (ITEM-UP), whose ending is
1838 ;; further than the position we're interested in.
1839 (let ((item-up (assoc-default end-pos acc-end
'>)))
1841 ;; Else part is for the bottom point.
1842 (if item-up
(+ (org-list-get-ind item-up struct
) 2) 0)
1845 (push (cons ind-pos pos
) end-list
)
1846 (push (cons end-pos pos
) acc-end
)))
1848 (setq end-list
(sort end-list
(lambda (e1 e2
) (< (cdr e1
) (cdr e2
)))))
1849 (org-list-struct-assoc-end struct end-list
)))
1851 (defun org-list-struct-apply-struct (struct old-struct
)
1852 "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
1854 OLD-STRUCT is the structure before any modifications, and STRUCT
1855 the structure to be applied. The function will only modify parts
1856 of the list which have changed.
1858 Initial position of cursor is restored after the changes."
1859 (let* ((origin (point-marker))
1860 (inlinetask-re (and (featurep 'org-inlinetask
)
1861 (org-inlinetask-outline-regexp)))
1862 (item-re (org-item-re))
1865 ;; Shift the indentation between END and BEG by DELTA.
1866 ;; Start from the line before END.
1867 (lambda (end beg delta
)
1869 (skip-chars-backward " \r\t\n")
1871 (while (or (> (point) beg
)
1872 (and (= (point) beg
)
1873 (not (looking-at item-re
))))
1875 ;; Skip inline tasks.
1876 ((and inlinetask-re
(looking-at inlinetask-re
))
1877 (org-inlinetask-goto-beginning))
1878 ;; Shift only non-empty lines.
1879 ((looking-at-p "^[ \t]*\\S-")
1880 (indent-line-to (+ (current-indentation) delta
))))
1881 (forward-line -
1)))))
1884 ;; Replace ITEM first line elements with new elements from
1885 ;; STRUCT, if appropriate.
1888 (let* ((new-ind (org-list-get-ind item struct
))
1889 (old-ind (current-indentation))
1890 (new-bul (org-list-bullet-string
1891 (org-list-get-bullet item struct
)))
1892 (old-bul (org-list-get-bullet item old-struct
))
1893 (new-box (org-list-get-checkbox item struct
)))
1894 (looking-at org-list-full-item-re
)
1895 ;; a. Replace bullet
1896 (unless (equal old-bul new-bul
)
1897 (replace-match new-bul nil nil nil
1))
1898 ;; b. Replace checkbox.
1900 ((equal (match-string 3) new-box
))
1901 ((and (match-string 3) new-box
)
1902 (replace-match new-box nil nil nil
3))
1904 (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
1905 (replace-match "" nil nil nil
1))
1906 (t (let ((counterp (match-end 2)))
1907 (goto-char (if counterp
(1+ counterp
) (match-end 1)))
1908 (insert (concat new-box
(unless counterp
" "))))))
1909 ;; c. Indent item to appropriate column.
1910 (unless (= new-ind old-ind
)
1911 (delete-region (goto-char (point-at-bol))
1912 (progn (skip-chars-forward " \t") (point)))
1913 (indent-to new-ind
)))))))
1914 ;; 1. First get list of items and position endings. We maintain
1915 ;; two alists: ITM-SHIFT, determining indentation shift needed
1916 ;; at item, and END-LIST, a pseudo-alist where key is ending
1917 ;; position and value point.
1918 (let (end-list acc-end itm-shift all-ends sliced-struct
)
1919 (dolist (e old-struct
)
1920 (let* ((pos (car e
))
1921 (ind-pos (org-list-get-ind pos struct
))
1922 (ind-old (org-list-get-ind pos old-struct
))
1923 (bul-pos (org-list-get-bullet pos struct
))
1924 (bul-old (org-list-get-bullet pos old-struct
))
1925 (ind-shift (- (+ ind-pos
(length bul-pos
))
1926 (+ ind-old
(length bul-old
))))
1927 (end-pos (org-list-get-item-end pos old-struct
)))
1928 (push (cons pos ind-shift
) itm-shift
)
1929 (unless (assq end-pos old-struct
)
1930 ;; To determine real ind of an ending position that
1931 ;; is not at an item, we have to find the item it
1932 ;; belongs to: it is the last item (ITEM-UP), whose
1933 ;; ending is further than the position we're
1935 (let ((item-up (assoc-default end-pos acc-end
#'>)))
1936 (push (cons end-pos item-up
) end-list
)))
1937 (push (cons end-pos pos
) acc-end
)))
1938 ;; 2. Slice the items into parts that should be shifted by the
1939 ;; same amount of indentation. Each slice follow the pattern
1940 ;; (END BEG DELTA). Slices are returned in reverse order.
1941 (setq all-ends
(sort (append (mapcar #'car itm-shift
)
1942 (org-uniquify (mapcar #'car end-list
)))
1944 acc-end
(nreverse acc-end
))
1945 (while (cdr all-ends
)
1946 (let* ((up (pop all-ends
))
1947 (down (car all-ends
))
1948 (itemp (assq up struct
))
1950 (if itemp
(cdr (assq up itm-shift
))
1951 ;; If we're not at an item, there's a child of the
1952 ;; item point belongs to above. Make sure the less
1953 ;; indented line in this slice has the same column
1955 (let* ((child (cdr (assq up acc-end
)))
1956 (ind (org-list-get-ind child struct
))
1957 (min-ind most-positive-fixnum
))
1960 (while (< (point) down
)
1961 ;; Ignore empty lines. Also ignore blocks and
1962 ;; drawers contents.
1963 (unless (looking-at-p "[ \t]*$")
1964 (setq min-ind
(min (current-indentation) min-ind
))
1966 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1968 (format "^[ \t]*#\\+END%s[ \t]*$"
1971 ((and (looking-at org-drawer-regexp
)
1972 (re-search-forward "^[ \t]*:END:[ \t]*$"
1976 (push (list down up delta
) sliced-struct
)))
1977 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1978 ;; end to beginning. Take a special action when beginning is
1980 (dolist (e sliced-struct
)
1981 (unless (zerop (nth 2 e
)) (apply shift-body-ind e
))
1982 (let* ((beg (nth 1 e
))
1983 (cell (assq beg struct
)))
1984 (unless (or (not cell
) (equal cell
(assq beg old-struct
)))
1985 (funcall modify-item beg
)))))
1986 ;; 4. Go back to initial position and clean marker.
1988 (move-marker origin nil
)))
1990 (defun org-list-write-struct (struct parents
&optional old-struct
)
1991 "Correct bullets, checkboxes and indentation in list at point.
1993 STRUCT is the list structure. PARENTS is the alist of parents,
1994 as returned by `org-list-parents-alist'.
1996 When non-nil, optional argument OLD-STRUCT is the reference
1997 structure of the list. It should be provided whenever STRUCT
1998 doesn't correspond anymore to the real list in buffer."
1999 ;; Order of functions matters here: checkboxes and endings need
2000 ;; correct indentation to be set, and indentation needs correct
2003 ;; 0. Save a copy of structure before modifications
2004 (let ((old-struct (or old-struct
(copy-tree struct
))))
2005 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
2006 ;; order to get items endings and bullets properly
2007 (org-list-struct-fix-ind struct parents
2)
2008 ;; 2. Fix each item end to get correct prevs alist.
2009 (org-list-struct-fix-item-end struct
)
2010 ;; 3. Get bullets right.
2011 (let ((prevs (org-list-prevs-alist struct
)))
2012 (org-list-struct-fix-bul struct prevs
)
2013 ;; 4. Now get real indentation.
2014 (org-list-struct-fix-ind struct parents
)
2015 ;; 5. Eventually fix checkboxes.
2016 (org-list-struct-fix-box struct parents prevs
))
2017 ;; 6. Apply structure modifications to buffer.
2018 (org-list-struct-apply-struct struct old-struct
))
2019 ;; 7. Return the updated structure
2026 (defun org-apply-on-list (function init-value
&rest args
)
2027 "Call FUNCTION on each item of the list at point.
2028 FUNCTION must be called with at least one argument: INIT-VALUE,
2029 that will contain the value returned by the function at the
2030 previous item, plus ARGS extra arguments.
2032 FUNCTION is applied on items in reverse order.
2034 As an example, \(org-apply-on-list \(lambda \(result) \(1+ result)) 0)
2035 will return the number of items in the current list.
2037 Sublists of the list are skipped. Cursor is always at the
2038 beginning of the item."
2039 (let* ((struct (org-list-struct))
2040 (prevs (org-list-prevs-alist struct
))
2041 (item (copy-marker (point-at-bol)))
2042 (all (org-list-get-all-items (marker-position item
) struct prevs
))
2046 (setq value
(apply function value args
)))
2049 (move-marker item nil
)
2052 (defun org-list-set-item-visibility (item struct view
)
2053 "Set visibility of ITEM in STRUCT to VIEW.
2055 Possible values are: `folded', `children' or `subtree'. See
2056 `org-cycle' for more information."
2059 (let ((item-end (org-list-get-item-end-before-blank item struct
)))
2061 (org-flag-region (save-excursion (goto-char item
) (line-end-position))
2062 item-end t
'outline
)))
2063 ((eq view
'children
)
2064 ;; First show everything.
2065 (org-list-set-item-visibility item struct
'subtree
)
2066 ;; Then fold every child.
2067 (let* ((parents (org-list-parents-alist struct
))
2068 (children (org-list-get-children item struct parents
)))
2070 (org-list-set-item-visibility e struct
'folded
))
2074 (let ((item-end (org-list-get-item-end item struct
)))
2075 (org-flag-region item item-end nil
'outline
)))))
2077 (defun org-list-item-body-column (item)
2078 "Return column at which body of ITEM should start."
2084 "[ \t]::\\([ \t]\\|$\\)" (line-beginning-position) t
))
2085 ;; Descriptive list item. Body starts after item's tag, if
2087 (let ((start (1+ (- (match-beginning 1) (line-beginning-position))))
2088 (ind (current-indentation)))
2089 (if (> start
(+ ind org-list-description-max-indent
))
2092 ;; Regular item. Body starts after bullet.
2093 (looking-at "[ \t]*\\(\\S-+\\)")
2094 (+ (progn (goto-char (match-end 1)) (current-column))
2095 (if (and org-list-two-spaces-after-bullet-regexp
2096 (string-match-p org-list-two-spaces-after-bullet-regexp
2103 ;;; Interactive functions
2105 (defalias 'org-list-get-item-begin
'org-in-item-p
)
2107 (defun org-beginning-of-item ()
2108 "Go to the beginning of the current item.
2109 Throw an error when not in a list."
2111 (let ((begin (org-in-item-p)))
2112 (if begin
(goto-char begin
) (error "Not in an item"))))
2114 (defun org-beginning-of-item-list ()
2115 "Go to the beginning item of the current list or sublist.
2116 Throw an error when not in a list."
2118 (let ((begin (org-in-item-p)))
2120 (error "Not in an item")
2122 (let* ((struct (org-list-struct))
2123 (prevs (org-list-prevs-alist struct
)))
2124 (goto-char (org-list-get-list-begin begin struct prevs
))))))
2126 (defun org-end-of-item-list ()
2127 "Go to the end of the current list or sublist.
2128 Throw an error when not in a list."
2130 (let ((begin (org-in-item-p)))
2132 (error "Not in an item")
2134 (let* ((struct (org-list-struct))
2135 (prevs (org-list-prevs-alist struct
)))
2136 (goto-char (org-list-get-list-end begin struct prevs
))))))
2138 (defun org-end-of-item ()
2139 "Go to the end of the current item.
2140 Throw an error when not in a list."
2142 (let ((begin (org-in-item-p)))
2144 (error "Not in an item")
2146 (let ((struct (org-list-struct)))
2147 (goto-char (org-list-get-item-end begin struct
))))))
2149 (defun org-previous-item ()
2150 "Move to the beginning of the previous item.
2151 Throw an error when not in a list. Also throw an error when at
2152 first item, unless `org-list-use-circular-motion' is non-nil."
2154 (let ((item (org-in-item-p)))
2156 (error "Not in an item")
2158 (let* ((struct (org-list-struct))
2159 (prevs (org-list-prevs-alist struct
))
2160 (prevp (org-list-get-prev-item item struct prevs
)))
2162 (prevp (goto-char prevp
))
2163 (org-list-use-circular-motion
2164 (goto-char (org-list-get-last-item item struct prevs
)))
2165 (t (error "On first item")))))))
2167 (defun org-next-item ()
2168 "Move to the beginning of the next item.
2169 Throw an error when not in a list. Also throw an error when at
2170 last item, unless `org-list-use-circular-motion' is non-nil."
2172 (let ((item (org-in-item-p)))
2174 (error "Not in an item")
2176 (let* ((struct (org-list-struct))
2177 (prevs (org-list-prevs-alist struct
))
2178 (prevp (org-list-get-next-item item struct prevs
)))
2180 (prevp (goto-char prevp
))
2181 (org-list-use-circular-motion
2182 (goto-char (org-list-get-first-item item struct prevs
)))
2183 (t (error "On last item")))))))
2185 (defun org-move-item-down ()
2186 "Move the item at point down, i.e. swap with following item.
2187 Sub-items (items with larger indentation) are considered part of
2188 the item, so this really moves item trees."
2190 (unless (org-at-item-p) (error "Not at an item"))
2191 (let* ((col (current-column))
2192 (item (point-at-bol))
2193 (struct (org-list-struct))
2194 (prevs (org-list-prevs-alist struct
))
2195 (next-item (org-list-get-next-item (point-at-bol) struct prevs
)))
2196 (unless (or next-item org-list-use-circular-motion
)
2197 (user-error "Cannot move this item further down"))
2199 (setq struct
(org-list-send-item item
'begin struct
))
2200 (setq struct
(org-list-swap-items item next-item struct
))
2202 (org-list-get-next-item item struct
(org-list-prevs-alist struct
))))
2203 (org-list-write-struct struct
(org-list-parents-alist struct
))
2204 (org-move-to-column col
)))
2206 (defun org-move-item-up ()
2207 "Move the item at point up, i.e. swap with previous item.
2208 Sub-items (items with larger indentation) are considered part of
2209 the item, so this really moves item trees."
2211 (unless (org-at-item-p) (error "Not at an item"))
2212 (let* ((col (current-column))
2213 (item (point-at-bol))
2214 (struct (org-list-struct))
2215 (prevs (org-list-prevs-alist struct
))
2216 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs
)))
2217 (unless (or prev-item org-list-use-circular-motion
)
2218 (user-error "Cannot move this item further up"))
2220 (setq struct
(org-list-send-item item
'end struct
))
2221 (setq struct
(org-list-swap-items prev-item item struct
)))
2222 (org-list-write-struct struct
(org-list-parents-alist struct
))
2223 (org-move-to-column col
)))
2225 (defun org-insert-item (&optional checkbox
)
2226 "Insert a new item at the current level.
2227 If cursor is before first character after bullet of the item, the
2228 new item will be created before the current one.
2230 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2232 Return t when things worked, nil when we are not in an item, or
2235 (let ((itemp (org-in-item-p))
2237 ;; If cursor isn't is a list or if list is invisible, return nil.
2238 (unless (or (not itemp
)
2244 (org-at-item-timer-p))
2245 ;; Timer list: delegate to `org-timer-item'.
2246 (progn (org-timer-item) t
)
2247 (let* ((struct (save-excursion (goto-char itemp
)
2249 (prevs (org-list-prevs-alist struct
))
2250 ;; If we're in a description list, ask for the new term.
2251 (desc (when (eq (org-list-get-list-type itemp struct prevs
)
2254 (setq struct
(org-list-insert-item pos struct prevs checkbox desc
))
2255 (org-list-write-struct struct
(org-list-parents-alist struct
))
2256 (when checkbox
(org-update-checkbox-count-maybe))
2257 (looking-at org-list-full-item-re
)
2258 (goto-char (if (and (match-beginning 4)
2260 (string-match "[.)]" (match-string 1))))
2263 (when desc
(backward-char 1))
2266 (defun org-list-repair ()
2267 "Fix indentation, bullets and checkboxes in the list at point."
2269 (unless (org-at-item-p) (error "This is not a list"))
2270 (let* ((struct (org-list-struct))
2271 (parents (org-list-parents-alist struct
)))
2272 (org-list-write-struct struct parents
)))
2274 (defun org-cycle-list-bullet (&optional which
)
2275 "Cycle through the different itemize/enumerate bullets.
2276 This cycle the entire list level through the sequence:
2278 `-' -> `+' -> `*' -> `1.' -> `1)'
2280 If WHICH is a valid string, use that as the new bullet. If WHICH
2281 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2282 `previous', cycle backwards."
2284 (unless (org-at-item-p) (error "Not at an item"))
2287 (let* ((struct (org-list-struct))
2288 (parents (org-list-parents-alist struct
))
2289 (prevs (org-list-prevs-alist struct
))
2290 (list-beg (org-list-get-first-item (point) struct prevs
))
2291 (bullet (org-list-get-bullet list-beg struct
))
2292 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs
))
2293 (case-fold-search nil
)
2295 ((string-match "[a-z]\\." bullet
) "a.")
2296 ((string-match "[a-z])" bullet
) "a)")
2297 ((string-match "[A-Z]\\." bullet
) "A.")
2298 ((string-match "[A-Z])" bullet
) "A)")
2299 ((string-match "\\." bullet
) "1.")
2300 ((string-match ")" bullet
) "1)")
2301 (t (org-trim bullet
))))
2302 ;; Compute list of possible bullets, depending on context.
2305 ;; *-bullets are not allowed at column 0.
2306 (unless (looking-at "\\S-") '("*"))
2307 ;; Description items cannot be numbered.
2308 (unless (or (eq org-plain-list-ordered-item-terminator ?\
))
2309 (org-at-item-description-p))
2311 (unless (or (eq org-plain-list-ordered-item-terminator ?.
)
2312 (org-at-item-description-p))
2314 (unless (or (not alpha-p
)
2315 (eq org-plain-list-ordered-item-terminator ?\
))
2316 (org-at-item-description-p))
2318 (unless (or (not alpha-p
)
2319 (eq org-plain-list-ordered-item-terminator ?.
)
2320 (org-at-item-description-p))
2322 (len (length bullet-list
))
2323 (item-index (- len
(length (member current bullet-list
))))
2324 (get-value (lambda (index) (nth (mod index len
) bullet-list
)))
2326 ((member which bullet-list
) which
)
2327 ((numberp which
) (funcall get-value which
))
2328 ((eq 'previous which
) (funcall get-value
(1- item-index
)))
2329 (t (funcall get-value
(1+ item-index
))))))
2330 ;; Use a short variation of `org-list-write-struct' as there's
2331 ;; no need to go through all the steps.
2332 (let ((old-struct (copy-tree struct
)))
2333 (org-list-set-bullet list-beg struct
(org-list-bullet-string new
))
2334 (org-list-struct-fix-bul struct prevs
)
2335 (org-list-struct-fix-ind struct parents
)
2336 (org-list-struct-apply-struct struct old-struct
)))))
2338 (defun org-toggle-checkbox (&optional toggle-presence
)
2339 "Toggle the checkbox in the current line.
2341 With prefix argument TOGGLE-PRESENCE, add or remove checkboxes.
2342 With a double prefix argument, set the checkbox to \"[-]\".
2344 When there is an active region, toggle status or presence of the
2345 first checkbox there, and make every item inside have the same
2346 status or presence, respectively.
2348 If point is on a headline, apply this to all checkbox items in
2349 the text below the heading, taking as reference the first item in
2350 subtree, ignoring planning line and any drawer following it."
2357 (orderedp (org-entry-get nil
"ORDERED"))
2359 ;; In a region, start at first item in region.
2361 ((org-region-active-p)
2362 (let ((limit (region-end)))
2363 (goto-char (region-beginning))
2364 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2365 (setq lim-up
(point-at-bol))
2366 (error "No item in region"))
2367 (setq lim-down
(copy-marker limit
))))
2369 ;; On a heading, start at first item after drawers and
2370 ;; time-stamps (scheduled, etc.).
2371 (let ((limit (save-excursion (outline-next-heading) (point))))
2372 (org-end-of-meta-data t
)
2373 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2374 (setq lim-up
(point-at-bol))
2375 (error "No item in subtree"))
2376 (setq lim-down
(copy-marker limit
))))
2377 ;; Just one item: set SINGLEP flag.
2380 (setq lim-up
(point-at-bol)
2381 lim-down
(copy-marker (point-at-eol))))
2382 (t (error "Not at an item or heading, and no active region"))))
2383 ;; Determine the checkbox going to be applied to all items
2388 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2390 ((equal toggle-presence
'(16)) "[-]")
2391 ((equal toggle-presence
'(4))
2392 (unless cbox
"[ ]"))
2393 ((equal "[X]" cbox
) "[ ]")
2395 ;; When an item is found within bounds, grab the full list at
2396 ;; point structure, then: (1) set check-box of all its items
2397 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2398 ;; whole list, (3) move point after the list.
2400 (while (and (< (point) lim-down
)
2401 (org-list-search-forward (org-item-beginning-re)
2403 (let* ((struct (org-list-struct))
2404 (struct-copy (copy-tree struct
))
2405 (parents (org-list-parents-alist struct
))
2406 (prevs (org-list-prevs-alist struct
))
2407 (bottom (copy-marker (org-list-get-bottom-point struct
)))
2408 (items-to-toggle (cl-remove-if
2409 (lambda (e) (or (< e lim-up
) (> e lim-down
)))
2410 (mapcar #'car struct
))))
2411 (mapc (lambda (e) (org-list-set-checkbox
2413 ;; If there is no box at item, leave as-is
2414 ;; unless function was called with C-u prefix.
2415 (let ((cur-box (org-list-get-checkbox e struct
)))
2416 (if (or cur-box
(equal toggle-presence
'(4)))
2420 (setq block-item
(org-list-struct-fix-box
2421 struct parents prevs orderedp
))
2422 ;; Report some problems due to ORDERED status of subtree.
2423 ;; If only one box was being checked, throw an error, else,
2424 ;; only signal problems.
2426 ((and singlep block-item
(> lim-up block-item
))
2428 "Checkbox blocked because of unchecked box at line %d"
2429 (org-current-line block-item
)))
2432 "Checkboxes were removed due to unchecked box at line %d"
2433 (org-current-line block-item
))))
2435 (move-marker bottom nil
)
2436 (org-list-struct-apply-struct struct struct-copy
)))
2437 (move-marker lim-down nil
)))
2438 (org-update-checkbox-count-maybe))
2440 (defun org-reset-checkbox-state-subtree ()
2441 "Reset all checkboxes in an entry subtree."
2443 (if (org-before-first-heading-p)
2444 (error "Not inside a tree")
2447 (org-narrow-to-subtree)
2449 (goto-char (point-min))
2450 (let ((end (point-max)))
2451 (while (< (point) end
)
2452 (when (org-at-item-checkbox-p)
2453 (replace-match "[ ]" t t nil
1))
2454 (beginning-of-line 2)))
2455 (org-update-checkbox-count-maybe 'all
)))))
2457 (defun org-update-checkbox-count (&optional all
)
2458 "Update the checkbox statistics in the current section.
2460 This will find all statistic cookies like [57%] and [6/12] and
2461 update them with the current numbers.
2463 With optional prefix argument ALL, do this for the whole buffer."
2465 (org-with-wide-buffer
2466 (let* ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2467 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\
2468 \\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2470 (or (not org-checkbox-hierarchical-statistics
)
2471 (string-match "\\<recursive\\>"
2472 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2473 (within-inlinetask (and (not all
)
2474 (featurep 'org-inlinetask
)
2475 (org-inlinetask-in-task-p)))
2476 (end (cond (all (point-max))
2478 (save-excursion (outline-next-heading) (point)))
2480 (org-with-limited-levels (outline-next-heading))
2483 (lambda (item structs recursivep
)
2484 ;; Return number of checked boxes and boxes of all types
2485 ;; in all structures in STRUCTS. If RECURSIVEP is
2486 ;; non-nil, also count boxes in sub-lists. If ITEM is
2487 ;; nil, count across the whole structure, else count only
2488 ;; across subtree whose ancestor is ITEM.
2489 (let ((c-on 0) (c-all 0))
2490 (dolist (s structs
(list c-on c-all
))
2491 (let* ((pre (org-list-prevs-alist s
))
2492 (par (org-list-parents-alist s
))
2495 ((and recursivep item
) (org-list-get-subtree item s
))
2496 (recursivep (mapcar #'car s
))
2497 (item (org-list-get-children item s par
))
2498 (t (org-list-get-all-items
2499 (org-list-get-top-point s
) s pre
))))
2500 (cookies (delq nil
(mapcar
2502 (org-list-get-checkbox e s
))
2504 (cl-incf c-all
(length cookies
))
2505 (cl-incf c-on
(cl-count "[X]" cookies
:test
#'equal
)))))))
2508 (cond (all (goto-char (point-min)))
2509 (within-inlinetask (org-back-to-heading t
))
2510 (t (org-with-limited-levels (outline-previous-heading))))
2511 ;; Build an alist for each cookie found. The key is the position
2512 ;; at beginning of cookie and values ending position, format of
2513 ;; cookie, number of checked boxes to report and total number of
2515 (while (re-search-forward cookie-re end t
)
2516 (let ((context (save-excursion (backward-char)
2517 (save-match-data (org-element-context)))))
2518 (when (eq (org-element-type context
) 'statistics-cookie
)
2521 (list (match-beginning 1) (match-end 1) (match-end 2))
2523 (org-element-lineage
2525 '(drawer center-block dynamic-block inlinetask item
2526 quote-block special-block verse-block
)))
2528 (org-element-property :contents-begin container
)
2530 (org-with-limited-levels
2531 (outline-previous-heading))
2533 (or (cdr (assq beg cache
))
2538 (org-element-property :contents-end container
)
2540 (org-with-limited-levels (outline-next-heading))
2543 (while (re-search-forward box-re end t
)
2544 (let ((element (org-element-at-point)))
2545 (when (eq (org-element-type element
) 'item
)
2546 (push (org-element-property :structure element
)
2548 ;; Skip whole list since we have its
2549 ;; structure anyway.
2550 (while (setq element
(org-element-lineage
2551 element
'(plain-list)))
2553 (min (org-element-property :end element
)
2555 ;; Cache count for cookies applying to the same
2556 ;; area. Then return it.
2558 (funcall count-boxes
2559 (and (eq (org-element-type container
)
2561 (org-element-property
2565 (push (cons beg count
) cache
)
2568 ;; Apply alist to buffer.
2569 (dolist (cookie cookies-list
)
2570 (let* ((beg (car cookie
))
2571 (end (nth 1 cookie
))
2572 (percent (nth 2 cookie
))
2573 (checked (nth 3 cookie
))
2574 (total (nth 4 cookie
)))
2577 (if percent
(format "[%d%%]" (floor (* 100.0 checked
)
2579 (format "[%d/%d]" checked total
)))
2580 (delete-region (point) (+ (point) (- end beg
)))
2581 (when org-auto-align-tags
(org-fix-tags-on-the-fly)))))))
2583 (defun org-get-checkbox-statistics-face ()
2584 "Select the face for checkbox statistics.
2585 The face will be `org-done' when all relevant boxes are checked.
2586 Otherwise it will be `org-todo'."
2588 (if (equal (match-string 1) "100%")
2589 'org-checkbox-statistics-done
2590 'org-checkbox-statistics-todo
)
2591 (if (and (> (match-end 2) (match-beginning 2))
2592 (equal (match-string 2) (match-string 3)))
2593 'org-checkbox-statistics-done
2594 'org-checkbox-statistics-todo
)))
2596 (defun org-update-checkbox-count-maybe (&optional all
)
2597 "Update checkbox statistics unless turned off by user.
2598 With an optional argument ALL, update them in the whole buffer."
2599 (when (cdr (assq 'checkbox org-list-automatic-rules
))
2600 (org-update-checkbox-count all
))
2601 (run-hooks 'org-checkbox-statistics-hook
))
2603 (defvar org-last-indent-begin-marker
(make-marker))
2604 (defvar org-last-indent-end-marker
(make-marker))
2605 (defun org-list-indent-item-generic (arg no-subtree struct
)
2606 "Indent a local list item including its children.
2607 When number ARG is a negative, item will be outdented, otherwise
2608 it will be indented.
2610 If a region is active, all items inside will be moved.
2612 If NO-SUBTREE is non-nil, only indent the item itself, not its
2615 STRUCT is the list structure.
2617 Return t if successful."
2619 (let* ((regionp (org-region-active-p))
2620 (rbeg (and regionp
(region-beginning)))
2621 (rend (and regionp
(region-end)))
2622 (top (org-list-get-top-point struct
))
2623 (parents (org-list-parents-alist struct
))
2624 (prevs (org-list-prevs-alist struct
))
2625 ;; Are we going to move the whole list?
2628 (= top
(point-at-bol))
2629 (cdr (assq 'indent org-list-automatic-rules
))
2632 "At first item: use S-M-<left/right> to move the whole list")
2634 ;; Determine begin and end points of zone to indent. If moving
2635 ;; more than one item, save them for subsequent moves.
2636 (unless (and (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
2637 (memq this-command
'(org-shiftmetaright org-shiftmetaleft
)))
2640 (set-marker org-last-indent-begin-marker rbeg
)
2641 (set-marker org-last-indent-end-marker rend
))
2642 (set-marker org-last-indent-begin-marker
(point-at-bol))
2643 (set-marker org-last-indent-end-marker
2645 (specialp (org-list-get-bottom-point struct
))
2646 (no-subtree (1+ (point-at-bol)))
2647 (t (org-list-get-item-end (point-at-bol) struct
))))))
2648 (let* ((beg (marker-position org-last-indent-begin-marker
))
2649 (end (marker-position org-last-indent-end-marker
)))
2651 ;; Special case: moving top-item with indent rule.
2653 (let* ((level-skip (org-level-increment))
2654 (offset (if (< arg
0) (- level-skip
) level-skip
))
2655 (top-ind (org-list-get-ind beg struct
))
2656 (old-struct (copy-tree struct
)))
2657 (if (< (+ top-ind offset
) 0)
2658 (error "Cannot outdent beyond margin")
2659 ;; Change bullet if necessary.
2660 (when (and (= (+ top-ind offset
) 0)
2662 (org-list-get-bullet beg struct
)))
2663 (org-list-set-bullet beg struct
2664 (org-list-bullet-string "-")))
2665 ;; Shift every item by OFFSET and fix bullets. Then
2666 ;; apply changes to buffer.
2668 (let ((ind (org-list-get-ind (car e
) struct
)))
2669 (org-list-set-ind (car e
) struct
(+ ind offset
))))
2671 (org-list-struct-fix-bul struct prevs
)
2672 (org-list-struct-apply-struct struct old-struct
))))
2675 ;; If only one item is moved, it mustn't have a child.
2678 (org-list-has-child-p beg struct
))
2679 ;; If a subtree or region is moved, the last item
2680 ;; of the subtree mustn't have a child.
2681 (let ((last-item (caar
2684 (lambda (e) (>= (car e
) end
))
2686 (org-list-has-child-p last-item struct
))))
2687 (error "Cannot outdent an item without its children"))
2692 (org-list-struct-outdent beg end struct parents
)
2693 (org-list-struct-indent beg end struct parents prevs
))))
2694 (org-list-write-struct struct new-parents
))
2695 (org-update-checkbox-count-maybe))))))
2698 (defun org-outdent-item ()
2699 "Outdent a local list item, but not its children.
2700 If a region is active, all items inside will be moved."
2702 (let ((regionp (org-region-active-p)))
2704 ((or (org-at-item-p)
2706 (save-excursion (goto-char (region-beginning))
2708 (let ((struct (if (not regionp
) (org-list-struct)
2709 (save-excursion (goto-char (region-beginning))
2710 (org-list-struct)))))
2711 (org-list-indent-item-generic -
1 t struct
)))
2712 (regionp (error "Region not starting at an item"))
2713 (t (error "Not at an item")))))
2715 (defun org-indent-item ()
2716 "Indent a local list item, but not its children.
2717 If a region is active, all items inside will be moved."
2719 (let ((regionp (org-region-active-p)))
2721 ((or (org-at-item-p)
2723 (save-excursion (goto-char (region-beginning))
2725 (let ((struct (if (not regionp
) (org-list-struct)
2726 (save-excursion (goto-char (region-beginning))
2727 (org-list-struct)))))
2728 (org-list-indent-item-generic 1 t struct
)))
2729 (regionp (error "Region not starting at an item"))
2730 (t (error "Not at an item")))))
2732 (defun org-outdent-item-tree ()
2733 "Outdent a local list item including its children.
2734 If a region is active, all items inside will be moved."
2736 (let ((regionp (org-region-active-p)))
2738 ((or (org-at-item-p)
2740 (save-excursion (goto-char (region-beginning))
2742 (let ((struct (if (not regionp
) (org-list-struct)
2743 (save-excursion (goto-char (region-beginning))
2744 (org-list-struct)))))
2745 (org-list-indent-item-generic -
1 nil struct
)))
2746 (regionp (error "Region not starting at an item"))
2747 (t (error "Not at an item")))))
2749 (defun org-indent-item-tree ()
2750 "Indent a local list item including its children.
2751 If a region is active, all items inside will be moved."
2753 (let ((regionp (org-region-active-p)))
2755 ((or (org-at-item-p)
2757 (save-excursion (goto-char (region-beginning))
2759 (let ((struct (if (not regionp
) (org-list-struct)
2760 (save-excursion (goto-char (region-beginning))
2761 (org-list-struct)))))
2762 (org-list-indent-item-generic 1 nil struct
)))
2763 (regionp (error "Region not starting at an item"))
2764 (t (error "Not at an item")))))
2766 (defvar org-tab-ind-state
)
2767 (defvar org-adapt-indentation
)
2768 (defun org-cycle-item-indentation ()
2769 "Cycle levels of indentation of an empty item.
2770 The first run indents the item, if applicable. Subsequent runs
2771 outdent it at meaningful levels in the list. When done, item is
2772 put back at its original position with its original bullet.
2774 Return t at each successful move."
2775 (when (org-at-item-p)
2776 (let* ((org-adapt-indentation nil
)
2777 (struct (org-list-struct))
2778 (ind (org-list-get-ind (point-at-bol) struct
))
2779 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2780 ;; Accept empty items or if cycle has already started.
2781 (when (or (eq last-command
'org-cycle-item-indentation
)
2782 (and (save-excursion
2784 (looking-at org-list-full-item-re
))
2785 (>= (match-end 0) (save-excursion
2786 (goto-char (org-list-get-item-end
2787 (point-at-bol) struct
))
2788 (skip-chars-backward " \r\t\n")
2790 (setq this-command
'org-cycle-item-indentation
)
2791 ;; When in the middle of the cycle, try to outdent first. If
2792 ;; it fails, and point is still at initial position, indent.
2793 ;; Else, re-create it at its original position.
2794 (if (eq last-command
'org-cycle-item-indentation
)
2796 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2797 ((and (= ind
(car org-tab-ind-state
))
2798 (ignore-errors (org-list-indent-item-generic 1 t struct
))))
2799 (t (delete-region (point-at-bol) (point-at-eol))
2800 (indent-to-column (car org-tab-ind-state
))
2801 (insert (cdr org-tab-ind-state
) " ")
2803 (setq this-command
'identity
)))
2804 ;; If a cycle is starting, remember indentation and bullet,
2805 ;; then try to indent. If it fails, try to outdent.
2806 (setq org-tab-ind-state
(cons ind bullet
))
2808 ((ignore-errors (org-list-indent-item-generic 1 t struct
)))
2809 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2810 (t (user-error "Cannot move item"))))
2813 (defun org-sort-list
2814 (&optional with-case sorting-type getkey-func compare-func interactive?
)
2816 The cursor may be at any item of the list that should be sorted.
2817 Sublists are not sorted. Checkboxes, if any, are ignored.
2819 Sorting can be alphabetically, numerically, by date/time as given
2820 by a time stamp, by a property or by priority.
2822 Comparing entries ignores case by default. However, with an
2823 optional argument WITH-CASE, the sorting considers case as well,
2824 if the current locale allows for it.
2826 The command prompts for the sorting type unless it has been given
2827 to the function through the SORTING-TYPE argument, which needs to
2828 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F ?x ?X). Here is the
2829 detailed meaning of each character:
2831 n Numerically, by converting the beginning of the item to a number.
2832 a Alphabetically. Only the first line of item is checked.
2833 t By date/time, either the first active time stamp in the entry, if
2834 any, or by the first inactive one. In a timer list, sort the timers.
2835 x By \"checked\" status of a check list.
2837 Capital letters will reverse the sort order.
2839 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2840 a function to be called with point at the beginning of the
2841 record. It must return a value that is compatible with COMPARE-FUNC,
2842 the function used to compare entries.
2844 Sorting is done against the visible part of the headlines, it
2845 ignores hidden links.
2847 A non-nil value for INTERACTIVE? is used to signal that this
2848 function is being called interactively."
2849 (interactive (list current-prefix-arg nil nil nil t
))
2850 (let* ((case-func (if with-case
'identity
'downcase
))
2851 (struct (org-list-struct))
2852 (prevs (org-list-prevs-alist struct
))
2853 (start (org-list-get-list-begin (point-at-bol) struct prevs
))
2854 (end (org-list-get-list-end (point-at-bol) struct prevs
))
2859 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc [x]checked A/N/T/F/X means reversed:")
2860 (read-char-exclusive))))
2861 (dcst (downcase sorting-type
))
2866 (org-read-function "Function for extracting keys: "))
2867 (error "Missing key extractor"))))
2870 ((= dcst ?a
) #'org-string-collate-lessp
)
2875 (concat "Function for comparing keys "
2876 "(empty for default `sort-subr' predicate): ")
2879 ((= dcst ?x
) #'string
<))))
2880 (message "Sorting items...")
2882 (narrow-to-region start end
)
2883 (goto-char (point-min))
2884 (let* ((case-fold-search nil
)
2885 (now (current-time))
2886 (next-record (lambda ()
2887 (skip-chars-forward " \r\t\n")
2888 (or (eobp) (beginning-of-line))))
2889 (end-record (lambda ()
2890 (goto-char (org-list-get-item-end-before-blank
2894 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2898 (org-sort-remove-invisible
2899 (buffer-substring (match-end 0) (point-at-eol)))))
2902 (org-sort-remove-invisible
2904 (match-end 0) (point-at-eol)))))
2907 ;; If it is a timer list, convert timer to seconds
2908 ((org-at-item-timer-p)
2909 (org-timer-hms-to-secs (match-string 1)))
2910 ((or (save-excursion
2911 (re-search-forward org-ts-regexp
(point-at-eol) t
))
2912 (save-excursion (re-search-forward org-ts-regexp-both
2914 (org-time-string-to-seconds (match-string 0)))
2915 (t (float-time now
))))
2916 ((= dcst ?x
) (or (and (stringp (match-string 1))
2921 (let ((value (funcall getkey-func
)))
2923 (funcall case-func value
)
2925 (error "Invalid key function `%s'" getkey-func
)))
2926 (t (error "Invalid sorting type `%c'" sorting-type
)))))))
2927 (sort-subr (/= dcst sorting-type
)
2933 ;; Read and fix list again, as `sort-subr' probably destroyed
2936 (run-hooks 'org-after-sorting-entries-or-items-hook
)
2937 (message "Sorting items...done")))))
2939 (defun org-toggle-item (arg)
2940 "Convert headings or normal lines to items, items to normal lines.
2941 If there is no active region, only the current line is considered.
2943 If the first non blank line in the region is a headline, convert
2944 all headlines to items, shifting text accordingly.
2946 If it is an item, convert all items to normal lines.
2948 If it is normal text, change region into a list of items.
2949 With a prefix argument ARG, change the region in a single item."
2953 ;; Shift text in current section to IND, from point to END.
2954 ;; The function leaves point to END line.
2955 (let ((min-i 1000) (end (copy-marker end
)))
2956 ;; First determine the minimum indentation (MIN-I) of
2960 (while (< (point) end
)
2961 (let ((i (current-indentation)))
2963 ;; Skip blank lines and inline tasks.
2964 ((looking-at "^[ \t]*$"))
2965 ((looking-at org-outline-regexp-bol
))
2966 ;; We can't find less than 0 indentation.
2967 ((zerop i
) (throw 'exit
(setq min-i
0)))
2968 ((< i min-i
) (setq min-i i
))))
2970 ;; Then indent each line so that a line indented to
2971 ;; MIN-I becomes indented to IND. Ignore blank lines
2972 ;; and inline tasks in the process.
2973 (let ((delta (- ind min-i
)))
2974 (while (< (point) end
)
2975 (unless (or (looking-at "^[ \t]*$")
2976 (looking-at org-outline-regexp-bol
))
2977 (indent-line-to (+ (current-indentation) delta
)))
2981 ;; Return beginning of first non-blank line, starting from
2985 (skip-chars-forward " \r\t\n")
2988 ;; Determine boundaries of changes.
2989 (if (org-region-active-p)
2990 (setq beg
(funcall skip-blanks
(region-beginning))
2991 end
(copy-marker (region-end)))
2992 (setq beg
(funcall skip-blanks
(point-at-bol))
2993 end
(copy-marker (point-at-eol))))
2994 ;; Depending on the starting line, choose an action on the text
2995 ;; between BEG and END.
2996 (org-with-limited-levels
3000 ;; Case 1. Start at an item: de-itemize. Note that it only
3001 ;; happens when a region is active: `org-ctrl-c-minus'
3002 ;; would call `org-cycle-list-bullet' otherwise.
3004 (while (< (point) end
)
3005 (when (org-at-item-p)
3006 (skip-chars-forward " \t")
3007 (delete-region (point) (match-end 0)))
3009 ;; Case 2. Start at an heading: convert to items.
3012 (let (org-loop-over-headlines-in-active-region)
3013 (org-list--delete-metadata))
3014 (let* ((bul (org-list-bullet-string "-"))
3015 (bul-len (length bul
))
3016 ;; Indentation of the first heading. It should be
3017 ;; relative to the indentation of its parent, if any.
3018 (start-ind (save-excursion
3020 ((not org-adapt-indentation
) 0)
3021 ((not (outline-previous-heading)) 0)
3022 (t (length (match-string 0))))))
3023 ;; Level of first heading. Further headings will be
3024 ;; compared to it to determine hierarchy in the list.
3025 (ref-level (org-reduced-level (org-outline-level))))
3026 (while (< (point) end
)
3027 (let* ((level (org-reduced-level (org-outline-level)))
3028 (delta (max 0 (- level ref-level
)))
3029 (todo-state (org-get-todo-state)))
3030 ;; If current headline is less indented than the first
3031 ;; one, set it as reference, in order to preserve
3033 (when (< level ref-level
) (setq ref-level level
))
3035 (let (org-loop-over-headlines-in-active-region)
3036 (org-list--delete-metadata))
3037 ;; Remove stars and TODO keyword.
3038 (let ((case-fold-search nil
)) (looking-at org-todo-line-regexp
))
3039 (delete-region (point) (or (match-beginning 3)
3040 (line-end-position)))
3042 (indent-line-to (+ start-ind
(* delta bul-len
)))
3043 ;; Turn TODO keyword into a check box.
3045 (let* ((struct (org-list-struct))
3046 (old (copy-tree struct
)))
3047 (org-list-set-checkbox
3048 (line-beginning-position)
3050 (if (member todo-state org-done-keywords
)
3053 (org-list-write-struct struct
3054 (org-list-parents-alist struct
)
3056 ;; Ensure all text down to END (or SECTION-END) belongs
3057 ;; to the newly created item.
3058 (let ((section-end (save-excursion
3059 (or (outline-next-heading) (point)))))
3062 (+ start-ind
(* (1+ delta
) bul-len
))
3063 (min end section-end
)))))))
3064 ;; Case 3. Normal line with ARG: make the first line of region
3065 ;; an item, and shift indentation of others lines to
3066 ;; set them as item's body.
3067 (arg (let* ((bul (org-list-bullet-string "-"))
3068 (bul-len (length bul
))
3069 (ref-ind (current-indentation)))
3070 (skip-chars-forward " \t")
3073 (while (< (point) end
)
3074 ;; Ensure that lines less indented than first one
3075 ;; still get included in item body.
3078 (min end
(save-excursion (or (outline-next-heading)
3081 ;; Case 4. Normal line without ARG: turn each non-item line
3084 (while (< (point) end
)
3085 (unless (or (org-at-heading-p) (org-at-item-p))
3086 (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
3088 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
3089 (forward-line))))))))
3092 ;;; Send and receive lists
3094 (defun org-list-to-lisp (&optional delete
)
3095 "Parse the list at point and maybe DELETE it.
3097 Return a list whose car is a symbol of list type, among
3098 `ordered', `unordered' and `descriptive'. Then, each item is
3099 a list of strings and other sub-lists.
3101 For example, the following list:
3106 more text in first item
3115 (\"[X] sub-item two\"))
3116 \"more text in first item\")
3117 (\"[@3] last item\"))
3119 Point is left at list's end."
3120 (letrec ((struct (org-list-struct))
3121 (prevs (org-list-prevs-alist struct
))
3122 (parents (org-list-parents-alist struct
))
3123 (top (org-list-get-top-point struct
))
3124 (bottom (org-list-get-bottom-point struct
))
3127 ;; Remove indentation and final newline from TEXT.
3128 (org-remove-indentation
3129 (if (string-match-p "\n\\'" text
)
3130 (substring text
0 -
1)
3134 ;; Return a list whose car is list type and cdr a list
3136 (cons (org-list-get-list-type (car e
) struct prevs
)
3137 (mapcar parse-item e
))))
3140 ;; Return a list containing counter of item, if any,
3141 ;; text and any sublist inside it.
3142 (let* ((end (org-list-get-item-end e struct
))
3143 (children (org-list-get-children e struct parents
))
3147 (looking-at "[ \t]*\\S-+[ \t]*")
3152 (make-string (string-width (match-string 0)) ?\s
)
3153 (buffer-substring-no-properties
3154 (match-end 0) (or (car children
) end
))))))))
3156 (let* ((child (car children
))
3157 (sub (org-list-get-all-items child struct prevs
))
3158 (last-in-sub (car (last sub
))))
3159 (push (funcall parse-sublist sub
) body
)
3160 ;; Remove whole sub-list from children.
3161 (setq children
(cdr (memq last-in-sub children
)))
3162 ;; There is a chunk of text belonging to the item
3163 ;; if last child doesn't end where next child
3164 ;; starts or where item ends.
3165 (let ((sub-end (org-list-get-item-end last-in-sub struct
))
3166 (next (or (car children
) end
)))
3167 (when (/= sub-end next
)
3170 (buffer-substring-no-properties sub-end next
))
3173 ;; Store output, take care of cursor position and deletion of
3174 ;; list, then return output.
3175 (prog1 (funcall parse-sublist
(org-list-get-all-items top struct prevs
))
3178 (delete-region top bottom
)
3179 (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re
))
3180 (replace-match ""))))))
3182 (defun org-list-make-subtree ()
3183 "Convert the plain list at point into a subtree."
3185 (if (not (ignore-errors (goto-char (org-in-item-p))))
3186 (error "Not in a list")
3187 (let ((list (save-excursion (org-list-to-lisp t
))))
3188 (insert (org-list-to-subtree list
)))))
3190 (defun org-list-to-generic (list params
)
3191 "Convert a LIST parsed through `org-list-to-lisp' to a custom format.
3193 LIST is a list as returned by `org-list-to-lisp', which see.
3194 PARAMS is a property list of parameters used to tweak the output
3197 Valid parameters are:
3201 Export back-end used as a basis to transcode elements of the
3202 list, when no specific parameter applies to it. It is also
3203 used to translate its contents. You can prevent this by
3204 setting :raw property to a non-nil value.
3208 When non-nil, only export the contents of the top most plain
3209 list, effectively ignoring its opening and closing lines.
3213 Strings to start and end an unordered list. They can also be
3214 set to a function returning a string or nil, which will be
3215 called with the depth of the list, counting from 1.
3219 Strings to start and end an ordered list. They can also be set
3220 to a function returning a string or nil, which will be called
3221 with the depth of the list, counting from 1.
3225 Strings to start and end a descriptive list. They can also be
3226 set to a function returning a string or nil, which will be
3227 called with the depth of the list, counting from 1.
3229 :dtstart, :dtend, :ddstart, :ddend
3231 Strings to start and end a descriptive term.
3235 Strings to start or end a list item, and to start a list item
3236 with a counter. They can also be set to a function returning
3237 a string or nil, which will be called with two arguments: the
3238 type of list and the depth of the item, counting from 1.
3242 Strings to start a list item with a counter. It can also be
3243 set to a function returning a string or nil, which will be
3244 called with three arguments: the type of list, the depth of the
3245 item, counting from 1, and the counter. Its value, when
3246 non-nil, has precedence over `:istart'.
3250 String used to separate items. It can also be set to
3251 a function returning a string or nil, which will be called with
3252 two arguments: the type of list and the depth of the item,
3253 counting from 1. It always start on a new line.
3257 Function to be applied to the contents of every item. It is
3258 called with two arguments: the type of list and the contents.
3260 :cbon, :cboff, :cbtrans
3262 String to insert, respectively, an un-checked check-box,
3263 a checked check-box and a check-box in transitional state."
3265 (let* ((backend (plist-get params
:backend
))
3267 (org-export-create-backend
3268 :parent
(or backend
'org
)
3270 `((plain-list .
,(org-list--to-generic-plain-list params
))
3271 (item .
,(org-list--to-generic-item params
))
3272 (macro .
(lambda (m c i
) (org-element-macro-interpreter m nil
))))))
3274 ;; Write LIST back into Org syntax and parse it.
3276 (let ((org-inhibit-startup t
)) (org-mode))
3277 (letrec ((insert-list
3280 (funcall insert-item i
(car l
)))))
3283 (let ((start (point)))
3284 (insert (if (eq type
'ordered
) "1. " "- "))
3286 (if (consp e
) (funcall insert-list e
)
3291 (let ((ind (if (eq type
'ordered
) 3 2)))
3292 (while (> (point) start
)
3293 (unless (looking-at-p "[ \t]*$")
3295 (forward-line -
1))))))))
3296 (funcall insert-list list
))
3298 (org-element-map (org-element-parse-buffer) 'plain-list
3300 (setf info
(org-export-get-environment backend nil params
)))
3301 (when (and backend
(symbolp backend
) (not (org-export-get-backend backend
)))
3302 (user-error "Unknown :backend value"))
3303 (unless backend
(require 'ox-org
))
3304 ;; When`:raw' property has a non-nil value, turn all objects back
3306 (when (and backend
(plist-get params
:raw
))
3307 (org-element-map data org-element-all-objects
3309 (org-element-set-element
3310 object
(org-element-interpret-data object
)))))
3311 ;; We use a low-level mechanism to export DATA so as to skip all
3312 ;; usual pre-processing and post-processing, i.e., hooks, filters,
3313 ;; Babel code evaluation, include keywords and macro expansion,
3315 (let ((output (org-export-data-with-backend data custom-backend info
)))
3316 ;; Remove final newline.
3317 (if (org-string-nw-p output
) (substring-no-properties output
0 -
1) ""))))
3319 (defun org-list--depth (element)
3320 "Return the level of ELEMENT within current plain list.
3321 ELEMENT is either an item or a plain list."
3322 (cl-count-if (lambda (ancestor) (eq (org-element-type ancestor
) 'plain-list
))
3323 (org-element-lineage element nil t
)))
3325 (defun org-list--trailing-newlines (string)
3326 "Return the number of trailing newlines in STRING."
3329 (skip-chars-backward " \t\n")
3330 (count-lines (line-beginning-position 2) (point-max))))
3332 (defun org-list--generic-eval (value &rest args
)
3333 "Evaluate VALUE according to its type.
3334 VALUE is either nil, a string or a function. In the latter case,
3335 it is called with arguments ARGS."
3336 (cond ((null value
) nil
)
3337 ((stringp value
) value
)
3338 ((functionp value
) (apply value args
))
3339 (t (error "Wrong value: %s" value
))))
3341 (defun org-list--to-generic-plain-list (params)
3342 "Return a transcoder for `plain-list' elements.
3343 PARAMS is a plist used to tweak the behavior of the transcoder."
3344 (let ((ustart (plist-get params
:ustart
))
3345 (uend (plist-get params
:uend
))
3346 (ostart (plist-get params
:ostart
))
3347 (oend (plist-get params
:oend
))
3348 (dstart (plist-get params
:dstart
))
3349 (dend (plist-get params
:dend
))
3350 (splice (plist-get params
:splice
))
3351 (backend (plist-get params
:backend
)))
3352 (lambda (plain-list contents info
)
3353 (let* ((type (org-element-property :type plain-list
))
3354 (depth (org-list--depth plain-list
))
3355 (start (and (not splice
)
3356 (org-list--generic-eval
3362 (end (and (not splice
)
3363 (org-list--generic-eval
3369 ;; Make sure trailing newlines in END appear in the output by
3370 ;; setting `:post-blank' property to their number.
3372 (org-element-put-property
3373 plain-list
:post-blank
(org-list--trailing-newlines end
)))
3375 (concat (and start
(concat start
"\n"))
3376 (if (or start end splice
(not backend
))
3378 (org-export-with-backend backend plain-list contents info
))
3381 (defun org-list--to-generic-item (params)
3382 "Return a transcoder for `item' elements.
3383 PARAMS is a plist used to tweak the behavior of the transcoder."
3384 (let ((backend (plist-get params
:backend
))
3385 (istart (plist-get params
:istart
))
3386 (iend (plist-get params
:iend
))
3387 (isep (plist-get params
:isep
))
3388 (icount (plist-get params
:icount
))
3389 (ifmt (plist-get params
:ifmt
))
3390 (cboff (plist-get params
:cboff
))
3391 (cbon (plist-get params
:cbon
))
3392 (cbtrans (plist-get params
:cbtrans
))
3393 (dtstart (plist-get params
:dtstart
))
3394 (dtend (plist-get params
:dtend
))
3395 (ddstart (plist-get params
:ddstart
))
3396 (ddend (plist-get params
:ddend
)))
3397 (lambda (item contents info
)
3399 (org-element-property :type
(org-element-property :parent item
)))
3400 (tag (org-element-property :tag item
))
3401 (depth (org-list--depth item
))
3402 (separator (and (org-export-get-next-element item info
)
3403 (org-list--generic-eval isep type depth
)))
3404 (closing (pcase (org-list--generic-eval iend type depth
)
3406 ((and (guard separator
) s
)
3407 (if (equal (substring s -
1) "\n") s
(concat s
"\n")))
3409 ;; When a closing line or a separator is provided, make sure
3410 ;; its trailing newlines are taken into account when building
3411 ;; output. This is done by setting `:post-blank' property to
3412 ;; the number of such lines in the last line to be added.
3413 (let ((last-string (or separator closing
)))
3415 (org-element-put-property
3418 (max (1- (org-list--trailing-newlines last-string
)) 0))))
3421 (let ((c (org-element-property :counter item
)))
3422 (if (and c icount
) (org-list--generic-eval icount type depth c
)
3423 (org-list--generic-eval istart type depth
)))
3425 (if (or istart iend icount ifmt cbon cboff cbtrans
(not backend
)
3426 (and (eq type
'descriptive
)
3427 (or dtstart dtend ddstart ddend
)))
3429 (pcase (org-element-property :checkbox item
)
3436 (org-export-data-with-backend
3438 (org-element-interpret-data tag
))
3442 (if (= (length contents
) 0) ""
3443 (substring contents
0 -
1))))
3444 (if ifmt
(org-list--generic-eval ifmt type contents
)
3447 (org-export-with-backend backend item contents info
))))
3448 ;; Remove final newline.
3449 (if (equal body
"") ""
3450 (substring (org-element-normalize-string body
) 0 -
1)))
3454 (defun org-list-to-latex (list &optional params
)
3455 "Convert LIST into a LaTeX list.
3456 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3457 PARAMS is a property list with overruling parameters for
3458 `org-list-to-generic'. Return converted list as a string."
3460 (org-list-to-generic list
(org-combine-plists '(:backend latex
) params
)))
3462 (defun org-list-to-html (list &optional params
)
3463 "Convert LIST into a HTML list.
3464 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3465 PARAMS is a property list with overruling parameters for
3466 `org-list-to-generic'. Return converted list as a string."
3468 (org-list-to-generic list
(org-combine-plists '(:backend html
) params
)))
3470 (defun org-list-to-texinfo (list &optional params
)
3471 "Convert LIST into a Texinfo list.
3472 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3473 PARAMS is a property list with overruling parameters for
3474 `org-list-to-generic'. Return converted list as a string."
3475 (require 'ox-texinfo
)
3476 (org-list-to-generic list
(org-combine-plists '(:backend texinfo
) params
)))
3478 (defun org-list-to-org (list &optional params
)
3479 "Convert LIST into an Org plain list.
3480 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3481 with overruling parameters for `org-list-to-generic'."
3483 (lambda (type _depth
&optional c
)
3484 (concat (if (eq type
'ordered
) "1. " "- ")
3485 (and c
(format "[@%d] " c
)))))
3487 (list :istart make-item
3489 :ifmt
(lambda (_type contents
)
3490 (replace-regexp-in-string "\n" "\n " contents
))
3495 (org-list-to-generic list
(org-combine-plists defaults params
))))
3497 (defun org-list-to-subtree (list &optional params
)
3498 "Convert LIST into an Org subtree.
3499 LIST is as returned by `org-list-to-lisp'. PARAMS is a property
3500 list with overruling parameters for `org-list-to-generic'."
3501 (let* ((blank (pcase (cdr (assq 'heading org-blank-before-new-entry
))
3503 (`auto
(save-excursion
3504 (org-with-limited-levels (outline-previous-heading))
3505 (org-previous-line-empty-p)))))
3506 (level (org-reduced-level (or (org-current-level) 0)))
3508 (lambda (_type depth
&optional _count
)
3509 ;; Return the string for the heading, depending on DEPTH
3510 ;; of current sub-list.
3511 (let ((oddeven-level (+ level depth
)))
3512 (concat (make-string (if org-odd-levels-only
3513 (1- (* 2 oddeven-level
))
3517 (org-list-to-generic
3523 :dtstart
" " :dtend
" "
3524 :isep
(if blank
"\n\n" "\n")
3525 :cbon
"DONE " :cboff
"TODO " :cbtrans
"TODO ")
3530 ;;; org-list.el ends here