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 core concept behind lists is their structure. A structure is
31 ;; a snapshot of the list, in the shape of a data tree (see
32 ;; `org-list-struct').
34 ;; Once the list structure is stored, it is possible to make changes
35 ;; on it that will be mirrored to the real list or to get information
36 ;; about the list, using accessors and methods provided in the
37 ;; library. Most of them require the use of one or two helper
38 ;; functions, namely `org-list-parents-alist' and
39 ;; `org-list-prevs-alist'.
41 ;; Structure is eventually applied to the buffer with
42 ;; `org-list-write-struct'. This function repairs (bullets,
43 ;; indentation, checkboxes) the list in the process. It should be
44 ;; called near the end of any function working on structures.
46 ;; Thus, a function applying to lists should usually follow this
49 ;; 1. Verify point is in a list and grab item beginning (with the same
50 ;; function `org-in-item-p'). If the function requires the cursor
51 ;; to be at item's bullet, `org-at-item-p' is more selective. It
52 ;; is also possible to move point to the closest item with
53 ;; `org-list-search-backward', or `org-list-search-forward',
54 ;; applied to the function `org-item-beginning-re'.
56 ;; 2. Get list structure with `org-list-struct'.
58 ;; 3. Compute one, or both, helper functions,
59 ;; (`org-list-parents-alist', `org-list-prevs-alist') depending on
62 ;; 4. Proceed with the modifications, using methods and accessors.
64 ;; 5. Verify and apply structure to buffer, using
65 ;; `org-list-write-struct'.
67 ;; 6. If changes made to the list might have modified check-boxes,
68 ;; call `org-update-checkbox-count-maybe'.
70 ;; Computing a structure can be a costly operation on huge lists (a
71 ;; few thousand lines long). Thus, code should follow the rule:
72 ;; "collect once, use many". As a corollary, it is usually a bad idea
73 ;; to use directly an interactive function inside the code, as those,
74 ;; being independant entities, read the whole list structure another
84 (defvar org-M-RET-may-split-line
)
85 (defvar org-auto-align-tags
)
86 (defvar org-blank-before-new-entry
)
87 (defvar org-clock-string
)
88 (defvar org-closed-string
)
89 (defvar org-deadline-string
)
90 (defvar org-description-max-indent
)
92 (defvar org-odd-levels-only
)
93 (defvar org-scheduled-string
)
94 (defvar org-ts-regexp
)
95 (defvar org-ts-regexp-both
)
97 (declare-function org-at-heading-p
"org" (&optional ignored
))
98 (declare-function org-before-first-heading-p
"org" ())
99 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
100 (declare-function org-combine-plists
"org" (&rest plists
))
101 (declare-function org-count
"org" (cl-item cl-seq
))
102 (declare-function org-current-level
"org" ())
103 (declare-function org-entry-get
"org"
104 (pom property
&optional inherit literal-nil
))
105 (declare-function org-fix-tags-on-the-fly
"org" ())
106 (declare-function org-get-indentation
"org" (&optional line
))
107 (declare-function org-icompleting-read
"org" (&rest args
))
108 (declare-function org-in-block-p
"org" (names))
109 (declare-function org-in-regexp
"org" (re &optional nlines visually
))
110 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
111 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
112 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
113 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
114 (declare-function org-level-increment
"org" ())
115 (declare-function org-narrow-to-subtree
"org" ())
116 (declare-function org-on-heading-p
"org" (&optional invisible-ok
))
117 (declare-function org-previous-line-empty-p
"org" ())
118 (declare-function org-remove-if
"org" (predicate seq
))
119 (declare-function org-reduced-level
"org" (L))
120 (declare-function org-show-subtree
"org" ())
121 (declare-function org-time-string-to-seconds
"org" (s))
122 (declare-function org-timer-hms-to-secs
"org-timer" (hms))
123 (declare-function org-timer-item
"org-timer" (&optional arg
))
124 (declare-function org-trim
"org" (s))
125 (declare-function org-uniquify
"org" (list))
126 (declare-function outline-invisible-p
"outline" (&optional pos
))
127 (declare-function outline-flag-region
"outline" (from to flag
))
128 (declare-function outline-next-heading
"outline" ())
129 (declare-function outline-previous-heading
"outline" ())
133 ;;; Configuration variables
135 (defgroup org-plain-lists nil
136 "Options concerning plain lists in Org-mode."
137 :tag
"Org Plain lists"
138 :group
'org-structure
)
140 (defcustom org-cycle-include-plain-lists t
141 "When t, make TAB cycle visibility on plain list items.
142 Cycling plain lists works only when the cursor is on a plain list
143 item. When the cursor is on an outline heading, plain lists are
144 treated as text. This is the most stable way of handling this,
145 which is why it is the default.
147 When this is the symbol `integrate', then during cycling, plain
148 list items will *temporarily* be interpreted as outline headlines
149 with a level given by 1000+i where i is the indentation of the
150 bullet. This setting can lead to strange effects when switching
151 visibility to `children', because the first \"child\" in a
152 subtree decides what children should be listed. If that first
153 \"child\" is a plain list item with an implied large level
154 number, all true children and grand children of the outline
155 heading will be exposed in a children' view."
156 :group
'org-plain-lists
158 (const :tag
"Never" nil
)
159 (const :tag
"With cursor in plain list (recommended)" t
)
160 (const :tag
"As children of outline headings" integrate
)))
162 (defcustom org-list-demote-modify-bullet nil
163 "Default bullet type installed when demoting an item.
164 This is an association list, for each bullet type, this alist will point
165 to the bullet that should be used when this item is demoted.
168 (setq org-list-demote-modify-bullet
169 '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
174 + Silence of the Lambs
177 + The Hunt for Red October
183 - Silence of the Lambs
186 - The Hunt for Red October
188 :group
'org-plain-lists
191 (choice :tag
"If the current bullet is "
197 (choice :tag
"demotion will change it to"
204 (defcustom org-plain-list-ordered-item-terminator t
205 "The character that makes a line with leading number an ordered list item.
206 Valid values are ?. and ?\). To get both terminators, use t."
207 :group
'org-plain-lists
208 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
209 (const :tag
"paren like in \"2)\"" ?\
))
210 (const :tab
"both" t
)))
212 (defcustom org-alphabetical-lists nil
213 "Non-nil means single character alphabetical bullets are allowed.
214 Both uppercase and lowercase are handled. Lists with more than
215 26 items will fallback to standard numbering. Alphabetical
216 counters like \"[@c]\" will be recognized."
217 :group
'org-plain-lists
220 (defcustom org-list-two-spaces-after-bullet-regexp nil
221 "A regular expression matching bullets that should have 2 spaces after them.
222 When nil, no bullet will have two spaces after them. When
223 a string, it will be used as a regular expression. When the
224 bullet type of a list is changed, the new bullet type will be
225 matched against this regexp. If it matches, there will be two
226 spaces instead of one after the bullet in each item of the list."
227 :group
'org-plain-lists
229 (const :tag
"never" nil
)
232 (defcustom org-list-ending-method
'both
233 "Determine where plain lists should end.
234 Valid values are: `regexp', `indent' or `both'.
236 When set to `regexp', Org will look into two variables,
237 `org-empty-line-terminates-plain-lists' and the more general
238 `org-list-end-regexp', to determine what will end lists.
240 When set to `indent', a list will end whenever a line following
241 an item, but not starting one, is less or equally indented than
242 the first item of the list.
244 When set to `both', each of the preceding methods is applied to
245 determine lists endings. This is the default method."
246 :group
'org-plain-lists
248 (const :tag
"With a regexp defining ending" regexp
)
249 (const :tag
"With indentation of regular (no bullet) text" indent
)
250 (const :tag
"With both methods" both
)))
252 (defcustom org-empty-line-terminates-plain-lists nil
253 "Non-nil means an empty line ends all plain list levels.
254 This variable only makes sense if `org-list-ending-method' is set
255 to `regexp' or `both'. This is then equivalent to set
256 `org-list-end-regexp' to \"^[ \\t]*$\"."
257 :group
'org-plain-lists
260 (defcustom org-list-end-regexp
"^[ \t]*\n[ \t]*\n"
261 "Regexp matching the end of all plain list levels.
262 It must start with \"^\" and end with \"\\n\". It defaults to 2
263 blank lines. `org-empty-line-terminates-plain-lists' has
265 :group
'org-plain-lists
268 (defcustom org-list-automatic-rules
'((bullet . t
)
271 "Non-nil means apply set of rules when acting on lists.
272 By default, automatic actions are taken when using
273 \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
274 \\[org-shiftmetaright], \\[org-shiftmetaleft],
275 \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
276 \\[org-insert-todo-heading]. You can disable individually these
277 rules by setting them to nil. Valid rules are:
279 bullet when non-nil, cycling bullet do not allow lists at
280 column 0 to have * as a bullet and descriptions lists
282 checkbox when non-nil, checkbox statistics is updated each time
283 you either insert a new checkbox or toggle a checkbox.
284 It also prevents from inserting a checkbox in a
286 indent when non-nil, indenting or outdenting list top-item
287 with its subtree will move the whole list and
288 outdenting a list whose bullet is * to column 0 will
289 change that bullet to \"-\"."
290 :group
'org-plain-lists
291 :type
'(alist :tag
"Sets of rules"
294 (const :tag
"Bullet" bullet
)
295 (const :tag
"Checkbox" checkbox
)
296 (const :tag
"Indent" indent
))
298 (boolean :tag
"Activate" :value t
)))
300 (defcustom org-list-use-circular-motion nil
301 "Non-nil means commands implying motion in lists should be cyclic.
303 In that case, the item following the last item is the first one,
304 and the item preceding the first item is the last one.
306 This affects the behavior of \\[org-move-item-up],
307 \\[org-move-item-down], \\[org-next-item] and
308 \\[org-previous-item]."
309 :group
'org-plain-lists
312 (defvar org-checkbox-statistics-hook nil
313 "Hook that is run whenever Org thinks checkbox statistics should be updated.
314 This hook runs even if checkbox rule in
315 `org-list-automatic-rules' does not apply, so it can be used to
316 implement alternative ways of collecting statistics
319 (defcustom org-hierarchical-checkbox-statistics t
320 "Non-nil means checkbox statistics counts only the state of direct children.
321 When nil, all boxes below the cookie are counted.
322 This can be set to nil on a per-node basis using a COOKIE_DATA property
323 with the word \"recursive\" in the value."
324 :group
'org-plain-lists
327 (defcustom org-description-max-indent
20
328 "Maximum indentation for the second line of a description list.
329 When the indentation would be larger than this, it will become
330 5 characters instead."
331 :group
'org-plain-lists
334 (defcustom org-list-indent-offset
0
335 "Additional indentation for sub-items in a list.
336 By setting this to a small number, usually 1 or 2, one can more
337 clearly distinguish sub-items in a list."
338 :group
'org-plain-lists
341 (defcustom org-list-radio-list-templates
342 '((latex-mode "% BEGIN RECEIVE ORGLST %n
343 % END RECEIVE ORGLST %n
345 #+ORGLST: SEND %n org-list-to-latex
348 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
349 @c END RECEIVE ORGLST %n
351 #+ORGLST: SEND %n org-list-to-texinfo
354 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
355 <!-- END RECEIVE ORGLST %n -->
357 #+ORGLST: SEND %n org-list-to-html
360 "Templates for radio lists in different major modes.
361 All occurrences of %n in a template will be replaced with the name of the
362 list, obtained by prompting the user."
363 :group
'org-plain-lists
365 (list (symbol :tag
"Major mode")
366 (string :tag
"Format"))))
368 (defvar org-list-forbidden-blocks
'("example" "verse" "src" "ascii" "beamer"
369 "docbook" "html" "latex" "odt")
370 "Names of blocks where lists are not allowed.
371 Names must be in lower case.")
373 (defvar org-list-export-context
'(block inlinetask
)
374 "Context types where lists will be interpreted during export.
376 Valid types are `drawer', `inlinetask' and `block'. More
377 specifically, type `block' is determined by the variable
378 `org-list-forbidden-blocks'.")
382 ;;; Predicates and regexps
384 (defconst org-list-end-re
(if org-empty-line-terminates-plain-lists
387 "Regex corresponding to the end of a list.
388 It depends on `org-empty-line-terminates-plain-lists'.")
390 (defconst org-list-full-item-re
391 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
392 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
393 "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
394 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
395 "Matches a list item and puts everything into groups:
399 group 4: description tag")
401 (defun org-item-re ()
402 "Return the correct regular expression for plain lists."
404 ((eq org-plain-list-ordered-item-terminator t
) "[.)]")
405 ((= org-plain-list-ordered-item-terminator ?\
)) ")")
406 ((= org-plain-list-ordered-item-terminator ?.
) "\\.")
408 (alpha (if org-alphabetical-lists
"\\|[A-Za-z]" "")))
409 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha
"\\)" term
410 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
412 (defsubst org-item-beginning-re
()
413 "Regexp matching the beginning of a plain list item."
414 (concat "^" (org-item-re)))
416 (defun org-list-at-regexp-after-bullet-p (regexp)
417 "Is point at a list item with REGEXP after bullet?"
420 (goto-char (match-end 0))
421 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
422 (if org-alphabetical-lists
423 "\\([0-9]+\\|[A-Za-z]\\)"
426 ;; Ignore counter if any
427 (when (looking-at counter-re
) (goto-char (match-end 0))))
428 (looking-at regexp
))))
430 (defun org-list-in-valid-context-p ()
431 "Is point in a context where lists are allowed?"
432 (not (org-in-block-p org-list-forbidden-blocks
)))
434 (defun org-in-item-p ()
435 "Return item beginning position when in a plain list, nil otherwise.
436 This checks `org-list-ending-method'."
439 (let* ((case-fold-search t
)
440 (context (org-list-context))
441 (lim-up (car context
))
442 (drawers-re (concat "^[ \t]*:\\("
443 (mapconcat 'regexp-quote org-drawers
"\\|")
445 (inlinetask-re (and (featurep 'org-inlinetask
)
446 (org-inlinetask-outline-regexp)))
447 (item-re (org-item-re))
448 ;; Indentation isn't meaningful when point starts at an empty
449 ;; line or an inline task.
450 (ind-ref (if (or (looking-at "^[ \t]*$")
451 (and inlinetask-re
(looking-at inlinetask-re
)))
453 (org-get-indentation))))
455 ((eq (nth 2 context
) 'invalid
) nil
)
456 ((looking-at item-re
) (point))
458 ;; Detect if cursor in amidst `org-list-end-re'. First, count
459 ;; number HL of hard lines it takes, then call `org-in-regexp'
460 ;; to compute its boundaries END-BOUNDS. When point is
461 ;; in-between, move cursor before regexp beginning.
462 (let ((hl 0) (i -
1) end-bounds
)
463 (when (and (not (eq org-list-ending-method
'indent
))
465 (while (setq i
(string-match
466 "[\r\n]" org-list-end-re
(1+ i
)))
468 (setq end-bounds
(org-in-regexp org-list-end-re hl
)))
469 (>= (point) (car end-bounds
))
470 (< (point) (cdr end-bounds
)))
471 (goto-char (car end-bounds
))
473 ;; Look for an item, less indented that reference line if
474 ;; `org-list-ending-method' isn't `regexp'.
477 (let ((ind (org-get-indentation)))
479 ;; This is exactly what we want.
480 ((and (looking-at item-re
)
482 (eq org-list-ending-method
'regexp
)))
483 (throw 'exit
(point)))
484 ;; At upper bound of search or looking at the end of a
485 ;; previous list: search is over.
486 ((<= (point) lim-up
) (throw 'exit nil
))
487 ((and (not (eq org-list-ending-method
'indent
))
488 (looking-at org-list-end-re
))
490 ;; Skip blocks, drawers, inline-tasks, blank lines
491 ((and (looking-at "^[ \t]*#\\+end_")
492 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
493 ((and (looking-at "^[ \t]*:END:")
494 (re-search-backward drawers-re lim-up t
))
496 ((and inlinetask-re
(looking-at inlinetask-re
))
497 (org-inlinetask-goto-beginning)
499 ((looking-at "^[ \t]*$") (forward-line -
1))
500 ;; Text at column 0 cannot belong to a list: stop.
501 ((zerop ind
) (throw 'exit nil
))
502 ;; Normal text less indented than reference line, take
503 ;; it as new reference.
507 (t (forward-line -
1)))))))))))
509 (defun org-at-item-p ()
510 "Is point in a line starting a hand-formatted item?"
513 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
515 (defun org-at-item-bullet-p ()
516 "Is point at the bullet of a plain list item?"
518 (not (member (char-after) '(?\ ?
\t)))
519 (< (point) (match-end 0))))
521 (defun org-at-item-timer-p ()
522 "Is point at a line starting a plain list item with a timer?"
523 (org-list-at-regexp-after-bullet-p
524 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
526 (defun org-at-item-description-p ()
527 "Is point at a description list item?"
528 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
530 (defun org-at-item-checkbox-p ()
531 "Is point at a line starting a plain-list item with a checklet?"
532 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
534 (defun org-at-item-counter-p ()
535 "Is point at a line starting a plain-list item with a counter?"
537 (looking-at org-list-full-item-re
)
542 ;;; Structures and helper functions
544 (defun org-list-context ()
545 "Determine context, and its boundaries, around point.
547 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
548 are boundaries and CONTEXT is a symbol among `drawer', `block',
549 `invalid', `inlinetask' and nil.
551 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
554 (org-with-limited-levels
556 (let ((case-fold-search t
) (pos (point)) beg end context-type
557 ;; Get positions of surrounding headings. This is the
559 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t
))
562 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
563 ;; Is point inside a drawer?
564 (let ((end-re "^[ \t]*:END:")
565 ;; Can't use org-drawers-regexp as this function might
566 ;; be called in buffers not in Org mode.
567 (beg-re (concat "^[ \t]*:\\("
568 (mapconcat 'regexp-quote org-drawers
"\\|")
570 (when (save-excursion
571 (and (not (looking-at beg-re
))
572 (not (looking-at end-re
))
573 (setq beg
(and (re-search-backward beg-re lim-up t
)
574 (1+ (point-at-eol))))
575 (setq end
(or (and (re-search-forward end-re lim-down t
)
576 (1- (match-beginning 0)))
579 (setq lim-up beg lim-down end context-type
'drawer
)))
580 ;; Is point strictly in a block, and of which type?
581 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type
)
582 (when (save-excursion
583 (and (not (looking-at block-re
))
584 (setq beg
(and (re-search-backward block-re lim-up t
)
585 (1+ (point-at-eol))))
586 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
587 (setq type
(downcase (match-string 1)))
589 (setq end
(or (and (re-search-forward block-re lim-down t
)
593 (equal (downcase (match-string 1)) "end")))
594 (setq lim-up beg lim-down end
595 context-type
(if (member type org-list-forbidden-blocks
)
597 ;; Is point in an inlinetask?
598 (when (and (featurep 'org-inlinetask
)
600 (let* ((beg-re (org-inlinetask-outline-regexp))
601 (end-re (concat beg-re
"END[ \t]*$")))
602 (and (not (looking-at "^\\*+"))
603 (setq beg
(and (re-search-backward beg-re lim-up t
)
604 (1+ (point-at-eol))))
605 (not (looking-at end-re
))
606 (setq end
(and (re-search-forward end-re lim-down t
)
607 (1- (match-beginning 0))))
609 (setq lim-up beg lim-down end context-type
'inlinetask
))
610 ;; Return context boundaries and type.
611 (list lim-up lim-down context-type
))))))
613 (defun org-list-struct ()
614 "Return structure of list at point.
616 A list structure is an alist where key is point at item, and
619 2. bullet with trailing whitespace,
620 3. bullet counter, if any,
622 5. description tag, if any,
623 6. position at item end.
625 Thus the following list, where numbers in parens are
630 5. [@5] sub-item 2 (34)
631 some other text belonging to first item (55)
633 + tag :: description (109)
636 will get the following structure:
638 \(\(1 0 \"- \" nil \"[X]\" nil 97\)
639 \(18 2 \"1. \" nil nil nil 34\)
640 \(34 2 \"5. \" \"5\" nil nil 55\)
641 \(97 0 \"- \" nil nil nil 131\)
642 \(109 2 \"+ \" nil nil \"tag\" 131\)
644 Assume point is at an item."
647 (let* ((case-fold-search t
)
648 (context (org-list-context))
649 (lim-up (car context
))
650 (lim-down (nth 1 context
))
652 (item-re (org-item-re))
653 (drawers-re (concat "^[ \t]*:\\("
654 (mapconcat 'regexp-quote org-drawers
"\\|")
656 (inlinetask-re (and (featurep 'org-inlinetask
)
657 (org-inlinetask-outline-regexp)))
658 (beg-cell (cons (point) (org-get-indentation)))
659 ind itm-lst itm-lst-2 end-lst end-lst-2 struct
662 ;; Return association at point.
664 (looking-at org-list-full-item-re
)
667 (match-string-no-properties 1) ; bullet
668 (match-string-no-properties 2) ; counter
669 (match-string-no-properties 3) ; checkbox
670 (match-string-no-properties 4))))) ; description tag
673 ;; Ensure list ends at the first blank line.
675 (skip-chars-backward " \r\t\n")
676 (min (1+ (point-at-eol)) lim-down
)))))
677 ;; 1. Read list from starting item to its beginning, and save
678 ;; top item position and indentation in BEG-CELL. Also store
679 ;; ending position of items in END-LST.
683 (let ((ind (+ (or (get-text-property (point) 'original-indentation
) 0)
684 (org-get-indentation))))
687 ;; At upward limit: if we ended at an item, store it,
688 ;; else dimiss useless data recorded above BEG-CELL.
692 (if (or (not (looking-at item-re
))
693 (get-text-property (point) 'org-example
))
694 (memq (assq (car beg-cell
) itm-lst
) itm-lst
)
695 (setq beg-cell
(cons (point) ind
))
696 (cons (funcall assoc-at-point ind
) itm-lst
)))))
697 ;; At a verbatim block, go before its beginning. Move
698 ;; from eol to ensure `previous-single-property-change'
699 ;; will return a value.
700 ((get-text-property (point) 'org-example
)
701 (goto-char (previous-single-property-change
702 (point-at-eol) 'org-example nil lim-up
))
704 ;; Looking at a list ending regexp. Dismiss useless
705 ;; data recorded above BEG-CELL. Jump to part 2.
706 ((and (not (eq org-list-ending-method
'indent
))
707 (looking-at org-list-end-re
))
710 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
711 ;; Point is at an item. Add data to ITM-LST. It may
712 ;; also end a previous item: save it in END-LST. If
713 ;; ind is less or equal than BEG-CELL and there is no
714 ;; end at this ind or lesser, this item becomes the new
716 ((looking-at item-re
)
717 (push (funcall assoc-at-point ind
) itm-lst
)
718 (push (cons ind
(point)) end-lst
)
719 (when (or (and (eq org-list-ending-method
'regexp
)
720 (<= ind
(cdr beg-cell
)))
721 (< ind text-min-ind
))
722 (setq beg-cell
(cons (point) ind
)))
724 ;; Skip blocks, drawers, inline tasks, blank lines.
725 ((and (looking-at "^[ \t]*#\\+end_")
726 (re-search-backward "^[ \t]*#\\+begin_" lim-up t
)))
727 ((and (looking-at "^[ \t]*:END:")
728 (re-search-backward drawers-re lim-up t
))
730 ((and inlinetask-re
(looking-at inlinetask-re
))
731 (org-inlinetask-goto-beginning)
733 ((looking-at "^[ \t]*$")
735 ;; From there, point is not at an item. Unless ending
736 ;; method is `regexp', interpret line's indentation:
737 ;; - text at column 0 is necessarily out of any list.
738 ;; Dismiss data recorded above BEG-CELL. Jump to
740 ;; - any other case may be an ending position for an
741 ;; hypothetical item above. Store it and proceed.
742 ((eq org-list-ending-method
'regexp
) (forward-line -
1))
746 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
748 (when (< ind text-min-ind
) (setq text-min-ind ind
))
749 (push (cons ind
(point)) end-lst
)
750 (forward-line -
1)))))))
751 ;; 2. Read list from starting point to its end, that is until we
752 ;; get out of context, or that a non-item line is less or
753 ;; equally indented than BEG-CELL's cdr. Also, store ending
754 ;; position of items in END-LST-2.
757 (let ((ind (+ (or (get-text-property (point) 'original-indentation
) 0)
758 (org-get-indentation))))
760 ((>= (point) lim-down
)
761 ;; At downward limit: this is de facto the end of the
762 ;; list. Save point as an ending position, and jump to
765 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
766 ;; At a verbatim block, move to its end. Point is at bol
767 ;; and 'org-example property is set by whole lines:
768 ;; `next-single-property-change' always return a value.
769 ((get-text-property (point) 'org-example
)
771 (next-single-property-change (point) 'org-example nil lim-down
)))
772 ;; Looking at a list ending regexp. Save point as an
773 ;; ending position and jump to part 3.
774 ((and (not (eq org-list-ending-method
'indent
))
775 (looking-at org-list-end-re
))
776 (throw 'exit
(push (cons 0 (point)) end-lst-2
)))
777 ((looking-at item-re
)
778 ;; Point is at an item. Add data to ITM-LST-2. It may
779 ;; also end a previous item, so save it in END-LST-2.
780 (push (funcall assoc-at-point ind
) itm-lst-2
)
781 (push (cons ind
(point)) end-lst-2
)
783 ;; Skip inline tasks and blank lines along the way
784 ((and inlinetask-re
(looking-at inlinetask-re
))
785 (org-inlinetask-goto-end))
786 ((looking-at "^[ \t]*$")
788 ;; Ind is lesser or equal than BEG-CELL's. The list is
789 ;; over: store point as an ending position and jump to
791 ((and (not (eq org-list-ending-method
'regexp
))
792 (<= ind
(cdr beg-cell
)))
794 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
795 ;; Else, if ind is lesser or equal than previous item's,
796 ;; this is an ending position: store it. In any case,
797 ;; skip block or drawer at point, and move to next line.
799 (when (and (not (eq org-list-ending-method
'regexp
))
800 (<= ind
(nth 1 (car itm-lst-2
))))
801 (push (cons ind
(point)) end-lst-2
))
803 ((and (looking-at "^[ \t]*#\\+begin_")
804 (re-search-forward "^[ \t]*#\\+end_" lim-down t
)))
805 ((and (looking-at drawers-re
)
806 (re-search-forward "^[ \t]*:END:" lim-down t
))))
807 (forward-line 1))))))
808 (setq struct
(append itm-lst
(cdr (nreverse itm-lst-2
)))
809 end-lst
(append end-lst
(cdr (nreverse end-lst-2
))))
810 ;; 3. Correct ill-formed lists by ensuring top item is the least
812 (let ((min-ind (nth 1 (car struct
))))
814 (let ((ind (nth 1 item
))
816 (when (< ind min-ind
)
817 (setcar (cdr item
) min-ind
)
818 ;; Trim bullet so item will be seen as different
819 ;; when compared with repaired version.
820 (setcar (nthcdr 2 item
) (org-trim bul
)))))
822 ;; 4. Associate each item to its end pos.
823 (org-list-struct-assoc-end struct end-lst
)
827 (defun org-list-struct-assoc-end (struct end-list
)
828 "Associate proper ending point to items in STRUCT.
830 END-LIST is a pseudo-alist where car is indentation and cdr is
833 This function modifies STRUCT."
834 (let ((endings end-list
))
837 (let ((pos (car elt
))
839 ;; Remove end candidates behind current item.
840 (while (or (<= (cdar endings
) pos
))
842 ;; Add end position to item assoc.
843 (let ((old-end (nthcdr 6 elt
))
844 (new-end (assoc-default ind endings
'<=)))
846 (setcar old-end new-end
)
847 (setcdr elt
(append (cdr elt
) (list new-end
)))))))
850 (defun org-list-prevs-alist (struct)
851 "Return alist between item and previous item in STRUCT."
852 (let ((item-end-alist (mapcar (lambda (e) (cons (car e
) (nth 6 e
)))
855 (let ((prev (car (rassq (car e
) item-end-alist
))))
856 (cons (car e
) prev
)))
859 (defun org-list-parents-alist (struct)
860 "Return alist between item and parent in STRUCT."
861 (let ((ind-to-ori (list (list (nth 1 (car struct
)))))
862 (prev-pos (list (caar struct
))))
864 (mapcar (lambda (item)
865 (let ((pos (car item
))
867 (prev-ind (caar ind-to-ori
)))
872 (member (assq ind ind-to-ori
) ind-to-ori
))
873 (cons pos
(cdar ind-to-ori
)))
875 (let ((origin (nth 1 prev-pos
)))
876 (push (cons ind origin
) ind-to-ori
)
878 (t (cons pos
(cdar ind-to-ori
))))))
885 (defsubst org-list-get-nth
(n key struct
)
886 "Return the Nth value of KEY in STRUCT."
887 (nth n
(assq key struct
)))
889 (defun org-list-set-nth (n key struct new
)
890 "Set the Nth value of KEY in STRUCT to NEW.
891 \nThis function modifies STRUCT."
892 (setcar (nthcdr n
(assq key struct
)) new
))
894 (defsubst org-list-get-ind
(item struct
)
895 "Return indentation of ITEM in STRUCT."
896 (org-list-get-nth 1 item struct
))
898 (defun org-list-set-ind (item struct ind
)
899 "Set indentation of ITEM in STRUCT to IND.
900 \nThis function modifies STRUCT."
901 (org-list-set-nth 1 item struct ind
))
903 (defsubst org-list-get-bullet
(item struct
)
904 "Return bullet of ITEM in STRUCT."
905 (org-list-get-nth 2 item struct
))
907 (defun org-list-set-bullet (item struct bullet
)
908 "Set bullet of ITEM in STRUCT to BULLET.
909 \nThis function modifies STRUCT."
910 (org-list-set-nth 2 item struct bullet
))
912 (defsubst org-list-get-counter
(item struct
)
913 "Return counter of ITEM in STRUCT."
914 (org-list-get-nth 3 item struct
))
916 (defsubst org-list-get-checkbox
(item struct
)
917 "Return checkbox of ITEM in STRUCT or nil."
918 (org-list-get-nth 4 item struct
))
920 (defun org-list-set-checkbox (item struct checkbox
)
921 "Set checkbox of ITEM in STRUCT to CHECKBOX.
922 \nThis function modifies STRUCT."
923 (org-list-set-nth 4 item struct checkbox
))
925 (defsubst org-list-get-tag
(item struct
)
926 "Return end position of ITEM in STRUCT."
927 (org-list-get-nth 5 item struct
))
929 (defun org-list-get-item-end (item struct
)
930 "Return end position of ITEM in STRUCT."
931 (org-list-get-nth 6 item struct
))
933 (defun org-list-get-item-end-before-blank (item struct
)
934 "Return point at end of ITEM in STRUCT, before any blank line.
935 Point returned is at end of line."
937 (goto-char (org-list-get-item-end item struct
))
938 (skip-chars-backward " \r\t\n")
941 (defun org-list-get-parent (item struct parents
)
942 "Return parent of ITEM or nil.
943 STRUCT is the list structure. PARENTS is the alist of parents,
944 as returned by `org-list-parents-alist'."
945 (let ((parents (or parents
(org-list-parents-alist struct
))))
946 (cdr (assq item parents
))))
948 (defun org-list-has-child-p (item struct
)
949 "Non-nil if ITEM has a child.
951 STRUCT is the list structure.
953 Value returned is the position of the first child of ITEM."
954 (let ((ind (org-list-get-ind item struct
))
955 (child-maybe (car (nth 1 (member (assq item struct
) struct
)))))
956 (when (and child-maybe
957 (< ind
(org-list-get-ind child-maybe struct
)))
960 (defun org-list-get-next-item (item struct prevs
)
961 "Return next item in same sub-list as ITEM, or nil.
962 STRUCT is the list structure. PREVS is the alist of previous
963 items, as returned by `org-list-prevs-alist'."
964 (car (rassq item prevs
)))
966 (defun org-list-get-prev-item (item struct prevs
)
967 "Return previous item in same sub-list as ITEM, or nil.
968 STRUCT is the list structure. PREVS is the alist of previous
969 items, as returned by `org-list-prevs-alist'."
970 (cdr (assq item prevs
)))
972 (defun org-list-get-subtree (item struct
)
973 "List all items having ITEM as a common ancestor, or nil.
974 STRUCT is the list structure."
975 (let* ((item-end (org-list-get-item-end item struct
))
976 (sub-struct (cdr (member (assq item struct
) struct
)))
981 (if (< pos item-end
) (push pos subtree
) (throw 'exit nil
))))
985 (defun org-list-get-all-items (item struct prevs
)
986 "List all items in the same sub-list as ITEM.
987 STRUCT is the list structure. PREVS is the alist of previous
988 items, as returned by `org-list-prevs-alist'."
989 (let ((prev-item item
)
991 before-item after-item
)
992 (while (setq prev-item
(org-list-get-prev-item prev-item struct prevs
))
993 (push prev-item before-item
))
994 (while (setq next-item
(org-list-get-next-item next-item struct prevs
))
995 (push next-item after-item
))
996 (append before-item
(list item
) (nreverse after-item
))))
998 (defun org-list-get-children (item struct parents
)
999 "List all children of ITEM, or nil.
1000 STRUCT is the list structure. PARENTS is the alist of parents,
1001 as returned by `org-list-parents-alist'."
1003 (while (setq child
(car (rassq item parents
)))
1004 (setq parents
(cdr (member (assq child parents
) parents
)))
1008 (defun org-list-get-top-point (struct)
1009 "Return point at beginning of list.
1010 STRUCT is the list structure."
1013 (defun org-list-get-bottom-point (struct)
1014 "Return point at bottom of list.
1015 STRUCT is the list structure."
1017 (mapcar (lambda (e) (org-list-get-item-end (car e
) struct
)) struct
)))
1019 (defun org-list-get-list-begin (item struct prevs
)
1020 "Return point at beginning of sub-list ITEM belongs.
1021 STRUCT is the list structure. PREVS is the alist of previous
1022 items, as returned by `org-list-prevs-alist'."
1023 (let ((first-item item
) prev-item
)
1024 (while (setq prev-item
(org-list-get-prev-item first-item struct prevs
))
1025 (setq first-item prev-item
))
1028 (defalias 'org-list-get-first-item
'org-list-get-list-begin
)
1030 (defun org-list-get-last-item (item struct prevs
)
1031 "Return point at last item of sub-list ITEM belongs.
1032 STRUCT is the list structure. PREVS is the alist of previous
1033 items, as returned by `org-list-prevs-alist'."
1034 (let ((last-item item
) next-item
)
1035 (while (setq next-item
(org-list-get-next-item last-item struct prevs
))
1036 (setq last-item next-item
))
1039 (defun org-list-get-list-end (item struct prevs
)
1040 "Return point at end of sub-list ITEM belongs.
1041 STRUCT is the list structure. PREVS is the alist of previous
1042 items, as returned by `org-list-prevs-alist'."
1043 (org-list-get-item-end (org-list-get-last-item item struct prevs
) struct
))
1045 (defun org-list-get-list-type (item struct prevs
)
1046 "Return the type of the list containing ITEM, as a symbol.
1048 STRUCT is the list structure. PREVS is the alist of previous
1049 items, as returned by `org-list-prevs-alist'.
1051 Possible types are `descriptive', `ordered' and `unordered'. The
1052 type is determined by the first item of the list."
1053 (let ((first (org-list-get-list-begin item struct prevs
)))
1055 ((org-list-get-tag first struct
) 'descriptive
)
1056 ((string-match "[[:alnum:]]" (org-list-get-bullet first struct
)) 'ordered
)
1063 (defun org-list-search-generic (search re bound noerr
)
1064 "Search a string in valid contexts for lists.
1065 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1066 in `re-search-forward'."
1068 (let ((origin (point)))
1070 ;; 1. No match: return to origin or bound, depending on NOERR.
1071 (unless (funcall search re bound noerr
)
1072 (throw 'exit
(and (goto-char (if (memq noerr
'(t nil
)) origin bound
))
1074 ;; 2. Match in valid context: return point. Else, continue
1076 (when (org-list-in-valid-context-p) (throw 'exit
(point)))))))
1078 (defun org-list-search-backward (regexp &optional bound noerror
)
1079 "Like `re-search-backward' but stop only where lists are recognized.
1080 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1081 `re-search-backward'."
1082 (org-list-search-generic #'re-search-backward
1083 regexp
(or bound
(point-min)) noerror
))
1085 (defun org-list-search-forward (regexp &optional bound noerror
)
1086 "Like `re-search-forward' but stop only where lists are recognized.
1087 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1088 `re-search-forward'."
1089 (org-list-search-generic #'re-search-forward
1090 regexp
(or bound
(point-max)) noerror
))
1094 ;;; Methods on structures
1096 (defsubst org-list-bullet-string
(bullet)
1097 "Return BULLET with the correct number of whitespaces.
1098 It determines the number of whitespaces to append by looking at
1099 `org-list-two-spaces-after-bullet-regexp'."
1101 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1103 org-list-two-spaces-after-bullet-regexp bullet
))
1106 (string-match "\\S-+\\([ \t]*\\)" bullet
)
1107 (replace-match spaces nil nil bullet
1))))
1109 (defun org-list-swap-items (beg-A beg-B struct
)
1110 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1111 Blank lines at the end of items are left in place. Return the
1112 new structure after the changes.
1114 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1115 to the same sub-list.
1117 This function modifies STRUCT."
1119 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct
))
1120 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct
))
1121 (end-A (org-list-get-item-end beg-A struct
))
1122 (end-B (org-list-get-item-end beg-B struct
))
1123 (size-A (- end-A-no-blank beg-A
))
1124 (size-B (- end-B-no-blank beg-B
))
1125 (body-A (buffer-substring beg-A end-A-no-blank
))
1126 (body-B (buffer-substring beg-B end-B-no-blank
))
1127 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B
))
1128 (sub-A (cons beg-A
(org-list-get-subtree beg-A struct
)))
1129 (sub-B (cons beg-B
(org-list-get-subtree beg-B struct
))))
1130 ;; 1. Move effectively items in buffer.
1132 (delete-region beg-A end-B-no-blank
)
1133 (insert (concat body-B between-A-no-blank-and-B body-A
))
1134 ;; 2. Now modify struct. No need to re-read the list, the
1135 ;; transformation is just a shift of positions. Some special
1136 ;; attention is required for items ending at END-A and END-B
1137 ;; as empty spaces are not moved there. In others words,
1138 ;; item BEG-A will end with whitespaces that were at the end
1139 ;; of BEG-B and the same applies to BEG-B.
1141 (let ((pos (car e
)))
1145 (let ((end-e (nth 6 e
)))
1146 (setcar e
(+ pos
(- end-B-no-blank end-A-no-blank
)))
1147 (setcar (nthcdr 6 e
)
1148 (+ end-e
(- end-B-no-blank end-A-no-blank
)))
1149 (when (= end-e end-A
) (setcar (nthcdr 6 e
) end-B
))))
1151 (let ((end-e (nth 6 e
)))
1152 (setcar e
(- (+ pos beg-A
) beg-B
))
1153 (setcar (nthcdr 6 e
) (+ end-e
(- beg-A beg-B
)))
1154 (when (= end-e end-B
)
1155 (setcar (nthcdr 6 e
)
1156 (+ beg-A size-B
(- end-A end-A-no-blank
))))))
1158 (let ((end-e (nth 6 e
)))
1159 (setcar e
(+ pos
(- size-B size-A
)))
1160 (setcar (nthcdr 6 e
) (+ end-e
(- size-B size-A
))))))))
1162 (sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))))
1164 (defun org-list-separating-blank-lines-number (pos struct prevs
)
1165 "Return number of blank lines that should separate items in list.
1167 POS is the position of point where `org-list-insert-item' was called.
1169 STRUCT is the list structure. PREVS is the alist of previous
1170 items, as returned by `org-list-prevs-alist'.
1172 Assume point is at item's beginning. If the item is alone, apply
1173 some heuristics to guess the result."
1175 (let ((item (point))
1177 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
1182 ;; Count blank lines above beginning of line.
1184 (count-lines (goto-char (point-at-bol))
1185 (progn (skip-chars-backward " \r\t\n")
1189 ;; Trivial cases where there should be none.
1190 ((or (and (not (eq org-list-ending-method
'indent
))
1191 org-empty-line-terminates-plain-lists
)
1192 (not insert-blank-p
)) 0)
1193 ;; When `org-blank-before-new-entry' says so, it is 1.
1194 ((eq insert-blank-p t
) 1)
1195 ;; `plain-list-item' is 'auto. Count blank lines separating
1196 ;; neighbours items in list.
1197 (t (let ((next-p (org-list-get-next-item item struct prevs
)))
1199 ;; Is there a next item?
1200 (next-p (goto-char next-p
)
1201 (funcall count-blanks
))
1202 ;; Is there a previous item?
1203 ((org-list-get-prev-item item struct prevs
)
1204 (funcall count-blanks
))
1205 ;; User inserted blank lines, trust him.
1206 ((and (> pos
(org-list-get-item-end-before-blank item struct
))
1207 (> (save-excursion (goto-char pos
)
1208 (setq usr-blank
(funcall count-blanks
)))
1211 ;; Are there blank lines inside the list so far?
1213 (goto-char (org-list-get-top-point struct
))
1214 (org-list-search-forward
1215 "^[ \t]*$" (org-list-get-item-end-before-blank item struct
) t
))
1217 ;; Default choice: no blank line.
1220 (defun org-list-insert-item (pos struct prevs
&optional checkbox after-bullet
)
1221 "Insert a new list item at POS and return the new structure.
1222 If POS is before first character after bullet of the item, the
1223 new item will be created before the current one.
1225 STRUCT is the list structure. PREVS is the the alist of previous
1226 items, as returned by `org-list-prevs-alist'.
1228 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1229 after the bullet. Cursor will be after this text once the
1232 This function modifies STRUCT."
1233 (let ((case-fold-search t
))
1234 ;; 1. Get information about list: position of point with regards
1235 ;; to item start (BEFOREP), blank lines number separating items
1236 ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
1237 (let* ((item (progn (goto-char pos
) (goto-char (org-list-get-item-begin))))
1238 (item-end (org-list-get-item-end item struct
))
1239 (item-end-no-blank (org-list-get-item-end-before-blank item struct
))
1240 (beforep (and (looking-at org-list-full-item-re
)
1241 (<= pos
(match-end 0))))
1242 (split-line-p (org-get-alist-option org-M-RET-may-split-line
'item
))
1243 (blank-nb (org-list-separating-blank-lines-number
1245 ;; 2. Build the new item to be created. Concatenate same
1246 ;; bullet as item, checkbox, text AFTER-BULLET if
1247 ;; provided, and text cut from point to end of item
1248 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1249 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1250 ;; between what was cut and what was inserted in buffer
1251 ;; is stored in SIZE-OFFSET.
1252 (ind (org-list-get-ind item struct
))
1253 (ind-size (if indent-tabs-mode
1254 (+ (/ ind tab-width
) (mod ind tab-width
))
1256 (bullet (org-list-bullet-string (org-list-get-bullet item struct
)))
1257 (box (when checkbox
"[ ]"))
1259 (and (not beforep
) split-line-p
1262 ;; If POS is greater than ITEM-END, then point is
1263 ;; in some white lines after the end of the list.
1264 ;; Those must be removed, or they will be left,
1265 ;; stacking up after the list.
1266 (when (< item-end pos
)
1267 (delete-region (1- item-end
) (point-at-eol)))
1268 (skip-chars-backward " \r\t\n")
1270 (delete-and-extract-region pos item-end-no-blank
))))
1271 (body (concat bullet
(when box
(concat box
" ")) after-bullet
1273 (if (string-match "\\`[ \t]+" text-cut
)
1274 (replace-match "" t t text-cut
)
1276 (item-sep (make-string (1+ blank-nb
) ?
\n))
1277 (item-size (+ ind-size
(length body
) (length item-sep
)))
1278 (size-offset (- item-size
(length text-cut
))))
1279 ;; 4. Insert effectively item into buffer.
1281 (org-indent-to-column ind
)
1282 (insert body item-sep
)
1283 ;; 5. Add new item to STRUCT.
1288 ;; Before inserted item, positions don't change but
1289 ;; an item ending after insertion has its end shifted
1292 (when (> end item
) (setcar (nthcdr 6 e
) (+ end size-offset
))))
1293 ;; Trivial cases where current item isn't split in
1294 ;; two. Just shift every item after new one by
1296 ((or beforep
(not split-line-p
))
1297 (setcar e
(+ p item-size
))
1298 (setcar (nthcdr 6 e
) (+ end item-size
)))
1299 ;; Item is split in two: elements before POS are just
1300 ;; shifted by ITEM-SIZE. In the case item would end
1301 ;; after split POS, ending is only shifted by
1304 (setcar e
(+ p item-size
))
1306 (setcar (nthcdr 6 e
) (+ end item-size
))
1307 (setcar (nthcdr 6 e
) (+ end size-offset
))))
1308 ;; Elements after POS are moved into new item.
1309 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1310 ;; doesn't appear in buffer yet.
1312 (setcar e
(+ p size-offset
(- item pos
(length item-sep
))))
1313 (if (= end item-end
)
1314 (setcar (nthcdr 6 e
) (+ item item-size
))
1315 (setcar (nthcdr 6 e
)
1317 (- item pos
(length item-sep
))))))
1318 ;; Elements at ITEM-END or after are only shifted by
1320 (t (setcar e
(+ p size-offset
))
1321 (setcar (nthcdr 6 e
) (+ end size-offset
))))))
1323 (push (list item ind bullet nil box nil
(+ item item-size
)) struct
)
1324 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1325 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1326 ;; exchange ITEM with the next item in list. Position cursor
1327 ;; after bullet, counter, checkbox, and label.
1330 (setq struct
(org-list-swap-items item
(+ item item-size
) struct
))
1331 (goto-char (org-list-get-next-item
1332 item struct
(org-list-prevs-alist struct
))))
1335 (defun org-list-delete-item (item struct
)
1336 "Remove ITEM from the list and return the new structure.
1338 STRUCT is the list structure."
1339 (let* ((end (org-list-get-item-end item struct
))
1340 (beg (if (= (org-list-get-bottom-point struct
) end
)
1341 ;; If ITEM ends with the list, delete blank lines
1345 (skip-chars-backward " \r\t\n")
1346 (min (1+ (point-at-eol)) (point-max)))
1348 ;; Remove item from buffer.
1349 (delete-region beg end
)
1350 ;; Remove item from structure and shift others items accordingly.
1351 ;; Don't forget to shift also ending position when appropriate.
1352 (let ((size (- end beg
)))
1353 (delq nil
(mapcar (lambda (e)
1354 (let ((pos (car e
)))
1357 (let ((end-e (nth 6 e
)))
1361 (append (butlast e
) (list beg
)))
1363 (append (butlast e
) (list (- end-e size
)))))))
1367 (append (butlast (cdr e
))
1368 (list (- (nth 6 e
) size
))))))))
1371 (defun org-list-send-item (item dest struct
)
1372 "Send ITEM to destination DEST.
1374 STRUCT is the list structure.
1376 DEST can have various values.
1378 If DEST is a buffer position, the function will assume it points
1379 to another item in the same list as ITEM, and will move the
1380 latter just before the former.
1382 If DEST is `begin' \(respectively `end'\), ITEM will be moved at
1383 the beginning \(respectively end\) of the list it belongs to.
1385 If DEST is a string like \"N\", where N is an integer, ITEM will
1386 be moved at the Nth position in the list.
1388 If DEST is `kill', ITEM will be deleted and its body will be
1389 added to the kill-ring.
1391 If DEST is `delete', ITEM will be deleted.
1393 This function returns, destructively, the new list structure."
1394 (let* ((prevs (org-list-prevs-alist struct
))
1395 (item-end (org-list-get-item-end item struct
))
1396 ;; Grab full item body minus its bullet.
1403 (regexp-quote (org-list-get-bullet item struct
))))
1406 ;; Change DEST into a buffer position. A trick is needed
1407 ;; when ITEM is meant to be sent at the end of the list.
1408 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1409 ;; nil and insertion point (INS-POINT) to the first line's
1410 ;; end of the last item, we ensure the new item will be
1411 ;; inserted after the last item, and not after any of its
1412 ;; hypothetical sub-items.
1414 ((or (eq dest
'kill
) (eq dest
'delete
)))
1416 (setq dest
(org-list-get-list-begin item struct prevs
)))
1418 (setq dest
(org-list-get-list-end item struct prevs
))
1420 (goto-char (org-list-get-last-item item struct prevs
))
1422 ((string-match "\\`[0-9]+\\'" dest
)
1423 (let* ((all (org-list-get-all-items item struct prevs
))
1425 (index (mod (string-to-number dest
) len
)))
1426 (if (not (zerop index
))
1427 (setq dest
(nth (1- index
) all
))
1428 ;; Send ITEM at the end of the list.
1429 (setq dest
(org-list-get-list-end item struct prevs
))
1432 (org-list-get-last-item item struct prevs
))
1435 (org-M-RET-may-split-line nil
))
1437 ((eq dest
'delete
) (org-list-delete-item item struct
))
1440 (org-list-delete-item item struct
))
1441 ((and (integerp dest
) (/= item ins-point
))
1442 (setq item
(copy-marker item
))
1443 (setq struct
(org-list-insert-item ins-point struct prevs nil body
))
1444 ;; 1. Structure returned by `org-list-insert-item' may not be
1445 ;; accurate, as it cannot see sub-items included in BODY.
1446 ;; Thus, first compute the real structure so far.
1448 (cons (marker-position item
)
1449 (org-list-get-subtree (marker-position item
) struct
)))
1450 (new-end (org-list-get-item-end (point) struct
))
1451 (old-end (org-list-get-item-end (marker-position item
) struct
))
1453 (shift (- (point) item
)))
1454 ;; 1.1. Remove the item just created in structure.
1455 (setq struct
(delete (assq new-item struct
) struct
))
1456 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1461 (let* ((cell (assq e struct
))
1465 (append (butlast (cdr cell
))
1466 (list (if (= end old-end
)
1470 (lambda (e1 e2
) (< (car e1
) (car e2
))))))
1471 ;; 2. Eventually delete extra copy of the item and clean marker.
1473 (org-list-delete-item (marker-position item
) struct
)
1474 (move-marker item nil
)))
1477 (defun org-list-struct-outdent (start end struct parents
)
1478 "Outdent items between positions START and END.
1480 STRUCT is the list structure. PARENTS is the alist of items'
1481 parents, as returned by `org-list-parents-alist'.
1483 START is included, END excluded."
1486 (let* ((item (car cell
))
1487 (parent (cdr cell
)))
1489 ;; Item not yet in zone: keep association.
1490 ((< item start
) cell
)
1491 ;; Item out of zone: follow associations in ACC.
1493 (let ((convert (and parent
(assq parent acc
))))
1494 (if convert
(cons item
(cdr convert
)) cell
)))
1495 ;; Item has no parent: error
1497 (error "Cannot outdent top-level items"))
1498 ;; Parent is outdented: keep association.
1500 (push (cons parent item
) acc
) cell
)
1502 ;; Parent isn't outdented: reparent to grand-parent.
1503 (let ((grand-parent (org-list-get-parent
1504 parent struct parents
)))
1505 (push (cons parent item
) acc
)
1506 (cons item grand-parent
))))))))
1507 (mapcar out parents
)))
1509 (defun org-list-struct-indent (start end struct parents prevs
)
1510 "Indent items between positions START and END.
1512 STRUCT is the list structure. PARENTS is the alist of parents
1513 and PREVS is the alist of previous items, returned by,
1514 respectively, `org-list-parents-alist' and
1515 `org-list-prevs-alist'.
1517 START is included and END excluded.
1519 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1520 bullets between START and END."
1522 (set-assoc (lambda (cell) (push cell acc
) cell
))
1523 (change-bullet-maybe
1526 (let* ((bul (org-trim (org-list-get-bullet item struct
)))
1527 (new-bul-p (cdr (assoc bul org-list-demote-modify-bullet
))))
1528 (when new-bul-p
(org-list-set-bullet item struct new-bul-p
))))))
1531 (let* ((item (car cell
))
1532 (parent (cdr cell
)))
1534 ;; Item not yet in zone: keep association.
1535 ((< item start
) cell
)
1537 ;; Item out of zone: follow associations in ACC.
1538 (let ((convert (assq parent acc
)))
1539 (if convert
(cons item
(cdr convert
)) cell
)))
1541 ;; Item is in zone...
1542 (let ((prev (org-list-get-prev-item item struct prevs
)))
1543 ;; Check if bullet needs to be changed.
1544 (funcall change-bullet-maybe item
)
1546 ;; First item indented but not parent: error
1547 ((and (not prev
) (< parent start
))
1548 (error "Cannot indent the first item of a list"))
1549 ;; First item and parent indented: keep same
1551 ((not prev
) (funcall set-assoc cell
))
1552 ;; Previous item not indented: reparent to it.
1553 ((< prev start
) (funcall set-assoc
(cons item prev
)))
1554 ;; Previous item indented: reparent like it.
1557 (cons item
(cdr (assq prev acc
)))))))))))))
1558 (mapcar ind parents
)))
1562 ;;; Repairing structures
1564 (defun org-list-use-alpha-bul-p (first struct prevs
)
1565 "Non-nil if list starting at FIRST can have alphabetical bullets.
1567 STRUCT is list structure. PREVS is the alist of previous items,
1568 as returned by `org-list-prevs-alist'."
1569 (and org-alphabetical-lists
1571 (let ((item first
) (ascii 64) (case-fold-search nil
))
1572 ;; Pretend that bullets are uppercase and check if alphabet
1573 ;; is sufficient, taking counters into account.
1575 (let ((bul (org-list-get-bullet item struct
))
1576 (count (org-list-get-counter item struct
)))
1577 ;; Virtually determine current bullet
1578 (if (and count
(string-match "[a-zA-Z]" count
))
1579 ;; Counters are not case-sensitive.
1580 (setq ascii
(string-to-char (upcase count
)))
1581 (setq ascii
(1+ ascii
)))
1582 ;; Test if bullet would be over z or Z.
1585 (setq item
(org-list-get-next-item item struct prevs
)))))
1586 ;; All items checked. All good.
1589 (defun org-list-inc-bullet-maybe (bullet)
1590 "Increment BULLET if applicable."
1591 (let ((case-fold-search nil
))
1593 ;; Num bullet: increment it.
1594 ((string-match "[0-9]+" bullet
)
1596 (number-to-string (1+ (string-to-number (match-string 0 bullet
))))
1598 ;; Alpha bullet: increment it.
1599 ((string-match "[A-Za-z]" bullet
)
1601 (char-to-string (1+ (string-to-char (match-string 0 bullet
))))
1603 ;; Unordered bullet: leave it.
1606 (defun org-list-struct-fix-bul (struct prevs
)
1607 "Verify and correct bullets in STRUCT.
1608 PREVS is the alist of previous items, as returned by
1609 `org-list-prevs-alist'.
1611 This function modifies STRUCT."
1612 (let ((case-fold-search nil
)
1615 ;; Set bullet of ITEM in STRUCT, depending on the type of
1616 ;; first item of the list, the previous bullet and counter
1619 (let* ((prev (org-list-get-prev-item item struct prevs
))
1620 (prev-bul (and prev
(org-list-get-bullet prev struct
)))
1621 (counter (org-list-get-counter item struct
))
1622 (bullet (org-list-get-bullet item struct
))
1623 (alphap (and (not prev
)
1624 (org-list-use-alpha-bul-p item struct prevs
))))
1625 (org-list-set-bullet
1627 (org-list-bullet-string
1629 ;; Alpha counter in alpha list: use counter.
1631 (string-match "[a-zA-Z]" counter
)
1632 (string-match "[a-zA-Z]" prev-bul
))
1633 ;; Use cond to be sure `string-match' is used in
1637 ((string-match "[a-z]" prev-bul
) (downcase counter
))
1638 ((string-match "[A-Z]" prev-bul
) (upcase counter
)))))
1639 (replace-match real-count nil nil prev-bul
)))
1640 ;; Num counter in a num list: use counter.
1642 (string-match "[0-9]+" counter
)
1643 (string-match "[0-9]+" prev-bul
))
1644 (replace-match counter nil nil prev-bul
))
1645 ;; No counter: increase, if needed, previous bullet.
1647 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct
)))
1648 ;; Alpha counter at first item: use counter.
1649 ((and counter
(org-list-use-alpha-bul-p item struct prevs
)
1650 (string-match "[A-Za-z]" counter
)
1651 (string-match "[A-Za-z]" bullet
))
1654 ((string-match "[a-z]" bullet
) (downcase counter
))
1655 ((string-match "[A-Z]" bullet
) (upcase counter
)))))
1656 (replace-match real-count nil nil bullet
)))
1657 ;; Num counter at first item: use counter.
1659 (string-match "[0-9]+" counter
)
1660 (string-match "[0-9]+" bullet
))
1661 (replace-match counter nil nil bullet
))
1662 ;; First bullet is alpha uppercase: use "A".
1663 ((and alphap
(string-match "[A-Z]" bullet
))
1664 (replace-match "A" nil nil bullet
))
1665 ;; First bullet is alpha lowercase: use "a".
1666 ((and alphap
(string-match "[a-z]" bullet
))
1667 (replace-match "a" nil nil bullet
))
1668 ;; First bullet is num: use "1".
1669 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet
)
1670 (replace-match "1" nil nil bullet
))
1671 ;; Not an ordered list: keep bullet.
1673 (mapc fix-bul
(mapcar 'car struct
))))
1675 (defun org-list-struct-fix-ind (struct parents
&optional bullet-size
)
1676 "Verify and correct indentation in STRUCT.
1678 PARENTS is the alist of parents, as returned by
1679 `org-list-parents-alist'.
1681 If numeric optional argument BULLET-SIZE is set, assume all
1682 bullets in list have this length to determine new indentation.
1684 This function modifies STRUCT."
1685 (let* ((ancestor (org-list-get-top-point struct
))
1686 (top-ind (org-list-get-ind ancestor struct
))
1689 (let ((parent (org-list-get-parent item struct parents
)))
1691 ;; Indent like parent + length of parent's bullet +
1694 item struct
(+ (or bullet-size
1696 (org-list-get-bullet parent struct
)))
1697 (org-list-get-ind parent struct
)
1698 org-list-indent-offset
))
1699 ;; If no parent, indent like top-point.
1700 (org-list-set-ind item struct top-ind
))))))
1701 (mapc new-ind
(mapcar 'car
(cdr struct
)))))
1703 (defun org-list-struct-fix-box (struct parents prevs
&optional ordered
)
1704 "Verify and correct checkboxes in STRUCT.
1706 PARENTS is the alist of parents and PREVS is the alist of
1707 previous items, as returned by, respectively,
1708 `org-list-parents-alist' and `org-list-prevs-alist'.
1710 If ORDERED is non-nil, a checkbox can only be checked when every
1711 checkbox before it is checked too. If there was an attempt to
1712 break this rule, the function will return the blocking item. In
1713 all others cases, the return value will be nil.
1715 This function modifies STRUCT."
1716 (let ((all-items (mapcar 'car struct
))
1721 (mapcar (lambda (child)
1722 (org-list-get-checkbox child struct
))
1723 (org-list-get-children item struct parents
))))
1724 (org-list-set-checkbox
1727 ((and (member "[ ]" box-list
) (member "[X]" box-list
)) "[-]")
1728 ((member "[-]" box-list
) "[-]")
1729 ((member "[X]" box-list
) "[X]")
1730 ((member "[ ]" box-list
) "[ ]")
1731 ;; Parent has no boxed child: leave box as-is.
1732 (t (org-list-get-checkbox item struct
))))))))
1734 ;; 1. List all parents with a checkbox.
1737 (let* ((parent (org-list-get-parent e struct parents
))
1738 (parent-box-p (org-list-get-checkbox parent struct
)))
1739 (when (and parent-box-p
(not (memq parent parent-list
)))
1740 (push parent parent-list
))))
1742 ;; 2. Sort those parents by decreasing indentation.
1743 (setq parent-list
(sort parent-list
1745 (> (org-list-get-ind e1 struct
)
1746 (org-list-get-ind e2 struct
)))))
1747 ;; 3. For each parent, get all children's checkboxes to determine
1748 ;; and set its checkbox accordingly.
1749 (mapc set-parent-box parent-list
)
1750 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1753 (mapcar (lambda (e) (org-list-get-checkbox e struct
)) all-items
))
1754 (after-unchecked (member "[ ]" box-list
)))
1755 ;; There are boxes checked after an unchecked one: fix that.
1756 (when (member "[X]" after-unchecked
)
1757 (let ((index (- (length struct
) (length after-unchecked
))))
1758 (mapc (lambda (e) (org-list-set-checkbox e struct
"[ ]"))
1759 (nthcdr index all-items
))
1760 ;; Verify once again the structure, without ORDERED.
1761 (org-list-struct-fix-box struct parents prevs nil
)
1762 ;; Return blocking item.
1763 (nth index all-items
)))))))
1765 (defun org-list-struct-apply-struct (struct old-struct
)
1766 "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
1768 OLD-STRUCT is the structure before any modifications, and STRUCT
1769 the structure to be applied. The function will only modify parts
1770 of the list which have changed.
1772 Initial position of cursor is restored after the changes."
1773 (let* ((origin (point-marker))
1774 (inlinetask-re (and (featurep 'org-inlinetask
)
1775 (org-inlinetask-outline-regexp)))
1776 (item-re (org-item-re))
1777 (box-rule-p (cdr (assq 'checkbox org-list-automatic-rules
)))
1780 ;; Shift the indentation between END and BEG by DELTA.
1781 ;; Start from the line before END.
1782 (lambda (end beg delta
)
1784 (skip-chars-backward " \r\t\n")
1786 (while (or (> (point) beg
)
1787 (and (= (point) beg
)
1788 (not (looking-at item-re
))))
1790 ;; Skip inline tasks.
1791 ((and inlinetask-re
(looking-at inlinetask-re
))
1792 (org-inlinetask-goto-beginning))
1793 ;; Shift only non-empty lines.
1794 ((org-looking-at-p "^[ \t]*\\S-")
1795 (let ((i (org-get-indentation)))
1796 (org-indent-line-to (+ i delta
)))))
1797 (forward-line -
1)))))
1800 ;; Replace ITEM first line elements with new elements from
1801 ;; STRUCT, if appropriate.
1804 (let* ((new-ind (org-list-get-ind item struct
))
1805 (old-ind (org-get-indentation))
1806 (new-bul (org-list-bullet-string
1807 (org-list-get-bullet item struct
)))
1808 (old-bul (org-list-get-bullet item old-struct
))
1809 (new-box (org-list-get-checkbox item struct
)))
1810 (looking-at org-list-full-item-re
)
1811 ;; a. Replace bullet
1812 (unless (equal old-bul new-bul
)
1813 (replace-match new-bul nil nil nil
1))
1814 ;; b. Replace checkbox.
1816 ((and new-box box-rule-p
1817 (save-match-data (org-at-item-description-p)))
1818 (message "Cannot add a checkbox to a description list item"))
1819 ((equal (match-string 3) new-box
))
1820 ((and (match-string 3) new-box
)
1821 (replace-match new-box nil nil nil
3))
1823 (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
1824 (replace-match "" nil nil nil
1))
1825 (t (let ((counterp (match-end 2)))
1826 (goto-char (if counterp
(1+ counterp
) (match-end 1)))
1827 (insert (concat new-box
(unless counterp
" "))))))
1828 ;; c. Indent item to appropriate column.
1829 (unless (= new-ind old-ind
)
1830 (delete-region (goto-char (point-at-bol))
1831 (progn (skip-chars-forward " \t") (point)))
1832 (indent-to new-ind
)))))))
1833 ;; 1. First get list of items and position endings. We maintain
1834 ;; two alists: ITM-SHIFT, determining indentation shift needed
1835 ;; at item, and END-POS, a pseudo-alist where key is ending
1836 ;; position and value point.
1837 (let (end-list acc-end itm-shift all-ends sliced-struct
)
1839 (let* ((pos (car e
))
1840 (ind-pos (org-list-get-ind pos struct
))
1841 (ind-old (org-list-get-ind pos old-struct
))
1842 (bul-pos (org-list-get-bullet pos struct
))
1843 (bul-old (org-list-get-bullet pos old-struct
))
1844 (ind-shift (- (+ ind-pos
(length bul-pos
))
1845 (+ ind-old
(length bul-old
))))
1846 (end-pos (org-list-get-item-end pos old-struct
)))
1847 (push (cons pos ind-shift
) itm-shift
)
1848 (unless (assq end-pos old-struct
)
1849 ;; To determine real ind of an ending position that
1850 ;; is not at an item, we have to find the item it
1851 ;; belongs to: it is the last item (ITEM-UP), whose
1852 ;; ending is further than the position we're
1854 (let ((item-up (assoc-default end-pos acc-end
'>)))
1855 (push (cons end-pos item-up
) end-list
)))
1856 (push (cons end-pos pos
) acc-end
)))
1858 ;; 2. Slice the items into parts that should be shifted by the
1859 ;; same amount of indentation. The slices are returned in
1860 ;; reverse order so changes modifying buffer do not change
1861 ;; positions they refer to.
1862 (setq all-ends
(sort (append (mapcar 'car itm-shift
)
1863 (org-uniquify (mapcar 'car end-list
)))
1865 (while (cdr all-ends
)
1866 (let* ((up (pop all-ends
))
1867 (down (car all-ends
))
1868 (ind (if (assq up struct
)
1869 (cdr (assq up itm-shift
))
1870 (cdr (assq (cdr (assq up end-list
)) itm-shift
)))))
1871 (push (list down up ind
) sliced-struct
)))
1872 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1873 ;; end to beginning. Take a special action when beginning is
1876 (unless (zerop (nth 2 e
)) (apply shift-body-ind e
))
1877 (let* ((beg (nth 1 e
))
1878 (cell (assq beg struct
)))
1879 (unless (or (not cell
) (equal cell
(assq beg old-struct
)))
1880 (funcall modify-item beg
))))
1882 ;; 4. Go back to initial position and clean marker.
1884 (move-marker origin nil
)))
1886 (defun org-list-write-struct (struct parents
)
1887 "Correct bullets, checkboxes and indentation in list at point.
1888 STRUCT is the list structure. PARENTS is the alist of parents,
1889 as returned by `org-list-parents-alist'."
1890 ;; Order of functions matters here: checkboxes and endings need
1891 ;; correct indentation to be set, and indentation needs correct
1894 ;; 0. Save a copy of structure before modifications
1895 (let ((old-struct (copy-tree struct
)))
1896 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
1897 ;; order to get items endings and bullets properly
1898 (org-list-struct-fix-ind struct parents
2)
1899 ;; 2. Get pseudo-alist of ending positions and sort it by position.
1900 ;; Then associate them to the structure.
1901 (let (end-list acc-end
)
1903 (let* ((pos (car e
))
1904 (ind-pos (org-list-get-ind pos struct
))
1905 (end-pos (org-list-get-item-end pos struct
)))
1906 (unless (assq end-pos struct
)
1907 ;; To determine real ind of an ending position that is
1908 ;; not at an item, we have to find the item it belongs
1909 ;; to: it is the last item (ITEM-UP), whose ending is
1910 ;; further than the position we're interested in.
1911 (let ((item-up (assoc-default end-pos acc-end
'>)))
1913 ;; Else part is for the bottom point.
1914 (if item-up
(+ (org-list-get-ind item-up struct
) 2) 0)
1917 (push (cons ind-pos pos
) end-list
)
1918 (push (cons end-pos pos
) acc-end
)))
1920 (setq end-list
(sort end-list
(lambda (e1 e2
) (< (cdr e1
) (cdr e2
)))))
1921 (org-list-struct-assoc-end struct end-list
))
1922 ;; 3. Get bullets right.
1923 (let ((prevs (org-list-prevs-alist struct
)))
1924 (org-list-struct-fix-bul struct prevs
)
1925 ;; 4. Now get real indentation.
1926 (org-list-struct-fix-ind struct parents
)
1927 ;; 5. Eventually fix checkboxes.
1928 (org-list-struct-fix-box struct parents prevs
))
1929 ;; 6. Apply structure modifications to buffer.
1930 (org-list-struct-apply-struct struct old-struct
)))
1936 (defun org-apply-on-list (function init-value
&rest args
)
1937 "Call FUNCTION on each item of the list at point.
1938 FUNCTION must be called with at least one argument: INIT-VALUE,
1939 that will contain the value returned by the function at the
1940 previous item, plus ARGS extra arguments.
1942 FUNCTION is applied on items in reverse order.
1944 As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
1945 will return the number of items in the current list.
1947 Sublists of the list are skipped. Cursor is always at the
1948 beginning of the item."
1949 (let* ((struct (org-list-struct))
1950 (prevs (org-list-prevs-alist struct
))
1951 (item (copy-marker (point-at-bol)))
1952 (all (org-list-get-all-items (marker-position item
) struct prevs
))
1956 (setq value
(apply function value args
)))
1959 (move-marker item nil
)
1962 (defun org-list-set-item-visibility (item struct view
)
1963 "Set visibility of ITEM in STRUCT to VIEW.
1965 Possible values are: `folded', `children' or `subtree'. See
1966 `org-cycle' for more information."
1969 (let ((item-end (org-list-get-item-end-before-blank item struct
)))
1971 (outline-flag-region (save-excursion (goto-char item
) (point-at-eol))
1973 ((eq view
'children
)
1974 ;; First show everything.
1975 (org-list-set-item-visibility item struct
'subtree
)
1976 ;; Then fold every child.
1977 (let* ((parents (org-list-parents-alist struct
))
1978 (children (org-list-get-children item struct parents
)))
1980 (org-list-set-item-visibility e struct
'folded
))
1984 (let ((item-end (org-list-get-item-end item struct
)))
1985 (outline-flag-region item item-end nil
)))))
1987 (defun org-list-item-body-column (item)
1988 "Return column at which body of ITEM should start."
1989 (let (bpos bcol tpos tcol
)
1992 (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?[ \t]+")
1993 (setq bpos
(match-beginning 1) tpos
(match-end 0)
1994 bcol
(progn (goto-char bpos
) (current-column))
1995 tcol
(progn (goto-char tpos
) (current-column)))
1996 (when (> tcol
(+ bcol org-description-max-indent
))
1997 (setq tcol
(+ bcol
5))))
2002 ;;; Interactive functions
2004 (defalias 'org-list-get-item-begin
'org-in-item-p
)
2006 (defun org-beginning-of-item ()
2007 "Go to the beginning of the current item.
2008 Throw an error when not in a list."
2010 (let ((begin (org-in-item-p)))
2011 (if begin
(goto-char begin
) (error "Not in an item"))))
2013 (defun org-beginning-of-item-list ()
2014 "Go to the beginning item 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-begin begin struct prevs
))))))
2025 (defun org-end-of-item-list ()
2026 "Go to the end of the current list or sublist.
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 (prevs (org-list-prevs-alist struct
)))
2035 (goto-char (org-list-get-list-end begin struct prevs
))))))
2037 (defun org-end-of-item ()
2038 "Go to the end of the current item.
2039 Throw an error when not in a list."
2041 (let ((begin (org-in-item-p)))
2043 (error "Not in an item")
2045 (let ((struct (org-list-struct)))
2046 (goto-char (org-list-get-item-end begin struct
))))))
2048 (defun org-previous-item ()
2049 "Move to the beginning of the previous item.
2050 Throw an error when not in a list. Also throw an error when at
2051 first item, unless `org-list-use-circular-motion' is non-nil."
2053 (let ((item (org-in-item-p)))
2055 (error "Not in an item")
2057 (let* ((struct (org-list-struct))
2058 (prevs (org-list-prevs-alist struct
))
2059 (prevp (org-list-get-prev-item item struct prevs
)))
2061 (prevp (goto-char prevp
))
2062 (org-list-use-circular-motion
2063 (goto-char (org-list-get-last-item item struct prevs
)))
2064 (t (error "On first item")))))))
2066 (defun org-next-item ()
2067 "Move to the beginning of the next item.
2068 Throw an error when not in a list. Also throw an error when at
2069 last item, unless `org-list-use-circular-motion' is non-nil."
2071 (let ((item (org-in-item-p)))
2073 (error "Not in an item")
2075 (let* ((struct (org-list-struct))
2076 (prevs (org-list-prevs-alist struct
))
2077 (prevp (org-list-get-next-item item struct prevs
)))
2079 (prevp (goto-char prevp
))
2080 (org-list-use-circular-motion
2081 (goto-char (org-list-get-first-item item struct prevs
)))
2082 (t (error "On last item")))))))
2084 (defun org-move-item-down ()
2085 "Move the item at point down, i.e. swap with following item.
2086 Sub-items (items with larger indentation) are considered part of
2087 the item, so this really moves item trees."
2089 (unless (org-at-item-p) (error "Not at an item"))
2090 (let* ((col (current-column))
2091 (item (point-at-bol))
2092 (struct (org-list-struct))
2093 (prevs (org-list-prevs-alist struct
))
2094 (next-item (org-list-get-next-item (point-at-bol) struct prevs
)))
2095 (unless (or next-item org-list-use-circular-motion
)
2096 (error "Cannot move this item further down"))
2098 (setq struct
(org-list-send-item item
'begin struct
))
2099 (setq struct
(org-list-swap-items item next-item struct
))
2101 (org-list-get-next-item item struct
(org-list-prevs-alist struct
))))
2102 (org-list-write-struct struct
(org-list-parents-alist struct
))
2103 (org-move-to-column col
)))
2105 (defun org-move-item-up ()
2106 "Move the item at point up, i.e. swap with previous item.
2107 Sub-items (items with larger indentation) are considered part of
2108 the item, so this really moves item trees."
2110 (unless (org-at-item-p) (error "Not at an item"))
2111 (let* ((col (current-column))
2112 (item (point-at-bol))
2113 (struct (org-list-struct))
2114 (prevs (org-list-prevs-alist struct
))
2115 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs
)))
2116 (unless (or prev-item org-list-use-circular-motion
)
2117 (error "Cannot move this item further up"))
2119 (setq struct
(org-list-send-item item
'end struct
))
2120 (setq struct
(org-list-swap-items prev-item item struct
)))
2121 (org-list-write-struct struct
(org-list-parents-alist struct
))
2122 (org-move-to-column col
)))
2124 (defun org-insert-item (&optional checkbox
)
2125 "Insert a new item at the current level.
2126 If cursor is before first character after bullet of the item, the
2127 new item will be created before the current one.
2129 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2131 Return t when things worked, nil when we are not in an item, or
2133 (let ((itemp (org-in-item-p))
2135 ;; If cursor isn't is a list or if list is invisible, return nil.
2136 (unless (or (not itemp
)
2139 (outline-invisible-p)))
2142 (org-at-item-timer-p))
2143 ;; Timer list: delegate to `org-timer-item'.
2144 (progn (org-timer-item) t
)
2145 (let* ((struct (save-excursion (goto-char itemp
)
2147 (prevs (org-list-prevs-alist struct
))
2148 ;; If we're in a description list, ask for the new term.
2149 (desc (when (org-list-get-tag itemp struct
)
2150 (concat (read-string "Term: ") " :: ")))
2151 ;; Don't insert a checkbox if checkbox rule is applied
2152 ;; and it is a description item.
2153 (checkp (and checkbox
2155 (not (cdr (assq 'checkbox
2156 org-list-automatic-rules
)))))))
2158 (org-list-insert-item pos struct prevs checkp desc
))
2159 (org-list-write-struct struct
(org-list-parents-alist struct
))
2160 (when checkp
(org-update-checkbox-count-maybe))
2161 (looking-at org-list-full-item-re
)
2162 (goto-char (match-end 0))
2165 (defun org-list-repair ()
2166 "Fix indentation, bullets and checkboxes is the list at point."
2168 (unless (org-at-item-p) (error "This is not a list"))
2169 (let* ((struct (org-list-struct))
2170 (parents (org-list-parents-alist struct
)))
2171 (org-list-write-struct struct parents
)))
2173 (defun org-cycle-list-bullet (&optional which
)
2174 "Cycle through the different itemize/enumerate bullets.
2175 This cycle the entire list level through the sequence:
2177 `-' -> `+' -> `*' -> `1.' -> `1)'
2179 If WHICH is a valid string, use that as the new bullet. If WHICH
2180 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2181 `previous', cycle backwards."
2183 (unless (org-at-item-p) (error "Not at an item"))
2186 (let* ((struct (org-list-struct))
2187 (parents (org-list-parents-alist struct
))
2188 (prevs (org-list-prevs-alist struct
))
2189 (list-beg (org-list-get-first-item (point) struct prevs
))
2190 (bullet (org-list-get-bullet list-beg struct
))
2191 (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules
)))
2192 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs
))
2193 (case-fold-search nil
)
2195 ((string-match "[a-z]\\." bullet
) "a.")
2196 ((string-match "[a-z])" bullet
) "a)")
2197 ((string-match "[A-Z]\\." bullet
) "A.")
2198 ((string-match "[A-Z])" bullet
) "A)")
2199 ((string-match "\\." bullet
) "1.")
2200 ((string-match ")" bullet
) "1)")
2201 (t (org-trim bullet
))))
2202 ;; Compute list of possible bullets, depending on context.
2205 ;; *-bullets are not allowed at column 0.
2206 (unless (and bullet-rule-p
2207 (looking-at "\\S-")) '("*"))
2208 ;; Description items cannot be numbered.
2209 (unless (or (eq org-plain-list-ordered-item-terminator ?\
))
2210 (and bullet-rule-p
(org-at-item-description-p)))
2212 (unless (or (eq org-plain-list-ordered-item-terminator ?.
)
2213 (and bullet-rule-p
(org-at-item-description-p)))
2215 (unless (or (not alpha-p
)
2216 (eq org-plain-list-ordered-item-terminator ?\
))
2217 (and bullet-rule-p
(org-at-item-description-p)))
2219 (unless (or (not alpha-p
)
2220 (eq org-plain-list-ordered-item-terminator ?.
)
2221 (and bullet-rule-p
(org-at-item-description-p)))
2223 (len (length bullet-list
))
2224 (item-index (- len
(length (member current bullet-list
))))
2225 (get-value (lambda (index) (nth (mod index len
) bullet-list
)))
2227 ((member which bullet-list
) which
)
2228 ((numberp which
) (funcall get-value which
))
2229 ((eq 'previous which
) (funcall get-value
(1- item-index
)))
2230 (t (funcall get-value
(1+ item-index
))))))
2231 ;; Use a short variation of `org-list-write-struct' as there's
2232 ;; no need to go through all the steps.
2233 (let ((old-struct (copy-tree struct
)))
2234 (org-list-set-bullet list-beg struct
(org-list-bullet-string new
))
2235 (org-list-struct-fix-bul struct prevs
)
2236 (org-list-struct-fix-ind struct parents
)
2237 (org-list-struct-apply-struct struct old-struct
)))))
2239 (defun org-toggle-checkbox (&optional toggle-presence
)
2240 "Toggle the checkbox in the current line.
2241 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2242 double prefix, set checkbox to [-].
2244 When there is an active region, toggle status or presence of the
2245 first checkbox there, and make every item inside have the same
2246 status or presence, respectively.
2248 If the cursor is in a headline, apply this to all checkbox items
2249 in the text below the heading, taking as reference the first item
2250 in subtree, ignoring drawers."
2257 (drawer-re (concat "^[ \t]*:\\("
2258 (mapconcat 'regexp-quote org-drawers
"\\|")
2260 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2261 "\\|" org-deadline-string
2262 "\\|" org-closed-string
2263 "\\|" org-clock-string
"\\)"
2264 " *[[<]\\([^]>]+\\)[]>]"))
2265 (orderedp (org-entry-get nil
"ORDERED"))
2267 ;; In a region, start at first item in region.
2269 ((org-region-active-p)
2270 (let ((limit (region-end)))
2271 (goto-char (region-beginning))
2272 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2273 (setq lim-up
(point-at-bol))
2274 (error "No item in region"))
2275 (setq lim-down
(copy-marker limit
))))
2277 ;; On an heading, start at first item after drawers and
2278 ;; time-stamps (scheduled, etc.).
2279 (let ((limit (save-excursion (outline-next-heading) (point))))
2281 (while (or (looking-at drawer-re
) (looking-at keyword-re
))
2282 (if (looking-at keyword-re
)
2284 (re-search-forward "^[ \t]*:END:" limit nil
)))
2285 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2286 (setq lim-up
(point-at-bol))
2287 (error "No item in subtree"))
2288 (setq lim-down
(copy-marker limit
))))
2289 ;; Just one item: set SINGLEP flag.
2292 (setq lim-up
(point-at-bol)
2293 lim-down
(copy-marker (point-at-eol))))
2294 (t (error "Not at an item or heading, and no active region"))))
2295 ;; Determine the checkbox going to be applied to all items
2300 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2302 ((equal toggle-presence
'(16)) "[-]")
2303 ((equal toggle-presence
'(4))
2304 (unless cbox
"[ ]"))
2305 ((equal "[X]" cbox
) "[ ]")
2307 ;; When an item is found within bounds, grab the full list at
2308 ;; point structure, then: (1) set check-box of all its items
2309 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2310 ;; whole list, (3) move point after the list.
2312 (while (and (< (point) lim-down
)
2313 (org-list-search-forward (org-item-beginning-re)
2315 (let* ((struct (org-list-struct))
2316 (struct-copy (copy-tree struct
))
2317 (parents (org-list-parents-alist struct
))
2318 (prevs (org-list-prevs-alist struct
))
2319 (bottom (copy-marker (org-list-get-bottom-point struct
)))
2320 (items-to-toggle (org-remove-if
2321 (lambda (e) (or (< e lim-up
) (> e lim-down
)))
2322 (mapcar 'car struct
))))
2323 (mapc (lambda (e) (org-list-set-checkbox
2325 ;; If there is no box at item, leave as-is
2326 ;; unless function was called with C-u prefix.
2327 (let ((cur-box (org-list-get-checkbox e struct
)))
2328 (if (or cur-box
(equal toggle-presence
'(4)))
2332 (setq block-item
(org-list-struct-fix-box
2333 struct parents prevs orderedp
))
2334 ;; Report some problems due to ORDERED status of subtree.
2335 ;; If only one box was being checked, throw an error, else,
2336 ;; only signal problems.
2338 ((and singlep block-item
(> lim-up block-item
))
2340 "Checkbox blocked because of unchecked box at line %d"
2341 (org-current-line block-item
)))
2344 "Checkboxes were removed due to unchecked box at line %d"
2345 (org-current-line block-item
))))
2347 (move-marker bottom nil
)
2348 (org-list-struct-apply-struct struct struct-copy
)))
2349 (move-marker lim-down nil
)))
2350 (org-update-checkbox-count-maybe))
2352 (defun org-reset-checkbox-state-subtree ()
2353 "Reset all checkboxes in an entry subtree."
2355 (if (org-before-first-heading-p)
2356 (error "Not inside a tree")
2359 (org-narrow-to-subtree)
2361 (goto-char (point-min))
2362 (let ((end (point-max)))
2363 (while (< (point) end
)
2364 (when (org-at-item-checkbox-p)
2365 (replace-match "[ ]" t t nil
1))
2366 (beginning-of-line 2)))
2367 (org-update-checkbox-count-maybe 'all
)))))
2369 (defun org-update-checkbox-count (&optional all
)
2370 "Update the checkbox statistics in the current section.
2371 This will find all statistic cookies like [57%] and [6/12] and
2372 update them with the current numbers.
2374 With optional prefix argument ALL, do this for the whole buffer."
2377 (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2378 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2380 (or (not org-hierarchical-checkbox-statistics
)
2381 (string-match "\\<recursive\\>"
2382 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2384 (cons (point-min) (point-max))
2385 (cons (or (ignore-errors (org-back-to-heading t
) (point))
2387 (save-excursion (outline-next-heading) (point)))))
2390 ;; Return number of checked boxes and boxes of all types
2391 ;; in all structures in STRUCTS. If RECURSIVEP is
2392 ;; non-nil, also count boxes in sub-lists. If ITEM is
2393 ;; nil, count across the whole structure, else count only
2394 ;; across subtree whose ancestor is ITEM.
2395 (lambda (item structs recursivep
)
2396 (let ((c-on 0) (c-all 0))
2399 (let* ((pre (org-list-prevs-alist s
))
2400 (par (org-list-parents-alist s
))
2403 ((and recursivep item
) (org-list-get-subtree item s
))
2404 (recursivep (mapcar 'car s
))
2405 (item (org-list-get-children item s par
))
2406 (t (org-list-get-all-items
2407 (org-list-get-top-point s
) s pre
))))
2408 (cookies (delq nil
(mapcar
2410 (org-list-get-checkbox e s
))
2412 (setq c-all
(+ (length cookies
) c-all
)
2413 c-on
(+ (org-count "[X]" cookies
) c-on
))))
2415 (cons c-on c-all
)))))
2417 cookies-list structs-bak box-num
)
2418 (goto-char (car bounds
))
2419 ;; 1. Build an alist for each cookie found within BOUNDS. The
2420 ;; key will be position at beginning of cookie and values
2421 ;; ending position, format of cookie, and a cell whose car is
2422 ;; number of checked boxes to report, and cdr total number of
2424 (while (re-search-forward cookie-re
(cdr bounds
) t
)
2429 (match-beginning 1) ; cookie start
2430 (match-end 1) ; cookie end
2431 (match-string 2) ; percent?
2433 ;; Cookie is at an heading, but specifically for todo,
2434 ;; not for checkboxes: skip it.
2435 ((and (org-on-heading-p)
2436 (string-match "\\<todo\\>"
2438 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2440 ;; Cookie is at an heading, but all lists before next
2441 ;; heading already have been read. Use data collected
2442 ;; in STRUCTS-BAK. This should only happen when
2443 ;; heading has more than one cookie on it.
2444 ((and (org-on-heading-p)
2445 (<= (save-excursion (outline-next-heading) (point))
2447 (funcall count-boxes nil structs-bak recursivep
))
2448 ;; Cookie is at a fresh heading. Grab structure of
2449 ;; every list containing a checkbox between point and
2450 ;; next headline, and save them in STRUCTS-BAK.
2452 (setq backup-end
(save-excursion
2453 (outline-next-heading) (point))
2455 (while (org-list-search-forward box-re backup-end
'move
)
2456 (let* ((struct (org-list-struct))
2457 (bottom (org-list-get-bottom-point struct
)))
2458 (push struct structs-bak
)
2459 (goto-char bottom
)))
2460 (funcall count-boxes nil structs-bak recursivep
))
2461 ;; Cookie is at an item, and we already have list
2462 ;; structure stored in STRUCTS-BAK.
2463 ((and (org-at-item-p)
2464 (< (point-at-bol) backup-end
)
2465 ;; Only lists in no special context are stored.
2466 (not (nth 2 (org-list-context))))
2467 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2468 ;; Cookie is at an item, but we need to compute list
2471 (let ((struct (org-list-struct)))
2472 (setq backup-end
(org-list-get-bottom-point struct
)
2473 structs-bak
(list struct
)))
2474 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2475 ;; Else, cookie found is at a wrong place. Skip it.
2476 (t (throw 'skip nil
))))
2478 ;; 2. Apply alist to buffer, in reverse order so positions stay
2479 ;; unchanged after cookie modifications.
2480 (mapc (lambda (cookie)
2481 (let* ((beg (car cookie
))
2482 (end (nth 1 cookie
))
2483 (percentp (nth 2 cookie
))
2484 (checked (car (nth 3 cookie
)))
2485 (total (cdr (nth 3 cookie
)))
2487 (format "[%d%%]" (/ (* 100 checked
)
2489 (format "[%d/%d]" checked total
))))
2492 (delete-region (point) (+ (point) (- end beg
)))
2493 (when org-auto-align-tags
(org-fix-tags-on-the-fly))))
2496 (defun org-get-checkbox-statistics-face ()
2497 "Select the face for checkbox statistics.
2498 The face will be `org-done' when all relevant boxes are checked.
2499 Otherwise it will be `org-todo'."
2501 (if (equal (match-string 1) "100%")
2502 'org-checkbox-statistics-done
2503 'org-checkbox-statistics-todo
)
2504 (if (and (> (match-end 2) (match-beginning 2))
2505 (equal (match-string 2) (match-string 3)))
2506 'org-checkbox-statistics-done
2507 'org-checkbox-statistics-todo
)))
2509 (defun org-update-checkbox-count-maybe (&optional all
)
2510 "Update checkbox statistics unless turned off by user.
2511 With an optional argument ALL, update them in the whole buffer."
2512 (when (cdr (assq 'checkbox org-list-automatic-rules
))
2513 (org-update-checkbox-count all
))
2514 (run-hooks 'org-checkbox-statistics-hook
))
2516 (defvar org-last-indent-begin-marker
(make-marker))
2517 (defvar org-last-indent-end-marker
(make-marker))
2518 (defun org-list-indent-item-generic (arg no-subtree struct
)
2519 "Indent a local list item including its children.
2520 When number ARG is a negative, item will be outdented, otherwise
2521 it will be indented.
2523 If a region is active, all items inside will be moved.
2525 If NO-SUBTREE is non-nil, only indent the item itself, not its
2528 STRUCT is the list structure.
2530 Return t if successful."
2533 (let* ((regionp (org-region-active-p))
2534 (rbeg (and regionp
(region-beginning)))
2535 (rend (and regionp
(region-end)))
2536 (top (org-list-get-top-point struct
))
2537 (parents (org-list-parents-alist struct
))
2538 (prevs (org-list-prevs-alist struct
))
2539 ;; Are we going to move the whole list?
2541 (and (= top
(point))
2542 (cdr (assq 'indent org-list-automatic-rules
))
2545 "First item of list cannot move without its subtree")
2547 ;; Determine begin and end points of zone to indent. If moving
2548 ;; more than one item, save them for subsequent moves.
2549 (unless (and (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
2550 (memq this-command
'(org-shiftmetaright org-shiftmetaleft
)))
2553 (set-marker org-last-indent-begin-marker rbeg
)
2554 (set-marker org-last-indent-end-marker rend
))
2555 (set-marker org-last-indent-begin-marker
(point))
2556 (set-marker org-last-indent-end-marker
2558 (specialp (org-list-get-bottom-point struct
))
2559 (no-subtree (1+ (point)))
2560 (t (org-list-get-item-end (point) struct
))))))
2561 (let* ((beg (marker-position org-last-indent-begin-marker
))
2562 (end (marker-position org-last-indent-end-marker
)))
2564 ;; Special case: moving top-item with indent rule.
2566 (let* ((level-skip (org-level-increment))
2567 (offset (if (< arg
0) (- level-skip
) level-skip
))
2568 (top-ind (org-list-get-ind beg struct
))
2569 (old-struct (copy-tree struct
)))
2570 (if (< (+ top-ind offset
) 0)
2571 (error "Cannot outdent beyond margin")
2572 ;; Change bullet if necessary.
2573 (when (and (= (+ top-ind offset
) 0)
2575 (org-list-get-bullet beg struct
)))
2576 (org-list-set-bullet beg struct
2577 (org-list-bullet-string "-")))
2578 ;; Shift every item by OFFSET and fix bullets. Then
2579 ;; apply changes to buffer.
2581 (let ((ind (org-list-get-ind (car e
) struct
)))
2582 (org-list-set-ind (car e
) struct
(+ ind offset
))))
2584 (org-list-struct-fix-bul struct prevs
)
2585 (org-list-struct-apply-struct struct old-struct
))))
2588 ;; If only one item is moved, it mustn't have a child.
2591 (org-list-has-child-p beg struct
))
2592 ;; If a subtree or region is moved, the last item
2593 ;; of the subtree mustn't have a child.
2594 (let ((last-item (caar
2597 (lambda (e) (>= (car e
) end
))
2599 (org-list-has-child-p last-item struct
))))
2600 (error "Cannot outdent an item without its children"))
2605 (org-list-struct-outdent beg end struct parents
)
2606 (org-list-struct-indent beg end struct parents prevs
))))
2607 (org-list-write-struct struct new-parents
))
2608 (org-update-checkbox-count-maybe))))))
2611 (defun org-outdent-item ()
2612 "Outdent a local list item, but not its children.
2613 If a region is active, all items inside will be moved."
2616 (let ((struct (org-list-struct)))
2617 (org-list-indent-item-generic -
1 t struct
))
2618 (error "Not at an item")))
2620 (defun org-indent-item ()
2621 "Indent a local list item, but not its children.
2622 If a region is active, all items inside will be moved."
2625 (let ((struct (org-list-struct)))
2626 (org-list-indent-item-generic 1 t struct
))
2627 (error "Not at an item")))
2629 (defun org-outdent-item-tree ()
2630 "Outdent a local list item including its children.
2631 If a region is active, all items inside will be moved."
2633 (let ((regionp (org-region-active-p)))
2635 ((or (org-at-item-p)
2636 (and (org-region-active-p)
2637 (goto-char (region-beginning))
2639 (let ((struct (org-list-struct)))
2640 (org-list-indent-item-generic -
1 nil struct
)))
2641 (regionp (error "Region not starting at an item"))
2642 (t (error "Not at an item")))))
2644 (defun org-indent-item-tree ()
2645 "Indent a local list item including its children.
2646 If a region is active, all items inside will be moved."
2648 (let ((regionp (org-region-active-p)))
2650 ((or (org-at-item-p)
2651 (and (org-region-active-p)
2652 (goto-char (region-beginning))
2654 (let ((struct (org-list-struct)))
2655 (org-list-indent-item-generic 1 nil struct
)))
2656 (regionp (error "Region not starting at an item"))
2657 (t (error "Not at an item")))))
2659 (defvar org-tab-ind-state
)
2660 (defun org-cycle-item-indentation ()
2661 "Cycle levels of indentation of an empty item.
2662 The first run indents the item, if applicable. Subsequents runs
2663 outdent it at meaningful levels in the list. When done, item is
2664 put back at its original position with its original bullet.
2666 Return t at each successful move."
2667 (when (org-at-item-p)
2668 (let* ((org-adapt-indentation nil
)
2669 (struct (org-list-struct))
2670 (ind (org-list-get-ind (point-at-bol) struct
))
2671 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2672 ;; Accept empty items or if cycle has already started.
2673 (when (or (eq last-command
'org-cycle-item-indentation
)
2674 (and (save-excursion
2676 (looking-at org-list-full-item-re
))
2677 (>= (match-end 0) (save-excursion
2678 (goto-char (org-list-get-item-end
2679 (point-at-bol) struct
))
2680 (skip-chars-backward " \r\t\n")
2682 (setq this-command
'org-cycle-item-indentation
)
2683 ;; When in the middle of the cycle, try to outdent first. If
2684 ;; it fails, and point is still at initial position, indent.
2685 ;; Else, re-create it at its original position.
2686 (if (eq last-command
'org-cycle-item-indentation
)
2688 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2689 ((and (= ind
(car org-tab-ind-state
))
2690 (ignore-errors (org-list-indent-item-generic 1 t struct
))))
2691 (t (delete-region (point-at-bol) (point-at-eol))
2692 (org-indent-to-column (car org-tab-ind-state
))
2693 (insert (cdr org-tab-ind-state
) " ")
2695 (setq this-command
'identity
)))
2696 ;; If a cycle is starting, remember indentation and bullet,
2697 ;; then try to indent. If it fails, try to outdent.
2698 (setq org-tab-ind-state
(cons ind bullet
))
2700 ((ignore-errors (org-list-indent-item-generic 1 t struct
)))
2701 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2702 (t (error "Cannot move item"))))
2705 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func
)
2707 The cursor may be at any item of the list that should be sorted.
2708 Sublists are not sorted. Checkboxes, if any, are ignored.
2710 Sorting can be alphabetically, numerically, by date/time as given
2711 by a time stamp, by a property or by priority.
2713 Comparing entries ignores case by default. However, with an
2714 optional argument WITH-CASE, the sorting considers case as well.
2716 The command prompts for the sorting type unless it has been given
2717 to the function through the SORTING-TYPE argument, which needs to
2718 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
2719 meaning of each character:
2721 n Numerically, by converting the beginning of the item to a number.
2722 a Alphabetically. Only the first line of item is checked.
2723 t By date/time, either the first active time stamp in the entry, if
2724 any, or by the first inactive one. In a timer list, sort the timers.
2726 Capital letters will reverse the sort order.
2728 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2729 a function to be called with point at the beginning of the
2730 record. It must return either a string or a number that should
2731 serve as the sorting key for that record. It will then use
2732 COMPARE-FUNC to compare entries."
2734 (let* ((case-func (if with-case
'identity
'downcase
))
2735 (struct (org-list-struct))
2736 (prevs (org-list-prevs-alist struct
))
2737 (start (org-list-get-list-begin (point-at-bol) struct prevs
))
2738 (end (org-list-get-list-end (point-at-bol) struct prevs
))
2742 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
2743 (read-char-exclusive)))
2744 (getkey-func (and (= (downcase sorting-type
) ?f
)
2745 (intern (org-icompleting-read "Sort using function: "
2746 obarray
'fboundp t nil nil
)))))
2747 (message "Sorting items...")
2749 (narrow-to-region start end
)
2750 (goto-char (point-min))
2751 (let* ((dcst (downcase sorting-type
))
2752 (case-fold-search nil
)
2753 (now (current-time))
2755 ((= dcst ?a
) 'string
<)
2756 ((= dcst ?f
) compare-func
)
2759 (next-record (lambda ()
2760 (skip-chars-forward " \r\t\n")
2761 (beginning-of-line)))
2762 (end-record (lambda ()
2763 (goto-char (org-list-get-item-end-before-blank
2767 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2770 (string-to-number (buffer-substring (match-end 0)
2774 (buffer-substring (match-end 0) (point-at-eol))))
2777 ;; If it is a timer list, convert timer to seconds
2778 ((org-at-item-timer-p)
2779 (org-timer-hms-to-secs (match-string 1)))
2780 ((or (re-search-forward org-ts-regexp
(point-at-eol) t
)
2781 (re-search-forward org-ts-regexp-both
2783 (org-time-string-to-seconds (match-string 0)))
2784 (t (org-float-time now
))))
2787 (let ((value (funcall getkey-func
)))
2789 (funcall case-func value
)
2791 (error "Invalid key function `%s'" getkey-func
)))
2792 (t (error "Invalid sorting type `%c'" sorting-type
)))))))
2793 (sort-subr (/= dcst sorting-type
)
2799 ;; Read and fix list again, as `sort-subr' probably destroyed
2802 (run-hooks 'org-after-sorting-entries-or-items-hook
)
2803 (message "Sorting items...done")))))
2807 ;;; Send and receive lists
2809 (defun org-list-parse-list (&optional delete
)
2810 "Parse the list at point and maybe DELETE it.
2812 Return a list whose car is a symbol of list type, among
2813 `ordered', `unordered' and `descriptive'. Then, each item is
2814 a list whose car is counter, and cdr are strings and other
2815 sub-lists. Inside strings, check-boxes are replaced by
2816 \"[CBON]\", \"[CBOFF]\" and \"[CBTRANS]\".
2818 For example, the following list:
2823 more text in first item
2829 \(nil \"first item\"
2831 \(nil \"sub-item one\"\)
2832 \(nil \"[CBON] sub-item two\"\)\)
2833 \"more text in first item\"\)
2834 \(3 \"last item\"\)\)
2836 Point is left at list end."
2837 (let* ((struct (org-list-struct))
2838 (prevs (org-list-prevs-alist struct
))
2839 (parents (org-list-parents-alist struct
))
2840 (top (org-list-get-top-point struct
))
2841 (bottom (org-list-get-bottom-point struct
))
2843 parse-item
; for byte-compiler
2846 ;; Return text between BEG and END, trimmed, with
2847 ;; checkboxes replaced.
2849 (let ((text (org-trim (buffer-substring beg end
))))
2850 (if (string-match "\\`\\[\\([-X ]\\)\\]" text
)
2852 (let ((box (match-string 1 text
)))
2854 ((equal box
" ") "CBOFF")
2855 ((equal box
"-") "CBTRANS")
2861 ;; Return a list whose car is list type and cdr a list of
2864 (cons (org-list-get-list-type (car e
) struct prevs
)
2865 (mapcar parse-item e
)))))
2868 ;; Return a list containing counter of item, if any, text
2869 ;; and any sublist inside it.
2871 (let ((start (save-excursion
2873 (looking-at "[ \t]*\\S-+\\([ \t]+\\[@\\(start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\]\\)?[ \t]*")
2875 ;; Get counter number. For alphabetic counter, get
2876 ;; its position in the alphabet.
2877 (counter (let ((c (org-list-get-counter e struct
)))
2880 ((string-match "[A-Za-z]" c
)
2881 (- (string-to-char (upcase (match-string 0 c
)))
2883 ((string-match "[0-9]+" c
)
2884 (string-to-number (match-string 0 c
))))))
2885 (childp (org-list-has-child-p e struct
))
2886 (end (org-list-get-item-end e struct
)))
2887 ;; If item has a child, store text between bullet and
2888 ;; next child, then recursively parse all sublists. At
2889 ;; the end of each sublist, check for the presence of
2890 ;; text belonging to the original item.
2892 (let* ((children (org-list-get-children e struct parents
))
2893 (body (list (funcall get-text start childp
))))
2895 (let* ((first (car children
))
2896 (sub (org-list-get-all-items first struct prevs
))
2897 (last-c (car (last sub
)))
2898 (last-end (org-list-get-item-end last-c struct
)))
2899 (push (funcall parse-sublist sub
) body
)
2900 ;; Remove children from the list just parsed.
2901 (setq children
(cdr (member last-c children
)))
2902 ;; There is a chunk of text belonging to the
2903 ;; item if last child doesn't end where next
2904 ;; child starts or where item ends.
2905 (unless (= (or (car children
) end
) last-end
)
2906 (push (funcall get-text
2907 last-end
(or (car children
) end
))
2909 (cons counter
(nreverse body
)))
2910 (list counter
(funcall get-text start end
))))))))
2911 ;; Store output, take care of cursor position and deletion of
2912 ;; list, then return output.
2913 (setq out
(funcall parse-sublist
(org-list-get-all-items top struct prevs
)))
2916 (delete-region top bottom
)
2917 (when (and (not (eq org-list-ending-method
'indent
))
2918 (not (looking-at "[ \t]*$"))
2919 (looking-at org-list-end-re
))
2920 (replace-match "")))
2923 (defun org-list-make-subtree ()
2924 "Convert the plain list at point into a subtree."
2926 (if (not (ignore-errors (goto-char (org-in-item-p))))
2927 (error "Not in a list")
2928 (let ((list (save-excursion (org-list-parse-list t
))))
2929 (insert (org-list-to-subtree list
)))))
2931 (defun org-list-insert-radio-list ()
2932 "Insert a radio list template appropriate for this major mode."
2934 (let* ((e (assq major-mode org-list-radio-list-templates
))
2937 (unless e
(error "No radio list setup defined for %s" major-mode
))
2938 (setq name
(read-string "List name: "))
2939 (while (string-match "%n" txt
)
2940 (setq txt
(replace-match name t t txt
)))
2941 (or (bolp) (insert "\n"))
2946 (defun org-list-send-list (&optional maybe
)
2947 "Send a transformed version of this list to the receiver position.
2948 With argument MAYBE, fail quietly if no transformation is defined
2952 (unless (org-at-item-p) (error "Not at a list item"))
2954 (re-search-backward "#\\+ORGLST" nil t
)
2955 (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
2958 (error "Don't know how to transform this list"))))
2959 (let* ((name (match-string 1))
2960 (transform (intern (match-string 2)))
2964 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t
)
2965 (match-beginning 0)))
2968 (re-search-backward "#\\+ORGLST" nil t
)
2969 (re-search-forward (org-item-beginning-re) bottom-point t
)
2970 (match-beginning 0)))
2971 (list (save-restriction
2972 (narrow-to-region top-point bottom-point
)
2973 (org-list-parse-list)))
2975 (unless (fboundp transform
)
2976 (error "No such transformation function %s" transform
))
2977 (let ((txt (funcall transform list
)))
2978 ;; Find the insertion place
2980 (goto-char (point-min))
2981 (unless (re-search-forward
2982 (concat "BEGIN RECEIVE ORGLST +"
2984 "\\([ \t]\\|$\\)") nil t
)
2985 (error "Don't know where to insert translated list"))
2986 (goto-char (match-beginning 0))
2987 (beginning-of-line 2)
2989 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
2990 (error "Cannot find end of insertion region"))
2991 (delete-region beg
(point-at-bol))
2994 (message "List converted and installed at receiver location"))))
2996 (defsubst org-list-item-trim-br
(item)
2997 "Trim line breaks in a list ITEM."
2998 (setq item
(replace-regexp-in-string "\n +" " " item
)))
3000 (defun org-list-to-generic (list params
)
3001 "Convert a LIST parsed through `org-list-parse-list' to other formats.
3002 Valid parameters PARAMS are:
3004 :ustart String to start an unordered list
3005 :uend String to end an unordered list
3007 :ostart String to start an ordered list
3008 :oend String to end an ordered list
3010 :dstart String to start a descriptive list
3011 :dend String to end a descriptive list
3012 :dtstart String to start a descriptive term
3013 :dtend String to end a descriptive term
3014 :ddstart String to start a description
3015 :ddend String to end a description
3017 :splice When set to t, return only list body lines, don't wrap
3018 them into :[u/o]start and :[u/o]end. Default is nil.
3020 :istart String to start a list item.
3021 :icount String to start an item with a counter.
3022 :iend String to end a list item
3023 :isep String to separate items
3024 :lsep String to separate sublists
3025 :csep String to separate text from a sub-list
3027 :cboff String to insert for an unchecked check-box
3028 :cbon String to insert for a checked check-box
3029 :cbtrans String to insert for a check-box in transitional state
3031 :nobr Non-nil means remove line breaks in lists items.
3033 Alternatively, each parameter can also be a form returning
3034 a string. These sexp can use keywords `counter' and `depth',
3035 reprensenting respectively counter associated to the current
3036 item, and depth of the current sub-list, starting at 0.
3037 Obviously, `counter' is only available for parameters applying to
3041 (splicep (plist-get p
:splice
))
3042 (ostart (plist-get p
:ostart
))
3043 (oend (plist-get p
:oend
))
3044 (ustart (plist-get p
:ustart
))
3045 (uend (plist-get p
:uend
))
3046 (dstart (plist-get p
:dstart
))
3047 (dend (plist-get p
:dend
))
3048 (dtstart (plist-get p
:dtstart
))
3049 (dtend (plist-get p
:dtend
))
3050 (ddstart (plist-get p
:ddstart
))
3051 (ddend (plist-get p
:ddend
))
3052 (istart (plist-get p
:istart
))
3053 (icount (plist-get p
:icount
))
3054 (iend (plist-get p
:iend
))
3055 (isep (plist-get p
:isep
))
3056 (lsep (plist-get p
:lsep
))
3057 (csep (plist-get p
:csep
))
3058 (cbon (plist-get p
:cbon
))
3059 (cboff (plist-get p
:cboff
))
3060 (cbtrans (plist-get p
:cbtrans
))
3061 (nobr (plist-get p
:nobr
))
3062 export-sublist
; for byte-compiler
3065 ;; Export an item ITEM of type TYPE, at DEPTH. First
3066 ;; string in item is treated in a special way as it can
3067 ;; bring extra information that needs to be processed.
3068 (lambda (item type depth
)
3069 (let* ((counter (pop item
))
3072 ((eq type
'descriptive
)
3073 ;; Stick DTSTART to ISTART by
3074 ;; left-trimming the latter.
3075 (concat (let ((s (eval istart
)))
3076 (or (and (string-match "[ \t\n\r]+\\'" s
)
3077 (replace-match "" t t s
))
3080 ((and counter
(eq type
'ordered
))
3081 (concat (eval icount
) "%s"))
3082 (t (concat (eval istart
) "%s")))
3085 ;; Replace checkbox if any is found.
3087 ((string-match "\\[CBON\\]" first
)
3088 (setq first
(replace-match cbon t t first
)))
3089 ((string-match "\\[CBOFF\\]" first
)
3090 (setq first
(replace-match cboff t t first
)))
3091 ((string-match "\\[CBTRANS\\]" first
)
3092 (setq first
(replace-match cbtrans t t first
))))
3093 ;; Replace line breaks if required
3094 (when nobr
(setq first
(org-list-item-trim-br first
)))
3095 ;; Insert descriptive term if TYPE is `descriptive'.
3096 (when (eq type
'descriptive
)
3097 (let* ((complete (string-match "^\\(.*\\)[ \t]+::" first
))
3100 (org-trim (match-string 1 first
)))
3103 (org-trim (substring first
(match-end 0)))
3105 (setq first
(concat (eval dtstart
) term
(eval dtend
)
3106 (eval ddstart
) desc
))))
3109 (mapconcat (lambda (e)
3111 (funcall export-sublist e
(1+ depth
))))
3112 item
(or (eval csep
) "")))))))
3115 ;; Export sublist SUB at DEPTH.
3117 (let* ((type (car sub
))
3122 (concat (eval ostart
) "%s" (eval oend
)))
3123 ((eq type
'descriptive
)
3124 (concat (eval dstart
) "%s" (eval dend
)))
3125 (t (concat (eval ustart
) "%s" (eval uend
))))
3127 (format fmt
(mapconcat (lambda (e)
3128 (funcall export-item e type depth
))
3129 items
(or (eval isep
) ""))))))))
3130 (concat (funcall export-sublist list
0) "\n")))
3132 (defun org-list-to-latex (list &optional params
)
3133 "Convert LIST into a LaTeX list.
3134 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3135 with overruling parameters for `org-list-to-generic'."
3136 (org-list-to-generic
3139 '(:splice nil
:ostart
"\\begin{enumerate}\n" :oend
"\\end{enumerate}"
3140 :ustart
"\\begin{itemize}\n" :uend
"\\end{itemize}"
3141 :dstart
"\\begin{description}\n" :dend
"\\end{description}"
3142 :dtstart
"[" :dtend
"] "
3143 :istart
"\\item " :iend
"\n"
3144 :icount
(let ((enum (nth depth
'("i" "ii" "iii" "iv"))))
3146 ;; LaTeX increments counter just before
3147 ;; using it, so set it to the desired
3148 ;; value, minus one.
3149 (format "\\setcounter{enum%s}{%s}\n\\item "
3153 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}"
3154 :cbtrans
"\\texttt{[-]}")
3157 (defun org-list-to-html (list &optional params
)
3158 "Convert LIST into a HTML list.
3159 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3160 with overruling parameters for `org-list-to-generic'."
3161 (org-list-to-generic
3164 '(:splice nil
:ostart
"<ol>\n" :oend
"\n</ol>"
3165 :ustart
"<ul>\n" :uend
"\n</ul>"
3166 :dstart
"<dl>\n" :dend
"\n</dl>"
3167 :dtstart
"<dt>" :dtend
"</dt>\n"
3168 :ddstart
"<dd>" :ddend
"</dd>"
3169 :istart
"<li>" :iend
"</li>"
3170 :icount
(format "<li value=\"%s\">" counter
)
3171 :isep
"\n" :lsep
"\n" :csep
"\n"
3172 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>"
3173 :cbtrans
"<code>[-]</code>")
3176 (defun org-list-to-texinfo (list &optional params
)
3177 "Convert LIST into a Texinfo list.
3178 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3179 with overruling parameters for `org-list-to-generic'."
3180 (org-list-to-generic
3183 '(:splice nil
:ostart
"@itemize @minus\n" :oend
"@end itemize"
3184 :ustart
"@enumerate\n" :uend
"@end enumerate"
3185 :dstart
"@table @asis\n" :dend
"@end table"
3186 :dtstart
" " :dtend
"\n"
3187 :istart
"@item\n" :iend
"\n"
3190 :cbon
"@code{[X]}" :cboff
"@code{[ ]}"
3191 :cbtrans
"@code{[-]}")
3194 (defun org-list-to-subtree (list &optional params
)
3195 "Convert LIST into an Org subtree.
3196 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3197 with overruling parameters for `org-list-to-generic'."
3198 (let* ((rule (cdr (assq 'heading org-blank-before-new-entry
)))
3199 (level (org-reduced-level (or (org-current-level) 0)))
3200 (blankp (or (eq rule t
)
3201 (and (eq rule
'auto
)
3203 (outline-previous-heading)
3204 (org-previous-line-empty-p)))))
3207 ;; Return the string for the heading, depending on depth D
3208 ;; of current sub-list.
3210 (let ((oddeven-level (+ level d
1)))
3211 (concat (make-string (if org-odd-levels-only
3212 (1- (* 2 oddeven-level
))
3216 (org-list-to-generic
3220 :dtstart
" " :dtend
" "
3221 :istart
(funcall get-stars depth
)
3222 :icount
(funcall get-stars depth
)
3223 :isep
(if blankp
"\n\n" "\n")
3224 :csep
(if blankp
"\n\n" "\n")
3225 :cbon
"DONE" :cboff
"TODO" :cbtrans
"TODO")
3230 ;;; org-list.el ends here