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
))
53 (defgroup org-plain-lists nil
54 "Options concerning plain lists in Org-mode."
55 :tag
"Org Plain lists"
56 :group
'org-structure
)
58 (defcustom org-cycle-include-plain-lists nil
59 "Non-nil means, include plain lists into visibility cycling.
60 This means that during cycling, plain list items will *temporarily* be
61 interpreted as outline headlines with a level given by 1000+i where i is the
62 indentation of the bullet. In all other operations, plain list items are
63 not seen as headlines. For example, you cannot assign a TODO keyword to
65 :group
'org-plain-lists
68 (defcustom org-plain-list-ordered-item-terminator t
69 "The character that makes a line with leading number an ordered list item.
70 Valid values are ?. and ?\). To get both terminators, use t. While
71 ?. may look nicer, it creates the danger that a line with leading
72 number may be incorrectly interpreted as an item. ?\) therefore is
74 :group
'org-plain-lists
75 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
76 (const :tag
"paren like in \"2)\"" ?\
))
77 (const :tab
"both" t
)))
79 (defcustom org-list-two-spaces-after-bullet-regexp nil
80 "A regular expression matching bullets that should have 2 spaces after them.
81 When nil, no bullet will have two spaces after them.
82 When a string, it will be used as a regular expression. When the bullet
83 type of a list is changed, the new bullet type will be matched against this
84 regexp. If it matches, there will be two spaces instead of one after
85 the bullet in each item of he list."
86 :group
'org-plain-list
88 (const :tag
"never" nil
)
91 (defcustom org-empty-line-terminates-plain-lists nil
92 "Non-nil means, an empty line ends all plain list levels.
93 When nil, empty lines are part of the preceding item."
94 :group
'org-plain-lists
97 (defcustom org-auto-renumber-ordered-lists t
98 "Non-nil means, automatically renumber ordered plain lists.
99 Renumbering happens when the sequence have been changed with
100 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
101 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
102 :group
'org-plain-lists
105 (defcustom org-provide-checkbox-statistics t
106 "Non-nil means, update checkbox statistics after insert and toggle.
107 When this is set, checkbox statistics is updated each time you either insert
108 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
109 with \\[org-ctrl-c-ctrl-c\\]."
110 :group
'org-plain-lists
113 (defcustom org-description-max-indent
20
114 "Maximum indentation for the second line of a description list.
115 When the indentation would be larger than this, it will become
116 5 characters instead."
117 :group
'org-plain-lists
120 (defvar org-list-beginning-re
121 "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +\\(.*\\)$")
123 (defcustom org-list-radio-list-templates
124 '((latex-mode "% BEGIN RECEIVE ORGLST %n
125 % END RECEIVE ORGLST %n
127 #+ORGLST: SEND %n org-list-to-latex
130 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
131 @c END RECEIVE ORGLST %n
133 #+ORGLST: SEND %n org-list-to-texinfo
136 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
137 <!-- END RECEIVE ORGLST %n -->
139 #+ORGLST: SEND %n org-list-to-html
142 "Templates for radio lists in different major modes.
143 All occurrences of %n in a template will be replaced with the name of the
144 list, obtained by prompting the user."
145 :group
'org-plain-lists
147 (list (symbol :tag
"Major mode")
148 (string :tag
"Format"))))
150 ;;;; Plain list items, including checkboxes
154 (defun org-at-item-p ()
155 "Is point in a line starting a hand-formatted item?"
156 (let ((llt org-plain-list-ordered-item-terminator
))
158 (goto-char (point-at-bol))
161 ((eq llt t
) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
162 ((= llt ?.
) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
163 ((= llt ?\
)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
164 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
166 (defun org-at-item-bullet-p ()
167 "Is point at the bullet of a plain list item?"
169 (not (member (char-after) '(?\ ?
\t)))
170 (< (point) (match-end 0))))
172 (defun org-in-item-p ()
173 "It the cursor inside a plain list item.
174 Does not have to be the first line."
178 (org-beginning-of-item)
183 (defun org-insert-item (&optional checkbox
)
184 "Insert a new item at the current level.
185 Return t when things worked, nil when we are not in an item."
186 (when (save-excursion
189 (org-beginning-of-item)
191 (if (org-invisible-p) (error "Invisible item"))
194 (let* ((bul (match-string 0))
195 (descp (save-excursion (goto-char (match-beginning 0))
196 (beginning-of-line 1)
198 (and (looking-at "[ \t]*\\(.*?\\) ::")
200 (empty-line-p (save-excursion
201 (goto-char (match-beginning 0))
203 (or (beginning-of-line 0) t
)
205 (looking-at "[ \t]*$")))))
208 (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
210 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
212 (blank-a (if org-empty-line-terminates-plain-lists
214 (cdr (assq 'plain-list-item org-blank-before-new-entry
))))
215 (blank (if (eq blank-a
'auto
) empty-line-p blank-a
))
217 (if descp
(setq checkbox nil
))
219 (progn (org-timer-item) t
)
221 ((and (org-at-item-p) (<= (point) eow
))
223 (beginning-of-line 1)
224 (open-line (if blank
2 1)))
226 (beginning-of-line 1))
228 (unless (org-get-alist-option org-M-RET-may-split-line
'item
)
230 (delete-horizontal-space))
231 (newline (if blank
2 1))))
233 (if checkbox
"[ ]" "")
234 (if descp
(concat (if checkbox
" " "")
235 (read-string "Term: ") " :: ") ""))
239 (unless (= (point) pos
) (just-one-space) (backward-delete-char 1)))
240 (org-maybe-renumber-ordered-list)
241 (and checkbox
(org-update-checkbox-count-maybe))
246 (defun org-at-item-checkbox-p ()
247 "Is point at a line starting a plain-list item with a checklet?"
250 (goto-char (match-end 0))
251 (skip-chars-forward " \t")
252 (looking-at "\\[[- X]\\]"))))
254 (defun org-toggle-checkbox (&optional toggle-presence
)
255 "Toggle the checkbox in the current line.
256 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
257 With double prefix, set checkbox to [-].
258 When there is an active region, toggle status or presence of the checkbox
259 in the first line, and make every item in the region have the same
260 status or presence, respectively.
261 If the cursor is in a headline, apply this to all checkbox items in the
262 text below the heading."
265 (let (beg end status first-present first-status blocked
)
267 ((org-region-active-p)
268 (setq beg
(region-beginning) end
(region-end)))
270 (setq beg
(point) end
(save-excursion (outline-next-heading) (point))))
271 ((org-at-item-checkbox-p)
273 (if (equal toggle-presence
'(4))
276 (goto-char (match-beginning 0))
278 (when (setq blocked
(org-checkbox-blocked-p))
279 (error "Checkbox blocked because of unchecked box in line %d"
282 (cond ((equal toggle-presence
'(16)) "[-]")
283 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
290 (goto-char (match-end 0))
293 (t (error "Not at a checkbox or heading, and no active region")))
294 (setq end
(move-marker (make-marker) end
))
297 (setq first-present
(org-at-item-checkbox-p)
300 (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t
)
301 (equal (match-string 1) "[X]"))))
302 (while (< (point) end
)
305 ((and first-present
(org-at-item-checkbox-p))
308 (goto-char (match-beginning 0))
310 ((and (not first-present
) (not (org-at-item-checkbox-p))
313 (goto-char (match-end 0))
315 (when (org-at-item-checkbox-p)
316 (setq status
(equal (match-string 0) "[X]"))
318 (if first-status
"[ ]" "[X]") t t
)))
319 (beginning-of-line 2)))))
320 (org-update-checkbox-count-maybe))
322 (defun org-checkbox-blocked-p ()
323 "Is the current checkbox blocked from for being checked now?
324 A checkbox is blocked if all of the following conditions are fulfilled:
326 1. The checkbox is not checked already.
327 2. The current entry has the ORDERED property set.
328 3. There is an unchecked checkbox in this entry before the current line."
332 (unless (org-at-item-checkbox-p) (throw 'exit nil
))
333 (when (equal (match-string 0) "[X]")
334 ;; the box is already checked!
336 (let ((end (point-at-bol)))
337 (condition-case nil
(org-back-to-heading t
)
338 (error (throw 'exit nil
)))
339 (unless (org-entry-get nil
"ORDERED") (throw 'exit nil
))
340 (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t
)
344 (defun org-update-checkbox-count-maybe ()
345 "Update checkbox statistics unless turned off by user."
346 (when org-provide-checkbox-statistics
347 (org-update-checkbox-count)))
349 (defun org-update-checkbox-count (&optional all
)
350 "Update the checkbox statistics in the current section.
351 This will find all statistic cookies like [57%] and [6/12] and update them
352 with the current numbers. With optional prefix argument ALL, do this for
356 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
357 (beg (condition-case nil
358 (progn (org-back-to-heading) (point))
359 (error (point-min))))
360 (end (move-marker (make-marker)
361 (progn (outline-next-heading) (point))))
362 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
363 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
364 (re-find (concat re
"\\|" re-box
))
365 beg-cookie end-cookie is-percent c-on c-off lim
366 eline curr-ind next-ind continue-from startsearch
370 (goto-char (point-min))
371 (outline-next-heading)
372 (setq beg
(point) end
(point-max)))
374 ;; find each statistic cookie
375 (while (re-search-backward re-find beg t
)
376 (setq beg-cookie
(match-beginning 1)
377 end-cookie
(match-end 1)
378 cstat
(+ cstat
(if end-cookie
1 0))
379 startsearch
(point-at-eol)
380 continue-from
(match-beginning 0)
381 is-percent
(match-beginning 2)
383 ((org-on-heading-p) (outline-next-heading) (point))
384 ((org-at-item-p) (org-end-of-item) (point))
389 ;; find first checkbox for this cookie and gather
390 ;; statistics from all that are at this indentation level
391 (goto-char startsearch
)
392 (if (re-search-forward re-box lim t
)
394 (org-beginning-of-item)
395 (setq curr-ind
(org-get-indentation))
396 (setq next-ind curr-ind
)
397 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind
))
398 (save-excursion (end-of-line) (setq eline
(point)))
399 (if (re-search-forward re-box eline t
)
400 (if (member (match-string 2) '("[ ]" "[-]"))
401 (setq c-off
(1+ c-off
))
402 (setq c-on
(1+ c-on
))
406 (setq next-ind
(org-get-indentation))
408 (goto-char continue-from
)
411 (delete-region beg-cookie end-cookie
)
412 (goto-char beg-cookie
)
415 (format "[%d%%]" (/ (* 100 c-on
) (max 1 (+ c-on c-off
))))
416 (format "[%d/%d]" c-on
(+ c-on c-off
)))))
417 ;; update items checkbox if it has one
418 (when (org-at-item-p)
419 (org-beginning-of-item)
420 (when (and (> (+ c-on c-off
) 0)
421 (re-search-forward re-box
(point-at-eol) t
))
422 (setq beg-cookie
(match-beginning 2)
423 end-cookie
(match-end 2))
424 (delete-region beg-cookie end-cookie
)
425 (goto-char beg-cookie
)
426 (cond ((= c-off
0) (insert "[X]"))
427 ((= c-on
0) (insert "[ ]"))
430 (goto-char continue-from
))
431 (when (interactive-p)
432 (message "Checkbox statistics updated %s (%d places)"
433 (if all
"in entire file" "in current outline entry") cstat
)))))
435 (defun org-get-checkbox-statistics-face ()
436 "Select the face for checkbox statistics.
437 The face will be `org-done' when all relevant boxes are checked. Otherwise
438 it will be `org-todo'."
440 (if (equal (match-string 1) "100%") 'org-done
'org-todo
)
441 (if (and (> (match-end 2) (match-beginning 2))
442 (equal (match-string 2) (match-string 3)))
446 (defun org-beginning-of-item ()
447 "Go to the beginning of the current hand-formatted item.
448 If the cursor is not in an item, throw an error."
451 (limit (save-excursion
454 (org-back-to-heading)
455 (beginning-of-line 2) (point))
456 (error (point-min)))))
457 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
460 (beginning-of-line 1)
461 (beginning-of-line 1)
462 (skip-chars-forward " \t")
463 (setq ind
(current-column))
466 (beginning-of-line 0)
467 (if (or (bobp) (< (point) limit
)) (throw 'exit nil
))
469 (if (looking-at "[ \t]*$")
470 (setq ind1 ind-empty
)
471 (skip-chars-forward " \t")
472 (setq ind1
(current-column)))
474 (progn (beginning-of-line 1) (throw 'exit
(org-at-item-p))))))
477 (error "Not in an item")))))
479 (defun org-end-of-item ()
480 "Go to the end of the current hand-formatted item.
481 If the cursor is not in an item, throw an error."
485 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
486 (limit (save-excursion (outline-next-heading) (point)))
488 (org-beginning-of-item)
489 (skip-chars-forward " \t")
493 (beginning-of-line 2)
494 (if (eobp) (throw 'exit
(point)))
495 (if (>= (point) limit
) (throw 'exit
(point-at-bol)))
496 (if (looking-at "[ \t]*$")
497 (setq ind1 ind-empty
)
498 (skip-chars-forward " \t")
499 (setq ind1
(current-column)))
501 (throw 'exit
(point-at-bol)))))))
505 (error "Not in an item"))))
507 (defun org-next-item ()
508 "Move to the beginning of the next item in the current plain list.
509 Error if not at a plain list, or if this is the last item in the list."
511 (let (ind ind1
(pos (point)))
512 (org-beginning-of-item)
513 (setq ind
(org-get-indentation))
515 (setq ind1
(org-get-indentation))
516 (unless (and (org-at-item-p) (= ind ind1
))
518 (error "On last item"))))
520 (defun org-previous-item ()
521 "Move to the beginning of the previous item in the current plain list.
522 Error if not at a plain list, or if this is the first item in the list."
524 (let (beg ind ind1
(pos (point)))
525 (org-beginning-of-item)
527 (setq ind
(org-get-indentation))
531 (beginning-of-line 0)
532 (if (looking-at "[ \t]*$")
534 (if (<= (setq ind1
(org-get-indentation)) ind
)
537 (if (or (not (org-at-item-p))
540 (org-beginning-of-item))
541 (error (goto-char pos
)
542 (error "On first item")))))
544 (defun org-first-list-item-p ()
545 "Is this heading the item in a plain list?"
546 (unless (org-at-item-p)
547 (error "Not at a plain list item"))
548 (org-beginning-of-item)
549 (= (point) (save-excursion (org-beginning-of-item-list))))
551 (defun org-move-item-down ()
552 "Move the plain list item at point down, i.e. swap with following item.
553 Subitems (items with larger indentation) are considered part of the item,
554 so this really moves item trees."
556 (let ((col (current-column))
558 beg beg0 end end0 ind ind1 txt ne-end ne-beg
)
559 (org-beginning-of-item)
562 (setq ne-beg
(org-back-over-empty-lines))
565 (setq ind
(org-get-indentation))
568 (setq ind1
(org-get-indentation))
569 (setq ne-end
(org-back-over-empty-lines))
572 (when (and (org-first-list-item-p) (< ne-end ne-beg
))
573 ;; include less whitespace
576 (forward-line (- ne-beg ne-end
))
579 (if (and (org-at-item-p) (= ind ind1
))
582 (org-back-over-empty-lines)
583 (setq txt
(buffer-substring beg end
))
585 (delete-region beg end
))
588 (goto-char pos
) (org-skip-whitespace)
589 (org-maybe-renumber-ordered-list)
590 (move-to-column col
))
593 (error "Cannot move this item further down"))))
595 (defun org-move-item-up (arg)
596 "Move the plain list item at point up, i.e. swap with previous item.
597 Subitems (items with larger indentation) are considered part of the item,
598 so this really moves item trees."
600 (let ((col (current-column)) (pos (point))
601 beg beg0 end ind ind1 txt
602 ne-beg ne-ins ins-end
)
603 (org-beginning-of-item)
605 (setq ind
(org-get-indentation))
607 (setq ne-beg
(org-back-over-empty-lines))
611 (org-back-over-empty-lines)
616 (beginning-of-line 0)
617 (if (looking-at "[ \t]*$")
618 (if org-empty-line-terminates-plain-lists
621 (error "Cannot move this item further up"))
623 (if (<= (setq ind1
(org-get-indentation)) ind
)
626 (org-beginning-of-item)
627 (error (goto-char beg0
)
629 (error "Cannot move this item further up")))
630 (setq ind1
(org-get-indentation))
631 (if (and (org-at-item-p) (= ind ind1
))
633 (setq ne-ins
(org-back-over-empty-lines))
634 (setq txt
(buffer-substring beg end
))
636 (delete-region beg end
))
639 (setq ins-end
(point))
640 (goto-char pos
) (org-skip-whitespace)
642 (when (and (org-first-list-item-p) (> ne-ins ne-beg
))
643 ;; Move whitespace back to beginning
646 (let ((kill-whole-line t
))
647 (kill-line (- ne-ins ne-beg
)) (point)))
648 (insert (make-string (- ne-ins ne-beg
) ?
\n)))
650 (org-maybe-renumber-ordered-list)
651 (move-to-column col
))
654 (error "Cannot move this item further up"))))
656 (defun org-maybe-renumber-ordered-list ()
657 "Renumber the ordered list at point if setup allows it.
658 This tests the user option `org-auto-renumber-ordered-lists' before
659 doing the renumbering."
661 (when (and org-auto-renumber-ordered-lists
663 (if (match-beginning 3)
664 (org-renumber-ordered-list 1)
665 (org-fix-bullet-type))))
667 (defun org-maybe-renumber-ordered-list-safe ()
670 (org-maybe-renumber-ordered-list))
673 (defun org-cycle-list-bullet (&optional which
)
674 "Cycle through the different itemize/enumerate bullets.
675 This cycle the entire list level through the sequence:
677 `-' -> `+' -> `*' -> `1.' -> `1)'
679 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
680 0 means `-', 1 means `+' etc."
683 (org-beginning-of-item-list)
685 (beginning-of-line 1)
686 (let ((current (match-string 0))
687 (prevp (eq which
'previous
))
690 ((and (numberp which
)
691 (nth (1- which
) '("-" "+" "*" "1." "1)"))))
692 ((string-match "-" current
) (if prevp
"1)" "+"))
693 ((string-match "\\+" current
)
694 (if prevp
"-" (if (looking-at "\\S-") "1." "*")))
695 ((string-match "\\*" current
) (if prevp
"+" "1."))
696 ((string-match "\\." current
)
697 (if prevp
(if (looking-at "\\S-") "+" "*") "1)"))
698 ((string-match ")" current
) (if prevp
"1." "-"))
699 (t (error "This should not happen"))))
700 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
701 (setq old
(match-string 2))
702 (replace-match (concat "\\1" new
)))
703 (org-shift-item-indentation (- (length new
) (length old
)))
704 (org-fix-bullet-type)
705 (org-maybe-renumber-ordered-list))))
707 (defun org-get-string-indentation (s)
708 "What indentation has S due to SPACE and TAB at the beginning of the string?"
709 (let ((n -
1) (i 0) (w tab-width
) c
)
711 (while (< (setq n
(1+ n
)) (length s
))
713 (cond ((= c ?\
) (setq i
(1+ i
)))
714 ((= c ?
\t) (setq i
(* (/ (+ w i
) w
) w
)))
715 (t (throw 'exit t
)))))
718 (defun org-renumber-ordered-list (arg)
719 "Renumber an ordered plain list.
720 Cursor needs to be in the first line of an item, the line that starts
721 with something like \"1.\" or \"2)\"."
723 (unless (and (org-at-item-p)
725 (error "This is not an ordered list"))
726 (let ((line (org-current-line))
727 (col (current-column))
728 (ind (org-get-string-indentation
729 (buffer-substring (point-at-bol) (match-beginning 3))))
730 ;; (term (substring (match-string 3) -1))
733 ;; find where this list begins
734 (org-beginning-of-item-list)
736 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
737 (setq fmt
(concat "%d" (or (match-string 1) ".")))
738 (beginning-of-line 0)
739 ;; walk forward and replace these numbers
743 (if bobp
(setq bobp nil
) (beginning-of-line 2))
744 (if (eobp) (throw 'exit nil
))
745 (if (looking-at "[ \t]*$") (throw 'next nil
))
746 (skip-chars-forward " \t") (setq ind1
(current-column))
747 (if (> ind1 ind
) (throw 'next t
))
748 (if (< ind1 ind
) (throw 'exit t
))
749 (if (not (org-at-item-p)) (throw 'exit nil
))
750 (setq old
(match-string 2))
751 (delete-region (match-beginning 2) (match-end 2))
752 (goto-char (match-beginning 2))
753 (insert (setq new
(format fmt
(setq n
(1+ n
)))))
754 (org-shift-item-indentation (- (length new
) (length old
))))))
756 (org-move-to-column col
)))
758 (defun org-fix-bullet-type ()
759 "Make sure all items in this list have the same bullet as the first item.
760 Also, fix the indentation."
762 (unless (org-at-item-p) (error "This is not a list"))
763 (let ((line (org-current-line))
764 (col (current-column))
765 (ind (current-indentation))
766 ind1 bullet oldbullet
)
767 ;; find where this list begins
768 (org-beginning-of-item-list)
769 (beginning-of-line 1)
770 ;; find out what the bullet type is
771 (looking-at "[ \t]*\\(\\S-+\\)")
772 (setq bullet
(concat (match-string 1) " "))
773 (if (and org-list-two-spaces-after-bullet-regexp
774 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
775 (setq bullet
(concat bullet
" ")))
776 ;; walk forward and replace these numbers
777 (beginning-of-line 0)
781 (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 (skip-chars-forward " \t")
789 (looking-at "\\S-+ *")
790 (setq oldbullet
(match-string 0))
791 (unless (equal bullet oldbullet
) (replace-match bullet
))
792 (org-shift-item-indentation (- (length bullet
) (length oldbullet
))))))
794 (org-move-to-column col
)
795 (if (string-match "[0-9]" bullet
)
796 (org-renumber-ordered-list 1))))
798 (defun org-shift-item-indentation (delta)
799 "Shift the indentation in current item by DELTA."
801 (let ((beg (point-at-bol))
802 (end (progn (org-end-of-item) (point)))
805 (beginning-of-line 0)
806 (while (> (point) beg
)
807 (when (looking-at "[ \t]*\\S-")
808 ;; this is not an empty line
809 (setq i
(org-get-indentation))
810 (if (and (> i
0) (> (setq i
(+ i delta
)) 0))
812 (beginning-of-line 0)))))
814 (defun org-beginning-of-item-list ()
815 "Go to the beginning of the current item list.
816 I.e. to the first item in this list."
818 (org-beginning-of-item)
819 (let ((pos (point-at-bol))
820 (ind (org-get-indentation))
822 ;; find where this list begins
826 (beginning-of-line 0)
827 (if (looking-at "[ \t]*$")
828 (throw (if (bobp) 'exit
'next
) t
))
829 (skip-chars-forward " \t") (setq ind1
(current-column))
832 (not (org-at-item-p)))
833 (and (= (point-at-bol) (point-min))
834 (setq pos
(point-min))))
836 (when (org-at-item-p) (setq pos
(point-at-bol)))))))
840 (defun org-end-of-item-list ()
841 "Go to the end of the current item list.
842 I.e. to the text after the last item."
844 (org-beginning-of-item)
845 (let ((pos (point-at-bol))
846 (ind (org-get-indentation))
848 ;; find where this list begins
852 (beginning-of-line 2)
853 (if (looking-at "[ \t]*$")
854 (throw (if (eobp) 'exit
'next
) t
))
855 (skip-chars-forward " \t") (setq ind1
(current-column))
858 (not (org-at-item-p)))
861 (setq pos
(point-at-bol))
866 (defvar org-last-indent-begin-marker
(make-marker))
867 (defvar org-last-indent-end-marker
(make-marker))
869 (defun org-outdent-item (arg)
870 "Outdent a local list item."
872 (org-indent-item (- arg
)))
874 (defun org-indent-item (arg)
875 "Indent a local list item."
877 (and (org-region-active-p) (org-cursor-to-region-beginning))
878 (unless (org-at-item-p)
879 (error "Not on an item"))
881 (let (beg end ind ind1 tmp delta ind-down ind-up
)
882 (setq end
(and (org-region-active-p) (region-end)))
883 (if (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
884 (setq beg org-last-indent-begin-marker
885 end org-last-indent-end-marker
)
886 (org-beginning-of-item)
887 (setq beg
(move-marker org-last-indent-begin-marker
(point)))
889 (setq end
(move-marker org-last-indent-end-marker
(or end
(point)))))
891 (setq tmp
(org-item-indent-positions)
896 (if ind-down
(- ind-down ind
) 2)
897 (if ind-up
(- ind-up ind
) -
2)))
898 (if (< (+ delta ind
) 0) (error "Cannot outdent beyond margin"))
899 (while (< (point) end
)
900 (beginning-of-line 1)
901 (skip-chars-forward " \t") (setq ind1
(current-column))
902 (delete-region (point-at-bol) (point))
903 (or (eolp) (org-indent-to-column (+ ind1 delta
)))
904 (beginning-of-line 2))))
905 (org-fix-bullet-type)
906 (org-maybe-renumber-ordered-list-safe)
908 (beginning-of-line 0)
909 (condition-case nil
(org-beginning-of-item) (error nil
))
910 (org-maybe-renumber-ordered-list-safe)))
912 (defun org-item-indent-positions ()
913 "Return indentation for plain list items.
914 This returns a list with three values: The current indentation, the
915 parent indentation and the indentation a child should have.
916 Assumes cursor in item line."
917 (let* ((bolpos (point-at-bol))
918 (ind (org-get-indentation))
921 (org-beginning-of-item-list)
922 (skip-chars-backward "\n\r \t")
923 (when (org-in-item-p)
924 (org-beginning-of-item)
925 (setq ind-up
(org-get-indentation))))
929 ((and (condition-case nil
(progn (org-previous-item) t
)
931 (or (forward-char 1) t
)
932 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t
))
933 (setq ind-down
(org-get-indentation)))
934 ((and (goto-char pos
)
936 (goto-char (match-end 0))
937 (skip-chars-forward " \t")
938 (setq ind-down
(current-column)))))
939 (list ind ind-up ind-down
)))
942 ;;; Send and receive lists
944 (defun org-list-parse-list (&optional delete
)
945 "Parse the list at point and maybe DELETE it.
946 Return a list containing first level items as strings and
947 sublevels as a list of strings."
948 (let* ((item-beginning (org-list-item-beginning))
949 (start (car item-beginning
))
950 (end (org-list-end (cdr item-beginning
)))
951 output itemsep ltype
)
952 (while (re-search-forward org-list-beginning-re end t
)
953 (goto-char (match-beginning 3))
955 (cond ((string-match "[0-9]" (match-string 2))
956 (setq itemsep
"[0-9]+\\(?:\\.\\|)\\)"
958 ((string-match "^.*::" (match-string 0))
959 (setq itemsep
"[-+]" ltype
'descriptive
))
960 (t (setq itemsep
"[-+]" ltype
'unordered
))))
961 (let* ((indent1 (match-string 1))
962 (nextitem (save-excursion
964 (or (and (re-search-forward
965 (concat "^" indent1 itemsep
" *?") end t
)
966 (match-beginning 0)) end
))))
967 (item (buffer-substring
969 (or (and (re-search-forward
970 org-list-beginning-re end t
)
971 (goto-char (match-beginning 0)))
973 (nextindent (match-string 1))
974 (item (org-trim item
))
975 (item (if (string-match "^\\[\\([xX ]\\)\\]" item
)
976 (replace-match (if (equal (match-string 1 item
) " ")
982 (when (> (length nextindent
)
984 (narrow-to-region (point) nextitem
)
985 (push (org-list-parse-list) output
)
987 (when delete
(delete-region start end
))
988 (setq output
(nreverse output
))
989 (push ltype output
)))
991 (defun org-list-item-beginning ()
992 "Find the beginning of the list item.
993 Return a cons which car is the beginning position of the item and
994 cdr is the indentation string."
996 (if (not (or (looking-at org-list-beginning-re
)
998 org-list-beginning-re nil t
)))
999 (progn (goto-char (point-min)) (point))
1000 (cons (match-beginning 0) (match-string 1)))))
1002 (defun org-list-end (indent)
1003 "Return the position of the end of the list.
1004 INDENT is the indentation of the list."
1007 (while (or (looking-at org-list-beginning-re
)
1008 (looking-at (concat "^" indent
"[ \t]+\\|^$")))
1009 (if (eq (point) (point-max))
1010 (throw 'exit
(point-max)))
1011 (forward-line 1))) (point)))
1013 (defun org-list-insert-radio-list ()
1014 "Insert a radio list template appropriate for this major mode."
1016 (let* ((e (assq major-mode org-list-radio-list-templates
))
1019 (unless e
(error "No radio list setup defined for %s" major-mode
))
1020 (setq name
(read-string "List name: "))
1021 (while (string-match "%n" txt
)
1022 (setq txt
(replace-match name t t txt
)))
1023 (or (bolp) (insert "\n"))
1028 (defun org-list-send-list (&optional maybe
)
1029 "Send a tranformed version of this list to the receiver position.
1030 With argument MAYBE, fail quietly if no transformation is defined for
1034 (unless (org-at-item-p) (error "Not at a list"))
1036 (goto-char (car (org-list-item-beginning)))
1037 (beginning-of-line 0)
1038 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1041 (error "Don't know how to transform this list"))))
1042 (let* ((name (match-string 1))
1043 (item-beginning (org-list-item-beginning))
1044 (transform (intern (match-string 2)))
1045 (txt (buffer-substring-no-properties
1046 (car item-beginning
)
1047 (org-list-end (cdr item-beginning
))))
1048 (list (org-list-parse-list))
1050 (unless (fboundp transform
)
1051 (error "No such transformation function %s" transform
))
1052 (setq txt
(funcall transform list
))
1053 ;; Find the insertion place
1055 (goto-char (point-min))
1056 (unless (re-search-forward
1057 (concat "BEGIN RECEIVE ORGLST +" name
"\\([ \t]\\|$\\)") nil t
)
1058 (error "Don't know where to insert translated list"))
1059 (goto-char (match-beginning 0))
1060 (beginning-of-line 2)
1062 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
1063 (error "Cannot find end of insertion region"))
1064 (beginning-of-line 1)
1065 (delete-region beg
(point))
1068 (message "List converted and installed at receiver location"))))
1070 (defun org-list-to-generic (list params
)
1071 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1073 Valid parameters PARAMS are
1075 :ustart String to start an unordered list
1076 :uend String to end an unordered list
1078 :ostart String to start an ordered list
1079 :oend String to end an ordered list
1081 :dstart String to start a descriptive list
1082 :dend String to end a descriptive list
1083 :dtstart String to start a descriptive term
1084 :dtend String to end a descriptive term
1085 :ddstart String to start a description
1086 :ddend String to end a description
1088 :splice When set to t, return only list body lines, don't wrap
1089 them into :[u/o]start and :[u/o]end. Default is nil.
1091 :istart String to start a list item
1092 :iend String to end a list item
1093 :isep String to separate items
1094 :lsep String to separate sublists
1096 :cboff String to insert for an unchecked checkbox
1097 :cbon String to insert for a checked checkbox"
1099 (let* ((p params
) sublist
1100 (splicep (plist-get p
:splice
))
1101 (ostart (plist-get p
:ostart
))
1102 (oend (plist-get p
:oend
))
1103 (ustart (plist-get p
:ustart
))
1104 (uend (plist-get p
:uend
))
1105 (dstart (plist-get p
:dstart
))
1106 (dend (plist-get p
:dend
))
1107 (dtstart (plist-get p
:dtstart
))
1108 (dtend (plist-get p
:dtend
))
1109 (ddstart (plist-get p
:ddstart
))
1110 (ddend (plist-get p
:ddend
))
1111 (istart (plist-get p
:istart
))
1112 (iend (plist-get p
:iend
))
1113 (isep (plist-get p
:isep
))
1114 (lsep (plist-get p
:lsep
))
1115 (cbon (plist-get p
:cbon
))
1116 (cboff (plist-get p
:cboff
)))
1118 (cond ((eq (car list
) 'ordered
)
1119 (concat ostart
"\n%s" oend
"\n"))
1120 ((eq (car list
) 'unordered
)
1121 (concat ustart
"\n%s" uend
"\n"))
1122 ((eq (car list
) 'descriptive
)
1123 (concat dstart
"\n%s" dend
"\n"))))
1124 rtn term defstart defend
)
1125 (while (setq sublist
(pop list
))
1126 (cond ((symbolp sublist
) nil
)
1128 (when (string-match "^\\(.*\\) ::" sublist
)
1129 (setq term
(org-trim (format (concat dtstart
"%s" dtend
)
1130 (match-string 1 sublist
))))
1131 (setq sublist
(substring sublist
(1+ (length term
)))))
1132 (if (string-match "\\[CBON\\]" sublist
)
1133 (setq sublist
(replace-match cbon t t sublist
)))
1134 (if (string-match "\\[CBOFF\\]" sublist
)
1135 (setq sublist
(replace-match cboff t t sublist
)))
1136 (setq rtn
(concat rtn istart term ddstart
1137 sublist ddend iend isep
)))
1138 (t (setq rtn
(concat rtn
;; previous list
1139 lsep
;; list separator
1140 (org-list-to-generic sublist p
)
1141 lsep
;; list separator
1143 (format wrapper rtn
))))
1145 (defun org-list-to-latex (list &optional params
)
1146 "Convert LIST into a LaTeX list.
1147 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1148 with overruling parameters for `org-list-to-generic'."
1149 (org-list-to-generic
1152 '(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
1153 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
1154 :dstart
"\\begin{description}" :dend
"\\end{description}"
1155 :dtstart
"[" :dtend
"]"
1156 :ddstart
"" :ddend
""
1157 :istart
"\\item " :iend
""
1158 :isep
"\n" :lsep
"\n"
1159 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
1162 (defun org-list-to-html (list &optional params
)
1163 "Convert LIST into a HTML list.
1164 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1165 with overruling parameters for `org-list-to-generic'."
1166 (org-list-to-generic
1169 '(:splicep nil
:ostart
"<ol>" :oend
"</ol>"
1170 :ustart
"<ul>" :uend
"</ul>"
1171 :dstart
"<dl>" :dend
"</dl>"
1172 :dtstart
"<dt>" :dtend
"</dt>"
1173 :ddstart
"<dd>" :ddend
"</dd>"
1174 :istart
"<li>" :iend
"</li>"
1175 :isep
"\n" :lsep
"\n"
1176 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>")
1179 (defun org-list-to-texinfo (list &optional params
)
1180 "Convert LIST into a Texinfo list.
1181 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1182 with overruling parameters for `org-list-to-generic'."
1183 (org-list-to-generic
1186 '(:splicep nil
:ostart
"@itemize @minus" :oend
"@end itemize"
1187 :ustart
"@enumerate" :uend
"@end enumerate"
1188 :dstart
"@table" :dend
"@end table"
1189 :dtstart
"@item " :dtend
"\n"
1190 :ddstart
"" :ddend
""
1191 :istart
"@item\n" :iend
""
1192 :isep
"\n" :lsep
"\n"
1193 :cbon
"@code{[X]}" :cboff
"@code{[ ]}")
1198 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1199 ;;; org-list.el ends here