1 ;;; org-list.el --- Plain lists for Org-mode
3 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Bastien Guerry <bzg AT gnu DOT 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
)
92 (defvar org-odd-levels-only
)
93 (defvar org-scheduled-string
)
94 (defvar org-ts-regexp
)
95 (defvar org-ts-regexp-both
)
97 (declare-function org-at-heading-p
"org" (&optional ignored
))
98 (declare-function org-before-first-heading-p
"org" ())
99 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
100 (declare-function org-combine-plists
"org" (&rest plists
))
101 (declare-function org-count
"org" (cl-item cl-seq
))
102 (declare-function org-current-level
"org" ())
103 (declare-function org-entry-get
"org"
104 (pom property
&optional inherit literal-nil
))
105 (declare-function org-fix-tags-on-the-fly
"org" ())
106 (declare-function org-get-indentation
"org" (&optional line
))
107 (declare-function org-icompleting-read
"org" (&rest args
))
108 (declare-function org-in-block-p
"org" (names))
109 (declare-function org-in-regexp
"org" (re &optional nlines visually
))
110 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
111 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
112 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
113 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
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-time-string-to-seconds
"org" (s))
122 (declare-function org-timer-hms-to-secs
"org-timer" (hms))
123 (declare-function org-timer-item
"org-timer" (&optional arg
))
124 (declare-function org-trim
"org" (s))
125 (declare-function org-uniquify
"org" (list))
126 (declare-function outline-invisible-p
"outline" (&optional pos
))
127 (declare-function outline-flag-region
"outline" (from to flag
))
128 (declare-function outline-next-heading
"outline" ())
129 (declare-function outline-previous-heading
"outline" ())
133 ;;; Configuration variables
135 (defgroup org-plain-lists nil
136 "Options concerning plain lists in Org-mode."
137 :tag
"Org Plain lists"
138 :group
'org-structure
)
140 (defcustom org-cycle-include-plain-lists t
141 "When t, make TAB cycle visibility on plain list items.
142 Cycling plain lists works only when the cursor is on a plain list
143 item. When the cursor is on an outline heading, plain lists are
144 treated as text. This is the most stable way of handling this,
145 which is why it is the default.
147 When this is the symbol `integrate', then during cycling, plain
148 list items will *temporarily* be interpreted as outline headlines
149 with a level given by 1000+i where i is the indentation of the
150 bullet. This setting can lead to strange effects when switching
151 visibility to `children', because the first \"child\" in a
152 subtree decides what children should be listed. If that first
153 \"child\" is a plain list item with an implied large level
154 number, all true children and grand children of the outline
155 heading will be exposed in a children' view."
156 :group
'org-plain-lists
158 (const :tag
"Never" nil
)
159 (const :tag
"With cursor in plain list (recommended)" t
)
160 (const :tag
"As children of outline headings" integrate
)))
162 (defcustom org-list-demote-modify-bullet nil
163 "Default bullet type installed when demoting an item.
164 This is an association list, for each bullet type, this alist will point
165 to the bullet that should be used when this item is demoted.
168 (setq org-list-demote-modify-bullet
169 '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
174 + Silence of the Lambs
177 + The Hunt for Red October
183 - Silence of the Lambs
186 - The Hunt for Red October
188 :group
'org-plain-lists
191 (choice :tag
"If the current bullet is "
197 (choice :tag
"demotion will change it to"
204 (defcustom org-plain-list-ordered-item-terminator t
205 "The character that makes a line with leading number an ordered list item.
206 Valid values are ?. and ?\). To get both terminators, use t."
207 :group
'org-plain-lists
208 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
209 (const :tag
"paren like in \"2)\"" ?\
))
210 (const :tab
"both" t
)))
212 (defcustom org-alphabetical-lists nil
213 "Non-nil means single character alphabetical bullets are allowed.
214 Both uppercase and lowercase are handled. Lists with more than
215 26 items will fallback to standard numbering. Alphabetical
216 counters like \"[@c]\" will be recognized."
217 :group
'org-plain-lists
221 (defcustom org-list-two-spaces-after-bullet-regexp nil
222 "A regular expression matching bullets that should have 2 spaces after them.
223 When nil, no bullet will have two spaces after them. When
224 a string, it will be used as a regular expression. When the
225 bullet type of a list is changed, the new bullet type will be
226 matched against this regexp. If it matches, there will be two
227 spaces instead of one after the bullet in each item of the list."
228 :group
'org-plain-lists
230 (const :tag
"never" nil
)
233 (defcustom org-empty-line-terminates-plain-lists nil
234 "Non-nil means an empty line ends all plain list levels.
235 Otherwise, two of them will be necessary."
236 :group
'org-plain-lists
239 (defcustom org-list-automatic-rules
'((bullet . t
)
242 "Non-nil means apply set of rules when acting on lists.
243 By default, automatic actions are taken when using
244 \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
245 \\[org-shiftmetaright], \\[org-shiftmetaleft],
246 \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
247 \\[org-insert-todo-heading]. You can disable individually these
248 rules by setting them to nil. Valid rules are:
250 bullet when non-nil, cycling bullet do not allow lists at
251 column 0 to have * as a bullet and descriptions lists
253 checkbox when non-nil, checkbox statistics is updated each time
254 you either insert a new checkbox or toggle a checkbox.
255 It also prevents from inserting a checkbox in a
257 indent when non-nil, indenting or outdenting list top-item
258 with its subtree will move the whole list and
259 outdenting a list whose bullet is * to column 0 will
260 change that bullet to \"-\"."
261 :group
'org-plain-lists
263 :type
'(alist :tag
"Sets of rules"
266 (const :tag
"Bullet" bullet
)
267 (const :tag
"Checkbox" checkbox
)
268 (const :tag
"Indent" indent
))
270 (boolean :tag
"Activate" :value t
)))
272 (defcustom org-list-use-circular-motion nil
273 "Non-nil means commands implying motion in lists should be cyclic.
275 In that case, the item following the last item is the first one,
276 and the item preceding the first item is the last one.
278 This affects the behavior of \\[org-move-item-up],
279 \\[org-move-item-down], \\[org-next-item] and
280 \\[org-previous-item]."
281 :group
'org-plain-lists
285 (defvar org-checkbox-statistics-hook nil
286 "Hook that is run whenever Org thinks checkbox statistics should be updated.
287 This hook runs even if checkbox rule in
288 `org-list-automatic-rules' does not apply, so it can be used to
289 implement alternative ways of collecting statistics
292 (defcustom org-hierarchical-checkbox-statistics t
293 "Non-nil means checkbox statistics counts only the state of direct children.
294 When nil, all boxes below the cookie are counted.
295 This can be set to nil on a per-node basis using a COOKIE_DATA property
296 with the word \"recursive\" in the value."
297 :group
'org-plain-lists
300 (defcustom org-description-max-indent
20
301 "Maximum indentation for the second line of a description list.
302 When the indentation would be larger than this, it will become
303 5 characters instead."
304 :group
'org-plain-lists
307 (defcustom org-list-indent-offset
0
308 "Additional indentation for sub-items in a list.
309 By setting this to a small number, usually 1 or 2, one can more
310 clearly distinguish sub-items in a list."
311 :group
'org-plain-lists
315 (defcustom org-list-radio-list-templates
316 '((latex-mode "% BEGIN RECEIVE ORGLST %n
317 % END RECEIVE ORGLST %n
319 #+ORGLST: SEND %n org-list-to-latex
322 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
323 @c END RECEIVE ORGLST %n
325 #+ORGLST: SEND %n org-list-to-texinfo
328 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
329 <!-- END RECEIVE ORGLST %n -->
331 #+ORGLST: SEND %n org-list-to-html
334 "Templates for radio lists in different major modes.
335 All occurrences of %n in a template will be replaced with the name of the
336 list, obtained by prompting the user."
337 :group
'org-plain-lists
339 (list (symbol :tag
"Major mode")
340 (string :tag
"Format"))))
342 (defvar org-list-forbidden-blocks
'("example" "verse" "src" "ascii" "beamer"
343 "docbook" "html" "latex" "odt")
344 "Names of blocks where lists are not allowed.
345 Names must be in lower case.")
347 (defvar org-list-export-context
'(block inlinetask
)
348 "Context types where lists will be interpreted during export.
350 Valid types are `drawer', `inlinetask' and `block'. More
351 specifically, type `block' is determined by the variable
352 `org-list-forbidden-blocks'.")
356 ;;; Predicates and regexps
358 (defconst org-list-end-re
(if org-empty-line-terminates-plain-lists
"^[ \t]*\n"
360 "Regex corresponding to the end of a list.
361 It depends on `org-empty-line-terminates-plain-lists'.")
363 (defconst org-list-full-item-re
364 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
365 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
366 "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
367 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
368 "Matches a list item and puts everything into groups:
372 group 4: description tag")
374 (defun org-item-re ()
375 "Return the correct regular expression for plain lists."
377 ((eq org-plain-list-ordered-item-terminator t
) "[.)]")
378 ((= org-plain-list-ordered-item-terminator ?\
)) ")")
379 ((= org-plain-list-ordered-item-terminator ?.
) "\\.")
381 (alpha (if org-alphabetical-lists
"\\|[A-Za-z]" "")))
382 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha
"\\)" term
383 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
385 (defsubst org-item-beginning-re
()
386 "Regexp matching the beginning of a plain list item."
387 (concat "^" (org-item-re)))
389 (defun org-list-at-regexp-after-bullet-p (regexp)
390 "Is point at a list item with REGEXP after bullet?"
393 (goto-char (match-end 0))
394 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
395 (if org-alphabetical-lists
396 "\\([0-9]+\\|[A-Za-z]\\)"
399 ;; Ignore counter if any
400 (when (looking-at counter-re
) (goto-char (match-end 0))))
401 (looking-at regexp
))))
403 (defun org-list-in-valid-context-p ()
404 "Is point in a context where lists are allowed?"
405 (not (org-in-block-p org-list-forbidden-blocks
)))
407 (defun org-in-item-p ()
408 "Return item beginning position when in a plain list, nil otherwise."
411 (let* ((case-fold-search t
)
412 (context (org-list-context))
413 (lim-up (car context
))
414 (drawers-re (concat "^[ \t]*:\\("
415 (mapconcat 'regexp-quote org-drawers
"\\|")
417 (inlinetask-re (and (featurep 'org-inlinetask
)
418 (org-inlinetask-outline-regexp)))
419 (item-re (org-item-re))
420 ;; Indentation isn't meaningful when point starts at an empty
421 ;; line or an inline task.
422 (ind-ref (if (or (looking-at "^[ \t]*$")
423 (and inlinetask-re
(looking-at inlinetask-re
)))
425 (org-get-indentation))))
427 ((eq (nth 2 context
) 'invalid
) nil
)
428 ((looking-at item-re
) (point))
430 ;; Detect if cursor in amidst `org-list-end-re'. First, count
431 ;; number HL of hard lines it takes, then call `org-in-regexp'
432 ;; to compute its boundaries END-BOUNDS. When point is
433 ;; in-between, move cursor before regexp beginning.
434 (let ((hl 0) (i -
1) end-bounds
)
436 (while (setq i
(string-match
437 "[\r\n]" org-list-end-re
(1+ i
)))
439 (setq end-bounds
(org-in-regexp org-list-end-re hl
)))
440 (>= (point) (car end-bounds
))
441 (< (point) (cdr end-bounds
)))
442 (goto-char (car end-bounds
))
444 ;; Look for an item, less indented that reference line.
447 (let ((ind (org-get-indentation)))
449 ;; This is exactly what we want.
450 ((and (looking-at item-re
) (< ind ind-ref
))
451 (throw 'exit
(point)))
452 ;; At upper bound of search or looking at the end of a
453 ;; previous list: search is over.
454 ((<= (point) lim-up
) (throw 'exit nil
))
455 ((looking-at org-list-end-re
) (throw 'exit nil
))
456 ;; Skip blocks, drawers, inline-tasks, blank lines
457 ((and (looking-at "^[ \t]*#\\+end_")
458 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
459 ((and (looking-at "^[ \t]*:END:")
460 (re-search-backward drawers-re lim-up t
))
462 ((and inlinetask-re
(looking-at inlinetask-re
))
463 (org-inlinetask-goto-beginning)
465 ((looking-at "^[ \t]*$") (forward-line -
1))
466 ;; Text at column 0 cannot belong to a list: stop.
467 ((zerop ind
) (throw 'exit nil
))
468 ;; Normal text less indented than reference line, take
469 ;; it as new reference.
473 (t (forward-line -
1)))))))))))
475 (defun org-at-item-p ()
476 "Is point in a line starting a hand-formatted item?"
479 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
481 (defun org-at-item-bullet-p ()
482 "Is point at the bullet of a plain list item?"
484 (not (member (char-after) '(?\ ?
\t)))
485 (< (point) (match-end 0))))
487 (defun org-at-item-timer-p ()
488 "Is point at a line starting a plain list item with a timer?"
489 (org-list-at-regexp-after-bullet-p
490 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
492 (defun org-at-item-description-p ()
493 "Is point at a description list item?"
494 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
496 (defun org-at-item-checkbox-p ()
497 "Is point at a line starting a plain-list item with a checklet?"
498 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
500 (defun org-at-item-counter-p ()
501 "Is point at a line starting a plain-list item with a counter?"
503 (looking-at org-list-full-item-re
)
508 ;;; Structures and helper functions
510 (defun org-list-context ()
511 "Determine context, and its boundaries, around point.
513 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
514 are boundaries and CONTEXT is a symbol among `drawer', `block',
515 `invalid', `inlinetask' and nil.
517 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
520 (org-with-limited-levels
522 (let ((case-fold-search t
) (pos (point)) beg end context-type
523 ;; Get positions of surrounding headings. This is the
525 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t
))
528 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
529 ;; Is point inside a drawer?
530 (let ((end-re "^[ \t]*:END:")
531 ;; Can't use org-drawers-regexp as this function might
532 ;; be called in buffers not in Org mode.
533 (beg-re (concat "^[ \t]*:\\("
534 (mapconcat 'regexp-quote org-drawers
"\\|")
536 (when (save-excursion
537 (and (not (looking-at beg-re
))
538 (not (looking-at end-re
))
539 (setq beg
(and (re-search-backward beg-re lim-up t
)
540 (1+ (point-at-eol))))
541 (setq end
(or (and (re-search-forward end-re lim-down t
)
542 (1- (match-beginning 0)))
545 (setq lim-up beg lim-down end context-type
'drawer
)))
546 ;; Is point strictly in a block, and of which type?
547 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type
)
548 (when (save-excursion
549 (and (not (looking-at block-re
))
550 (setq beg
(and (re-search-backward block-re lim-up t
)
551 (1+ (point-at-eol))))
552 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
553 (setq type
(downcase (match-string 1)))
555 (setq end
(or (and (re-search-forward block-re lim-down t
)
559 (equal (downcase (match-string 1)) "end")))
560 (setq lim-up beg lim-down end
561 context-type
(if (member type org-list-forbidden-blocks
)
563 ;; Is point in an inlinetask?
564 (when (and (featurep 'org-inlinetask
)
566 (let* ((beg-re (org-inlinetask-outline-regexp))
567 (end-re (concat beg-re
"END[ \t]*$")))
568 (and (not (looking-at "^\\*+"))
569 (setq beg
(and (re-search-backward beg-re lim-up t
)
570 (1+ (point-at-eol))))
571 (not (looking-at end-re
))
572 (setq end
(and (re-search-forward end-re lim-down t
)
573 (1- (match-beginning 0))))
575 (setq lim-up beg lim-down end context-type
'inlinetask
))
576 ;; Return context boundaries and type.
577 (list lim-up lim-down context-type
))))))
579 (defun org-list-struct ()
580 "Return structure of list at point.
582 A list structure is an alist where key is point at item, and
585 2. bullet with trailing whitespace,
586 3. bullet counter, if any,
588 5. description tag, if any,
589 6. position at item end.
591 Thus the following list, where numbers in parens are
596 5. [@5] sub-item 2 (34)
597 some other text belonging to first item (55)
599 + tag :: description (109)
602 will get the following structure:
604 \(\(1 0 \"- \" nil \"[X]\" nil 97\)
605 \(18 2 \"1. \" nil nil nil 34\)
606 \(34 2 \"5. \" \"5\" nil nil 55\)
607 \(97 0 \"- \" nil nil nil 131\)
608 \(109 2 \"+ \" nil nil \"tag\" 131\)
610 Assume point is at an item."
613 (let* ((case-fold-search t
)
614 (context (org-list-context))
615 (lim-up (car context
))
616 (lim-down (nth 1 context
))
618 (item-re (org-item-re))
619 (drawers-re (concat "^[ \t]*:\\("
620 (mapconcat 'regexp-quote org-drawers
"\\|")
622 (inlinetask-re (and (featurep 'org-inlinetask
)
623 (org-inlinetask-outline-regexp)))
624 (beg-cell (cons (point) (org-get-indentation)))
625 ind itm-lst itm-lst-2 end-lst end-lst-2 struct
628 ;; Return association at point.
630 (looking-at org-list-full-item-re
)
633 (match-string-no-properties 1) ; bullet
634 (match-string-no-properties 2) ; counter
635 (match-string-no-properties 3) ; checkbox
636 (match-string-no-properties 4))))) ; description tag
639 ;; Ensure list ends at the first blank line.
641 (skip-chars-backward " \r\t\n")
642 (min (1+ (point-at-eol)) lim-down
)))))
643 ;; 1. Read list from starting item to its beginning, and save
644 ;; top item position and indentation in BEG-CELL. Also store
645 ;; ending position of items in END-LST.
649 (let ((ind (+ (or (get-text-property (point) 'original-indentation
) 0)
650 (org-get-indentation))))
653 ;; At upward limit: if we ended at an item, store it,
654 ;; else dismiss useless data recorded above BEG-CELL.
658 (if (or (not (looking-at item-re
))
659 (get-text-property (point) 'org-example
))
660 (memq (assq (car beg-cell
) itm-lst
) itm-lst
)
661 (setq beg-cell
(cons (point) ind
))
662 (cons (funcall assoc-at-point ind
) itm-lst
)))))
663 ;; At a verbatim block, go before its beginning. Move
664 ;; from eol to ensure `previous-single-property-change'
665 ;; will return a value.
666 ((get-text-property (point) 'org-example
)
667 (goto-char (previous-single-property-change
668 (point-at-eol) 'org-example nil lim-up
))
670 ;; Looking at a list ending regexp. Dismiss useless
671 ;; data recorded above BEG-CELL. Jump to part 2.
672 ((looking-at org-list-end-re
)
675 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
676 ;; Point is at an item. Add data to ITM-LST. It may
677 ;; also end a previous item: save it in END-LST. If
678 ;; ind is less or equal than BEG-CELL and there is no
679 ;; end at this ind or lesser, this item becomes the new
681 ((looking-at item-re
)
682 (push (funcall assoc-at-point ind
) itm-lst
)
683 (push (cons ind
(point)) end-lst
)
684 (when (or (<= ind
(cdr beg-cell
)) (< ind text-min-ind
))
685 (setq beg-cell
(cons (point) ind
)))
687 ;; Skip blocks, drawers, inline tasks, blank lines.
688 ((and (looking-at "^[ \t]*#\\+end_")
689 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
690 ((and (looking-at "^[ \t]*:END:")
691 (re-search-backward drawers-re lim-up t
))
693 ((and inlinetask-re
(looking-at inlinetask-re
))
694 (org-inlinetask-goto-beginning)
696 ((looking-at "^[ \t]*$")
698 ;; From there, point is not at an item. Interpret
699 ;; line's indentation:
700 ;; - text at column 0 is necessarily out of any list.
701 ;; Dismiss data recorded above BEG-CELL. Jump to
703 ;; - any other case may be an ending position for an
704 ;; hypothetical item above. Store it and proceed.
708 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
710 (when (< ind text-min-ind
) (setq text-min-ind ind
))
711 (push (cons ind
(point)) end-lst
)
712 (forward-line -
1)))))))
713 ;; 2. Read list from starting point to its end, that is until we
714 ;; get out of context, or that a non-item line is less or
715 ;; equally indented than BEG-CELL's cdr. Also, store ending
716 ;; position of items in END-LST-2.
719 (let ((ind (+ (or (get-text-property (point) 'original-indentation
) 0)
720 (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 ;; At a verbatim block, move to its end. Point is at bol
729 ;; and 'org-example property is set by whole lines:
730 ;; `next-single-property-change' always return a value.
731 ((get-text-property (point) 'org-example
)
733 (next-single-property-change (point) 'org-example nil lim-down
)))
734 ;; Looking at a list ending regexp. Save point as an
735 ;; ending position and jump to part 3.
736 ((looking-at org-list-end-re
)
737 (throw 'exit
(push (cons 0 (point)) end-lst-2
)))
738 ((looking-at item-re
)
739 ;; Point is at an item. Add data to ITM-LST-2. It may
740 ;; also end a previous item, so save it in END-LST-2.
741 (push (funcall assoc-at-point ind
) itm-lst-2
)
742 (push (cons ind
(point)) end-lst-2
)
744 ;; Skip inline tasks and blank lines along the way
745 ((and inlinetask-re
(looking-at inlinetask-re
))
746 (org-inlinetask-goto-end))
747 ((looking-at "^[ \t]*$")
749 ;; Ind is lesser or equal than BEG-CELL's. The list is
750 ;; over: store point as an ending position and jump to
752 ((<= ind
(cdr beg-cell
))
754 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
755 ;; Else, if ind is lesser or equal than previous item's,
756 ;; this is an ending position: store it. In any case,
757 ;; skip block or drawer at point, and move to next line.
759 (when (<= ind
(nth 1 (car itm-lst-2
)))
760 (push (cons ind
(point)) end-lst-2
))
762 ((and (looking-at "^[ \t]*#\\+begin_")
763 (re-search-forward "^[ \t]*#\\+end_" lim-down t
)))
764 ((and (looking-at drawers-re
)
765 (re-search-forward "^[ \t]*:END:" lim-down t
))))
766 (forward-line 1))))))
767 (setq struct
(append itm-lst
(cdr (nreverse itm-lst-2
)))
768 end-lst
(append end-lst
(cdr (nreverse end-lst-2
))))
769 ;; 3. Associate each item to its end position.
770 (org-list-struct-assoc-end struct end-lst
)
774 (defun org-list-struct-assoc-end (struct end-list
)
775 "Associate proper ending point to items in STRUCT.
777 END-LIST is a pseudo-alist where car is indentation and cdr is
780 This function modifies STRUCT."
781 (let ((endings end-list
))
784 (let ((pos (car elt
))
786 ;; Remove end candidates behind current item.
787 (while (or (<= (cdar endings
) pos
))
789 ;; Add end position to item assoc.
790 (let ((old-end (nthcdr 6 elt
))
791 (new-end (assoc-default ind endings
'<=)))
793 (setcar old-end new-end
)
794 (setcdr elt
(append (cdr elt
) (list new-end
)))))))
797 (defun org-list-prevs-alist (struct)
798 "Return alist between item and previous item in STRUCT."
799 (let ((item-end-alist (mapcar (lambda (e) (cons (car e
) (nth 6 e
)))
802 (let ((prev (car (rassq (car e
) item-end-alist
))))
803 (cons (car e
) prev
)))
806 (defun org-list-parents-alist (struct)
807 "Return alist between item and parent in STRUCT."
808 (let* ((ind-to-ori (list (list (nth 1 (car struct
)))))
809 (top-item (org-list-get-top-point struct
))
810 (prev-pos (list top-item
)))
812 (mapcar (lambda (item)
813 (let ((pos (car item
))
815 (prev-ind (caar ind-to-ori
)))
819 ;; A sub-list is over. Find the associated
820 ;; origin in IND-TO-ORI. If it cannot be
821 ;; found (ill-formed list), set its parent as
822 ;; the first item less indented. If there is
823 ;; none, make it a top-level item.
825 (or (member (assq ind ind-to-ori
) ind-to-ori
)
829 (when (< (car e
) ind
)
830 (throw 'exit
(member e ind-to-ori
))))
833 (cons pos
(cdar ind-to-ori
)))
834 ;; A sub-list starts. Every item at IND will
835 ;; have previous item as its parent.
837 (let ((origin (nth 1 prev-pos
)))
838 (push (cons ind origin
) ind-to-ori
)
840 ;; Another item in the same sub-list: it shares
841 ;; the same parent as the previous item.
842 (t (cons pos
(cdar ind-to-ori
))))))
849 (defsubst org-list-get-nth
(n key struct
)
850 "Return the Nth value of KEY in STRUCT."
851 (nth n
(assq key struct
)))
853 (defun org-list-set-nth (n key struct new
)
854 "Set the Nth value of KEY in STRUCT to NEW.
855 \nThis function modifies STRUCT."
856 (setcar (nthcdr n
(assq key struct
)) new
))
858 (defsubst org-list-get-ind
(item struct
)
859 "Return indentation of ITEM in STRUCT."
860 (org-list-get-nth 1 item struct
))
862 (defun org-list-set-ind (item struct ind
)
863 "Set indentation of ITEM in STRUCT to IND.
864 \nThis function modifies STRUCT."
865 (org-list-set-nth 1 item struct ind
))
867 (defsubst org-list-get-bullet
(item struct
)
868 "Return bullet of ITEM in STRUCT."
869 (org-list-get-nth 2 item struct
))
871 (defun org-list-set-bullet (item struct bullet
)
872 "Set bullet of ITEM in STRUCT to BULLET.
873 \nThis function modifies STRUCT."
874 (org-list-set-nth 2 item struct bullet
))
876 (defsubst org-list-get-counter
(item struct
)
877 "Return counter of ITEM in STRUCT."
878 (org-list-get-nth 3 item struct
))
880 (defsubst org-list-get-checkbox
(item struct
)
881 "Return checkbox of ITEM in STRUCT or nil."
882 (org-list-get-nth 4 item struct
))
884 (defun org-list-set-checkbox (item struct checkbox
)
885 "Set checkbox of ITEM in STRUCT to CHECKBOX.
886 \nThis function modifies STRUCT."
887 (org-list-set-nth 4 item struct checkbox
))
889 (defsubst org-list-get-tag
(item struct
)
890 "Return end position of ITEM in STRUCT."
891 (org-list-get-nth 5 item struct
))
893 (defun org-list-get-item-end (item struct
)
894 "Return end position of ITEM in STRUCT."
895 (org-list-get-nth 6 item struct
))
897 (defun org-list-get-item-end-before-blank (item struct
)
898 "Return point at end of ITEM in STRUCT, before any blank line.
899 Point returned is at end of line."
901 (goto-char (org-list-get-item-end item struct
))
902 (skip-chars-backward " \r\t\n")
905 (defun org-list-get-parent (item struct parents
)
906 "Return parent of ITEM or nil.
907 STRUCT is the list structure. PARENTS is the alist of parents,
908 as returned by `org-list-parents-alist'."
909 (let ((parents (or parents
(org-list-parents-alist struct
))))
910 (cdr (assq item parents
))))
912 (defun org-list-has-child-p (item struct
)
913 "Non-nil if ITEM has a child.
915 STRUCT is the list structure.
917 Value returned is the position of the first child of ITEM."
918 (let ((ind (org-list-get-ind item struct
))
919 (child-maybe (car (nth 1 (member (assq item struct
) struct
)))))
920 (when (and child-maybe
921 (< ind
(org-list-get-ind child-maybe struct
)))
924 (defun org-list-get-next-item (item struct prevs
)
925 "Return next 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 (car (rassq item prevs
)))
930 (defun org-list-get-prev-item (item struct prevs
)
931 "Return previous item in same sub-list as ITEM, or nil.
932 STRUCT is the list structure. PREVS is the alist of previous
933 items, as returned by `org-list-prevs-alist'."
934 (cdr (assq item prevs
)))
936 (defun org-list-get-subtree (item struct
)
937 "List all items having ITEM as a common ancestor, or nil.
938 STRUCT is the list structure."
939 (let* ((item-end (org-list-get-item-end item struct
))
940 (sub-struct (cdr (member (assq item struct
) struct
)))
945 (if (< pos item-end
) (push pos subtree
) (throw 'exit nil
))))
949 (defun org-list-get-all-items (item struct prevs
)
950 "List all items in the same sub-list as ITEM.
951 STRUCT is the list structure. PREVS is the alist of previous
952 items, as returned by `org-list-prevs-alist'."
953 (let ((prev-item item
)
955 before-item after-item
)
956 (while (setq prev-item
(org-list-get-prev-item prev-item struct prevs
))
957 (push prev-item before-item
))
958 (while (setq next-item
(org-list-get-next-item next-item struct prevs
))
959 (push next-item after-item
))
960 (append before-item
(list item
) (nreverse after-item
))))
962 (defun org-list-get-children (item struct parents
)
963 "List all children of ITEM, or nil.
964 STRUCT is the list structure. PARENTS is the alist of parents,
965 as returned by `org-list-parents-alist'."
967 (while (setq child
(car (rassq item parents
)))
968 (setq parents
(cdr (member (assq child parents
) parents
)))
972 (defun org-list-get-top-point (struct)
973 "Return point at beginning of list.
974 STRUCT is the list structure."
977 (defun org-list-get-bottom-point (struct)
978 "Return point at bottom of list.
979 STRUCT is the list structure."
981 (mapcar (lambda (e) (org-list-get-item-end (car e
) struct
)) struct
)))
983 (defun org-list-get-list-begin (item struct prevs
)
984 "Return point at beginning of sub-list ITEM belongs.
985 STRUCT is the list structure. PREVS is the alist of previous
986 items, as returned by `org-list-prevs-alist'."
987 (let ((first-item item
) prev-item
)
988 (while (setq prev-item
(org-list-get-prev-item first-item struct prevs
))
989 (setq first-item prev-item
))
992 (defalias 'org-list-get-first-item
'org-list-get-list-begin
)
994 (defun org-list-get-last-item (item struct prevs
)
995 "Return point at last item of sub-list ITEM belongs.
996 STRUCT is the list structure. PREVS is the alist of previous
997 items, as returned by `org-list-prevs-alist'."
998 (let ((last-item item
) next-item
)
999 (while (setq next-item
(org-list-get-next-item last-item struct prevs
))
1000 (setq last-item next-item
))
1003 (defun org-list-get-list-end (item struct prevs
)
1004 "Return point at end of sub-list ITEM belongs.
1005 STRUCT is the list structure. PREVS is the alist of previous
1006 items, as returned by `org-list-prevs-alist'."
1007 (org-list-get-item-end (org-list-get-last-item item struct prevs
) struct
))
1009 (defun org-list-get-list-type (item struct prevs
)
1010 "Return the type of the list containing ITEM, as a symbol.
1012 STRUCT is the list structure. PREVS is the alist of previous
1013 items, as returned by `org-list-prevs-alist'.
1015 Possible types are `descriptive', `ordered' and `unordered'. The
1016 type is determined by the first item of the list."
1017 (let ((first (org-list-get-list-begin item struct prevs
)))
1019 ((org-list-get-tag first struct
) 'descriptive
)
1020 ((string-match "[[:alnum:]]" (org-list-get-bullet first struct
)) 'ordered
)
1023 (defun org-list-get-item-number (item struct prevs parents
)
1024 "Return ITEM's sequence number.
1026 STRUCT is the list structure. PREVS is the alist of previous
1027 items, as returned by `org-list-prevs-alist'. PARENTS is the
1028 alist of ancestors, as returned by `org-list-parents-alist'.
1030 Return value is a list of integers. Counters have an impact on
1032 (let ((get-relative-number
1034 (lambda (item struct prevs
)
1035 ;; Return relative sequence number of ITEM in the sub-list
1036 ;; it belongs. STRUCT is the list structure. PREVS is
1037 ;; the alist of previous items.
1038 (let ((seq 0) (pos item
) counter
)
1039 (while (and (not (setq counter
(org-list-get-counter pos struct
)))
1040 (setq pos
(org-list-get-prev-item pos struct prevs
)))
1042 (if (not counter
) (1+ seq
)
1044 ((string-match "[A-Za-z]" counter
)
1045 (+ (- (string-to-char (upcase (match-string 0 counter
))) 64)
1047 ((string-match "[0-9]+" counter
)
1048 (+ (string-to-number (match-string 0 counter
)) seq
))
1050 ;; Cons each parent relative number into return value (OUT).
1051 (let ((out (list (funcall get-relative-number item struct prevs
)))
1053 (while (setq parent
(org-list-get-parent parent struct parents
))
1054 (push (funcall get-relative-number parent struct prevs
) out
))
1062 (defun org-list-search-generic (search re bound noerr
)
1063 "Search a string in valid contexts for lists.
1064 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1065 in `re-search-forward'."
1067 (let ((origin (point)))
1069 ;; 1. No match: return to origin or bound, depending on NOERR.
1070 (unless (funcall search re bound noerr
)
1071 (throw 'exit
(and (goto-char (if (memq noerr
'(t nil
)) origin bound
))
1073 ;; 2. Match in valid context: return point. Else, continue
1075 (when (org-list-in-valid-context-p) (throw 'exit
(point)))))))
1077 (defun org-list-search-backward (regexp &optional bound noerror
)
1078 "Like `re-search-backward' but stop only where lists are recognized.
1079 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1080 `re-search-backward'."
1081 (org-list-search-generic #'re-search-backward
1082 regexp
(or bound
(point-min)) noerror
))
1084 (defun org-list-search-forward (regexp &optional bound noerror
)
1085 "Like `re-search-forward' but stop only where lists are recognized.
1086 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1087 `re-search-forward'."
1088 (org-list-search-generic #'re-search-forward
1089 regexp
(or bound
(point-max)) noerror
))
1093 ;;; Methods on structures
1095 (defsubst org-list-bullet-string
(bullet)
1096 "Return BULLET with the correct number of whitespaces.
1097 It determines the number of whitespaces to append by looking at
1098 `org-list-two-spaces-after-bullet-regexp'."
1100 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1102 org-list-two-spaces-after-bullet-regexp bullet
))
1105 (string-match "\\S-+\\([ \t]*\\)" bullet
)
1106 (replace-match spaces nil nil bullet
1))))
1108 (defun org-list-swap-items (beg-A beg-B struct
)
1109 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1110 Blank lines at the end of items are left in place. Return the
1111 new structure after the changes.
1113 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1114 to the same sub-list.
1116 This function modifies STRUCT."
1118 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct
))
1119 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct
))
1120 (end-A (org-list-get-item-end beg-A struct
))
1121 (end-B (org-list-get-item-end beg-B struct
))
1122 (size-A (- end-A-no-blank beg-A
))
1123 (size-B (- end-B-no-blank beg-B
))
1124 (body-A (buffer-substring beg-A end-A-no-blank
))
1125 (body-B (buffer-substring beg-B end-B-no-blank
))
1126 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B
))
1127 (sub-A (cons beg-A
(org-list-get-subtree beg-A struct
)))
1128 (sub-B (cons beg-B
(org-list-get-subtree beg-B struct
))))
1129 ;; 1. Move effectively items in buffer.
1131 (delete-region beg-A end-B-no-blank
)
1132 (insert (concat body-B between-A-no-blank-and-B body-A
))
1133 ;; 2. Now modify struct. No need to re-read the list, the
1134 ;; transformation is just a shift of positions. Some special
1135 ;; attention is required for items ending at END-A and END-B
1136 ;; as empty spaces are not moved there. In others words,
1137 ;; item BEG-A will end with whitespaces that were at the end
1138 ;; of BEG-B and the same applies to BEG-B.
1140 (let ((pos (car e
)))
1144 (let ((end-e (nth 6 e
)))
1145 (setcar e
(+ pos
(- end-B-no-blank end-A-no-blank
)))
1146 (setcar (nthcdr 6 e
)
1147 (+ end-e
(- end-B-no-blank end-A-no-blank
)))
1148 (when (= end-e end-A
) (setcar (nthcdr 6 e
) end-B
))))
1150 (let ((end-e (nth 6 e
)))
1151 (setcar e
(- (+ pos beg-A
) beg-B
))
1152 (setcar (nthcdr 6 e
) (+ end-e
(- beg-A beg-B
)))
1153 (when (= end-e end-B
)
1154 (setcar (nthcdr 6 e
)
1155 (+ beg-A size-B
(- end-A end-A-no-blank
))))))
1157 (let ((end-e (nth 6 e
)))
1158 (setcar e
(+ pos
(- size-B size-A
)))
1159 (setcar (nthcdr 6 e
) (+ end-e
(- size-B size-A
))))))))
1161 (sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))))
1163 (defun org-list-separating-blank-lines-number (pos struct prevs
)
1164 "Return number of blank lines that should separate items in list.
1166 POS is the position of point where `org-list-insert-item' was called.
1168 STRUCT is the list structure. PREVS is the alist of previous
1169 items, as returned by `org-list-prevs-alist'.
1171 Assume point is at item's beginning. If the item is alone, apply
1172 some heuristics to guess the result."
1174 (let ((item (point))
1176 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
1181 ;; Count blank lines above beginning of line.
1183 (count-lines (goto-char (point-at-bol))
1184 (progn (skip-chars-backward " \r\t\n")
1188 ;; Trivial cases where there should be none.
1189 ((or org-empty-line-terminates-plain-lists
(not insert-blank-p
)) 0)
1190 ;; When `org-blank-before-new-entry' says so, it is 1.
1191 ((eq insert-blank-p t
) 1)
1192 ;; `plain-list-item' is 'auto. Count blank lines separating
1193 ;; neighbours items in list.
1194 (t (let ((next-p (org-list-get-next-item item struct prevs
)))
1196 ;; Is there a next item?
1197 (next-p (goto-char next-p
)
1198 (funcall count-blanks
))
1199 ;; Is there a previous item?
1200 ((org-list-get-prev-item item struct prevs
)
1201 (funcall count-blanks
))
1202 ;; User inserted blank lines, trust him.
1203 ((and (> pos
(org-list-get-item-end-before-blank item struct
))
1204 (> (save-excursion (goto-char pos
)
1205 (setq usr-blank
(funcall count-blanks
)))
1208 ;; Are there blank lines inside the list so far?
1210 (goto-char (org-list-get-top-point struct
))
1211 (org-list-search-forward
1212 "^[ \t]*$" (org-list-get-item-end-before-blank item struct
) t
))
1214 ;; Default choice: no blank line.
1217 (defun org-list-insert-item (pos struct prevs
&optional checkbox after-bullet
)
1218 "Insert a new list item at POS and return the new structure.
1219 If POS is before first character after bullet of the item, the
1220 new item will be created before the current one.
1222 STRUCT is the list structure. PREVS is the the alist of previous
1223 items, as returned by `org-list-prevs-alist'.
1225 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1226 after the bullet. Cursor will be after this text once the
1229 This function modifies STRUCT."
1230 (let ((case-fold-search t
))
1231 ;; 1. Get information about list: position of point with regards
1232 ;; to item start (BEFOREP), blank lines number separating items
1233 ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
1234 (let* ((item (progn (goto-char pos
) (goto-char (org-list-get-item-begin))))
1235 (item-end (org-list-get-item-end item struct
))
1236 (item-end-no-blank (org-list-get-item-end-before-blank item struct
))
1237 (beforep (and (looking-at org-list-full-item-re
)
1238 (<= pos
(match-end 0))))
1239 (split-line-p (org-get-alist-option org-M-RET-may-split-line
'item
))
1240 (blank-nb (org-list-separating-blank-lines-number
1242 ;; 2. Build the new item to be created. Concatenate same
1243 ;; bullet as item, checkbox, text AFTER-BULLET if
1244 ;; provided, and text cut from point to end of item
1245 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1246 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1247 ;; between what was cut and what was inserted in buffer
1248 ;; is stored in SIZE-OFFSET.
1249 (ind (org-list-get-ind item struct
))
1250 (ind-size (if indent-tabs-mode
1251 (+ (/ ind tab-width
) (mod ind tab-width
))
1253 (bullet (org-list-bullet-string (org-list-get-bullet item struct
)))
1254 (box (when checkbox
"[ ]"))
1256 (and (not beforep
) split-line-p
1259 ;; If POS is greater than ITEM-END, then point is
1260 ;; in some white lines after the end of the list.
1261 ;; Those must be removed, or they will be left,
1262 ;; stacking up after the list.
1263 (when (< item-end pos
)
1264 (delete-region (1- item-end
) (point-at-eol)))
1265 (skip-chars-backward " \r\t\n")
1267 (delete-and-extract-region pos item-end-no-blank
))))
1268 (body (concat bullet
(when box
(concat box
" ")) after-bullet
1270 (if (string-match "\\`[ \t]+" text-cut
)
1271 (replace-match "" t t text-cut
)
1273 (item-sep (make-string (1+ blank-nb
) ?
\n))
1274 (item-size (+ ind-size
(length body
) (length item-sep
)))
1275 (size-offset (- item-size
(length text-cut
))))
1276 ;; 4. Insert effectively item into buffer.
1278 (org-indent-to-column ind
)
1279 (insert body item-sep
)
1280 ;; 5. Add new item to STRUCT.
1282 (let ((p (car e
)) (end (nth 6 e
)))
1284 ;; Before inserted item, positions don't change but
1285 ;; an item ending after insertion has its end shifted
1288 (when (> end item
) (setcar (nthcdr 6 e
) (+ end size-offset
))))
1289 ;; Trivial cases where current item isn't split in
1290 ;; two. Just shift every item after new one by
1292 ((or beforep
(not split-line-p
))
1293 (setcar e
(+ p item-size
))
1294 (setcar (nthcdr 6 e
) (+ end item-size
)))
1295 ;; Item is split in two: elements before POS are just
1296 ;; shifted by ITEM-SIZE. In the case item would end
1297 ;; after split POS, ending is only shifted by
1300 (setcar e
(+ p item-size
))
1302 (setcar (nthcdr 6 e
) (+ end item-size
))
1303 (setcar (nthcdr 6 e
) (+ end size-offset
))))
1304 ;; Elements after POS are moved into new item.
1305 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1306 ;; doesn't appear in buffer yet.
1308 (setcar e
(+ p size-offset
(- item pos
(length item-sep
))))
1309 (if (= end item-end
)
1310 (setcar (nthcdr 6 e
) (+ item item-size
))
1311 (setcar (nthcdr 6 e
)
1313 (- item pos
(length item-sep
))))))
1314 ;; Elements at ITEM-END or after are only shifted by
1316 (t (setcar e
(+ p size-offset
))
1317 (setcar (nthcdr 6 e
) (+ end size-offset
))))))
1319 (push (list item ind bullet nil box nil
(+ item item-size
)) struct
)
1320 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1321 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1322 ;; exchange ITEM with the next item in list. Position cursor
1323 ;; after bullet, counter, checkbox, and label.
1326 (setq struct
(org-list-swap-items item
(+ item item-size
) struct
))
1327 (goto-char (org-list-get-next-item
1328 item struct
(org-list-prevs-alist struct
))))
1331 (defun org-list-delete-item (item struct
)
1332 "Remove ITEM from the list and return the new structure.
1334 STRUCT is the list structure."
1335 (let* ((end (org-list-get-item-end item struct
))
1336 (beg (if (= (org-list-get-bottom-point struct
) end
)
1337 ;; If ITEM ends with the list, delete blank lines
1341 (skip-chars-backward " \r\t\n")
1342 (min (1+ (point-at-eol)) (point-max)))
1344 ;; Remove item from buffer.
1345 (delete-region beg end
)
1346 ;; Remove item from structure and shift others items accordingly.
1347 ;; Don't forget to shift also ending position when appropriate.
1348 (let ((size (- end beg
)))
1349 (delq nil
(mapcar (lambda (e)
1350 (let ((pos (car e
)))
1353 (let ((end-e (nth 6 e
)))
1357 (append (butlast e
) (list beg
)))
1359 (append (butlast e
) (list (- end-e size
)))))))
1363 (append (butlast (cdr e
))
1364 (list (- (nth 6 e
) size
))))))))
1367 (defun org-list-send-item (item dest struct
)
1368 "Send ITEM to destination DEST.
1370 STRUCT is the list structure.
1372 DEST can have various values.
1374 If DEST is a buffer position, the function will assume it points
1375 to another item in the same list as ITEM, and will move the
1376 latter just before the former.
1378 If DEST is `begin' \(respectively `end'\), ITEM will be moved at
1379 the beginning \(respectively end\) of the list it belongs to.
1381 If DEST is a string like \"N\", where N is an integer, ITEM will
1382 be moved at the Nth position in the list.
1384 If DEST is `kill', ITEM will be deleted and its body will be
1385 added to the kill-ring.
1387 If DEST is `delete', ITEM will be deleted.
1389 This function returns, destructively, the new list structure."
1390 (let* ((prevs (org-list-prevs-alist struct
))
1391 (item-end (org-list-get-item-end item struct
))
1392 ;; Grab full item body minus its bullet.
1399 (regexp-quote (org-list-get-bullet item struct
))))
1402 ;; Change DEST into a buffer position. A trick is needed
1403 ;; when ITEM is meant to be sent at the end of the list.
1404 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1405 ;; nil and insertion point (INS-POINT) to the first line's
1406 ;; end of the last item, we ensure the new item will be
1407 ;; inserted after the last item, and not after any of its
1408 ;; hypothetical sub-items.
1410 ((or (eq dest
'kill
) (eq dest
'delete
)))
1412 (setq dest
(org-list-get-list-begin item struct prevs
)))
1414 (setq dest
(org-list-get-list-end item struct prevs
))
1416 (goto-char (org-list-get-last-item item struct prevs
))
1418 ((string-match "\\`[0-9]+\\'" dest
)
1419 (let* ((all (org-list-get-all-items item struct prevs
))
1421 (index (mod (string-to-number dest
) len
)))
1422 (if (not (zerop index
))
1423 (setq dest
(nth (1- index
) all
))
1424 ;; Send ITEM at the end of the list.
1425 (setq dest
(org-list-get-list-end item struct prevs
))
1428 (org-list-get-last-item item struct prevs
))
1431 (org-M-RET-may-split-line nil
))
1433 ((eq dest
'delete
) (org-list-delete-item item struct
))
1436 (org-list-delete-item item struct
))
1437 ((and (integerp dest
) (/= item ins-point
))
1438 (setq item
(copy-marker item
))
1439 (setq struct
(org-list-insert-item ins-point struct prevs nil body
))
1440 ;; 1. Structure returned by `org-list-insert-item' may not be
1441 ;; accurate, as it cannot see sub-items included in BODY.
1442 ;; Thus, first compute the real structure so far.
1444 (cons (marker-position item
)
1445 (org-list-get-subtree (marker-position item
) struct
)))
1446 (new-end (org-list-get-item-end (point) struct
))
1447 (old-end (org-list-get-item-end (marker-position item
) struct
))
1449 (shift (- (point) item
)))
1450 ;; 1.1. Remove the item just created in structure.
1451 (setq struct
(delete (assq new-item struct
) struct
))
1452 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1457 (let* ((cell (assq e struct
))
1461 (append (butlast (cdr cell
))
1462 (list (if (= end old-end
)
1466 (lambda (e1 e2
) (< (car e1
) (car e2
))))))
1467 ;; 2. Eventually delete extra copy of the item and clean marker.
1469 (org-list-delete-item (marker-position item
) struct
)
1470 (move-marker item nil
)))
1473 (defun org-list-struct-outdent (start end struct parents
)
1474 "Outdent items between positions START and END.
1476 STRUCT is the list structure. PARENTS is the alist of items'
1477 parents, as returned by `org-list-parents-alist'.
1479 START is included, END excluded."
1482 (let* ((item (car cell
))
1483 (parent (cdr cell
)))
1485 ;; Item not yet in zone: keep association.
1486 ((< item start
) cell
)
1487 ;; Item out of zone: follow associations in ACC.
1489 (let ((convert (and parent
(assq parent acc
))))
1490 (if convert
(cons item
(cdr convert
)) cell
)))
1491 ;; Item has no parent: error
1493 (error "Cannot outdent top-level items"))
1494 ;; Parent is outdented: keep association.
1496 (push (cons parent item
) acc
) cell
)
1498 ;; Parent isn't outdented: reparent to grand-parent.
1499 (let ((grand-parent (org-list-get-parent
1500 parent struct parents
)))
1501 (push (cons parent item
) acc
)
1502 (cons item grand-parent
))))))))
1503 (mapcar out parents
)))
1505 (defun org-list-struct-indent (start end struct parents prevs
)
1506 "Indent items between positions START and END.
1508 STRUCT is the list structure. PARENTS is the alist of parents
1509 and PREVS is the alist of previous items, returned by,
1510 respectively, `org-list-parents-alist' and
1511 `org-list-prevs-alist'.
1513 START is included and END excluded.
1515 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1516 bullets between START and END."
1518 (set-assoc (lambda (cell) (push cell acc
) cell
))
1519 (change-bullet-maybe
1522 (let* ((bul (org-trim (org-list-get-bullet item struct
)))
1523 (new-bul-p (cdr (assoc bul org-list-demote-modify-bullet
))))
1524 (when new-bul-p
(org-list-set-bullet item struct new-bul-p
))))))
1527 (let* ((item (car cell
))
1528 (parent (cdr cell
)))
1530 ;; Item not yet in zone: keep association.
1531 ((< item start
) cell
)
1533 ;; Item out of zone: follow associations in ACC.
1534 (let ((convert (assq parent acc
)))
1535 (if convert
(cons item
(cdr convert
)) cell
)))
1537 ;; Item is in zone...
1538 (let ((prev (org-list-get-prev-item item struct prevs
)))
1539 ;; Check if bullet needs to be changed.
1540 (funcall change-bullet-maybe item
)
1542 ;; First item indented but not parent: error
1543 ((and (not prev
) (< parent start
))
1544 (error "Cannot indent the first item of a list"))
1545 ;; First item and parent indented: keep same
1547 ((not prev
) (funcall set-assoc cell
))
1548 ;; Previous item not indented: reparent to it.
1549 ((< prev start
) (funcall set-assoc
(cons item prev
)))
1550 ;; Previous item indented: reparent like it.
1553 (cons item
(cdr (assq prev acc
)))))))))))))
1554 (mapcar ind parents
)))
1558 ;;; Repairing structures
1560 (defun org-list-use-alpha-bul-p (first struct prevs
)
1561 "Non-nil if list starting at FIRST can have alphabetical bullets.
1563 STRUCT is list structure. PREVS is the alist of previous items,
1564 as returned by `org-list-prevs-alist'."
1565 (and org-alphabetical-lists
1567 (let ((item first
) (ascii 64) (case-fold-search nil
))
1568 ;; Pretend that bullets are uppercase and check if alphabet
1569 ;; is sufficient, taking counters into account.
1571 (let ((bul (org-list-get-bullet item struct
))
1572 (count (org-list-get-counter item struct
)))
1573 ;; Virtually determine current bullet
1574 (if (and count
(string-match "[a-zA-Z]" count
))
1575 ;; Counters are not case-sensitive.
1576 (setq ascii
(string-to-char (upcase count
)))
1577 (setq ascii
(1+ ascii
)))
1578 ;; Test if bullet would be over z or Z.
1581 (setq item
(org-list-get-next-item item struct prevs
)))))
1582 ;; All items checked. All good.
1585 (defun org-list-inc-bullet-maybe (bullet)
1586 "Increment BULLET if applicable."
1587 (let ((case-fold-search nil
))
1589 ;; Num bullet: increment it.
1590 ((string-match "[0-9]+" bullet
)
1592 (number-to-string (1+ (string-to-number (match-string 0 bullet
))))
1594 ;; Alpha bullet: increment it.
1595 ((string-match "[A-Za-z]" bullet
)
1597 (char-to-string (1+ (string-to-char (match-string 0 bullet
))))
1599 ;; Unordered bullet: leave it.
1602 (defun org-list-struct-fix-bul (struct prevs
)
1603 "Verify and correct bullets in STRUCT.
1604 PREVS is the alist of previous items, as returned by
1605 `org-list-prevs-alist'.
1607 This function modifies STRUCT."
1608 (let ((case-fold-search nil
)
1611 ;; Set bullet of ITEM in STRUCT, depending on the type of
1612 ;; first item of the list, the previous bullet and counter
1615 (let* ((prev (org-list-get-prev-item item struct prevs
))
1616 (prev-bul (and prev
(org-list-get-bullet prev struct
)))
1617 (counter (org-list-get-counter item struct
))
1618 (bullet (org-list-get-bullet item struct
))
1619 (alphap (and (not prev
)
1620 (org-list-use-alpha-bul-p item struct prevs
))))
1621 (org-list-set-bullet
1623 (org-list-bullet-string
1625 ;; Alpha counter in alpha list: use counter.
1627 (string-match "[a-zA-Z]" counter
)
1628 (string-match "[a-zA-Z]" prev-bul
))
1629 ;; Use cond to be sure `string-match' is used in
1633 ((string-match "[a-z]" prev-bul
) (downcase counter
))
1634 ((string-match "[A-Z]" prev-bul
) (upcase counter
)))))
1635 (replace-match real-count nil nil prev-bul
)))
1636 ;; Num counter in a num list: use counter.
1638 (string-match "[0-9]+" counter
)
1639 (string-match "[0-9]+" prev-bul
))
1640 (replace-match counter nil nil prev-bul
))
1641 ;; No counter: increase, if needed, previous bullet.
1643 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct
)))
1644 ;; Alpha counter at first item: use counter.
1645 ((and counter
(org-list-use-alpha-bul-p item struct prevs
)
1646 (string-match "[A-Za-z]" counter
)
1647 (string-match "[A-Za-z]" bullet
))
1650 ((string-match "[a-z]" bullet
) (downcase counter
))
1651 ((string-match "[A-Z]" bullet
) (upcase counter
)))))
1652 (replace-match real-count nil nil bullet
)))
1653 ;; Num counter at first item: use counter.
1655 (string-match "[0-9]+" counter
)
1656 (string-match "[0-9]+" bullet
))
1657 (replace-match counter nil nil bullet
))
1658 ;; First bullet is alpha uppercase: use "A".
1659 ((and alphap
(string-match "[A-Z]" bullet
))
1660 (replace-match "A" nil nil bullet
))
1661 ;; First bullet is alpha lowercase: use "a".
1662 ((and alphap
(string-match "[a-z]" bullet
))
1663 (replace-match "a" nil nil bullet
))
1664 ;; First bullet is num: use "1".
1665 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet
)
1666 (replace-match "1" nil nil bullet
))
1667 ;; Not an ordered list: keep bullet.
1669 (mapc fix-bul
(mapcar 'car struct
))))
1671 (defun org-list-struct-fix-ind (struct parents
&optional bullet-size
)
1672 "Verify and correct indentation in STRUCT.
1674 PARENTS is the alist of parents, as returned by
1675 `org-list-parents-alist'.
1677 If numeric optional argument BULLET-SIZE is set, assume all
1678 bullets in list have this length to determine new indentation.
1680 This function modifies STRUCT."
1681 (let* ((ancestor (org-list-get-top-point struct
))
1682 (top-ind (org-list-get-ind ancestor struct
))
1685 (let ((parent (org-list-get-parent item struct parents
)))
1687 ;; Indent like parent + length of parent's bullet +
1690 item struct
(+ (or bullet-size
1692 (org-list-get-bullet parent struct
)))
1693 (org-list-get-ind parent struct
)
1694 org-list-indent-offset
))
1695 ;; If no parent, indent like top-point.
1696 (org-list-set-ind item struct top-ind
))))))
1697 (mapc new-ind
(mapcar 'car
(cdr struct
)))))
1699 (defun org-list-struct-fix-box (struct parents prevs
&optional ordered
)
1700 "Verify and correct checkboxes in STRUCT.
1702 PARENTS is the alist of parents and PREVS is the alist of
1703 previous items, as returned by, respectively,
1704 `org-list-parents-alist' and `org-list-prevs-alist'.
1706 If ORDERED is non-nil, a checkbox can only be checked when every
1707 checkbox before it is checked too. If there was an attempt to
1708 break this rule, the function will return the blocking item. In
1709 all others cases, the return value will be nil.
1711 This function modifies STRUCT."
1712 (let ((all-items (mapcar 'car struct
))
1717 (mapcar (lambda (child)
1718 (org-list-get-checkbox child struct
))
1719 (org-list-get-children item struct parents
))))
1720 (org-list-set-checkbox
1723 ((and (member "[ ]" box-list
) (member "[X]" box-list
)) "[-]")
1724 ((member "[-]" box-list
) "[-]")
1725 ((member "[X]" box-list
) "[X]")
1726 ((member "[ ]" box-list
) "[ ]")
1727 ;; Parent has no boxed child: leave box as-is.
1728 (t (org-list-get-checkbox item struct
))))))))
1730 ;; 1. List all parents with a checkbox.
1733 (let* ((parent (org-list-get-parent e struct parents
))
1734 (parent-box-p (org-list-get-checkbox parent struct
)))
1735 (when (and parent-box-p
(not (memq parent parent-list
)))
1736 (push parent parent-list
))))
1738 ;; 2. Sort those parents by decreasing indentation.
1739 (setq parent-list
(sort parent-list
1741 (> (org-list-get-ind e1 struct
)
1742 (org-list-get-ind e2 struct
)))))
1743 ;; 3. For each parent, get all children's checkboxes to determine
1744 ;; and set its checkbox accordingly.
1745 (mapc set-parent-box parent-list
)
1746 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1749 (mapcar (lambda (e) (org-list-get-checkbox e struct
)) all-items
))
1750 (after-unchecked (member "[ ]" box-list
)))
1751 ;; There are boxes checked after an unchecked one: fix that.
1752 (when (member "[X]" after-unchecked
)
1753 (let ((index (- (length struct
) (length after-unchecked
))))
1754 (mapc (lambda (e) (org-list-set-checkbox e struct
"[ ]"))
1755 (nthcdr index all-items
))
1756 ;; Verify once again the structure, without ORDERED.
1757 (org-list-struct-fix-box struct parents prevs nil
)
1758 ;; Return blocking item.
1759 (nth index all-items
)))))))
1761 (defun org-list-struct-fix-item-end (struct)
1762 "Verify and correct each item end position in STRUCT.
1764 This function modifies STRUCT."
1765 (let (end-list acc-end
)
1767 (let* ((pos (car e
))
1768 (ind-pos (org-list-get-ind pos struct
))
1769 (end-pos (org-list-get-item-end pos struct
)))
1770 (unless (assq end-pos struct
)
1771 ;; To determine real ind of an ending position that is
1772 ;; not at an item, we have to find the item it belongs
1773 ;; to: it is the last item (ITEM-UP), whose ending is
1774 ;; further than the position we're interested in.
1775 (let ((item-up (assoc-default end-pos acc-end
'>)))
1777 ;; Else part is for the bottom point.
1778 (if item-up
(+ (org-list-get-ind item-up struct
) 2) 0)
1781 (push (cons ind-pos pos
) end-list
)
1782 (push (cons end-pos pos
) acc-end
)))
1784 (setq end-list
(sort end-list
(lambda (e1 e2
) (< (cdr e1
) (cdr e2
)))))
1785 (org-list-struct-assoc-end struct end-list
)))
1787 (defun org-list-struct-apply-struct (struct old-struct
)
1788 "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
1790 OLD-STRUCT is the structure before any modifications, and STRUCT
1791 the structure to be applied. The function will only modify parts
1792 of the list which have changed.
1794 Initial position of cursor is restored after the changes."
1795 (let* ((origin (point-marker))
1796 (inlinetask-re (and (featurep 'org-inlinetask
)
1797 (org-inlinetask-outline-regexp)))
1798 (item-re (org-item-re))
1799 (box-rule-p (cdr (assq 'checkbox org-list-automatic-rules
)))
1802 ;; Shift the indentation between END and BEG by DELTA.
1803 ;; Start from the line before END.
1804 (lambda (end beg delta
)
1806 (skip-chars-backward " \r\t\n")
1808 (while (or (> (point) beg
)
1809 (and (= (point) beg
)
1810 (not (looking-at item-re
))))
1812 ;; Skip inline tasks.
1813 ((and inlinetask-re
(looking-at inlinetask-re
))
1814 (org-inlinetask-goto-beginning))
1815 ;; Shift only non-empty lines.
1816 ((org-looking-at-p "^[ \t]*\\S-")
1817 (let ((i (org-get-indentation)))
1818 (org-indent-line-to (+ i delta
)))))
1819 (forward-line -
1)))))
1822 ;; Replace ITEM first line elements with new elements from
1823 ;; STRUCT, if appropriate.
1826 (let* ((new-ind (org-list-get-ind item struct
))
1827 (old-ind (org-get-indentation))
1828 (new-bul (org-list-bullet-string
1829 (org-list-get-bullet item struct
)))
1830 (old-bul (org-list-get-bullet item old-struct
))
1831 (new-box (org-list-get-checkbox item struct
)))
1832 (looking-at org-list-full-item-re
)
1833 ;; a. Replace bullet
1834 (unless (equal old-bul new-bul
)
1835 (replace-match new-bul nil nil nil
1))
1836 ;; b. Replace checkbox.
1838 ((and new-box box-rule-p
1839 (save-match-data (org-at-item-description-p)))
1840 (message "Cannot add a checkbox to a description list item"))
1841 ((equal (match-string 3) new-box
))
1842 ((and (match-string 3) new-box
)
1843 (replace-match new-box nil nil nil
3))
1845 (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
1846 (replace-match "" nil nil nil
1))
1847 (t (let ((counterp (match-end 2)))
1848 (goto-char (if counterp
(1+ counterp
) (match-end 1)))
1849 (insert (concat new-box
(unless counterp
" "))))))
1850 ;; c. Indent item to appropriate column.
1851 (unless (= new-ind old-ind
)
1852 (delete-region (goto-char (point-at-bol))
1853 (progn (skip-chars-forward " \t") (point)))
1854 (indent-to new-ind
)))))))
1855 ;; 1. First get list of items and position endings. We maintain
1856 ;; two alists: ITM-SHIFT, determining indentation shift needed
1857 ;; at item, and END-POS, a pseudo-alist where key is ending
1858 ;; position and value point.
1859 (let (end-list acc-end itm-shift all-ends sliced-struct
)
1861 (let* ((pos (car e
))
1862 (ind-pos (org-list-get-ind pos struct
))
1863 (ind-old (org-list-get-ind pos old-struct
))
1864 (bul-pos (org-list-get-bullet pos struct
))
1865 (bul-old (org-list-get-bullet pos old-struct
))
1866 (ind-shift (- (+ ind-pos
(length bul-pos
))
1867 (+ ind-old
(length bul-old
))))
1868 (end-pos (org-list-get-item-end pos old-struct
)))
1869 (push (cons pos ind-shift
) itm-shift
)
1870 (unless (assq end-pos old-struct
)
1871 ;; To determine real ind of an ending position that
1872 ;; is not at an item, we have to find the item it
1873 ;; belongs to: it is the last item (ITEM-UP), whose
1874 ;; ending is further than the position we're
1876 (let ((item-up (assoc-default end-pos acc-end
'>)))
1877 (push (cons end-pos item-up
) end-list
)))
1878 (push (cons end-pos pos
) acc-end
)))
1880 ;; 2. Slice the items into parts that should be shifted by the
1881 ;; same amount of indentation. The slices are returned in
1882 ;; reverse order so changes modifying buffer do not change
1883 ;; positions they refer to.
1884 (setq all-ends
(sort (append (mapcar 'car itm-shift
)
1885 (org-uniquify (mapcar 'car end-list
)))
1887 (while (cdr all-ends
)
1888 (let* ((up (pop all-ends
))
1889 (down (car all-ends
))
1890 (ind (if (assq up struct
)
1891 (cdr (assq up itm-shift
))
1892 (cdr (assq (cdr (assq up end-list
)) itm-shift
)))))
1893 (push (list down up ind
) sliced-struct
)))
1894 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1895 ;; end to beginning. Take a special action when beginning is
1898 (unless (zerop (nth 2 e
)) (apply shift-body-ind e
))
1899 (let* ((beg (nth 1 e
))
1900 (cell (assq beg struct
)))
1901 (unless (or (not cell
) (equal cell
(assq beg old-struct
)))
1902 (funcall modify-item beg
))))
1904 ;; 4. Go back to initial position and clean marker.
1906 (move-marker origin nil
)))
1908 (defun org-list-write-struct (struct parents
&optional old-struct
)
1909 "Correct bullets, checkboxes and indentation in list at point.
1911 STRUCT is the list structure. PARENTS is the alist of parents,
1912 as returned by `org-list-parents-alist'.
1914 When non-nil, optional argument OLD-STRUCT is the reference
1915 structure of the list. It should be provided whenever STRUCT
1916 doesn't correspond anymore to the real list in buffer."
1917 ;; Order of functions matters here: checkboxes and endings need
1918 ;; correct indentation to be set, and indentation needs correct
1921 ;; 0. Save a copy of structure before modifications
1922 (let ((old-struct (or old-struct
(copy-tree struct
))))
1923 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
1924 ;; order to get items endings and bullets properly
1925 (org-list-struct-fix-ind struct parents
2)
1926 ;; 2. Fix each item end to get correct prevs alist.
1927 (org-list-struct-fix-item-end struct
)
1928 ;; 3. Get bullets right.
1929 (let ((prevs (org-list-prevs-alist struct
)))
1930 (org-list-struct-fix-bul struct prevs
)
1931 ;; 4. Now get real indentation.
1932 (org-list-struct-fix-ind struct parents
)
1933 ;; 5. Eventually fix checkboxes.
1934 (org-list-struct-fix-box struct parents prevs
))
1935 ;; 6. Apply structure modifications to buffer.
1936 (org-list-struct-apply-struct struct old-struct
)))
1942 (defun org-apply-on-list (function init-value
&rest args
)
1943 "Call FUNCTION on each item of the list at point.
1944 FUNCTION must be called with at least one argument: INIT-VALUE,
1945 that will contain the value returned by the function at the
1946 previous item, plus ARGS extra arguments.
1948 FUNCTION is applied on items in reverse order.
1950 As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
1951 will return the number of items in the current list.
1953 Sublists of the list are skipped. Cursor is always at the
1954 beginning of the item."
1955 (let* ((struct (org-list-struct))
1956 (prevs (org-list-prevs-alist struct
))
1957 (item (copy-marker (point-at-bol)))
1958 (all (org-list-get-all-items (marker-position item
) struct prevs
))
1962 (setq value
(apply function value args
)))
1965 (move-marker item nil
)
1968 (defun org-list-set-item-visibility (item struct view
)
1969 "Set visibility of ITEM in STRUCT to VIEW.
1971 Possible values are: `folded', `children' or `subtree'. See
1972 `org-cycle' for more information."
1975 (let ((item-end (org-list-get-item-end-before-blank item struct
)))
1977 (outline-flag-region (save-excursion (goto-char item
) (point-at-eol))
1979 ((eq view
'children
)
1980 ;; First show everything.
1981 (org-list-set-item-visibility item struct
'subtree
)
1982 ;; Then fold every child.
1983 (let* ((parents (org-list-parents-alist struct
))
1984 (children (org-list-get-children item struct parents
)))
1986 (org-list-set-item-visibility e struct
'folded
))
1990 (let ((item-end (org-list-get-item-end item struct
)))
1991 (outline-flag-region item item-end nil
)))))
1993 (defun org-list-item-body-column (item)
1994 "Return column at which body of ITEM should start."
1995 (let (bpos bcol tpos tcol
)
1998 (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?[ \t]+")
1999 (setq bpos
(match-beginning 1) tpos
(match-end 0)
2000 bcol
(progn (goto-char bpos
) (current-column))
2001 tcol
(progn (goto-char tpos
) (current-column)))
2002 (when (> tcol
(+ bcol org-description-max-indent
))
2003 (setq tcol
(+ bcol
5))))
2008 ;;; Interactive functions
2010 (defalias 'org-list-get-item-begin
'org-in-item-p
)
2012 (defun org-beginning-of-item ()
2013 "Go to the beginning of the current item.
2014 Throw an error when not in a list."
2016 (let ((begin (org-in-item-p)))
2017 (if begin
(goto-char begin
) (error "Not in an item"))))
2019 (defun org-beginning-of-item-list ()
2020 "Go to the beginning item of the current list or sublist.
2021 Throw an error when not in a list."
2023 (let ((begin (org-in-item-p)))
2025 (error "Not in an item")
2027 (let* ((struct (org-list-struct))
2028 (prevs (org-list-prevs-alist struct
)))
2029 (goto-char (org-list-get-list-begin begin struct prevs
))))))
2031 (defun org-end-of-item-list ()
2032 "Go to the end of the current list or sublist.
2033 Throw an error when not in a list."
2035 (let ((begin (org-in-item-p)))
2037 (error "Not in an item")
2039 (let* ((struct (org-list-struct))
2040 (prevs (org-list-prevs-alist struct
)))
2041 (goto-char (org-list-get-list-end begin struct prevs
))))))
2043 (defun org-end-of-item ()
2044 "Go to the end of the current item.
2045 Throw an error when not in a list."
2047 (let ((begin (org-in-item-p)))
2049 (error "Not in an item")
2051 (let ((struct (org-list-struct)))
2052 (goto-char (org-list-get-item-end begin struct
))))))
2054 (defun org-previous-item ()
2055 "Move to the beginning of the previous item.
2056 Throw an error when not in a list. Also throw an error when at
2057 first item, unless `org-list-use-circular-motion' is non-nil."
2059 (let ((item (org-in-item-p)))
2061 (error "Not in an item")
2063 (let* ((struct (org-list-struct))
2064 (prevs (org-list-prevs-alist struct
))
2065 (prevp (org-list-get-prev-item item struct prevs
)))
2067 (prevp (goto-char prevp
))
2068 (org-list-use-circular-motion
2069 (goto-char (org-list-get-last-item item struct prevs
)))
2070 (t (error "On first item")))))))
2072 (defun org-next-item ()
2073 "Move to the beginning of the next item.
2074 Throw an error when not in a list. Also throw an error when at
2075 last item, unless `org-list-use-circular-motion' is non-nil."
2077 (let ((item (org-in-item-p)))
2079 (error "Not in an item")
2081 (let* ((struct (org-list-struct))
2082 (prevs (org-list-prevs-alist struct
))
2083 (prevp (org-list-get-next-item item struct prevs
)))
2085 (prevp (goto-char prevp
))
2086 (org-list-use-circular-motion
2087 (goto-char (org-list-get-first-item item struct prevs
)))
2088 (t (error "On last item")))))))
2090 (defun org-move-item-down ()
2091 "Move the item at point down, i.e. swap with following item.
2092 Sub-items (items with larger indentation) are considered part of
2093 the item, so this really moves item trees."
2095 (unless (org-at-item-p) (error "Not at an item"))
2096 (let* ((col (current-column))
2097 (item (point-at-bol))
2098 (struct (org-list-struct))
2099 (prevs (org-list-prevs-alist struct
))
2100 (next-item (org-list-get-next-item (point-at-bol) struct prevs
)))
2101 (unless (or next-item org-list-use-circular-motion
)
2102 (error "Cannot move this item further down"))
2104 (setq struct
(org-list-send-item item
'begin struct
))
2105 (setq struct
(org-list-swap-items item next-item struct
))
2107 (org-list-get-next-item item struct
(org-list-prevs-alist struct
))))
2108 (org-list-write-struct struct
(org-list-parents-alist struct
))
2109 (org-move-to-column col
)))
2111 (defun org-move-item-up ()
2112 "Move the item at point up, i.e. swap with previous item.
2113 Sub-items (items with larger indentation) are considered part of
2114 the item, so this really moves item trees."
2116 (unless (org-at-item-p) (error "Not at an item"))
2117 (let* ((col (current-column))
2118 (item (point-at-bol))
2119 (struct (org-list-struct))
2120 (prevs (org-list-prevs-alist struct
))
2121 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs
)))
2122 (unless (or prev-item org-list-use-circular-motion
)
2123 (error "Cannot move this item further up"))
2125 (setq struct
(org-list-send-item item
'end struct
))
2126 (setq struct
(org-list-swap-items prev-item item struct
)))
2127 (org-list-write-struct struct
(org-list-parents-alist struct
))
2128 (org-move-to-column col
)))
2130 (defun org-insert-item (&optional checkbox
)
2131 "Insert a new item at the current level.
2132 If cursor is before first character after bullet of the item, the
2133 new item will be created before the current one.
2135 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2137 Return t when things worked, nil when we are not in an item, or
2139 (let ((itemp (org-in-item-p))
2141 ;; If cursor isn't is a list or if list is invisible, return nil.
2142 (unless (or (not itemp
)
2145 (outline-invisible-p)))
2148 (org-at-item-timer-p))
2149 ;; Timer list: delegate to `org-timer-item'.
2150 (progn (org-timer-item) t
)
2151 (let* ((struct (save-excursion (goto-char itemp
)
2153 (prevs (org-list-prevs-alist struct
))
2154 ;; If we're in a description list, ask for the new term.
2155 (desc (when (org-list-get-tag itemp struct
)
2156 (concat (read-string "Term: ") " :: ")))
2157 ;; Don't insert a checkbox if checkbox rule is applied
2158 ;; and it is a description item.
2159 (checkp (and checkbox
2161 (not (cdr (assq 'checkbox
2162 org-list-automatic-rules
)))))))
2164 (org-list-insert-item pos struct prevs checkp desc
))
2165 (org-list-write-struct struct
(org-list-parents-alist struct
))
2166 (when checkp
(org-update-checkbox-count-maybe))
2167 (looking-at org-list-full-item-re
)
2168 (goto-char (match-end 0))
2171 (defun org-mark-list ()
2172 "Mark the current list.
2173 If this is a sublist, only mark the sublist."
2175 (let* ((item (org-list-get-item-begin))
2176 (struct (org-list-struct))
2177 (prevs (org-list-prevs-alist struct
))
2178 (lbeg (org-list-get-list-begin item struct prevs
))
2179 (lend (org-list-get-list-end item struct prevs
)))
2180 (push-mark lend nil t
)
2183 (defun org-list-repair ()
2184 "Fix indentation, bullets and checkboxes is the list at point."
2186 (unless (org-at-item-p) (error "This is not a list"))
2187 (let* ((struct (org-list-struct))
2188 (parents (org-list-parents-alist struct
)))
2189 (org-list-write-struct struct parents
)))
2191 (defun org-cycle-list-bullet (&optional which
)
2192 "Cycle through the different itemize/enumerate bullets.
2193 This cycle the entire list level through the sequence:
2195 `-' -> `+' -> `*' -> `1.' -> `1)'
2197 If WHICH is a valid string, use that as the new bullet. If WHICH
2198 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2199 `previous', cycle backwards."
2201 (unless (org-at-item-p) (error "Not at an item"))
2204 (let* ((struct (org-list-struct))
2205 (parents (org-list-parents-alist struct
))
2206 (prevs (org-list-prevs-alist struct
))
2207 (list-beg (org-list-get-first-item (point) struct prevs
))
2208 (bullet (org-list-get-bullet list-beg struct
))
2209 (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules
)))
2210 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs
))
2211 (case-fold-search nil
)
2213 ((string-match "[a-z]\\." bullet
) "a.")
2214 ((string-match "[a-z])" bullet
) "a)")
2215 ((string-match "[A-Z]\\." bullet
) "A.")
2216 ((string-match "[A-Z])" bullet
) "A)")
2217 ((string-match "\\." bullet
) "1.")
2218 ((string-match ")" bullet
) "1)")
2219 (t (org-trim bullet
))))
2220 ;; Compute list of possible bullets, depending on context.
2223 ;; *-bullets are not allowed at column 0.
2224 (unless (and bullet-rule-p
2225 (looking-at "\\S-")) '("*"))
2226 ;; Description items cannot be numbered.
2227 (unless (or (eq org-plain-list-ordered-item-terminator ?\
))
2228 (and bullet-rule-p
(org-at-item-description-p)))
2230 (unless (or (eq org-plain-list-ordered-item-terminator ?.
)
2231 (and bullet-rule-p
(org-at-item-description-p)))
2233 (unless (or (not alpha-p
)
2234 (eq org-plain-list-ordered-item-terminator ?\
))
2235 (and bullet-rule-p
(org-at-item-description-p)))
2237 (unless (or (not alpha-p
)
2238 (eq org-plain-list-ordered-item-terminator ?.
)
2239 (and bullet-rule-p
(org-at-item-description-p)))
2241 (len (length bullet-list
))
2242 (item-index (- len
(length (member current bullet-list
))))
2243 (get-value (lambda (index) (nth (mod index len
) bullet-list
)))
2245 ((member which bullet-list
) which
)
2246 ((numberp which
) (funcall get-value which
))
2247 ((eq 'previous which
) (funcall get-value
(1- item-index
)))
2248 (t (funcall get-value
(1+ item-index
))))))
2249 ;; Use a short variation of `org-list-write-struct' as there's
2250 ;; no need to go through all the steps.
2251 (let ((old-struct (copy-tree struct
)))
2252 (org-list-set-bullet list-beg struct
(org-list-bullet-string new
))
2253 (org-list-struct-fix-bul struct prevs
)
2254 (org-list-struct-fix-ind struct parents
)
2255 (org-list-struct-apply-struct struct old-struct
)))))
2257 (defun org-toggle-checkbox (&optional toggle-presence
)
2258 "Toggle the checkbox in the current line.
2259 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2260 double prefix, set checkbox to [-].
2262 When there is an active region, toggle status or presence of the
2263 first checkbox there, and make every item inside have the same
2264 status or presence, respectively.
2266 If the cursor is in a headline, apply this to all checkbox items
2267 in the text below the heading, taking as reference the first item
2268 in subtree, ignoring drawers."
2275 (drawer-re (concat "^[ \t]*:\\("
2276 (mapconcat 'regexp-quote org-drawers
"\\|")
2278 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2279 "\\|" org-deadline-string
2280 "\\|" org-closed-string
2281 "\\|" org-clock-string
"\\)"
2282 " *[[<]\\([^]>]+\\)[]>]"))
2283 (orderedp (org-entry-get nil
"ORDERED"))
2285 ;; In a region, start at first item in region.
2287 ((org-region-active-p)
2288 (let ((limit (region-end)))
2289 (goto-char (region-beginning))
2290 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2291 (setq lim-up
(point-at-bol))
2292 (error "No item in region"))
2293 (setq lim-down
(copy-marker limit
))))
2295 ;; On an heading, start at first item after drawers and
2296 ;; time-stamps (scheduled, etc.).
2297 (let ((limit (save-excursion (outline-next-heading) (point))))
2299 (while (or (looking-at drawer-re
) (looking-at keyword-re
))
2300 (if (looking-at keyword-re
)
2302 (re-search-forward "^[ \t]*:END:" limit nil
)))
2303 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2304 (setq lim-up
(point-at-bol))
2305 (error "No item in subtree"))
2306 (setq lim-down
(copy-marker limit
))))
2307 ;; Just one item: set SINGLEP flag.
2310 (setq lim-up
(point-at-bol)
2311 lim-down
(copy-marker (point-at-eol))))
2312 (t (error "Not at an item or heading, and no active region"))))
2313 ;; Determine the checkbox going to be applied to all items
2318 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2320 ((equal toggle-presence
'(16)) "[-]")
2321 ((equal toggle-presence
'(4))
2322 (unless cbox
"[ ]"))
2323 ((equal "[X]" cbox
) "[ ]")
2325 ;; When an item is found within bounds, grab the full list at
2326 ;; point structure, then: (1) set check-box of all its items
2327 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2328 ;; whole list, (3) move point after the list.
2330 (while (and (< (point) lim-down
)
2331 (org-list-search-forward (org-item-beginning-re)
2333 (let* ((struct (org-list-struct))
2334 (struct-copy (copy-tree struct
))
2335 (parents (org-list-parents-alist struct
))
2336 (prevs (org-list-prevs-alist struct
))
2337 (bottom (copy-marker (org-list-get-bottom-point struct
)))
2338 (items-to-toggle (org-remove-if
2339 (lambda (e) (or (< e lim-up
) (> e lim-down
)))
2340 (mapcar 'car struct
))))
2341 (mapc (lambda (e) (org-list-set-checkbox
2343 ;; If there is no box at item, leave as-is
2344 ;; unless function was called with C-u prefix.
2345 (let ((cur-box (org-list-get-checkbox e struct
)))
2346 (if (or cur-box
(equal toggle-presence
'(4)))
2350 (setq block-item
(org-list-struct-fix-box
2351 struct parents prevs orderedp
))
2352 ;; Report some problems due to ORDERED status of subtree.
2353 ;; If only one box was being checked, throw an error, else,
2354 ;; only signal problems.
2356 ((and singlep block-item
(> lim-up block-item
))
2358 "Checkbox blocked because of unchecked box at line %d"
2359 (org-current-line block-item
)))
2362 "Checkboxes were removed due to unchecked box at line %d"
2363 (org-current-line block-item
))))
2365 (move-marker bottom nil
)
2366 (org-list-struct-apply-struct struct struct-copy
)))
2367 (move-marker lim-down nil
)))
2368 (org-update-checkbox-count-maybe))
2370 (defun org-reset-checkbox-state-subtree ()
2371 "Reset all checkboxes in an entry subtree."
2373 (if (org-before-first-heading-p)
2374 (error "Not inside a tree")
2377 (org-narrow-to-subtree)
2379 (goto-char (point-min))
2380 (let ((end (point-max)))
2381 (while (< (point) end
)
2382 (when (org-at-item-checkbox-p)
2383 (replace-match "[ ]" t t nil
1))
2384 (beginning-of-line 2)))
2385 (org-update-checkbox-count-maybe 'all
)))))
2387 (defun org-update-checkbox-count (&optional all
)
2388 "Update the checkbox statistics in the current section.
2389 This will find all statistic cookies like [57%] and [6/12] and
2390 update them with the current numbers.
2392 With optional prefix argument ALL, do this for the whole buffer."
2395 (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2396 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2398 (or (not org-hierarchical-checkbox-statistics
)
2399 (string-match "\\<recursive\\>"
2400 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2402 (cons (point-min) (point-max))
2403 (cons (or (ignore-errors (org-back-to-heading t
) (point))
2405 (save-excursion (outline-next-heading) (point)))))
2408 ;; Return number of checked boxes and boxes of all types
2409 ;; in all structures in STRUCTS. If RECURSIVEP is
2410 ;; non-nil, also count boxes in sub-lists. If ITEM is
2411 ;; nil, count across the whole structure, else count only
2412 ;; across subtree whose ancestor is ITEM.
2413 (lambda (item structs recursivep
)
2414 (let ((c-on 0) (c-all 0))
2417 (let* ((pre (org-list-prevs-alist s
))
2418 (par (org-list-parents-alist s
))
2421 ((and recursivep item
) (org-list-get-subtree item s
))
2422 (recursivep (mapcar 'car s
))
2423 (item (org-list-get-children item s par
))
2424 (t (org-list-get-all-items
2425 (org-list-get-top-point s
) s pre
))))
2426 (cookies (delq nil
(mapcar
2428 (org-list-get-checkbox e s
))
2430 (setq c-all
(+ (length cookies
) c-all
)
2431 c-on
(+ (org-count "[X]" cookies
) c-on
))))
2433 (cons c-on c-all
)))))
2435 cookies-list structs-bak box-num
)
2436 (goto-char (car bounds
))
2437 ;; 1. Build an alist for each cookie found within BOUNDS. The
2438 ;; key will be position at beginning of cookie and values
2439 ;; ending position, format of cookie, and a cell whose car is
2440 ;; number of checked boxes to report, and cdr total number of
2442 (while (re-search-forward cookie-re
(cdr bounds
) t
)
2447 (match-beginning 1) ; cookie start
2448 (match-end 1) ; cookie end
2449 (match-string 2) ; percent?
2451 ;; Cookie is at an heading, but specifically for todo,
2452 ;; not for checkboxes: skip it.
2453 ((and (org-at-heading-p)
2454 (string-match "\\<todo\\>"
2456 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2458 ;; Cookie is at an heading, but all lists before next
2459 ;; heading already have been read. Use data collected
2460 ;; in STRUCTS-BAK. This should only happen when
2461 ;; heading has more than one cookie on it.
2462 ((and (org-at-heading-p)
2463 (<= (save-excursion (outline-next-heading) (point))
2465 (funcall count-boxes nil structs-bak recursivep
))
2466 ;; Cookie is at a fresh heading. Grab structure of
2467 ;; every list containing a checkbox between point and
2468 ;; next headline, and save them in STRUCTS-BAK.
2470 (setq backup-end
(save-excursion
2471 (outline-next-heading) (point))
2473 (while (org-list-search-forward box-re backup-end
'move
)
2474 (let* ((struct (org-list-struct))
2475 (bottom (org-list-get-bottom-point struct
)))
2476 (push struct structs-bak
)
2477 (goto-char bottom
)))
2478 (funcall count-boxes nil structs-bak recursivep
))
2479 ;; Cookie is at an item, and we already have list
2480 ;; structure stored in STRUCTS-BAK.
2481 ((and (org-at-item-p)
2482 (< (point-at-bol) backup-end
)
2483 ;; Only lists in no special context are stored.
2484 (not (nth 2 (org-list-context))))
2485 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2486 ;; Cookie is at an item, but we need to compute list
2489 (let ((struct (org-list-struct)))
2490 (setq backup-end
(org-list-get-bottom-point struct
)
2491 structs-bak
(list struct
)))
2492 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2493 ;; Else, cookie found is at a wrong place. Skip it.
2494 (t (throw 'skip nil
))))
2496 ;; 2. Apply alist to buffer, in reverse order so positions stay
2497 ;; unchanged after cookie modifications.
2498 (mapc (lambda (cookie)
2499 (let* ((beg (car cookie
))
2500 (end (nth 1 cookie
))
2501 (percentp (nth 2 cookie
))
2502 (checked (car (nth 3 cookie
)))
2503 (total (cdr (nth 3 cookie
)))
2505 (format "[%d%%]" (/ (* 100 checked
)
2507 (format "[%d/%d]" checked total
))))
2510 (delete-region (point) (+ (point) (- end beg
)))
2511 (when org-auto-align-tags
(org-fix-tags-on-the-fly))))
2514 (defun org-get-checkbox-statistics-face ()
2515 "Select the face for checkbox statistics.
2516 The face will be `org-done' when all relevant boxes are checked.
2517 Otherwise it will be `org-todo'."
2519 (if (equal (match-string 1) "100%")
2520 'org-checkbox-statistics-done
2521 'org-checkbox-statistics-todo
)
2522 (if (and (> (match-end 2) (match-beginning 2))
2523 (equal (match-string 2) (match-string 3)))
2524 'org-checkbox-statistics-done
2525 'org-checkbox-statistics-todo
)))
2527 (defun org-update-checkbox-count-maybe (&optional all
)
2528 "Update checkbox statistics unless turned off by user.
2529 With an optional argument ALL, update them in the whole buffer."
2530 (when (cdr (assq 'checkbox org-list-automatic-rules
))
2531 (org-update-checkbox-count all
))
2532 (run-hooks 'org-checkbox-statistics-hook
))
2534 (defvar org-last-indent-begin-marker
(make-marker))
2535 (defvar org-last-indent-end-marker
(make-marker))
2536 (defun org-list-indent-item-generic (arg no-subtree struct
)
2537 "Indent a local list item including its children.
2538 When number ARG is a negative, item will be outdented, otherwise
2539 it will be indented.
2541 If a region is active, all items inside will be moved.
2543 If NO-SUBTREE is non-nil, only indent the item itself, not its
2546 STRUCT is the list structure.
2548 Return t if successful."
2551 (let* ((regionp (org-region-active-p))
2552 (rbeg (and regionp
(region-beginning)))
2553 (rend (and regionp
(region-end)))
2554 (top (org-list-get-top-point struct
))
2555 (parents (org-list-parents-alist struct
))
2556 (prevs (org-list-prevs-alist struct
))
2557 ;; Are we going to move the whole list?
2559 (and (= top
(point))
2560 (cdr (assq 'indent org-list-automatic-rules
))
2563 "First item of list cannot move without its subtree")
2565 ;; Determine begin and end points of zone to indent. If moving
2566 ;; more than one item, save them for subsequent moves.
2567 (unless (and (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
2568 (memq this-command
'(org-shiftmetaright org-shiftmetaleft
)))
2571 (set-marker org-last-indent-begin-marker rbeg
)
2572 (set-marker org-last-indent-end-marker rend
))
2573 (set-marker org-last-indent-begin-marker
(point))
2574 (set-marker org-last-indent-end-marker
2576 (specialp (org-list-get-bottom-point struct
))
2577 (no-subtree (1+ (point)))
2578 (t (org-list-get-item-end (point) struct
))))))
2579 (let* ((beg (marker-position org-last-indent-begin-marker
))
2580 (end (marker-position org-last-indent-end-marker
)))
2582 ;; Special case: moving top-item with indent rule.
2584 (let* ((level-skip (org-level-increment))
2585 (offset (if (< arg
0) (- level-skip
) level-skip
))
2586 (top-ind (org-list-get-ind beg struct
))
2587 (old-struct (copy-tree struct
)))
2588 (if (< (+ top-ind offset
) 0)
2589 (error "Cannot outdent beyond margin")
2590 ;; Change bullet if necessary.
2591 (when (and (= (+ top-ind offset
) 0)
2593 (org-list-get-bullet beg struct
)))
2594 (org-list-set-bullet beg struct
2595 (org-list-bullet-string "-")))
2596 ;; Shift every item by OFFSET and fix bullets. Then
2597 ;; apply changes to buffer.
2599 (let ((ind (org-list-get-ind (car e
) struct
)))
2600 (org-list-set-ind (car e
) struct
(+ ind offset
))))
2602 (org-list-struct-fix-bul struct prevs
)
2603 (org-list-struct-apply-struct struct old-struct
))))
2606 ;; If only one item is moved, it mustn't have a child.
2609 (org-list-has-child-p beg struct
))
2610 ;; If a subtree or region is moved, the last item
2611 ;; of the subtree mustn't have a child.
2612 (let ((last-item (caar
2615 (lambda (e) (>= (car e
) end
))
2617 (org-list-has-child-p last-item struct
))))
2618 (error "Cannot outdent an item without its children"))
2623 (org-list-struct-outdent beg end struct parents
)
2624 (org-list-struct-indent beg end struct parents prevs
))))
2625 (org-list-write-struct struct new-parents
))
2626 (org-update-checkbox-count-maybe))))))
2629 (defun org-outdent-item ()
2630 "Outdent a local list item, but not its children.
2631 If a region is active, all items inside will be moved."
2634 (let ((struct (org-list-struct)))
2635 (org-list-indent-item-generic -
1 t struct
))
2636 (error "Not at an item")))
2638 (defun org-indent-item ()
2639 "Indent a local list item, but not its children.
2640 If a region is active, all items inside will be moved."
2643 (let ((struct (org-list-struct)))
2644 (org-list-indent-item-generic 1 t struct
))
2645 (error "Not at an item")))
2647 (defun org-outdent-item-tree ()
2648 "Outdent a local list item including its children.
2649 If a region is active, all items inside will be moved."
2651 (let ((regionp (org-region-active-p)))
2653 ((or (org-at-item-p)
2654 (and (org-region-active-p)
2655 (goto-char (region-beginning))
2657 (let ((struct (org-list-struct)))
2658 (org-list-indent-item-generic -
1 nil struct
)))
2659 (regionp (error "Region not starting at an item"))
2660 (t (error "Not at an item")))))
2662 (defun org-indent-item-tree ()
2663 "Indent a local list item including its children.
2664 If a region is active, all items inside will be moved."
2666 (let ((regionp (org-region-active-p)))
2668 ((or (org-at-item-p)
2669 (and (org-region-active-p)
2670 (goto-char (region-beginning))
2672 (let ((struct (org-list-struct)))
2673 (org-list-indent-item-generic 1 nil struct
)))
2674 (regionp (error "Region not starting at an item"))
2675 (t (error "Not at an item")))))
2677 (defvar org-tab-ind-state
)
2678 (defun org-cycle-item-indentation ()
2679 "Cycle levels of indentation of an empty item.
2680 The first run indents the item, if applicable. Subsequent runs
2681 outdent it at meaningful levels in the list. When done, item is
2682 put back at its original position with its original bullet.
2684 Return t at each successful move."
2685 (when (org-at-item-p)
2686 (let* ((org-adapt-indentation nil
)
2687 (struct (org-list-struct))
2688 (ind (org-list-get-ind (point-at-bol) struct
))
2689 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2690 ;; Accept empty items or if cycle has already started.
2691 (when (or (eq last-command
'org-cycle-item-indentation
)
2692 (and (save-excursion
2694 (looking-at org-list-full-item-re
))
2695 (>= (match-end 0) (save-excursion
2696 (goto-char (org-list-get-item-end
2697 (point-at-bol) struct
))
2698 (skip-chars-backward " \r\t\n")
2700 (setq this-command
'org-cycle-item-indentation
)
2701 ;; When in the middle of the cycle, try to outdent first. If
2702 ;; it fails, and point is still at initial position, indent.
2703 ;; Else, re-create it at its original position.
2704 (if (eq last-command
'org-cycle-item-indentation
)
2706 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2707 ((and (= ind
(car org-tab-ind-state
))
2708 (ignore-errors (org-list-indent-item-generic 1 t struct
))))
2709 (t (delete-region (point-at-bol) (point-at-eol))
2710 (org-indent-to-column (car org-tab-ind-state
))
2711 (insert (cdr org-tab-ind-state
) " ")
2713 (setq this-command
'identity
)))
2714 ;; If a cycle is starting, remember indentation and bullet,
2715 ;; then try to indent. If it fails, try to outdent.
2716 (setq org-tab-ind-state
(cons ind bullet
))
2718 ((ignore-errors (org-list-indent-item-generic 1 t struct
)))
2719 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2720 (t (error "Cannot move item"))))
2723 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func
)
2725 The cursor may be at any item of the list that should be sorted.
2726 Sublists are not sorted. Checkboxes, if any, are ignored.
2728 Sorting can be alphabetically, numerically, by date/time as given
2729 by a time stamp, by a property or by priority.
2731 Comparing entries ignores case by default. However, with an
2732 optional argument WITH-CASE, the sorting considers case as well.
2734 The command prompts for the sorting type unless it has been given
2735 to the function through the SORTING-TYPE argument, which needs to
2736 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
2737 meaning of each character:
2739 n Numerically, by converting the beginning of the item to a number.
2740 a Alphabetically. Only the first line of item is checked.
2741 t By date/time, either the first active time stamp in the entry, if
2742 any, or by the first inactive one. In a timer list, sort the timers.
2744 Capital letters will reverse the sort order.
2746 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2747 a function to be called with point at the beginning of the
2748 record. It must return either a string or a number that should
2749 serve as the sorting key for that record. It will then use
2750 COMPARE-FUNC to compare entries."
2752 (let* ((case-func (if with-case
'identity
'downcase
))
2753 (struct (org-list-struct))
2754 (prevs (org-list-prevs-alist struct
))
2755 (start (org-list-get-list-begin (point-at-bol) struct prevs
))
2756 (end (org-list-get-list-end (point-at-bol) struct prevs
))
2760 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
2761 (read-char-exclusive)))
2762 (getkey-func (and (= (downcase sorting-type
) ?f
)
2763 (intern (org-icompleting-read "Sort using function: "
2764 obarray
'fboundp t nil nil
)))))
2765 (message "Sorting items...")
2767 (narrow-to-region start end
)
2768 (goto-char (point-min))
2769 (let* ((dcst (downcase sorting-type
))
2770 (case-fold-search nil
)
2771 (now (current-time))
2773 ((= dcst ?a
) 'string
<)
2774 ((= dcst ?f
) compare-func
)
2777 (next-record (lambda ()
2778 (skip-chars-forward " \r\t\n")
2779 (beginning-of-line)))
2780 (end-record (lambda ()
2781 (goto-char (org-list-get-item-end-before-blank
2785 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2788 (string-to-number (buffer-substring (match-end 0)
2792 (buffer-substring (match-end 0) (point-at-eol))))
2795 ;; If it is a timer list, convert timer to seconds
2796 ((org-at-item-timer-p)
2797 (org-timer-hms-to-secs (match-string 1)))
2798 ((or (re-search-forward org-ts-regexp
(point-at-eol) t
)
2799 (re-search-forward org-ts-regexp-both
2801 (org-time-string-to-seconds (match-string 0)))
2802 (t (org-float-time now
))))
2805 (let ((value (funcall getkey-func
)))
2807 (funcall case-func value
)
2809 (error "Invalid key function `%s'" getkey-func
)))
2810 (t (error "Invalid sorting type `%c'" sorting-type
)))))))
2811 (sort-subr (/= dcst sorting-type
)
2817 ;; Read and fix list again, as `sort-subr' probably destroyed
2820 (run-hooks 'org-after-sorting-entries-or-items-hook
)
2821 (message "Sorting items...done")))))
2825 ;;; Send and receive lists
2827 (defun org-list-parse-list (&optional delete
)
2828 "Parse the list at point and maybe DELETE it.
2830 Return a list whose car is a symbol of list type, among
2831 `ordered', `unordered' and `descriptive'. Then, each item is
2832 a list whose car is counter, and cdr are strings and other
2833 sub-lists. Inside strings, check-boxes are replaced by
2834 \"[CBON]\", \"[CBOFF]\" and \"[CBTRANS]\".
2836 For example, the following list:
2841 more text in first item
2847 \(nil \"first item\"
2849 \(nil \"sub-item one\"\)
2850 \(nil \"[CBON] sub-item two\"\)\)
2851 \"more text in first item\"\)
2852 \(3 \"last item\"\)\)
2854 Point is left at list end."
2855 (let* ((struct (org-list-struct))
2856 (prevs (org-list-prevs-alist struct
))
2857 (parents (org-list-parents-alist struct
))
2858 (top (org-list-get-top-point struct
))
2859 (bottom (org-list-get-bottom-point struct
))
2861 parse-item
; for byte-compiler
2864 ;; Return text between BEG and END, trimmed, with
2865 ;; checkboxes replaced.
2867 (let ((text (org-trim (buffer-substring beg end
))))
2868 (if (string-match "\\`\\[\\([-X ]\\)\\]" text
)
2870 (let ((box (match-string 1 text
)))
2872 ((equal box
" ") "CBOFF")
2873 ((equal box
"-") "CBTRANS")
2879 ;; Return a list whose car is list type and cdr a list of
2882 (cons (org-list-get-list-type (car e
) struct prevs
)
2883 (mapcar parse-item e
)))))
2886 ;; Return a list containing counter of item, if any, text
2887 ;; and any sublist inside it.
2889 (let ((start (save-excursion
2891 (looking-at "[ \t]*\\S-+\\([ \t]+\\[@\\(start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\]\\)?[ \t]*")
2893 ;; Get counter number. For alphabetic counter, get
2894 ;; its position in the alphabet.
2895 (counter (let ((c (org-list-get-counter e struct
)))
2898 ((string-match "[A-Za-z]" c
)
2899 (- (string-to-char (upcase (match-string 0 c
)))
2901 ((string-match "[0-9]+" c
)
2902 (string-to-number (match-string 0 c
))))))
2903 (childp (org-list-has-child-p e struct
))
2904 (end (org-list-get-item-end e struct
)))
2905 ;; If item has a child, store text between bullet and
2906 ;; next child, then recursively parse all sublists. At
2907 ;; the end of each sublist, check for the presence of
2908 ;; text belonging to the original item.
2910 (let* ((children (org-list-get-children e struct parents
))
2911 (body (list (funcall get-text start childp
))))
2913 (let* ((first (car children
))
2914 (sub (org-list-get-all-items first struct prevs
))
2915 (last-c (car (last sub
)))
2916 (last-end (org-list-get-item-end last-c struct
)))
2917 (push (funcall parse-sublist sub
) body
)
2918 ;; Remove children from the list just parsed.
2919 (setq children
(cdr (member last-c children
)))
2920 ;; There is a chunk of text belonging to the
2921 ;; item if last child doesn't end where next
2922 ;; child starts or where item ends.
2923 (unless (= (or (car children
) end
) last-end
)
2924 (push (funcall get-text
2925 last-end
(or (car children
) end
))
2927 (cons counter
(nreverse body
)))
2928 (list counter
(funcall get-text start end
))))))))
2929 ;; Store output, take care of cursor position and deletion of
2930 ;; list, then return output.
2931 (setq out
(funcall parse-sublist
(org-list-get-all-items top struct prevs
)))
2934 (delete-region top bottom
)
2935 (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re
))
2936 (replace-match "")))
2939 (defun org-list-make-subtree ()
2940 "Convert the plain list at point into a subtree."
2942 (if (not (ignore-errors (goto-char (org-in-item-p))))
2943 (error "Not in a list")
2944 (let ((list (save-excursion (org-list-parse-list t
))))
2945 (insert (org-list-to-subtree list
)))))
2947 (defun org-list-insert-radio-list ()
2948 "Insert a radio list template appropriate for this major mode."
2950 (let* ((e (assq major-mode org-list-radio-list-templates
))
2953 (unless e
(error "No radio list setup defined for %s" major-mode
))
2954 (setq name
(read-string "List name: "))
2955 (while (string-match "%n" txt
)
2956 (setq txt
(replace-match name t t txt
)))
2957 (or (bolp) (insert "\n"))
2962 (defun org-list-send-list (&optional maybe
)
2963 "Send a transformed version of this list to the receiver position.
2964 With argument MAYBE, fail quietly if no transformation is defined
2968 (unless (org-at-item-p) (error "Not at a list item"))
2970 (re-search-backward "#\\+ORGLST" nil t
)
2971 (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
2974 (error "Don't know how to transform this list"))))
2975 (let* ((name (match-string 1))
2976 (transform (intern (match-string 2)))
2980 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t
)
2981 (match-beginning 0)))
2984 (re-search-backward "#\\+ORGLST" nil t
)
2985 (re-search-forward (org-item-beginning-re) bottom-point t
)
2986 (match-beginning 0)))
2987 (list (save-restriction
2988 (narrow-to-region top-point bottom-point
)
2989 (org-list-parse-list)))
2991 (unless (fboundp transform
)
2992 (error "No such transformation function %s" transform
))
2993 (let ((txt (funcall transform list
)))
2994 ;; Find the insertion place
2996 (goto-char (point-min))
2997 (unless (re-search-forward
2998 (concat "BEGIN RECEIVE ORGLST +"
3000 "\\([ \t]\\|$\\)") nil t
)
3001 (error "Don't know where to insert translated list"))
3002 (goto-char (match-beginning 0))
3003 (beginning-of-line 2)
3005 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
3006 (error "Cannot find end of insertion region"))
3007 (delete-region beg
(point-at-bol))
3010 (message "List converted and installed at receiver location"))))
3012 (defsubst org-list-item-trim-br
(item)
3013 "Trim line breaks in a list ITEM."
3014 (setq item
(replace-regexp-in-string "\n +" " " item
)))
3016 (defun org-list-to-generic (list params
)
3017 "Convert a LIST parsed through `org-list-parse-list' to other formats.
3018 Valid parameters PARAMS are:
3020 :ustart String to start an unordered list
3021 :uend String to end an unordered list
3023 :ostart String to start an ordered list
3024 :oend String to end an ordered list
3026 :dstart String to start a descriptive list
3027 :dend String to end a descriptive list
3028 :dtstart String to start a descriptive term
3029 :dtend String to end a descriptive term
3030 :ddstart String to start a description
3031 :ddend String to end a description
3033 :splice When set to t, return only list body lines, don't wrap
3034 them into :[u/o]start and :[u/o]end. Default is nil.
3036 :istart String to start a list item.
3037 :icount String to start an item with a counter.
3038 :iend String to end a list item
3039 :isep String to separate items
3040 :lsep String to separate sublists
3041 :csep String to separate text from a sub-list
3043 :cboff String to insert for an unchecked check-box
3044 :cbon String to insert for a checked check-box
3045 :cbtrans String to insert for a check-box in transitional state
3047 :nobr Non-nil means remove line breaks in lists items.
3049 Alternatively, each parameter can also be a form returning
3050 a string. These sexp can use keywords `counter' and `depth',
3051 representing respectively counter associated to the current
3052 item, and depth of the current sub-list, starting at 0.
3053 Obviously, `counter' is only available for parameters applying to
3057 (splicep (plist-get p
:splice
))
3058 (ostart (plist-get p
:ostart
))
3059 (oend (plist-get p
:oend
))
3060 (ustart (plist-get p
:ustart
))
3061 (uend (plist-get p
:uend
))
3062 (dstart (plist-get p
:dstart
))
3063 (dend (plist-get p
:dend
))
3064 (dtstart (plist-get p
:dtstart
))
3065 (dtend (plist-get p
:dtend
))
3066 (ddstart (plist-get p
:ddstart
))
3067 (ddend (plist-get p
:ddend
))
3068 (istart (plist-get p
:istart
))
3069 (icount (plist-get p
:icount
))
3070 (iend (plist-get p
:iend
))
3071 (isep (plist-get p
:isep
))
3072 (lsep (plist-get p
:lsep
))
3073 (csep (plist-get p
:csep
))
3074 (cbon (plist-get p
:cbon
))
3075 (cboff (plist-get p
:cboff
))
3076 (cbtrans (plist-get p
:cbtrans
))
3077 (nobr (plist-get p
:nobr
))
3078 export-sublist
; for byte-compiler
3081 ;; Export an item ITEM of type TYPE, at DEPTH. First
3082 ;; string in item is treated in a special way as it can
3083 ;; bring extra information that needs to be processed.
3084 (lambda (item type depth
)
3085 (let* ((counter (pop item
))
3088 ((eq type
'descriptive
)
3089 ;; Stick DTSTART to ISTART by
3090 ;; left-trimming the latter.
3091 (concat (let ((s (eval istart
)))
3092 (or (and (string-match "[ \t\n\r]+\\'" s
)
3093 (replace-match "" t t s
))
3096 ((and counter
(eq type
'ordered
))
3097 (concat (eval icount
) "%s"))
3098 (t (concat (eval istart
) "%s")))
3101 ;; Replace checkbox if any is found.
3103 ((string-match "\\[CBON\\]" first
)
3104 (setq first
(replace-match cbon t t first
)))
3105 ((string-match "\\[CBOFF\\]" first
)
3106 (setq first
(replace-match cboff t t first
)))
3107 ((string-match "\\[CBTRANS\\]" first
)
3108 (setq first
(replace-match cbtrans t t first
))))
3109 ;; Replace line breaks if required
3110 (when nobr
(setq first
(org-list-item-trim-br first
)))
3111 ;; Insert descriptive term if TYPE is `descriptive'.
3112 (when (eq type
'descriptive
)
3113 (let* ((complete (string-match "^\\(.*\\)[ \t]+::" first
))
3116 (org-trim (match-string 1 first
)))
3119 (org-trim (substring first
(match-end 0)))
3121 (setq first
(concat (eval dtstart
) term
(eval dtend
)
3122 (eval ddstart
) desc
))))
3125 (mapconcat (lambda (e)
3127 (funcall export-sublist e
(1+ depth
))))
3128 item
(or (eval csep
) "")))))))
3131 ;; Export sublist SUB at DEPTH.
3133 (let* ((type (car sub
))
3138 (concat (eval ostart
) "%s" (eval oend
)))
3139 ((eq type
'descriptive
)
3140 (concat (eval dstart
) "%s" (eval dend
)))
3141 (t (concat (eval ustart
) "%s" (eval uend
))))
3143 (format fmt
(mapconcat (lambda (e)
3144 (funcall export-item e type depth
))
3145 items
(or (eval isep
) ""))))))))
3146 (concat (funcall export-sublist list
0) "\n")))
3148 (defun org-list-to-latex (list &optional params
)
3149 "Convert LIST into a LaTeX list.
3150 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3151 with overruling parameters for `org-list-to-generic'."
3152 (org-list-to-generic
3155 '(:splice nil
:ostart
"\\begin{enumerate}\n" :oend
"\\end{enumerate}"
3156 :ustart
"\\begin{itemize}\n" :uend
"\\end{itemize}"
3157 :dstart
"\\begin{description}\n" :dend
"\\end{description}"
3158 :dtstart
"[" :dtend
"] "
3159 :istart
"\\item " :iend
"\n"
3160 :icount
(let ((enum (nth depth
'("i" "ii" "iii" "iv"))))
3162 ;; LaTeX increments counter just before
3163 ;; using it, so set it to the desired
3164 ;; value, minus one.
3165 (format "\\setcounter{enum%s}{%s}\n\\item "
3169 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}"
3170 :cbtrans
"\\texttt{[-]}")
3173 (defun org-list-to-html (list &optional params
)
3174 "Convert LIST into a HTML list.
3175 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3176 with overruling parameters for `org-list-to-generic'."
3177 (org-list-to-generic
3180 '(:splice nil
:ostart
"<ol>\n" :oend
"\n</ol>"
3181 :ustart
"<ul>\n" :uend
"\n</ul>"
3182 :dstart
"<dl>\n" :dend
"\n</dl>"
3183 :dtstart
"<dt>" :dtend
"</dt>\n"
3184 :ddstart
"<dd>" :ddend
"</dd>"
3185 :istart
"<li>" :iend
"</li>"
3186 :icount
(format "<li value=\"%s\">" counter
)
3187 :isep
"\n" :lsep
"\n" :csep
"\n"
3188 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>"
3189 :cbtrans
"<code>[-]</code>")
3192 (defun org-list-to-texinfo (list &optional params
)
3193 "Convert LIST into a Texinfo list.
3194 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3195 with overruling parameters for `org-list-to-generic'."
3196 (org-list-to-generic
3199 '(:splice nil
:ostart
"@itemize @minus\n" :oend
"@end itemize"
3200 :ustart
"@enumerate\n" :uend
"@end enumerate"
3201 :dstart
"@table @asis\n" :dend
"@end table"
3202 :dtstart
" " :dtend
"\n"
3203 :istart
"@item\n" :iend
"\n"
3206 :cbon
"@code{[X]}" :cboff
"@code{[ ]}"
3207 :cbtrans
"@code{[-]}")
3210 (defun org-list-to-subtree (list &optional params
)
3211 "Convert LIST into an Org subtree.
3212 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3213 with overruling parameters for `org-list-to-generic'."
3214 (let* ((rule (cdr (assq 'heading org-blank-before-new-entry
)))
3215 (level (org-reduced-level (or (org-current-level) 0)))
3216 (blankp (or (eq rule t
)
3217 (and (eq rule
'auto
)
3219 (outline-previous-heading)
3220 (org-previous-line-empty-p)))))
3223 ;; Return the string for the heading, depending on depth D
3224 ;; of current sub-list.
3226 (let ((oddeven-level (+ level d
1)))
3227 (concat (make-string (if org-odd-levels-only
3228 (1- (* 2 oddeven-level
))
3232 (org-list-to-generic
3236 :dtstart
" " :dtend
" "
3237 :istart
(funcall get-stars depth
)
3238 :icount
(funcall get-stars depth
)
3239 :isep
(if blankp
"\n\n" "\n")
3240 :csep
(if blankp
"\n\n" "\n")
3241 :cbon
"DONE" :cboff
"TODO" :cbtrans
"TODO")
3246 ;;; org-list.el ends here