1 ;;; org-list.el --- Plain lists for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2017 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: http://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 <http://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))
110 org-element-lineage
"org-element" (blob &optional types with-self
))
111 (declare-function org-element-macro-interpreter
"org-element" (macro ##))
113 org-element-map
"org-element"
114 (data types fun
&optional info first-match no-recursion with-affiliated
))
115 (declare-function org-element-normalize-string
"org-element" (s))
116 (declare-function org-element-parse-buffer
"org-element"
117 (&optional granularity visible-only
))
118 (declare-function org-element-property
"org-element" (property element
))
119 (declare-function org-element-put-property
"org-element"
120 (element property value
))
121 (declare-function org-element-set-element
"org-element" (old new
))
122 (declare-function org-element-type
"org-element" (element))
123 (declare-function org-element-update-syntax
"org-element" ())
124 (declare-function org-entry-get
"org"
125 (pom property
&optional inherit literal-nil
))
126 (declare-function org-export-create-backend
"ox" (&rest rest
) t
)
127 (declare-function org-export-data-with-backend
"ox" (data backend info
))
128 (declare-function org-export-get-backend
"ox" (name))
129 (declare-function org-export-get-environment
"ox"
130 (&optional backend subtreep ext-plist
))
131 (declare-function org-export-get-next-element
"ox"
132 (blob info
&optional n
))
133 (declare-function org-export-with-backend
"ox"
134 (backend data
&optional contents info
))
135 (declare-function org-fix-tags-on-the-fly
"org" ())
136 (declare-function org-get-indentation
"org" (&optional line
))
137 (declare-function org-get-todo-state
"org" ())
138 (declare-function org-in-block-p
"org" (names))
139 (declare-function org-in-regexp
"org" (re &optional nlines visually
))
140 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
141 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
142 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
143 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
144 (declare-function org-level-increment
"org" ())
145 (declare-function org-narrow-to-subtree
"org" ())
146 (declare-function org-outline-level
"org" ())
147 (declare-function org-previous-line-empty-p
"org" ())
148 (declare-function org-reduced-level
"org" (L))
149 (declare-function org-remove-indentation
"org" (code &optional n
))
150 (declare-function org-show-subtree
"org" ())
151 (declare-function org-sort-remove-invisible
"org" (S))
152 (declare-function org-time-string-to-seconds
"org" (s))
153 (declare-function org-timer-hms-to-secs
"org-timer" (hms))
154 (declare-function org-timer-item
"org-timer" (&optional arg
))
155 (declare-function org-trim
"org" (s &optional keep-lead
))
156 (declare-function org-uniquify
"org" (list))
157 (declare-function org-invisible-p
"org" (&optional pos
))
158 (declare-function outline-flag-region
"outline" (from to flag
))
159 (declare-function outline-next-heading
"outline" ())
160 (declare-function outline-previous-heading
"outline" ())
164 ;;; Configuration variables
166 (defgroup org-plain-lists nil
167 "Options concerning plain lists in Org mode."
168 :tag
"Org Plain lists"
169 :group
'org-structure
)
171 (defcustom org-cycle-include-plain-lists t
172 "When t, make TAB cycle visibility on plain list items.
173 Cycling plain lists works only when the cursor is on a plain list
174 item. When the cursor is on an outline heading, plain lists are
175 treated as text. This is the most stable way of handling this,
176 which is why it is the default.
178 When this is the symbol `integrate', then integrate plain list
179 items when cycling, as if they were children of outline headings.
181 This setting can lead to strange effects when switching visibility
182 to `children', because the first \"child\" in a subtree decides
183 what children should be listed. If that first \"child\" is a
184 plain list item with an implied large level number, all true
185 children and grand children of the outline heading will be
186 exposed in a children' view."
187 :group
'org-plain-lists
190 (const :tag
"Never" nil
)
191 (const :tag
"With cursor in plain list (recommended)" t
)
192 (const :tag
"As children of outline headings" integrate
)))
194 (defcustom org-list-demote-modify-bullet nil
195 "Default bullet type installed when demoting an item.
196 This is an association list, for each bullet type, this alist will point
197 to the bullet that should be used when this item is demoted.
200 (setq org-list-demote-modify-bullet
201 \\='((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
206 + Silence of the Lambs
209 + The Hunt for Red October
215 - Silence of the Lambs
218 - The Hunt for Red October
220 :group
'org-plain-lists
223 (choice :tag
"If the current bullet is "
229 (choice :tag
"demotion will change it to"
236 (defcustom org-plain-list-ordered-item-terminator t
237 "The character that makes a line with leading number an ordered list item.
238 Valid values are ?. and ?\). To get both terminators, use t.
240 This variable needs to be set before org.el is loaded. If you
241 need to make a change while Emacs is running, use the customize
242 interface or run the following code after updating it:
244 `\\[org-element-update-syntax]'"
245 :group
'org-plain-lists
246 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
247 (const :tag
"paren like in \"2)\"" ?\
))
248 (const :tag
"both" t
))
249 :set
(lambda (var val
) (set var val
)
250 (when (featurep 'org-element
) (org-element-update-syntax))))
252 (defcustom org-list-allow-alphabetical nil
253 "Non-nil means single character alphabetical bullets are allowed.
255 Both uppercase and lowercase are handled. Lists with more than
256 26 items will fallback to standard numbering. Alphabetical
257 counters like \"[@c]\" will be recognized.
259 This variable needs to be set before org.el is loaded. If you
260 need to make a change while Emacs is running, use the customize
261 interface or run the following code after updating it:
263 `\\[org-element-update-syntax]'"
264 :group
'org-plain-lists
267 :set
(lambda (var val
) (set var val
)
268 (when (featurep 'org-element
) (org-element-update-syntax))))
270 (defcustom org-list-two-spaces-after-bullet-regexp nil
271 "A regular expression matching bullets that should have 2 spaces after them.
272 When nil, no bullet will have two spaces after them. When
273 a string, it will be used as a regular expression. When the
274 bullet type of a list is changed, the new bullet type will be
275 matched against this regexp. If it matches, there will be two
276 spaces instead of one after the bullet in each item of the list."
277 :group
'org-plain-lists
279 (const :tag
"never" nil
)
282 (defcustom org-list-automatic-rules
'((checkbox . t
)
284 "Non-nil means apply set of rules when acting on lists.
286 By default, automatic actions are taken when using
287 `\\[org-meta-return]',
290 `\\[org-shiftmetaright]',
291 `\\[org-shiftmetaleft]',
292 `\\[org-ctrl-c-minus]',
293 `\\[org-toggle-checkbox]',
294 `\\[org-insert-todo-heading]'.
296 You can disable individually these rules by setting them to nil.
299 checkbox when non-nil, checkbox statistics is updated each time
300 you either insert a new checkbox or toggle a checkbox.
301 indent when non-nil, indenting or outdenting list top-item
302 with its subtree will move the whole list and
303 outdenting a list whose bullet is * to column 0 will
304 change that bullet to \"-\"."
305 :group
'org-plain-lists
307 :type
'(alist :tag
"Sets of rules"
310 (const :tag
"Checkbox" checkbox
)
311 (const :tag
"Indent" indent
))
313 (boolean :tag
"Activate" :value t
)))
315 (defcustom org-list-use-circular-motion nil
316 "Non-nil means commands implying motion in lists should be cyclic.
318 In that case, the item following the last item is the first one,
319 and the item preceding the first item is the last one.
321 This affects the behavior of
322 `\\[org-move-item-up]',
323 `\\[org-move-item-down]',
325 `\\[org-previous-item]'."
326 :group
'org-plain-lists
330 (defvar org-checkbox-statistics-hook nil
331 "Hook that is run whenever Org thinks checkbox statistics should be updated.
332 This hook runs even if checkbox rule in
333 `org-list-automatic-rules' does not apply, so it can be used to
334 implement alternative ways of collecting statistics
337 (defcustom org-checkbox-hierarchical-statistics t
338 "Non-nil means checkbox statistics counts only the state of direct children.
339 When nil, all boxes below the cookie are counted.
340 This can be set to nil on a per-node basis using a COOKIE_DATA property
341 with the word \"recursive\" in the value."
342 :group
'org-plain-lists
345 (defcustom org-list-description-max-indent
20
346 "Maximum indentation for the second line of a description list.
347 When the indentation would be larger than this, it will become
348 5 characters instead."
349 :group
'org-plain-lists
352 (defcustom org-list-indent-offset
0
353 "Additional indentation for sub-items in a list.
354 By setting this to a small number, usually 1 or 2, one can more
355 clearly distinguish sub-items in a list."
356 :group
'org-plain-lists
360 (defcustom org-list-radio-list-templates
361 '((latex-mode "% BEGIN RECEIVE ORGLST %n
362 % END RECEIVE ORGLST %n
364 #+ORGLST: SEND %n org-list-to-latex
367 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
368 @c END RECEIVE ORGLST %n
370 #+ORGLST: SEND %n org-list-to-texinfo
373 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
374 <!-- END RECEIVE ORGLST %n -->
376 #+ORGLST: SEND %n org-list-to-html
379 "Templates for radio lists in different major modes.
380 All occurrences of %n in a template will be replaced with the name of the
381 list, obtained by prompting the user."
382 :group
'org-plain-lists
384 (list (symbol :tag
"Major mode")
385 (string :tag
"Format"))))
387 (defvar org-list-forbidden-blocks
'("example" "verse" "src" "export")
388 "Names of blocks where lists are not allowed.
389 Names must be in lower case.")
391 (defvar org-list-export-context
'(block inlinetask
)
392 "Context types where lists will be interpreted during export.
394 Valid types are `drawer', `inlinetask' and `block'. More
395 specifically, type `block' is determined by the variable
396 `org-list-forbidden-blocks'.")
400 ;;; Predicates and regexps
402 (defconst org-list-end-re
"^[ \t]*\n[ \t]*\n"
403 "Regex matching the end of a plain list.")
405 (defconst org-list-full-item-re
406 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
407 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
408 "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
409 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
410 "Matches a list item and puts everything into groups:
414 group 4: description tag")
416 (defun org-item-re ()
417 "Return the correct regular expression for plain lists."
419 ((eq org-plain-list-ordered-item-terminator t
) "[.)]")
420 ((= org-plain-list-ordered-item-terminator ?\
)) ")")
421 ((= org-plain-list-ordered-item-terminator ?.
) "\\.")
423 (alpha (if org-list-allow-alphabetical
"\\|[A-Za-z]" "")))
424 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha
"\\)" term
425 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
427 (defsubst org-item-beginning-re
()
428 "Regexp matching the beginning of a plain list item."
429 (concat "^" (org-item-re)))
431 (defun org-list-at-regexp-after-bullet-p (regexp)
432 "Is point at a list item with REGEXP after bullet?"
435 (goto-char (match-end 0))
436 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
437 (if org-list-allow-alphabetical
438 "\\([0-9]+\\|[A-Za-z]\\)"
441 ;; Ignore counter if any
442 (when (looking-at counter-re
) (goto-char (match-end 0))))
443 (looking-at regexp
))))
445 (defun org-list-in-valid-context-p ()
446 "Is point in a context where lists are allowed?"
447 (not (org-in-block-p org-list-forbidden-blocks
)))
449 (defun org-in-item-p ()
450 "Return item beginning position when in a plain list, nil otherwise."
453 (let* ((case-fold-search t
)
454 (context (org-list-context))
455 (lim-up (car context
))
456 (inlinetask-re (and (featurep 'org-inlinetask
)
457 (org-inlinetask-outline-regexp)))
458 (item-re (org-item-re))
459 ;; Indentation isn't meaningful when point starts at an empty
460 ;; line or an inline task.
461 (ind-ref (if (or (looking-at "^[ \t]*$")
462 (and inlinetask-re
(looking-at inlinetask-re
)))
464 (org-get-indentation))))
466 ((eq (nth 2 context
) 'invalid
) nil
)
467 ((looking-at item-re
) (point))
469 ;; Detect if cursor in amidst `org-list-end-re'. First, count
470 ;; number HL of hard lines it takes, then call `org-in-regexp'
471 ;; to compute its boundaries END-BOUNDS. When point is
472 ;; in-between, move cursor before regexp beginning.
473 (let ((hl 0) (i -
1) end-bounds
)
475 (while (setq i
(string-match
476 "[\r\n]" org-list-end-re
(1+ i
)))
478 (setq end-bounds
(org-in-regexp org-list-end-re hl
)))
479 (>= (point) (car end-bounds
))
480 (< (point) (cdr end-bounds
)))
481 (goto-char (car end-bounds
))
483 ;; Look for an item, less indented that reference line.
486 (let ((ind (org-get-indentation)))
488 ;; This is exactly what we want.
489 ((and (looking-at item-re
) (< ind ind-ref
))
490 (throw 'exit
(point)))
491 ;; At upper bound of search or looking at the end of a
492 ;; previous list: search is over.
493 ((<= (point) lim-up
) (throw 'exit nil
))
494 ((looking-at org-list-end-re
) (throw 'exit nil
))
495 ;; Skip blocks, drawers, inline-tasks, blank lines
496 ((and (looking-at "^[ \t]*#\\+end_")
497 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
498 ((and (looking-at "^[ \t]*:END:")
499 (re-search-backward org-drawer-regexp lim-up t
))
501 ((and inlinetask-re
(looking-at inlinetask-re
))
502 (org-inlinetask-goto-beginning)
504 ((looking-at "^[ \t]*$") (forward-line -
1))
505 ;; Text at column 0 cannot belong to a list: stop.
506 ((zerop ind
) (throw 'exit nil
))
507 ;; Normal text less indented than reference line, take
508 ;; it as new reference.
512 (t (forward-line -
1)))))))))))
514 (defun org-at-item-p ()
515 "Is point in a line starting a hand-formatted item?"
518 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
520 (defun org-at-item-bullet-p ()
521 "Is point at the bullet of a plain list item?"
523 (not (member (char-after) '(?\ ?
\t)))
524 (< (point) (match-end 0))))
526 (defun org-at-item-timer-p ()
527 "Is point at a line starting a plain list item with a timer?"
528 (org-list-at-regexp-after-bullet-p
529 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
531 (defun org-at-item-description-p ()
532 "Is point at a description list item?"
533 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::\\([ \t]+\\|$\\)"))
535 (defun org-at-item-checkbox-p ()
536 "Is point at a line starting a plain-list item with a checklet?"
537 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
539 (defun org-at-item-counter-p ()
540 "Is point at a line starting a plain-list item with a counter?"
542 (looking-at org-list-full-item-re
)
547 ;;; Structures and helper functions
549 (defun org-list-context ()
550 "Determine context, and its boundaries, around point.
552 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
553 are boundaries and CONTEXT is a symbol among `drawer', `block',
554 `invalid', `inlinetask' and nil.
556 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
559 (org-with-limited-levels
561 (let ((case-fold-search t
) (pos (point)) beg end context-type
562 ;; Get positions of surrounding headings. This is the
564 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t
))
567 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
568 ;; Is point inside a drawer?
569 (let ((end-re "^[ \t]*:END:")
570 (beg-re org-drawer-regexp
))
571 (when (save-excursion
572 (and (not (looking-at beg-re
))
573 (not (looking-at end-re
))
574 (setq beg
(and (re-search-backward beg-re lim-up t
)
575 (1+ (point-at-eol))))
576 (setq end
(or (and (re-search-forward end-re lim-down t
)
577 (1- (match-beginning 0)))
580 (setq lim-up beg lim-down end context-type
'drawer
)))
581 ;; Is point strictly in a block, and of which type?
582 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type
)
583 (when (save-excursion
584 (and (not (looking-at block-re
))
585 (setq beg
(and (re-search-backward block-re lim-up t
)
586 (1+ (point-at-eol))))
587 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
588 (setq type
(downcase (match-string 1)))
590 (setq end
(or (and (re-search-forward block-re lim-down t
)
594 (equal (downcase (match-string 1)) "end")))
595 (setq lim-up beg lim-down end
596 context-type
(if (member type org-list-forbidden-blocks
)
598 ;; Is point in an inlinetask?
599 (when (and (featurep 'org-inlinetask
)
601 (let* ((beg-re (org-inlinetask-outline-regexp))
602 (end-re (concat beg-re
"END[ \t]*$")))
603 (and (not (looking-at "^\\*+"))
604 (setq beg
(and (re-search-backward beg-re lim-up t
)
605 (1+ (point-at-eol))))
606 (not (looking-at end-re
))
607 (setq end
(and (re-search-forward end-re lim-down t
)
608 (1- (match-beginning 0))))
610 (setq lim-up beg lim-down end context-type
'inlinetask
))
611 ;; Return context boundaries and type.
612 (list lim-up lim-down context-type
))))))
614 (defun org-list-struct ()
615 "Return structure of list at point.
617 A list structure is an alist where key is point at item, and
620 2. bullet with trailing whitespace,
621 3. bullet counter, if any,
623 5. description tag, if any,
624 6. position at item end.
626 Thus the following list, where numbers in parens are
631 5. [@5] sub-item 2 (34)
632 some other text belonging to first item (55)
634 + tag :: description (109)
637 will get the following structure:
639 ((1 0 \"- \" nil \"[X]\" nil 97)
640 (18 2 \"1. \" nil nil nil 34)
641 (34 2 \"5. \" \"5\" nil nil 55)
642 (97 0 \"- \" nil nil nil 131)
643 (109 2 \"+ \" nil nil \"tag\" 131))
645 Assume point is at an item."
648 (let* ((case-fold-search t
)
649 (context (org-list-context))
650 (lim-up (car context
))
651 (lim-down (nth 1 context
))
653 (item-re (org-item-re))
654 (inlinetask-re (and (featurep 'org-inlinetask
)
655 (org-inlinetask-outline-regexp)))
656 (beg-cell (cons (point) (org-get-indentation)))
657 itm-lst itm-lst-2 end-lst end-lst-2 struct
660 ;; Return association at point.
662 (looking-at org-list-full-item-re
)
663 (let ((bullet (match-string-no-properties 1)))
667 (match-string-no-properties 2) ; counter
668 (match-string-no-properties 3) ; checkbox
670 (and (string-match-p "[-+*]" bullet
)
671 (match-string-no-properties 4)))))))
674 ;; Ensure list ends at the first blank line.
676 (skip-chars-backward " \r\t\n")
677 (min (1+ (point-at-eol)) lim-down
)))))
678 ;; 1. Read list from starting item to its beginning, and save
679 ;; top item position and indentation in BEG-CELL. Also store
680 ;; ending position of items in END-LST.
684 (let ((ind (org-get-indentation)))
687 ;; At upward limit: if we ended at an item, store it,
688 ;; else dismiss useless data recorded above BEG-CELL.
692 (if (not (looking-at item-re
))
693 (memq (assq (car beg-cell
) itm-lst
) itm-lst
)
694 (setq beg-cell
(cons (point) ind
))
695 (cons (funcall assoc-at-point ind
) itm-lst
)))))
696 ;; Looking at a list ending regexp. Dismiss useless
697 ;; data recorded above BEG-CELL. Jump to part 2.
698 ((looking-at org-list-end-re
)
701 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
702 ;; Point is at an item. Add data to ITM-LST. It may
703 ;; also end a previous item: save it in END-LST. If
704 ;; ind is less or equal than BEG-CELL and there is no
705 ;; end at this ind or lesser, this item becomes the new
707 ((looking-at item-re
)
708 (push (funcall assoc-at-point ind
) itm-lst
)
709 (push (cons ind
(point)) end-lst
)
710 (when (< ind text-min-ind
) (setq beg-cell
(cons (point) ind
)))
712 ;; Skip blocks, drawers, inline tasks, blank lines.
713 ((and (looking-at "^[ \t]*#\\+end_")
714 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
715 ((and (looking-at "^[ \t]*:END:")
716 (re-search-backward org-drawer-regexp lim-up t
))
718 ((and inlinetask-re
(looking-at inlinetask-re
))
719 (org-inlinetask-goto-beginning)
721 ((looking-at "^[ \t]*$")
723 ;; From there, point is not at an item. Interpret
724 ;; line's indentation:
725 ;; - text at column 0 is necessarily out of any list.
726 ;; Dismiss data recorded above BEG-CELL. Jump to
728 ;; - any other case may be an ending position for an
729 ;; hypothetical item above. Store it and proceed.
733 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
735 (when (< ind text-min-ind
) (setq text-min-ind ind
))
736 (push (cons ind
(point)) end-lst
)
737 (forward-line -
1)))))))
738 ;; 2. Read list from starting point to its end, that is until we
739 ;; get out of context, or that a non-item line is less or
740 ;; equally indented than BEG-CELL's cdr. Also, store ending
741 ;; position of items in END-LST-2.
744 (let ((ind (org-get-indentation)))
746 ((>= (point) lim-down
)
747 ;; At downward limit: this is de facto the end of the
748 ;; list. Save point as an ending position, and jump to
751 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
752 ;; Looking at a list ending regexp. Save point as an
753 ;; ending position and jump to part 3.
754 ((looking-at org-list-end-re
)
755 (throw 'exit
(push (cons 0 (point)) end-lst-2
)))
756 ((looking-at item-re
)
757 ;; Point is at an item. Add data to ITM-LST-2. It may
758 ;; also end a previous item, so save it in END-LST-2.
759 (push (funcall assoc-at-point ind
) itm-lst-2
)
760 (push (cons ind
(point)) end-lst-2
)
762 ;; Skip inline tasks and blank lines along the way
763 ((and inlinetask-re
(looking-at inlinetask-re
))
764 (org-inlinetask-goto-end))
765 ((looking-at "^[ \t]*$")
767 ;; Ind is lesser or equal than BEG-CELL's. The list is
768 ;; over: store point as an ending position and jump to
770 ((<= ind
(cdr beg-cell
))
772 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
773 ;; Else, if ind is lesser or equal than previous item's,
774 ;; this is an ending position: store it. In any case,
775 ;; skip block or drawer at point, and move to next line.
777 (when (<= ind
(nth 1 (car itm-lst-2
)))
778 (push (cons ind
(point)) end-lst-2
))
780 ((and (looking-at "^[ \t]*#\\+begin_")
781 (re-search-forward "^[ \t]*#\\+end_" lim-down t
)))
782 ((and (looking-at org-drawer-regexp
)
783 (re-search-forward "^[ \t]*:END:" lim-down t
))))
784 (forward-line 1))))))
785 (setq struct
(append itm-lst
(cdr (nreverse itm-lst-2
)))
786 end-lst
(append end-lst
(cdr (nreverse end-lst-2
))))
787 ;; 3. Associate each item to its end position.
788 (org-list-struct-assoc-end struct end-lst
)
792 (defun org-list-struct-assoc-end (struct end-list
)
793 "Associate proper ending point to items in STRUCT.
795 END-LIST is a pseudo-alist where car is indentation and cdr is
798 This function modifies STRUCT."
799 (let ((endings end-list
))
802 (let ((pos (car elt
))
804 ;; Remove end candidates behind current item.
805 (while (or (<= (cdar endings
) pos
))
807 ;; Add end position to item assoc.
808 (let ((old-end (nthcdr 6 elt
))
809 (new-end (assoc-default ind endings
'<=)))
811 (setcar old-end new-end
)
812 (setcdr elt
(append (cdr elt
) (list new-end
)))))))
815 (defun org-list-prevs-alist (struct)
816 "Return alist between item and previous item in STRUCT."
817 (let ((item-end-alist (mapcar (lambda (e) (cons (car e
) (nth 6 e
)))
820 (let ((prev (car (rassq (car e
) item-end-alist
))))
821 (cons (car e
) prev
)))
824 (defun org-list-parents-alist (struct)
825 "Return alist between item and parent in STRUCT."
826 (let* ((ind-to-ori (list (list (nth 1 (car struct
)))))
827 (top-item (org-list-get-top-point struct
))
828 (prev-pos (list top-item
)))
830 (mapcar (lambda (item)
831 (let ((pos (car item
))
833 (prev-ind (caar ind-to-ori
)))
837 ;; A sub-list is over. Find the associated
838 ;; origin in IND-TO-ORI. If it cannot be
839 ;; found (ill-formed list), set its parent as
840 ;; the first item less indented. If there is
841 ;; none, make it a top-level item.
843 (or (member (assq ind ind-to-ori
) ind-to-ori
)
847 (when (< (car e
) ind
)
848 (throw 'exit
(member e ind-to-ori
))))
851 (cons pos
(cdar ind-to-ori
)))
852 ;; A sub-list starts. Every item at IND will
853 ;; have previous item as its parent.
855 (let ((origin (nth 1 prev-pos
)))
856 (push (cons ind origin
) ind-to-ori
)
858 ;; Another item in the same sub-list: it shares
859 ;; the same parent as the previous item.
860 (t (cons pos
(cdar ind-to-ori
))))))
867 (defsubst org-list-get-nth
(n key struct
)
868 "Return the Nth value of KEY in STRUCT."
869 (nth n
(assq key struct
)))
871 (defun org-list-set-nth (n key struct new
)
872 "Set the Nth value of KEY in STRUCT to NEW.
873 \nThis function modifies STRUCT."
874 (setcar (nthcdr n
(assq key struct
)) new
))
876 (defsubst org-list-get-ind
(item struct
)
877 "Return indentation of ITEM in STRUCT."
878 (org-list-get-nth 1 item struct
))
880 (defun org-list-set-ind (item struct ind
)
881 "Set indentation of ITEM in STRUCT to IND.
882 \nThis function modifies STRUCT."
883 (org-list-set-nth 1 item struct ind
))
885 (defsubst org-list-get-bullet
(item struct
)
886 "Return bullet of ITEM in STRUCT."
887 (org-list-get-nth 2 item struct
))
889 (defun org-list-set-bullet (item struct bullet
)
890 "Set bullet of ITEM in STRUCT to BULLET.
891 \nThis function modifies STRUCT."
892 (org-list-set-nth 2 item struct bullet
))
894 (defsubst org-list-get-counter
(item struct
)
895 "Return counter of ITEM in STRUCT."
896 (org-list-get-nth 3 item struct
))
898 (defsubst org-list-get-checkbox
(item struct
)
899 "Return checkbox of ITEM in STRUCT or nil."
900 (org-list-get-nth 4 item struct
))
902 (defun org-list-set-checkbox (item struct checkbox
)
903 "Set checkbox of ITEM in STRUCT to CHECKBOX.
904 \nThis function modifies STRUCT."
905 (org-list-set-nth 4 item struct checkbox
))
907 (defsubst org-list-get-tag
(item struct
)
908 "Return end position of ITEM in STRUCT."
909 (org-list-get-nth 5 item struct
))
911 (defun org-list-get-item-end (item struct
)
912 "Return end position of ITEM in STRUCT."
913 (org-list-get-nth 6 item struct
))
915 (defun org-list-get-item-end-before-blank (item struct
)
916 "Return point at end of ITEM in STRUCT, before any blank line.
917 Point returned is at end of line."
919 (goto-char (org-list-get-item-end item struct
))
920 (skip-chars-backward " \r\t\n")
923 (defun org-list-get-parent (item struct parents
)
924 "Return parent of ITEM or nil.
925 STRUCT is the list structure. PARENTS is the alist of parents,
926 as returned by `org-list-parents-alist'."
927 (let ((parents (or parents
(org-list-parents-alist struct
))))
928 (cdr (assq item parents
))))
930 (defun org-list-has-child-p (item struct
)
931 "Non-nil if ITEM has a child.
933 STRUCT is the list structure.
935 Value returned is the position of the first child of ITEM."
936 (let ((ind (org-list-get-ind item struct
))
937 (child-maybe (car (nth 1 (member (assq item struct
) struct
)))))
938 (when (and child-maybe
939 (< ind
(org-list-get-ind child-maybe struct
)))
942 (defun org-list-get-next-item (item _struct prevs
)
943 "Return next item in same sub-list as ITEM, or nil.
944 STRUCT is the list structure. PREVS is the alist of previous
945 items, as returned by `org-list-prevs-alist'."
946 (car (rassq item prevs
)))
948 (defun org-list-get-prev-item (item _struct prevs
)
949 "Return previous item in same sub-list as ITEM, or nil.
950 STRUCT is the list structure. PREVS is the alist of previous
951 items, as returned by `org-list-prevs-alist'."
952 (cdr (assq item prevs
)))
954 (defun org-list-get-subtree (item struct
)
955 "List all items having ITEM as a common ancestor, or nil.
956 STRUCT is the list structure."
957 (let* ((item-end (org-list-get-item-end item struct
))
958 (sub-struct (cdr (member (assq item struct
) struct
)))
963 (if (< pos item-end
) (push pos subtree
) (throw 'exit nil
))))
967 (defun org-list-get-all-items (item struct prevs
)
968 "List all items in the same sub-list as ITEM.
969 STRUCT is the list structure. PREVS is the alist of previous
970 items, as returned by `org-list-prevs-alist'."
971 (let ((prev-item item
)
973 before-item after-item
)
974 (while (setq prev-item
(org-list-get-prev-item prev-item struct prevs
))
975 (push prev-item before-item
))
976 (while (setq next-item
(org-list-get-next-item next-item struct prevs
))
977 (push next-item after-item
))
978 (append before-item
(list item
) (nreverse after-item
))))
980 (defun org-list-get-children (item _struct parents
)
981 "List all children of ITEM, or nil.
982 STRUCT is the list structure. PARENTS is the alist of parents,
983 as returned by `org-list-parents-alist'."
985 (while (setq child
(car (rassq item parents
)))
986 (setq parents
(cdr (member (assq child parents
) parents
)))
990 (defun org-list-get-top-point (struct)
991 "Return point at beginning of list.
992 STRUCT is the list structure."
995 (defun org-list-get-bottom-point (struct)
996 "Return point at bottom of list.
997 STRUCT is the list structure."
999 (mapcar (lambda (e) (org-list-get-item-end (car e
) struct
)) struct
)))
1001 (defun org-list-get-list-begin (item struct prevs
)
1002 "Return point at beginning of sub-list ITEM belongs.
1003 STRUCT is the list structure. PREVS is the alist of previous
1004 items, as returned by `org-list-prevs-alist'."
1005 (let ((first-item item
) prev-item
)
1006 (while (setq prev-item
(org-list-get-prev-item first-item struct prevs
))
1007 (setq first-item prev-item
))
1010 (defalias 'org-list-get-first-item
'org-list-get-list-begin
)
1012 (defun org-list-get-last-item (item struct prevs
)
1013 "Return point at last item of sub-list ITEM belongs.
1014 STRUCT is the list structure. PREVS is the alist of previous
1015 items, as returned by `org-list-prevs-alist'."
1016 (let ((last-item item
) next-item
)
1017 (while (setq next-item
(org-list-get-next-item last-item struct prevs
))
1018 (setq last-item next-item
))
1021 (defun org-list-get-list-end (item struct prevs
)
1022 "Return point at end of sub-list ITEM belongs.
1023 STRUCT is the list structure. PREVS is the alist of previous
1024 items, as returned by `org-list-prevs-alist'."
1025 (org-list-get-item-end (org-list-get-last-item item struct prevs
) struct
))
1027 (defun org-list-get-list-type (item struct prevs
)
1028 "Return the type of the list containing ITEM, as a symbol.
1030 STRUCT is the list structure. PREVS is the alist of previous
1031 items, as returned by `org-list-prevs-alist'.
1033 Possible types are `descriptive', `ordered' and `unordered'. The
1034 type is determined by the first item of the list."
1035 (let ((first (org-list-get-list-begin item struct prevs
)))
1037 ((string-match-p "[[:alnum:]]" (org-list-get-bullet first struct
)) 'ordered
)
1038 ((org-list-get-tag first struct
) 'descriptive
)
1041 (defun org-list-get-item-number (item struct prevs parents
)
1042 "Return ITEM's sequence number.
1044 STRUCT is the list structure. PREVS is the alist of previous
1045 items, as returned by `org-list-prevs-alist'. PARENTS is the
1046 alist of ancestors, as returned by `org-list-parents-alist'.
1048 Return value is a list of integers. Counters have an impact on
1050 (let ((get-relative-number
1052 (lambda (item struct prevs
)
1053 ;; Return relative sequence number of ITEM in the sub-list
1054 ;; it belongs. STRUCT is the list structure. PREVS is
1055 ;; the alist of previous items.
1056 (let ((seq 0) (pos item
) counter
)
1057 (while (and (not (setq counter
(org-list-get-counter pos struct
)))
1058 (setq pos
(org-list-get-prev-item pos struct prevs
)))
1060 (if (not counter
) (1+ seq
)
1062 ((string-match "[A-Za-z]" counter
)
1063 (+ (- (string-to-char (upcase (match-string 0 counter
))) 64)
1065 ((string-match "[0-9]+" counter
)
1066 (+ (string-to-number (match-string 0 counter
)) seq
))
1068 ;; Cons each parent relative number into return value (OUT).
1069 (let ((out (list (funcall get-relative-number item struct prevs
)))
1071 (while (setq parent
(org-list-get-parent parent struct parents
))
1072 (push (funcall get-relative-number parent struct prevs
) out
))
1080 (defun org-list-search-generic (search re bound noerr
)
1081 "Search a string in valid contexts for lists.
1082 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1083 in `re-search-forward'."
1085 (let ((origin (point)))
1087 ;; 1. No match: return to origin or bound, depending on NOERR.
1088 (unless (funcall search re bound noerr
)
1089 (throw 'exit
(and (goto-char (if (memq noerr
'(t nil
)) origin bound
))
1091 ;; 2. Match in valid context: return point. Else, continue
1093 (when (org-list-in-valid-context-p) (throw 'exit
(point)))))))
1095 (defun org-list-search-backward (regexp &optional bound noerror
)
1096 "Like `re-search-backward' but stop only where lists are recognized.
1097 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1098 `re-search-backward'."
1099 (org-list-search-generic #'re-search-backward
1100 regexp
(or bound
(point-min)) noerror
))
1102 (defun org-list-search-forward (regexp &optional bound noerror
)
1103 "Like `re-search-forward' but stop only where lists are recognized.
1104 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1105 `re-search-forward'."
1106 (org-list-search-generic #'re-search-forward
1107 regexp
(or bound
(point-max)) noerror
))
1111 ;;; Methods on structures
1113 (defsubst org-list-bullet-string
(bullet)
1114 "Return BULLET with the correct number of whitespaces.
1115 It determines the number of whitespaces to append by looking at
1116 `org-list-two-spaces-after-bullet-regexp'."
1118 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1120 org-list-two-spaces-after-bullet-regexp bullet
))
1123 (if (string-match "\\S-+\\([ \t]*\\)" bullet
)
1124 (replace-match spaces nil nil bullet
1)
1127 (defun org-list-swap-items (beg-A beg-B struct
)
1128 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1130 Blank lines at the end of items are left in place. Item
1131 visibility is preserved. Return the new structure after the
1134 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1135 to the same sub-list.
1137 This function modifies STRUCT."
1139 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct
))
1140 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct
))
1141 (end-A (org-list-get-item-end beg-A struct
))
1142 (end-B (org-list-get-item-end beg-B struct
))
1143 (size-A (- end-A-no-blank beg-A
))
1144 (size-B (- end-B-no-blank beg-B
))
1145 (body-A (buffer-substring beg-A end-A-no-blank
))
1146 (body-B (buffer-substring beg-B end-B-no-blank
))
1147 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B
))
1148 (sub-A (cons beg-A
(org-list-get-subtree beg-A struct
)))
1149 (sub-B (cons beg-B
(org-list-get-subtree beg-B struct
)))
1150 ;; Store overlays responsible for visibility status. We
1151 ;; also need to store their boundaries as they will be
1152 ;; removed from buffer.
1157 (and (>= (overlay-start o
) beg-A
)
1158 (<= (overlay-end o
) end-A
)
1159 (list o
(overlay-start o
) (overlay-end o
))))
1160 (overlays-in beg-A end-A
)))
1163 (and (>= (overlay-start o
) beg-B
)
1164 (<= (overlay-end o
) end-B
)
1165 (list o
(overlay-start o
) (overlay-end o
))))
1166 (overlays-in beg-B end-B
))))))
1167 ;; 1. Move effectively items in buffer.
1169 (delete-region beg-A end-B-no-blank
)
1170 (insert (concat body-B between-A-no-blank-and-B body-A
))
1171 ;; 2. Now modify struct. No need to re-read the list, the
1172 ;; transformation is just a shift of positions. Some special
1173 ;; attention is required for items ending at END-A and END-B
1174 ;; as empty spaces are not moved there. In others words,
1175 ;; item BEG-A will end with whitespaces that were at the end
1176 ;; of BEG-B and the same applies to BEG-B.
1178 (let ((pos (car e
)))
1182 (let ((end-e (nth 6 e
)))
1183 (setcar e
(+ pos
(- end-B-no-blank end-A-no-blank
)))
1184 (setcar (nthcdr 6 e
)
1185 (+ end-e
(- end-B-no-blank end-A-no-blank
)))
1186 (when (= end-e end-A
) (setcar (nthcdr 6 e
) end-B
))))
1188 (let ((end-e (nth 6 e
)))
1189 (setcar e
(- (+ pos beg-A
) beg-B
))
1190 (setcar (nthcdr 6 e
) (+ end-e
(- beg-A beg-B
)))
1191 (when (= end-e end-B
)
1192 (setcar (nthcdr 6 e
)
1193 (+ beg-A size-B
(- end-A end-A-no-blank
))))))
1195 (let ((end-e (nth 6 e
)))
1196 (setcar e
(+ pos
(- size-B size-A
)))
1197 (setcar (nthcdr 6 e
) (+ end-e
(- size-B size-A
))))))))
1198 (setq struct
(sort struct
#'car-less-than-car
))
1199 ;; Restore visibility status, by moving overlays to their new
1201 (dolist (ov (car overlays
))
1204 (+ (nth 1 ov
) (- (+ beg-B
(- size-B size-A
)) beg-A
))
1205 (+ (nth 2 ov
) (- (+ beg-B
(- size-B size-A
)) beg-A
))))
1206 (dolist (ov (cdr overlays
))
1207 (move-overlay (car ov
)
1208 (+ (nth 1 ov
) (- beg-A beg-B
))
1209 (+ (nth 2 ov
) (- beg-A beg-B
))))
1210 ;; Return structure.
1213 (defun org-list-separating-blank-lines-number (pos struct prevs
)
1214 "Return number of blank lines that should separate items in list.
1216 POS is the position of point where `org-list-insert-item' was called.
1218 STRUCT is the list structure. PREVS is the alist of previous
1219 items, as returned by `org-list-prevs-alist'.
1221 Assume point is at item's beginning. If the item is alone, apply
1222 some heuristics to guess the result."
1224 (let ((item (point))
1226 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
1231 ;; Count blank lines above beginning of line.
1233 (count-lines (goto-char (point-at-bol))
1234 (progn (skip-chars-backward " \r\t\n")
1238 ;; Trivial cases where there should be none.
1239 ((not insert-blank-p
) 0)
1240 ;; When `org-blank-before-new-entry' says so, it is 1.
1241 ((eq insert-blank-p t
) 1)
1242 ;; `plain-list-item' is 'auto. Count blank lines separating
1243 ;; neighbors' items in list.
1244 (t (let ((next-p (org-list-get-next-item item struct prevs
)))
1246 ;; Is there a next item?
1247 (next-p (goto-char next-p
)
1248 (funcall count-blanks
))
1249 ;; Is there a previous item?
1250 ((org-list-get-prev-item item struct prevs
)
1251 (funcall count-blanks
))
1252 ;; User inserted blank lines, trust him.
1253 ((and (> pos
(org-list-get-item-end-before-blank item struct
))
1254 (> (save-excursion (goto-char pos
)
1255 (setq usr-blank
(funcall count-blanks
)))
1258 ;; Are there blank lines inside the list so far?
1260 (goto-char (org-list-get-top-point struct
))
1261 ;; Do not use `org-list-search-forward' so blank lines
1262 ;; in blocks can be counted in.
1264 "^[ \t]*$" (org-list-get-item-end-before-blank item struct
) t
))
1266 ;; Default choice: no blank line.
1269 (defun org-list-insert-item (pos struct prevs
&optional checkbox after-bullet
)
1270 "Insert a new list item at POS and return the new structure.
1271 If POS is before first character after bullet of the item, the
1272 new item will be created before the current one.
1274 STRUCT is the list structure. PREVS is the alist of previous
1275 items, as returned by `org-list-prevs-alist'.
1277 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1278 after the bullet. Cursor will be after this text once the
1281 This function modifies STRUCT."
1282 (let ((case-fold-search t
))
1283 ;; 1. Get information about list: position of point with regards
1284 ;; to item start (BEFOREP), blank lines number separating items
1285 ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
1286 (let* ((item (progn (goto-char pos
) (goto-char (org-list-get-item-begin))))
1287 (item-end (org-list-get-item-end item struct
))
1288 (item-end-no-blank (org-list-get-item-end-before-blank item struct
))
1291 (looking-at org-list-full-item-re
)
1294 ((not (match-beginning 4)) (match-end 0))
1295 ;; Ignore tag in a non-descriptive list.
1296 ((save-match-data (string-match "[.)]" (match-string 1)))
1297 (match-beginning 4))
1299 (goto-char (match-end 4))
1300 (skip-chars-forward " \t")
1302 (split-line-p (org-get-alist-option org-M-RET-may-split-line
'item
))
1303 (blank-nb (org-list-separating-blank-lines-number
1305 ;; 2. Build the new item to be created. Concatenate same
1306 ;; bullet as item, checkbox, text AFTER-BULLET if
1307 ;; provided, and text cut from point to end of item
1308 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1309 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1310 ;; between what was cut and what was inserted in buffer
1311 ;; is stored in SIZE-OFFSET.
1312 (ind (org-list-get-ind item struct
))
1313 (ind-size (if indent-tabs-mode
1314 (+ (/ ind tab-width
) (mod ind tab-width
))
1316 (bullet (org-list-bullet-string (org-list-get-bullet item struct
)))
1317 (box (when checkbox
"[ ]"))
1319 (and (not beforep
) split-line-p
1322 ;; If POS is greater than ITEM-END, then point is
1323 ;; in some white lines after the end of the list.
1324 ;; Those must be removed, or they will be left,
1325 ;; stacking up after the list.
1326 (when (< item-end pos
)
1327 (delete-region (1- item-end
) (point-at-eol)))
1328 (skip-chars-backward " \r\t\n")
1330 (delete-and-extract-region pos item-end-no-blank
))))
1331 (body (concat bullet
(when box
(concat box
" ")) after-bullet
1333 (if (string-match "\\`[ \t]+" text-cut
)
1334 (replace-match "" t t text-cut
)
1336 (item-sep (make-string (1+ blank-nb
) ?
\n))
1337 (item-size (+ ind-size
(length body
) (length item-sep
)))
1338 (size-offset (- item-size
(length text-cut
))))
1339 ;; 4. Insert effectively item into buffer.
1341 (indent-to-column ind
)
1342 (insert body item-sep
)
1343 ;; 5. Add new item to STRUCT.
1345 (let ((p (car e
)) (end (nth 6 e
)))
1347 ;; Before inserted item, positions don't change but
1348 ;; an item ending after insertion has its end shifted
1351 (when (> end item
) (setcar (nthcdr 6 e
) (+ end size-offset
))))
1352 ;; Trivial cases where current item isn't split in
1353 ;; two. Just shift every item after new one by
1355 ((or beforep
(not split-line-p
))
1356 (setcar e
(+ p item-size
))
1357 (setcar (nthcdr 6 e
) (+ end item-size
)))
1358 ;; Item is split in two: elements before POS are just
1359 ;; shifted by ITEM-SIZE. In the case item would end
1360 ;; after split POS, ending is only shifted by
1363 (setcar e
(+ p item-size
))
1365 (setcar (nthcdr 6 e
) (+ end item-size
))
1366 (setcar (nthcdr 6 e
) (+ end size-offset
))))
1367 ;; Elements after POS are moved into new item.
1368 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1369 ;; doesn't appear in buffer yet.
1371 (setcar e
(+ p size-offset
(- item pos
(length item-sep
))))
1372 (if (= end item-end
)
1373 (setcar (nthcdr 6 e
) (+ item item-size
))
1374 (setcar (nthcdr 6 e
)
1376 (- item pos
(length item-sep
))))))
1377 ;; Elements at ITEM-END or after are only shifted by
1379 (t (setcar e
(+ p size-offset
))
1380 (setcar (nthcdr 6 e
) (+ end size-offset
))))))
1382 (push (list item ind bullet nil box nil
(+ item item-size
)) struct
)
1383 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1384 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1385 ;; exchange ITEM with the next item in list. Position cursor
1386 ;; after bullet, counter, checkbox, and label.
1389 (setq struct
(org-list-swap-items item
(+ item item-size
) struct
))
1390 (goto-char (org-list-get-next-item
1391 item struct
(org-list-prevs-alist struct
))))
1394 (defun org-list-delete-item (item struct
)
1395 "Remove ITEM from the list and return the new structure.
1397 STRUCT is the list structure."
1398 (let* ((end (org-list-get-item-end item struct
))
1399 (beg (if (= (org-list-get-bottom-point struct
) end
)
1400 ;; If ITEM ends with the list, delete blank lines
1404 (skip-chars-backward " \r\t\n")
1405 (min (1+ (point-at-eol)) (point-max)))
1407 ;; Remove item from buffer.
1408 (delete-region beg end
)
1409 ;; Remove item from structure and shift others items accordingly.
1410 ;; Don't forget to shift also ending position when appropriate.
1411 (let ((size (- end beg
)))
1412 (delq nil
(mapcar (lambda (e)
1413 (let ((pos (car e
)))
1416 (let ((end-e (nth 6 e
)))
1420 (append (butlast e
) (list beg
)))
1422 (append (butlast e
) (list (- end-e size
)))))))
1426 (append (butlast (cdr e
))
1427 (list (- (nth 6 e
) size
))))))))
1430 (defun org-list-send-item (item dest struct
)
1431 "Send ITEM to destination DEST.
1433 STRUCT is the list structure.
1435 DEST can have various values.
1437 If DEST is a buffer position, the function will assume it points
1438 to another item in the same list as ITEM, and will move the
1439 latter just before the former.
1441 If DEST is `begin' (respectively `end'), ITEM will be moved at
1442 the beginning (respectively end) of the list it belongs to.
1444 If DEST is a string like \"N\", where N is an integer, ITEM will
1445 be moved at the Nth position in the list.
1447 If DEST is `kill', ITEM will be deleted and its body will be
1448 added to the kill-ring.
1450 If DEST is `delete', ITEM will be deleted.
1452 Visibility of item is preserved.
1454 This function returns, destructively, the new list structure."
1455 (let* ((prevs (org-list-prevs-alist struct
))
1456 (item-end (org-list-get-item-end item struct
))
1457 ;; Grab full item body minus its bullet.
1464 (regexp-quote (org-list-get-bullet item struct
))))
1467 ;; Change DEST into a buffer position. A trick is needed
1468 ;; when ITEM is meant to be sent at the end of the list.
1469 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1470 ;; nil and insertion point (INS-POINT) to the first line's
1471 ;; end of the last item, we ensure the new item will be
1472 ;; inserted after the last item, and not after any of its
1473 ;; hypothetical sub-items.
1475 ((or (eq dest
'kill
) (eq dest
'delete
)))
1477 (setq dest
(org-list-get-list-begin item struct prevs
)))
1479 (setq dest
(org-list-get-list-end item struct prevs
))
1481 (goto-char (org-list-get-last-item item struct prevs
))
1483 ((string-match-p "\\`[0-9]+\\'" dest
)
1484 (let* ((all (org-list-get-all-items item struct prevs
))
1486 (index (mod (string-to-number dest
) len
)))
1487 (if (not (zerop index
))
1488 (setq dest
(nth (1- index
) all
))
1489 ;; Send ITEM at the end of the list.
1490 (setq dest
(org-list-get-list-end item struct prevs
))
1493 (org-list-get-last-item item struct prevs
))
1496 (org-M-RET-may-split-line nil
)
1497 ;; Store inner overlays (to preserve visibility).
1498 (overlays (cl-remove-if (lambda (o) (or (< (overlay-start o
) item
)
1499 (> (overlay-end o
) item
)))
1500 (overlays-in item item-end
))))
1502 ((eq dest
'delete
) (org-list-delete-item item struct
))
1505 (org-list-delete-item item struct
))
1506 ((and (integerp dest
) (/= item ins-point
))
1507 (setq item
(copy-marker item
))
1508 (setq struct
(org-list-insert-item ins-point struct prevs nil body
))
1509 ;; 1. Structure returned by `org-list-insert-item' may not be
1510 ;; accurate, as it cannot see sub-items included in BODY.
1511 ;; Thus, first compute the real structure so far.
1513 (cons (marker-position item
)
1514 (org-list-get-subtree (marker-position item
) struct
)))
1515 (new-end (org-list-get-item-end (point) struct
))
1516 (old-end (org-list-get-item-end (marker-position item
) struct
))
1518 (shift (- (point) item
)))
1519 ;; 1.1. Remove the item just created in structure.
1520 (setq struct
(delete (assq new-item struct
) struct
))
1521 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1526 (let* ((cell (assq e struct
))
1530 (append (butlast (cdr cell
))
1531 (list (if (= end old-end
)
1535 #'car-less-than-car
)))
1536 ;; 2. Restore inner overlays.
1537 (dolist (o overlays
)
1539 (+ (overlay-start o
) (- (point) item
))
1540 (+ (overlay-end o
) (- (point) item
))))
1541 ;; 3. Eventually delete extra copy of the item and clean marker.
1542 (prog1 (org-list-delete-item (marker-position item
) struct
)
1543 (move-marker item nil
)))
1546 (defun org-list-struct-outdent (start end struct parents
)
1547 "Outdent items between positions START and END.
1549 STRUCT is the list structure. PARENTS is the alist of items'
1550 parents, as returned by `org-list-parents-alist'.
1552 START is included, END excluded."
1555 (let* ((item (car cell
))
1556 (parent (cdr cell
)))
1558 ;; Item not yet in zone: keep association.
1559 ((< item start
) cell
)
1560 ;; Item out of zone: follow associations in ACC.
1562 (let ((convert (and parent
(assq parent acc
))))
1563 (if convert
(cons item
(cdr convert
)) cell
)))
1564 ;; Item has no parent: error
1566 (error "Cannot outdent top-level items"))
1567 ;; Parent is outdented: keep association.
1569 (push (cons parent item
) acc
) cell
)
1571 ;; Parent isn't outdented: reparent to grand-parent.
1572 (let ((grand-parent (org-list-get-parent
1573 parent struct parents
)))
1574 (push (cons parent item
) acc
)
1575 (cons item grand-parent
))))))))
1576 (mapcar out parents
)))
1578 (defun org-list-struct-indent (start end struct parents prevs
)
1579 "Indent items between positions START and END.
1581 STRUCT is the list structure. PARENTS is the alist of parents
1582 and PREVS is the alist of previous items, returned by,
1583 respectively, `org-list-parents-alist' and
1584 `org-list-prevs-alist'.
1586 START is included and END excluded.
1588 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1589 bullets between START and END."
1591 (set-assoc (lambda (cell) (push cell acc
) cell
))
1592 (change-bullet-maybe
1597 ;; Normalize ordered bullets.
1598 (let ((bul (org-trim
1599 (org-list-get-bullet item struct
))))
1600 (cond ((string-match "[A-Z]\\." bul
) "A.")
1601 ((string-match "[A-Z])" bul
) "A)")
1602 ((string-match "[a-z]\\." bul
) "a.")
1603 ((string-match "[a-z])" bul
) "a)")
1604 ((string-match "[0-9]\\." bul
) "1.")
1605 ((string-match "[0-9])" bul
) "1)")
1607 org-list-demote-modify-bullet
))))
1608 (when new-bul-p
(org-list-set-bullet item struct new-bul-p
))))))
1611 (let* ((item (car cell
))
1612 (parent (cdr cell
)))
1614 ;; Item not yet in zone: keep association.
1615 ((< item start
) cell
)
1617 ;; Item out of zone: follow associations in ACC.
1618 (let ((convert (assq parent acc
)))
1619 (if convert
(cons item
(cdr convert
)) cell
)))
1621 ;; Item is in zone...
1622 (let ((prev (org-list-get-prev-item item struct prevs
)))
1623 ;; Check if bullet needs to be changed.
1624 (funcall change-bullet-maybe item
)
1626 ;; First item indented but not parent: error
1627 ((and (not prev
) (< parent start
))
1628 (error "Cannot indent the first item of a list"))
1629 ;; First item and parent indented: keep same
1631 ((not prev
) (funcall set-assoc cell
))
1632 ;; Previous item not indented: reparent to it.
1633 ((< prev start
) (funcall set-assoc
(cons item prev
)))
1634 ;; Previous item indented: reparent like it.
1637 (cons item
(cdr (assq prev acc
)))))))))))))
1638 (mapcar ind parents
)))
1642 ;;; Repairing structures
1644 (defun org-list-use-alpha-bul-p (first struct prevs
)
1645 "Non-nil if list starting at FIRST can have alphabetical bullets.
1647 STRUCT is list structure. PREVS is the alist of previous items,
1648 as returned by `org-list-prevs-alist'."
1649 (and org-list-allow-alphabetical
1651 (let ((item first
) (ascii 64) (case-fold-search nil
))
1652 ;; Pretend that bullets are uppercase and check if alphabet
1653 ;; is sufficient, taking counters into account.
1655 (let ((count (org-list-get-counter item struct
)))
1656 ;; Virtually determine current bullet
1657 (if (and count
(string-match-p "[a-zA-Z]" count
))
1658 ;; Counters are not case-sensitive.
1659 (setq ascii
(string-to-char (upcase count
)))
1660 (setq ascii
(1+ ascii
)))
1661 ;; Test if bullet would be over z or Z.
1664 (setq item
(org-list-get-next-item item struct prevs
)))))
1665 ;; All items checked. All good.
1668 (defun org-list-inc-bullet-maybe (bullet)
1669 "Increment BULLET if applicable."
1670 (let ((case-fold-search nil
))
1672 ;; Num bullet: increment it.
1673 ((string-match "[0-9]+" bullet
)
1675 (number-to-string (1+ (string-to-number (match-string 0 bullet
))))
1677 ;; Alpha bullet: increment it.
1678 ((string-match "[A-Za-z]" bullet
)
1680 (char-to-string (1+ (string-to-char (match-string 0 bullet
))))
1682 ;; Unordered bullet: leave it.
1685 (defun org-list-struct-fix-bul (struct prevs
)
1686 "Verify and correct bullets in STRUCT.
1687 PREVS is the alist of previous items, as returned by
1688 `org-list-prevs-alist'.
1690 This function modifies STRUCT."
1691 (let ((case-fold-search nil
)
1694 ;; Set bullet of ITEM in STRUCT, depending on the type of
1695 ;; first item of the list, the previous bullet and counter
1698 (let* ((prev (org-list-get-prev-item item struct prevs
))
1699 (prev-bul (and prev
(org-list-get-bullet prev struct
)))
1700 (counter (org-list-get-counter item struct
))
1701 (bullet (org-list-get-bullet item struct
))
1702 (alphap (and (not prev
)
1703 (org-list-use-alpha-bul-p item struct prevs
))))
1704 (org-list-set-bullet
1706 (org-list-bullet-string
1708 ;; Alpha counter in alpha list: use counter.
1710 (string-match "[a-zA-Z]" counter
)
1711 (string-match "[a-zA-Z]" prev-bul
))
1712 ;; Use cond to be sure `string-match' is used in
1716 ((string-match "[a-z]" prev-bul
) (downcase counter
))
1717 ((string-match "[A-Z]" prev-bul
) (upcase counter
)))))
1718 (replace-match real-count nil nil prev-bul
)))
1719 ;; Num counter in a num list: use counter.
1721 (string-match "[0-9]+" counter
)
1722 (string-match "[0-9]+" prev-bul
))
1723 (replace-match counter nil nil prev-bul
))
1724 ;; No counter: increase, if needed, previous bullet.
1726 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct
)))
1727 ;; Alpha counter at first item: use counter.
1728 ((and counter
(org-list-use-alpha-bul-p item struct prevs
)
1729 (string-match "[A-Za-z]" counter
)
1730 (string-match "[A-Za-z]" bullet
))
1733 ((string-match "[a-z]" bullet
) (downcase counter
))
1734 ((string-match "[A-Z]" bullet
) (upcase counter
)))))
1735 (replace-match real-count nil nil bullet
)))
1736 ;; Num counter at first item: use counter.
1738 (string-match "[0-9]+" counter
)
1739 (string-match "[0-9]+" bullet
))
1740 (replace-match counter nil nil bullet
))
1741 ;; First bullet is alpha uppercase: use "A".
1742 ((and alphap
(string-match "[A-Z]" bullet
))
1743 (replace-match "A" nil nil bullet
))
1744 ;; First bullet is alpha lowercase: use "a".
1745 ((and alphap
(string-match "[a-z]" bullet
))
1746 (replace-match "a" nil nil bullet
))
1747 ;; First bullet is num: use "1".
1748 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet
)
1749 (replace-match "1" nil nil bullet
))
1750 ;; Not an ordered list: keep bullet.
1752 (mapc fix-bul
(mapcar #'car struct
))))
1754 (defun org-list-struct-fix-ind (struct parents
&optional bullet-size
)
1755 "Verify and correct indentation in STRUCT.
1757 PARENTS is the alist of parents, as returned by
1758 `org-list-parents-alist'.
1760 If numeric optional argument BULLET-SIZE is set, assume all
1761 bullets in list have this length to determine new indentation.
1763 This function modifies STRUCT."
1764 (let* ((ancestor (org-list-get-top-point struct
))
1765 (top-ind (org-list-get-ind ancestor struct
))
1768 (let ((parent (org-list-get-parent item struct parents
)))
1770 ;; Indent like parent + length of parent's bullet +
1773 item struct
(+ (or bullet-size
1775 (org-list-get-bullet parent struct
)))
1776 (org-list-get-ind parent struct
)
1777 org-list-indent-offset
))
1778 ;; If no parent, indent like top-point.
1779 (org-list-set-ind item struct top-ind
))))))
1780 (mapc new-ind
(mapcar #'car
(cdr struct
)))))
1782 (defun org-list-struct-fix-box (struct parents prevs
&optional ordered
)
1783 "Verify and correct checkboxes in STRUCT.
1785 PARENTS is the alist of parents and PREVS is the alist of
1786 previous items, as returned by, respectively,
1787 `org-list-parents-alist' and `org-list-prevs-alist'.
1789 If ORDERED is non-nil, a checkbox can only be checked when every
1790 checkbox before it is checked too. If there was an attempt to
1791 break this rule, the function will return the blocking item. In
1792 all others cases, the return value will be nil.
1794 This function modifies STRUCT."
1795 (let ((all-items (mapcar #'car struct
))
1800 (mapcar (lambda (child)
1801 (org-list-get-checkbox child struct
))
1802 (org-list-get-children item struct parents
))))
1803 (org-list-set-checkbox
1806 ((and (member "[ ]" box-list
) (member "[X]" box-list
)) "[-]")
1807 ((member "[-]" box-list
) "[-]")
1808 ((member "[X]" box-list
) "[X]")
1809 ((member "[ ]" box-list
) "[ ]")
1810 ;; Parent has no boxed child: leave box as-is.
1811 (t (org-list-get-checkbox item struct
))))))))
1813 ;; 1. List all parents with a checkbox.
1816 (let* ((parent (org-list-get-parent e struct parents
))
1817 (parent-box-p (org-list-get-checkbox parent struct
)))
1818 (when (and parent-box-p
(not (memq parent parent-list
)))
1819 (push parent parent-list
))))
1821 ;; 2. Sort those parents by decreasing indentation.
1822 (setq parent-list
(sort parent-list
1824 (> (org-list-get-ind e1 struct
)
1825 (org-list-get-ind e2 struct
)))))
1826 ;; 3. For each parent, get all children's checkboxes to determine
1827 ;; and set its checkbox accordingly.
1828 (mapc set-parent-box parent-list
)
1829 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1832 (mapcar (lambda (e) (org-list-get-checkbox e struct
)) all-items
))
1833 (after-unchecked (member "[ ]" box-list
)))
1834 ;; There are boxes checked after an unchecked one: fix that.
1835 (when (member "[X]" after-unchecked
)
1836 (let ((index (- (length struct
) (length after-unchecked
))))
1838 (when (org-list-get-checkbox e struct
)
1839 (org-list-set-checkbox e struct
"[ ]")))
1840 (nthcdr index all-items
))
1841 ;; Verify once again the structure, without ORDERED.
1842 (org-list-struct-fix-box struct parents prevs nil
)
1843 ;; Return blocking item.
1844 (nth index all-items
)))))))
1846 (defun org-list-struct-fix-item-end (struct)
1847 "Verify and correct each item end position in STRUCT.
1849 This function modifies STRUCT."
1850 (let (end-list acc-end
)
1852 (let* ((pos (car e
))
1853 (ind-pos (org-list-get-ind pos struct
))
1854 (end-pos (org-list-get-item-end pos struct
)))
1855 (unless (assq end-pos struct
)
1856 ;; To determine real ind of an ending position that is
1857 ;; not at an item, we have to find the item it belongs
1858 ;; to: it is the last item (ITEM-UP), whose ending is
1859 ;; further than the position we're interested in.
1860 (let ((item-up (assoc-default end-pos acc-end
'>)))
1862 ;; Else part is for the bottom point.
1863 (if item-up
(+ (org-list-get-ind item-up struct
) 2) 0)
1866 (push (cons ind-pos pos
) end-list
)
1867 (push (cons end-pos pos
) acc-end
)))
1869 (setq end-list
(sort end-list
(lambda (e1 e2
) (< (cdr e1
) (cdr e2
)))))
1870 (org-list-struct-assoc-end struct end-list
)))
1872 (defun org-list-struct-apply-struct (struct old-struct
)
1873 "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
1875 OLD-STRUCT is the structure before any modifications, and STRUCT
1876 the structure to be applied. The function will only modify parts
1877 of the list which have changed.
1879 Initial position of cursor is restored after the changes."
1880 (let* ((origin (point-marker))
1881 (inlinetask-re (and (featurep 'org-inlinetask
)
1882 (org-inlinetask-outline-regexp)))
1883 (item-re (org-item-re))
1886 ;; Shift the indentation between END and BEG by DELTA.
1887 ;; Start from the line before END.
1888 (lambda (end beg delta
)
1890 (skip-chars-backward " \r\t\n")
1892 (while (or (> (point) beg
)
1893 (and (= (point) beg
)
1894 (not (looking-at item-re
))))
1896 ;; Skip inline tasks.
1897 ((and inlinetask-re
(looking-at inlinetask-re
))
1898 (org-inlinetask-goto-beginning))
1899 ;; Shift only non-empty lines.
1900 ((looking-at-p "^[ \t]*\\S-")
1901 (indent-line-to (+ (org-get-indentation) delta
))))
1902 (forward-line -
1)))))
1905 ;; Replace ITEM first line elements with new elements from
1906 ;; STRUCT, if appropriate.
1909 (let* ((new-ind (org-list-get-ind item struct
))
1910 (old-ind (org-get-indentation))
1911 (new-bul (org-list-bullet-string
1912 (org-list-get-bullet item struct
)))
1913 (old-bul (org-list-get-bullet item old-struct
))
1914 (new-box (org-list-get-checkbox item struct
)))
1915 (looking-at org-list-full-item-re
)
1916 ;; a. Replace bullet
1917 (unless (equal old-bul new-bul
)
1918 (replace-match new-bul nil nil nil
1))
1919 ;; b. Replace checkbox.
1921 ((equal (match-string 3) new-box
))
1922 ((and (match-string 3) new-box
)
1923 (replace-match new-box nil nil nil
3))
1925 (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
1926 (replace-match "" nil nil nil
1))
1927 (t (let ((counterp (match-end 2)))
1928 (goto-char (if counterp
(1+ counterp
) (match-end 1)))
1929 (insert (concat new-box
(unless counterp
" "))))))
1930 ;; c. Indent item to appropriate column.
1931 (unless (= new-ind old-ind
)
1932 (delete-region (goto-char (point-at-bol))
1933 (progn (skip-chars-forward " \t") (point)))
1934 (indent-to new-ind
)))))))
1935 ;; 1. First get list of items and position endings. We maintain
1936 ;; two alists: ITM-SHIFT, determining indentation shift needed
1937 ;; at item, and END-LIST, a pseudo-alist where key is ending
1938 ;; position and value point.
1939 (let (end-list acc-end itm-shift all-ends sliced-struct
)
1940 (dolist (e old-struct
)
1941 (let* ((pos (car e
))
1942 (ind-pos (org-list-get-ind pos struct
))
1943 (ind-old (org-list-get-ind pos old-struct
))
1944 (bul-pos (org-list-get-bullet pos struct
))
1945 (bul-old (org-list-get-bullet pos old-struct
))
1946 (ind-shift (- (+ ind-pos
(length bul-pos
))
1947 (+ ind-old
(length bul-old
))))
1948 (end-pos (org-list-get-item-end pos old-struct
)))
1949 (push (cons pos ind-shift
) itm-shift
)
1950 (unless (assq end-pos old-struct
)
1951 ;; To determine real ind of an ending position that
1952 ;; is not at an item, we have to find the item it
1953 ;; belongs to: it is the last item (ITEM-UP), whose
1954 ;; ending is further than the position we're
1956 (let ((item-up (assoc-default end-pos acc-end
#'>)))
1957 (push (cons end-pos item-up
) end-list
)))
1958 (push (cons end-pos pos
) acc-end
)))
1959 ;; 2. Slice the items into parts that should be shifted by the
1960 ;; same amount of indentation. Each slice follow the pattern
1961 ;; (END BEG DELTA). Slices are returned in reverse order.
1962 (setq all-ends
(sort (append (mapcar #'car itm-shift
)
1963 (org-uniquify (mapcar #'car end-list
)))
1965 acc-end
(nreverse acc-end
))
1966 (while (cdr all-ends
)
1967 (let* ((up (pop all-ends
))
1968 (down (car all-ends
))
1969 (itemp (assq up struct
))
1971 (if itemp
(cdr (assq up itm-shift
))
1972 ;; If we're not at an item, there's a child of the
1973 ;; item point belongs to above. Make sure the less
1974 ;; indented line in this slice has the same column
1976 (let* ((child (cdr (assq up acc-end
)))
1977 (ind (org-list-get-ind child struct
))
1978 (min-ind most-positive-fixnum
))
1981 (while (< (point) down
)
1982 ;; Ignore empty lines. Also ignore blocks and
1983 ;; drawers contents.
1984 (unless (looking-at-p "[ \t]*$")
1985 (setq min-ind
(min (org-get-indentation) min-ind
))
1987 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1989 (format "^[ \t]*#\\+END%s[ \t]*$"
1992 ((and (looking-at org-drawer-regexp
)
1993 (re-search-forward "^[ \t]*:END:[ \t]*$"
1997 (push (list down up delta
) sliced-struct
)))
1998 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1999 ;; end to beginning. Take a special action when beginning is
2001 (dolist (e sliced-struct
)
2002 (unless (zerop (nth 2 e
)) (apply shift-body-ind e
))
2003 (let* ((beg (nth 1 e
))
2004 (cell (assq beg struct
)))
2005 (unless (or (not cell
) (equal cell
(assq beg old-struct
)))
2006 (funcall modify-item beg
)))))
2007 ;; 4. Go back to initial position and clean marker.
2009 (move-marker origin nil
)))
2011 (defun org-list-write-struct (struct parents
&optional old-struct
)
2012 "Correct bullets, checkboxes and indentation in list at point.
2014 STRUCT is the list structure. PARENTS is the alist of parents,
2015 as returned by `org-list-parents-alist'.
2017 When non-nil, optional argument OLD-STRUCT is the reference
2018 structure of the list. It should be provided whenever STRUCT
2019 doesn't correspond anymore to the real list in buffer."
2020 ;; Order of functions matters here: checkboxes and endings need
2021 ;; correct indentation to be set, and indentation needs correct
2024 ;; 0. Save a copy of structure before modifications
2025 (let ((old-struct (or old-struct
(copy-tree struct
))))
2026 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
2027 ;; order to get items endings and bullets properly
2028 (org-list-struct-fix-ind struct parents
2)
2029 ;; 2. Fix each item end to get correct prevs alist.
2030 (org-list-struct-fix-item-end struct
)
2031 ;; 3. Get bullets right.
2032 (let ((prevs (org-list-prevs-alist struct
)))
2033 (org-list-struct-fix-bul struct prevs
)
2034 ;; 4. Now get real indentation.
2035 (org-list-struct-fix-ind struct parents
)
2036 ;; 5. Eventually fix checkboxes.
2037 (org-list-struct-fix-box struct parents prevs
))
2038 ;; 6. Apply structure modifications to buffer.
2039 (org-list-struct-apply-struct struct old-struct
)))
2045 (defun org-apply-on-list (function init-value
&rest args
)
2046 "Call FUNCTION on each item of the list at point.
2047 FUNCTION must be called with at least one argument: INIT-VALUE,
2048 that will contain the value returned by the function at the
2049 previous item, plus ARGS extra arguments.
2051 FUNCTION is applied on items in reverse order.
2053 As an example, \(org-apply-on-list \(lambda \(result) \(1+ result)) 0)
2054 will return the number of items in the current list.
2056 Sublists of the list are skipped. Cursor is always at the
2057 beginning of the item."
2058 (let* ((struct (org-list-struct))
2059 (prevs (org-list-prevs-alist struct
))
2060 (item (copy-marker (point-at-bol)))
2061 (all (org-list-get-all-items (marker-position item
) struct prevs
))
2065 (setq value
(apply function value args
)))
2068 (move-marker item nil
)
2071 (defun org-list-set-item-visibility (item struct view
)
2072 "Set visibility of ITEM in STRUCT to VIEW.
2074 Possible values are: `folded', `children' or `subtree'. See
2075 `org-cycle' for more information."
2078 (let ((item-end (org-list-get-item-end-before-blank item struct
)))
2080 (outline-flag-region (save-excursion (goto-char item
) (point-at-eol))
2082 ((eq view
'children
)
2083 ;; First show everything.
2084 (org-list-set-item-visibility item struct
'subtree
)
2085 ;; Then fold every child.
2086 (let* ((parents (org-list-parents-alist struct
))
2087 (children (org-list-get-children item struct parents
)))
2089 (org-list-set-item-visibility e struct
'folded
))
2093 (let ((item-end (org-list-get-item-end item struct
)))
2094 (outline-flag-region item item-end nil
)))))
2096 (defun org-list-item-body-column (item)
2097 "Return column at which body of ITEM should start."
2103 "[ \t]::\\([ \t]\\|$\\)" (line-beginning-position) t
))
2104 ;; Descriptive list item. Body starts after item's tag, if
2106 (let ((start (1+ (- (match-beginning 1) (line-beginning-position))))
2107 (ind (org-get-indentation)))
2108 (if (> start
(+ ind org-list-description-max-indent
))
2111 ;; Regular item. Body starts after bullet.
2112 (looking-at "[ \t]*\\(\\S-+\\)")
2113 (+ (progn (goto-char (match-end 1)) (current-column))
2114 (if (and org-list-two-spaces-after-bullet-regexp
2115 (string-match-p org-list-two-spaces-after-bullet-regexp
2122 ;;; Interactive functions
2124 (defalias 'org-list-get-item-begin
'org-in-item-p
)
2126 (defun org-beginning-of-item ()
2127 "Go to the beginning of the current item.
2128 Throw an error when not in a list."
2130 (let ((begin (org-in-item-p)))
2131 (if begin
(goto-char begin
) (error "Not in an item"))))
2133 (defun org-beginning-of-item-list ()
2134 "Go to the beginning item of the current list or sublist.
2135 Throw an error when not in a list."
2137 (let ((begin (org-in-item-p)))
2139 (error "Not in an item")
2141 (let* ((struct (org-list-struct))
2142 (prevs (org-list-prevs-alist struct
)))
2143 (goto-char (org-list-get-list-begin begin struct prevs
))))))
2145 (defun org-end-of-item-list ()
2146 "Go to the end of the current list or sublist.
2147 Throw an error when not in a list."
2149 (let ((begin (org-in-item-p)))
2151 (error "Not in an item")
2153 (let* ((struct (org-list-struct))
2154 (prevs (org-list-prevs-alist struct
)))
2155 (goto-char (org-list-get-list-end begin struct prevs
))))))
2157 (defun org-end-of-item ()
2158 "Go to the end of the current item.
2159 Throw an error when not in a list."
2161 (let ((begin (org-in-item-p)))
2163 (error "Not in an item")
2165 (let ((struct (org-list-struct)))
2166 (goto-char (org-list-get-item-end begin struct
))))))
2168 (defun org-previous-item ()
2169 "Move to the beginning of the previous item.
2170 Throw an error when not in a list. Also throw an error when at
2171 first item, unless `org-list-use-circular-motion' is non-nil."
2173 (let ((item (org-in-item-p)))
2175 (error "Not in an item")
2177 (let* ((struct (org-list-struct))
2178 (prevs (org-list-prevs-alist struct
))
2179 (prevp (org-list-get-prev-item item struct prevs
)))
2181 (prevp (goto-char prevp
))
2182 (org-list-use-circular-motion
2183 (goto-char (org-list-get-last-item item struct prevs
)))
2184 (t (error "On first item")))))))
2186 (defun org-next-item ()
2187 "Move to the beginning of the next item.
2188 Throw an error when not in a list. Also throw an error when at
2189 last item, unless `org-list-use-circular-motion' is non-nil."
2191 (let ((item (org-in-item-p)))
2193 (error "Not in an item")
2195 (let* ((struct (org-list-struct))
2196 (prevs (org-list-prevs-alist struct
))
2197 (prevp (org-list-get-next-item item struct prevs
)))
2199 (prevp (goto-char prevp
))
2200 (org-list-use-circular-motion
2201 (goto-char (org-list-get-first-item item struct prevs
)))
2202 (t (error "On last item")))))))
2204 (defun org-move-item-down ()
2205 "Move the item at point down, i.e. swap with following item.
2206 Sub-items (items with larger indentation) are considered part of
2207 the item, so this really moves item trees."
2209 (unless (org-at-item-p) (error "Not at an item"))
2210 (let* ((col (current-column))
2211 (item (point-at-bol))
2212 (struct (org-list-struct))
2213 (prevs (org-list-prevs-alist struct
))
2214 (next-item (org-list-get-next-item (point-at-bol) struct prevs
)))
2215 (unless (or next-item org-list-use-circular-motion
)
2216 (user-error "Cannot move this item further down"))
2218 (setq struct
(org-list-send-item item
'begin struct
))
2219 (setq struct
(org-list-swap-items item next-item struct
))
2221 (org-list-get-next-item item struct
(org-list-prevs-alist struct
))))
2222 (org-list-write-struct struct
(org-list-parents-alist struct
))
2223 (org-move-to-column col
)))
2225 (defun org-move-item-up ()
2226 "Move the item at point up, i.e. swap with previous item.
2227 Sub-items (items with larger indentation) are considered part of
2228 the item, so this really moves item trees."
2230 (unless (org-at-item-p) (error "Not at an item"))
2231 (let* ((col (current-column))
2232 (item (point-at-bol))
2233 (struct (org-list-struct))
2234 (prevs (org-list-prevs-alist struct
))
2235 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs
)))
2236 (unless (or prev-item org-list-use-circular-motion
)
2237 (user-error "Cannot move this item further up"))
2239 (setq struct
(org-list-send-item item
'end struct
))
2240 (setq struct
(org-list-swap-items prev-item item struct
)))
2241 (org-list-write-struct struct
(org-list-parents-alist struct
))
2242 (org-move-to-column col
)))
2244 (defun org-insert-item (&optional checkbox
)
2245 "Insert a new item at the current level.
2246 If cursor is before first character after bullet of the item, the
2247 new item will be created before the current one.
2249 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2251 Return t when things worked, nil when we are not in an item, or
2253 (let ((itemp (org-in-item-p))
2255 ;; If cursor isn't is a list or if list is invisible, return nil.
2256 (unless (or (not itemp
)
2262 (org-at-item-timer-p))
2263 ;; Timer list: delegate to `org-timer-item'.
2264 (progn (org-timer-item) t
)
2265 (let* ((struct (save-excursion (goto-char itemp
)
2267 (prevs (org-list-prevs-alist struct
))
2268 ;; If we're in a description list, ask for the new term.
2269 (desc (when (eq (org-list-get-list-type itemp struct prevs
)
2272 (setq struct
(org-list-insert-item pos struct prevs checkbox desc
))
2273 (org-list-write-struct struct
(org-list-parents-alist struct
))
2274 (when checkbox
(org-update-checkbox-count-maybe))
2275 (looking-at org-list-full-item-re
)
2276 (goto-char (if (and (match-beginning 4)
2278 (string-match "[.)]" (match-string 1))))
2281 (if desc
(backward-char 1))
2284 (defun org-list-repair ()
2285 "Fix indentation, bullets and checkboxes in the list at point."
2287 (unless (org-at-item-p) (error "This is not a list"))
2288 (let* ((struct (org-list-struct))
2289 (parents (org-list-parents-alist struct
)))
2290 (org-list-write-struct struct parents
)))
2292 (defun org-cycle-list-bullet (&optional which
)
2293 "Cycle through the different itemize/enumerate bullets.
2294 This cycle the entire list level through the sequence:
2296 `-' -> `+' -> `*' -> `1.' -> `1)'
2298 If WHICH is a valid string, use that as the new bullet. If WHICH
2299 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2300 `previous', cycle backwards."
2302 (unless (org-at-item-p) (error "Not at an item"))
2305 (let* ((struct (org-list-struct))
2306 (parents (org-list-parents-alist struct
))
2307 (prevs (org-list-prevs-alist struct
))
2308 (list-beg (org-list-get-first-item (point) struct prevs
))
2309 (bullet (org-list-get-bullet list-beg struct
))
2310 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs
))
2311 (case-fold-search nil
)
2313 ((string-match "[a-z]\\." bullet
) "a.")
2314 ((string-match "[a-z])" bullet
) "a)")
2315 ((string-match "[A-Z]\\." bullet
) "A.")
2316 ((string-match "[A-Z])" bullet
) "A)")
2317 ((string-match "\\." bullet
) "1.")
2318 ((string-match ")" bullet
) "1)")
2319 (t (org-trim bullet
))))
2320 ;; Compute list of possible bullets, depending on context.
2323 ;; *-bullets are not allowed at column 0.
2324 (unless (looking-at "\\S-") '("*"))
2325 ;; Description items cannot be numbered.
2326 (unless (or (eq org-plain-list-ordered-item-terminator ?\
))
2327 (org-at-item-description-p))
2329 (unless (or (eq org-plain-list-ordered-item-terminator ?.
)
2330 (org-at-item-description-p))
2332 (unless (or (not alpha-p
)
2333 (eq org-plain-list-ordered-item-terminator ?\
))
2334 (org-at-item-description-p))
2336 (unless (or (not alpha-p
)
2337 (eq org-plain-list-ordered-item-terminator ?.
)
2338 (org-at-item-description-p))
2340 (len (length bullet-list
))
2341 (item-index (- len
(length (member current bullet-list
))))
2342 (get-value (lambda (index) (nth (mod index len
) bullet-list
)))
2344 ((member which bullet-list
) which
)
2345 ((numberp which
) (funcall get-value which
))
2346 ((eq 'previous which
) (funcall get-value
(1- item-index
)))
2347 (t (funcall get-value
(1+ item-index
))))))
2348 ;; Use a short variation of `org-list-write-struct' as there's
2349 ;; no need to go through all the steps.
2350 (let ((old-struct (copy-tree struct
)))
2351 (org-list-set-bullet list-beg struct
(org-list-bullet-string new
))
2352 (org-list-struct-fix-bul struct prevs
)
2353 (org-list-struct-fix-ind struct parents
)
2354 (org-list-struct-apply-struct struct old-struct
)))))
2356 (defun org-toggle-checkbox (&optional toggle-presence
)
2357 "Toggle the checkbox in the current line.
2358 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2359 double prefix, set checkbox to [-].
2361 When there is an active region, toggle status or presence of the
2362 first checkbox there, and make every item inside have the same
2363 status or presence, respectively.
2365 If the cursor is in a headline, apply this to all checkbox items
2366 in the text below the heading, taking as reference the first item
2367 in subtree, ignoring drawers."
2374 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2375 "\\|" org-deadline-string
2376 "\\|" org-closed-string
2377 "\\|" org-clock-string
"\\)"
2378 " *[[<]\\([^]>]+\\)[]>]"))
2379 (orderedp (org-entry-get nil
"ORDERED"))
2381 ;; In a region, start at first item in region.
2383 ((org-region-active-p)
2384 (let ((limit (region-end)))
2385 (goto-char (region-beginning))
2386 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2387 (setq lim-up
(point-at-bol))
2388 (error "No item in region"))
2389 (setq lim-down
(copy-marker limit
))))
2391 ;; On an heading, start at first item after drawers and
2392 ;; time-stamps (scheduled, etc.).
2393 (let ((limit (save-excursion (outline-next-heading) (point))))
2395 (while (or (looking-at org-drawer-regexp
)
2396 (looking-at keyword-re
))
2397 (if (looking-at keyword-re
)
2399 (re-search-forward "^[ \t]*:END:" limit nil
)))
2400 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2401 (setq lim-up
(point-at-bol))
2402 (error "No item in subtree"))
2403 (setq lim-down
(copy-marker limit
))))
2404 ;; Just one item: set SINGLEP flag.
2407 (setq lim-up
(point-at-bol)
2408 lim-down
(copy-marker (point-at-eol))))
2409 (t (error "Not at an item or heading, and no active region"))))
2410 ;; Determine the checkbox going to be applied to all items
2415 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2417 ((equal toggle-presence
'(16)) "[-]")
2418 ((equal toggle-presence
'(4))
2419 (unless cbox
"[ ]"))
2420 ((equal "[X]" cbox
) "[ ]")
2422 ;; When an item is found within bounds, grab the full list at
2423 ;; point structure, then: (1) set check-box of all its items
2424 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2425 ;; whole list, (3) move point after the list.
2427 (while (and (< (point) lim-down
)
2428 (org-list-search-forward (org-item-beginning-re)
2430 (let* ((struct (org-list-struct))
2431 (struct-copy (copy-tree struct
))
2432 (parents (org-list-parents-alist struct
))
2433 (prevs (org-list-prevs-alist struct
))
2434 (bottom (copy-marker (org-list-get-bottom-point struct
)))
2435 (items-to-toggle (cl-remove-if
2436 (lambda (e) (or (< e lim-up
) (> e lim-down
)))
2437 (mapcar #'car struct
))))
2438 (mapc (lambda (e) (org-list-set-checkbox
2440 ;; If there is no box at item, leave as-is
2441 ;; unless function was called with C-u prefix.
2442 (let ((cur-box (org-list-get-checkbox e struct
)))
2443 (if (or cur-box
(equal toggle-presence
'(4)))
2447 (setq block-item
(org-list-struct-fix-box
2448 struct parents prevs orderedp
))
2449 ;; Report some problems due to ORDERED status of subtree.
2450 ;; If only one box was being checked, throw an error, else,
2451 ;; only signal problems.
2453 ((and singlep block-item
(> lim-up block-item
))
2455 "Checkbox blocked because of unchecked box at line %d"
2456 (org-current-line block-item
)))
2459 "Checkboxes were removed due to unchecked box at line %d"
2460 (org-current-line block-item
))))
2462 (move-marker bottom nil
)
2463 (org-list-struct-apply-struct struct struct-copy
)))
2464 (move-marker lim-down nil
)))
2465 (org-update-checkbox-count-maybe))
2467 (defun org-reset-checkbox-state-subtree ()
2468 "Reset all checkboxes in an entry subtree."
2470 (if (org-before-first-heading-p)
2471 (error "Not inside a tree")
2474 (org-narrow-to-subtree)
2476 (goto-char (point-min))
2477 (let ((end (point-max)))
2478 (while (< (point) end
)
2479 (when (org-at-item-checkbox-p)
2480 (replace-match "[ ]" t t nil
1))
2481 (beginning-of-line 2)))
2482 (org-update-checkbox-count-maybe 'all
)))))
2484 (defun org-update-checkbox-count (&optional all
)
2485 "Update the checkbox statistics in the current section.
2487 This will find all statistic cookies like [57%] and [6/12] and
2488 update them with the current numbers.
2490 With optional prefix argument ALL, do this for the whole buffer."
2492 (org-with-wide-buffer
2493 (let* ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2494 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\
2495 \\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2497 (or (not org-checkbox-hierarchical-statistics
)
2498 (string-match "\\<recursive\\>"
2499 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2500 (within-inlinetask (and (not all
)
2501 (featurep 'org-inlinetask
)
2502 (org-inlinetask-in-task-p)))
2503 (end (cond (all (point-max))
2505 (save-excursion (outline-next-heading) (point)))
2507 (org-with-limited-levels (outline-next-heading))
2510 (lambda (item structs recursivep
)
2511 ;; Return number of checked boxes and boxes of all types
2512 ;; in all structures in STRUCTS. If RECURSIVEP is
2513 ;; non-nil, also count boxes in sub-lists. If ITEM is
2514 ;; nil, count across the whole structure, else count only
2515 ;; across subtree whose ancestor is ITEM.
2516 (let ((c-on 0) (c-all 0))
2517 (dolist (s structs
(list c-on c-all
))
2518 (let* ((pre (org-list-prevs-alist s
))
2519 (par (org-list-parents-alist s
))
2522 ((and recursivep item
) (org-list-get-subtree item s
))
2523 (recursivep (mapcar #'car s
))
2524 (item (org-list-get-children item s par
))
2525 (t (org-list-get-all-items
2526 (org-list-get-top-point s
) s pre
))))
2527 (cookies (delq nil
(mapcar
2529 (org-list-get-checkbox e s
))
2531 (cl-incf c-all
(length cookies
))
2532 (cl-incf c-on
(cl-count "[X]" cookies
:test
#'equal
)))))))
2535 (cond (all (goto-char (point-min)))
2536 (within-inlinetask (org-back-to-heading t
))
2537 (t (org-with-limited-levels (outline-previous-heading))))
2538 ;; Build an alist for each cookie found. The key is the position
2539 ;; at beginning of cookie and values ending position, format of
2540 ;; cookie, number of checked boxes to report and total number of
2542 (while (re-search-forward cookie-re end t
)
2543 (let ((context (save-excursion (backward-char)
2544 (save-match-data (org-element-context)))))
2545 (when (eq (org-element-type context
) 'statistics-cookie
)
2548 (list (match-beginning 1) (match-end 1) (match-end 2))
2550 (org-element-lineage
2552 '(drawer center-block dynamic-block inlinetask item
2553 quote-block special-block verse-block
)))
2555 (org-element-property :contents-begin container
)
2557 (org-with-limited-levels
2558 (outline-previous-heading))
2560 (or (cdr (assq beg cache
))
2565 (org-element-property :contents-end container
)
2567 (org-with-limited-levels (outline-next-heading))
2570 (while (re-search-forward box-re end t
)
2571 (let ((element (org-element-at-point)))
2572 (when (eq (org-element-type element
) 'item
)
2573 (push (org-element-property :structure element
)
2575 ;; Skip whole list since we have its
2576 ;; structure anyway.
2577 (while (setq element
(org-element-lineage
2578 element
'(plain-list)))
2580 (min (org-element-property :end element
)
2582 ;; Cache count for cookies applying to the same
2583 ;; area. Then return it.
2585 (funcall count-boxes
2586 (and (eq (org-element-type container
)
2588 (org-element-property
2592 (push (cons beg count
) cache
)
2595 ;; Apply alist to buffer.
2596 (dolist (cookie cookies-list
)
2597 (let* ((beg (car cookie
))
2598 (end (nth 1 cookie
))
2599 (percent (nth 2 cookie
))
2600 (checked (nth 3 cookie
))
2601 (total (nth 4 cookie
)))
2604 (if percent
(format "[%d%%]" (floor (* 100.0 checked
)
2606 (format "[%d/%d]" checked total
)))
2607 (delete-region (point) (+ (point) (- end beg
)))
2608 (when org-auto-align-tags
(org-fix-tags-on-the-fly)))))))
2610 (defun org-get-checkbox-statistics-face ()
2611 "Select the face for checkbox statistics.
2612 The face will be `org-done' when all relevant boxes are checked.
2613 Otherwise it will be `org-todo'."
2615 (if (equal (match-string 1) "100%")
2616 'org-checkbox-statistics-done
2617 'org-checkbox-statistics-todo
)
2618 (if (and (> (match-end 2) (match-beginning 2))
2619 (equal (match-string 2) (match-string 3)))
2620 'org-checkbox-statistics-done
2621 'org-checkbox-statistics-todo
)))
2623 (defun org-update-checkbox-count-maybe (&optional all
)
2624 "Update checkbox statistics unless turned off by user.
2625 With an optional argument ALL, update them in the whole buffer."
2626 (when (cdr (assq 'checkbox org-list-automatic-rules
))
2627 (org-update-checkbox-count all
))
2628 (run-hooks 'org-checkbox-statistics-hook
))
2630 (defvar org-last-indent-begin-marker
(make-marker))
2631 (defvar org-last-indent-end-marker
(make-marker))
2632 (defun org-list-indent-item-generic (arg no-subtree struct
)
2633 "Indent a local list item including its children.
2634 When number ARG is a negative, item will be outdented, otherwise
2635 it will be indented.
2637 If a region is active, all items inside will be moved.
2639 If NO-SUBTREE is non-nil, only indent the item itself, not its
2642 STRUCT is the list structure.
2644 Return t if successful."
2646 (let* ((regionp (org-region-active-p))
2647 (rbeg (and regionp
(region-beginning)))
2648 (rend (and regionp
(region-end)))
2649 (top (org-list-get-top-point struct
))
2650 (parents (org-list-parents-alist struct
))
2651 (prevs (org-list-prevs-alist struct
))
2652 ;; Are we going to move the whole list?
2655 (= top
(point-at-bol))
2656 (cdr (assq 'indent org-list-automatic-rules
))
2659 "First item of list cannot move without its subtree")
2661 ;; Determine begin and end points of zone to indent. If moving
2662 ;; more than one item, save them for subsequent moves.
2663 (unless (and (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
2664 (memq this-command
'(org-shiftmetaright org-shiftmetaleft
)))
2667 (set-marker org-last-indent-begin-marker rbeg
)
2668 (set-marker org-last-indent-end-marker rend
))
2669 (set-marker org-last-indent-begin-marker
(point-at-bol))
2670 (set-marker org-last-indent-end-marker
2672 (specialp (org-list-get-bottom-point struct
))
2673 (no-subtree (1+ (point-at-bol)))
2674 (t (org-list-get-item-end (point-at-bol) struct
))))))
2675 (let* ((beg (marker-position org-last-indent-begin-marker
))
2676 (end (marker-position org-last-indent-end-marker
)))
2678 ;; Special case: moving top-item with indent rule.
2680 (let* ((level-skip (org-level-increment))
2681 (offset (if (< arg
0) (- level-skip
) level-skip
))
2682 (top-ind (org-list-get-ind beg struct
))
2683 (old-struct (copy-tree struct
)))
2684 (if (< (+ top-ind offset
) 0)
2685 (error "Cannot outdent beyond margin")
2686 ;; Change bullet if necessary.
2687 (when (and (= (+ top-ind offset
) 0)
2689 (org-list-get-bullet beg struct
)))
2690 (org-list-set-bullet beg struct
2691 (org-list-bullet-string "-")))
2692 ;; Shift every item by OFFSET and fix bullets. Then
2693 ;; apply changes to buffer.
2695 (let ((ind (org-list-get-ind (car e
) struct
)))
2696 (org-list-set-ind (car e
) struct
(+ ind offset
))))
2698 (org-list-struct-fix-bul struct prevs
)
2699 (org-list-struct-apply-struct struct old-struct
))))
2702 ;; If only one item is moved, it mustn't have a child.
2705 (org-list-has-child-p beg struct
))
2706 ;; If a subtree or region is moved, the last item
2707 ;; of the subtree mustn't have a child.
2708 (let ((last-item (caar
2711 (lambda (e) (>= (car e
) end
))
2713 (org-list-has-child-p last-item struct
))))
2714 (error "Cannot outdent an item without its children"))
2719 (org-list-struct-outdent beg end struct parents
)
2720 (org-list-struct-indent beg end struct parents prevs
))))
2721 (org-list-write-struct struct new-parents
))
2722 (org-update-checkbox-count-maybe))))))
2725 (defun org-outdent-item ()
2726 "Outdent a local list item, but not its children.
2727 If a region is active, all items inside will be moved."
2729 (let ((regionp (org-region-active-p)))
2731 ((or (org-at-item-p)
2733 (save-excursion (goto-char (region-beginning))
2735 (let ((struct (if (not regionp
) (org-list-struct)
2736 (save-excursion (goto-char (region-beginning))
2737 (org-list-struct)))))
2738 (org-list-indent-item-generic -
1 t struct
)))
2739 (regionp (error "Region not starting at an item"))
2740 (t (error "Not at an item")))))
2742 (defun org-indent-item ()
2743 "Indent a local list item, but not its children.
2744 If a region is active, all items inside will be moved."
2746 (let ((regionp (org-region-active-p)))
2748 ((or (org-at-item-p)
2750 (save-excursion (goto-char (region-beginning))
2752 (let ((struct (if (not regionp
) (org-list-struct)
2753 (save-excursion (goto-char (region-beginning))
2754 (org-list-struct)))))
2755 (org-list-indent-item-generic 1 t struct
)))
2756 (regionp (error "Region not starting at an item"))
2757 (t (error "Not at an item")))))
2759 (defun org-outdent-item-tree ()
2760 "Outdent a local list item including its children.
2761 If a region is active, all items inside will be moved."
2763 (let ((regionp (org-region-active-p)))
2765 ((or (org-at-item-p)
2767 (save-excursion (goto-char (region-beginning))
2769 (let ((struct (if (not regionp
) (org-list-struct)
2770 (save-excursion (goto-char (region-beginning))
2771 (org-list-struct)))))
2772 (org-list-indent-item-generic -
1 nil struct
)))
2773 (regionp (error "Region not starting at an item"))
2774 (t (error "Not at an item")))))
2776 (defun org-indent-item-tree ()
2777 "Indent a local list item including its children.
2778 If a region is active, all items inside will be moved."
2780 (let ((regionp (org-region-active-p)))
2782 ((or (org-at-item-p)
2784 (save-excursion (goto-char (region-beginning))
2786 (let ((struct (if (not regionp
) (org-list-struct)
2787 (save-excursion (goto-char (region-beginning))
2788 (org-list-struct)))))
2789 (org-list-indent-item-generic 1 nil struct
)))
2790 (regionp (error "Region not starting at an item"))
2791 (t (error "Not at an item")))))
2793 (defvar org-tab-ind-state
)
2794 (defvar org-adapt-indentation
)
2795 (defun org-cycle-item-indentation ()
2796 "Cycle levels of indentation of an empty item.
2797 The first run indents the item, if applicable. Subsequent runs
2798 outdent it at meaningful levels in the list. When done, item is
2799 put back at its original position with its original bullet.
2801 Return t at each successful move."
2802 (when (org-at-item-p)
2803 (let* ((org-adapt-indentation nil
)
2804 (struct (org-list-struct))
2805 (ind (org-list-get-ind (point-at-bol) struct
))
2806 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2807 ;; Accept empty items or if cycle has already started.
2808 (when (or (eq last-command
'org-cycle-item-indentation
)
2809 (and (save-excursion
2811 (looking-at org-list-full-item-re
))
2812 (>= (match-end 0) (save-excursion
2813 (goto-char (org-list-get-item-end
2814 (point-at-bol) struct
))
2815 (skip-chars-backward " \r\t\n")
2817 (setq this-command
'org-cycle-item-indentation
)
2818 ;; When in the middle of the cycle, try to outdent first. If
2819 ;; it fails, and point is still at initial position, indent.
2820 ;; Else, re-create it at its original position.
2821 (if (eq last-command
'org-cycle-item-indentation
)
2823 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2824 ((and (= ind
(car org-tab-ind-state
))
2825 (ignore-errors (org-list-indent-item-generic 1 t struct
))))
2826 (t (delete-region (point-at-bol) (point-at-eol))
2827 (indent-to-column (car org-tab-ind-state
))
2828 (insert (cdr org-tab-ind-state
) " ")
2830 (setq this-command
'identity
)))
2831 ;; If a cycle is starting, remember indentation and bullet,
2832 ;; then try to indent. If it fails, try to outdent.
2833 (setq org-tab-ind-state
(cons ind bullet
))
2835 ((ignore-errors (org-list-indent-item-generic 1 t struct
)))
2836 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2837 (t (user-error "Cannot move item"))))
2840 (defun org-sort-list
2841 (&optional with-case sorting-type getkey-func compare-func interactive?
)
2843 The cursor may be at any item of the list that should be sorted.
2844 Sublists are not sorted. Checkboxes, if any, are ignored.
2846 Sorting can be alphabetically, numerically, by date/time as given
2847 by a time stamp, by a property or by priority.
2849 Comparing entries ignores case by default. However, with an
2850 optional argument WITH-CASE, the sorting considers case as well.
2852 The command prompts for the sorting type unless it has been given
2853 to the function through the SORTING-TYPE argument, which needs to
2854 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F ?x ?X). Here is the
2855 detailed meaning of each character:
2857 n Numerically, by converting the beginning of the item to a number.
2858 a Alphabetically. Only the first line of item is checked.
2859 t By date/time, either the first active time stamp in the entry, if
2860 any, or by the first inactive one. In a timer list, sort the timers.
2861 x By \"checked\" status of a check list.
2863 Capital letters will reverse the sort order.
2865 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2866 a function to be called with point at the beginning of the
2867 record. It must return a value that is compatible with COMPARE-FUNC,
2868 the function used to compare entries.
2870 Sorting is done against the visible part of the headlines, it
2871 ignores hidden links.
2873 A non-nil value for INTERACTIVE? is used to signal that this
2874 function is being called interactively."
2875 (interactive (list current-prefix-arg nil nil nil t
))
2876 (let* ((case-func (if with-case
'identity
'downcase
))
2877 (struct (org-list-struct))
2878 (prevs (org-list-prevs-alist struct
))
2879 (start (org-list-get-list-begin (point-at-bol) struct prevs
))
2880 (end (org-list-get-list-end (point-at-bol) struct prevs
))
2885 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc [x]checked A/N/T/F/X means reversed:")
2886 (read-char-exclusive))))
2887 (dcst (downcase sorting-type
))
2892 (org-read-function "Function for extracting keys: "))
2893 (error "Missing key extractor"))))
2896 ((= dcst ?a
) #'string
<)
2901 (concat "Function for comparing keys "
2902 "(empty for default `sort-subr' predicate): ")
2905 ((= dcst ?x
) #'string
<))))
2906 (message "Sorting items...")
2908 (narrow-to-region start end
)
2909 (goto-char (point-min))
2910 (let* ((case-fold-search nil
)
2911 (now (current-time))
2912 (next-record (lambda ()
2913 (skip-chars-forward " \r\t\n")
2914 (or (eobp) (beginning-of-line))))
2915 (end-record (lambda ()
2916 (goto-char (org-list-get-item-end-before-blank
2920 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2924 (org-sort-remove-invisible
2925 (buffer-substring (match-end 0) (point-at-eol)))))
2928 (org-sort-remove-invisible
2930 (match-end 0) (point-at-eol)))))
2933 ;; If it is a timer list, convert timer to seconds
2934 ((org-at-item-timer-p)
2935 (org-timer-hms-to-secs (match-string 1)))
2936 ((or (save-excursion
2937 (re-search-forward org-ts-regexp
(point-at-eol) t
))
2938 (save-excursion (re-search-forward org-ts-regexp-both
2940 (org-time-string-to-seconds (match-string 0)))
2941 (t (float-time now
))))
2942 ((= dcst ?x
) (or (and (stringp (match-string 1))
2947 (let ((value (funcall getkey-func
)))
2949 (funcall case-func value
)
2951 (error "Invalid key function `%s'" getkey-func
)))
2952 (t (error "Invalid sorting type `%c'" sorting-type
)))))))
2953 (sort-subr (/= dcst sorting-type
)
2959 ;; Read and fix list again, as `sort-subr' probably destroyed
2962 (run-hooks 'org-after-sorting-entries-or-items-hook
)
2963 (message "Sorting items...done")))))
2965 (defun org-toggle-item (arg)
2966 "Convert headings or normal lines to items, items to normal lines.
2967 If there is no active region, only the current line is considered.
2969 If the first non blank line in the region is a headline, convert
2970 all headlines to items, shifting text accordingly.
2972 If it is an item, convert all items to normal lines.
2974 If it is normal text, change region into a list of items.
2975 With a prefix argument ARG, change the region in a single item."
2979 ;; Shift text in current section to IND, from point to END.
2980 ;; The function leaves point to END line.
2981 (let ((min-i 1000) (end (copy-marker end
)))
2982 ;; First determine the minimum indentation (MIN-I) of
2986 (while (< (point) end
)
2987 (let ((i (org-get-indentation)))
2989 ;; Skip blank lines and inline tasks.
2990 ((looking-at "^[ \t]*$"))
2991 ((looking-at org-outline-regexp-bol
))
2992 ;; We can't find less than 0 indentation.
2993 ((zerop i
) (throw 'exit
(setq min-i
0)))
2994 ((< i min-i
) (setq min-i i
))))
2996 ;; Then indent each line so that a line indented to
2997 ;; MIN-I becomes indented to IND. Ignore blank lines
2998 ;; and inline tasks in the process.
2999 (let ((delta (- ind min-i
)))
3000 (while (< (point) end
)
3001 (unless (or (looking-at "^[ \t]*$")
3002 (looking-at org-outline-regexp-bol
))
3003 (indent-line-to (+ (org-get-indentation) delta
)))
3007 ;; Return beginning of first non-blank line, starting from
3011 (skip-chars-forward " \r\t\n")
3014 ;; Determine boundaries of changes.
3015 (if (org-region-active-p)
3016 (setq beg
(funcall skip-blanks
(region-beginning))
3017 end
(copy-marker (region-end)))
3018 (setq beg
(funcall skip-blanks
(point-at-bol))
3019 end
(copy-marker (point-at-eol))))
3020 ;; Depending on the starting line, choose an action on the text
3021 ;; between BEG and END.
3022 (org-with-limited-levels
3026 ;; Case 1. Start at an item: de-itemize. Note that it only
3027 ;; happens when a region is active: `org-ctrl-c-minus'
3028 ;; would call `org-cycle-list-bullet' otherwise.
3030 (while (< (point) end
)
3031 (when (org-at-item-p)
3032 (skip-chars-forward " \t")
3033 (delete-region (point) (match-end 0)))
3035 ;; Case 2. Start at an heading: convert to items.
3037 (let* ((bul (org-list-bullet-string "-"))
3038 (bul-len (length bul
))
3039 ;; Indentation of the first heading. It should be
3040 ;; relative to the indentation of its parent, if any.
3041 (start-ind (save-excursion
3043 ((not org-adapt-indentation
) 0)
3044 ((not (outline-previous-heading)) 0)
3045 (t (length (match-string 0))))))
3046 ;; Level of first heading. Further headings will be
3047 ;; compared to it to determine hierarchy in the list.
3048 (ref-level (org-reduced-level (org-outline-level))))
3049 (while (< (point) end
)
3050 (let* ((level (org-reduced-level (org-outline-level)))
3051 (delta (max 0 (- level ref-level
)))
3052 (todo-state (org-get-todo-state)))
3053 ;; If current headline is less indented than the first
3054 ;; one, set it as reference, in order to preserve
3056 (when (< level ref-level
) (setq ref-level level
))
3057 ;; Remove stars and TODO keyword.
3058 (let ((case-fold-search nil
)) (looking-at org-todo-line-regexp
))
3059 (delete-region (point) (or (match-beginning 3)
3060 (line-end-position)))
3062 (indent-line-to (+ start-ind
(* delta bul-len
)))
3063 ;; Turn TODO keyword into a check box.
3065 (let* ((struct (org-list-struct))
3066 (old (copy-tree struct
)))
3067 (org-list-set-checkbox
3068 (line-beginning-position)
3070 (if (member todo-state org-done-keywords
)
3073 (org-list-write-struct struct
3074 (org-list-parents-alist struct
)
3076 ;; Ensure all text down to END (or SECTION-END) belongs
3077 ;; to the newly created item.
3078 (let ((section-end (save-excursion
3079 (or (outline-next-heading) (point)))))
3082 (+ start-ind
(* (1+ delta
) bul-len
))
3083 (min end section-end
)))))))
3084 ;; Case 3. Normal line with ARG: make the first line of region
3085 ;; an item, and shift indentation of others lines to
3086 ;; set them as item's body.
3087 (arg (let* ((bul (org-list-bullet-string "-"))
3088 (bul-len (length bul
))
3089 (ref-ind (org-get-indentation)))
3090 (skip-chars-forward " \t")
3093 (while (< (point) end
)
3094 ;; Ensure that lines less indented than first one
3095 ;; still get included in item body.
3098 (min end
(save-excursion (or (outline-next-heading)
3101 ;; Case 4. Normal line without ARG: turn each non-item line
3104 (while (< (point) end
)
3105 (unless (or (org-at-heading-p) (org-at-item-p))
3106 (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
3108 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
3109 (forward-line))))))))
3112 ;;; Send and receive lists
3114 (defun org-list-to-lisp (&optional delete
)
3115 "Parse the list at point and maybe DELETE it.
3117 Return a list whose car is a symbol of list type, among
3118 `ordered', `unordered' and `descriptive'. Then, each item is
3119 a list of strings and other sub-lists.
3121 For example, the following list:
3126 more text in first item
3135 (\"[X] sub-item two\"))
3136 \"more text in first item\")
3137 (\"[@3] last item\"))
3139 Point is left at list's end."
3140 (letrec ((struct (org-list-struct))
3141 (prevs (org-list-prevs-alist struct
))
3142 (parents (org-list-parents-alist struct
))
3143 (top (org-list-get-top-point struct
))
3144 (bottom (org-list-get-bottom-point struct
))
3147 ;; Remove indentation and final newline from TEXT.
3148 (org-remove-indentation
3149 (if (string-match-p "\n\\'" text
)
3150 (substring text
0 -
1)
3154 ;; Return a list whose car is list type and cdr a list
3156 (cons (org-list-get-list-type (car e
) struct prevs
)
3157 (mapcar parse-item e
))))
3160 ;; Return a list containing counter of item, if any,
3161 ;; text and any sublist inside it.
3162 (let* ((end (org-list-get-item-end e struct
))
3163 (children (org-list-get-children e struct parents
))
3167 (looking-at "[ \t]*\\S-+[ \t]*")
3172 (make-string (string-width (match-string 0)) ?\s
)
3173 (buffer-substring-no-properties
3174 (match-end 0) (or (car children
) end
))))))))
3176 (let* ((child (car children
))
3177 (sub (org-list-get-all-items child struct prevs
))
3178 (last-in-sub (car (last sub
))))
3179 (push (funcall parse-sublist sub
) body
)
3180 ;; Remove whole sub-list from children.
3181 (setq children
(cdr (memq last-in-sub children
)))
3182 ;; There is a chunk of text belonging to the item
3183 ;; if last child doesn't end where next child
3184 ;; starts or where item ends.
3185 (let ((sub-end (org-list-get-item-end last-in-sub struct
))
3186 (next (or (car children
) end
)))
3187 (when (/= sub-end next
)
3190 (buffer-substring-no-properties sub-end next
))
3193 ;; Store output, take care of cursor position and deletion of
3194 ;; list, then return output.
3195 (prog1 (funcall parse-sublist
(org-list-get-all-items top struct prevs
))
3198 (delete-region top bottom
)
3199 (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re
))
3200 (replace-match ""))))))
3202 (defun org-list-make-subtree ()
3203 "Convert the plain list at point into a subtree."
3205 (if (not (ignore-errors (goto-char (org-in-item-p))))
3206 (error "Not in a list")
3207 (let ((list (save-excursion (org-list-to-lisp t
))))
3208 (insert (org-list-to-subtree list
)))))
3210 (defun org-list-insert-radio-list ()
3211 "Insert a radio list template appropriate for this major mode."
3213 (let* ((e (cl-assoc-if #'derived-mode-p org-list-radio-list-templates
))
3216 (unless e
(error "No radio list setup defined for %s" major-mode
))
3217 (setq name
(read-string "List name: "))
3218 (while (string-match "%n" txt
)
3219 (setq txt
(replace-match name t t txt
)))
3220 (or (bolp) (insert "\n"))
3225 (defun org-list-send-list (&optional maybe
)
3226 "Send a transformed version of this list to the receiver position.
3227 With argument MAYBE, fail quietly if no transformation is defined
3231 (unless (org-at-item-p) (error "Not at a list item"))
3233 (let ((case-fold-search t
))
3234 (re-search-backward "^[ \t]*#\\+ORGLST:" nil t
)
3236 "[ \t]*#\\+ORGLST:[ \t]+SEND[ \t]+\\(\\S-+\\)[ \t]+\\([^ \t\n]+\\)")
3237 (if maybe
(throw 'exit nil
)
3238 (error "Don't know how to transform this list")))))
3239 (let* ((name (regexp-quote (match-string 1)))
3240 (transform (intern (match-string 2)))
3244 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t
)
3245 (match-beginning 0)))
3248 (re-search-backward "#\\+ORGLST" nil t
)
3249 (re-search-forward (org-item-beginning-re) bottom-point t
)
3250 (match-beginning 0)))
3251 (plain-list (save-excursion
3252 (goto-char top-point
)
3253 (org-list-to-lisp))))
3254 (unless (fboundp transform
)
3255 (error "No such transformation function %s" transform
))
3256 (let ((txt (funcall transform plain-list
)))
3257 ;; Find the insertion(s) place(s).
3259 (goto-char (point-min))
3260 (let ((receiver-count 0)
3261 (begin-re (format "BEGIN +RECEIVE +ORGLST +%s\\([ \t]\\|$\\)"
3263 (end-re (format "END +RECEIVE +ORGLST +%s\\([ \t]\\|$\\)"
3265 (while (re-search-forward begin-re nil t
)
3266 (cl-incf receiver-count
)
3267 (let ((beg (line-beginning-position 2)))
3268 (unless (re-search-forward end-re nil t
)
3269 (user-error "Cannot find end of receiver location at %d" beg
))
3271 (delete-region beg
(point))
3274 ((> receiver-count
1)
3275 (message "List converted and installed at receiver locations"))
3276 ((= receiver-count
1)
3277 (message "List converted and installed at receiver location"))
3278 (t (user-error "No valid receiver location found")))))))))
3280 (defun org-list-to-generic (list params
)
3281 "Convert a LIST parsed through `org-list-to-lisp' to a custom format.
3283 LIST is a list as returned by `org-list-to-lisp', which see.
3284 PARAMS is a property list of parameters used to tweak the output
3287 Valid parameters are:
3291 Export back-end used as a basis to transcode elements of the
3292 list, when no specific parameter applies to it. It is also
3293 used to translate its contents. You can prevent this by
3294 setting :raw property to a non-nil value.
3298 When non-nil, only export the contents of the top most plain
3299 list, effectively ignoring its opening and closing lines.
3303 Strings to start and end an unordered list. They can also be
3304 set to a function returning a string or nil, which will be
3305 called with the depth of the list, counting from 1.
3309 Strings to start and end an ordered list. They can also be set
3310 to a function returning a string or nil, which will be called
3311 with the depth of the list, counting from 1.
3315 Strings to start and end a descriptive list. They can also be
3316 set to a function returning a string or nil, which will be
3317 called with the depth of the list, counting from 1.
3319 :dtstart, :dtend, :ddstart, :ddend
3321 Strings to start and end a descriptive term.
3325 Strings to start or end a list item, and to start a list item
3326 with a counter. They can also be set to a function returning
3327 a string or nil, which will be called with the depth of the
3328 item, counting from 1.
3332 Strings to start a list item with a counter. It can also be
3333 set to a function returning a string or nil, which will be
3334 called with two arguments: the depth of the item, counting from
3335 1, and the counter. Its value, when non-nil, has precedence
3340 String used to separate items. It can also be set to
3341 a function returning a string or nil, which will be called with
3342 the depth of the items, counting from 1. It always start on
3345 :cbon, :cboff, :cbtrans
3347 String to insert, respectively, an un-checked check-box,
3348 a checked check-box and a check-box in transitional state."
3350 (let* ((backend (plist-get params
:backend
))
3352 (org-export-create-backend
3353 :parent
(or backend
'org
)
3355 `((plain-list .
,(org-list--to-generic-plain-list params
))
3356 (item .
,(org-list--to-generic-item params
))
3357 (macro .
(lambda (m c i
) (org-element-macro-interpreter m nil
))))))
3359 ;; Write LIST back into Org syntax and parse it.
3361 (let ((org-inhibit-startup t
)) (org-mode))
3362 (letrec ((insert-list
3365 (funcall insert-item i
(car l
)))))
3368 (let ((start (point)))
3369 (insert (if (eq type
'ordered
) "1. " "- "))
3371 (if (consp e
) (funcall insert-list e
)
3376 (let ((ind (if (eq type
'ordered
) 3 2)))
3377 (while (> (point) start
)
3378 (unless (looking-at-p "[ \t]*$")
3380 (forward-line -
1))))))))
3381 (funcall insert-list list
))
3383 (org-element-map (org-element-parse-buffer) 'plain-list
3385 (setf info
(org-export-get-environment backend nil params
)))
3386 (when (and backend
(symbolp backend
) (not (org-export-get-backend backend
)))
3387 (user-error "Unknown :backend value"))
3388 (unless backend
(require 'ox-org
))
3389 ;; When`:raw' property has a non-nil value, turn all objects back
3391 (when (and backend
(plist-get params
:raw
))
3392 (org-element-map data org-element-all-objects
3394 (org-element-set-element
3395 object
(org-element-interpret-data object
)))))
3396 ;; We use a low-level mechanism to export DATA so as to skip all
3397 ;; usual pre-processing and post-processing, i.e., hooks, filters,
3398 ;; Babel code evaluation, include keywords and macro expansion,
3400 (let ((output (org-export-data-with-backend data custom-backend info
)))
3401 ;; Remove final newline.
3402 (if (org-string-nw-p output
) (substring-no-properties output
0 -
1) ""))))
3404 (defun org-list--depth (element)
3405 "Return the level of ELEMENT within current plain list.
3406 ELEMENT is either an item or a plain list."
3407 (cl-count-if (lambda (ancestor) (eq (org-element-type ancestor
) 'plain-list
))
3408 (org-element-lineage element nil t
)))
3410 (defun org-list--trailing-newlines (string)
3411 "Return the number of trailing newlines in STRING."
3414 (skip-chars-backward " \t\n")
3415 (count-lines (line-beginning-position 2) (point-max))))
3417 (defun org-list--generic-eval (value &rest args
)
3418 "Evaluate VALUE according to its type.
3419 VALUE is either nil, a string or a function. In the latter case,
3420 it is called with arguments ARGS."
3421 (cond ((null value
) nil
)
3422 ((stringp value
) value
)
3423 ((functionp value
) (apply value args
))
3424 (t (error "Wrong value: %s" value
))))
3426 (defun org-list--to-generic-plain-list (params)
3427 "Return a transcoder for `plain-list' elements.
3428 PARAMS is a plist used to tweak the behavior of the transcoder."
3429 (let ((ustart (plist-get params
:ustart
))
3430 (uend (plist-get params
:uend
))
3431 (ostart (plist-get params
:ostart
))
3432 (oend (plist-get params
:oend
))
3433 (dstart (plist-get params
:dstart
))
3434 (dend (plist-get params
:dend
))
3435 (splice (plist-get params
:splice
))
3436 (backend (plist-get params
:backend
)))
3437 (lambda (plain-list contents info
)
3438 (let* ((type (org-element-property :type plain-list
))
3439 (depth (org-list--depth plain-list
))
3440 (start (and (not splice
)
3441 (org-list--generic-eval
3447 (end (and (not splice
)
3448 (org-list--generic-eval
3454 ;; Make sure trailing newlines in END appear in the output by
3455 ;; setting `:post-blank' property to their number.
3457 (org-element-put-property
3458 plain-list
:post-blank
(org-list--trailing-newlines end
)))
3460 (concat (and start
(concat start
"\n"))
3461 (if (or start end splice
(not backend
))
3463 (org-export-with-backend backend plain-list contents info
))
3466 (defun org-list--to-generic-item (params)
3467 "Return a transcoder for `item' elements.
3468 PARAMS is a plist used to tweak the behavior of the transcoder."
3469 (let ((backend (plist-get params
:backend
))
3470 (istart (plist-get params
:istart
))
3471 (iend (plist-get params
:iend
))
3472 (isep (plist-get params
:isep
))
3473 (icount (plist-get params
:icount
))
3474 (cboff (plist-get params
:cboff
))
3475 (cbon (plist-get params
:cbon
))
3476 (cbtrans (plist-get params
:cbtrans
))
3477 (dtstart (plist-get params
:dtstart
))
3478 (dtend (plist-get params
:dtend
))
3479 (ddstart (plist-get params
:ddstart
))
3480 (ddend (plist-get params
:ddend
)))
3481 (lambda (item contents info
)
3483 (org-element-property :type
(org-element-property :parent item
)))
3484 (tag (org-element-property :tag item
))
3485 (depth (org-list--depth item
))
3486 (separator (and (org-export-get-next-element item info
)
3487 (org-list--generic-eval isep depth
)))
3488 (closing (pcase (org-list--generic-eval iend depth
)
3489 ((or `nil
`"") "\n")
3490 ((and (guard separator
) s
)
3491 (if (equal (substring s -
1) "\n") s
(concat s
"\n")))
3493 ;; When a closing line or a separator is provided, make sure
3494 ;; its trailing newlines are taken into account when building
3495 ;; output. This is done by setting `:post-blank' property to
3496 ;; the number of such lines in the last line to be added.
3497 (let ((last-string (or separator closing
)))
3499 (org-element-put-property
3502 (max (1- (org-list--trailing-newlines last-string
)) 0))))
3505 (let ((c (org-element-property :counter item
)))
3506 (if c
(org-list--generic-eval icount depth c
)
3507 (org-list--generic-eval istart depth
)))
3509 (if (or istart iend icount cbon cboff cbtrans
(not backend
)
3510 (and (eq type
'descriptive
)
3511 (or dtstart dtend ddstart ddend
)))
3513 (pcase (org-element-property :checkbox item
)
3520 (org-export-data-with-backend
3522 (org-element-interpret-data tag
))
3525 (if (= (length contents
) 0) "" (substring contents
0 -
1))
3527 (org-export-with-backend backend item contents info
))))
3528 ;; Remove final newline.
3529 (if (equal body
"") ""
3530 (substring (org-element-normalize-string body
) 0 -
1)))
3534 (defun org-list-to-latex (list &optional params
)
3535 "Convert LIST into a LaTeX list.
3536 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3537 PARAMS is a property list with overruling parameters for
3538 `org-list-to-generic'. Return converted list as a string."
3540 (org-list-to-generic list
(org-combine-plists '(:backend latex
) params
)))
3542 (defun org-list-to-html (list &optional params
)
3543 "Convert LIST into a HTML list.
3544 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3545 PARAMS is a property list with overruling parameters for
3546 `org-list-to-generic'. Return converted list as a string."
3548 (org-list-to-generic list
(org-combine-plists '(:backend html
) params
)))
3550 (defun org-list-to-texinfo (list &optional params
)
3551 "Convert LIST into a Texinfo list.
3552 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3553 PARAMS is a property list with overruling parameters for
3554 `org-list-to-generic'. Return converted list as a string."
3555 (require 'ox-texinfo
)
3556 (org-list-to-generic list
(org-combine-plists '(:backend texinfo
) params
)))
3558 (defun org-list-to-subtree (list &optional params
)
3559 "Convert LIST into an Org subtree.
3560 LIST is as returned by `org-list-to-lisp'. PARAMS is a property
3561 list with overruling parameters for `org-list-to-generic'."
3562 (let* ((blank (pcase (cdr (assq 'heading org-blank-before-new-entry
))
3564 (`auto
(save-excursion
3565 (org-with-limited-levels (outline-previous-heading))
3566 (org-previous-line-empty-p)))))
3567 (level (org-reduced-level (or (org-current-level) 0)))
3570 ;; Return the string for the heading, depending on DEPTH
3571 ;; of current sub-list.
3572 (let ((oddeven-level (+ level depth
)))
3573 (concat (make-string (if org-odd-levels-only
3574 (1- (* 2 oddeven-level
))
3578 (org-list-to-generic
3584 :dtstart
" " :dtend
" "
3585 :isep
(if blank
"\n\n" "\n")
3586 :cbon
"DONE " :cboff
"TODO " :cbtrans
"TODO ")
3591 ;;; org-list.el ends here