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 (cdr (assq 'plain-list-item org-blank-before-new-entry
)))
213 (blank (if (eq blank-a
'auto
) empty-line-p blank-a
))
215 (if descp
(setq checkbox nil
))
217 (progn (org-timer-item) t
)
219 ((and (org-at-item-p) (<= (point) eow
))
221 (beginning-of-line 1)
222 (open-line (if blank
2 1)))
224 (beginning-of-line 1))
226 (unless (org-get-alist-option org-M-RET-may-split-line
'item
)
228 (delete-horizontal-space))
229 (newline (if blank
2 1))))
231 (if checkbox
"[ ]" "")
232 (if descp
(concat (if checkbox
" " "")
233 (read-string "Term: ") " :: ") ""))
237 (unless (= (point) pos
) (just-one-space) (backward-delete-char 1)))
238 (org-maybe-renumber-ordered-list)
239 (and checkbox
(org-update-checkbox-count-maybe))
244 (defun org-at-item-checkbox-p ()
245 "Is point at a line starting a plain-list item with a checklet?"
248 (goto-char (match-end 0))
249 (skip-chars-forward " \t")
250 (looking-at "\\[[- X]\\]"))))
252 (defun org-toggle-checkbox (&optional toggle-presence
)
253 "Toggle the checkbox in the current line.
254 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
255 With double prefix, set checkbox to [-].
256 When there is an active region, toggle status or presence of the checkbox
257 in the first line, and make every item in the region have the same
258 status or presence, respectively.
259 If the cursor is in a headline, apply this to all checkbox items in the
260 text below the heading."
263 (let (beg end status first-present first-status blocked
)
265 ((org-region-active-p)
266 (setq beg
(region-beginning) end
(region-end)))
268 (setq beg
(point) end
(save-excursion (outline-next-heading) (point))))
269 ((org-at-item-checkbox-p)
271 (if (equal toggle-presence
'(4))
274 (goto-char (match-beginning 0))
276 (when (setq blocked
(org-checkbox-blocked-p))
277 (error "Checkbox blocked because of unchecked box in line %d"
280 (cond ((equal toggle-presence
'(16)) "[-]")
281 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
288 (goto-char (match-end 0))
291 (t (error "Not at a checkbox or heading, and no active region")))
292 (setq end
(move-marker (make-marker) end
))
295 (setq first-present
(org-at-item-checkbox-p)
298 (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t
)
299 (equal (match-string 1) "[X]"))))
300 (while (< (point) end
)
303 ((and first-present
(org-at-item-checkbox-p))
306 (goto-char (match-beginning 0))
308 ((and (not first-present
) (not (org-at-item-checkbox-p))
311 (goto-char (match-end 0))
313 (when (org-at-item-checkbox-p)
314 (setq status
(equal (match-string 0) "[X]"))
316 (if first-status
"[ ]" "[X]") t t
)))
317 (beginning-of-line 2)))))
318 (org-update-checkbox-count-maybe))
320 (defun org-checkbox-blocked-p ()
321 "Is the current checkbox blocked from for being checked now?
322 A checkbox is blocked if all of the following conditions are fulfilled:
324 1. The checkbox is not checked already.
325 2. The current entry has the ORDERED property set.
326 3. There is an unchecked checkbox in this entry before the current line."
330 (unless (org-at-item-checkbox-p) (throw 'exit nil
))
331 (when (equal (match-string 0) "[X]")
332 ;; the box is already checked!
334 (let ((end (point-at-bol)))
335 (condition-case nil
(org-back-to-heading t
)
336 (error (throw 'exit nil
)))
337 (unless (org-entry-get nil
"ORDERED") (throw 'exit nil
))
338 (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t
)
342 (defun org-update-checkbox-count-maybe ()
343 "Update checkbox statistics unless turned off by user."
344 (when org-provide-checkbox-statistics
345 (org-update-checkbox-count)))
347 (defun org-update-checkbox-count (&optional all
)
348 "Update the checkbox statistics in the current section.
349 This will find all statistic cookies like [57%] and [6/12] and update them
350 with the current numbers. With optional prefix argument ALL, do this for
354 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
355 (beg (condition-case nil
356 (progn (org-back-to-heading) (point))
357 (error (point-min))))
358 (end (move-marker (make-marker)
359 (progn (outline-next-heading) (point))))
360 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
361 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
362 (re-find (concat re
"\\|" re-box
))
363 beg-cookie end-cookie is-percent c-on c-off lim
364 eline curr-ind next-ind continue-from startsearch
368 (goto-char (point-min))
369 (outline-next-heading)
370 (setq beg
(point) end
(point-max)))
372 ;; find each statistic cookie
373 (while (re-search-backward re-find beg t
)
374 (setq beg-cookie
(match-beginning 1)
375 end-cookie
(match-end 1)
376 cstat
(+ cstat
(if end-cookie
1 0))
377 startsearch
(point-at-eol)
378 continue-from
(match-beginning 0)
379 is-percent
(match-beginning 2)
381 ((org-on-heading-p) (outline-next-heading) (point))
382 ((org-at-item-p) (org-end-of-item) (point))
387 ;; find first checkbox for this cookie and gather
388 ;; statistics from all that are at this indentation level
389 (goto-char startsearch
)
390 (if (re-search-forward re-box lim t
)
392 (org-beginning-of-item)
393 (setq curr-ind
(org-get-indentation))
394 (setq next-ind curr-ind
)
395 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind
))
396 (save-excursion (end-of-line) (setq eline
(point)))
397 (if (re-search-forward re-box eline t
)
398 (if (member (match-string 2) '("[ ]" "[-]"))
399 (setq c-off
(1+ c-off
))
400 (setq c-on
(1+ c-on
))
404 (setq next-ind
(org-get-indentation))
406 (goto-char continue-from
)
409 (delete-region beg-cookie end-cookie
)
410 (goto-char beg-cookie
)
413 (format "[%d%%]" (/ (* 100 c-on
) (max 1 (+ c-on c-off
))))
414 (format "[%d/%d]" c-on
(+ c-on c-off
)))))
415 ;; update items checkbox if it has one
416 (when (org-at-item-p)
417 (org-beginning-of-item)
418 (when (and (> (+ c-on c-off
) 0)
419 (re-search-forward re-box
(point-at-eol) t
))
420 (setq beg-cookie
(match-beginning 2)
421 end-cookie
(match-end 2))
422 (delete-region beg-cookie end-cookie
)
423 (goto-char beg-cookie
)
424 (cond ((= c-off
0) (insert "[X]"))
425 ((= c-on
0) (insert "[ ]"))
428 (goto-char continue-from
))
429 (when (interactive-p)
430 (message "Checkbox statistics updated %s (%d places)"
431 (if all
"in entire file" "in current outline entry") cstat
)))))
433 (defun org-get-checkbox-statistics-face ()
434 "Select the face for checkbox statistics.
435 The face will be `org-done' when all relevant boxes are checked. Otherwise
436 it will be `org-todo'."
438 (if (equal (match-string 1) "100%") 'org-done
'org-todo
)
439 (if (and (> (match-end 2) (match-beginning 2))
440 (equal (match-string 2) (match-string 3)))
444 (defun org-beginning-of-item ()
445 "Go to the beginning of the current hand-formatted item.
446 If the cursor is not in an item, throw an error."
449 (limit (save-excursion
452 (org-back-to-heading)
453 (beginning-of-line 2) (point))
454 (error (point-min)))))
455 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
458 (beginning-of-line 1)
459 (beginning-of-line 1)
460 (skip-chars-forward " \t")
461 (setq ind
(current-column))
464 (beginning-of-line 0)
465 (if (or (bobp) (< (point) limit
)) (throw 'exit nil
))
467 (if (looking-at "[ \t]*$")
468 (setq ind1 ind-empty
)
469 (skip-chars-forward " \t")
470 (setq ind1
(current-column)))
472 (progn (beginning-of-line 1) (throw 'exit
(org-at-item-p))))))
475 (error "Not in an item")))))
477 (defun org-end-of-item ()
478 "Go to the end of the current hand-formatted item.
479 If the cursor is not in an item, throw an error."
483 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
484 (limit (save-excursion (outline-next-heading) (point)))
486 (org-beginning-of-item)
487 (skip-chars-forward " \t")
491 (beginning-of-line 2)
492 (if (eobp) (throw 'exit
(point)))
493 (if (>= (point) limit
) (throw 'exit
(point-at-bol)))
494 (if (looking-at "[ \t]*$")
495 (setq ind1 ind-empty
)
496 (skip-chars-forward " \t")
497 (setq ind1
(current-column)))
499 (throw 'exit
(point-at-bol)))))))
503 (error "Not in an item"))))
505 (defun org-next-item ()
506 "Move to the beginning of the next item in the current plain list.
507 Error if not at a plain list, or if this is the last item in the list."
509 (let (ind ind1
(pos (point)))
510 (org-beginning-of-item)
511 (setq ind
(org-get-indentation))
513 (setq ind1
(org-get-indentation))
514 (unless (and (org-at-item-p) (= ind ind1
))
516 (error "On last item"))))
518 (defun org-previous-item ()
519 "Move to the beginning of the previous item in the current plain list.
520 Error if not at a plain list, or if this is the first item in the list."
522 (let (beg ind ind1
(pos (point)))
523 (org-beginning-of-item)
525 (setq ind
(org-get-indentation))
529 (beginning-of-line 0)
530 (if (looking-at "[ \t]*$")
532 (if (<= (setq ind1
(org-get-indentation)) ind
)
535 (if (or (not (org-at-item-p))
538 (org-beginning-of-item))
539 (error (goto-char pos
)
540 (error "On first item")))))
542 (defun org-first-list-item-p ()
543 "Is this heading the item in a plain list?"
544 (unless (org-at-item-p)
545 (error "Not at a plain list item"))
546 (org-beginning-of-item)
547 (= (point) (save-excursion (org-beginning-of-item-list))))
549 (defun org-move-item-down ()
550 "Move the plain list item at point down, i.e. swap with following item.
551 Subitems (items with larger indentation) are considered part of the item,
552 so this really moves item trees."
554 (let ((col (current-column))
556 beg beg0 end end0 ind ind1 txt ne-end ne-beg
)
557 (org-beginning-of-item)
560 (setq ne-beg
(org-back-over-empty-lines))
563 (setq ind
(org-get-indentation))
566 (setq ind1
(org-get-indentation))
567 (setq ne-end
(org-back-over-empty-lines))
570 (when (and (org-first-list-item-p) (< ne-end ne-beg
))
571 ;; include less whitespace
574 (forward-line (- ne-beg ne-end
))
577 (if (and (org-at-item-p) (= ind ind1
))
580 (org-back-over-empty-lines)
581 (setq txt
(buffer-substring beg end
))
583 (delete-region beg end
))
586 (goto-char pos
) (org-skip-whitespace)
587 (org-maybe-renumber-ordered-list)
588 (move-to-column col
))
591 (error "Cannot move this item further down"))))
593 (defun org-move-item-up (arg)
594 "Move the plain list item at point up, i.e. swap with previous item.
595 Subitems (items with larger indentation) are considered part of the item,
596 so this really moves item trees."
598 (let ((col (current-column)) (pos (point))
599 beg beg0 end ind ind1 txt
600 ne-beg ne-ins ins-end
)
601 (org-beginning-of-item)
603 (setq ind
(org-get-indentation))
605 (setq ne-beg
(org-back-over-empty-lines))
609 (org-back-over-empty-lines)
614 (beginning-of-line 0)
615 (if (looking-at "[ \t]*$")
616 (if org-empty-line-terminates-plain-lists
619 (error "Cannot move this item further up"))
621 (if (<= (setq ind1
(org-get-indentation)) ind
)
624 (org-beginning-of-item)
625 (error (goto-char beg0
)
627 (error "Cannot move this item further up")))
628 (setq ind1
(org-get-indentation))
629 (if (and (org-at-item-p) (= ind ind1
))
631 (setq ne-ins
(org-back-over-empty-lines))
632 (setq txt
(buffer-substring beg end
))
634 (delete-region beg end
))
637 (setq ins-end
(point))
638 (goto-char pos
) (org-skip-whitespace)
640 (when (and (org-first-list-item-p) (> ne-ins ne-beg
))
641 ;; Move whitespace back to beginning
644 (let ((kill-whole-line t
))
645 (kill-line (- ne-ins ne-beg
)) (point)))
646 (insert (make-string (- ne-ins ne-beg
) ?
\n)))
648 (org-maybe-renumber-ordered-list)
649 (move-to-column col
))
652 (error "Cannot move this item further up"))))
654 (defun org-maybe-renumber-ordered-list ()
655 "Renumber the ordered list at point if setup allows it.
656 This tests the user option `org-auto-renumber-ordered-lists' before
657 doing the renumbering."
659 (when (and org-auto-renumber-ordered-lists
661 (if (match-beginning 3)
662 (org-renumber-ordered-list 1)
663 (org-fix-bullet-type))))
665 (defun org-maybe-renumber-ordered-list-safe ()
668 (org-maybe-renumber-ordered-list))
671 (defun org-cycle-list-bullet (&optional which
)
672 "Cycle through the different itemize/enumerate bullets.
673 This cycle the entire list level through the sequence:
675 `-' -> `+' -> `*' -> `1.' -> `1)'
677 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
678 0 means `-', 1 means `+' etc."
681 (org-beginning-of-item-list)
683 (beginning-of-line 1)
684 (let ((current (match-string 0))
685 (prevp (eq which
'previous
))
688 ((and (numberp which
)
689 (nth (1- which
) '("-" "+" "*" "1." "1)"))))
690 ((string-match "-" current
) (if prevp
"1)" "+"))
691 ((string-match "\\+" current
)
692 (if prevp
"-" (if (looking-at "\\S-") "1." "*")))
693 ((string-match "\\*" current
) (if prevp
"+" "1."))
694 ((string-match "\\." current
)
695 (if prevp
(if (looking-at "\\S-") "+" "*") "1)"))
696 ((string-match ")" current
) (if prevp
"1." "-"))
697 (t (error "This should not happen"))))
698 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
699 (setq old
(match-string 2))
700 (replace-match (concat "\\1" new
)))
701 (org-shift-item-indentation (- (length new
) (length old
)))
702 (org-fix-bullet-type)
703 (org-maybe-renumber-ordered-list))))
705 (defun org-get-string-indentation (s)
706 "What indentation has S due to SPACE and TAB at the beginning of the string?"
707 (let ((n -
1) (i 0) (w tab-width
) c
)
709 (while (< (setq n
(1+ n
)) (length s
))
711 (cond ((= c ?\
) (setq i
(1+ i
)))
712 ((= c ?
\t) (setq i
(* (/ (+ w i
) w
) w
)))
713 (t (throw 'exit t
)))))
716 (defun org-renumber-ordered-list (arg)
717 "Renumber an ordered plain list.
718 Cursor needs to be in the first line of an item, the line that starts
719 with something like \"1.\" or \"2)\"."
721 (unless (and (org-at-item-p)
723 (error "This is not an ordered list"))
724 (let ((line (org-current-line))
725 (col (current-column))
726 (ind (org-get-string-indentation
727 (buffer-substring (point-at-bol) (match-beginning 3))))
728 ;; (term (substring (match-string 3) -1))
731 ;; find where this list begins
732 (org-beginning-of-item-list)
734 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
735 (setq fmt
(concat "%d" (or (match-string 1) ".")))
736 (beginning-of-line 0)
737 ;; walk forward and replace these numbers
741 (if bobp
(setq bobp nil
) (beginning-of-line 2))
742 (if (eobp) (throw 'exit nil
))
743 (if (looking-at "[ \t]*$") (throw 'next nil
))
744 (skip-chars-forward " \t") (setq ind1
(current-column))
745 (if (> ind1 ind
) (throw 'next t
))
746 (if (< ind1 ind
) (throw 'exit t
))
747 (if (not (org-at-item-p)) (throw 'exit nil
))
748 (setq old
(match-string 2))
749 (delete-region (match-beginning 2) (match-end 2))
750 (goto-char (match-beginning 2))
751 (insert (setq new
(format fmt
(setq n
(1+ n
)))))
752 (org-shift-item-indentation (- (length new
) (length old
))))))
754 (org-move-to-column col
)))
756 (defun org-fix-bullet-type ()
757 "Make sure all items in this list have the same bullet as the first item.
758 Also, fix the indentation."
760 (unless (org-at-item-p) (error "This is not a list"))
761 (let ((line (org-current-line))
762 (col (current-column))
763 (ind (current-indentation))
764 ind1 bullet oldbullet
)
765 ;; find where this list begins
766 (org-beginning-of-item-list)
767 (beginning-of-line 1)
768 ;; find out what the bullet type is
769 (looking-at "[ \t]*\\(\\S-+\\)")
770 (setq bullet
(concat (match-string 1) " "))
771 (if (and org-list-two-spaces-after-bullet-regexp
772 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
773 (setq bullet
(concat bullet
" ")))
774 ;; walk forward and replace these numbers
775 (beginning-of-line 0)
779 (beginning-of-line 2)
780 (if (eobp) (throw 'exit nil
))
781 (if (looking-at "[ \t]*$") (throw 'next nil
))
782 (skip-chars-forward " \t") (setq ind1
(current-column))
783 (if (> ind1 ind
) (throw 'next t
))
784 (if (< ind1 ind
) (throw 'exit t
))
785 (if (not (org-at-item-p)) (throw 'exit nil
))
786 (skip-chars-forward " \t")
787 (looking-at "\\S-+ *")
788 (setq oldbullet
(match-string 0))
789 (unless (equal bullet oldbullet
) (replace-match bullet
))
790 (org-shift-item-indentation (- (length bullet
) (length oldbullet
))))))
792 (org-move-to-column col
)
793 (if (string-match "[0-9]" bullet
)
794 (org-renumber-ordered-list 1))))
796 (defun org-shift-item-indentation (delta)
797 "Shift the indentation in current item by DELTA."
799 (let ((beg (point-at-bol))
800 (end (progn (org-end-of-item) (point)))
803 (beginning-of-line 0)
804 (while (> (point) beg
)
805 (when (looking-at "[ \t]*\\S-")
806 ;; this is not an empty line
807 (setq i
(org-get-indentation))
808 (if (and (> i
0) (> (setq i
(+ i delta
)) 0))
810 (beginning-of-line 0)))))
812 (defun org-beginning-of-item-list ()
813 "Go to the beginning of the current item list.
814 I.e. to the first item in this list."
816 (org-beginning-of-item)
817 (let ((pos (point-at-bol))
818 (ind (org-get-indentation))
820 ;; find where this list begins
824 (beginning-of-line 0)
825 (if (looking-at "[ \t]*$")
826 (throw (if (bobp) 'exit
'next
) t
))
827 (skip-chars-forward " \t") (setq ind1
(current-column))
830 (not (org-at-item-p)))
831 (and (= (point-at-bol) (point-min))
832 (setq pos
(point-min))))
834 (when (org-at-item-p) (setq pos
(point-at-bol)))))))
838 (defun org-end-of-item-list ()
839 "Go to the end of the current item list.
840 I.e. to the text after the last item."
842 (org-beginning-of-item)
843 (let ((pos (point-at-bol))
844 (ind (org-get-indentation))
846 ;; find where this list begins
850 (beginning-of-line 2)
851 (if (looking-at "[ \t]*$")
852 (throw (if (eobp) 'exit
'next
) t
))
853 (skip-chars-forward " \t") (setq ind1
(current-column))
856 (not (org-at-item-p)))
859 (setq pos
(point-at-bol))
864 (defvar org-last-indent-begin-marker
(make-marker))
865 (defvar org-last-indent-end-marker
(make-marker))
867 (defun org-outdent-item (arg)
868 "Outdent a local list item."
870 (org-indent-item (- arg
)))
872 (defun org-indent-item (arg)
873 "Indent a local list item."
875 (and (org-region-active-p) (org-cursor-to-region-beginning))
876 (unless (org-at-item-p)
877 (error "Not on an item"))
879 (let (beg end ind ind1 tmp delta ind-down ind-up
)
880 (setq end
(and (org-region-active-p) (region-end)))
881 (if (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
882 (setq beg org-last-indent-begin-marker
883 end org-last-indent-end-marker
)
884 (org-beginning-of-item)
885 (setq beg
(move-marker org-last-indent-begin-marker
(point)))
887 (setq end
(move-marker org-last-indent-end-marker
(or end
(point)))))
889 (setq tmp
(org-item-indent-positions)
894 (if ind-down
(- ind-down ind
) 2)
895 (if ind-up
(- ind-up ind
) -
2)))
896 (if (< (+ delta ind
) 0) (error "Cannot outdent beyond margin"))
897 (while (< (point) end
)
898 (beginning-of-line 1)
899 (skip-chars-forward " \t") (setq ind1
(current-column))
900 (delete-region (point-at-bol) (point))
901 (or (eolp) (org-indent-to-column (+ ind1 delta
)))
902 (beginning-of-line 2))))
903 (org-fix-bullet-type)
904 (org-maybe-renumber-ordered-list-safe)
906 (beginning-of-line 0)
907 (condition-case nil
(org-beginning-of-item) (error nil
))
908 (org-maybe-renumber-ordered-list-safe)))
910 (defun org-item-indent-positions ()
911 "Return indentation for plain list items.
912 This returns a list with three values: The current indentation, the
913 parent indentation and the indentation a child should have.
914 Assumes cursor in item line."
915 (let* ((bolpos (point-at-bol))
916 (ind (org-get-indentation))
919 (org-beginning-of-item-list)
920 (skip-chars-backward "\n\r \t")
921 (when (org-in-item-p)
922 (org-beginning-of-item)
923 (setq ind-up
(org-get-indentation))))
927 ((and (condition-case nil
(progn (org-previous-item) t
)
929 (or (forward-char 1) t
)
930 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t
))
931 (setq ind-down
(org-get-indentation)))
932 ((and (goto-char pos
)
934 (goto-char (match-end 0))
935 (skip-chars-forward " \t")
936 (setq ind-down
(current-column)))))
937 (list ind ind-up ind-down
)))
940 ;;; Send and receive lists
942 (defun org-list-parse-list (&optional delete
)
943 "Parse the list at point and maybe DELETE it.
944 Return a list containing first level items as strings and
945 sublevels as a list of strings."
946 (let* ((item-beginning (org-list-item-beginning))
947 (start (car item-beginning
))
948 (end (org-list-end (cdr item-beginning
)))
949 output itemsep ltype
)
950 (while (re-search-forward org-list-beginning-re end t
)
951 (goto-char (match-beginning 3))
953 (cond ((string-match "[0-9]" (match-string 2))
954 (setq itemsep
"[0-9]+\\(?:\\.\\|)\\)"
956 ((string-match "^.*::" (match-string 0))
957 (setq itemsep
"[-+]" ltype
'descriptive
))
958 (t (setq itemsep
"[-+]" ltype
'unordered
))))
959 (let* ((indent1 (match-string 1))
960 (nextitem (save-excursion
962 (or (and (re-search-forward
963 (concat "^" indent1 itemsep
" *?") end t
)
964 (match-beginning 0)) end
))))
965 (item (buffer-substring
967 (or (and (re-search-forward
968 org-list-beginning-re end t
)
969 (goto-char (match-beginning 0)))
971 (nextindent (match-string 1))
972 (item (org-trim item
))
973 (item (if (string-match "^\\[\\([xX ]\\)\\]" item
)
974 (replace-match (if (equal (match-string 1 item
) " ")
980 (when (> (length nextindent
)
982 (narrow-to-region (point) nextitem
)
983 (push (org-list-parse-list) output
)
985 (when delete
(delete-region start end
))
986 (setq output
(nreverse output
))
987 (push ltype output
)))
989 (defun org-list-item-beginning ()
990 "Find the beginning of the list item.
991 Return a cons which car is the beginning position of the item and
992 cdr is the indentation string."
994 (if (not (or (looking-at org-list-beginning-re
)
996 org-list-beginning-re nil t
)))
997 (progn (goto-char (point-min)) (point))
998 (cons (match-beginning 0) (match-string 1)))))
1000 (defun org-list-end (indent)
1001 "Return the position of the end of the list.
1002 INDENT is the indentation of the list."
1005 (while (or (looking-at org-list-beginning-re
)
1006 (looking-at (concat "^" indent
"[ \t]+\\|^$")))
1007 (if (eq (point) (point-max))
1008 (throw 'exit
(point-max)))
1009 (forward-line 1))) (point)))
1011 (defun org-list-insert-radio-list ()
1012 "Insert a radio list template appropriate for this major mode."
1014 (let* ((e (assq major-mode org-list-radio-list-templates
))
1017 (unless e
(error "No radio list setup defined for %s" major-mode
))
1018 (setq name
(read-string "List name: "))
1019 (while (string-match "%n" txt
)
1020 (setq txt
(replace-match name t t txt
)))
1021 (or (bolp) (insert "\n"))
1026 (defun org-list-send-list (&optional maybe
)
1027 "Send a tranformed version of this list to the receiver position.
1028 With argument MAYBE, fail quietly if no transformation is defined for
1032 (unless (org-at-item-p) (error "Not at a list"))
1034 (goto-char (car (org-list-item-beginning)))
1035 (beginning-of-line 0)
1036 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1039 (error "Don't know how to transform this list"))))
1040 (let* ((name (match-string 1))
1041 (item-beginning (org-list-item-beginning))
1042 (transform (intern (match-string 2)))
1043 (txt (buffer-substring-no-properties
1044 (car item-beginning
)
1045 (org-list-end (cdr item-beginning
))))
1046 (list (org-list-parse-list))
1048 (unless (fboundp transform
)
1049 (error "No such transformation function %s" transform
))
1050 (setq txt
(funcall transform list
))
1051 ;; Find the insertion place
1053 (goto-char (point-min))
1054 (unless (re-search-forward
1055 (concat "BEGIN RECEIVE ORGLST +" name
"\\([ \t]\\|$\\)") nil t
)
1056 (error "Don't know where to insert translated list"))
1057 (goto-char (match-beginning 0))
1058 (beginning-of-line 2)
1060 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
1061 (error "Cannot find end of insertion region"))
1062 (beginning-of-line 1)
1063 (delete-region beg
(point))
1066 (message "List converted and installed at receiver location"))))
1068 (defun org-list-to-generic (list params
)
1069 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1071 Valid parameters PARAMS are
1073 :ustart String to start an unordered list
1074 :uend String to end an unordered list
1076 :ostart String to start an ordered list
1077 :oend String to end an ordered list
1079 :dstart String to start a descriptive list
1080 :dend String to end a descriptive list
1081 :dtstart String to start a descriptive term
1082 :dtend String to end a descriptive term
1083 :ddstart String to start a description
1084 :ddend String to end a description
1086 :splice When set to t, return only list body lines, don't wrap
1087 them into :[u/o]start and :[u/o]end. Default is nil.
1089 :istart String to start a list item
1090 :iend String to end a list item
1091 :isep String to separate items
1092 :lsep String to separate sublists
1094 :cboff String to insert for an unchecked checkbox
1095 :cbon String to insert for a checked checkbox"
1097 (let* ((p params
) sublist
1098 (splicep (plist-get p
:splice
))
1099 (ostart (plist-get p
:ostart
))
1100 (oend (plist-get p
:oend
))
1101 (ustart (plist-get p
:ustart
))
1102 (uend (plist-get p
:uend
))
1103 (dstart (plist-get p
:dstart
))
1104 (dend (plist-get p
:dend
))
1105 (dtstart (plist-get p
:dtstart
))
1106 (dtend (plist-get p
:dtend
))
1107 (ddstart (plist-get p
:ddstart
))
1108 (ddend (plist-get p
:ddend
))
1109 (istart (plist-get p
:istart
))
1110 (iend (plist-get p
:iend
))
1111 (isep (plist-get p
:isep
))
1112 (lsep (plist-get p
:lsep
))
1113 (cbon (plist-get p
:cbon
))
1114 (cboff (plist-get p
:cboff
)))
1116 (cond ((eq (car list
) 'ordered
)
1117 (concat ostart
"\n%s" oend
"\n"))
1118 ((eq (car list
) 'unordered
)
1119 (concat ustart
"\n%s" uend
"\n"))
1120 ((eq (car list
) 'descriptive
)
1121 (concat dstart
"\n%s" dend
"\n"))))
1122 rtn term defstart defend
)
1123 (while (setq sublist
(pop list
))
1124 (cond ((symbolp sublist
) nil
)
1126 (when (string-match "^\\(.*\\) ::" sublist
)
1127 (setq term
(org-trim (format (concat dtstart
"%s" dtend
)
1128 (match-string 1 sublist
))))
1129 (setq sublist
(substring sublist
(1+ (length term
)))))
1130 (if (string-match "\\[CBON\\]" sublist
)
1131 (setq sublist
(replace-match cbon t t sublist
)))
1132 (if (string-match "\\[CBOFF\\]" sublist
)
1133 (setq sublist
(replace-match cboff t t sublist
)))
1134 (setq rtn
(concat rtn istart term ddstart
1135 sublist ddend iend isep
)))
1136 (t (setq rtn
(concat rtn
;; previous list
1137 lsep
;; list separator
1138 (org-list-to-generic sublist p
)
1139 lsep
;; list separator
1141 (format wrapper rtn
))))
1143 (defun org-list-to-latex (list &optional params
)
1144 "Convert LIST into a LaTeX list.
1145 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1146 with overruling parameters for `org-list-to-generic'."
1147 (org-list-to-generic
1150 '(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
1151 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
1152 :dstart
"\\begin{description}" :dend
"\\end{description}"
1153 :dtstart
"[" :dtend
"]"
1154 :ddstart
"" :ddend
""
1155 :istart
"\\item " :iend
""
1156 :isep
"\n" :lsep
"\n"
1157 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
1160 (defun org-list-to-html (list &optional params
)
1161 "Convert LIST into a HTML list.
1162 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1163 with overruling parameters for `org-list-to-generic'."
1164 (org-list-to-generic
1167 '(:splicep nil
:ostart
"<ol>" :oend
"</ol>"
1168 :ustart
"<ul>" :uend
"</ul>"
1169 :dstart
"<dl>" :dend
"</dl>"
1170 :dtstart
"<dt>" :dtend
"</dt>"
1171 :ddstart
"<dd>" :ddend
"</dd>"
1172 :istart
"<li>" :iend
"</li>"
1173 :isep
"\n" :lsep
"\n"
1174 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>")
1177 (defun org-list-to-texinfo (list &optional params
)
1178 "Convert LIST into a Texinfo list.
1179 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1180 with overruling parameters for `org-list-to-generic'."
1181 (org-list-to-generic
1184 '(:splicep nil
:ostart
"@itemize @minus" :oend
"@end itemize"
1185 :ustart
"@enumerate" :uend
"@end enumerate"
1186 :dstart
"@table" :dend
"@end table"
1187 :dtstart
"@item " :dtend
"\n"
1188 :ddstart
"" :ddend
""
1189 :istart
"@item\n" :iend
""
1190 :isep
"\n" :lsep
"\n"
1191 :cbon
"@code{[X]}" :cboff
"@code{[ ]}")
1196 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1197 ;;; org-list.el ends here