1 ;;; org-list.el --- Plain lists for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Bastien Guerry <bzg AT altern DOT org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; This file contains the code dealing with plain lists in Org-mode.
36 (defvar org-blank-before-new-entry)
37 (defvar org-M-RET-may-split-line)
39 (declare-function org-invisible-p "org" ())
40 (declare-function org-on-heading-p "org" (&optional invisible-ok))
41 (declare-function outline-next-heading "outline" ())
42 (declare-function outline-back-to-heading "outline" (&optional invisible-ok))
43 (declare-function org-back-to-heading "org" (&optional invisible-ok))
44 (declare-function org-back-over-empty-lines "org" ())
45 (declare-function org-skip-whitespace "org" ())
46 (declare-function org-trim "org" (s))
47 (declare-function org-get-indentation "org" (&optional line))
49 (defgroup org-plain-lists nil
50 "Options concerning plain lists in Org-mode."
51 :tag "Org Plain lists"
52 :group 'org-structure)
54 (defcustom org-cycle-include-plain-lists nil
55 "Non-nil means, include plain lists into visibility cycling.
56 This means that during cycling, plain list items will *temporarily* be
57 interpreted as outline headlines with a level given by 1000+i where i is the
58 indentation of the bullet. In all other operations, plain list items are
59 not seen as headlines. For example, you cannot assign a TODO keyword to
61 :group 'org-plain-lists
64 (defcustom org-plain-list-ordered-item-terminator t
65 "The character that makes a line with leading number an ordered list item.
66 Valid values are ?. and ?\). To get both terminators, use t. While
67 ?. may look nicer, it creates the danger that a line with leading
68 number may be incorrectly interpreted as an item. ?\) therefore is
70 :group 'org-plain-lists
71 :type '(choice (const :tag "dot like in \"2.\"" ?.)
72 (const :tag "paren like in \"2)\"" ?\))
73 (const :tab "both" t)))
75 (defcustom org-list-two-spaces-after-bullet-regexp nil
76 "A regular expression matching bullets that should have 2 spaces after them.
77 When nil, no bullet will have two spaces after them.
78 When a string, it will be used as a regular expression. When the bullet
79 type of a list is changed, the new bullet type will be matched against this
80 regexp. If it matches, there will be two spaces instead of one after
81 the bullet in each item of he list."
82 :group 'org-plain-list
84 (const :tag "never" nil)
87 (defcustom org-empty-line-terminates-plain-lists nil
88 "Non-nil means, an empty line ends all plain list levels.
89 When nil, empty lines are part of the preceeding item."
90 :group 'org-plain-lists
93 (defcustom org-auto-renumber-ordered-lists t
94 "Non-nil means, automatically renumber ordered plain lists.
95 Renumbering happens when the sequence have been changed with
96 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
97 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
98 :group 'org-plain-lists
101 (defcustom org-provide-checkbox-statistics t
102 "Non-nil means, update checkbox statistics after insert and toggle.
103 When this is set, checkbox statistics is updated each time you either insert
104 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
105 with \\[org-ctrl-c-ctrl-c\\]."
106 :group 'org-plain-lists
109 (defcustom org-description-max-indent 20
110 "Maximum indentation for the second line of a description list.
111 When the indentation would be larger than this, it will become
112 5 characters instead."
113 :group 'org-plain-lists
116 (defvar org-list-beginning-re
117 "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +\\(.*\\)$")
119 (defcustom org-list-radio-list-templates
120 '((latex-mode "% BEGIN RECEIVE ORGLST %n
121 % END RECEIVE ORGLST %n
123 #+ORGLST: SEND %n org-list-to-latex
126 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
127 @c END RECEIVE ORGLST %n
129 #+ORGLST: SEND %n org-list-to-texinfo
132 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
133 <!-- END RECEIVE ORGLST %n -->
135 #+ORGLST: SEND %n org-list-to-html
138 "Templates for radio lists in different major modes.
139 All occurrences of %n in a template will be replaced with the name of the
140 list, obtained by prompting the user."
141 :group 'org-plain-lists
143 (list (symbol :tag "Major mode")
144 (string :tag "Format"))))
146 ;;;; Plain list items, including checkboxes
150 (defun org-at-item-p ()
151 "Is point in a line starting a hand-formatted item?"
152 (let ((llt org-plain-list-ordered-item-terminator))
154 (goto-char (point-at-bol))
157 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
158 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
159 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
160 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
162 (defun org-in-item-p ()
163 "It the cursor inside a plain list item.
164 Does not have to be the first line."
168 (org-beginning-of-item)
173 (defun org-insert-item (&optional checkbox)
174 "Insert a new item at the current level.
175 Return t when things worked, nil when we are not in an item."
176 (when (save-excursion
179 (org-beginning-of-item)
181 (if (org-invisible-p) (error "Invisible item"))
184 (let* ((bul (match-string 0))
185 (descp (save-excursion (goto-char (match-beginning 0))
186 (beginning-of-line 1)
188 (looking-at "[ \t]*.*? ::"))))
189 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
191 (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
193 (if descp (setq checkbox nil))
195 ((and (org-at-item-p) (<= (point) eow))
197 (beginning-of-line 1)
198 (open-line (if blank 2 1)))
200 (beginning-of-line 1))
202 (unless (org-get-alist-option org-M-RET-may-split-line 'item)
204 (delete-horizontal-space))
205 (newline (if blank 2 1))))
207 (if checkbox "[ ]" "")
208 (if descp (concat (if checkbox " " "")
209 (read-string "Term: ") " :: ") ""))
213 (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
214 (org-maybe-renumber-ordered-list)
215 (and checkbox (org-update-checkbox-count-maybe))
220 (defun org-at-item-checkbox-p ()
221 "Is point at a line starting a plain-list item with a checklet?"
224 (goto-char (match-end 0))
225 (skip-chars-forward " \t")
226 (looking-at "\\[[- X]\\]"))))
228 (defun org-toggle-checkbox (&optional arg)
229 "Toggle the checkbox in the current line."
232 (let (beg end status (firstnew 'unknown))
234 ((org-region-active-p)
235 (setq beg (region-beginning) end (region-end)))
237 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
238 ((org-at-item-checkbox-p)
242 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
247 (t (error "Not at a checkbox or heading, and no active region")))
250 (while (< (point) end)
251 (when (org-at-item-checkbox-p)
252 (setq status (equal (match-string 0) "[X]"))
253 (when (eq firstnew 'unknown)
254 (setq firstnew (not status)))
256 (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
257 (beginning-of-line 2)))))
258 (org-update-checkbox-count-maybe))
260 (defun org-update-checkbox-count-maybe ()
261 "Update checkbox statistics unless turned off by user."
262 (when org-provide-checkbox-statistics
263 (org-update-checkbox-count)))
265 (defun org-update-checkbox-count (&optional all)
266 "Update the checkbox statistics in the current section.
267 This will find all statistic cookies like [57%] and [6/12] and update them
268 with the current numbers. With optional prefix argument ALL, do this for
272 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
273 (beg (condition-case nil
274 (progn (org-back-to-heading) (point))
275 (error (point-min))))
276 (end (move-marker (make-marker)
277 (progn (outline-next-heading) (point))))
278 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
279 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
280 (re-find (concat re "\\|" re-box))
281 beg-cookie end-cookie is-percent c-on c-off lim
282 eline curr-ind next-ind continue-from startsearch
286 (goto-char (point-min))
287 (outline-next-heading)
288 (setq beg (point) end (point-max)))
290 ;; find each statistic cookie
291 (while (re-search-backward re-find beg t)
292 (setq beg-cookie (match-beginning 1)
293 end-cookie (match-end 1)
294 cstat (+ cstat (if end-cookie 1 0))
295 startsearch (point-at-eol)
296 continue-from (point-at-bol)
297 is-percent (match-beginning 2)
299 ((org-on-heading-p) (outline-next-heading) (point))
300 ((org-at-item-p) (org-end-of-item) (point))
305 ;; find first checkbox for this cookie and gather
306 ;; statistics from all that are at this indentation level
307 (goto-char startsearch)
308 (if (re-search-forward re-box lim t)
310 (org-beginning-of-item)
311 (setq curr-ind (org-get-indentation))
312 (setq next-ind curr-ind)
313 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
314 (save-excursion (end-of-line) (setq eline (point)))
315 (if (re-search-forward re-box eline t)
316 (if (member (match-string 2) '("[ ]" "[-]"))
317 (setq c-off (1+ c-off))
318 (setq c-on (1+ c-on))
322 (setq next-ind (org-get-indentation))
324 (goto-char continue-from)
327 (delete-region beg-cookie end-cookie)
328 (goto-char beg-cookie)
331 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
332 (format "[%d/%d]" c-on (+ c-on c-off)))))
333 ;; update items checkbox if it has one
334 (when (org-at-item-p)
335 (org-beginning-of-item)
336 (when (and (> (+ c-on c-off) 0)
337 (re-search-forward re-box (point-at-eol) t))
338 (setq beg-cookie (match-beginning 2)
339 end-cookie (match-end 2))
340 (delete-region beg-cookie end-cookie)
341 (goto-char beg-cookie)
342 (cond ((= c-off 0) (insert "[X]"))
343 ((= c-on 0) (insert "[ ]"))
346 (goto-char continue-from))
347 (when (interactive-p)
348 (message "Checkbox satistics updated %s (%d places)"
349 (if all "in entire file" "in current outline entry") cstat)))))
351 (defun org-get-checkbox-statistics-face ()
352 "Select the face for checkbox statistics.
353 The face will be `org-done' when all relevant boxes are checked. Otherwise
354 it will be `org-todo'."
356 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
357 (if (and (> (match-end 2) (match-beginning 2))
358 (equal (match-string 2) (match-string 3)))
362 (defun org-beginning-of-item ()
363 "Go to the beginning of the current hand-formatted item.
364 If the cursor is not in an item, throw an error."
367 (limit (save-excursion
370 (org-back-to-heading)
371 (beginning-of-line 2) (point))
372 (error (point-min)))))
373 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
376 (beginning-of-line 1)
377 (beginning-of-line 1)
378 (skip-chars-forward " \t")
379 (setq ind (current-column))
382 (beginning-of-line 0)
383 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
385 (if (looking-at "[ \t]*$")
386 (setq ind1 ind-empty)
387 (skip-chars-forward " \t")
388 (setq ind1 (current-column)))
390 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
393 (error "Not in an item")))))
395 (defun org-end-of-item ()
396 "Go to the end of the current hand-formatted item.
397 If the cursor is not in an item, throw an error."
401 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
402 (limit (save-excursion (outline-next-heading) (point)))
404 (org-beginning-of-item)
405 (skip-chars-forward " \t")
409 (beginning-of-line 2)
410 (if (eobp) (throw 'exit (point)))
411 (if (>= (point) limit) (throw 'exit (point-at-bol)))
412 (if (looking-at "[ \t]*$")
413 (setq ind1 ind-empty)
414 (skip-chars-forward " \t")
415 (setq ind1 (current-column)))
417 (throw 'exit (point-at-bol)))))))
421 (error "Not in an item"))))
423 (defun org-next-item ()
424 "Move to the beginning of the next item in the current plain list.
425 Error if not at a plain list, or if this is the last item in the list."
427 (let (ind ind1 (pos (point)))
428 (org-beginning-of-item)
429 (setq ind (org-get-indentation))
431 (setq ind1 (org-get-indentation))
432 (unless (and (org-at-item-p) (= ind ind1))
434 (error "On last item"))))
436 (defun org-previous-item ()
437 "Move to the beginning of the previous item in the current plain list.
438 Error if not at a plain list, or if this is the first item in the list."
440 (let (beg ind ind1 (pos (point)))
441 (org-beginning-of-item)
443 (setq ind (org-get-indentation))
447 (beginning-of-line 0)
448 (if (looking-at "[ \t]*$")
450 (if (<= (setq ind1 (org-get-indentation)) ind)
453 (if (or (not (org-at-item-p))
456 (org-beginning-of-item))
457 (error (goto-char pos)
458 (error "On first item")))))
460 (defun org-first-list-item-p ()
461 "Is this heading the item in a plain list?"
462 (unless (org-at-item-p)
463 (error "Not at a plain list item"))
464 (org-beginning-of-item)
465 (= (point) (save-excursion (org-beginning-of-item-list))))
467 (defun org-move-item-down ()
468 "Move the plain list item at point down, i.e. swap with following item.
469 Subitems (items with larger indentation) are considered part of the item,
470 so this really moves item trees."
472 (let ((col (current-column))
474 beg beg0 end end0 ind ind1 txt ne-end ne-beg)
475 (org-beginning-of-item)
478 (setq ne-beg (org-back-over-empty-lines))
481 (setq ind (org-get-indentation))
484 (setq ind1 (org-get-indentation))
485 (setq ne-end (org-back-over-empty-lines))
488 (when (and (org-first-list-item-p) (< ne-end ne-beg))
489 ;; include less whitespace
492 (forward-line (- ne-beg ne-end))
495 (if (and (org-at-item-p) (= ind ind1))
498 (org-back-over-empty-lines)
499 (setq txt (buffer-substring beg end))
501 (delete-region beg end))
504 (goto-char pos) (org-skip-whitespace)
505 (org-maybe-renumber-ordered-list)
506 (move-to-column col))
509 (error "Cannot move this item further down"))))
511 (defun org-move-item-up (arg)
512 "Move the plain list item at point up, i.e. swap with previous item.
513 Subitems (items with larger indentation) are considered part of the item,
514 so this really moves item trees."
516 (let ((col (current-column)) (pos (point))
517 beg beg0 end ind ind1 txt
518 ne-beg ne-ins ins-end)
519 (org-beginning-of-item)
521 (setq ind (org-get-indentation))
523 (setq ne-beg (org-back-over-empty-lines))
527 (org-back-over-empty-lines)
532 (beginning-of-line 0)
533 (if (looking-at "[ \t]*$")
534 (if org-empty-line-terminates-plain-lists
537 (error "Cannot move this item further up"))
539 (if (<= (setq ind1 (org-get-indentation)) ind)
542 (org-beginning-of-item)
543 (error (goto-char beg0)
545 (error "Cannot move this item further up")))
546 (setq ind1 (org-get-indentation))
547 (if (and (org-at-item-p) (= ind ind1))
549 (setq ne-ins (org-back-over-empty-lines))
550 (setq txt (buffer-substring beg end))
552 (delete-region beg end))
555 (setq ins-end (point))
556 (goto-char pos) (org-skip-whitespace)
558 (when (and (org-first-list-item-p) (> ne-ins ne-beg))
559 ;; Move whitespace back to beginning
562 (let ((kill-whole-line t))
563 (kill-line (- ne-ins ne-beg)) (point)))
564 (insert (make-string (- ne-ins ne-beg) ?\n)))
566 (org-maybe-renumber-ordered-list)
567 (move-to-column col))
570 (error "Cannot move this item further up"))))
572 (defun org-maybe-renumber-ordered-list ()
573 "Renumber the ordered list at point if setup allows it.
574 This tests the user option `org-auto-renumber-ordered-lists' before
575 doing the renumbering."
577 (when (and org-auto-renumber-ordered-lists
579 (if (match-beginning 3)
580 (org-renumber-ordered-list 1)
581 (org-fix-bullet-type))))
583 (defun org-maybe-renumber-ordered-list-safe ()
586 (org-maybe-renumber-ordered-list))
589 (defun org-cycle-list-bullet (&optional which)
590 "Cycle through the different itemize/enumerate bullets.
591 This cycle the entire list level through the sequence:
593 `-' -> `+' -> `*' -> `1.' -> `1)'
595 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
596 0 meand `-', 1 means `+' etc."
599 (org-beginning-of-item-list)
601 (beginning-of-line 1)
602 (let ((current (match-string 0))
603 (prevp (eq which 'previous))
606 ((and (numberp which)
607 (nth (1- which) '("-" "+" "*" "1." "1)"))))
608 ((string-match "-" current) (if prevp "1)" "+"))
609 ((string-match "\\+" current)
610 (if prevp "-" (if (looking-at "\\S-") "1." "*")))
611 ((string-match "\\*" current) (if prevp "+" "1."))
612 ((string-match "\\." current)
613 (if prevp (if (looking-at "\\S-") "+" "*") "1)"))
614 ((string-match ")" current) (if prevp "1." "-"))
615 (t (error "This should not happen"))))
616 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
617 (setq old (match-string 2))
618 (replace-match (concat "\\1" new)))
619 (org-shift-item-indentation (- (length new) (length old)))
620 (org-fix-bullet-type)
621 (org-maybe-renumber-ordered-list))))
623 (defun org-get-string-indentation (s)
624 "What indentation has S due to SPACE and TAB at the beginning of the string?"
625 (let ((n -1) (i 0) (w tab-width) c)
627 (while (< (setq n (1+ n)) (length s))
629 (cond ((= c ?\ ) (setq i (1+ i)))
630 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
631 (t (throw 'exit t)))))
634 (defun org-renumber-ordered-list (arg)
635 "Renumber an ordered plain list.
636 Cursor needs to be in the first line of an item, the line that starts
637 with something like \"1.\" or \"2)\"."
639 (unless (and (org-at-item-p)
641 (error "This is not an ordered list"))
642 (let ((line (org-current-line))
643 (col (current-column))
644 (ind (org-get-string-indentation
645 (buffer-substring (point-at-bol) (match-beginning 3))))
646 ;; (term (substring (match-string 3) -1))
649 ;; find where this list begins
650 (org-beginning-of-item-list)
652 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
653 (setq fmt (concat "%d" (match-string 1)))
654 (beginning-of-line 0)
655 ;; walk forward and replace these numbers
659 (if bobp (setq bobp nil) (beginning-of-line 2))
660 (if (eobp) (throw 'exit nil))
661 (if (looking-at "[ \t]*$") (throw 'next nil))
662 (skip-chars-forward " \t") (setq ind1 (current-column))
663 (if (> ind1 ind) (throw 'next t))
664 (if (< ind1 ind) (throw 'exit t))
665 (if (not (org-at-item-p)) (throw 'exit nil))
666 (setq old (match-string 2))
667 (delete-region (match-beginning 2) (match-end 2))
668 (goto-char (match-beginning 2))
669 (insert (setq new (format fmt (setq n (1+ n)))))
670 (org-shift-item-indentation (- (length new) (length old))))))
672 (org-move-to-column col)))
674 (defun org-fix-bullet-type ()
675 "Make sure all items in this list have the same bullet as the first item.
676 Also, fix the indentation."
678 (unless (org-at-item-p) (error "This is not a list"))
679 (let ((line (org-current-line))
680 (col (current-column))
681 (ind (current-indentation))
682 ind1 bullet oldbullet)
683 ;; find where this list begins
684 (org-beginning-of-item-list)
685 (beginning-of-line 1)
686 ;; find out what the bullet type is
687 (looking-at "[ \t]*\\(\\S-+\\)")
688 (setq bullet (concat (match-string 1) " "))
689 (if (and org-list-two-spaces-after-bullet-regexp
690 (string-match org-list-two-spaces-after-bullet-regexp bullet))
691 (setq bullet (concat bullet " ")))
692 ;; walk forward and replace these numbers
693 (beginning-of-line 0)
697 (beginning-of-line 2)
698 (if (eobp) (throw 'exit nil))
699 (if (looking-at "[ \t]*$") (throw 'next nil))
700 (skip-chars-forward " \t") (setq ind1 (current-column))
701 (if (> ind1 ind) (throw 'next t))
702 (if (< ind1 ind) (throw 'exit t))
703 (if (not (org-at-item-p)) (throw 'exit nil))
704 (skip-chars-forward " \t")
705 (looking-at "\\S-+ *")
706 (setq oldbullet (match-string 0))
707 (replace-match bullet)
708 (org-shift-item-indentation (- (length bullet) (length oldbullet))))))
710 (org-move-to-column col)
711 (if (string-match "[0-9]" bullet)
712 (org-renumber-ordered-list 1))))
714 (defun org-shift-item-indentation (delta)
715 "Shift the indentation in current item by DELTA."
717 (let ((beg (point-at-bol))
718 (end (progn (org-end-of-item) (point)))
721 (beginning-of-line 0)
722 (while (> (point) beg)
723 (when (looking-at "[ \t]*\\S-")
724 ;; this is not an empty line
725 (setq i (org-get-indentation))
726 (if (and (> i 0) (> (setq i (+ i delta)) 0))
728 (beginning-of-line 0)))))
730 (defun org-beginning-of-item-list ()
731 "Go to the beginning of the current item list.
732 I.e. to the first item in this list."
734 (org-beginning-of-item)
735 (let ((pos (point-at-bol))
736 (ind (org-get-indentation))
738 ;; find where this list begins
742 (beginning-of-line 0)
743 (if (looking-at "[ \t]*$")
744 (throw (if (bobp) 'exit 'next) t))
745 (skip-chars-forward " \t") (setq ind1 (current-column))
748 (not (org-at-item-p)))
749 (and (= (point-at-bol) (point-min))
750 (setq pos (point-min))))
752 (when (org-at-item-p) (setq pos (point-at-bol)))))))
756 (defun org-end-of-item-list ()
757 "Go to the end of the current item list.
758 I.e. to the text after the last item."
760 (org-beginning-of-item)
761 (let ((pos (point-at-bol))
762 (ind (org-get-indentation))
764 ;; find where this list begins
768 (beginning-of-line 2)
769 (if (looking-at "[ \t]*$")
770 (throw (if (eobp) 'exit 'next) t))
771 (skip-chars-forward " \t") (setq ind1 (current-column))
774 (not (org-at-item-p)))
777 (setq pos (point-at-bol))
782 (defvar org-last-indent-begin-marker (make-marker))
783 (defvar org-last-indent-end-marker (make-marker))
785 (defun org-outdent-item (arg)
786 "Outdent a local list item."
788 (org-indent-item (- arg)))
790 (defun org-indent-item (arg)
791 "Indent a local list item."
793 (unless (org-at-item-p)
794 (error "Not on an item"))
796 (let (beg end ind ind1 tmp delta ind-down ind-up)
797 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
798 (setq beg org-last-indent-begin-marker
799 end org-last-indent-end-marker)
800 (org-beginning-of-item)
801 (setq beg (move-marker org-last-indent-begin-marker (point)))
803 (setq end (move-marker org-last-indent-end-marker (point))))
805 (setq tmp (org-item-indent-positions)
810 (if ind-down (- ind-down ind) 2)
811 (if ind-up (- ind-up ind) -2)))
812 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
813 (while (< (point) end)
814 (beginning-of-line 1)
815 (skip-chars-forward " \t") (setq ind1 (current-column))
816 (delete-region (point-at-bol) (point))
817 (or (eolp) (org-indent-to-column (+ ind1 delta)))
818 (beginning-of-line 2))))
819 (org-fix-bullet-type)
820 (org-maybe-renumber-ordered-list-safe)
822 (beginning-of-line 0)
823 (condition-case nil (org-beginning-of-item) (error nil))
824 (org-maybe-renumber-ordered-list-safe)))
826 (defun org-item-indent-positions ()
827 "Return indentation for plain list items.
828 This returns a list with three values: The current indentation, the
829 parent indentation and the indentation a child should habe.
830 Assumes cursor in item line."
831 (let* ((bolpos (point-at-bol))
832 (ind (org-get-indentation))
835 (org-beginning-of-item-list)
836 (skip-chars-backward "\n\r \t")
837 (when (org-in-item-p)
838 (org-beginning-of-item)
839 (setq ind-up (org-get-indentation))))
843 ((and (condition-case nil (progn (org-previous-item) t)
845 (or (forward-char 1) t)
846 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
847 (setq ind-down (org-get-indentation)))
848 ((and (goto-char pos)
850 (goto-char (match-end 0))
851 (skip-chars-forward " \t")
852 (setq ind-down (current-column)))))
853 (list ind ind-up ind-down)))
856 ;;; Send and receive lists
858 (defun org-list-parse-list (&optional delete)
859 "Parse the list at point and maybe DELETE it.
860 Return a list containing first level items as strings and
861 sublevels as a list of strings."
862 (let* ((item-beginning (org-list-item-beginning))
863 (start (car item-beginning))
864 (end (org-list-end (cdr item-beginning)))
865 output itemsep ltype)
866 (while (re-search-forward org-list-beginning-re end t)
867 (goto-char (match-beginning 3))
869 (cond ((string-match "[0-9]" (match-string 2))
870 (setq itemsep "[0-9]+\\(?:\\.\\|)\\)"
872 ((string-match "^.*::" (match-string 0))
873 (setq itemsep "[-+]" ltype 'descriptive))
874 (t (setq itemsep "[-+]" ltype 'unordered))))
875 (let* ((indent1 (match-string 1))
876 (nextitem (save-excursion
878 (or (and (re-search-forward
879 (concat "^" indent1 itemsep " *?") end t)
880 (match-beginning 0)) end))))
881 (item (buffer-substring
883 (or (and (re-search-forward
884 org-list-beginning-re end t)
885 (goto-char (match-beginning 0)))
887 (nextindent (match-string 1))
888 (item (org-trim item))
889 (item (if (string-match "^\\[.+\\]" item)
890 (replace-match "\\\\texttt{\\&}"
893 (when (> (length nextindent)
895 (narrow-to-region (point) nextitem)
896 (push (org-list-parse-list) output)
898 (when delete (delete-region start end))
899 (setq output (nreverse output))
900 (push ltype output)))
902 (defun org-list-item-beginning ()
903 "Find the beginning of the list item.
904 Return a cons which car is the beginning position of the item and
905 cdr is the indentation string."
907 (if (not (or (looking-at org-list-beginning-re)
909 org-list-beginning-re nil t)))
910 (progn (goto-char (point-min)) (point))
911 (cons (match-beginning 0) (match-string 1)))))
913 (defun org-list-end (indent)
914 "Return the position of the end of the list.
915 INDENT is the indentation of the list."
918 (while (or (looking-at org-list-beginning-re)
919 (looking-at (concat "^" indent "[ \t]+\\|^$")))
920 (if (eq (point) (point-max))
921 (throw 'exit (point-max)))
922 (forward-line 1))) (point)))
924 (defun org-list-insert-radio-list ()
925 "Insert a radio list template appropriate for this major mode."
927 (let* ((e (assq major-mode org-list-radio-list-templates))
930 (unless e (error "No radio list setup defined for %s" major-mode))
931 (setq name (read-string "List name: "))
932 (while (string-match "%n" txt)
933 (setq txt (replace-match name t t txt)))
934 (or (bolp) (insert "\n"))
939 (defun org-list-send-list (&optional maybe)
940 "Send a tranformed version of this list to the receiver position.
941 With argument MAYBE, fail quietly if no transformation is defined for
945 (unless (org-at-item-p) (error "Not at a list"))
947 (goto-char (car (org-list-item-beginning)))
948 (beginning-of-line 0)
949 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
952 (error "Don't know how to transform this list"))))
953 (let* ((name (match-string 1))
954 (item-beginning (org-list-item-beginning))
955 (transform (intern (match-string 2)))
956 (txt (buffer-substring-no-properties
958 (org-list-end (cdr item-beginning))))
959 (list (org-list-parse-list))
961 (unless (fboundp transform)
962 (error "No such transformation function %s" transform))
963 (setq txt (funcall transform list))
964 ;; Find the insertion place
966 (goto-char (point-min))
967 (unless (re-search-forward
968 (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
969 (error "Don't know where to insert translated list"))
970 (goto-char (match-beginning 0))
971 (beginning-of-line 2)
973 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
974 (error "Cannot find end of insertion region"))
975 (beginning-of-line 1)
976 (delete-region beg (point))
979 (message "List converted and installed at receiver location"))))
981 (defun org-list-to-generic (list params)
982 "Convert a LIST parsed through `org-list-parse-list' to other formats.
984 Valid parameters PARAMS are
986 :ustart String to start an unordered list
987 :uend String to end an unordered list
989 :ostart String to start an ordered list
990 :oend String to end an ordered list
992 :dstart String to start a descriptive list
993 :dend String to end a descriptive list
994 :dtstart String to start a descriptive term
995 :dtend String to end a descriptive term
996 :ddstart String to start a description
997 :ddend String to end a description
999 :splice When set to t, return only list body lines, don't wrap
1000 them into :[u/o]start and :[u/o]end. Default is nil.
1002 :istart String to start a list item
1003 :iend String to end a list item
1004 :isep String to separate items
1005 :lsep String to separate sublists"
1007 (let* ((p params) sublist
1008 (splicep (plist-get p :splice))
1009 (ostart (plist-get p :ostart))
1010 (oend (plist-get p :oend))
1011 (ustart (plist-get p :ustart))
1012 (uend (plist-get p :uend))
1013 (dstart (plist-get p :dstart))
1014 (dend (plist-get p :dend))
1015 (dtstart (plist-get p :dtstart))
1016 (dtend (plist-get p :dtend))
1017 (ddstart (plist-get p :ddstart))
1018 (ddend (plist-get p :ddend))
1019 (istart (plist-get p :istart))
1020 (iend (plist-get p :iend))
1021 (isep (plist-get p :isep))
1022 (lsep (plist-get p :lsep)))
1024 (cond ((eq (car list) 'ordered)
1025 (concat ostart "\n%s" oend "\n"))
1026 ((eq (car list) 'unordered)
1027 (concat ustart "\n%s" uend "\n"))
1028 ((eq (car list) 'descriptive)
1029 (concat dstart "\n%s" dend "\n"))))
1030 rtn term defstart defend)
1031 (while (setq sublist (pop list))
1032 (cond ((symbolp sublist) nil)
1034 (when (string-match "^\\(.*\\) ::" sublist)
1035 (setq term (org-trim (format (concat dtstart "%s" dtend)
1036 (match-string 1 sublist))))
1037 (setq sublist (substring sublist (1+ (length term)))))
1038 (setq rtn (concat rtn istart term ddstart
1039 sublist ddend iend isep)))
1040 (t (setq rtn (concat rtn ;; previous list
1041 lsep ;; list separator
1042 (org-list-to-generic sublist p)
1043 lsep ;; list separator
1045 (format wrapper rtn))))
1047 (defun org-list-to-latex (list)
1048 "Convert LIST into a LaTeX list."
1049 (org-list-to-generic
1050 list '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
1051 :ustart "\\begin{itemize}" :uend "\\end{itemize}"
1052 :dstart "\\begin{description}" :dend "\\end{description}"
1053 :dtstart "[" :dtend "]"
1054 :ddstart "" :ddend ""
1055 :istart "\\item " :iend ""
1056 :isep "\n" :lsep "\n")))
1058 (defun org-list-to-html (list)
1059 "Convert LIST into a HTML list."
1060 (org-list-to-generic
1061 list '(:splicep nil :ostart "<ol>" :oend "</ol>"
1062 :ustart "<ul>" :uend "</ul>"
1063 :dstart "<dl>" :dend "</dl>"
1064 :dtstart "<dt>" :dtend "</dt>"
1065 :ddstart "<dd>" :ddend "</dd>"
1066 :istart "<li>" :iend "</li>"
1067 :isep "\n" :lsep "\n")))
1069 (defun org-list-to-texinfo (list)
1070 "Convert LIST into a Texinfo list."
1071 (org-list-to-generic
1072 list '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
1073 :ustart "@enumerate" :uend "@end enumerate"
1074 :dstart "@table" :dend "@end table"
1075 :dtstart "@item " :dtend "\n"
1076 :ddstart "" :ddend ""
1077 :istart "@item\n" :iend ""
1078 :isep "\n" :lsep "\n")))
1082 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1083 ;;; org-list.el ends here