1 ;;; org-list.el --- Plain lists for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2018 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-odd-levels-only
)
95 (defvar org-outline-regexp-bol
)
96 (defvar org-scheduled-string
)
97 (defvar org-todo-line-regexp
)
98 (defvar org-ts-regexp
)
99 (defvar org-ts-regexp-both
)
101 (declare-function org-at-heading-p
"org" (&optional invisible-ok
))
102 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
103 (declare-function org-before-first-heading-p
"org" ())
104 (declare-function org-combine-plists
"org" (&rest plists
))
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-indentation
"org" (&optional line
))
129 (declare-function org-get-todo-state
"org" ())
130 (declare-function org-in-block-p
"org" (names))
131 (declare-function org-in-regexp
"org" (re &optional nlines visually
))
132 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
133 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
134 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
135 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
136 (declare-function org-level-increment
"org" ())
137 (declare-function org-narrow-to-subtree
"org" ())
138 (declare-function org-outline-level
"org" ())
139 (declare-function org-previous-line-empty-p
"org" ())
140 (declare-function org-reduced-level
"org" (L))
141 (declare-function org-remove-indentation
"org" (code &optional n
))
142 (declare-function org-show-subtree
"org" ())
143 (declare-function org-sort-remove-invisible
"org" (S))
144 (declare-function org-time-string-to-seconds
"org" (s))
145 (declare-function org-timer-hms-to-secs
"org-timer" (hms))
146 (declare-function org-timer-item
"org-timer" (&optional arg
))
147 (declare-function org-trim
"org" (s &optional keep-lead
))
148 (declare-function org-uniquify
"org" (list))
149 (declare-function org-invisible-p
"org" (&optional pos
))
150 (declare-function outline-next-heading
"outline" ())
151 (declare-function outline-previous-heading
"outline" ())
155 ;;; Configuration variables
157 (defgroup org-plain-lists nil
158 "Options concerning plain lists in Org mode."
159 :tag
"Org Plain lists"
160 :group
'org-structure
)
162 (defcustom org-cycle-include-plain-lists t
163 "When t, make TAB cycle visibility on plain list items.
164 Cycling plain lists works only when the cursor is on a plain list
165 item. When the cursor is on an outline heading, plain lists are
166 treated as text. This is the most stable way of handling this,
167 which is why it is the default.
169 When this is the symbol `integrate', then integrate plain list
170 items when cycling, as if they were children of outline headings.
172 This setting can lead to strange effects when switching visibility
173 to `children', because the first \"child\" in a subtree decides
174 what children should be listed. If that first \"child\" is a
175 plain list item with an implied large level number, all true
176 children and grand children of the outline heading will be
177 exposed in a children' view."
178 :group
'org-plain-lists
181 (const :tag
"Never" nil
)
182 (const :tag
"With cursor in plain list (recommended)" t
)
183 (const :tag
"As children of outline headings" integrate
)))
185 (defcustom org-list-demote-modify-bullet nil
186 "Default bullet type installed when demoting an item.
187 This is an association list, for each bullet type, this alist will point
188 to the bullet that should be used when this item is demoted.
191 (setq org-list-demote-modify-bullet
192 \\='((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
197 + Silence of the Lambs
200 + The Hunt for Red October
206 - Silence of the Lambs
209 - The Hunt for Red October
211 :group
'org-plain-lists
214 (choice :tag
"If the current bullet is "
220 (choice :tag
"demotion will change it to"
227 (defcustom org-plain-list-ordered-item-terminator t
228 "The character that makes a line with leading number an ordered list item.
229 Valid values are ?. and ?\). To get both terminators, use t.
231 This variable needs to be set before org.el is loaded. If you
232 need to make a change while Emacs is running, use the customize
233 interface or run the following code after updating it:
235 `\\[org-element-update-syntax]'"
236 :group
'org-plain-lists
237 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
238 (const :tag
"paren like in \"2)\"" ?\
))
239 (const :tag
"both" t
))
240 :set
(lambda (var val
) (set var val
)
241 (when (featurep 'org-element
) (org-element-update-syntax))))
243 (defcustom org-list-allow-alphabetical nil
244 "Non-nil means single character alphabetical bullets are allowed.
246 Both uppercase and lowercase are handled. Lists with more than
247 26 items will fallback to standard numbering. Alphabetical
248 counters like \"[@c]\" will be recognized.
250 This variable needs to be set before org.el is loaded. If you
251 need to make a change while Emacs is running, use the customize
252 interface or run the following code after updating it:
254 `\\[org-element-update-syntax]'"
255 :group
'org-plain-lists
258 :set
(lambda (var val
) (set var val
)
259 (when (featurep 'org-element
) (org-element-update-syntax))))
261 (defcustom org-list-two-spaces-after-bullet-regexp nil
262 "A regular expression matching bullets that should have 2 spaces after them.
263 When nil, no bullet will have two spaces after them. When
264 a string, it will be used as a regular expression. When the
265 bullet type of a list is changed, the new bullet type will be
266 matched against this regexp. If it matches, there will be two
267 spaces instead of one after the bullet in each item of the list."
268 :group
'org-plain-lists
270 (const :tag
"never" nil
)
273 (defcustom org-list-automatic-rules
'((checkbox . t
)
275 "Non-nil means apply set of rules when acting on lists.
277 By default, automatic actions are taken when using
278 `\\[org-meta-return]',
281 `\\[org-shiftmetaright]',
282 `\\[org-shiftmetaleft]',
283 `\\[org-ctrl-c-minus]',
284 `\\[org-toggle-checkbox]',
285 `\\[org-insert-todo-heading]'.
287 You can disable individually these rules by setting them to nil.
290 checkbox when non-nil, checkbox statistics is updated each time
291 you either insert a new checkbox or toggle a checkbox.
292 indent when non-nil, indenting or outdenting list top-item
293 with its subtree will move the whole list and
294 outdenting a list whose bullet is * to column 0 will
295 change that bullet to \"-\"."
296 :group
'org-plain-lists
298 :type
'(alist :tag
"Sets of rules"
301 (const :tag
"Checkbox" checkbox
)
302 (const :tag
"Indent" indent
))
304 (boolean :tag
"Activate" :value t
)))
306 (defcustom org-list-use-circular-motion nil
307 "Non-nil means commands implying motion in lists should be cyclic.
309 In that case, the item following the last item is the first one,
310 and the item preceding the first item is the last one.
312 This affects the behavior of
313 `\\[org-move-item-up]',
314 `\\[org-move-item-down]',
316 `\\[org-previous-item]'."
317 :group
'org-plain-lists
321 (defvar org-checkbox-statistics-hook nil
322 "Hook that is run whenever Org thinks checkbox statistics should be updated.
323 This hook runs even if checkbox rule in
324 `org-list-automatic-rules' does not apply, so it can be used to
325 implement alternative ways of collecting statistics
328 (defcustom org-checkbox-hierarchical-statistics t
329 "Non-nil means checkbox statistics counts only the state of direct children.
330 When nil, all boxes below the cookie are counted.
331 This can be set to nil on a per-node basis using a COOKIE_DATA property
332 with the word \"recursive\" in the value."
333 :group
'org-plain-lists
336 (defcustom org-list-description-max-indent
20
337 "Maximum indentation for the second line of a description list.
338 When the indentation would be larger than this, it will become
339 5 characters instead."
340 :group
'org-plain-lists
344 (defcustom org-list-indent-offset
0
345 "Additional indentation for sub-items in a list.
346 By setting this to a small number, usually 1 or 2, one can more
347 clearly distinguish sub-items in a list."
348 :group
'org-plain-lists
352 (defvar org-list-forbidden-blocks
'("example" "verse" "src" "export")
353 "Names of blocks where lists are not allowed.
354 Names must be in lower case.")
356 (defvar org-list-export-context
'(block inlinetask
)
357 "Context types where lists will be interpreted during export.
359 Valid types are `drawer', `inlinetask' and `block'. More
360 specifically, type `block' is determined by the variable
361 `org-list-forbidden-blocks'.")
365 ;;; Predicates and regexps
367 (defconst org-list-end-re
"^[ \t]*\n[ \t]*\n"
368 "Regex matching the end of a plain list.")
370 (defconst org-list-full-item-re
371 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
372 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
373 "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
374 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
375 "Matches a list item and puts everything into groups:
379 group 4: description tag")
381 (defun org-item-re ()
382 "Return the correct regular expression for plain lists."
384 ((eq org-plain-list-ordered-item-terminator t
) "[.)]")
385 ((= org-plain-list-ordered-item-terminator ?\
)) ")")
386 ((= org-plain-list-ordered-item-terminator ?.
) "\\.")
388 (alpha (if org-list-allow-alphabetical
"\\|[A-Za-z]" "")))
389 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha
"\\)" term
390 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
392 (defsubst org-item-beginning-re
()
393 "Regexp matching the beginning of a plain list item."
394 (concat "^" (org-item-re)))
396 (defun org-list-at-regexp-after-bullet-p (regexp)
397 "Is point at a list item with REGEXP after bullet?"
400 (goto-char (match-end 0))
401 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
402 (if org-list-allow-alphabetical
403 "\\([0-9]+\\|[A-Za-z]\\)"
406 ;; Ignore counter if any
407 (when (looking-at counter-re
) (goto-char (match-end 0))))
408 (looking-at regexp
))))
410 (defun org-list-in-valid-context-p ()
411 "Is point in a context where lists are allowed?"
412 (not (org-in-block-p org-list-forbidden-blocks
)))
414 (defun org-in-item-p ()
415 "Return item beginning position when in a plain list, nil otherwise."
418 (let* ((case-fold-search t
)
419 (context (org-list-context))
420 (lim-up (car context
))
421 (inlinetask-re (and (featurep 'org-inlinetask
)
422 (org-inlinetask-outline-regexp)))
423 (item-re (org-item-re))
424 ;; Indentation isn't meaningful when point starts at an empty
425 ;; line or an inline task.
426 (ind-ref (if (or (looking-at "^[ \t]*$")
427 (and inlinetask-re
(looking-at inlinetask-re
)))
429 (org-get-indentation))))
431 ((eq (nth 2 context
) 'invalid
) nil
)
432 ((looking-at item-re
) (point))
434 ;; Detect if cursor in amidst `org-list-end-re'. First, count
435 ;; number HL of hard lines it takes, then call `org-in-regexp'
436 ;; to compute its boundaries END-BOUNDS. When point is
437 ;; in-between, move cursor before regexp beginning.
438 (let ((hl 0) (i -
1) end-bounds
)
440 (while (setq i
(string-match
441 "[\r\n]" org-list-end-re
(1+ i
)))
443 (setq end-bounds
(org-in-regexp org-list-end-re hl
)))
444 (>= (point) (car end-bounds
))
445 (< (point) (cdr end-bounds
)))
446 (goto-char (car end-bounds
))
448 ;; Look for an item, less indented that reference line.
451 (let ((ind (org-get-indentation)))
453 ;; This is exactly what we want.
454 ((and (looking-at item-re
) (< ind ind-ref
))
455 (throw 'exit
(point)))
456 ;; At upper bound of search or looking at the end of a
457 ;; previous list: search is over.
458 ((<= (point) lim-up
) (throw 'exit nil
))
459 ((looking-at org-list-end-re
) (throw 'exit nil
))
460 ;; Skip blocks, drawers, inline-tasks, blank lines
461 ((and (looking-at "^[ \t]*#\\+end_")
462 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
463 ((and (looking-at "^[ \t]*:END:")
464 (re-search-backward org-drawer-regexp lim-up t
))
466 ((and inlinetask-re
(looking-at inlinetask-re
))
467 (org-inlinetask-goto-beginning)
469 ((looking-at "^[ \t]*$") (forward-line -
1))
470 ;; Text at column 0 cannot belong to a list: stop.
471 ((zerop ind
) (throw 'exit nil
))
472 ;; Normal text less indented than reference line, take
473 ;; it as new reference.
477 (t (forward-line -
1)))))))))))
479 (defun org-at-item-p ()
480 "Is point in a line starting a hand-formatted item?"
483 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
485 (defun org-at-item-bullet-p ()
486 "Is point at the bullet of a plain list item?"
488 (not (member (char-after) '(?\ ?
\t)))
489 (< (point) (match-end 0))))
491 (defun org-at-item-timer-p ()
492 "Is point at a line starting a plain list item with a timer?"
493 (org-list-at-regexp-after-bullet-p
494 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
496 (defun org-at-item-description-p ()
497 "Is point at a description list item?"
498 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::\\([ \t]+\\|$\\)"))
500 (defun org-at-item-checkbox-p ()
501 "Is point at a line starting a plain-list item with a checklet?"
502 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
504 (defun org-at-item-counter-p ()
505 "Is point at a line starting a plain-list item with a counter?"
507 (looking-at org-list-full-item-re
)
512 ;;; Structures and helper functions
514 (defun org-list-context ()
515 "Determine context, and its boundaries, around point.
517 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
518 are boundaries and CONTEXT is a symbol among `drawer', `block',
519 `invalid', `inlinetask' and nil.
521 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
524 (org-with-limited-levels
526 (let ((case-fold-search t
) (pos (point)) beg end context-type
527 ;; Get positions of surrounding headings. This is the
529 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t
))
532 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
533 ;; Is point inside a drawer?
534 (let ((end-re "^[ \t]*:END:")
535 (beg-re org-drawer-regexp
))
536 (when (save-excursion
537 (and (not (looking-at beg-re
))
538 (not (looking-at end-re
))
539 (setq beg
(and (re-search-backward beg-re lim-up t
)
540 (1+ (point-at-eol))))
541 (setq end
(or (and (re-search-forward end-re lim-down t
)
542 (1- (match-beginning 0)))
545 (setq lim-up beg lim-down end context-type
'drawer
)))
546 ;; Is point strictly in a block, and of which type?
547 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type
)
548 (when (save-excursion
549 (and (not (looking-at block-re
))
550 (setq beg
(and (re-search-backward block-re lim-up t
)
551 (1+ (point-at-eol))))
552 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
553 (setq type
(downcase (match-string 1)))
555 (setq end
(or (and (re-search-forward block-re lim-down t
)
559 (equal (downcase (match-string 1)) "end")))
560 (setq lim-up beg lim-down end
561 context-type
(if (member type org-list-forbidden-blocks
)
563 ;; Is point in an inlinetask?
564 (when (and (featurep 'org-inlinetask
)
566 (let* ((beg-re (org-inlinetask-outline-regexp))
567 (end-re (concat beg-re
"END[ \t]*$")))
568 (and (not (looking-at "^\\*+"))
569 (setq beg
(and (re-search-backward beg-re lim-up t
)
570 (1+ (point-at-eol))))
571 (not (looking-at end-re
))
572 (setq end
(and (re-search-forward end-re lim-down t
)
573 (1- (match-beginning 0))))
575 (setq lim-up beg lim-down end context-type
'inlinetask
))
576 ;; Return context boundaries and type.
577 (list lim-up lim-down context-type
))))))
579 (defun org-list-struct ()
580 "Return structure of list at point.
582 A list structure is an alist where key is point at item, and
585 2. bullet with trailing whitespace,
586 3. bullet counter, if any,
588 5. description tag, if any,
589 6. position at item end.
591 Thus the following list, where numbers in parens are
596 5. [@5] sub-item 2 (34)
597 some other text belonging to first item (55)
599 + tag :: description (109)
602 will get the following structure:
604 ((1 0 \"- \" nil \"[X]\" nil 97)
605 (18 2 \"1. \" nil nil nil 34)
606 (34 2 \"5. \" \"5\" nil nil 55)
607 (97 0 \"- \" nil nil nil 131)
608 (109 2 \"+ \" nil nil \"tag\" 131))
610 Assume point is at an item."
613 (let* ((case-fold-search t
)
614 (context (org-list-context))
615 (lim-up (car context
))
616 (lim-down (nth 1 context
))
618 (item-re (org-item-re))
619 (inlinetask-re (and (featurep 'org-inlinetask
)
620 (org-inlinetask-outline-regexp)))
621 (beg-cell (cons (point) (org-get-indentation)))
622 itm-lst itm-lst-2 end-lst end-lst-2 struct
625 ;; Return association at point.
627 (looking-at org-list-full-item-re
)
628 (let ((bullet (match-string-no-properties 1)))
632 (match-string-no-properties 2) ; counter
633 (match-string-no-properties 3) ; checkbox
635 (and (string-match-p "[-+*]" bullet
)
636 (match-string-no-properties 4)))))))
639 ;; Ensure list ends at the first blank line.
641 (skip-chars-backward " \r\t\n")
642 (min (1+ (point-at-eol)) lim-down
)))))
643 ;; 1. Read list from starting item to its beginning, and save
644 ;; top item position and indentation in BEG-CELL. Also store
645 ;; ending position of items in END-LST.
649 (let ((ind (org-get-indentation)))
652 ;; At upward limit: if we ended at an item, store it,
653 ;; else dismiss useless data recorded above BEG-CELL.
657 (if (not (looking-at item-re
))
658 (memq (assq (car beg-cell
) itm-lst
) itm-lst
)
659 (setq beg-cell
(cons (point) ind
))
660 (cons (funcall assoc-at-point ind
) itm-lst
)))))
661 ;; Looking at a list ending regexp. Dismiss useless
662 ;; data recorded above BEG-CELL. Jump to part 2.
663 ((looking-at org-list-end-re
)
666 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
667 ;; Point is at an item. Add data to ITM-LST. It may
668 ;; also end a previous item: save it in END-LST. If
669 ;; ind is less or equal than BEG-CELL and there is no
670 ;; end at this ind or lesser, this item becomes the new
672 ((looking-at item-re
)
673 (push (funcall assoc-at-point ind
) itm-lst
)
674 (push (cons ind
(point)) end-lst
)
675 (when (< ind text-min-ind
) (setq beg-cell
(cons (point) ind
)))
677 ;; Skip blocks, drawers, inline tasks, blank lines.
678 ((and (looking-at "^[ \t]*#\\+end_")
679 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
680 ((and (looking-at "^[ \t]*:END:")
681 (re-search-backward org-drawer-regexp lim-up t
))
683 ((and inlinetask-re
(looking-at inlinetask-re
))
684 (org-inlinetask-goto-beginning)
686 ((looking-at "^[ \t]*$")
688 ;; From there, point is not at an item. Interpret
689 ;; line's indentation:
690 ;; - text at column 0 is necessarily out of any list.
691 ;; Dismiss data recorded above BEG-CELL. Jump to
693 ;; - any other case may be an ending position for an
694 ;; hypothetical item above. Store it and proceed.
698 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
700 (when (< ind text-min-ind
) (setq text-min-ind ind
))
701 (push (cons ind
(point)) end-lst
)
702 (forward-line -
1)))))))
703 ;; 2. Read list from starting point to its end, that is until we
704 ;; get out of context, or that a non-item line is less or
705 ;; equally indented than BEG-CELL's cdr. Also, store ending
706 ;; position of items in END-LST-2.
709 (let ((ind (org-get-indentation)))
711 ((>= (point) lim-down
)
712 ;; At downward limit: this is de facto the end of the
713 ;; list. Save point as an ending position, and jump to
716 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
717 ;; Looking at a list ending regexp. Save point as an
718 ;; ending position and jump to part 3.
719 ((looking-at org-list-end-re
)
720 (throw 'exit
(push (cons 0 (point)) end-lst-2
)))
721 ((looking-at item-re
)
722 ;; Point is at an item. Add data to ITM-LST-2. It may
723 ;; also end a previous item, so save it in END-LST-2.
724 (push (funcall assoc-at-point ind
) itm-lst-2
)
725 (push (cons ind
(point)) end-lst-2
)
727 ;; Skip inline tasks and blank lines along the way
728 ((and inlinetask-re
(looking-at inlinetask-re
))
729 (org-inlinetask-goto-end))
730 ((looking-at "^[ \t]*$")
732 ;; Ind is lesser or equal than BEG-CELL's. The list is
733 ;; over: store point as an ending position and jump to
735 ((<= ind
(cdr beg-cell
))
737 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
738 ;; Else, if ind is lesser or equal than previous item's,
739 ;; this is an ending position: store it. In any case,
740 ;; skip block or drawer at point, and move to next line.
742 (when (<= ind
(nth 1 (car itm-lst-2
)))
743 (push (cons ind
(point)) end-lst-2
))
745 ((and (looking-at "^[ \t]*#\\+begin_")
746 (re-search-forward "^[ \t]*#\\+end_" lim-down t
)))
747 ((and (looking-at org-drawer-regexp
)
748 (re-search-forward "^[ \t]*:END:" lim-down t
))))
749 (forward-line 1))))))
750 (setq struct
(append itm-lst
(cdr (nreverse itm-lst-2
)))
751 end-lst
(append end-lst
(cdr (nreverse end-lst-2
))))
752 ;; 3. Associate each item to its end position.
753 (org-list-struct-assoc-end struct end-lst
)
757 (defun org-list-struct-assoc-end (struct end-list
)
758 "Associate proper ending point to items in STRUCT.
760 END-LIST is a pseudo-alist where car is indentation and cdr is
763 This function modifies STRUCT."
764 (let ((endings end-list
))
767 (let ((pos (car elt
))
769 ;; Remove end candidates behind current item.
770 (while (or (<= (cdar endings
) pos
))
772 ;; Add end position to item assoc.
773 (let ((old-end (nthcdr 6 elt
))
774 (new-end (assoc-default ind endings
'<=)))
776 (setcar old-end new-end
)
777 (setcdr elt
(append (cdr elt
) (list new-end
)))))))
780 (defun org-list-prevs-alist (struct)
781 "Return alist between item and previous item in STRUCT."
782 (let ((item-end-alist (mapcar (lambda (e) (cons (car e
) (nth 6 e
)))
785 (let ((prev (car (rassq (car e
) item-end-alist
))))
786 (cons (car e
) prev
)))
789 (defun org-list-parents-alist (struct)
790 "Return alist between item and parent in STRUCT."
791 (let* ((ind-to-ori (list (list (nth 1 (car struct
)))))
792 (top-item (org-list-get-top-point struct
))
793 (prev-pos (list top-item
)))
795 (mapcar (lambda (item)
796 (let ((pos (car item
))
798 (prev-ind (caar ind-to-ori
)))
802 ;; A sub-list is over. Find the associated
803 ;; origin in IND-TO-ORI. If it cannot be
804 ;; found (ill-formed list), set its parent as
805 ;; the first item less indented. If there is
806 ;; none, make it a top-level item.
808 (or (member (assq ind ind-to-ori
) ind-to-ori
)
812 (when (< (car e
) ind
)
813 (throw 'exit
(member e ind-to-ori
))))
816 (cons pos
(cdar ind-to-ori
)))
817 ;; A sub-list starts. Every item at IND will
818 ;; have previous item as its parent.
820 (let ((origin (nth 1 prev-pos
)))
821 (push (cons ind origin
) ind-to-ori
)
823 ;; Another item in the same sub-list: it shares
824 ;; the same parent as the previous item.
825 (t (cons pos
(cdar ind-to-ori
))))))
832 (defsubst org-list-get-nth
(n key struct
)
833 "Return the Nth value of KEY in STRUCT."
834 (nth n
(assq key struct
)))
836 (defun org-list-set-nth (n key struct new
)
837 "Set the Nth value of KEY in STRUCT to NEW.
838 \nThis function modifies STRUCT."
839 (setcar (nthcdr n
(assq key struct
)) new
))
841 (defsubst org-list-get-ind
(item struct
)
842 "Return indentation of ITEM in STRUCT."
843 (org-list-get-nth 1 item struct
))
845 (defun org-list-set-ind (item struct ind
)
846 "Set indentation of ITEM in STRUCT to IND.
847 \nThis function modifies STRUCT."
848 (org-list-set-nth 1 item struct ind
))
850 (defsubst org-list-get-bullet
(item struct
)
851 "Return bullet of ITEM in STRUCT."
852 (org-list-get-nth 2 item struct
))
854 (defun org-list-set-bullet (item struct bullet
)
855 "Set bullet of ITEM in STRUCT to BULLET.
856 \nThis function modifies STRUCT."
857 (org-list-set-nth 2 item struct bullet
))
859 (defsubst org-list-get-counter
(item struct
)
860 "Return counter of ITEM in STRUCT."
861 (org-list-get-nth 3 item struct
))
863 (defsubst org-list-get-checkbox
(item struct
)
864 "Return checkbox of ITEM in STRUCT or nil."
865 (org-list-get-nth 4 item struct
))
867 (defun org-list-set-checkbox (item struct checkbox
)
868 "Set checkbox of ITEM in STRUCT to CHECKBOX.
869 \nThis function modifies STRUCT."
870 (org-list-set-nth 4 item struct checkbox
))
872 (defsubst org-list-get-tag
(item struct
)
873 "Return end position of ITEM in STRUCT."
874 (org-list-get-nth 5 item struct
))
876 (defun org-list-get-item-end (item struct
)
877 "Return end position of ITEM in STRUCT."
878 (org-list-get-nth 6 item struct
))
880 (defun org-list-get-item-end-before-blank (item struct
)
881 "Return point at end of ITEM in STRUCT, before any blank line.
882 Point returned is at end of line."
884 (goto-char (org-list-get-item-end item struct
))
885 (skip-chars-backward " \r\t\n")
888 (defun org-list-get-parent (item struct parents
)
889 "Return parent of ITEM or nil.
890 STRUCT is the list structure. PARENTS is the alist of parents,
891 as returned by `org-list-parents-alist'."
892 (let ((parents (or parents
(org-list-parents-alist struct
))))
893 (cdr (assq item parents
))))
895 (defun org-list-has-child-p (item struct
)
896 "Non-nil if ITEM has a child.
898 STRUCT is the list structure.
900 Value returned is the position of the first child of ITEM."
901 (let ((ind (org-list-get-ind item struct
))
902 (child-maybe (car (nth 1 (member (assq item struct
) struct
)))))
903 (when (and child-maybe
904 (< ind
(org-list-get-ind child-maybe struct
)))
907 (defun org-list-get-next-item (item _struct prevs
)
908 "Return next item in same sub-list as ITEM, or nil.
909 STRUCT is the list structure. PREVS is the alist of previous
910 items, as returned by `org-list-prevs-alist'."
911 (car (rassq item prevs
)))
913 (defun org-list-get-prev-item (item _struct prevs
)
914 "Return previous 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 (cdr (assq item prevs
)))
919 (defun org-list-get-subtree (item struct
)
920 "List all items having ITEM as a common ancestor, or nil.
921 STRUCT is the list structure."
922 (let* ((item-end (org-list-get-item-end item struct
))
923 (sub-struct (cdr (member (assq item struct
) struct
)))
928 (if (< pos item-end
) (push pos subtree
) (throw 'exit nil
))))
932 (defun org-list-get-all-items (item struct prevs
)
933 "List all items in the same sub-list as ITEM.
934 STRUCT is the list structure. PREVS is the alist of previous
935 items, as returned by `org-list-prevs-alist'."
936 (let ((prev-item item
)
938 before-item after-item
)
939 (while (setq prev-item
(org-list-get-prev-item prev-item struct prevs
))
940 (push prev-item before-item
))
941 (while (setq next-item
(org-list-get-next-item next-item struct prevs
))
942 (push next-item after-item
))
943 (append before-item
(list item
) (nreverse after-item
))))
945 (defun org-list-get-children (item _struct parents
)
946 "List all children of ITEM, or nil.
947 STRUCT is the list structure. PARENTS is the alist of parents,
948 as returned by `org-list-parents-alist'."
950 (while (setq child
(car (rassq item parents
)))
951 (setq parents
(cdr (member (assq child parents
) parents
)))
955 (defun org-list-get-top-point (struct)
956 "Return point at beginning of list.
957 STRUCT is the list structure."
960 (defun org-list-get-bottom-point (struct)
961 "Return point at bottom of list.
962 STRUCT is the list structure."
964 (mapcar (lambda (e) (org-list-get-item-end (car e
) struct
)) struct
)))
966 (defun org-list-get-list-begin (item struct prevs
)
967 "Return point at beginning of sub-list ITEM belongs.
968 STRUCT is the list structure. PREVS is the alist of previous
969 items, as returned by `org-list-prevs-alist'."
970 (let ((first-item item
) prev-item
)
971 (while (setq prev-item
(org-list-get-prev-item first-item struct prevs
))
972 (setq first-item prev-item
))
975 (defalias 'org-list-get-first-item
'org-list-get-list-begin
)
977 (defun org-list-get-last-item (item struct prevs
)
978 "Return point at last item of sub-list ITEM belongs.
979 STRUCT is the list structure. PREVS is the alist of previous
980 items, as returned by `org-list-prevs-alist'."
981 (let ((last-item item
) next-item
)
982 (while (setq next-item
(org-list-get-next-item last-item struct prevs
))
983 (setq last-item next-item
))
986 (defun org-list-get-list-end (item struct prevs
)
987 "Return point at end of sub-list ITEM belongs.
988 STRUCT is the list structure. PREVS is the alist of previous
989 items, as returned by `org-list-prevs-alist'."
990 (org-list-get-item-end (org-list-get-last-item item struct prevs
) struct
))
992 (defun org-list-get-list-type (item struct prevs
)
993 "Return the type of the list containing ITEM, as a symbol.
995 STRUCT is the list structure. PREVS is the alist of previous
996 items, as returned by `org-list-prevs-alist'.
998 Possible types are `descriptive', `ordered' and `unordered'. The
999 type is determined by the first item of the list."
1000 (let ((first (org-list-get-list-begin item struct prevs
)))
1002 ((string-match-p "[[:alnum:]]" (org-list-get-bullet first struct
)) 'ordered
)
1003 ((org-list-get-tag first struct
) 'descriptive
)
1006 (defun org-list-get-item-number (item struct prevs parents
)
1007 "Return ITEM's sequence number.
1009 STRUCT is the list structure. PREVS is the alist of previous
1010 items, as returned by `org-list-prevs-alist'. PARENTS is the
1011 alist of ancestors, as returned by `org-list-parents-alist'.
1013 Return value is a list of integers. Counters have an impact on
1015 (let ((get-relative-number
1017 (lambda (item struct prevs
)
1018 ;; Return relative sequence number of ITEM in the sub-list
1019 ;; it belongs. STRUCT is the list structure. PREVS is
1020 ;; the alist of previous items.
1021 (let ((seq 0) (pos item
) counter
)
1022 (while (and (not (setq counter
(org-list-get-counter pos struct
)))
1023 (setq pos
(org-list-get-prev-item pos struct prevs
)))
1025 (if (not counter
) (1+ seq
)
1027 ((string-match "[A-Za-z]" counter
)
1028 (+ (- (string-to-char (upcase (match-string 0 counter
))) 64)
1030 ((string-match "[0-9]+" counter
)
1031 (+ (string-to-number (match-string 0 counter
)) seq
))
1033 ;; Cons each parent relative number into return value (OUT).
1034 (let ((out (list (funcall get-relative-number item struct prevs
)))
1036 (while (setq parent
(org-list-get-parent parent struct parents
))
1037 (push (funcall get-relative-number parent struct prevs
) out
))
1045 (defun org-list-search-generic (search re bound noerr
)
1046 "Search a string in valid contexts for lists.
1047 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1048 in `re-search-forward'."
1050 (let ((origin (point)))
1052 ;; 1. No match: return to origin or bound, depending on NOERR.
1053 (unless (funcall search re bound noerr
)
1054 (throw 'exit
(and (goto-char (if (memq noerr
'(t nil
)) origin bound
))
1056 ;; 2. Match in valid context: return point. Else, continue
1058 (when (org-list-in-valid-context-p) (throw 'exit
(point)))))))
1060 (defun org-list-search-backward (regexp &optional bound noerror
)
1061 "Like `re-search-backward' but stop only where lists are recognized.
1062 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1063 `re-search-backward'."
1064 (org-list-search-generic #'re-search-backward
1065 regexp
(or bound
(point-min)) noerror
))
1067 (defun org-list-search-forward (regexp &optional bound noerror
)
1068 "Like `re-search-forward' but stop only where lists are recognized.
1069 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1070 `re-search-forward'."
1071 (org-list-search-generic #'re-search-forward
1072 regexp
(or bound
(point-max)) noerror
))
1076 ;;; Methods on structures
1078 (defsubst org-list-bullet-string
(bullet)
1079 "Return BULLET with the correct number of whitespaces.
1080 It determines the number of whitespaces to append by looking at
1081 `org-list-two-spaces-after-bullet-regexp'."
1083 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1085 org-list-two-spaces-after-bullet-regexp bullet
))
1088 (if (string-match "\\S-+\\([ \t]*\\)" bullet
)
1089 (replace-match spaces nil nil bullet
1)
1092 (defun org-list-swap-items (beg-A beg-B struct
)
1093 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1095 Blank lines at the end of items are left in place. Item
1096 visibility is preserved. Return the new structure after the
1099 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1100 to the same sub-list.
1102 This function modifies STRUCT."
1104 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct
))
1105 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct
))
1106 (end-A (org-list-get-item-end beg-A struct
))
1107 (end-B (org-list-get-item-end beg-B struct
))
1108 (size-A (- end-A-no-blank beg-A
))
1109 (size-B (- end-B-no-blank beg-B
))
1110 (body-A (buffer-substring beg-A end-A-no-blank
))
1111 (body-B (buffer-substring beg-B end-B-no-blank
))
1112 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B
))
1113 (sub-A (cons beg-A
(org-list-get-subtree beg-A struct
)))
1114 (sub-B (cons beg-B
(org-list-get-subtree beg-B struct
)))
1115 ;; Store overlays responsible for visibility status. We
1116 ;; also need to store their boundaries as they will be
1117 ;; removed from buffer.
1122 (and (>= (overlay-start o
) beg-A
)
1123 (<= (overlay-end o
) end-A
)
1124 (list o
(overlay-start o
) (overlay-end o
))))
1125 (overlays-in beg-A end-A
)))
1128 (and (>= (overlay-start o
) beg-B
)
1129 (<= (overlay-end o
) end-B
)
1130 (list o
(overlay-start o
) (overlay-end o
))))
1131 (overlays-in beg-B end-B
))))))
1132 ;; 1. Move effectively items in buffer.
1134 (delete-region beg-A end-B-no-blank
)
1135 (insert (concat body-B between-A-no-blank-and-B body-A
))
1136 ;; 2. Now modify struct. No need to re-read the list, the
1137 ;; transformation is just a shift of positions. Some special
1138 ;; attention is required for items ending at END-A and END-B
1139 ;; as empty spaces are not moved there. In others words,
1140 ;; item BEG-A will end with whitespaces that were at the end
1141 ;; of BEG-B and the same applies to BEG-B.
1143 (let ((pos (car e
)))
1147 (let ((end-e (nth 6 e
)))
1148 (setcar e
(+ pos
(- end-B-no-blank end-A-no-blank
)))
1149 (setcar (nthcdr 6 e
)
1150 (+ end-e
(- end-B-no-blank end-A-no-blank
)))
1151 (when (= end-e end-A
) (setcar (nthcdr 6 e
) end-B
))))
1153 (let ((end-e (nth 6 e
)))
1154 (setcar e
(- (+ pos beg-A
) beg-B
))
1155 (setcar (nthcdr 6 e
) (+ end-e
(- beg-A beg-B
)))
1156 (when (= end-e end-B
)
1157 (setcar (nthcdr 6 e
)
1158 (+ beg-A size-B
(- end-A end-A-no-blank
))))))
1160 (let ((end-e (nth 6 e
)))
1161 (setcar e
(+ pos
(- size-B size-A
)))
1162 (setcar (nthcdr 6 e
) (+ end-e
(- size-B size-A
))))))))
1163 (setq struct
(sort struct
#'car-less-than-car
))
1164 ;; Restore visibility status, by moving overlays to their new
1166 (dolist (ov (car overlays
))
1169 (+ (nth 1 ov
) (- (+ beg-B
(- size-B size-A
)) beg-A
))
1170 (+ (nth 2 ov
) (- (+ beg-B
(- size-B size-A
)) beg-A
))))
1171 (dolist (ov (cdr overlays
))
1172 (move-overlay (car ov
)
1173 (+ (nth 1 ov
) (- beg-A beg-B
))
1174 (+ (nth 2 ov
) (- beg-A beg-B
))))
1175 ;; Return structure.
1178 (defun org-list-separating-blank-lines-number (pos struct prevs
)
1179 "Return number of blank lines that should separate items in list.
1181 POS is the position of point where `org-list-insert-item' was called.
1183 STRUCT is the list structure. PREVS is the alist of previous
1184 items, as returned by `org-list-prevs-alist'.
1186 Assume point is at item's beginning. If the item is alone, apply
1187 some heuristics to guess the result."
1189 (let ((item (point))
1191 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
1196 ;; Count blank lines above beginning of line.
1198 (count-lines (goto-char (point-at-bol))
1199 (progn (skip-chars-backward " \r\t\n")
1203 ;; Trivial cases where there should be none.
1204 ((not insert-blank-p
) 0)
1205 ;; When `org-blank-before-new-entry' says so, it is 1.
1206 ((eq insert-blank-p t
) 1)
1207 ;; `plain-list-item' is 'auto. Count blank lines separating
1208 ;; neighbors' items in list.
1209 (t (let ((next-p (org-list-get-next-item item struct prevs
)))
1211 ;; Is there a next item?
1212 (next-p (goto-char next-p
)
1213 (funcall count-blanks
))
1214 ;; Is there a previous item?
1215 ((org-list-get-prev-item item struct prevs
)
1216 (funcall count-blanks
))
1217 ;; User inserted blank lines, trust him.
1218 ((and (> pos
(org-list-get-item-end-before-blank item struct
))
1219 (> (save-excursion (goto-char pos
)
1220 (setq usr-blank
(funcall count-blanks
)))
1223 ;; Are there blank lines inside the list so far?
1225 (goto-char (org-list-get-top-point struct
))
1226 ;; Do not use `org-list-search-forward' so blank lines
1227 ;; in blocks can be counted in.
1229 "^[ \t]*$" (org-list-get-item-end-before-blank item struct
) t
))
1231 ;; Default choice: no blank line.
1234 (defun org-list-insert-item (pos struct prevs
&optional checkbox after-bullet
)
1235 "Insert a new list item at POS and return the new structure.
1236 If POS is before first character after bullet of the item, the
1237 new item will be created before the current one.
1239 STRUCT is the list structure. PREVS is the alist of previous
1240 items, as returned by `org-list-prevs-alist'.
1242 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1243 after the bullet. Cursor will be after this text once the
1246 This function modifies STRUCT."
1247 (let ((case-fold-search t
))
1248 ;; 1. Get information about list: position of point with regards
1249 ;; to item start (BEFOREP), blank lines number separating items
1250 ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
1251 (let* ((item (progn (goto-char pos
) (goto-char (org-list-get-item-begin))))
1252 (item-end (org-list-get-item-end item struct
))
1253 (item-end-no-blank (org-list-get-item-end-before-blank item struct
))
1256 (looking-at org-list-full-item-re
)
1259 ((not (match-beginning 4)) (match-end 0))
1260 ;; Ignore tag in a non-descriptive list.
1261 ((save-match-data (string-match "[.)]" (match-string 1)))
1262 (match-beginning 4))
1264 (goto-char (match-end 4))
1265 (skip-chars-forward " \t")
1267 (split-line-p (org-get-alist-option org-M-RET-may-split-line
'item
))
1268 (blank-nb (org-list-separating-blank-lines-number
1270 ;; 2. Build the new item to be created. Concatenate same
1271 ;; bullet as item, checkbox, text AFTER-BULLET if
1272 ;; provided, and text cut from point to end of item
1273 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1274 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1275 ;; between what was cut and what was inserted in buffer
1276 ;; is stored in SIZE-OFFSET.
1277 (ind (org-list-get-ind item struct
))
1278 (ind-size (if indent-tabs-mode
1279 (+ (/ ind tab-width
) (mod ind tab-width
))
1281 (bullet (org-list-bullet-string (org-list-get-bullet item struct
)))
1282 (box (when checkbox
"[ ]"))
1284 (and (not beforep
) split-line-p
1287 ;; If POS is greater than ITEM-END, then point is
1288 ;; in some white lines after the end of the list.
1289 ;; Those must be removed, or they will be left,
1290 ;; stacking up after the list.
1291 (when (< item-end pos
)
1292 (delete-region (1- item-end
) (point-at-eol)))
1293 (skip-chars-backward " \r\t\n")
1295 (delete-and-extract-region pos item-end-no-blank
))))
1296 (body (concat bullet
(when box
(concat box
" ")) after-bullet
1298 (if (string-match "\\`[ \t]+" text-cut
)
1299 (replace-match "" t t text-cut
)
1301 (item-sep (make-string (1+ blank-nb
) ?
\n))
1302 (item-size (+ ind-size
(length body
) (length item-sep
)))
1303 (size-offset (- item-size
(length text-cut
))))
1304 ;; 4. Insert effectively item into buffer.
1306 (indent-to-column ind
)
1307 (insert body item-sep
)
1308 ;; 5. Add new item to STRUCT.
1310 (let ((p (car e
)) (end (nth 6 e
)))
1312 ;; Before inserted item, positions don't change but
1313 ;; an item ending after insertion has its end shifted
1316 (when (> end item
) (setcar (nthcdr 6 e
) (+ end size-offset
))))
1317 ;; Trivial cases where current item isn't split in
1318 ;; two. Just shift every item after new one by
1320 ((or beforep
(not split-line-p
))
1321 (setcar e
(+ p item-size
))
1322 (setcar (nthcdr 6 e
) (+ end item-size
)))
1323 ;; Item is split in two: elements before POS are just
1324 ;; shifted by ITEM-SIZE. In the case item would end
1325 ;; after split POS, ending is only shifted by
1328 (setcar e
(+ p item-size
))
1330 (setcar (nthcdr 6 e
) (+ end item-size
))
1331 (setcar (nthcdr 6 e
) (+ end size-offset
))))
1332 ;; Elements after POS are moved into new item.
1333 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1334 ;; doesn't appear in buffer yet.
1336 (setcar e
(+ p size-offset
(- item pos
(length item-sep
))))
1337 (if (= end item-end
)
1338 (setcar (nthcdr 6 e
) (+ item item-size
))
1339 (setcar (nthcdr 6 e
)
1341 (- item pos
(length item-sep
))))))
1342 ;; Elements at ITEM-END or after are only shifted by
1344 (t (setcar e
(+ p size-offset
))
1345 (setcar (nthcdr 6 e
) (+ end size-offset
))))))
1347 (push (list item ind bullet nil box nil
(+ item item-size
)) struct
)
1348 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1349 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1350 ;; exchange ITEM with the next item in list. Position cursor
1351 ;; after bullet, counter, checkbox, and label.
1354 (setq struct
(org-list-swap-items item
(+ item item-size
) struct
))
1355 (goto-char (org-list-get-next-item
1356 item struct
(org-list-prevs-alist struct
))))
1359 (defun org-list-delete-item (item struct
)
1360 "Remove ITEM from the list and return the new structure.
1362 STRUCT is the list structure."
1363 (let* ((end (org-list-get-item-end item struct
))
1364 (beg (if (= (org-list-get-bottom-point struct
) end
)
1365 ;; If ITEM ends with the list, delete blank lines
1369 (skip-chars-backward " \r\t\n")
1370 (min (1+ (point-at-eol)) (point-max)))
1372 ;; Remove item from buffer.
1373 (delete-region beg end
)
1374 ;; Remove item from structure and shift others items accordingly.
1375 ;; Don't forget to shift also ending position when appropriate.
1376 (let ((size (- end beg
)))
1377 (delq nil
(mapcar (lambda (e)
1378 (let ((pos (car e
)))
1381 (let ((end-e (nth 6 e
)))
1385 (append (butlast e
) (list beg
)))
1387 (append (butlast e
) (list (- end-e size
)))))))
1391 (append (butlast (cdr e
))
1392 (list (- (nth 6 e
) size
))))))))
1395 (defun org-list-send-item (item dest struct
)
1396 "Send ITEM to destination DEST.
1398 STRUCT is the list structure.
1400 DEST can have various values.
1402 If DEST is a buffer position, the function will assume it points
1403 to another item in the same list as ITEM, and will move the
1404 latter just before the former.
1406 If DEST is `begin' (respectively `end'), ITEM will be moved at
1407 the beginning (respectively end) of the list it belongs to.
1409 If DEST is a string like \"N\", where N is an integer, ITEM will
1410 be moved at the Nth position in the list.
1412 If DEST is `kill', ITEM will be deleted and its body will be
1413 added to the kill-ring.
1415 If DEST is `delete', ITEM will be deleted.
1417 Visibility of item is preserved.
1419 This function returns, destructively, the new list structure."
1420 (let* ((prevs (org-list-prevs-alist struct
))
1421 (item-end (org-list-get-item-end item struct
))
1422 ;; Grab full item body minus its bullet.
1429 (regexp-quote (org-list-get-bullet item struct
))))
1432 ;; Change DEST into a buffer position. A trick is needed
1433 ;; when ITEM is meant to be sent at the end of the list.
1434 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1435 ;; nil and insertion point (INS-POINT) to the first line's
1436 ;; end of the last item, we ensure the new item will be
1437 ;; inserted after the last item, and not after any of its
1438 ;; hypothetical sub-items.
1440 ((or (eq dest
'kill
) (eq dest
'delete
)))
1442 (setq dest
(org-list-get-list-begin item struct prevs
)))
1444 (setq dest
(org-list-get-list-end item struct prevs
))
1446 (goto-char (org-list-get-last-item item struct prevs
))
1448 ((string-match-p "\\`[0-9]+\\'" dest
)
1449 (let* ((all (org-list-get-all-items item struct prevs
))
1451 (index (mod (string-to-number dest
) len
)))
1452 (if (not (zerop index
))
1453 (setq dest
(nth (1- index
) all
))
1454 ;; Send ITEM at the end of the list.
1455 (setq dest
(org-list-get-list-end item struct prevs
))
1458 (org-list-get-last-item item struct prevs
))
1461 (org-M-RET-may-split-line nil
)
1462 ;; Store inner overlays (to preserve visibility).
1463 (overlays (cl-remove-if (lambda (o) (or (< (overlay-start o
) item
)
1464 (> (overlay-end o
) item
)))
1465 (overlays-in item item-end
))))
1467 ((eq dest
'delete
) (org-list-delete-item item struct
))
1470 (org-list-delete-item item struct
))
1471 ((and (integerp dest
) (/= item ins-point
))
1472 (setq item
(copy-marker item
))
1473 (setq struct
(org-list-insert-item ins-point struct prevs nil body
))
1474 ;; 1. Structure returned by `org-list-insert-item' may not be
1475 ;; accurate, as it cannot see sub-items included in BODY.
1476 ;; Thus, first compute the real structure so far.
1478 (cons (marker-position item
)
1479 (org-list-get-subtree (marker-position item
) struct
)))
1480 (new-end (org-list-get-item-end (point) struct
))
1481 (old-end (org-list-get-item-end (marker-position item
) struct
))
1483 (shift (- (point) item
)))
1484 ;; 1.1. Remove the item just created in structure.
1485 (setq struct
(delete (assq new-item struct
) struct
))
1486 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1491 (let* ((cell (assq e struct
))
1495 (append (butlast (cdr cell
))
1496 (list (if (= end old-end
)
1500 #'car-less-than-car
)))
1501 ;; 2. Restore inner overlays.
1502 (dolist (o overlays
)
1504 (+ (overlay-start o
) (- (point) item
))
1505 (+ (overlay-end o
) (- (point) item
))))
1506 ;; 3. Eventually delete extra copy of the item and clean marker.
1507 (prog1 (org-list-delete-item (marker-position item
) struct
)
1508 (move-marker item nil
)))
1511 (defun org-list-struct-outdent (start end struct parents
)
1512 "Outdent items between positions START and END.
1514 STRUCT is the list structure. PARENTS is the alist of items'
1515 parents, as returned by `org-list-parents-alist'.
1517 START is included, END excluded."
1520 (let* ((item (car cell
))
1521 (parent (cdr cell
)))
1523 ;; Item not yet in zone: keep association.
1524 ((< item start
) cell
)
1525 ;; Item out of zone: follow associations in ACC.
1527 (let ((convert (and parent
(assq parent acc
))))
1528 (if convert
(cons item
(cdr convert
)) cell
)))
1529 ;; Item has no parent: error
1531 (error "Cannot outdent top-level items"))
1532 ;; Parent is outdented: keep association.
1534 (push (cons parent item
) acc
) cell
)
1536 ;; Parent isn't outdented: reparent to grand-parent.
1537 (let ((grand-parent (org-list-get-parent
1538 parent struct parents
)))
1539 (push (cons parent item
) acc
)
1540 (cons item grand-parent
))))))))
1541 (mapcar out parents
)))
1543 (defun org-list-struct-indent (start end struct parents prevs
)
1544 "Indent items between positions START and END.
1546 STRUCT is the list structure. PARENTS is the alist of parents
1547 and PREVS is the alist of previous items, returned by,
1548 respectively, `org-list-parents-alist' and
1549 `org-list-prevs-alist'.
1551 START is included and END excluded.
1553 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1554 bullets between START and END."
1556 (set-assoc (lambda (cell) (push cell acc
) cell
))
1557 (change-bullet-maybe
1562 ;; Normalize ordered bullets.
1563 (let ((bul (org-trim
1564 (org-list-get-bullet item struct
))))
1565 (cond ((string-match "[A-Z]\\." bul
) "A.")
1566 ((string-match "[A-Z])" bul
) "A)")
1567 ((string-match "[a-z]\\." bul
) "a.")
1568 ((string-match "[a-z])" bul
) "a)")
1569 ((string-match "[0-9]\\." bul
) "1.")
1570 ((string-match "[0-9])" bul
) "1)")
1572 org-list-demote-modify-bullet
))))
1573 (when new-bul-p
(org-list-set-bullet item struct new-bul-p
))))))
1576 (let* ((item (car cell
))
1577 (parent (cdr cell
)))
1579 ;; Item not yet in zone: keep association.
1580 ((< item start
) cell
)
1582 ;; Item out of zone: follow associations in ACC.
1583 (let ((convert (assq parent acc
)))
1584 (if convert
(cons item
(cdr convert
)) cell
)))
1586 ;; Item is in zone...
1587 (let ((prev (org-list-get-prev-item item struct prevs
)))
1588 ;; Check if bullet needs to be changed.
1589 (funcall change-bullet-maybe item
)
1591 ;; First item indented but not parent: error
1592 ((and (not prev
) (< parent start
))
1593 (error "Cannot indent the first item of a list"))
1594 ;; First item and parent indented: keep same
1596 ((not prev
) (funcall set-assoc cell
))
1597 ;; Previous item not indented: reparent to it.
1598 ((< prev start
) (funcall set-assoc
(cons item prev
)))
1599 ;; Previous item indented: reparent like it.
1602 (cons item
(cdr (assq prev acc
)))))))))))))
1603 (mapcar ind parents
)))
1607 ;;; Repairing structures
1609 (defun org-list-use-alpha-bul-p (first struct prevs
)
1610 "Non-nil if list starting at FIRST can have alphabetical bullets.
1612 STRUCT is list structure. PREVS is the alist of previous items,
1613 as returned by `org-list-prevs-alist'."
1614 (and org-list-allow-alphabetical
1616 (let ((item first
) (ascii 64) (case-fold-search nil
))
1617 ;; Pretend that bullets are uppercase and check if alphabet
1618 ;; is sufficient, taking counters into account.
1620 (let ((count (org-list-get-counter item struct
)))
1621 ;; Virtually determine current bullet
1622 (if (and count
(string-match-p "[a-zA-Z]" count
))
1623 ;; Counters are not case-sensitive.
1624 (setq ascii
(string-to-char (upcase count
)))
1625 (setq ascii
(1+ ascii
)))
1626 ;; Test if bullet would be over z or Z.
1629 (setq item
(org-list-get-next-item item struct prevs
)))))
1630 ;; All items checked. All good.
1633 (defun org-list-inc-bullet-maybe (bullet)
1634 "Increment BULLET if applicable."
1635 (let ((case-fold-search nil
))
1637 ;; Num bullet: increment it.
1638 ((string-match "[0-9]+" bullet
)
1640 (number-to-string (1+ (string-to-number (match-string 0 bullet
))))
1642 ;; Alpha bullet: increment it.
1643 ((string-match "[A-Za-z]" bullet
)
1645 (char-to-string (1+ (string-to-char (match-string 0 bullet
))))
1647 ;; Unordered bullet: leave it.
1650 (defun org-list-struct-fix-bul (struct prevs
)
1651 "Verify and correct bullets in STRUCT.
1652 PREVS is the alist of previous items, as returned by
1653 `org-list-prevs-alist'.
1655 This function modifies STRUCT."
1656 (let ((case-fold-search nil
)
1659 ;; Set bullet of ITEM in STRUCT, depending on the type of
1660 ;; first item of the list, the previous bullet and counter
1663 (let* ((prev (org-list-get-prev-item item struct prevs
))
1664 (prev-bul (and prev
(org-list-get-bullet prev struct
)))
1665 (counter (org-list-get-counter item struct
))
1666 (bullet (org-list-get-bullet item struct
))
1667 (alphap (and (not prev
)
1668 (org-list-use-alpha-bul-p item struct prevs
))))
1669 (org-list-set-bullet
1671 (org-list-bullet-string
1673 ;; Alpha counter in alpha list: use counter.
1675 (string-match "[a-zA-Z]" counter
)
1676 (string-match "[a-zA-Z]" prev-bul
))
1677 ;; Use cond to be sure `string-match' is used in
1681 ((string-match "[a-z]" prev-bul
) (downcase counter
))
1682 ((string-match "[A-Z]" prev-bul
) (upcase counter
)))))
1683 (replace-match real-count nil nil prev-bul
)))
1684 ;; Num counter in a num list: use counter.
1686 (string-match "[0-9]+" counter
)
1687 (string-match "[0-9]+" prev-bul
))
1688 (replace-match counter nil nil prev-bul
))
1689 ;; No counter: increase, if needed, previous bullet.
1691 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct
)))
1692 ;; Alpha counter at first item: use counter.
1693 ((and counter
(org-list-use-alpha-bul-p item struct prevs
)
1694 (string-match "[A-Za-z]" counter
)
1695 (string-match "[A-Za-z]" bullet
))
1698 ((string-match "[a-z]" bullet
) (downcase counter
))
1699 ((string-match "[A-Z]" bullet
) (upcase counter
)))))
1700 (replace-match real-count nil nil bullet
)))
1701 ;; Num counter at first item: use counter.
1703 (string-match "[0-9]+" counter
)
1704 (string-match "[0-9]+" bullet
))
1705 (replace-match counter nil nil bullet
))
1706 ;; First bullet is alpha uppercase: use "A".
1707 ((and alphap
(string-match "[A-Z]" bullet
))
1708 (replace-match "A" nil nil bullet
))
1709 ;; First bullet is alpha lowercase: use "a".
1710 ((and alphap
(string-match "[a-z]" bullet
))
1711 (replace-match "a" nil nil bullet
))
1712 ;; First bullet is num: use "1".
1713 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet
)
1714 (replace-match "1" nil nil bullet
))
1715 ;; Not an ordered list: keep bullet.
1717 (mapc fix-bul
(mapcar #'car struct
))))
1719 (defun org-list-struct-fix-ind (struct parents
&optional bullet-size
)
1720 "Verify and correct indentation in STRUCT.
1722 PARENTS is the alist of parents, as returned by
1723 `org-list-parents-alist'.
1725 If numeric optional argument BULLET-SIZE is set, assume all
1726 bullets in list have this length to determine new indentation.
1728 This function modifies STRUCT."
1729 (let* ((ancestor (org-list-get-top-point struct
))
1730 (top-ind (org-list-get-ind ancestor struct
))
1733 (let ((parent (org-list-get-parent item struct parents
)))
1735 ;; Indent like parent + length of parent's bullet +
1738 item struct
(+ (or bullet-size
1740 (org-list-get-bullet parent struct
)))
1741 (org-list-get-ind parent struct
)
1742 org-list-indent-offset
))
1743 ;; If no parent, indent like top-point.
1744 (org-list-set-ind item struct top-ind
))))))
1745 (mapc new-ind
(mapcar #'car
(cdr struct
)))))
1747 (defun org-list-struct-fix-box (struct parents prevs
&optional ordered
)
1748 "Verify and correct checkboxes in STRUCT.
1750 PARENTS is the alist of parents and PREVS is the alist of
1751 previous items, as returned by, respectively,
1752 `org-list-parents-alist' and `org-list-prevs-alist'.
1754 If ORDERED is non-nil, a checkbox can only be checked when every
1755 checkbox before it is checked too. If there was an attempt to
1756 break this rule, the function will return the blocking item. In
1757 all others cases, the return value will be nil.
1759 This function modifies STRUCT."
1760 (let ((all-items (mapcar #'car struct
))
1765 (mapcar (lambda (child)
1766 (org-list-get-checkbox child struct
))
1767 (org-list-get-children item struct parents
))))
1768 (org-list-set-checkbox
1771 ((and (member "[ ]" box-list
) (member "[X]" box-list
)) "[-]")
1772 ((member "[-]" box-list
) "[-]")
1773 ((member "[X]" box-list
) "[X]")
1774 ((member "[ ]" box-list
) "[ ]")
1775 ;; Parent has no boxed child: leave box as-is.
1776 (t (org-list-get-checkbox item struct
))))))))
1778 ;; 1. List all parents with a checkbox.
1781 (let* ((parent (org-list-get-parent e struct parents
))
1782 (parent-box-p (org-list-get-checkbox parent struct
)))
1783 (when (and parent-box-p
(not (memq parent parent-list
)))
1784 (push parent parent-list
))))
1786 ;; 2. Sort those parents by decreasing indentation.
1787 (setq parent-list
(sort parent-list
1789 (> (org-list-get-ind e1 struct
)
1790 (org-list-get-ind e2 struct
)))))
1791 ;; 3. For each parent, get all children's checkboxes to determine
1792 ;; and set its checkbox accordingly.
1793 (mapc set-parent-box parent-list
)
1794 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1797 (mapcar (lambda (e) (org-list-get-checkbox e struct
)) all-items
))
1798 (after-unchecked (member "[ ]" box-list
)))
1799 ;; There are boxes checked after an unchecked one: fix that.
1800 (when (member "[X]" after-unchecked
)
1801 (let ((index (- (length struct
) (length after-unchecked
))))
1803 (when (org-list-get-checkbox e struct
)
1804 (org-list-set-checkbox e struct
"[ ]")))
1805 (nthcdr index all-items
))
1806 ;; Verify once again the structure, without ORDERED.
1807 (org-list-struct-fix-box struct parents prevs nil
)
1808 ;; Return blocking item.
1809 (nth index all-items
)))))))
1811 (defun org-list-struct-fix-item-end (struct)
1812 "Verify and correct each item end position in STRUCT.
1814 This function modifies STRUCT."
1815 (let (end-list acc-end
)
1817 (let* ((pos (car e
))
1818 (ind-pos (org-list-get-ind pos struct
))
1819 (end-pos (org-list-get-item-end pos struct
)))
1820 (unless (assq end-pos struct
)
1821 ;; To determine real ind of an ending position that is
1822 ;; not at an item, we have to find the item it belongs
1823 ;; to: it is the last item (ITEM-UP), whose ending is
1824 ;; further than the position we're interested in.
1825 (let ((item-up (assoc-default end-pos acc-end
'>)))
1827 ;; Else part is for the bottom point.
1828 (if item-up
(+ (org-list-get-ind item-up struct
) 2) 0)
1831 (push (cons ind-pos pos
) end-list
)
1832 (push (cons end-pos pos
) acc-end
)))
1834 (setq end-list
(sort end-list
(lambda (e1 e2
) (< (cdr e1
) (cdr e2
)))))
1835 (org-list-struct-assoc-end struct end-list
)))
1837 (defun org-list-struct-apply-struct (struct old-struct
)
1838 "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
1840 OLD-STRUCT is the structure before any modifications, and STRUCT
1841 the structure to be applied. The function will only modify parts
1842 of the list which have changed.
1844 Initial position of cursor is restored after the changes."
1845 (let* ((origin (point-marker))
1846 (inlinetask-re (and (featurep 'org-inlinetask
)
1847 (org-inlinetask-outline-regexp)))
1848 (item-re (org-item-re))
1851 ;; Shift the indentation between END and BEG by DELTA.
1852 ;; Start from the line before END.
1853 (lambda (end beg delta
)
1855 (skip-chars-backward " \r\t\n")
1857 (while (or (> (point) beg
)
1858 (and (= (point) beg
)
1859 (not (looking-at item-re
))))
1861 ;; Skip inline tasks.
1862 ((and inlinetask-re
(looking-at inlinetask-re
))
1863 (org-inlinetask-goto-beginning))
1864 ;; Shift only non-empty lines.
1865 ((looking-at-p "^[ \t]*\\S-")
1866 (indent-line-to (+ (org-get-indentation) delta
))))
1867 (forward-line -
1)))))
1870 ;; Replace ITEM first line elements with new elements from
1871 ;; STRUCT, if appropriate.
1874 (let* ((new-ind (org-list-get-ind item struct
))
1875 (old-ind (org-get-indentation))
1876 (new-bul (org-list-bullet-string
1877 (org-list-get-bullet item struct
)))
1878 (old-bul (org-list-get-bullet item old-struct
))
1879 (new-box (org-list-get-checkbox item struct
)))
1880 (looking-at org-list-full-item-re
)
1881 ;; a. Replace bullet
1882 (unless (equal old-bul new-bul
)
1883 (replace-match new-bul nil nil nil
1))
1884 ;; b. Replace checkbox.
1886 ((equal (match-string 3) new-box
))
1887 ((and (match-string 3) new-box
)
1888 (replace-match new-box nil nil nil
3))
1890 (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
1891 (replace-match "" nil nil nil
1))
1892 (t (let ((counterp (match-end 2)))
1893 (goto-char (if counterp
(1+ counterp
) (match-end 1)))
1894 (insert (concat new-box
(unless counterp
" "))))))
1895 ;; c. Indent item to appropriate column.
1896 (unless (= new-ind old-ind
)
1897 (delete-region (goto-char (point-at-bol))
1898 (progn (skip-chars-forward " \t") (point)))
1899 (indent-to new-ind
)))))))
1900 ;; 1. First get list of items and position endings. We maintain
1901 ;; two alists: ITM-SHIFT, determining indentation shift needed
1902 ;; at item, and END-LIST, a pseudo-alist where key is ending
1903 ;; position and value point.
1904 (let (end-list acc-end itm-shift all-ends sliced-struct
)
1905 (dolist (e old-struct
)
1906 (let* ((pos (car e
))
1907 (ind-pos (org-list-get-ind pos struct
))
1908 (ind-old (org-list-get-ind pos old-struct
))
1909 (bul-pos (org-list-get-bullet pos struct
))
1910 (bul-old (org-list-get-bullet pos old-struct
))
1911 (ind-shift (- (+ ind-pos
(length bul-pos
))
1912 (+ ind-old
(length bul-old
))))
1913 (end-pos (org-list-get-item-end pos old-struct
)))
1914 (push (cons pos ind-shift
) itm-shift
)
1915 (unless (assq end-pos old-struct
)
1916 ;; To determine real ind of an ending position that
1917 ;; is not at an item, we have to find the item it
1918 ;; belongs to: it is the last item (ITEM-UP), whose
1919 ;; ending is further than the position we're
1921 (let ((item-up (assoc-default end-pos acc-end
#'>)))
1922 (push (cons end-pos item-up
) end-list
)))
1923 (push (cons end-pos pos
) acc-end
)))
1924 ;; 2. Slice the items into parts that should be shifted by the
1925 ;; same amount of indentation. Each slice follow the pattern
1926 ;; (END BEG DELTA). Slices are returned in reverse order.
1927 (setq all-ends
(sort (append (mapcar #'car itm-shift
)
1928 (org-uniquify (mapcar #'car end-list
)))
1930 acc-end
(nreverse acc-end
))
1931 (while (cdr all-ends
)
1932 (let* ((up (pop all-ends
))
1933 (down (car all-ends
))
1934 (itemp (assq up struct
))
1936 (if itemp
(cdr (assq up itm-shift
))
1937 ;; If we're not at an item, there's a child of the
1938 ;; item point belongs to above. Make sure the less
1939 ;; indented line in this slice has the same column
1941 (let* ((child (cdr (assq up acc-end
)))
1942 (ind (org-list-get-ind child struct
))
1943 (min-ind most-positive-fixnum
))
1946 (while (< (point) down
)
1947 ;; Ignore empty lines. Also ignore blocks and
1948 ;; drawers contents.
1949 (unless (looking-at-p "[ \t]*$")
1950 (setq min-ind
(min (org-get-indentation) min-ind
))
1952 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1954 (format "^[ \t]*#\\+END%s[ \t]*$"
1957 ((and (looking-at org-drawer-regexp
)
1958 (re-search-forward "^[ \t]*:END:[ \t]*$"
1962 (push (list down up delta
) sliced-struct
)))
1963 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1964 ;; end to beginning. Take a special action when beginning is
1966 (dolist (e sliced-struct
)
1967 (unless (zerop (nth 2 e
)) (apply shift-body-ind e
))
1968 (let* ((beg (nth 1 e
))
1969 (cell (assq beg struct
)))
1970 (unless (or (not cell
) (equal cell
(assq beg old-struct
)))
1971 (funcall modify-item beg
)))))
1972 ;; 4. Go back to initial position and clean marker.
1974 (move-marker origin nil
)))
1976 (defun org-list-write-struct (struct parents
&optional old-struct
)
1977 "Correct bullets, checkboxes and indentation in list at point.
1979 STRUCT is the list structure. PARENTS is the alist of parents,
1980 as returned by `org-list-parents-alist'.
1982 When non-nil, optional argument OLD-STRUCT is the reference
1983 structure of the list. It should be provided whenever STRUCT
1984 doesn't correspond anymore to the real list in buffer."
1985 ;; Order of functions matters here: checkboxes and endings need
1986 ;; correct indentation to be set, and indentation needs correct
1989 ;; 0. Save a copy of structure before modifications
1990 (let ((old-struct (or old-struct
(copy-tree struct
))))
1991 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
1992 ;; order to get items endings and bullets properly
1993 (org-list-struct-fix-ind struct parents
2)
1994 ;; 2. Fix each item end to get correct prevs alist.
1995 (org-list-struct-fix-item-end struct
)
1996 ;; 3. Get bullets right.
1997 (let ((prevs (org-list-prevs-alist struct
)))
1998 (org-list-struct-fix-bul struct prevs
)
1999 ;; 4. Now get real indentation.
2000 (org-list-struct-fix-ind struct parents
)
2001 ;; 5. Eventually fix checkboxes.
2002 (org-list-struct-fix-box struct parents prevs
))
2003 ;; 6. Apply structure modifications to buffer.
2004 (org-list-struct-apply-struct struct old-struct
)))
2010 (defun org-apply-on-list (function init-value
&rest args
)
2011 "Call FUNCTION on each item of the list at point.
2012 FUNCTION must be called with at least one argument: INIT-VALUE,
2013 that will contain the value returned by the function at the
2014 previous item, plus ARGS extra arguments.
2016 FUNCTION is applied on items in reverse order.
2018 As an example, \(org-apply-on-list \(lambda \(result) \(1+ result)) 0)
2019 will return the number of items in the current list.
2021 Sublists of the list are skipped. Cursor is always at the
2022 beginning of the item."
2023 (let* ((struct (org-list-struct))
2024 (prevs (org-list-prevs-alist struct
))
2025 (item (copy-marker (point-at-bol)))
2026 (all (org-list-get-all-items (marker-position item
) struct prevs
))
2030 (setq value
(apply function value args
)))
2033 (move-marker item nil
)
2036 (defun org-list-set-item-visibility (item struct view
)
2037 "Set visibility of ITEM in STRUCT to VIEW.
2039 Possible values are: `folded', `children' or `subtree'. See
2040 `org-cycle' for more information."
2043 (let ((item-end (org-list-get-item-end-before-blank item struct
)))
2045 (org-flag-region (save-excursion (goto-char item
) (line-end-position))
2046 item-end t
'outline
)))
2047 ((eq view
'children
)
2048 ;; First show everything.
2049 (org-list-set-item-visibility item struct
'subtree
)
2050 ;; Then fold every child.
2051 (let* ((parents (org-list-parents-alist struct
))
2052 (children (org-list-get-children item struct parents
)))
2054 (org-list-set-item-visibility e struct
'folded
))
2058 (let ((item-end (org-list-get-item-end item struct
)))
2059 (org-flag-region item item-end nil
'outline
)))))
2061 (defun org-list-item-body-column (item)
2062 "Return column at which body of ITEM should start."
2068 "[ \t]::\\([ \t]\\|$\\)" (line-beginning-position) t
))
2069 ;; Descriptive list item. Body starts after item's tag, if
2071 (let ((start (1+ (- (match-beginning 1) (line-beginning-position))))
2072 (ind (org-get-indentation)))
2073 (if (> start
(+ ind org-list-description-max-indent
))
2076 ;; Regular item. Body starts after bullet.
2077 (looking-at "[ \t]*\\(\\S-+\\)")
2078 (+ (progn (goto-char (match-end 1)) (current-column))
2079 (if (and org-list-two-spaces-after-bullet-regexp
2080 (string-match-p org-list-two-spaces-after-bullet-regexp
2087 ;;; Interactive functions
2089 (defalias 'org-list-get-item-begin
'org-in-item-p
)
2091 (defun org-beginning-of-item ()
2092 "Go to the beginning of the current item.
2093 Throw an error when not in a list."
2095 (let ((begin (org-in-item-p)))
2096 (if begin
(goto-char begin
) (error "Not in an item"))))
2098 (defun org-beginning-of-item-list ()
2099 "Go to the beginning item of the current list or sublist.
2100 Throw an error when not in a list."
2102 (let ((begin (org-in-item-p)))
2104 (error "Not in an item")
2106 (let* ((struct (org-list-struct))
2107 (prevs (org-list-prevs-alist struct
)))
2108 (goto-char (org-list-get-list-begin begin struct prevs
))))))
2110 (defun org-end-of-item-list ()
2111 "Go to the end of the current list or sublist.
2112 Throw an error when not in a list."
2114 (let ((begin (org-in-item-p)))
2116 (error "Not in an item")
2118 (let* ((struct (org-list-struct))
2119 (prevs (org-list-prevs-alist struct
)))
2120 (goto-char (org-list-get-list-end begin struct prevs
))))))
2122 (defun org-end-of-item ()
2123 "Go to the end of the current item.
2124 Throw an error when not in a list."
2126 (let ((begin (org-in-item-p)))
2128 (error "Not in an item")
2130 (let ((struct (org-list-struct)))
2131 (goto-char (org-list-get-item-end begin struct
))))))
2133 (defun org-previous-item ()
2134 "Move to the beginning of the previous item.
2135 Throw an error when not in a list. Also throw an error when at
2136 first item, unless `org-list-use-circular-motion' is non-nil."
2138 (let ((item (org-in-item-p)))
2140 (error "Not in an item")
2142 (let* ((struct (org-list-struct))
2143 (prevs (org-list-prevs-alist struct
))
2144 (prevp (org-list-get-prev-item item struct prevs
)))
2146 (prevp (goto-char prevp
))
2147 (org-list-use-circular-motion
2148 (goto-char (org-list-get-last-item item struct prevs
)))
2149 (t (error "On first item")))))))
2151 (defun org-next-item ()
2152 "Move to the beginning of the next item.
2153 Throw an error when not in a list. Also throw an error when at
2154 last item, unless `org-list-use-circular-motion' is non-nil."
2156 (let ((item (org-in-item-p)))
2158 (error "Not in an item")
2160 (let* ((struct (org-list-struct))
2161 (prevs (org-list-prevs-alist struct
))
2162 (prevp (org-list-get-next-item item struct prevs
)))
2164 (prevp (goto-char prevp
))
2165 (org-list-use-circular-motion
2166 (goto-char (org-list-get-first-item item struct prevs
)))
2167 (t (error "On last item")))))))
2169 (defun org-move-item-down ()
2170 "Move the item at point down, i.e. swap with following item.
2171 Sub-items (items with larger indentation) are considered part of
2172 the item, so this really moves item trees."
2174 (unless (org-at-item-p) (error "Not at an item"))
2175 (let* ((col (current-column))
2176 (item (point-at-bol))
2177 (struct (org-list-struct))
2178 (prevs (org-list-prevs-alist struct
))
2179 (next-item (org-list-get-next-item (point-at-bol) struct prevs
)))
2180 (unless (or next-item org-list-use-circular-motion
)
2181 (user-error "Cannot move this item further down"))
2183 (setq struct
(org-list-send-item item
'begin struct
))
2184 (setq struct
(org-list-swap-items item next-item struct
))
2186 (org-list-get-next-item item struct
(org-list-prevs-alist struct
))))
2187 (org-list-write-struct struct
(org-list-parents-alist struct
))
2188 (org-move-to-column col
)))
2190 (defun org-move-item-up ()
2191 "Move the item at point up, i.e. swap with previous item.
2192 Sub-items (items with larger indentation) are considered part of
2193 the item, so this really moves item trees."
2195 (unless (org-at-item-p) (error "Not at an item"))
2196 (let* ((col (current-column))
2197 (item (point-at-bol))
2198 (struct (org-list-struct))
2199 (prevs (org-list-prevs-alist struct
))
2200 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs
)))
2201 (unless (or prev-item org-list-use-circular-motion
)
2202 (user-error "Cannot move this item further up"))
2204 (setq struct
(org-list-send-item item
'end struct
))
2205 (setq struct
(org-list-swap-items prev-item item struct
)))
2206 (org-list-write-struct struct
(org-list-parents-alist struct
))
2207 (org-move-to-column col
)))
2209 (defun org-insert-item (&optional checkbox
)
2210 "Insert a new item at the current level.
2211 If cursor is before first character after bullet of the item, the
2212 new item will be created before the current one.
2214 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2216 Return t when things worked, nil when we are not in an item, or
2219 (let ((itemp (org-in-item-p))
2221 ;; If cursor isn't is a list or if list is invisible, return nil.
2222 (unless (or (not itemp
)
2228 (org-at-item-timer-p))
2229 ;; Timer list: delegate to `org-timer-item'.
2230 (progn (org-timer-item) t
)
2231 (let* ((struct (save-excursion (goto-char itemp
)
2233 (prevs (org-list-prevs-alist struct
))
2234 ;; If we're in a description list, ask for the new term.
2235 (desc (when (eq (org-list-get-list-type itemp struct prevs
)
2238 (setq struct
(org-list-insert-item pos struct prevs checkbox desc
))
2239 (org-list-write-struct struct
(org-list-parents-alist struct
))
2240 (when checkbox
(org-update-checkbox-count-maybe))
2241 (looking-at org-list-full-item-re
)
2242 (goto-char (if (and (match-beginning 4)
2244 (string-match "[.)]" (match-string 1))))
2247 (if desc
(backward-char 1))
2250 (defun org-list-repair ()
2251 "Fix indentation, bullets and checkboxes in the list at point."
2253 (unless (org-at-item-p) (error "This is not a list"))
2254 (let* ((struct (org-list-struct))
2255 (parents (org-list-parents-alist struct
)))
2256 (org-list-write-struct struct parents
)))
2258 (defun org-cycle-list-bullet (&optional which
)
2259 "Cycle through the different itemize/enumerate bullets.
2260 This cycle the entire list level through the sequence:
2262 `-' -> `+' -> `*' -> `1.' -> `1)'
2264 If WHICH is a valid string, use that as the new bullet. If WHICH
2265 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2266 `previous', cycle backwards."
2268 (unless (org-at-item-p) (error "Not at an item"))
2271 (let* ((struct (org-list-struct))
2272 (parents (org-list-parents-alist struct
))
2273 (prevs (org-list-prevs-alist struct
))
2274 (list-beg (org-list-get-first-item (point) struct prevs
))
2275 (bullet (org-list-get-bullet list-beg struct
))
2276 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs
))
2277 (case-fold-search nil
)
2279 ((string-match "[a-z]\\." bullet
) "a.")
2280 ((string-match "[a-z])" bullet
) "a)")
2281 ((string-match "[A-Z]\\." bullet
) "A.")
2282 ((string-match "[A-Z])" bullet
) "A)")
2283 ((string-match "\\." bullet
) "1.")
2284 ((string-match ")" bullet
) "1)")
2285 (t (org-trim bullet
))))
2286 ;; Compute list of possible bullets, depending on context.
2289 ;; *-bullets are not allowed at column 0.
2290 (unless (looking-at "\\S-") '("*"))
2291 ;; Description items cannot be numbered.
2292 (unless (or (eq org-plain-list-ordered-item-terminator ?\
))
2293 (org-at-item-description-p))
2295 (unless (or (eq org-plain-list-ordered-item-terminator ?.
)
2296 (org-at-item-description-p))
2298 (unless (or (not alpha-p
)
2299 (eq org-plain-list-ordered-item-terminator ?\
))
2300 (org-at-item-description-p))
2302 (unless (or (not alpha-p
)
2303 (eq org-plain-list-ordered-item-terminator ?.
)
2304 (org-at-item-description-p))
2306 (len (length bullet-list
))
2307 (item-index (- len
(length (member current bullet-list
))))
2308 (get-value (lambda (index) (nth (mod index len
) bullet-list
)))
2310 ((member which bullet-list
) which
)
2311 ((numberp which
) (funcall get-value which
))
2312 ((eq 'previous which
) (funcall get-value
(1- item-index
)))
2313 (t (funcall get-value
(1+ item-index
))))))
2314 ;; Use a short variation of `org-list-write-struct' as there's
2315 ;; no need to go through all the steps.
2316 (let ((old-struct (copy-tree struct
)))
2317 (org-list-set-bullet list-beg struct
(org-list-bullet-string new
))
2318 (org-list-struct-fix-bul struct prevs
)
2319 (org-list-struct-fix-ind struct parents
)
2320 (org-list-struct-apply-struct struct old-struct
)))))
2322 (defun org-toggle-checkbox (&optional toggle-presence
)
2323 "Toggle the checkbox in the current line.
2325 With prefix argument TOGGLE-PRESENCE, add or remove checkboxes.
2326 With a double prefix argument, set the checkbox to \"[-]\".
2328 When there is an active region, toggle status or presence of the
2329 first checkbox there, and make every item inside have the same
2330 status or presence, respectively.
2332 If point is on a headline, apply this to all checkbox items in
2333 the text below the heading, taking as reference the first item in
2334 subtree, ignoring planning line and any drawer following it."
2341 (orderedp (org-entry-get nil
"ORDERED"))
2343 ;; In a region, start at first item in region.
2345 ((org-region-active-p)
2346 (let ((limit (region-end)))
2347 (goto-char (region-beginning))
2348 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2349 (setq lim-up
(point-at-bol))
2350 (error "No item in region"))
2351 (setq lim-down
(copy-marker limit
))))
2353 ;; On a heading, start at first item after drawers and
2354 ;; time-stamps (scheduled, etc.).
2355 (let ((limit (save-excursion (outline-next-heading) (point))))
2356 (org-end-of-meta-data t
)
2357 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2358 (setq lim-up
(point-at-bol))
2359 (error "No item in subtree"))
2360 (setq lim-down
(copy-marker limit
))))
2361 ;; Just one item: set SINGLEP flag.
2364 (setq lim-up
(point-at-bol)
2365 lim-down
(copy-marker (point-at-eol))))
2366 (t (error "Not at an item or heading, and no active region"))))
2367 ;; Determine the checkbox going to be applied to all items
2372 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2374 ((equal toggle-presence
'(16)) "[-]")
2375 ((equal toggle-presence
'(4))
2376 (unless cbox
"[ ]"))
2377 ((equal "[X]" cbox
) "[ ]")
2379 ;; When an item is found within bounds, grab the full list at
2380 ;; point structure, then: (1) set check-box of all its items
2381 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2382 ;; whole list, (3) move point after the list.
2384 (while (and (< (point) lim-down
)
2385 (org-list-search-forward (org-item-beginning-re)
2387 (let* ((struct (org-list-struct))
2388 (struct-copy (copy-tree struct
))
2389 (parents (org-list-parents-alist struct
))
2390 (prevs (org-list-prevs-alist struct
))
2391 (bottom (copy-marker (org-list-get-bottom-point struct
)))
2392 (items-to-toggle (cl-remove-if
2393 (lambda (e) (or (< e lim-up
) (> e lim-down
)))
2394 (mapcar #'car struct
))))
2395 (mapc (lambda (e) (org-list-set-checkbox
2397 ;; If there is no box at item, leave as-is
2398 ;; unless function was called with C-u prefix.
2399 (let ((cur-box (org-list-get-checkbox e struct
)))
2400 (if (or cur-box
(equal toggle-presence
'(4)))
2404 (setq block-item
(org-list-struct-fix-box
2405 struct parents prevs orderedp
))
2406 ;; Report some problems due to ORDERED status of subtree.
2407 ;; If only one box was being checked, throw an error, else,
2408 ;; only signal problems.
2410 ((and singlep block-item
(> lim-up block-item
))
2412 "Checkbox blocked because of unchecked box at line %d"
2413 (org-current-line block-item
)))
2416 "Checkboxes were removed due to unchecked box at line %d"
2417 (org-current-line block-item
))))
2419 (move-marker bottom nil
)
2420 (org-list-struct-apply-struct struct struct-copy
)))
2421 (move-marker lim-down nil
)))
2422 (org-update-checkbox-count-maybe))
2424 (defun org-reset-checkbox-state-subtree ()
2425 "Reset all checkboxes in an entry subtree."
2427 (if (org-before-first-heading-p)
2428 (error "Not inside a tree")
2431 (org-narrow-to-subtree)
2433 (goto-char (point-min))
2434 (let ((end (point-max)))
2435 (while (< (point) end
)
2436 (when (org-at-item-checkbox-p)
2437 (replace-match "[ ]" t t nil
1))
2438 (beginning-of-line 2)))
2439 (org-update-checkbox-count-maybe 'all
)))))
2441 (defun org-update-checkbox-count (&optional all
)
2442 "Update the checkbox statistics in the current section.
2444 This will find all statistic cookies like [57%] and [6/12] and
2445 update them with the current numbers.
2447 With optional prefix argument ALL, do this for the whole buffer."
2449 (org-with-wide-buffer
2450 (let* ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2451 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\
2452 \\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2454 (or (not org-checkbox-hierarchical-statistics
)
2455 (string-match "\\<recursive\\>"
2456 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2457 (within-inlinetask (and (not all
)
2458 (featurep 'org-inlinetask
)
2459 (org-inlinetask-in-task-p)))
2460 (end (cond (all (point-max))
2462 (save-excursion (outline-next-heading) (point)))
2464 (org-with-limited-levels (outline-next-heading))
2467 (lambda (item structs recursivep
)
2468 ;; Return number of checked boxes and boxes of all types
2469 ;; in all structures in STRUCTS. If RECURSIVEP is
2470 ;; non-nil, also count boxes in sub-lists. If ITEM is
2471 ;; nil, count across the whole structure, else count only
2472 ;; across subtree whose ancestor is ITEM.
2473 (let ((c-on 0) (c-all 0))
2474 (dolist (s structs
(list c-on c-all
))
2475 (let* ((pre (org-list-prevs-alist s
))
2476 (par (org-list-parents-alist s
))
2479 ((and recursivep item
) (org-list-get-subtree item s
))
2480 (recursivep (mapcar #'car s
))
2481 (item (org-list-get-children item s par
))
2482 (t (org-list-get-all-items
2483 (org-list-get-top-point s
) s pre
))))
2484 (cookies (delq nil
(mapcar
2486 (org-list-get-checkbox e s
))
2488 (cl-incf c-all
(length cookies
))
2489 (cl-incf c-on
(cl-count "[X]" cookies
:test
#'equal
)))))))
2492 (cond (all (goto-char (point-min)))
2493 (within-inlinetask (org-back-to-heading t
))
2494 (t (org-with-limited-levels (outline-previous-heading))))
2495 ;; Build an alist for each cookie found. The key is the position
2496 ;; at beginning of cookie and values ending position, format of
2497 ;; cookie, number of checked boxes to report and total number of
2499 (while (re-search-forward cookie-re end t
)
2500 (let ((context (save-excursion (backward-char)
2501 (save-match-data (org-element-context)))))
2502 (when (eq (org-element-type context
) 'statistics-cookie
)
2505 (list (match-beginning 1) (match-end 1) (match-end 2))
2507 (org-element-lineage
2509 '(drawer center-block dynamic-block inlinetask item
2510 quote-block special-block verse-block
)))
2512 (org-element-property :contents-begin container
)
2514 (org-with-limited-levels
2515 (outline-previous-heading))
2517 (or (cdr (assq beg cache
))
2522 (org-element-property :contents-end container
)
2524 (org-with-limited-levels (outline-next-heading))
2527 (while (re-search-forward box-re end t
)
2528 (let ((element (org-element-at-point)))
2529 (when (eq (org-element-type element
) 'item
)
2530 (push (org-element-property :structure element
)
2532 ;; Skip whole list since we have its
2533 ;; structure anyway.
2534 (while (setq element
(org-element-lineage
2535 element
'(plain-list)))
2537 (min (org-element-property :end element
)
2539 ;; Cache count for cookies applying to the same
2540 ;; area. Then return it.
2542 (funcall count-boxes
2543 (and (eq (org-element-type container
)
2545 (org-element-property
2549 (push (cons beg count
) cache
)
2552 ;; Apply alist to buffer.
2553 (dolist (cookie cookies-list
)
2554 (let* ((beg (car cookie
))
2555 (end (nth 1 cookie
))
2556 (percent (nth 2 cookie
))
2557 (checked (nth 3 cookie
))
2558 (total (nth 4 cookie
)))
2561 (if percent
(format "[%d%%]" (floor (* 100.0 checked
)
2563 (format "[%d/%d]" checked total
)))
2564 (delete-region (point) (+ (point) (- end beg
)))
2565 (when org-auto-align-tags
(org-fix-tags-on-the-fly)))))))
2567 (defun org-get-checkbox-statistics-face ()
2568 "Select the face for checkbox statistics.
2569 The face will be `org-done' when all relevant boxes are checked.
2570 Otherwise it will be `org-todo'."
2572 (if (equal (match-string 1) "100%")
2573 'org-checkbox-statistics-done
2574 'org-checkbox-statistics-todo
)
2575 (if (and (> (match-end 2) (match-beginning 2))
2576 (equal (match-string 2) (match-string 3)))
2577 'org-checkbox-statistics-done
2578 'org-checkbox-statistics-todo
)))
2580 (defun org-update-checkbox-count-maybe (&optional all
)
2581 "Update checkbox statistics unless turned off by user.
2582 With an optional argument ALL, update them in the whole buffer."
2583 (when (cdr (assq 'checkbox org-list-automatic-rules
))
2584 (org-update-checkbox-count all
))
2585 (run-hooks 'org-checkbox-statistics-hook
))
2587 (defvar org-last-indent-begin-marker
(make-marker))
2588 (defvar org-last-indent-end-marker
(make-marker))
2589 (defun org-list-indent-item-generic (arg no-subtree struct
)
2590 "Indent a local list item including its children.
2591 When number ARG is a negative, item will be outdented, otherwise
2592 it will be indented.
2594 If a region is active, all items inside will be moved.
2596 If NO-SUBTREE is non-nil, only indent the item itself, not its
2599 STRUCT is the list structure.
2601 Return t if successful."
2603 (let* ((regionp (org-region-active-p))
2604 (rbeg (and regionp
(region-beginning)))
2605 (rend (and regionp
(region-end)))
2606 (top (org-list-get-top-point struct
))
2607 (parents (org-list-parents-alist struct
))
2608 (prevs (org-list-prevs-alist struct
))
2609 ;; Are we going to move the whole list?
2612 (= top
(point-at-bol))
2613 (cdr (assq 'indent org-list-automatic-rules
))
2616 "First item of list cannot move without its subtree")
2618 ;; Determine begin and end points of zone to indent. If moving
2619 ;; more than one item, save them for subsequent moves.
2620 (unless (and (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
2621 (memq this-command
'(org-shiftmetaright org-shiftmetaleft
)))
2624 (set-marker org-last-indent-begin-marker rbeg
)
2625 (set-marker org-last-indent-end-marker rend
))
2626 (set-marker org-last-indent-begin-marker
(point-at-bol))
2627 (set-marker org-last-indent-end-marker
2629 (specialp (org-list-get-bottom-point struct
))
2630 (no-subtree (1+ (point-at-bol)))
2631 (t (org-list-get-item-end (point-at-bol) struct
))))))
2632 (let* ((beg (marker-position org-last-indent-begin-marker
))
2633 (end (marker-position org-last-indent-end-marker
)))
2635 ;; Special case: moving top-item with indent rule.
2637 (let* ((level-skip (org-level-increment))
2638 (offset (if (< arg
0) (- level-skip
) level-skip
))
2639 (top-ind (org-list-get-ind beg struct
))
2640 (old-struct (copy-tree struct
)))
2641 (if (< (+ top-ind offset
) 0)
2642 (error "Cannot outdent beyond margin")
2643 ;; Change bullet if necessary.
2644 (when (and (= (+ top-ind offset
) 0)
2646 (org-list-get-bullet beg struct
)))
2647 (org-list-set-bullet beg struct
2648 (org-list-bullet-string "-")))
2649 ;; Shift every item by OFFSET and fix bullets. Then
2650 ;; apply changes to buffer.
2652 (let ((ind (org-list-get-ind (car e
) struct
)))
2653 (org-list-set-ind (car e
) struct
(+ ind offset
))))
2655 (org-list-struct-fix-bul struct prevs
)
2656 (org-list-struct-apply-struct struct old-struct
))))
2659 ;; If only one item is moved, it mustn't have a child.
2662 (org-list-has-child-p beg struct
))
2663 ;; If a subtree or region is moved, the last item
2664 ;; of the subtree mustn't have a child.
2665 (let ((last-item (caar
2668 (lambda (e) (>= (car e
) end
))
2670 (org-list-has-child-p last-item struct
))))
2671 (error "Cannot outdent an item without its children"))
2676 (org-list-struct-outdent beg end struct parents
)
2677 (org-list-struct-indent beg end struct parents prevs
))))
2678 (org-list-write-struct struct new-parents
))
2679 (org-update-checkbox-count-maybe))))))
2682 (defun org-outdent-item ()
2683 "Outdent a local list item, but not its children.
2684 If a region is active, all items inside will be moved."
2686 (let ((regionp (org-region-active-p)))
2688 ((or (org-at-item-p)
2690 (save-excursion (goto-char (region-beginning))
2692 (let ((struct (if (not regionp
) (org-list-struct)
2693 (save-excursion (goto-char (region-beginning))
2694 (org-list-struct)))))
2695 (org-list-indent-item-generic -
1 t struct
)))
2696 (regionp (error "Region not starting at an item"))
2697 (t (error "Not at an item")))))
2699 (defun org-indent-item ()
2700 "Indent a local list item, but not its children.
2701 If a region is active, all items inside will be moved."
2703 (let ((regionp (org-region-active-p)))
2705 ((or (org-at-item-p)
2707 (save-excursion (goto-char (region-beginning))
2709 (let ((struct (if (not regionp
) (org-list-struct)
2710 (save-excursion (goto-char (region-beginning))
2711 (org-list-struct)))))
2712 (org-list-indent-item-generic 1 t struct
)))
2713 (regionp (error "Region not starting at an item"))
2714 (t (error "Not at an item")))))
2716 (defun org-outdent-item-tree ()
2717 "Outdent a local list item including its children.
2718 If a region is active, all items inside will be moved."
2720 (let ((regionp (org-region-active-p)))
2722 ((or (org-at-item-p)
2724 (save-excursion (goto-char (region-beginning))
2726 (let ((struct (if (not regionp
) (org-list-struct)
2727 (save-excursion (goto-char (region-beginning))
2728 (org-list-struct)))))
2729 (org-list-indent-item-generic -
1 nil struct
)))
2730 (regionp (error "Region not starting at an item"))
2731 (t (error "Not at an item")))))
2733 (defun org-indent-item-tree ()
2734 "Indent a local list item including its children.
2735 If a region is active, all items inside will be moved."
2737 (let ((regionp (org-region-active-p)))
2739 ((or (org-at-item-p)
2741 (save-excursion (goto-char (region-beginning))
2743 (let ((struct (if (not regionp
) (org-list-struct)
2744 (save-excursion (goto-char (region-beginning))
2745 (org-list-struct)))))
2746 (org-list-indent-item-generic 1 nil struct
)))
2747 (regionp (error "Region not starting at an item"))
2748 (t (error "Not at an item")))))
2750 (defvar org-tab-ind-state
)
2751 (defvar org-adapt-indentation
)
2752 (defun org-cycle-item-indentation ()
2753 "Cycle levels of indentation of an empty item.
2754 The first run indents the item, if applicable. Subsequent runs
2755 outdent it at meaningful levels in the list. When done, item is
2756 put back at its original position with its original bullet.
2758 Return t at each successful move."
2759 (when (org-at-item-p)
2760 (let* ((org-adapt-indentation nil
)
2761 (struct (org-list-struct))
2762 (ind (org-list-get-ind (point-at-bol) struct
))
2763 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2764 ;; Accept empty items or if cycle has already started.
2765 (when (or (eq last-command
'org-cycle-item-indentation
)
2766 (and (save-excursion
2768 (looking-at org-list-full-item-re
))
2769 (>= (match-end 0) (save-excursion
2770 (goto-char (org-list-get-item-end
2771 (point-at-bol) struct
))
2772 (skip-chars-backward " \r\t\n")
2774 (setq this-command
'org-cycle-item-indentation
)
2775 ;; When in the middle of the cycle, try to outdent first. If
2776 ;; it fails, and point is still at initial position, indent.
2777 ;; Else, re-create it at its original position.
2778 (if (eq last-command
'org-cycle-item-indentation
)
2780 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2781 ((and (= ind
(car org-tab-ind-state
))
2782 (ignore-errors (org-list-indent-item-generic 1 t struct
))))
2783 (t (delete-region (point-at-bol) (point-at-eol))
2784 (indent-to-column (car org-tab-ind-state
))
2785 (insert (cdr org-tab-ind-state
) " ")
2787 (setq this-command
'identity
)))
2788 ;; If a cycle is starting, remember indentation and bullet,
2789 ;; then try to indent. If it fails, try to outdent.
2790 (setq org-tab-ind-state
(cons ind bullet
))
2792 ((ignore-errors (org-list-indent-item-generic 1 t struct
)))
2793 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2794 (t (user-error "Cannot move item"))))
2797 (defun org-sort-list
2798 (&optional with-case sorting-type getkey-func compare-func interactive?
)
2800 The cursor may be at any item of the list that should be sorted.
2801 Sublists are not sorted. Checkboxes, if any, are ignored.
2803 Sorting can be alphabetically, numerically, by date/time as given
2804 by a time stamp, by a property or by priority.
2806 Comparing entries ignores case by default. However, with an
2807 optional argument WITH-CASE, the sorting considers case as well.
2809 The command prompts for the sorting type unless it has been given
2810 to the function through the SORTING-TYPE argument, which needs to
2811 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F ?x ?X). Here is the
2812 detailed meaning of each character:
2814 n Numerically, by converting the beginning of the item to a number.
2815 a Alphabetically. Only the first line of item is checked.
2816 t By date/time, either the first active time stamp in the entry, if
2817 any, or by the first inactive one. In a timer list, sort the timers.
2818 x By \"checked\" status of a check list.
2820 Capital letters will reverse the sort order.
2822 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2823 a function to be called with point at the beginning of the
2824 record. It must return a value that is compatible with COMPARE-FUNC,
2825 the function used to compare entries.
2827 Sorting is done against the visible part of the headlines, it
2828 ignores hidden links.
2830 A non-nil value for INTERACTIVE? is used to signal that this
2831 function is being called interactively."
2832 (interactive (list current-prefix-arg nil nil nil t
))
2833 (let* ((case-func (if with-case
'identity
'downcase
))
2834 (struct (org-list-struct))
2835 (prevs (org-list-prevs-alist struct
))
2836 (start (org-list-get-list-begin (point-at-bol) struct prevs
))
2837 (end (org-list-get-list-end (point-at-bol) struct prevs
))
2842 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc [x]checked A/N/T/F/X means reversed:")
2843 (read-char-exclusive))))
2844 (dcst (downcase sorting-type
))
2849 (org-read-function "Function for extracting keys: "))
2850 (error "Missing key extractor"))))
2853 ((= dcst ?a
) #'string
<)
2858 (concat "Function for comparing keys "
2859 "(empty for default `sort-subr' predicate): ")
2862 ((= dcst ?x
) #'string
<))))
2863 (message "Sorting items...")
2865 (narrow-to-region start end
)
2866 (goto-char (point-min))
2867 (let* ((case-fold-search nil
)
2868 (now (current-time))
2869 (next-record (lambda ()
2870 (skip-chars-forward " \r\t\n")
2871 (or (eobp) (beginning-of-line))))
2872 (end-record (lambda ()
2873 (goto-char (org-list-get-item-end-before-blank
2877 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2881 (org-sort-remove-invisible
2882 (buffer-substring (match-end 0) (point-at-eol)))))
2885 (org-sort-remove-invisible
2887 (match-end 0) (point-at-eol)))))
2890 ;; If it is a timer list, convert timer to seconds
2891 ((org-at-item-timer-p)
2892 (org-timer-hms-to-secs (match-string 1)))
2893 ((or (save-excursion
2894 (re-search-forward org-ts-regexp
(point-at-eol) t
))
2895 (save-excursion (re-search-forward org-ts-regexp-both
2897 (org-time-string-to-seconds (match-string 0)))
2898 (t (float-time now
))))
2899 ((= dcst ?x
) (or (and (stringp (match-string 1))
2904 (let ((value (funcall getkey-func
)))
2906 (funcall case-func value
)
2908 (error "Invalid key function `%s'" getkey-func
)))
2909 (t (error "Invalid sorting type `%c'" sorting-type
)))))))
2910 (sort-subr (/= dcst sorting-type
)
2916 ;; Read and fix list again, as `sort-subr' probably destroyed
2919 (run-hooks 'org-after-sorting-entries-or-items-hook
)
2920 (message "Sorting items...done")))))
2922 (defun org-toggle-item (arg)
2923 "Convert headings or normal lines to items, items to normal lines.
2924 If there is no active region, only the current line is considered.
2926 If the first non blank line in the region is a headline, convert
2927 all headlines to items, shifting text accordingly.
2929 If it is an item, convert all items to normal lines.
2931 If it is normal text, change region into a list of items.
2932 With a prefix argument ARG, change the region in a single item."
2936 ;; Shift text in current section to IND, from point to END.
2937 ;; The function leaves point to END line.
2938 (let ((min-i 1000) (end (copy-marker end
)))
2939 ;; First determine the minimum indentation (MIN-I) of
2943 (while (< (point) end
)
2944 (let ((i (org-get-indentation)))
2946 ;; Skip blank lines and inline tasks.
2947 ((looking-at "^[ \t]*$"))
2948 ((looking-at org-outline-regexp-bol
))
2949 ;; We can't find less than 0 indentation.
2950 ((zerop i
) (throw 'exit
(setq min-i
0)))
2951 ((< i min-i
) (setq min-i i
))))
2953 ;; Then indent each line so that a line indented to
2954 ;; MIN-I becomes indented to IND. Ignore blank lines
2955 ;; and inline tasks in the process.
2956 (let ((delta (- ind min-i
)))
2957 (while (< (point) end
)
2958 (unless (or (looking-at "^[ \t]*$")
2959 (looking-at org-outline-regexp-bol
))
2960 (indent-line-to (+ (org-get-indentation) delta
)))
2964 ;; Return beginning of first non-blank line, starting from
2968 (skip-chars-forward " \r\t\n")
2971 ;; Determine boundaries of changes.
2972 (if (org-region-active-p)
2973 (setq beg
(funcall skip-blanks
(region-beginning))
2974 end
(copy-marker (region-end)))
2975 (setq beg
(funcall skip-blanks
(point-at-bol))
2976 end
(copy-marker (point-at-eol))))
2977 ;; Depending on the starting line, choose an action on the text
2978 ;; between BEG and END.
2979 (org-with-limited-levels
2983 ;; Case 1. Start at an item: de-itemize. Note that it only
2984 ;; happens when a region is active: `org-ctrl-c-minus'
2985 ;; would call `org-cycle-list-bullet' otherwise.
2987 (while (< (point) end
)
2988 (when (org-at-item-p)
2989 (skip-chars-forward " \t")
2990 (delete-region (point) (match-end 0)))
2992 ;; Case 2. Start at an heading: convert to items.
2994 (let* ((bul (org-list-bullet-string "-"))
2995 (bul-len (length bul
))
2996 ;; Indentation of the first heading. It should be
2997 ;; relative to the indentation of its parent, if any.
2998 (start-ind (save-excursion
3000 ((not org-adapt-indentation
) 0)
3001 ((not (outline-previous-heading)) 0)
3002 (t (length (match-string 0))))))
3003 ;; Level of first heading. Further headings will be
3004 ;; compared to it to determine hierarchy in the list.
3005 (ref-level (org-reduced-level (org-outline-level))))
3006 (while (< (point) end
)
3007 (let* ((level (org-reduced-level (org-outline-level)))
3008 (delta (max 0 (- level ref-level
)))
3009 (todo-state (org-get-todo-state)))
3010 ;; If current headline is less indented than the first
3011 ;; one, set it as reference, in order to preserve
3013 (when (< level ref-level
) (setq ref-level level
))
3014 ;; Remove stars and TODO keyword.
3015 (let ((case-fold-search nil
)) (looking-at org-todo-line-regexp
))
3016 (delete-region (point) (or (match-beginning 3)
3017 (line-end-position)))
3019 (indent-line-to (+ start-ind
(* delta bul-len
)))
3020 ;; Turn TODO keyword into a check box.
3022 (let* ((struct (org-list-struct))
3023 (old (copy-tree struct
)))
3024 (org-list-set-checkbox
3025 (line-beginning-position)
3027 (if (member todo-state org-done-keywords
)
3030 (org-list-write-struct struct
3031 (org-list-parents-alist struct
)
3033 ;; Ensure all text down to END (or SECTION-END) belongs
3034 ;; to the newly created item.
3035 (let ((section-end (save-excursion
3036 (or (outline-next-heading) (point)))))
3039 (+ start-ind
(* (1+ delta
) bul-len
))
3040 (min end section-end
)))))))
3041 ;; Case 3. Normal line with ARG: make the first line of region
3042 ;; an item, and shift indentation of others lines to
3043 ;; set them as item's body.
3044 (arg (let* ((bul (org-list-bullet-string "-"))
3045 (bul-len (length bul
))
3046 (ref-ind (org-get-indentation)))
3047 (skip-chars-forward " \t")
3050 (while (< (point) end
)
3051 ;; Ensure that lines less indented than first one
3052 ;; still get included in item body.
3055 (min end
(save-excursion (or (outline-next-heading)
3058 ;; Case 4. Normal line without ARG: turn each non-item line
3061 (while (< (point) end
)
3062 (unless (or (org-at-heading-p) (org-at-item-p))
3063 (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
3065 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
3066 (forward-line))))))))
3069 ;;; Send and receive lists
3071 (defun org-list-to-lisp (&optional delete
)
3072 "Parse the list at point and maybe DELETE it.
3074 Return a list whose car is a symbol of list type, among
3075 `ordered', `unordered' and `descriptive'. Then, each item is
3076 a list of strings and other sub-lists.
3078 For example, the following list:
3083 more text in first item
3092 (\"[X] sub-item two\"))
3093 \"more text in first item\")
3094 (\"[@3] last item\"))
3096 Point is left at list's end."
3097 (letrec ((struct (org-list-struct))
3098 (prevs (org-list-prevs-alist struct
))
3099 (parents (org-list-parents-alist struct
))
3100 (top (org-list-get-top-point struct
))
3101 (bottom (org-list-get-bottom-point struct
))
3104 ;; Remove indentation and final newline from TEXT.
3105 (org-remove-indentation
3106 (if (string-match-p "\n\\'" text
)
3107 (substring text
0 -
1)
3111 ;; Return a list whose car is list type and cdr a list
3113 (cons (org-list-get-list-type (car e
) struct prevs
)
3114 (mapcar parse-item e
))))
3117 ;; Return a list containing counter of item, if any,
3118 ;; text and any sublist inside it.
3119 (let* ((end (org-list-get-item-end e struct
))
3120 (children (org-list-get-children e struct parents
))
3124 (looking-at "[ \t]*\\S-+[ \t]*")
3129 (make-string (string-width (match-string 0)) ?\s
)
3130 (buffer-substring-no-properties
3131 (match-end 0) (or (car children
) end
))))))))
3133 (let* ((child (car children
))
3134 (sub (org-list-get-all-items child struct prevs
))
3135 (last-in-sub (car (last sub
))))
3136 (push (funcall parse-sublist sub
) body
)
3137 ;; Remove whole sub-list from children.
3138 (setq children
(cdr (memq last-in-sub children
)))
3139 ;; There is a chunk of text belonging to the item
3140 ;; if last child doesn't end where next child
3141 ;; starts or where item ends.
3142 (let ((sub-end (org-list-get-item-end last-in-sub struct
))
3143 (next (or (car children
) end
)))
3144 (when (/= sub-end next
)
3147 (buffer-substring-no-properties sub-end next
))
3150 ;; Store output, take care of cursor position and deletion of
3151 ;; list, then return output.
3152 (prog1 (funcall parse-sublist
(org-list-get-all-items top struct prevs
))
3155 (delete-region top bottom
)
3156 (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re
))
3157 (replace-match ""))))))
3159 (defun org-list-make-subtree ()
3160 "Convert the plain list at point into a subtree."
3162 (if (not (ignore-errors (goto-char (org-in-item-p))))
3163 (error "Not in a list")
3164 (let ((list (save-excursion (org-list-to-lisp t
))))
3165 (insert (org-list-to-subtree list
)))))
3167 (defun org-list-to-generic (list params
)
3168 "Convert a LIST parsed through `org-list-to-lisp' to a custom format.
3170 LIST is a list as returned by `org-list-to-lisp', which see.
3171 PARAMS is a property list of parameters used to tweak the output
3174 Valid parameters are:
3178 Export back-end used as a basis to transcode elements of the
3179 list, when no specific parameter applies to it. It is also
3180 used to translate its contents. You can prevent this by
3181 setting :raw property to a non-nil value.
3185 When non-nil, only export the contents of the top most plain
3186 list, effectively ignoring its opening and closing lines.
3190 Strings to start and end an unordered list. They can also be
3191 set to a function returning a string or nil, which will be
3192 called with the depth of the list, counting from 1.
3196 Strings to start and end an ordered list. They can also be set
3197 to a function returning a string or nil, which will be called
3198 with the depth of the list, counting from 1.
3202 Strings to start and end a descriptive list. They can also be
3203 set to a function returning a string or nil, which will be
3204 called with the depth of the list, counting from 1.
3206 :dtstart, :dtend, :ddstart, :ddend
3208 Strings to start and end a descriptive term.
3212 Strings to start or end a list item, and to start a list item
3213 with a counter. They can also be set to a function returning
3214 a string or nil, which will be called with two arguments: the
3215 type of list and the depth of the item, counting from 1.
3219 Strings to start a list item with a counter. It can also be
3220 set to a function returning a string or nil, which will be
3221 called with three arguments: the type of list, the depth of the
3222 item, counting from 1, and the counter. Its value, when
3223 non-nil, has precedence over `:istart'.
3227 String used to separate items. It can also be set to
3228 a function returning a string or nil, which will be called with
3229 two arguments: the type of list and the depth of the item,
3230 counting from 1. It always start on a new line.
3234 Function to be applied to the contents of every item. It is
3235 called with two arguments: the type of list and the contents.
3237 :cbon, :cboff, :cbtrans
3239 String to insert, respectively, an un-checked check-box,
3240 a checked check-box and a check-box in transitional state."
3242 (let* ((backend (plist-get params
:backend
))
3244 (org-export-create-backend
3245 :parent
(or backend
'org
)
3247 `((plain-list .
,(org-list--to-generic-plain-list params
))
3248 (item .
,(org-list--to-generic-item params
))
3249 (macro .
(lambda (m c i
) (org-element-macro-interpreter m nil
))))))
3251 ;; Write LIST back into Org syntax and parse it.
3253 (let ((org-inhibit-startup t
)) (org-mode))
3254 (letrec ((insert-list
3257 (funcall insert-item i
(car l
)))))
3260 (let ((start (point)))
3261 (insert (if (eq type
'ordered
) "1. " "- "))
3263 (if (consp e
) (funcall insert-list e
)
3268 (let ((ind (if (eq type
'ordered
) 3 2)))
3269 (while (> (point) start
)
3270 (unless (looking-at-p "[ \t]*$")
3272 (forward-line -
1))))))))
3273 (funcall insert-list list
))
3275 (org-element-map (org-element-parse-buffer) 'plain-list
3277 (setf info
(org-export-get-environment backend nil params
)))
3278 (when (and backend
(symbolp backend
) (not (org-export-get-backend backend
)))
3279 (user-error "Unknown :backend value"))
3280 (unless backend
(require 'ox-org
))
3281 ;; When`:raw' property has a non-nil value, turn all objects back
3283 (when (and backend
(plist-get params
:raw
))
3284 (org-element-map data org-element-all-objects
3286 (org-element-set-element
3287 object
(org-element-interpret-data object
)))))
3288 ;; We use a low-level mechanism to export DATA so as to skip all
3289 ;; usual pre-processing and post-processing, i.e., hooks, filters,
3290 ;; Babel code evaluation, include keywords and macro expansion,
3292 (let ((output (org-export-data-with-backend data custom-backend info
)))
3293 ;; Remove final newline.
3294 (if (org-string-nw-p output
) (substring-no-properties output
0 -
1) ""))))
3296 (defun org-list--depth (element)
3297 "Return the level of ELEMENT within current plain list.
3298 ELEMENT is either an item or a plain list."
3299 (cl-count-if (lambda (ancestor) (eq (org-element-type ancestor
) 'plain-list
))
3300 (org-element-lineage element nil t
)))
3302 (defun org-list--trailing-newlines (string)
3303 "Return the number of trailing newlines in STRING."
3306 (skip-chars-backward " \t\n")
3307 (count-lines (line-beginning-position 2) (point-max))))
3309 (defun org-list--generic-eval (value &rest args
)
3310 "Evaluate VALUE according to its type.
3311 VALUE is either nil, a string or a function. In the latter case,
3312 it is called with arguments ARGS."
3313 (cond ((null value
) nil
)
3314 ((stringp value
) value
)
3315 ((functionp value
) (apply value args
))
3316 (t (error "Wrong value: %s" value
))))
3318 (defun org-list--to-generic-plain-list (params)
3319 "Return a transcoder for `plain-list' elements.
3320 PARAMS is a plist used to tweak the behavior of the transcoder."
3321 (let ((ustart (plist-get params
:ustart
))
3322 (uend (plist-get params
:uend
))
3323 (ostart (plist-get params
:ostart
))
3324 (oend (plist-get params
:oend
))
3325 (dstart (plist-get params
:dstart
))
3326 (dend (plist-get params
:dend
))
3327 (splice (plist-get params
:splice
))
3328 (backend (plist-get params
:backend
)))
3329 (lambda (plain-list contents info
)
3330 (let* ((type (org-element-property :type plain-list
))
3331 (depth (org-list--depth plain-list
))
3332 (start (and (not splice
)
3333 (org-list--generic-eval
3339 (end (and (not splice
)
3340 (org-list--generic-eval
3346 ;; Make sure trailing newlines in END appear in the output by
3347 ;; setting `:post-blank' property to their number.
3349 (org-element-put-property
3350 plain-list
:post-blank
(org-list--trailing-newlines end
)))
3352 (concat (and start
(concat start
"\n"))
3353 (if (or start end splice
(not backend
))
3355 (org-export-with-backend backend plain-list contents info
))
3358 (defun org-list--to-generic-item (params)
3359 "Return a transcoder for `item' elements.
3360 PARAMS is a plist used to tweak the behavior of the transcoder."
3361 (let ((backend (plist-get params
:backend
))
3362 (istart (plist-get params
:istart
))
3363 (iend (plist-get params
:iend
))
3364 (isep (plist-get params
:isep
))
3365 (icount (plist-get params
:icount
))
3366 (ifmt (plist-get params
:ifmt
))
3367 (cboff (plist-get params
:cboff
))
3368 (cbon (plist-get params
:cbon
))
3369 (cbtrans (plist-get params
:cbtrans
))
3370 (dtstart (plist-get params
:dtstart
))
3371 (dtend (plist-get params
:dtend
))
3372 (ddstart (plist-get params
:ddstart
))
3373 (ddend (plist-get params
:ddend
)))
3374 (lambda (item contents info
)
3376 (org-element-property :type
(org-element-property :parent item
)))
3377 (tag (org-element-property :tag item
))
3378 (depth (org-list--depth item
))
3379 (separator (and (org-export-get-next-element item info
)
3380 (org-list--generic-eval isep type depth
)))
3381 (closing (pcase (org-list--generic-eval iend type depth
)
3383 ((and (guard separator
) s
)
3384 (if (equal (substring s -
1) "\n") s
(concat s
"\n")))
3386 ;; When a closing line or a separator is provided, make sure
3387 ;; its trailing newlines are taken into account when building
3388 ;; output. This is done by setting `:post-blank' property to
3389 ;; the number of such lines in the last line to be added.
3390 (let ((last-string (or separator closing
)))
3392 (org-element-put-property
3395 (max (1- (org-list--trailing-newlines last-string
)) 0))))
3398 (let ((c (org-element-property :counter item
)))
3399 (if (and c icount
) (org-list--generic-eval icount type depth c
)
3400 (org-list--generic-eval istart type depth
)))
3402 (if (or istart iend icount ifmt cbon cboff cbtrans
(not backend
)
3403 (and (eq type
'descriptive
)
3404 (or dtstart dtend ddstart ddend
)))
3406 (pcase (org-element-property :checkbox item
)
3413 (org-export-data-with-backend
3415 (org-element-interpret-data tag
))
3419 (if (= (length contents
) 0) ""
3420 (substring contents
0 -
1))))
3421 (if ifmt
(org-list--generic-eval ifmt type contents
)
3424 (org-export-with-backend backend item contents info
))))
3425 ;; Remove final newline.
3426 (if (equal body
"") ""
3427 (substring (org-element-normalize-string body
) 0 -
1)))
3431 (defun org-list-to-latex (list &optional params
)
3432 "Convert LIST into a LaTeX list.
3433 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3434 PARAMS is a property list with overruling parameters for
3435 `org-list-to-generic'. Return converted list as a string."
3437 (org-list-to-generic list
(org-combine-plists '(:backend latex
) params
)))
3439 (defun org-list-to-html (list &optional params
)
3440 "Convert LIST into a HTML list.
3441 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3442 PARAMS is a property list with overruling parameters for
3443 `org-list-to-generic'. Return converted list as a string."
3445 (org-list-to-generic list
(org-combine-plists '(:backend html
) params
)))
3447 (defun org-list-to-texinfo (list &optional params
)
3448 "Convert LIST into a Texinfo list.
3449 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3450 PARAMS is a property list with overruling parameters for
3451 `org-list-to-generic'. Return converted list as a string."
3452 (require 'ox-texinfo
)
3453 (org-list-to-generic list
(org-combine-plists '(:backend texinfo
) params
)))
3455 (defun org-list-to-org (list &optional params
)
3456 "Convert LIST into an Org plain list.
3457 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3458 with overruling parameters for `org-list-to-generic'."
3460 (lambda (type _depth
&optional c
)
3461 (concat (if (eq type
'ordered
) "1. " "- ")
3462 (and c
(format "[@%d] " c
)))))
3464 (list :istart make-item
3466 :ifmt
(lambda (_type contents
)
3467 (replace-regexp-in-string "\n" "\n " contents
))
3472 (org-list-to-generic list
(org-combine-plists defaults params
))))
3474 (defun org-list-to-subtree (list &optional params
)
3475 "Convert LIST into an Org subtree.
3476 LIST is as returned by `org-list-to-lisp'. PARAMS is a property
3477 list with overruling parameters for `org-list-to-generic'."
3478 (let* ((blank (pcase (cdr (assq 'heading org-blank-before-new-entry
))
3480 (`auto
(save-excursion
3481 (org-with-limited-levels (outline-previous-heading))
3482 (org-previous-line-empty-p)))))
3483 (level (org-reduced-level (or (org-current-level) 0)))
3485 (lambda (_type depth
&optional _count
)
3486 ;; Return the string for the heading, depending on DEPTH
3487 ;; of current sub-list.
3488 (let ((oddeven-level (+ level depth
)))
3489 (concat (make-string (if org-odd-levels-only
3490 (1- (* 2 oddeven-level
))
3494 (org-list-to-generic
3500 :dtstart
" " :dtend
" "
3501 :isep
(if blank
"\n\n" "\n")
3502 :cbon
"DONE " :cboff
"TODO " :cbtrans
"TODO ")
3507 ;;; org-list.el ends here