1 ;;; org-list.el --- Plain lists for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Bastien Guerry <bzg AT altern DOT org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; This file contains the code dealing with plain lists in Org-mode.
32 ;; The fundamental idea behind lists work is to use structures. A
33 ;; structure is a snapshot of the list, in the shape of data tree (see
34 ;; `org-list-struct').
36 ;; Once the list structure is stored, it is possible to make changes
37 ;; directly on it or get useful information about the list, with the
38 ;; two helper functions, namely `org-list-parents-alist' and
39 ;; `org-list-prevs-alist', and using accessors or methods.
41 ;; Structure is eventually applied to the buffer with
42 ;; `org-list-write-struct'. This function repairs (bullets,
43 ;; indentation, checkboxes) the structure before applying it. It
44 ;; should be called near the end of any function working on
47 ;; Thus, a function applying to lists should usually follow this
50 ;; 1. Verify point is in a list and grab item beginning (with the same
51 ;; function `org-in-item-p'). If the function requires the cursor
52 ;; to be at item's bullet, `org-at-item-p' is more selective. If
53 ;; the cursor is amidst the buffer, it is possible to find the
54 ;; closest item with `org-list-search-backward', or
55 ;; `org-list-search-forward', applied to `org-item-beginning-re'.
57 ;; 2. Get list structure with `org-list-struct'.
59 ;; 3. Compute one, or both, helper functions,
60 ;; (`org-list-parents-alist', `org-list-prevs-alist') depending on
63 ;; 4. Proceed with the modifications, using methods and accessors.
65 ;; 5. Verify and apply structure to buffer, using
66 ;; `org-list-write-struct'. Possibly use
67 ;; `org-update-checkbox-count-maybe' if checkboxes might have been
70 ;; Computing a list structure can be a costly operation on huge lists
71 ;; (a few thousand lines long). Thus, code should follow the rule :
72 ;; "collect once, use many". As a corollary, it is usally 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-blank-before-new-entry
)
85 (defvar org-complex-heading-regexp
)
86 (defvar org-description-max-indent
)
87 (defvar org-drawer-regexp
)
89 (defvar org-M-RET-may-split-line
)
90 (defvar org-odd-levels-only
)
91 (defvar org-outline-regexp
)
92 (defvar org-ts-regexp
)
93 (defvar org-ts-regexp-both
)
95 (declare-function org-at-heading-p
"org" (&optional ignored
))
96 (declare-function org-back-over-empty-lines
"org" ())
97 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
98 (declare-function org-combine-plists
"org" (&rest plists
))
99 (declare-function org-count
"org" (cl-item cl-seq
))
100 (declare-function org-current-level
"org" ())
101 (declare-function org-entry-get
"org"
102 (pom property
&optional inherit literal-nil
))
103 (declare-function org-get-indentation
"org" (&optional line
))
104 (declare-function org-icompleting-read
"org" (&rest args
))
105 (declare-function org-in-regexp
"org" (re &optional nlines visually
))
106 (declare-function org-in-regexps-block-p
"org"
107 (start-re end-re
&optional bound
))
108 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
109 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
110 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
111 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
112 (declare-function org-level-increment
"org" ())
113 (declare-function org-narrow-to-subtree
"org" ())
114 (declare-function org-on-heading-p
"org" (&optional invisible-ok
))
115 (declare-function org-previous-line-empty-p
"org" ())
116 (declare-function org-remove-if
"org" (predicate seq
))
117 (declare-function org-show-subtree
"org" ())
118 (declare-function org-time-string-to-seconds
"org" (s))
119 (declare-function org-timer-hms-to-secs
"org-timer" (hms))
120 (declare-function org-timer-item
"org-timer" (&optional arg
))
121 (declare-function org-trim
"org" (s))
122 (declare-function org-uniquify
"org" (list))
123 (declare-function outline-invisible-p
"outline" (&optional pos
))
124 (declare-function outline-flag-region
"outline" (from to flag
))
125 (declare-function outline-next-heading
"outline" ())
126 (declare-function outline-previous-heading
"outline" ())
128 ;;; Configuration variables
130 (defgroup org-plain-lists nil
131 "Options concerning plain lists in Org-mode."
132 :tag
"Org Plain lists"
133 :group
'org-structure
)
135 (defcustom org-cycle-include-plain-lists t
136 "When t, make TAB cycle visibility on plain list items.
137 Cycling plain lists works only when the cursor is on a plain list
138 item. When the cursor is on an outline heading, plain lists are
139 treated as text. This is the most stable way of handling this,
140 which is why it is the default.
142 When this is the symbol `integrate', then during cycling, plain
143 list items will *temporarily* be interpreted as outline headlines
144 with a level given by 1000+i where i is the indentation of the
145 bullet. This setting can lead to strange effects when switching
146 visibility to `children', because the first \"child\" in a
147 subtree decides what children should be listed. If that first
148 \"child\" is a plain list item with an implied large level
149 number, all true children and grand children of the outline
150 heading will be exposed in a children' view."
151 :group
'org-plain-lists
153 (const :tag
"Never" nil
)
154 (const :tag
"With cursor in plain list (recommended)" t
)
155 (const :tag
"As children of outline headings" integrate
)))
157 (defcustom org-list-demote-modify-bullet nil
158 "Default bullet type installed when demoting an item.
159 This is an association list, for each bullet type, this alist will point
160 to the bullet that should be used when this item is demoted.
163 (setq org-list-demote-modify-bullet
164 '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
169 + Silence of the Lambs
172 + The Hunt for Red October
178 - Silence of the Lambs
181 - The Hunt for Red October
183 :group
'org-plain-lists
186 (choice :tag
"If the current bullet is "
192 (choice :tag
"demotion will change it to"
199 (defcustom org-plain-list-ordered-item-terminator t
200 "The character that makes a line with leading number an ordered list item.
201 Valid values are ?. and ?\). To get both terminators, use t. While
202 ?. may look nicer, it creates the danger that a line with leading
203 number may be incorrectly interpreted as an item. ?\) therefore is
205 :group
'org-plain-lists
206 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
207 (const :tag
"paren like in \"2)\"" ?\
))
208 (const :tab
"both" t
)))
210 (defcustom org-alphabetical-lists nil
211 "Non-nil means single character alphabetical bullets are allowed.
212 Both uppercase and lowercase are handled. Lists with more than 26
213 items will fallback to standard numbering. Alphabetical counters
214 like \"[@c]\" will be recognized."
215 :group
'org-plain-lists
218 (defcustom org-list-two-spaces-after-bullet-regexp nil
219 "A regular expression matching bullets that should have 2 spaces after them.
220 When nil, no bullet will have two spaces after them.
221 When a string, it will be used as a regular expression. When the
222 bullet type of a list is changed, the new bullet type will be
223 matched against this regexp. If it matches, there will be two
224 spaces instead of one after the bullet in each item of the list."
225 :group
'org-plain-lists
227 (const :tag
"never" nil
)
230 (defcustom org-list-ending-method
'both
231 "Determine where plain lists should end.
232 Valid values are: `regexp', `indent' or `both'.
234 When set to `regexp', Org will look into two variables,
235 `org-empty-line-terminates-plain-lists' and the more general
236 `org-list-end-regexp', to determine what will end lists.
238 When set to `indent', a list will end whenever a line following
239 an item, but not starting one, is less or equally indented than
240 the first item of the list.
242 When set to `both', each of the preceding methods is applied to
243 determine lists endings. This is the default method."
244 :group
'org-plain-lists
246 (const :tag
"With a regexp defining ending" regexp
)
247 (const :tag
"With indentation of regular (no bullet) text" indent
)
248 (const :tag
"With both methods" both
)))
250 (defcustom org-empty-line-terminates-plain-lists nil
251 "Non-nil means an empty line ends all plain list levels.
252 This variable only makes sense if `org-list-ending-method' is set
253 to `regexp' or `both'. This is then equivalent to set
254 `org-list-end-regexp' to \"^[ \\t]*$\"."
255 :group
'org-plain-lists
258 (defcustom org-list-end-regexp
"^[ \t]*\n[ \t]*\n"
259 "Regexp matching the end of all plain list levels.
260 It must start with \"^\" and end with \"\\n\". It defaults to 2
261 blank lines. `org-empty-line-terminates-plain-lists' has
263 :group
'org-plain-lists
266 (defcustom org-list-automatic-rules
'((bullet . t
)
269 "Non-nil means apply set of rules when acting on lists.
270 By default, automatic actions are taken when using
271 \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
272 \\[org-shiftmetaright], \\[org-shiftmetaleft],
273 \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
274 \\[org-insert-todo-heading]. You can disable individually these
275 rules by setting them to nil. Valid rules are:
277 bullet when non-nil, cycling bullet do not allow lists at
278 column 0 to have * as a bullet and descriptions lists
280 checkbox when non-nil, checkbox statistics is updated each time
281 you either insert a new checkbox or toggle a checkbox.
282 It also prevents from inserting a checkbox in a
284 indent when non-nil, indenting or outdenting list top-item
285 with its subtree will move the whole list and
286 outdenting a list whose bullet is * to column 0 will
287 change that bullet to \"-\"."
288 :group
'org-plain-lists
289 :type
'(alist :tag
"Sets of rules"
292 (const :tag
"Bullet" bullet
)
293 (const :tag
"Checkbox" checkbox
)
294 (const :tag
"Indent" indent
))
296 (boolean :tag
"Activate" :value t
)))
298 (defvar org-checkbox-statistics-hook nil
299 "Hook that is run whenever Org thinks checkbox statistics should be updated.
300 This hook runs even if checkbox rule in
301 `org-list-automatic-rules' does not apply, so it can be used to
302 implement alternative ways of collecting statistics
305 (defcustom org-hierarchical-checkbox-statistics t
306 "Non-nil means checkbox statistics counts only the state of direct children.
307 When nil, all boxes below the cookie are counted.
308 This can be set to nil on a per-node basis using a COOKIE_DATA property
309 with the word \"recursive\" in the value."
310 :group
'org-plain-lists
313 (defcustom org-description-max-indent
20
314 "Maximum indentation for the second line of a description list.
315 When the indentation would be larger than this, it will become
316 5 characters instead."
317 :group
'org-plain-lists
320 (defcustom org-list-radio-list-templates
321 '((latex-mode "% BEGIN RECEIVE ORGLST %n
322 % END RECEIVE ORGLST %n
324 #+ORGLST: SEND %n org-list-to-latex
327 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
328 @c END RECEIVE ORGLST %n
330 #+ORGLST: SEND %n org-list-to-texinfo
333 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
334 <!-- END RECEIVE ORGLST %n -->
336 #+ORGLST: SEND %n org-list-to-html
339 "Templates for radio lists in different major modes.
340 All occurrences of %n in a template will be replaced with the name of the
341 list, obtained by prompting the user."
342 :group
'org-plain-lists
344 (list (symbol :tag
"Major mode")
345 (string :tag
"Format"))))
347 (defvar org-list-forbidden-blocks
'("example" "verse" "src")
348 "Names of blocks where lists are not allowed.
349 Names must be in lower case.")
351 (defvar org-list-export-context
'(block inlinetask
)
352 "Context types where lists will be interpreted during export.
354 Valid types are `drawer', `inlinetask' and `block'. More
355 specifically, type `block' is determined by the variable
356 `org-list-forbidden-blocks'.")
359 ;;; Predicates and regexps
361 (defconst org-list-end-re
(if org-empty-line-terminates-plain-lists
364 "Regex corresponding to the end of a list.
365 It depends on `org-empty-line-terminates-plain-lists'.")
367 (defconst org-list-full-item-re
368 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]*\\)"
369 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\]\\)?"
370 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
371 "\\(?:\\(.*\\)[ \t]+::[ \t]+\\)?")
372 "Matches a list item and puts everything into groups:
376 group 4: description tag")
378 (defun org-item-re ()
379 "Return the correct regular expression for plain lists."
381 ((eq org-plain-list-ordered-item-terminator t
) "[.)]")
382 ((= org-plain-list-ordered-item-terminator ?\
)) ")")
383 ((= org-plain-list-ordered-item-terminator ?.
) "\\.")
385 (alpha (if org-alphabetical-lists
"\\|[A-Za-z]" "")))
386 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha
"\\)" term
387 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
389 (defsubst org-item-beginning-re
()
390 "Regexp matching the beginning of a plain list item."
391 (concat "^" (org-item-re)))
393 (defun org-list-at-regexp-after-bullet-p (regexp)
394 "Is point at a list item with REGEXP after bullet?"
397 (goto-char (match-end 0))
398 ;; Ignore counter if any
399 (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
400 (goto-char (match-end 0)))
401 (looking-at regexp
))))
403 (defun org-in-item-p ()
404 "Return item beginning position when in a plain list, nil otherwise.
405 This checks `org-list-ending-method'."
408 (let* ((case-fold-search t
)
409 (context (org-list-context))
410 (lim-up (car context
))
411 (inlinetask-re (and (featurep 'org-inlinetask
)
412 (org-inlinetask-outline-regexp)))
413 (item-re (org-item-re))
414 ;; Indentation isn't meaningful when point starts at an empty
415 ;; line or an inline task.
416 (ind-ref (if (or (looking-at "^[ \t]*$")
417 (and inlinetask-re
(looking-at inlinetask-re
)))
419 (org-get-indentation))))
421 ((eq (nth 2 context
) 'invalid
) nil
)
422 ((looking-at item-re
) (point))
424 ;; Detect if cursor in amidst `org-list-end-re'. First, count
425 ;; number HL of hard lines it takes, then call `org-in-regexp'
426 ;; to compute its boundaries END-BOUNDS. When point is
427 ;; in-between, move cursor before regexp beginning.
428 (let ((hl 0) (i -
1) end-bounds
)
429 (when (and (not (eq org-list-ending-method
'indent
))
431 (while (setq i
(string-match
432 "[\r\n]" org-list-end-re
(1+ i
)))
434 (setq end-bounds
(org-in-regexp org-list-end-re hl
)))
435 (>= (point) (car end-bounds
))
436 (< (point) (cdr end-bounds
)))
437 (goto-char (car end-bounds
))
439 ;; Look for an item, less indented that reference line if
440 ;; `org-list-ending-method' isn't `regexp'.
443 (let ((ind (org-get-indentation)))
445 ;; This is exactly what we want.
446 ((and (looking-at item-re
)
448 (eq org-list-ending-method
'regexp
)))
449 (throw 'exit
(point)))
450 ;; At upper bound of search or looking at the end of a
451 ;; previous list: search is over.
452 ((<= (point) lim-up
) (throw 'exit nil
))
453 ((and (not (eq org-list-ending-method
'indent
))
454 (looking-at org-list-end-re
))
456 ;; Skip blocks, drawers, inline-tasks, blank lines
457 ((looking-at "^[ \t]*#\\+end_")
458 (re-search-backward "^[ \t]*#\\+begin_" nil t
))
459 ((looking-at "^[ \t]*:END:")
460 (re-search-backward org-drawer-regexp nil t
)
462 ((and inlinetask-re
(looking-at inlinetask-re
))
463 (org-inlinetask-goto-beginning)
465 ((looking-at "^[ \t]*$") (forward-line -
1))
466 ;; Text at column 0 cannot belong to a list: stop.
467 ((zerop ind
) (throw 'exit nil
))
468 ;; Normal text less indented than reference line, take
469 ;; it as new reference.
473 (t (forward-line -
1)))))))))))
475 (defun org-at-item-p ()
476 "Is point in a line starting a hand-formatted item?"
479 (and (looking-at (org-item-re))
480 (not (eq (nth 2 (org-list-context)) 'invalid
)))))
482 (defun org-at-item-bullet-p ()
483 "Is point at the bullet of a plain list item?"
485 (not (member (char-after) '(?\ ?
\t)))
486 (< (point) (match-end 0))))
488 (defun org-at-item-timer-p ()
489 "Is point at a line starting a plain list item with a timer?"
490 (org-list-at-regexp-after-bullet-p
491 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
493 (defun org-at-item-description-p ()
494 "Is point at a description list item?"
495 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
497 (defun org-at-item-checkbox-p ()
498 "Is point at a line starting a plain-list item with a checklet?"
499 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
501 (defun org-at-item-counter-p ()
502 "Is point at a line starting a plain-list item with a counter?"
504 (looking-at org-list-full-item-re
)
508 ;;; Structures and helper functions
510 (defun org-list-context ()
511 "Determine context, and its boundaries, around point.
513 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
514 are boundaries and CONTEXT is a symbol among `drawer', `block',
515 `invalid', `inlinetask' and nil.
517 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
521 (let* ((case-fold-search t
) (pos (point)) beg end
522 ;; Compute position of surrounding headings. This is the
525 (org-with-limited-levels
527 (list (or (and (org-at-heading-p) (point-at-bol))
528 (outline-previous-heading)
530 (or (outline-next-heading) (point-max))
532 (prev-head (car heading
))
533 (next-head (nth 1 heading
))
534 ;; Is point inside a drawer?
537 (let ((end-re "^[ \t]*:END:")
538 ;; Can't use org-drawers-regexp as this function
539 ;; might be called in buffers not in Org mode
540 (drawers-re (concat "^[ \t]*:\\("
541 (mapconcat 'regexp-quote org-drawers
"\\|")
543 (and (not (looking-at drawers-re
))
544 (not (looking-at end-re
))
545 (setq beg
(and (re-search-backward drawers-re prev-head t
)
546 (1+ (point-at-eol))))
547 (setq end
(or (and (re-search-forward end-re next-head t
)
548 (1- (match-beginning 0)))
551 (list beg end
'drawer
)))))
552 ;; Is point strictly in a block, and of which type?
555 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type
)
556 (and (not (looking-at block-re
))
557 (setq beg
(and (re-search-backward block-re prev-head t
)
558 (1+ (point-at-eol))))
559 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
560 (setq type
(downcase (match-string 1)))
562 (setq end
(or (and (re-search-forward block-re next-head t
)
566 (equal (downcase (match-string 1)) "end")
567 (list beg end
(if (member type org-list-forbidden-blocks
)
568 'invalid
'block
))))))
569 ;; Is point in an inlinetask?
571 (when (featurep 'org-inlinetask
)
573 (let* ((stars-re (org-inlinetask-outline-regexp))
574 (end-re (concat stars-re
"END[ \t]*$")))
575 (and (not (looking-at "^\\*+"))
576 (setq beg
(and (re-search-backward stars-re prev-head t
)
577 (1+ (point-at-eol))))
578 (not (looking-at end-re
))
579 (setq end
(and (re-search-forward end-re next-head t
)
580 (1- (match-beginning 0))))
582 (list beg end
'inlinetask
))))))
583 ;; List actual candidates
584 (context-list (delq nil
(list heading drawerp blockp inlinetaskp
))))
585 ;; Return the closest context around
586 (assq (apply 'max
(mapcar 'car context-list
)) context-list
)))))
588 (defun org-list-struct ()
589 "Return structure of list at point.
591 A list structure is an alist where key is point at item, and
594 2. bullet with trailing whitespace,
595 3. bullet counter, if any,
597 5. position at item end,
598 6. description tag, if any.
600 Thus the following list, where numbers in parens are
605 5. [@5] sub-item 2 (34)
606 some other text belonging to first item (55)
608 + tag :: description (109)
611 will get the following structure:
613 \(\(1 0 \"- \" nil \"[X]\" nil 97\)
614 \(18 2 \"1. \" nil nil nil 34\)
615 \(34 2 \"5. \" \"5\" nil nil 55\)
616 \(97 0 \"- \" nil nil nil 131\)
617 \(109 2 \"+ \" nil nil \"tag\" 131\)
619 Assume point is at an item."
622 (let* ((case-fold-search t
)
623 (context (org-list-context))
624 (lim-up (car context
))
625 (lim-down (nth 1 context
))
627 (item-re (org-item-re))
628 (drawers-re (concat "^[ \t]*:\\("
629 (mapconcat 'regexp-quote org-drawers
"\\|")
631 (inlinetask-re (and (featurep 'org-inlinetask
)
632 (org-inlinetask-outline-regexp)))
633 (beg-cell (cons (point) (org-get-indentation)))
634 ind itm-lst itm-lst-2 end-lst end-lst-2 struct
637 ;; Return association at point.
639 (looking-at org-list-full-item-re
)
642 (match-string-no-properties 1) ; bullet
643 (match-string-no-properties 2) ; counter
644 (match-string-no-properties 3) ; checkbox
645 (match-string-no-properties 4))))) ; description tag
648 ;; Ensure list ends at the first blank line.
650 (skip-chars-backward " \r\t\n")
651 (min (1+ (point-at-eol)) lim-down
)))))
652 ;; 1. Read list from starting item to its beginning, and save
653 ;; top item position and indentation in BEG-CELL. Also store
654 ;; ending position of items in END-LST.
658 (let ((ind (+ (or (get-text-property (point) 'original-indentation
) 0)
659 (org-get-indentation))))
662 ;; At upward limit: if we ended at an item, store it,
663 ;; else dimiss useless data recorded above BEG-CELL.
667 (if (or (not (looking-at item-re
))
668 (get-text-property (point) 'org-example
))
669 (memq (assq (car beg-cell
) itm-lst
) itm-lst
)
670 (setq beg-cell
(cons (point) ind
))
671 (cons (funcall assoc-at-point ind
) itm-lst
)))))
672 ;; At a verbatim block, go before its beginning. Move
673 ;; from eol to ensure `previous-single-property-change'
674 ;; will return a value.
675 ((get-text-property (point) 'org-example
)
676 (goto-char (previous-single-property-change
677 (point-at-eol) 'org-example nil lim-up
))
679 ;; Looking at a list ending regexp. Dismiss useless
680 ;; data recorded above BEG-CELL. Jump to part 2.
681 ((and (not (eq org-list-ending-method
'indent
))
682 (looking-at org-list-end-re
))
685 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
686 ;; Skip blocks, drawers, inline tasks, blank lines
688 ((looking-at "^[ \t]*#\\+end_")
689 (re-search-backward "^[ \t]*#\\+begin_" nil t
))
690 ((looking-at "^[ \t]*:END:")
691 (re-search-backward drawers-re nil t
)
693 ((and inlinetask-re
(looking-at inlinetask-re
))
694 (org-inlinetask-goto-beginning)
696 ((looking-at "^[ \t]*$")
698 ((looking-at item-re
)
699 ;; Point is at an item. Add data to ITM-LST. It may
700 ;; also end a previous item: save it in END-LST. If
701 ;; ind is less or equal than BEG-CELL and there is no
702 ;; end at this ind or lesser, this item becomes the
704 (push (funcall assoc-at-point ind
) itm-lst
)
705 (push (cons ind
(point)) end-lst
)
706 (when (or (and (eq org-list-ending-method
'regexp
)
707 (<= ind
(cdr beg-cell
)))
708 (< ind text-min-ind
))
709 (setq beg-cell
(cons (point) ind
)))
711 ;; From there, point is not at an item. Unless ending
712 ;; method is `regexp', interpret line's indentation:
713 ;; - text at column 0 is necessarily out of any list.
714 ;; Dismiss data recorded above BEG-CELL. Jump to
716 ;; - any other case, it can possibly be an ending
717 ;; position for an item above. Save it and proceed.
718 ((eq org-list-ending-method
'regexp
) (forward-line -
1))
722 (memq (assq (car beg-cell
) itm-lst
) itm-lst
))))
724 (when (< ind text-min-ind
) (setq text-min-ind ind
))
725 (push (cons ind
(point)) end-lst
)
726 (forward-line -
1)))))))
727 ;; 2. Read list from starting point to its end, that is until we
728 ;; get out of context, or a non-item line is less or equally
729 ;; indented that BEG-CELL's cdr. Also store ending position
730 ;; of items in END-LST-2.
733 (let ((ind (+ (or (get-text-property (point) 'original-indentation
) 0)
734 (org-get-indentation))))
736 ((>= (point) lim-down
)
737 ;; At downward limit: this is de facto the end of the
738 ;; list. Save point as an ending position, and jump to
741 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
742 ;; At a verbatim block, move to its end. Point is at bol
743 ;; and 'org-example property is set by whole lines:
744 ;; `next-single-property-change' always return a value.
745 ((get-text-property (point) 'org-example
)
747 (next-single-property-change (point) 'org-example nil lim-down
)))
748 ;; Looking at a list ending regexp. Save point as an
749 ;; ending position and jump to part 3.
750 ((and (not (eq org-list-ending-method
'indent
))
751 (looking-at org-list-end-re
))
752 (throw 'exit
(push (cons 0 (point)) end-lst-2
)))
753 ;; Skip blocks, drawers, inline tasks and blank lines
755 ((looking-at "^[ \t]*#\\+begin_")
756 (re-search-forward "^[ \t]*#\\+end_")
758 ((looking-at drawers-re
)
759 (re-search-forward "^[ \t]*:END:" nil t
)
761 ((and inlinetask-re
(looking-at inlinetask-re
))
762 (org-inlinetask-goto-end))
763 ((looking-at "^[ \t]*$")
765 ((looking-at item-re
)
766 ;; Point is at an item. Add data to ITM-LST-2. It may also
767 ;; end a previous item, so save it in END-LST-2.
768 (push (funcall assoc-at-point ind
) itm-lst-2
)
769 (push (cons ind
(point)) end-lst-2
)
771 ;; From there, point is not at an item. If ending method
772 ;; is not `regexp', two situations are of interest:
773 ;; - ind is lesser or equal than BEG-CELL's. The list is
774 ;; over. Store point as an ending position and jump to
776 ;; - ind is lesser or equal than previous item's. This
777 ;; is an ending position. Store it and proceed.
778 ((eq org-list-ending-method
'regexp
) (forward-line 1))
779 ((<= ind
(cdr beg-cell
))
781 (push (cons 0 (funcall end-before-blank
)) end-lst-2
)))
782 ((<= ind
(nth 1 (car itm-lst-2
)))
783 (push (cons ind
(point)) end-lst-2
)
785 (t (forward-line 1))))))
786 (setq struct
(append itm-lst
(cdr (nreverse itm-lst-2
))))
787 (setq end-lst
(append end-lst
(cdr (nreverse end-lst-2
))))
788 ;; 3. Correct ill-formed lists by ensuring top item is the least
790 (let ((min-ind (nth 1 (car struct
))))
792 (let ((ind (nth 1 item
))
794 (when (< ind min-ind
)
795 (setcar (cdr item
) min-ind
)
796 ;; Modify bullet to be sure item will be modified
797 (setcar (nthcdr 2 item
) (org-trim bul
)))))
799 ;; 4. Associate each item to its end pos.
800 (org-list-struct-assoc-end struct end-lst
)
804 (defun org-list-struct-assoc-end (struct end-list
)
805 "Associate proper ending point to items in STRUCT.
807 END-LIST is a pseudo-alist where car is indentation and cdr is
810 This function modifies STRUCT."
811 (let ((endings end-list
))
814 (let ((pos (car elt
))
816 ;; Remove end candidates behind current item
817 (while (or (<= (cdar endings
) pos
))
819 ;; Add end position to item assoc
820 (let ((old-end (nthcdr 6 elt
))
821 (new-end (assoc-default ind endings
'<=)))
823 (setcar old-end new-end
)
824 (setcdr elt
(append (cdr elt
) (list new-end
)))))))
827 (defun org-list-prevs-alist (struct)
828 "Return alist between item and previous item in STRUCT."
829 (let ((item-end-alist (mapcar (lambda (e) (cons (car e
) (nth 6 e
)))
832 (let ((prev (car (rassq (car e
) item-end-alist
))))
833 (cons (car e
) prev
)))
836 (defun org-list-parents-alist (struct)
837 "Return alist between item and parent in STRUCT."
838 (let ((ind-to-ori (list (list (nth 1 (car struct
)))))
839 (prev-pos (list (caar struct
))))
841 (mapcar (lambda (item)
842 (let ((pos (car item
))
844 (prev-ind (caar ind-to-ori
)))
849 (member (assq ind ind-to-ori
) ind-to-ori
))
850 (cons pos
(cdar ind-to-ori
)))
852 (let ((origin (nth 1 prev-pos
)))
853 (push (cons ind origin
) ind-to-ori
)
855 (t (cons pos
(cdar ind-to-ori
))))))
861 (defsubst org-list-get-nth
(n key struct
)
862 "Return the Nth value of KEY in STRUCT."
863 (nth n
(assq key struct
)))
865 (defun org-list-set-nth (n key struct new
)
866 "Set the Nth value of KEY in STRUCT to NEW.
867 \nThis function modifies STRUCT."
868 (setcar (nthcdr n
(assq key struct
)) new
))
870 (defsubst org-list-get-ind
(item struct
)
871 "Return indentation of ITEM in STRUCT."
872 (org-list-get-nth 1 item struct
))
874 (defun org-list-set-ind (item struct ind
)
875 "Set indentation of ITEM in STRUCT to IND.
876 \nThis function modifies STRUCT."
877 (org-list-set-nth 1 item struct ind
))
879 (defsubst org-list-get-bullet
(item struct
)
880 "Return bullet of ITEM in STRUCT."
881 (org-list-get-nth 2 item struct
))
883 (defun org-list-set-bullet (item struct bullet
)
884 "Set bullet of ITEM in STRUCT to BULLET.
885 \nThis function modifies STRUCT."
886 (org-list-set-nth 2 item struct bullet
))
888 (defsubst org-list-get-counter
(item struct
)
889 "Return counter of ITEM in STRUCT."
890 (org-list-get-nth 3 item struct
))
892 (defsubst org-list-get-checkbox
(item struct
)
893 "Return checkbox of ITEM in STRUCT or nil."
894 (org-list-get-nth 4 item struct
))
896 (defun org-list-set-checkbox (item struct checkbox
)
897 "Set checkbox of ITEM in STRUCT to CHECKBOX.
898 \nThis function modifies STRUCT."
899 (org-list-set-nth 4 item struct checkbox
))
901 (defsubst org-list-get-tag
(item struct
)
902 "Return end position of ITEM in STRUCT."
903 (org-list-get-nth 5 item struct
))
905 (defun org-list-get-item-end (item struct
)
906 "Return end position of ITEM in STRUCT."
907 (org-list-get-nth 6 item struct
))
909 (defun org-list-get-item-end-before-blank (item struct
)
910 "Return point at end of ITEM in STRUCT, before any blank line.
911 Point returned is at end of line."
913 (goto-char (org-list-get-item-end item struct
))
914 (skip-chars-backward " \r\t\n")
917 (defun org-list-get-parent (item struct parents
)
918 "Return parent of ITEM or nil.
919 STRUCT is the list structure. PARENTS is the alist of parents, as
920 returned by `org-list-parents-alist'."
921 (let ((parents (or parents
(org-list-parents-alist struct
))))
922 (cdr (assq item parents
))))
924 (defun org-list-has-child-p (item struct
)
925 "Non-nil if ITEM has a child.
927 STRUCT is the list structure.
929 Value returned is the position of the first child of ITEM."
930 (let ((ind (org-list-get-ind item struct
))
931 (child-maybe (car (nth 1 (member (assq item struct
) struct
)))))
932 (when (and child-maybe
933 (< ind
(org-list-get-ind child-maybe struct
)))
936 (defun org-list-get-next-item (item struct prevs
)
937 "Return next item in same sub-list as ITEM, or nil.
938 STRUCT is the list structure. PREVS is the alist of previous
939 items, as returned by `org-list-prevs-alist'."
940 (car (rassq item prevs
)))
942 (defun org-list-get-prev-item (item struct prevs
)
943 "Return previous item in same sub-list as ITEM, or nil.
944 STRUCT is the list structure. PREVS is the alist of previous
945 items, as returned by `org-list-prevs-alist'."
946 (cdr (assq item prevs
)))
948 (defun org-list-get-subtree (item struct
)
949 "List all items having ITEM as a common ancestor, or nil.
950 STRUCT is the list structure."
951 (let* ((item-end (org-list-get-item-end item struct
))
952 (sub-struct (cdr (member (assq item struct
) struct
)))
957 (if (< pos item-end
) (push pos subtree
) (throw 'exit nil
))))
961 (defun org-list-get-all-items (item struct prevs
)
962 "List all items in the same sub-list as ITEM.
963 STRUCT is the list structure. PREVS is the alist of previous
964 items, as returned by `org-list-prevs-alist'."
965 (let ((prev-item item
)
967 before-item after-item
)
968 (while (setq prev-item
(org-list-get-prev-item prev-item struct prevs
))
969 (push prev-item before-item
))
970 (while (setq next-item
(org-list-get-next-item next-item struct prevs
))
971 (push next-item after-item
))
972 (append before-item
(list item
) (nreverse after-item
))))
974 (defun org-list-get-children (item struct parents
)
975 "List all children of ITEM, or nil.
976 STRUCT is the list structure. PARENTS is the alist of parents, as
977 returned by `org-list-parents-alist'."
979 (while (setq child
(car (rassq item parents
)))
980 (setq parents
(cdr (member (assq child parents
) parents
)))
984 (defun org-list-get-top-point (struct)
985 "Return point at beginning of list.
986 STRUCT is the list structure."
989 (defun org-list-get-bottom-point (struct)
990 "Return point at bottom of list.
991 STRUCT is the list structure."
993 (mapcar (lambda (e) (org-list-get-item-end (car e
) struct
)) struct
)))
995 (defun org-list-get-list-begin (item struct prevs
)
996 "Return point at beginning of sub-list ITEM belongs.
997 STRUCT is the list structure. PREVS is the alist of previous
998 items, as returned by `org-list-prevs-alist'."
999 (let ((first-item item
) prev-item
)
1000 (while (setq prev-item
(org-list-get-prev-item first-item struct prevs
))
1001 (setq first-item prev-item
))
1004 (defalias 'org-list-get-first-item
'org-list-get-list-begin
)
1006 (defun org-list-get-last-item (item struct prevs
)
1007 "Return point at last item of sub-list ITEM belongs.
1008 STRUCT is the list structure. PREVS is the alist of previous
1009 items, as returned by `org-list-prevs-alist'."
1010 (let ((last-item item
) next-item
)
1011 (while (setq next-item
(org-list-get-next-item last-item struct prevs
))
1012 (setq last-item next-item
))
1015 (defun org-list-get-list-end (item struct prevs
)
1016 "Return point at end of sub-list ITEM belongs.
1017 STRUCT is the list structure. PREVS is the alist of previous
1018 items, as returned by `org-list-prevs-alist'."
1019 (org-list-get-item-end (org-list-get-last-item item struct prevs
) struct
))
1021 (defun org-list-get-list-type (item struct prevs
)
1022 "Return the type of the list containing ITEM, as a symbol.
1024 STRUCT is the list structure. PREVS is the alist of previous
1025 items, as returned by `org-list-prevs-alist'.
1027 Possible types are `descriptive', `ordered' and `unordered'. The
1028 type is determined by the first item of the list."
1029 (let ((first (org-list-get-list-begin item struct prevs
)))
1031 ((org-list-get-tag first struct
) 'descriptive
)
1032 ((string-match "[[:alnum:]]" (org-list-get-bullet first struct
)) 'ordered
)
1038 (defun org-list-search-generic (search re bound noerr
)
1039 "Search a string in valid contexts for lists.
1040 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1041 in `re-search-forward'."
1043 (let ((origin (point)))
1045 ;; 1. No match: return to origin or bound, depending on NOERR.
1046 (unless (funcall search re bound noerr
)
1047 (throw 'exit
(and (goto-char (if (memq noerr
'(t nil
)) origin bound
))
1049 ;; 2. Match in an `invalid' context: continue searching. Else,
1051 (unless (eq (org-list-context) 'invalid
) (throw 'exit
(point)))))))
1053 (defun org-list-search-backward (regexp &optional bound noerror
)
1054 "Like `re-search-backward' but stop only where lists are recognized.
1055 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1056 `re-search-backward'."
1057 (org-list-search-generic #'re-search-backward
1058 regexp
(or bound
(point-min)) noerror
))
1060 (defun org-list-search-forward (regexp &optional bound noerror
)
1061 "Like `re-search-forward' but stop only where lists are recognized.
1062 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1063 `re-search-forward'."
1064 (org-list-search-generic #'re-search-forward
1065 regexp
(or bound
(point-max)) noerror
))
1068 ;;; Methods on structures
1070 (defsubst org-list-bullet-string
(bullet)
1071 "Return BULLET with the correct number of whitespaces.
1072 It determines the number of whitespaces to append by looking at
1073 `org-list-two-spaces-after-bullet-regexp'."
1075 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1077 org-list-two-spaces-after-bullet-regexp bullet
))
1080 (string-match "\\S-+\\([ \t]*\\)" bullet
)
1081 (replace-match spaces nil nil bullet
1))))
1083 (defun org-list-separating-blank-lines-number (pos struct prevs
)
1084 "Return number of blank lines that should separate items in list.
1086 POS is the position at item beginning to be considered.
1088 STRUCT is the list structure. PREVS is the alist of previous
1089 items, as returned by `org-list-prevs-alist'.
1091 Assume point is at item's beginning. If the item is alone, apply
1092 some heuristics to guess the result."
1094 (let ((insert-blank-p
1095 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
1098 ;; Trivial cases where there should be none.
1099 ((or (and (not (eq org-list-ending-method
'indent
))
1100 org-empty-line-terminates-plain-lists
)
1101 (not insert-blank-p
)) 0)
1102 ;; When `org-blank-before-new-entry' says so, it is 1.
1103 ((eq insert-blank-p t
) 1)
1104 ;; plain-list-item is 'auto. Count blank lines separating
1105 ;; neighbours items in list.
1106 (t (let ((next-p (org-list-get-next-item (point) struct prevs
)))
1108 ;; Is there a next item?
1109 (next-p (goto-char next-p
)
1110 (org-back-over-empty-lines))
1111 ;; Is there a previous item?
1112 ((org-list-get-prev-item (point) struct prevs
)
1113 (org-back-over-empty-lines))
1114 ;; User inserted blank lines, trust him
1115 ((and (> pos
(org-list-get-item-end-before-blank pos struct
))
1118 (skip-chars-backward " \t")
1119 (setq usr-blank
(org-back-over-empty-lines))) 0))
1121 ;; Are there blank lines inside the item ?
1123 (org-list-search-forward
1124 "^[ \t]*$" (org-list-get-item-end-before-blank pos struct
) t
))
1126 ;; No parent: no blank line.
1129 (defun org-list-insert-item (pos struct prevs
&optional checkbox after-bullet
)
1130 "Insert a new list item at POS and return the new structure.
1131 If POS is before first character after bullet of the item, the
1132 new item will be created before the current one.
1134 STRUCT is the list structure. PREVS is the the alist of previous
1135 items, as returned by `org-list-prevs-alist'.
1137 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1138 after the bullet. Cursor will be after this text once the
1141 This function modifies STRUCT."
1142 (let ((case-fold-search t
))
1143 ;; 1. Get information about list: structure, usual helper
1144 ;; functions, position of point with regards to item start
1145 ;; (BEFOREP), blank lines number separating items (BLANK-NB),
1146 ;; position of split (POS) if we're allowed to (SPLIT-LINE-P).
1147 (let* ((item (goto-char (org-list-get-item-begin)))
1148 (item-end (org-list-get-item-end item struct
))
1149 (item-end-no-blank (org-list-get-item-end-before-blank item struct
))
1150 (beforep (and (looking-at org-list-full-item-re
)
1151 (<= pos
(match-end 0))))
1152 (split-line-p (org-get-alist-option org-M-RET-may-split-line
'item
))
1153 (blank-nb (org-list-separating-blank-lines-number
1155 ;; 2. Build the new item to be created. Concatenate same
1156 ;; bullet as item, checkbox, text AFTER-BULLET if
1157 ;; provided, and text cut from point to end of item
1158 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1159 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1160 ;; between what was cut and what was inserted in buffer
1161 ;; is stored in SIZE-OFFSET.
1162 (ind (let ((ind-ref (org-list-get-ind item struct
)))
1163 (if (not indent-tabs-mode
)
1165 (+ (/ ind-ref tab-width
) (mod ind-ref tab-width
)))))
1166 (bullet (org-list-bullet-string (org-list-get-bullet item struct
)))
1167 (box (when checkbox
"[ ]"))
1169 (and (not beforep
) split-line-p
1172 (skip-chars-backward " \r\t\n")
1174 (delete-and-extract-region pos item-end-no-blank
))))
1175 (body (concat bullet
(when box
(concat box
" ")) after-bullet
1177 (if (string-match "\\`[ \t]+" text-cut
)
1178 (replace-match "" t t text-cut
)
1181 (item-sep (make-string (1+ blank-nb
) ?
\n))
1182 (item-size (+ ind
(length body
) (length item-sep
)))
1183 (size-offset (- item-size
(length text-cut
))))
1184 ;; 4. Insert effectively item into buffer
1186 (org-indent-to-column ind
)
1187 (insert body item-sep
)
1188 ;; 5. Add new item to STRUCT.
1193 ;; Before inserted item, positions don't change but
1194 ;; an item ending after insertion has its end shifted
1197 (when (> end item
) (setcar (nthcdr 6 e
) (+ end size-offset
))))
1198 ;; Trivial cases where current item isn't split in
1199 ;; two. Just shift every item after new one by
1201 ((or beforep
(not split-line-p
))
1202 (setcar e
(+ p item-size
))
1203 (setcar (nthcdr 6 e
) (+ end item-size
)))
1204 ;; Item is split in two: elements before POS are just
1205 ;; shifted by ITEM-SIZE. In the case item would end
1206 ;; after split POS, ending is only shifted by
1209 (setcar e
(+ p item-size
))
1211 (setcar (nthcdr 6 e
) (+ end item-size
))
1212 (setcar (nthcdr 6 e
) (+ end size-offset
))))
1213 ;; Elements after POS are moved into new item. Length
1214 ;; of ITEM-SEP has to be removed as ITEM-SEP
1215 ;; doesn't appear in buffer yet.
1217 (setcar e
(+ p size-offset
(- item pos
(length item-sep
))))
1218 (if (= end item-end
)
1219 (setcar (nthcdr 6 e
) (+ item item-size
))
1220 (setcar (nthcdr 6 e
)
1222 (- item pos
(length item-sep
))))))
1223 ;; Elements at ITEM-END or after are only shifted by
1225 (t (setcar e
(+ p size-offset
))
1226 (setcar (nthcdr 6 e
) (+ end size-offset
))))))
1228 (push (list item ind bullet nil box nil
(+ item item-size
)) struct
)
1229 (setq struct
(sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))
1230 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1231 ;; exchange ITEM with the next item in list. Position cursor
1232 ;; after bullet, counter, checkbox, and label.
1235 (setq struct
(org-list-exchange-items item
(+ item item-size
) struct
))
1236 (goto-char (org-list-get-next-item
1237 item struct
(org-list-prevs-alist struct
))))
1240 (defun org-list-exchange-items (beg-A beg-B struct
)
1241 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1242 Blank lines at the end of items are left in place. Return the new
1243 structure after the changes.
1245 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1246 to the same sub-list.
1248 This function modifies STRUCT."
1250 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct
))
1251 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct
))
1252 (end-A (org-list-get-item-end beg-A struct
))
1253 (end-B (org-list-get-item-end beg-B struct
))
1254 (size-A (- end-A-no-blank beg-A
))
1255 (size-B (- end-B-no-blank beg-B
))
1256 (body-A (buffer-substring beg-A end-A-no-blank
))
1257 (body-B (buffer-substring beg-B end-B-no-blank
))
1258 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B
))
1259 (sub-A (cons beg-A
(org-list-get-subtree beg-A struct
)))
1260 (sub-B (cons beg-B
(org-list-get-subtree beg-B struct
))))
1261 ;; 1. Move effectively items in buffer.
1263 (delete-region beg-A end-B-no-blank
)
1264 (insert (concat body-B between-A-no-blank-and-B body-A
))
1265 ;; 2. Now modify struct. No need to re-read the list, the
1266 ;; transformation is just a shift of positions. Some special
1267 ;; attention is required for items ending at END-A and END-B
1268 ;; as empty spaces are not moved there. In others words, item
1269 ;; BEG-A will end with whitespaces that were at the end of
1270 ;; BEG-B and the same applies to BEG-B.
1272 (let ((pos (car e
)))
1276 (let ((end-e (nth 6 e
)))
1277 (setcar e
(+ pos
(- end-B-no-blank end-A-no-blank
)))
1278 (setcar (nthcdr 6 e
)
1279 (+ end-e
(- end-B-no-blank end-A-no-blank
)))
1280 (when (= end-e end-A
) (setcar (nthcdr 6 e
) end-B
))))
1282 (let ((end-e (nth 6 e
)))
1283 (setcar e
(- (+ pos beg-A
) beg-B
))
1284 (setcar (nthcdr 6 e
) (+ end-e
(- beg-A beg-B
)))
1285 (when (= end-e end-B
)
1286 (setcar (nthcdr 6 e
)
1287 (+ beg-A size-B
(- end-A end-A-no-blank
))))))
1289 (let ((end-e (nth 6 e
)))
1290 (setcar e
(+ pos
(- size-B size-A
)))
1291 (setcar (nthcdr 6 e
) (+ end-e
(- size-B size-A
))))))))
1293 (sort struct
(lambda (e1 e2
) (< (car e1
) (car e2
)))))))
1295 (defun org-list-struct-outdent (start end struct parents
)
1296 "Outdent items between positions START and END.
1298 STRUCT is the list structure. PARENTS is the alist of items'
1299 parents, as returned by `org-list-parents-alist'.
1301 START is included, END excluded."
1304 (let* ((item (car cell
))
1305 (parent (cdr cell
)))
1307 ;; Item not yet in zone: keep association
1308 ((< item start
) cell
)
1309 ;; Item out of zone: follow associations in acc
1311 (let ((convert (and parent
(assq parent acc
))))
1312 (if convert
(cons item
(cdr convert
)) cell
)))
1313 ;; Item has no parent: error
1315 (error "Cannot outdent top-level items"))
1316 ;; Parent is outdented: keep association
1318 (push (cons parent item
) acc
) cell
)
1320 ;; Parent isn't outdented: reparent to grand-parent
1321 (let ((grand-parent (org-list-get-parent
1322 parent struct parents
)))
1323 (push (cons parent item
) acc
)
1324 (cons item grand-parent
))))))))
1325 (mapcar out parents
)))
1327 (defun org-list-struct-indent (start end struct parents prevs
)
1328 "Indent items between positions START and END.
1330 STRUCT is the list structure. PARENTS is the alist of parents and
1331 PREVS is the alist of previous items, returned by, respectively,
1332 `org-list-parents-alist' and `org-list-prevs-alist'.
1334 START is included and END excluded.
1336 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1337 bullets between START and END."
1339 (set-assoc (lambda (cell) (push cell acc
) cell
))
1340 (change-bullet-maybe
1343 (let* ((bul (org-trim (org-list-get-bullet item struct
)))
1344 (new-bul-p (cdr (assoc bul org-list-demote-modify-bullet
))))
1345 (when new-bul-p
(org-list-set-bullet item struct new-bul-p
))))))
1348 (let* ((item (car cell
))
1349 (parent (cdr cell
)))
1351 ;; Item not yet in zone: keep association
1352 ((< item start
) cell
)
1354 ;; Item out of zone: follow associations in acc
1355 (let ((convert (assq parent acc
)))
1356 (if convert
(cons item
(cdr convert
)) cell
)))
1358 ;; Item is in zone...
1359 (let ((prev (org-list-get-prev-item item struct prevs
)))
1360 ;; Check if bullet needs to be changed
1361 (funcall change-bullet-maybe item
)
1363 ;; First item indented but not parent: error
1364 ((and (not prev
) (< parent start
))
1365 (error "Cannot indent the first item of a list"))
1366 ;; First item and parent indented: keep same parent
1367 ((not prev
) (funcall set-assoc cell
))
1368 ;; Previous item not indented: reparent to it
1369 ((< prev start
) (funcall set-assoc
(cons item prev
)))
1370 ;; Previous item indented: reparent like it
1373 (cons item
(cdr (assq prev acc
)))))))))))))
1374 (mapcar ind parents
)))
1377 ;;; Repairing structures
1379 (defun org-list-use-alpha-bul-p (first struct prevs
)
1380 "Non-nil if list starting at FIRST can have alphabetical bullets.
1382 STRUCT is list structure. PREVS is the alist of previous items,
1383 as returned by `org-list-prevs-alist'."
1384 (and org-alphabetical-lists
1386 (let ((item first
) (ascii 64) (case-fold-search nil
))
1387 ;; Pretend that bullets are uppercase and check if alphabet
1388 ;; is sufficient, taking counters into account.
1390 (let ((bul (org-list-get-bullet item struct
))
1391 (count (org-list-get-counter item struct
)))
1392 ;; Virtually determine current bullet
1393 (if (and count
(string-match "[a-zA-Z]" count
))
1394 ;; Counters are not case-sensitive.
1395 (setq ascii
(string-to-char (upcase count
)))
1396 (setq ascii
(1+ ascii
)))
1397 ;; Test if bullet would be over z or Z.
1400 (setq item
(org-list-get-next-item item struct prevs
)))))
1401 ;; All items checked. All good.
1404 (defun org-list-inc-bullet-maybe (bullet)
1405 "Increment BULLET if applicable."
1406 (let ((case-fold-search nil
))
1408 ;; Num bullet: increment it.
1409 ((string-match "[0-9]+" bullet
)
1411 (number-to-string (1+ (string-to-number (match-string 0 bullet
))))
1413 ;; Alpha bullet: increment it.
1414 ((string-match "[A-Za-z]" bullet
)
1416 (char-to-string (1+ (string-to-char (match-string 0 bullet
))))
1418 ;; Unordered bullet: leave it.
1421 (defun org-list-struct-fix-bul (struct prevs
)
1422 "Verify and correct bullets in STRUCT.
1423 PREVS is the alist of previous items, as returned by
1424 `org-list-prevs-alist'.
1426 This function modifies STRUCT."
1427 (let ((case-fold-search nil
)
1430 ;; Set bullet of ITEM in STRUCT, depending on the type of
1431 ;; first item of the list, the previous bullet and counter
1434 (let* ((prev (org-list-get-prev-item item struct prevs
))
1435 (prev-bul (and prev
(org-list-get-bullet prev struct
)))
1436 (counter (org-list-get-counter item struct
))
1437 (bullet (org-list-get-bullet item struct
))
1438 (alphap (and (not prev
)
1439 (org-list-use-alpha-bul-p item struct prevs
))))
1440 (org-list-set-bullet
1442 (org-list-bullet-string
1444 ;; Alpha counter in alpha list: use counter.
1446 (string-match "[a-zA-Z]" counter
)
1447 (string-match "[a-zA-Z]" prev-bul
))
1448 ;; Use cond to be sure `string-match' is used in
1452 ((string-match "[a-z]" prev-bul
) (downcase counter
))
1453 ((string-match "[A-Z]" prev-bul
) (upcase counter
)))))
1454 (replace-match real-count nil nil prev-bul
)))
1455 ;; Num counter in a num list: use counter.
1457 (string-match "[0-9]+" counter
)
1458 (string-match "[0-9]+" prev-bul
))
1459 (replace-match counter nil nil prev-bul
))
1460 ;; No counter: increase, if needed, previous bullet.
1462 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct
)))
1463 ;; Alpha counter at first item: use counter.
1464 ((and counter
(org-list-use-alpha-bul-p item struct prevs
)
1465 (string-match "[A-Za-z]" counter
)
1466 (string-match "[A-Za-z]" bullet
))
1469 ((string-match "[a-z]" bullet
) (downcase counter
))
1470 ((string-match "[A-Z]" bullet
) (upcase counter
)))))
1471 (replace-match real-count nil nil bullet
)))
1472 ;; Num counter at first item: use counter.
1474 (string-match "[0-9]+" counter
)
1475 (string-match "[0-9]+" bullet
))
1476 (replace-match counter nil nil bullet
))
1477 ;; First bullet is alpha uppercase: use "A".
1478 ((and alphap
(string-match "[A-Z]" bullet
))
1479 (replace-match "A" nil nil bullet
))
1480 ;; First bullet is alpha lowercase: use "a".
1481 ((and alphap
(string-match "[a-z]" bullet
))
1482 (replace-match "a" nil nil bullet
))
1483 ;; First bullet is num: use "1".
1484 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet
)
1485 (replace-match "1" nil nil bullet
))
1486 ;; Not an ordered list: keep bullet.
1488 (mapc fix-bul
(mapcar 'car struct
))))
1490 (defun org-list-struct-fix-ind (struct parents
&optional bullet-size
)
1491 "Verify and correct indentation in STRUCT.
1493 PARENTS is the alist of parents, as returned by
1494 `org-list-parents-alist'.
1496 If numeric optional argument BULLET-SIZE is set, assume all
1497 bullets in list have this length to determine new indentation.
1499 This function modifies STRUCT."
1500 (let* ((ancestor (org-list-get-top-point struct
))
1501 (top-ind (org-list-get-ind ancestor struct
))
1504 (let ((parent (org-list-get-parent item struct parents
)))
1506 ;; Indent like parent + length of parent's bullet
1508 item struct
(+ (or bullet-size
1510 (org-list-get-bullet parent struct
)))
1511 (org-list-get-ind parent struct
)))
1512 ;; If no parent, indent like top-point
1513 (org-list-set-ind item struct top-ind
))))))
1514 (mapc new-ind
(mapcar 'car
(cdr struct
)))))
1516 (defun org-list-struct-fix-box (struct parents prevs
&optional ordered
)
1517 "Verify and correct checkboxes in STRUCT.
1519 PARENTS is the alist of parents and PREVS is the alist of
1520 previous items, as returned by, respectively,
1521 `org-list-parents-alist' and `org-list-prevs-alist'.
1523 If ORDERED is non-nil, a checkbox can only be checked when every
1524 checkbox before it is checked too. If there was an attempt to
1525 break this rule, the function will return the blocking item. In
1526 all others cases, the return value will be nil.
1528 This function modifies STRUCT."
1529 (let ((all-items (mapcar 'car struct
))
1534 (mapcar (lambda (child)
1535 (org-list-get-checkbox child struct
))
1536 (org-list-get-children item struct parents
))))
1537 (org-list-set-checkbox
1540 ((and (member "[ ]" box-list
) (member "[X]" box-list
)) "[-]")
1541 ((member "[-]" box-list
) "[-]")
1542 ((member "[X]" box-list
) "[X]")
1543 ((member "[ ]" box-list
) "[ ]")
1544 ;; parent has no boxed child: leave box as-is
1545 (t (org-list-get-checkbox item struct
))))))))
1547 ;; 1. List all parents with a checkbox
1550 (let* ((parent (org-list-get-parent e struct parents
))
1551 (parent-box-p (org-list-get-checkbox parent struct
)))
1552 (when (and parent-box-p
(not (memq parent parent-list
)))
1553 (push parent parent-list
))))
1555 ;; 2. Sort those parents by decreasing indentation
1556 (setq parent-list
(sort parent-list
1558 (> (org-list-get-ind e1 struct
)
1559 (org-list-get-ind e2 struct
)))))
1560 ;; 3. For each parent, get all children's checkboxes to determine
1561 ;; and set its checkbox accordingly
1562 (mapc set-parent-box parent-list
)
1563 ;; 4. If ORDERED is set, see if we need to uncheck some boxes
1566 (mapcar (lambda (e) (org-list-get-checkbox e struct
)) all-items
))
1567 (after-unchecked (member "[ ]" box-list
)))
1568 ;; there are boxes checked after an unchecked one: fix that
1569 (when (member "[X]" after-unchecked
)
1570 (let ((index (- (length struct
) (length after-unchecked
))))
1571 (mapc (lambda (e) (org-list-set-checkbox e struct
"[ ]"))
1572 (nthcdr index all-items
))
1573 ;; Verify once again the structure, without ORDERED
1574 (org-list-struct-fix-box struct parents prevs nil
)
1575 ;; return blocking item
1576 (nth index all-items
)))))))
1578 (defun org-list-struct-apply-struct (struct old-struct
)
1579 "Apply set-difference between STRUCT and OLD-STRUCT to the buffer.
1581 OLD-STRUCT is the structure before any modifications, and STRUCT
1582 the structure to be applied. The function will only modify parts
1583 of the list which have changed.
1585 Initial position of cursor is restored after the changes."
1586 (let* ((pos (copy-marker (point)))
1587 (inlinetask-re (and (featurep 'org-inlinetask
)
1588 (org-inlinetask-outline-regexp)))
1589 (item-re (org-item-re))
1590 (box-rule-p (cdr (assq 'checkbox org-list-automatic-rules
)))
1593 ;; Shift the indentation between END and BEG by DELTA.
1594 ;; Start from the line before END.
1595 (lambda (end beg delta
)
1597 (skip-chars-backward " \r\t\n")
1599 (while (or (> (point) beg
)
1600 (and (= (point) beg
)
1601 (not (looking-at item-re
))))
1603 ;; Skip inline tasks
1604 ((and inlinetask-re
(looking-at inlinetask-re
))
1605 (org-inlinetask-goto-beginning))
1606 ;; Shift only non-empty lines
1607 ((org-looking-at-p "^[ \t]*\\S-")
1608 (let ((i (org-get-indentation)))
1609 (org-indent-line-to (+ i delta
)))))
1610 (forward-line -
1)))))
1613 ;; Replace ITEM first line elements with new elements from
1614 ;; STRUCT, if appropriate.
1617 (let* ((new-ind (org-list-get-ind item struct
))
1618 (old-ind (org-get-indentation))
1619 (new-bul (org-list-bullet-string
1620 (org-list-get-bullet item struct
)))
1621 (old-bul (org-list-get-bullet item old-struct
))
1622 (new-box (org-list-get-checkbox item struct
)))
1623 (looking-at org-list-full-item-re
)
1624 ;; a. Replace bullet
1625 (unless (equal old-bul new-bul
)
1626 (replace-match new-bul nil nil nil
1))
1627 ;; b. Replace checkbox
1629 ((and new-box box-rule-p
1630 (save-match-data (org-at-item-description-p)))
1631 (message "Cannot add a checkbox to a description list item"))
1632 ((equal (match-string 3) new-box
))
1633 ((and (match-string 3) new-box
)
1634 (replace-match new-box nil nil nil
3))
1636 (goto-char (or (match-end 2) (match-end 1)))
1637 (looking-at "\\[[ X-]\\][ \t]+")
1639 (t (goto-char (or (match-end 2) (match-end 1)))
1640 (insert (concat new-box
" "))))
1641 ;; c. Indent item to appropriate column
1642 (unless (= new-ind old-ind
)
1643 (delete-region (goto-char (point-at-bol))
1644 (progn (skip-chars-forward " \t") (point)))
1645 (indent-to new-ind
)))))))
1646 ;; 1. First get list of items and position endings. We maintain
1647 ;; two alists: ITM-SHIFT, determining indentation shift needed
1648 ;; at item, and END-POS, a pseudo-alist where key is ending
1649 ;; position and value point
1650 (let (end-list acc-end itm-shift all-ends sliced-struct
)
1652 (let* ((pos (car e
))
1653 (ind-pos (org-list-get-ind pos struct
))
1654 (ind-old (org-list-get-ind pos old-struct
))
1655 (bul-pos (org-list-get-bullet pos struct
))
1656 (bul-old (org-list-get-bullet pos old-struct
))
1657 (ind-shift (- (+ ind-pos
(length bul-pos
))
1658 (+ ind-old
(length bul-old
))))
1659 (end-pos (org-list-get-item-end pos old-struct
)))
1660 (push (cons pos ind-shift
) itm-shift
)
1661 (unless (assq end-pos old-struct
)
1662 ;; To determine real ind of an ending position that is
1663 ;; not at an item, we have to find the item it belongs
1664 ;; to: it is the last item (ITEM-UP), whose ending is
1665 ;; further than the position we're interested in.
1666 (let ((item-up (assoc-default end-pos acc-end
'>)))
1667 (push (cons end-pos item-up
) end-list
)))
1668 (push (cons end-pos pos
) acc-end
)))
1670 ;; 2. Slice the items into parts that should be shifted by the
1671 ;; same amount of indentation. The slices are returned in
1672 ;; reverse order so changes modifying buffer do not change
1673 ;; positions they refer to.
1674 (setq all-ends
(sort (append (mapcar 'car itm-shift
)
1675 (org-uniquify (mapcar 'car end-list
)))
1677 (while (cdr all-ends
)
1678 (let* ((up (pop all-ends
))
1679 (down (car all-ends
))
1680 (ind (if (assq up struct
)
1681 (cdr (assq up itm-shift
))
1682 (cdr (assq (cdr (assq up end-list
)) itm-shift
)))))
1683 (push (list down up ind
) sliced-struct
)))
1684 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1685 ;; end to beginning. Take a special action when beginning is
1688 (unless (zerop (nth 2 e
)) (apply shift-body-ind e
))
1689 (let* ((beg (nth 1 e
))
1690 (cell (assq beg struct
)))
1691 (unless (or (not cell
) (equal cell
(assq beg old-struct
)))
1692 (funcall modify-item beg
))))
1694 ;; 4. Go back to initial position
1697 (defun org-list-write-struct (struct parents
)
1698 "Correct bullets, checkboxes and indentation in list at point.
1699 STRUCT is the list structure. PARENTS is the alist of parents, as
1700 returned by `org-list-parents-alist'."
1701 ;; Order of functions matters here: checkboxes and endings need
1702 ;; correct indentation to be set, and indentation needs correct
1705 ;; 0. Save a copy of structure before modifications
1706 (let ((old-struct (copy-tree struct
)))
1707 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
1708 ;; order to get items endings and bullets properly
1709 (org-list-struct-fix-ind struct parents
2)
1710 ;; 2. Get pseudo-alist of ending positions and sort it by position.
1711 ;; Then associate them to the structure.
1712 (let (end-list acc-end
)
1714 (let* ((pos (car e
))
1715 (ind-pos (org-list-get-ind pos struct
))
1716 (end-pos (org-list-get-item-end pos struct
)))
1717 (unless (assq end-pos struct
)
1718 ;; To determine real ind of an ending position that is
1719 ;; not at an item, we have to find the item it belongs
1720 ;; to: it is the last item (ITEM-UP), whose ending is
1721 ;; further than the position we're interested in.
1722 (let ((item-up (assoc-default end-pos acc-end
'>)))
1724 ;; Else part is for the bottom point
1725 (if item-up
(+ (org-list-get-ind item-up struct
) 2) 0)
1728 (push (cons ind-pos pos
) end-list
)
1729 (push (cons end-pos pos
) acc-end
)))
1731 (setq end-list
(sort end-list
(lambda (e1 e2
) (< (cdr e1
) (cdr e2
)))))
1732 (org-list-struct-assoc-end struct end-list
))
1733 ;; 3. Get bullets right
1734 (let ((prevs (org-list-prevs-alist struct
)))
1735 (org-list-struct-fix-bul struct prevs
)
1736 ;; 4. Now get real indentation
1737 (org-list-struct-fix-ind struct parents
)
1738 ;; 5. Eventually fix checkboxes
1739 (org-list-struct-fix-box struct parents prevs
))
1740 ;; 6. Apply structure modifications to buffer
1741 (org-list-struct-apply-struct struct old-struct
)))
1746 (defun org-apply-on-list (function init-value
&rest args
)
1747 "Call FUNCTION on each item of the list at point.
1748 FUNCTION must be called with at least one argument: INIT-VALUE,
1749 that will contain the value returned by the function at the
1750 previous item, plus ARGS extra arguments.
1752 FUNCTION is applied on items in reverse order.
1754 As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
1755 will return the number of items in the current list.
1757 Sublists of the list are skipped. Cursor is always at the
1758 beginning of the item."
1759 (let* ((struct (org-list-struct))
1760 (prevs (org-list-prevs-alist struct
))
1761 (item (copy-marker (point-at-bol)))
1762 (all (org-list-get-all-items (marker-position item
) struct prevs
))
1766 (setq value
(apply function value args
)))
1771 (defun org-list-set-item-visibility (item struct view
)
1772 "Set visibility of ITEM in STRUCT to VIEW.
1774 Possible values are: `folded', `children' or `subtree'. See
1775 `org-cycle' for more information."
1778 (let ((item-end (org-list-get-item-end-before-blank item struct
)))
1780 (outline-flag-region (save-excursion (goto-char item
) (point-at-eol))
1782 ((eq view
'children
)
1783 ;; First show everything.
1784 (org-list-set-item-visibility item struct
'subtree
)
1785 ;; Then fold every child.
1786 (let* ((parents (org-list-parents-alist struct
))
1787 (children (org-list-get-children item struct parents
)))
1789 (org-list-set-item-visibility e struct
'folded
))
1793 (let ((item-end (org-list-get-item-end item struct
)))
1794 (outline-flag-region item item-end nil
)))))
1796 (defun org-list-item-body-column (item)
1797 "Return column at which body of ITEM should start."
1798 (let (bpos bcol tpos tcol
)
1801 (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?[ \t]+")
1802 (setq bpos
(match-beginning 1) tpos
(match-end 0)
1803 bcol
(progn (goto-char bpos
) (current-column))
1804 tcol
(progn (goto-char tpos
) (current-column)))
1805 (when (> tcol
(+ bcol org-description-max-indent
))
1806 (setq tcol
(+ bcol
5))))
1810 ;;; Interactive functions
1812 (defalias 'org-list-get-item-begin
'org-in-item-p
)
1814 (defun org-beginning-of-item ()
1815 "Go to the beginning of the current item.
1816 Throw an error when not in a list."
1818 (let ((begin (org-in-item-p)))
1819 (if begin
(goto-char begin
) (error "Not in an item"))))
1821 (defun org-beginning-of-item-list ()
1822 "Go to the beginning item of the current list or sublist.
1823 Throw an error when not in a list."
1825 (let ((begin (org-in-item-p)))
1827 (error "Not in an item")
1829 (let* ((struct (org-list-struct))
1830 (prevs (org-list-prevs-alist struct
)))
1831 (goto-char (org-list-get-list-begin begin struct prevs
))))))
1833 (defun org-end-of-item-list ()
1834 "Go to the end of the current list or sublist.
1835 Throw an error when not in a list."
1837 (let ((begin (org-in-item-p)))
1839 (error "Not in an item")
1841 (let* ((struct (org-list-struct))
1842 (prevs (org-list-prevs-alist struct
)))
1843 (goto-char (org-list-get-list-end begin struct prevs
))))))
1845 (defun org-end-of-item ()
1846 "Go to the end of the current item.
1847 Throw an error when not in a list."
1849 (let ((begin (org-in-item-p)))
1851 (error "Not in an item")
1853 (let ((struct (org-list-struct)))
1854 (goto-char (org-list-get-item-end begin struct
))))))
1856 (defun org-previous-item ()
1857 "Move to the beginning of the previous item.
1858 Throw an error when not in a list, or at first item."
1860 (let ((begin (org-in-item-p)))
1862 (error "Not in an item")
1864 (let* ((struct (org-list-struct))
1865 (prevs (org-list-prevs-alist struct
))
1866 (prevp (org-list-get-prev-item begin struct prevs
)))
1867 (if prevp
(goto-char prevp
) (error "On first item"))))))
1869 (defun org-next-item ()
1870 "Move to the beginning of the next item.
1871 Throw an error when not in a plain list, or at last item."
1873 (let ((begin (org-in-item-p)))
1875 (error "Not in an item")
1877 (let* ((struct (org-list-struct))
1878 (prevs (org-list-prevs-alist struct
))
1879 (prevp (org-list-get-next-item begin struct prevs
)))
1880 (if prevp
(goto-char prevp
) (error "On last item"))))))
1882 (defun org-move-item-down ()
1883 "Move the item at point down, i.e. swap with following item.
1884 Subitems (items with larger indentation) are considered part of
1885 the item, so this really moves item trees."
1887 (unless (org-at-item-p) (error "Not at an item"))
1888 (let* ((pos (point))
1889 (col (current-column))
1890 (actual-item (point-at-bol))
1891 (struct (org-list-struct))
1892 (prevs (org-list-prevs-alist struct
))
1893 (next-item (org-list-get-next-item (point-at-bol) struct prevs
)))
1897 (error "Cannot move this item further down"))
1899 (org-list-exchange-items actual-item next-item struct
))
1900 ;; Use a short variation of `org-list-write-struct' as there's
1901 ;; no need to go through all the steps.
1902 (let ((old-struct (copy-tree struct
))
1903 (prevs (org-list-prevs-alist struct
))
1904 (parents (org-list-parents-alist struct
)))
1905 (org-list-struct-fix-bul struct prevs
)
1906 (org-list-struct-fix-ind struct parents
)
1907 (org-list-struct-apply-struct struct old-struct
)
1908 (goto-char (org-list-get-next-item (point-at-bol) struct prevs
)))
1909 (org-move-to-column col
))))
1911 (defun org-move-item-up ()
1912 "Move the item at point up, i.e. swap with previous item.
1913 Subitems (items with larger indentation) are considered part of
1914 the item, so this really moves item trees."
1916 (unless (org-at-item-p) (error "Not at an item"))
1917 (let* ((pos (point))
1918 (col (current-column))
1919 (actual-item (point-at-bol))
1920 (struct (org-list-struct))
1921 (prevs (org-list-prevs-alist struct
))
1922 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs
)))
1926 (error "Cannot move this item further up"))
1928 (org-list-exchange-items prev-item actual-item struct
))
1929 ;; Use a short variation of `org-list-write-struct' as there's
1930 ;; no need to go through all the steps.
1931 (let ((old-struct (copy-tree struct
))
1932 (prevs (org-list-prevs-alist struct
))
1933 (parents (org-list-parents-alist struct
)))
1934 (org-list-struct-fix-bul struct prevs
)
1935 (org-list-struct-fix-ind struct parents
)
1936 (org-list-struct-apply-struct struct old-struct
))
1937 (org-move-to-column col
))))
1939 (defun org-insert-item (&optional checkbox
)
1940 "Insert a new item at the current level.
1941 If cursor is before first character after bullet of the item, the
1942 new item will be created before the current one.
1944 If CHECKBOX is non-nil, add a checkbox next to the bullet.
1946 Return t when things worked, nil when we are not in an item, or
1948 (let ((itemp (org-in-item-p))
1950 ;; If cursor isn't is a list or if list is invisible, return nil.
1951 (unless (or (not itemp
)
1954 (outline-invisible-p)))
1957 (org-at-item-timer-p))
1958 ;; Timer list: delegate to `org-timer-item'.
1959 (progn (org-timer-item) t
)
1961 (let* ((struct (org-list-struct))
1962 (prevs (org-list-prevs-alist struct
))
1963 ;; If we're in a description list, ask for the new term.
1964 (desc (when (org-list-get-tag itemp struct
)
1965 (concat (read-string "Term: ") " :: ")))
1966 ;; Don't insert a checkbox if checkbox rule is applied
1967 ;; and it is a description item.
1968 (checkp (and checkbox
1970 (not (cdr (assq 'checkbox
1971 org-list-automatic-rules
)))))))
1973 (org-list-insert-item pos struct prevs checkp desc
))
1974 (org-list-write-struct struct
(org-list-parents-alist struct
))
1975 (when checkp
(org-update-checkbox-count-maybe))
1976 (looking-at org-list-full-item-re
)
1977 (goto-char (match-end 0))
1980 (defun org-list-repair ()
1981 "Fix indentation, bullets and checkboxes is the list at point."
1983 (unless (org-at-item-p) (error "This is not a list"))
1984 (let* ((struct (org-list-struct))
1985 (parents (org-list-parents-alist struct
)))
1986 (org-list-write-struct struct parents
)))
1988 (defun org-cycle-list-bullet (&optional which
)
1989 "Cycle through the different itemize/enumerate bullets.
1990 This cycle the entire list level through the sequence:
1992 `-' -> `+' -> `*' -> `1.' -> `1)'
1994 If WHICH is a valid string, use that as the new bullet. If WHICH
1995 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
1996 `previous', cycle backwards."
1998 (unless (org-at-item-p) (error "Not at an item"))
2001 (let* ((struct (org-list-struct))
2002 (parents (org-list-parents-alist struct
))
2003 (prevs (org-list-prevs-alist struct
))
2004 (list-beg (org-list-get-first-item (point) struct prevs
))
2005 (bullet (org-list-get-bullet list-beg struct
))
2006 (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules
)))
2007 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs
))
2008 (case-fold-search nil
)
2010 ((string-match "[a-z]\\." bullet
) "a.")
2011 ((string-match "[a-z])" bullet
) "a)")
2012 ((string-match "[A-Z]\\." bullet
) "A.")
2013 ((string-match "[A-Z])" bullet
) "A)")
2014 ((string-match "\\." bullet
) "1.")
2015 ((string-match ")" bullet
) "1)")
2016 (t (org-trim bullet
))))
2017 ;; Compute list of possible bullets, depending on context
2020 ;; *-bullets are not allowed at column 0
2021 (unless (and bullet-rule-p
2022 (looking-at "\\S-")) '("*"))
2023 ;; Description items cannot be numbered
2024 (unless (or (eq org-plain-list-ordered-item-terminator ?\
))
2025 (and bullet-rule-p
(org-at-item-description-p)))
2027 (unless (or (eq org-plain-list-ordered-item-terminator ?.
)
2028 (and bullet-rule-p
(org-at-item-description-p)))
2030 (unless (or (not alpha-p
)
2031 (eq org-plain-list-ordered-item-terminator ?\
))
2032 (and bullet-rule-p
(org-at-item-description-p)))
2034 (unless (or (not alpha-p
)
2035 (eq org-plain-list-ordered-item-terminator ?.
)
2036 (and bullet-rule-p
(org-at-item-description-p)))
2038 (len (length bullet-list
))
2039 (item-index (- len
(length (member current bullet-list
))))
2040 (get-value (lambda (index) (nth (mod index len
) bullet-list
)))
2042 ((member which bullet-list
) which
)
2043 ((numberp which
) (funcall get-value which
))
2044 ((eq 'previous which
) (funcall get-value
(1- item-index
)))
2045 (t (funcall get-value
(1+ item-index
))))))
2046 ;; Use a short variation of `org-list-write-struct' as there's
2047 ;; no need to go through all the steps.
2048 (let ((old-struct (copy-tree struct
)))
2049 (org-list-set-bullet list-beg struct
(org-list-bullet-string new
))
2050 (org-list-struct-fix-bul struct prevs
)
2051 (org-list-struct-fix-ind struct parents
)
2052 (org-list-struct-apply-struct struct old-struct
)))))
2054 (defun org-toggle-checkbox (&optional toggle-presence
)
2055 "Toggle the checkbox in the current line.
2056 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2057 double prefix, set checkbox to [-].
2059 When there is an active region, toggle status or presence of the
2060 first checkbox there, and make every item inside have the same
2061 status or presence, respectively.
2063 If the cursor is in a headline, apply this to all checkbox items
2064 in the text below the heading, taking as reference the first item
2065 in subtree, ignoring drawers."
2072 (orderedp (org-entry-get nil
"ORDERED"))
2074 ;; In a region, start at first item in region
2076 ((org-region-active-p)
2077 (let ((limit (region-end)))
2078 (goto-char (region-beginning))
2079 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2080 (setq lim-up
(point-at-bol))
2081 (error "No item in region"))
2082 (setq lim-down
(copy-marker limit
))))
2084 ;; On an heading, start at first item after drawers
2085 (let ((limit (save-excursion (outline-next-heading) (point))))
2087 (when (looking-at org-drawer-regexp
)
2088 (re-search-forward "^[ \t]*:END:" limit nil
))
2089 (if (org-list-search-forward (org-item-beginning-re) limit t
)
2090 (setq lim-up
(point-at-bol))
2091 (error "No item in subtree"))
2092 (setq lim-down
(copy-marker limit
))))
2093 ;; Just one item: set singlep flag
2096 (setq lim-up
(point-at-bol)
2097 lim-down
(point-at-eol)))
2098 (t (error "Not at an item or heading, and no active region"))))
2099 ;; Determine the checkbox going to be applied to all items
2104 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2106 ((equal toggle-presence
'(16)) "[-]")
2107 ((equal toggle-presence
'(4))
2108 (unless cbox
"[ ]"))
2109 ((equal "[ ]" cbox
) "[X]")
2111 ;; When an item is found within bounds, grab the full list at
2112 ;; point structure, then: 1. set checkbox of all its items
2113 ;; within bounds to ref-checkbox; 2. fix checkboxes of the whole
2114 ;; list; 3. move point after the list.
2116 (while (and (< (point) lim-down
)
2117 (org-list-search-forward (org-item-beginning-re)
2119 (let* ((struct (org-list-struct))
2120 (struct-copy (copy-tree struct
))
2121 (parents (org-list-parents-alist struct
))
2122 (prevs (org-list-prevs-alist struct
))
2123 (bottom (copy-marker (org-list-get-bottom-point struct
)))
2124 (items-to-toggle (org-remove-if
2125 (lambda (e) (or (< e lim-up
) (> e lim-down
)))
2126 (mapcar 'car struct
))))
2127 (mapc (lambda (e) (org-list-set-checkbox
2129 ;; if there is no box at item, leave as-is
2130 ;; unless function was called with C-u prefix
2131 (let ((cur-box (org-list-get-checkbox e struct
)))
2132 (if (or cur-box
(equal toggle-presence
'(4)))
2136 (setq block-item
(org-list-struct-fix-box
2137 struct parents prevs orderedp
))
2138 ;; Report some problems due to ORDERED status of subtree. If
2139 ;; only one box was being checked, throw an error, else,
2140 ;; only signal problems.
2142 ((and singlep block-item
(> lim-up block-item
))
2144 "Checkbox blocked because of unchecked box at line %d"
2145 (org-current-line block-item
)))
2148 "Checkboxes were removed due to unchecked box at line %d"
2149 (org-current-line block-item
))))
2151 (org-list-struct-apply-struct struct struct-copy
))))
2152 (org-update-checkbox-count-maybe)))
2154 (defun org-reset-checkbox-state-subtree ()
2155 "Reset all checkboxes in an entry subtree."
2159 (org-narrow-to-subtree)
2161 (goto-char (point-min))
2162 (let ((end (point-max)))
2163 (while (< (point) end
)
2164 (when (org-at-item-checkbox-p)
2165 (replace-match "[ ]" t t nil
1))
2166 (beginning-of-line 2))))
2167 (org-update-checkbox-count-maybe)))
2169 (defun org-update-checkbox-count (&optional all
)
2170 "Update the checkbox statistics in the current section.
2171 This will find all statistic cookies like [57%] and [6/12] and
2172 update them with the current numbers.
2174 With optional prefix argument ALL, do this for the whole buffer."
2177 (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2178 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2180 (or (not org-hierarchical-checkbox-statistics
)
2181 (string-match "\\<recursive\\>"
2182 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2184 (cons (point-min) (point-max))
2185 (cons (or (ignore-errors (org-back-to-heading) (point))
2187 (save-excursion (outline-next-heading) (point)))))
2190 ;; Return number of checked boxes and boxes of all types
2191 ;; in all structures in STRUCTS. If RECURSIVEP is non-nil,
2192 ;; also count boxes in sub-lists. If ITEM is nil, count
2193 ;; across the whole structure, else count only across
2194 ;; subtree whose ancestor is ITEM.
2195 (lambda (item structs recursivep
)
2196 (let ((c-on 0) (c-all 0))
2199 (let* ((pre (org-list-prevs-alist s
))
2200 (par (org-list-parents-alist s
))
2203 ((and recursivep item
) (org-list-get-subtree item s
))
2204 (recursivep (mapcar 'car s
))
2205 (item (org-list-get-children item s par
))
2206 (t (org-list-get-all-items
2207 (org-list-get-top-point s
) s pre
))))
2208 (cookies (delq nil
(mapcar
2210 (org-list-get-checkbox e s
))
2212 (setq c-all
(+ (length cookies
) c-all
)
2213 c-on
(+ (org-count "[X]" cookies
) c-on
))))
2215 (cons c-on c-all
)))))
2217 cookies-list structs-bak box-num
)
2218 (goto-char (car bounds
))
2219 ;; 1. Build an alist for each cookie found within BOUNDS. The
2220 ;; key will be position at beginning of cookie and values
2221 ;; ending position, format of cookie, and a cell whose car is
2222 ;; number of checked boxes to report, and cdr total number of
2224 (while (re-search-forward cookie-re
(cdr bounds
) t
)
2229 (match-beginning 1) ; cookie start
2230 (match-end 1) ; cookie end
2231 (match-string 2) ; percent?
2233 ;; Cookie is at an heading, but specifically for todo,
2234 ;; not for checkboxes: skip it.
2235 ((and (org-on-heading-p)
2236 (string-match "\\<todo\\>"
2238 (or (org-entry-get nil
"COOKIE_DATA") ""))))
2240 ;; Cookie is at an heading, but all lists before next
2241 ;; heading already have been read. Use data collected
2242 ;; in STRUCTS-BAK. This should only happen when heading
2243 ;; has more than one cookie on it.
2244 ((and (org-on-heading-p)
2245 (<= (save-excursion (outline-next-heading) (point))
2247 (funcall count-boxes nil structs-bak recursivep
))
2248 ;; Cookie is at a fresh heading. Grab structure of
2249 ;; every list containing a checkbox between point and
2250 ;; next headline, and save them in STRUCTS-BAK.
2252 (setq backup-end
(save-excursion
2253 (outline-next-heading) (point)))
2254 (while (org-list-search-forward box-re backup-end
'move
)
2255 (let* ((struct (org-list-struct))
2256 (bottom (org-list-get-bottom-point struct
)))
2257 (push struct structs-bak
)
2258 (goto-char bottom
)))
2259 (funcall count-boxes nil structs-bak recursivep
))
2260 ;; Cookie is at an item, and we already have list
2261 ;; structure stored in STRUCTS-BAK.
2262 ((and (org-at-item-p)
2263 (< (point-at-bol) backup-end
)
2264 ;; Only lists in no special context are stored.
2265 (not (nth 2 (org-list-context))))
2266 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2267 ;; Cookie is at an item, but we need to compute list
2270 (let ((struct (org-list-struct)))
2271 (setq backup-end
(org-list-get-bottom-point struct
)
2272 structs-bak
(list struct
)))
2273 (funcall count-boxes
(point-at-bol) structs-bak recursivep
))
2274 ;; Else, cookie found is at a wrong place. Skip it.
2275 (t (throw 'skip nil
))))
2277 ;; 2. Apply alist to buffer, in reverse order so positions stay
2278 ;; unchanged after cookie modifications.
2279 (mapc (lambda (cookie)
2280 (let* ((beg (car cookie
))
2281 (end (nth 1 cookie
))
2282 (percentp (nth 2 cookie
))
2283 (checked (car (nth 3 cookie
)))
2284 (total (cdr (nth 3 cookie
)))
2286 (format "[%d%%]" (/ (* 100 checked
)
2288 (format "[%d/%d]" checked total
))))
2291 (delete-region (point) (+ (point) (- end beg
)))))
2294 (defun org-get-checkbox-statistics-face ()
2295 "Select the face for checkbox statistics.
2296 The face will be `org-done' when all relevant boxes are checked.
2297 Otherwise it will be `org-todo'."
2299 (if (equal (match-string 1) "100%")
2300 'org-checkbox-statistics-done
2301 'org-checkbox-statistics-todo
)
2302 (if (and (> (match-end 2) (match-beginning 2))
2303 (equal (match-string 2) (match-string 3)))
2304 'org-checkbox-statistics-done
2305 'org-checkbox-statistics-todo
)))
2307 (defun org-update-checkbox-count-maybe ()
2308 "Update checkbox statistics unless turned off by user."
2309 (when (cdr (assq 'checkbox org-list-automatic-rules
))
2310 (org-update-checkbox-count))
2311 (run-hooks 'org-checkbox-statistics-hook
))
2313 (defvar org-last-indent-begin-marker
(make-marker))
2314 (defvar org-last-indent-end-marker
(make-marker))
2315 (defun org-list-indent-item-generic (arg no-subtree struct
)
2316 "Indent a local list item including its children.
2317 When number ARG is a negative, item will be outdented, otherwise
2318 it will be indented.
2320 If a region is active, all items inside will be moved.
2322 If NO-SUBTREE is non-nil, only indent the item itself, not its
2325 STRUCT is the list structure.
2327 Return t if successful."
2330 (let* ((regionp (org-region-active-p))
2331 (rbeg (and regionp
(region-beginning)))
2332 (rend (and regionp
(region-end)))
2333 (top (org-list-get-top-point struct
))
2334 (parents (org-list-parents-alist struct
))
2335 (prevs (org-list-prevs-alist struct
))
2336 ;; Are we going to move the whole list?
2338 (and (= top
(point))
2339 (cdr (assq 'indent org-list-automatic-rules
))
2342 "First item of list cannot move without its subtree")
2344 ;; Determine begin and end points of zone to indent. If moving
2345 ;; more than one item, save them for subsequent moves.
2346 (unless (and (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
2347 (memq this-command
'(org-shiftmetaright org-shiftmetaleft
)))
2350 (set-marker org-last-indent-begin-marker rbeg
)
2351 (set-marker org-last-indent-end-marker rend
))
2352 (set-marker org-last-indent-begin-marker
(point))
2353 (set-marker org-last-indent-end-marker
2355 (specialp (org-list-get-bottom-point struct
))
2356 (no-subtree (1+ (point)))
2357 (t (org-list-get-item-end (point) struct
))))))
2358 (let* ((beg (marker-position org-last-indent-begin-marker
))
2359 (end (marker-position org-last-indent-end-marker
)))
2361 ;; Special case: moving top-item with indent rule
2363 (let* ((level-skip (org-level-increment))
2364 (offset (if (< arg
0) (- level-skip
) level-skip
))
2365 (top-ind (org-list-get-ind beg struct
))
2366 (old-struct (copy-tree struct
)))
2367 (if (< (+ top-ind offset
) 0)
2368 (error "Cannot outdent beyond margin")
2369 ;; Change bullet if necessary
2370 (when (and (= (+ top-ind offset
) 0)
2372 (org-list-get-bullet beg struct
)))
2373 (org-list-set-bullet beg struct
2374 (org-list-bullet-string "-")))
2375 ;; Shift every item by OFFSET and fix bullets. Then
2376 ;; apply changes to buffer.
2378 (let ((ind (org-list-get-ind (car e
) struct
)))
2379 (org-list-set-ind (car e
) struct
(+ ind offset
))))
2381 (org-list-struct-fix-bul struct prevs
)
2382 (org-list-struct-apply-struct struct old-struct
))))
2385 ;; If only one item is moved, it mustn't have a child
2388 (org-list-has-child-p beg struct
))
2389 ;; If a subtree or region is moved, the last item
2390 ;; of the subtree mustn't have a child
2391 (let ((last-item (caar
2394 (lambda (e) (>= (car e
) end
))
2396 (org-list-has-child-p last-item struct
))))
2397 (error "Cannot outdent an item without its children"))
2402 (org-list-struct-outdent beg end struct parents
)
2403 (org-list-struct-indent beg end struct parents prevs
))))
2404 (org-list-write-struct struct new-parents
))
2405 (org-update-checkbox-count-maybe))))))
2408 (defun org-outdent-item ()
2409 "Outdent a local list item, but not its children.
2410 If a region is active, all items inside will be moved."
2413 (let ((struct (org-list-struct)))
2414 (org-list-indent-item-generic -
1 t struct
))
2415 (error "Not at an item")))
2417 (defun org-indent-item ()
2418 "Indent a local list item, but not its children.
2419 If a region is active, all items inside will be moved."
2422 (let ((struct (org-list-struct)))
2423 (org-list-indent-item-generic 1 t struct
))
2424 (error "Not at an item")))
2426 (defun org-outdent-item-tree ()
2427 "Outdent a local list item including its children.
2428 If a region is active, all items inside will be moved."
2430 (let ((regionp (org-region-active-p)))
2432 ((or (org-at-item-p)
2433 (and (org-region-active-p)
2434 (goto-char (region-beginning))
2436 (let ((struct (org-list-struct)))
2437 (org-list-indent-item-generic -
1 nil struct
)))
2438 (regionp (error "Region not starting at an item"))
2439 (t (error "Not at an item")))))
2441 (defun org-indent-item-tree ()
2442 "Indent a local list item including its children.
2443 If a region is active, all items inside will be moved."
2445 (let ((regionp (org-region-active-p)))
2447 ((or (org-at-item-p)
2448 (and (org-region-active-p)
2449 (goto-char (region-beginning))
2451 (let ((struct (org-list-struct)))
2452 (org-list-indent-item-generic 1 nil struct
)))
2453 (regionp (error "Region not starting at an item"))
2454 (t (error "Not at an item")))))
2456 (defvar org-tab-ind-state
)
2457 (defun org-cycle-item-indentation ()
2458 "Cycle levels of indentation of an empty item.
2459 The first run indents the item, if applicable. Subsequents runs
2460 outdent it at meaningful levels in the list. When done, item is
2461 put back at its original position with its original bullet.
2463 Return t at each successful move."
2464 (when (org-at-item-p)
2465 (let* ((org-adapt-indentation nil
)
2466 (struct (org-list-struct))
2467 (ind (org-list-get-ind (point-at-bol) struct
))
2468 (bullet (org-list-get-bullet (point-at-bol) struct
)))
2469 ;; Accept empty items or if cycle has already started.
2470 (when (or (eq last-command
'org-cycle-item-indentation
)
2471 (and (save-excursion
2473 (looking-at org-list-full-item-re
))
2474 (>= (match-end 0) (save-excursion
2475 (goto-char (org-list-get-item-end
2476 (point-at-bol) struct
))
2477 (skip-chars-backward " \r\t\n")
2479 (setq this-command
'org-cycle-item-indentation
)
2480 ;; When in the middle of the cycle, try to outdent first. If it
2481 ;; fails, and point is still at initial position, indent. Else,
2482 ;; go back to original position.
2483 (if (eq last-command
'org-cycle-item-indentation
)
2485 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2486 ((and (= ind
(car org-tab-ind-state
))
2487 (ignore-errors (org-list-indent-item-generic 1 t struct
))))
2488 (t (delete-region (point-at-bol) (point-at-eol))
2489 (org-indent-to-column (car org-tab-ind-state
))
2490 (insert (cdr org-tab-ind-state
))
2492 (setq this-command
'identity
)))
2493 ;; If a cycle is starting, remember indentation and bullet,
2494 ;; then try to indent. If it fails, try to outdent.
2495 (setq org-tab-ind-state
(cons ind bullet
))
2497 ((ignore-errors (org-list-indent-item-generic 1 t struct
)))
2498 ((ignore-errors (org-list-indent-item-generic -
1 t struct
)))
2499 (t (error "Cannot move item"))))
2502 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func
)
2504 The cursor may be at any item of the list that should be sorted.
2505 Sublists are not sorted. Checkboxes, if any, are ignored.
2507 Sorting can be alphabetically, numerically, by date/time as given by
2508 a time stamp, by a property or by priority.
2510 Comparing entries ignores case by default. However, with an
2511 optional argument WITH-CASE, the sorting considers case as well.
2513 The command prompts for the sorting type unless it has been given
2514 to the function through the SORTING-TYPE argument, which needs to
2515 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
2516 meaning of each character:
2518 n Numerically, by converting the beginning of the item to a number.
2519 a Alphabetically. Only the first line of item is checked.
2520 t By date/time, either the first active time stamp in the entry, if
2521 any, or by the first inactive one. In a timer list, sort the timers.
2523 Capital letters will reverse the sort order.
2525 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
2526 function to be called with point at the beginning of the record.
2527 It must return either a string or a number that should serve as
2528 the sorting key for that record. It will then use COMPARE-FUNC to
2531 (let* ((case-func (if with-case
'identity
'downcase
))
2532 (struct (org-list-struct))
2533 (prevs (org-list-prevs-alist struct
))
2534 (start (org-list-get-list-begin (point-at-bol) struct prevs
))
2535 (end (org-list-get-list-end (point-at-bol) struct prevs
))
2539 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
2540 (read-char-exclusive)))
2541 (getkey-func (and (= (downcase sorting-type
) ?f
)
2542 (org-icompleting-read "Sort using function: "
2543 obarray
'fboundp t nil nil
)
2544 (intern getkey-func
))))
2545 (message "Sorting items...")
2547 (narrow-to-region start end
)
2548 (goto-char (point-min))
2549 (let* ((dcst (downcase sorting-type
))
2550 (case-fold-search nil
)
2551 (now (current-time))
2553 ((= dcst ?a
) 'string
<)
2554 ((= dcst ?f
) compare-func
)
2557 (next-record (lambda ()
2558 (skip-chars-forward " \r\t\n")
2559 (beginning-of-line)))
2560 (end-record (lambda ()
2561 (goto-char (org-list-get-item-end-before-blank
2565 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2568 (string-to-number (buffer-substring (match-end 0)
2571 (buffer-substring (match-end 0) (point-at-eol)))
2574 ;; If it is a timer list, convert timer to seconds
2575 ((org-at-item-timer-p)
2576 (org-timer-hms-to-secs (match-string 1)))
2577 ((or (re-search-forward org-ts-regexp
(point-at-eol) t
)
2578 (re-search-forward org-ts-regexp-both
2580 (org-time-string-to-seconds (match-string 0)))
2581 (t (org-float-time now
))))
2584 (let ((value (funcall getkey-func
)))
2586 (funcall case-func value
)
2588 (error "Invalid key function `%s'" getkey-func
)))
2589 (t (error "Invalid sorting type `%c'" sorting-type
)))))))
2590 (sort-subr (/= dcst sorting-type
)
2596 ;; Read and fix list again, as `sort-subr' probably destroyed
2599 (run-hooks 'org-after-sorting-entries-or-items-hook
)
2600 (message "Sorting items...done")))))
2603 ;;; Send and receive lists
2605 (defun org-list-parse-list (&optional delete
)
2606 "Parse the list at point and maybe DELETE it.
2608 Return a list whose car is a symbol of list type, among
2609 `ordered', `unordered' and `descriptive'. Then, each item is a
2610 list whose car is counter, and cdr are strings and other
2611 sub-lists. Inside strings, checkboxes are replaced by \"[CBON]\"
2614 For example, the following list:
2619 more text in first item
2625 \(nil \"first item\"
2627 \(nil \"sub-item one\"\)
2628 \(nil \"[CBON] sub-item two\"\)\)
2629 \"more text in first item\"\)
2630 \(3 \"last item\"\)\)
2632 Point is left at list end."
2633 (let* ((struct (org-list-struct))
2634 (prevs (org-list-prevs-alist struct
))
2635 (parents (org-list-parents-alist struct
))
2636 (top (org-list-get-top-point struct
))
2637 (bottom (org-list-get-bottom-point struct
))
2639 parse-item
; for byte-compiler
2642 ;; Return text between BEG and END, trimmed, with
2643 ;; checkboxes replaced.
2645 (let ((text (org-trim (buffer-substring beg end
))))
2646 (if (string-match "\\`\\[\\([X ]\\)\\]" text
)
2648 (if (equal (match-string 1 text
) " ") "CBOFF" "CBON")
2653 ;; Return a list whose car is list type and cdr a list of
2656 (cons (org-list-get-list-type (car e
) struct prevs
)
2657 (mapcar parse-item e
)))))
2660 ;; Return a list containing counter of item, if any, text
2661 ;; and any sublist inside it.
2663 (let ((start (save-excursion
2665 (looking-at "[ \t]*\\S-+[ \t]+\\(\\[@[:[:alnum:]]+\\][ \t]*\\)?")
2667 ;; Get counter number. For alphabetic counter, get
2668 ;; its position in the alphabet.
2669 (counter (let ((c (org-list-get-counter e struct
)))
2672 ((string-match "[A-Za-z]" c
)
2673 (- (string-to-char (upcase (match-string 0 c
)))
2675 ((string-match "[0-9]+" c
)
2676 (string-to-number (match-string 0 c
))))))
2677 (childp (org-list-has-child-p e struct
))
2678 (end (org-list-get-item-end e struct
)))
2679 ;; If item has a child, store text between bullet and
2680 ;; next child, then recursively parse all sublists. At
2681 ;; the end of each sublist, check for the presence of
2682 ;; text belonging to the original item.
2684 (let* ((children (org-list-get-children e struct parents
))
2685 (body (list (funcall get-text start childp
))))
2687 (let* ((first (car children
))
2688 (sub (org-list-get-all-items first struct prevs
))
2689 (last-c (car (last sub
)))
2690 (last-end (org-list-get-item-end last-c struct
)))
2691 (push (funcall parse-sublist sub
) body
)
2692 ;; Remove children from the list just parsed.
2693 (setq children
(cdr (member last-c children
)))
2694 ;; There is a chunk of text belonging to the
2695 ;; item if last child doesn't end where next
2696 ;; child starts or where item ends.
2697 (unless (= (or (car children
) end
) last-end
)
2698 (push (funcall get-text
2699 last-end
(or (car children
) end
))
2701 (cons counter
(nreverse body
)))
2702 (list counter
(funcall get-text start end
))))))))
2703 ;; Store output, take care of cursor position and deletion of
2704 ;; list, then return output.
2705 (setq out
(funcall parse-sublist
(org-list-get-all-items top struct prevs
)))
2708 (delete-region top bottom
)
2709 (when (and (not (eq org-list-ending-method
'indent
))
2710 (looking-at org-list-end-re
))
2711 (replace-match "\n")))
2714 (defun org-list-make-subtree ()
2715 "Convert the plain list at point into a subtree."
2717 (if (not (ignore-errors (goto-char (org-in-item-p))))
2718 (error "Not in a list")
2719 (let ((list (save-excursion (org-list-parse-list t
))))
2720 (insert (org-list-to-subtree list
)))))
2722 (defun org-list-insert-radio-list ()
2723 "Insert a radio list template appropriate for this major mode."
2725 (let* ((e (assq major-mode org-list-radio-list-templates
))
2728 (unless e
(error "No radio list setup defined for %s" major-mode
))
2729 (setq name
(read-string "List name: "))
2730 (while (string-match "%n" txt
)
2731 (setq txt
(replace-match name t t txt
)))
2732 (or (bolp) (insert "\n"))
2737 (defun org-list-send-list (&optional maybe
)
2738 "Send a transformed version of this list to the receiver position.
2739 With argument MAYBE, fail quietly if no transformation is defined for
2743 (unless (org-at-item-p) (error "Not at a list item"))
2745 (re-search-backward "#\\+ORGLST" nil t
)
2746 (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
2749 (error "Don't know how to transform this list"))))
2750 (let* ((name (match-string 1))
2751 (transform (intern (match-string 2)))
2755 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t
)
2756 (match-beginning 0)))
2759 (re-search-backward "#\\+ORGLST" nil t
)
2760 (re-search-forward (org-item-beginning-re) bottom-point t
)
2761 (match-beginning 0)))
2762 (list (save-restriction
2763 (narrow-to-region top-point bottom-point
)
2764 (org-list-parse-list)))
2766 (unless (fboundp transform
)
2767 (error "No such transformation function %s" transform
))
2768 (let ((txt (funcall transform list
)))
2769 ;; Find the insertion place
2771 (goto-char (point-min))
2772 (unless (re-search-forward
2773 (concat "BEGIN RECEIVE ORGLST +"
2775 "\\([ \t]\\|$\\)") nil t
)
2776 (error "Don't know where to insert translated list"))
2777 (goto-char (match-beginning 0))
2778 (beginning-of-line 2)
2780 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
2781 (error "Cannot find end of insertion region"))
2782 (delete-region beg
(point-at-bol))
2785 (message "List converted and installed at receiver location"))))
2787 (defun org-list-to-generic (list params
)
2788 "Convert a LIST parsed through `org-list-parse-list' to other formats.
2789 Valid parameters PARAMS are
2791 :ustart String to start an unordered list
2792 :uend String to end an unordered list
2794 :ostart String to start an ordered list
2795 :oend String to end an ordered list
2797 :dstart String to start a descriptive list
2798 :dend String to end a descriptive list
2799 :dtstart String to start a descriptive term
2800 :dtend String to end a descriptive term
2801 :ddstart String to start a description
2802 :ddend String to end a description
2804 :splice When set to t, return only list body lines, don't wrap
2805 them into :[u/o]start and :[u/o]end. Default is nil.
2807 :istart String to start a list item.
2808 :icount String to start an item with a counter.
2809 :iend String to end a list item
2810 :isep String to separate items
2811 :lsep String to separate sublists
2812 :csep String to separate text from a sub-list
2814 :cboff String to insert for an unchecked checkbox
2815 :cbon String to insert for a checked checkbox
2817 Alternatively, each parameter can also be a form returning a
2818 string. These sexp can use keywords `counter' and `depth',
2819 reprensenting respectively counter associated to the current
2820 item, and depth of the current sub-list, starting at 0.
2821 Obviously, `counter' is only available for parameters applying to
2825 (splicep (plist-get p
:splice
))
2826 (ostart (plist-get p
:ostart
))
2827 (oend (plist-get p
:oend
))
2828 (ustart (plist-get p
:ustart
))
2829 (uend (plist-get p
:uend
))
2830 (dstart (plist-get p
:dstart
))
2831 (dend (plist-get p
:dend
))
2832 (dtstart (plist-get p
:dtstart
))
2833 (dtend (plist-get p
:dtend
))
2834 (ddstart (plist-get p
:ddstart
))
2835 (ddend (plist-get p
:ddend
))
2836 (istart (plist-get p
:istart
))
2837 (icount (plist-get p
:icount
))
2838 (iend (plist-get p
:iend
))
2839 (isep (plist-get p
:isep
))
2840 (lsep (plist-get p
:lsep
))
2841 (csep (plist-get p
:csep
))
2842 (cbon (plist-get p
:cbon
))
2843 (cboff (plist-get p
:cboff
))
2844 export-sublist
; for byte-compiler
2847 ;; Export an item ITEM of type TYPE, at DEPTH. First string
2848 ;; in item is treated in a special way as it can bring
2849 ;; extra information that needs to be processed.
2850 (lambda (item type depth
)
2851 (let* ((counter (pop item
))
2853 ((eq type
'descriptive
)
2854 (concat (org-trim (eval istart
)) "%s"
2856 ((and counter
(eq type
'ordered
))
2857 (concat (eval icount
) "%s"))
2858 (t (concat (eval istart
) "%s")))
2861 ;; Replace checkbox if any is found.
2863 ((string-match "\\[CBON\\]" first
)
2864 (setq first
(replace-match cbon t t first
)))
2865 ((string-match "\\[CBOFF\\]" first
)
2866 (setq first
(replace-match cboff t t first
)))
2867 ((string-match "\\[-\\]" first
)
2868 (setq first
(replace-match "$\\boxminus$" t t first
))))
2869 ;; Insert descriptive term if TYPE is `descriptive'.
2870 (when (and (eq type
'descriptive
)
2871 (string-match "^\\(.*\\)[ \t]+::" first
))
2873 (eval dtstart
) (org-trim (match-string 1 first
))
2874 (eval dtend
) (eval ddstart
)
2875 (org-trim (substring first
(match-end 0))) "")))
2878 (mapconcat (lambda (e)
2880 (funcall export-sublist e
(1+ depth
))))
2881 item
(or (eval csep
) "")))))))
2884 ;; Export sublist SUB at DEPTH
2886 (let* ((type (car sub
))
2891 (concat (eval ostart
) "\n%s" (eval oend
)))
2892 ((eq type
'descriptive
)
2893 (concat (eval dstart
) "\n%s" (eval dend
)))
2894 (t (concat (eval ustart
) "\n%s" (eval uend
))))
2896 (format fmt
(mapconcat (lambda (e)
2897 (funcall export-item e type depth
))
2898 items
(or (eval isep
) ""))))))))
2899 (concat (funcall export-sublist list
0) "\n")))
2901 (defun org-list-to-latex (list &optional params
)
2902 "Convert LIST into a LaTeX list.
2903 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
2904 with overruling parameters for `org-list-to-generic'."
2905 (org-list-to-generic
2908 '(:splice nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
2909 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
2910 :dstart
"\\begin{description}" :dend
"\\end{description}"
2911 :dtstart
"[" :dtend
"] "
2912 :istart
"\\item " :iend
"\n"
2913 :icount
(let ((enum (nth depth
'("i" "ii" "iii" "iv"))))
2915 (format "\\setcounter{enum%s}{%s}\n\\item "
2919 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
2922 (defun org-list-to-html (list &optional params
)
2923 "Convert LIST into a HTML list.
2924 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
2925 with overruling parameters for `org-list-to-generic'."
2926 (org-list-to-generic
2929 '(:splice nil
:ostart
"<ol>" :oend
"\n</ol>"
2930 :ustart
"<ul>" :uend
"\n</ul>"
2931 :dstart
"<dl>" :dend
"</dl>"
2932 :dtstart
"<dt>" :dtend
"</dt>\n"
2933 :ddstart
"<dd>" :ddend
"</dd>"
2934 :istart
"<li>" :iend
"\n</li>"
2935 :icount
(format "<li value=\"%s\">" counter
)
2936 :isep
"\n" :lsep
"\n" :csep
"\n"
2937 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>")
2940 (defun org-list-to-texinfo (list &optional params
)
2941 "Convert LIST into a Texinfo list.
2942 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
2943 with overruling parameters for `org-list-to-generic'."
2944 (org-list-to-generic
2947 '(:splice nil
:ostart
"@itemize @minus" :oend
"@end itemize"
2948 :ustart
"@enumerate" :uend
"@end enumerate"
2949 :dstart
"@table @asis" :dend
"@end table"
2950 :dtstart
" " :dtend
"\n"
2951 :istart
"@item\n" :iend
"\n"
2954 :cbon
"@code{[X]}" :cboff
"@code{[ ]}")
2957 (defun org-list-to-subtree (list &optional params
)
2958 "Convert LIST into an Org subtree.
2959 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
2960 with overruling parameters for `org-list-to-generic'."
2961 (let* ((rule (cdr (assq 'heading org-blank-before-new-entry
)))
2962 (level (or (org-current-level) 0))
2963 (blankp (or (eq rule t
)
2964 (and (eq rule
'auto
)
2966 (outline-previous-heading)
2967 (org-previous-line-empty-p)))))
2970 ;; Return the string for the heading, depending on depth D
2971 ;; of current sub-list.
2974 (make-string (+ level
2975 (if org-odd-levels-only
(* 2 (1+ d
)) (1+ d
)))
2978 (org-list-to-generic
2982 :dtstart
" " :dtend
" "
2983 :istart
(funcall get-stars depth
)
2984 :icount
(funcall get-stars depth
)
2985 :isep
(if blankp
"\n\n" "\n")
2986 :csep
(if blankp
"\n\n" "\n")
2987 :cbon
"DONE" :cboff
"TODO")
2992 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
2993 ;;; org-list.el ends here