Merge branch 'maint'
[org-mode.git] / lisp / org-list.el
bloba84452d30bca7685ad860692c0706be802b424ce
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 outline-flag-region "outline" (from to flag))
158 (declare-function outline-invisible-p "outline" (&optional pos))
159 (declare-function outline-next-heading "outline" ())
160 (declare-function outline-previous-heading "outline" ())
164 ;;; Configuration variables
166 (defgroup org-plain-lists nil
167 "Options concerning plain lists in Org mode."
168 :tag "Org Plain lists"
169 :group 'org-structure)
171 (defcustom org-cycle-include-plain-lists t
172 "When t, make TAB cycle visibility on plain list items.
173 Cycling plain lists works only when the cursor is on a plain list
174 item. When the cursor is on an outline heading, plain lists are
175 treated as text. This is the most stable way of handling this,
176 which is why it is the default.
178 When this is the symbol `integrate', then integrate plain list
179 items when cycling, as if they were children of outline headings.
181 This setting can lead to strange effects when switching visibility
182 to `children', because the first \"child\" in a subtree decides
183 what children should be listed. If that first \"child\" is a
184 plain list item with an implied large level number, all true
185 children and grand children of the outline heading will be
186 exposed in a children' view."
187 :group 'org-plain-lists
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 (interactive "P")
2254 (let ((itemp (org-in-item-p))
2255 (pos (point)))
2256 ;; If cursor isn't is a list or if list is invisible, return nil.
2257 (unless (or (not itemp)
2258 (save-excursion
2259 (goto-char itemp)
2260 (outline-invisible-p)))
2261 (if (save-excursion
2262 (goto-char itemp)
2263 (org-at-item-timer-p))
2264 ;; Timer list: delegate to `org-timer-item'.
2265 (progn (org-timer-item) t)
2266 (let* ((struct (save-excursion (goto-char itemp)
2267 (org-list-struct)))
2268 (prevs (org-list-prevs-alist struct))
2269 ;; If we're in a description list, ask for the new term.
2270 (desc (when (eq (org-list-get-list-type itemp struct prevs)
2271 'descriptive)
2272 " :: ")))
2273 (setq struct (org-list-insert-item pos struct prevs checkbox desc))
2274 (org-list-write-struct struct (org-list-parents-alist struct))
2275 (when checkbox (org-update-checkbox-count-maybe))
2276 (looking-at org-list-full-item-re)
2277 (goto-char (if (and (match-beginning 4)
2278 (save-match-data
2279 (string-match "[.)]" (match-string 1))))
2280 (match-beginning 4)
2281 (match-end 0)))
2282 (if desc (backward-char 1))
2283 t)))))
2285 (defun org-list-repair ()
2286 "Fix indentation, bullets and checkboxes in the list at point."
2287 (interactive)
2288 (unless (org-at-item-p) (error "This is not a list"))
2289 (let* ((struct (org-list-struct))
2290 (parents (org-list-parents-alist struct)))
2291 (org-list-write-struct struct parents)))
2293 (defun org-cycle-list-bullet (&optional which)
2294 "Cycle through the different itemize/enumerate bullets.
2295 This cycle the entire list level through the sequence:
2297 `-' -> `+' -> `*' -> `1.' -> `1)'
2299 If WHICH is a valid string, use that as the new bullet. If WHICH
2300 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2301 `previous', cycle backwards."
2302 (interactive "P")
2303 (unless (org-at-item-p) (error "Not at an item"))
2304 (save-excursion
2305 (beginning-of-line)
2306 (let* ((struct (org-list-struct))
2307 (parents (org-list-parents-alist struct))
2308 (prevs (org-list-prevs-alist struct))
2309 (list-beg (org-list-get-first-item (point) struct prevs))
2310 (bullet (org-list-get-bullet list-beg struct))
2311 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs))
2312 (case-fold-search nil)
2313 (current (cond
2314 ((string-match "[a-z]\\." bullet) "a.")
2315 ((string-match "[a-z])" bullet) "a)")
2316 ((string-match "[A-Z]\\." bullet) "A.")
2317 ((string-match "[A-Z])" bullet) "A)")
2318 ((string-match "\\." bullet) "1.")
2319 ((string-match ")" bullet) "1)")
2320 (t (org-trim bullet))))
2321 ;; Compute list of possible bullets, depending on context.
2322 (bullet-list
2323 (append '("-" "+" )
2324 ;; *-bullets are not allowed at column 0.
2325 (unless (looking-at "\\S-") '("*"))
2326 ;; Description items cannot be numbered.
2327 (unless (or (eq org-plain-list-ordered-item-terminator ?\))
2328 (org-at-item-description-p))
2329 '("1."))
2330 (unless (or (eq org-plain-list-ordered-item-terminator ?.)
2331 (org-at-item-description-p))
2332 '("1)"))
2333 (unless (or (not alpha-p)
2334 (eq org-plain-list-ordered-item-terminator ?\))
2335 (org-at-item-description-p))
2336 '("a." "A."))
2337 (unless (or (not alpha-p)
2338 (eq org-plain-list-ordered-item-terminator ?.)
2339 (org-at-item-description-p))
2340 '("a)" "A)"))))
2341 (len (length bullet-list))
2342 (item-index (- len (length (member current bullet-list))))
2343 (get-value (lambda (index) (nth (mod index len) bullet-list)))
2344 (new (cond
2345 ((member which bullet-list) which)
2346 ((numberp which) (funcall get-value which))
2347 ((eq 'previous which) (funcall get-value (1- item-index)))
2348 (t (funcall get-value (1+ item-index))))))
2349 ;; Use a short variation of `org-list-write-struct' as there's
2350 ;; no need to go through all the steps.
2351 (let ((old-struct (copy-tree struct)))
2352 (org-list-set-bullet list-beg struct (org-list-bullet-string new))
2353 (org-list-struct-fix-bul struct prevs)
2354 (org-list-struct-fix-ind struct parents)
2355 (org-list-struct-apply-struct struct old-struct)))))
2357 (defun org-toggle-checkbox (&optional toggle-presence)
2358 "Toggle the checkbox in the current line.
2359 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2360 double prefix, set checkbox to [-].
2362 When there is an active region, toggle status or presence of the
2363 first checkbox there, and make every item inside have the same
2364 status or presence, respectively.
2366 If the cursor is in a headline, apply this to all checkbox items
2367 in the text below the heading, taking as reference the first item
2368 in subtree, ignoring drawers."
2369 (interactive "P")
2370 (save-excursion
2371 (let* (singlep
2372 block-item
2373 lim-up
2374 lim-down
2375 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2376 "\\|" org-deadline-string
2377 "\\|" org-closed-string
2378 "\\|" org-clock-string "\\)"
2379 " *[[<]\\([^]>]+\\)[]>]"))
2380 (orderedp (org-entry-get nil "ORDERED"))
2381 (_bounds
2382 ;; In a region, start at first item in region.
2383 (cond
2384 ((org-region-active-p)
2385 (let ((limit (region-end)))
2386 (goto-char (region-beginning))
2387 (if (org-list-search-forward (org-item-beginning-re) limit t)
2388 (setq lim-up (point-at-bol))
2389 (error "No item in region"))
2390 (setq lim-down (copy-marker limit))))
2391 ((org-at-heading-p)
2392 ;; On an heading, start at first item after drawers and
2393 ;; time-stamps (scheduled, etc.).
2394 (let ((limit (save-excursion (outline-next-heading) (point))))
2395 (forward-line 1)
2396 (while (or (looking-at org-drawer-regexp)
2397 (looking-at keyword-re))
2398 (if (looking-at keyword-re)
2399 (forward-line 1)
2400 (re-search-forward "^[ \t]*:END:" limit nil)))
2401 (if (org-list-search-forward (org-item-beginning-re) limit t)
2402 (setq lim-up (point-at-bol))
2403 (error "No item in subtree"))
2404 (setq lim-down (copy-marker limit))))
2405 ;; Just one item: set SINGLEP flag.
2406 ((org-at-item-p)
2407 (setq singlep t)
2408 (setq lim-up (point-at-bol)
2409 lim-down (copy-marker (point-at-eol))))
2410 (t (error "Not at an item or heading, and no active region"))))
2411 ;; Determine the checkbox going to be applied to all items
2412 ;; within bounds.
2413 (ref-checkbox
2414 (progn
2415 (goto-char lim-up)
2416 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2417 (cond
2418 ((equal toggle-presence '(16)) "[-]")
2419 ((equal toggle-presence '(4))
2420 (unless cbox "[ ]"))
2421 ((equal "[X]" cbox) "[ ]")
2422 (t "[X]"))))))
2423 ;; When an item is found within bounds, grab the full list at
2424 ;; point structure, then: (1) set check-box of all its items
2425 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2426 ;; whole list, (3) move point after the list.
2427 (goto-char lim-up)
2428 (while (and (< (point) lim-down)
2429 (org-list-search-forward (org-item-beginning-re)
2430 lim-down 'move))
2431 (let* ((struct (org-list-struct))
2432 (struct-copy (copy-tree struct))
2433 (parents (org-list-parents-alist struct))
2434 (prevs (org-list-prevs-alist struct))
2435 (bottom (copy-marker (org-list-get-bottom-point struct)))
2436 (items-to-toggle (cl-remove-if
2437 (lambda (e) (or (< e lim-up) (> e lim-down)))
2438 (mapcar #'car struct))))
2439 (mapc (lambda (e) (org-list-set-checkbox
2440 e struct
2441 ;; If there is no box at item, leave as-is
2442 ;; unless function was called with C-u prefix.
2443 (let ((cur-box (org-list-get-checkbox e struct)))
2444 (if (or cur-box (equal toggle-presence '(4)))
2445 ref-checkbox
2446 cur-box))))
2447 items-to-toggle)
2448 (setq block-item (org-list-struct-fix-box
2449 struct parents prevs orderedp))
2450 ;; Report some problems due to ORDERED status of subtree.
2451 ;; If only one box was being checked, throw an error, else,
2452 ;; only signal problems.
2453 (cond
2454 ((and singlep block-item (> lim-up block-item))
2455 (error
2456 "Checkbox blocked because of unchecked box at line %d"
2457 (org-current-line block-item)))
2458 (block-item
2459 (message
2460 "Checkboxes were removed due to unchecked box at line %d"
2461 (org-current-line block-item))))
2462 (goto-char bottom)
2463 (move-marker bottom nil)
2464 (org-list-struct-apply-struct struct struct-copy)))
2465 (move-marker lim-down nil)))
2466 (org-update-checkbox-count-maybe))
2468 (defun org-reset-checkbox-state-subtree ()
2469 "Reset all checkboxes in an entry subtree."
2470 (interactive "*")
2471 (if (org-before-first-heading-p)
2472 (error "Not inside a tree")
2473 (save-restriction
2474 (save-excursion
2475 (org-narrow-to-subtree)
2476 (org-show-subtree)
2477 (goto-char (point-min))
2478 (let ((end (point-max)))
2479 (while (< (point) end)
2480 (when (org-at-item-checkbox-p)
2481 (replace-match "[ ]" t t nil 1))
2482 (beginning-of-line 2)))
2483 (org-update-checkbox-count-maybe 'all)))))
2485 (defun org-update-checkbox-count (&optional all)
2486 "Update the checkbox statistics in the current section.
2488 This will find all statistic cookies like [57%] and [6/12] and
2489 update them with the current numbers.
2491 With optional prefix argument ALL, do this for the whole buffer."
2492 (interactive "P")
2493 (org-with-wide-buffer
2494 (let* ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2495 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\
2496 \\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2497 (recursivep
2498 (or (not org-checkbox-hierarchical-statistics)
2499 (string-match "\\<recursive\\>"
2500 (or (org-entry-get nil "COOKIE_DATA") ""))))
2501 (within-inlinetask (and (not all)
2502 (featurep 'org-inlinetask)
2503 (org-inlinetask-in-task-p)))
2504 (end (cond (all (point-max))
2505 (within-inlinetask
2506 (save-excursion (outline-next-heading) (point)))
2507 (t (save-excursion
2508 (org-with-limited-levels (outline-next-heading))
2509 (point)))))
2510 (count-boxes
2511 (lambda (item structs recursivep)
2512 ;; Return number of checked boxes and boxes of all types
2513 ;; in all structures in STRUCTS. If RECURSIVEP is
2514 ;; non-nil, also count boxes in sub-lists. If ITEM is
2515 ;; nil, count across the whole structure, else count only
2516 ;; across subtree whose ancestor is ITEM.
2517 (let ((c-on 0) (c-all 0))
2518 (dolist (s structs (list c-on c-all))
2519 (let* ((pre (org-list-prevs-alist s))
2520 (par (org-list-parents-alist s))
2521 (items
2522 (cond
2523 ((and recursivep item) (org-list-get-subtree item s))
2524 (recursivep (mapcar #'car s))
2525 (item (org-list-get-children item s par))
2526 (t (org-list-get-all-items
2527 (org-list-get-top-point s) s pre))))
2528 (cookies (delq nil (mapcar
2529 (lambda (e)
2530 (org-list-get-checkbox e s))
2531 items))))
2532 (cl-incf c-all (length cookies))
2533 (cl-incf c-on (cl-count "[X]" cookies :test #'equal)))))))
2534 cookies-list cache)
2535 ;; Move to start.
2536 (cond (all (goto-char (point-min)))
2537 (within-inlinetask (org-back-to-heading t))
2538 (t (org-with-limited-levels (outline-previous-heading))))
2539 ;; Build an alist for each cookie found. The key is the position
2540 ;; at beginning of cookie and values ending position, format of
2541 ;; cookie, number of checked boxes to report and total number of
2542 ;; boxes.
2543 (while (re-search-forward cookie-re end t)
2544 (let ((context (save-excursion (backward-char)
2545 (save-match-data (org-element-context)))))
2546 (when (eq (org-element-type context) 'statistics-cookie)
2547 (push
2548 (append
2549 (list (match-beginning 1) (match-end 1) (match-end 2))
2550 (let* ((container
2551 (org-element-lineage
2552 context
2553 '(drawer center-block dynamic-block inlinetask item
2554 quote-block special-block verse-block)))
2555 (beg (if container
2556 (org-element-property :contents-begin container)
2557 (save-excursion
2558 (org-with-limited-levels
2559 (outline-previous-heading))
2560 (point)))))
2561 (or (cdr (assq beg cache))
2562 (save-excursion
2563 (goto-char beg)
2564 (let ((end
2565 (if container
2566 (org-element-property :contents-end container)
2567 (save-excursion
2568 (org-with-limited-levels (outline-next-heading))
2569 (point))))
2570 structs)
2571 (while (re-search-forward box-re end t)
2572 (let ((element (org-element-at-point)))
2573 (when (eq (org-element-type element) 'item)
2574 (push (org-element-property :structure element)
2575 structs)
2576 ;; Skip whole list since we have its
2577 ;; structure anyway.
2578 (while (setq element (org-element-lineage
2579 element '(plain-list)))
2580 (goto-char
2581 (min (org-element-property :end element)
2582 end))))))
2583 ;; Cache count for cookies applying to the same
2584 ;; area. Then return it.
2585 (let ((count
2586 (funcall count-boxes
2587 (and (eq (org-element-type container)
2588 'item)
2589 (org-element-property
2590 :begin container))
2591 structs
2592 recursivep)))
2593 (push (cons beg count) cache)
2594 count))))))
2595 cookies-list))))
2596 ;; Apply alist to buffer.
2597 (dolist (cookie cookies-list)
2598 (let* ((beg (car cookie))
2599 (end (nth 1 cookie))
2600 (percent (nth 2 cookie))
2601 (checked (nth 3 cookie))
2602 (total (nth 4 cookie)))
2603 (goto-char beg)
2604 (insert
2605 (if percent (format "[%d%%]" (floor (* 100.0 checked)
2606 (max 1 total)))
2607 (format "[%d/%d]" checked total)))
2608 (delete-region (point) (+ (point) (- end beg)))
2609 (when org-auto-align-tags (org-fix-tags-on-the-fly)))))))
2611 (defun org-get-checkbox-statistics-face ()
2612 "Select the face for checkbox statistics.
2613 The face will be `org-done' when all relevant boxes are checked.
2614 Otherwise it will be `org-todo'."
2615 (if (match-end 1)
2616 (if (equal (match-string 1) "100%")
2617 'org-checkbox-statistics-done
2618 'org-checkbox-statistics-todo)
2619 (if (and (> (match-end 2) (match-beginning 2))
2620 (equal (match-string 2) (match-string 3)))
2621 'org-checkbox-statistics-done
2622 'org-checkbox-statistics-todo)))
2624 (defun org-update-checkbox-count-maybe (&optional all)
2625 "Update checkbox statistics unless turned off by user.
2626 With an optional argument ALL, update them in the whole buffer."
2627 (when (cdr (assq 'checkbox org-list-automatic-rules))
2628 (org-update-checkbox-count all))
2629 (run-hooks 'org-checkbox-statistics-hook))
2631 (defvar org-last-indent-begin-marker (make-marker))
2632 (defvar org-last-indent-end-marker (make-marker))
2633 (defun org-list-indent-item-generic (arg no-subtree struct)
2634 "Indent a local list item including its children.
2635 When number ARG is a negative, item will be outdented, otherwise
2636 it will be indented.
2638 If a region is active, all items inside will be moved.
2640 If NO-SUBTREE is non-nil, only indent the item itself, not its
2641 children.
2643 STRUCT is the list structure.
2645 Return t if successful."
2646 (save-excursion
2647 (let* ((regionp (org-region-active-p))
2648 (rbeg (and regionp (region-beginning)))
2649 (rend (and regionp (region-end)))
2650 (top (org-list-get-top-point struct))
2651 (parents (org-list-parents-alist struct))
2652 (prevs (org-list-prevs-alist struct))
2653 ;; Are we going to move the whole list?
2654 (specialp
2655 (and (not regionp)
2656 (= top (point-at-bol))
2657 (cdr (assq 'indent org-list-automatic-rules))
2658 (if no-subtree
2659 (error
2660 "First item of list cannot move without its subtree")
2661 t))))
2662 ;; Determine begin and end points of zone to indent. If moving
2663 ;; more than one item, save them for subsequent moves.
2664 (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
2665 (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
2666 (if regionp
2667 (progn
2668 (set-marker org-last-indent-begin-marker rbeg)
2669 (set-marker org-last-indent-end-marker rend))
2670 (set-marker org-last-indent-begin-marker (point-at-bol))
2671 (set-marker org-last-indent-end-marker
2672 (cond
2673 (specialp (org-list-get-bottom-point struct))
2674 (no-subtree (1+ (point-at-bol)))
2675 (t (org-list-get-item-end (point-at-bol) struct))))))
2676 (let* ((beg (marker-position org-last-indent-begin-marker))
2677 (end (marker-position org-last-indent-end-marker)))
2678 (cond
2679 ;; Special case: moving top-item with indent rule.
2680 (specialp
2681 (let* ((level-skip (org-level-increment))
2682 (offset (if (< arg 0) (- level-skip) level-skip))
2683 (top-ind (org-list-get-ind beg struct))
2684 (old-struct (copy-tree struct)))
2685 (if (< (+ top-ind offset) 0)
2686 (error "Cannot outdent beyond margin")
2687 ;; Change bullet if necessary.
2688 (when (and (= (+ top-ind offset) 0)
2689 (string-match "*"
2690 (org-list-get-bullet beg struct)))
2691 (org-list-set-bullet beg struct
2692 (org-list-bullet-string "-")))
2693 ;; Shift every item by OFFSET and fix bullets. Then
2694 ;; apply changes to buffer.
2695 (mapc (lambda (e)
2696 (let ((ind (org-list-get-ind (car e) struct)))
2697 (org-list-set-ind (car e) struct (+ ind offset))))
2698 struct)
2699 (org-list-struct-fix-bul struct prevs)
2700 (org-list-struct-apply-struct struct old-struct))))
2701 ;; Forbidden move:
2702 ((and (< arg 0)
2703 ;; If only one item is moved, it mustn't have a child.
2704 (or (and no-subtree
2705 (not regionp)
2706 (org-list-has-child-p beg struct))
2707 ;; If a subtree or region is moved, the last item
2708 ;; of the subtree mustn't have a child.
2709 (let ((last-item (caar
2710 (reverse
2711 (cl-remove-if
2712 (lambda (e) (>= (car e) end))
2713 struct)))))
2714 (org-list-has-child-p last-item struct))))
2715 (error "Cannot outdent an item without its children"))
2716 ;; Normal shifting
2718 (let* ((new-parents
2719 (if (< arg 0)
2720 (org-list-struct-outdent beg end struct parents)
2721 (org-list-struct-indent beg end struct parents prevs))))
2722 (org-list-write-struct struct new-parents))
2723 (org-update-checkbox-count-maybe))))))
2726 (defun org-outdent-item ()
2727 "Outdent a local list item, but not its children.
2728 If a region is active, all items inside will be moved."
2729 (interactive)
2730 (let ((regionp (org-region-active-p)))
2731 (cond
2732 ((or (org-at-item-p)
2733 (and regionp
2734 (save-excursion (goto-char (region-beginning))
2735 (org-at-item-p))))
2736 (let ((struct (if (not regionp) (org-list-struct)
2737 (save-excursion (goto-char (region-beginning))
2738 (org-list-struct)))))
2739 (org-list-indent-item-generic -1 t struct)))
2740 (regionp (error "Region not starting at an item"))
2741 (t (error "Not at an item")))))
2743 (defun org-indent-item ()
2744 "Indent a local list item, but not its children.
2745 If a region is active, all items inside will be moved."
2746 (interactive)
2747 (let ((regionp (org-region-active-p)))
2748 (cond
2749 ((or (org-at-item-p)
2750 (and regionp
2751 (save-excursion (goto-char (region-beginning))
2752 (org-at-item-p))))
2753 (let ((struct (if (not regionp) (org-list-struct)
2754 (save-excursion (goto-char (region-beginning))
2755 (org-list-struct)))))
2756 (org-list-indent-item-generic 1 t struct)))
2757 (regionp (error "Region not starting at an item"))
2758 (t (error "Not at an item")))))
2760 (defun org-outdent-item-tree ()
2761 "Outdent a local list item including its children.
2762 If a region is active, all items inside will be moved."
2763 (interactive)
2764 (let ((regionp (org-region-active-p)))
2765 (cond
2766 ((or (org-at-item-p)
2767 (and regionp
2768 (save-excursion (goto-char (region-beginning))
2769 (org-at-item-p))))
2770 (let ((struct (if (not regionp) (org-list-struct)
2771 (save-excursion (goto-char (region-beginning))
2772 (org-list-struct)))))
2773 (org-list-indent-item-generic -1 nil struct)))
2774 (regionp (error "Region not starting at an item"))
2775 (t (error "Not at an item")))))
2777 (defun org-indent-item-tree ()
2778 "Indent a local list item including its children.
2779 If a region is active, all items inside will be moved."
2780 (interactive)
2781 (let ((regionp (org-region-active-p)))
2782 (cond
2783 ((or (org-at-item-p)
2784 (and regionp
2785 (save-excursion (goto-char (region-beginning))
2786 (org-at-item-p))))
2787 (let ((struct (if (not regionp) (org-list-struct)
2788 (save-excursion (goto-char (region-beginning))
2789 (org-list-struct)))))
2790 (org-list-indent-item-generic 1 nil struct)))
2791 (regionp (error "Region not starting at an item"))
2792 (t (error "Not at an item")))))
2794 (defvar org-tab-ind-state)
2795 (defvar org-adapt-indentation)
2796 (defun org-cycle-item-indentation ()
2797 "Cycle levels of indentation of an empty item.
2798 The first run indents the item, if applicable. Subsequent runs
2799 outdent it at meaningful levels in the list. When done, item is
2800 put back at its original position with its original bullet.
2802 Return t at each successful move."
2803 (when (org-at-item-p)
2804 (let* ((org-adapt-indentation nil)
2805 (struct (org-list-struct))
2806 (ind (org-list-get-ind (point-at-bol) struct))
2807 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2808 ;; Accept empty items or if cycle has already started.
2809 (when (or (eq last-command 'org-cycle-item-indentation)
2810 (and (save-excursion
2811 (beginning-of-line)
2812 (looking-at org-list-full-item-re))
2813 (>= (match-end 0) (save-excursion
2814 (goto-char (org-list-get-item-end
2815 (point-at-bol) struct))
2816 (skip-chars-backward " \r\t\n")
2817 (point)))))
2818 (setq this-command 'org-cycle-item-indentation)
2819 ;; When in the middle of the cycle, try to outdent first. If
2820 ;; it fails, and point is still at initial position, indent.
2821 ;; Else, re-create it at its original position.
2822 (if (eq last-command 'org-cycle-item-indentation)
2823 (cond
2824 ((ignore-errors (org-list-indent-item-generic -1 t struct)))
2825 ((and (= ind (car org-tab-ind-state))
2826 (ignore-errors (org-list-indent-item-generic 1 t struct))))
2827 (t (delete-region (point-at-bol) (point-at-eol))
2828 (indent-to-column (car org-tab-ind-state))
2829 (insert (cdr org-tab-ind-state) " ")
2830 ;; Break cycle
2831 (setq this-command 'identity)))
2832 ;; If a cycle is starting, remember indentation and bullet,
2833 ;; then try to indent. If it fails, try to outdent.
2834 (setq org-tab-ind-state (cons ind bullet))
2835 (cond
2836 ((ignore-errors (org-list-indent-item-generic 1 t struct)))
2837 ((ignore-errors (org-list-indent-item-generic -1 t struct)))
2838 (t (user-error "Cannot move item"))))
2839 t))))
2841 (defun org-sort-list
2842 (&optional with-case sorting-type getkey-func compare-func interactive?)
2843 "Sort list items.
2844 The cursor may be at any item of the list that should be sorted.
2845 Sublists are not sorted. Checkboxes, if any, are ignored.
2847 Sorting can be alphabetically, numerically, by date/time as given
2848 by a time stamp, by a property or by priority.
2850 Comparing entries ignores case by default. However, with an
2851 optional argument WITH-CASE, the sorting considers case as well.
2853 The command prompts for the sorting type unless it has been given
2854 to the function through the SORTING-TYPE argument, which needs to
2855 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F ?x ?X). Here is the
2856 detailed meaning of each character:
2858 n Numerically, by converting the beginning of the item to a number.
2859 a Alphabetically. Only the first line of item is checked.
2860 t By date/time, either the first active time stamp in the entry, if
2861 any, or by the first inactive one. In a timer list, sort the timers.
2862 x By \"checked\" status of a check list.
2864 Capital letters will reverse the sort order.
2866 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2867 a function to be called with point at the beginning of the
2868 record. It must return a value that is compatible with COMPARE-FUNC,
2869 the function used to compare entries.
2871 Sorting is done against the visible part of the headlines, it
2872 ignores hidden links.
2874 A non-nil value for INTERACTIVE? is used to signal that this
2875 function is being called interactively."
2876 (interactive (list current-prefix-arg nil nil nil t))
2877 (let* ((case-func (if with-case 'identity 'downcase))
2878 (struct (org-list-struct))
2879 (prevs (org-list-prevs-alist struct))
2880 (start (org-list-get-list-begin (point-at-bol) struct prevs))
2881 (end (org-list-get-list-end (point-at-bol) struct prevs))
2882 (sorting-type
2883 (or sorting-type
2884 (progn
2885 (message
2886 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc [x]checked A/N/T/F/X means reversed:")
2887 (read-char-exclusive))))
2888 (dcst (downcase sorting-type))
2889 (getkey-func
2890 (and (= dcst ?f)
2891 (or getkey-func
2892 (and interactive?
2893 (org-read-function "Function for extracting keys: "))
2894 (error "Missing key extractor"))))
2895 (sort-func
2896 (cond
2897 ((= dcst ?a) #'string<)
2898 ((= dcst ?f)
2899 (or compare-func
2900 (and interactive?
2901 (org-read-function
2902 (concat "Function for comparing keys "
2903 "(empty for default `sort-subr' predicate): ")
2904 'allow-empty))))
2905 ((= dcst ?t) #'<)
2906 ((= dcst ?x) #'string<))))
2907 (message "Sorting items...")
2908 (save-restriction
2909 (narrow-to-region start end)
2910 (goto-char (point-min))
2911 (let* ((case-fold-search nil)
2912 (now (current-time))
2913 (next-record (lambda ()
2914 (skip-chars-forward " \r\t\n")
2915 (or (eobp) (beginning-of-line))))
2916 (end-record (lambda ()
2917 (goto-char (org-list-get-item-end-before-blank
2918 (point) struct))))
2919 (value-to-sort
2920 (lambda ()
2921 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2922 (cond
2923 ((= dcst ?n)
2924 (string-to-number
2925 (org-sort-remove-invisible
2926 (buffer-substring (match-end 0) (point-at-eol)))))
2927 ((= dcst ?a)
2928 (funcall case-func
2929 (org-sort-remove-invisible
2930 (buffer-substring
2931 (match-end 0) (point-at-eol)))))
2932 ((= dcst ?t)
2933 (cond
2934 ;; If it is a timer list, convert timer to seconds
2935 ((org-at-item-timer-p)
2936 (org-timer-hms-to-secs (match-string 1)))
2937 ((or (save-excursion
2938 (re-search-forward org-ts-regexp (point-at-eol) t))
2939 (save-excursion (re-search-forward org-ts-regexp-both
2940 (point-at-eol) t)))
2941 (org-time-string-to-seconds (match-string 0)))
2942 (t (float-time now))))
2943 ((= dcst ?x) (or (and (stringp (match-string 1))
2944 (match-string 1))
2945 ""))
2946 ((= dcst ?f)
2947 (if getkey-func
2948 (let ((value (funcall getkey-func)))
2949 (if (stringp value)
2950 (funcall case-func value)
2951 value))
2952 (error "Invalid key function `%s'" getkey-func)))
2953 (t (error "Invalid sorting type `%c'" sorting-type)))))))
2954 (sort-subr (/= dcst sorting-type)
2955 next-record
2956 end-record
2957 value-to-sort
2959 sort-func)
2960 ;; Read and fix list again, as `sort-subr' probably destroyed
2961 ;; its structure.
2962 (org-list-repair)
2963 (run-hooks 'org-after-sorting-entries-or-items-hook)
2964 (message "Sorting items...done")))))
2966 (defun org-toggle-item (arg)
2967 "Convert headings or normal lines to items, items to normal lines.
2968 If there is no active region, only the current line is considered.
2970 If the first non blank line in the region is a headline, convert
2971 all headlines to items, shifting text accordingly.
2973 If it is an item, convert all items to normal lines.
2975 If it is normal text, change region into a list of items.
2976 With a prefix argument ARG, change the region in a single item."
2977 (interactive "P")
2978 (let ((shift-text
2979 (lambda (ind end)
2980 ;; Shift text in current section to IND, from point to END.
2981 ;; The function leaves point to END line.
2982 (let ((min-i 1000) (end (copy-marker end)))
2983 ;; First determine the minimum indentation (MIN-I) of
2984 ;; the text.
2985 (save-excursion
2986 (catch 'exit
2987 (while (< (point) end)
2988 (let ((i (org-get-indentation)))
2989 (cond
2990 ;; Skip blank lines and inline tasks.
2991 ((looking-at "^[ \t]*$"))
2992 ((looking-at org-outline-regexp-bol))
2993 ;; We can't find less than 0 indentation.
2994 ((zerop i) (throw 'exit (setq min-i 0)))
2995 ((< i min-i) (setq min-i i))))
2996 (forward-line))))
2997 ;; Then indent each line so that a line indented to
2998 ;; MIN-I becomes indented to IND. Ignore blank lines
2999 ;; and inline tasks in the process.
3000 (let ((delta (- ind min-i)))
3001 (while (< (point) end)
3002 (unless (or (looking-at "^[ \t]*$")
3003 (looking-at org-outline-regexp-bol))
3004 (indent-line-to (+ (org-get-indentation) delta)))
3005 (forward-line))))))
3006 (skip-blanks
3007 (lambda (pos)
3008 ;; Return beginning of first non-blank line, starting from
3009 ;; line at POS.
3010 (save-excursion
3011 (goto-char pos)
3012 (skip-chars-forward " \r\t\n")
3013 (point-at-bol))))
3014 beg end)
3015 ;; Determine boundaries of changes.
3016 (if (org-region-active-p)
3017 (setq beg (funcall skip-blanks (region-beginning))
3018 end (copy-marker (region-end)))
3019 (setq beg (funcall skip-blanks (point-at-bol))
3020 end (copy-marker (point-at-eol))))
3021 ;; Depending on the starting line, choose an action on the text
3022 ;; between BEG and END.
3023 (org-with-limited-levels
3024 (save-excursion
3025 (goto-char beg)
3026 (cond
3027 ;; Case 1. Start at an item: de-itemize. Note that it only
3028 ;; happens when a region is active: `org-ctrl-c-minus'
3029 ;; would call `org-cycle-list-bullet' otherwise.
3030 ((org-at-item-p)
3031 (while (< (point) end)
3032 (when (org-at-item-p)
3033 (skip-chars-forward " \t")
3034 (delete-region (point) (match-end 0)))
3035 (forward-line)))
3036 ;; Case 2. Start at an heading: convert to items.
3037 ((org-at-heading-p)
3038 (let* ((bul (org-list-bullet-string "-"))
3039 (bul-len (length bul))
3040 ;; Indentation of the first heading. It should be
3041 ;; relative to the indentation of its parent, if any.
3042 (start-ind (save-excursion
3043 (cond
3044 ((not org-adapt-indentation) 0)
3045 ((not (outline-previous-heading)) 0)
3046 (t (length (match-string 0))))))
3047 ;; Level of first heading. Further headings will be
3048 ;; compared to it to determine hierarchy in the list.
3049 (ref-level (org-reduced-level (org-outline-level))))
3050 (while (< (point) end)
3051 (let* ((level (org-reduced-level (org-outline-level)))
3052 (delta (max 0 (- level ref-level)))
3053 (todo-state (org-get-todo-state)))
3054 ;; If current headline is less indented than the first
3055 ;; one, set it as reference, in order to preserve
3056 ;; subtrees.
3057 (when (< level ref-level) (setq ref-level level))
3058 ;; Remove stars and TODO keyword.
3059 (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
3060 (delete-region (point) (or (match-beginning 3)
3061 (line-end-position)))
3062 (insert bul)
3063 (indent-line-to (+ start-ind (* delta bul-len)))
3064 ;; Turn TODO keyword into a check box.
3065 (when todo-state
3066 (let* ((struct (org-list-struct))
3067 (old (copy-tree struct)))
3068 (org-list-set-checkbox
3069 (line-beginning-position)
3070 struct
3071 (if (member todo-state org-done-keywords)
3072 "[X]"
3073 "[ ]"))
3074 (org-list-write-struct struct
3075 (org-list-parents-alist struct)
3076 old)))
3077 ;; Ensure all text down to END (or SECTION-END) belongs
3078 ;; to the newly created item.
3079 (let ((section-end (save-excursion
3080 (or (outline-next-heading) (point)))))
3081 (forward-line)
3082 (funcall shift-text
3083 (+ start-ind (* (1+ delta) bul-len))
3084 (min end section-end)))))))
3085 ;; Case 3. Normal line with ARG: make the first line of region
3086 ;; an item, and shift indentation of others lines to
3087 ;; set them as item's body.
3088 (arg (let* ((bul (org-list-bullet-string "-"))
3089 (bul-len (length bul))
3090 (ref-ind (org-get-indentation)))
3091 (skip-chars-forward " \t")
3092 (insert bul)
3093 (forward-line)
3094 (while (< (point) end)
3095 ;; Ensure that lines less indented than first one
3096 ;; still get included in item body.
3097 (funcall shift-text
3098 (+ ref-ind bul-len)
3099 (min end (save-excursion (or (outline-next-heading)
3100 (point)))))
3101 (forward-line))))
3102 ;; Case 4. Normal line without ARG: turn each non-item line
3103 ;; into an item.
3105 (while (< (point) end)
3106 (unless (or (org-at-heading-p) (org-at-item-p))
3107 (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
3108 (replace-match
3109 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
3110 (forward-line))))))))
3113 ;;; Send and receive lists
3115 (defun org-list-to-lisp (&optional delete)
3116 "Parse the list at point and maybe DELETE it.
3118 Return a list whose car is a symbol of list type, among
3119 `ordered', `unordered' and `descriptive'. Then, each item is
3120 a list of strings and other sub-lists.
3122 For example, the following list:
3124 1. first item
3125 + sub-item one
3126 + [X] sub-item two
3127 more text in first item
3128 2. [@3] last item
3130 is parsed as
3132 (ordered
3133 (\"first item\"
3134 (unordered
3135 (\"sub-item one\")
3136 (\"[X] sub-item two\"))
3137 \"more text in first item\")
3138 (\"[@3] last item\"))
3140 Point is left at list's end."
3141 (letrec ((struct (org-list-struct))
3142 (prevs (org-list-prevs-alist struct))
3143 (parents (org-list-parents-alist struct))
3144 (top (org-list-get-top-point struct))
3145 (bottom (org-list-get-bottom-point struct))
3146 (trim
3147 (lambda (text)
3148 ;; Remove indentation and final newline from TEXT.
3149 (org-remove-indentation
3150 (if (string-match-p "\n\\'" text)
3151 (substring text 0 -1)
3152 text))))
3153 (parse-sublist
3154 (lambda (e)
3155 ;; Return a list whose car is list type and cdr a list
3156 ;; of items' body.
3157 (cons (org-list-get-list-type (car e) struct prevs)
3158 (mapcar parse-item e))))
3159 (parse-item
3160 (lambda (e)
3161 ;; Return a list containing counter of item, if any,
3162 ;; text and any sublist inside it.
3163 (let* ((end (org-list-get-item-end e struct))
3164 (children (org-list-get-children e struct parents))
3165 (body
3166 (save-excursion
3167 (goto-char e)
3168 (looking-at "[ \t]*\\S-+[ \t]*")
3169 (list
3170 (funcall
3171 trim
3172 (concat
3173 (make-string (string-width (match-string 0)) ?\s)
3174 (buffer-substring-no-properties
3175 (match-end 0) (or (car children) end))))))))
3176 (while children
3177 (let* ((child (car children))
3178 (sub (org-list-get-all-items child struct prevs))
3179 (last-in-sub (car (last sub))))
3180 (push (funcall parse-sublist sub) body)
3181 ;; Remove whole sub-list from children.
3182 (setq children (cdr (memq last-in-sub children)))
3183 ;; There is a chunk of text belonging to the item
3184 ;; if last child doesn't end where next child
3185 ;; starts or where item ends.
3186 (let ((sub-end (org-list-get-item-end last-in-sub struct))
3187 (next (or (car children) end)))
3188 (when (/= sub-end next)
3189 (push (funcall
3190 trim
3191 (buffer-substring-no-properties sub-end next))
3192 body)))))
3193 (nreverse body)))))
3194 ;; Store output, take care of cursor position and deletion of
3195 ;; list, then return output.
3196 (prog1 (funcall parse-sublist (org-list-get-all-items top struct prevs))
3197 (goto-char top)
3198 (when delete
3199 (delete-region top bottom)
3200 (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re))
3201 (replace-match ""))))))
3203 (defun org-list-make-subtree ()
3204 "Convert the plain list at point into a subtree."
3205 (interactive)
3206 (if (not (ignore-errors (goto-char (org-in-item-p))))
3207 (error "Not in a list")
3208 (let ((list (save-excursion (org-list-to-lisp t))))
3209 (insert (org-list-to-subtree list)))))
3211 (defun org-list-insert-radio-list ()
3212 "Insert a radio list template appropriate for this major mode."
3213 (interactive)
3214 (let* ((e (cl-assoc-if #'derived-mode-p org-list-radio-list-templates))
3215 (txt (nth 1 e))
3216 name pos)
3217 (unless e (error "No radio list setup defined for %s" major-mode))
3218 (setq name (read-string "List name: "))
3219 (while (string-match "%n" txt)
3220 (setq txt (replace-match name t t txt)))
3221 (or (bolp) (insert "\n"))
3222 (setq pos (point))
3223 (insert txt)
3224 (goto-char pos)))
3226 (defun org-list-send-list (&optional maybe)
3227 "Send a transformed version of this list to the receiver position.
3228 With argument MAYBE, fail quietly if no transformation is defined
3229 for this list."
3230 (interactive)
3231 (catch 'exit
3232 (unless (org-at-item-p) (error "Not at a list item"))
3233 (save-excursion
3234 (let ((case-fold-search t))
3235 (re-search-backward "^[ \t]*#\\+ORGLST:" nil t)
3236 (unless (looking-at
3237 "[ \t]*#\\+ORGLST:[ \t]+SEND[ \t]+\\(\\S-+\\)[ \t]+\\([^ \t\n]+\\)")
3238 (if maybe (throw 'exit nil)
3239 (error "Don't know how to transform this list")))))
3240 (let* ((name (regexp-quote (match-string 1)))
3241 (transform (intern (match-string 2)))
3242 (bottom-point
3243 (save-excursion
3244 (re-search-forward
3245 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
3246 (match-beginning 0)))
3247 (top-point
3248 (progn
3249 (re-search-backward "#\\+ORGLST" nil t)
3250 (re-search-forward (org-item-beginning-re) bottom-point t)
3251 (match-beginning 0)))
3252 (plain-list (save-excursion
3253 (goto-char top-point)
3254 (org-list-to-lisp))))
3255 (unless (fboundp transform)
3256 (error "No such transformation function %s" transform))
3257 (let ((txt (funcall transform plain-list)))
3258 ;; Find the insertion(s) place(s).
3259 (save-excursion
3260 (goto-char (point-min))
3261 (let ((receiver-count 0)
3262 (begin-re (format "BEGIN +RECEIVE +ORGLST +%s\\([ \t]\\|$\\)"
3263 name))
3264 (end-re (format "END +RECEIVE +ORGLST +%s\\([ \t]\\|$\\)"
3265 name)))
3266 (while (re-search-forward begin-re nil t)
3267 (cl-incf receiver-count)
3268 (let ((beg (line-beginning-position 2)))
3269 (unless (re-search-forward end-re nil t)
3270 (user-error "Cannot find end of receiver location at %d" beg))
3271 (beginning-of-line)
3272 (delete-region beg (point))
3273 (insert txt "\n")))
3274 (cond
3275 ((> receiver-count 1)
3276 (message "List converted and installed at receiver locations"))
3277 ((= receiver-count 1)
3278 (message "List converted and installed at receiver location"))
3279 (t (user-error "No valid receiver location found")))))))))
3281 (defun org-list-to-generic (list params)
3282 "Convert a LIST parsed through `org-list-to-lisp' to a custom format.
3284 LIST is a list as returned by `org-list-to-lisp', which see.
3285 PARAMS is a property list of parameters used to tweak the output
3286 format.
3288 Valid parameters are:
3290 :backend, :raw
3292 Export back-end used as a basis to transcode elements of the
3293 list, when no specific parameter applies to it. It is also
3294 used to translate its contents. You can prevent this by
3295 setting :raw property to a non-nil value.
3297 :splice
3299 When non-nil, only export the contents of the top most plain
3300 list, effectively ignoring its opening and closing lines.
3302 :ustart, :uend
3304 Strings to start and end an unordered list. They can also be
3305 set to a function returning a string or nil, which will be
3306 called with the depth of the list, counting from 1.
3308 :ostart, :oend
3310 Strings to start and end an ordered list. They can also be set
3311 to a function returning a string or nil, which will be called
3312 with the depth of the list, counting from 1.
3314 :dstart, :dend
3316 Strings to start and end a descriptive list. They can also be
3317 set to a function returning a string or nil, which will be
3318 called with the depth of the list, counting from 1.
3320 :dtstart, :dtend, :ddstart, :ddend
3322 Strings to start and end a descriptive term.
3324 :istart, :iend
3326 Strings to start or end a list item, and to start a list item
3327 with a counter. They can also be set to a function returning
3328 a string or nil, which will be called with two arguments: the
3329 type of list and the depth of the item, counting from 1.
3331 :icount
3333 Strings to start a list item with a counter. It can also be
3334 set to a function returning a string or nil, which will be
3335 called with three arguments: the type of list, the depth of the
3336 item, counting from 1, and the counter. Its value, when
3337 non-nil, has precedence over `:istart'.
3339 :isep
3341 String used to separate items. It can also be set to
3342 a function returning a string or nil, which will be called with
3343 two arguments: the type of list and the depth of the item,
3344 counting from 1. It always start on a new line.
3346 :ifmt
3348 Function to be applied to the contents of every item. It is
3349 called with two arguments: the type of list and the contents.
3351 :cbon, :cboff, :cbtrans
3353 String to insert, respectively, an un-checked check-box,
3354 a checked check-box and a check-box in transitional state."
3355 (require 'ox)
3356 (let* ((backend (plist-get params :backend))
3357 (custom-backend
3358 (org-export-create-backend
3359 :parent (or backend 'org)
3360 :transcoders
3361 `((plain-list . ,(org-list--to-generic-plain-list params))
3362 (item . ,(org-list--to-generic-item params))
3363 (macro . (lambda (m c i) (org-element-macro-interpreter m nil))))))
3364 data info)
3365 ;; Write LIST back into Org syntax and parse it.
3366 (with-temp-buffer
3367 (let ((org-inhibit-startup t)) (org-mode))
3368 (letrec ((insert-list
3369 (lambda (l)
3370 (dolist (i (cdr l))
3371 (funcall insert-item i (car l)))))
3372 (insert-item
3373 (lambda (i type)
3374 (let ((start (point)))
3375 (insert (if (eq type 'ordered) "1. " "- "))
3376 (dolist (e i)
3377 (if (consp e) (funcall insert-list e)
3378 (insert e)
3379 (insert "\n")))
3380 (beginning-of-line)
3381 (save-excursion
3382 (let ((ind (if (eq type 'ordered) 3 2)))
3383 (while (> (point) start)
3384 (unless (looking-at-p "[ \t]*$")
3385 (indent-to ind))
3386 (forward-line -1))))))))
3387 (funcall insert-list list))
3388 (setf data
3389 (org-element-map (org-element-parse-buffer) 'plain-list
3390 #'identity nil t))
3391 (setf info (org-export-get-environment backend nil params)))
3392 (when (and backend (symbolp backend) (not (org-export-get-backend backend)))
3393 (user-error "Unknown :backend value"))
3394 (unless backend (require 'ox-org))
3395 ;; When`:raw' property has a non-nil value, turn all objects back
3396 ;; into Org syntax.
3397 (when (and backend (plist-get params :raw))
3398 (org-element-map data org-element-all-objects
3399 (lambda (object)
3400 (org-element-set-element
3401 object (org-element-interpret-data object)))))
3402 ;; We use a low-level mechanism to export DATA so as to skip all
3403 ;; usual pre-processing and post-processing, i.e., hooks, filters,
3404 ;; Babel code evaluation, include keywords and macro expansion,
3405 ;; and filters.
3406 (let ((output (org-export-data-with-backend data custom-backend info)))
3407 ;; Remove final newline.
3408 (if (org-string-nw-p output) (substring-no-properties output 0 -1) ""))))
3410 (defun org-list--depth (element)
3411 "Return the level of ELEMENT within current plain list.
3412 ELEMENT is either an item or a plain list."
3413 (cl-count-if (lambda (ancestor) (eq (org-element-type ancestor) 'plain-list))
3414 (org-element-lineage element nil t)))
3416 (defun org-list--trailing-newlines (string)
3417 "Return the number of trailing newlines in STRING."
3418 (with-temp-buffer
3419 (insert string)
3420 (skip-chars-backward " \t\n")
3421 (count-lines (line-beginning-position 2) (point-max))))
3423 (defun org-list--generic-eval (value &rest args)
3424 "Evaluate VALUE according to its type.
3425 VALUE is either nil, a string or a function. In the latter case,
3426 it is called with arguments ARGS."
3427 (cond ((null value) nil)
3428 ((stringp value) value)
3429 ((functionp value) (apply value args))
3430 (t (error "Wrong value: %s" value))))
3432 (defun org-list--to-generic-plain-list (params)
3433 "Return a transcoder for `plain-list' elements.
3434 PARAMS is a plist used to tweak the behavior of the transcoder."
3435 (let ((ustart (plist-get params :ustart))
3436 (uend (plist-get params :uend))
3437 (ostart (plist-get params :ostart))
3438 (oend (plist-get params :oend))
3439 (dstart (plist-get params :dstart))
3440 (dend (plist-get params :dend))
3441 (splice (plist-get params :splice))
3442 (backend (plist-get params :backend)))
3443 (lambda (plain-list contents info)
3444 (let* ((type (org-element-property :type plain-list))
3445 (depth (org-list--depth plain-list))
3446 (start (and (not splice)
3447 (org-list--generic-eval
3448 (pcase type
3449 (`ordered ostart)
3450 (`unordered ustart)
3451 (_ dstart))
3452 depth)))
3453 (end (and (not splice)
3454 (org-list--generic-eval
3455 (pcase type
3456 (`ordered oend)
3457 (`unordered uend)
3458 (_ dend))
3459 depth))))
3460 ;; Make sure trailing newlines in END appear in the output by
3461 ;; setting `:post-blank' property to their number.
3462 (when end
3463 (org-element-put-property
3464 plain-list :post-blank (org-list--trailing-newlines end)))
3465 ;; Build output.
3466 (concat (and start (concat start "\n"))
3467 (if (or start end splice (not backend))
3468 contents
3469 (org-export-with-backend backend plain-list contents info))
3470 end)))))
3472 (defun org-list--to-generic-item (params)
3473 "Return a transcoder for `item' elements.
3474 PARAMS is a plist used to tweak the behavior of the transcoder."
3475 (let ((backend (plist-get params :backend))
3476 (istart (plist-get params :istart))
3477 (iend (plist-get params :iend))
3478 (isep (plist-get params :isep))
3479 (icount (plist-get params :icount))
3480 (ifmt (plist-get params :ifmt))
3481 (cboff (plist-get params :cboff))
3482 (cbon (plist-get params :cbon))
3483 (cbtrans (plist-get params :cbtrans))
3484 (dtstart (plist-get params :dtstart))
3485 (dtend (plist-get params :dtend))
3486 (ddstart (plist-get params :ddstart))
3487 (ddend (plist-get params :ddend)))
3488 (lambda (item contents info)
3489 (let* ((type
3490 (org-element-property :type (org-element-property :parent item)))
3491 (tag (org-element-property :tag item))
3492 (depth (org-list--depth item))
3493 (separator (and (org-export-get-next-element item info)
3494 (org-list--generic-eval isep type depth)))
3495 (closing (pcase (org-list--generic-eval iend type depth)
3496 ((or `nil "") "\n")
3497 ((and (guard separator) s)
3498 (if (equal (substring s -1) "\n") s (concat s "\n")))
3499 (s s))))
3500 ;; When a closing line or a separator is provided, make sure
3501 ;; its trailing newlines are taken into account when building
3502 ;; output. This is done by setting `:post-blank' property to
3503 ;; the number of such lines in the last line to be added.
3504 (let ((last-string (or separator closing)))
3505 (when last-string
3506 (org-element-put-property
3507 item
3508 :post-blank
3509 (max (1- (org-list--trailing-newlines last-string)) 0))))
3510 ;; Build output.
3511 (concat
3512 (let ((c (org-element-property :counter item)))
3513 (if (and c icount) (org-list--generic-eval icount type depth c)
3514 (org-list--generic-eval istart type depth)))
3515 (let ((body
3516 (if (or istart iend icount ifmt cbon cboff cbtrans (not backend)
3517 (and (eq type 'descriptive)
3518 (or dtstart dtend ddstart ddend)))
3519 (concat
3520 (pcase (org-element-property :checkbox item)
3521 (`on cbon)
3522 (`off cboff)
3523 (`trans cbtrans))
3524 (and tag
3525 (concat dtstart
3526 (if backend
3527 (org-export-data-with-backend
3528 tag backend info)
3529 (org-element-interpret-data tag))
3530 dtend))
3531 (and tag ddstart)
3532 (let ((contents
3533 (if (= (length contents) 0) ""
3534 (substring contents 0 -1))))
3535 (if ifmt (org-list--generic-eval ifmt type contents)
3536 contents))
3537 (and tag ddend))
3538 (org-export-with-backend backend item contents info))))
3539 ;; Remove final newline.
3540 (if (equal body "") ""
3541 (substring (org-element-normalize-string body) 0 -1)))
3542 closing
3543 separator)))))
3545 (defun org-list-to-latex (list &optional params)
3546 "Convert LIST into a LaTeX list.
3547 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3548 PARAMS is a property list with overruling parameters for
3549 `org-list-to-generic'. Return converted list as a string."
3550 (require 'ox-latex)
3551 (org-list-to-generic list (org-combine-plists '(:backend latex) params)))
3553 (defun org-list-to-html (list &optional params)
3554 "Convert LIST into a HTML list.
3555 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3556 PARAMS is a property list with overruling parameters for
3557 `org-list-to-generic'. Return converted list as a string."
3558 (require 'ox-html)
3559 (org-list-to-generic list (org-combine-plists '(:backend html) params)))
3561 (defun org-list-to-texinfo (list &optional params)
3562 "Convert LIST into a Texinfo list.
3563 LIST is a parsed plain list, as returned by `org-list-to-lisp'.
3564 PARAMS is a property list with overruling parameters for
3565 `org-list-to-generic'. Return converted list as a string."
3566 (require 'ox-texinfo)
3567 (org-list-to-generic list (org-combine-plists '(:backend texinfo) params)))
3569 (defun org-list-to-org (list &optional params)
3570 "Convert LIST into an Org plain list.
3571 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3572 with overruling parameters for `org-list-to-generic'."
3573 (let* ((make-item
3574 (lambda (type _depth &optional c)
3575 (concat (if (eq type 'ordered) "1. " "- ")
3576 (and c (format "[@%d] " c)))))
3577 (defaults
3578 (list :istart make-item
3579 :icount make-item
3580 :ifmt (lambda (_type contents)
3581 (replace-regexp-in-string "\n" "\n " contents))
3582 :dtend " :: "
3583 :cbon "[X] "
3584 :cboff "[ ] "
3585 :cbtrans "[-] ")))
3586 (org-list-to-generic list (org-combine-plists defaults params))))
3588 (defun org-list-to-subtree (list &optional params)
3589 "Convert LIST into an Org subtree.
3590 LIST is as returned by `org-list-to-lisp'. PARAMS is a property
3591 list with overruling parameters for `org-list-to-generic'."
3592 (let* ((blank (pcase (cdr (assq 'heading org-blank-before-new-entry))
3593 (`t t)
3594 (`auto (save-excursion
3595 (org-with-limited-levels (outline-previous-heading))
3596 (org-previous-line-empty-p)))))
3597 (level (org-reduced-level (or (org-current-level) 0)))
3598 (make-stars
3599 (lambda (_type depth &optional _count)
3600 ;; Return the string for the heading, depending on DEPTH
3601 ;; of current sub-list.
3602 (let ((oddeven-level (+ level depth)))
3603 (concat (make-string (if org-odd-levels-only
3604 (1- (* 2 oddeven-level))
3605 oddeven-level)
3607 " ")))))
3608 (org-list-to-generic
3609 list
3610 (org-combine-plists
3611 (list :splice t
3612 :istart make-stars
3613 :icount make-stars
3614 :dtstart " " :dtend " "
3615 :isep (if blank "\n\n" "\n")
3616 :cbon "DONE " :cboff "TODO " :cbtrans "TODO ")
3617 params))))
3619 (provide 'org-list)
3621 ;;; org-list.el ends here