1 ;;; org-list.el --- Plain lists for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2016 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 outline-flag-region
"outline" (from to flag
))
158 (declare-function outline-invisible-p
"outline" (&optional pos
))
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.
285 By default, automatic actions are taken when using
286 \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
287 \\[org-shiftmetaright], \\[org-shiftmetaleft],
288 \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
289 \\[org-insert-todo-heading]. You can disable individually these
290 rules by setting them to nil. Valid rules are:
292 checkbox when non-nil, checkbox statistics is updated each time
293 you either insert a new checkbox or toggle a checkbox.
294 indent when non-nil, indenting or outdenting list top-item
295 with its subtree will move the whole list and
296 outdenting a list whose bullet is * to column 0 will
297 change that bullet to \"-\"."
298 :group
'org-plain-lists
300 :type
'(alist :tag
"Sets of rules"
303 (const :tag
"Checkbox" checkbox
)
304 (const :tag
"Indent" indent
))
306 (boolean :tag
"Activate" :value t
)))
308 (defcustom org-list-use-circular-motion nil
309 "Non-nil means commands implying motion in lists should be cyclic.
311 In that case, the item following the last item is the first one,
312 and the item preceding the first item is the last one.
314 This affects the behavior of \\[org-move-item-up],
315 \\[org-move-item-down], \\[org-next-item] and
316 \\[org-previous-item]."
317 :group
'org-plain-lists
321 (defvar org-checkbox-statistics-hook nil
322 "Hook that is run whenever Org thinks checkbox statistics should be updated.
323 This hook runs even if checkbox rule in
324 `org-list-automatic-rules' does not apply, so it can be used to
325 implement alternative ways of collecting statistics
328 (defcustom org-checkbox-hierarchical-statistics t
329 "Non-nil means checkbox statistics counts only the state of direct children.
330 When nil, all boxes below the cookie are counted.
331 This can be set to nil on a per-node basis using a COOKIE_DATA property
332 with the word \"recursive\" in the value."
333 :group
'org-plain-lists
336 (defcustom org-list-description-max-indent
20
337 "Maximum indentation for the second line of a description list.
338 When the indentation would be larger than this, it will become
339 5 characters instead."
340 :group
'org-plain-lists
343 (defcustom org-list-indent-offset
0
344 "Additional indentation for sub-items in a list.
345 By setting this to a small number, usually 1 or 2, one can more
346 clearly distinguish sub-items in a list."
347 :group
'org-plain-lists
351 (defcustom org-list-radio-list-templates
352 '((latex-mode "% BEGIN RECEIVE ORGLST %n
353 % END RECEIVE ORGLST %n
355 #+ORGLST: SEND %n org-list-to-latex
358 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
359 @c END RECEIVE ORGLST %n
361 #+ORGLST: SEND %n org-list-to-texinfo
364 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
365 <!-- END RECEIVE ORGLST %n -->
367 #+ORGLST: SEND %n org-list-to-html
370 "Templates for radio lists in different major modes.
371 All occurrences of %n in a template will be replaced with the name of the
372 list, obtained by prompting the user."
373 :group
'org-plain-lists
375 (list (symbol :tag
"Major mode")
376 (string :tag
"Format"))))
378 (defvar org-list-forbidden-blocks
'("example" "verse" "src" "export")
379 "Names of blocks where lists are not allowed.
380 Names must be in lower case.")
382 (defvar org-list-export-context
'(block inlinetask
)
383 "Context types where lists will be interpreted during export.
385 Valid types are `drawer', `inlinetask' and `block'. More
386 specifically, type `block' is determined by the variable
387 `org-list-forbidden-blocks'.")
391 ;;; Predicates and regexps
393 (defconst org-list-end-re
"^[ \t]*\n[ \t]*\n"
394 "Regex matching the end of a plain list.")
396 (defconst org-list-full-item-re
397 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
398 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
399 "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
400 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
401 "Matches a list item and puts everything into groups:
405 group 4: description tag")
407 (defun org-item-re ()
408 "Return the correct regular expression for plain lists."
410 ((eq org-plain-list-ordered-item-terminator t
) "[.)]")
411 ((= org-plain-list-ordered-item-terminator ?\
)) ")")
412 ((= org-plain-list-ordered-item-terminator ?.
) "\\.")
414 (alpha (if org-list-allow-alphabetical
"\\|[A-Za-z]" "")))
415 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha
"\\)" term
416 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
418 (defsubst org-item-beginning-re
()
419 "Regexp matching the beginning of a plain list item."
420 (concat "^" (org-item-re)))
422 (defun org-list-at-regexp-after-bullet-p (regexp)
423 "Is point at a list item with REGEXP after bullet?"
426 (goto-char (match-end 0))
427 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
428 (if org-list-allow-alphabetical
429 "\\([0-9]+\\|[A-Za-z]\\)"
432 ;; Ignore counter if any
433 (when (looking-at counter-re
) (goto-char (match-end 0))))
434 (looking-at regexp
))))
436 (defun org-list-in-valid-context-p ()
437 "Is point in a context where lists are allowed?"
438 (not (org-in-block-p org-list-forbidden-blocks
)))
440 (defun org-in-item-p ()
441 "Return item beginning position when in a plain list, nil otherwise."
444 (let* ((case-fold-search t
)
445 (context (org-list-context))
446 (lim-up (car context
))
447 (inlinetask-re (and (featurep 'org-inlinetask
)
448 (org-inlinetask-outline-regexp)))
449 (item-re (org-item-re))
450 ;; Indentation isn't meaningful when point starts at an empty
451 ;; line or an inline task.
452 (ind-ref (if (or (looking-at "^[ \t]*$")
453 (and inlinetask-re
(looking-at inlinetask-re
)))
455 (org-get-indentation))))
457 ((eq (nth 2 context
) 'invalid
) nil
)
458 ((looking-at item-re
) (point))
460 ;; Detect if cursor in amidst `org-list-end-re'. First, count
461 ;; number HL of hard lines it takes, then call `org-in-regexp'
462 ;; to compute its boundaries END-BOUNDS. When point is
463 ;; in-between, move cursor before regexp beginning.
464 (let ((hl 0) (i -
1) end-bounds
)
466 (while (setq i
(string-match
467 "[\r\n]" org-list-end-re
(1+ i
)))
469 (setq end-bounds
(org-in-regexp org-list-end-re hl
)))
470 (>= (point) (car end-bounds
))
471 (< (point) (cdr end-bounds
)))
472 (goto-char (car end-bounds
))
474 ;; Look for an item, less indented that reference line.
477 (let ((ind (org-get-indentation)))
479 ;; This is exactly what we want.
480 ((and (looking-at item-re
) (< ind ind-ref
))
481 (throw 'exit
(point)))
482 ;; At upper bound of search or looking at the end of a
483 ;; previous list: search is over.
484 ((<= (point) lim-up
) (throw 'exit nil
))
485 ((looking-at org-list-end-re
) (throw 'exit nil
))
486 ;; Skip blocks, drawers, inline-tasks, blank lines
487 ((and (looking-at "^[ \t]*#\\+end_")
488 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
489 ((and (looking-at "^[ \t]*:END:")
490 (re-search-backward org-drawer-regexp lim-up t
))
492 ((and inlinetask-re
(looking-at inlinetask-re
))
493 (org-inlinetask-goto-beginning)
495 ((looking-at "^[ \t]*$") (forward-line -
1))
496 ;; Text at column 0 cannot belong to a list: stop.
497 ((zerop ind
) (throw 'exit nil
))
498 ;; Normal text less indented than reference line, take
499 ;; it as new reference.
503 (t (forward-line -
1)))))))))))
505 (defun org-at-item-p ()
506 "Is point in a line starting a hand-formatted item?"
509 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
511 (defun org-at-item-bullet-p ()
512 "Is point at the bullet of a plain list item?"
514 (not (member (char-after) '(?\ ?
\t)))
515 (< (point) (match-end 0))))
517 (defun org-at-item-timer-p ()
518 "Is point at a line starting a plain list item with a timer?"
519 (org-list-at-regexp-after-bullet-p
520 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
522 (defun org-at-item-description-p ()
523 "Is point at a description list item?"
524 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::\\([ \t]+\\|$\\)"))
526 (defun org-at-item-checkbox-p ()
527 "Is point at a line starting a plain-list item with a checklet?"
528 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
530 (defun org-at-item-counter-p ()
531 "Is point at a line starting a plain-list item with a counter?"
533 (looking-at org-list-full-item-re
)
538 ;;; Structures and helper functions
540 (defun org-list-context ()
541 "Determine context, and its boundaries, around point.
543 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
544 are boundaries and CONTEXT is a symbol among `drawer', `block',
545 `invalid', `inlinetask' and nil.
547 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
550 (org-with-limited-levels
552 (let ((case-fold-search t
) (pos (point)) beg end context-type
553 ;; Get positions of surrounding headings. This is the
555 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t
))
558 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
559 ;; Is point inside a drawer?
560 (let ((end-re "^[ \t]*:END:")
561 (beg-re org-drawer-regexp
))
562 (when (save-excursion
563 (and (not (looking-at beg-re
))
564 (not (looking-at end-re
))
565 (setq beg
(and (re-search-backward beg-re lim-up t
)
566 (1+ (point-at-eol))))
567 (setq end
(or (and (re-search-forward end-re lim-down t
)
568 (1- (match-beginning 0)))
571 (setq lim-up beg lim-down end context-type
'drawer
)))
572 ;; Is point strictly in a block, and of which type?
573 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type
)
574 (when (save-excursion
575 (and (not (looking-at block-re
))
576 (setq beg
(and (re-search-backward block-re lim-up t
)
577 (1+ (point-at-eol))))
578 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
579 (setq type
(downcase (match-string 1)))
581 (setq end
(or (and (re-search-forward block-re lim-down t
)
585 (equal (downcase (match-string 1)) "end")))
586 (setq lim-up beg lim-down end
587 context-type
(if (member type org-list-forbidden-blocks
)
589 ;; Is point in an inlinetask?
590 (when (and (featurep 'org-inlinetask
)
592 (let* ((beg-re (org-inlinetask-outline-regexp))
593 (end-re (concat beg-re
"END[ \t]*$")))
594 (and (not (looking-at "^\\*+"))
595 (setq beg
(and (re-search-backward beg-re lim-up t
)
596 (1+ (point-at-eol))))
597 (not (looking-at end-re
))
598 (setq end
(and (re-search-forward end-re lim-down t
)
599 (1- (match-beginning 0))))
601 (setq lim-up beg lim-down end context-type
'inlinetask
))
602 ;; Return context boundaries and type.
603 (list lim-up lim-down context-type
))))))
605 (defun org-list-struct ()
606 "Return structure of list at point.
608 A list structure is an alist where key is point at item, and
611 2. bullet with trailing whitespace,
612 3. bullet counter, if any,
614 5. description tag, if any,
615 6. position at item end.
617 Thus the following list, where numbers in parens are
622 5. [@5] sub-item 2 (34)
623 some other text belonging to first item (55)
625 + tag :: description (109)
628 will get the following structure:
630 ((1 0 \"- \" nil \"[X]\" nil 97)
631 (18 2 \"1. \" nil nil nil 34)
632 (34 2 \"5. \" \"5\" nil nil 55)
633 (97 0 \"- \" nil nil nil 131)
634 (109 2 \"+ \" nil nil \"tag\" 131))
636 Assume point is at an item."
639 (let* ((case-fold-search t
)
640 (context (org-list-context))
641 (lim-up (car context
))
642 (lim-down (nth 1 context
))
644 (item-re (org-item-re))
645 (inlinetask-re (and (featurep 'org-inlinetask
)
646 (org-inlinetask-outline-regexp)))
647 (beg-cell (cons (point) (org-get-indentation)))
648 itm-lst itm-lst-2 end-lst end-lst-2 struct
651 ;; Return association at point.
653 (looking-at org-list-full-item-re
)
654 (let ((bullet (match-string-no-properties 1)))
658 (match-string-no-properties 2) ; counter
659 (match-string-no-properties 3) ; checkbox
661 (and (string-match-p "[-+*]" bullet
)
662 (match-string-no-properties 4)))))))
665 ;; Ensure list ends at the first blank line.
667 (skip-chars-backward " \r\t\n")
668 (min (1+ (point-at-eol)) lim-down
)))))
669 ;; 1. Read list from starting item to its beginning, and save
670 ;; top item position and indentation in BEG-CELL. Also store
671 ;; ending position of items in END-LST.
675 (let ((ind (org-get-indentation)))
678 ;; At upward limit: if we ended at an item, store it,
679 ;; else dismiss useless data recorded above BEG-CELL.
683 (if (not (looking-at item-re
))
684 (memq (assq (car beg-cell
) itm-lst
) itm-lst
)
685 (setq beg-cell
(cons (point) ind
))
686 (cons (funcall assoc-at-point ind
) itm-lst
)))))
687 ;; Looking at a list ending regexp. Dismiss useless
688 ;; data recorded above BEG-CELL. Jump to part 2.
689 ((looking-at org-list-end-re
)
692 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
693 ;; Point is at an item. Add data to ITM-LST. It may
694 ;; also end a previous item: save it in END-LST. If
695 ;; ind is less or equal than BEG-CELL and there is no
696 ;; end at this ind or lesser, this item becomes the new
698 ((looking-at item-re
)
699 (push (funcall assoc-at-point ind
) itm-lst
)
700 (push (cons ind
(point)) end-lst
)
701 (when (< ind text-min-ind
) (setq beg-cell
(cons (point) ind
)))
703 ;; Skip blocks, drawers, inline tasks, blank lines.
704 ((and (looking-at "^[ \t]*#\\+end_")
705 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
706 ((and (looking-at "^[ \t]*:END:")
707 (re-search-backward org-drawer-regexp lim-up t
))
709 ((and inlinetask-re
(looking-at inlinetask-re
))
710 (org-inlinetask-goto-beginning)
712 ((looking-at "^[ \t]*$")
714 ;; From there, point is not at an item. Interpret
715 ;; line's indentation:
716 ;; - text at column 0 is necessarily out of any list.
717 ;; Dismiss data recorded above BEG-CELL. Jump to
719 ;; - any other case may be an ending position for an
720 ;; hypothetical item above. Store it and proceed.
724 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
726 (when (< ind text-min-ind
) (setq text-min-ind ind
))
727 (push (cons ind
(point)) end-lst
)
728 (forward-line -
1)))))))
729 ;; 2. Read list from starting point to its end, that is until we
730 ;; get out of context, or that a non-item line is less or
731 ;; equally indented than BEG-CELL's cdr. Also, store ending
732 ;; position of items in END-LST-2.
735 (let ((ind (org-get-indentation)))
737 ((>= (point) lim-down
)
738 ;; At downward limit: this is de facto the end of the
739 ;; list. Save point as an ending position, and jump to
742 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
743 ;; Looking at a list ending regexp. Save point as an
744 ;; ending position and jump to part 3.
745 ((looking-at org-list-end-re
)
746 (throw 'exit
(push (cons 0 (point)) end-lst-2
)))
747 ((looking-at item-re
)
748 ;; Point is at an item. Add data to ITM-LST-2. It may
749 ;; also end a previous item, so save it in END-LST-2.
750 (push (funcall assoc-at-point ind
) itm-lst-2
)
751 (push (cons ind
(point)) end-lst-2
)
753 ;; Skip inline tasks and blank lines along the way
754 ((and inlinetask-re
(looking-at inlinetask-re
))
755 (org-inlinetask-goto-end))
756 ((looking-at "^[ \t]*$")
758 ;; Ind is lesser or equal than BEG-CELL's. The list is
759 ;; over: store point as an ending position and jump to
761 ((<= ind
(cdr beg-cell
))
763 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
764 ;; Else, if ind is lesser or equal than previous item's,
765 ;; this is an ending position: store it. In any case,
766 ;; skip block or drawer at point, and move to next line.
768 (when (<= ind
(nth 1 (car itm-lst-2
)))
769 (push (cons ind
(point)) end-lst-2
))
771 ((and (looking-at "^[ \t]*#\\+begin_")
772 (re-search-forward "^[ \t]*#\\+end_" lim-down t
)))
773 ((and (looking-at org-drawer-regexp
)
774 (re-search-forward "^[ \t]*:END:" lim-down t
))))
775 (forward-line 1))))))
776 (setq struct
(append itm-lst
(cdr (nreverse itm-lst-2
)))
777 end-lst
(append end-lst
(cdr (nreverse end-lst-2
))))
778 ;; 3. Associate each item to its end position.
779 (org-list-struct-assoc-end struct end-lst
)
783 (defun org-list-struct-assoc-end (struct end-list
)
784 "Associate proper ending point to items in STRUCT.
786 END-LIST is a pseudo-alist where car is indentation and cdr is
789 This function modifies STRUCT."
790 (let ((endings end-list
))
793 (let ((pos (car elt
))
795 ;; Remove end candidates behind current item.
796 (while (or (<= (cdar endings
) pos
))
798 ;; Add end position to item assoc.
799 (let ((old-end (nthcdr 6 elt
))
800 (new-end (assoc-default ind endings
'<=)))
802 (setcar old-end new-end
)
803 (setcdr elt
(append (cdr elt
) (list new-end
)))))))
806 (defun org-list-prevs-alist (struct)
807 "Return alist between item and previous item in STRUCT."
808 (let ((item-end-alist (mapcar (lambda (e) (cons (car e
) (nth 6 e
)))
811 (let ((prev (car (rassq (car e
) item-end-alist
))))
812 (cons (car e
) prev
)))
815 (defun org-list-parents-alist (struct)
816 "Return alist between item and parent in STRUCT."
817 (let* ((ind-to-ori (list (list (nth 1 (car struct
)))))
818 (top-item (org-list-get-top-point struct
))
819 (prev-pos (list top-item
)))
821 (mapcar (lambda (item)
822 (let ((pos (car item
))
824 (prev-ind (caar ind-to-ori
)))
828 ;; A sub-list is over. Find the associated
829 ;; origin in IND-TO-ORI. If it cannot be
830 ;; found (ill-formed list), set its parent as
831 ;; the first item less indented. If there is
832 ;; none, make it a top-level item.
834 (or (member (assq ind ind-to-ori
) ind-to-ori
)
838 (when (< (car e
) ind
)
839 (throw 'exit
(member e ind-to-ori
))))
842 (cons pos
(cdar ind-to-ori
)))
843 ;; A sub-list starts. Every item at IND will
844 ;; have previous item as its parent.
846 (let ((origin (nth 1 prev-pos
)))
847 (push (cons ind origin
) ind-to-ori
)
849 ;; Another item in the same sub-list: it shares
850 ;; the same parent as the previous item.
851 (t (cons pos
(cdar ind-to-ori
))))))
858 (defsubst org-list-get-nth
(n key struct
)
859 "Return the Nth value of KEY in STRUCT."
860 (nth n
(assq key struct
)))
862 (defun org-list-set-nth (n key struct new
)
863 "Set the Nth value of KEY in STRUCT to NEW.
864 \nThis function modifies STRUCT."
865 (setcar (nthcdr n
(assq key struct
)) new
))
867 (defsubst org-list-get-ind
(item struct
)
868 "Return indentation of ITEM in STRUCT."
869 (org-list-get-nth 1 item struct
))
871 (defun org-list-set-ind (item struct ind
)
872 "Set indentation of ITEM in STRUCT to IND.
873 \nThis function modifies STRUCT."
874 (org-list-set-nth 1 item struct ind
))
876 (defsubst org-list-get-bullet
(item struct
)
877 "Return bullet of ITEM in STRUCT."
878 (org-list-get-nth 2 item struct
))
880 (defun org-list-set-bullet (item struct bullet
)
881 "Set bullet of ITEM in STRUCT to BULLET.
882 \nThis function modifies STRUCT."
883 (org-list-set-nth 2 item struct bullet
))
885 (defsubst org-list-get-counter
(item struct
)
886 "Return counter of ITEM in STRUCT."
887 (org-list-get-nth 3 item struct
))
889 (defsubst org-list-get-checkbox
(item struct
)
890 "Return checkbox of ITEM in STRUCT or nil."
891 (org-list-get-nth 4 item struct
))
893 (defun org-list-set-checkbox (item struct checkbox
)
894 "Set checkbox of ITEM in STRUCT to CHECKBOX.
895 \nThis function modifies STRUCT."
896 (org-list-set-nth 4 item struct checkbox
))
898 (defsubst org-list-get-tag
(item struct
)
899 "Return end position of ITEM in STRUCT."
900 (org-list-get-nth 5 item struct
))
902 (defun org-list-get-item-end (item struct
)
903 "Return end position of ITEM in STRUCT."
904 (org-list-get-nth 6 item struct
))
906 (defun org-list-get-item-end-before-blank (item struct
)
907 "Return point at end of ITEM in STRUCT, before any blank line.
908 Point returned is at end of line."
910 (goto-char (org-list-get-item-end item struct
))
911 (skip-chars-backward " \r\t\n")
914 (defun org-list-get-parent (item struct parents
)
915 "Return parent of ITEM or nil.
916 STRUCT is the list structure. PARENTS is the alist of parents,
917 as returned by `org-list-parents-alist'."
918 (let ((parents (or parents
(org-list-parents-alist struct
))))
919 (cdr (assq item parents
))))
921 (defun org-list-has-child-p (item struct
)
922 "Non-nil if ITEM has a child.
924 STRUCT is the list structure.
926 Value returned is the position of the first child of ITEM."
927 (let ((ind (org-list-get-ind item struct
))
928 (child-maybe (car (nth 1 (member (assq item struct
) struct
)))))
929 (when (and child-maybe
930 (< ind
(org-list-get-ind child-maybe struct
)))
933 (defun org-list-get-next-item (item _struct prevs
)
934 "Return next item in same sub-list as ITEM, or nil.
935 STRUCT is the list structure. PREVS is the alist of previous
936 items, as returned by `org-list-prevs-alist'."
937 (car (rassq item prevs
)))
939 (defun org-list-get-prev-item (item _struct prevs
)
940 "Return previous item in same sub-list as ITEM, or nil.
941 STRUCT is the list structure. PREVS is the alist of previous
942 items, as returned by `org-list-prevs-alist'."
943 (cdr (assq item prevs
)))
945 (defun org-list-get-subtree (item struct
)
946 "List all items having ITEM as a common ancestor, or nil.
947 STRUCT is the list structure."
948 (let* ((item-end (org-list-get-item-end item struct
))
949 (sub-struct (cdr (member (assq item struct
) struct
)))
954 (if (< pos item-end
) (push pos subtree
) (throw 'exit nil
))))
958 (defun org-list-get-all-items (item struct prevs
)
959 "List all items in the same sub-list as ITEM.
960 STRUCT is the list structure. PREVS is the alist of previous
961 items, as returned by `org-list-prevs-alist'."
962 (let ((prev-item item
)
964 before-item after-item
)
965 (while (setq prev-item
(org-list-get-prev-item prev-item struct prevs
))
966 (push prev-item before-item
))
967 (while (setq next-item
(org-list-get-next-item next-item struct prevs
))
968 (push next-item after-item
))
969 (append before-item
(list item
) (nreverse after-item
))))
971 (defun org-list-get-children (item _struct parents
)
972 "List all children of ITEM, or nil.
973 STRUCT is the list structure. PARENTS is the alist of parents,
974 as returned by `org-list-parents-alist'."
976 (while (setq child
(car (rassq item parents
)))
977 (setq parents
(cdr (member (assq child parents
) parents
)))
981 (defun org-list-get-top-point (struct)
982 "Return point at beginning of list.
983 STRUCT is the list structure."
986 (defun org-list-get-bottom-point (struct)
987 "Return point at bottom of list.
988 STRUCT is the list structure."
990 (mapcar (lambda (e) (org-list-get-item-end (car e
) struct
)) struct
)))
992 (defun org-list-get-list-begin (item struct prevs
)
993 "Return point at beginning of sub-list ITEM belongs.
994 STRUCT is the list structure. PREVS is the alist of previous
995 items, as returned by `org-list-prevs-alist'."
996 (let ((first-item item
) prev-item
)
997 (while (setq prev-item
(org-list-get-prev-item first-item struct prevs
))
998 (setq first-item prev-item
))
1001 (defalias 'org-list-get-first-item
'org-list-get-list-begin
)
1003 (defun org-list-get-last-item (item struct prevs
)
1004 "Return point at last item of sub-list ITEM belongs.
1005 STRUCT is the list structure. PREVS is the alist of previous
1006 items, as returned by `org-list-prevs-alist'."
1007 (let ((last-item item
) next-item
)
1008 (while (setq next-item
(org-list-get-next-item last-item struct prevs
))
1009 (setq last-item next-item
))
1012 (defun org-list-get-list-end (item struct prevs
)
1013 "Return point at end 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 (org-list-get-item-end (org-list-get-last-item item struct prevs
) struct
))
1018 (defun org-list-get-list-type (item struct prevs
)
1019 "Return the type of the list containing ITEM, as a symbol.
1021 STRUCT is the list structure. PREVS is the alist of previous
1022 items, as returned by `org-list-prevs-alist'.
1024 Possible types are `descriptive', `ordered' and `unordered'. The
1025 type is determined by the first item of the list."
1026 (let ((first (org-list-get-list-begin item struct prevs
)))
1028 ((string-match-p "[[:alnum:]]" (org-list-get-bullet first struct
)) 'ordered
)
1029 ((org-list-get-tag first struct
) 'descriptive
)
1032 (defun org-list-get-item-number (item struct prevs parents
)
1033 "Return ITEM's sequence number.
1035 STRUCT is the list structure. PREVS is the alist of previous
1036 items, as returned by `org-list-prevs-alist'. PARENTS is the
1037 alist of ancestors, as returned by `org-list-parents-alist'.
1039 Return value is a list of integers. Counters have an impact on
1041 (let ((get-relative-number
1043 (lambda (item struct prevs
)
1044 ;; Return relative sequence number of ITEM in the sub-list
1045 ;; it belongs. STRUCT is the list structure. PREVS is
1046 ;; the alist of previous items.
1047 (let ((seq 0) (pos item
) counter
)
1048 (while (and (not (setq counter
(org-list-get-counter pos struct
)))
1049 (setq pos
(org-list-get-prev-item pos struct prevs
)))
1051 (if (not counter
) (1+ seq
)
1053 ((string-match "[A-Za-z]" counter
)
1054 (+ (- (string-to-char (upcase (match-string 0 counter
))) 64)
1056 ((string-match "[0-9]+" counter
)
1057 (+ (string-to-number (match-string 0 counter
)) seq
))
1059 ;; Cons each parent relative number into return value (OUT).
1060 (let ((out (list (funcall get-relative-number item struct prevs
)))
1062 (while (setq parent
(org-list-get-parent parent struct parents
))
1063 (push (funcall get-relative-number parent struct prevs
) out
))
1071 (defun org-list-search-generic (search re bound noerr
)
1072 "Search a string in valid contexts for lists.
1073 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1074 in `re-search-forward'."
1076 (let ((origin (point)))
1078 ;; 1. No match: return to origin or bound, depending on NOERR.
1079 (unless (funcall search re bound noerr
)
1080 (throw 'exit
(and (goto-char (if (memq noerr
'(t nil
)) origin bound
))
1082 ;; 2. Match in valid context: return point. Else, continue
1084 (when (org-list-in-valid-context-p) (throw 'exit
(point)))))))
1086 (defun org-list-search-backward (regexp &optional bound noerror
)
1087 "Like `re-search-backward' but stop only where lists are recognized.
1088 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1089 `re-search-backward'."
1090 (org-list-search-generic #'re-search-backward
1091 regexp
(or bound
(point-min)) noerror
))
1093 (defun org-list-search-forward (regexp &optional bound noerror
)
1094 "Like `re-search-forward' but stop only where lists are recognized.
1095 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1096 `re-search-forward'."
1097 (org-list-search-generic #'re-search-forward
1098 regexp
(or bound
(point-max)) noerror
))
1102 ;;; Methods on structures
1104 (defsubst org-list-bullet-string
(bullet)
1105 "Return BULLET with the correct number of whitespaces.
1106 It determines the number of whitespaces to append by looking at
1107 `org-list-two-spaces-after-bullet-regexp'."
1109 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1111 org-list-two-spaces-after-bullet-regexp bullet
))
1114 (if (string-match "\\S-+\\([ \t]*\\)" bullet
)
1115 (replace-match spaces nil nil bullet
1)
1118 (defun org-list-swap-items (beg-A beg-B struct
)
1119 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1121 Blank lines at the end of items are left in place. Item
1122 visibility is preserved. Return the new structure after the
1125 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1126 to the same sub-list.
1128 This function modifies STRUCT."
1130 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct
))
1131 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct
))
1132 (end-A (org-list-get-item-end beg-A struct
))
1133 (end-B (org-list-get-item-end beg-B struct
))
1134 (size-A (- end-A-no-blank beg-A
))
1135 (size-B (- end-B-no-blank beg-B
))
1136 (body-A (buffer-substring beg-A end-A-no-blank
))
1137 (body-B (buffer-substring beg-B end-B-no-blank
))
1138 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B
))
1139 (sub-A (cons beg-A
(org-list-get-subtree beg-A struct
)))
1140 (sub-B (cons beg-B
(org-list-get-subtree beg-B struct
)))
1141 ;; Store overlays responsible for visibility status. We
1142 ;; also need to store their boundaries as they will be
1143 ;; removed from buffer.
1148 (and (>= (overlay-start o
) beg-A
)
1149 (<= (overlay-end o
) end-A
)
1150 (list o
(overlay-start o
) (overlay-end o
))))
1151 (overlays-in beg-A end-A
)))
1154 (and (>= (overlay-start o
) beg-B
)
1155 (<= (overlay-end o
) end-B
)
1156 (list o
(overlay-start o
) (overlay-end o
))))
1157 (overlays-in beg-B end-B
))))))
1158 ;; 1. Move effectively items in buffer.
1160 (delete-region beg-A end-B-no-blank
)
1161 (insert (concat body-B between-A-no-blank-and-B body-A
))
1162 ;; 2. Now modify struct. No need to re-read the list, the
1163 ;; transformation is just a shift of positions. Some special
1164 ;; attention is required for items ending at END-A and END-B
1165 ;; as empty spaces are not moved there. In others words,
1166 ;; item BEG-A will end with whitespaces that were at the end
1167 ;; of BEG-B and the same applies to BEG-B.
1169 (let ((pos (car e
)))
1173 (let ((end-e (nth 6 e
)))
1174 (setcar e
(+ pos
(- end-B-no-blank end-A-no-blank
)))
1175 (setcar (nthcdr 6 e
)
1176 (+ end-e
(- end-B-no-blank end-A-no-blank
)))
1177 (when (= end-e end-A
) (setcar (nthcdr 6 e
) end-B
))))
1179 (let ((end-e (nth 6 e
)))
1180 (setcar e
(- (+ pos beg-A
) beg-B
))
1181 (setcar (nthcdr 6 e
) (+ end-e
(- beg-A beg-B
)))
1182 (when (= end-e end-B
)
1183 (setcar (nthcdr 6 e
)
1184 (+ beg-A size-B
(- end-A end-A-no-blank
))))))
1186 (let ((end-e (nth 6 e
)))
1187 (setcar e
(+ pos
(- size-B size-A
)))
1188 (setcar (nthcdr 6 e
) (+ end-e
(- size-B size-A
))))))))
1189 (setq struct
(sort struct
#'car-less-than-car
))
1190 ;; Restore visibility status, by moving overlays to their new
1192 (dolist (ov (car overlays
))
1195 (+ (nth 1 ov
) (- (+ beg-B
(- size-B size-A
)) beg-A
))
1196 (+ (nth 2 ov
) (- (+ beg-B
(- size-B size-A
)) beg-A
))))
1197 (dolist (ov (cdr overlays
))
1198 (move-overlay (car ov
)
1199 (+ (nth 1 ov
) (- beg-A beg-B
))
1200 (+ (nth 2 ov
) (- beg-A beg-B
))))
1201 ;; Return structure.
1204 (defun org-list-separating-blank-lines-number (pos struct prevs
)
1205 "Return number of blank lines that should separate items in list.
1207 POS is the position of point where `org-list-insert-item' was called.
1209 STRUCT is the list structure. PREVS is the alist of previous
1210 items, as returned by `org-list-prevs-alist'.
1212 Assume point is at item's beginning. If the item is alone, apply
1213 some heuristics to guess the result."
1215 (let ((item (point))
1217 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
1222 ;; Count blank lines above beginning of line.
1224 (count-lines (goto-char (point-at-bol))
1225 (progn (skip-chars-backward " \r\t\n")
1229 ;; Trivial cases where there should be none.
1230 ((not insert-blank-p
) 0)
1231 ;; When `org-blank-before-new-entry' says so, it is 1.
1232 ((eq insert-blank-p t
) 1)
1233 ;; `plain-list-item' is 'auto. Count blank lines separating
1234 ;; neighbors' items in list.
1235 (t (let ((next-p (org-list-get-next-item item struct prevs
)))
1237 ;; Is there a next item?
1238 (next-p (goto-char next-p
)
1239 (funcall count-blanks
))
1240 ;; Is there a previous item?
1241 ((org-list-get-prev-item item struct prevs
)
1242 (funcall count-blanks
))
1243 ;; User inserted blank lines, trust him.
1244 ((and (> pos
(org-list-get-item-end-before-blank item struct
))
1245 (> (save-excursion (goto-char pos
)
1246 (setq usr-blank
(funcall count-blanks
)))
1249 ;; Are there blank lines inside the list so far?
1251 (goto-char (org-list-get-top-point struct
))
1252 ;; Do not use `org-list-search-forward' so blank lines
1253 ;; in blocks can be counted in.
1255 "^[ \t]*$" (org-list-get-item-end-before-blank item struct
) t
))
1257 ;; Default choice: no blank line.
1260 (defun org-list-insert-item (pos struct prevs
&optional checkbox after-bullet
)
1261 "Insert a new list item at POS and return the new structure.
1262 If POS is before first character after bullet of the item, the
1263 new item will be created before the current one.
1265 STRUCT is the list structure. PREVS is the alist of previous
1266 items, as returned by `org-list-prevs-alist'.
1268 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1269 after the bullet. Cursor will be after this text once the
1272 This function modifies STRUCT."
1273 (let ((case-fold-search t
))
1274 ;; 1. Get information about list: position of point with regards
1275 ;; to item start (BEFOREP), blank lines number separating items
1276 ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
1277 (let* ((item (progn (goto-char pos
) (goto-char (org-list-get-item-begin))))
1278 (item-end (org-list-get-item-end item struct
))
1279 (item-end-no-blank (org-list-get-item-end-before-blank item struct
))
1282 (looking-at org-list-full-item-re
)
1285 ((not (match-beginning 4)) (match-end 0))
1286 ;; Ignore tag in a non-descriptive list.
1287 ((save-match-data (string-match "[.)]" (match-string 1)))
1288 (match-beginning 4))
1290 (goto-char (match-end 4))
1291 (skip-chars-forward " \t")
1293 (split-line-p (org-get-alist-option org-M-RET-may-split-line
'item
))
1294 (blank-nb (org-list-separating-blank-lines-number
1296 ;; 2. Build the new item to be created. Concatenate same
1297 ;; bullet as item, checkbox, text AFTER-BULLET if
1298 ;; provided, and text cut from point to end of item
1299 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1300 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1301 ;; between what was cut and what was inserted in buffer
1302 ;; is stored in SIZE-OFFSET.
1303 (ind (org-list-get-ind item struct
))
1304 (ind-size (if indent-tabs-mode
1305 (+ (/ ind tab-width
) (mod ind tab-width
))
1307 (bullet (org-list-bullet-string (org-list-get-bullet item struct
)))
1308 (box (when checkbox
"[ ]"))
1310 (and (not beforep
) split-line-p
1313 ;; If POS is greater than ITEM-END, then point is
1314 ;; in some white lines after the end of the list.
1315 ;; Those must be removed, or they will be left,
1316 ;; stacking up after the list.
1317 (when (< item-end pos
)
1318 (delete-region (1- item-end
) (point-at-eol)))
1319 (skip-chars-backward " \r\t\n")
1321 (delete-and-extract-region pos item-end-no-blank
))))
1322 (body (concat bullet
(when box
(concat box
" ")) after-bullet
1324 (if (string-match "\\`[ \t]+" text-cut
)
1325 (replace-match "" t t text-cut
)
1327 (item-sep (make-string (1+ blank-nb
) ?
\n))
1328 (item-size (+ ind-size
(length body
) (length item-sep
)))
1329 (size-offset (- item-size
(length text-cut
))))
1330 ;; 4. Insert effectively item into buffer.
1332 (indent-to-column ind
)
1333 (insert body item-sep
)
1334 ;; 5. Add new item to STRUCT.
1336 (let ((p (car e
)) (end (nth 6 e
)))
1338 ;; Before inserted item, positions don't change but
1339 ;; an item ending after insertion has its end shifted
1342 (when (> end item
) (setcar (nthcdr 6 e
) (+ end size-offset
))))
1343 ;; Trivial cases where current item isn't split in
1344 ;; two. Just shift every item after new one by
1346 ((or beforep
(not split-line-p
))
1347 (setcar e
(+ p item-size
))
1348 (setcar (nthcdr 6 e
) (+ end item-size
)))
1349 ;; Item is split in two: elements before POS are just
1350 ;; shifted by ITEM-SIZE. In the case item would end
1351 ;; after split POS, ending is only shifted by
1354 (setcar e
(+ p item-size
))
1356 (setcar (nthcdr 6 e
) (+ end item-size
))
1357 (setcar (nthcdr 6 e
) (+ end size-offset
))))
1358 ;; Elements after POS are moved into new item.
1359 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1360 ;; doesn't appear in buffer yet.
1362 (setcar e
(+ p size-offset
(- item pos
(length item-sep
))))
1363 (if (= end item-end
)
1364 (setcar (nthcdr 6 e
) (+ item item-size
))
1365 (setcar (nthcdr 6 e
)
1367 (- item pos
(length item-sep
))))))
1368 ;; Elements at ITEM-END or after are only shifted by
1370 (t (setcar e
(+ p size-offset
))
1371 (setcar (nthcdr 6 e
) (+ end size-offset
))))))
1373 (push (list item ind bullet nil box nil
(+ item item-size
)) struct
)
1374 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1375 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1376 ;; exchange ITEM with the next item in list. Position cursor
1377 ;; after bullet, counter, checkbox, and label.
1380 (setq struct
(org-list-swap-items item
(+ item item-size
) struct
))
1381 (goto-char (org-list-get-next-item
1382 item struct
(org-list-prevs-alist struct
))))
1385 (defun org-list-delete-item (item struct
)
1386 "Remove ITEM from the list and return the new structure.
1388 STRUCT is the list structure."
1389 (let* ((end (org-list-get-item-end item struct
))
1390 (beg (if (= (org-list-get-bottom-point struct
) end
)
1391 ;; If ITEM ends with the list, delete blank lines
1395 (skip-chars-backward " \r\t\n")
1396 (min (1+ (point-at-eol)) (point-max)))
1398 ;; Remove item from buffer.
1399 (delete-region beg end
)
1400 ;; Remove item from structure and shift others items accordingly.
1401 ;; Don't forget to shift also ending position when appropriate.
1402 (let ((size (- end beg
)))
1403 (delq nil
(mapcar (lambda (e)
1404 (let ((pos (car e
)))
1407 (let ((end-e (nth 6 e
)))
1411 (append (butlast e
) (list beg
)))
1413 (append (butlast e
) (list (- end-e size
)))))))
1417 (append (butlast (cdr e
))
1418 (list (- (nth 6 e
) size
))))))))
1421 (defun org-list-send-item (item dest struct
)
1422 "Send ITEM to destination DEST.
1424 STRUCT is the list structure.
1426 DEST can have various values.
1428 If DEST is a buffer position, the function will assume it points
1429 to another item in the same list as ITEM, and will move the
1430 latter just before the former.
1432 If DEST is `begin' (respectively `end'), ITEM will be moved at
1433 the beginning (respectively end) of the list it belongs to.
1435 If DEST is a string like \"N\", where N is an integer, ITEM will
1436 be moved at the Nth position in the list.
1438 If DEST is `kill', ITEM will be deleted and its body will be
1439 added to the kill-ring.
1441 If DEST is `delete', ITEM will be deleted.
1443 Visibility of item is preserved.
1445 This function returns, destructively, the new list structure."
1446 (let* ((prevs (org-list-prevs-alist struct
))
1447 (item-end (org-list-get-item-end item struct
))
1448 ;; Grab full item body minus its bullet.
1455 (regexp-quote (org-list-get-bullet item struct
))))
1458 ;; Change DEST into a buffer position. A trick is needed
1459 ;; when ITEM is meant to be sent at the end of the list.
1460 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1461 ;; nil and insertion point (INS-POINT) to the first line's
1462 ;; end of the last item, we ensure the new item will be
1463 ;; inserted after the last item, and not after any of its
1464 ;; hypothetical sub-items.
1466 ((or (eq dest
'kill
) (eq dest
'delete
)))
1468 (setq dest
(org-list-get-list-begin item struct prevs
)))
1470 (setq dest
(org-list-get-list-end item struct prevs
))
1472 (goto-char (org-list-get-last-item item struct prevs
))
1474 ((string-match-p "\\`[0-9]+\\'" dest
)
1475 (let* ((all (org-list-get-all-items item struct prevs
))
1477 (index (mod (string-to-number dest
) len
)))
1478 (if (not (zerop index
))
1479 (setq dest
(nth (1- index
) all
))
1480 ;; Send ITEM at the end of the list.
1481 (setq dest
(org-list-get-list-end item struct prevs
))
1484 (org-list-get-last-item item struct prevs
))
1487 (org-M-RET-may-split-line nil
)
1488 ;; Store inner overlays (to preserve visibility).
1489 (overlays (cl-remove-if (lambda (o) (or (< (overlay-start o
) item
)
1490 (> (overlay-end o
) item
)))
1491 (overlays-in item item-end
))))
1493 ((eq dest
'delete
) (org-list-delete-item item struct
))
1496 (org-list-delete-item item struct
))
1497 ((and (integerp dest
) (/= item ins-point
))
1498 (setq item
(copy-marker item
))
1499 (setq struct
(org-list-insert-item ins-point struct prevs nil body
))
1500 ;; 1. Structure returned by `org-list-insert-item' may not be
1501 ;; accurate, as it cannot see sub-items included in BODY.
1502 ;; Thus, first compute the real structure so far.
1504 (cons (marker-position item
)
1505 (org-list-get-subtree (marker-position item
) struct
)))
1506 (new-end (org-list-get-item-end (point) struct
))
1507 (old-end (org-list-get-item-end (marker-position item
) struct
))
1509 (shift (- (point) item
)))
1510 ;; 1.1. Remove the item just created in structure.
1511 (setq struct
(delete (assq new-item struct
) struct
))
1512 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1517 (let* ((cell (assq e struct
))
1521 (append (butlast (cdr cell
))
1522 (list (if (= end old-end
)
1526 #'car-less-than-car
)))
1527 ;; 2. Restore inner overlays.
1528 (dolist (o overlays
)
1530 (+ (overlay-start o
) (- (point) item
))
1531 (+ (overlay-end o
) (- (point) item
))))
1532 ;; 3. Eventually delete extra copy of the item and clean marker.
1533 (prog1 (org-list-delete-item (marker-position item
) struct
)
1534 (move-marker item nil
)))
1537 (defun org-list-struct-outdent (start end struct parents
)
1538 "Outdent items between positions START and END.
1540 STRUCT is the list structure. PARENTS is the alist of items'
1541 parents, as returned by `org-list-parents-alist'.
1543 START is included, END excluded."
1546 (let* ((item (car cell
))
1547 (parent (cdr cell
)))
1549 ;; Item not yet in zone: keep association.
1550 ((< item start
) cell
)
1551 ;; Item out of zone: follow associations in ACC.
1553 (let ((convert (and parent
(assq parent acc
))))
1554 (if convert
(cons item
(cdr convert
)) cell
)))
1555 ;; Item has no parent: error
1557 (error "Cannot outdent top-level items"))
1558 ;; Parent is outdented: keep association.
1560 (push (cons parent item
) acc
) cell
)
1562 ;; Parent isn't outdented: reparent to grand-parent.
1563 (let ((grand-parent (org-list-get-parent
1564 parent struct parents
)))
1565 (push (cons parent item
) acc
)
1566 (cons item grand-parent
))))))))
1567 (mapcar out parents
)))
1569 (defun org-list-struct-indent (start end struct parents prevs
)
1570 "Indent items between positions START and END.
1572 STRUCT is the list structure. PARENTS is the alist of parents
1573 and PREVS is the alist of previous items, returned by,
1574 respectively, `org-list-parents-alist' and
1575 `org-list-prevs-alist'.
1577 START is included and END excluded.
1579 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1580 bullets between START and END."
1582 (set-assoc (lambda (cell) (push cell acc
) cell
))
1583 (change-bullet-maybe
1588 ;; Normalize ordered bullets.
1589 (let ((bul (org-trim
1590 (org-list-get-bullet item struct
))))
1591 (cond ((string-match "[A-Z]\\." bul
) "A.")
1592 ((string-match "[A-Z])" bul
) "A)")
1593 ((string-match "[a-z]\\." bul
) "a.")
1594 ((string-match "[a-z])" bul
) "a)")
1595 ((string-match "[0-9]\\." bul
) "1.")
1596 ((string-match "[0-9])" bul
) "1)")
1598 org-list-demote-modify-bullet
))))
1599 (when new-bul-p
(org-list-set-bullet item struct new-bul-p
))))))
1602 (let* ((item (car cell
))
1603 (parent (cdr cell
)))
1605 ;; Item not yet in zone: keep association.
1606 ((< item start
) cell
)
1608 ;; Item out of zone: follow associations in ACC.
1609 (let ((convert (assq parent acc
)))
1610 (if convert
(cons item
(cdr convert
)) cell
)))
1612 ;; Item is in zone...
1613 (let ((prev (org-list-get-prev-item item struct prevs
)))
1614 ;; Check if bullet needs to be changed.
1615 (funcall change-bullet-maybe item
)
1617 ;; First item indented but not parent: error
1618 ((and (not prev
) (< parent start
))
1619 (error "Cannot indent the first item of a list"))
1620 ;; First item and parent indented: keep same
1622 ((not prev
) (funcall set-assoc cell
))
1623 ;; Previous item not indented: reparent to it.
1624 ((< prev start
) (funcall set-assoc
(cons item prev
)))
1625 ;; Previous item indented: reparent like it.
1628 (cons item
(cdr (assq prev acc
)))))))))))))
1629 (mapcar ind parents
)))
1633 ;;; Repairing structures
1635 (defun org-list-use-alpha-bul-p (first struct prevs
)
1636 "Non-nil if list starting at FIRST can have alphabetical bullets.
1638 STRUCT is list structure. PREVS is the alist of previous items,
1639 as returned by `org-list-prevs-alist'."
1640 (and org-list-allow-alphabetical
1642 (let ((item first
) (ascii 64) (case-fold-search nil
))
1643 ;; Pretend that bullets are uppercase and check if alphabet
1644 ;; is sufficient, taking counters into account.
1646 (let ((count (org-list-get-counter item struct
)))
1647 ;; Virtually determine current bullet
1648 (if (and count
(string-match-p "[a-zA-Z]" count
))
1649 ;; Counters are not case-sensitive.
1650 (setq ascii
(string-to-char (upcase count
)))
1651 (setq ascii
(1+ ascii
)))
1652 ;; Test if bullet would be over z or Z.
1655 (setq item
(org-list-get-next-item item struct prevs
)))))
1656 ;; All items checked. All good.
1659 (defun org-list-inc-bullet-maybe (bullet)
1660 "Increment BULLET if applicable."
1661 (let ((case-fold-search nil
))
1663 ;; Num bullet: increment it.
1664 ((string-match "[0-9]+" bullet
)
1666 (number-to-string (1+ (string-to-number (match-string 0 bullet
))))
1668 ;; Alpha bullet: increment it.
1669 ((string-match "[A-Za-z]" bullet
)
1671 (char-to-string (1+ (string-to-char (match-string 0 bullet
))))
1673 ;; Unordered bullet: leave it.
1676 (defun org-list-struct-fix-bul (struct prevs
)
1677 "Verify and correct bullets in STRUCT.
1678 PREVS is the alist of previous items, as returned by
1679 `org-list-prevs-alist'.
1681 This function modifies STRUCT."
1682 (let ((case-fold-search nil
)
1685 ;; Set bullet of ITEM in STRUCT, depending on the type of
1686 ;; first item of the list, the previous bullet and counter
1689 (let* ((prev (org-list-get-prev-item item struct prevs
))
1690 (prev-bul (and prev
(org-list-get-bullet prev struct
)))
1691 (counter (org-list-get-counter item struct
))
1692 (bullet (org-list-get-bullet item struct
))
1693 (alphap (and (not prev
)
1694 (org-list-use-alpha-bul-p item struct prevs
))))
1695 (org-list-set-bullet
1697 (org-list-bullet-string
1699 ;; Alpha counter in alpha list: use counter.
1701 (string-match "[a-zA-Z]" counter
)
1702 (string-match "[a-zA-Z]" prev-bul
))
1703 ;; Use cond to be sure `string-match' is used in
1707 ((string-match "[a-z]" prev-bul
) (downcase counter
))
1708 ((string-match "[A-Z]" prev-bul
) (upcase counter
)))))
1709 (replace-match real-count nil nil prev-bul
)))
1710 ;; Num counter in a num list: use counter.
1712 (string-match "[0-9]+" counter
)
1713 (string-match "[0-9]+" prev-bul
))
1714 (replace-match counter nil nil prev-bul
))
1715 ;; No counter: increase, if needed, previous bullet.
1717 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct
)))
1718 ;; Alpha counter at first item: use counter.
1719 ((and counter
(org-list-use-alpha-bul-p item struct prevs
)
1720 (string-match "[A-Za-z]" counter
)
1721 (string-match "[A-Za-z]" bullet
))
1724 ((string-match "[a-z]" bullet
) (downcase counter
))
1725 ((string-match "[A-Z]" bullet
) (upcase counter
)))))
1726 (replace-match real-count nil nil bullet
)))
1727 ;; Num counter at first item: use counter.
1729 (string-match "[0-9]+" counter
)
1730 (string-match "[0-9]+" bullet
))
1731 (replace-match counter nil nil bullet
))
1732 ;; First bullet is alpha uppercase: use "A".
1733 ((and alphap
(string-match "[A-Z]" bullet
))
1734 (replace-match "A" nil nil bullet
))
1735 ;; First bullet is alpha lowercase: use "a".
1736 ((and alphap
(string-match "[a-z]" bullet
))
1737 (replace-match "a" nil nil bullet
))
1738 ;; First bullet is num: use "1".
1739 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet
)
1740 (replace-match "1" nil nil bullet
))
1741 ;; Not an ordered list: keep bullet.
1743 (mapc fix-bul
(mapcar #'car struct
))))
1745 (defun org-list-struct-fix-ind (struct parents
&optional bullet-size
)
1746 "Verify and correct indentation in STRUCT.
1748 PARENTS is the alist of parents, as returned by
1749 `org-list-parents-alist'.
1751 If numeric optional argument BULLET-SIZE is set, assume all
1752 bullets in list have this length to determine new indentation.
1754 This function modifies STRUCT."
1755 (let* ((ancestor (org-list-get-top-point struct
))
1756 (top-ind (org-list-get-ind ancestor struct
))
1759 (let ((parent (org-list-get-parent item struct parents
)))
1761 ;; Indent like parent + length of parent's bullet +
1764 item struct
(+ (or bullet-size
1766 (org-list-get-bullet parent struct
)))
1767 (org-list-get-ind parent struct
)
1768 org-list-indent-offset
))
1769 ;; If no parent, indent like top-point.
1770 (org-list-set-ind item struct top-ind
))))))
1771 (mapc new-ind
(mapcar #'car
(cdr struct
)))))
1773 (defun org-list-struct-fix-box (struct parents prevs
&optional ordered
)
1774 "Verify and correct checkboxes in STRUCT.
1776 PARENTS is the alist of parents and PREVS is the alist of
1777 previous items, as returned by, respectively,
1778 `org-list-parents-alist' and `org-list-prevs-alist'.
1780 If ORDERED is non-nil, a checkbox can only be checked when every
1781 checkbox before it is checked too. If there was an attempt to
1782 break this rule, the function will return the blocking item. In
1783 all others cases, the return value will be nil.
1785 This function modifies STRUCT."
1786 (let ((all-items (mapcar #'car struct
))
1791 (mapcar (lambda (child)
1792 (org-list-get-checkbox child struct
))
1793 (org-list-get-children item struct parents
))))
1794 (org-list-set-checkbox
1797 ((and (member "[ ]" box-list
) (member "[X]" box-list
)) "[-]")
1798 ((member "[-]" box-list
) "[-]")
1799 ((member "[X]" box-list
) "[X]")
1800 ((member "[ ]" box-list
) "[ ]")
1801 ;; Parent has no boxed child: leave box as-is.
1802 (t (org-list-get-checkbox item struct
))))))))
1804 ;; 1. List all parents with a checkbox.
1807 (let* ((parent (org-list-get-parent e struct parents
))
1808 (parent-box-p (org-list-get-checkbox parent struct
)))
1809 (when (and parent-box-p
(not (memq parent parent-list
)))
1810 (push parent parent-list
))))
1812 ;; 2. Sort those parents by decreasing indentation.
1813 (setq parent-list
(sort parent-list
1815 (> (org-list-get-ind e1 struct
)
1816 (org-list-get-ind e2 struct
)))))
1817 ;; 3. For each parent, get all children's checkboxes to determine
1818 ;; and set its checkbox accordingly.
1819 (mapc set-parent-box parent-list
)
1820 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1823 (mapcar (lambda (e) (org-list-get-checkbox e struct
)) all-items
))
1824 (after-unchecked (member "[ ]" box-list
)))
1825 ;; There are boxes checked after an unchecked one: fix that.
1826 (when (member "[X]" after-unchecked
)
1827 (let ((index (- (length struct
) (length after-unchecked
))))
1829 (when (org-list-get-checkbox e struct
)
1830 (org-list-set-checkbox e struct
"[ ]")))
1831 (nthcdr index all-items
))
1832 ;; Verify once again the structure, without ORDERED.
1833 (org-list-struct-fix-box struct parents prevs nil
)
1834 ;; Return blocking item.
1835 (nth index all-items
)))))))
1837 (defun org-list-struct-fix-item-end (struct)
1838 "Verify and correct each item end position in STRUCT.
1840 This function modifies STRUCT."
1841 (let (end-list acc-end
)
1843 (let* ((pos (car e
))
1844 (ind-pos (org-list-get-ind pos struct
))
1845 (end-pos (org-list-get-item-end pos struct
)))
1846 (unless (assq end-pos struct
)
1847 ;; To determine real ind of an ending position that is
1848 ;; not at an item, we have to find the item it belongs
1849 ;; to: it is the last item (ITEM-UP), whose ending is
1850 ;; further than the position we're interested in.
1851 (let ((item-up (assoc-default end-pos acc-end
'>)))
1853 ;; Else part is for the bottom point.
1854 (if item-up
(+ (org-list-get-ind item-up struct
) 2) 0)
1857 (push (cons ind-pos pos
) end-list
)
1858 (push (cons end-pos pos
) acc-end
)))
1860 (setq end-list
(sort end-list
(lambda (e1 e2
) (< (cdr e1
) (cdr e2
)))))
1861 (org-list-struct-assoc-end struct end-list
)))
1863 (defun org-list-struct-apply-struct (struct old-struct
)
1864 "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
1866 OLD-STRUCT is the structure before any modifications, and STRUCT
1867 the structure to be applied. The function will only modify parts
1868 of the list which have changed.
1870 Initial position of cursor is restored after the changes."
1871 (let* ((origin (point-marker))
1872 (inlinetask-re (and (featurep 'org-inlinetask
)
1873 (org-inlinetask-outline-regexp)))
1874 (item-re (org-item-re))
1877 ;; Shift the indentation between END and BEG by DELTA.
1878 ;; Start from the line before END.
1879 (lambda (end beg delta
)
1881 (skip-chars-backward " \r\t\n")
1883 (while (or (> (point) beg
)
1884 (and (= (point) beg
)
1885 (not (looking-at item-re
))))
1887 ;; Skip inline tasks.
1888 ((and inlinetask-re
(looking-at inlinetask-re
))
1889 (org-inlinetask-goto-beginning))
1890 ;; Shift only non-empty lines.
1891 ((looking-at-p "^[ \t]*\\S-")
1892 (indent-line-to (+ (org-get-indentation) delta
))))
1893 (forward-line -
1)))))
1896 ;; Replace ITEM first line elements with new elements from
1897 ;; STRUCT, if appropriate.
1900 (let* ((new-ind (org-list-get-ind item struct
))
1901 (old-ind (org-get-indentation))
1902 (new-bul (org-list-bullet-string
1903 (org-list-get-bullet item struct
)))
1904 (old-bul (org-list-get-bullet item old-struct
))
1905 (new-box (org-list-get-checkbox item struct
)))
1906 (looking-at org-list-full-item-re
)
1907 ;; a. Replace bullet
1908 (unless (equal old-bul new-bul
)
1909 (replace-match new-bul nil nil nil
1))
1910 ;; b. Replace checkbox.
1912 ((equal (match-string 3) new-box
))
1913 ((and (match-string 3) new-box
)
1914 (replace-match new-box nil nil nil
3))
1916 (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
1917 (replace-match "" nil nil nil
1))
1918 (t (let ((counterp (match-end 2)))
1919 (goto-char (if counterp
(1+ counterp
) (match-end 1)))
1920 (insert (concat new-box
(unless counterp
" "))))))
1921 ;; c. Indent item to appropriate column.
1922 (unless (= new-ind old-ind
)
1923 (delete-region (goto-char (point-at-bol))
1924 (progn (skip-chars-forward " \t") (point)))
1925 (indent-to new-ind
)))))))
1926 ;; 1. First get list of items and position endings. We maintain
1927 ;; two alists: ITM-SHIFT, determining indentation shift needed
1928 ;; at item, and END-LIST, a pseudo-alist where key is ending
1929 ;; position and value point.
1930 (let (end-list acc-end itm-shift all-ends sliced-struct
)
1931 (dolist (e old-struct
)
1932 (let* ((pos (car e
))
1933 (ind-pos (org-list-get-ind pos struct
))
1934 (ind-old (org-list-get-ind pos old-struct
))
1935 (bul-pos (org-list-get-bullet pos struct
))
1936 (bul-old (org-list-get-bullet pos old-struct
))
1937 (ind-shift (- (+ ind-pos
(length bul-pos
))
1938 (+ ind-old
(length bul-old
))))
1939 (end-pos (org-list-get-item-end pos old-struct
)))
1940 (push (cons pos ind-shift
) itm-shift
)
1941 (unless (assq end-pos old-struct
)
1942 ;; To determine real ind of an ending position that
1943 ;; is not at an item, we have to find the item it
1944 ;; belongs to: it is the last item (ITEM-UP), whose
1945 ;; ending is further than the position we're
1947 (let ((item-up (assoc-default end-pos acc-end
#'>)))
1948 (push (cons end-pos item-up
) end-list
)))
1949 (push (cons end-pos pos
) acc-end
)))
1950 ;; 2. Slice the items into parts that should be shifted by the
1951 ;; same amount of indentation. Each slice follow the pattern
1952 ;; (END BEG DELTA). Slices are returned in reverse order.
1953 (setq all-ends
(sort (append (mapcar #'car itm-shift
)
1954 (org-uniquify (mapcar #'car end-list
)))
1956 acc-end
(nreverse acc-end
))
1957 (while (cdr all-ends
)
1958 (let* ((up (pop all-ends
))
1959 (down (car all-ends
))
1960 (itemp (assq up struct
))
1962 (if itemp
(cdr (assq up itm-shift
))
1963 ;; If we're not at an item, there's a child of the
1964 ;; item point belongs to above. Make sure the less
1965 ;; indented line in this slice has the same column
1967 (let* ((child (cdr (assq up acc-end
)))
1968 (ind (org-list-get-ind child struct
))
1969 (min-ind most-positive-fixnum
))
1972 (while (< (point) down
)
1973 ;; Ignore empty lines. Also ignore blocks and
1974 ;; drawers contents.
1975 (unless (looking-at-p "[ \t]*$")
1976 (setq min-ind
(min (org-get-indentation) min-ind
))
1978 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1980 (format "^[ \t]*#\\+END%s[ \t]*$"
1983 ((and (looking-at org-drawer-regexp
)
1984 (re-search-forward "^[ \t]*:END:[ \t]*$"
1988 (push (list down up delta
) sliced-struct
)))
1989 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1990 ;; end to beginning. Take a special action when beginning is
1992 (dolist (e sliced-struct
)
1993 (unless (zerop (nth 2 e
)) (apply shift-body-ind e
))
1994 (let* ((beg (nth 1 e
))
1995 (cell (assq beg struct
)))
1996 (unless (or (not cell
) (equal cell
(assq beg old-struct
)))
1997 (funcall modify-item beg
)))))
1998 ;; 4. Go back to initial position and clean marker.
2000 (move-marker origin nil
)))
2002 (defun org-list-write-struct (struct parents
&optional old-struct
)
2003 "Correct bullets, checkboxes and indentation in list at point.
2005 STRUCT is the list structure. PARENTS is the alist of parents,
2006 as returned by `org-list-parents-alist'.
2008 When non-nil, optional argument OLD-STRUCT is the reference
2009 structure of the list. It should be provided whenever STRUCT
2010 doesn't correspond anymore to the real list in buffer."
2011 ;; Order of functions matters here: checkboxes and endings need
2012 ;; correct indentation to be set, and indentation needs correct
2015 ;; 0. Save a copy of structure before modifications
2016 (let ((old-struct (or old-struct
(copy-tree struct
))))
2017 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
2018 ;; order to get items endings and bullets properly
2019 (org-list-struct-fix-ind struct parents
2)
2020 ;; 2. Fix each item end to get correct prevs alist.
2021 (org-list-struct-fix-item-end struct
)
2022 ;; 3. Get bullets right.
2023 (let ((prevs (org-list-prevs-alist struct
)))
2024 (org-list-struct-fix-bul struct prevs
)
2025 ;; 4. Now get real indentation.
2026 (org-list-struct-fix-ind struct parents
)
2027 ;; 5. Eventually fix checkboxes.
2028 (org-list-struct-fix-box struct parents prevs
))
2029 ;; 6. Apply structure modifications to buffer.
2030 (org-list-struct-apply-struct struct old-struct
)))
2036 (defun org-apply-on-list (function init-value
&rest args
)
2037 "Call FUNCTION on each item of the list at point.
2038 FUNCTION must be called with at least one argument: INIT-VALUE,
2039 that will contain the value returned by the function at the
2040 previous item, plus ARGS extra arguments.
2042 FUNCTION is applied on items in reverse order.
2044 As an example, \(org-apply-on-list \(lambda \(result) \(1+ result)) 0)
2045 will return the number of items in the current list.
2047 Sublists of the list are skipped. Cursor is always at the
2048 beginning of the item."
2049 (let* ((struct (org-list-struct))
2050 (prevs (org-list-prevs-alist struct
))
2051 (item (copy-marker (point-at-bol)))
2052 (all (org-list-get-all-items (marker-position item
) struct prevs
))
2056 (setq value
(apply function value args
)))
2059 (move-marker item nil
)
2062 (defun org-list-set-item-visibility (item struct view
)
2063 "Set visibility of ITEM in STRUCT to VIEW.
2065 Possible values are: `folded', `children' or `subtree'. See
2066 `org-cycle' for more information."
2069 (let ((item-end (org-list-get-item-end-before-blank item struct
)))
2071 (outline-flag-region (save-excursion (goto-char item
) (point-at-eol))
2073 ((eq view
'children
)
2074 ;; First show everything.
2075 (org-list-set-item-visibility item struct
'subtree
)
2076 ;; Then fold every child.
2077 (let* ((parents (org-list-parents-alist struct
))
2078 (children (org-list-get-children item struct parents
)))
2080 (org-list-set-item-visibility e struct
'folded
))
2084 (let ((item-end (org-list-get-item-end item struct
)))
2085 (outline-flag-region item item-end nil
)))))
2087 (defun org-list-item-body-column (item)
2088 "Return column at which body of ITEM should start."
2091 (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?\\([ \t]+\\|$\\)")
2092 (if (match-beginning 2)
2093 (let ((start (1+ (match-end 2)))
2094 (ind (org-get-indentation)))
2095 (if (> start
(+ ind org-list-description-max-indent
)) (+ ind
5) start
))
2096 (+ (progn (goto-char (match-end 1)) (current-column))
2097 (if (and org-list-two-spaces-after-bullet-regexp
2098 (string-match-p org-list-two-spaces-after-bullet-regexp
2105 ;;; Interactive functions
2107 (defalias 'org-list-get-item-begin
'org-in-item-p
)
2109 (defun org-beginning-of-item ()
2110 "Go to the beginning of the current item.
2111 Throw an error when not in a list."
2113 (let ((begin (org-in-item-p)))
2114 (if begin
(goto-char begin
) (error "Not in an item"))))
2116 (defun org-beginning-of-item-list ()
2117 "Go to the beginning item of the current list or sublist.
2118 Throw an error when not in a list."
2120 (let ((begin (org-in-item-p)))
2122 (error "Not in an item")
2124 (let* ((struct (org-list-struct))
2125 (prevs (org-list-prevs-alist struct
)))
2126 (goto-char (org-list-get-list-begin begin struct prevs
))))))
2128 (defun org-end-of-item-list ()
2129 "Go to the end of the current list or sublist.
2130 Throw an error when not in a list."
2132 (let ((begin (org-in-item-p)))
2134 (error "Not in an item")
2136 (let* ((struct (org-list-struct))
2137 (prevs (org-list-prevs-alist struct
)))
2138 (goto-char (org-list-get-list-end begin struct prevs
))))))
2140 (defun org-end-of-item ()
2141 "Go to the end of the current item.
2142 Throw an error when not in a list."
2144 (let ((begin (org-in-item-p)))
2146 (error "Not in an item")
2148 (let ((struct (org-list-struct)))
2149 (goto-char (org-list-get-item-end begin struct
))))))
2151 (defun org-previous-item ()
2152 "Move to the beginning of the previous item.
2153 Throw an error when not in a list. Also throw an error when at
2154 first item, unless `org-list-use-circular-motion' is non-nil."
2156 (let ((item (org-in-item-p)))
2158 (error "Not in an item")
2160 (let* ((struct (org-list-struct))
2161 (prevs (org-list-prevs-alist struct
))
2162 (prevp (org-list-get-prev-item item struct prevs
)))
2164 (prevp (goto-char prevp
))
2165 (org-list-use-circular-motion
2166 (goto-char (org-list-get-last-item item struct prevs
)))
2167 (t (error "On first item")))))))
2169 (defun org-next-item ()
2170 "Move to the beginning of the next item.
2171 Throw an error when not in a list. Also throw an error when at
2172 last item, unless `org-list-use-circular-motion' is non-nil."
2174 (let ((item (org-in-item-p)))
2176 (error "Not in an item")
2178 (let* ((struct (org-list-struct))
2179 (prevs (org-list-prevs-alist struct
))
2180 (prevp (org-list-get-next-item item struct prevs
)))
2182 (prevp (goto-char prevp
))
2183 (org-list-use-circular-motion
2184 (goto-char (org-list-get-first-item item struct prevs
)))
2185 (t (error "On last item")))))))
2187 (defun org-move-item-down ()
2188 "Move the item at point down, i.e. swap with following item.
2189 Sub-items (items with larger indentation) are considered part of
2190 the item, so this really moves item trees."
2192 (unless (org-at-item-p) (error "Not at an item"))
2193 (let* ((col (current-column))
2194 (item (point-at-bol))
2195 (struct (org-list-struct))
2196 (prevs (org-list-prevs-alist struct
))
2197 (next-item (org-list-get-next-item (point-at-bol) struct prevs
)))
2198 (unless (or next-item org-list-use-circular-motion
)
2199 (user-error "Cannot move this item further down"))
2201 (setq struct
(org-list-send-item item
'begin struct
))
2202 (setq struct
(org-list-swap-items item next-item struct
))
2204 (org-list-get-next-item item struct
(org-list-prevs-alist struct
))))
2205 (org-list-write-struct struct
(org-list-parents-alist struct
))
2206 (org-move-to-column col
)))
2208 (defun org-move-item-up ()
2209 "Move the item at point up, i.e. swap with previous item.
2210 Sub-items (items with larger indentation) are considered part of
2211 the item, so this really moves item trees."
2213 (unless (org-at-item-p) (error "Not at an item"))
2214 (let* ((col (current-column))
2215 (item (point-at-bol))
2216 (struct (org-list-struct))
2217 (prevs (org-list-prevs-alist struct
))
2218 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs
)))
2219 (unless (or prev-item org-list-use-circular-motion
)
2220 (user-error "Cannot move this item further up"))
2222 (setq struct
(org-list-send-item item
'end struct
))
2223 (setq struct
(org-list-swap-items prev-item item struct
)))
2224 (org-list-write-struct struct
(org-list-parents-alist struct
))
2225 (org-move-to-column col
)))
2227 (defun org-insert-item (&optional checkbox
)
2228 "Insert a new item at the current level.
2229 If cursor is before first character after bullet of the item, the
2230 new item will be created before the current one.
2232 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2234 Return t when things worked, nil when we are not in an item, or
2236 (let ((itemp (org-in-item-p))
2238 ;; If cursor isn't is a list or if list is invisible, return nil.
2239 (unless (or (not itemp
)
2242 (outline-invisible-p)))
2245 (org-at-item-timer-p))
2246 ;; Timer list: delegate to `org-timer-item'.
2247 (progn (org-timer-item) t
)
2248 (let* ((struct (save-excursion (goto-char itemp
)
2250 (prevs (org-list-prevs-alist struct
))
2251 ;; If we're in a description list, ask for the new term.
2252 (desc (when (eq (org-list-get-list-type itemp struct prevs
)
2255 (setq struct
(org-list-insert-item pos struct prevs checkbox desc
))
2256 (org-list-write-struct struct
(org-list-parents-alist struct
))
2257 (when checkbox
(org-update-checkbox-count-maybe))
2258 (looking-at org-list-full-item-re
)
2259 (goto-char (if (and (match-beginning 4)
2261 (string-match "[.)]" (match-string 1))))
2264 (if desc
(backward-char 1))
2267 (defun org-list-repair ()
2268 "Fix indentation, bullets and checkboxes in the list at point."
2270 (unless (org-at-item-p) (error "This is not a list"))
2271 (let* ((struct (org-list-struct))
2272 (parents (org-list-parents-alist struct
)))
2273 (org-list-write-struct struct parents
)))
2275 (defun org-cycle-list-bullet (&optional which
)
2276 "Cycle through the different itemize/enumerate bullets.
2277 This cycle the entire list level through the sequence:
2279 `-' -> `+' -> `*' -> `1.' -> `1)'
2281 If WHICH is a valid string, use that as the new bullet. If WHICH
2282 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2283 `previous', cycle backwards."
2285 (unless (org-at-item-p) (error "Not at an item"))
2288 (let* ((struct (org-list-struct))
2289 (parents (org-list-parents-alist struct
))
2290 (prevs (org-list-prevs-alist struct
))
2291 (list-beg (org-list-get-first-item (point) struct prevs
))
2292 (bullet (org-list-get-bullet list-beg struct
))
2293 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs
))
2294 (case-fold-search nil
)
2296 ((string-match "[a-z]\\." bullet
) "a.")
2297 ((string-match "[a-z])" bullet
) "a)")
2298 ((string-match "[A-Z]\\." bullet
) "A.")
2299 ((string-match "[A-Z])" bullet
) "A)")
2300 ((string-match "\\." bullet
) "1.")
2301 ((string-match ")" bullet
) "1)")
2302 (t (org-trim bullet
))))
2303 ;; Compute list of possible bullets, depending on context.
2306 ;; *-bullets are not allowed at column 0.
2307 (unless (looking-at "\\S-") '("*"))
2308 ;; Description items cannot be numbered.
2309 (unless (or (eq org-plain-list-ordered-item-terminator ?\
))
2310 (org-at-item-description-p))
2312 (unless (or (eq org-plain-list-ordered-item-terminator ?.
)
2313 (org-at-item-description-p))
2315 (unless (or (not alpha-p
)
2316 (eq org-plain-list-ordered-item-terminator ?\
))
2317 (org-at-item-description-p))
2319 (unless (or (not alpha-p
)
2320 (eq org-plain-list-ordered-item-terminator ?.
)
2321 (org-at-item-description-p))
2323 (len (length bullet-list
))
2324 (item-index (- len
(length (member current bullet-list
))))
2325 (get-value (lambda (index) (nth (mod index len
) bullet-list
)))
2327 ((member which bullet-list
) which
)
2328 ((numberp which
) (funcall get-value which
))
2329 ((eq 'previous which
) (funcall get-value
(1- item-index
)))
2330 (t (funcall get-value
(1+ item-index
))))))
2331 ;; Use a short variation of `org-list-write-struct' as there's
2332 ;; no need to go through all the steps.
2333 (let ((old-struct (copy-tree struct
)))
2334 (org-list-set-bullet list-beg struct
(org-list-bullet-string new
))
2335 (org-list-struct-fix-bul struct prevs
)
2336 (org-list-struct-fix-ind struct parents
)
2337 (org-list-struct-apply-struct struct old-struct
)))))
2339 (defun org-toggle-checkbox (&optional toggle-presence
)
2340 "Toggle the checkbox in the current line.
2341 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2342 double prefix, set checkbox to [-].
2344 When there is an active region, toggle status or presence of the
2345 first checkbox there, and make every item inside have the same
2346 status or presence, respectively.
2348 If the cursor is in a headline, apply this to all checkbox items
2349 in the text below the heading, taking as reference the first item
2350 in subtree, ignoring drawers."
2357 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2358 "\\|" org-deadline-string
2359 "\\|" org-closed-string
2360 "\\|" org-clock-string
"\\)"
2361 " *[[<]\\([^]>]+\\)[]>]"))
2362 (orderedp (org-entry-get nil
"ORDERED"))
2364 ;; In a region, start at first item in region.
2366 ((org-region-active-p)
2367 (let ((limit (region-end)))
2368 (goto-char (region-beginning))
2369 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2370 (setq lim-up
(point-at-bol))
2371 (error "No item in region"))
2372 (setq lim-down
(copy-marker limit
))))
2374 ;; On an heading, start at first item after drawers and
2375 ;; time-stamps (scheduled, etc.).
2376 (let ((limit (save-excursion (outline-next-heading) (point))))
2378 (while (or (looking-at org-drawer-regexp
)
2379 (looking-at keyword-re
))
2380 (if (looking-at keyword-re
)
2382 (re-search-forward "^[ \t]*:END:" limit nil
)))
2383 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2384 (setq lim-up
(point-at-bol))
2385 (error "No item in subtree"))
2386 (setq lim-down
(copy-marker limit
))))
2387 ;; Just one item: set SINGLEP flag.
2390 (setq lim-up
(point-at-bol)
2391 lim-down
(copy-marker (point-at-eol))))
2392 (t (error "Not at an item or heading, and no active region"))))
2393 ;; Determine the checkbox going to be applied to all items
2398 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2400 ((equal toggle-presence
'(16)) "[-]")
2401 ((equal toggle-presence
'(4))
2402 (unless cbox
"[ ]"))
2403 ((equal "[X]" cbox
) "[ ]")
2405 ;; When an item is found within bounds, grab the full list at
2406 ;; point structure, then: (1) set check-box of all its items
2407 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2408 ;; whole list, (3) move point after the list.
2410 (while (and (< (point) lim-down
)
2411 (org-list-search-forward (org-item-beginning-re)
2413 (let* ((struct (org-list-struct))
2414 (struct-copy (copy-tree struct
))
2415 (parents (org-list-parents-alist struct
))
2416 (prevs (org-list-prevs-alist struct
))
2417 (bottom (copy-marker (org-list-get-bottom-point struct
)))
2418 (items-to-toggle (cl-remove-if
2419 (lambda (e) (or (< e lim-up
) (> e lim-down
)))
2420 (mapcar #'car struct
))))
2421 (mapc (lambda (e) (org-list-set-checkbox
2423 ;; If there is no box at item, leave as-is
2424 ;; unless function was called with C-u prefix.
2425 (let ((cur-box (org-list-get-checkbox e struct
)))
2426 (if (or cur-box
(equal toggle-presence
'(4)))
2430 (setq block-item
(org-list-struct-fix-box
2431 struct parents prevs orderedp
))
2432 ;; Report some problems due to ORDERED status of subtree.
2433 ;; If only one box was being checked, throw an error, else,
2434 ;; only signal problems.
2436 ((and singlep block-item
(> lim-up block-item
))
2438 "Checkbox blocked because of unchecked box at line %d"
2439 (org-current-line block-item
)))
2442 "Checkboxes were removed due to unchecked box at line %d"
2443 (org-current-line block-item
))))
2445 (move-marker bottom nil
)
2446 (org-list-struct-apply-struct struct struct-copy
)))
2447 (move-marker lim-down nil
)))
2448 (org-update-checkbox-count-maybe))
2450 (defun org-reset-checkbox-state-subtree ()
2451 "Reset all checkboxes in an entry subtree."
2453 (if (org-before-first-heading-p)
2454 (error "Not inside a tree")
2457 (org-narrow-to-subtree)
2459 (goto-char (point-min))
2460 (let ((end (point-max)))
2461 (while (< (point) end
)
2462 (when (org-at-item-checkbox-p)
2463 (replace-match "[ ]" t t nil
1))
2464 (beginning-of-line 2)))
2465 (org-update-checkbox-count-maybe 'all
)))))
2467 (defun org-update-checkbox-count (&optional all
)
2468 "Update the checkbox statistics in the current section.
2470 This will find all statistic cookies like [57%] and [6/12] and
2471 update them with the current numbers.
2473 With optional prefix argument ALL, do this for the whole buffer."
2475 (org-with-wide-buffer
2476 (let* ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2477 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\
2478 \\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2480 (or (not org-checkbox-hierarchical-statistics
)
2481 (string-match "\\<recursive\\>"
2482 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2483 (within-inlinetask (and (not all
)
2484 (featurep 'org-inlinetask
)
2485 (org-inlinetask-in-task-p)))
2486 (end (cond (all (point-max))
2488 (save-excursion (outline-next-heading) (point)))
2490 (org-with-limited-levels (outline-next-heading))
2493 (lambda (item structs recursivep
)
2494 ;; Return number of checked boxes and boxes of all types
2495 ;; in all structures in STRUCTS. If RECURSIVEP is
2496 ;; non-nil, also count boxes in sub-lists. If ITEM is
2497 ;; nil, count across the whole structure, else count only
2498 ;; across subtree whose ancestor is ITEM.
2499 (let ((c-on 0) (c-all 0))
2500 (dolist (s structs
(list c-on c-all
))
2501 (let* ((pre (org-list-prevs-alist s
))
2502 (par (org-list-parents-alist s
))
2505 ((and recursivep item
) (org-list-get-subtree item s
))
2506 (recursivep (mapcar #'car s
))
2507 (item (org-list-get-children item s par
))
2508 (t (org-list-get-all-items
2509 (org-list-get-top-point s
) s pre
))))
2510 (cookies (delq nil
(mapcar
2512 (org-list-get-checkbox e s
))
2514 (cl-incf c-all
(length cookies
))
2515 (cl-incf c-on
(cl-count "[X]" cookies
:test
#'equal
)))))))
2518 (cond (all (goto-char (point-min)))
2519 (within-inlinetask (org-back-to-heading t
))
2520 (t (org-with-limited-levels (outline-previous-heading))))
2521 ;; Build an alist for each cookie found. The key is the position
2522 ;; at beginning of cookie and values ending position, format of
2523 ;; cookie, number of checked boxes to report and total number of
2525 (while (re-search-forward cookie-re end t
)
2526 (let ((context (save-excursion (backward-char)
2527 (save-match-data (org-element-context)))))
2528 (when (eq (org-element-type context
) 'statistics-cookie
)
2531 (list (match-beginning 1) (match-end 1) (match-end 2))
2533 (org-element-lineage
2535 '(drawer center-block dynamic-block inlinetask item
2536 quote-block special-block verse-block
)))
2538 (org-element-property :contents-begin container
)
2540 (org-with-limited-levels
2541 (outline-previous-heading))
2543 (or (cdr (assq beg cache
))
2548 (org-element-property :contents-end container
)
2550 (org-with-limited-levels (outline-next-heading))
2553 (while (re-search-forward box-re end t
)
2554 (let ((element (org-element-at-point)))
2555 (when (eq (org-element-type element
) 'item
)
2556 (push (org-element-property :structure element
)
2558 ;; Skip whole list since we have its
2559 ;; structure anyway.
2560 (while (setq element
(org-element-lineage
2561 element
'(plain-list)))
2563 (min (org-element-property :end element
)
2565 ;; Cache count for cookies applying to the same
2566 ;; area. Then return it.
2568 (funcall count-boxes
2569 (and (eq (org-element-type container
)
2571 (org-element-property
2575 (push (cons beg count
) cache
)
2578 ;; Apply alist to buffer.
2579 (dolist (cookie cookies-list
)
2580 (let* ((beg (car cookie
))
2581 (end (nth 1 cookie
))
2582 (percent (nth 2 cookie
))
2583 (checked (nth 3 cookie
))
2584 (total (nth 4 cookie
)))
2587 (if percent
(format "[%d%%]" (floor (* 100.0 checked
)
2589 (format "[%d/%d]" checked total
)))
2590 (delete-region (point) (+ (point) (- end beg
)))
2591 (when org-auto-align-tags
(org-fix-tags-on-the-fly)))))))
2593 (defun org-get-checkbox-statistics-face ()
2594 "Select the face for checkbox statistics.
2595 The face will be `org-done' when all relevant boxes are checked.
2596 Otherwise it will be `org-todo'."
2598 (if (equal (match-string 1) "100%")
2599 'org-checkbox-statistics-done
2600 'org-checkbox-statistics-todo
)
2601 (if (and (> (match-end 2) (match-beginning 2))
2602 (equal (match-string 2) (match-string 3)))
2603 'org-checkbox-statistics-done
2604 'org-checkbox-statistics-todo
)))
2606 (defun org-update-checkbox-count-maybe (&optional all
)
2607 "Update checkbox statistics unless turned off by user.
2608 With an optional argument ALL, update them in the whole buffer."
2609 (when (cdr (assq 'checkbox org-list-automatic-rules
))
2610 (org-update-checkbox-count all
))
2611 (run-hooks 'org-checkbox-statistics-hook
))
2613 (defvar org-last-indent-begin-marker
(make-marker))
2614 (defvar org-last-indent-end-marker
(make-marker))
2615 (defun org-list-indent-item-generic (arg no-subtree struct
)
2616 "Indent a local list item including its children.
2617 When number ARG is a negative, item will be outdented, otherwise
2618 it will be indented.
2620 If a region is active, all items inside will be moved.
2622 If NO-SUBTREE is non-nil, only indent the item itself, not its
2625 STRUCT is the list structure.
2627 Return t if successful."
2629 (let* ((regionp (org-region-active-p))
2630 (rbeg (and regionp
(region-beginning)))
2631 (rend (and regionp
(region-end)))
2632 (top (org-list-get-top-point struct
))
2633 (parents (org-list-parents-alist struct
))
2634 (prevs (org-list-prevs-alist struct
))
2635 ;; Are we going to move the whole list?
2638 (= top
(point-at-bol))
2639 (cdr (assq 'indent org-list-automatic-rules
))
2642 "First item of list cannot move without its subtree")
2644 ;; Determine begin and end points of zone to indent. If moving
2645 ;; more than one item, save them for subsequent moves.
2646 (unless (and (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
2647 (memq this-command
'(org-shiftmetaright org-shiftmetaleft
)))
2650 (set-marker org-last-indent-begin-marker rbeg
)
2651 (set-marker org-last-indent-end-marker rend
))
2652 (set-marker org-last-indent-begin-marker
(point-at-bol))
2653 (set-marker org-last-indent-end-marker
2655 (specialp (org-list-get-bottom-point struct
))
2656 (no-subtree (1+ (point-at-bol)))
2657 (t (org-list-get-item-end (point-at-bol) struct
))))))
2658 (let* ((beg (marker-position org-last-indent-begin-marker
))
2659 (end (marker-position org-last-indent-end-marker
)))
2661 ;; Special case: moving top-item with indent rule.
2663 (let* ((level-skip (org-level-increment))
2664 (offset (if (< arg
0) (- level-skip
) level-skip
))
2665 (top-ind (org-list-get-ind beg struct
))
2666 (old-struct (copy-tree struct
)))
2667 (if (< (+ top-ind offset
) 0)
2668 (error "Cannot outdent beyond margin")
2669 ;; Change bullet if necessary.
2670 (when (and (= (+ top-ind offset
) 0)
2672 (org-list-get-bullet beg struct
)))
2673 (org-list-set-bullet beg struct
2674 (org-list-bullet-string "-")))
2675 ;; Shift every item by OFFSET and fix bullets. Then
2676 ;; apply changes to buffer.
2678 (let ((ind (org-list-get-ind (car e
) struct
)))
2679 (org-list-set-ind (car e
) struct
(+ ind offset
))))
2681 (org-list-struct-fix-bul struct prevs
)
2682 (org-list-struct-apply-struct struct old-struct
))))
2685 ;; If only one item is moved, it mustn't have a child.
2688 (org-list-has-child-p beg struct
))
2689 ;; If a subtree or region is moved, the last item
2690 ;; of the subtree mustn't have a child.
2691 (let ((last-item (caar
2694 (lambda (e) (>= (car e
) end
))
2696 (org-list-has-child-p last-item struct
))))
2697 (error "Cannot outdent an item without its children"))
2702 (org-list-struct-outdent beg end struct parents
)
2703 (org-list-struct-indent beg end struct parents prevs
))))
2704 (org-list-write-struct struct new-parents
))
2705 (org-update-checkbox-count-maybe))))))
2708 (defun org-outdent-item ()
2709 "Outdent a local list item, but not its children.
2710 If a region is active, all items inside will be moved."
2712 (let ((regionp (org-region-active-p)))
2714 ((or (org-at-item-p)
2716 (save-excursion (goto-char (region-beginning))
2718 (let ((struct (if (not regionp
) (org-list-struct)
2719 (save-excursion (goto-char (region-beginning))
2720 (org-list-struct)))))
2721 (org-list-indent-item-generic -
1 t struct
)))
2722 (regionp (error "Region not starting at an item"))
2723 (t (error "Not at an item")))))
2725 (defun org-indent-item ()
2726 "Indent 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-outdent-item-tree ()
2743 "Outdent a local list item including 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 nil struct
)))
2756 (regionp (error "Region not starting at an item"))
2757 (t (error "Not at an item")))))
2759 (defun org-indent-item-tree ()
2760 "Indent 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 (defvar org-tab-ind-state
)
2777 (defvar org-adapt-indentation
)
2778 (defun org-cycle-item-indentation ()
2779 "Cycle levels of indentation of an empty item.
2780 The first run indents the item, if applicable. Subsequent runs
2781 outdent it at meaningful levels in the list. When done, item is
2782 put back at its original position with its original bullet.
2784 Return t at each successful move."
2785 (when (org-at-item-p)
2786 (let* ((org-adapt-indentation nil
)
2787 (struct (org-list-struct))
2788 (ind (org-list-get-ind (point-at-bol) struct
))
2789 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2790 ;; Accept empty items or if cycle has already started.
2791 (when (or (eq last-command
'org-cycle-item-indentation
)
2792 (and (save-excursion
2794 (looking-at org-list-full-item-re
))
2795 (>= (match-end 0) (save-excursion
2796 (goto-char (org-list-get-item-end
2797 (point-at-bol) struct
))
2798 (skip-chars-backward " \r\t\n")
2800 (setq this-command
'org-cycle-item-indentation
)
2801 ;; When in the middle of the cycle, try to outdent first. If
2802 ;; it fails, and point is still at initial position, indent.
2803 ;; Else, re-create it at its original position.
2804 (if (eq last-command
'org-cycle-item-indentation
)
2806 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2807 ((and (= ind
(car org-tab-ind-state
))
2808 (ignore-errors (org-list-indent-item-generic 1 t struct
))))
2809 (t (delete-region (point-at-bol) (point-at-eol))
2810 (indent-to-column (car org-tab-ind-state
))
2811 (insert (cdr org-tab-ind-state
) " ")
2813 (setq this-command
'identity
)))
2814 ;; If a cycle is starting, remember indentation and bullet,
2815 ;; then try to indent. If it fails, try to outdent.
2816 (setq org-tab-ind-state
(cons ind bullet
))
2818 ((ignore-errors (org-list-indent-item-generic 1 t struct
)))
2819 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2820 (t (user-error "Cannot move item"))))
2823 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func
)
2825 The cursor may be at any item of the list that should be sorted.
2826 Sublists are not sorted. Checkboxes, if any, are ignored.
2828 Sorting can be alphabetically, numerically, by date/time as given
2829 by a time stamp, by a property or by priority.
2831 Comparing entries ignores case by default. However, with an
2832 optional argument WITH-CASE, the sorting considers case as well.
2834 The command prompts for the sorting type unless it has been given
2835 to the function through the SORTING-TYPE argument, which needs to
2836 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F ?x ?X). Here is the
2837 detailed meaning of each character:
2839 n Numerically, by converting the beginning of the item to a number.
2840 a Alphabetically. Only the first line of item is checked.
2841 t By date/time, either the first active time stamp in the entry, if
2842 any, or by the first inactive one. In a timer list, sort the timers.
2843 x By \"checked\" status of a check list.
2845 Capital letters will reverse the sort order.
2847 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2848 a function to be called with point at the beginning of the
2849 record. It must return either a string or a number that should
2850 serve as the sorting key for that record. It will then use
2851 COMPARE-FUNC to compare entries.
2853 Sorting is done against the visible part of the headlines, it
2854 ignores hidden links."
2856 (let* ((case-func (if with-case
'identity
'downcase
))
2857 (struct (org-list-struct))
2858 (prevs (org-list-prevs-alist struct
))
2859 (start (org-list-get-list-begin (point-at-bol) struct prevs
))
2860 (end (org-list-get-list-end (point-at-bol) struct prevs
))
2865 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc [x]checked A/N/T/F/X means reversed:")
2866 (read-char-exclusive))))
2869 (and (= (downcase sorting-type
) ?f
)
2870 (intern (completing-read "Sort using function: "
2871 obarray
'fboundp t nil nil
))))))
2872 (message "Sorting items...")
2874 (narrow-to-region start end
)
2875 (goto-char (point-min))
2876 (let* ((dcst (downcase sorting-type
))
2877 (case-fold-search nil
)
2878 (now (current-time))
2880 ((= dcst ?a
) 'string
<)
2881 ((= dcst ?f
) compare-func
)
2883 ((= dcst ?x
) 'string
<)))
2884 (next-record (lambda ()
2885 (skip-chars-forward " \r\t\n")
2886 (or (eobp) (beginning-of-line))))
2887 (end-record (lambda ()
2888 (goto-char (org-list-get-item-end-before-blank
2892 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2896 (org-sort-remove-invisible
2897 (buffer-substring (match-end 0) (point-at-eol)))))
2900 (org-sort-remove-invisible
2902 (match-end 0) (point-at-eol)))))
2905 ;; If it is a timer list, convert timer to seconds
2906 ((org-at-item-timer-p)
2907 (org-timer-hms-to-secs (match-string 1)))
2908 ((or (save-excursion
2909 (re-search-forward org-ts-regexp
(point-at-eol) t
))
2910 (save-excursion (re-search-forward org-ts-regexp-both
2912 (org-time-string-to-seconds (match-string 0)))
2913 (t (float-time now
))))
2914 ((= dcst ?x
) (or (and (stringp (match-string 1))
2919 (let ((value (funcall getkey-func
)))
2921 (funcall case-func value
)
2923 (error "Invalid key function `%s'" getkey-func
)))
2924 (t (error "Invalid sorting type `%c'" sorting-type
)))))))
2925 (sort-subr (/= dcst sorting-type
)
2931 ;; Read and fix list again, as `sort-subr' probably destroyed
2934 (run-hooks 'org-after-sorting-entries-or-items-hook
)
2935 (message "Sorting items...done")))))
2937 (defun org-toggle-item (arg)
2938 "Convert headings or normal lines to items, items to normal lines.
2939 If there is no active region, only the current line is considered.
2941 If the first non blank line in the region is a headline, convert
2942 all headlines to items, shifting text accordingly.
2944 If it is an item, convert all items to normal lines.
2946 If it is normal text, change region into a list of items.
2947 With a prefix argument ARG, change the region in a single item."
2951 ;; Shift text in current section to IND, from point to END.
2952 ;; The function leaves point to END line.
2953 (let ((min-i 1000) (end (copy-marker end
)))
2954 ;; First determine the minimum indentation (MIN-I) of
2958 (while (< (point) end
)
2959 (let ((i (org-get-indentation)))
2961 ;; Skip blank lines and inline tasks.
2962 ((looking-at "^[ \t]*$"))
2963 ((looking-at org-outline-regexp-bol
))
2964 ;; We can't find less than 0 indentation.
2965 ((zerop i
) (throw 'exit
(setq min-i
0)))
2966 ((< i min-i
) (setq min-i i
))))
2968 ;; Then indent each line so that a line indented to
2969 ;; MIN-I becomes indented to IND. Ignore blank lines
2970 ;; and inline tasks in the process.
2971 (let ((delta (- ind min-i
)))
2972 (while (< (point) end
)
2973 (unless (or (looking-at "^[ \t]*$")
2974 (looking-at org-outline-regexp-bol
))
2975 (indent-line-to (+ (org-get-indentation) delta
)))
2979 ;; Return beginning of first non-blank line, starting from
2983 (skip-chars-forward " \r\t\n")
2986 ;; Determine boundaries of changes.
2987 (if (org-region-active-p)
2988 (setq beg
(funcall skip-blanks
(region-beginning))
2989 end
(copy-marker (region-end)))
2990 (setq beg
(funcall skip-blanks
(point-at-bol))
2991 end
(copy-marker (point-at-eol))))
2992 ;; Depending on the starting line, choose an action on the text
2993 ;; between BEG and END.
2994 (org-with-limited-levels
2998 ;; Case 1. Start at an item: de-itemize. Note that it only
2999 ;; happens when a region is active: `org-ctrl-c-minus'
3000 ;; would call `org-cycle-list-bullet' otherwise.
3002 (while (< (point) end
)
3003 (when (org-at-item-p)
3004 (skip-chars-forward " \t")
3005 (delete-region (point) (match-end 0)))
3007 ;; Case 2. Start at an heading: convert to items.
3009 (let* ((bul (org-list-bullet-string "-"))
3010 (bul-len (length bul
))
3011 ;; Indentation of the first heading. It should be
3012 ;; relative to the indentation of its parent, if any.
3013 (start-ind (save-excursion
3015 ((not org-adapt-indentation
) 0)
3016 ((not (outline-previous-heading)) 0)
3017 (t (length (match-string 0))))))
3018 ;; Level of first heading. Further headings will be
3019 ;; compared to it to determine hierarchy in the list.
3020 (ref-level (org-reduced-level (org-outline-level))))
3021 (while (< (point) end
)
3022 (let* ((level (org-reduced-level (org-outline-level)))
3023 (delta (max 0 (- level ref-level
)))
3024 (todo-state (org-get-todo-state)))
3025 ;; If current headline is less indented than the first
3026 ;; one, set it as reference, in order to preserve
3028 (when (< level ref-level
) (setq ref-level level
))
3029 ;; Remove stars and TODO keyword.
3030 (looking-at org-todo-line-regexp
)
3031 (delete-region (point) (or (match-beginning 3)
3032 (line-end-position)))
3034 (indent-line-to (+ start-ind
(* delta bul-len
)))
3035 ;; Turn TODO keyword into a check box.
3037 (let* ((struct (org-list-struct))
3038 (old (copy-tree struct
)))
3039 (org-list-set-checkbox
3040 (line-beginning-position)
3042 (if (member todo-state org-done-keywords
)
3045 (org-list-write-struct struct
3046 (org-list-parents-alist struct
)
3048 ;; Ensure all text down to END (or SECTION-END) belongs
3049 ;; to the newly created item.
3050 (let ((section-end (save-excursion
3051 (or (outline-next-heading) (point)))))
3054 (+ start-ind
(* (1+ delta
) bul-len
))
3055 (min end section-end
)))))))
3056 ;; Case 3. Normal line with ARG: make the first line of region
3057 ;; an item, and shift indentation of others lines to
3058 ;; set them as item's body.
3059 (arg (let* ((bul (org-list-bullet-string "-"))
3060 (bul-len (length bul
))
3061 (ref-ind (org-get-indentation)))
3062 (skip-chars-forward " \t")
3065 (while (< (point) end
)
3066 ;; Ensure that lines less indented than first one
3067 ;; still get included in item body.
3070 (min end
(save-excursion (or (outline-next-heading)
3073 ;; Case 4. Normal line without ARG: turn each non-item line
3076 (while (< (point) end
)
3077 (unless (or (org-at-heading-p) (org-at-item-p))
3078 (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
3080 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
3081 (forward-line))))))))
3084 ;;; Send and receive lists
3086 (defun org-list-to-lisp (&optional delete
)
3087 "Parse the list at point and maybe DELETE it.
3089 Return a list whose car is a symbol of list type, among
3090 `ordered', `unordered' and `descriptive'. Then, each item is
3091 a list of strings and other sub-lists.
3093 For example, the following list:
3098 more text in first item
3107 \(\"[X] sub-item two\"))
3108 \"more text in first item\")
3109 \(\"[@3] last item\"))
3111 Point is left at list's end."
3112 (letrec ((struct (org-list-struct))
3113 (prevs (org-list-prevs-alist struct
))
3114 (parents (org-list-parents-alist struct
))
3115 (top (org-list-get-top-point struct
))
3116 (bottom (org-list-get-bottom-point struct
))
3119 ;; Remove indentation and final newline from TEXT.
3120 (org-remove-indentation
3121 (if (string-match-p "\n\\'" text
)
3122 (substring text
0 -
1)
3126 ;; Return a list whose car is list type and cdr a list
3128 (cons (org-list-get-list-type (car e
) struct prevs
)
3129 (mapcar parse-item e
))))
3132 ;; Return a list containing counter of item, if any,
3133 ;; text and any sublist inside it.
3134 (let* ((end (org-list-get-item-end e struct
))
3135 (children (org-list-get-children e struct parents
))
3139 (looking-at "[ \t]*\\S-+[ \t]*")
3144 (make-string (string-width (match-string 0)) ?\s
)
3145 (buffer-substring-no-properties
3146 (match-end 0) (or (car children
) end
))))))))
3148 (let* ((child (car children
))
3149 (sub (org-list-get-all-items child struct prevs
))
3150 (last-in-sub (car (last sub
))))
3151 (push (funcall parse-sublist sub
) body
)
3152 ;; Remove whole sub-list from children.
3153 (setq children
(cdr (memq last-in-sub children
)))
3154 ;; There is a chunk of text belonging to the item
3155 ;; if last child doesn't end where next child
3156 ;; starts or where item ends.
3157 (let ((sub-end (org-list-get-item-end last-in-sub struct
))
3158 (next (or (car children
) end
)))
3159 (when (/= sub-end next
)
3162 (buffer-substring-no-properties sub-end next
))
3165 ;; Store output, take care of cursor position and deletion of
3166 ;; list, then return output.
3167 (prog1 (funcall parse-sublist
(org-list-get-all-items top struct prevs
))
3170 (delete-region top bottom
)
3171 (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re
))
3172 (replace-match ""))))))
3174 (defun org-list-make-subtree ()
3175 "Convert the plain list at point into a subtree."
3177 (if (not (ignore-errors (goto-char (org-in-item-p))))
3178 (error "Not in a list")
3179 (let ((list (save-excursion (org-list-to-lisp t
))))
3180 (insert (org-list-to-subtree list
)))))
3182 (defun org-list-insert-radio-list ()
3183 "Insert a radio list template appropriate for this major mode."
3185 (let* ((e (assq major-mode org-list-radio-list-templates
))
3188 (unless e
(error "No radio list setup defined for %s" major-mode
))
3189 (setq name
(read-string "List name: "))
3190 (while (string-match "%n" txt
)
3191 (setq txt
(replace-match name t t txt
)))
3192 (or (bolp) (insert "\n"))
3197 (defun org-list-send-list (&optional maybe
)
3198 "Send a transformed version of this list to the receiver position.
3199 With argument MAYBE, fail quietly if no transformation is defined
3203 (unless (org-at-item-p) (error "Not at a list item"))
3205 (let ((case-fold-search t
))
3206 (re-search-backward "^[ \t]*#\\+ORGLST:" nil t
)
3208 "[ \t]*#\\+ORGLST:[ \t]+SEND[ \t]+\\(\\S-+\\)[ \t]+\\([^ \t\n]+\\)")
3209 (if maybe
(throw 'exit nil
)
3210 (error "Don't know how to transform this list")))))
3211 (let* ((name (regexp-quote (match-string 1)))
3212 (transform (intern (match-string 2)))
3216 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t
)
3217 (match-beginning 0)))
3220 (re-search-backward "#\\+ORGLST" nil t
)
3221 (re-search-forward (org-item-beginning-re) bottom-point t
)
3222 (match-beginning 0)))
3223 (plain-list (save-excursion
3224 (goto-char top-point
)
3225 (org-list-to-lisp))))
3226 (unless (fboundp transform
)
3227 (error "No such transformation function %s" transform
))
3228 (let ((txt (funcall transform plain-list
)))
3229 ;; Find the insertion(s) place(s).
3231 (goto-char (point-min))
3232 (let ((receiver-count 0)
3233 (begin-re (format "BEGIN +RECEIVE +ORGLST +%s\\([ \t]\\|$\\)"
3235 (end-re (format "END +RECEIVE +ORGLST +%s\\([ \t]\\|$\\)"
3237 (while (re-search-forward begin-re nil t
)
3238 (cl-incf receiver-count
)
3239 (let ((beg (line-beginning-position 2)))
3240 (unless (re-search-forward end-re nil t
)
3241 (user-error "Cannot find end of receiver location at %d" beg
))
3243 (delete-region beg
(point))
3246 ((> receiver-count
1)
3247 (message "List converted and installed at receiver locations"))
3248 ((= receiver-count
1)
3249 (message "List converted and installed at receiver location"))
3250 (t (user-error "No valid receiver location found")))))))))
3252 (defun org-list-to-generic (list params
)
3253 "Convert a LIST parsed through `org-list-to-lisp' to a custom format.
3255 LIST is a list as returned by `org-list-to-lisp', which see.
3256 PARAMS is a property list of parameters used to tweak the output
3259 Valid parameters are:
3263 Export back-end used as a basis to transcode elements of the
3264 list, when no specific parameter applies to it. It is also
3265 used to translate its contents. You can prevent this by
3266 setting :raw property to a non-nil value.
3270 When non-nil, only export the contents of the top most plain
3271 list, effectively ignoring its opening and closing lines.
3275 Strings to start and end an unordered list. They can also be
3276 set to a function returning a string or nil, which will be
3277 called with the depth of the list, counting from 1.
3281 Strings to start and end an ordered list. They can also be set
3282 to a function returning a string or nil, which will be called
3283 with the depth of the list, counting from 1.
3287 Strings to start and end a descriptive list. They can also be
3288 set to a function returning a string or nil, which will be
3289 called with the depth of the list, counting from 1.
3291 :dtstart, :dtend, :ddstart, :ddend
3293 Strings to start and end a descriptive term.
3297 Strings to start or end a list item, and to start a list item
3298 with a counter. They can also be set to a function returning
3299 a string or nil, which will be called with the depth of the
3300 item, counting from 1.
3304 Strings to start a list item with a counter. It can also be
3305 set to a function returning a string or nil, which will be
3306 called with two arguments: the depth of the item, counting from
3307 1, and the counter. Its value, when non-nil, has precedence
3312 String used to separate items. It can also be set to
3313 a function returning a string or nil, which will be called with
3314 the depth of the items, counting from 1. It always start on
3317 :cbon, :cboff, :cbtrans
3319 String to insert, respectively, an un-checked check-box,
3320 a checked check-box and a check-box in transitional state."
3322 (let* ((backend (plist-get params
:backend
))
3324 (org-export-create-backend
3325 :parent
(or backend
'org
)
3327 `((plain-list .
,(org-list--to-generic-plain-list params
))
3328 (item .
,(org-list--to-generic-item params
))
3329 (macro .
(lambda (m c i
) (org-element-macro-interpreter m nil
))))))
3331 ;; Write LIST back into Org syntax and parse it.
3333 (let ((org-inhibit-startup t
)) (org-mode))
3334 (letrec ((insert-list
3337 (funcall insert-item i
(car l
)))))
3340 (let ((start (point)))
3341 (insert (if (eq type
'ordered
) "1. " "- "))
3343 (if (consp e
) (funcall insert-list e
)
3348 (let ((ind (if (eq type
'ordered
) 3 2)))
3349 (while (> (point) start
)
3350 (unless (looking-at-p "[ \t]*$")
3352 (forward-line -
1))))))))
3353 (funcall insert-list list
))
3355 (org-element-map (org-element-parse-buffer) 'plain-list
3357 (setf info
(org-export-get-environment backend nil params
)))
3358 (when (and backend
(symbolp backend
) (not (org-export-get-backend backend
)))
3359 (user-error "Unknown :backend value"))
3360 (unless backend
(require 'ox-org
))
3361 ;; When`:raw' property has a non-nil value, turn all objects back
3363 (when (and backend
(plist-get params
:raw
))
3364 (org-element-map data org-element-all-objects
3366 (org-element-set-element
3367 object
(org-element-interpret-data object
)))))
3368 ;; We use a low-level mechanism to export DATA so as to skip all
3369 ;; usual pre-processing and post-processing, i.e., hooks, filters,
3370 ;; Babel code evaluation, include keywords and macro expansion,
3372 (let ((output (org-export-data-with-backend data custom-backend info
)))
3373 ;; Remove final newline.
3374 (if (org-string-nw-p output
) (substring-no-properties output
0 -
1) ""))))
3376 (defun org-list--depth (element)
3377 "Return the level of ELEMENT within current plain list.
3378 ELEMENT is either an item or a plain list."
3379 (cl-count-if (lambda (ancestor) (eq (org-element-type ancestor
) 'plain-list
))
3380 (org-element-lineage element nil t
)))
3382 (defun org-list--trailing-newlines (string)
3383 "Return the number of trailing newlines in STRING."
3386 (skip-chars-backward " \t\n")
3387 (count-lines (line-beginning-position 2) (point-max))))
3389 (defun org-list--generic-eval (value &rest args
)
3390 "Evaluate VALUE according to its type.
3391 VALUE is either nil, a string or a function. In the latter case,
3392 it is called with arguments ARGS."
3393 (cond ((null value
) nil
)
3394 ((stringp value
) value
)
3395 ((functionp value
) (apply value args
))
3396 (t (error "Wrong value: %s" value
))))
3398 (defun org-list--to-generic-plain-list (params)
3399 "Return a transcoder for `plain-list' elements.
3400 PARAMS is a plist used to tweak the behavior of the transcoder."
3401 (let ((ustart (plist-get params
:ustart
))
3402 (uend (plist-get params
:uend
))
3403 (ostart (plist-get params
:ostart
))
3404 (oend (plist-get params
:oend
))
3405 (dstart (plist-get params
:dstart
))
3406 (dend (plist-get params
:dend
))
3407 (splice (plist-get params
:splice
))
3408 (backend (plist-get params
:backend
)))
3409 (lambda (plain-list contents info
)
3410 (let* ((type (org-element-property :type plain-list
))
3411 (depth (org-list--depth plain-list
))
3412 (start (and (not splice
)
3413 (org-list--generic-eval
3419 (end (and (not splice
)
3420 (org-list--generic-eval
3426 ;; Make sure trailing newlines in END appear in the output by
3427 ;; setting `:post-blank' property to their number.
3429 (org-element-put-property
3430 plain-list
:post-blank
(org-list--trailing-newlines end
)))
3432 (concat (and start
(concat start
"\n"))
3433 (if (or start end splice
(not backend
))
3435 (org-export-with-backend backend plain-list contents info
))
3438 (defun org-list--to-generic-item (params)
3439 "Return a transcoder for `item' elements.
3440 PARAMS is a plist used to tweak the behavior of the transcoder."
3441 (let ((backend (plist-get params
:backend
))
3442 (istart (plist-get params
:istart
))
3443 (iend (plist-get params
:iend
))
3444 (isep (plist-get params
:isep
))
3445 (icount (plist-get params
:icount
))
3446 (cboff (plist-get params
:cboff
))
3447 (cbon (plist-get params
:cbon
))
3448 (cbtrans (plist-get params
:cbtrans
))
3449 (dtstart (plist-get params
:dtstart
))
3450 (dtend (plist-get params
:dtend
))
3451 (ddstart (plist-get params
:ddstart
))
3452 (ddend (plist-get params
:ddend
)))
3453 (lambda (item contents info
)
3455 (org-element-property :type
(org-element-property :parent item
)))
3456 (tag (org-element-property :tag item
))
3457 (depth (org-list--depth item
))
3458 (separator (and (org-export-get-next-element item info
)
3459 (org-list--generic-eval isep depth
)))
3460 (closing (pcase (org-list--generic-eval iend depth
)
3461 ((or `nil
`"") "\n")
3462 ((and (guard separator
) s
)
3463 (if (equal (substring s -
1) "\n") s
(concat s
"\n")))
3465 ;; When a closing line or a separator is provided, make sure
3466 ;; its trailing newlines are taken into account when building
3467 ;; output. This is done by setting `:post-blank' property to
3468 ;; the number of such lines in the last line to be added.
3469 (let ((last-string (or separator closing
)))
3471 (org-element-put-property
3474 (max (1- (org-list--trailing-newlines last-string
)) 0))))
3477 (let ((c (org-element-property :counter item
)))
3478 (if c
(org-list--generic-eval icount depth c
)
3479 (org-list--generic-eval istart depth
)))
3481 (if (or istart iend icount cbon cboff cbtrans
(not backend
)
3482 (and (eq type
'descriptive
)
3483 (or dtstart dtend ddstart ddend
)))
3485 (pcase (org-element-property :checkbox item
)
3492 (org-export-data-with-backend
3494 (org-element-interpret-data tag
))
3497 (if (= (length contents
) 0) "" (substring contents
0 -
1))
3499 (org-export-with-backend backend item contents info
))))
3500 ;; Remove final newline.
3501 (if (equal body
"") ""
3502 (substring (org-element-normalize-string body
) 0 -
1)))
3506 (defun org-list-to-latex (list &optional params
)
3507 "Convert LIST into a LaTeX list.
3508 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3509 Return converted list as a string."
3511 (org-list-to-generic list
(org-combine-plists '(:backend latex
) params
)))
3513 (defun org-list-to-html (list &optional params
)
3514 "Convert LIST into a HTML list.
3515 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3516 Return converted list as a string."
3518 (org-list-to-generic list
(org-combine-plists '(:backend html
) params
)))
3520 (defun org-list-to-texinfo (list &optional params
)
3521 "Convert LIST into a Texinfo list.
3522 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3523 Return converted list as a string."
3524 (require 'ox-texinfo
)
3525 (org-list-to-generic list
(org-combine-plists '(:backend texinfo
) params
)))
3527 (defun org-list-to-subtree (list &optional params
)
3528 "Convert LIST into an Org subtree.
3529 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3530 with overruling parameters for `org-list-to-generic'."
3531 (let* ((blank (pcase (cdr (assq 'heading org-blank-before-new-entry
))
3533 (`auto
(save-excursion
3534 (org-with-limited-levels (outline-previous-heading))
3535 (org-previous-line-empty-p)))))
3536 (level (org-reduced-level (or (org-current-level) 0)))
3539 ;; Return the string for the heading, depending on DEPTH
3540 ;; of current sub-list.
3541 (let ((oddeven-level (+ level depth
)))
3542 (concat (make-string (if org-odd-levels-only
3543 (1- (* 2 oddeven-level
))
3547 (org-list-to-generic
3553 :dtstart
" " :dtend
" "
3554 :isep
(if blank
"\n\n" "\n")
3555 :cbon
"DONE " :cboff
"TODO " :cbtrans
"TODO ")
3560 ;;; org-list.el ends here