1 ;;; org-list.el --- Plain lists for Org-mode
3 ;; Copyright (C) 2004-2011 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 fundamental idea behind lists work is to use structures.
31 ;; A structure is a snapshot of the list, in the shape of data tree
32 ;; (see `org-list-struct').
34 ;; Once the list structure is stored, it is possible to make changes
35 ;; directly on it or get useful information about the list, with the
36 ;; two helper functions, namely `org-list-parents-alist' and
37 ;; `org-list-prevs-alist', and using accessors or methods.
39 ;; Structure is eventually applied to the buffer with
40 ;; `org-list-write-struct'. This function repairs (bullets,
41 ;; indentation, checkboxes) the structure before applying it. It
42 ;; should be called near the end of any function working on
45 ;; Thus, a function applying to lists should usually follow this
48 ;; 1. Verify point is in a list and grab item beginning (with the same
49 ;; function `org-in-item-p'). If the function requires the cursor
50 ;; to be at item's bullet, `org-at-item-p' is more selective. If
51 ;; the cursor is amidst the buffer, it is possible to find the
52 ;; closest item with `org-list-search-backward', or
53 ;; `org-list-search-forward', applied to `org-item-beginning-re'.
55 ;; 2. Get list structure with `org-list-struct'.
57 ;; 3. Compute one, or both, helper functions,
58 ;; (`org-list-parents-alist', `org-list-prevs-alist') depending on
61 ;; 4. Proceed with the modifications, using methods and accessors.
63 ;; 5. Verify and apply structure to buffer, using
64 ;; `org-list-write-struct'. Possibly use
65 ;; `org-update-checkbox-count-maybe' if checkboxes might have been
68 ;; Computing a list structure can be a costly operation on huge lists
69 ;; (a few thousand lines long). Thus, code should follow the rule :
70 ;; "collect once, use many". As a corollary, it is usally a bad idea
71 ;; to use directly an interactive function inside the code, as those,
72 ;; being independant entities, read the whole list structure another
82 (defvar org-M-RET-may-split-line
)
83 (defvar org-auto-align-tags
)
84 (defvar org-blank-before-new-entry
)
85 (defvar org-clock-string
)
86 (defvar org-closed-string
)
87 (defvar org-deadline-string
)
88 (defvar org-description-max-indent
)
90 (defvar org-odd-levels-only
)
91 (defvar org-scheduled-string
)
92 (defvar org-ts-regexp
)
93 (defvar org-ts-regexp-both
)
95 (declare-function org-at-heading-p
"org" (&optional ignored
))
96 (declare-function org-before-first-heading-p
"org" ())
97 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
98 (declare-function org-combine-plists
"org" (&rest plists
))
99 (declare-function org-count
"org" (cl-item cl-seq
))
100 (declare-function org-current-level
"org" ())
101 (declare-function org-entry-get
"org"
102 (pom property
&optional inherit literal-nil
))
103 (declare-function org-fix-tags-on-the-fly
"org" ())
104 (declare-function org-get-indentation
"org" (&optional line
))
105 (declare-function org-icompleting-read
"org" (&rest args
))
106 (declare-function org-in-block-p
"org" (names))
107 (declare-function org-in-regexp
"org" (re &optional nlines visually
))
108 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
109 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
110 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
111 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
112 (declare-function org-level-increment
"org" ())
113 (declare-function org-narrow-to-subtree
"org" ())
114 (declare-function org-on-heading-p
"org" (&optional invisible-ok
))
115 (declare-function org-previous-line-empty-p
"org" ())
116 (declare-function org-remove-if
"org" (predicate seq
))
117 (declare-function org-reduced-level
"org" (L))
118 (declare-function org-show-subtree
"org" ())
119 (declare-function org-time-string-to-seconds
"org" (s))
120 (declare-function org-timer-hms-to-secs
"org-timer" (hms))
121 (declare-function org-timer-item
"org-timer" (&optional arg
))
122 (declare-function org-trim
"org" (s))
123 (declare-function org-uniquify
"org" (list))
124 (declare-function outline-invisible-p
"outline" (&optional pos
))
125 (declare-function outline-flag-region
"outline" (from to flag
))
126 (declare-function outline-next-heading
"outline" ())
127 (declare-function outline-previous-heading
"outline" ())
129 ;;; Configuration variables
131 (defgroup org-plain-lists nil
132 "Options concerning plain lists in Org-mode."
133 :tag
"Org Plain lists"
134 :group
'org-structure
)
136 (defcustom org-cycle-include-plain-lists t
137 "When t, make TAB cycle visibility on plain list items.
138 Cycling plain lists works only when the cursor is on a plain list
139 item. When the cursor is on an outline heading, plain lists are
140 treated as text. This is the most stable way of handling this,
141 which is why it is the default.
143 When this is the symbol `integrate', then during cycling, plain
144 list items will *temporarily* be interpreted as outline headlines
145 with a level given by 1000+i where i is the indentation of the
146 bullet. This setting can lead to strange effects when switching
147 visibility to `children', because the first \"child\" in a
148 subtree decides what children should be listed. If that first
149 \"child\" is a plain list item with an implied large level
150 number, all true children and grand children of the outline
151 heading will be exposed in a children' view."
152 :group
'org-plain-lists
154 (const :tag
"Never" nil
)
155 (const :tag
"With cursor in plain list (recommended)" t
)
156 (const :tag
"As children of outline headings" integrate
)))
158 (defcustom org-list-demote-modify-bullet nil
159 "Default bullet type installed when demoting an item.
160 This is an association list, for each bullet type, this alist will point
161 to the bullet that should be used when this item is demoted.
164 (setq org-list-demote-modify-bullet
165 '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
170 + Silence of the Lambs
173 + The Hunt for Red October
179 - Silence of the Lambs
182 - The Hunt for Red October
184 :group
'org-plain-lists
187 (choice :tag
"If the current bullet is "
193 (choice :tag
"demotion will change it to"
200 (defcustom org-plain-list-ordered-item-terminator t
201 "The character that makes a line with leading number an ordered list item.
202 Valid values are ?. and ?\). To get both terminators, use t."
203 :group
'org-plain-lists
204 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
205 (const :tag
"paren like in \"2)\"" ?\
))
206 (const :tab
"both" t
)))
208 (defcustom org-alphabetical-lists nil
209 "Non-nil means single character alphabetical bullets are allowed.
210 Both uppercase and lowercase are handled. Lists with more than
211 26 items will fallback to standard numbering. Alphabetical
212 counters like \"[@c]\" will be recognized."
213 :group
'org-plain-lists
216 (defcustom org-list-two-spaces-after-bullet-regexp nil
217 "A regular expression matching bullets that should have 2 spaces after them.
218 When nil, no bullet will have two spaces after them. When
219 a string, it will be used as a regular expression. When the
220 bullet type of a list is changed, the new bullet type will be
221 matched against this regexp. If it matches, there will be two
222 spaces instead of one after the bullet in each item of the list."
223 :group
'org-plain-lists
225 (const :tag
"never" nil
)
228 (defcustom org-list-ending-method
'both
229 "Determine where plain lists should end.
230 Valid values are: `regexp', `indent' or `both'.
232 When set to `regexp', Org will look into two variables,
233 `org-empty-line-terminates-plain-lists' and the more general
234 `org-list-end-regexp', to determine what will end lists.
236 When set to `indent', a list will end whenever a line following
237 an item, but not starting one, is less or equally indented than
238 the first item of the list.
240 When set to `both', each of the preceding methods is applied to
241 determine lists endings. This is the default method."
242 :group
'org-plain-lists
244 (const :tag
"With a regexp defining ending" regexp
)
245 (const :tag
"With indentation of regular (no bullet) text" indent
)
246 (const :tag
"With both methods" both
)))
248 (defcustom org-empty-line-terminates-plain-lists nil
249 "Non-nil means an empty line ends all plain list levels.
250 This variable only makes sense if `org-list-ending-method' is set
251 to `regexp' or `both'. This is then equivalent to set
252 `org-list-end-regexp' to \"^[ \\t]*$\"."
253 :group
'org-plain-lists
256 (defcustom org-list-end-regexp
"^[ \t]*\n[ \t]*\n"
257 "Regexp matching the end of all plain list levels.
258 It must start with \"^\" and end with \"\\n\". It defaults to 2
259 blank lines. `org-empty-line-terminates-plain-lists' has
261 :group
'org-plain-lists
264 (defcustom org-list-automatic-rules
'((bullet . t
)
267 "Non-nil means apply set of rules when acting on lists.
268 By default, automatic actions are taken when using
269 \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
270 \\[org-shiftmetaright], \\[org-shiftmetaleft],
271 \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
272 \\[org-insert-todo-heading]. You can disable individually these
273 rules by setting them to nil. Valid rules are:
275 bullet when non-nil, cycling bullet do not allow lists at
276 column 0 to have * as a bullet and descriptions lists
278 checkbox when non-nil, checkbox statistics is updated each time
279 you either insert a new checkbox or toggle a checkbox.
280 It also prevents from inserting a checkbox in a
282 indent when non-nil, indenting or outdenting list top-item
283 with its subtree will move the whole list and
284 outdenting a list whose bullet is * to column 0 will
285 change that bullet to \"-\"."
286 :group
'org-plain-lists
287 :type
'(alist :tag
"Sets of rules"
290 (const :tag
"Bullet" bullet
)
291 (const :tag
"Checkbox" checkbox
)
292 (const :tag
"Indent" indent
))
294 (boolean :tag
"Activate" :value t
)))
296 (defcustom org-list-use-circular-motion nil
297 "Non-nil means commands implying motion in lists should be cyclic.
299 In that case, the item following the last item is the first one,
300 and the item preceding the first item is the last one.
302 This affects the behavior of \\[org-move-item-up],
303 \\[org-move-item-down], \\[org-next-item] and
304 \\[org-previous-item]."
305 :group
'org-plain-lists
308 (defvar org-checkbox-statistics-hook nil
309 "Hook that is run whenever Org thinks checkbox statistics should be updated.
310 This hook runs even if checkbox rule in
311 `org-list-automatic-rules' does not apply, so it can be used to
312 implement alternative ways of collecting statistics
315 (defcustom org-hierarchical-checkbox-statistics t
316 "Non-nil means checkbox statistics counts only the state of direct children.
317 When nil, all boxes below the cookie are counted.
318 This can be set to nil on a per-node basis using a COOKIE_DATA property
319 with the word \"recursive\" in the value."
320 :group
'org-plain-lists
323 (defcustom org-description-max-indent
20
324 "Maximum indentation for the second line of a description list.
325 When the indentation would be larger than this, it will become
326 5 characters instead."
327 :group
'org-plain-lists
330 (defcustom org-list-indent-offset
0
331 "Additional indentation for sub-items in a list.
332 By setting this to a small number, usually 1 or 2, one can more
333 clearly distinguish sub-items in a list."
334 :group
'org-plain-lists
337 (defcustom org-list-radio-list-templates
338 '((latex-mode "% BEGIN RECEIVE ORGLST %n
339 % END RECEIVE ORGLST %n
341 #+ORGLST: SEND %n org-list-to-latex
344 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
345 @c END RECEIVE ORGLST %n
347 #+ORGLST: SEND %n org-list-to-texinfo
350 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
351 <!-- END RECEIVE ORGLST %n -->
353 #+ORGLST: SEND %n org-list-to-html
356 "Templates for radio lists in different major modes.
357 All occurrences of %n in a template will be replaced with the name of the
358 list, obtained by prompting the user."
359 :group
'org-plain-lists
361 (list (symbol :tag
"Major mode")
362 (string :tag
"Format"))))
364 (defvar org-list-forbidden-blocks
'("example" "verse" "src" "ascii" "beamer"
365 "docbook" "html" "latex" "odt")
366 "Names of blocks where lists are not allowed.
367 Names must be in lower case.")
369 (defvar org-list-export-context
'(block inlinetask
)
370 "Context types where lists will be interpreted during export.
372 Valid types are `drawer', `inlinetask' and `block'. More
373 specifically, type `block' is determined by the variable
374 `org-list-forbidden-blocks'.")
377 ;;; Predicates and regexps
379 (defconst org-list-end-re
(if org-empty-line-terminates-plain-lists
382 "Regex corresponding to the end of a list.
383 It depends on `org-empty-line-terminates-plain-lists'.")
385 (defconst org-list-full-item-re
386 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
387 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
388 "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
389 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
390 "Matches a list item and puts everything into groups:
394 group 4: description tag")
396 (defun org-item-re ()
397 "Return the correct regular expression for plain lists."
399 ((eq org-plain-list-ordered-item-terminator t
) "[.)]")
400 ((= org-plain-list-ordered-item-terminator ?\
)) ")")
401 ((= org-plain-list-ordered-item-terminator ?.
) "\\.")
403 (alpha (if org-alphabetical-lists
"\\|[A-Za-z]" "")))
404 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha
"\\)" term
405 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
407 (defsubst org-item-beginning-re
()
408 "Regexp matching the beginning of a plain list item."
409 (concat "^" (org-item-re)))
411 (defun org-list-at-regexp-after-bullet-p (regexp)
412 "Is point at a list item with REGEXP after bullet?"
415 (goto-char (match-end 0))
416 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
417 (if org-alphabetical-lists
418 "\\([0-9]+\\|[A-Za-z]\\)"
421 ;; Ignore counter if any
422 (when (looking-at counter-re
) (goto-char (match-end 0))))
423 (looking-at regexp
))))
425 (defun org-list-in-valid-context-p ()
426 "Is point in a context where lists are allowed?"
427 (not (org-in-block-p org-list-forbidden-blocks
)))
429 (defun org-in-item-p ()
430 "Return item beginning position when in a plain list, nil otherwise.
431 This checks `org-list-ending-method'."
434 (let* ((case-fold-search t
)
435 (context (org-list-context))
436 (lim-up (car context
))
437 (drawers-re (concat "^[ \t]*:\\("
438 (mapconcat 'regexp-quote org-drawers
"\\|")
440 (inlinetask-re (and (featurep 'org-inlinetask
)
441 (org-inlinetask-outline-regexp)))
442 (item-re (org-item-re))
443 ;; Indentation isn't meaningful when point starts at an empty
444 ;; line or an inline task.
445 (ind-ref (if (or (looking-at "^[ \t]*$")
446 (and inlinetask-re
(looking-at inlinetask-re
)))
448 (org-get-indentation))))
450 ((eq (nth 2 context
) 'invalid
) nil
)
451 ((looking-at item-re
) (point))
453 ;; Detect if cursor in amidst `org-list-end-re'. First, count
454 ;; number HL of hard lines it takes, then call `org-in-regexp'
455 ;; to compute its boundaries END-BOUNDS. When point is
456 ;; in-between, move cursor before regexp beginning.
457 (let ((hl 0) (i -
1) end-bounds
)
458 (when (and (not (eq org-list-ending-method
'indent
))
460 (while (setq i
(string-match
461 "[\r\n]" org-list-end-re
(1+ i
)))
463 (setq end-bounds
(org-in-regexp org-list-end-re hl
)))
464 (>= (point) (car end-bounds
))
465 (< (point) (cdr end-bounds
)))
466 (goto-char (car end-bounds
))
468 ;; Look for an item, less indented that reference line if
469 ;; `org-list-ending-method' isn't `regexp'.
472 (let ((ind (org-get-indentation)))
474 ;; This is exactly what we want.
475 ((and (looking-at item-re
)
477 (eq org-list-ending-method
'regexp
)))
478 (throw 'exit
(point)))
479 ;; At upper bound of search or looking at the end of a
480 ;; previous list: search is over.
481 ((<= (point) lim-up
) (throw 'exit nil
))
482 ((and (not (eq org-list-ending-method
'indent
))
483 (looking-at org-list-end-re
))
485 ;; Skip blocks, drawers, inline-tasks, blank lines
486 ((and (looking-at "^[ \t]*#\\+end_")
487 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
488 ((and (looking-at "^[ \t]*:END:")
489 (re-search-backward drawers-re lim-up t
))
491 ((and inlinetask-re
(looking-at inlinetask-re
))
492 (org-inlinetask-goto-beginning)
494 ((looking-at "^[ \t]*$") (forward-line -
1))
495 ;; Text at column 0 cannot belong to a list: stop.
496 ((zerop ind
) (throw 'exit nil
))
497 ;; Normal text less indented than reference line, take
498 ;; it as new reference.
502 (t (forward-line -
1)))))))))))
504 (defun org-at-item-p ()
505 "Is point in a line starting a hand-formatted item?"
508 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
510 (defun org-at-item-bullet-p ()
511 "Is point at the bullet of a plain list item?"
513 (not (member (char-after) '(?\ ?
\t)))
514 (< (point) (match-end 0))))
516 (defun org-at-item-timer-p ()
517 "Is point at a line starting a plain list item with a timer?"
518 (org-list-at-regexp-after-bullet-p
519 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
521 (defun org-at-item-description-p ()
522 "Is point at a description list item?"
523 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
525 (defun org-at-item-checkbox-p ()
526 "Is point at a line starting a plain-list item with a checklet?"
527 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
529 (defun org-at-item-counter-p ()
530 "Is point at a line starting a plain-list item with a counter?"
532 (looking-at org-list-full-item-re
)
536 ;;; Structures and helper functions
538 (defun org-list-context ()
539 "Determine context, and its boundaries, around point.
541 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
542 are boundaries and CONTEXT is a symbol among `drawer', `block',
543 `invalid', `inlinetask' and nil.
545 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
548 (org-with-limited-levels
550 (let ((case-fold-search t
) (pos (point)) beg end context-type
551 ;; Get positions of surrounding headings. This is the
553 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t
))
556 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
557 ;; Is point inside a drawer?
558 (let ((end-re "^[ \t]*:END:")
559 ;; Can't use org-drawers-regexp as this function might
560 ;; be called in buffers not in Org mode.
561 (beg-re (concat "^[ \t]*:\\("
562 (mapconcat 'regexp-quote org-drawers
"\\|")
564 (when (save-excursion
565 (and (not (looking-at beg-re
))
566 (not (looking-at end-re
))
567 (setq beg
(and (re-search-backward beg-re lim-up t
)
568 (1+ (point-at-eol))))
569 (setq end
(or (and (re-search-forward end-re lim-down t
)
570 (1- (match-beginning 0)))
573 (setq lim-up beg lim-down end context-type
'drawer
)))
574 ;; Is point strictly in a block, and of which type?
575 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type
)
576 (when (save-excursion
577 (and (not (looking-at block-re
))
578 (setq beg
(and (re-search-backward block-re lim-up t
)
579 (1+ (point-at-eol))))
580 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
581 (setq type
(downcase (match-string 1)))
583 (setq end
(or (and (re-search-forward block-re lim-down t
)
587 (equal (downcase (match-string 1)) "end")))
588 (setq lim-up beg lim-down end
589 context-type
(if (member type org-list-forbidden-blocks
)
591 ;; Is point in an inlinetask?
592 (when (and (featurep 'org-inlinetask
)
594 (let* ((beg-re (org-inlinetask-outline-regexp))
595 (end-re (concat beg-re
"END[ \t]*$")))
596 (and (not (looking-at "^\\*+"))
597 (setq beg
(and (re-search-backward beg-re lim-up t
)
598 (1+ (point-at-eol))))
599 (not (looking-at end-re
))
600 (setq end
(and (re-search-forward end-re lim-down t
)
601 (1- (match-beginning 0))))
603 (setq lim-up beg lim-down end context-type
'inlinetask
))
604 ;; Return context boundaries and type.
605 (list lim-up lim-down context-type
))))))
607 (defun org-list-struct ()
608 "Return structure of list at point.
610 A list structure is an alist where key is point at item, and
613 2. bullet with trailing whitespace,
614 3. bullet counter, if any,
616 5. description tag, if any,
617 6. position at item end.
619 Thus the following list, where numbers in parens are
624 5. [@5] sub-item 2 (34)
625 some other text belonging to first item (55)
627 + tag :: description (109)
630 will get the following structure:
632 \(\(1 0 \"- \" nil \"[X]\" nil 97\)
633 \(18 2 \"1. \" nil nil nil 34\)
634 \(34 2 \"5. \" \"5\" nil nil 55\)
635 \(97 0 \"- \" nil nil nil 131\)
636 \(109 2 \"+ \" nil nil \"tag\" 131\)
638 Assume point is at an item."
641 (let* ((case-fold-search t
)
642 (context (org-list-context))
643 (lim-up (car context
))
644 (lim-down (nth 1 context
))
646 (item-re (org-item-re))
647 (drawers-re (concat "^[ \t]*:\\("
648 (mapconcat 'regexp-quote org-drawers
"\\|")
650 (inlinetask-re (and (featurep 'org-inlinetask
)
651 (org-inlinetask-outline-regexp)))
652 (beg-cell (cons (point) (org-get-indentation)))
653 ind itm-lst itm-lst-2 end-lst end-lst-2 struct
656 ;; Return association at point.
658 (looking-at org-list-full-item-re
)
661 (match-string-no-properties 1) ; bullet
662 (match-string-no-properties 2) ; counter
663 (match-string-no-properties 3) ; checkbox
664 (match-string-no-properties 4))))) ; description tag
667 ;; Ensure list ends at the first blank line.
669 (skip-chars-backward " \r\t\n")
670 (min (1+ (point-at-eol)) lim-down
)))))
671 ;; 1. Read list from starting item to its beginning, and save
672 ;; top item position and indentation in BEG-CELL. Also store
673 ;; ending position of items in END-LST.
677 (let ((ind (+ (or (get-text-property (point) 'original-indentation
) 0)
678 (org-get-indentation))))
681 ;; At upward limit: if we ended at an item, store it,
682 ;; else dimiss useless data recorded above BEG-CELL.
686 (if (or (not (looking-at item-re
))
687 (get-text-property (point) 'org-example
))
688 (memq (assq (car beg-cell
) itm-lst
) itm-lst
)
689 (setq beg-cell
(cons (point) ind
))
690 (cons (funcall assoc-at-point ind
) itm-lst
)))))
691 ;; At a verbatim block, go before its beginning. Move
692 ;; from eol to ensure `previous-single-property-change'
693 ;; will return a value.
694 ((get-text-property (point) 'org-example
)
695 (goto-char (previous-single-property-change
696 (point-at-eol) 'org-example nil lim-up
))
698 ;; Looking at a list ending regexp. Dismiss useless
699 ;; data recorded above BEG-CELL. Jump to part 2.
700 ((and (not (eq org-list-ending-method
'indent
))
701 (looking-at org-list-end-re
))
704 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
705 ;; Point is at an item. Add data to ITM-LST. It may
706 ;; also end a previous item: save it in END-LST. If
707 ;; ind is less or equal than BEG-CELL and there is no
708 ;; end at this ind or lesser, this item becomes the new
710 ((looking-at item-re
)
711 (push (funcall assoc-at-point ind
) itm-lst
)
712 (push (cons ind
(point)) end-lst
)
713 (when (or (and (eq org-list-ending-method
'regexp
)
714 (<= ind
(cdr beg-cell
)))
715 (< ind text-min-ind
))
716 (setq beg-cell
(cons (point) ind
)))
718 ;; Skip blocks, drawers, inline tasks, blank lines.
719 ((and (looking-at "^[ \t]*#\\+end_")
720 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
721 ((and (looking-at "^[ \t]*:END:")
722 (re-search-backward drawers-re lim-up t
))
724 ((and inlinetask-re
(looking-at inlinetask-re
))
725 (org-inlinetask-goto-beginning)
727 ((looking-at "^[ \t]*$")
729 ;; From there, point is not at an item. Unless ending
730 ;; method is `regexp', interpret line's indentation:
731 ;; - text at column 0 is necessarily out of any list.
732 ;; Dismiss data recorded above BEG-CELL. Jump to
734 ;; - any other case may be an ending position for an
735 ;; hypothetical item above. Store it and proceed.
736 ((eq org-list-ending-method
'regexp
) (forward-line -
1))
740 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
742 (when (< ind text-min-ind
) (setq text-min-ind ind
))
743 (push (cons ind
(point)) end-lst
)
744 (forward-line -
1)))))))
745 ;; 2. Read list from starting point to its end, that is until we
746 ;; get out of context, or that a non-item line is less or
747 ;; equally indented than BEG-CELL's cdr. Also, store ending
748 ;; position of items in END-LST-2.
751 (let ((ind (+ (or (get-text-property (point) 'original-indentation
) 0)
752 (org-get-indentation))))
754 ((>= (point) lim-down
)
755 ;; At downward limit: this is de facto the end of the
756 ;; list. Save point as an ending position, and jump to
759 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
760 ;; At a verbatim block, move to its end. Point is at bol
761 ;; and 'org-example property is set by whole lines:
762 ;; `next-single-property-change' always return a value.
763 ((get-text-property (point) 'org-example
)
765 (next-single-property-change (point) 'org-example nil lim-down
)))
766 ;; Looking at a list ending regexp. Save point as an
767 ;; ending position and jump to part 3.
768 ((and (not (eq org-list-ending-method
'indent
))
769 (looking-at org-list-end-re
))
770 (throw 'exit
(push (cons 0 (point)) end-lst-2
)))
771 ((looking-at item-re
)
772 ;; Point is at an item. Add data to ITM-LST-2. It may
773 ;; also end a previous item, so save it in END-LST-2.
774 (push (funcall assoc-at-point ind
) itm-lst-2
)
775 (push (cons ind
(point)) end-lst-2
)
777 ;; Skip inline tasks and blank lines along the way
778 ((and inlinetask-re
(looking-at inlinetask-re
))
779 (org-inlinetask-goto-end))
780 ((looking-at "^[ \t]*$")
782 ;; Ind is lesser or equal than BEG-CELL's. The list is
783 ;; over: store point as an ending position and jump to
785 ((and (not (eq org-list-ending-method
'regexp
))
786 (<= ind
(cdr beg-cell
)))
788 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
789 ;; Else, if ind is lesser or equal than previous item's,
790 ;; this is an ending position: store it. In any case,
791 ;; skip block or drawer at point, and move to next line.
793 (when (and (not (eq org-list-ending-method
'regexp
))
794 (<= ind
(nth 1 (car itm-lst-2
))))
795 (push (cons ind
(point)) end-lst-2
))
797 ((and (looking-at "^[ \t]*#\\+begin_")
798 (re-search-forward "^[ \t]*#\\+end_" lim-down t
)))
799 ((and (looking-at drawers-re
)
800 (re-search-forward "^[ \t]*:END:" lim-down t
))))
801 (forward-line 1))))))
802 (setq struct
(append itm-lst
(cdr (nreverse itm-lst-2
)))
803 end-lst
(append end-lst
(cdr (nreverse end-lst-2
))))
804 ;; 3. Correct ill-formed lists by ensuring top item is the least
806 (let ((min-ind (nth 1 (car struct
))))
808 (let ((ind (nth 1 item
))
810 (when (< ind min-ind
)
811 (setcar (cdr item
) min-ind
)
812 ;; Trim bullet so item will be seen as different
813 ;; when compared with repaired version.
814 (setcar (nthcdr 2 item
) (org-trim bul
)))))
816 ;; 4. Associate each item to its end pos.
817 (org-list-struct-assoc-end struct end-lst
)
821 (defun org-list-struct-assoc-end (struct end-list
)
822 "Associate proper ending point to items in STRUCT.
824 END-LIST is a pseudo-alist where car is indentation and cdr is
827 This function modifies STRUCT."
828 (let ((endings end-list
))
831 (let ((pos (car elt
))
833 ;; Remove end candidates behind current item.
834 (while (or (<= (cdar endings
) pos
))
836 ;; Add end position to item assoc.
837 (let ((old-end (nthcdr 6 elt
))
838 (new-end (assoc-default ind endings
'<=)))
840 (setcar old-end new-end
)
841 (setcdr elt
(append (cdr elt
) (list new-end
)))))))
844 (defun org-list-prevs-alist (struct)
845 "Return alist between item and previous item in STRUCT."
846 (let ((item-end-alist (mapcar (lambda (e) (cons (car e
) (nth 6 e
)))
849 (let ((prev (car (rassq (car e
) item-end-alist
))))
850 (cons (car e
) prev
)))
853 (defun org-list-parents-alist (struct)
854 "Return alist between item and parent in STRUCT."
855 (let ((ind-to-ori (list (list (nth 1 (car struct
)))))
856 (prev-pos (list (caar struct
))))
858 (mapcar (lambda (item)
859 (let ((pos (car item
))
861 (prev-ind (caar ind-to-ori
)))
866 (member (assq ind ind-to-ori
) ind-to-ori
))
867 (cons pos
(cdar ind-to-ori
)))
869 (let ((origin (nth 1 prev-pos
)))
870 (push (cons ind origin
) ind-to-ori
)
872 (t (cons pos
(cdar ind-to-ori
))))))
878 (defsubst org-list-get-nth
(n key struct
)
879 "Return the Nth value of KEY in STRUCT."
880 (nth n
(assq key struct
)))
882 (defun org-list-set-nth (n key struct new
)
883 "Set the Nth value of KEY in STRUCT to NEW.
884 \nThis function modifies STRUCT."
885 (setcar (nthcdr n
(assq key struct
)) new
))
887 (defsubst org-list-get-ind
(item struct
)
888 "Return indentation of ITEM in STRUCT."
889 (org-list-get-nth 1 item struct
))
891 (defun org-list-set-ind (item struct ind
)
892 "Set indentation of ITEM in STRUCT to IND.
893 \nThis function modifies STRUCT."
894 (org-list-set-nth 1 item struct ind
))
896 (defsubst org-list-get-bullet
(item struct
)
897 "Return bullet of ITEM in STRUCT."
898 (org-list-get-nth 2 item struct
))
900 (defun org-list-set-bullet (item struct bullet
)
901 "Set bullet of ITEM in STRUCT to BULLET.
902 \nThis function modifies STRUCT."
903 (org-list-set-nth 2 item struct bullet
))
905 (defsubst org-list-get-counter
(item struct
)
906 "Return counter of ITEM in STRUCT."
907 (org-list-get-nth 3 item struct
))
909 (defsubst org-list-get-checkbox
(item struct
)
910 "Return checkbox of ITEM in STRUCT or nil."
911 (org-list-get-nth 4 item struct
))
913 (defun org-list-set-checkbox (item struct checkbox
)
914 "Set checkbox of ITEM in STRUCT to CHECKBOX.
915 \nThis function modifies STRUCT."
916 (org-list-set-nth 4 item struct checkbox
))
918 (defsubst org-list-get-tag
(item struct
)
919 "Return end position of ITEM in STRUCT."
920 (org-list-get-nth 5 item struct
))
922 (defun org-list-get-item-end (item struct
)
923 "Return end position of ITEM in STRUCT."
924 (org-list-get-nth 6 item struct
))
926 (defun org-list-get-item-end-before-blank (item struct
)
927 "Return point at end of ITEM in STRUCT, before any blank line.
928 Point returned is at end of line."
930 (goto-char (org-list-get-item-end item struct
))
931 (skip-chars-backward " \r\t\n")
934 (defun org-list-get-parent (item struct parents
)
935 "Return parent of ITEM or nil.
936 STRUCT is the list structure. PARENTS is the alist of parents,
937 as returned by `org-list-parents-alist'."
938 (let ((parents (or parents
(org-list-parents-alist struct
))))
939 (cdr (assq item parents
))))
941 (defun org-list-has-child-p (item struct
)
942 "Non-nil if ITEM has a child.
944 STRUCT is the list structure.
946 Value returned is the position of the first child of ITEM."
947 (let ((ind (org-list-get-ind item struct
))
948 (child-maybe (car (nth 1 (member (assq item struct
) struct
)))))
949 (when (and child-maybe
950 (< ind
(org-list-get-ind child-maybe struct
)))
953 (defun org-list-get-next-item (item struct prevs
)
954 "Return next item in same sub-list as ITEM, or nil.
955 STRUCT is the list structure. PREVS is the alist of previous
956 items, as returned by `org-list-prevs-alist'."
957 (car (rassq item prevs
)))
959 (defun org-list-get-prev-item (item struct prevs
)
960 "Return previous item in same sub-list as ITEM, or nil.
961 STRUCT is the list structure. PREVS is the alist of previous
962 items, as returned by `org-list-prevs-alist'."
963 (cdr (assq item prevs
)))
965 (defun org-list-get-subtree (item struct
)
966 "List all items having ITEM as a common ancestor, or nil.
967 STRUCT is the list structure."
968 (let* ((item-end (org-list-get-item-end item struct
))
969 (sub-struct (cdr (member (assq item struct
) struct
)))
974 (if (< pos item-end
) (push pos subtree
) (throw 'exit nil
))))
978 (defun org-list-get-all-items (item struct prevs
)
979 "List all items in the same sub-list as ITEM.
980 STRUCT is the list structure. PREVS is the alist of previous
981 items, as returned by `org-list-prevs-alist'."
982 (let ((prev-item item
)
984 before-item after-item
)
985 (while (setq prev-item
(org-list-get-prev-item prev-item struct prevs
))
986 (push prev-item before-item
))
987 (while (setq next-item
(org-list-get-next-item next-item struct prevs
))
988 (push next-item after-item
))
989 (append before-item
(list item
) (nreverse after-item
))))
991 (defun org-list-get-children (item struct parents
)
992 "List all children of ITEM, or nil.
993 STRUCT is the list structure. PARENTS is the alist of parents, as
994 returned by `org-list-parents-alist'."
996 (while (setq child
(car (rassq item parents
)))
997 (setq parents
(cdr (member (assq child parents
) parents
)))
1001 (defun org-list-get-top-point (struct)
1002 "Return point at beginning of list.
1003 STRUCT is the list structure."
1006 (defun org-list-get-bottom-point (struct)
1007 "Return point at bottom of list.
1008 STRUCT is the list structure."
1010 (mapcar (lambda (e) (org-list-get-item-end (car e
) struct
)) struct
)))
1012 (defun org-list-get-list-begin (item struct prevs
)
1013 "Return point at beginning of sub-list ITEM belongs.
1014 STRUCT is the list structure. PREVS is the alist of previous
1015 items, as returned by `org-list-prevs-alist'."
1016 (let ((first-item item
) prev-item
)
1017 (while (setq prev-item
(org-list-get-prev-item first-item struct prevs
))
1018 (setq first-item prev-item
))
1021 (defalias 'org-list-get-first-item
'org-list-get-list-begin
)
1023 (defun org-list-get-last-item (item struct prevs
)
1024 "Return point at last item of sub-list ITEM belongs.
1025 STRUCT is the list structure. PREVS is the alist of previous
1026 items, as returned by `org-list-prevs-alist'."
1027 (let ((last-item item
) next-item
)
1028 (while (setq next-item
(org-list-get-next-item last-item struct prevs
))
1029 (setq last-item next-item
))
1032 (defun org-list-get-list-end (item struct prevs
)
1033 "Return point at end of sub-list ITEM belongs.
1034 STRUCT is the list structure. PREVS is the alist of previous
1035 items, as returned by `org-list-prevs-alist'."
1036 (org-list-get-item-end (org-list-get-last-item item struct prevs
) struct
))
1038 (defun org-list-get-list-type (item struct prevs
)
1039 "Return the type of the list containing ITEM, as a symbol.
1041 STRUCT is the list structure. PREVS is the alist of previous
1042 items, as returned by `org-list-prevs-alist'.
1044 Possible types are `descriptive', `ordered' and `unordered'. The
1045 type is determined by the first item of the list."
1046 (let ((first (org-list-get-list-begin item struct prevs
)))
1048 ((org-list-get-tag first struct
) 'descriptive
)
1049 ((string-match "[[:alnum:]]" (org-list-get-bullet first struct
)) 'ordered
)
1055 (defun org-list-search-generic (search re bound noerr
)
1056 "Search a string in valid contexts for lists.
1057 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1058 in `re-search-forward'."
1060 (let ((origin (point)))
1062 ;; 1. No match: return to origin or bound, depending on NOERR.
1063 (unless (funcall search re bound noerr
)
1064 (throw 'exit
(and (goto-char (if (memq noerr
'(t nil
)) origin bound
))
1066 ;; 2. Match in valid context: return point. Else, continue
1068 (when (org-list-in-valid-context-p) (throw 'exit
(point)))))))
1070 (defun org-list-search-backward (regexp &optional bound noerror
)
1071 "Like `re-search-backward' but stop only where lists are recognized.
1072 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1073 `re-search-backward'."
1074 (org-list-search-generic #'re-search-backward
1075 regexp
(or bound
(point-min)) noerror
))
1077 (defun org-list-search-forward (regexp &optional bound noerror
)
1078 "Like `re-search-forward' but stop only where lists are recognized.
1079 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1080 `re-search-forward'."
1081 (org-list-search-generic #'re-search-forward
1082 regexp
(or bound
(point-max)) noerror
))
1085 ;;; Methods on structures
1087 (defsubst org-list-bullet-string
(bullet)
1088 "Return BULLET with the correct number of whitespaces.
1089 It determines the number of whitespaces to append by looking at
1090 `org-list-two-spaces-after-bullet-regexp'."
1092 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1094 org-list-two-spaces-after-bullet-regexp bullet
))
1097 (string-match "\\S-+\\([ \t]*\\)" bullet
)
1098 (replace-match spaces nil nil bullet
1))))
1100 (defun org-list-swap-items (beg-A beg-B struct
)
1101 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1102 Blank lines at the end of items are left in place. Return the
1103 new structure after the changes.
1105 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1106 to the same sub-list.
1108 This function modifies STRUCT."
1110 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct
))
1111 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct
))
1112 (end-A (org-list-get-item-end beg-A struct
))
1113 (end-B (org-list-get-item-end beg-B struct
))
1114 (size-A (- end-A-no-blank beg-A
))
1115 (size-B (- end-B-no-blank beg-B
))
1116 (body-A (buffer-substring beg-A end-A-no-blank
))
1117 (body-B (buffer-substring beg-B end-B-no-blank
))
1118 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B
))
1119 (sub-A (cons beg-A
(org-list-get-subtree beg-A struct
)))
1120 (sub-B (cons beg-B
(org-list-get-subtree beg-B struct
))))
1121 ;; 1. Move effectively items in buffer.
1123 (delete-region beg-A end-B-no-blank
)
1124 (insert (concat body-B between-A-no-blank-and-B body-A
))
1125 ;; 2. Now modify struct. No need to re-read the list, the
1126 ;; transformation is just a shift of positions. Some special
1127 ;; attention is required for items ending at END-A and END-B
1128 ;; as empty spaces are not moved there. In others words,
1129 ;; item BEG-A will end with whitespaces that were at the end
1130 ;; of BEG-B and the same applies to BEG-B.
1132 (let ((pos (car e
)))
1136 (let ((end-e (nth 6 e
)))
1137 (setcar e
(+ pos
(- end-B-no-blank end-A-no-blank
)))
1138 (setcar (nthcdr 6 e
)
1139 (+ end-e
(- end-B-no-blank end-A-no-blank
)))
1140 (when (= end-e end-A
) (setcar (nthcdr 6 e
) end-B
))))
1142 (let ((end-e (nth 6 e
)))
1143 (setcar e
(- (+ pos beg-A
) beg-B
))
1144 (setcar (nthcdr 6 e
) (+ end-e
(- beg-A beg-B
)))
1145 (when (= end-e end-B
)
1146 (setcar (nthcdr 6 e
)
1147 (+ beg-A size-B
(- end-A end-A-no-blank
))))))
1149 (let ((end-e (nth 6 e
)))
1150 (setcar e
(+ pos
(- size-B size-A
)))
1151 (setcar (nthcdr 6 e
) (+ end-e
(- size-B size-A
))))))))
1153 (sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))))
1155 (defun org-list-separating-blank-lines-number (pos struct prevs
)
1156 "Return number of blank lines that should separate items in list.
1158 POS is the position of point where `org-list-insert-item' was called.
1160 STRUCT is the list structure. PREVS is the alist of previous
1161 items, as returned by `org-list-prevs-alist'.
1163 Assume point is at item's beginning. If the item is alone, apply
1164 some heuristics to guess the result."
1166 (let ((item (point))
1168 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
1173 ;; Count blank lines above beginning of line.
1175 (count-lines (goto-char (point-at-bol))
1176 (progn (skip-chars-backward " \r\t\n")
1180 ;; Trivial cases where there should be none.
1181 ((or (and (not (eq org-list-ending-method
'indent
))
1182 org-empty-line-terminates-plain-lists
)
1183 (not insert-blank-p
)) 0)
1184 ;; When `org-blank-before-new-entry' says so, it is 1.
1185 ((eq insert-blank-p t
) 1)
1186 ;; `plain-list-item' is 'auto. Count blank lines separating
1187 ;; neighbours items in list.
1188 (t (let ((next-p (org-list-get-next-item item struct prevs
)))
1190 ;; Is there a next item?
1191 (next-p (goto-char next-p
)
1192 (funcall count-blanks
))
1193 ;; Is there a previous item?
1194 ((org-list-get-prev-item item struct prevs
)
1195 (funcall count-blanks
))
1196 ;; User inserted blank lines, trust him.
1197 ((and (> pos
(org-list-get-item-end-before-blank item struct
))
1198 (> (save-excursion (goto-char pos
)
1199 (setq usr-blank
(funcall count-blanks
)))
1202 ;; Are there blank lines inside the list so far?
1204 (goto-char (org-list-get-top-point struct
))
1205 (org-list-search-forward
1206 "^[ \t]*$" (org-list-get-item-end-before-blank item struct
) t
))
1208 ;; Default choice: no blank line.
1211 (defun org-list-insert-item (pos struct prevs
&optional checkbox after-bullet
)
1212 "Insert a new list item at POS and return the new structure.
1213 If POS is before first character after bullet of the item, the
1214 new item will be created before the current one.
1216 STRUCT is the list structure. PREVS is the the alist of previous
1217 items, as returned by `org-list-prevs-alist'.
1219 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1220 after the bullet. Cursor will be after this text once the
1223 This function modifies STRUCT."
1224 (let ((case-fold-search t
))
1225 ;; 1. Get information about list: position of point with regards
1226 ;; to item start (BEFOREP), blank lines number separating items
1227 ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
1228 (let* ((item (progn (goto-char pos
) (goto-char (org-list-get-item-begin))))
1229 (item-end (org-list-get-item-end item struct
))
1230 (item-end-no-blank (org-list-get-item-end-before-blank item struct
))
1231 (beforep (and (looking-at org-list-full-item-re
)
1232 (<= pos
(match-end 0))))
1233 (split-line-p (org-get-alist-option org-M-RET-may-split-line
'item
))
1234 (blank-nb (org-list-separating-blank-lines-number
1236 ;; 2. Build the new item to be created. Concatenate same
1237 ;; bullet as item, checkbox, text AFTER-BULLET if
1238 ;; provided, and text cut from point to end of item
1239 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1240 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1241 ;; between what was cut and what was inserted in buffer
1242 ;; is stored in SIZE-OFFSET.
1243 (ind (org-list-get-ind item struct
))
1244 (ind-size (if indent-tabs-mode
1245 (+ (/ ind tab-width
) (mod ind tab-width
))
1247 (bullet (org-list-bullet-string (org-list-get-bullet item struct
)))
1248 (box (when checkbox
"[ ]"))
1250 (and (not beforep
) split-line-p
1253 ;; If POS is greater than ITEM-END, then point is
1254 ;; in some white lines after the end of the list.
1255 ;; Those must be removed, or they will be left,
1256 ;; stacking up after the list.
1257 (when (< item-end pos
)
1258 (delete-region (1- item-end
) (point-at-eol)))
1259 (skip-chars-backward " \r\t\n")
1261 (delete-and-extract-region pos item-end-no-blank
))))
1262 (body (concat bullet
(when box
(concat box
" ")) after-bullet
1264 (if (string-match "\\`[ \t]+" text-cut
)
1265 (replace-match "" t t text-cut
)
1267 (item-sep (make-string (1+ blank-nb
) ?
\n))
1268 (item-size (+ ind-size
(length body
) (length item-sep
)))
1269 (size-offset (- item-size
(length text-cut
))))
1270 ;; 4. Insert effectively item into buffer.
1272 (org-indent-to-column ind
)
1273 (insert body item-sep
)
1274 ;; 5. Add new item to STRUCT.
1279 ;; Before inserted item, positions don't change but
1280 ;; an item ending after insertion has its end shifted
1283 (when (> end item
) (setcar (nthcdr 6 e
) (+ end size-offset
))))
1284 ;; Trivial cases where current item isn't split in
1285 ;; two. Just shift every item after new one by
1287 ((or beforep
(not split-line-p
))
1288 (setcar e
(+ p item-size
))
1289 (setcar (nthcdr 6 e
) (+ end item-size
)))
1290 ;; Item is split in two: elements before POS are just
1291 ;; shifted by ITEM-SIZE. In the case item would end
1292 ;; after split POS, ending is only shifted by
1295 (setcar e
(+ p item-size
))
1297 (setcar (nthcdr 6 e
) (+ end item-size
))
1298 (setcar (nthcdr 6 e
) (+ end size-offset
))))
1299 ;; Elements after POS are moved into new item.
1300 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1301 ;; doesn't appear in buffer yet.
1303 (setcar e
(+ p size-offset
(- item pos
(length item-sep
))))
1304 (if (= end item-end
)
1305 (setcar (nthcdr 6 e
) (+ item item-size
))
1306 (setcar (nthcdr 6 e
)
1308 (- item pos
(length item-sep
))))))
1309 ;; Elements at ITEM-END or after are only shifted by
1311 (t (setcar e
(+ p size-offset
))
1312 (setcar (nthcdr 6 e
) (+ end size-offset
))))))
1314 (push (list item ind bullet nil box nil
(+ item item-size
)) struct
)
1315 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1316 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1317 ;; exchange ITEM with the next item in list. Position cursor
1318 ;; after bullet, counter, checkbox, and label.
1321 (setq struct
(org-list-swap-items item
(+ item item-size
) struct
))
1322 (goto-char (org-list-get-next-item
1323 item struct
(org-list-prevs-alist struct
))))
1326 (defun org-list-delete-item (item struct
)
1327 "Remove ITEM from the list and return the new structure.
1329 STRUCT is the list structure."
1330 (let* ((end (org-list-get-item-end item struct
))
1331 (beg (if (= (org-list-get-bottom-point struct
) end
)
1332 ;; If ITEM ends with the list, delete blank lines
1336 (skip-chars-backward " \r\t\n")
1337 (min (1+ (point-at-eol)) (point-max)))
1339 ;; Remove item from buffer.
1340 (delete-region beg end
)
1341 ;; Remove item from structure and shift others items accordingly.
1342 ;; Don't forget to shift also ending position when appropriate.
1343 (let ((size (- end beg
)))
1344 (delq nil
(mapcar (lambda (e)
1345 (let ((pos (car e
)))
1348 (let ((end-e (nth 6 e
)))
1352 (append (butlast e
) (list beg
)))
1354 (append (butlast e
) (list (- end-e size
)))))))
1358 (append (butlast (cdr e
))
1359 (list (- (nth 6 e
) size
))))))))
1362 (defun org-list-send-item (item dest struct
)
1363 "Send ITEM to destination DEST.
1365 STRUCT is the list structure.
1367 DEST can have various values.
1369 If DEST is a buffer position, the function will assume it points
1370 to another item in the same list as ITEM, and will move the
1371 latter just before the former.
1373 If DEST is `begin' \(resp. `end'\), ITEM will be moved at the
1374 beginning \(resp. end\) of the list it belongs to.
1376 If DEST is a string like \"N\", where N is an integer, ITEM will
1377 be moved at the Nth position in the list.
1379 If DEST is `kill', ITEM will be deleted and its body will be
1380 added to the kill-ring.
1382 If DEST is `delete', ITEM will be deleted.
1384 This function returns, destructively, the new list structure."
1385 (let* ((prevs (org-list-prevs-alist struct
))
1386 (item-end (org-list-get-item-end item struct
))
1387 ;; Grab full item body minus its bullet.
1394 (regexp-quote (org-list-get-bullet item struct
))))
1397 ;; Change DEST into a buffer position. A trick is needed
1398 ;; when ITEM is meant to be sent at the end of the list.
1399 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1400 ;; nil and insertion point (INS-POINT) to the first line's
1401 ;; end of the last item, we ensure the new item will be
1402 ;; inserted after the last item, and not after any of its
1403 ;; hypothetical sub-items.
1405 ((or (eq dest
'kill
) (eq dest
'delete
)))
1407 (setq dest
(org-list-get-list-begin item struct prevs
)))
1409 (setq dest
(org-list-get-list-end item struct prevs
))
1411 (goto-char (org-list-get-last-item item struct prevs
))
1413 ((string-match "\\`[0-9]+\\'" dest
)
1414 (let* ((all (org-list-get-all-items item struct prevs
))
1416 (index (mod (string-to-number dest
) len
)))
1417 (if (not (zerop index
))
1418 (setq dest
(nth (1- index
) all
))
1419 ;; Send ITEM at the end of the list.
1420 (setq dest
(org-list-get-list-end item struct prevs
))
1423 (org-list-get-last-item item struct prevs
))
1426 (org-M-RET-may-split-line nil
))
1428 ((eq dest
'delete
) (org-list-delete-item item struct
))
1431 (org-list-delete-item item struct
))
1432 ((and (integerp dest
) (/= item ins-point
))
1433 (setq item
(copy-marker item
))
1434 (setq struct
(org-list-insert-item ins-point struct prevs nil body
))
1435 ;; 1. Structure returned by `org-list-insert-item' may not be
1436 ;; accurate, as it cannot see sub-items included in BODY.
1437 ;; Thus, first compute the real structure so far.
1439 (cons (marker-position item
)
1440 (org-list-get-subtree (marker-position item
) struct
)))
1441 (new-end (org-list-get-item-end (point) struct
))
1442 (old-end (org-list-get-item-end (marker-position item
) struct
))
1444 (shift (- (point) item
)))
1445 ;; 1.1. Remove the item just created in structure.
1446 (setq struct
(delete (assq new-item struct
) struct
))
1447 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1452 (let* ((cell (assq e struct
))
1456 (append (butlast (cdr cell
))
1457 (list (if (= end old-end
)
1461 (lambda (e1 e2
) (< (car e1
) (car e2
))))))
1462 ;; 2. Eventually delete extra copy of the item and clean marker.
1464 (org-list-delete-item (marker-position item
) struct
)
1465 (move-marker item nil
)))
1468 (defun org-list-struct-outdent (start end struct parents
)
1469 "Outdent items between positions START and END.
1471 STRUCT is the list structure. PARENTS is the alist of items'
1472 parents, as returned by `org-list-parents-alist'.
1474 START is included, END excluded."
1477 (let* ((item (car cell
))
1478 (parent (cdr cell
)))
1480 ;; Item not yet in zone: keep association.
1481 ((< item start
) cell
)
1482 ;; Item out of zone: follow associations in ACC.
1484 (let ((convert (and parent
(assq parent acc
))))
1485 (if convert
(cons item
(cdr convert
)) cell
)))
1486 ;; Item has no parent: error
1488 (error "Cannot outdent top-level items"))
1489 ;; Parent is outdented: keep association.
1491 (push (cons parent item
) acc
) cell
)
1493 ;; Parent isn't outdented: reparent to grand-parent.
1494 (let ((grand-parent (org-list-get-parent
1495 parent struct parents
)))
1496 (push (cons parent item
) acc
)
1497 (cons item grand-parent
))))))))
1498 (mapcar out parents
)))
1500 (defun org-list-struct-indent (start end struct parents prevs
)
1501 "Indent items between positions START and END.
1503 STRUCT is the list structure. PARENTS is the alist of parents
1504 and PREVS is the alist of previous items, returned by,
1505 respectively, `org-list-parents-alist' and
1506 `org-list-prevs-alist'.
1508 START is included and END excluded.
1510 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1511 bullets between START and END."
1513 (set-assoc (lambda (cell) (push cell acc
) cell
))
1514 (change-bullet-maybe
1517 (let* ((bul (org-trim (org-list-get-bullet item struct
)))
1518 (new-bul-p (cdr (assoc bul org-list-demote-modify-bullet
))))
1519 (when new-bul-p
(org-list-set-bullet item struct new-bul-p
))))))
1522 (let* ((item (car cell
))
1523 (parent (cdr cell
)))
1525 ;; Item not yet in zone: keep association.
1526 ((< item start
) cell
)
1528 ;; Item out of zone: follow associations in ACC.
1529 (let ((convert (assq parent acc
)))
1530 (if convert
(cons item
(cdr convert
)) cell
)))
1532 ;; Item is in zone...
1533 (let ((prev (org-list-get-prev-item item struct prevs
)))
1534 ;; Check if bullet needs to be changed.
1535 (funcall change-bullet-maybe item
)
1537 ;; First item indented but not parent: error
1538 ((and (not prev
) (< parent start
))
1539 (error "Cannot indent the first item of a list"))
1540 ;; First item and parent indented: keep same
1542 ((not prev
) (funcall set-assoc cell
))
1543 ;; Previous item not indented: reparent to it.
1544 ((< prev start
) (funcall set-assoc
(cons item prev
)))
1545 ;; Previous item indented: reparent like it.
1548 (cons item
(cdr (assq prev acc
)))))))))))))
1549 (mapcar ind parents
)))
1552 ;;; Repairing structures
1554 (defun org-list-use-alpha-bul-p (first struct prevs
)
1555 "Non-nil if list starting at FIRST can have alphabetical bullets.
1557 STRUCT is list structure. PREVS is the alist of previous items,
1558 as returned by `org-list-prevs-alist'."
1559 (and org-alphabetical-lists
1561 (let ((item first
) (ascii 64) (case-fold-search nil
))
1562 ;; Pretend that bullets are uppercase and check if alphabet
1563 ;; is sufficient, taking counters into account.
1565 (let ((bul (org-list-get-bullet item struct
))
1566 (count (org-list-get-counter item struct
)))
1567 ;; Virtually determine current bullet
1568 (if (and count
(string-match "[a-zA-Z]" count
))
1569 ;; Counters are not case-sensitive.
1570 (setq ascii
(string-to-char (upcase count
)))
1571 (setq ascii
(1+ ascii
)))
1572 ;; Test if bullet would be over z or Z.
1575 (setq item
(org-list-get-next-item item struct prevs
)))))
1576 ;; All items checked. All good.
1579 (defun org-list-inc-bullet-maybe (bullet)
1580 "Increment BULLET if applicable."
1581 (let ((case-fold-search nil
))
1583 ;; Num bullet: increment it.
1584 ((string-match "[0-9]+" bullet
)
1586 (number-to-string (1+ (string-to-number (match-string 0 bullet
))))
1588 ;; Alpha bullet: increment it.
1589 ((string-match "[A-Za-z]" bullet
)
1591 (char-to-string (1+ (string-to-char (match-string 0 bullet
))))
1593 ;; Unordered bullet: leave it.
1596 (defun org-list-struct-fix-bul (struct prevs
)
1597 "Verify and correct bullets in STRUCT.
1598 PREVS is the alist of previous items, as returned by
1599 `org-list-prevs-alist'.
1601 This function modifies STRUCT."
1602 (let ((case-fold-search nil
)
1605 ;; Set bullet of ITEM in STRUCT, depending on the type of
1606 ;; first item of the list, the previous bullet and counter
1609 (let* ((prev (org-list-get-prev-item item struct prevs
))
1610 (prev-bul (and prev
(org-list-get-bullet prev struct
)))
1611 (counter (org-list-get-counter item struct
))
1612 (bullet (org-list-get-bullet item struct
))
1613 (alphap (and (not prev
)
1614 (org-list-use-alpha-bul-p item struct prevs
))))
1615 (org-list-set-bullet
1617 (org-list-bullet-string
1619 ;; Alpha counter in alpha list: use counter.
1621 (string-match "[a-zA-Z]" counter
)
1622 (string-match "[a-zA-Z]" prev-bul
))
1623 ;; Use cond to be sure `string-match' is used in
1627 ((string-match "[a-z]" prev-bul
) (downcase counter
))
1628 ((string-match "[A-Z]" prev-bul
) (upcase counter
)))))
1629 (replace-match real-count nil nil prev-bul
)))
1630 ;; Num counter in a num list: use counter.
1632 (string-match "[0-9]+" counter
)
1633 (string-match "[0-9]+" prev-bul
))
1634 (replace-match counter nil nil prev-bul
))
1635 ;; No counter: increase, if needed, previous bullet.
1637 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct
)))
1638 ;; Alpha counter at first item: use counter.
1639 ((and counter
(org-list-use-alpha-bul-p item struct prevs
)
1640 (string-match "[A-Za-z]" counter
)
1641 (string-match "[A-Za-z]" bullet
))
1644 ((string-match "[a-z]" bullet
) (downcase counter
))
1645 ((string-match "[A-Z]" bullet
) (upcase counter
)))))
1646 (replace-match real-count nil nil bullet
)))
1647 ;; Num counter at first item: use counter.
1649 (string-match "[0-9]+" counter
)
1650 (string-match "[0-9]+" bullet
))
1651 (replace-match counter nil nil bullet
))
1652 ;; First bullet is alpha uppercase: use "A".
1653 ((and alphap
(string-match "[A-Z]" bullet
))
1654 (replace-match "A" nil nil bullet
))
1655 ;; First bullet is alpha lowercase: use "a".
1656 ((and alphap
(string-match "[a-z]" bullet
))
1657 (replace-match "a" nil nil bullet
))
1658 ;; First bullet is num: use "1".
1659 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet
)
1660 (replace-match "1" nil nil bullet
))
1661 ;; Not an ordered list: keep bullet.
1663 (mapc fix-bul
(mapcar 'car struct
))))
1665 (defun org-list-struct-fix-ind (struct parents
&optional bullet-size
)
1666 "Verify and correct indentation in STRUCT.
1668 PARENTS is the alist of parents, as returned by
1669 `org-list-parents-alist'.
1671 If numeric optional argument BULLET-SIZE is set, assume all
1672 bullets in list have this length to determine new indentation.
1674 This function modifies STRUCT."
1675 (let* ((ancestor (org-list-get-top-point struct
))
1676 (top-ind (org-list-get-ind ancestor struct
))
1679 (let ((parent (org-list-get-parent item struct parents
)))
1681 ;; Indent like parent + length of parent's bullet +
1684 item struct
(+ (or bullet-size
1686 (org-list-get-bullet parent struct
)))
1687 (org-list-get-ind parent struct
)
1688 org-list-indent-offset
))
1689 ;; If no parent, indent like top-point.
1690 (org-list-set-ind item struct top-ind
))))))
1691 (mapc new-ind
(mapcar 'car
(cdr struct
)))))
1693 (defun org-list-struct-fix-box (struct parents prevs
&optional ordered
)
1694 "Verify and correct checkboxes in STRUCT.
1696 PARENTS is the alist of parents and PREVS is the alist of
1697 previous items, as returned by, respectively,
1698 `org-list-parents-alist' and `org-list-prevs-alist'.
1700 If ORDERED is non-nil, a checkbox can only be checked when every
1701 checkbox before it is checked too. If there was an attempt to
1702 break this rule, the function will return the blocking item. In
1703 all others cases, the return value will be nil.
1705 This function modifies STRUCT."
1706 (let ((all-items (mapcar 'car struct
))
1711 (mapcar (lambda (child)
1712 (org-list-get-checkbox child struct
))
1713 (org-list-get-children item struct parents
))))
1714 (org-list-set-checkbox
1717 ((and (member "[ ]" box-list
) (member "[X]" box-list
)) "[-]")
1718 ((member "[-]" box-list
) "[-]")
1719 ((member "[X]" box-list
) "[X]")
1720 ((member "[ ]" box-list
) "[ ]")
1721 ;; Parent has no boxed child: leave box as-is.
1722 (t (org-list-get-checkbox item struct
))))))))
1724 ;; 1. List all parents with a checkbox.
1727 (let* ((parent (org-list-get-parent e struct parents
))
1728 (parent-box-p (org-list-get-checkbox parent struct
)))
1729 (when (and parent-box-p
(not (memq parent parent-list
)))
1730 (push parent parent-list
))))
1732 ;; 2. Sort those parents by decreasing indentation.
1733 (setq parent-list
(sort parent-list
1735 (> (org-list-get-ind e1 struct
)
1736 (org-list-get-ind e2 struct
)))))
1737 ;; 3. For each parent, get all children's checkboxes to determine
1738 ;; and set its checkbox accordingly.
1739 (mapc set-parent-box parent-list
)
1740 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1743 (mapcar (lambda (e) (org-list-get-checkbox e struct
)) all-items
))
1744 (after-unchecked (member "[ ]" box-list
)))
1745 ;; There are boxes checked after an unchecked one: fix that.
1746 (when (member "[X]" after-unchecked
)
1747 (let ((index (- (length struct
) (length after-unchecked
))))
1748 (mapc (lambda (e) (org-list-set-checkbox e struct
"[ ]"))
1749 (nthcdr index all-items
))
1750 ;; Verify once again the structure, without ORDERED.
1751 (org-list-struct-fix-box struct parents prevs nil
)
1752 ;; Return blocking item.
1753 (nth index all-items
)))))))
1755 (defun org-list-struct-apply-struct (struct old-struct
)
1756 "Apply set-difference between STRUCT and OLD-STRUCT to the buffer.
1758 OLD-STRUCT is the structure before any modifications, and STRUCT
1759 the structure to be applied. The function will only modify parts
1760 of the list which have changed.
1762 Initial position of cursor is restored after the changes."
1763 (let* ((origin (copy-marker (point)))
1764 (inlinetask-re (and (featurep 'org-inlinetask
)
1765 (org-inlinetask-outline-regexp)))
1766 (item-re (org-item-re))
1767 (box-rule-p (cdr (assq 'checkbox org-list-automatic-rules
)))
1770 ;; Shift the indentation between END and BEG by DELTA.
1771 ;; Start from the line before END.
1772 (lambda (end beg delta
)
1774 (skip-chars-backward " \r\t\n")
1776 (while (or (> (point) beg
)
1777 (and (= (point) beg
)
1778 (not (looking-at item-re
))))
1780 ;; Skip inline tasks.
1781 ((and inlinetask-re
(looking-at inlinetask-re
))
1782 (org-inlinetask-goto-beginning))
1783 ;; Shift only non-empty lines.
1784 ((org-looking-at-p "^[ \t]*\\S-")
1785 (let ((i (org-get-indentation)))
1786 (org-indent-line-to (+ i delta
)))))
1787 (forward-line -
1)))))
1790 ;; Replace ITEM first line elements with new elements from
1791 ;; STRUCT, if appropriate.
1794 (let* ((new-ind (org-list-get-ind item struct
))
1795 (old-ind (org-get-indentation))
1796 (new-bul (org-list-bullet-string
1797 (org-list-get-bullet item struct
)))
1798 (old-bul (org-list-get-bullet item old-struct
))
1799 (new-box (org-list-get-checkbox item struct
)))
1800 (looking-at org-list-full-item-re
)
1801 ;; a. Replace bullet
1802 (unless (equal old-bul new-bul
)
1803 (replace-match new-bul nil nil nil
1))
1804 ;; b. Replace checkbox.
1806 ((and new-box box-rule-p
1807 (save-match-data (org-at-item-description-p)))
1808 (message "Cannot add a checkbox to a description list item"))
1809 ((equal (match-string 3) new-box
))
1810 ((and (match-string 3) new-box
)
1811 (replace-match new-box nil nil nil
3))
1813 (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
1814 (replace-match "" nil nil nil
1))
1815 (t (let ((counterp (match-end 2)))
1816 (goto-char (if counterp
(1+ counterp
) (match-end 1)))
1817 (insert (concat new-box
(unless counterp
" "))))))
1818 ;; c. Indent item to appropriate column.
1819 (unless (= new-ind old-ind
)
1820 (delete-region (goto-char (point-at-bol))
1821 (progn (skip-chars-forward " \t") (point)))
1822 (indent-to new-ind
)))))))
1823 ;; 1. First get list of items and position endings. We maintain
1824 ;; two alists: ITM-SHIFT, determining indentation shift needed
1825 ;; at item, and END-POS, a pseudo-alist where key is ending
1826 ;; position and value point.
1827 (let (end-list acc-end itm-shift all-ends sliced-struct
)
1829 (let* ((pos (car e
))
1830 (ind-pos (org-list-get-ind pos struct
))
1831 (ind-old (org-list-get-ind pos old-struct
))
1832 (bul-pos (org-list-get-bullet pos struct
))
1833 (bul-old (org-list-get-bullet pos old-struct
))
1834 (ind-shift (- (+ ind-pos
(length bul-pos
))
1835 (+ ind-old
(length bul-old
))))
1836 (end-pos (org-list-get-item-end pos old-struct
)))
1837 (push (cons pos ind-shift
) itm-shift
)
1838 (unless (assq end-pos old-struct
)
1839 ;; To determine real ind of an ending position that
1840 ;; is not at an item, we have to find the item it
1841 ;; belongs to: it is the last item (ITEM-UP), whose
1842 ;; ending is further than the position we're
1844 (let ((item-up (assoc-default end-pos acc-end
'>)))
1845 (push (cons end-pos item-up
) end-list
)))
1846 (push (cons end-pos pos
) acc-end
)))
1848 ;; 2. Slice the items into parts that should be shifted by the
1849 ;; same amount of indentation. The slices are returned in
1850 ;; reverse order so changes modifying buffer do not change
1851 ;; positions they refer to.
1852 (setq all-ends
(sort (append (mapcar 'car itm-shift
)
1853 (org-uniquify (mapcar 'car end-list
)))
1855 (while (cdr all-ends
)
1856 (let* ((up (pop all-ends
))
1857 (down (car all-ends
))
1858 (ind (if (assq up struct
)
1859 (cdr (assq up itm-shift
))
1860 (cdr (assq (cdr (assq up end-list
)) itm-shift
)))))
1861 (push (list down up ind
) sliced-struct
)))
1862 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1863 ;; end to beginning. Take a special action when beginning is
1866 (unless (zerop (nth 2 e
)) (apply shift-body-ind e
))
1867 (let* ((beg (nth 1 e
))
1868 (cell (assq beg struct
)))
1869 (unless (or (not cell
) (equal cell
(assq beg old-struct
)))
1870 (funcall modify-item beg
))))
1872 ;; 4. Go back to initial position and clean marker.
1874 (move-marker origin nil
)))
1876 (defun org-list-write-struct (struct parents
)
1877 "Correct bullets, checkboxes and indentation in list at point.
1878 STRUCT is the list structure. PARENTS is the alist of parents,
1879 as returned by `org-list-parents-alist'."
1880 ;; Order of functions matters here: checkboxes and endings need
1881 ;; correct indentation to be set, and indentation needs correct
1884 ;; 0. Save a copy of structure before modifications
1885 (let ((old-struct (copy-tree struct
)))
1886 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
1887 ;; order to get items endings and bullets properly
1888 (org-list-struct-fix-ind struct parents
2)
1889 ;; 2. Get pseudo-alist of ending positions and sort it by position.
1890 ;; Then associate them to the structure.
1891 (let (end-list acc-end
)
1893 (let* ((pos (car e
))
1894 (ind-pos (org-list-get-ind pos struct
))
1895 (end-pos (org-list-get-item-end pos struct
)))
1896 (unless (assq end-pos struct
)
1897 ;; To determine real ind of an ending position that is
1898 ;; not at an item, we have to find the item it belongs
1899 ;; to: it is the last item (ITEM-UP), whose ending is
1900 ;; further than the position we're interested in.
1901 (let ((item-up (assoc-default end-pos acc-end
'>)))
1903 ;; Else part is for the bottom point.
1904 (if item-up
(+ (org-list-get-ind item-up struct
) 2) 0)
1907 (push (cons ind-pos pos
) end-list
)
1908 (push (cons end-pos pos
) acc-end
)))
1910 (setq end-list
(sort end-list
(lambda (e1 e2
) (< (cdr e1
) (cdr e2
)))))
1911 (org-list-struct-assoc-end struct end-list
))
1912 ;; 3. Get bullets right.
1913 (let ((prevs (org-list-prevs-alist struct
)))
1914 (org-list-struct-fix-bul struct prevs
)
1915 ;; 4. Now get real indentation.
1916 (org-list-struct-fix-ind struct parents
)
1917 ;; 5. Eventually fix checkboxes.
1918 (org-list-struct-fix-box struct parents prevs
))
1919 ;; 6. Apply structure modifications to buffer.
1920 (org-list-struct-apply-struct struct old-struct
)))
1925 (defun org-apply-on-list (function init-value
&rest args
)
1926 "Call FUNCTION on each item of the list at point.
1927 FUNCTION must be called with at least one argument: INIT-VALUE,
1928 that will contain the value returned by the function at the
1929 previous item, plus ARGS extra arguments.
1931 FUNCTION is applied on items in reverse order.
1933 As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
1934 will return the number of items in the current list.
1936 Sublists of the list are skipped. Cursor is always at the
1937 beginning of the item."
1938 (let* ((struct (org-list-struct))
1939 (prevs (org-list-prevs-alist struct
))
1940 (item (copy-marker (point-at-bol)))
1941 (all (org-list-get-all-items (marker-position item
) struct prevs
))
1945 (setq value
(apply function value args
)))
1948 (move-marker item nil
)
1951 (defun org-list-set-item-visibility (item struct view
)
1952 "Set visibility of ITEM in STRUCT to VIEW.
1954 Possible values are: `folded', `children' or `subtree'. See
1955 `org-cycle' for more information."
1958 (let ((item-end (org-list-get-item-end-before-blank item struct
)))
1960 (outline-flag-region (save-excursion (goto-char item
) (point-at-eol))
1962 ((eq view
'children
)
1963 ;; First show everything.
1964 (org-list-set-item-visibility item struct
'subtree
)
1965 ;; Then fold every child.
1966 (let* ((parents (org-list-parents-alist struct
))
1967 (children (org-list-get-children item struct parents
)))
1969 (org-list-set-item-visibility e struct
'folded
))
1973 (let ((item-end (org-list-get-item-end item struct
)))
1974 (outline-flag-region item item-end nil
)))))
1976 (defun org-list-item-body-column (item)
1977 "Return column at which body of ITEM should start."
1978 (let (bpos bcol tpos tcol
)
1981 (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?[ \t]+")
1982 (setq bpos
(match-beginning 1) tpos
(match-end 0)
1983 bcol
(progn (goto-char bpos
) (current-column))
1984 tcol
(progn (goto-char tpos
) (current-column)))
1985 (when (> tcol
(+ bcol org-description-max-indent
))
1986 (setq tcol
(+ bcol
5))))
1990 ;;; Interactive functions
1992 (defalias 'org-list-get-item-begin
'org-in-item-p
)
1994 (defun org-beginning-of-item ()
1995 "Go to the beginning of the current item.
1996 Throw an error when not in a list."
1998 (let ((begin (org-in-item-p)))
1999 (if begin
(goto-char begin
) (error "Not in an item"))))
2001 (defun org-beginning-of-item-list ()
2002 "Go to the beginning item of the current list or sublist.
2003 Throw an error when not in a list."
2005 (let ((begin (org-in-item-p)))
2007 (error "Not in an item")
2009 (let* ((struct (org-list-struct))
2010 (prevs (org-list-prevs-alist struct
)))
2011 (goto-char (org-list-get-list-begin begin struct prevs
))))))
2013 (defun org-end-of-item-list ()
2014 "Go to the end of the current list or sublist.
2015 Throw an error when not in a list."
2017 (let ((begin (org-in-item-p)))
2019 (error "Not in an item")
2021 (let* ((struct (org-list-struct))
2022 (prevs (org-list-prevs-alist struct
)))
2023 (goto-char (org-list-get-list-end begin struct prevs
))))))
2025 (defun org-end-of-item ()
2026 "Go to the end of the current item.
2027 Throw an error when not in a list."
2029 (let ((begin (org-in-item-p)))
2031 (error "Not in an item")
2033 (let ((struct (org-list-struct)))
2034 (goto-char (org-list-get-item-end begin struct
))))))
2036 (defun org-previous-item ()
2037 "Move to the beginning of the previous item.
2038 Throw an error when not in a list. Also throw an error when at
2039 first item, unless `org-list-use-circular-motion' is non-nil."
2041 (let ((item (org-in-item-p)))
2043 (error "Not in an item")
2045 (let* ((struct (org-list-struct))
2046 (prevs (org-list-prevs-alist struct
))
2047 (prevp (org-list-get-prev-item item struct prevs
)))
2049 (prevp (goto-char prevp
))
2050 (org-list-use-circular-motion
2051 (goto-char (org-list-get-last-item item struct prevs
)))
2052 (t (error "On first item")))))))
2054 (defun org-next-item ()
2055 "Move to the beginning of the next item.
2056 Throw an error when not in a list. Also throw an error when at
2057 last 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-next-item item struct prevs
)))
2067 (prevp (goto-char prevp
))
2068 (org-list-use-circular-motion
2069 (goto-char (org-list-get-first-item item struct prevs
)))
2070 (t (error "On last item")))))))
2072 (defun org-move-item-down ()
2073 "Move the item at point down, i.e. swap with following item.
2074 Sub-items (items with larger indentation) are considered part of
2075 the item, so this really moves item trees."
2077 (unless (org-at-item-p) (error "Not at an item"))
2078 (let* ((col (current-column))
2079 (item (point-at-bol))
2080 (struct (org-list-struct))
2081 (prevs (org-list-prevs-alist struct
))
2082 (next-item (org-list-get-next-item (point-at-bol) struct prevs
)))
2083 (unless (or next-item org-list-use-circular-motion
)
2084 (error "Cannot move this item further down"))
2086 (setq struct
(org-list-send-item item
'begin struct
))
2087 (setq struct
(org-list-swap-items item next-item struct
))
2089 (org-list-get-next-item item struct
(org-list-prevs-alist struct
))))
2090 (org-list-write-struct struct
(org-list-parents-alist struct
))
2091 (org-move-to-column col
)))
2093 (defun org-move-item-up ()
2094 "Move the item at point up, i.e. swap with previous item.
2095 Sub-items (items with larger indentation) are considered part of
2096 the item, so this really moves item trees."
2098 (unless (org-at-item-p) (error "Not at an item"))
2099 (let* ((col (current-column))
2100 (item (point-at-bol))
2101 (struct (org-list-struct))
2102 (prevs (org-list-prevs-alist struct
))
2103 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs
)))
2104 (unless (or prev-item org-list-use-circular-motion
)
2105 (error "Cannot move this item further up"))
2107 (setq struct
(org-list-send-item item
'end struct
))
2108 (setq struct
(org-list-swap-items prev-item item struct
)))
2109 (org-list-write-struct struct
(org-list-parents-alist struct
))
2110 (org-move-to-column col
)))
2112 (defun org-insert-item (&optional checkbox
)
2113 "Insert a new item at the current level.
2114 If cursor is before first character after bullet of the item, the
2115 new item will be created before the current one.
2117 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2119 Return t when things worked, nil when we are not in an item, or
2121 (let ((itemp (org-in-item-p))
2123 ;; If cursor isn't is a list or if list is invisible, return nil.
2124 (unless (or (not itemp
)
2127 (outline-invisible-p)))
2130 (org-at-item-timer-p))
2131 ;; Timer list: delegate to `org-timer-item'.
2132 (progn (org-timer-item) t
)
2133 (let* ((struct (save-excursion (goto-char itemp
)
2135 (prevs (org-list-prevs-alist struct
))
2136 ;; If we're in a description list, ask for the new term.
2137 (desc (when (org-list-get-tag itemp struct
)
2138 (concat (read-string "Term: ") " :: ")))
2139 ;; Don't insert a checkbox if checkbox rule is applied
2140 ;; and it is a description item.
2141 (checkp (and checkbox
2143 (not (cdr (assq 'checkbox
2144 org-list-automatic-rules
)))))))
2146 (org-list-insert-item pos struct prevs checkp desc
))
2147 (org-list-write-struct struct
(org-list-parents-alist struct
))
2148 (when checkp
(org-update-checkbox-count-maybe))
2149 (looking-at org-list-full-item-re
)
2150 (goto-char (match-end 0))
2153 (defun org-list-repair ()
2154 "Fix indentation, bullets and checkboxes is the list at point."
2156 (unless (org-at-item-p) (error "This is not a list"))
2157 (let* ((struct (org-list-struct))
2158 (parents (org-list-parents-alist struct
)))
2159 (org-list-write-struct struct parents
)))
2161 (defun org-cycle-list-bullet (&optional which
)
2162 "Cycle through the different itemize/enumerate bullets.
2163 This cycle the entire list level through the sequence:
2165 `-' -> `+' -> `*' -> `1.' -> `1)'
2167 If WHICH is a valid string, use that as the new bullet. If WHICH
2168 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2169 `previous', cycle backwards."
2171 (unless (org-at-item-p) (error "Not at an item"))
2174 (let* ((struct (org-list-struct))
2175 (parents (org-list-parents-alist struct
))
2176 (prevs (org-list-prevs-alist struct
))
2177 (list-beg (org-list-get-first-item (point) struct prevs
))
2178 (bullet (org-list-get-bullet list-beg struct
))
2179 (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules
)))
2180 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs
))
2181 (case-fold-search nil
)
2183 ((string-match "[a-z]\\." bullet
) "a.")
2184 ((string-match "[a-z])" bullet
) "a)")
2185 ((string-match "[A-Z]\\." bullet
) "A.")
2186 ((string-match "[A-Z])" bullet
) "A)")
2187 ((string-match "\\." bullet
) "1.")
2188 ((string-match ")" bullet
) "1)")
2189 (t (org-trim bullet
))))
2190 ;; Compute list of possible bullets, depending on context.
2193 ;; *-bullets are not allowed at column 0.
2194 (unless (and bullet-rule-p
2195 (looking-at "\\S-")) '("*"))
2196 ;; Description items cannot be numbered.
2197 (unless (or (eq org-plain-list-ordered-item-terminator ?\
))
2198 (and bullet-rule-p
(org-at-item-description-p)))
2200 (unless (or (eq org-plain-list-ordered-item-terminator ?.
)
2201 (and bullet-rule-p
(org-at-item-description-p)))
2203 (unless (or (not alpha-p
)
2204 (eq org-plain-list-ordered-item-terminator ?\
))
2205 (and bullet-rule-p
(org-at-item-description-p)))
2207 (unless (or (not alpha-p
)
2208 (eq org-plain-list-ordered-item-terminator ?.
)
2209 (and bullet-rule-p
(org-at-item-description-p)))
2211 (len (length bullet-list
))
2212 (item-index (- len
(length (member current bullet-list
))))
2213 (get-value (lambda (index) (nth (mod index len
) bullet-list
)))
2215 ((member which bullet-list
) which
)
2216 ((numberp which
) (funcall get-value which
))
2217 ((eq 'previous which
) (funcall get-value
(1- item-index
)))
2218 (t (funcall get-value
(1+ item-index
))))))
2219 ;; Use a short variation of `org-list-write-struct' as there's
2220 ;; no need to go through all the steps.
2221 (let ((old-struct (copy-tree struct
)))
2222 (org-list-set-bullet list-beg struct
(org-list-bullet-string new
))
2223 (org-list-struct-fix-bul struct prevs
)
2224 (org-list-struct-fix-ind struct parents
)
2225 (org-list-struct-apply-struct struct old-struct
)))))
2227 (defun org-toggle-checkbox (&optional toggle-presence
)
2228 "Toggle the checkbox in the current line.
2229 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2230 double prefix, set checkbox to [-].
2232 When there is an active region, toggle status or presence of the
2233 first checkbox there, and make every item inside have the same
2234 status or presence, respectively.
2236 If the cursor is in a headline, apply this to all checkbox items
2237 in the text below the heading, taking as reference the first item
2238 in subtree, ignoring drawers."
2245 (drawer-re (concat "^[ \t]*:\\("
2246 (mapconcat 'regexp-quote org-drawers
"\\|")
2248 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2249 "\\|" org-deadline-string
2250 "\\|" org-closed-string
2251 "\\|" org-clock-string
"\\)"
2252 " *[[<]\\([^]>]+\\)[]>]"))
2253 (orderedp (org-entry-get nil
"ORDERED"))
2255 ;; In a region, start at first item in region.
2257 ((org-region-active-p)
2258 (let ((limit (region-end)))
2259 (goto-char (region-beginning))
2260 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2261 (setq lim-up
(point-at-bol))
2262 (error "No item in region"))
2263 (setq lim-down
(copy-marker limit
))))
2265 ;; On an heading, start at first item after drawers and
2266 ;; time-stamps (scheduled, etc.).
2267 (let ((limit (save-excursion (outline-next-heading) (point))))
2269 (while (or (looking-at drawer-re
) (looking-at keyword-re
))
2270 (if (looking-at keyword-re
)
2272 (re-search-forward "^[ \t]*:END:" limit nil
)))
2273 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2274 (setq lim-up
(point-at-bol))
2275 (error "No item in subtree"))
2276 (setq lim-down
(copy-marker limit
))))
2277 ;; Just one item: set SINGLEP flag.
2280 (setq lim-up
(point-at-bol)
2281 lim-down
(copy-marker (point-at-eol))))
2282 (t (error "Not at an item or heading, and no active region"))))
2283 ;; Determine the checkbox going to be applied to all items
2288 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2290 ((equal toggle-presence
'(16)) "[-]")
2291 ((equal toggle-presence
'(4))
2292 (unless cbox
"[ ]"))
2293 ((equal "[X]" cbox
) "[ ]")
2295 ;; When an item is found within bounds, grab the full list at
2296 ;; point structure, then: (1) set check-box of all its items
2297 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2298 ;; whole list, (3) move point after the list.
2300 (while (and (< (point) lim-down
)
2301 (org-list-search-forward (org-item-beginning-re)
2303 (let* ((struct (org-list-struct))
2304 (struct-copy (copy-tree struct
))
2305 (parents (org-list-parents-alist struct
))
2306 (prevs (org-list-prevs-alist struct
))
2307 (bottom (copy-marker (org-list-get-bottom-point struct
)))
2308 (items-to-toggle (org-remove-if
2309 (lambda (e) (or (< e lim-up
) (> e lim-down
)))
2310 (mapcar 'car struct
))))
2311 (mapc (lambda (e) (org-list-set-checkbox
2313 ;; If there is no box at item, leave as-is
2314 ;; unless function was called with C-u prefix.
2315 (let ((cur-box (org-list-get-checkbox e struct
)))
2316 (if (or cur-box
(equal toggle-presence
'(4)))
2320 (setq block-item
(org-list-struct-fix-box
2321 struct parents prevs orderedp
))
2322 ;; Report some problems due to ORDERED status of subtree.
2323 ;; If only one box was being checked, throw an error, else,
2324 ;; only signal problems.
2326 ((and singlep block-item
(> lim-up block-item
))
2328 "Checkbox blocked because of unchecked box at line %d"
2329 (org-current-line block-item
)))
2332 "Checkboxes were removed due to unchecked box at line %d"
2333 (org-current-line block-item
))))
2335 (move-marker bottom nil
)
2336 (org-list-struct-apply-struct struct struct-copy
)))
2337 (move-marker lim-down nil
)))
2338 (org-update-checkbox-count-maybe))
2340 (defun org-reset-checkbox-state-subtree ()
2341 "Reset all checkboxes in an entry subtree."
2343 (if (org-before-first-heading-p)
2344 (error "Not inside a tree")
2347 (org-narrow-to-subtree)
2349 (goto-char (point-min))
2350 (let ((end (point-max)))
2351 (while (< (point) end
)
2352 (when (org-at-item-checkbox-p)
2353 (replace-match "[ ]" t t nil
1))
2354 (beginning-of-line 2)))
2355 (org-update-checkbox-count-maybe 'all
)))))
2357 (defun org-update-checkbox-count (&optional all
)
2358 "Update the checkbox statistics in the current section.
2359 This will find all statistic cookies like [57%] and [6/12] and
2360 update them with the current numbers.
2362 With optional prefix argument ALL, do this for the whole buffer."
2365 (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2366 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2368 (or (not org-hierarchical-checkbox-statistics
)
2369 (string-match "\\<recursive\\>"
2370 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2372 (cons (point-min) (point-max))
2373 (cons (or (ignore-errors (org-back-to-heading t
) (point))
2375 (save-excursion (outline-next-heading) (point)))))
2378 ;; Return number of checked boxes and boxes of all types
2379 ;; in all structures in STRUCTS. If RECURSIVEP is
2380 ;; non-nil, also count boxes in sub-lists. If ITEM is
2381 ;; nil, count across the whole structure, else count only
2382 ;; across subtree whose ancestor is ITEM.
2383 (lambda (item structs recursivep
)
2384 (let ((c-on 0) (c-all 0))
2387 (let* ((pre (org-list-prevs-alist s
))
2388 (par (org-list-parents-alist s
))
2391 ((and recursivep item
) (org-list-get-subtree item s
))
2392 (recursivep (mapcar 'car s
))
2393 (item (org-list-get-children item s par
))
2394 (t (org-list-get-all-items
2395 (org-list-get-top-point s
) s pre
))))
2396 (cookies (delq nil
(mapcar
2398 (org-list-get-checkbox e s
))
2400 (setq c-all
(+ (length cookies
) c-all
)
2401 c-on
(+ (org-count "[X]" cookies
) c-on
))))
2403 (cons c-on c-all
)))))
2405 cookies-list structs-bak box-num
)
2406 (goto-char (car bounds
))
2407 ;; 1. Build an alist for each cookie found within BOUNDS. The
2408 ;; key will be position at beginning of cookie and values
2409 ;; ending position, format of cookie, and a cell whose car is
2410 ;; number of checked boxes to report, and cdr total number of
2412 (while (re-search-forward cookie-re
(cdr bounds
) t
)
2417 (match-beginning 1) ; cookie start
2418 (match-end 1) ; cookie end
2419 (match-string 2) ; percent?
2421 ;; Cookie is at an heading, but specifically for todo,
2422 ;; not for checkboxes: skip it.
2423 ((and (org-on-heading-p)
2424 (string-match "\\<todo\\>"
2426 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2428 ;; Cookie is at an heading, but all lists before next
2429 ;; heading already have been read. Use data collected
2430 ;; in STRUCTS-BAK. This should only happen when
2431 ;; heading has more than one cookie on it.
2432 ((and (org-on-heading-p)
2433 (<= (save-excursion (outline-next-heading) (point))
2435 (funcall count-boxes nil structs-bak recursivep
))
2436 ;; Cookie is at a fresh heading. Grab structure of
2437 ;; every list containing a checkbox between point and
2438 ;; next headline, and save them in STRUCTS-BAK.
2440 (setq backup-end
(save-excursion
2441 (outline-next-heading) (point))
2443 (while (org-list-search-forward box-re backup-end
'move
)
2444 (let* ((struct (org-list-struct))
2445 (bottom (org-list-get-bottom-point struct
)))
2446 (push struct structs-bak
)
2447 (goto-char bottom
)))
2448 (funcall count-boxes nil structs-bak recursivep
))
2449 ;; Cookie is at an item, and we already have list
2450 ;; structure stored in STRUCTS-BAK.
2451 ((and (org-at-item-p)
2452 (< (point-at-bol) backup-end
)
2453 ;; Only lists in no special context are stored.
2454 (not (nth 2 (org-list-context))))
2455 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2456 ;; Cookie is at an item, but we need to compute list
2459 (let ((struct (org-list-struct)))
2460 (setq backup-end
(org-list-get-bottom-point struct
)
2461 structs-bak
(list struct
)))
2462 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2463 ;; Else, cookie found is at a wrong place. Skip it.
2464 (t (throw 'skip nil
))))
2466 ;; 2. Apply alist to buffer, in reverse order so positions stay
2467 ;; unchanged after cookie modifications.
2468 (mapc (lambda (cookie)
2469 (let* ((beg (car cookie
))
2470 (end (nth 1 cookie
))
2471 (percentp (nth 2 cookie
))
2472 (checked (car (nth 3 cookie
)))
2473 (total (cdr (nth 3 cookie
)))
2475 (format "[%d%%]" (/ (* 100 checked
)
2477 (format "[%d/%d]" checked total
))))
2480 (delete-region (point) (+ (point) (- end beg
)))
2481 (when org-auto-align-tags
(org-fix-tags-on-the-fly))))
2484 (defun org-get-checkbox-statistics-face ()
2485 "Select the face for checkbox statistics.
2486 The face will be `org-done' when all relevant boxes are checked.
2487 Otherwise it will be `org-todo'."
2489 (if (equal (match-string 1) "100%")
2490 'org-checkbox-statistics-done
2491 'org-checkbox-statistics-todo
)
2492 (if (and (> (match-end 2) (match-beginning 2))
2493 (equal (match-string 2) (match-string 3)))
2494 'org-checkbox-statistics-done
2495 'org-checkbox-statistics-todo
)))
2497 (defun org-update-checkbox-count-maybe (&optional all
)
2498 "Update checkbox statistics unless turned off by user."
2499 (when (cdr (assq 'checkbox org-list-automatic-rules
))
2500 (org-update-checkbox-count all
))
2501 (run-hooks 'org-checkbox-statistics-hook
))
2503 (defvar org-last-indent-begin-marker
(make-marker))
2504 (defvar org-last-indent-end-marker
(make-marker))
2505 (defun org-list-indent-item-generic (arg no-subtree struct
)
2506 "Indent a local list item including its children.
2507 When number ARG is a negative, item will be outdented, otherwise
2508 it will be indented.
2510 If a region is active, all items inside will be moved.
2512 If NO-SUBTREE is non-nil, only indent the item itself, not its
2515 STRUCT is the list structure.
2517 Return t if successful."
2520 (let* ((regionp (org-region-active-p))
2521 (rbeg (and regionp
(region-beginning)))
2522 (rend (and regionp
(region-end)))
2523 (top (org-list-get-top-point struct
))
2524 (parents (org-list-parents-alist struct
))
2525 (prevs (org-list-prevs-alist struct
))
2526 ;; Are we going to move the whole list?
2528 (and (= top
(point))
2529 (cdr (assq 'indent org-list-automatic-rules
))
2532 "First item of list cannot move without its subtree")
2534 ;; Determine begin and end points of zone to indent. If moving
2535 ;; more than one item, save them for subsequent moves.
2536 (unless (and (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
2537 (memq this-command
'(org-shiftmetaright org-shiftmetaleft
)))
2540 (set-marker org-last-indent-begin-marker rbeg
)
2541 (set-marker org-last-indent-end-marker rend
))
2542 (set-marker org-last-indent-begin-marker
(point))
2543 (set-marker org-last-indent-end-marker
2545 (specialp (org-list-get-bottom-point struct
))
2546 (no-subtree (1+ (point)))
2547 (t (org-list-get-item-end (point) struct
))))))
2548 (let* ((beg (marker-position org-last-indent-begin-marker
))
2549 (end (marker-position org-last-indent-end-marker
)))
2551 ;; Special case: moving top-item with indent rule.
2553 (let* ((level-skip (org-level-increment))
2554 (offset (if (< arg
0) (- level-skip
) level-skip
))
2555 (top-ind (org-list-get-ind beg struct
))
2556 (old-struct (copy-tree struct
)))
2557 (if (< (+ top-ind offset
) 0)
2558 (error "Cannot outdent beyond margin")
2559 ;; Change bullet if necessary.
2560 (when (and (= (+ top-ind offset
) 0)
2562 (org-list-get-bullet beg struct
)))
2563 (org-list-set-bullet beg struct
2564 (org-list-bullet-string "-")))
2565 ;; Shift every item by OFFSET and fix bullets. Then
2566 ;; apply changes to buffer.
2568 (let ((ind (org-list-get-ind (car e
) struct
)))
2569 (org-list-set-ind (car e
) struct
(+ ind offset
))))
2571 (org-list-struct-fix-bul struct prevs
)
2572 (org-list-struct-apply-struct struct old-struct
))))
2575 ;; If only one item is moved, it mustn't have a child.
2578 (org-list-has-child-p beg struct
))
2579 ;; If a subtree or region is moved, the last item
2580 ;; of the subtree mustn't have a child.
2581 (let ((last-item (caar
2584 (lambda (e) (>= (car e
) end
))
2586 (org-list-has-child-p last-item struct
))))
2587 (error "Cannot outdent an item without its children"))
2592 (org-list-struct-outdent beg end struct parents
)
2593 (org-list-struct-indent beg end struct parents prevs
))))
2594 (org-list-write-struct struct new-parents
))
2595 (org-update-checkbox-count-maybe))))))
2598 (defun org-outdent-item ()
2599 "Outdent a local list item, but not its children.
2600 If a region is active, all items inside will be moved."
2603 (let ((struct (org-list-struct)))
2604 (org-list-indent-item-generic -
1 t struct
))
2605 (error "Not at an item")))
2607 (defun org-indent-item ()
2608 "Indent a local list item, but not its children.
2609 If a region is active, all items inside will be moved."
2612 (let ((struct (org-list-struct)))
2613 (org-list-indent-item-generic 1 t struct
))
2614 (error "Not at an item")))
2616 (defun org-outdent-item-tree ()
2617 "Outdent a local list item including its children.
2618 If a region is active, all items inside will be moved."
2620 (let ((regionp (org-region-active-p)))
2622 ((or (org-at-item-p)
2623 (and (org-region-active-p)
2624 (goto-char (region-beginning))
2626 (let ((struct (org-list-struct)))
2627 (org-list-indent-item-generic -
1 nil struct
)))
2628 (regionp (error "Region not starting at an item"))
2629 (t (error "Not at an item")))))
2631 (defun org-indent-item-tree ()
2632 "Indent a local list item including its children.
2633 If a region is active, all items inside will be moved."
2635 (let ((regionp (org-region-active-p)))
2637 ((or (org-at-item-p)
2638 (and (org-region-active-p)
2639 (goto-char (region-beginning))
2641 (let ((struct (org-list-struct)))
2642 (org-list-indent-item-generic 1 nil struct
)))
2643 (regionp (error "Region not starting at an item"))
2644 (t (error "Not at an item")))))
2646 (defvar org-tab-ind-state
)
2647 (defun org-cycle-item-indentation ()
2648 "Cycle levels of indentation of an empty item.
2649 The first run indents the item, if applicable. Subsequents runs
2650 outdent it at meaningful levels in the list. When done, item is
2651 put back at its original position with its original bullet.
2653 Return t at each successful move."
2654 (when (org-at-item-p)
2655 (let* ((org-adapt-indentation nil
)
2656 (struct (org-list-struct))
2657 (ind (org-list-get-ind (point-at-bol) struct
))
2658 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2659 ;; Accept empty items or if cycle has already started.
2660 (when (or (eq last-command
'org-cycle-item-indentation
)
2661 (and (save-excursion
2663 (looking-at org-list-full-item-re
))
2664 (>= (match-end 0) (save-excursion
2665 (goto-char (org-list-get-item-end
2666 (point-at-bol) struct
))
2667 (skip-chars-backward " \r\t\n")
2669 (setq this-command
'org-cycle-item-indentation
)
2670 ;; When in the middle of the cycle, try to outdent first. If
2671 ;; it fails, and point is still at initial position, indent.
2672 ;; Else, re-create it at its original position.
2673 (if (eq last-command
'org-cycle-item-indentation
)
2675 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2676 ((and (= ind
(car org-tab-ind-state
))
2677 (ignore-errors (org-list-indent-item-generic 1 t struct
))))
2678 (t (delete-region (point-at-bol) (point-at-eol))
2679 (org-indent-to-column (car org-tab-ind-state
))
2680 (insert (cdr org-tab-ind-state
) " ")
2682 (setq this-command
'identity
)))
2683 ;; If a cycle is starting, remember indentation and bullet,
2684 ;; then try to indent. If it fails, try to outdent.
2685 (setq org-tab-ind-state
(cons ind bullet
))
2687 ((ignore-errors (org-list-indent-item-generic 1 t struct
)))
2688 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2689 (t (error "Cannot move item"))))
2692 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func
)
2694 The cursor may be at any item of the list that should be sorted.
2695 Sublists are not sorted. Checkboxes, if any, are ignored.
2697 Sorting can be alphabetically, numerically, by date/time as given
2698 by a time stamp, by a property or by priority.
2700 Comparing entries ignores case by default. However, with an
2701 optional argument WITH-CASE, the sorting considers case as well.
2703 The command prompts for the sorting type unless it has been given
2704 to the function through the SORTING-TYPE argument, which needs to
2705 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
2706 meaning of each character:
2708 n Numerically, by converting the beginning of the item to a number.
2709 a Alphabetically. Only the first line of item is checked.
2710 t By date/time, either the first active time stamp in the entry, if
2711 any, or by the first inactive one. In a timer list, sort the timers.
2713 Capital letters will reverse the sort order.
2715 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2716 a function to be called with point at the beginning of the
2717 record. It must return either a string or a number that should
2718 serve as the sorting key for that record. It will then use
2719 COMPARE-FUNC to compare entries."
2721 (let* ((case-func (if with-case
'identity
'downcase
))
2722 (struct (org-list-struct))
2723 (prevs (org-list-prevs-alist struct
))
2724 (start (org-list-get-list-begin (point-at-bol) struct prevs
))
2725 (end (org-list-get-list-end (point-at-bol) struct prevs
))
2729 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
2730 (read-char-exclusive)))
2731 (getkey-func (and (= (downcase sorting-type
) ?f
)
2732 (intern (org-icompleting-read "Sort using function: "
2733 obarray
'fboundp t nil nil
)))))
2734 (message "Sorting items...")
2736 (narrow-to-region start end
)
2737 (goto-char (point-min))
2738 (let* ((dcst (downcase sorting-type
))
2739 (case-fold-search nil
)
2740 (now (current-time))
2742 ((= dcst ?a
) 'string
<)
2743 ((= dcst ?f
) compare-func
)
2746 (next-record (lambda ()
2747 (skip-chars-forward " \r\t\n")
2748 (beginning-of-line)))
2749 (end-record (lambda ()
2750 (goto-char (org-list-get-item-end-before-blank
2754 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2757 (string-to-number (buffer-substring (match-end 0)
2761 (buffer-substring (match-end 0) (point-at-eol))))
2764 ;; If it is a timer list, convert timer to seconds
2765 ((org-at-item-timer-p)
2766 (org-timer-hms-to-secs (match-string 1)))
2767 ((or (re-search-forward org-ts-regexp
(point-at-eol) t
)
2768 (re-search-forward org-ts-regexp-both
2770 (org-time-string-to-seconds (match-string 0)))
2771 (t (org-float-time now
))))
2774 (let ((value (funcall getkey-func
)))
2776 (funcall case-func value
)
2778 (error "Invalid key function `%s'" getkey-func
)))
2779 (t (error "Invalid sorting type `%c'" sorting-type
)))))))
2780 (sort-subr (/= dcst sorting-type
)
2786 ;; Read and fix list again, as `sort-subr' probably destroyed
2789 (run-hooks 'org-after-sorting-entries-or-items-hook
)
2790 (message "Sorting items...done")))))
2793 ;;; Send and receive lists
2795 (defun org-list-parse-list (&optional delete
)
2796 "Parse the list at point and maybe DELETE it.
2798 Return a list whose car is a symbol of list type, among
2799 `ordered', `unordered' and `descriptive'. Then, each item is
2800 a list whose car is counter, and cdr are strings and other
2801 sub-lists. Inside strings, check-boxes are replaced by
2802 \"[CBON]\", \"[CBOFF]\" and \"[CBTRANS]\".
2804 For example, the following list:
2809 more text in first item
2815 \(nil \"first item\"
2817 \(nil \"sub-item one\"\)
2818 \(nil \"[CBON] sub-item two\"\)\)
2819 \"more text in first item\"\)
2820 \(3 \"last item\"\)\)
2822 Point is left at list end."
2823 (let* ((struct (org-list-struct))
2824 (prevs (org-list-prevs-alist struct
))
2825 (parents (org-list-parents-alist struct
))
2826 (top (org-list-get-top-point struct
))
2827 (bottom (org-list-get-bottom-point struct
))
2829 parse-item
; for byte-compiler
2832 ;; Return text between BEG and END, trimmed, with
2833 ;; checkboxes replaced.
2835 (let ((text (org-trim (buffer-substring beg end
))))
2836 (if (string-match "\\`\\[\\([-X ]\\)\\]" text
)
2838 (let ((box (match-string 1 text
)))
2840 ((equal box
" ") "CBOFF")
2841 ((equal box
"-") "CBTRANS")
2847 ;; Return a list whose car is list type and cdr a list of
2850 (cons (org-list-get-list-type (car e
) struct prevs
)
2851 (mapcar parse-item e
)))))
2854 ;; Return a list containing counter of item, if any, text
2855 ;; and any sublist inside it.
2857 (let ((start (save-excursion
2859 (looking-at "[ \t]*\\S-+\\([ \t]+\\[@\\(start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\]\\)?[ \t]*")
2861 ;; Get counter number. For alphabetic counter, get
2862 ;; its position in the alphabet.
2863 (counter (let ((c (org-list-get-counter e struct
)))
2866 ((string-match "[A-Za-z]" c
)
2867 (- (string-to-char (upcase (match-string 0 c
)))
2869 ((string-match "[0-9]+" c
)
2870 (string-to-number (match-string 0 c
))))))
2871 (childp (org-list-has-child-p e struct
))
2872 (end (org-list-get-item-end e struct
)))
2873 ;; If item has a child, store text between bullet and
2874 ;; next child, then recursively parse all sublists. At
2875 ;; the end of each sublist, check for the presence of
2876 ;; text belonging to the original item.
2878 (let* ((children (org-list-get-children e struct parents
))
2879 (body (list (funcall get-text start childp
))))
2881 (let* ((first (car children
))
2882 (sub (org-list-get-all-items first struct prevs
))
2883 (last-c (car (last sub
)))
2884 (last-end (org-list-get-item-end last-c struct
)))
2885 (push (funcall parse-sublist sub
) body
)
2886 ;; Remove children from the list just parsed.
2887 (setq children
(cdr (member last-c children
)))
2888 ;; There is a chunk of text belonging to the
2889 ;; item if last child doesn't end where next
2890 ;; child starts or where item ends.
2891 (unless (= (or (car children
) end
) last-end
)
2892 (push (funcall get-text
2893 last-end
(or (car children
) end
))
2895 (cons counter
(nreverse body
)))
2896 (list counter
(funcall get-text start end
))))))))
2897 ;; Store output, take care of cursor position and deletion of
2898 ;; list, then return output.
2899 (setq out
(funcall parse-sublist
(org-list-get-all-items top struct prevs
)))
2902 (delete-region top bottom
)
2903 (when (and (not (eq org-list-ending-method
'indent
))
2904 (not (looking-at "[ \t]*$"))
2905 (looking-at org-list-end-re
))
2906 (replace-match "")))
2909 (defun org-list-make-subtree ()
2910 "Convert the plain list at point into a subtree."
2912 (if (not (ignore-errors (goto-char (org-in-item-p))))
2913 (error "Not in a list")
2914 (let ((list (save-excursion (org-list-parse-list t
))))
2915 (insert (org-list-to-subtree list
)))))
2917 (defun org-list-insert-radio-list ()
2918 "Insert a radio list template appropriate for this major mode."
2920 (let* ((e (assq major-mode org-list-radio-list-templates
))
2923 (unless e
(error "No radio list setup defined for %s" major-mode
))
2924 (setq name
(read-string "List name: "))
2925 (while (string-match "%n" txt
)
2926 (setq txt
(replace-match name t t txt
)))
2927 (or (bolp) (insert "\n"))
2932 (defun org-list-send-list (&optional maybe
)
2933 "Send a transformed version of this list to the receiver position.
2934 With argument MAYBE, fail quietly if no transformation is defined
2938 (unless (org-at-item-p) (error "Not at a list item"))
2940 (re-search-backward "#\\+ORGLST" nil t
)
2941 (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
2944 (error "Don't know how to transform this list"))))
2945 (let* ((name (match-string 1))
2946 (transform (intern (match-string 2)))
2950 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t
)
2951 (match-beginning 0)))
2954 (re-search-backward "#\\+ORGLST" nil t
)
2955 (re-search-forward (org-item-beginning-re) bottom-point t
)
2956 (match-beginning 0)))
2957 (list (save-restriction
2958 (narrow-to-region top-point bottom-point
)
2959 (org-list-parse-list)))
2961 (unless (fboundp transform
)
2962 (error "No such transformation function %s" transform
))
2963 (let ((txt (funcall transform list
)))
2964 ;; Find the insertion place
2966 (goto-char (point-min))
2967 (unless (re-search-forward
2968 (concat "BEGIN RECEIVE ORGLST +"
2970 "\\([ \t]\\|$\\)") nil t
)
2971 (error "Don't know where to insert translated list"))
2972 (goto-char (match-beginning 0))
2973 (beginning-of-line 2)
2975 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
2976 (error "Cannot find end of insertion region"))
2977 (delete-region beg
(point-at-bol))
2980 (message "List converted and installed at receiver location"))))
2982 (defsubst org-list-item-trim-br
(item)
2983 "Trim line breaks in a list ITEM."
2984 (setq item
(replace-regexp-in-string "\n +" " " item
)))
2986 (defun org-list-to-generic (list params
)
2987 "Convert a LIST parsed through `org-list-parse-list' to other formats.
2988 Valid parameters PARAMS are:
2990 :ustart String to start an unordered list
2991 :uend String to end an unordered list
2993 :ostart String to start an ordered list
2994 :oend String to end an ordered list
2996 :dstart String to start a descriptive list
2997 :dend String to end a descriptive list
2998 :dtstart String to start a descriptive term
2999 :dtend String to end a descriptive term
3000 :ddstart String to start a description
3001 :ddend String to end a description
3003 :splice When set to t, return only list body lines, don't wrap
3004 them into :[u/o]start and :[u/o]end. Default is nil.
3006 :istart String to start a list item.
3007 :icount String to start an item with a counter.
3008 :iend String to end a list item
3009 :isep String to separate items
3010 :lsep String to separate sublists
3011 :csep String to separate text from a sub-list
3013 :cboff String to insert for an unchecked check-box
3014 :cbon String to insert for a checked check-box
3015 :cbtrans String to insert for a check-box in transitional state
3017 :nobr Non-nil means remove line breaks in lists items.
3019 Alternatively, each parameter can also be a form returning
3020 a string. These sexp can use keywords `counter' and `depth',
3021 reprensenting respectively counter associated to the current
3022 item, and depth of the current sub-list, starting at 0.
3023 Obviously, `counter' is only available for parameters applying to
3027 (splicep (plist-get p
:splice
))
3028 (ostart (plist-get p
:ostart
))
3029 (oend (plist-get p
:oend
))
3030 (ustart (plist-get p
:ustart
))
3031 (uend (plist-get p
:uend
))
3032 (dstart (plist-get p
:dstart
))
3033 (dend (plist-get p
:dend
))
3034 (dtstart (plist-get p
:dtstart
))
3035 (dtend (plist-get p
:dtend
))
3036 (ddstart (plist-get p
:ddstart
))
3037 (ddend (plist-get p
:ddend
))
3038 (istart (plist-get p
:istart
))
3039 (icount (plist-get p
:icount
))
3040 (iend (plist-get p
:iend
))
3041 (isep (plist-get p
:isep
))
3042 (lsep (plist-get p
:lsep
))
3043 (csep (plist-get p
:csep
))
3044 (cbon (plist-get p
:cbon
))
3045 (cboff (plist-get p
:cboff
))
3046 (cbtrans (plist-get p
:cbtrans
))
3047 (nobr (plist-get p
:nobr
))
3048 export-sublist
; for byte-compiler
3051 ;; Export an item ITEM of type TYPE, at DEPTH. First
3052 ;; string in item is treated in a special way as it can
3053 ;; bring extra information that needs to be processed.
3054 (lambda (item type depth
)
3055 (let* ((counter (pop item
))
3058 ((eq type
'descriptive
)
3059 ;; Stick DTSTART to ISTART by
3060 ;; left-trimming the latter.
3061 (concat (let ((s (eval istart
)))
3062 (or (and (string-match "[ \t\n\r]+\\'" s
)
3063 (replace-match "" t t s
))
3066 ((and counter
(eq type
'ordered
))
3067 (concat (eval icount
) "%s"))
3068 (t (concat (eval istart
) "%s")))
3071 ;; Replace checkbox if any is found.
3073 ((string-match "\\[CBON\\]" first
)
3074 (setq first
(replace-match cbon t t first
)))
3075 ((string-match "\\[CBOFF\\]" first
)
3076 (setq first
(replace-match cboff t t first
)))
3077 ((string-match "\\[CBTRANS\\]" first
)
3078 (setq first
(replace-match cbtrans t t first
))))
3079 ;; Replace line breaks if required
3080 (when nobr
(setq first
(org-list-item-trim-br first
)))
3081 ;; Insert descriptive term if TYPE is `descriptive'.
3082 (when (eq type
'descriptive
)
3083 (let* ((complete (string-match "^\\(.*\\)[ \t]+::" first
))
3086 (org-trim (match-string 1 first
)))
3089 (org-trim (substring first
(match-end 0)))
3091 (setq first
(concat (eval dtstart
) term
(eval dtend
)
3092 (eval ddstart
) desc
))))
3095 (mapconcat (lambda (e)
3097 (funcall export-sublist e
(1+ depth
))))
3098 item
(or (eval csep
) "")))))))
3101 ;; Export sublist SUB at DEPTH.
3103 (let* ((type (car sub
))
3108 (concat (eval ostart
) "%s" (eval oend
)))
3109 ((eq type
'descriptive
)
3110 (concat (eval dstart
) "%s" (eval dend
)))
3111 (t (concat (eval ustart
) "%s" (eval uend
))))
3113 (format fmt
(mapconcat (lambda (e)
3114 (funcall export-item e type depth
))
3115 items
(or (eval isep
) ""))))))))
3116 (concat (funcall export-sublist list
0) "\n")))
3118 (defun org-list-to-latex (list &optional params
)
3119 "Convert LIST into a LaTeX list.
3120 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3121 with overruling parameters for `org-list-to-generic'."
3122 (org-list-to-generic
3125 '(:splice nil
:ostart
"\\begin{enumerate}\n" :oend
"\\end{enumerate}"
3126 :ustart
"\\begin{itemize}\n" :uend
"\\end{itemize}"
3127 :dstart
"\\begin{description}\n" :dend
"\\end{description}"
3128 :dtstart
"[" :dtend
"] "
3129 :istart
"\\item " :iend
"\n"
3130 :icount
(let ((enum (nth depth
'("i" "ii" "iii" "iv"))))
3132 ;; LaTeX increments counter just before
3133 ;; using it, so set it to the desired
3134 ;; value, minus one.
3135 (format "\\setcounter{enum%s}{%s}\n\\item "
3139 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}"
3140 :cbtrans
"\\texttt{[-]}")
3143 (defun org-list-to-html (list &optional params
)
3144 "Convert LIST into a HTML list.
3145 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3146 with overruling parameters for `org-list-to-generic'."
3147 (org-list-to-generic
3150 '(:splice nil
:ostart
"<ol>\n" :oend
"\n</ol>"
3151 :ustart
"<ul>\n" :uend
"\n</ul>"
3152 :dstart
"<dl>\n" :dend
"\n</dl>"
3153 :dtstart
"<dt>" :dtend
"</dt>\n"
3154 :ddstart
"<dd>" :ddend
"</dd>"
3155 :istart
"<li>" :iend
"</li>"
3156 :icount
(format "<li value=\"%s\">" counter
)
3157 :isep
"\n" :lsep
"\n" :csep
"\n"
3158 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>"
3159 :cbtrans
"<code>[-]</code>")
3162 (defun org-list-to-texinfo (list &optional params
)
3163 "Convert LIST into a Texinfo list.
3164 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3165 with overruling parameters for `org-list-to-generic'."
3166 (org-list-to-generic
3169 '(:splice nil
:ostart
"@itemize @minus\n" :oend
"@end itemize"
3170 :ustart
"@enumerate\n" :uend
"@end enumerate"
3171 :dstart
"@table @asis\n" :dend
"@end table"
3172 :dtstart
" " :dtend
"\n"
3173 :istart
"@item\n" :iend
"\n"
3176 :cbon
"@code{[X]}" :cboff
"@code{[ ]}"
3177 :cbtrans
"@code{[-]}")
3180 (defun org-list-to-subtree (list &optional params
)
3181 "Convert LIST into an Org subtree.
3182 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3183 with overruling parameters for `org-list-to-generic'."
3184 (let* ((rule (cdr (assq 'heading org-blank-before-new-entry
)))
3185 (level (org-reduced-level (or (org-current-level) 0)))
3186 (blankp (or (eq rule t
)
3187 (and (eq rule
'auto
)
3189 (outline-previous-heading)
3190 (org-previous-line-empty-p)))))
3193 ;; Return the string for the heading, depending on depth D
3194 ;; of current sub-list.
3196 (let ((oddeven-level (+ level d
1)))
3197 (concat (make-string (if org-odd-levels-only
3198 (1- (* 2 oddeven-level
))
3202 (org-list-to-generic
3206 :dtstart
" " :dtend
" "
3207 :istart
(funcall get-stars depth
)
3208 :icount
(funcall get-stars depth
)
3209 :isep
(if blankp
"\n\n" "\n")
3210 :csep
(if blankp
"\n\n" "\n")
3211 :cbon
"DONE" :cboff
"TODO" :cbtrans
"TODO")
3216 ;;; org-list.el ends here