* test/lisp/net/tramp-tests.el (tramp-test17-dired-with-wildcards): New test.
[emacs.git] / lisp / org / org-list.el
bloba3e26256f9bb8e47e95d0bc07488ed38ef6d53d4
1 ;;; org-list.el --- Plain lists for Org -*- lexical-binding: t; -*-
2 ;;
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
4 ;;
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
9 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
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
47 ;; template:
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
60 ;; needed accessors.
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
75 ;; time.
77 ;;; Code:
79 (require 'cl-lib)
80 (require 'org-macs)
81 (require 'org-compat)
83 (defvar org-M-RET-may-split-line)
84 (defvar org-auto-align-tags)
85 (defvar org-blank-before-new-entry)
86 (defvar org-clock-string)
87 (defvar org-closed-string)
88 (defvar org-deadline-string)
89 (defvar org-description-max-indent)
90 (defvar org-done-keywords)
91 (defvar org-drawer-regexp)
92 (defvar org-element-all-objects)
93 (defvar org-inhibit-startup)
94 (defvar org-odd-levels-only)
95 (defvar org-outline-regexp-bol)
96 (defvar org-scheduled-string)
97 (defvar org-todo-line-regexp)
98 (defvar org-ts-regexp)
99 (defvar org-ts-regexp-both)
101 (declare-function org-at-heading-p "org" (&optional invisible-ok))
102 (declare-function org-back-to-heading "org" (&optional invisible-ok))
103 (declare-function org-before-first-heading-p "org" ())
104 (declare-function org-combine-plists "org" (&rest plists))
105 (declare-function org-current-level "org" ())
106 (declare-function org-element-at-point "org-element" ())
107 (declare-function org-element-context "org-element" (&optional element))
108 (declare-function org-element-interpret-data "org-element" (data))
109 (declare-function
110 org-element-lineage "org-element" (blob &optional types with-self))
111 (declare-function org-element-macro-interpreter "org-element" (macro ##))
112 (declare-function
113 org-element-map "org-element"
114 (data types fun &optional info first-match no-recursion with-affiliated))
115 (declare-function org-element-normalize-string "org-element" (s))
116 (declare-function org-element-parse-buffer "org-element"
117 (&optional granularity visible-only))
118 (declare-function org-element-property "org-element" (property element))
119 (declare-function org-element-put-property "org-element"
120 (element property value))
121 (declare-function org-element-set-element "org-element" (old new))
122 (declare-function org-element-type "org-element" (element))
123 (declare-function org-element-update-syntax "org-element" ())
124 (declare-function org-entry-get "org"
125 (pom property &optional inherit literal-nil))
126 (declare-function org-export-create-backend "ox" (&rest rest) t)
127 (declare-function org-export-data-with-backend "ox" (data backend info))
128 (declare-function org-export-get-backend "ox" (name))
129 (declare-function org-export-get-environment "ox"
130 (&optional backend subtreep ext-plist))
131 (declare-function org-export-get-next-element "ox"
132 (blob info &optional n))
133 (declare-function org-export-with-backend "ox"
134 (backend data &optional contents info))
135 (declare-function org-fix-tags-on-the-fly "org" ())
136 (declare-function org-get-indentation "org" (&optional line))
137 (declare-function org-get-todo-state "org" ())
138 (declare-function org-in-block-p "org" (names))
139 (declare-function org-in-regexp "org" (re &optional nlines visually))
140 (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
141 (declare-function org-inlinetask-goto-end "org-inlinetask" ())
142 (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
143 (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
144 (declare-function org-level-increment "org" ())
145 (declare-function org-narrow-to-subtree "org" ())
146 (declare-function org-outline-level "org" ())
147 (declare-function org-previous-line-empty-p "org" ())
148 (declare-function org-reduced-level "org" (L))
149 (declare-function org-remove-indentation "org" (code &optional n))
150 (declare-function org-show-subtree "org" ())
151 (declare-function org-sort-remove-invisible "org" (S))
152 (declare-function org-time-string-to-seconds "org" (s))
153 (declare-function org-timer-hms-to-secs "org-timer" (hms))
154 (declare-function org-timer-item "org-timer" (&optional arg))
155 (declare-function org-trim "org" (s &optional keep-lead))
156 (declare-function org-uniquify "org" (list))
157 (declare-function org-invisible-p "org" (&optional pos))
158 (declare-function outline-flag-region "outline" (from to flag))
159 (declare-function outline-next-heading "outline" ())
160 (declare-function outline-previous-heading "outline" ())
164 ;;; Configuration variables
166 (defgroup org-plain-lists nil
167 "Options concerning plain lists in Org mode."
168 :tag "Org Plain lists"
169 :group 'org-structure)
171 (defcustom org-cycle-include-plain-lists t
172 "When t, make TAB cycle visibility on plain list items.
173 Cycling plain lists works only when the cursor is on a plain list
174 item. When the cursor is on an outline heading, plain lists are
175 treated as text. This is the most stable way of handling this,
176 which is why it is the default.
178 When this is the symbol `integrate', then integrate plain list
179 items when cycling, as if they were children of outline headings.
181 This setting can lead to strange effects when switching visibility
182 to `children', because the first \"child\" in a subtree decides
183 what children should be listed. If that first \"child\" is a
184 plain list item with an implied large level number, all true
185 children and grand children of the outline heading will be
186 exposed in a children' view."
187 :group 'org-plain-lists
188 :group 'org-cycle
189 :type '(choice
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.
198 For example,
200 (setq org-list-demote-modify-bullet
201 \\='((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
203 will make
205 + Movies
206 + Silence of the Lambs
207 + My Cousin Vinny
208 + Books
209 + The Hunt for Red October
210 + The Road to Omaha
212 into
214 + Movies
215 - Silence of the Lambs
216 - My Cousin Vinny
217 + Books
218 - The Hunt for Red October
219 - The Road to Omaha"
220 :group 'org-plain-lists
221 :type '(repeat
222 (cons
223 (choice :tag "If the current bullet is "
224 (const "-")
225 (const "+")
226 (const "*")
227 (const "1.")
228 (const "1)"))
229 (choice :tag "demotion will change it to"
230 (const "-")
231 (const "+")
232 (const "*")
233 (const "1.")
234 (const "1)")))))
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
265 :version "24.1"
266 :type 'boolean
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
278 :type '(choice
279 (const :tag "never" nil)
280 (regexp)))
282 (defcustom org-list-automatic-rules '((checkbox . t)
283 (indent . t))
284 "Non-nil means apply set of rules when acting on lists.
285 \\<org-mode-map>
286 By default, automatic actions are taken when using
287 `\\[org-meta-return]',
288 `\\[org-metaright]',
289 `\\[org-metaleft]',
290 `\\[org-shiftmetaright]',
291 `\\[org-shiftmetaleft]',
292 `\\[org-ctrl-c-minus]',
293 `\\[org-toggle-checkbox]',
294 `\\[org-insert-todo-heading]'.
296 You can disable individually these rules by setting them to nil.
297 Valid rules are:
299 checkbox when non-nil, checkbox statistics is updated each time
300 you either insert a new checkbox or toggle a checkbox.
301 indent when non-nil, indenting or outdenting list top-item
302 with its subtree will move the whole list and
303 outdenting a list whose bullet is * to column 0 will
304 change that bullet to \"-\"."
305 :group 'org-plain-lists
306 :version "24.1"
307 :type '(alist :tag "Sets of rules"
308 :key-type
309 (choice
310 (const :tag "Checkbox" checkbox)
311 (const :tag "Indent" indent))
312 :value-type
313 (boolean :tag "Activate" :value t)))
315 (defcustom org-list-use-circular-motion nil
316 "Non-nil means commands implying motion in lists should be cyclic.
317 \\<org-mode-map>
318 In that case, the item following the last item is the first one,
319 and the item preceding the first item is the last one.
321 This affects the behavior of
322 `\\[org-move-item-up]',
323 `\\[org-move-item-down]',
324 `\\[org-next-item]',
325 `\\[org-previous-item]'."
326 :group 'org-plain-lists
327 :version "24.1"
328 :type 'boolean)
330 (defvar org-checkbox-statistics-hook nil
331 "Hook that is run whenever Org thinks checkbox statistics should be updated.
332 This hook runs even if checkbox rule in
333 `org-list-automatic-rules' does not apply, so it can be used to
334 implement alternative ways of collecting statistics
335 information.")
337 (defcustom org-checkbox-hierarchical-statistics t
338 "Non-nil means checkbox statistics counts only the state of direct children.
339 When nil, all boxes below the cookie are counted.
340 This can be set to nil on a per-node basis using a COOKIE_DATA property
341 with the word \"recursive\" in the value."
342 :group 'org-plain-lists
343 :type 'boolean)
345 (defcustom org-list-description-max-indent 20
346 "Maximum indentation for the second line of a description list.
347 When the indentation would be larger than this, it will become
348 5 characters instead."
349 :group 'org-plain-lists
350 :type 'integer)
352 (defcustom org-list-indent-offset 0
353 "Additional indentation for sub-items in a list.
354 By setting this to a small number, usually 1 or 2, one can more
355 clearly distinguish sub-items in a list."
356 :group 'org-plain-lists
357 :version "24.1"
358 :type 'integer)
360 (defcustom org-list-radio-list-templates
361 '((latex-mode "% BEGIN RECEIVE ORGLST %n
362 % END RECEIVE ORGLST %n
363 \\begin{comment}
364 #+ORGLST: SEND %n org-list-to-latex
366 \\end{comment}\n")
367 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
368 @c END RECEIVE ORGLST %n
369 @ignore
370 #+ORGLST: SEND %n org-list-to-texinfo
372 @end ignore\n")
373 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
374 <!-- END RECEIVE ORGLST %n -->
375 <!--
376 #+ORGLST: SEND %n org-list-to-html
378 -->\n"))
379 "Templates for radio lists in different major modes.
380 All occurrences of %n in a template will be replaced with the name of the
381 list, obtained by prompting the user."
382 :group 'org-plain-lists
383 :type '(repeat
384 (list (symbol :tag "Major mode")
385 (string :tag "Format"))))
387 (defvar org-list-forbidden-blocks '("example" "verse" "src" "export")
388 "Names of blocks where lists are not allowed.
389 Names must be in lower case.")
391 (defvar org-list-export-context '(block inlinetask)
392 "Context types where lists will be interpreted during export.
394 Valid types are `drawer', `inlinetask' and `block'. More
395 specifically, type `block' is determined by the variable
396 `org-list-forbidden-blocks'.")
400 ;;; Predicates and regexps
402 (defconst org-list-end-re "^[ \t]*\n[ \t]*\n"
403 "Regex matching the end of a plain list.")
405 (defconst org-list-full-item-re
406 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
407 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
408 "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
409 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
410 "Matches a list item and puts everything into groups:
411 group 1: bullet
412 group 2: counter
413 group 3: checkbox
414 group 4: description tag")
416 (defun org-item-re ()
417 "Return the correct regular expression for plain lists."
418 (let ((term (cond
419 ((eq org-plain-list-ordered-item-terminator t) "[.)]")
420 ((= org-plain-list-ordered-item-terminator ?\)) ")")
421 ((= org-plain-list-ordered-item-terminator ?.) "\\.")
422 (t "[.)]")))
423 (alpha (if org-list-allow-alphabetical "\\|[A-Za-z]" "")))
424 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha "\\)" term
425 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
427 (defsubst org-item-beginning-re ()
428 "Regexp matching the beginning of a plain list item."
429 (concat "^" (org-item-re)))
431 (defun org-list-at-regexp-after-bullet-p (regexp)
432 "Is point at a list item with REGEXP after bullet?"
433 (and (org-at-item-p)
434 (save-excursion
435 (goto-char (match-end 0))
436 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
437 (if org-list-allow-alphabetical
438 "\\([0-9]+\\|[A-Za-z]\\)"
439 "[0-9]+")
440 "\\][ \t]*\\)")))
441 ;; Ignore counter if any
442 (when (looking-at counter-re) (goto-char (match-end 0))))
443 (looking-at regexp))))
445 (defun org-list-in-valid-context-p ()
446 "Is point in a context where lists are allowed?"
447 (not (org-in-block-p org-list-forbidden-blocks)))
449 (defun org-in-item-p ()
450 "Return item beginning position when in a plain list, nil otherwise."
451 (save-excursion
452 (beginning-of-line)
453 (let* ((case-fold-search t)
454 (context (org-list-context))
455 (lim-up (car context))
456 (inlinetask-re (and (featurep 'org-inlinetask)
457 (org-inlinetask-outline-regexp)))
458 (item-re (org-item-re))
459 ;; Indentation isn't meaningful when point starts at an empty
460 ;; line or an inline task.
461 (ind-ref (if (or (looking-at "^[ \t]*$")
462 (and inlinetask-re (looking-at inlinetask-re)))
463 10000
464 (org-get-indentation))))
465 (cond
466 ((eq (nth 2 context) 'invalid) nil)
467 ((looking-at item-re) (point))
469 ;; Detect if cursor in amidst `org-list-end-re'. First, count
470 ;; number HL of hard lines it takes, then call `org-in-regexp'
471 ;; to compute its boundaries END-BOUNDS. When point is
472 ;; in-between, move cursor before regexp beginning.
473 (let ((hl 0) (i -1) end-bounds)
474 (when (and (progn
475 (while (setq i (string-match
476 "[\r\n]" org-list-end-re (1+ i)))
477 (setq hl (1+ hl)))
478 (setq end-bounds (org-in-regexp org-list-end-re hl)))
479 (>= (point) (car end-bounds))
480 (< (point) (cdr end-bounds)))
481 (goto-char (car end-bounds))
482 (forward-line -1)))
483 ;; Look for an item, less indented that reference line.
484 (catch 'exit
485 (while t
486 (let ((ind (org-get-indentation)))
487 (cond
488 ;; This is exactly what we want.
489 ((and (looking-at item-re) (< ind ind-ref))
490 (throw 'exit (point)))
491 ;; At upper bound of search or looking at the end of a
492 ;; previous list: search is over.
493 ((<= (point) lim-up) (throw 'exit nil))
494 ((looking-at org-list-end-re) (throw 'exit nil))
495 ;; Skip blocks, drawers, inline-tasks, blank lines
496 ((and (looking-at "^[ \t]*#\\+end_")
497 (re-search-backward "^[ \t]*#\\+begin_" lim-up t)))
498 ((and (looking-at "^[ \t]*:END:")
499 (re-search-backward org-drawer-regexp lim-up t))
500 (beginning-of-line))
501 ((and inlinetask-re (looking-at inlinetask-re))
502 (org-inlinetask-goto-beginning)
503 (forward-line -1))
504 ((looking-at "^[ \t]*$") (forward-line -1))
505 ;; Text at column 0 cannot belong to a list: stop.
506 ((zerop ind) (throw 'exit nil))
507 ;; Normal text less indented than reference line, take
508 ;; it as new reference.
509 ((< ind ind-ref)
510 (setq ind-ref ind)
511 (forward-line -1))
512 (t (forward-line -1)))))))))))
514 (defun org-at-item-p ()
515 "Is point in a line starting a hand-formatted item?"
516 (save-excursion
517 (beginning-of-line)
518 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
520 (defun org-at-item-bullet-p ()
521 "Is point at the bullet of a plain list item?"
522 (and (org-at-item-p)
523 (not (member (char-after) '(?\ ?\t)))
524 (< (point) (match-end 0))))
526 (defun org-at-item-timer-p ()
527 "Is point at a line starting a plain list item with a timer?"
528 (org-list-at-regexp-after-bullet-p
529 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
531 (defun org-at-item-description-p ()
532 "Is point at a description list item?"
533 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::\\([ \t]+\\|$\\)"))
535 (defun org-at-item-checkbox-p ()
536 "Is point at a line starting a plain-list item with a checklet?"
537 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
539 (defun org-at-item-counter-p ()
540 "Is point at a line starting a plain-list item with a counter?"
541 (and (org-at-item-p)
542 (looking-at org-list-full-item-re)
543 (match-string 2)))
547 ;;; Structures and helper functions
549 (defun org-list-context ()
550 "Determine context, and its boundaries, around point.
552 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
553 are boundaries and CONTEXT is a symbol among `drawer', `block',
554 `invalid', `inlinetask' and nil.
556 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
557 (save-match-data
558 (save-excursion
559 (org-with-limited-levels
560 (beginning-of-line)
561 (let ((case-fold-search t) (pos (point)) beg end context-type
562 ;; Get positions of surrounding headings. This is the
563 ;; default context.
564 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t))
565 (point)))
566 (point-min)))
567 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
568 ;; Is point inside a drawer?
569 (let ((end-re "^[ \t]*:END:")
570 (beg-re org-drawer-regexp))
571 (when (save-excursion
572 (and (not (looking-at beg-re))
573 (not (looking-at end-re))
574 (setq beg (and (re-search-backward beg-re lim-up t)
575 (1+ (point-at-eol))))
576 (setq end (or (and (re-search-forward end-re lim-down t)
577 (1- (match-beginning 0)))
578 lim-down))
579 (>= end pos)))
580 (setq lim-up beg lim-down end context-type 'drawer)))
581 ;; Is point strictly in a block, and of which type?
582 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type)
583 (when (save-excursion
584 (and (not (looking-at block-re))
585 (setq beg (and (re-search-backward block-re lim-up t)
586 (1+ (point-at-eol))))
587 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
588 (setq type (downcase (match-string 1)))
589 (goto-char beg)
590 (setq end (or (and (re-search-forward block-re lim-down t)
591 (1- (point-at-bol)))
592 lim-down))
593 (>= end pos)
594 (equal (downcase (match-string 1)) "end")))
595 (setq lim-up beg lim-down end
596 context-type (if (member type org-list-forbidden-blocks)
597 'invalid 'block))))
598 ;; Is point in an inlinetask?
599 (when (and (featurep 'org-inlinetask)
600 (save-excursion
601 (let* ((beg-re (org-inlinetask-outline-regexp))
602 (end-re (concat beg-re "END[ \t]*$")))
603 (and (not (looking-at "^\\*+"))
604 (setq beg (and (re-search-backward beg-re lim-up t)
605 (1+ (point-at-eol))))
606 (not (looking-at end-re))
607 (setq end (and (re-search-forward end-re lim-down t)
608 (1- (match-beginning 0))))
609 (> (point) pos)))))
610 (setq lim-up beg lim-down end context-type 'inlinetask))
611 ;; Return context boundaries and type.
612 (list lim-up lim-down context-type))))))
614 (defun org-list-struct ()
615 "Return structure of list at point.
617 A list structure is an alist where key is point at item, and
618 values are:
619 1. indentation,
620 2. bullet with trailing whitespace,
621 3. bullet counter, if any,
622 4. checkbox, if any,
623 5. description tag, if any,
624 6. position at item end.
626 Thus the following list, where numbers in parens are
627 point-at-bol:
629 - [X] first item (1)
630 1. sub-item 1 (18)
631 5. [@5] sub-item 2 (34)
632 some other text belonging to first item (55)
633 - last item (97)
634 + tag :: description (109)
635 (131)
637 will get the following structure:
639 ((1 0 \"- \" nil \"[X]\" nil 97)
640 (18 2 \"1. \" nil nil nil 34)
641 (34 2 \"5. \" \"5\" nil nil 55)
642 (97 0 \"- \" nil nil nil 131)
643 (109 2 \"+ \" nil nil \"tag\" 131))
645 Assume point is at an item."
646 (save-excursion
647 (beginning-of-line)
648 (let* ((case-fold-search t)
649 (context (org-list-context))
650 (lim-up (car context))
651 (lim-down (nth 1 context))
652 (text-min-ind 10000)
653 (item-re (org-item-re))
654 (inlinetask-re (and (featurep 'org-inlinetask)
655 (org-inlinetask-outline-regexp)))
656 (beg-cell (cons (point) (org-get-indentation)))
657 itm-lst itm-lst-2 end-lst end-lst-2 struct
658 (assoc-at-point
659 (function
660 ;; Return association at point.
661 (lambda (ind)
662 (looking-at org-list-full-item-re)
663 (let ((bullet (match-string-no-properties 1)))
664 (list (point)
666 bullet
667 (match-string-no-properties 2) ; counter
668 (match-string-no-properties 3) ; checkbox
669 ;; Description tag.
670 (and (string-match-p "[-+*]" bullet)
671 (match-string-no-properties 4)))))))
672 (end-before-blank
673 (function
674 ;; Ensure list ends at the first blank line.
675 (lambda ()
676 (skip-chars-backward " \r\t\n")
677 (min (1+ (point-at-eol)) lim-down)))))
678 ;; 1. Read list from starting item to its beginning, and save
679 ;; top item position and indentation in BEG-CELL. Also store
680 ;; ending position of items in END-LST.
681 (save-excursion
682 (catch 'exit
683 (while t
684 (let ((ind (org-get-indentation)))
685 (cond
686 ((<= (point) lim-up)
687 ;; At upward limit: if we ended at an item, store it,
688 ;; else dismiss useless data recorded above BEG-CELL.
689 ;; Jump to part 2.
690 (throw 'exit
691 (setq itm-lst
692 (if (not (looking-at item-re))
693 (memq (assq (car beg-cell) itm-lst) itm-lst)
694 (setq beg-cell (cons (point) ind))
695 (cons (funcall assoc-at-point ind) itm-lst)))))
696 ;; Looking at a list ending regexp. Dismiss useless
697 ;; data recorded above BEG-CELL. Jump to part 2.
698 ((looking-at org-list-end-re)
699 (throw 'exit
700 (setq itm-lst
701 (memq (assq (car beg-cell) itm-lst) itm-lst))))
702 ;; Point is at an item. Add data to ITM-LST. It may
703 ;; also end a previous item: save it in END-LST. If
704 ;; ind is less or equal than BEG-CELL and there is no
705 ;; end at this ind or lesser, this item becomes the new
706 ;; BEG-CELL.
707 ((looking-at item-re)
708 (push (funcall assoc-at-point ind) itm-lst)
709 (push (cons ind (point)) end-lst)
710 (when (< ind text-min-ind) (setq beg-cell (cons (point) ind)))
711 (forward-line -1))
712 ;; Skip blocks, drawers, inline tasks, blank lines.
713 ((and (looking-at "^[ \t]*#\\+end_")
714 (re-search-backward "^[ \t]*#\\+begin_" lim-up t)))
715 ((and (looking-at "^[ \t]*:END:")
716 (re-search-backward org-drawer-regexp lim-up t))
717 (beginning-of-line))
718 ((and inlinetask-re (looking-at inlinetask-re))
719 (org-inlinetask-goto-beginning)
720 (forward-line -1))
721 ((looking-at "^[ \t]*$")
722 (forward-line -1))
723 ;; From there, point is not at an item. Interpret
724 ;; line's indentation:
725 ;; - text at column 0 is necessarily out of any list.
726 ;; Dismiss data recorded above BEG-CELL. Jump to
727 ;; part 2.
728 ;; - any other case may be an ending position for an
729 ;; hypothetical item above. Store it and proceed.
730 ((zerop ind)
731 (throw 'exit
732 (setq itm-lst
733 (memq (assq (car beg-cell) itm-lst) itm-lst))))
735 (when (< ind text-min-ind) (setq text-min-ind ind))
736 (push (cons ind (point)) end-lst)
737 (forward-line -1)))))))
738 ;; 2. Read list from starting point to its end, that is until we
739 ;; get out of context, or that a non-item line is less or
740 ;; equally indented than BEG-CELL's cdr. Also, store ending
741 ;; position of items in END-LST-2.
742 (catch 'exit
743 (while t
744 (let ((ind (org-get-indentation)))
745 (cond
746 ((>= (point) lim-down)
747 ;; At downward limit: this is de facto the end of the
748 ;; list. Save point as an ending position, and jump to
749 ;; part 3.
750 (throw 'exit
751 (push (cons 0 (funcall end-before-blank)) end-lst-2)))
752 ;; Looking at a list ending regexp. Save point as an
753 ;; ending position and jump to part 3.
754 ((looking-at org-list-end-re)
755 (throw 'exit (push (cons 0 (point)) end-lst-2)))
756 ((looking-at item-re)
757 ;; Point is at an item. Add data to ITM-LST-2. It may
758 ;; also end a previous item, so save it in END-LST-2.
759 (push (funcall assoc-at-point ind) itm-lst-2)
760 (push (cons ind (point)) end-lst-2)
761 (forward-line 1))
762 ;; Skip inline tasks and blank lines along the way
763 ((and inlinetask-re (looking-at inlinetask-re))
764 (org-inlinetask-goto-end))
765 ((looking-at "^[ \t]*$")
766 (forward-line 1))
767 ;; Ind is lesser or equal than BEG-CELL's. The list is
768 ;; over: store point as an ending position and jump to
769 ;; part 3.
770 ((<= ind (cdr beg-cell))
771 (throw 'exit
772 (push (cons 0 (funcall end-before-blank)) end-lst-2)))
773 ;; Else, if ind is lesser or equal than previous item's,
774 ;; this is an ending position: store it. In any case,
775 ;; skip block or drawer at point, and move to next line.
777 (when (<= ind (nth 1 (car itm-lst-2)))
778 (push (cons ind (point)) end-lst-2))
779 (cond
780 ((and (looking-at "^[ \t]*#\\+begin_")
781 (re-search-forward "^[ \t]*#\\+end_" lim-down t)))
782 ((and (looking-at org-drawer-regexp)
783 (re-search-forward "^[ \t]*:END:" lim-down t))))
784 (forward-line 1))))))
785 (setq struct (append itm-lst (cdr (nreverse itm-lst-2)))
786 end-lst (append end-lst (cdr (nreverse end-lst-2))))
787 ;; 3. Associate each item to its end position.
788 (org-list-struct-assoc-end struct end-lst)
789 ;; 4. Return STRUCT
790 struct)))
792 (defun org-list-struct-assoc-end (struct end-list)
793 "Associate proper ending point to items in STRUCT.
795 END-LIST is a pseudo-alist where car is indentation and cdr is
796 ending position.
798 This function modifies STRUCT."
799 (let ((endings end-list))
800 (mapc
801 (lambda (elt)
802 (let ((pos (car elt))
803 (ind (nth 1 elt)))
804 ;; Remove end candidates behind current item.
805 (while (or (<= (cdar endings) pos))
806 (pop endings))
807 ;; Add end position to item assoc.
808 (let ((old-end (nthcdr 6 elt))
809 (new-end (assoc-default ind endings '<=)))
810 (if old-end
811 (setcar old-end new-end)
812 (setcdr elt (append (cdr elt) (list new-end)))))))
813 struct)))
815 (defun org-list-prevs-alist (struct)
816 "Return alist between item and previous item in STRUCT."
817 (let ((item-end-alist (mapcar (lambda (e) (cons (car e) (nth 6 e)))
818 struct)))
819 (mapcar (lambda (e)
820 (let ((prev (car (rassq (car e) item-end-alist))))
821 (cons (car e) prev)))
822 struct)))
824 (defun org-list-parents-alist (struct)
825 "Return alist between item and parent in STRUCT."
826 (let* ((ind-to-ori (list (list (nth 1 (car struct)))))
827 (top-item (org-list-get-top-point struct))
828 (prev-pos (list top-item)))
829 (cons prev-pos
830 (mapcar (lambda (item)
831 (let ((pos (car item))
832 (ind (nth 1 item))
833 (prev-ind (caar ind-to-ori)))
834 (push pos prev-pos)
835 (cond
836 ((> prev-ind ind)
837 ;; A sub-list is over. Find the associated
838 ;; origin in IND-TO-ORI. If it cannot be
839 ;; found (ill-formed list), set its parent as
840 ;; the first item less indented. If there is
841 ;; none, make it a top-level item.
842 (setq ind-to-ori
843 (or (member (assq ind ind-to-ori) ind-to-ori)
844 (catch 'exit
845 (mapc
846 (lambda (e)
847 (when (< (car e) ind)
848 (throw 'exit (member e ind-to-ori))))
849 ind-to-ori)
850 (list (list ind)))))
851 (cons pos (cdar ind-to-ori)))
852 ;; A sub-list starts. Every item at IND will
853 ;; have previous item as its parent.
854 ((< prev-ind ind)
855 (let ((origin (nth 1 prev-pos)))
856 (push (cons ind origin) ind-to-ori)
857 (cons pos origin)))
858 ;; Another item in the same sub-list: it shares
859 ;; the same parent as the previous item.
860 (t (cons pos (cdar ind-to-ori))))))
861 (cdr struct)))))
865 ;;; Accessors
867 (defsubst org-list-get-nth (n key struct)
868 "Return the Nth value of KEY in STRUCT."
869 (nth n (assq key struct)))
871 (defun org-list-set-nth (n key struct new)
872 "Set the Nth value of KEY in STRUCT to NEW.
873 \nThis function modifies STRUCT."
874 (setcar (nthcdr n (assq key struct)) new))
876 (defsubst org-list-get-ind (item struct)
877 "Return indentation of ITEM in STRUCT."
878 (org-list-get-nth 1 item struct))
880 (defun org-list-set-ind (item struct ind)
881 "Set indentation of ITEM in STRUCT to IND.
882 \nThis function modifies STRUCT."
883 (org-list-set-nth 1 item struct ind))
885 (defsubst org-list-get-bullet (item struct)
886 "Return bullet of ITEM in STRUCT."
887 (org-list-get-nth 2 item struct))
889 (defun org-list-set-bullet (item struct bullet)
890 "Set bullet of ITEM in STRUCT to BULLET.
891 \nThis function modifies STRUCT."
892 (org-list-set-nth 2 item struct bullet))
894 (defsubst org-list-get-counter (item struct)
895 "Return counter of ITEM in STRUCT."
896 (org-list-get-nth 3 item struct))
898 (defsubst org-list-get-checkbox (item struct)
899 "Return checkbox of ITEM in STRUCT or nil."
900 (org-list-get-nth 4 item struct))
902 (defun org-list-set-checkbox (item struct checkbox)
903 "Set checkbox of ITEM in STRUCT to CHECKBOX.
904 \nThis function modifies STRUCT."
905 (org-list-set-nth 4 item struct checkbox))
907 (defsubst org-list-get-tag (item struct)
908 "Return end position of ITEM in STRUCT."
909 (org-list-get-nth 5 item struct))
911 (defun org-list-get-item-end (item struct)
912 "Return end position of ITEM in STRUCT."
913 (org-list-get-nth 6 item struct))
915 (defun org-list-get-item-end-before-blank (item struct)
916 "Return point at end of ITEM in STRUCT, before any blank line.
917 Point returned is at end of line."
918 (save-excursion
919 (goto-char (org-list-get-item-end item struct))
920 (skip-chars-backward " \r\t\n")
921 (point-at-eol)))
923 (defun org-list-get-parent (item struct parents)
924 "Return parent of ITEM or nil.
925 STRUCT is the list structure. PARENTS is the alist of parents,
926 as returned by `org-list-parents-alist'."
927 (let ((parents (or parents (org-list-parents-alist struct))))
928 (cdr (assq item parents))))
930 (defun org-list-has-child-p (item struct)
931 "Non-nil if ITEM has a child.
933 STRUCT is the list structure.
935 Value returned is the position of the first child of ITEM."
936 (let ((ind (org-list-get-ind item struct))
937 (child-maybe (car (nth 1 (member (assq item struct) struct)))))
938 (when (and child-maybe
939 (< ind (org-list-get-ind child-maybe struct)))
940 child-maybe)))
942 (defun org-list-get-next-item (item _struct prevs)
943 "Return next item in same sub-list as ITEM, or nil.
944 STRUCT is the list structure. PREVS is the alist of previous
945 items, as returned by `org-list-prevs-alist'."
946 (car (rassq item prevs)))
948 (defun org-list-get-prev-item (item _struct prevs)
949 "Return previous item in same sub-list as ITEM, or nil.
950 STRUCT is the list structure. PREVS is the alist of previous
951 items, as returned by `org-list-prevs-alist'."
952 (cdr (assq item prevs)))
954 (defun org-list-get-subtree (item struct)
955 "List all items having ITEM as a common ancestor, or nil.
956 STRUCT is the list structure."
957 (let* ((item-end (org-list-get-item-end item struct))
958 (sub-struct (cdr (member (assq item struct) struct)))
959 subtree)
960 (catch 'exit
961 (mapc (lambda (e)
962 (let ((pos (car e)))
963 (if (< pos item-end) (push pos subtree) (throw 'exit nil))))
964 sub-struct))
965 (nreverse subtree)))
967 (defun org-list-get-all-items (item struct prevs)
968 "List all items in the same sub-list as ITEM.
969 STRUCT is the list structure. PREVS is the alist of previous
970 items, as returned by `org-list-prevs-alist'."
971 (let ((prev-item item)
972 (next-item item)
973 before-item after-item)
974 (while (setq prev-item (org-list-get-prev-item prev-item struct prevs))
975 (push prev-item before-item))
976 (while (setq next-item (org-list-get-next-item next-item struct prevs))
977 (push next-item after-item))
978 (append before-item (list item) (nreverse after-item))))
980 (defun org-list-get-children (item _struct parents)
981 "List all children of ITEM, or nil.
982 STRUCT is the list structure. PARENTS is the alist of parents,
983 as returned by `org-list-parents-alist'."
984 (let (all child)
985 (while (setq child (car (rassq item parents)))
986 (setq parents (cdr (member (assq child parents) parents)))
987 (push child all))
988 (nreverse all)))
990 (defun org-list-get-top-point (struct)
991 "Return point at beginning of list.
992 STRUCT is the list structure."
993 (caar struct))
995 (defun org-list-get-bottom-point (struct)
996 "Return point at bottom of list.
997 STRUCT is the list structure."
998 (apply #'max
999 (mapcar (lambda (e) (org-list-get-item-end (car e) struct)) struct)))
1001 (defun org-list-get-list-begin (item struct prevs)
1002 "Return point at beginning of sub-list ITEM belongs.
1003 STRUCT is the list structure. PREVS is the alist of previous
1004 items, as returned by `org-list-prevs-alist'."
1005 (let ((first-item item) prev-item)
1006 (while (setq prev-item (org-list-get-prev-item first-item struct prevs))
1007 (setq first-item prev-item))
1008 first-item))
1010 (defalias 'org-list-get-first-item 'org-list-get-list-begin)
1012 (defun org-list-get-last-item (item struct prevs)
1013 "Return point at last item of sub-list ITEM belongs.
1014 STRUCT is the list structure. PREVS is the alist of previous
1015 items, as returned by `org-list-prevs-alist'."
1016 (let ((last-item item) next-item)
1017 (while (setq next-item (org-list-get-next-item last-item struct prevs))
1018 (setq last-item next-item))
1019 last-item))
1021 (defun org-list-get-list-end (item struct prevs)
1022 "Return point at end of sub-list ITEM belongs.
1023 STRUCT is the list structure. PREVS is the alist of previous
1024 items, as returned by `org-list-prevs-alist'."
1025 (org-list-get-item-end (org-list-get-last-item item struct prevs) struct))
1027 (defun org-list-get-list-type (item struct prevs)
1028 "Return the type of the list containing ITEM, as a symbol.
1030 STRUCT is the list structure. PREVS is the alist of previous
1031 items, as returned by `org-list-prevs-alist'.
1033 Possible types are `descriptive', `ordered' and `unordered'. The
1034 type is determined by the first item of the list."
1035 (let ((first (org-list-get-list-begin item struct prevs)))
1036 (cond
1037 ((string-match-p "[[:alnum:]]" (org-list-get-bullet first struct)) 'ordered)
1038 ((org-list-get-tag first struct) 'descriptive)
1039 (t 'unordered))))
1041 (defun org-list-get-item-number (item struct prevs parents)
1042 "Return ITEM's sequence number.
1044 STRUCT is the list structure. PREVS is the alist of previous
1045 items, as returned by `org-list-prevs-alist'. PARENTS is the
1046 alist of ancestors, as returned by `org-list-parents-alist'.
1048 Return value is a list of integers. Counters have an impact on
1049 that value."
1050 (let ((get-relative-number
1051 (function
1052 (lambda (item struct prevs)
1053 ;; Return relative sequence number of ITEM in the sub-list
1054 ;; it belongs. STRUCT is the list structure. PREVS is
1055 ;; the alist of previous items.
1056 (let ((seq 0) (pos item) counter)
1057 (while (and (not (setq counter (org-list-get-counter pos struct)))
1058 (setq pos (org-list-get-prev-item pos struct prevs)))
1059 (cl-incf seq))
1060 (if (not counter) (1+ seq)
1061 (cond
1062 ((string-match "[A-Za-z]" counter)
1063 (+ (- (string-to-char (upcase (match-string 0 counter))) 64)
1064 seq))
1065 ((string-match "[0-9]+" counter)
1066 (+ (string-to-number (match-string 0 counter)) seq))
1067 (t (1+ seq)))))))))
1068 ;; Cons each parent relative number into return value (OUT).
1069 (let ((out (list (funcall get-relative-number item struct prevs)))
1070 (parent item))
1071 (while (setq parent (org-list-get-parent parent struct parents))
1072 (push (funcall get-relative-number parent struct prevs) out))
1073 ;; Return value.
1074 out)))
1078 ;;; Searching
1080 (defun org-list-search-generic (search re bound noerr)
1081 "Search a string in valid contexts for lists.
1082 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1083 in `re-search-forward'."
1084 (catch 'exit
1085 (let ((origin (point)))
1086 (while t
1087 ;; 1. No match: return to origin or bound, depending on NOERR.
1088 (unless (funcall search re bound noerr)
1089 (throw 'exit (and (goto-char (if (memq noerr '(t nil)) origin bound))
1090 nil)))
1091 ;; 2. Match in valid context: return point. Else, continue
1092 ;; searching.
1093 (when (org-list-in-valid-context-p) (throw 'exit (point)))))))
1095 (defun org-list-search-backward (regexp &optional bound noerror)
1096 "Like `re-search-backward' but stop only where lists are recognized.
1097 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1098 `re-search-backward'."
1099 (org-list-search-generic #'re-search-backward
1100 regexp (or bound (point-min)) noerror))
1102 (defun org-list-search-forward (regexp &optional bound noerror)
1103 "Like `re-search-forward' but stop only where lists are recognized.
1104 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1105 `re-search-forward'."
1106 (org-list-search-generic #'re-search-forward
1107 regexp (or bound (point-max)) noerror))
1111 ;;; Methods on structures
1113 (defsubst org-list-bullet-string (bullet)
1114 "Return BULLET with the correct number of whitespaces.
1115 It determines the number of whitespaces to append by looking at
1116 `org-list-two-spaces-after-bullet-regexp'."
1117 (save-match-data
1118 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1119 (string-match
1120 org-list-two-spaces-after-bullet-regexp bullet))
1122 " ")))
1123 (if (string-match "\\S-+\\([ \t]*\\)" bullet)
1124 (replace-match spaces nil nil bullet 1)
1125 bullet))))
1127 (defun org-list-swap-items (beg-A beg-B struct)
1128 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1130 Blank lines at the end of items are left in place. Item
1131 visibility is preserved. Return the new structure after the
1132 changes.
1134 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1135 to the same sub-list.
1137 This function modifies STRUCT."
1138 (save-excursion
1139 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct))
1140 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct))
1141 (end-A (org-list-get-item-end beg-A struct))
1142 (end-B (org-list-get-item-end beg-B struct))
1143 (size-A (- end-A-no-blank beg-A))
1144 (size-B (- end-B-no-blank beg-B))
1145 (body-A (buffer-substring beg-A end-A-no-blank))
1146 (body-B (buffer-substring beg-B end-B-no-blank))
1147 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B))
1148 (sub-A (cons beg-A (org-list-get-subtree beg-A struct)))
1149 (sub-B (cons beg-B (org-list-get-subtree beg-B struct)))
1150 ;; Store overlays responsible for visibility status. We
1151 ;; also need to store their boundaries as they will be
1152 ;; removed from buffer.
1153 (overlays
1154 (cons
1155 (delq nil
1156 (mapcar (lambda (o)
1157 (and (>= (overlay-start o) beg-A)
1158 (<= (overlay-end o) end-A)
1159 (list o (overlay-start o) (overlay-end o))))
1160 (overlays-in beg-A end-A)))
1161 (delq nil
1162 (mapcar (lambda (o)
1163 (and (>= (overlay-start o) beg-B)
1164 (<= (overlay-end o) end-B)
1165 (list o (overlay-start o) (overlay-end o))))
1166 (overlays-in beg-B end-B))))))
1167 ;; 1. Move effectively items in buffer.
1168 (goto-char beg-A)
1169 (delete-region beg-A end-B-no-blank)
1170 (insert (concat body-B between-A-no-blank-and-B body-A))
1171 ;; 2. Now modify struct. No need to re-read the list, the
1172 ;; transformation is just a shift of positions. Some special
1173 ;; attention is required for items ending at END-A and END-B
1174 ;; as empty spaces are not moved there. In others words,
1175 ;; item BEG-A will end with whitespaces that were at the end
1176 ;; of BEG-B and the same applies to BEG-B.
1177 (dolist (e struct)
1178 (let ((pos (car e)))
1179 (cond
1180 ((< pos beg-A))
1181 ((memq pos sub-A)
1182 (let ((end-e (nth 6 e)))
1183 (setcar e (+ pos (- end-B-no-blank end-A-no-blank)))
1184 (setcar (nthcdr 6 e)
1185 (+ end-e (- end-B-no-blank end-A-no-blank)))
1186 (when (= end-e end-A) (setcar (nthcdr 6 e) end-B))))
1187 ((memq pos sub-B)
1188 (let ((end-e (nth 6 e)))
1189 (setcar e (- (+ pos beg-A) beg-B))
1190 (setcar (nthcdr 6 e) (+ end-e (- beg-A beg-B)))
1191 (when (= end-e end-B)
1192 (setcar (nthcdr 6 e)
1193 (+ beg-A size-B (- end-A end-A-no-blank))))))
1194 ((< pos beg-B)
1195 (let ((end-e (nth 6 e)))
1196 (setcar e (+ pos (- size-B size-A)))
1197 (setcar (nthcdr 6 e) (+ end-e (- size-B size-A))))))))
1198 (setq struct (sort struct #'car-less-than-car))
1199 ;; Restore visibility status, by moving overlays to their new
1200 ;; position.
1201 (dolist (ov (car overlays))
1202 (move-overlay
1203 (car ov)
1204 (+ (nth 1 ov) (- (+ beg-B (- size-B size-A)) beg-A))
1205 (+ (nth 2 ov) (- (+ beg-B (- size-B size-A)) beg-A))))
1206 (dolist (ov (cdr overlays))
1207 (move-overlay (car ov)
1208 (+ (nth 1 ov) (- beg-A beg-B))
1209 (+ (nth 2 ov) (- beg-A beg-B))))
1210 ;; Return structure.
1211 struct)))
1213 (defun org-list-separating-blank-lines-number (pos struct prevs)
1214 "Return number of blank lines that should separate items in list.
1216 POS is the position of point where `org-list-insert-item' was called.
1218 STRUCT is the list structure. PREVS is the alist of previous
1219 items, as returned by `org-list-prevs-alist'.
1221 Assume point is at item's beginning. If the item is alone, apply
1222 some heuristics to guess the result."
1223 (save-excursion
1224 (let ((item (point))
1225 (insert-blank-p
1226 (cdr (assq 'plain-list-item org-blank-before-new-entry)))
1227 usr-blank
1228 (count-blanks
1229 (function
1230 (lambda ()
1231 ;; Count blank lines above beginning of line.
1232 (save-excursion
1233 (count-lines (goto-char (point-at-bol))
1234 (progn (skip-chars-backward " \r\t\n")
1235 (forward-line)
1236 (point))))))))
1237 (cond
1238 ;; Trivial cases where there should be none.
1239 ((not insert-blank-p) 0)
1240 ;; When `org-blank-before-new-entry' says so, it is 1.
1241 ((eq insert-blank-p t) 1)
1242 ;; `plain-list-item' is 'auto. Count blank lines separating
1243 ;; neighbors' items in list.
1244 (t (let ((next-p (org-list-get-next-item item struct prevs)))
1245 (cond
1246 ;; Is there a next item?
1247 (next-p (goto-char next-p)
1248 (funcall count-blanks))
1249 ;; Is there a previous item?
1250 ((org-list-get-prev-item item struct prevs)
1251 (funcall count-blanks))
1252 ;; User inserted blank lines, trust him.
1253 ((and (> pos (org-list-get-item-end-before-blank item struct))
1254 (> (save-excursion (goto-char pos)
1255 (setq usr-blank (funcall count-blanks)))
1257 usr-blank)
1258 ;; Are there blank lines inside the list so far?
1259 ((save-excursion
1260 (goto-char (org-list-get-top-point struct))
1261 ;; Do not use `org-list-search-forward' so blank lines
1262 ;; in blocks can be counted in.
1263 (re-search-forward
1264 "^[ \t]*$" (org-list-get-item-end-before-blank item struct) t))
1266 ;; Default choice: no blank line.
1267 (t 0))))))))
1269 (defun org-list-insert-item (pos struct prevs &optional checkbox after-bullet)
1270 "Insert a new list item at POS and return the new structure.
1271 If POS is before first character after bullet of the item, the
1272 new item will be created before the current one.
1274 STRUCT is the list structure. PREVS is the alist of previous
1275 items, as returned by `org-list-prevs-alist'.
1277 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1278 after the bullet. Cursor will be after this text once the
1279 function ends.
1281 This function modifies STRUCT."
1282 (let ((case-fold-search t))
1283 ;; 1. Get information about list: position of point with regards
1284 ;; to item start (BEFOREP), blank lines number separating items
1285 ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
1286 (let* ((item (progn (goto-char pos) (goto-char (org-list-get-item-begin))))
1287 (item-end (org-list-get-item-end item struct))
1288 (item-end-no-blank (org-list-get-item-end-before-blank item struct))
1289 (beforep
1290 (progn
1291 (looking-at org-list-full-item-re)
1292 (<= pos
1293 (cond
1294 ((not (match-beginning 4)) (match-end 0))
1295 ;; Ignore tag in a non-descriptive list.
1296 ((save-match-data (string-match "[.)]" (match-string 1)))
1297 (match-beginning 4))
1298 (t (save-excursion
1299 (goto-char (match-end 4))
1300 (skip-chars-forward " \t")
1301 (point)))))))
1302 (split-line-p (org-get-alist-option org-M-RET-may-split-line 'item))
1303 (blank-nb (org-list-separating-blank-lines-number
1304 pos struct prevs))
1305 ;; 2. Build the new item to be created. Concatenate same
1306 ;; bullet as item, checkbox, text AFTER-BULLET if
1307 ;; provided, and text cut from point to end of item
1308 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1309 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1310 ;; between what was cut and what was inserted in buffer
1311 ;; is stored in SIZE-OFFSET.
1312 (ind (org-list-get-ind item struct))
1313 (ind-size (if indent-tabs-mode
1314 (+ (/ ind tab-width) (mod ind tab-width))
1315 ind))
1316 (bullet (org-list-bullet-string (org-list-get-bullet item struct)))
1317 (box (when checkbox "[ ]"))
1318 (text-cut
1319 (and (not beforep) split-line-p
1320 (progn
1321 (goto-char pos)
1322 ;; If POS is greater than ITEM-END, then point is
1323 ;; in some white lines after the end of the list.
1324 ;; Those must be removed, or they will be left,
1325 ;; stacking up after the list.
1326 (when (< item-end pos)
1327 (delete-region (1- item-end) (point-at-eol)))
1328 (skip-chars-backward " \r\t\n")
1329 (setq pos (point))
1330 (delete-and-extract-region pos item-end-no-blank))))
1331 (body (concat bullet (when box (concat box " ")) after-bullet
1332 (and text-cut
1333 (if (string-match "\\`[ \t]+" text-cut)
1334 (replace-match "" t t text-cut)
1335 text-cut))))
1336 (item-sep (make-string (1+ blank-nb) ?\n))
1337 (item-size (+ ind-size (length body) (length item-sep)))
1338 (size-offset (- item-size (length text-cut))))
1339 ;; 4. Insert effectively item into buffer.
1340 (goto-char item)
1341 (indent-to-column ind)
1342 (insert body item-sep)
1343 ;; 5. Add new item to STRUCT.
1344 (mapc (lambda (e)
1345 (let ((p (car e)) (end (nth 6 e)))
1346 (cond
1347 ;; Before inserted item, positions don't change but
1348 ;; an item ending after insertion has its end shifted
1349 ;; by SIZE-OFFSET.
1350 ((< p item)
1351 (when (> end item) (setcar (nthcdr 6 e) (+ end size-offset))))
1352 ;; Trivial cases where current item isn't split in
1353 ;; two. Just shift every item after new one by
1354 ;; ITEM-SIZE.
1355 ((or beforep (not split-line-p))
1356 (setcar e (+ p item-size))
1357 (setcar (nthcdr 6 e) (+ end item-size)))
1358 ;; Item is split in two: elements before POS are just
1359 ;; shifted by ITEM-SIZE. In the case item would end
1360 ;; after split POS, ending is only shifted by
1361 ;; SIZE-OFFSET.
1362 ((< p pos)
1363 (setcar e (+ p item-size))
1364 (if (< end pos)
1365 (setcar (nthcdr 6 e) (+ end item-size))
1366 (setcar (nthcdr 6 e) (+ end size-offset))))
1367 ;; Elements after POS are moved into new item.
1368 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1369 ;; doesn't appear in buffer yet.
1370 ((< p item-end)
1371 (setcar e (+ p size-offset (- item pos (length item-sep))))
1372 (if (= end item-end)
1373 (setcar (nthcdr 6 e) (+ item item-size))
1374 (setcar (nthcdr 6 e)
1375 (+ end size-offset
1376 (- item pos (length item-sep))))))
1377 ;; Elements at ITEM-END or after are only shifted by
1378 ;; SIZE-OFFSET.
1379 (t (setcar e (+ p size-offset))
1380 (setcar (nthcdr 6 e) (+ end size-offset))))))
1381 struct)
1382 (push (list item ind bullet nil box nil (+ item item-size)) struct)
1383 (setq struct (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))
1384 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1385 ;; exchange ITEM with the next item in list. Position cursor
1386 ;; after bullet, counter, checkbox, and label.
1387 (if beforep
1388 (goto-char item)
1389 (setq struct (org-list-swap-items item (+ item item-size) struct))
1390 (goto-char (org-list-get-next-item
1391 item struct (org-list-prevs-alist struct))))
1392 struct)))
1394 (defun org-list-delete-item (item struct)
1395 "Remove ITEM from the list and return the new structure.
1397 STRUCT is the list structure."
1398 (let* ((end (org-list-get-item-end item struct))
1399 (beg (if (= (org-list-get-bottom-point struct) end)
1400 ;; If ITEM ends with the list, delete blank lines
1401 ;; before it.
1402 (save-excursion
1403 (goto-char item)
1404 (skip-chars-backward " \r\t\n")
1405 (min (1+ (point-at-eol)) (point-max)))
1406 item)))
1407 ;; Remove item from buffer.
1408 (delete-region beg end)
1409 ;; Remove item from structure and shift others items accordingly.
1410 ;; Don't forget to shift also ending position when appropriate.
1411 (let ((size (- end beg)))
1412 (delq nil (mapcar (lambda (e)
1413 (let ((pos (car e)))
1414 (cond
1415 ((< pos item)
1416 (let ((end-e (nth 6 e)))
1417 (cond
1418 ((< end-e item) e)
1419 ((= end-e item)
1420 (append (butlast e) (list beg)))
1422 (append (butlast e) (list (- end-e size)))))))
1423 ((< pos end) nil)
1425 (cons (- pos size)
1426 (append (butlast (cdr e))
1427 (list (- (nth 6 e) size))))))))
1428 struct)))))
1430 (defun org-list-send-item (item dest struct)
1431 "Send ITEM to destination DEST.
1433 STRUCT is the list structure.
1435 DEST can have various values.
1437 If DEST is a buffer position, the function will assume it points
1438 to another item in the same list as ITEM, and will move the
1439 latter just before the former.
1441 If DEST is `begin' (respectively `end'), ITEM will be moved at
1442 the beginning (respectively end) of the list it belongs to.
1444 If DEST is a string like \"N\", where N is an integer, ITEM will
1445 be moved at the Nth position in the list.
1447 If DEST is `kill', ITEM will be deleted and its body will be
1448 added to the kill-ring.
1450 If DEST is `delete', ITEM will be deleted.
1452 Visibility of item is preserved.
1454 This function returns, destructively, the new list structure."
1455 (let* ((prevs (org-list-prevs-alist struct))
1456 (item-end (org-list-get-item-end item struct))
1457 ;; Grab full item body minus its bullet.
1458 (body (org-trim
1459 (buffer-substring
1460 (save-excursion
1461 (goto-char item)
1462 (looking-at
1463 (concat "[ \t]*"
1464 (regexp-quote (org-list-get-bullet item struct))))
1465 (match-end 0))
1466 item-end)))
1467 ;; Change DEST into a buffer position. A trick is needed
1468 ;; when ITEM is meant to be sent at the end of the list.
1469 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1470 ;; nil and insertion point (INS-POINT) to the first line's
1471 ;; end of the last item, we ensure the new item will be
1472 ;; inserted after the last item, and not after any of its
1473 ;; hypothetical sub-items.
1474 (ins-point (cond
1475 ((or (eq dest 'kill) (eq dest 'delete)))
1476 ((eq dest 'begin)
1477 (setq dest (org-list-get-list-begin item struct prevs)))
1478 ((eq dest 'end)
1479 (setq dest (org-list-get-list-end item struct prevs))
1480 (save-excursion
1481 (goto-char (org-list-get-last-item item struct prevs))
1482 (point-at-eol)))
1483 ((string-match-p "\\`[0-9]+\\'" dest)
1484 (let* ((all (org-list-get-all-items item struct prevs))
1485 (len (length all))
1486 (index (mod (string-to-number dest) len)))
1487 (if (not (zerop index))
1488 (setq dest (nth (1- index) all))
1489 ;; Send ITEM at the end of the list.
1490 (setq dest (org-list-get-list-end item struct prevs))
1491 (save-excursion
1492 (goto-char
1493 (org-list-get-last-item item struct prevs))
1494 (point-at-eol)))))
1495 (t dest)))
1496 (org-M-RET-may-split-line nil)
1497 ;; Store inner overlays (to preserve visibility).
1498 (overlays (cl-remove-if (lambda (o) (or (< (overlay-start o) item)
1499 (> (overlay-end o) item)))
1500 (overlays-in item item-end))))
1501 (cond
1502 ((eq dest 'delete) (org-list-delete-item item struct))
1503 ((eq dest 'kill)
1504 (kill-new body)
1505 (org-list-delete-item item struct))
1506 ((and (integerp dest) (/= item ins-point))
1507 (setq item (copy-marker item))
1508 (setq struct (org-list-insert-item ins-point struct prevs nil body))
1509 ;; 1. Structure returned by `org-list-insert-item' may not be
1510 ;; accurate, as it cannot see sub-items included in BODY.
1511 ;; Thus, first compute the real structure so far.
1512 (let ((moved-items
1513 (cons (marker-position item)
1514 (org-list-get-subtree (marker-position item) struct)))
1515 (new-end (org-list-get-item-end (point) struct))
1516 (old-end (org-list-get-item-end (marker-position item) struct))
1517 (new-item (point))
1518 (shift (- (point) item)))
1519 ;; 1.1. Remove the item just created in structure.
1520 (setq struct (delete (assq new-item struct) struct))
1521 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1522 (setq struct (sort
1523 (append
1524 struct
1525 (mapcar (lambda (e)
1526 (let* ((cell (assq e struct))
1527 (pos (car cell))
1528 (end (nth 6 cell)))
1529 (cons (+ pos shift)
1530 (append (butlast (cdr cell))
1531 (list (if (= end old-end)
1532 new-end
1533 (+ end shift)))))))
1534 moved-items))
1535 #'car-less-than-car)))
1536 ;; 2. Restore inner overlays.
1537 (dolist (o overlays)
1538 (move-overlay o
1539 (+ (overlay-start o) (- (point) item))
1540 (+ (overlay-end o) (- (point) item))))
1541 ;; 3. Eventually delete extra copy of the item and clean marker.
1542 (prog1 (org-list-delete-item (marker-position item) struct)
1543 (move-marker item nil)))
1544 (t struct))))
1546 (defun org-list-struct-outdent (start end struct parents)
1547 "Outdent items between positions START and END.
1549 STRUCT is the list structure. PARENTS is the alist of items'
1550 parents, as returned by `org-list-parents-alist'.
1552 START is included, END excluded."
1553 (let* (acc
1554 (out (lambda (cell)
1555 (let* ((item (car cell))
1556 (parent (cdr cell)))
1557 (cond
1558 ;; Item not yet in zone: keep association.
1559 ((< item start) cell)
1560 ;; Item out of zone: follow associations in ACC.
1561 ((>= item end)
1562 (let ((convert (and parent (assq parent acc))))
1563 (if convert (cons item (cdr convert)) cell)))
1564 ;; Item has no parent: error
1565 ((not parent)
1566 (error "Cannot outdent top-level items"))
1567 ;; Parent is outdented: keep association.
1568 ((>= parent start)
1569 (push (cons parent item) acc) cell)
1571 ;; Parent isn't outdented: reparent to grand-parent.
1572 (let ((grand-parent (org-list-get-parent
1573 parent struct parents)))
1574 (push (cons parent item) acc)
1575 (cons item grand-parent))))))))
1576 (mapcar out parents)))
1578 (defun org-list-struct-indent (start end struct parents prevs)
1579 "Indent items between positions START and END.
1581 STRUCT is the list structure. PARENTS is the alist of parents
1582 and PREVS is the alist of previous items, returned by,
1583 respectively, `org-list-parents-alist' and
1584 `org-list-prevs-alist'.
1586 START is included and END excluded.
1588 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1589 bullets between START and END."
1590 (let* (acc
1591 (set-assoc (lambda (cell) (push cell acc) cell))
1592 (change-bullet-maybe
1593 (function
1594 (lambda (item)
1595 (let ((new-bul-p
1596 (cdr (assoc
1597 ;; Normalize ordered bullets.
1598 (let ((bul (org-trim
1599 (org-list-get-bullet item struct))))
1600 (cond ((string-match "[A-Z]\\." bul) "A.")
1601 ((string-match "[A-Z])" bul) "A)")
1602 ((string-match "[a-z]\\." bul) "a.")
1603 ((string-match "[a-z])" bul) "a)")
1604 ((string-match "[0-9]\\." bul) "1.")
1605 ((string-match "[0-9])" bul) "1)")
1606 (t bul)))
1607 org-list-demote-modify-bullet))))
1608 (when new-bul-p (org-list-set-bullet item struct new-bul-p))))))
1609 (ind
1610 (lambda (cell)
1611 (let* ((item (car cell))
1612 (parent (cdr cell)))
1613 (cond
1614 ;; Item not yet in zone: keep association.
1615 ((< item start) cell)
1616 ((>= item end)
1617 ;; Item out of zone: follow associations in ACC.
1618 (let ((convert (assq parent acc)))
1619 (if convert (cons item (cdr convert)) cell)))
1621 ;; Item is in zone...
1622 (let ((prev (org-list-get-prev-item item struct prevs)))
1623 ;; Check if bullet needs to be changed.
1624 (funcall change-bullet-maybe item)
1625 (cond
1626 ;; First item indented but not parent: error
1627 ((and (not prev) (< parent start))
1628 (error "Cannot indent the first item of a list"))
1629 ;; First item and parent indented: keep same
1630 ;; parent.
1631 ((not prev) (funcall set-assoc cell))
1632 ;; Previous item not indented: reparent to it.
1633 ((< prev start) (funcall set-assoc (cons item prev)))
1634 ;; Previous item indented: reparent like it.
1636 (funcall set-assoc
1637 (cons item (cdr (assq prev acc)))))))))))))
1638 (mapcar ind parents)))
1642 ;;; Repairing structures
1644 (defun org-list-use-alpha-bul-p (first struct prevs)
1645 "Non-nil if list starting at FIRST can have alphabetical bullets.
1647 STRUCT is list structure. PREVS is the alist of previous items,
1648 as returned by `org-list-prevs-alist'."
1649 (and org-list-allow-alphabetical
1650 (catch 'exit
1651 (let ((item first) (ascii 64) (case-fold-search nil))
1652 ;; Pretend that bullets are uppercase and check if alphabet
1653 ;; is sufficient, taking counters into account.
1654 (while item
1655 (let ((count (org-list-get-counter item struct)))
1656 ;; Virtually determine current bullet
1657 (if (and count (string-match-p "[a-zA-Z]" count))
1658 ;; Counters are not case-sensitive.
1659 (setq ascii (string-to-char (upcase count)))
1660 (setq ascii (1+ ascii)))
1661 ;; Test if bullet would be over z or Z.
1662 (if (> ascii 90)
1663 (throw 'exit nil)
1664 (setq item (org-list-get-next-item item struct prevs)))))
1665 ;; All items checked. All good.
1666 t))))
1668 (defun org-list-inc-bullet-maybe (bullet)
1669 "Increment BULLET if applicable."
1670 (let ((case-fold-search nil))
1671 (cond
1672 ;; Num bullet: increment it.
1673 ((string-match "[0-9]+" bullet)
1674 (replace-match
1675 (number-to-string (1+ (string-to-number (match-string 0 bullet))))
1676 nil nil bullet))
1677 ;; Alpha bullet: increment it.
1678 ((string-match "[A-Za-z]" bullet)
1679 (replace-match
1680 (char-to-string (1+ (string-to-char (match-string 0 bullet))))
1681 nil nil bullet))
1682 ;; Unordered bullet: leave it.
1683 (t bullet))))
1685 (defun org-list-struct-fix-bul (struct prevs)
1686 "Verify and correct bullets in STRUCT.
1687 PREVS is the alist of previous items, as returned by
1688 `org-list-prevs-alist'.
1690 This function modifies STRUCT."
1691 (let ((case-fold-search nil)
1692 (fix-bul
1693 (function
1694 ;; Set bullet of ITEM in STRUCT, depending on the type of
1695 ;; first item of the list, the previous bullet and counter
1696 ;; if any.
1697 (lambda (item)
1698 (let* ((prev (org-list-get-prev-item item struct prevs))
1699 (prev-bul (and prev (org-list-get-bullet prev struct)))
1700 (counter (org-list-get-counter item struct))
1701 (bullet (org-list-get-bullet item struct))
1702 (alphap (and (not prev)
1703 (org-list-use-alpha-bul-p item struct prevs))))
1704 (org-list-set-bullet
1705 item struct
1706 (org-list-bullet-string
1707 (cond
1708 ;; Alpha counter in alpha list: use counter.
1709 ((and prev counter
1710 (string-match "[a-zA-Z]" counter)
1711 (string-match "[a-zA-Z]" prev-bul))
1712 ;; Use cond to be sure `string-match' is used in
1713 ;; both cases.
1714 (let ((real-count
1715 (cond
1716 ((string-match "[a-z]" prev-bul) (downcase counter))
1717 ((string-match "[A-Z]" prev-bul) (upcase counter)))))
1718 (replace-match real-count nil nil prev-bul)))
1719 ;; Num counter in a num list: use counter.
1720 ((and prev counter
1721 (string-match "[0-9]+" counter)
1722 (string-match "[0-9]+" prev-bul))
1723 (replace-match counter nil nil prev-bul))
1724 ;; No counter: increase, if needed, previous bullet.
1725 (prev
1726 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct)))
1727 ;; Alpha counter at first item: use counter.
1728 ((and counter (org-list-use-alpha-bul-p item struct prevs)
1729 (string-match "[A-Za-z]" counter)
1730 (string-match "[A-Za-z]" bullet))
1731 (let ((real-count
1732 (cond
1733 ((string-match "[a-z]" bullet) (downcase counter))
1734 ((string-match "[A-Z]" bullet) (upcase counter)))))
1735 (replace-match real-count nil nil bullet)))
1736 ;; Num counter at first item: use counter.
1737 ((and counter
1738 (string-match "[0-9]+" counter)
1739 (string-match "[0-9]+" bullet))
1740 (replace-match counter nil nil bullet))
1741 ;; First bullet is alpha uppercase: use "A".
1742 ((and alphap (string-match "[A-Z]" bullet))
1743 (replace-match "A" nil nil bullet))
1744 ;; First bullet is alpha lowercase: use "a".
1745 ((and alphap (string-match "[a-z]" bullet))
1746 (replace-match "a" nil nil bullet))
1747 ;; First bullet is num: use "1".
1748 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet)
1749 (replace-match "1" nil nil bullet))
1750 ;; Not an ordered list: keep bullet.
1751 (t bullet)))))))))
1752 (mapc fix-bul (mapcar #'car struct))))
1754 (defun org-list-struct-fix-ind (struct parents &optional bullet-size)
1755 "Verify and correct indentation in STRUCT.
1757 PARENTS is the alist of parents, as returned by
1758 `org-list-parents-alist'.
1760 If numeric optional argument BULLET-SIZE is set, assume all
1761 bullets in list have this length to determine new indentation.
1763 This function modifies STRUCT."
1764 (let* ((ancestor (org-list-get-top-point struct))
1765 (top-ind (org-list-get-ind ancestor struct))
1766 (new-ind
1767 (lambda (item)
1768 (let ((parent (org-list-get-parent item struct parents)))
1769 (if parent
1770 ;; Indent like parent + length of parent's bullet +
1771 ;; sub-list offset.
1772 (org-list-set-ind
1773 item struct (+ (or bullet-size
1774 (length
1775 (org-list-get-bullet parent struct)))
1776 (org-list-get-ind parent struct)
1777 org-list-indent-offset))
1778 ;; If no parent, indent like top-point.
1779 (org-list-set-ind item struct top-ind))))))
1780 (mapc new-ind (mapcar #'car (cdr struct)))))
1782 (defun org-list-struct-fix-box (struct parents prevs &optional ordered)
1783 "Verify and correct checkboxes in STRUCT.
1785 PARENTS is the alist of parents and PREVS is the alist of
1786 previous items, as returned by, respectively,
1787 `org-list-parents-alist' and `org-list-prevs-alist'.
1789 If ORDERED is non-nil, a checkbox can only be checked when every
1790 checkbox before it is checked too. If there was an attempt to
1791 break this rule, the function will return the blocking item. In
1792 all others cases, the return value will be nil.
1794 This function modifies STRUCT."
1795 (let ((all-items (mapcar #'car struct))
1796 (set-parent-box
1797 (function
1798 (lambda (item)
1799 (let* ((box-list
1800 (mapcar (lambda (child)
1801 (org-list-get-checkbox child struct))
1802 (org-list-get-children item struct parents))))
1803 (org-list-set-checkbox
1804 item struct
1805 (cond
1806 ((and (member "[ ]" box-list) (member "[X]" box-list)) "[-]")
1807 ((member "[-]" box-list) "[-]")
1808 ((member "[X]" box-list) "[X]")
1809 ((member "[ ]" box-list) "[ ]")
1810 ;; Parent has no boxed child: leave box as-is.
1811 (t (org-list-get-checkbox item struct))))))))
1812 parent-list)
1813 ;; 1. List all parents with a checkbox.
1814 (mapc
1815 (lambda (e)
1816 (let* ((parent (org-list-get-parent e struct parents))
1817 (parent-box-p (org-list-get-checkbox parent struct)))
1818 (when (and parent-box-p (not (memq parent parent-list)))
1819 (push parent parent-list))))
1820 all-items)
1821 ;; 2. Sort those parents by decreasing indentation.
1822 (setq parent-list (sort parent-list
1823 (lambda (e1 e2)
1824 (> (org-list-get-ind e1 struct)
1825 (org-list-get-ind e2 struct)))))
1826 ;; 3. For each parent, get all children's checkboxes to determine
1827 ;; and set its checkbox accordingly.
1828 (mapc set-parent-box parent-list)
1829 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1830 (when ordered
1831 (let* ((box-list
1832 (mapcar (lambda (e) (org-list-get-checkbox e struct)) all-items))
1833 (after-unchecked (member "[ ]" box-list)))
1834 ;; There are boxes checked after an unchecked one: fix that.
1835 (when (member "[X]" after-unchecked)
1836 (let ((index (- (length struct) (length after-unchecked))))
1837 (mapc (lambda (e)
1838 (when (org-list-get-checkbox e struct)
1839 (org-list-set-checkbox e struct "[ ]")))
1840 (nthcdr index all-items))
1841 ;; Verify once again the structure, without ORDERED.
1842 (org-list-struct-fix-box struct parents prevs nil)
1843 ;; Return blocking item.
1844 (nth index all-items)))))))
1846 (defun org-list-struct-fix-item-end (struct)
1847 "Verify and correct each item end position in STRUCT.
1849 This function modifies STRUCT."
1850 (let (end-list acc-end)
1851 (mapc (lambda (e)
1852 (let* ((pos (car e))
1853 (ind-pos (org-list-get-ind pos struct))
1854 (end-pos (org-list-get-item-end pos struct)))
1855 (unless (assq end-pos struct)
1856 ;; To determine real ind of an ending position that is
1857 ;; not at an item, we have to find the item it belongs
1858 ;; to: it is the last item (ITEM-UP), whose ending is
1859 ;; further than the position we're interested in.
1860 (let ((item-up (assoc-default end-pos acc-end '>)))
1861 (push (cons
1862 ;; Else part is for the bottom point.
1863 (if item-up (+ (org-list-get-ind item-up struct) 2) 0)
1864 end-pos)
1865 end-list)))
1866 (push (cons ind-pos pos) end-list)
1867 (push (cons end-pos pos) acc-end)))
1868 struct)
1869 (setq end-list (sort end-list (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
1870 (org-list-struct-assoc-end struct end-list)))
1872 (defun org-list-struct-apply-struct (struct old-struct)
1873 "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
1875 OLD-STRUCT is the structure before any modifications, and STRUCT
1876 the structure to be applied. The function will only modify parts
1877 of the list which have changed.
1879 Initial position of cursor is restored after the changes."
1880 (let* ((origin (point-marker))
1881 (inlinetask-re (and (featurep 'org-inlinetask)
1882 (org-inlinetask-outline-regexp)))
1883 (item-re (org-item-re))
1884 (shift-body-ind
1885 (function
1886 ;; Shift the indentation between END and BEG by DELTA.
1887 ;; Start from the line before END.
1888 (lambda (end beg delta)
1889 (goto-char end)
1890 (skip-chars-backward " \r\t\n")
1891 (beginning-of-line)
1892 (while (or (> (point) beg)
1893 (and (= (point) beg)
1894 (not (looking-at item-re))))
1895 (cond
1896 ;; Skip inline tasks.
1897 ((and inlinetask-re (looking-at inlinetask-re))
1898 (org-inlinetask-goto-beginning))
1899 ;; Shift only non-empty lines.
1900 ((looking-at-p "^[ \t]*\\S-")
1901 (indent-line-to (+ (org-get-indentation) delta))))
1902 (forward-line -1)))))
1903 (modify-item
1904 (function
1905 ;; Replace ITEM first line elements with new elements from
1906 ;; STRUCT, if appropriate.
1907 (lambda (item)
1908 (goto-char item)
1909 (let* ((new-ind (org-list-get-ind item struct))
1910 (old-ind (org-get-indentation))
1911 (new-bul (org-list-bullet-string
1912 (org-list-get-bullet item struct)))
1913 (old-bul (org-list-get-bullet item old-struct))
1914 (new-box (org-list-get-checkbox item struct)))
1915 (looking-at org-list-full-item-re)
1916 ;; a. Replace bullet
1917 (unless (equal old-bul new-bul)
1918 (replace-match new-bul nil nil nil 1))
1919 ;; b. Replace checkbox.
1920 (cond
1921 ((equal (match-string 3) new-box))
1922 ((and (match-string 3) new-box)
1923 (replace-match new-box nil nil nil 3))
1924 ((match-string 3)
1925 (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
1926 (replace-match "" nil nil nil 1))
1927 (t (let ((counterp (match-end 2)))
1928 (goto-char (if counterp (1+ counterp) (match-end 1)))
1929 (insert (concat new-box (unless counterp " "))))))
1930 ;; c. Indent item to appropriate column.
1931 (unless (= new-ind old-ind)
1932 (delete-region (goto-char (point-at-bol))
1933 (progn (skip-chars-forward " \t") (point)))
1934 (indent-to new-ind)))))))
1935 ;; 1. First get list of items and position endings. We maintain
1936 ;; two alists: ITM-SHIFT, determining indentation shift needed
1937 ;; at item, and END-LIST, a pseudo-alist where key is ending
1938 ;; position and value point.
1939 (let (end-list acc-end itm-shift all-ends sliced-struct)
1940 (dolist (e old-struct)
1941 (let* ((pos (car e))
1942 (ind-pos (org-list-get-ind pos struct))
1943 (ind-old (org-list-get-ind pos old-struct))
1944 (bul-pos (org-list-get-bullet pos struct))
1945 (bul-old (org-list-get-bullet pos old-struct))
1946 (ind-shift (- (+ ind-pos (length bul-pos))
1947 (+ ind-old (length bul-old))))
1948 (end-pos (org-list-get-item-end pos old-struct)))
1949 (push (cons pos ind-shift) itm-shift)
1950 (unless (assq end-pos old-struct)
1951 ;; To determine real ind of an ending position that
1952 ;; is not at an item, we have to find the item it
1953 ;; belongs to: it is the last item (ITEM-UP), whose
1954 ;; ending is further than the position we're
1955 ;; interested in.
1956 (let ((item-up (assoc-default end-pos acc-end #'>)))
1957 (push (cons end-pos item-up) end-list)))
1958 (push (cons end-pos pos) acc-end)))
1959 ;; 2. Slice the items into parts that should be shifted by the
1960 ;; same amount of indentation. Each slice follow the pattern
1961 ;; (END BEG DELTA). Slices are returned in reverse order.
1962 (setq all-ends (sort (append (mapcar #'car itm-shift)
1963 (org-uniquify (mapcar #'car end-list)))
1964 #'<)
1965 acc-end (nreverse acc-end))
1966 (while (cdr all-ends)
1967 (let* ((up (pop all-ends))
1968 (down (car all-ends))
1969 (itemp (assq up struct))
1970 (delta
1971 (if itemp (cdr (assq up itm-shift))
1972 ;; If we're not at an item, there's a child of the
1973 ;; item point belongs to above. Make sure the less
1974 ;; indented line in this slice has the same column
1975 ;; as that child.
1976 (let* ((child (cdr (assq up acc-end)))
1977 (ind (org-list-get-ind child struct))
1978 (min-ind most-positive-fixnum))
1979 (save-excursion
1980 (goto-char up)
1981 (while (< (point) down)
1982 ;; Ignore empty lines. Also ignore blocks and
1983 ;; drawers contents.
1984 (unless (looking-at-p "[ \t]*$")
1985 (setq min-ind (min (org-get-indentation) min-ind))
1986 (cond
1987 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1988 (re-search-forward
1989 (format "^[ \t]*#\\+END%s[ \t]*$"
1990 (match-string 1))
1991 down t)))
1992 ((and (looking-at org-drawer-regexp)
1993 (re-search-forward "^[ \t]*:END:[ \t]*$"
1994 down t)))))
1995 (forward-line)))
1996 (- ind min-ind)))))
1997 (push (list down up delta) sliced-struct)))
1998 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1999 ;; end to beginning. Take a special action when beginning is
2000 ;; at item bullet.
2001 (dolist (e sliced-struct)
2002 (unless (zerop (nth 2 e)) (apply shift-body-ind e))
2003 (let* ((beg (nth 1 e))
2004 (cell (assq beg struct)))
2005 (unless (or (not cell) (equal cell (assq beg old-struct)))
2006 (funcall modify-item beg)))))
2007 ;; 4. Go back to initial position and clean marker.
2008 (goto-char origin)
2009 (move-marker origin nil)))
2011 (defun org-list-write-struct (struct parents &optional old-struct)
2012 "Correct bullets, checkboxes and indentation in list at point.
2014 STRUCT is the list structure. PARENTS is the alist of parents,
2015 as returned by `org-list-parents-alist'.
2017 When non-nil, optional argument OLD-STRUCT is the reference
2018 structure of the list. It should be provided whenever STRUCT
2019 doesn't correspond anymore to the real list in buffer."
2020 ;; Order of functions matters here: checkboxes and endings need
2021 ;; correct indentation to be set, and indentation needs correct
2022 ;; bullets.
2024 ;; 0. Save a copy of structure before modifications
2025 (let ((old-struct (or old-struct (copy-tree struct))))
2026 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
2027 ;; order to get items endings and bullets properly
2028 (org-list-struct-fix-ind struct parents 2)
2029 ;; 2. Fix each item end to get correct prevs alist.
2030 (org-list-struct-fix-item-end struct)
2031 ;; 3. Get bullets right.
2032 (let ((prevs (org-list-prevs-alist struct)))
2033 (org-list-struct-fix-bul struct prevs)
2034 ;; 4. Now get real indentation.
2035 (org-list-struct-fix-ind struct parents)
2036 ;; 5. Eventually fix checkboxes.
2037 (org-list-struct-fix-box struct parents prevs))
2038 ;; 6. Apply structure modifications to buffer.
2039 (org-list-struct-apply-struct struct old-struct)))
2043 ;;; Misc Tools
2045 (defun org-apply-on-list (function init-value &rest args)
2046 "Call FUNCTION on each item of the list at point.
2047 FUNCTION must be called with at least one argument: INIT-VALUE,
2048 that will contain the value returned by the function at the
2049 previous item, plus ARGS extra arguments.
2051 FUNCTION is applied on items in reverse order.
2053 As an example, \(org-apply-on-list \(lambda \(result) \(1+ result)) 0)
2054 will return the number of items in the current list.
2056 Sublists of the list are skipped. Cursor is always at the
2057 beginning of the item."
2058 (let* ((struct (org-list-struct))
2059 (prevs (org-list-prevs-alist struct))
2060 (item (copy-marker (point-at-bol)))
2061 (all (org-list-get-all-items (marker-position item) struct prevs))
2062 (value init-value))
2063 (mapc (lambda (e)
2064 (goto-char e)
2065 (setq value (apply function value args)))
2066 (nreverse all))
2067 (goto-char item)
2068 (move-marker item nil)
2069 value))
2071 (defun org-list-set-item-visibility (item struct view)
2072 "Set visibility of ITEM in STRUCT to VIEW.
2074 Possible values are: `folded', `children' or `subtree'. See
2075 `org-cycle' for more information."
2076 (cond
2077 ((eq view 'folded)
2078 (let ((item-end (org-list-get-item-end-before-blank item struct)))
2079 ;; Hide from eol
2080 (outline-flag-region (save-excursion (goto-char item) (point-at-eol))
2081 item-end t)))
2082 ((eq view 'children)
2083 ;; First show everything.
2084 (org-list-set-item-visibility item struct 'subtree)
2085 ;; Then fold every child.
2086 (let* ((parents (org-list-parents-alist struct))
2087 (children (org-list-get-children item struct parents)))
2088 (mapc (lambda (e)
2089 (org-list-set-item-visibility e struct 'folded))
2090 children)))
2091 ((eq view 'subtree)
2092 ;; Show everything
2093 (let ((item-end (org-list-get-item-end item struct)))
2094 (outline-flag-region item item-end nil)))))
2096 (defun org-list-item-body-column (item)
2097 "Return column at which body of ITEM should start."
2098 (save-excursion
2099 (goto-char item)
2100 (if (save-excursion
2101 (end-of-line)
2102 (re-search-backward
2103 "[ \t]::\\([ \t]\\|$\\)" (line-beginning-position) t))
2104 ;; Descriptive list item. Body starts after item's tag, if
2105 ;; possible.
2106 (let ((start (1+ (- (match-beginning 1) (line-beginning-position))))
2107 (ind (org-get-indentation)))
2108 (if (> start (+ ind org-list-description-max-indent))
2109 (+ ind 5)
2110 start))
2111 ;; Regular item. Body starts after bullet.
2112 (looking-at "[ \t]*\\(\\S-+\\)")
2113 (+ (progn (goto-char (match-end 1)) (current-column))
2114 (if (and org-list-two-spaces-after-bullet-regexp
2115 (string-match-p org-list-two-spaces-after-bullet-regexp
2116 (match-string 1)))
2118 1)))))
2122 ;;; Interactive functions
2124 (defalias 'org-list-get-item-begin 'org-in-item-p)
2126 (defun org-beginning-of-item ()
2127 "Go to the beginning of the current item.
2128 Throw an error when not in a list."
2129 (interactive)
2130 (let ((begin (org-in-item-p)))
2131 (if begin (goto-char begin) (error "Not in an item"))))
2133 (defun org-beginning-of-item-list ()
2134 "Go to the beginning item of the current list or sublist.
2135 Throw an error when not in a list."
2136 (interactive)
2137 (let ((begin (org-in-item-p)))
2138 (if (not begin)
2139 (error "Not in an item")
2140 (goto-char begin)
2141 (let* ((struct (org-list-struct))
2142 (prevs (org-list-prevs-alist struct)))
2143 (goto-char (org-list-get-list-begin begin struct prevs))))))
2145 (defun org-end-of-item-list ()
2146 "Go to the end of the current list or sublist.
2147 Throw an error when not in a list."
2148 (interactive)
2149 (let ((begin (org-in-item-p)))
2150 (if (not begin)
2151 (error "Not in an item")
2152 (goto-char begin)
2153 (let* ((struct (org-list-struct))
2154 (prevs (org-list-prevs-alist struct)))
2155 (goto-char (org-list-get-list-end begin struct prevs))))))
2157 (defun org-end-of-item ()
2158 "Go to the end of the current item.
2159 Throw an error when not in a list."
2160 (interactive)
2161 (let ((begin (org-in-item-p)))
2162 (if (not begin)
2163 (error "Not in an item")
2164 (goto-char begin)
2165 (let ((struct (org-list-struct)))
2166 (goto-char (org-list-get-item-end begin struct))))))
2168 (defun org-previous-item ()
2169 "Move to the beginning of the previous item.
2170 Throw an error when not in a list. Also throw an error when at
2171 first item, unless `org-list-use-circular-motion' is non-nil."
2172 (interactive)
2173 (let ((item (org-in-item-p)))
2174 (if (not item)
2175 (error "Not in an item")
2176 (goto-char item)
2177 (let* ((struct (org-list-struct))
2178 (prevs (org-list-prevs-alist struct))
2179 (prevp (org-list-get-prev-item item struct prevs)))
2180 (cond
2181 (prevp (goto-char prevp))
2182 (org-list-use-circular-motion
2183 (goto-char (org-list-get-last-item item struct prevs)))
2184 (t (error "On first item")))))))
2186 (defun org-next-item ()
2187 "Move to the beginning of the next item.
2188 Throw an error when not in a list. Also throw an error when at
2189 last item, unless `org-list-use-circular-motion' is non-nil."
2190 (interactive)
2191 (let ((item (org-in-item-p)))
2192 (if (not item)
2193 (error "Not in an item")
2194 (goto-char item)
2195 (let* ((struct (org-list-struct))
2196 (prevs (org-list-prevs-alist struct))
2197 (prevp (org-list-get-next-item item struct prevs)))
2198 (cond
2199 (prevp (goto-char prevp))
2200 (org-list-use-circular-motion
2201 (goto-char (org-list-get-first-item item struct prevs)))
2202 (t (error "On last item")))))))
2204 (defun org-move-item-down ()
2205 "Move the item at point down, i.e. swap with following item.
2206 Sub-items (items with larger indentation) are considered part of
2207 the item, so this really moves item trees."
2208 (interactive)
2209 (unless (org-at-item-p) (error "Not at an item"))
2210 (let* ((col (current-column))
2211 (item (point-at-bol))
2212 (struct (org-list-struct))
2213 (prevs (org-list-prevs-alist struct))
2214 (next-item (org-list-get-next-item (point-at-bol) struct prevs)))
2215 (unless (or next-item org-list-use-circular-motion)
2216 (user-error "Cannot move this item further down"))
2217 (if (not next-item)
2218 (setq struct (org-list-send-item item 'begin struct))
2219 (setq struct (org-list-swap-items item next-item struct))
2220 (goto-char
2221 (org-list-get-next-item item struct (org-list-prevs-alist struct))))
2222 (org-list-write-struct struct (org-list-parents-alist struct))
2223 (org-move-to-column col)))
2225 (defun org-move-item-up ()
2226 "Move the item at point up, i.e. swap with previous item.
2227 Sub-items (items with larger indentation) are considered part of
2228 the item, so this really moves item trees."
2229 (interactive)
2230 (unless (org-at-item-p) (error "Not at an item"))
2231 (let* ((col (current-column))
2232 (item (point-at-bol))
2233 (struct (org-list-struct))
2234 (prevs (org-list-prevs-alist struct))
2235 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs)))
2236 (unless (or prev-item org-list-use-circular-motion)
2237 (user-error "Cannot move this item further up"))
2238 (if (not prev-item)
2239 (setq struct (org-list-send-item item 'end struct))
2240 (setq struct (org-list-swap-items prev-item item struct)))
2241 (org-list-write-struct struct (org-list-parents-alist struct))
2242 (org-move-to-column col)))
2244 (defun org-insert-item (&optional checkbox)
2245 "Insert a new item at the current level.
2246 If cursor is before first character after bullet of the item, the
2247 new item will be created before the current one.
2249 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2251 Return t when things worked, nil when we are not in an item, or
2252 item is invisible."
2253 (let ((itemp (org-in-item-p))
2254 (pos (point)))
2255 ;; If cursor isn't is a list or if list is invisible, return nil.
2256 (unless (or (not itemp)
2257 (save-excursion
2258 (goto-char itemp)
2259 (org-invisible-p)))
2260 (if (save-excursion
2261 (goto-char itemp)
2262 (org-at-item-timer-p))
2263 ;; Timer list: delegate to `org-timer-item'.
2264 (progn (org-timer-item) t)
2265 (let* ((struct (save-excursion (goto-char itemp)
2266 (org-list-struct)))
2267 (prevs (org-list-prevs-alist struct))
2268 ;; If we're in a description list, ask for the new term.
2269 (desc (when (eq (org-list-get-list-type itemp struct prevs)
2270 'descriptive)
2271 " :: ")))
2272 (setq struct (org-list-insert-item pos struct prevs checkbox desc))
2273 (org-list-write-struct struct (org-list-parents-alist struct))
2274 (when checkbox (org-update-checkbox-count-maybe))
2275 (looking-at org-list-full-item-re)
2276 (goto-char (if (and (match-beginning 4)
2277 (save-match-data
2278 (string-match "[.)]" (match-string 1))))
2279 (match-beginning 4)
2280 (match-end 0)))
2281 (if desc (backward-char 1))
2282 t)))))
2284 (defun org-list-repair ()
2285 "Fix indentation, bullets and checkboxes in the list at point."
2286 (interactive)
2287 (unless (org-at-item-p) (error "This is not a list"))
2288 (let* ((struct (org-list-struct))
2289 (parents (org-list-parents-alist struct)))
2290 (org-list-write-struct struct parents)))
2292 (defun org-cycle-list-bullet (&optional which)
2293 "Cycle through the different itemize/enumerate bullets.
2294 This cycle the entire list level through the sequence:
2296 `-' -> `+' -> `*' -> `1.' -> `1)'
2298 If WHICH is a valid string, use that as the new bullet. If WHICH
2299 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2300 `previous', cycle backwards."
2301 (interactive "P")
2302 (unless (org-at-item-p) (error "Not at an item"))
2303 (save-excursion
2304 (beginning-of-line)
2305 (let* ((struct (org-list-struct))
2306 (parents (org-list-parents-alist struct))
2307 (prevs (org-list-prevs-alist struct))
2308 (list-beg (org-list-get-first-item (point) struct prevs))
2309 (bullet (org-list-get-bullet list-beg struct))
2310 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs))
2311 (case-fold-search nil)
2312 (current (cond
2313 ((string-match "[a-z]\\." bullet) "a.")
2314 ((string-match "[a-z])" bullet) "a)")
2315 ((string-match "[A-Z]\\." bullet) "A.")
2316 ((string-match "[A-Z])" bullet) "A)")
2317 ((string-match "\\." bullet) "1.")
2318 ((string-match ")" bullet) "1)")
2319 (t (org-trim bullet))))
2320 ;; Compute list of possible bullets, depending on context.
2321 (bullet-list
2322 (append '("-" "+" )
2323 ;; *-bullets are not allowed at column 0.
2324 (unless (looking-at "\\S-") '("*"))
2325 ;; Description items cannot be numbered.
2326 (unless (or (eq org-plain-list-ordered-item-terminator ?\))
2327 (org-at-item-description-p))
2328 '("1."))
2329 (unless (or (eq org-plain-list-ordered-item-terminator ?.)
2330 (org-at-item-description-p))
2331 '("1)"))
2332 (unless (or (not alpha-p)
2333 (eq org-plain-list-ordered-item-terminator ?\))
2334 (org-at-item-description-p))
2335 '("a." "A."))
2336 (unless (or (not alpha-p)
2337 (eq org-plain-list-ordered-item-terminator ?.)
2338 (org-at-item-description-p))
2339 '("a)" "A)"))))
2340 (len (length bullet-list))
2341 (item-index (- len (length (member current bullet-list))))
2342 (get-value (lambda (index) (nth (mod index len) bullet-list)))
2343 (new (cond
2344 ((member which bullet-list) which)
2345 ((numberp which) (funcall get-value which))
2346 ((eq 'previous which) (funcall get-value (1- item-index)))
2347 (t (funcall get-value (1+ item-index))))))
2348 ;; Use a short variation of `org-list-write-struct' as there's
2349 ;; no need to go through all the steps.
2350 (let ((old-struct (copy-tree struct)))
2351 (org-list-set-bullet list-beg struct (org-list-bullet-string new))
2352 (org-list-struct-fix-bul struct prevs)
2353 (org-list-struct-fix-ind struct parents)
2354 (org-list-struct-apply-struct struct old-struct)))))
2356 (defun org-toggle-checkbox (&optional toggle-presence)
2357 "Toggle the checkbox in the current line.
2358 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2359 double prefix, set checkbox to [-].
2361 When there is an active region, toggle status or presence of the
2362 first checkbox there, and make every item inside have the same
2363 status or presence, respectively.
2365 If the cursor is in a headline, apply this to all checkbox items
2366 in the text below the heading, taking as reference the first item
2367 in subtree, ignoring drawers."
2368 (interactive "P")
2369 (save-excursion
2370 (let* (singlep
2371 block-item
2372 lim-up
2373 lim-down
2374 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2375 "\\|" org-deadline-string
2376 "\\|" org-closed-string
2377 "\\|" org-clock-string "\\)"
2378 " *[[<]\\([^]>]+\\)[]>]"))
2379 (orderedp (org-entry-get nil "ORDERED"))
2380 (_bounds
2381 ;; In a region, start at first item in region.
2382 (cond
2383 ((org-region-active-p)
2384 (let ((limit (region-end)))
2385 (goto-char (region-beginning))
2386 (if (org-list-search-forward (org-item-beginning-re) limit t)
2387 (setq lim-up (point-at-bol))
2388 (error "No item in region"))
2389 (setq lim-down (copy-marker limit))))
2390 ((org-at-heading-p)
2391 ;; On an heading, start at first item after drawers and
2392 ;; time-stamps (scheduled, etc.).
2393 (let ((limit (save-excursion (outline-next-heading) (point))))
2394 (forward-line 1)
2395 (while (or (looking-at org-drawer-regexp)
2396 (looking-at keyword-re))
2397 (if (looking-at keyword-re)
2398 (forward-line 1)
2399 (re-search-forward "^[ \t]*:END:" limit nil)))
2400 (if (org-list-search-forward (org-item-beginning-re) limit t)
2401 (setq lim-up (point-at-bol))
2402 (error "No item in subtree"))
2403 (setq lim-down (copy-marker limit))))
2404 ;; Just one item: set SINGLEP flag.
2405 ((org-at-item-p)
2406 (setq singlep t)
2407 (setq lim-up (point-at-bol)
2408 lim-down (copy-marker (point-at-eol))))
2409 (t (error "Not at an item or heading, and no active region"))))
2410 ;; Determine the checkbox going to be applied to all items
2411 ;; within bounds.
2412 (ref-checkbox
2413 (progn
2414 (goto-char lim-up)
2415 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2416 (cond
2417 ((equal toggle-presence '(16)) "[-]")
2418 ((equal toggle-presence '(4))
2419 (unless cbox "[ ]"))
2420 ((equal "[X]" cbox) "[ ]")
2421 (t "[X]"))))))
2422 ;; When an item is found within bounds, grab the full list at
2423 ;; point structure, then: (1) set check-box of all its items
2424 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2425 ;; whole list, (3) move point after the list.
2426 (goto-char lim-up)
2427 (while (and (< (point) lim-down)
2428 (org-list-search-forward (org-item-beginning-re)
2429 lim-down 'move))
2430 (let* ((struct (org-list-struct))
2431 (struct-copy (copy-tree struct))
2432 (parents (org-list-parents-alist struct))
2433 (prevs (org-list-prevs-alist struct))
2434 (bottom (copy-marker (org-list-get-bottom-point struct)))
2435 (items-to-toggle (cl-remove-if
2436 (lambda (e) (or (< e lim-up) (> e lim-down)))
2437 (mapcar #'car struct))))
2438 (mapc (lambda (e) (org-list-set-checkbox
2439 e struct
2440 ;; If there is no box at item, leave as-is
2441 ;; unless function was called with C-u prefix.
2442 (let ((cur-box (org-list-get-checkbox e struct)))
2443 (if (or cur-box (equal toggle-presence '(4)))
2444 ref-checkbox
2445 cur-box))))
2446 items-to-toggle)
2447 (setq block-item (org-list-struct-fix-box
2448 struct parents prevs orderedp))
2449 ;; Report some problems due to ORDERED status of subtree.
2450 ;; If only one box was being checked, throw an error, else,
2451 ;; only signal problems.
2452 (cond
2453 ((and singlep block-item (> lim-up block-item))
2454 (error
2455 "Checkbox blocked because of unchecked box at line %d"
2456 (org-current-line block-item)))
2457 (block-item
2458 (message
2459 "Checkboxes were removed due to unchecked box at line %d"
2460 (org-current-line block-item))))
2461 (goto-char bottom)
2462 (move-marker bottom nil)
2463 (org-list-struct-apply-struct struct struct-copy)))
2464 (move-marker lim-down nil)))
2465 (org-update-checkbox-count-maybe))
2467 (defun org-reset-checkbox-state-subtree ()
2468 "Reset all checkboxes in an entry subtree."
2469 (interactive "*")
2470 (if (org-before-first-heading-p)
2471 (error "Not inside a tree")
2472 (save-restriction
2473 (save-excursion
2474 (org-narrow-to-subtree)
2475 (org-show-subtree)
2476 (goto-char (point-min))
2477 (let ((end (point-max)))
2478 (while (< (point) end)
2479 (when (org-at-item-checkbox-p)
2480 (replace-match "[ ]" t t nil 1))
2481 (beginning-of-line 2)))
2482 (org-update-checkbox-count-maybe 'all)))))
2484 (defun org-update-checkbox-count (&optional all)
2485 "Update the checkbox statistics in the current section.
2487 This will find all statistic cookies like [57%] and [6/12] and
2488 update them with the current numbers.
2490 With optional prefix argument ALL, do this for the whole buffer."
2491 (interactive "P")
2492 (org-with-wide-buffer
2493 (let* ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2494 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\
2495 \\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2496 (recursivep
2497 (or (not org-checkbox-hierarchical-statistics)
2498 (string-match "\\<recursive\\>"
2499 (or (org-entry-get nil "COOKIE_DATA") ""))))
2500 (within-inlinetask (and (not all)
2501 (featurep 'org-inlinetask)
2502 (org-inlinetask-in-task-p)))
2503 (end (cond (all (point-max))
2504 (within-inlinetask
2505 (save-excursion (outline-next-heading) (point)))
2506 (t (save-excursion
2507 (org-with-limited-levels (outline-next-heading))
2508 (point)))))
2509 (count-boxes
2510 (lambda (item structs recursivep)
2511 ;; Return number of checked boxes and boxes of all types
2512 ;; in all structures in STRUCTS. If RECURSIVEP is
2513 ;; non-nil, also count boxes in sub-lists. If ITEM is
2514 ;; nil, count across the whole structure, else count only
2515 ;; across subtree whose ancestor is ITEM.
2516 (let ((c-on 0) (c-all 0))
2517 (dolist (s structs (list c-on c-all))
2518 (let* ((pre (org-list-prevs-alist s))
2519 (par (org-list-parents-alist s))
2520 (items
2521 (cond
2522 ((and recursivep item) (org-list-get-subtree item s))
2523 (recursivep (mapcar #'car s))
2524 (item (org-list-get-children item s par))
2525 (t (org-list-get-all-items
2526 (org-list-get-top-point s) s pre))))
2527 (cookies (delq nil (mapcar
2528 (lambda (e)
2529 (org-list-get-checkbox e s))
2530 items))))
2531 (cl-incf c-all (length cookies))
2532 (cl-incf c-on (cl-count "[X]" cookies :test #'equal)))))))
2533 cookies-list cache)
2534 ;; Move to start.
2535 (cond (all (goto-char (point-min)))
2536 (within-inlinetask (org-back-to-heading t))
2537 (t (org-with-limited-levels (outline-previous-heading))))
2538 ;; Build an alist for each cookie found. The key is the position
2539 ;; at beginning of cookie and values ending position, format of
2540 ;; cookie, number of checked boxes to report and total number of
2541 ;; boxes.
2542 (while (re-search-forward cookie-re end t)
2543 (let ((context (save-excursion (backward-char)
2544 (save-match-data (org-element-context)))))
2545 (when (eq (org-element-type context) 'statistics-cookie)
2546 (push
2547 (append
2548 (list (match-beginning 1) (match-end 1) (match-end 2))
2549 (let* ((container
2550 (org-element-lineage
2551 context
2552 '(drawer center-block dynamic-block inlinetask item
2553 quote-block special-block verse-block)))
2554 (beg (if container
2555 (org-element-property :contents-begin container)
2556 (save-excursion
2557 (org-with-limited-levels
2558 (outline-previous-heading))
2559 (point)))))
2560 (or (cdr (assq beg cache))
2561 (save-excursion
2562 (goto-char beg)
2563 (let ((end
2564 (if container
2565 (org-element-property :contents-end container)
2566 (save-excursion
2567 (org-with-limited-levels (outline-next-heading))
2568 (point))))
2569 structs)
2570 (while (re-search-forward box-re end t)
2571 (let ((element (org-element-at-point)))
2572 (when (eq (org-element-type element) 'item)
2573 (push (org-element-property :structure element)
2574 structs)
2575 ;; Skip whole list since we have its
2576 ;; structure anyway.
2577 (while (setq element (org-element-lineage
2578 element '(plain-list)))
2579 (goto-char
2580 (min (org-element-property :end element)
2581 end))))))
2582 ;; Cache count for cookies applying to the same
2583 ;; area. Then return it.
2584 (let ((count
2585 (funcall count-boxes
2586 (and (eq (org-element-type container)
2587 'item)
2588 (org-element-property
2589 :begin container))
2590 structs
2591 recursivep)))
2592 (push (cons beg count) cache)
2593 count))))))
2594 cookies-list))))
2595 ;; Apply alist to buffer.
2596 (dolist (cookie cookies-list)
2597 (let* ((beg (car cookie))
2598 (end (nth 1 cookie))
2599 (percent (nth 2 cookie))
2600 (checked (nth 3 cookie))
2601 (total (nth 4 cookie)))
2602 (goto-char beg)
2603 (insert
2604 (if percent (format "[%d%%]" (floor (* 100.0 checked)
2605 (max 1 total)))
2606 (format "[%d/%d]" checked total)))
2607 (delete-region (point) (+ (point) (- end beg)))
2608 (when org-auto-align-tags (org-fix-tags-on-the-fly)))))))
2610 (defun org-get-checkbox-statistics-face ()
2611 "Select the face for checkbox statistics.
2612 The face will be `org-done' when all relevant boxes are checked.
2613 Otherwise it will be `org-todo'."
2614 (if (match-end 1)
2615 (if (equal (match-string 1) "100%")
2616 'org-checkbox-statistics-done
2617 'org-checkbox-statistics-todo)
2618 (if (and (> (match-end 2) (match-beginning 2))
2619 (equal (match-string 2) (match-string 3)))
2620 'org-checkbox-statistics-done
2621 'org-checkbox-statistics-todo)))
2623 (defun org-update-checkbox-count-maybe (&optional all)
2624 "Update checkbox statistics unless turned off by user.
2625 With an optional argument ALL, update them in the whole buffer."
2626 (when (cdr (assq 'checkbox org-list-automatic-rules))
2627 (org-update-checkbox-count all))
2628 (run-hooks 'org-checkbox-statistics-hook))
2630 (defvar org-last-indent-begin-marker (make-marker))
2631 (defvar org-last-indent-end-marker (make-marker))
2632 (defun org-list-indent-item-generic (arg no-subtree struct)
2633 "Indent a local list item including its children.
2634 When number ARG is a negative, item will be outdented, otherwise
2635 it will be indented.
2637 If a region is active, all items inside will be moved.
2639 If NO-SUBTREE is non-nil, only indent the item itself, not its
2640 children.
2642 STRUCT is the list structure.
2644 Return t if successful."
2645 (save-excursion
2646 (let* ((regionp (org-region-active-p))
2647 (rbeg (and regionp (region-beginning)))
2648 (rend (and regionp (region-end)))
2649 (top (org-list-get-top-point struct))
2650 (parents (org-list-parents-alist struct))
2651 (prevs (org-list-prevs-alist struct))
2652 ;; Are we going to move the whole list?
2653 (specialp
2654 (and (not regionp)
2655 (= top (point-at-bol))
2656 (cdr (assq 'indent org-list-automatic-rules))
2657 (if no-subtree
2658 (error
2659 "First item of list cannot move without its subtree")
2660 t))))
2661 ;; Determine begin and end points of zone to indent. If moving
2662 ;; more than one item, save them for subsequent moves.
2663 (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
2664 (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
2665 (if regionp
2666 (progn
2667 (set-marker org-last-indent-begin-marker rbeg)
2668 (set-marker org-last-indent-end-marker rend))
2669 (set-marker org-last-indent-begin-marker (point-at-bol))
2670 (set-marker org-last-indent-end-marker
2671 (cond
2672 (specialp (org-list-get-bottom-point struct))
2673 (no-subtree (1+ (point-at-bol)))
2674 (t (org-list-get-item-end (point-at-bol) struct))))))
2675 (let* ((beg (marker-position org-last-indent-begin-marker))
2676 (end (marker-position org-last-indent-end-marker)))
2677 (cond
2678 ;; Special case: moving top-item with indent rule.
2679 (specialp
2680 (let* ((level-skip (org-level-increment))
2681 (offset (if (< arg 0) (- level-skip) level-skip))
2682 (top-ind (org-list-get-ind beg struct))
2683 (old-struct (copy-tree struct)))
2684 (if (< (+ top-ind offset) 0)
2685 (error "Cannot outdent beyond margin")
2686 ;; Change bullet if necessary.
2687 (when (and (= (+ top-ind offset) 0)
2688 (string-match "*"
2689 (org-list-get-bullet beg struct)))
2690 (org-list-set-bullet beg struct
2691 (org-list-bullet-string "-")))
2692 ;; Shift every item by OFFSET and fix bullets. Then
2693 ;; apply changes to buffer.
2694 (mapc (lambda (e)
2695 (let ((ind (org-list-get-ind (car e) struct)))
2696 (org-list-set-ind (car e) struct (+ ind offset))))
2697 struct)
2698 (org-list-struct-fix-bul struct prevs)
2699 (org-list-struct-apply-struct struct old-struct))))
2700 ;; Forbidden move:
2701 ((and (< arg 0)
2702 ;; If only one item is moved, it mustn't have a child.
2703 (or (and no-subtree
2704 (not regionp)
2705 (org-list-has-child-p beg struct))
2706 ;; If a subtree or region is moved, the last item
2707 ;; of the subtree mustn't have a child.
2708 (let ((last-item (caar
2709 (reverse
2710 (cl-remove-if
2711 (lambda (e) (>= (car e) end))
2712 struct)))))
2713 (org-list-has-child-p last-item struct))))
2714 (error "Cannot outdent an item without its children"))
2715 ;; Normal shifting
2717 (let* ((new-parents
2718 (if (< arg 0)
2719 (org-list-struct-outdent beg end struct parents)
2720 (org-list-struct-indent beg end struct parents prevs))))
2721 (org-list-write-struct struct new-parents))
2722 (org-update-checkbox-count-maybe))))))
2725 (defun org-outdent-item ()
2726 "Outdent a local list item, but not its children.
2727 If a region is active, all items inside will be moved."
2728 (interactive)
2729 (let ((regionp (org-region-active-p)))
2730 (cond
2731 ((or (org-at-item-p)
2732 (and regionp
2733 (save-excursion (goto-char (region-beginning))
2734 (org-at-item-p))))
2735 (let ((struct (if (not regionp) (org-list-struct)
2736 (save-excursion (goto-char (region-beginning))
2737 (org-list-struct)))))
2738 (org-list-indent-item-generic -1 t struct)))
2739 (regionp (error "Region not starting at an item"))
2740 (t (error "Not at an item")))))
2742 (defun org-indent-item ()
2743 "Indent a local list item, but not its children.
2744 If a region is active, all items inside will be moved."
2745 (interactive)
2746 (let ((regionp (org-region-active-p)))
2747 (cond
2748 ((or (org-at-item-p)
2749 (and regionp
2750 (save-excursion (goto-char (region-beginning))
2751 (org-at-item-p))))
2752 (let ((struct (if (not regionp) (org-list-struct)
2753 (save-excursion (goto-char (region-beginning))
2754 (org-list-struct)))))
2755 (org-list-indent-item-generic 1 t struct)))
2756 (regionp (error "Region not starting at an item"))
2757 (t (error "Not at an item")))))
2759 (defun org-outdent-item-tree ()
2760 "Outdent a local list item including its children.
2761 If a region is active, all items inside will be moved."
2762 (interactive)
2763 (let ((regionp (org-region-active-p)))
2764 (cond
2765 ((or (org-at-item-p)
2766 (and regionp
2767 (save-excursion (goto-char (region-beginning))
2768 (org-at-item-p))))
2769 (let ((struct (if (not regionp) (org-list-struct)
2770 (save-excursion (goto-char (region-beginning))
2771 (org-list-struct)))))
2772 (org-list-indent-item-generic -1 nil struct)))
2773 (regionp (error "Region not starting at an item"))
2774 (t (error "Not at an item")))))
2776 (defun org-indent-item-tree ()
2777 "Indent a local list item including its children.
2778 If a region is active, all items inside will be moved."
2779 (interactive)
2780 (let ((regionp (org-region-active-p)))
2781 (cond
2782 ((or (org-at-item-p)
2783 (and regionp
2784 (save-excursion (goto-char (region-beginning))
2785 (org-at-item-p))))
2786 (let ((struct (if (not regionp) (org-list-struct)
2787 (save-excursion (goto-char (region-beginning))
2788 (org-list-struct)))))
2789 (org-list-indent-item-generic 1 nil struct)))
2790 (regionp (error "Region not starting at an item"))
2791 (t (error "Not at an item")))))
2793 (defvar org-tab-ind-state)
2794 (defvar org-adapt-indentation)
2795 (defun org-cycle-item-indentation ()
2796 "Cycle levels of indentation of an empty item.
2797 The first run indents the item, if applicable. Subsequent runs
2798 outdent it at meaningful levels in the list. When done, item is
2799 put back at its original position with its original bullet.
2801 Return t at each successful move."
2802 (when (org-at-item-p)
2803 (let* ((org-adapt-indentation nil)
2804 (struct (org-list-struct))
2805 (ind (org-list-get-ind (point-at-bol) struct))
2806 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2807 ;; Accept empty items or if cycle has already started.
2808 (when (or (eq last-command 'org-cycle-item-indentation)
2809 (and (save-excursion
2810 (beginning-of-line)
2811 (looking-at org-list-full-item-re))
2812 (>= (match-end 0) (save-excursion
2813 (goto-char (org-list-get-item-end
2814 (point-at-bol) struct))
2815 (skip-chars-backward " \r\t\n")
2816 (point)))))
2817 (setq this-command 'org-cycle-item-indentation)
2818 ;; When in the middle of the cycle, try to outdent first. If
2819 ;; it fails, and point is still at initial position, indent.
2820 ;; Else, re-create it at its original position.
2821 (if (eq last-command 'org-cycle-item-indentation)
2822 (cond
2823 ((ignore-errors (org-list-indent-item-generic -1 t struct)))
2824 ((and (= ind (car org-tab-ind-state))
2825 (ignore-errors (org-list-indent-item-generic 1 t struct))))
2826 (t (delete-region (point-at-bol) (point-at-eol))
2827 (indent-to-column (car org-tab-ind-state))
2828 (insert (cdr org-tab-ind-state) " ")
2829 ;; Break cycle
2830 (setq this-command 'identity)))
2831 ;; If a cycle is starting, remember indentation and bullet,
2832 ;; then try to indent. If it fails, try to outdent.
2833 (setq org-tab-ind-state (cons ind bullet))
2834 (cond
2835 ((ignore-errors (org-list-indent-item-generic 1 t struct)))
2836 ((ignore-errors (org-list-indent-item-generic -1 t struct)))
2837 (t (user-error "Cannot move item"))))
2838 t))))
2840 (defun org-sort-list
2841 (&optional with-case sorting-type getkey-func compare-func interactive?)
2842 "Sort list items.
2843 The cursor may be at any item of the list that should be sorted.
2844 Sublists are not sorted. Checkboxes, if any, are ignored.
2846 Sorting can be alphabetically, numerically, by date/time as given
2847 by a time stamp, by a property or by priority.
2849 Comparing entries ignores case by default. However, with an
2850 optional argument WITH-CASE, the sorting considers case as well.
2852 The command prompts for the sorting type unless it has been given
2853 to the function through the SORTING-TYPE argument, which needs to
2854 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F ?x ?X). Here is the
2855 detailed meaning of each character:
2857 n Numerically, by converting the beginning of the item to a number.
2858 a Alphabetically. Only the first line of item is checked.
2859 t By date/time, either the first active time stamp in the entry, if
2860 any, or by the first inactive one. In a timer list, sort the timers.
2861 x By \"checked\" status of a check list.
2863 Capital letters will reverse the sort order.
2865 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2866 a function to be called with point at the beginning of the
2867 record. It must return a value that is compatible with COMPARE-FUNC,
2868 the function used to compare entries.
2870 Sorting is done against the visible part of the headlines, it
2871 ignores hidden links.
2873 A non-nil value for INTERACTIVE? is used to signal that this
2874 function is being called interactively."
2875 (interactive (list current-prefix-arg nil nil nil t))
2876 (let* ((case-func (if with-case 'identity 'downcase))
2877 (struct (org-list-struct))
2878 (prevs (org-list-prevs-alist struct))
2879 (start (org-list-get-list-begin (point-at-bol) struct prevs))
2880 (end (org-list-get-list-end (point-at-bol) struct prevs))
2881 (sorting-type
2882 (or sorting-type
2883 (progn
2884 (message
2885 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc [x]checked A/N/T/F/X means reversed:")
2886 (read-char-exclusive))))
2887 (dcst (downcase sorting-type))
2888 (getkey-func
2889 (and (= dcst ?f)
2890 (or getkey-func
2891 (and interactive?
2892 (org-read-function "Function for extracting keys: "))
2893 (error "Missing key extractor"))))
2894 (sort-func
2895 (cond
2896 ((= dcst ?a) #'string<)
2897 ((= dcst ?f)
2898 (or compare-func
2899 (and interactive?
2900 (org-read-function
2901 (concat "Function for comparing keys "
2902 "(empty for default `sort-subr' predicate): ")
2903 'allow-empty))))
2904 ((= dcst ?t) #'<)
2905 ((= dcst ?x) #'string<))))
2906 (message "Sorting items...")
2907 (save-restriction
2908 (narrow-to-region start end)
2909 (goto-char (point-min))
2910 (let* ((case-fold-search nil)
2911 (now (current-time))
2912 (next-record (lambda ()
2913 (skip-chars-forward " \r\t\n")
2914 (or (eobp) (beginning-of-line))))
2915 (end-record (lambda ()
2916 (goto-char (org-list-get-item-end-before-blank
2917 (point) struct))))
2918 (value-to-sort
2919 (lambda ()
2920 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2921 (cond
2922 ((= dcst ?n)
2923 (string-to-number
2924 (org-sort-remove-invisible
2925 (buffer-substring (match-end 0) (point-at-eol)))))
2926 ((= dcst ?a)
2927 (funcall case-func
2928 (org-sort-remove-invisible
2929 (buffer-substring
2930 (match-end 0) (point-at-eol)))))
2931 ((= dcst ?t)
2932 (cond
2933 ;; If it is a timer list, convert timer to seconds
2934 ((org-at-item-timer-p)
2935 (org-timer-hms-to-secs (match-string 1)))
2936 ((or (save-excursion
2937 (re-search-forward org-ts-regexp (point-at-eol) t))
2938 (save-excursion (re-search-forward org-ts-regexp-both
2939 (point-at-eol) t)))
2940 (org-time-string-to-seconds (match-string 0)))
2941 (t (float-time now))))
2942 ((= dcst ?x) (or (and (stringp (match-string 1))
2943 (match-string 1))
2944 ""))
2945 ((= dcst ?f)
2946 (if getkey-func
2947 (let ((value (funcall getkey-func)))
2948 (if (stringp value)
2949 (funcall case-func value)
2950 value))
2951 (error "Invalid key function `%s'" getkey-func)))
2952 (t (error "Invalid sorting type `%c'" sorting-type)))))))
2953 (sort-subr (/= dcst sorting-type)
2954 next-record
2955 end-record
2956 value-to-sort
2958 sort-func)
2959 ;; Read and fix list again, as `sort-subr' probably destroyed
2960 ;; its structure.
2961 (org-list-repair)
2962 (run-hooks 'org-after-sorting-entries-or-items-hook)
2963 (message "Sorting items...done")))))
2965 (defun org-toggle-item (arg)
2966 "Convert headings or normal lines to items, items to normal lines.
2967 If there is no active region, only the current line is considered.
2969 If the first non blank line in the region is a headline, convert
2970 all headlines to items, shifting text accordingly.
2972 If it is an item, convert all items to normal lines.
2974 If it is normal text, change region into a list of items.
2975 With a prefix argument ARG, change the region in a single item."
2976 (interactive "P")
2977 (let ((shift-text
2978 (lambda (ind end)
2979 ;; Shift text in current section to IND, from point to END.
2980 ;; The function leaves point to END line.
2981 (let ((min-i 1000) (end (copy-marker end)))
2982 ;; First determine the minimum indentation (MIN-I) of
2983 ;; the text.
2984 (save-excursion
2985 (catch 'exit
2986 (while (< (point) end)
2987 (let ((i (org-get-indentation)))
2988 (cond
2989 ;; Skip blank lines and inline tasks.
2990 ((looking-at "^[ \t]*$"))
2991 ((looking-at org-outline-regexp-bol))
2992 ;; We can't find less than 0 indentation.
2993 ((zerop i) (throw 'exit (setq min-i 0)))
2994 ((< i min-i) (setq min-i i))))
2995 (forward-line))))
2996 ;; Then indent each line so that a line indented to
2997 ;; MIN-I becomes indented to IND. Ignore blank lines
2998 ;; and inline tasks in the process.
2999 (let ((delta (- ind min-i)))
3000 (while (< (point) end)
3001 (unless (or (looking-at "^[ \t]*$")
3002 (looking-at org-outline-regexp-bol))
3003 (indent-line-to (+ (org-get-indentation) delta)))
3004 (forward-line))))))
3005 (skip-blanks
3006 (lambda (pos)
3007 ;; Return beginning of first non-blank line, starting from
3008 ;; line at POS.
3009 (save-excursion
3010 (goto-char pos)
3011 (skip-chars-forward " \r\t\n")
3012 (point-at-bol))))
3013 beg end)
3014 ;; Determine boundaries of changes.
3015 (if (org-region-active-p)
3016 (setq beg (funcall skip-blanks (region-beginning))
3017 end (copy-marker (region-end)))
3018 (setq beg (funcall skip-blanks (point-at-bol))
3019 end (copy-marker (point-at-eol))))
3020 ;; Depending on the starting line, choose an action on the text
3021 ;; between BEG and END.
3022 (org-with-limited-levels
3023 (save-excursion
3024 (goto-char beg)
3025 (cond
3026 ;; Case 1. Start at an item: de-itemize. Note that it only
3027 ;; happens when a region is active: `org-ctrl-c-minus'
3028 ;; would call `org-cycle-list-bullet' otherwise.
3029 ((org-at-item-p)
3030 (while (< (point) end)
3031 (when (org-at-item-p)
3032 (skip-chars-forward " \t")
3033 (delete-region (point) (match-end 0)))
3034 (forward-line)))
3035 ;; Case 2. Start at an heading: convert to items.
3036 ((org-at-heading-p)
3037 (let* ((bul (org-list-bullet-string "-"))
3038 (bul-len (length bul))
3039 ;; Indentation of the first heading. It should be
3040 ;; relative to the indentation of its parent, if any.
3041 (start-ind (save-excursion
3042 (cond
3043 ((not org-adapt-indentation) 0)
3044 ((not (outline-previous-heading)) 0)
3045 (t (length (match-string 0))))))
3046 ;; Level of first heading. Further headings will be
3047 ;; compared to it to determine hierarchy in the list.
3048 (ref-level (org-reduced-level (org-outline-level))))
3049 (while (< (point) end)
3050 (let* ((level (org-reduced-level (org-outline-level)))
3051 (delta (max 0 (- level ref-level)))
3052 (todo-state (org-get-todo-state)))
3053 ;; If current headline is less indented than the first
3054 ;; one, set it as reference, in order to preserve
3055 ;; subtrees.
3056 (when (< level ref-level) (setq ref-level level))
3057 ;; Remove stars and TODO keyword.
3058 (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
3059 (delete-region (point) (or (match-beginning 3)
3060 (line-end-position)))
3061 (insert bul)
3062 (indent-line-to (+ start-ind (* delta bul-len)))
3063 ;; Turn TODO keyword into a check box.
3064 (when todo-state
3065 (let* ((struct (org-list-struct))
3066 (old (copy-tree struct)))
3067 (org-list-set-checkbox
3068 (line-beginning-position)
3069 struct
3070 (if (member todo-state org-done-keywords)
3071 "[X]"
3072 "[ ]"))
3073 (org-list-write-struct struct
3074 (org-list-parents-alist struct)
3075 old)))
3076 ;; Ensure all text down to END (or SECTION-END) belongs
3077 ;; to the newly created item.
3078 (let ((section-end (save-excursion
3079 (or (outline-next-heading) (point)))))
3080 (forward-line)
3081 (funcall shift-text
3082 (+ start-ind (* (1+ delta) bul-len))
3083 (min end section-end)))))))
3084 ;; Case 3. Normal line with ARG: make the first line of region
3085 ;; an item, and shift indentation of others lines to
3086 ;; set them as item's body.
3087 (arg (let* ((bul (org-list-bullet-string "-"))
3088 (bul-len (length bul))
3089 (ref-ind (org-get-indentation)))
3090 (skip-chars-forward " \t")
3091 (insert bul)
3092 (forward-line)
3093 (while (< (point) end)
3094 ;; Ensure that lines less indented than first one
3095 ;; still get included in item body.
3096 (funcall shift-text
3097 (+ ref-ind bul-len)
3098 (min end (save-excursion (or (outline-next-heading)
3099 (point)))))
3100 (forward-line))))
3101 ;; Case 4. Normal line without ARG: turn each non-item line
3102 ;; into an item.
3104 (while (< (point) end)
3105 (unless (or (org-at-heading-p) (org-at-item-p))
3106 (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
3107 (replace-match
3108 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
3109 (forward-line))))))))
3112 ;;; Send and receive lists
3114 (defun org-list-to-lisp (&optional delete)
3115 "Parse the list at point and maybe DELETE it.
3117 Return a list whose car is a symbol of list type, among
3118 `ordered', `unordered' and `descriptive'. Then, each item is
3119 a list of strings and other sub-lists.
3121 For example, the following list:
3123 1. first item
3124 + sub-item one
3125 + [X] sub-item two
3126 more text in first item
3127 2. [@3] last item
3129 is parsed as
3131 (ordered
3132 (\"first item\"
3133 (unordered
3134 (\"sub-item one\")
3135 (\"[X] sub-item two\"))
3136 \"more text in first item\")
3137 (\"[@3] last item\"))
3139 Point is left at list's end."
3140 (letrec ((struct (org-list-struct))
3141 (prevs (org-list-prevs-alist struct))
3142 (parents (org-list-parents-alist struct))
3143 (top (org-list-get-top-point struct))
3144 (bottom (org-list-get-bottom-point struct))
3145 (trim
3146 (lambda (text)
3147 ;; Remove indentation and final newline from TEXT.
3148 (org-remove-indentation
3149 (if (string-match-p "\n\\'" text)
3150 (substring text 0 -1)
3151 text))))
3152 (parse-sublist
3153 (lambda (e)
3154 ;; Return a list whose car is list type and cdr a list
3155 ;; of items' body.
3156 (cons (org-list-get-list-type (car e) struct prevs)
3157 (mapcar parse-item e))))
3158 (parse-item
3159 (lambda (e)
3160 ;; Return a list containing counter of item, if any,
3161 ;; text and any sublist inside it.
3162 (let* ((end (org-list-get-item-end e struct))
3163 (children (org-list-get-children e struct parents))
3164 (body
3165 (save-excursion
3166 (goto-char e)
3167 (looking-at "[ \t]*\\S-+[ \t]*")
3168 (list
3169 (funcall
3170 trim
3171 (concat
3172 (make-string (string-width (match-string 0)) ?\s)
3173 (buffer-substring-no-properties
3174 (match-end 0) (or (car children) end))))))))
3175 (while children
3176 (let* ((child (car children))
3177 (sub (org-list-get-all-items child struct prevs))
3178 (last-in-sub (car (last sub))))
3179 (push (funcall parse-sublist sub) body)
3180 ;; Remove whole sub-list from children.
3181 (setq children (cdr (memq last-in-sub children)))
3182 ;; There is a chunk of text belonging to the item
3183 ;; if last child doesn't end where next child
3184 ;; starts or where item ends.
3185 (let ((sub-end (org-list-get-item-end last-in-sub struct))
3186 (next (or (car children) end)))
3187 (when (/= sub-end next)
3188 (push (funcall
3189 trim
3190 (buffer-substring-no-properties sub-end next))
3191 body)))))
3192 (nreverse body)))))
3193 ;; Store output, take care of cursor position and deletion of
3194 ;; list, then return output.
3195 (prog1 (funcall parse-sublist (org-list-get-all-items top struct prevs))
3196 (goto-char top)
3197 (when delete
3198 (delete-region top bottom)
3199 (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re))
3200 (replace-match ""))))))
3202 (defun org-list-make-subtree ()
3203 "Convert the plain list at point into a subtree."
3204 (interactive)
3205 (if (not (ignore-errors (goto-char (org-in-item-p))))
3206 (error "Not in a list")
3207 (let ((list (save-excursion (org-list-to-lisp t))))
3208 (insert (org-list-to-subtree list)))))
3210 (defun org-list-insert-radio-list ()
3211 "Insert a radio list template appropriate for this major mode."
3212 (interactive)
3213 (let* ((e (cl-assoc-if #'derived-mode-p org-list-radio-list-templates))
3214 (txt (nth 1 e))
3215 name pos)
3216 (unless e (error "No radio list setup defined for %s" major-mode))
3217 (setq name (read-string "List name: "))
3218 (while (string-match "%n" txt)
3219 (setq txt (replace-match name t t txt)))
3220 (or (bolp) (insert "\n"))
3221 (setq pos (point))
3222 (insert txt)
3223 (goto-char pos)))
3225 (defun org-list-send-list (&optional maybe)
3226 "Send a transformed version of this list to the receiver position.
3227 With argument MAYBE, fail quietly if no transformation is defined
3228 for this list."
3229 (interactive)
3230 (catch 'exit
3231 (unless (org-at-item-p) (error "Not at a list item"))
3232 (save-excursion
3233 (let ((case-fold-search t))
3234 (re-search-backward "^[ \t]*#\\+ORGLST:" nil t)
3235 (unless (looking-at
3236 "[ \t]*#\\+ORGLST:[ \t]+SEND[ \t]+\\(\\S-+\\)[ \t]+\\([^ \t\n]+\\)")
3237 (if maybe (throw 'exit nil)
3238 (error "Don't know how to transform this list")))))
3239 (let* ((name (regexp-quote (match-string 1)))
3240 (transform (intern (match-string 2)))
3241 (bottom-point
3242 (save-excursion
3243 (re-search-forward
3244 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
3245 (match-beginning 0)))
3246 (top-point
3247 (progn
3248 (re-search-backward "#\\+ORGLST" nil t)
3249 (re-search-forward (org-item-beginning-re) bottom-point t)
3250 (match-beginning 0)))
3251 (plain-list (save-excursion
3252 (goto-char top-point)
3253 (org-list-to-lisp))))
3254 (unless (fboundp transform)
3255 (error "No such transformation function %s" transform))
3256 (let ((txt (funcall transform plain-list)))
3257 ;; Find the insertion(s) place(s).
3258 (save-excursion
3259 (goto-char (point-min))
3260 (let ((receiver-count 0)
3261 (begin-re (format "BEGIN +RECEIVE +ORGLST +%s\\([ \t]\\|$\\)"
3262 name))
3263 (end-re (format "END +RECEIVE +ORGLST +%s\\([ \t]\\|$\\)"
3264 name)))
3265 (while (re-search-forward begin-re nil t)
3266 (cl-incf receiver-count)
3267 (let ((beg (line-beginning-position 2)))
3268 (unless (re-search-forward end-re nil t)
3269 (user-error "Cannot find end of receiver location at %d" beg))
3270 (beginning-of-line)
3271 (delete-region beg (point))
3272 (insert txt "\n")))
3273 (cond
3274 ((> receiver-count 1)
3275 (message "List converted and installed at receiver locations"))
3276 ((= receiver-count 1)
3277 (message "List converted and installed at receiver location"))
3278 (t (user-error "No valid receiver location found")))))))))
3280 (defun org-list-to-generic (list params)
3281 "Convert a LIST parsed through `org-list-to-lisp' to a custom format.
3283 LIST is a list as returned by `org-list-to-lisp', which see.
3284 PARAMS is a property list of parameters used to tweak the output
3285 format.
3287 Valid parameters are:
3289 :backend, :raw
3291 Export back-end used as a basis to transcode elements of the
3292 list, when no specific parameter applies to it. It is also
3293 used to translate its contents. You can prevent this by
3294 setting :raw property to a non-nil value.
3296 :splice
3298 When non-nil, only export the contents of the top most plain
3299 list, effectively ignoring its opening and closing lines.
3301 :ustart, :uend
3303 Strings to start and end an unordered list. They can also be
3304 set to a function returning a string or nil, which will be
3305 called with the depth of the list, counting from 1.
3307 :ostart, :oend
3309 Strings to start and end an ordered list. They can also be set
3310 to a function returning a string or nil, which will be called
3311 with the depth of the list, counting from 1.
3313 :dstart, :dend
3315 Strings to start and end a descriptive list. They can also be
3316 set to a function returning a string or nil, which will be
3317 called with the depth of the list, counting from 1.
3319 :dtstart, :dtend, :ddstart, :ddend
3321 Strings to start and end a descriptive term.
3323 :istart, :iend
3325 Strings to start or end a list item, and to start a list item
3326 with a counter. They can also be set to a function returning
3327 a string or nil, which will be called with the depth of the
3328 item, counting from 1.
3330 :icount
3332 Strings to start a list item with a counter. It can also be
3333 set to a function returning a string or nil, which will be
3334 called with two arguments: the depth of the item, counting from
3335 1, and the counter. Its value, when non-nil, has precedence
3336 over `:istart'.
3338 :isep
3340 String used to separate items. It can also be set to
3341 a function returning a string or nil, which will be called with
3342 the depth of the items, counting from 1. It always start on
3343 a new line.
3345 :cbon, :cboff, :cbtrans
3347 String to insert, respectively, an un-checked check-box,
3348 a checked check-box and a check-box in transitional state."
3349 (require 'ox)
3350 (let* ((backend (plist-get params :backend))
3351 (custom-backend
3352 (org-export-create-backend
3353 :parent (or backend 'org)
3354 :transcoders
3355 `((plain-list . ,(org-list--to-generic-plain-list params))
3356 (item . ,(org-list--to-generic-item params))
3357 (macro . (lambda (m c i) (org-element-macro-interpreter m nil))))))
3358 data info)
3359 ;; Write LIST back into Org syntax and parse it.
3360 (with-temp-buffer
3361 (let ((org-inhibit-startup t)) (org-mode))
3362 (letrec ((insert-list
3363 (lambda (l)
3364 (dolist (i (cdr l))
3365 (funcall insert-item i (car l)))))
3366 (insert-item
3367 (lambda (i type)
3368 (let ((start (point)))
3369 (insert (if (eq type 'ordered) "1. " "- "))
3370 (dolist (e i)
3371 (if (consp e) (funcall insert-list e)
3372 (insert e)
3373 (insert "\n")))
3374 (beginning-of-line)
3375 (save-excursion
3376 (let ((ind (if (eq type 'ordered) 3 2)))
3377 (while (> (point) start)
3378 (unless (looking-at-p "[ \t]*$")
3379 (indent-to ind))
3380 (forward-line -1))))))))
3381 (funcall insert-list list))
3382 (setf data
3383 (org-element-map (org-element-parse-buffer) 'plain-list
3384 #'identity nil t))
3385 (setf info (org-export-get-environment backend nil params)))
3386 (when (and backend (symbolp backend) (not (org-export-get-backend backend)))
3387 (user-error "Unknown :backend value"))
3388 (unless backend (require 'ox-org))
3389 ;; When`:raw' property has a non-nil value, turn all objects back
3390 ;; into Org syntax.
3391 (when (and backend (plist-get params :raw))
3392 (org-element-map data org-element-all-objects
3393 (lambda (object)
3394 (org-element-set-element
3395 object (org-element-interpret-data object)))))
3396 ;; We use a low-level mechanism to export DATA so as to skip all
3397 ;; usual pre-processing and post-processing, i.e., hooks, filters,
3398 ;; Babel code evaluation, include keywords and macro expansion,
3399 ;; and filters.
3400 (let ((output (org-export-data-with-backend data custom-backend info)))
3401 ;; Remove final newline.
3402 (if (org-string-nw-p output) (substring-no-properties output 0 -1) ""))))
3404 (defun org-list--depth (element)
3405 "Return the level of ELEMENT within current plain list.
3406 ELEMENT is either an item or a plain list."
3407 (cl-count-if (lambda (ancestor) (eq (org-element-type ancestor) 'plain-list))
3408 (org-element-lineage element nil t)))
3410 (defun org-list--trailing-newlines (string)
3411 "Return the number of trailing newlines in STRING."
3412 (with-temp-buffer
3413 (insert string)
3414 (skip-chars-backward " \t\n")
3415 (count-lines (line-beginning-position 2) (point-max))))
3417 (defun org-list--generic-eval (value &rest args)
3418 "Evaluate VALUE according to its type.
3419 VALUE is either nil, a string or a function. In the latter case,
3420 it is called with arguments ARGS."
3421 (cond ((null value) nil)
3422 ((stringp value) value)
3423 ((functionp value) (apply value args))
3424 (t (error "Wrong value: %s" value))))
3426 (defun org-list--to-generic-plain-list (params)
3427 "Return a transcoder for `plain-list' elements.
3428 PARAMS is a plist used to tweak the behavior of the transcoder."
3429 (let ((ustart (plist-get params :ustart))
3430 (uend (plist-get params :uend))
3431 (ostart (plist-get params :ostart))
3432 (oend (plist-get params :oend))
3433 (dstart (plist-get params :dstart))
3434 (dend (plist-get params :dend))
3435 (splice (plist-get params :splice))
3436 (backend (plist-get params :backend)))
3437 (lambda (plain-list contents info)
3438 (let* ((type (org-element-property :type plain-list))
3439 (depth (org-list--depth plain-list))
3440 (start (and (not splice)
3441 (org-list--generic-eval
3442 (pcase type
3443 (`ordered ostart)
3444 (`unordered ustart)
3445 (_ dstart))
3446 depth)))
3447 (end (and (not splice)
3448 (org-list--generic-eval
3449 (pcase type
3450 (`ordered oend)
3451 (`unordered uend)
3452 (_ dend))
3453 depth))))
3454 ;; Make sure trailing newlines in END appear in the output by
3455 ;; setting `:post-blank' property to their number.
3456 (when end
3457 (org-element-put-property
3458 plain-list :post-blank (org-list--trailing-newlines end)))
3459 ;; Build output.
3460 (concat (and start (concat start "\n"))
3461 (if (or start end splice (not backend))
3462 contents
3463 (org-export-with-backend backend plain-list contents info))
3464 end)))))
3466 (defun org-list--to-generic-item (params)
3467 "Return a transcoder for `item' elements.
3468 PARAMS is a plist used to tweak the behavior of the transcoder."
3469 (let ((backend (plist-get params :backend))
3470 (istart (plist-get params :istart))
3471 (iend (plist-get params :iend))
3472 (isep (plist-get params :isep))
3473 (icount (plist-get params :icount))
3474 (cboff (plist-get params :cboff))
3475 (cbon (plist-get params :cbon))
3476 (cbtrans (plist-get params :cbtrans))
3477 (dtstart (plist-get params :dtstart))
3478 (dtend (plist-get params :dtend))
3479 (ddstart (plist-get params :ddstart))
3480 (ddend (plist-get params :ddend)))
3481 (lambda (item contents info)
3482 (let* ((type
3483 (org-element-property :type (org-element-property :parent item)))
3484 (tag (org-element-property :tag item))
3485 (depth (org-list--depth item))
3486 (separator (and (org-export-get-next-element item info)
3487 (org-list--generic-eval isep depth)))
3488 (closing (pcase (org-list--generic-eval iend depth)
3489 ((or `nil `"") "\n")
3490 ((and (guard separator) s)
3491 (if (equal (substring s -1) "\n") s (concat s "\n")))
3492 (s s))))
3493 ;; When a closing line or a separator is provided, make sure
3494 ;; its trailing newlines are taken into account when building
3495 ;; output. This is done by setting `:post-blank' property to
3496 ;; the number of such lines in the last line to be added.
3497 (let ((last-string (or separator closing)))
3498 (when last-string
3499 (org-element-put-property
3500 item
3501 :post-blank
3502 (max (1- (org-list--trailing-newlines last-string)) 0))))
3503 ;; Build output.
3504 (concat
3505 (let ((c (org-element-property :counter item)))
3506 (if c (org-list--generic-eval icount depth c)
3507 (org-list--generic-eval istart depth)))
3508 (let ((body
3509 (if (or istart iend icount cbon cboff cbtrans (not backend)
3510 (and (eq type 'descriptive)
3511 (or dtstart dtend ddstart ddend)))
3512 (concat
3513 (pcase (org-element-property :checkbox item)
3514 (`on cbon)
3515 (`off cboff)
3516 (`trans cbtrans))
3517 (and tag
3518 (concat dtstart
3519 (if backend
3520 (org-export-data-with-backend
3521 tag backend info)
3522 (org-element-interpret-data tag))
3523 dtend))
3524 (and tag ddstart)
3525 (if (= (length contents) 0) "" (substring contents 0 -1))
3526 (and tag ddend))
3527 (org-export-with-backend backend item contents info))))
3528 ;; Remove final newline.
3529 (if (equal body "") ""
3530 (substring (org-element-normalize-string body) 0 -1)))
3531 closing
3532 separator)))))
3534 (defun org-list-to-latex (list &optional params)
3535 "Convert LIST into a LaTeX list.
3536 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3537 PARAMS is a property list with overruling parameters for
3538 `org-list-to-generic'. Return converted list as a string."
3539 (require 'ox-latex)
3540 (org-list-to-generic list (org-combine-plists '(:backend latex) params)))
3542 (defun org-list-to-html (list &optional params)
3543 "Convert LIST into a HTML list.
3544 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3545 PARAMS is a property list with overruling parameters for
3546 `org-list-to-generic'. Return converted list as a string."
3547 (require 'ox-html)
3548 (org-list-to-generic list (org-combine-plists '(:backend html) params)))
3550 (defun org-list-to-texinfo (list &optional params)
3551 "Convert LIST into a Texinfo list.
3552 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3553 PARAMS is a property list with overruling parameters for
3554 `org-list-to-generic'. Return converted list as a string."
3555 (require 'ox-texinfo)
3556 (org-list-to-generic list (org-combine-plists '(:backend texinfo) params)))
3558 (defun org-list-to-subtree (list &optional params)
3559 "Convert LIST into an Org subtree.
3560 LIST is as returned by `org-list-to-lisp'. PARAMS is a property
3561 list with overruling parameters for `org-list-to-generic'."
3562 (let* ((blank (pcase (cdr (assq 'heading org-blank-before-new-entry))
3563 (`t t)
3564 (`auto (save-excursion
3565 (org-with-limited-levels (outline-previous-heading))
3566 (org-previous-line-empty-p)))))
3567 (level (org-reduced-level (or (org-current-level) 0)))
3568 (make-stars
3569 (lambda (depth)
3570 ;; Return the string for the heading, depending on DEPTH
3571 ;; of current sub-list.
3572 (let ((oddeven-level (+ level depth)))
3573 (concat (make-string (if org-odd-levels-only
3574 (1- (* 2 oddeven-level))
3575 oddeven-level)
3577 " ")))))
3578 (org-list-to-generic
3579 list
3580 (org-combine-plists
3581 (list :splice t
3582 :istart make-stars
3583 :icount make-stars
3584 :dtstart " " :dtend " "
3585 :isep (if blank "\n\n" "\n")
3586 :cbon "DONE " :cboff "TODO " :cbtrans "TODO ")
3587 params))))
3589 (provide 'org-list)
3591 ;;; org-list.el ends here