1 ;;; org-list.el --- Plain lists for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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.
37 (defvar org-blank-before-new-entry
)
38 (defvar org-M-RET-may-split-line
)
40 (declare-function org-invisible-p
"org" ())
41 (declare-function org-on-heading-p
"org" (&optional invisible-ok
))
42 (declare-function outline-next-heading
"outline" ())
43 (declare-function outline-back-to-heading
"outline" (&optional invisible-ok
))
44 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
45 (declare-function org-back-over-empty-lines
"org" ())
46 (declare-function org-skip-whitespace
"org" ())
47 (declare-function org-trim
"org" (s))
48 (declare-function org-get-indentation
"org" (&optional line
))
49 (declare-function org-timer-item
"org-timer" (&optional arg
))
50 (declare-function org-combine-plists
"org" (&rest plists
))
51 (declare-function org-entry-get
"org" (pom property
&optional inherit
))
52 (declare-function org-narrow-to-subtree
"org" ())
53 (declare-function org-show-subtree
"org" ())
55 (defgroup org-plain-lists nil
56 "Options concerning plain lists in Org-mode."
57 :tag
"Org Plain lists"
58 :group
'org-structure
)
60 (defcustom org-cycle-include-plain-lists nil
61 "Non-nil means, include plain lists into visibility cycling.
62 This means that during cycling, plain list items will *temporarily* be
63 interpreted as outline headlines with a level given by 1000+i where i is the
64 indentation of the bullet. In all other operations, plain list items are
65 not seen as headlines. For example, you cannot assign a TODO keyword to
67 :group
'org-plain-lists
70 (defcustom org-plain-list-ordered-item-terminator t
71 "The character that makes a line with leading number an ordered list item.
72 Valid values are ?. and ?\). To get both terminators, use t. While
73 ?. may look nicer, it creates the danger that a line with leading
74 number may be incorrectly interpreted as an item. ?\) therefore is
76 :group
'org-plain-lists
77 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
78 (const :tag
"paren like in \"2)\"" ?\
))
79 (const :tab
"both" t
)))
81 (defcustom org-list-two-spaces-after-bullet-regexp nil
82 "A regular expression matching bullets that should have 2 spaces after them.
83 When nil, no bullet will have two spaces after them.
84 When a string, it will be used as a regular expression. When the bullet
85 type of a list is changed, the new bullet type will be matched against this
86 regexp. If it matches, there will be two spaces instead of one after
87 the bullet in each item of he list."
88 :group
'org-plain-list
90 (const :tag
"never" nil
)
93 (defcustom org-empty-line-terminates-plain-lists nil
94 "Non-nil means, an empty line ends all plain list levels.
95 When nil, empty lines are part of the preceding item."
96 :group
'org-plain-lists
99 (defcustom org-auto-renumber-ordered-lists t
100 "Non-nil means, automatically renumber ordered plain lists.
101 Renumbering happens when the sequence have been changed with
102 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
103 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
104 :group
'org-plain-lists
107 (defcustom org-provide-checkbox-statistics t
108 "Non-nil means, update checkbox statistics after insert and toggle.
109 When this is set, checkbox statistics is updated each time you either insert
110 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
111 with \\[org-ctrl-c-ctrl-c\\]."
112 :group
'org-plain-lists
115 (defcustom org-hierarchical-checkbox-statistics t
116 "Non-nil means, checkbox statistics counts only the state of direct children.
117 When nil, all boxes below the cookie are counted."
118 :group
'org-plain-lists
121 (defcustom org-description-max-indent
20
122 "Maximum indentation for the second line of a description list.
123 When the indentation would be larger than this, it will become
124 5 characters instead."
125 :group
'org-plain-lists
128 (defvar org-list-beginning-re
129 "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +\\(.*\\)$")
131 (defcustom org-list-radio-list-templates
132 '((latex-mode "% BEGIN RECEIVE ORGLST %n
133 % END RECEIVE ORGLST %n
135 #+ORGLST: SEND %n org-list-to-latex
138 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
139 @c END RECEIVE ORGLST %n
141 #+ORGLST: SEND %n org-list-to-texinfo
144 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
145 <!-- END RECEIVE ORGLST %n -->
147 #+ORGLST: SEND %n org-list-to-html
150 "Templates for radio lists in different major modes.
151 All occurrences of %n in a template will be replaced with the name of the
152 list, obtained by prompting the user."
153 :group
'org-plain-lists
155 (list (symbol :tag
"Major mode")
156 (string :tag
"Format"))))
158 ;;;; Plain list items, including checkboxes
162 (defun org-at-item-p ()
163 "Is point in a line starting a hand-formatted item?"
164 (let ((llt org-plain-list-ordered-item-terminator
))
166 (goto-char (point-at-bol))
169 ((eq llt t
) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
170 ((= llt ?.
) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
171 ((= llt ?\
)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
172 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
174 (defun org-at-item-bullet-p ()
175 "Is point at the bullet of a plain list item?"
177 (not (member (char-after) '(?\ ?
\t)))
178 (< (point) (match-end 0))))
180 (defun org-in-item-p ()
181 "It the cursor inside a plain list item.
182 Does not have to be the first line."
186 (org-beginning-of-item)
191 (defun org-insert-item (&optional checkbox
)
192 "Insert a new item at the current level.
193 Return t when things worked, nil when we are not in an item."
194 (when (save-excursion
197 (org-beginning-of-item)
199 (if (org-invisible-p) (error "Invisible item"))
202 (let* ((bul (match-string 0))
203 (descp (save-excursion (goto-char (match-beginning 0))
204 (beginning-of-line 1)
206 (and (looking-at "[ \t]*\\(.*?\\) ::")
208 (empty-line-p (save-excursion
209 (goto-char (match-beginning 0))
211 (or (beginning-of-line 0) t
)
213 (looking-at "[ \t]*$")))))
216 (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
218 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
220 (blank-a (if org-empty-line-terminates-plain-lists
222 (cdr (assq 'plain-list-item org-blank-before-new-entry
))))
223 (blank (if (eq blank-a
'auto
) empty-line-p blank-a
))
225 (if descp
(setq checkbox nil
))
227 (progn (org-timer-item) t
)
229 ((and (org-at-item-p) (<= (point) eow
))
231 (beginning-of-line 1)
232 (open-line (if blank
2 1)))
234 (beginning-of-line 1))
236 (unless (org-get-alist-option org-M-RET-may-split-line
'item
)
238 (delete-horizontal-space))
239 (newline (if blank
2 1))))
241 (if checkbox
"[ ]" "")
242 (if descp
(concat (if checkbox
" " "")
243 (read-string "Term: ") " :: ") ""))
247 (unless (= (point) pos
) (just-one-space) (backward-delete-char 1)))
248 (org-maybe-renumber-ordered-list)
249 (and checkbox
(org-update-checkbox-count-maybe))
254 (defun org-at-item-checkbox-p ()
255 "Is point at a line starting a plain-list item with a checklet?"
258 (goto-char (match-end 0))
259 (skip-chars-forward " \t")
260 (looking-at "\\[[- X]\\]"))))
262 (defun org-toggle-checkbox (&optional toggle-presence
)
263 "Toggle the checkbox in the current line.
264 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
265 With double prefix, set checkbox to [-].
266 When there is an active region, toggle status or presence of the checkbox
267 in the first line, and make every item in the region have the same
268 status or presence, respectively.
269 If the cursor is in a headline, apply this to all checkbox items in the
270 text below the heading."
273 (let (beg end status first-present first-status blocked
)
275 ((org-region-active-p)
276 (setq beg
(region-beginning) end
(region-end)))
278 (setq beg
(point) end
(save-excursion (outline-next-heading) (point))))
279 ((org-at-item-checkbox-p)
281 (if (equal toggle-presence
'(4))
284 (goto-char (match-beginning 0))
286 (when (setq blocked
(org-checkbox-blocked-p))
287 (error "Checkbox blocked because of unchecked box in line %d"
290 (cond ((equal toggle-presence
'(16)) "[-]")
291 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
298 (goto-char (match-end 0))
301 (t (error "Not at a checkbox or heading, and no active region")))
302 (setq end
(move-marker (make-marker) end
))
305 (setq first-present
(org-at-item-checkbox-p)
308 (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t
)
309 (equal (match-string 1) "[X]"))))
310 (while (< (point) end
)
313 ((and first-present
(org-at-item-checkbox-p))
316 (goto-char (match-beginning 0))
318 ((and (not first-present
) (not (org-at-item-checkbox-p))
321 (goto-char (match-end 0))
323 (when (org-at-item-checkbox-p)
324 (setq status
(equal (match-string 0) "[X]"))
326 (if first-status
"[ ]" "[X]") t t
)))
327 (beginning-of-line 2)))))
328 (org-update-checkbox-count-maybe))
330 (defun org-reset-checkbox-state-subtree ()
331 "Reset all checkboxes in an entry subtree."
335 (org-narrow-to-subtree)
337 (goto-char (point-min))
338 (let ((end (point-max)))
339 (while (< (point) end
)
340 (when (org-at-item-checkbox-p)
341 (replace-match "[ ]" t t
))
342 (beginning-of-line 2))))
343 (org-update-checkbox-count-maybe)))
345 (defun org-checkbox-blocked-p ()
346 "Is the current checkbox blocked from for being checked now?
347 A checkbox is blocked if all of the following conditions are fulfilled:
349 1. The checkbox is not checked already.
350 2. The current entry has the ORDERED property set.
351 3. There is an unchecked checkbox in this entry before the current line."
355 (unless (org-at-item-checkbox-p) (throw 'exit nil
))
356 (when (equal (match-string 0) "[X]")
357 ;; the box is already checked!
359 (let ((end (point-at-bol)))
360 (condition-case nil
(org-back-to-heading t
)
361 (error (throw 'exit nil
)))
362 (unless (org-entry-get nil
"ORDERED") (throw 'exit nil
))
363 (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t
)
367 (defun org-update-checkbox-count-maybe ()
368 "Update checkbox statistics unless turned off by user."
369 (when org-provide-checkbox-statistics
370 (org-update-checkbox-count)))
372 (defun org-update-checkbox-count (&optional all
)
373 "Update the checkbox statistics in the current section.
374 This will find all statistic cookies like [57%] and [6/12] and update them
375 with the current numbers. With optional prefix argument ALL, do this for
379 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
380 (beg (condition-case nil
381 (progn (org-back-to-heading) (point))
382 (error (point-min))))
383 (end (move-marker (make-marker)
384 (progn (outline-next-heading) (point))))
385 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
386 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
387 (re-find (concat re
"\\|" re-box
))
388 beg-cookie end-cookie is-percent c-on c-off lim
389 eline curr-ind next-ind continue-from startsearch
393 (goto-char (point-min))
394 (outline-next-heading)
395 (setq beg
(point) end
(point-max)))
397 ;; find each statistics cookie
398 (while (and (re-search-backward re-find beg t
)
399 (not (save-match-data
400 (and (org-on-heading-p)
407 (setq beg-cookie
(match-beginning 1)
408 end-cookie
(match-end 1)
409 cstat
(+ cstat
(if end-cookie
1 0))
410 startsearch
(point-at-eol)
411 continue-from
(match-beginning 0)
412 is-percent
(match-beginning 2)
414 ((org-on-heading-p) (outline-next-heading) (point))
415 ((org-at-item-p) (org-end-of-item) (point))
420 ;; find first checkbox for this cookie and gather
421 ;; statistics from all that are at this indentation level
422 (goto-char startsearch
)
423 (if (re-search-forward re-box lim t
)
425 (org-beginning-of-item)
426 (setq curr-ind
(org-get-indentation))
427 (setq next-ind curr-ind
)
428 (while (and (bolp) (org-at-item-p)
429 (if org-hierarchical-checkbox-statistics
430 (= curr-ind next-ind
)
431 (<= curr-ind next-ind
)))
432 (save-excursion (end-of-line) (setq eline
(point)))
433 (if (re-search-forward re-box eline t
)
434 (if (member (match-string 2) '("[ ]" "[-]"))
435 (setq c-off
(1+ c-off
))
436 (setq c-on
(1+ c-on
))
439 (if org-hierarchical-checkbox-statistics
442 (when (re-search-forward org-list-beginning-re lim t
)
443 (beginning-of-line)))
444 (setq next-ind
(org-get-indentation))
446 (goto-char continue-from
)
449 (delete-region beg-cookie end-cookie
)
450 (goto-char beg-cookie
)
453 (format "[%d%%]" (/ (* 100 c-on
) (max 1 (+ c-on c-off
))))
454 (format "[%d/%d]" c-on
(+ c-on c-off
)))))
455 ;; update items checkbox if it has one
456 (when (org-at-item-p)
457 (org-beginning-of-item)
458 (when (and (> (+ c-on c-off
) 0)
459 (re-search-forward re-box
(point-at-eol) t
))
460 (setq beg-cookie
(match-beginning 2)
461 end-cookie
(match-end 2))
462 (delete-region beg-cookie end-cookie
)
463 (goto-char beg-cookie
)
464 (cond ((= c-off
0) (insert "[X]"))
465 ((= c-on
0) (insert "[ ]"))
468 (goto-char continue-from
))
469 (when (interactive-p)
470 (message "Checkbox statistics updated %s (%d places)"
471 (if all
"in entire file" "in current outline entry") cstat
)))))
473 (defun org-get-checkbox-statistics-face ()
474 "Select the face for checkbox statistics.
475 The face will be `org-done' when all relevant boxes are checked. Otherwise
476 it will be `org-todo'."
478 (if (equal (match-string 1) "100%") 'org-done
'org-todo
)
479 (if (and (> (match-end 2) (match-beginning 2))
480 (equal (match-string 2) (match-string 3)))
484 (defun org-beginning-of-item ()
485 "Go to the beginning of the current hand-formatted item.
486 If the cursor is not in an item, throw an error."
489 (limit (save-excursion
492 (org-back-to-heading)
493 (beginning-of-line 2) (point))
494 (error (point-min)))))
495 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
498 (beginning-of-line 1)
499 (beginning-of-line 1)
500 (skip-chars-forward " \t")
501 (setq ind
(current-column))
504 (beginning-of-line 0)
505 (if (or (bobp) (< (point) limit
)) (throw 'exit nil
))
507 (if (looking-at "[ \t]*$")
508 (setq ind1 ind-empty
)
509 (skip-chars-forward " \t")
510 (setq ind1
(current-column)))
512 (progn (beginning-of-line 1) (throw 'exit
(org-at-item-p))))))
515 (error "Not in an item")))))
517 (defun org-end-of-item ()
518 "Go to the end of the current hand-formatted item.
519 If the cursor is not in an item, throw an error."
523 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
524 (limit (save-excursion (outline-next-heading) (point)))
526 (org-beginning-of-item)
527 (skip-chars-forward " \t")
531 (beginning-of-line 2)
532 (if (eobp) (throw 'exit
(point)))
533 (if (>= (point) limit
) (throw 'exit
(point-at-bol)))
534 (if (looking-at "[ \t]*$")
535 (setq ind1 ind-empty
)
536 (skip-chars-forward " \t")
537 (setq ind1
(current-column)))
539 (throw 'exit
(point-at-bol)))))))
543 (error "Not in an item"))))
545 (defun org-next-item ()
546 "Move to the beginning of the next item in the current plain list.
547 Error if not at a plain list, or if this is the last item in the list."
549 (let (ind ind1
(pos (point)))
550 (org-beginning-of-item)
551 (setq ind
(org-get-indentation))
553 (setq ind1
(org-get-indentation))
554 (unless (and (org-at-item-p) (= ind ind1
))
556 (error "On last item"))))
558 (defun org-previous-item ()
559 "Move to the beginning of the previous item in the current plain list.
560 Error if not at a plain list, or if this is the first item in the list."
562 (let (beg ind ind1
(pos (point)))
563 (org-beginning-of-item)
565 (setq ind
(org-get-indentation))
569 (beginning-of-line 0)
570 (if (looking-at "[ \t]*$")
572 (if (<= (setq ind1
(org-get-indentation)) ind
)
575 (if (or (not (org-at-item-p))
578 (org-beginning-of-item))
579 (error (goto-char pos
)
580 (error "On first item")))))
582 (defun org-first-list-item-p ()
583 "Is this heading the item in a plain list?"
584 (unless (org-at-item-p)
585 (error "Not at a plain list item"))
586 (org-beginning-of-item)
587 (= (point) (save-excursion (org-beginning-of-item-list))))
589 (defun org-move-item-down ()
590 "Move the plain list item at point down, i.e. swap with following item.
591 Subitems (items with larger indentation) are considered part of the item,
592 so this really moves item trees."
594 (let ((col (current-column))
596 beg beg0 end end0 ind ind1 txt ne-end ne-beg
)
597 (org-beginning-of-item)
600 (setq ne-beg
(org-back-over-empty-lines))
603 (setq ind
(org-get-indentation))
606 (setq ind1
(org-get-indentation))
607 (setq ne-end
(org-back-over-empty-lines))
610 (when (and (org-first-list-item-p) (< ne-end ne-beg
))
611 ;; include less whitespace
614 (forward-line (- ne-beg ne-end
))
617 (if (and (org-at-item-p) (= ind ind1
))
620 (org-back-over-empty-lines)
621 (setq txt
(buffer-substring beg end
))
623 (delete-region beg end
))
626 (goto-char pos
) (org-skip-whitespace)
627 (org-maybe-renumber-ordered-list)
628 (move-to-column col
))
631 (error "Cannot move this item further down"))))
633 (defun org-move-item-up (arg)
634 "Move the plain list item at point up, i.e. swap with previous item.
635 Subitems (items with larger indentation) are considered part of the item,
636 so this really moves item trees."
638 (let ((col (current-column)) (pos (point))
639 beg beg0 end ind ind1 txt
640 ne-beg ne-ins ins-end
)
641 (org-beginning-of-item)
643 (setq ind
(org-get-indentation))
645 (setq ne-beg
(org-back-over-empty-lines))
649 (org-back-over-empty-lines)
654 (beginning-of-line 0)
655 (if (looking-at "[ \t]*$")
656 (if org-empty-line-terminates-plain-lists
659 (error "Cannot move this item further up"))
661 (if (<= (setq ind1
(org-get-indentation)) ind
)
664 (org-beginning-of-item)
665 (error (goto-char beg0
)
667 (error "Cannot move this item further up")))
668 (setq ind1
(org-get-indentation))
669 (if (and (org-at-item-p) (= ind ind1
))
671 (setq ne-ins
(org-back-over-empty-lines))
672 (setq txt
(buffer-substring beg end
))
674 (delete-region beg end
))
677 (setq ins-end
(point))
678 (goto-char pos
) (org-skip-whitespace)
680 (when (and (org-first-list-item-p) (> ne-ins ne-beg
))
681 ;; Move whitespace back to beginning
684 (let ((kill-whole-line t
))
685 (kill-line (- ne-ins ne-beg
)) (point)))
686 (insert (make-string (- ne-ins ne-beg
) ?
\n)))
688 (org-maybe-renumber-ordered-list)
689 (move-to-column col
))
692 (error "Cannot move this item further up"))))
694 (defun org-maybe-renumber-ordered-list ()
695 "Renumber the ordered list at point if setup allows it.
696 This tests the user option `org-auto-renumber-ordered-lists' before
697 doing the renumbering."
699 (when (and org-auto-renumber-ordered-lists
701 (if (match-beginning 3)
702 (org-renumber-ordered-list 1)
703 (org-fix-bullet-type))))
705 (defun org-maybe-renumber-ordered-list-safe ()
708 (org-maybe-renumber-ordered-list))
711 (defun org-cycle-list-bullet (&optional which
)
712 "Cycle through the different itemize/enumerate bullets.
713 This cycle the entire list level through the sequence:
715 `-' -> `+' -> `*' -> `1.' -> `1)'
717 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
718 0 means `-', 1 means `+' etc."
721 (org-beginning-of-item-list)
723 (beginning-of-line 1)
724 (let ((current (match-string 0))
725 (prevp (eq which
'previous
))
728 ((and (numberp which
)
729 (nth (1- which
) '("-" "+" "*" "1." "1)"))))
730 ((string-match "-" current
) (if prevp
"1)" "+"))
731 ((string-match "\\+" current
)
732 (if prevp
"-" (if (looking-at "\\S-") "1." "*")))
733 ((string-match "\\*" current
) (if prevp
"+" "1."))
734 ((string-match "\\." current
)
735 (if prevp
(if (looking-at "\\S-") "+" "*") "1)"))
736 ((string-match ")" current
) (if prevp
"1." "-"))
737 (t (error "This should not happen"))))
738 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
739 (setq old
(match-string 2))
740 (replace-match (concat "\\1" new
)))
741 (org-shift-item-indentation (- (length new
) (length old
)))
742 (org-fix-bullet-type)
743 (org-maybe-renumber-ordered-list))))
745 (defun org-get-string-indentation (s)
746 "What indentation has S due to SPACE and TAB at the beginning of the string?"
747 (let ((n -
1) (i 0) (w tab-width
) c
)
749 (while (< (setq n
(1+ n
)) (length s
))
751 (cond ((= c ?\
) (setq i
(1+ i
)))
752 ((= c ?
\t) (setq i
(* (/ (+ w i
) w
) w
)))
753 (t (throw 'exit t
)))))
756 (defun org-renumber-ordered-list (arg)
757 "Renumber an ordered plain list.
758 Cursor needs to be in the first line of an item, the line that starts
759 with something like \"1.\" or \"2)\"."
761 (unless (and (org-at-item-p)
763 (error "This is not an ordered list"))
764 (let ((line (org-current-line))
765 (col (current-column))
766 (ind (org-get-string-indentation
767 (buffer-substring (point-at-bol) (match-beginning 3))))
768 ;; (term (substring (match-string 3) -1))
771 ;; find where this list begins
772 (org-beginning-of-item-list)
774 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
775 (setq fmt
(concat "%d" (or (match-string 1) ".")))
776 (beginning-of-line 0)
777 ;; walk forward and replace these numbers
781 (if bobp
(setq bobp nil
) (beginning-of-line 2))
782 (if (eobp) (throw 'exit nil
))
783 (if (looking-at "[ \t]*$") (throw 'next nil
))
784 (skip-chars-forward " \t") (setq ind1
(current-column))
785 (if (> ind1 ind
) (throw 'next t
))
786 (if (< ind1 ind
) (throw 'exit t
))
787 (if (not (org-at-item-p)) (throw 'exit nil
))
788 (setq old
(match-string 2))
789 (delete-region (match-beginning 2) (match-end 2))
790 (goto-char (match-beginning 2))
791 (insert (setq new
(format fmt
(setq n
(1+ n
)))))
792 (org-shift-item-indentation (- (length new
) (length old
))))))
794 (org-move-to-column col
)))
796 (defun org-fix-bullet-type ()
797 "Make sure all items in this list have the same bullet as the first item.
798 Also, fix the indentation."
800 (unless (org-at-item-p) (error "This is not a list"))
801 (let ((line (org-current-line))
802 (col (current-column))
803 (ind (current-indentation))
804 ind1 bullet oldbullet
)
805 ;; find where this list begins
806 (org-beginning-of-item-list)
807 (beginning-of-line 1)
808 ;; find out what the bullet type is
809 (looking-at "[ \t]*\\(\\S-+\\)")
810 (setq bullet
(concat (match-string 1) " "))
811 (if (and org-list-two-spaces-after-bullet-regexp
812 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
813 (setq bullet
(concat bullet
" ")))
814 ;; walk forward and replace these numbers
815 (beginning-of-line 0)
819 (beginning-of-line 2)
820 (if (eobp) (throw 'exit nil
))
821 (if (looking-at "[ \t]*$") (throw 'next nil
))
822 (skip-chars-forward " \t") (setq ind1
(current-column))
823 (if (> ind1 ind
) (throw 'next t
))
824 (if (< ind1 ind
) (throw 'exit t
))
825 (if (not (org-at-item-p)) (throw 'exit nil
))
826 (skip-chars-forward " \t")
827 (looking-at "\\S-+ *")
828 (setq oldbullet
(match-string 0))
829 (unless (equal bullet oldbullet
) (replace-match bullet
))
830 (org-shift-item-indentation (- (length bullet
) (length oldbullet
))))))
832 (org-move-to-column col
)
833 (if (string-match "[0-9]" bullet
)
834 (org-renumber-ordered-list 1))))
836 (defun org-shift-item-indentation (delta)
837 "Shift the indentation in current item by DELTA."
839 (let ((beg (point-at-bol))
840 (end (progn (org-end-of-item) (point)))
843 (beginning-of-line 0)
844 (while (> (point) beg
)
845 (when (looking-at "[ \t]*\\S-")
846 ;; this is not an empty line
847 (setq i
(org-get-indentation))
848 (if (and (> i
0) (> (setq i
(+ i delta
)) 0))
850 (beginning-of-line 0)))))
852 (defun org-beginning-of-item-list ()
853 "Go to the beginning of the current item list.
854 I.e. to the first item in this list."
856 (org-beginning-of-item)
857 (let ((pos (point-at-bol))
858 (ind (org-get-indentation))
860 ;; find where this list begins
864 (beginning-of-line 0)
865 (if (looking-at "[ \t]*$")
866 (throw (if (bobp) 'exit
'next
) t
))
867 (skip-chars-forward " \t") (setq ind1
(current-column))
870 (not (org-at-item-p)))
871 (and (= (point-at-bol) (point-min))
872 (setq pos
(point-min))))
874 (when (org-at-item-p) (setq pos
(point-at-bol)))))))
878 (defun org-end-of-item-list ()
879 "Go to the end of the current item list.
880 I.e. to the text after the last item."
882 (org-beginning-of-item)
883 (let ((pos (point-at-bol))
884 (ind (org-get-indentation))
886 ;; find where this list begins
890 (beginning-of-line 2)
891 (if (looking-at "[ \t]*$")
893 (progn (setq pos
(point)) (throw 'exit t
))
895 (skip-chars-forward " \t") (setq ind1
(current-column))
898 (not (org-at-item-p)))
901 (setq pos
(point-at-bol))
906 (defvar org-last-indent-begin-marker
(make-marker))
907 (defvar org-last-indent-end-marker
(make-marker))
909 (defun org-outdent-item (arg)
910 "Outdent a local list item."
912 (org-indent-item (- arg
)))
914 (defun org-indent-item (arg)
915 "Indent a local list item."
917 (and (org-region-active-p) (org-cursor-to-region-beginning))
918 (unless (org-at-item-p)
919 (error "Not on an item"))
921 (let (beg end ind ind1 tmp delta ind-down ind-up
)
922 (setq end
(and (org-region-active-p) (region-end)))
923 (if (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
924 (setq beg org-last-indent-begin-marker
925 end org-last-indent-end-marker
)
926 (org-beginning-of-item)
927 (setq beg
(move-marker org-last-indent-begin-marker
(point)))
929 (setq end
(move-marker org-last-indent-end-marker
(or end
(point)))))
931 (setq tmp
(org-item-indent-positions)
936 (if ind-down
(- ind-down ind
) 2)
937 (if ind-up
(- ind-up ind
) -
2)))
938 (if (< (+ delta ind
) 0) (error "Cannot outdent beyond margin"))
939 (while (< (point) end
)
940 (beginning-of-line 1)
941 (skip-chars-forward " \t") (setq ind1
(current-column))
942 (delete-region (point-at-bol) (point))
943 (or (eolp) (org-indent-to-column (+ ind1 delta
)))
944 (beginning-of-line 2))))
945 (org-fix-bullet-type)
946 (org-maybe-renumber-ordered-list-safe)
948 (beginning-of-line 0)
949 (condition-case nil
(org-beginning-of-item) (error nil
))
950 (org-maybe-renumber-ordered-list-safe)))
952 (defun org-item-indent-positions ()
953 "Return indentation for plain list items.
954 This returns a list with three values: The current indentation, the
955 parent indentation and the indentation a child should have.
956 Assumes cursor in item line."
957 (let* ((bolpos (point-at-bol))
958 (ind (org-get-indentation))
961 (org-beginning-of-item-list)
962 (skip-chars-backward "\n\r \t")
963 (when (org-in-item-p)
964 (org-beginning-of-item)
965 (setq ind-up
(org-get-indentation))))
969 ((and (condition-case nil
(progn (org-previous-item) t
)
971 (or (forward-char 1) t
)
972 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t
))
973 (setq ind-down
(org-get-indentation)))
974 ((and (goto-char pos
)
976 (goto-char (match-end 0))
977 (skip-chars-forward " \t")
978 (setq ind-down
(current-column)))))
979 (list ind ind-up ind-down
)))
982 ;;; Send and receive lists
984 (defun org-list-parse-list (&optional delete
)
985 "Parse the list at point and maybe DELETE it.
986 Return a list containing first level items as strings and
987 sublevels as a list of strings."
988 (let* ((item-beginning (org-list-item-beginning))
989 (start (car item-beginning
))
990 (end (org-list-end (cdr item-beginning
)))
991 output itemsep ltype
)
992 (while (re-search-forward org-list-beginning-re end t
)
993 (goto-char (match-beginning 3))
995 (cond ((string-match "[0-9]" (match-string 2))
996 (setq itemsep
"[0-9]+\\(?:\\.\\|)\\)"
998 ((string-match "^.*::" (match-string 0))
999 (setq itemsep
"[-+]" ltype
'descriptive
))
1000 (t (setq itemsep
"[-+]" ltype
'unordered
))))
1001 (let* ((indent1 (match-string 1))
1002 (nextitem (save-excursion
1004 (or (and (re-search-forward
1005 (concat "^" indent1 itemsep
" *?") end t
)
1006 (match-beginning 0)) end
))))
1007 (item (buffer-substring
1009 (or (and (re-search-forward
1010 org-list-beginning-re end t
)
1011 (goto-char (match-beginning 0)))
1013 (nextindent (match-string 1))
1014 (item (org-trim item
))
1015 (item (if (string-match "^\\[\\([xX ]\\)\\]" item
)
1016 (replace-match (if (equal (match-string 1 item
) " ")
1022 (when (> (length nextindent
)
1024 (narrow-to-region (point) nextitem
)
1025 (push (org-list-parse-list) output
)
1027 (when delete
(delete-region start end
))
1028 (setq output
(nreverse output
))
1029 (push ltype output
)))
1031 (defun org-list-item-beginning ()
1032 "Find the beginning of the list item.
1033 Return a cons which car is the beginning position of the item and
1034 cdr is the indentation string."
1036 (if (not (or (looking-at org-list-beginning-re
)
1038 org-list-beginning-re nil t
)))
1039 (progn (goto-char (point-min)) (point))
1040 (cons (match-beginning 0) (match-string 1)))))
1042 (defun org-list-end (indent)
1043 "Return the position of the end of the list.
1044 INDENT is the indentation of the list."
1047 (while (or (looking-at org-list-beginning-re
)
1048 (looking-at (concat "^" indent
"[ \t]+\\|^$")))
1049 (if (eq (point) (point-max))
1050 (throw 'exit
(point-max)))
1051 (forward-line 1))) (point)))
1053 (defun org-list-insert-radio-list ()
1054 "Insert a radio list template appropriate for this major mode."
1056 (let* ((e (assq major-mode org-list-radio-list-templates
))
1059 (unless e
(error "No radio list setup defined for %s" major-mode
))
1060 (setq name
(read-string "List name: "))
1061 (while (string-match "%n" txt
)
1062 (setq txt
(replace-match name t t txt
)))
1063 (or (bolp) (insert "\n"))
1068 (defun org-list-send-list (&optional maybe
)
1069 "Send a tranformed version of this list to the receiver position.
1070 With argument MAYBE, fail quietly if no transformation is defined for
1074 (unless (org-at-item-p) (error "Not at a list"))
1076 (goto-char (car (org-list-item-beginning)))
1077 (beginning-of-line 0)
1078 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1081 (error "Don't know how to transform this list"))))
1082 (let* ((name (match-string 1))
1083 (item-beginning (org-list-item-beginning))
1084 (transform (intern (match-string 2)))
1085 (txt (buffer-substring-no-properties
1086 (car item-beginning
)
1087 (org-list-end (cdr item-beginning
))))
1088 (list (org-list-parse-list))
1090 (unless (fboundp transform
)
1091 (error "No such transformation function %s" transform
))
1092 (setq txt
(funcall transform list
))
1093 ;; Find the insertion place
1095 (goto-char (point-min))
1096 (unless (re-search-forward
1097 (concat "BEGIN RECEIVE ORGLST +" name
"\\([ \t]\\|$\\)") nil t
)
1098 (error "Don't know where to insert translated list"))
1099 (goto-char (match-beginning 0))
1100 (beginning-of-line 2)
1102 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
1103 (error "Cannot find end of insertion region"))
1104 (beginning-of-line 1)
1105 (delete-region beg
(point))
1108 (message "List converted and installed at receiver location"))))
1110 (defun org-list-to-generic (list params
)
1111 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1113 Valid parameters PARAMS are
1115 :ustart String to start an unordered list
1116 :uend String to end an unordered list
1118 :ostart String to start an ordered list
1119 :oend String to end an ordered list
1121 :dstart String to start a descriptive list
1122 :dend String to end a descriptive list
1123 :dtstart String to start a descriptive term
1124 :dtend String to end a descriptive term
1125 :ddstart String to start a description
1126 :ddend String to end a description
1128 :splice When set to t, return only list body lines, don't wrap
1129 them into :[u/o]start and :[u/o]end. Default is nil.
1131 :istart String to start a list item
1132 :iend String to end a list item
1133 :isep String to separate items
1134 :lsep String to separate sublists
1136 :cboff String to insert for an unchecked checkbox
1137 :cbon String to insert for a checked checkbox"
1139 (let* ((p params
) sublist
1140 (splicep (plist-get p
:splice
))
1141 (ostart (plist-get p
:ostart
))
1142 (oend (plist-get p
:oend
))
1143 (ustart (plist-get p
:ustart
))
1144 (uend (plist-get p
:uend
))
1145 (dstart (plist-get p
:dstart
))
1146 (dend (plist-get p
:dend
))
1147 (dtstart (plist-get p
:dtstart
))
1148 (dtend (plist-get p
:dtend
))
1149 (ddstart (plist-get p
:ddstart
))
1150 (ddend (plist-get p
:ddend
))
1151 (istart (plist-get p
:istart
))
1152 (iend (plist-get p
:iend
))
1153 (isep (plist-get p
:isep
))
1154 (lsep (plist-get p
:lsep
))
1155 (cbon (plist-get p
:cbon
))
1156 (cboff (plist-get p
:cboff
)))
1158 (cond ((eq (car list
) 'ordered
)
1159 (concat ostart
"\n%s" oend
"\n"))
1160 ((eq (car list
) 'unordered
)
1161 (concat ustart
"\n%s" uend
"\n"))
1162 ((eq (car list
) 'descriptive
)
1163 (concat dstart
"\n%s" dend
"\n"))))
1164 rtn term defstart defend
)
1165 (while (setq sublist
(pop list
))
1166 (cond ((symbolp sublist
) nil
)
1168 (when (string-match "^\\(.*\\) ::" sublist
)
1169 (setq term
(org-trim (format (concat dtstart
"%s" dtend
)
1170 (match-string 1 sublist
))))
1171 (setq sublist
(substring sublist
(1+ (length term
)))))
1172 (if (string-match "\\[CBON\\]" sublist
)
1173 (setq sublist
(replace-match cbon t t sublist
)))
1174 (if (string-match "\\[CBOFF\\]" sublist
)
1175 (setq sublist
(replace-match cboff t t sublist
)))
1176 (setq rtn
(concat rtn istart term ddstart
1177 sublist ddend iend isep
)))
1178 (t (setq rtn
(concat rtn
;; previous list
1179 lsep
;; list separator
1180 (org-list-to-generic sublist p
)
1181 lsep
;; list separator
1183 (format wrapper rtn
))))
1185 (defun org-list-to-latex (list &optional params
)
1186 "Convert LIST into a LaTeX list.
1187 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1188 with overruling parameters for `org-list-to-generic'."
1189 (org-list-to-generic
1192 '(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
1193 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
1194 :dstart
"\\begin{description}" :dend
"\\end{description}"
1195 :dtstart
"[" :dtend
"]"
1196 :ddstart
"" :ddend
""
1197 :istart
"\\item " :iend
""
1198 :isep
"\n" :lsep
"\n"
1199 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
1202 (defun org-list-to-html (list &optional params
)
1203 "Convert LIST into a HTML list.
1204 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1205 with overruling parameters for `org-list-to-generic'."
1206 (org-list-to-generic
1209 '(:splicep nil
:ostart
"<ol>" :oend
"</ol>"
1210 :ustart
"<ul>" :uend
"</ul>"
1211 :dstart
"<dl>" :dend
"</dl>"
1212 :dtstart
"<dt>" :dtend
"</dt>"
1213 :ddstart
"<dd>" :ddend
"</dd>"
1214 :istart
"<li>" :iend
"</li>"
1215 :isep
"\n" :lsep
"\n"
1216 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>")
1219 (defun org-list-to-texinfo (list &optional params
)
1220 "Convert LIST into a Texinfo list.
1221 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1222 with overruling parameters for `org-list-to-generic'."
1223 (org-list-to-generic
1226 '(:splicep nil
:ostart
"@itemize @minus" :oend
"@end itemize"
1227 :ustart
"@enumerate" :uend
"@end enumerate"
1228 :dstart
"@table" :dend
"@end table"
1229 :dtstart
"@item " :dtend
"\n"
1230 :ddstart
"" :ddend
""
1231 :istart
"@item\n" :iend
""
1232 :isep
"\n" :lsep
"\n"
1233 :cbon
"@code{[X]}" :cboff
"@code{[ ]}")
1238 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1239 ;;; org-list.el ends here