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
))
52 (defgroup org-plain-lists nil
53 "Options concerning plain lists in Org-mode."
54 :tag
"Org Plain lists"
55 :group
'org-structure
)
57 (defcustom org-cycle-include-plain-lists nil
58 "Non-nil means, include plain lists into visibility cycling.
59 This means that during cycling, plain list items will *temporarily* be
60 interpreted as outline headlines with a level given by 1000+i where i is the
61 indentation of the bullet. In all other operations, plain list items are
62 not seen as headlines. For example, you cannot assign a TODO keyword to
64 :group
'org-plain-lists
67 (defcustom org-plain-list-ordered-item-terminator t
68 "The character that makes a line with leading number an ordered list item.
69 Valid values are ?. and ?\). To get both terminators, use t. While
70 ?. may look nicer, it creates the danger that a line with leading
71 number may be incorrectly interpreted as an item. ?\) therefore is
73 :group
'org-plain-lists
74 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
75 (const :tag
"paren like in \"2)\"" ?\
))
76 (const :tab
"both" t
)))
78 (defcustom org-list-two-spaces-after-bullet-regexp nil
79 "A regular expression matching bullets that should have 2 spaces after them.
80 When nil, no bullet will have two spaces after them.
81 When a string, it will be used as a regular expression. When the bullet
82 type of a list is changed, the new bullet type will be matched against this
83 regexp. If it matches, there will be two spaces instead of one after
84 the bullet in each item of he list."
85 :group
'org-plain-list
87 (const :tag
"never" nil
)
90 (defcustom org-empty-line-terminates-plain-lists nil
91 "Non-nil means, an empty line ends all plain list levels.
92 When nil, empty lines are part of the preceeding item."
93 :group
'org-plain-lists
96 (defcustom org-auto-renumber-ordered-lists t
97 "Non-nil means, automatically renumber ordered plain lists.
98 Renumbering happens when the sequence have been changed with
99 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
100 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
101 :group
'org-plain-lists
104 (defcustom org-provide-checkbox-statistics t
105 "Non-nil means, update checkbox statistics after insert and toggle.
106 When this is set, checkbox statistics is updated each time you either insert
107 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
108 with \\[org-ctrl-c-ctrl-c\\]."
109 :group
'org-plain-lists
112 (defcustom org-description-max-indent
20
113 "Maximum indentation for the second line of a description list.
114 When the indentation would be larger than this, it will become
115 5 characters instead."
116 :group
'org-plain-lists
119 (defvar org-list-beginning-re
120 "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +\\(.*\\)$")
122 (defcustom org-list-radio-list-templates
123 '((latex-mode "% BEGIN RECEIVE ORGLST %n
124 % END RECEIVE ORGLST %n
126 #+ORGLST: SEND %n org-list-to-latex
129 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
130 @c END RECEIVE ORGLST %n
132 #+ORGLST: SEND %n org-list-to-texinfo
135 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
136 <!-- END RECEIVE ORGLST %n -->
138 #+ORGLST: SEND %n org-list-to-html
141 "Templates for radio lists in different major modes.
142 All occurrences of %n in a template will be replaced with the name of the
143 list, obtained by prompting the user."
144 :group
'org-plain-lists
146 (list (symbol :tag
"Major mode")
147 (string :tag
"Format"))))
149 ;;;; Plain list items, including checkboxes
153 (defun org-at-item-p ()
154 "Is point in a line starting a hand-formatted item?"
155 (let ((llt org-plain-list-ordered-item-terminator
))
157 (goto-char (point-at-bol))
160 ((eq llt t
) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
161 ((= llt ?.
) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
162 ((= llt ?\
)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
163 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
165 (defun org-at-item-bullet-p ()
166 "Is point at the bullet of a plain list item?"
168 (not (member (char-after) '(?\ ?
\t)))
169 (< (point) (match-end 0))))
171 (defun org-in-item-p ()
172 "It the cursor inside a plain list item.
173 Does not have to be the first line."
177 (org-beginning-of-item)
182 (defun org-insert-item (&optional checkbox
)
183 "Insert a new item at the current level.
184 Return t when things worked, nil when we are not in an item."
185 (when (save-excursion
188 (org-beginning-of-item)
190 (if (org-invisible-p) (error "Invisible item"))
193 (let* ((bul (match-string 0))
194 (descp (save-excursion (goto-char (match-beginning 0))
195 (beginning-of-line 1)
197 (and (looking-at "[ \t]*\\(.*?\\) ::")
199 (empty-line-p (save-excursion
200 (goto-char (match-beginning 0))
202 (or (beginning-of-line 0) t
)
204 (looking-at "[ \t]*$")))))
207 (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
209 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
211 (blank-a (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
212 (blank (if (eq blank-a
'auto
) empty-line-p blank-a
))
214 (if descp
(setq checkbox nil
))
216 (progn (org-timer-item) t
)
218 ((and (org-at-item-p) (<= (point) eow
))
220 (beginning-of-line 1)
221 (open-line (if blank
2 1)))
223 (beginning-of-line 1))
225 (unless (org-get-alist-option org-M-RET-may-split-line
'item
)
227 (delete-horizontal-space))
228 (newline (if blank
2 1))))
230 (if checkbox
"[ ]" "")
231 (if descp
(concat (if checkbox
" " "")
232 (read-string "Term: ") " :: ") ""))
236 (unless (= (point) pos
) (just-one-space) (backward-delete-char 1)))
237 (org-maybe-renumber-ordered-list)
238 (and checkbox
(org-update-checkbox-count-maybe))
243 (defun org-at-item-checkbox-p ()
244 "Is point at a line starting a plain-list item with a checklet?"
247 (goto-char (match-end 0))
248 (skip-chars-forward " \t")
249 (looking-at "\\[[- X]\\]"))))
251 (defun org-toggle-checkbox (&optional arg
)
252 "Toggle the checkbox in the current line."
255 (let (beg end status
(firstnew 'unknown
))
257 ((org-region-active-p)
258 (setq beg
(region-beginning) end
(region-end)))
260 (setq beg
(point) end
(save-excursion (outline-next-heading) (point))))
261 ((org-at-item-checkbox-p)
265 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
270 (t (error "Not at a checkbox or heading, and no active region")))
273 (while (< (point) end
)
274 (when (org-at-item-checkbox-p)
275 (setq status
(equal (match-string 0) "[X]"))
276 (when (eq firstnew
'unknown
)
277 (setq firstnew
(not status
)))
279 (if (if arg
(not status
) firstnew
) "[X]" "[ ]") t t
))
280 (beginning-of-line 2)))))
281 (org-update-checkbox-count-maybe))
283 (defun org-update-checkbox-count-maybe ()
284 "Update checkbox statistics unless turned off by user."
285 (when org-provide-checkbox-statistics
286 (org-update-checkbox-count)))
288 (defun org-update-checkbox-count (&optional all
)
289 "Update the checkbox statistics in the current section.
290 This will find all statistic cookies like [57%] and [6/12] and update them
291 with the current numbers. With optional prefix argument ALL, do this for
295 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
296 (beg (condition-case nil
297 (progn (org-back-to-heading) (point))
298 (error (point-min))))
299 (end (move-marker (make-marker)
300 (progn (outline-next-heading) (point))))
301 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
302 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
303 (re-find (concat re
"\\|" re-box
))
304 beg-cookie end-cookie is-percent c-on c-off lim
305 eline curr-ind next-ind continue-from startsearch
309 (goto-char (point-min))
310 (outline-next-heading)
311 (setq beg
(point) end
(point-max)))
313 ;; find each statistic cookie
314 (while (re-search-backward re-find beg t
)
315 (setq beg-cookie
(match-beginning 1)
316 end-cookie
(match-end 1)
317 cstat
(+ cstat
(if end-cookie
1 0))
318 startsearch
(point-at-eol)
319 continue-from
(point-at-bol)
320 is-percent
(match-beginning 2)
322 ((org-on-heading-p) (outline-next-heading) (point))
323 ((org-at-item-p) (org-end-of-item) (point))
328 ;; find first checkbox for this cookie and gather
329 ;; statistics from all that are at this indentation level
330 (goto-char startsearch
)
331 (if (re-search-forward re-box lim t
)
333 (org-beginning-of-item)
334 (setq curr-ind
(org-get-indentation))
335 (setq next-ind curr-ind
)
336 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind
))
337 (save-excursion (end-of-line) (setq eline
(point)))
338 (if (re-search-forward re-box eline t
)
339 (if (member (match-string 2) '("[ ]" "[-]"))
340 (setq c-off
(1+ c-off
))
341 (setq c-on
(1+ c-on
))
345 (setq next-ind
(org-get-indentation))
347 (goto-char continue-from
)
350 (delete-region beg-cookie end-cookie
)
351 (goto-char beg-cookie
)
354 (format "[%d%%]" (/ (* 100 c-on
) (max 1 (+ c-on c-off
))))
355 (format "[%d/%d]" c-on
(+ c-on c-off
)))))
356 ;; update items checkbox if it has one
357 (when (org-at-item-p)
358 (org-beginning-of-item)
359 (when (and (> (+ c-on c-off
) 0)
360 (re-search-forward re-box
(point-at-eol) t
))
361 (setq beg-cookie
(match-beginning 2)
362 end-cookie
(match-end 2))
363 (delete-region beg-cookie end-cookie
)
364 (goto-char beg-cookie
)
365 (cond ((= c-off
0) (insert "[X]"))
366 ((= c-on
0) (insert "[ ]"))
369 (goto-char continue-from
))
370 (when (interactive-p)
371 (message "Checkbox statistics updated %s (%d places)"
372 (if all
"in entire file" "in current outline entry") cstat
)))))
374 (defun org-get-checkbox-statistics-face ()
375 "Select the face for checkbox statistics.
376 The face will be `org-done' when all relevant boxes are checked. Otherwise
377 it will be `org-todo'."
379 (if (equal (match-string 1) "100%") 'org-done
'org-todo
)
380 (if (and (> (match-end 2) (match-beginning 2))
381 (equal (match-string 2) (match-string 3)))
385 (defun org-beginning-of-item ()
386 "Go to the beginning of the current hand-formatted item.
387 If the cursor is not in an item, throw an error."
390 (limit (save-excursion
393 (org-back-to-heading)
394 (beginning-of-line 2) (point))
395 (error (point-min)))))
396 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
399 (beginning-of-line 1)
400 (beginning-of-line 1)
401 (skip-chars-forward " \t")
402 (setq ind
(current-column))
405 (beginning-of-line 0)
406 (if (or (bobp) (< (point) limit
)) (throw 'exit nil
))
408 (if (looking-at "[ \t]*$")
409 (setq ind1 ind-empty
)
410 (skip-chars-forward " \t")
411 (setq ind1
(current-column)))
413 (progn (beginning-of-line 1) (throw 'exit
(org-at-item-p))))))
416 (error "Not in an item")))))
418 (defun org-end-of-item ()
419 "Go to the end of the current hand-formatted item.
420 If the cursor is not in an item, throw an error."
424 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
425 (limit (save-excursion (outline-next-heading) (point)))
427 (org-beginning-of-item)
428 (skip-chars-forward " \t")
432 (beginning-of-line 2)
433 (if (eobp) (throw 'exit
(point)))
434 (if (>= (point) limit
) (throw 'exit
(point-at-bol)))
435 (if (looking-at "[ \t]*$")
436 (setq ind1 ind-empty
)
437 (skip-chars-forward " \t")
438 (setq ind1
(current-column)))
440 (throw 'exit
(point-at-bol)))))))
444 (error "Not in an item"))))
446 (defun org-next-item ()
447 "Move to the beginning of the next item in the current plain list.
448 Error if not at a plain list, or if this is the last item in the list."
450 (let (ind ind1
(pos (point)))
451 (org-beginning-of-item)
452 (setq ind
(org-get-indentation))
454 (setq ind1
(org-get-indentation))
455 (unless (and (org-at-item-p) (= ind ind1
))
457 (error "On last item"))))
459 (defun org-previous-item ()
460 "Move to the beginning of the previous item in the current plain list.
461 Error if not at a plain list, or if this is the first item in the list."
463 (let (beg ind ind1
(pos (point)))
464 (org-beginning-of-item)
466 (setq ind
(org-get-indentation))
470 (beginning-of-line 0)
471 (if (looking-at "[ \t]*$")
473 (if (<= (setq ind1
(org-get-indentation)) ind
)
476 (if (or (not (org-at-item-p))
479 (org-beginning-of-item))
480 (error (goto-char pos
)
481 (error "On first item")))))
483 (defun org-first-list-item-p ()
484 "Is this heading the item in a plain list?"
485 (unless (org-at-item-p)
486 (error "Not at a plain list item"))
487 (org-beginning-of-item)
488 (= (point) (save-excursion (org-beginning-of-item-list))))
490 (defun org-move-item-down ()
491 "Move the plain list item at point down, i.e. swap with following item.
492 Subitems (items with larger indentation) are considered part of the item,
493 so this really moves item trees."
495 (let ((col (current-column))
497 beg beg0 end end0 ind ind1 txt ne-end ne-beg
)
498 (org-beginning-of-item)
501 (setq ne-beg
(org-back-over-empty-lines))
504 (setq ind
(org-get-indentation))
507 (setq ind1
(org-get-indentation))
508 (setq ne-end
(org-back-over-empty-lines))
511 (when (and (org-first-list-item-p) (< ne-end ne-beg
))
512 ;; include less whitespace
515 (forward-line (- ne-beg ne-end
))
518 (if (and (org-at-item-p) (= ind ind1
))
521 (org-back-over-empty-lines)
522 (setq txt
(buffer-substring beg end
))
524 (delete-region beg end
))
527 (goto-char pos
) (org-skip-whitespace)
528 (org-maybe-renumber-ordered-list)
529 (move-to-column col
))
532 (error "Cannot move this item further down"))))
534 (defun org-move-item-up (arg)
535 "Move the plain list item at point up, i.e. swap with previous item.
536 Subitems (items with larger indentation) are considered part of the item,
537 so this really moves item trees."
539 (let ((col (current-column)) (pos (point))
540 beg beg0 end ind ind1 txt
541 ne-beg ne-ins ins-end
)
542 (org-beginning-of-item)
544 (setq ind
(org-get-indentation))
546 (setq ne-beg
(org-back-over-empty-lines))
550 (org-back-over-empty-lines)
555 (beginning-of-line 0)
556 (if (looking-at "[ \t]*$")
557 (if org-empty-line-terminates-plain-lists
560 (error "Cannot move this item further up"))
562 (if (<= (setq ind1
(org-get-indentation)) ind
)
565 (org-beginning-of-item)
566 (error (goto-char beg0
)
568 (error "Cannot move this item further up")))
569 (setq ind1
(org-get-indentation))
570 (if (and (org-at-item-p) (= ind ind1
))
572 (setq ne-ins
(org-back-over-empty-lines))
573 (setq txt
(buffer-substring beg end
))
575 (delete-region beg end
))
578 (setq ins-end
(point))
579 (goto-char pos
) (org-skip-whitespace)
581 (when (and (org-first-list-item-p) (> ne-ins ne-beg
))
582 ;; Move whitespace back to beginning
585 (let ((kill-whole-line t
))
586 (kill-line (- ne-ins ne-beg
)) (point)))
587 (insert (make-string (- ne-ins ne-beg
) ?
\n)))
589 (org-maybe-renumber-ordered-list)
590 (move-to-column col
))
593 (error "Cannot move this item further up"))))
595 (defun org-maybe-renumber-ordered-list ()
596 "Renumber the ordered list at point if setup allows it.
597 This tests the user option `org-auto-renumber-ordered-lists' before
598 doing the renumbering."
600 (when (and org-auto-renumber-ordered-lists
602 (if (match-beginning 3)
603 (org-renumber-ordered-list 1)
604 (org-fix-bullet-type))))
606 (defun org-maybe-renumber-ordered-list-safe ()
609 (org-maybe-renumber-ordered-list))
612 (defun org-cycle-list-bullet (&optional which
)
613 "Cycle through the different itemize/enumerate bullets.
614 This cycle the entire list level through the sequence:
616 `-' -> `+' -> `*' -> `1.' -> `1)'
618 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
619 0 means `-', 1 means `+' etc."
622 (org-beginning-of-item-list)
624 (beginning-of-line 1)
625 (let ((current (match-string 0))
626 (prevp (eq which
'previous
))
629 ((and (numberp which
)
630 (nth (1- which
) '("-" "+" "*" "1." "1)"))))
631 ((string-match "-" current
) (if prevp
"1)" "+"))
632 ((string-match "\\+" current
)
633 (if prevp
"-" (if (looking-at "\\S-") "1." "*")))
634 ((string-match "\\*" current
) (if prevp
"+" "1."))
635 ((string-match "\\." current
)
636 (if prevp
(if (looking-at "\\S-") "+" "*") "1)"))
637 ((string-match ")" current
) (if prevp
"1." "-"))
638 (t (error "This should not happen"))))
639 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
640 (setq old
(match-string 2))
641 (replace-match (concat "\\1" new
)))
642 (org-shift-item-indentation (- (length new
) (length old
)))
643 (org-fix-bullet-type)
644 (org-maybe-renumber-ordered-list))))
646 (defun org-get-string-indentation (s)
647 "What indentation has S due to SPACE and TAB at the beginning of the string?"
648 (let ((n -
1) (i 0) (w tab-width
) c
)
650 (while (< (setq n
(1+ n
)) (length s
))
652 (cond ((= c ?\
) (setq i
(1+ i
)))
653 ((= c ?
\t) (setq i
(* (/ (+ w i
) w
) w
)))
654 (t (throw 'exit t
)))))
657 (defun org-renumber-ordered-list (arg)
658 "Renumber an ordered plain list.
659 Cursor needs to be in the first line of an item, the line that starts
660 with something like \"1.\" or \"2)\"."
662 (unless (and (org-at-item-p)
664 (error "This is not an ordered list"))
665 (let ((line (org-current-line))
666 (col (current-column))
667 (ind (org-get-string-indentation
668 (buffer-substring (point-at-bol) (match-beginning 3))))
669 ;; (term (substring (match-string 3) -1))
672 ;; find where this list begins
673 (org-beginning-of-item-list)
675 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
676 (setq fmt
(concat "%d" (match-string 1)))
677 (beginning-of-line 0)
678 ;; walk forward and replace these numbers
682 (if bobp
(setq bobp nil
) (beginning-of-line 2))
683 (if (eobp) (throw 'exit nil
))
684 (if (looking-at "[ \t]*$") (throw 'next nil
))
685 (skip-chars-forward " \t") (setq ind1
(current-column))
686 (if (> ind1 ind
) (throw 'next t
))
687 (if (< ind1 ind
) (throw 'exit t
))
688 (if (not (org-at-item-p)) (throw 'exit nil
))
689 (setq old
(match-string 2))
690 (delete-region (match-beginning 2) (match-end 2))
691 (goto-char (match-beginning 2))
692 (insert (setq new
(format fmt
(setq n
(1+ n
)))))
693 (org-shift-item-indentation (- (length new
) (length old
))))))
695 (org-move-to-column col
)))
697 (defun org-fix-bullet-type ()
698 "Make sure all items in this list have the same bullet as the first item.
699 Also, fix the indentation."
701 (unless (org-at-item-p) (error "This is not a list"))
702 (let ((line (org-current-line))
703 (col (current-column))
704 (ind (current-indentation))
705 ind1 bullet oldbullet
)
706 ;; find where this list begins
707 (org-beginning-of-item-list)
708 (beginning-of-line 1)
709 ;; find out what the bullet type is
710 (looking-at "[ \t]*\\(\\S-+\\)")
711 (setq bullet
(concat (match-string 1) " "))
712 (if (and org-list-two-spaces-after-bullet-regexp
713 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
714 (setq bullet
(concat bullet
" ")))
715 ;; walk forward and replace these numbers
716 (beginning-of-line 0)
720 (beginning-of-line 2)
721 (if (eobp) (throw 'exit nil
))
722 (if (looking-at "[ \t]*$") (throw 'next nil
))
723 (skip-chars-forward " \t") (setq ind1
(current-column))
724 (if (> ind1 ind
) (throw 'next t
))
725 (if (< ind1 ind
) (throw 'exit t
))
726 (if (not (org-at-item-p)) (throw 'exit nil
))
727 (skip-chars-forward " \t")
728 (looking-at "\\S-+ *")
729 (setq oldbullet
(match-string 0))
730 (replace-match bullet
)
731 (org-shift-item-indentation (- (length bullet
) (length oldbullet
))))))
733 (org-move-to-column col
)
734 (if (string-match "[0-9]" bullet
)
735 (org-renumber-ordered-list 1))))
737 (defun org-shift-item-indentation (delta)
738 "Shift the indentation in current item by DELTA."
740 (let ((beg (point-at-bol))
741 (end (progn (org-end-of-item) (point)))
744 (beginning-of-line 0)
745 (while (> (point) beg
)
746 (when (looking-at "[ \t]*\\S-")
747 ;; this is not an empty line
748 (setq i
(org-get-indentation))
749 (if (and (> i
0) (> (setq i
(+ i delta
)) 0))
751 (beginning-of-line 0)))))
753 (defun org-beginning-of-item-list ()
754 "Go to the beginning of the current item list.
755 I.e. to the first item in this list."
757 (org-beginning-of-item)
758 (let ((pos (point-at-bol))
759 (ind (org-get-indentation))
761 ;; find where this list begins
765 (beginning-of-line 0)
766 (if (looking-at "[ \t]*$")
767 (throw (if (bobp) 'exit
'next
) t
))
768 (skip-chars-forward " \t") (setq ind1
(current-column))
771 (not (org-at-item-p)))
772 (and (= (point-at-bol) (point-min))
773 (setq pos
(point-min))))
775 (when (org-at-item-p) (setq pos
(point-at-bol)))))))
779 (defun org-end-of-item-list ()
780 "Go to the end of the current item list.
781 I.e. to the text after the last item."
783 (org-beginning-of-item)
784 (let ((pos (point-at-bol))
785 (ind (org-get-indentation))
787 ;; find where this list begins
791 (beginning-of-line 2)
792 (if (looking-at "[ \t]*$")
793 (throw (if (eobp) 'exit
'next
) t
))
794 (skip-chars-forward " \t") (setq ind1
(current-column))
797 (not (org-at-item-p)))
800 (setq pos
(point-at-bol))
805 (defvar org-last-indent-begin-marker
(make-marker))
806 (defvar org-last-indent-end-marker
(make-marker))
808 (defun org-outdent-item (arg)
809 "Outdent a local list item."
811 (org-indent-item (- arg
)))
813 (defun org-indent-item (arg)
814 "Indent a local list item."
816 (unless (org-at-item-p)
817 (error "Not on an item"))
819 (let (beg end ind ind1 tmp delta ind-down ind-up
)
820 (if (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
821 (setq beg org-last-indent-begin-marker
822 end org-last-indent-end-marker
)
823 (org-beginning-of-item)
824 (setq beg
(move-marker org-last-indent-begin-marker
(point)))
826 (setq end
(move-marker org-last-indent-end-marker
(point))))
828 (setq tmp
(org-item-indent-positions)
833 (if ind-down
(- ind-down ind
) 2)
834 (if ind-up
(- ind-up ind
) -
2)))
835 (if (< (+ delta ind
) 0) (error "Cannot outdent beyond margin"))
836 (while (< (point) end
)
837 (beginning-of-line 1)
838 (skip-chars-forward " \t") (setq ind1
(current-column))
839 (delete-region (point-at-bol) (point))
840 (or (eolp) (org-indent-to-column (+ ind1 delta
)))
841 (beginning-of-line 2))))
842 (org-fix-bullet-type)
843 (org-maybe-renumber-ordered-list-safe)
845 (beginning-of-line 0)
846 (condition-case nil
(org-beginning-of-item) (error nil
))
847 (org-maybe-renumber-ordered-list-safe)))
849 (defun org-item-indent-positions ()
850 "Return indentation for plain list items.
851 This returns a list with three values: The current indentation, the
852 parent indentation and the indentation a child should have.
853 Assumes cursor in item line."
854 (let* ((bolpos (point-at-bol))
855 (ind (org-get-indentation))
858 (org-beginning-of-item-list)
859 (skip-chars-backward "\n\r \t")
860 (when (org-in-item-p)
861 (org-beginning-of-item)
862 (setq ind-up
(org-get-indentation))))
866 ((and (condition-case nil
(progn (org-previous-item) t
)
868 (or (forward-char 1) t
)
869 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t
))
870 (setq ind-down
(org-get-indentation)))
871 ((and (goto-char pos
)
873 (goto-char (match-end 0))
874 (skip-chars-forward " \t")
875 (setq ind-down
(current-column)))))
876 (list ind ind-up ind-down
)))
879 ;;; Send and receive lists
881 (defun org-list-parse-list (&optional delete
)
882 "Parse the list at point and maybe DELETE it.
883 Return a list containing first level items as strings and
884 sublevels as a list of strings."
885 (let* ((item-beginning (org-list-item-beginning))
886 (start (car item-beginning
))
887 (end (org-list-end (cdr item-beginning
)))
888 output itemsep ltype
)
889 (while (re-search-forward org-list-beginning-re end t
)
890 (goto-char (match-beginning 3))
892 (cond ((string-match "[0-9]" (match-string 2))
893 (setq itemsep
"[0-9]+\\(?:\\.\\|)\\)"
895 ((string-match "^.*::" (match-string 0))
896 (setq itemsep
"[-+]" ltype
'descriptive
))
897 (t (setq itemsep
"[-+]" ltype
'unordered
))))
898 (let* ((indent1 (match-string 1))
899 (nextitem (save-excursion
901 (or (and (re-search-forward
902 (concat "^" indent1 itemsep
" *?") end t
)
903 (match-beginning 0)) end
))))
904 (item (buffer-substring
906 (or (and (re-search-forward
907 org-list-beginning-re end t
)
908 (goto-char (match-beginning 0)))
910 (nextindent (match-string 1))
911 (item (org-trim item
))
912 (item (if (string-match "^\\[\\([xX ]\\)\\]" item
)
913 (replace-match (if (equal (match-string 1 item
) " ")
919 (when (> (length nextindent
)
921 (narrow-to-region (point) nextitem
)
922 (push (org-list-parse-list) output
)
924 (when delete
(delete-region start end
))
925 (setq output
(nreverse output
))
926 (push ltype output
)))
928 (defun org-list-item-beginning ()
929 "Find the beginning of the list item.
930 Return a cons which car is the beginning position of the item and
931 cdr is the indentation string."
933 (if (not (or (looking-at org-list-beginning-re
)
935 org-list-beginning-re nil t
)))
936 (progn (goto-char (point-min)) (point))
937 (cons (match-beginning 0) (match-string 1)))))
939 (defun org-list-end (indent)
940 "Return the position of the end of the list.
941 INDENT is the indentation of the list."
944 (while (or (looking-at org-list-beginning-re
)
945 (looking-at (concat "^" indent
"[ \t]+\\|^$")))
946 (if (eq (point) (point-max))
947 (throw 'exit
(point-max)))
948 (forward-line 1))) (point)))
950 (defun org-list-insert-radio-list ()
951 "Insert a radio list template appropriate for this major mode."
953 (let* ((e (assq major-mode org-list-radio-list-templates
))
956 (unless e
(error "No radio list setup defined for %s" major-mode
))
957 (setq name
(read-string "List name: "))
958 (while (string-match "%n" txt
)
959 (setq txt
(replace-match name t t txt
)))
960 (or (bolp) (insert "\n"))
965 (defun org-list-send-list (&optional maybe
)
966 "Send a tranformed version of this list to the receiver position.
967 With argument MAYBE, fail quietly if no transformation is defined for
971 (unless (org-at-item-p) (error "Not at a list"))
973 (goto-char (car (org-list-item-beginning)))
974 (beginning-of-line 0)
975 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
978 (error "Don't know how to transform this list"))))
979 (let* ((name (match-string 1))
980 (item-beginning (org-list-item-beginning))
981 (transform (intern (match-string 2)))
982 (txt (buffer-substring-no-properties
984 (org-list-end (cdr item-beginning
))))
985 (list (org-list-parse-list))
987 (unless (fboundp transform
)
988 (error "No such transformation function %s" transform
))
989 (setq txt
(funcall transform list
))
990 ;; Find the insertion place
992 (goto-char (point-min))
993 (unless (re-search-forward
994 (concat "BEGIN RECEIVE ORGLST +" name
"\\([ \t]\\|$\\)") nil t
)
995 (error "Don't know where to insert translated list"))
996 (goto-char (match-beginning 0))
997 (beginning-of-line 2)
999 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
1000 (error "Cannot find end of insertion region"))
1001 (beginning-of-line 1)
1002 (delete-region beg
(point))
1005 (message "List converted and installed at receiver location"))))
1007 (defun org-list-to-generic (list params
)
1008 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1010 Valid parameters PARAMS are
1012 :ustart String to start an unordered list
1013 :uend String to end an unordered list
1015 :ostart String to start an ordered list
1016 :oend String to end an ordered list
1018 :dstart String to start a descriptive list
1019 :dend String to end a descriptive list
1020 :dtstart String to start a descriptive term
1021 :dtend String to end a descriptive term
1022 :ddstart String to start a description
1023 :ddend String to end a description
1025 :splice When set to t, return only list body lines, don't wrap
1026 them into :[u/o]start and :[u/o]end. Default is nil.
1028 :istart String to start a list item
1029 :iend String to end a list item
1030 :isep String to separate items
1031 :lsep String to separate sublists
1033 :cboff String to insert for an unchecked checkbox
1034 :cbon String to insert for a checked checkbox"
1036 (let* ((p params
) sublist
1037 (splicep (plist-get p
:splice
))
1038 (ostart (plist-get p
:ostart
))
1039 (oend (plist-get p
:oend
))
1040 (ustart (plist-get p
:ustart
))
1041 (uend (plist-get p
:uend
))
1042 (dstart (plist-get p
:dstart
))
1043 (dend (plist-get p
:dend
))
1044 (dtstart (plist-get p
:dtstart
))
1045 (dtend (plist-get p
:dtend
))
1046 (ddstart (plist-get p
:ddstart
))
1047 (ddend (plist-get p
:ddend
))
1048 (istart (plist-get p
:istart
))
1049 (iend (plist-get p
:iend
))
1050 (isep (plist-get p
:isep
))
1051 (lsep (plist-get p
:lsep
))
1052 (cbon (plist-get p
:cbon
))
1053 (cboff (plist-get p
:cboff
)))
1055 (cond ((eq (car list
) 'ordered
)
1056 (concat ostart
"\n%s" oend
"\n"))
1057 ((eq (car list
) 'unordered
)
1058 (concat ustart
"\n%s" uend
"\n"))
1059 ((eq (car list
) 'descriptive
)
1060 (concat dstart
"\n%s" dend
"\n"))))
1061 rtn term defstart defend
)
1062 (while (setq sublist
(pop list
))
1063 (cond ((symbolp sublist
) nil
)
1065 (when (string-match "^\\(.*\\) ::" sublist
)
1066 (setq term
(org-trim (format (concat dtstart
"%s" dtend
)
1067 (match-string 1 sublist
))))
1068 (setq sublist
(substring sublist
(1+ (length term
)))))
1069 (if (string-match "\\[CBON\\]" sublist
)
1070 (setq sublist
(replace-match cbon t t sublist
)))
1071 (if (string-match "\\[CBOFF\\]" sublist
)
1072 (setq sublist
(replace-match cboff t t sublist
)))
1073 (setq rtn
(concat rtn istart term ddstart
1074 sublist ddend iend isep
)))
1075 (t (setq rtn
(concat rtn
;; previous list
1076 lsep
;; list separator
1077 (org-list-to-generic sublist p
)
1078 lsep
;; list separator
1080 (format wrapper rtn
))))
1082 (defun org-list-to-latex (list &optional params
)
1083 "Convert LIST into a LaTeX list.
1084 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1085 with overruling parameters for `org-list-to-generic'."
1086 (org-list-to-generic
1089 '(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
1090 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
1091 :dstart
"\\begin{description}" :dend
"\\end{description}"
1092 :dtstart
"[" :dtend
"]"
1093 :ddstart
"" :ddend
""
1094 :istart
"\\item " :iend
""
1095 :isep
"\n" :lsep
"\n"
1096 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
1099 (defun org-list-to-html (list &optional params
)
1100 "Convert LIST into a HTML list.
1101 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1102 with overruling parameters for `org-list-to-generic'."
1103 (org-list-to-generic
1106 '(:splicep nil
:ostart
"<ol>" :oend
"</ol>"
1107 :ustart
"<ul>" :uend
"</ul>"
1108 :dstart
"<dl>" :dend
"</dl>"
1109 :dtstart
"<dt>" :dtend
"</dt>"
1110 :ddstart
"<dd>" :ddend
"</dd>"
1111 :istart
"<li>" :iend
"</li>"
1112 :isep
"\n" :lsep
"\n"
1113 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>")
1116 (defun org-list-to-texinfo (list &optional params
)
1117 "Convert LIST into a Texinfo list.
1118 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1119 with overruling parameters for `org-list-to-generic'."
1120 (org-list-to-generic
1123 '(:splicep nil
:ostart
"@itemize @minus" :oend
"@end itemize"
1124 :ustart
"@enumerate" :uend
"@end enumerate"
1125 :dstart
"@table" :dend
"@end table"
1126 :dtstart
"@item " :dtend
"\n"
1127 :ddstart
"" :ddend
""
1128 :istart
"@item\n" :iend
""
1129 :isep
"\n" :lsep
"\n"
1130 :cbon
"@code{[X]}" :cboff
"@code{[ ]}")
1135 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1136 ;;; org-list.el ends here