1 ;;; org-list.el --- Plain lists for Org-mode
3 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Bastien Guerry <bzg@gnu.org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains the code dealing with plain lists in Org-mode.
30 ;; The core concept behind lists is their structure. A structure is
31 ;; a snapshot of the list, in the shape of a data tree (see
32 ;; `org-list-struct').
34 ;; Once the list structure is stored, it is possible to make changes
35 ;; on it that will be mirrored to the real list or to get information
36 ;; about the list, using accessors and methods provided in the
37 ;; library. Most of them require the use of one or two helper
38 ;; functions, namely `org-list-parents-alist' and
39 ;; `org-list-prevs-alist'.
41 ;; Structure is eventually applied to the buffer with
42 ;; `org-list-write-struct'. This function repairs (bullets,
43 ;; indentation, checkboxes) the list in the process. It should be
44 ;; called near the end of any function working on structures.
46 ;; Thus, a function applying to lists should usually follow this
49 ;; 1. Verify point is in a list and grab item beginning (with the same
50 ;; function `org-in-item-p'). If the function requires the cursor
51 ;; to be at item's bullet, `org-at-item-p' is more selective. It
52 ;; is also possible to move point to the closest item with
53 ;; `org-list-search-backward', or `org-list-search-forward',
54 ;; applied to the function `org-item-beginning-re'.
56 ;; 2. Get list structure with `org-list-struct'.
58 ;; 3. Compute one, or both, helper functions,
59 ;; (`org-list-parents-alist', `org-list-prevs-alist') depending on
62 ;; 4. Proceed with the modifications, using methods and accessors.
64 ;; 5. Verify and apply structure to buffer, using
65 ;; `org-list-write-struct'.
67 ;; 6. If changes made to the list might have modified check-boxes,
68 ;; call `org-update-checkbox-count-maybe'.
70 ;; Computing a structure can be a costly operation on huge lists (a
71 ;; few thousand lines long). Thus, code should follow the rule:
72 ;; "collect once, use many". As a corollary, it is usually a bad idea
73 ;; to use directly an interactive function inside the code, as those,
74 ;; being independent entities, read the whole list structure another
84 (defvar org-M-RET-may-split-line
)
85 (defvar org-auto-align-tags
)
86 (defvar org-blank-before-new-entry
)
87 (defvar org-clock-string
)
88 (defvar org-closed-string
)
89 (defvar org-deadline-string
)
90 (defvar org-description-max-indent
)
91 (defvar org-odd-levels-only
)
92 (defvar org-scheduled-string
)
93 (defvar org-ts-regexp
)
94 (defvar org-ts-regexp-both
)
96 (declare-function outline-invisible-p
"outline" (&optional pos
))
97 (declare-function outline-flag-region
"outline" (from to flag
))
98 (declare-function outline-next-heading
"outline" ())
99 (declare-function outline-previous-heading
"outline" ())
101 (declare-function org-at-heading-p
"org" (&optional ignored
))
102 (declare-function org-before-first-heading-p
"org" ())
103 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
104 (declare-function org-combine-plists
"org" (&rest plists
))
105 (declare-function org-count
"org" (cl-item cl-seq
))
106 (declare-function org-current-level
"org" ())
107 (declare-function org-entry-get
"org"
108 (pom property
&optional inherit literal-nil
))
109 (declare-function org-fix-tags-on-the-fly
"org" ())
110 (declare-function org-get-indentation
"org" (&optional line
))
111 (declare-function org-icompleting-read
"org" (&rest args
))
112 (declare-function org-in-block-p
"org" (names))
113 (declare-function org-in-regexp
"org" (re &optional nlines visually
))
114 (declare-function org-level-increment
"org" ())
115 (declare-function org-narrow-to-subtree
"org" ())
116 (declare-function org-at-heading-p
"org" (&optional invisible-ok
))
117 (declare-function org-previous-line-empty-p
"org" ())
118 (declare-function org-remove-if
"org" (predicate seq
))
119 (declare-function org-reduced-level
"org" (L))
120 (declare-function org-show-subtree
"org" ())
121 (declare-function org-sort-remove-invisible
"org" (S))
122 (declare-function org-time-string-to-seconds
"org" (s))
123 (declare-function org-timer-hms-to-secs
"org-timer" (hms))
124 (declare-function org-timer-item
"org-timer" (&optional arg
))
125 (declare-function org-trim
"org" (s))
126 (declare-function org-uniquify
"org" (list))
128 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
129 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
130 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
131 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
133 (declare-function org-export-string-as
"ox"
134 (string backend
&optional body-only ext-plist
))
139 ;;; Configuration variables
141 (defgroup org-plain-lists nil
142 "Options concerning plain lists in Org-mode."
143 :tag
"Org Plain lists"
144 :group
'org-structure
)
146 (defcustom org-cycle-include-plain-lists t
147 "When t, make TAB cycle visibility on plain list items.
148 Cycling plain lists works only when the cursor is on a plain list
149 item. When the cursor is on an outline heading, plain lists are
150 treated as text. This is the most stable way of handling this,
151 which is why it is the default.
153 When this is the symbol `integrate', then integrate plain list
154 items when cycling, as if they were children of outline headings.
156 This setting can lead to strange effects when switching visibility
157 to `children', because the first \"child\" in a subtree decides
158 what children should be listed. If that first \"child\" is a
159 plain list item with an implied large level number, all true
160 children and grand children of the outline heading will be
161 exposed in a children' view."
162 :group
'org-plain-lists
165 (const :tag
"Never" nil
)
166 (const :tag
"With cursor in plain list (recommended)" t
)
167 (const :tag
"As children of outline headings" integrate
)))
169 (defcustom org-list-demote-modify-bullet nil
170 "Default bullet type installed when demoting an item.
171 This is an association list, for each bullet type, this alist will point
172 to the bullet that should be used when this item is demoted.
175 (setq org-list-demote-modify-bullet
176 '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
181 + Silence of the Lambs
184 + The Hunt for Red October
190 - Silence of the Lambs
193 - The Hunt for Red October
195 :group
'org-plain-lists
198 (choice :tag
"If the current bullet is "
204 (choice :tag
"demotion will change it to"
211 (defcustom org-plain-list-ordered-item-terminator t
212 "The character that makes a line with leading number an ordered list item.
213 Valid values are ?. and ?\). To get both terminators, use t."
214 :group
'org-plain-lists
215 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
216 (const :tag
"paren like in \"2)\"" ?\
))
217 (const :tag
"both" t
)))
219 (define-obsolete-variable-alias 'org-alphabetical-lists
220 'org-list-allow-alphabetical
"24.4") ; Since 8.0
221 (defcustom org-list-allow-alphabetical nil
222 "Non-nil means single character alphabetical bullets are allowed.
224 Both uppercase and lowercase are handled. Lists with more than
225 26 items will fallback to standard numbering. Alphabetical
226 counters like \"[@c]\" will be recognized.
228 This variable needs to be set before org.el is loaded. If you
229 need to make a change while Emacs is running, use the customize
230 interface or run the following code after updating it:
232 \(when (featurep 'org-element) (load \"org-element\" t t))"
233 :group
'org-plain-lists
236 :set
(lambda (var val
)
237 (when (featurep 'org-element
) (load "org-element" t t
))
240 (defcustom org-list-two-spaces-after-bullet-regexp nil
241 "A regular expression matching bullets that should have 2 spaces after them.
242 When nil, no bullet will have two spaces after them. When
243 a string, it will be used as a regular expression. When the
244 bullet type of a list is changed, the new bullet type will be
245 matched against this regexp. If it matches, there will be two
246 spaces instead of one after the bullet in each item of the list."
247 :group
'org-plain-lists
249 (const :tag
"never" nil
)
252 (define-obsolete-variable-alias 'org-empty-line-terminates-plain-lists
253 'org-list-empty-line-terminates-plain-lists
"24.4") ;; Since 8.0
254 (defcustom org-list-empty-line-terminates-plain-lists nil
255 "Non-nil means an empty line ends all plain list levels.
256 Otherwise, two of them will be necessary."
257 :group
'org-plain-lists
260 (defcustom org-list-automatic-rules
'((checkbox . t
)
262 "Non-nil means apply set of rules when acting on lists.
263 By default, automatic actions are taken when using
264 \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
265 \\[org-shiftmetaright], \\[org-shiftmetaleft],
266 \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
267 \\[org-insert-todo-heading]. You can disable individually these
268 rules by setting them to nil. Valid rules are:
270 checkbox when non-nil, checkbox statistics is updated each time
271 you either insert a new checkbox or toggle a checkbox.
272 indent when non-nil, indenting or outdenting list top-item
273 with its subtree will move the whole list and
274 outdenting a list whose bullet is * to column 0 will
275 change that bullet to \"-\"."
276 :group
'org-plain-lists
278 :type
'(alist :tag
"Sets of rules"
281 (const :tag
"Checkbox" checkbox
)
282 (const :tag
"Indent" indent
))
284 (boolean :tag
"Activate" :value t
)))
286 (defcustom org-list-use-circular-motion nil
287 "Non-nil means commands implying motion in lists should be cyclic.
289 In that case, the item following the last item is the first one,
290 and the item preceding the first item is the last one.
292 This affects the behavior of \\[org-move-item-up],
293 \\[org-move-item-down], \\[org-next-item] and
294 \\[org-previous-item]."
295 :group
'org-plain-lists
299 (defvar org-checkbox-statistics-hook nil
300 "Hook that is run whenever Org thinks checkbox statistics should be updated.
301 This hook runs even if checkbox rule in
302 `org-list-automatic-rules' does not apply, so it can be used to
303 implement alternative ways of collecting statistics
306 (define-obsolete-variable-alias 'org-hierarchical-checkbox-statistics
307 'org-checkbox-hierarchical-statistics
"24.4") ;; Since 8.0
308 (defcustom org-checkbox-hierarchical-statistics t
309 "Non-nil means checkbox statistics counts only the state of direct children.
310 When nil, all boxes below the cookie are counted.
311 This can be set to nil on a per-node basis using a COOKIE_DATA property
312 with the word \"recursive\" in the value."
313 :group
'org-plain-lists
316 (org-defvaralias 'org-description-max-indent
317 'org-list-description-max-indent
) ;; Since 8.0
318 (defcustom org-list-description-max-indent
20
319 "Maximum indentation for the second line of a description list.
320 When the indentation would be larger than this, it will become
321 5 characters instead."
322 :group
'org-plain-lists
325 (defcustom org-list-indent-offset
0
326 "Additional indentation for sub-items in a list.
327 By setting this to a small number, usually 1 or 2, one can more
328 clearly distinguish sub-items in a list."
329 :group
'org-plain-lists
333 (defcustom org-list-radio-list-templates
334 '((latex-mode "% BEGIN RECEIVE ORGLST %n
335 % END RECEIVE ORGLST %n
337 #+ORGLST: SEND %n org-list-to-latex
340 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
341 @c END RECEIVE ORGLST %n
343 #+ORGLST: SEND %n org-list-to-texinfo
346 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
347 <!-- END RECEIVE ORGLST %n -->
349 #+ORGLST: SEND %n org-list-to-html
352 "Templates for radio lists in different major modes.
353 All occurrences of %n in a template will be replaced with the name of the
354 list, obtained by prompting the user."
355 :group
'org-plain-lists
357 (list (symbol :tag
"Major mode")
358 (string :tag
"Format"))))
360 (defvar org-list-forbidden-blocks
'("example" "verse" "src" "ascii" "beamer"
361 "html" "latex" "odt")
362 "Names of blocks where lists are not allowed.
363 Names must be in lower case.")
365 (defvar org-list-export-context
'(block inlinetask
)
366 "Context types where lists will be interpreted during export.
368 Valid types are `drawer', `inlinetask' and `block'. More
369 specifically, type `block' is determined by the variable
370 `org-list-forbidden-blocks'.")
374 ;;; Predicates and regexps
376 (defconst org-list-end-re
(if org-list-empty-line-terminates-plain-lists
"^[ \t]*\n"
378 "Regex corresponding to the end of a list.
379 It depends on `org-list-empty-line-terminates-plain-lists'.")
381 (defconst org-list-full-item-re
382 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
383 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
384 "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
385 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
386 "Matches a list item and puts everything into groups:
390 group 4: description tag")
392 (defun org-item-re ()
393 "Return the correct regular expression for plain lists."
395 ((eq org-plain-list-ordered-item-terminator t
) "[.)]")
396 ((= org-plain-list-ordered-item-terminator ?\
)) ")")
397 ((= org-plain-list-ordered-item-terminator ?.
) "\\.")
399 (alpha (if org-list-allow-alphabetical
"\\|[A-Za-z]" "")))
400 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha
"\\)" term
401 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
403 (defsubst org-item-beginning-re
()
404 "Regexp matching the beginning of a plain list item."
405 (concat "^" (org-item-re)))
407 (defun org-list-at-regexp-after-bullet-p (regexp)
408 "Is point at a list item with REGEXP after bullet?"
411 (goto-char (match-end 0))
412 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
413 (if org-list-allow-alphabetical
414 "\\([0-9]+\\|[A-Za-z]\\)"
417 ;; Ignore counter if any
418 (when (looking-at counter-re
) (goto-char (match-end 0))))
419 (looking-at regexp
))))
421 (defun org-list-in-valid-context-p ()
422 "Is point in a context where lists are allowed?"
423 (not (org-in-block-p org-list-forbidden-blocks
)))
425 (defun org-in-item-p ()
426 "Return item beginning position when in a plain list, nil otherwise."
429 (let* ((case-fold-search t
)
430 (context (org-list-context))
431 (lim-up (car context
))
432 (inlinetask-re (and (featurep 'org-inlinetask
)
433 (org-inlinetask-outline-regexp)))
434 (item-re (org-item-re))
435 ;; Indentation isn't meaningful when point starts at an empty
436 ;; line or an inline task.
437 (ind-ref (if (or (looking-at "^[ \t]*$")
438 (and inlinetask-re
(looking-at inlinetask-re
)))
440 (org-get-indentation))))
442 ((eq (nth 2 context
) 'invalid
) nil
)
443 ((looking-at item-re
) (point))
445 ;; Detect if cursor in amidst `org-list-end-re'. First, count
446 ;; number HL of hard lines it takes, then call `org-in-regexp'
447 ;; to compute its boundaries END-BOUNDS. When point is
448 ;; in-between, move cursor before regexp beginning.
449 (let ((hl 0) (i -
1) end-bounds
)
451 (while (setq i
(string-match
452 "[\r\n]" org-list-end-re
(1+ i
)))
454 (setq end-bounds
(org-in-regexp org-list-end-re hl
)))
455 (>= (point) (car end-bounds
))
456 (< (point) (cdr end-bounds
)))
457 (goto-char (car end-bounds
))
459 ;; Look for an item, less indented that reference line.
462 (let ((ind (org-get-indentation)))
464 ;; This is exactly what we want.
465 ((and (looking-at item-re
) (< ind ind-ref
))
466 (throw 'exit
(point)))
467 ;; At upper bound of search or looking at the end of a
468 ;; previous list: search is over.
469 ((<= (point) lim-up
) (throw 'exit nil
))
470 ((looking-at org-list-end-re
) (throw 'exit nil
))
471 ;; Skip blocks, drawers, inline-tasks, blank lines
472 ((and (looking-at "^[ \t]*#\\+end_")
473 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
474 ((and (looking-at "^[ \t]*:END:")
475 (re-search-backward org-drawer-regexp lim-up t
))
477 ((and inlinetask-re
(looking-at inlinetask-re
))
478 (org-inlinetask-goto-beginning)
480 ((looking-at "^[ \t]*$") (forward-line -
1))
481 ;; Text at column 0 cannot belong to a list: stop.
482 ((zerop ind
) (throw 'exit nil
))
483 ;; Normal text less indented than reference line, take
484 ;; it as new reference.
488 (t (forward-line -
1)))))))))))
490 (defun org-at-item-p ()
491 "Is point in a line starting a hand-formatted item?"
494 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
496 (defun org-at-item-bullet-p ()
497 "Is point at the bullet of a plain list item?"
499 (not (member (char-after) '(?\ ?
\t)))
500 (< (point) (match-end 0))))
502 (defun org-at-item-timer-p ()
503 "Is point at a line starting a plain list item with a timer?"
504 (org-list-at-regexp-after-bullet-p
505 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
507 (defun org-at-item-description-p ()
508 "Is point at a description list item?"
509 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::\\([ \t]+\\|$\\)"))
511 (defun org-at-item-checkbox-p ()
512 "Is point at a line starting a plain-list item with a checklet?"
513 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
515 (defun org-at-item-counter-p ()
516 "Is point at a line starting a plain-list item with a counter?"
518 (looking-at org-list-full-item-re
)
523 ;;; Structures and helper functions
525 (defun org-list-context ()
526 "Determine context, and its boundaries, around point.
528 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
529 are boundaries and CONTEXT is a symbol among `drawer', `block',
530 `invalid', `inlinetask' and nil.
532 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
535 (org-with-limited-levels
537 (let ((case-fold-search t
) (pos (point)) beg end context-type
538 ;; Get positions of surrounding headings. This is the
540 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t
))
543 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
544 ;; Is point inside a drawer?
545 (let ((end-re "^[ \t]*:END:")
546 (beg-re org-drawer-regexp
))
547 (when (save-excursion
548 (and (not (looking-at beg-re
))
549 (not (looking-at end-re
))
550 (setq beg
(and (re-search-backward beg-re lim-up t
)
551 (1+ (point-at-eol))))
552 (setq end
(or (and (re-search-forward end-re lim-down t
)
553 (1- (match-beginning 0)))
556 (setq lim-up beg lim-down end context-type
'drawer
)))
557 ;; Is point strictly in a block, and of which type?
558 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type
)
559 (when (save-excursion
560 (and (not (looking-at block-re
))
561 (setq beg
(and (re-search-backward block-re lim-up t
)
562 (1+ (point-at-eol))))
563 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
564 (setq type
(downcase (match-string 1)))
566 (setq end
(or (and (re-search-forward block-re lim-down t
)
570 (equal (downcase (match-string 1)) "end")))
571 (setq lim-up beg lim-down end
572 context-type
(if (member type org-list-forbidden-blocks
)
574 ;; Is point in an inlinetask?
575 (when (and (featurep 'org-inlinetask
)
577 (let* ((beg-re (org-inlinetask-outline-regexp))
578 (end-re (concat beg-re
"END[ \t]*$")))
579 (and (not (looking-at "^\\*+"))
580 (setq beg
(and (re-search-backward beg-re lim-up t
)
581 (1+ (point-at-eol))))
582 (not (looking-at end-re
))
583 (setq end
(and (re-search-forward end-re lim-down t
)
584 (1- (match-beginning 0))))
586 (setq lim-up beg lim-down end context-type
'inlinetask
))
587 ;; Return context boundaries and type.
588 (list lim-up lim-down context-type
))))))
590 (defun org-list-struct ()
591 "Return structure of list at point.
593 A list structure is an alist where key is point at item, and
596 2. bullet with trailing whitespace,
597 3. bullet counter, if any,
599 5. description tag, if any,
600 6. position at item end.
602 Thus the following list, where numbers in parens are
607 5. [@5] sub-item 2 (34)
608 some other text belonging to first item (55)
610 + tag :: description (109)
613 will get the following structure:
615 \(\(1 0 \"- \" nil \"[X]\" nil 97\)
616 \(18 2 \"1. \" nil nil nil 34\)
617 \(34 2 \"5. \" \"5\" nil nil 55\)
618 \(97 0 \"- \" nil nil nil 131\)
619 \(109 2 \"+ \" nil nil \"tag\" 131\)
621 Assume point is at an item."
624 (let* ((case-fold-search t
)
625 (context (org-list-context))
626 (lim-up (car context
))
627 (lim-down (nth 1 context
))
629 (item-re (org-item-re))
630 (inlinetask-re (and (featurep 'org-inlinetask
)
631 (org-inlinetask-outline-regexp)))
632 (beg-cell (cons (point) (org-get-indentation)))
633 ind itm-lst itm-lst-2 end-lst end-lst-2 struct
636 ;; Return association at point.
638 (looking-at org-list-full-item-re
)
639 (let ((bullet (match-string-no-properties 1)))
643 (match-string-no-properties 2) ; counter
644 (match-string-no-properties 3) ; checkbox
646 (and (save-match-data (string-match "[-+*]" bullet
))
647 (match-string-no-properties 4)))))))
650 ;; Ensure list ends at the first blank line.
652 (skip-chars-backward " \r\t\n")
653 (min (1+ (point-at-eol)) lim-down
)))))
654 ;; 1. Read list from starting item to its beginning, and save
655 ;; top item position and indentation in BEG-CELL. Also store
656 ;; ending position of items in END-LST.
660 (let ((ind (org-get-indentation)))
663 ;; At upward limit: if we ended at an item, store it,
664 ;; else dismiss useless data recorded above BEG-CELL.
668 (if (not (looking-at item-re
))
669 (memq (assq (car beg-cell
) itm-lst
) itm-lst
)
670 (setq beg-cell
(cons (point) ind
))
671 (cons (funcall assoc-at-point ind
) itm-lst
)))))
672 ;; Looking at a list ending regexp. Dismiss useless
673 ;; data recorded above BEG-CELL. Jump to part 2.
674 ((looking-at org-list-end-re
)
677 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
678 ;; Point is at an item. Add data to ITM-LST. It may
679 ;; also end a previous item: save it in END-LST. If
680 ;; ind is less or equal than BEG-CELL and there is no
681 ;; end at this ind or lesser, this item becomes the new
683 ((looking-at item-re
)
684 (push (funcall assoc-at-point ind
) itm-lst
)
685 (push (cons ind
(point)) end-lst
)
686 (when (< ind text-min-ind
) (setq beg-cell
(cons (point) ind
)))
688 ;; Skip blocks, drawers, inline tasks, blank lines.
689 ((and (looking-at "^[ \t]*#\\+end_")
690 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
691 ((and (looking-at "^[ \t]*:END:")
692 (re-search-backward org-drawer-regexp lim-up t
))
694 ((and inlinetask-re
(looking-at inlinetask-re
))
695 (org-inlinetask-goto-beginning)
697 ((looking-at "^[ \t]*$")
699 ;; From there, point is not at an item. Interpret
700 ;; line's indentation:
701 ;; - text at column 0 is necessarily out of any list.
702 ;; Dismiss data recorded above BEG-CELL. Jump to
704 ;; - any other case may be an ending position for an
705 ;; hypothetical item above. Store it and proceed.
709 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
711 (when (< ind text-min-ind
) (setq text-min-ind ind
))
712 (push (cons ind
(point)) end-lst
)
713 (forward-line -
1)))))))
714 ;; 2. Read list from starting point to its end, that is until we
715 ;; get out of context, or that a non-item line is less or
716 ;; equally indented than BEG-CELL's cdr. Also, store ending
717 ;; position of items in END-LST-2.
720 (let ((ind (org-get-indentation)))
722 ((>= (point) lim-down
)
723 ;; At downward limit: this is de facto the end of the
724 ;; list. Save point as an ending position, and jump to
727 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
728 ;; Looking at a list ending regexp. Save point as an
729 ;; ending position and jump to part 3.
730 ((looking-at org-list-end-re
)
731 (throw 'exit
(push (cons 0 (point)) end-lst-2
)))
732 ((looking-at item-re
)
733 ;; Point is at an item. Add data to ITM-LST-2. It may
734 ;; also end a previous item, so save it in END-LST-2.
735 (push (funcall assoc-at-point ind
) itm-lst-2
)
736 (push (cons ind
(point)) end-lst-2
)
738 ;; Skip inline tasks and blank lines along the way
739 ((and inlinetask-re
(looking-at inlinetask-re
))
740 (org-inlinetask-goto-end))
741 ((looking-at "^[ \t]*$")
743 ;; Ind is lesser or equal than BEG-CELL's. The list is
744 ;; over: store point as an ending position and jump to
746 ((<= ind
(cdr beg-cell
))
748 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
749 ;; Else, if ind is lesser or equal than previous item's,
750 ;; this is an ending position: store it. In any case,
751 ;; skip block or drawer at point, and move to next line.
753 (when (<= ind
(nth 1 (car itm-lst-2
)))
754 (push (cons ind
(point)) end-lst-2
))
756 ((and (looking-at "^[ \t]*#\\+begin_")
757 (re-search-forward "^[ \t]*#\\+end_" lim-down t
)))
758 ((and (looking-at org-drawer-regexp
)
759 (re-search-forward "^[ \t]*:END:" lim-down t
))))
760 (forward-line 1))))))
761 (setq struct
(append itm-lst
(cdr (nreverse itm-lst-2
)))
762 end-lst
(append end-lst
(cdr (nreverse end-lst-2
))))
763 ;; 3. Associate each item to its end position.
764 (org-list-struct-assoc-end struct end-lst
)
768 (defun org-list-struct-assoc-end (struct end-list
)
769 "Associate proper ending point to items in STRUCT.
771 END-LIST is a pseudo-alist where car is indentation and cdr is
774 This function modifies STRUCT."
775 (let ((endings end-list
))
778 (let ((pos (car elt
))
780 ;; Remove end candidates behind current item.
781 (while (or (<= (cdar endings
) pos
))
783 ;; Add end position to item assoc.
784 (let ((old-end (nthcdr 6 elt
))
785 (new-end (assoc-default ind endings
'<=)))
787 (setcar old-end new-end
)
788 (setcdr elt
(append (cdr elt
) (list new-end
)))))))
791 (defun org-list-prevs-alist (struct)
792 "Return alist between item and previous item in STRUCT."
793 (let ((item-end-alist (mapcar (lambda (e) (cons (car e
) (nth 6 e
)))
796 (let ((prev (car (rassq (car e
) item-end-alist
))))
797 (cons (car e
) prev
)))
800 (defun org-list-parents-alist (struct)
801 "Return alist between item and parent in STRUCT."
802 (let* ((ind-to-ori (list (list (nth 1 (car struct
)))))
803 (top-item (org-list-get-top-point struct
))
804 (prev-pos (list top-item
)))
806 (mapcar (lambda (item)
807 (let ((pos (car item
))
809 (prev-ind (caar ind-to-ori
)))
813 ;; A sub-list is over. Find the associated
814 ;; origin in IND-TO-ORI. If it cannot be
815 ;; found (ill-formed list), set its parent as
816 ;; the first item less indented. If there is
817 ;; none, make it a top-level item.
819 (or (member (assq ind ind-to-ori
) ind-to-ori
)
823 (when (< (car e
) ind
)
824 (throw 'exit
(member e ind-to-ori
))))
827 (cons pos
(cdar ind-to-ori
)))
828 ;; A sub-list starts. Every item at IND will
829 ;; have previous item as its parent.
831 (let ((origin (nth 1 prev-pos
)))
832 (push (cons ind origin
) ind-to-ori
)
834 ;; Another item in the same sub-list: it shares
835 ;; the same parent as the previous item.
836 (t (cons pos
(cdar ind-to-ori
))))))
843 (defsubst org-list-get-nth
(n key struct
)
844 "Return the Nth value of KEY in STRUCT."
845 (nth n
(assq key struct
)))
847 (defun org-list-set-nth (n key struct new
)
848 "Set the Nth value of KEY in STRUCT to NEW.
849 \nThis function modifies STRUCT."
850 (setcar (nthcdr n
(assq key struct
)) new
))
852 (defsubst org-list-get-ind
(item struct
)
853 "Return indentation of ITEM in STRUCT."
854 (org-list-get-nth 1 item struct
))
856 (defun org-list-set-ind (item struct ind
)
857 "Set indentation of ITEM in STRUCT to IND.
858 \nThis function modifies STRUCT."
859 (org-list-set-nth 1 item struct ind
))
861 (defsubst org-list-get-bullet
(item struct
)
862 "Return bullet of ITEM in STRUCT."
863 (org-list-get-nth 2 item struct
))
865 (defun org-list-set-bullet (item struct bullet
)
866 "Set bullet of ITEM in STRUCT to BULLET.
867 \nThis function modifies STRUCT."
868 (org-list-set-nth 2 item struct bullet
))
870 (defsubst org-list-get-counter
(item struct
)
871 "Return counter of ITEM in STRUCT."
872 (org-list-get-nth 3 item struct
))
874 (defsubst org-list-get-checkbox
(item struct
)
875 "Return checkbox of ITEM in STRUCT or nil."
876 (org-list-get-nth 4 item struct
))
878 (defun org-list-set-checkbox (item struct checkbox
)
879 "Set checkbox of ITEM in STRUCT to CHECKBOX.
880 \nThis function modifies STRUCT."
881 (org-list-set-nth 4 item struct checkbox
))
883 (defsubst org-list-get-tag
(item struct
)
884 "Return end position of ITEM in STRUCT."
885 (org-list-get-nth 5 item struct
))
887 (defun org-list-get-item-end (item struct
)
888 "Return end position of ITEM in STRUCT."
889 (org-list-get-nth 6 item struct
))
891 (defun org-list-get-item-end-before-blank (item struct
)
892 "Return point at end of ITEM in STRUCT, before any blank line.
893 Point returned is at end of line."
895 (goto-char (org-list-get-item-end item struct
))
896 (skip-chars-backward " \r\t\n")
899 (defun org-list-get-parent (item struct parents
)
900 "Return parent of ITEM or nil.
901 STRUCT is the list structure. PARENTS is the alist of parents,
902 as returned by `org-list-parents-alist'."
903 (let ((parents (or parents
(org-list-parents-alist struct
))))
904 (cdr (assq item parents
))))
906 (defun org-list-has-child-p (item struct
)
907 "Non-nil if ITEM has a child.
909 STRUCT is the list structure.
911 Value returned is the position of the first child of ITEM."
912 (let ((ind (org-list-get-ind item struct
))
913 (child-maybe (car (nth 1 (member (assq item struct
) struct
)))))
914 (when (and child-maybe
915 (< ind
(org-list-get-ind child-maybe struct
)))
918 (defun org-list-get-next-item (item struct prevs
)
919 "Return next item in same sub-list as ITEM, or nil.
920 STRUCT is the list structure. PREVS is the alist of previous
921 items, as returned by `org-list-prevs-alist'."
922 (car (rassq item prevs
)))
924 (defun org-list-get-prev-item (item struct prevs
)
925 "Return previous item in same sub-list as ITEM, or nil.
926 STRUCT is the list structure. PREVS is the alist of previous
927 items, as returned by `org-list-prevs-alist'."
928 (cdr (assq item prevs
)))
930 (defun org-list-get-subtree (item struct
)
931 "List all items having ITEM as a common ancestor, or nil.
932 STRUCT is the list structure."
933 (let* ((item-end (org-list-get-item-end item struct
))
934 (sub-struct (cdr (member (assq item struct
) struct
)))
939 (if (< pos item-end
) (push pos subtree
) (throw 'exit nil
))))
943 (defun org-list-get-all-items (item struct prevs
)
944 "List all items in the same sub-list as ITEM.
945 STRUCT is the list structure. PREVS is the alist of previous
946 items, as returned by `org-list-prevs-alist'."
947 (let ((prev-item item
)
949 before-item after-item
)
950 (while (setq prev-item
(org-list-get-prev-item prev-item struct prevs
))
951 (push prev-item before-item
))
952 (while (setq next-item
(org-list-get-next-item next-item struct prevs
))
953 (push next-item after-item
))
954 (append before-item
(list item
) (nreverse after-item
))))
956 (defun org-list-get-children (item struct parents
)
957 "List all children of ITEM, or nil.
958 STRUCT is the list structure. PARENTS is the alist of parents,
959 as returned by `org-list-parents-alist'."
961 (while (setq child
(car (rassq item parents
)))
962 (setq parents
(cdr (member (assq child parents
) parents
)))
966 (defun org-list-get-top-point (struct)
967 "Return point at beginning of list.
968 STRUCT is the list structure."
971 (defun org-list-get-bottom-point (struct)
972 "Return point at bottom of list.
973 STRUCT is the list structure."
975 (mapcar (lambda (e) (org-list-get-item-end (car e
) struct
)) struct
)))
977 (defun org-list-get-list-begin (item struct prevs
)
978 "Return point at beginning of sub-list ITEM belongs.
979 STRUCT is the list structure. PREVS is the alist of previous
980 items, as returned by `org-list-prevs-alist'."
981 (let ((first-item item
) prev-item
)
982 (while (setq prev-item
(org-list-get-prev-item first-item struct prevs
))
983 (setq first-item prev-item
))
986 (defalias 'org-list-get-first-item
'org-list-get-list-begin
)
988 (defun org-list-get-last-item (item struct prevs
)
989 "Return point at last item of sub-list ITEM belongs.
990 STRUCT is the list structure. PREVS is the alist of previous
991 items, as returned by `org-list-prevs-alist'."
992 (let ((last-item item
) next-item
)
993 (while (setq next-item
(org-list-get-next-item last-item struct prevs
))
994 (setq last-item next-item
))
997 (defun org-list-get-list-end (item struct prevs
)
998 "Return point at end of sub-list ITEM belongs.
999 STRUCT is the list structure. PREVS is the alist of previous
1000 items, as returned by `org-list-prevs-alist'."
1001 (org-list-get-item-end (org-list-get-last-item item struct prevs
) struct
))
1003 (defun org-list-get-list-type (item struct prevs
)
1004 "Return the type of the list containing ITEM, as a symbol.
1006 STRUCT is the list structure. PREVS is the alist of previous
1007 items, as returned by `org-list-prevs-alist'.
1009 Possible types are `descriptive', `ordered' and `unordered'. The
1010 type is determined by the first item of the list."
1011 (let ((first (org-list-get-list-begin item struct prevs
)))
1013 ((string-match "[[:alnum:]]" (org-list-get-bullet first struct
)) 'ordered
)
1014 ((org-list-get-tag first struct
) 'descriptive
)
1017 (defun org-list-get-item-number (item struct prevs parents
)
1018 "Return ITEM's sequence number.
1020 STRUCT is the list structure. PREVS is the alist of previous
1021 items, as returned by `org-list-prevs-alist'. PARENTS is the
1022 alist of ancestors, as returned by `org-list-parents-alist'.
1024 Return value is a list of integers. Counters have an impact on
1026 (let ((get-relative-number
1028 (lambda (item struct prevs
)
1029 ;; Return relative sequence number of ITEM in the sub-list
1030 ;; it belongs. STRUCT is the list structure. PREVS is
1031 ;; the alist of previous items.
1032 (let ((seq 0) (pos item
) counter
)
1033 (while (and (not (setq counter
(org-list-get-counter pos struct
)))
1034 (setq pos
(org-list-get-prev-item pos struct prevs
)))
1036 (if (not counter
) (1+ seq
)
1038 ((string-match "[A-Za-z]" counter
)
1039 (+ (- (string-to-char (upcase (match-string 0 counter
))) 64)
1041 ((string-match "[0-9]+" counter
)
1042 (+ (string-to-number (match-string 0 counter
)) seq
))
1044 ;; Cons each parent relative number into return value (OUT).
1045 (let ((out (list (funcall get-relative-number item struct prevs
)))
1047 (while (setq parent
(org-list-get-parent parent struct parents
))
1048 (push (funcall get-relative-number parent struct prevs
) out
))
1056 (defun org-list-search-generic (search re bound noerr
)
1057 "Search a string in valid contexts for lists.
1058 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1059 in `re-search-forward'."
1061 (let ((origin (point)))
1063 ;; 1. No match: return to origin or bound, depending on NOERR.
1064 (unless (funcall search re bound noerr
)
1065 (throw 'exit
(and (goto-char (if (memq noerr
'(t nil
)) origin bound
))
1067 ;; 2. Match in valid context: return point. Else, continue
1069 (when (org-list-in-valid-context-p) (throw 'exit
(point)))))))
1071 (defun org-list-search-backward (regexp &optional bound noerror
)
1072 "Like `re-search-backward' but stop only where lists are recognized.
1073 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1074 `re-search-backward'."
1075 (org-list-search-generic #'re-search-backward
1076 regexp
(or bound
(point-min)) noerror
))
1078 (defun org-list-search-forward (regexp &optional bound noerror
)
1079 "Like `re-search-forward' but stop only where lists are recognized.
1080 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1081 `re-search-forward'."
1082 (org-list-search-generic #'re-search-forward
1083 regexp
(or bound
(point-max)) noerror
))
1087 ;;; Methods on structures
1089 (defsubst org-list-bullet-string
(bullet)
1090 "Return BULLET with the correct number of whitespaces.
1091 It determines the number of whitespaces to append by looking at
1092 `org-list-two-spaces-after-bullet-regexp'."
1094 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1096 org-list-two-spaces-after-bullet-regexp bullet
))
1099 (if (string-match "\\S-+\\([ \t]*\\)" bullet
)
1100 (replace-match spaces nil nil bullet
1)
1103 (defun org-list-swap-items (beg-A beg-B struct
)
1104 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1106 Blank lines at the end of items are left in place. Item
1107 visibility is preserved. Return the new structure after the
1110 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1111 to the same sub-list.
1113 This function modifies STRUCT."
1115 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct
))
1116 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct
))
1117 (end-A (org-list-get-item-end beg-A struct
))
1118 (end-B (org-list-get-item-end beg-B struct
))
1119 (size-A (- end-A-no-blank beg-A
))
1120 (size-B (- end-B-no-blank beg-B
))
1121 (body-A (buffer-substring beg-A end-A-no-blank
))
1122 (body-B (buffer-substring beg-B end-B-no-blank
))
1123 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B
))
1124 (sub-A (cons beg-A
(org-list-get-subtree beg-A struct
)))
1125 (sub-B (cons beg-B
(org-list-get-subtree beg-B struct
)))
1126 ;; Store overlays responsible for visibility status. We
1127 ;; also need to store their boundaries as they will be
1128 ;; removed from buffer.
1130 (mapcar (lambda (ov)
1131 (list ov
(overlay-start ov
) (overlay-end ov
)))
1132 (overlays-in beg-A end-A
))
1133 (mapcar (lambda (ov)
1134 (list ov
(overlay-start ov
) (overlay-end ov
)))
1135 (overlays-in beg-B end-B
)))))
1136 ;; 1. Move effectively items in buffer.
1138 (delete-region beg-A end-B-no-blank
)
1139 (insert (concat body-B between-A-no-blank-and-B body-A
))
1140 ;; 2. Now modify struct. No need to re-read the list, the
1141 ;; transformation is just a shift of positions. Some special
1142 ;; attention is required for items ending at END-A and END-B
1143 ;; as empty spaces are not moved there. In others words,
1144 ;; item BEG-A will end with whitespaces that were at the end
1145 ;; of BEG-B and the same applies to BEG-B.
1147 (let ((pos (car e
)))
1151 (let ((end-e (nth 6 e
)))
1152 (setcar e
(+ pos
(- end-B-no-blank end-A-no-blank
)))
1153 (setcar (nthcdr 6 e
)
1154 (+ end-e
(- end-B-no-blank end-A-no-blank
)))
1155 (when (= end-e end-A
) (setcar (nthcdr 6 e
) end-B
))))
1157 (let ((end-e (nth 6 e
)))
1158 (setcar e
(- (+ pos beg-A
) beg-B
))
1159 (setcar (nthcdr 6 e
) (+ end-e
(- beg-A beg-B
)))
1160 (when (= end-e end-B
)
1161 (setcar (nthcdr 6 e
)
1162 (+ beg-A size-B
(- end-A end-A-no-blank
))))))
1164 (let ((end-e (nth 6 e
)))
1165 (setcar e
(+ pos
(- size-B size-A
)))
1166 (setcar (nthcdr 6 e
) (+ end-e
(- size-B size-A
))))))))
1168 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1169 ;; Restore visibility status, by moving overlays to their new
1174 (+ (nth 1 ov
) (- (+ beg-B
(- size-B size-A
)) beg-A
))
1175 (+ (nth 2 ov
) (- (+ beg-B
(- size-B size-A
)) beg-A
))))
1178 (move-overlay (car ov
)
1179 (+ (nth 1 ov
) (- beg-A beg-B
))
1180 (+ (nth 2 ov
) (- beg-A beg-B
))))
1182 ;; Return structure.
1185 (defun org-list-separating-blank-lines-number (pos struct prevs
)
1186 "Return number of blank lines that should separate items in list.
1188 POS is the position of point where `org-list-insert-item' was called.
1190 STRUCT is the list structure. PREVS is the alist of previous
1191 items, as returned by `org-list-prevs-alist'.
1193 Assume point is at item's beginning. If the item is alone, apply
1194 some heuristics to guess the result."
1196 (let ((item (point))
1198 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
1203 ;; Count blank lines above beginning of line.
1205 (count-lines (goto-char (point-at-bol))
1206 (progn (skip-chars-backward " \r\t\n")
1210 ;; Trivial cases where there should be none.
1211 ((or org-list-empty-line-terminates-plain-lists
(not insert-blank-p
)) 0)
1212 ;; When `org-blank-before-new-entry' says so, it is 1.
1213 ((eq insert-blank-p t
) 1)
1214 ;; `plain-list-item' is 'auto. Count blank lines separating
1215 ;; neighbours items in list.
1216 (t (let ((next-p (org-list-get-next-item item struct prevs
)))
1218 ;; Is there a next item?
1219 (next-p (goto-char next-p
)
1220 (funcall count-blanks
))
1221 ;; Is there a previous item?
1222 ((org-list-get-prev-item item struct prevs
)
1223 (funcall count-blanks
))
1224 ;; User inserted blank lines, trust him.
1225 ((and (> pos
(org-list-get-item-end-before-blank item struct
))
1226 (> (save-excursion (goto-char pos
)
1227 (setq usr-blank
(funcall count-blanks
)))
1230 ;; Are there blank lines inside the list so far?
1232 (goto-char (org-list-get-top-point struct
))
1233 ;; Do not use `org-list-search-forward' so blank lines
1234 ;; in blocks can be counted in.
1236 "^[ \t]*$" (org-list-get-item-end-before-blank item struct
) t
))
1238 ;; Default choice: no blank line.
1241 (defun org-list-insert-item (pos struct prevs
&optional checkbox after-bullet
)
1242 "Insert a new list item at POS and return the new structure.
1243 If POS is before first character after bullet of the item, the
1244 new item will be created before the current one.
1246 STRUCT is the list structure. PREVS is the the alist of previous
1247 items, as returned by `org-list-prevs-alist'.
1249 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1250 after the bullet. Cursor will be after this text once the
1253 This function modifies STRUCT."
1254 (let ((case-fold-search t
))
1255 ;; 1. Get information about list: position of point with regards
1256 ;; to item start (BEFOREP), blank lines number separating items
1257 ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
1258 (let* ((item (progn (goto-char pos
) (goto-char (org-list-get-item-begin))))
1259 (item-end (org-list-get-item-end item struct
))
1260 (item-end-no-blank (org-list-get-item-end-before-blank item struct
))
1263 (looking-at org-list-full-item-re
)
1264 ;; Do not count tag in a non-descriptive list.
1265 (<= pos
(if (and (match-beginning 4)
1267 (string-match "[.)]" (match-string 1))))
1270 (split-line-p (org-get-alist-option org-M-RET-may-split-line
'item
))
1271 (blank-nb (org-list-separating-blank-lines-number
1273 ;; 2. Build the new item to be created. Concatenate same
1274 ;; bullet as item, checkbox, text AFTER-BULLET if
1275 ;; provided, and text cut from point to end of item
1276 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1277 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1278 ;; between what was cut and what was inserted in buffer
1279 ;; is stored in SIZE-OFFSET.
1280 (ind (org-list-get-ind item struct
))
1281 (ind-size (if indent-tabs-mode
1282 (+ (/ ind tab-width
) (mod ind tab-width
))
1284 (bullet (org-list-bullet-string (org-list-get-bullet item struct
)))
1285 (box (when checkbox
"[ ]"))
1287 (and (not beforep
) split-line-p
1290 ;; If POS is greater than ITEM-END, then point is
1291 ;; in some white lines after the end of the list.
1292 ;; Those must be removed, or they will be left,
1293 ;; stacking up after the list.
1294 (when (< item-end pos
)
1295 (delete-region (1- item-end
) (point-at-eol)))
1296 (skip-chars-backward " \r\t\n")
1298 (delete-and-extract-region pos item-end-no-blank
))))
1299 (body (concat bullet
(when box
(concat box
" ")) after-bullet
1301 (if (string-match "\\`[ \t]+" text-cut
)
1302 (replace-match "" t t text-cut
)
1304 (item-sep (make-string (1+ blank-nb
) ?
\n))
1305 (item-size (+ ind-size
(length body
) (length item-sep
)))
1306 (size-offset (- item-size
(length text-cut
))))
1307 ;; 4. Insert effectively item into buffer.
1309 (org-indent-to-column ind
)
1310 (insert body item-sep
)
1311 ;; 5. Add new item to STRUCT.
1313 (let ((p (car e
)) (end (nth 6 e
)))
1315 ;; Before inserted item, positions don't change but
1316 ;; an item ending after insertion has its end shifted
1319 (when (> end item
) (setcar (nthcdr 6 e
) (+ end size-offset
))))
1320 ;; Trivial cases where current item isn't split in
1321 ;; two. Just shift every item after new one by
1323 ((or beforep
(not split-line-p
))
1324 (setcar e
(+ p item-size
))
1325 (setcar (nthcdr 6 e
) (+ end item-size
)))
1326 ;; Item is split in two: elements before POS are just
1327 ;; shifted by ITEM-SIZE. In the case item would end
1328 ;; after split POS, ending is only shifted by
1331 (setcar e
(+ p item-size
))
1333 (setcar (nthcdr 6 e
) (+ end item-size
))
1334 (setcar (nthcdr 6 e
) (+ end size-offset
))))
1335 ;; Elements after POS are moved into new item.
1336 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1337 ;; doesn't appear in buffer yet.
1339 (setcar e
(+ p size-offset
(- item pos
(length item-sep
))))
1340 (if (= end item-end
)
1341 (setcar (nthcdr 6 e
) (+ item item-size
))
1342 (setcar (nthcdr 6 e
)
1344 (- item pos
(length item-sep
))))))
1345 ;; Elements at ITEM-END or after are only shifted by
1347 (t (setcar e
(+ p size-offset
))
1348 (setcar (nthcdr 6 e
) (+ end size-offset
))))))
1350 (push (list item ind bullet nil box nil
(+ item item-size
)) struct
)
1351 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1352 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1353 ;; exchange ITEM with the next item in list. Position cursor
1354 ;; after bullet, counter, checkbox, and label.
1357 (setq struct
(org-list-swap-items item
(+ item item-size
) struct
))
1358 (goto-char (org-list-get-next-item
1359 item struct
(org-list-prevs-alist struct
))))
1362 (defun org-list-delete-item (item struct
)
1363 "Remove ITEM from the list and return the new structure.
1365 STRUCT is the list structure."
1366 (let* ((end (org-list-get-item-end item struct
))
1367 (beg (if (= (org-list-get-bottom-point struct
) end
)
1368 ;; If ITEM ends with the list, delete blank lines
1372 (skip-chars-backward " \r\t\n")
1373 (min (1+ (point-at-eol)) (point-max)))
1375 ;; Remove item from buffer.
1376 (delete-region beg end
)
1377 ;; Remove item from structure and shift others items accordingly.
1378 ;; Don't forget to shift also ending position when appropriate.
1379 (let ((size (- end beg
)))
1380 (delq nil
(mapcar (lambda (e)
1381 (let ((pos (car e
)))
1384 (let ((end-e (nth 6 e
)))
1388 (append (butlast e
) (list beg
)))
1390 (append (butlast e
) (list (- end-e size
)))))))
1394 (append (butlast (cdr e
))
1395 (list (- (nth 6 e
) size
))))))))
1398 (defun org-list-send-item (item dest struct
)
1399 "Send ITEM to destination DEST.
1401 STRUCT is the list structure.
1403 DEST can have various values.
1405 If DEST is a buffer position, the function will assume it points
1406 to another item in the same list as ITEM, and will move the
1407 latter just before the former.
1409 If DEST is `begin' (respectively `end'), ITEM will be moved at
1410 the beginning (respectively end) of the list it belongs to.
1412 If DEST is a string like \"N\", where N is an integer, ITEM will
1413 be moved at the Nth position in the list.
1415 If DEST is `kill', ITEM will be deleted and its body will be
1416 added to the kill-ring.
1418 If DEST is `delete', ITEM will be deleted.
1420 Visibility of item is preserved.
1422 This function returns, destructively, the new list structure."
1423 (let* ((prevs (org-list-prevs-alist struct
))
1424 (item-end (org-list-get-item-end item struct
))
1425 ;; Grab full item body minus its bullet.
1432 (regexp-quote (org-list-get-bullet item struct
))))
1435 ;; Change DEST into a buffer position. A trick is needed
1436 ;; when ITEM is meant to be sent at the end of the list.
1437 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1438 ;; nil and insertion point (INS-POINT) to the first line's
1439 ;; end of the last item, we ensure the new item will be
1440 ;; inserted after the last item, and not after any of its
1441 ;; hypothetical sub-items.
1443 ((or (eq dest
'kill
) (eq dest
'delete
)))
1445 (setq dest
(org-list-get-list-begin item struct prevs
)))
1447 (setq dest
(org-list-get-list-end item struct prevs
))
1449 (goto-char (org-list-get-last-item item struct prevs
))
1451 ((string-match "\\`[0-9]+\\'" dest
)
1452 (let* ((all (org-list-get-all-items item struct prevs
))
1454 (index (mod (string-to-number dest
) len
)))
1455 (if (not (zerop index
))
1456 (setq dest
(nth (1- index
) all
))
1457 ;; Send ITEM at the end of the list.
1458 (setq dest
(org-list-get-list-end item struct prevs
))
1461 (org-list-get-last-item item struct prevs
))
1464 (org-M-RET-may-split-line nil
)
1465 ;; Store visibility.
1466 (visibility (overlays-in item item-end
)))
1468 ((eq dest
'delete
) (org-list-delete-item item struct
))
1471 (org-list-delete-item item struct
))
1472 ((and (integerp dest
) (/= item ins-point
))
1473 (setq item
(copy-marker item
))
1474 (setq struct
(org-list-insert-item ins-point struct prevs nil body
))
1475 ;; 1. Structure returned by `org-list-insert-item' may not be
1476 ;; accurate, as it cannot see sub-items included in BODY.
1477 ;; Thus, first compute the real structure so far.
1479 (cons (marker-position item
)
1480 (org-list-get-subtree (marker-position item
) struct
)))
1481 (new-end (org-list-get-item-end (point) struct
))
1482 (old-end (org-list-get-item-end (marker-position item
) struct
))
1484 (shift (- (point) item
)))
1485 ;; 1.1. Remove the item just created in structure.
1486 (setq struct
(delete (assq new-item struct
) struct
))
1487 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1492 (let* ((cell (assq e struct
))
1496 (append (butlast (cdr cell
))
1497 (list (if (= end old-end
)
1501 (lambda (e1 e2
) (< (car e1
) (car e2
))))))
1502 ;; 2. Restore visibility.
1505 (+ (overlay-start ov
) (- (point) item
))
1506 (+ (overlay-end ov
) (- (point) item
))))
1508 ;; 3. Eventually delete extra copy of the item and clean marker.
1509 (prog1 (org-list-delete-item (marker-position item
) struct
)
1510 (move-marker item nil
)))
1513 (defun org-list-struct-outdent (start end struct parents
)
1514 "Outdent items between positions START and END.
1516 STRUCT is the list structure. PARENTS is the alist of items'
1517 parents, as returned by `org-list-parents-alist'.
1519 START is included, END excluded."
1522 (let* ((item (car cell
))
1523 (parent (cdr cell
)))
1525 ;; Item not yet in zone: keep association.
1526 ((< item start
) cell
)
1527 ;; Item out of zone: follow associations in ACC.
1529 (let ((convert (and parent
(assq parent acc
))))
1530 (if convert
(cons item
(cdr convert
)) cell
)))
1531 ;; Item has no parent: error
1533 (error "Cannot outdent top-level items"))
1534 ;; Parent is outdented: keep association.
1536 (push (cons parent item
) acc
) cell
)
1538 ;; Parent isn't outdented: reparent to grand-parent.
1539 (let ((grand-parent (org-list-get-parent
1540 parent struct parents
)))
1541 (push (cons parent item
) acc
)
1542 (cons item grand-parent
))))))))
1543 (mapcar out parents
)))
1545 (defun org-list-struct-indent (start end struct parents prevs
)
1546 "Indent items between positions START and END.
1548 STRUCT is the list structure. PARENTS is the alist of parents
1549 and PREVS is the alist of previous items, returned by,
1550 respectively, `org-list-parents-alist' and
1551 `org-list-prevs-alist'.
1553 START is included and END excluded.
1555 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1556 bullets between START and END."
1558 (set-assoc (lambda (cell) (push cell acc
) cell
))
1559 (change-bullet-maybe
1564 ;; Normalize ordered bullets.
1565 (let ((bul (org-trim
1566 (org-list-get-bullet item struct
))))
1567 (cond ((string-match "[A-Z]\\." bul
) "A.")
1568 ((string-match "[A-Z])" bul
) "A)")
1569 ((string-match "[a-z]\\." bul
) "a.")
1570 ((string-match "[a-z])" bul
) "a)")
1571 ((string-match "[0-9]\\." bul
) "1.")
1572 ((string-match "[0-9])" bul
) "1)")
1574 org-list-demote-modify-bullet
))))
1575 (when new-bul-p
(org-list-set-bullet item struct new-bul-p
))))))
1578 (let* ((item (car cell
))
1579 (parent (cdr cell
)))
1581 ;; Item not yet in zone: keep association.
1582 ((< item start
) cell
)
1584 ;; Item out of zone: follow associations in ACC.
1585 (let ((convert (assq parent acc
)))
1586 (if convert
(cons item
(cdr convert
)) cell
)))
1588 ;; Item is in zone...
1589 (let ((prev (org-list-get-prev-item item struct prevs
)))
1590 ;; Check if bullet needs to be changed.
1591 (funcall change-bullet-maybe item
)
1593 ;; First item indented but not parent: error
1594 ((and (not prev
) (< parent start
))
1595 (error "Cannot indent the first item of a list"))
1596 ;; First item and parent indented: keep same
1598 ((not prev
) (funcall set-assoc cell
))
1599 ;; Previous item not indented: reparent to it.
1600 ((< prev start
) (funcall set-assoc
(cons item prev
)))
1601 ;; Previous item indented: reparent like it.
1604 (cons item
(cdr (assq prev acc
)))))))))))))
1605 (mapcar ind parents
)))
1609 ;;; Repairing structures
1611 (defun org-list-use-alpha-bul-p (first struct prevs
)
1612 "Non-nil if list starting at FIRST can have alphabetical bullets.
1614 STRUCT is list structure. PREVS is the alist of previous items,
1615 as returned by `org-list-prevs-alist'."
1616 (and org-list-allow-alphabetical
1618 (let ((item first
) (ascii 64) (case-fold-search nil
))
1619 ;; Pretend that bullets are uppercase and check if alphabet
1620 ;; is sufficient, taking counters into account.
1622 (let ((bul (org-list-get-bullet item struct
))
1623 (count (org-list-get-counter item struct
)))
1624 ;; Virtually determine current bullet
1625 (if (and count
(string-match "[a-zA-Z]" count
))
1626 ;; Counters are not case-sensitive.
1627 (setq ascii
(string-to-char (upcase count
)))
1628 (setq ascii
(1+ ascii
)))
1629 ;; Test if bullet would be over z or Z.
1632 (setq item
(org-list-get-next-item item struct prevs
)))))
1633 ;; All items checked. All good.
1636 (defun org-list-inc-bullet-maybe (bullet)
1637 "Increment BULLET if applicable."
1638 (let ((case-fold-search nil
))
1640 ;; Num bullet: increment it.
1641 ((string-match "[0-9]+" bullet
)
1643 (number-to-string (1+ (string-to-number (match-string 0 bullet
))))
1645 ;; Alpha bullet: increment it.
1646 ((string-match "[A-Za-z]" bullet
)
1648 (char-to-string (1+ (string-to-char (match-string 0 bullet
))))
1650 ;; Unordered bullet: leave it.
1653 (defun org-list-struct-fix-bul (struct prevs
)
1654 "Verify and correct bullets in STRUCT.
1655 PREVS is the alist of previous items, as returned by
1656 `org-list-prevs-alist'.
1658 This function modifies STRUCT."
1659 (let ((case-fold-search nil
)
1662 ;; Set bullet of ITEM in STRUCT, depending on the type of
1663 ;; first item of the list, the previous bullet and counter
1666 (let* ((prev (org-list-get-prev-item item struct prevs
))
1667 (prev-bul (and prev
(org-list-get-bullet prev struct
)))
1668 (counter (org-list-get-counter item struct
))
1669 (bullet (org-list-get-bullet item struct
))
1670 (alphap (and (not prev
)
1671 (org-list-use-alpha-bul-p item struct prevs
))))
1672 (org-list-set-bullet
1674 (org-list-bullet-string
1676 ;; Alpha counter in alpha list: use counter.
1678 (string-match "[a-zA-Z]" counter
)
1679 (string-match "[a-zA-Z]" prev-bul
))
1680 ;; Use cond to be sure `string-match' is used in
1684 ((string-match "[a-z]" prev-bul
) (downcase counter
))
1685 ((string-match "[A-Z]" prev-bul
) (upcase counter
)))))
1686 (replace-match real-count nil nil prev-bul
)))
1687 ;; Num counter in a num list: use counter.
1689 (string-match "[0-9]+" counter
)
1690 (string-match "[0-9]+" prev-bul
))
1691 (replace-match counter nil nil prev-bul
))
1692 ;; No counter: increase, if needed, previous bullet.
1694 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct
)))
1695 ;; Alpha counter at first item: use counter.
1696 ((and counter
(org-list-use-alpha-bul-p item struct prevs
)
1697 (string-match "[A-Za-z]" counter
)
1698 (string-match "[A-Za-z]" bullet
))
1701 ((string-match "[a-z]" bullet
) (downcase counter
))
1702 ((string-match "[A-Z]" bullet
) (upcase counter
)))))
1703 (replace-match real-count nil nil bullet
)))
1704 ;; Num counter at first item: use counter.
1706 (string-match "[0-9]+" counter
)
1707 (string-match "[0-9]+" bullet
))
1708 (replace-match counter nil nil bullet
))
1709 ;; First bullet is alpha uppercase: use "A".
1710 ((and alphap
(string-match "[A-Z]" bullet
))
1711 (replace-match "A" nil nil bullet
))
1712 ;; First bullet is alpha lowercase: use "a".
1713 ((and alphap
(string-match "[a-z]" bullet
))
1714 (replace-match "a" nil nil bullet
))
1715 ;; First bullet is num: use "1".
1716 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet
)
1717 (replace-match "1" nil nil bullet
))
1718 ;; Not an ordered list: keep bullet.
1720 (mapc fix-bul
(mapcar 'car struct
))))
1722 (defun org-list-struct-fix-ind (struct parents
&optional bullet-size
)
1723 "Verify and correct indentation in STRUCT.
1725 PARENTS is the alist of parents, as returned by
1726 `org-list-parents-alist'.
1728 If numeric optional argument BULLET-SIZE is set, assume all
1729 bullets in list have this length to determine new indentation.
1731 This function modifies STRUCT."
1732 (let* ((ancestor (org-list-get-top-point struct
))
1733 (top-ind (org-list-get-ind ancestor struct
))
1736 (let ((parent (org-list-get-parent item struct parents
)))
1738 ;; Indent like parent + length of parent's bullet +
1741 item struct
(+ (or bullet-size
1743 (org-list-get-bullet parent struct
)))
1744 (org-list-get-ind parent struct
)
1745 org-list-indent-offset
))
1746 ;; If no parent, indent like top-point.
1747 (org-list-set-ind item struct top-ind
))))))
1748 (mapc new-ind
(mapcar 'car
(cdr struct
)))))
1750 (defun org-list-struct-fix-box (struct parents prevs
&optional ordered
)
1751 "Verify and correct checkboxes in STRUCT.
1753 PARENTS is the alist of parents and PREVS is the alist of
1754 previous items, as returned by, respectively,
1755 `org-list-parents-alist' and `org-list-prevs-alist'.
1757 If ORDERED is non-nil, a checkbox can only be checked when every
1758 checkbox before it is checked too. If there was an attempt to
1759 break this rule, the function will return the blocking item. In
1760 all others cases, the return value will be nil.
1762 This function modifies STRUCT."
1763 (let ((all-items (mapcar 'car struct
))
1768 (mapcar (lambda (child)
1769 (org-list-get-checkbox child struct
))
1770 (org-list-get-children item struct parents
))))
1771 (org-list-set-checkbox
1774 ((and (member "[ ]" box-list
) (member "[X]" box-list
)) "[-]")
1775 ((member "[-]" box-list
) "[-]")
1776 ((member "[X]" box-list
) "[X]")
1777 ((member "[ ]" box-list
) "[ ]")
1778 ;; Parent has no boxed child: leave box as-is.
1779 (t (org-list-get-checkbox item struct
))))))))
1781 ;; 1. List all parents with a checkbox.
1784 (let* ((parent (org-list-get-parent e struct parents
))
1785 (parent-box-p (org-list-get-checkbox parent struct
)))
1786 (when (and parent-box-p
(not (memq parent parent-list
)))
1787 (push parent parent-list
))))
1789 ;; 2. Sort those parents by decreasing indentation.
1790 (setq parent-list
(sort parent-list
1792 (> (org-list-get-ind e1 struct
)
1793 (org-list-get-ind e2 struct
)))))
1794 ;; 3. For each parent, get all children's checkboxes to determine
1795 ;; and set its checkbox accordingly.
1796 (mapc set-parent-box parent-list
)
1797 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1800 (mapcar (lambda (e) (org-list-get-checkbox e struct
)) all-items
))
1801 (after-unchecked (member "[ ]" box-list
)))
1802 ;; There are boxes checked after an unchecked one: fix that.
1803 (when (member "[X]" after-unchecked
)
1804 (let ((index (- (length struct
) (length after-unchecked
))))
1806 (when (org-list-get-checkbox e struct
)
1807 (org-list-set-checkbox e struct
"[ ]")))
1808 (nthcdr index all-items
))
1809 ;; Verify once again the structure, without ORDERED.
1810 (org-list-struct-fix-box struct parents prevs nil
)
1811 ;; Return blocking item.
1812 (nth index all-items
)))))))
1814 (defun org-list-struct-fix-item-end (struct)
1815 "Verify and correct each item end position in STRUCT.
1817 This function modifies STRUCT."
1818 (let (end-list acc-end
)
1820 (let* ((pos (car e
))
1821 (ind-pos (org-list-get-ind pos struct
))
1822 (end-pos (org-list-get-item-end pos struct
)))
1823 (unless (assq end-pos struct
)
1824 ;; To determine real ind of an ending position that is
1825 ;; not at an item, we have to find the item it belongs
1826 ;; to: it is the last item (ITEM-UP), whose ending is
1827 ;; further than the position we're interested in.
1828 (let ((item-up (assoc-default end-pos acc-end
'>)))
1830 ;; Else part is for the bottom point.
1831 (if item-up
(+ (org-list-get-ind item-up struct
) 2) 0)
1834 (push (cons ind-pos pos
) end-list
)
1835 (push (cons end-pos pos
) acc-end
)))
1837 (setq end-list
(sort end-list
(lambda (e1 e2
) (< (cdr e1
) (cdr e2
)))))
1838 (org-list-struct-assoc-end struct end-list
)))
1840 (defun org-list-struct-apply-struct (struct old-struct
)
1841 "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
1843 OLD-STRUCT is the structure before any modifications, and STRUCT
1844 the structure to be applied. The function will only modify parts
1845 of the list which have changed.
1847 Initial position of cursor is restored after the changes."
1848 (let* ((origin (point-marker))
1849 (inlinetask-re (and (featurep 'org-inlinetask
)
1850 (org-inlinetask-outline-regexp)))
1851 (item-re (org-item-re))
1854 ;; Shift the indentation between END and BEG by DELTA. If
1855 ;; MAX-IND is non-nil, ensure that no line will be indented
1856 ;; more than that number. Start from the line before END.
1857 (lambda (end beg delta max-ind
)
1859 (skip-chars-backward " \r\t\n")
1861 (while (or (> (point) beg
)
1862 (and (= (point) beg
)
1863 (not (looking-at item-re
))))
1865 ;; Skip inline tasks.
1866 ((and inlinetask-re
(looking-at inlinetask-re
))
1867 (org-inlinetask-goto-beginning))
1868 ;; Shift only non-empty lines.
1869 ((org-looking-at-p "^[ \t]*\\S-")
1870 (let ((i (org-get-indentation)))
1872 (if max-ind
(min (+ i delta
) max-ind
) (+ i delta
))))))
1873 (forward-line -
1)))))
1876 ;; Replace ITEM first line elements with new elements from
1877 ;; STRUCT, if appropriate.
1880 (let* ((new-ind (org-list-get-ind item struct
))
1881 (old-ind (org-get-indentation))
1882 (new-bul (org-list-bullet-string
1883 (org-list-get-bullet item struct
)))
1884 (old-bul (org-list-get-bullet item old-struct
))
1885 (new-box (org-list-get-checkbox item struct
)))
1886 (looking-at org-list-full-item-re
)
1887 ;; a. Replace bullet
1888 (unless (equal old-bul new-bul
)
1889 (replace-match new-bul nil nil nil
1))
1890 ;; b. Replace checkbox.
1892 ((equal (match-string 3) new-box
))
1893 ((and (match-string 3) new-box
)
1894 (replace-match new-box nil nil nil
3))
1896 (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
1897 (replace-match "" nil nil nil
1))
1898 (t (let ((counterp (match-end 2)))
1899 (goto-char (if counterp
(1+ counterp
) (match-end 1)))
1900 (insert (concat new-box
(unless counterp
" "))))))
1901 ;; c. Indent item to appropriate column.
1902 (unless (= new-ind old-ind
)
1903 (delete-region (goto-char (point-at-bol))
1904 (progn (skip-chars-forward " \t") (point)))
1905 (indent-to new-ind
)))))))
1906 ;; 1. First get list of items and position endings. We maintain
1907 ;; two alists: ITM-SHIFT, determining indentation shift needed
1908 ;; at item, and END-LIST, a pseudo-alist where key is ending
1909 ;; position and value point.
1910 (let (end-list acc-end itm-shift all-ends sliced-struct
)
1911 (dolist (e old-struct
)
1912 (let* ((pos (car e
))
1913 (ind-pos (org-list-get-ind pos struct
))
1914 (ind-old (org-list-get-ind pos old-struct
))
1915 (bul-pos (org-list-get-bullet pos struct
))
1916 (bul-old (org-list-get-bullet pos old-struct
))
1917 (ind-shift (- (+ ind-pos
(length bul-pos
))
1918 (+ ind-old
(length bul-old
))))
1919 (end-pos (org-list-get-item-end pos old-struct
)))
1920 (push (cons pos ind-shift
) itm-shift
)
1921 (unless (assq end-pos old-struct
)
1922 ;; To determine real ind of an ending position that
1923 ;; is not at an item, we have to find the item it
1924 ;; belongs to: it is the last item (ITEM-UP), whose
1925 ;; ending is further than the position we're
1927 (let ((item-up (assoc-default end-pos acc-end
'>)))
1928 (push (cons end-pos item-up
) end-list
)))
1929 (push (cons end-pos pos
) acc-end
)))
1930 ;; 2. Slice the items into parts that should be shifted by the
1931 ;; same amount of indentation. Each slice follow the pattern
1932 ;; (END BEG DELTA MAX-IND-OR-NIL). Slices are returned in
1934 (setq all-ends
(sort (append (mapcar 'car itm-shift
)
1935 (org-uniquify (mapcar 'car end-list
)))
1937 (while (cdr all-ends
)
1938 (let* ((up (pop all-ends
))
1939 (down (car all-ends
))
1940 (itemp (assq up struct
))
1941 (item (if itemp up
(cdr (assq up end-list
))))
1942 (ind (cdr (assq item itm-shift
)))
1943 ;; If we're not at an item, there's a child of the item
1944 ;; point belongs to above. Make sure this slice isn't
1945 ;; moved within that child by specifying a maximum
1947 (max-ind (and (not itemp
)
1948 (+ (org-list-get-ind item struct
)
1949 (length (org-list-get-bullet item struct
))
1950 org-list-indent-offset
))))
1951 (push (list down up ind max-ind
) sliced-struct
)))
1952 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1953 ;; end to beginning. Take a special action when beginning is
1955 (dolist (e sliced-struct
)
1956 (unless (and (zerop (nth 2 e
)) (not (nth 3 e
)))
1957 (apply shift-body-ind e
))
1958 (let* ((beg (nth 1 e
))
1959 (cell (assq beg struct
)))
1960 (unless (or (not cell
) (equal cell
(assq beg old-struct
)))
1961 (funcall modify-item beg
)))))
1962 ;; 4. Go back to initial position and clean marker.
1964 (move-marker origin nil
)))
1966 (defun org-list-write-struct (struct parents
&optional old-struct
)
1967 "Correct bullets, checkboxes and indentation in list at point.
1969 STRUCT is the list structure. PARENTS is the alist of parents,
1970 as returned by `org-list-parents-alist'.
1972 When non-nil, optional argument OLD-STRUCT is the reference
1973 structure of the list. It should be provided whenever STRUCT
1974 doesn't correspond anymore to the real list in buffer."
1975 ;; Order of functions matters here: checkboxes and endings need
1976 ;; correct indentation to be set, and indentation needs correct
1979 ;; 0. Save a copy of structure before modifications
1980 (let ((old-struct (or old-struct
(copy-tree struct
))))
1981 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
1982 ;; order to get items endings and bullets properly
1983 (org-list-struct-fix-ind struct parents
2)
1984 ;; 2. Fix each item end to get correct prevs alist.
1985 (org-list-struct-fix-item-end struct
)
1986 ;; 3. Get bullets right.
1987 (let ((prevs (org-list-prevs-alist struct
)))
1988 (org-list-struct-fix-bul struct prevs
)
1989 ;; 4. Now get real indentation.
1990 (org-list-struct-fix-ind struct parents
)
1991 ;; 5. Eventually fix checkboxes.
1992 (org-list-struct-fix-box struct parents prevs
))
1993 ;; 6. Apply structure modifications to buffer.
1994 (org-list-struct-apply-struct struct old-struct
)))
2000 (defun org-apply-on-list (function init-value
&rest args
)
2001 "Call FUNCTION on each item of the list at point.
2002 FUNCTION must be called with at least one argument: INIT-VALUE,
2003 that will contain the value returned by the function at the
2004 previous item, plus ARGS extra arguments.
2006 FUNCTION is applied on items in reverse order.
2008 As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
2009 will return the number of items in the current list.
2011 Sublists of the list are skipped. Cursor is always at the
2012 beginning of the item."
2013 (let* ((struct (org-list-struct))
2014 (prevs (org-list-prevs-alist struct
))
2015 (item (copy-marker (point-at-bol)))
2016 (all (org-list-get-all-items (marker-position item
) struct prevs
))
2020 (setq value
(apply function value args
)))
2023 (move-marker item nil
)
2026 (defun org-list-set-item-visibility (item struct view
)
2027 "Set visibility of ITEM in STRUCT to VIEW.
2029 Possible values are: `folded', `children' or `subtree'. See
2030 `org-cycle' for more information."
2033 (let ((item-end (org-list-get-item-end-before-blank item struct
)))
2035 (outline-flag-region (save-excursion (goto-char item
) (point-at-eol))
2037 ((eq view
'children
)
2038 ;; First show everything.
2039 (org-list-set-item-visibility item struct
'subtree
)
2040 ;; Then fold every child.
2041 (let* ((parents (org-list-parents-alist struct
))
2042 (children (org-list-get-children item struct parents
)))
2044 (org-list-set-item-visibility e struct
'folded
))
2048 (let ((item-end (org-list-get-item-end item struct
)))
2049 (outline-flag-region item item-end nil
)))))
2051 (defun org-list-item-body-column (item)
2052 "Return column at which body of ITEM should start."
2053 (let (bpos bcol tpos tcol
)
2056 (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?\\([ \t]+\\|$\\)")
2057 (setq bpos
(match-beginning 1) tpos
(match-end 0)
2058 bcol
(progn (goto-char bpos
) (current-column))
2059 tcol
(progn (goto-char tpos
) (current-column)))
2060 (when (> tcol
(+ bcol org-description-max-indent
))
2061 (setq tcol
(+ bcol
5))))
2066 ;;; Interactive functions
2068 (defalias 'org-list-get-item-begin
'org-in-item-p
)
2070 (defun org-beginning-of-item ()
2071 "Go to the beginning of the current item.
2072 Throw an error when not in a list."
2074 (let ((begin (org-in-item-p)))
2075 (if begin
(goto-char begin
) (error "Not in an item"))))
2077 (defun org-beginning-of-item-list ()
2078 "Go to the beginning item of the current list or sublist.
2079 Throw an error when not in a list."
2081 (let ((begin (org-in-item-p)))
2083 (error "Not in an item")
2085 (let* ((struct (org-list-struct))
2086 (prevs (org-list-prevs-alist struct
)))
2087 (goto-char (org-list-get-list-begin begin struct prevs
))))))
2089 (defun org-end-of-item-list ()
2090 "Go to the end of the current list or sublist.
2091 Throw an error when not in a list."
2093 (let ((begin (org-in-item-p)))
2095 (error "Not in an item")
2097 (let* ((struct (org-list-struct))
2098 (prevs (org-list-prevs-alist struct
)))
2099 (goto-char (org-list-get-list-end begin struct prevs
))))))
2101 (defun org-end-of-item ()
2102 "Go to the end of the current item.
2103 Throw an error when not in a list."
2105 (let ((begin (org-in-item-p)))
2107 (error "Not in an item")
2109 (let ((struct (org-list-struct)))
2110 (goto-char (org-list-get-item-end begin struct
))))))
2112 (defun org-previous-item ()
2113 "Move to the beginning of the previous item.
2114 Throw an error when not in a list. Also throw an error when at
2115 first item, unless `org-list-use-circular-motion' is non-nil."
2117 (let ((item (org-in-item-p)))
2119 (error "Not in an item")
2121 (let* ((struct (org-list-struct))
2122 (prevs (org-list-prevs-alist struct
))
2123 (prevp (org-list-get-prev-item item struct prevs
)))
2125 (prevp (goto-char prevp
))
2126 (org-list-use-circular-motion
2127 (goto-char (org-list-get-last-item item struct prevs
)))
2128 (t (error "On first item")))))))
2130 (defun org-next-item ()
2131 "Move to the beginning of the next item.
2132 Throw an error when not in a list. Also throw an error when at
2133 last item, unless `org-list-use-circular-motion' is non-nil."
2135 (let ((item (org-in-item-p)))
2137 (error "Not in an item")
2139 (let* ((struct (org-list-struct))
2140 (prevs (org-list-prevs-alist struct
))
2141 (prevp (org-list-get-next-item item struct prevs
)))
2143 (prevp (goto-char prevp
))
2144 (org-list-use-circular-motion
2145 (goto-char (org-list-get-first-item item struct prevs
)))
2146 (t (error "On last item")))))))
2148 (defun org-move-item-down ()
2149 "Move the item at point down, i.e. swap with following item.
2150 Sub-items (items with larger indentation) are considered part of
2151 the item, so this really moves item trees."
2153 (unless (org-at-item-p) (error "Not at an item"))
2154 (let* ((col (current-column))
2155 (item (point-at-bol))
2156 (struct (org-list-struct))
2157 (prevs (org-list-prevs-alist struct
))
2158 (next-item (org-list-get-next-item (point-at-bol) struct prevs
)))
2159 (unless (or next-item org-list-use-circular-motion
)
2160 (user-error "Cannot move this item further down"))
2162 (setq struct
(org-list-send-item item
'begin struct
))
2163 (setq struct
(org-list-swap-items item next-item struct
))
2165 (org-list-get-next-item item struct
(org-list-prevs-alist struct
))))
2166 (org-list-write-struct struct
(org-list-parents-alist struct
))
2167 (org-move-to-column col
)))
2169 (defun org-move-item-up ()
2170 "Move the item at point up, i.e. swap with previous item.
2171 Sub-items (items with larger indentation) are considered part of
2172 the item, so this really moves item trees."
2174 (unless (org-at-item-p) (error "Not at an item"))
2175 (let* ((col (current-column))
2176 (item (point-at-bol))
2177 (struct (org-list-struct))
2178 (prevs (org-list-prevs-alist struct
))
2179 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs
)))
2180 (unless (or prev-item org-list-use-circular-motion
)
2181 (user-error "Cannot move this item further up"))
2183 (setq struct
(org-list-send-item item
'end struct
))
2184 (setq struct
(org-list-swap-items prev-item item struct
)))
2185 (org-list-write-struct struct
(org-list-parents-alist struct
))
2186 (org-move-to-column col
)))
2188 (defun org-insert-item (&optional checkbox
)
2189 "Insert a new item at the current level.
2190 If cursor is before first character after bullet of the item, the
2191 new item will be created before the current one.
2193 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2195 Return t when things worked, nil when we are not in an item, or
2197 (let ((itemp (org-in-item-p))
2199 ;; If cursor isn't is a list or if list is invisible, return nil.
2200 (unless (or (not itemp
)
2203 (outline-invisible-p)))
2206 (org-at-item-timer-p))
2207 ;; Timer list: delegate to `org-timer-item'.
2208 (progn (org-timer-item) t
)
2209 (let* ((struct (save-excursion (goto-char itemp
)
2211 (prevs (org-list-prevs-alist struct
))
2212 ;; If we're in a description list, ask for the new term.
2213 (desc (when (eq (org-list-get-list-type itemp struct prevs
)
2216 (setq struct
(org-list-insert-item pos struct prevs checkbox desc
))
2217 (org-list-write-struct struct
(org-list-parents-alist struct
))
2218 (when checkbox
(org-update-checkbox-count-maybe))
2219 (looking-at org-list-full-item-re
)
2220 (goto-char (if (and (match-beginning 4)
2222 (string-match "[.)]" (match-string 1))))
2225 (if desc
(backward-char 1))
2228 (defun org-list-repair ()
2229 "Fix indentation, bullets and checkboxes is the list at point."
2231 (unless (org-at-item-p) (error "This is not a list"))
2232 (let* ((struct (org-list-struct))
2233 (parents (org-list-parents-alist struct
)))
2234 (org-list-write-struct struct parents
)))
2236 (defun org-cycle-list-bullet (&optional which
)
2237 "Cycle through the different itemize/enumerate bullets.
2238 This cycle the entire list level through the sequence:
2240 `-' -> `+' -> `*' -> `1.' -> `1)'
2242 If WHICH is a valid string, use that as the new bullet. If WHICH
2243 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2244 `previous', cycle backwards."
2246 (unless (org-at-item-p) (error "Not at an item"))
2249 (let* ((struct (org-list-struct))
2250 (parents (org-list-parents-alist struct
))
2251 (prevs (org-list-prevs-alist struct
))
2252 (list-beg (org-list-get-first-item (point) struct prevs
))
2253 (bullet (org-list-get-bullet list-beg struct
))
2254 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs
))
2255 (case-fold-search nil
)
2257 ((string-match "[a-z]\\." bullet
) "a.")
2258 ((string-match "[a-z])" bullet
) "a)")
2259 ((string-match "[A-Z]\\." bullet
) "A.")
2260 ((string-match "[A-Z])" bullet
) "A)")
2261 ((string-match "\\." bullet
) "1.")
2262 ((string-match ")" bullet
) "1)")
2263 (t (org-trim bullet
))))
2264 ;; Compute list of possible bullets, depending on context.
2267 ;; *-bullets are not allowed at column 0.
2268 (unless (looking-at "\\S-") '("*"))
2269 ;; Description items cannot be numbered.
2270 (unless (or (eq org-plain-list-ordered-item-terminator ?\
))
2271 (org-at-item-description-p))
2273 (unless (or (eq org-plain-list-ordered-item-terminator ?.
)
2274 (org-at-item-description-p))
2276 (unless (or (not alpha-p
)
2277 (eq org-plain-list-ordered-item-terminator ?\
))
2278 (org-at-item-description-p))
2280 (unless (or (not alpha-p
)
2281 (eq org-plain-list-ordered-item-terminator ?.
)
2282 (org-at-item-description-p))
2284 (len (length bullet-list
))
2285 (item-index (- len
(length (member current bullet-list
))))
2286 (get-value (lambda (index) (nth (mod index len
) bullet-list
)))
2288 ((member which bullet-list
) which
)
2289 ((numberp which
) (funcall get-value which
))
2290 ((eq 'previous which
) (funcall get-value
(1- item-index
)))
2291 (t (funcall get-value
(1+ item-index
))))))
2292 ;; Use a short variation of `org-list-write-struct' as there's
2293 ;; no need to go through all the steps.
2294 (let ((old-struct (copy-tree struct
)))
2295 (org-list-set-bullet list-beg struct
(org-list-bullet-string new
))
2296 (org-list-struct-fix-bul struct prevs
)
2297 (org-list-struct-fix-ind struct parents
)
2298 (org-list-struct-apply-struct struct old-struct
)))))
2300 (defun org-toggle-checkbox (&optional toggle-presence
)
2301 "Toggle the checkbox in the current line.
2302 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2303 double prefix, set checkbox to [-].
2305 When there is an active region, toggle status or presence of the
2306 first checkbox there, and make every item inside have the same
2307 status or presence, respectively.
2309 If the cursor is in a headline, apply this to all checkbox items
2310 in the text below the heading, taking as reference the first item
2311 in subtree, ignoring drawers."
2318 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2319 "\\|" org-deadline-string
2320 "\\|" org-closed-string
2321 "\\|" org-clock-string
"\\)"
2322 " *[[<]\\([^]>]+\\)[]>]"))
2323 (orderedp (org-entry-get nil
"ORDERED"))
2325 ;; In a region, start at first item in region.
2327 ((org-region-active-p)
2328 (let ((limit (region-end)))
2329 (goto-char (region-beginning))
2330 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2331 (setq lim-up
(point-at-bol))
2332 (error "No item in region"))
2333 (setq lim-down
(copy-marker limit
))))
2335 ;; On an heading, start at first item after drawers and
2336 ;; time-stamps (scheduled, etc.).
2337 (let ((limit (save-excursion (outline-next-heading) (point))))
2339 (while (or (looking-at org-drawer-regexp
)
2340 (looking-at keyword-re
))
2341 (if (looking-at keyword-re
)
2343 (re-search-forward "^[ \t]*:END:" limit nil
)))
2344 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2345 (setq lim-up
(point-at-bol))
2346 (error "No item in subtree"))
2347 (setq lim-down
(copy-marker limit
))))
2348 ;; Just one item: set SINGLEP flag.
2351 (setq lim-up
(point-at-bol)
2352 lim-down
(copy-marker (point-at-eol))))
2353 (t (error "Not at an item or heading, and no active region"))))
2354 ;; Determine the checkbox going to be applied to all items
2359 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2361 ((equal toggle-presence
'(16)) "[-]")
2362 ((equal toggle-presence
'(4))
2363 (unless cbox
"[ ]"))
2364 ((equal "[X]" cbox
) "[ ]")
2366 ;; When an item is found within bounds, grab the full list at
2367 ;; point structure, then: (1) set check-box of all its items
2368 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2369 ;; whole list, (3) move point after the list.
2371 (while (and (< (point) lim-down
)
2372 (org-list-search-forward (org-item-beginning-re)
2374 (let* ((struct (org-list-struct))
2375 (struct-copy (copy-tree struct
))
2376 (parents (org-list-parents-alist struct
))
2377 (prevs (org-list-prevs-alist struct
))
2378 (bottom (copy-marker (org-list-get-bottom-point struct
)))
2379 (items-to-toggle (org-remove-if
2380 (lambda (e) (or (< e lim-up
) (> e lim-down
)))
2381 (mapcar 'car struct
))))
2382 (mapc (lambda (e) (org-list-set-checkbox
2384 ;; If there is no box at item, leave as-is
2385 ;; unless function was called with C-u prefix.
2386 (let ((cur-box (org-list-get-checkbox e struct
)))
2387 (if (or cur-box
(equal toggle-presence
'(4)))
2391 (setq block-item
(org-list-struct-fix-box
2392 struct parents prevs orderedp
))
2393 ;; Report some problems due to ORDERED status of subtree.
2394 ;; If only one box was being checked, throw an error, else,
2395 ;; only signal problems.
2397 ((and singlep block-item
(> lim-up block-item
))
2399 "Checkbox blocked because of unchecked box at line %d"
2400 (org-current-line block-item
)))
2403 "Checkboxes were removed due to unchecked box at line %d"
2404 (org-current-line block-item
))))
2406 (move-marker bottom nil
)
2407 (org-list-struct-apply-struct struct struct-copy
)))
2408 (move-marker lim-down nil
)))
2409 (org-update-checkbox-count-maybe))
2411 (defun org-reset-checkbox-state-subtree ()
2412 "Reset all checkboxes in an entry subtree."
2414 (if (org-before-first-heading-p)
2415 (error "Not inside a tree")
2418 (org-narrow-to-subtree)
2420 (goto-char (point-min))
2421 (let ((end (point-max)))
2422 (while (< (point) end
)
2423 (when (org-at-item-checkbox-p)
2424 (replace-match "[ ]" t t nil
1))
2425 (beginning-of-line 2)))
2426 (org-update-checkbox-count-maybe 'all
)))))
2428 (defun org-update-checkbox-count (&optional all
)
2429 "Update the checkbox statistics in the current section.
2430 This will find all statistic cookies like [57%] and [6/12] and
2431 update them with the current numbers.
2433 With optional prefix argument ALL, do this for the whole buffer."
2436 (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2437 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2439 (or (not org-checkbox-hierarchical-statistics
)
2440 (string-match "\\<recursive\\>"
2441 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2443 (cons (point-min) (point-max))
2444 (cons (or (ignore-errors (org-back-to-heading t
) (point))
2446 (save-excursion (outline-next-heading) (point)))))
2449 ;; Return number of checked boxes and boxes of all types
2450 ;; in all structures in STRUCTS. If RECURSIVEP is
2451 ;; non-nil, also count boxes in sub-lists. If ITEM is
2452 ;; nil, count across the whole structure, else count only
2453 ;; across subtree whose ancestor is ITEM.
2454 (lambda (item structs recursivep
)
2455 (let ((c-on 0) (c-all 0))
2458 (let* ((pre (org-list-prevs-alist s
))
2459 (par (org-list-parents-alist s
))
2462 ((and recursivep item
) (org-list-get-subtree item s
))
2463 (recursivep (mapcar 'car s
))
2464 (item (org-list-get-children item s par
))
2465 (t (org-list-get-all-items
2466 (org-list-get-top-point s
) s pre
))))
2467 (cookies (delq nil
(mapcar
2469 (org-list-get-checkbox e s
))
2471 (setq c-all
(+ (length cookies
) c-all
)
2472 c-on
(+ (org-count "[X]" cookies
) c-on
))))
2474 (cons c-on c-all
)))))
2476 cookies-list structs-bak box-num
)
2477 (goto-char (car bounds
))
2478 ;; 1. Build an alist for each cookie found within BOUNDS. The
2479 ;; key will be position at beginning of cookie and values
2480 ;; ending position, format of cookie, and a cell whose car is
2481 ;; number of checked boxes to report, and cdr total number of
2483 (while (re-search-forward cookie-re
(cdr bounds
) t
)
2488 (match-beginning 1) ; cookie start
2489 (match-end 1) ; cookie end
2490 (match-string 2) ; percent?
2492 ;; Cookie is at an heading, but specifically for todo,
2493 ;; not for checkboxes: skip it.
2494 ((and (org-at-heading-p)
2495 (string-match "\\<todo\\>"
2497 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2499 ;; Cookie is at an heading, but all lists before next
2500 ;; heading already have been read. Use data collected
2501 ;; in STRUCTS-BAK. This should only happen when
2502 ;; heading has more than one cookie on it.
2503 ((and (org-at-heading-p)
2504 (<= (save-excursion (outline-next-heading) (point))
2506 (funcall count-boxes nil structs-bak recursivep
))
2507 ;; Cookie is at a fresh heading. Grab structure of
2508 ;; every list containing a checkbox between point and
2509 ;; next headline, and save them in STRUCTS-BAK.
2511 (setq backup-end
(save-excursion
2512 (outline-next-heading) (point))
2514 (while (org-list-search-forward box-re backup-end
'move
)
2515 (let* ((struct (org-list-struct))
2516 (bottom (org-list-get-bottom-point struct
)))
2517 (push struct structs-bak
)
2518 (goto-char bottom
)))
2519 (funcall count-boxes nil structs-bak recursivep
))
2520 ;; Cookie is at an item, and we already have list
2521 ;; structure stored in STRUCTS-BAK.
2522 ((and (org-at-item-p)
2523 (< (point-at-bol) backup-end
)
2524 ;; Only lists in no special context are stored.
2525 (not (nth 2 (org-list-context))))
2526 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2527 ;; Cookie is at an item, but we need to compute list
2530 (let ((struct (org-list-struct)))
2531 (setq backup-end
(org-list-get-bottom-point struct
)
2532 structs-bak
(list struct
)))
2533 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2534 ;; Else, cookie found is at a wrong place. Skip it.
2535 (t (throw 'skip nil
))))
2537 ;; 2. Apply alist to buffer, in reverse order so positions stay
2538 ;; unchanged after cookie modifications.
2539 (mapc (lambda (cookie)
2540 (let* ((beg (car cookie
))
2541 (end (nth 1 cookie
))
2542 (percentp (nth 2 cookie
))
2543 (checked (car (nth 3 cookie
)))
2544 (total (cdr (nth 3 cookie
)))
2546 (format "[%d%%]" (/ (* 100 checked
)
2548 (format "[%d/%d]" checked total
))))
2551 (delete-region (point) (+ (point) (- end beg
)))
2552 (when org-auto-align-tags
(org-fix-tags-on-the-fly))))
2555 (defun org-get-checkbox-statistics-face ()
2556 "Select the face for checkbox statistics.
2557 The face will be `org-done' when all relevant boxes are checked.
2558 Otherwise it will be `org-todo'."
2560 (if (equal (match-string 1) "100%")
2561 'org-checkbox-statistics-done
2562 'org-checkbox-statistics-todo
)
2563 (if (and (> (match-end 2) (match-beginning 2))
2564 (equal (match-string 2) (match-string 3)))
2565 'org-checkbox-statistics-done
2566 'org-checkbox-statistics-todo
)))
2568 (defun org-update-checkbox-count-maybe (&optional all
)
2569 "Update checkbox statistics unless turned off by user.
2570 With an optional argument ALL, update them in the whole buffer."
2571 (when (cdr (assq 'checkbox org-list-automatic-rules
))
2572 (org-update-checkbox-count all
))
2573 (run-hooks 'org-checkbox-statistics-hook
))
2575 (defvar org-last-indent-begin-marker
(make-marker))
2576 (defvar org-last-indent-end-marker
(make-marker))
2577 (defun org-list-indent-item-generic (arg no-subtree struct
)
2578 "Indent a local list item including its children.
2579 When number ARG is a negative, item will be outdented, otherwise
2580 it will be indented.
2582 If a region is active, all items inside will be moved.
2584 If NO-SUBTREE is non-nil, only indent the item itself, not its
2587 STRUCT is the list structure.
2589 Return t if successful."
2591 (let* ((regionp (org-region-active-p))
2592 (rbeg (and regionp
(region-beginning)))
2593 (rend (and regionp
(region-end)))
2594 (top (org-list-get-top-point struct
))
2595 (parents (org-list-parents-alist struct
))
2596 (prevs (org-list-prevs-alist struct
))
2597 ;; Are we going to move the whole list?
2600 (= top
(point-at-bol))
2601 (cdr (assq 'indent org-list-automatic-rules
))
2604 "First item of list cannot move without its subtree")
2606 ;; Determine begin and end points of zone to indent. If moving
2607 ;; more than one item, save them for subsequent moves.
2608 (unless (and (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
2609 (memq this-command
'(org-shiftmetaright org-shiftmetaleft
)))
2612 (set-marker org-last-indent-begin-marker rbeg
)
2613 (set-marker org-last-indent-end-marker rend
))
2614 (set-marker org-last-indent-begin-marker
(point-at-bol))
2615 (set-marker org-last-indent-end-marker
2617 (specialp (org-list-get-bottom-point struct
))
2618 (no-subtree (1+ (point-at-bol)))
2619 (t (org-list-get-item-end (point-at-bol) struct
))))))
2620 (let* ((beg (marker-position org-last-indent-begin-marker
))
2621 (end (marker-position org-last-indent-end-marker
)))
2623 ;; Special case: moving top-item with indent rule.
2625 (let* ((level-skip (org-level-increment))
2626 (offset (if (< arg
0) (- level-skip
) level-skip
))
2627 (top-ind (org-list-get-ind beg struct
))
2628 (old-struct (copy-tree struct
)))
2629 (if (< (+ top-ind offset
) 0)
2630 (error "Cannot outdent beyond margin")
2631 ;; Change bullet if necessary.
2632 (when (and (= (+ top-ind offset
) 0)
2634 (org-list-get-bullet beg struct
)))
2635 (org-list-set-bullet beg struct
2636 (org-list-bullet-string "-")))
2637 ;; Shift every item by OFFSET and fix bullets. Then
2638 ;; apply changes to buffer.
2640 (let ((ind (org-list-get-ind (car e
) struct
)))
2641 (org-list-set-ind (car e
) struct
(+ ind offset
))))
2643 (org-list-struct-fix-bul struct prevs
)
2644 (org-list-struct-apply-struct struct old-struct
))))
2647 ;; If only one item is moved, it mustn't have a child.
2650 (org-list-has-child-p beg struct
))
2651 ;; If a subtree or region is moved, the last item
2652 ;; of the subtree mustn't have a child.
2653 (let ((last-item (caar
2656 (lambda (e) (>= (car e
) end
))
2658 (org-list-has-child-p last-item struct
))))
2659 (error "Cannot outdent an item without its children"))
2664 (org-list-struct-outdent beg end struct parents
)
2665 (org-list-struct-indent beg end struct parents prevs
))))
2666 (org-list-write-struct struct new-parents
))
2667 (org-update-checkbox-count-maybe))))))
2670 (defun org-outdent-item ()
2671 "Outdent a local list item, but not its children.
2672 If a region is active, all items inside will be moved."
2674 (let ((regionp (org-region-active-p)))
2676 ((or (org-at-item-p)
2678 (save-excursion (goto-char (region-beginning))
2680 (let ((struct (if (not regionp
) (org-list-struct)
2681 (save-excursion (goto-char (region-beginning))
2682 (org-list-struct)))))
2683 (org-list-indent-item-generic -
1 t struct
)))
2684 (regionp (error "Region not starting at an item"))
2685 (t (error "Not at an item")))))
2687 (defun org-indent-item ()
2688 "Indent a local list item, but not its children.
2689 If a region is active, all items inside will be moved."
2691 (let ((regionp (org-region-active-p)))
2693 ((or (org-at-item-p)
2695 (save-excursion (goto-char (region-beginning))
2697 (let ((struct (if (not regionp
) (org-list-struct)
2698 (save-excursion (goto-char (region-beginning))
2699 (org-list-struct)))))
2700 (org-list-indent-item-generic 1 t struct
)))
2701 (regionp (error "Region not starting at an item"))
2702 (t (error "Not at an item")))))
2704 (defun org-outdent-item-tree ()
2705 "Outdent a local list item including its children.
2706 If a region is active, all items inside will be moved."
2708 (let ((regionp (org-region-active-p)))
2710 ((or (org-at-item-p)
2712 (save-excursion (goto-char (region-beginning))
2714 (let ((struct (if (not regionp
) (org-list-struct)
2715 (save-excursion (goto-char (region-beginning))
2716 (org-list-struct)))))
2717 (org-list-indent-item-generic -
1 nil struct
)))
2718 (regionp (error "Region not starting at an item"))
2719 (t (error "Not at an item")))))
2721 (defun org-indent-item-tree ()
2722 "Indent a local list item including its children.
2723 If a region is active, all items inside will be moved."
2725 (let ((regionp (org-region-active-p)))
2727 ((or (org-at-item-p)
2729 (save-excursion (goto-char (region-beginning))
2731 (let ((struct (if (not regionp
) (org-list-struct)
2732 (save-excursion (goto-char (region-beginning))
2733 (org-list-struct)))))
2734 (org-list-indent-item-generic 1 nil struct
)))
2735 (regionp (error "Region not starting at an item"))
2736 (t (error "Not at an item")))))
2738 (defvar org-tab-ind-state
)
2739 (defun org-cycle-item-indentation ()
2740 "Cycle levels of indentation of an empty item.
2741 The first run indents the item, if applicable. Subsequent runs
2742 outdent it at meaningful levels in the list. When done, item is
2743 put back at its original position with its original bullet.
2745 Return t at each successful move."
2746 (when (org-at-item-p)
2747 (let* ((org-adapt-indentation nil
)
2748 (struct (org-list-struct))
2749 (ind (org-list-get-ind (point-at-bol) struct
))
2750 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2751 ;; Accept empty items or if cycle has already started.
2752 (when (or (eq last-command
'org-cycle-item-indentation
)
2753 (and (save-excursion
2755 (looking-at org-list-full-item-re
))
2756 (>= (match-end 0) (save-excursion
2757 (goto-char (org-list-get-item-end
2758 (point-at-bol) struct
))
2759 (skip-chars-backward " \r\t\n")
2761 (setq this-command
'org-cycle-item-indentation
)
2762 ;; When in the middle of the cycle, try to outdent first. If
2763 ;; it fails, and point is still at initial position, indent.
2764 ;; Else, re-create it at its original position.
2765 (if (eq last-command
'org-cycle-item-indentation
)
2767 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2768 ((and (= ind
(car org-tab-ind-state
))
2769 (ignore-errors (org-list-indent-item-generic 1 t struct
))))
2770 (t (delete-region (point-at-bol) (point-at-eol))
2771 (org-indent-to-column (car org-tab-ind-state
))
2772 (insert (cdr org-tab-ind-state
) " ")
2774 (setq this-command
'identity
)))
2775 ;; If a cycle is starting, remember indentation and bullet,
2776 ;; then try to indent. If it fails, try to outdent.
2777 (setq org-tab-ind-state
(cons ind bullet
))
2779 ((ignore-errors (org-list-indent-item-generic 1 t struct
)))
2780 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2781 (t (user-error "Cannot move item"))))
2784 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func
)
2786 The cursor may be at any item of the list that should be sorted.
2787 Sublists are not sorted. Checkboxes, if any, are ignored.
2789 Sorting can be alphabetically, numerically, by date/time as given
2790 by a time stamp, by a property or by priority.
2792 Comparing entries ignores case by default. However, with an
2793 optional argument WITH-CASE, the sorting considers case as well.
2795 The command prompts for the sorting type unless it has been given
2796 to the function through the SORTING-TYPE argument, which needs to
2797 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F ?x ?X). Here is the
2798 detailed meaning of each character:
2800 n Numerically, by converting the beginning of the item to a number.
2801 a Alphabetically. Only the first line of item is checked.
2802 t By date/time, either the first active time stamp in the entry, if
2803 any, or by the first inactive one. In a timer list, sort the timers.
2804 x By \"checked\" status of a check list.
2806 Capital letters will reverse the sort order.
2808 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2809 a function to be called with point at the beginning of the
2810 record. It must return either a string or a number that should
2811 serve as the sorting key for that record. It will then use
2812 COMPARE-FUNC to compare entries.
2814 Sorting is done against the visible part of the headlines, it
2815 ignores hidden links."
2817 (let* ((case-func (if with-case
'identity
'downcase
))
2818 (struct (org-list-struct))
2819 (prevs (org-list-prevs-alist struct
))
2820 (start (org-list-get-list-begin (point-at-bol) struct prevs
))
2821 (end (org-list-get-list-end (point-at-bol) struct prevs
))
2826 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc [x]checked A/N/T/F/X means reversed:")
2827 (read-char-exclusive))))
2830 (and (= (downcase sorting-type
) ?f
)
2831 (intern (org-icompleting-read "Sort using function: "
2832 obarray
'fboundp t nil nil
))))))
2833 (message "Sorting items...")
2835 (narrow-to-region start end
)
2836 (goto-char (point-min))
2837 (let* ((dcst (downcase sorting-type
))
2838 (case-fold-search nil
)
2839 (now (current-time))
2841 ((= dcst ?a
) 'string
<)
2842 ((= dcst ?f
) compare-func
)
2844 ((= dcst ?x
) 'string
<)))
2845 (next-record (lambda ()
2846 (skip-chars-forward " \r\t\n")
2847 (or (eobp) (beginning-of-line))))
2848 (end-record (lambda ()
2849 (goto-char (org-list-get-item-end-before-blank
2853 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2857 (org-sort-remove-invisible
2858 (buffer-substring (match-end 0) (point-at-eol)))))
2861 (org-sort-remove-invisible
2863 (match-end 0) (point-at-eol)))))
2866 ;; If it is a timer list, convert timer to seconds
2867 ((org-at-item-timer-p)
2868 (org-timer-hms-to-secs (match-string 1)))
2869 ((or (save-excursion
2870 (re-search-forward org-ts-regexp
(point-at-eol) t
))
2871 (save-excursion (re-search-forward org-ts-regexp-both
2873 (org-time-string-to-seconds (match-string 0)))
2874 (t (org-float-time now
))))
2875 ((= dcst ?x
) (or (and (stringp (match-string 1))
2880 (let ((value (funcall getkey-func
)))
2882 (funcall case-func value
)
2884 (error "Invalid key function `%s'" getkey-func
)))
2885 (t (error "Invalid sorting type `%c'" sorting-type
)))))))
2886 (sort-subr (/= dcst sorting-type
)
2892 ;; Read and fix list again, as `sort-subr' probably destroyed
2895 (run-hooks 'org-after-sorting-entries-or-items-hook
)
2896 (message "Sorting items...done")))))
2900 ;;; Send and receive lists
2902 (defun org-list-parse-list (&optional delete
)
2903 "Parse the list at point and maybe DELETE it.
2905 Return a list whose car is a symbol of list type, among
2906 `ordered', `unordered' and `descriptive'. Then, each item is
2907 a list whose car is counter, and cdr are strings and other
2908 sub-lists. Inside strings, check-boxes are replaced by
2909 \"[CBON]\", \"[CBOFF]\" and \"[CBTRANS]\".
2911 For example, the following list:
2916 more text in first item
2922 \(nil \"first item\"
2924 \(nil \"sub-item one\"\)
2925 \(nil \"[CBON] sub-item two\"\)\)
2926 \"more text in first item\"\)
2927 \(3 \"last item\"\)\)
2929 Point is left at list end."
2930 (let* ((struct (org-list-struct))
2931 (prevs (org-list-prevs-alist struct
))
2932 (parents (org-list-parents-alist struct
))
2933 (top (org-list-get-top-point struct
))
2934 (bottom (org-list-get-bottom-point struct
))
2936 parse-item
; for byte-compiler
2939 ;; Return text between BEG and END, trimmed, with
2940 ;; checkboxes replaced.
2942 (let ((text (org-trim (buffer-substring beg end
))))
2943 (if (string-match "\\`\\[\\([-X ]\\)\\]" text
)
2945 (let ((box (match-string 1 text
)))
2947 ((equal box
" ") "CBOFF")
2948 ((equal box
"-") "CBTRANS")
2954 ;; Return a list whose car is list type and cdr a list of
2957 (cons (org-list-get-list-type (car e
) struct prevs
)
2958 (mapcar parse-item e
)))))
2961 ;; Return a list containing counter of item, if any, text
2962 ;; and any sublist inside it.
2964 (let ((start (save-excursion
2966 (looking-at "[ \t]*\\S-+\\([ \t]+\\[@\\(start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\]\\)?[ \t]*")
2968 ;; Get counter number. For alphabetic counter, get
2969 ;; its position in the alphabet.
2970 (counter (let ((c (org-list-get-counter e struct
)))
2973 ((string-match "[A-Za-z]" c
)
2974 (- (string-to-char (upcase (match-string 0 c
)))
2976 ((string-match "[0-9]+" c
)
2977 (string-to-number (match-string 0 c
))))))
2978 (childp (org-list-has-child-p e struct
))
2979 (end (org-list-get-item-end e struct
)))
2980 ;; If item has a child, store text between bullet and
2981 ;; next child, then recursively parse all sublists. At
2982 ;; the end of each sublist, check for the presence of
2983 ;; text belonging to the original item.
2985 (let* ((children (org-list-get-children e struct parents
))
2986 (body (list (funcall get-text start childp
))))
2988 (let* ((first (car children
))
2989 (sub (org-list-get-all-items first struct prevs
))
2990 (last-c (car (last sub
)))
2991 (last-end (org-list-get-item-end last-c struct
)))
2992 (push (funcall parse-sublist sub
) body
)
2993 ;; Remove children from the list just parsed.
2994 (setq children
(cdr (member last-c children
)))
2995 ;; There is a chunk of text belonging to the
2996 ;; item if last child doesn't end where next
2997 ;; child starts or where item ends.
2998 (unless (= (or (car children
) end
) last-end
)
2999 (push (funcall get-text
3000 last-end
(or (car children
) end
))
3002 (cons counter
(nreverse body
)))
3003 (list counter
(funcall get-text start end
))))))))
3004 ;; Store output, take care of cursor position and deletion of
3005 ;; list, then return output.
3006 (setq out
(funcall parse-sublist
(org-list-get-all-items top struct prevs
)))
3009 (delete-region top bottom
)
3010 (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re
))
3011 (replace-match "")))
3014 (defun org-list-make-subtree ()
3015 "Convert the plain list at point into a subtree."
3017 (if (not (ignore-errors (goto-char (org-in-item-p))))
3018 (error "Not in a list")
3019 (let ((list (save-excursion (org-list-parse-list t
))))
3020 (insert (org-list-to-subtree list
)))))
3022 (defun org-list-insert-radio-list ()
3023 "Insert a radio list template appropriate for this major mode."
3025 (let* ((e (assq major-mode org-list-radio-list-templates
))
3028 (unless e
(error "No radio list setup defined for %s" major-mode
))
3029 (setq name
(read-string "List name: "))
3030 (while (string-match "%n" txt
)
3031 (setq txt
(replace-match name t t txt
)))
3032 (or (bolp) (insert "\n"))
3037 (defun org-list-send-list (&optional maybe
)
3038 "Send a transformed version of this list to the receiver position.
3039 With argument MAYBE, fail quietly if no transformation is defined
3043 (unless (org-at-item-p) (error "Not at a list item"))
3045 (re-search-backward "#\\+ORGLST" nil t
)
3046 (unless (looking-at "#\\+ORGLST:[ \t]+SEND[ \t]+\\(\\S-+\\)[ \t]+\\(\\S-+\\)")
3047 (if maybe
(throw 'exit nil
)
3048 (error "Don't know how to transform this list"))))
3049 (let* ((name (match-string 1))
3050 (transform (intern (match-string 2)))
3054 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t
)
3055 (match-beginning 0)))
3058 (re-search-backward "#\\+ORGLST" nil t
)
3059 (re-search-forward (org-item-beginning-re) bottom-point t
)
3060 (match-beginning 0)))
3061 (plain-list (buffer-substring-no-properties top-point bottom-point
))
3063 (unless (fboundp transform
)
3064 (error "No such transformation function %s" transform
))
3065 (let ((txt (funcall transform plain-list
)))
3066 ;; Find the insertion place
3068 (goto-char (point-min))
3069 (unless (re-search-forward
3070 (concat "BEGIN RECEIVE ORGLST +"
3072 "\\([ \t]\\|$\\)") nil t
)
3073 (error "Don't know where to insert translated list"))
3074 (goto-char (match-beginning 0))
3075 (beginning-of-line 2)
3077 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
3078 (error "Cannot find end of insertion region"))
3079 (delete-region beg
(point-at-bol))
3082 (message "List converted and installed at receiver location"))))
3084 (defsubst org-list-item-trim-br
(item)
3085 "Trim line breaks in a list ITEM."
3086 (setq item
(replace-regexp-in-string "\n +" " " item
)))
3088 (defun org-list-to-generic (list params
)
3089 "Convert a LIST parsed through `org-list-parse-list' to other formats.
3090 Valid parameters PARAMS are:
3092 :ustart String to start an unordered list
3093 :uend String to end an unordered list
3095 :ostart String to start an ordered list
3096 :oend String to end an ordered list
3098 :dstart String to start a descriptive list
3099 :dend String to end a descriptive list
3100 :dtstart String to start a descriptive term
3101 :dtend String to end a descriptive term
3102 :ddstart String to start a description
3103 :ddend String to end a description
3105 :splice When set to t, return only list body lines, don't wrap
3106 them into :[u/o]start and :[u/o]end. Default is nil.
3108 :istart String to start a list item.
3109 :icount String to start an item with a counter.
3110 :iend String to end a list item
3111 :isep String to separate items
3112 :lsep String to separate sublists
3113 :csep String to separate text from a sub-list
3115 :cboff String to insert for an unchecked check-box
3116 :cbon String to insert for a checked check-box
3117 :cbtrans String to insert for a check-box in transitional state
3119 :nobr Non-nil means remove line breaks in lists items.
3121 Alternatively, each parameter can also be a form returning
3122 a string. These sexp can use keywords `counter' and `depth',
3123 representing respectively counter associated to the current
3124 item, and depth of the current sub-list, starting at 0.
3125 Obviously, `counter' is only available for parameters applying to
3129 (splicep (plist-get p
:splice
))
3130 (ostart (plist-get p
:ostart
))
3131 (oend (plist-get p
:oend
))
3132 (ustart (plist-get p
:ustart
))
3133 (uend (plist-get p
:uend
))
3134 (dstart (plist-get p
:dstart
))
3135 (dend (plist-get p
:dend
))
3136 (dtstart (plist-get p
:dtstart
))
3137 (dtend (plist-get p
:dtend
))
3138 (ddstart (plist-get p
:ddstart
))
3139 (ddend (plist-get p
:ddend
))
3140 (istart (plist-get p
:istart
))
3141 (icount (plist-get p
:icount
))
3142 (iend (plist-get p
:iend
))
3143 (isep (plist-get p
:isep
))
3144 (lsep (plist-get p
:lsep
))
3145 (csep (plist-get p
:csep
))
3146 (cbon (plist-get p
:cbon
))
3147 (cboff (plist-get p
:cboff
))
3148 (cbtrans (plist-get p
:cbtrans
))
3149 (nobr (plist-get p
:nobr
))
3150 export-sublist
; for byte-compiler
3153 ;; Export an item ITEM of type TYPE, at DEPTH. First
3154 ;; string in item is treated in a special way as it can
3155 ;; bring extra information that needs to be processed.
3156 (lambda (item type depth
)
3157 (let* ((counter (pop item
))
3160 ((eq type
'descriptive
)
3161 ;; Stick DTSTART to ISTART by
3162 ;; left-trimming the latter.
3163 (concat (let ((s (eval istart
)))
3164 (or (and (string-match "[ \t\n\r]+\\'" s
)
3165 (replace-match "" t t s
))
3168 ((and counter
(eq type
'ordered
))
3169 (concat (eval icount
) "%s"))
3170 (t (concat (eval istart
) "%s")))
3173 ;; Replace checkbox if any is found.
3175 ((string-match "\\[CBON\\]" first
)
3176 (setq first
(replace-match cbon t t first
)))
3177 ((string-match "\\[CBOFF\\]" first
)
3178 (setq first
(replace-match cboff t t first
)))
3179 ((string-match "\\[CBTRANS\\]" first
)
3180 (setq first
(replace-match cbtrans t t first
))))
3181 ;; Replace line breaks if required
3182 (when nobr
(setq first
(org-list-item-trim-br first
)))
3183 ;; Insert descriptive term if TYPE is `descriptive'.
3184 (when (eq type
'descriptive
)
3185 (let* ((complete (string-match "^\\(.*\\)[ \t]+::" first
))
3188 (org-trim (match-string 1 first
)))
3191 (org-trim (substring first
(match-end 0)))
3193 (setq first
(concat (eval dtstart
) term
(eval dtend
)
3194 (eval ddstart
) desc
))))
3197 (mapconcat (lambda (e)
3199 (funcall export-sublist e
(1+ depth
))))
3200 item
(or (eval csep
) "")))))))
3203 ;; Export sublist SUB at DEPTH.
3205 (let* ((type (car sub
))
3210 (concat (eval ostart
) "%s" (eval oend
)))
3211 ((eq type
'descriptive
)
3212 (concat (eval dstart
) "%s" (eval dend
)))
3213 (t (concat (eval ustart
) "%s" (eval uend
))))
3215 (format fmt
(mapconcat (lambda (e)
3216 (funcall export-item e type depth
))
3217 items
(or (eval isep
) ""))))))))
3218 (concat (funcall export-sublist list
0) "\n")))
3220 (defun org-list-to-latex (list &optional params
)
3221 "Convert LIST into a LaTeX list.
3222 LIST is as string representing the list to transform, as Org
3223 syntax. Return converted list as a string."
3225 (org-export-string-as list
'latex t
))
3227 (defun org-list-to-html (list)
3228 "Convert LIST into a HTML list.
3229 LIST is as string representing the list to transform, as Org
3230 syntax. Return converted list as a string."
3232 (org-export-string-as list
'html t
))
3234 (defun org-list-to-texinfo (list &optional params
)
3235 "Convert LIST into a Texinfo list.
3236 LIST is as string representing the list to transform, as Org
3237 syntax. Return converted list as a string."
3238 (require 'ox-texinfo
)
3239 (org-export-string-as list
'texinfo t
))
3241 (defun org-list-to-subtree (list &optional params
)
3242 "Convert LIST into an Org subtree.
3243 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3244 with overruling parameters for `org-list-to-generic'."
3245 (let* ((rule (cdr (assq 'heading org-blank-before-new-entry
)))
3246 (level (org-reduced-level (or (org-current-level) 0)))
3247 (blankp (or (eq rule t
)
3248 (and (eq rule
'auto
)
3250 (outline-previous-heading)
3251 (org-previous-line-empty-p)))))
3254 ;; Return the string for the heading, depending on depth D
3255 ;; of current sub-list.
3257 (let ((oddeven-level (+ level d
1)))
3258 (concat (make-string (if org-odd-levels-only
3259 (1- (* 2 oddeven-level
))
3263 (org-list-to-generic
3267 :dtstart
" " :dtend
" "
3268 :istart
(funcall get-stars depth
)
3269 :icount
(funcall get-stars depth
)
3270 :isep
(if blankp
"\n\n" "\n")
3271 :csep
(if blankp
"\n\n" "\n")
3272 :cbon
"DONE" :cboff
"TODO" :cbtrans
"TODO")
3277 ;;; org-list.el ends here