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 preceding 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 toggle-presence
)
252 "Toggle the checkbox in the current line.
253 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
254 When there is an active region, toggle status or presence of the checkbox
255 in the first line, and make every item in the region have the same
256 status or presence, respectively.
257 If the cursor is in a headline, apply this to all checkbox items in the
258 text below the heading."
261 (let (beg end status first-present first-status
)
263 ((org-region-active-p)
264 (setq beg
(region-beginning) end
(region-end)))
266 (setq beg
(point) end
(save-excursion (outline-next-heading) (point))))
267 ((org-at-item-checkbox-p)
272 (goto-char (match-beginning 0))
275 (cond ((member (match-string 0) '("[ ]" "[-]")) "[X]")
283 (goto-char (match-end 0))
286 (t (error "Not at a checkbox or heading, and no active region")))
287 (setq end
(move-marker (make-marker) end
))
290 (setq first-present
(org-at-item-checkbox-p)
293 (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t
)
294 (equal (match-string 1) "[X]"))))
295 (while (< (point) end
)
298 ((and first-present
(org-at-item-checkbox-p))
301 (goto-char (match-beginning 0))
303 ((and (not first-present
) (not (org-at-item-checkbox-p))
306 (goto-char (match-end 0))
308 (when (org-at-item-checkbox-p)
309 (setq status
(equal (match-string 0) "[X]"))
311 (if first-status
"[ ]" "[X]") t t
)))
312 (beginning-of-line 2)))))
313 (org-update-checkbox-count-maybe))
315 (defun org-update-checkbox-count-maybe ()
316 "Update checkbox statistics unless turned off by user."
317 (when org-provide-checkbox-statistics
318 (org-update-checkbox-count)))
320 (defun org-update-checkbox-count (&optional all
)
321 "Update the checkbox statistics in the current section.
322 This will find all statistic cookies like [57%] and [6/12] and update them
323 with the current numbers. With optional prefix argument ALL, do this for
327 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
328 (beg (condition-case nil
329 (progn (org-back-to-heading) (point))
330 (error (point-min))))
331 (end (move-marker (make-marker)
332 (progn (outline-next-heading) (point))))
333 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
334 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
335 (re-find (concat re
"\\|" re-box
))
336 beg-cookie end-cookie is-percent c-on c-off lim
337 eline curr-ind next-ind continue-from startsearch
341 (goto-char (point-min))
342 (outline-next-heading)
343 (setq beg
(point) end
(point-max)))
345 ;; find each statistic cookie
346 (while (re-search-backward re-find beg t
)
347 (setq beg-cookie
(match-beginning 1)
348 end-cookie
(match-end 1)
349 cstat
(+ cstat
(if end-cookie
1 0))
350 startsearch
(point-at-eol)
351 continue-from
(match-beginning 0)
352 is-percent
(match-beginning 2)
354 ((org-on-heading-p) (outline-next-heading) (point))
355 ((org-at-item-p) (org-end-of-item) (point))
360 ;; find first checkbox for this cookie and gather
361 ;; statistics from all that are at this indentation level
362 (goto-char startsearch
)
363 (if (re-search-forward re-box lim t
)
365 (org-beginning-of-item)
366 (setq curr-ind
(org-get-indentation))
367 (setq next-ind curr-ind
)
368 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind
))
369 (save-excursion (end-of-line) (setq eline
(point)))
370 (if (re-search-forward re-box eline t
)
371 (if (member (match-string 2) '("[ ]" "[-]"))
372 (setq c-off
(1+ c-off
))
373 (setq c-on
(1+ c-on
))
377 (setq next-ind
(org-get-indentation))
379 (goto-char continue-from
)
382 (delete-region beg-cookie end-cookie
)
383 (goto-char beg-cookie
)
386 (format "[%d%%]" (/ (* 100 c-on
) (max 1 (+ c-on c-off
))))
387 (format "[%d/%d]" c-on
(+ c-on c-off
)))))
388 ;; update items checkbox if it has one
389 (when (org-at-item-p)
390 (org-beginning-of-item)
391 (when (and (> (+ c-on c-off
) 0)
392 (re-search-forward re-box
(point-at-eol) t
))
393 (setq beg-cookie
(match-beginning 2)
394 end-cookie
(match-end 2))
395 (delete-region beg-cookie end-cookie
)
396 (goto-char beg-cookie
)
397 (cond ((= c-off
0) (insert "[X]"))
398 ((= c-on
0) (insert "[ ]"))
401 (goto-char continue-from
))
402 (when (interactive-p)
403 (message "Checkbox statistics updated %s (%d places)"
404 (if all
"in entire file" "in current outline entry") cstat
)))))
406 (defun org-get-checkbox-statistics-face ()
407 "Select the face for checkbox statistics.
408 The face will be `org-done' when all relevant boxes are checked. Otherwise
409 it will be `org-todo'."
411 (if (equal (match-string 1) "100%") 'org-done
'org-todo
)
412 (if (and (> (match-end 2) (match-beginning 2))
413 (equal (match-string 2) (match-string 3)))
417 (defun org-beginning-of-item ()
418 "Go to the beginning of the current hand-formatted item.
419 If the cursor is not in an item, throw an error."
422 (limit (save-excursion
425 (org-back-to-heading)
426 (beginning-of-line 2) (point))
427 (error (point-min)))))
428 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
431 (beginning-of-line 1)
432 (beginning-of-line 1)
433 (skip-chars-forward " \t")
434 (setq ind
(current-column))
437 (beginning-of-line 0)
438 (if (or (bobp) (< (point) limit
)) (throw 'exit nil
))
440 (if (looking-at "[ \t]*$")
441 (setq ind1 ind-empty
)
442 (skip-chars-forward " \t")
443 (setq ind1
(current-column)))
445 (progn (beginning-of-line 1) (throw 'exit
(org-at-item-p))))))
448 (error "Not in an item")))))
450 (defun org-end-of-item ()
451 "Go to the end of the current hand-formatted item.
452 If the cursor is not in an item, throw an error."
456 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
457 (limit (save-excursion (outline-next-heading) (point)))
459 (org-beginning-of-item)
460 (skip-chars-forward " \t")
464 (beginning-of-line 2)
465 (if (eobp) (throw 'exit
(point)))
466 (if (>= (point) limit
) (throw 'exit
(point-at-bol)))
467 (if (looking-at "[ \t]*$")
468 (setq ind1 ind-empty
)
469 (skip-chars-forward " \t")
470 (setq ind1
(current-column)))
472 (throw 'exit
(point-at-bol)))))))
476 (error "Not in an item"))))
478 (defun org-next-item ()
479 "Move to the beginning of the next item in the current plain list.
480 Error if not at a plain list, or if this is the last item in the list."
482 (let (ind ind1
(pos (point)))
483 (org-beginning-of-item)
484 (setq ind
(org-get-indentation))
486 (setq ind1
(org-get-indentation))
487 (unless (and (org-at-item-p) (= ind ind1
))
489 (error "On last item"))))
491 (defun org-previous-item ()
492 "Move to the beginning of the previous item in the current plain list.
493 Error if not at a plain list, or if this is the first item in the list."
495 (let (beg ind ind1
(pos (point)))
496 (org-beginning-of-item)
498 (setq ind
(org-get-indentation))
502 (beginning-of-line 0)
503 (if (looking-at "[ \t]*$")
505 (if (<= (setq ind1
(org-get-indentation)) ind
)
508 (if (or (not (org-at-item-p))
511 (org-beginning-of-item))
512 (error (goto-char pos
)
513 (error "On first item")))))
515 (defun org-first-list-item-p ()
516 "Is this heading the item in a plain list?"
517 (unless (org-at-item-p)
518 (error "Not at a plain list item"))
519 (org-beginning-of-item)
520 (= (point) (save-excursion (org-beginning-of-item-list))))
522 (defun org-move-item-down ()
523 "Move the plain list item at point down, i.e. swap with following item.
524 Subitems (items with larger indentation) are considered part of the item,
525 so this really moves item trees."
527 (let ((col (current-column))
529 beg beg0 end end0 ind ind1 txt ne-end ne-beg
)
530 (org-beginning-of-item)
533 (setq ne-beg
(org-back-over-empty-lines))
536 (setq ind
(org-get-indentation))
539 (setq ind1
(org-get-indentation))
540 (setq ne-end
(org-back-over-empty-lines))
543 (when (and (org-first-list-item-p) (< ne-end ne-beg
))
544 ;; include less whitespace
547 (forward-line (- ne-beg ne-end
))
550 (if (and (org-at-item-p) (= ind ind1
))
553 (org-back-over-empty-lines)
554 (setq txt
(buffer-substring beg end
))
556 (delete-region beg end
))
559 (goto-char pos
) (org-skip-whitespace)
560 (org-maybe-renumber-ordered-list)
561 (move-to-column col
))
564 (error "Cannot move this item further down"))))
566 (defun org-move-item-up (arg)
567 "Move the plain list item at point up, i.e. swap with previous item.
568 Subitems (items with larger indentation) are considered part of the item,
569 so this really moves item trees."
571 (let ((col (current-column)) (pos (point))
572 beg beg0 end ind ind1 txt
573 ne-beg ne-ins ins-end
)
574 (org-beginning-of-item)
576 (setq ind
(org-get-indentation))
578 (setq ne-beg
(org-back-over-empty-lines))
582 (org-back-over-empty-lines)
587 (beginning-of-line 0)
588 (if (looking-at "[ \t]*$")
589 (if org-empty-line-terminates-plain-lists
592 (error "Cannot move this item further up"))
594 (if (<= (setq ind1
(org-get-indentation)) ind
)
597 (org-beginning-of-item)
598 (error (goto-char beg0
)
600 (error "Cannot move this item further up")))
601 (setq ind1
(org-get-indentation))
602 (if (and (org-at-item-p) (= ind ind1
))
604 (setq ne-ins
(org-back-over-empty-lines))
605 (setq txt
(buffer-substring beg end
))
607 (delete-region beg end
))
610 (setq ins-end
(point))
611 (goto-char pos
) (org-skip-whitespace)
613 (when (and (org-first-list-item-p) (> ne-ins ne-beg
))
614 ;; Move whitespace back to beginning
617 (let ((kill-whole-line t
))
618 (kill-line (- ne-ins ne-beg
)) (point)))
619 (insert (make-string (- ne-ins ne-beg
) ?
\n)))
621 (org-maybe-renumber-ordered-list)
622 (move-to-column col
))
625 (error "Cannot move this item further up"))))
627 (defun org-maybe-renumber-ordered-list ()
628 "Renumber the ordered list at point if setup allows it.
629 This tests the user option `org-auto-renumber-ordered-lists' before
630 doing the renumbering."
632 (when (and org-auto-renumber-ordered-lists
634 (if (match-beginning 3)
635 (org-renumber-ordered-list 1)
636 (org-fix-bullet-type))))
638 (defun org-maybe-renumber-ordered-list-safe ()
641 (org-maybe-renumber-ordered-list))
644 (defun org-cycle-list-bullet (&optional which
)
645 "Cycle through the different itemize/enumerate bullets.
646 This cycle the entire list level through the sequence:
648 `-' -> `+' -> `*' -> `1.' -> `1)'
650 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
651 0 means `-', 1 means `+' etc."
654 (org-beginning-of-item-list)
656 (beginning-of-line 1)
657 (let ((current (match-string 0))
658 (prevp (eq which
'previous
))
661 ((and (numberp which
)
662 (nth (1- which
) '("-" "+" "*" "1." "1)"))))
663 ((string-match "-" current
) (if prevp
"1)" "+"))
664 ((string-match "\\+" current
)
665 (if prevp
"-" (if (looking-at "\\S-") "1." "*")))
666 ((string-match "\\*" current
) (if prevp
"+" "1."))
667 ((string-match "\\." current
)
668 (if prevp
(if (looking-at "\\S-") "+" "*") "1)"))
669 ((string-match ")" current
) (if prevp
"1." "-"))
670 (t (error "This should not happen"))))
671 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
672 (setq old
(match-string 2))
673 (replace-match (concat "\\1" new
)))
674 (org-shift-item-indentation (- (length new
) (length old
)))
675 (org-fix-bullet-type)
676 (org-maybe-renumber-ordered-list))))
678 (defun org-get-string-indentation (s)
679 "What indentation has S due to SPACE and TAB at the beginning of the string?"
680 (let ((n -
1) (i 0) (w tab-width
) c
)
682 (while (< (setq n
(1+ n
)) (length s
))
684 (cond ((= c ?\
) (setq i
(1+ i
)))
685 ((= c ?
\t) (setq i
(* (/ (+ w i
) w
) w
)))
686 (t (throw 'exit t
)))))
689 (defun org-renumber-ordered-list (arg)
690 "Renumber an ordered plain list.
691 Cursor needs to be in the first line of an item, the line that starts
692 with something like \"1.\" or \"2)\"."
694 (unless (and (org-at-item-p)
696 (error "This is not an ordered list"))
697 (let ((line (org-current-line))
698 (col (current-column))
699 (ind (org-get-string-indentation
700 (buffer-substring (point-at-bol) (match-beginning 3))))
701 ;; (term (substring (match-string 3) -1))
704 ;; find where this list begins
705 (org-beginning-of-item-list)
707 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
708 (setq fmt
(concat "%d" (match-string 1)))
709 (beginning-of-line 0)
710 ;; walk forward and replace these numbers
714 (if bobp
(setq bobp nil
) (beginning-of-line 2))
715 (if (eobp) (throw 'exit nil
))
716 (if (looking-at "[ \t]*$") (throw 'next nil
))
717 (skip-chars-forward " \t") (setq ind1
(current-column))
718 (if (> ind1 ind
) (throw 'next t
))
719 (if (< ind1 ind
) (throw 'exit t
))
720 (if (not (org-at-item-p)) (throw 'exit nil
))
721 (setq old
(match-string 2))
722 (delete-region (match-beginning 2) (match-end 2))
723 (goto-char (match-beginning 2))
724 (insert (setq new
(format fmt
(setq n
(1+ n
)))))
725 (org-shift-item-indentation (- (length new
) (length old
))))))
727 (org-move-to-column col
)))
729 (defun org-fix-bullet-type ()
730 "Make sure all items in this list have the same bullet as the first item.
731 Also, fix the indentation."
733 (unless (org-at-item-p) (error "This is not a list"))
734 (let ((line (org-current-line))
735 (col (current-column))
736 (ind (current-indentation))
737 ind1 bullet oldbullet
)
738 ;; find where this list begins
739 (org-beginning-of-item-list)
740 (beginning-of-line 1)
741 ;; find out what the bullet type is
742 (looking-at "[ \t]*\\(\\S-+\\)")
743 (setq bullet
(concat (match-string 1) " "))
744 (if (and org-list-two-spaces-after-bullet-regexp
745 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
746 (setq bullet
(concat bullet
" ")))
747 ;; walk forward and replace these numbers
748 (beginning-of-line 0)
752 (beginning-of-line 2)
753 (if (eobp) (throw 'exit nil
))
754 (if (looking-at "[ \t]*$") (throw 'next nil
))
755 (skip-chars-forward " \t") (setq ind1
(current-column))
756 (if (> ind1 ind
) (throw 'next t
))
757 (if (< ind1 ind
) (throw 'exit t
))
758 (if (not (org-at-item-p)) (throw 'exit nil
))
759 (skip-chars-forward " \t")
760 (looking-at "\\S-+ *")
761 (setq oldbullet
(match-string 0))
762 (replace-match bullet
)
763 (org-shift-item-indentation (- (length bullet
) (length oldbullet
))))))
765 (org-move-to-column col
)
766 (if (string-match "[0-9]" bullet
)
767 (org-renumber-ordered-list 1))))
769 (defun org-shift-item-indentation (delta)
770 "Shift the indentation in current item by DELTA."
772 (let ((beg (point-at-bol))
773 (end (progn (org-end-of-item) (point)))
776 (beginning-of-line 0)
777 (while (> (point) beg
)
778 (when (looking-at "[ \t]*\\S-")
779 ;; this is not an empty line
780 (setq i
(org-get-indentation))
781 (if (and (> i
0) (> (setq i
(+ i delta
)) 0))
783 (beginning-of-line 0)))))
785 (defun org-beginning-of-item-list ()
786 "Go to the beginning of the current item list.
787 I.e. to the first item in this list."
789 (org-beginning-of-item)
790 (let ((pos (point-at-bol))
791 (ind (org-get-indentation))
793 ;; find where this list begins
797 (beginning-of-line 0)
798 (if (looking-at "[ \t]*$")
799 (throw (if (bobp) 'exit
'next
) t
))
800 (skip-chars-forward " \t") (setq ind1
(current-column))
803 (not (org-at-item-p)))
804 (and (= (point-at-bol) (point-min))
805 (setq pos
(point-min))))
807 (when (org-at-item-p) (setq pos
(point-at-bol)))))))
811 (defun org-end-of-item-list ()
812 "Go to the end of the current item list.
813 I.e. to the text after the last item."
815 (org-beginning-of-item)
816 (let ((pos (point-at-bol))
817 (ind (org-get-indentation))
819 ;; find where this list begins
823 (beginning-of-line 2)
824 (if (looking-at "[ \t]*$")
825 (throw (if (eobp) 'exit
'next
) t
))
826 (skip-chars-forward " \t") (setq ind1
(current-column))
829 (not (org-at-item-p)))
832 (setq pos
(point-at-bol))
837 (defvar org-last-indent-begin-marker
(make-marker))
838 (defvar org-last-indent-end-marker
(make-marker))
840 (defun org-outdent-item (arg)
841 "Outdent a local list item."
843 (org-indent-item (- arg
)))
845 (defun org-indent-item (arg)
846 "Indent a local list item."
848 (unless (org-at-item-p)
849 (error "Not on an item"))
851 (let (beg end ind ind1 tmp delta ind-down ind-up
)
852 (if (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
853 (setq beg org-last-indent-begin-marker
854 end org-last-indent-end-marker
)
855 (org-beginning-of-item)
856 (setq beg
(move-marker org-last-indent-begin-marker
(point)))
858 (setq end
(move-marker org-last-indent-end-marker
(point))))
860 (setq tmp
(org-item-indent-positions)
865 (if ind-down
(- ind-down ind
) 2)
866 (if ind-up
(- ind-up ind
) -
2)))
867 (if (< (+ delta ind
) 0) (error "Cannot outdent beyond margin"))
868 (while (< (point) end
)
869 (beginning-of-line 1)
870 (skip-chars-forward " \t") (setq ind1
(current-column))
871 (delete-region (point-at-bol) (point))
872 (or (eolp) (org-indent-to-column (+ ind1 delta
)))
873 (beginning-of-line 2))))
874 (org-fix-bullet-type)
875 (org-maybe-renumber-ordered-list-safe)
877 (beginning-of-line 0)
878 (condition-case nil
(org-beginning-of-item) (error nil
))
879 (org-maybe-renumber-ordered-list-safe)))
881 (defun org-item-indent-positions ()
882 "Return indentation for plain list items.
883 This returns a list with three values: The current indentation, the
884 parent indentation and the indentation a child should have.
885 Assumes cursor in item line."
886 (let* ((bolpos (point-at-bol))
887 (ind (org-get-indentation))
890 (org-beginning-of-item-list)
891 (skip-chars-backward "\n\r \t")
892 (when (org-in-item-p)
893 (org-beginning-of-item)
894 (setq ind-up
(org-get-indentation))))
898 ((and (condition-case nil
(progn (org-previous-item) t
)
900 (or (forward-char 1) t
)
901 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t
))
902 (setq ind-down
(org-get-indentation)))
903 ((and (goto-char pos
)
905 (goto-char (match-end 0))
906 (skip-chars-forward " \t")
907 (setq ind-down
(current-column)))))
908 (list ind ind-up ind-down
)))
911 ;;; Send and receive lists
913 (defun org-list-parse-list (&optional delete
)
914 "Parse the list at point and maybe DELETE it.
915 Return a list containing first level items as strings and
916 sublevels as a list of strings."
917 (let* ((item-beginning (org-list-item-beginning))
918 (start (car item-beginning
))
919 (end (org-list-end (cdr item-beginning
)))
920 output itemsep ltype
)
921 (while (re-search-forward org-list-beginning-re end t
)
922 (goto-char (match-beginning 3))
924 (cond ((string-match "[0-9]" (match-string 2))
925 (setq itemsep
"[0-9]+\\(?:\\.\\|)\\)"
927 ((string-match "^.*::" (match-string 0))
928 (setq itemsep
"[-+]" ltype
'descriptive
))
929 (t (setq itemsep
"[-+]" ltype
'unordered
))))
930 (let* ((indent1 (match-string 1))
931 (nextitem (save-excursion
933 (or (and (re-search-forward
934 (concat "^" indent1 itemsep
" *?") end t
)
935 (match-beginning 0)) end
))))
936 (item (buffer-substring
938 (or (and (re-search-forward
939 org-list-beginning-re end t
)
940 (goto-char (match-beginning 0)))
942 (nextindent (match-string 1))
943 (item (org-trim item
))
944 (item (if (string-match "^\\[\\([xX ]\\)\\]" item
)
945 (replace-match (if (equal (match-string 1 item
) " ")
951 (when (> (length nextindent
)
953 (narrow-to-region (point) nextitem
)
954 (push (org-list-parse-list) output
)
956 (when delete
(delete-region start end
))
957 (setq output
(nreverse output
))
958 (push ltype output
)))
960 (defun org-list-item-beginning ()
961 "Find the beginning of the list item.
962 Return a cons which car is the beginning position of the item and
963 cdr is the indentation string."
965 (if (not (or (looking-at org-list-beginning-re
)
967 org-list-beginning-re nil t
)))
968 (progn (goto-char (point-min)) (point))
969 (cons (match-beginning 0) (match-string 1)))))
971 (defun org-list-end (indent)
972 "Return the position of the end of the list.
973 INDENT is the indentation of the list."
976 (while (or (looking-at org-list-beginning-re
)
977 (looking-at (concat "^" indent
"[ \t]+\\|^$")))
978 (if (eq (point) (point-max))
979 (throw 'exit
(point-max)))
980 (forward-line 1))) (point)))
982 (defun org-list-insert-radio-list ()
983 "Insert a radio list template appropriate for this major mode."
985 (let* ((e (assq major-mode org-list-radio-list-templates
))
988 (unless e
(error "No radio list setup defined for %s" major-mode
))
989 (setq name
(read-string "List name: "))
990 (while (string-match "%n" txt
)
991 (setq txt
(replace-match name t t txt
)))
992 (or (bolp) (insert "\n"))
997 (defun org-list-send-list (&optional maybe
)
998 "Send a tranformed version of this list to the receiver position.
999 With argument MAYBE, fail quietly if no transformation is defined for
1003 (unless (org-at-item-p) (error "Not at a list"))
1005 (goto-char (car (org-list-item-beginning)))
1006 (beginning-of-line 0)
1007 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1010 (error "Don't know how to transform this list"))))
1011 (let* ((name (match-string 1))
1012 (item-beginning (org-list-item-beginning))
1013 (transform (intern (match-string 2)))
1014 (txt (buffer-substring-no-properties
1015 (car item-beginning
)
1016 (org-list-end (cdr item-beginning
))))
1017 (list (org-list-parse-list))
1019 (unless (fboundp transform
)
1020 (error "No such transformation function %s" transform
))
1021 (setq txt
(funcall transform list
))
1022 ;; Find the insertion place
1024 (goto-char (point-min))
1025 (unless (re-search-forward
1026 (concat "BEGIN RECEIVE ORGLST +" name
"\\([ \t]\\|$\\)") nil t
)
1027 (error "Don't know where to insert translated list"))
1028 (goto-char (match-beginning 0))
1029 (beginning-of-line 2)
1031 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
1032 (error "Cannot find end of insertion region"))
1033 (beginning-of-line 1)
1034 (delete-region beg
(point))
1037 (message "List converted and installed at receiver location"))))
1039 (defun org-list-to-generic (list params
)
1040 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1042 Valid parameters PARAMS are
1044 :ustart String to start an unordered list
1045 :uend String to end an unordered list
1047 :ostart String to start an ordered list
1048 :oend String to end an ordered list
1050 :dstart String to start a descriptive list
1051 :dend String to end a descriptive list
1052 :dtstart String to start a descriptive term
1053 :dtend String to end a descriptive term
1054 :ddstart String to start a description
1055 :ddend String to end a description
1057 :splice When set to t, return only list body lines, don't wrap
1058 them into :[u/o]start and :[u/o]end. Default is nil.
1060 :istart String to start a list item
1061 :iend String to end a list item
1062 :isep String to separate items
1063 :lsep String to separate sublists
1065 :cboff String to insert for an unchecked checkbox
1066 :cbon String to insert for a checked checkbox"
1068 (let* ((p params
) sublist
1069 (splicep (plist-get p
:splice
))
1070 (ostart (plist-get p
:ostart
))
1071 (oend (plist-get p
:oend
))
1072 (ustart (plist-get p
:ustart
))
1073 (uend (plist-get p
:uend
))
1074 (dstart (plist-get p
:dstart
))
1075 (dend (plist-get p
:dend
))
1076 (dtstart (plist-get p
:dtstart
))
1077 (dtend (plist-get p
:dtend
))
1078 (ddstart (plist-get p
:ddstart
))
1079 (ddend (plist-get p
:ddend
))
1080 (istart (plist-get p
:istart
))
1081 (iend (plist-get p
:iend
))
1082 (isep (plist-get p
:isep
))
1083 (lsep (plist-get p
:lsep
))
1084 (cbon (plist-get p
:cbon
))
1085 (cboff (plist-get p
:cboff
)))
1087 (cond ((eq (car list
) 'ordered
)
1088 (concat ostart
"\n%s" oend
"\n"))
1089 ((eq (car list
) 'unordered
)
1090 (concat ustart
"\n%s" uend
"\n"))
1091 ((eq (car list
) 'descriptive
)
1092 (concat dstart
"\n%s" dend
"\n"))))
1093 rtn term defstart defend
)
1094 (while (setq sublist
(pop list
))
1095 (cond ((symbolp sublist
) nil
)
1097 (when (string-match "^\\(.*\\) ::" sublist
)
1098 (setq term
(org-trim (format (concat dtstart
"%s" dtend
)
1099 (match-string 1 sublist
))))
1100 (setq sublist
(substring sublist
(1+ (length term
)))))
1101 (if (string-match "\\[CBON\\]" sublist
)
1102 (setq sublist
(replace-match cbon t t sublist
)))
1103 (if (string-match "\\[CBOFF\\]" sublist
)
1104 (setq sublist
(replace-match cboff t t sublist
)))
1105 (setq rtn
(concat rtn istart term ddstart
1106 sublist ddend iend isep
)))
1107 (t (setq rtn
(concat rtn
;; previous list
1108 lsep
;; list separator
1109 (org-list-to-generic sublist p
)
1110 lsep
;; list separator
1112 (format wrapper rtn
))))
1114 (defun org-list-to-latex (list &optional params
)
1115 "Convert LIST into a LaTeX list.
1116 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1117 with overruling parameters for `org-list-to-generic'."
1118 (org-list-to-generic
1121 '(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
1122 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
1123 :dstart
"\\begin{description}" :dend
"\\end{description}"
1124 :dtstart
"[" :dtend
"]"
1125 :ddstart
"" :ddend
""
1126 :istart
"\\item " :iend
""
1127 :isep
"\n" :lsep
"\n"
1128 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
1131 (defun org-list-to-html (list &optional params
)
1132 "Convert LIST into a HTML list.
1133 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1134 with overruling parameters for `org-list-to-generic'."
1135 (org-list-to-generic
1138 '(:splicep nil
:ostart
"<ol>" :oend
"</ol>"
1139 :ustart
"<ul>" :uend
"</ul>"
1140 :dstart
"<dl>" :dend
"</dl>"
1141 :dtstart
"<dt>" :dtend
"</dt>"
1142 :ddstart
"<dd>" :ddend
"</dd>"
1143 :istart
"<li>" :iend
"</li>"
1144 :isep
"\n" :lsep
"\n"
1145 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>")
1148 (defun org-list-to-texinfo (list &optional params
)
1149 "Convert LIST into a Texinfo list.
1150 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1151 with overruling parameters for `org-list-to-generic'."
1152 (org-list-to-generic
1155 '(:splicep nil
:ostart
"@itemize @minus" :oend
"@end itemize"
1156 :ustart
"@enumerate" :uend
"@end enumerate"
1157 :dstart
"@table" :dend
"@end table"
1158 :dtstart
"@item " :dtend
"\n"
1159 :ddstart
"" :ddend
""
1160 :istart
"@item\n" :iend
""
1161 :isep
"\n" :lsep
"\n"
1162 :cbon
"@code{[X]}" :cboff
"@code{[ ]}")
1167 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1168 ;;; org-list.el ends here