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 With double prefix, set checkbox to [-].
255 When there is an active region, toggle status or presence of the checkbox
256 in the first line, and make every item in the region have the same
257 status or presence, respectively.
258 If the cursor is in a headline, apply this to all checkbox items in the
259 text below the heading."
262 (let (beg end status first-present first-status
)
264 ((org-region-active-p)
265 (setq beg
(region-beginning) end
(region-end)))
267 (setq beg
(point) end
(save-excursion (outline-next-heading) (point))))
268 ((org-at-item-checkbox-p)
270 (if (equal toggle-presence
'(4))
273 (goto-char (match-beginning 0))
276 (cond ((equal toggle-presence
'(16)) "[-]")
277 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
285 (goto-char (match-end 0))
288 (t (error "Not at a checkbox or heading, and no active region")))
289 (setq end
(move-marker (make-marker) end
))
292 (setq first-present
(org-at-item-checkbox-p)
295 (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t
)
296 (equal (match-string 1) "[X]"))))
297 (while (< (point) end
)
300 ((and first-present
(org-at-item-checkbox-p))
303 (goto-char (match-beginning 0))
305 ((and (not first-present
) (not (org-at-item-checkbox-p))
308 (goto-char (match-end 0))
310 (when (org-at-item-checkbox-p)
311 (setq status
(equal (match-string 0) "[X]"))
313 (if first-status
"[ ]" "[X]") t t
)))
314 (beginning-of-line 2)))))
315 (org-update-checkbox-count-maybe))
317 (defun org-update-checkbox-count-maybe ()
318 "Update checkbox statistics unless turned off by user."
319 (when org-provide-checkbox-statistics
320 (org-update-checkbox-count)))
322 (defun org-update-checkbox-count (&optional all
)
323 "Update the checkbox statistics in the current section.
324 This will find all statistic cookies like [57%] and [6/12] and update them
325 with the current numbers. With optional prefix argument ALL, do this for
329 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
330 (beg (condition-case nil
331 (progn (org-back-to-heading) (point))
332 (error (point-min))))
333 (end (move-marker (make-marker)
334 (progn (outline-next-heading) (point))))
335 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
336 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
337 (re-find (concat re
"\\|" re-box
))
338 beg-cookie end-cookie is-percent c-on c-off lim
339 eline curr-ind next-ind continue-from startsearch
343 (goto-char (point-min))
344 (outline-next-heading)
345 (setq beg
(point) end
(point-max)))
347 ;; find each statistic cookie
348 (while (re-search-backward re-find beg t
)
349 (setq beg-cookie
(match-beginning 1)
350 end-cookie
(match-end 1)
351 cstat
(+ cstat
(if end-cookie
1 0))
352 startsearch
(point-at-eol)
353 continue-from
(match-beginning 0)
354 is-percent
(match-beginning 2)
356 ((org-on-heading-p) (outline-next-heading) (point))
357 ((org-at-item-p) (org-end-of-item) (point))
362 ;; find first checkbox for this cookie and gather
363 ;; statistics from all that are at this indentation level
364 (goto-char startsearch
)
365 (if (re-search-forward re-box lim t
)
367 (org-beginning-of-item)
368 (setq curr-ind
(org-get-indentation))
369 (setq next-ind curr-ind
)
370 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind
))
371 (save-excursion (end-of-line) (setq eline
(point)))
372 (if (re-search-forward re-box eline t
)
373 (if (member (match-string 2) '("[ ]" "[-]"))
374 (setq c-off
(1+ c-off
))
375 (setq c-on
(1+ c-on
))
379 (setq next-ind
(org-get-indentation))
381 (goto-char continue-from
)
384 (delete-region beg-cookie end-cookie
)
385 (goto-char beg-cookie
)
388 (format "[%d%%]" (/ (* 100 c-on
) (max 1 (+ c-on c-off
))))
389 (format "[%d/%d]" c-on
(+ c-on c-off
)))))
390 ;; update items checkbox if it has one
391 (when (org-at-item-p)
392 (org-beginning-of-item)
393 (when (and (> (+ c-on c-off
) 0)
394 (re-search-forward re-box
(point-at-eol) t
))
395 (setq beg-cookie
(match-beginning 2)
396 end-cookie
(match-end 2))
397 (delete-region beg-cookie end-cookie
)
398 (goto-char beg-cookie
)
399 (cond ((= c-off
0) (insert "[X]"))
400 ((= c-on
0) (insert "[ ]"))
403 (goto-char continue-from
))
404 (when (interactive-p)
405 (message "Checkbox statistics updated %s (%d places)"
406 (if all
"in entire file" "in current outline entry") cstat
)))))
408 (defun org-get-checkbox-statistics-face ()
409 "Select the face for checkbox statistics.
410 The face will be `org-done' when all relevant boxes are checked. Otherwise
411 it will be `org-todo'."
413 (if (equal (match-string 1) "100%") 'org-done
'org-todo
)
414 (if (and (> (match-end 2) (match-beginning 2))
415 (equal (match-string 2) (match-string 3)))
419 (defun org-beginning-of-item ()
420 "Go to the beginning of the current hand-formatted item.
421 If the cursor is not in an item, throw an error."
424 (limit (save-excursion
427 (org-back-to-heading)
428 (beginning-of-line 2) (point))
429 (error (point-min)))))
430 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
433 (beginning-of-line 1)
434 (beginning-of-line 1)
435 (skip-chars-forward " \t")
436 (setq ind
(current-column))
439 (beginning-of-line 0)
440 (if (or (bobp) (< (point) limit
)) (throw 'exit nil
))
442 (if (looking-at "[ \t]*$")
443 (setq ind1 ind-empty
)
444 (skip-chars-forward " \t")
445 (setq ind1
(current-column)))
447 (progn (beginning-of-line 1) (throw 'exit
(org-at-item-p))))))
450 (error "Not in an item")))))
452 (defun org-end-of-item ()
453 "Go to the end of the current hand-formatted item.
454 If the cursor is not in an item, throw an error."
458 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
459 (limit (save-excursion (outline-next-heading) (point)))
461 (org-beginning-of-item)
462 (skip-chars-forward " \t")
466 (beginning-of-line 2)
467 (if (eobp) (throw 'exit
(point)))
468 (if (>= (point) limit
) (throw 'exit
(point-at-bol)))
469 (if (looking-at "[ \t]*$")
470 (setq ind1 ind-empty
)
471 (skip-chars-forward " \t")
472 (setq ind1
(current-column)))
474 (throw 'exit
(point-at-bol)))))))
478 (error "Not in an item"))))
480 (defun org-next-item ()
481 "Move to the beginning of the next item in the current plain list.
482 Error if not at a plain list, or if this is the last item in the list."
484 (let (ind ind1
(pos (point)))
485 (org-beginning-of-item)
486 (setq ind
(org-get-indentation))
488 (setq ind1
(org-get-indentation))
489 (unless (and (org-at-item-p) (= ind ind1
))
491 (error "On last item"))))
493 (defun org-previous-item ()
494 "Move to the beginning of the previous item in the current plain list.
495 Error if not at a plain list, or if this is the first item in the list."
497 (let (beg ind ind1
(pos (point)))
498 (org-beginning-of-item)
500 (setq ind
(org-get-indentation))
504 (beginning-of-line 0)
505 (if (looking-at "[ \t]*$")
507 (if (<= (setq ind1
(org-get-indentation)) ind
)
510 (if (or (not (org-at-item-p))
513 (org-beginning-of-item))
514 (error (goto-char pos
)
515 (error "On first item")))))
517 (defun org-first-list-item-p ()
518 "Is this heading the item in a plain list?"
519 (unless (org-at-item-p)
520 (error "Not at a plain list item"))
521 (org-beginning-of-item)
522 (= (point) (save-excursion (org-beginning-of-item-list))))
524 (defun org-move-item-down ()
525 "Move the plain list item at point down, i.e. swap with following item.
526 Subitems (items with larger indentation) are considered part of the item,
527 so this really moves item trees."
529 (let ((col (current-column))
531 beg beg0 end end0 ind ind1 txt ne-end ne-beg
)
532 (org-beginning-of-item)
535 (setq ne-beg
(org-back-over-empty-lines))
538 (setq ind
(org-get-indentation))
541 (setq ind1
(org-get-indentation))
542 (setq ne-end
(org-back-over-empty-lines))
545 (when (and (org-first-list-item-p) (< ne-end ne-beg
))
546 ;; include less whitespace
549 (forward-line (- ne-beg ne-end
))
552 (if (and (org-at-item-p) (= ind ind1
))
555 (org-back-over-empty-lines)
556 (setq txt
(buffer-substring beg end
))
558 (delete-region beg end
))
561 (goto-char pos
) (org-skip-whitespace)
562 (org-maybe-renumber-ordered-list)
563 (move-to-column col
))
566 (error "Cannot move this item further down"))))
568 (defun org-move-item-up (arg)
569 "Move the plain list item at point up, i.e. swap with previous item.
570 Subitems (items with larger indentation) are considered part of the item,
571 so this really moves item trees."
573 (let ((col (current-column)) (pos (point))
574 beg beg0 end ind ind1 txt
575 ne-beg ne-ins ins-end
)
576 (org-beginning-of-item)
578 (setq ind
(org-get-indentation))
580 (setq ne-beg
(org-back-over-empty-lines))
584 (org-back-over-empty-lines)
589 (beginning-of-line 0)
590 (if (looking-at "[ \t]*$")
591 (if org-empty-line-terminates-plain-lists
594 (error "Cannot move this item further up"))
596 (if (<= (setq ind1
(org-get-indentation)) ind
)
599 (org-beginning-of-item)
600 (error (goto-char beg0
)
602 (error "Cannot move this item further up")))
603 (setq ind1
(org-get-indentation))
604 (if (and (org-at-item-p) (= ind ind1
))
606 (setq ne-ins
(org-back-over-empty-lines))
607 (setq txt
(buffer-substring beg end
))
609 (delete-region beg end
))
612 (setq ins-end
(point))
613 (goto-char pos
) (org-skip-whitespace)
615 (when (and (org-first-list-item-p) (> ne-ins ne-beg
))
616 ;; Move whitespace back to beginning
619 (let ((kill-whole-line t
))
620 (kill-line (- ne-ins ne-beg
)) (point)))
621 (insert (make-string (- ne-ins ne-beg
) ?
\n)))
623 (org-maybe-renumber-ordered-list)
624 (move-to-column col
))
627 (error "Cannot move this item further up"))))
629 (defun org-maybe-renumber-ordered-list ()
630 "Renumber the ordered list at point if setup allows it.
631 This tests the user option `org-auto-renumber-ordered-lists' before
632 doing the renumbering."
634 (when (and org-auto-renumber-ordered-lists
636 (if (match-beginning 3)
637 (org-renumber-ordered-list 1)
638 (org-fix-bullet-type))))
640 (defun org-maybe-renumber-ordered-list-safe ()
643 (org-maybe-renumber-ordered-list))
646 (defun org-cycle-list-bullet (&optional which
)
647 "Cycle through the different itemize/enumerate bullets.
648 This cycle the entire list level through the sequence:
650 `-' -> `+' -> `*' -> `1.' -> `1)'
652 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
653 0 means `-', 1 means `+' etc."
656 (org-beginning-of-item-list)
658 (beginning-of-line 1)
659 (let ((current (match-string 0))
660 (prevp (eq which
'previous
))
663 ((and (numberp which
)
664 (nth (1- which
) '("-" "+" "*" "1." "1)"))))
665 ((string-match "-" current
) (if prevp
"1)" "+"))
666 ((string-match "\\+" current
)
667 (if prevp
"-" (if (looking-at "\\S-") "1." "*")))
668 ((string-match "\\*" current
) (if prevp
"+" "1."))
669 ((string-match "\\." current
)
670 (if prevp
(if (looking-at "\\S-") "+" "*") "1)"))
671 ((string-match ")" current
) (if prevp
"1." "-"))
672 (t (error "This should not happen"))))
673 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
674 (setq old
(match-string 2))
675 (replace-match (concat "\\1" new
)))
676 (org-shift-item-indentation (- (length new
) (length old
)))
677 (org-fix-bullet-type)
678 (org-maybe-renumber-ordered-list))))
680 (defun org-get-string-indentation (s)
681 "What indentation has S due to SPACE and TAB at the beginning of the string?"
682 (let ((n -
1) (i 0) (w tab-width
) c
)
684 (while (< (setq n
(1+ n
)) (length s
))
686 (cond ((= c ?\
) (setq i
(1+ i
)))
687 ((= c ?
\t) (setq i
(* (/ (+ w i
) w
) w
)))
688 (t (throw 'exit t
)))))
691 (defun org-renumber-ordered-list (arg)
692 "Renumber an ordered plain list.
693 Cursor needs to be in the first line of an item, the line that starts
694 with something like \"1.\" or \"2)\"."
696 (unless (and (org-at-item-p)
698 (error "This is not an ordered list"))
699 (let ((line (org-current-line))
700 (col (current-column))
701 (ind (org-get-string-indentation
702 (buffer-substring (point-at-bol) (match-beginning 3))))
703 ;; (term (substring (match-string 3) -1))
706 ;; find where this list begins
707 (org-beginning-of-item-list)
709 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
710 (setq fmt
(concat "%d" (match-string 1)))
711 (beginning-of-line 0)
712 ;; walk forward and replace these numbers
716 (if bobp
(setq bobp nil
) (beginning-of-line 2))
717 (if (eobp) (throw 'exit nil
))
718 (if (looking-at "[ \t]*$") (throw 'next nil
))
719 (skip-chars-forward " \t") (setq ind1
(current-column))
720 (if (> ind1 ind
) (throw 'next t
))
721 (if (< ind1 ind
) (throw 'exit t
))
722 (if (not (org-at-item-p)) (throw 'exit nil
))
723 (setq old
(match-string 2))
724 (delete-region (match-beginning 2) (match-end 2))
725 (goto-char (match-beginning 2))
726 (insert (setq new
(format fmt
(setq n
(1+ n
)))))
727 (org-shift-item-indentation (- (length new
) (length old
))))))
729 (org-move-to-column col
)))
731 (defun org-fix-bullet-type ()
732 "Make sure all items in this list have the same bullet as the first item.
733 Also, fix the indentation."
735 (unless (org-at-item-p) (error "This is not a list"))
736 (let ((line (org-current-line))
737 (col (current-column))
738 (ind (current-indentation))
739 ind1 bullet oldbullet
)
740 ;; find where this list begins
741 (org-beginning-of-item-list)
742 (beginning-of-line 1)
743 ;; find out what the bullet type is
744 (looking-at "[ \t]*\\(\\S-+\\)")
745 (setq bullet
(concat (match-string 1) " "))
746 (if (and org-list-two-spaces-after-bullet-regexp
747 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
748 (setq bullet
(concat bullet
" ")))
749 ;; walk forward and replace these numbers
750 (beginning-of-line 0)
754 (beginning-of-line 2)
755 (if (eobp) (throw 'exit nil
))
756 (if (looking-at "[ \t]*$") (throw 'next nil
))
757 (skip-chars-forward " \t") (setq ind1
(current-column))
758 (if (> ind1 ind
) (throw 'next t
))
759 (if (< ind1 ind
) (throw 'exit t
))
760 (if (not (org-at-item-p)) (throw 'exit nil
))
761 (skip-chars-forward " \t")
762 (looking-at "\\S-+ *")
763 (setq oldbullet
(match-string 0))
764 (replace-match bullet
)
765 (org-shift-item-indentation (- (length bullet
) (length oldbullet
))))))
767 (org-move-to-column col
)
768 (if (string-match "[0-9]" bullet
)
769 (org-renumber-ordered-list 1))))
771 (defun org-shift-item-indentation (delta)
772 "Shift the indentation in current item by DELTA."
774 (let ((beg (point-at-bol))
775 (end (progn (org-end-of-item) (point)))
778 (beginning-of-line 0)
779 (while (> (point) beg
)
780 (when (looking-at "[ \t]*\\S-")
781 ;; this is not an empty line
782 (setq i
(org-get-indentation))
783 (if (and (> i
0) (> (setq i
(+ i delta
)) 0))
785 (beginning-of-line 0)))))
787 (defun org-beginning-of-item-list ()
788 "Go to the beginning of the current item list.
789 I.e. to the first item in this list."
791 (org-beginning-of-item)
792 (let ((pos (point-at-bol))
793 (ind (org-get-indentation))
795 ;; find where this list begins
799 (beginning-of-line 0)
800 (if (looking-at "[ \t]*$")
801 (throw (if (bobp) 'exit
'next
) t
))
802 (skip-chars-forward " \t") (setq ind1
(current-column))
805 (not (org-at-item-p)))
806 (and (= (point-at-bol) (point-min))
807 (setq pos
(point-min))))
809 (when (org-at-item-p) (setq pos
(point-at-bol)))))))
813 (defun org-end-of-item-list ()
814 "Go to the end of the current item list.
815 I.e. to the text after the last item."
817 (org-beginning-of-item)
818 (let ((pos (point-at-bol))
819 (ind (org-get-indentation))
821 ;; find where this list begins
825 (beginning-of-line 2)
826 (if (looking-at "[ \t]*$")
827 (throw (if (eobp) 'exit
'next
) t
))
828 (skip-chars-forward " \t") (setq ind1
(current-column))
831 (not (org-at-item-p)))
834 (setq pos
(point-at-bol))
839 (defvar org-last-indent-begin-marker
(make-marker))
840 (defvar org-last-indent-end-marker
(make-marker))
842 (defun org-outdent-item (arg)
843 "Outdent a local list item."
845 (org-indent-item (- arg
)))
847 (defun org-indent-item (arg)
848 "Indent a local list item."
850 (unless (org-at-item-p)
851 (error "Not on an item"))
853 (let (beg end ind ind1 tmp delta ind-down ind-up
)
854 (if (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
855 (setq beg org-last-indent-begin-marker
856 end org-last-indent-end-marker
)
857 (org-beginning-of-item)
858 (setq beg
(move-marker org-last-indent-begin-marker
(point)))
860 (setq end
(move-marker org-last-indent-end-marker
(point))))
862 (setq tmp
(org-item-indent-positions)
867 (if ind-down
(- ind-down ind
) 2)
868 (if ind-up
(- ind-up ind
) -
2)))
869 (if (< (+ delta ind
) 0) (error "Cannot outdent beyond margin"))
870 (while (< (point) end
)
871 (beginning-of-line 1)
872 (skip-chars-forward " \t") (setq ind1
(current-column))
873 (delete-region (point-at-bol) (point))
874 (or (eolp) (org-indent-to-column (+ ind1 delta
)))
875 (beginning-of-line 2))))
876 (org-fix-bullet-type)
877 (org-maybe-renumber-ordered-list-safe)
879 (beginning-of-line 0)
880 (condition-case nil
(org-beginning-of-item) (error nil
))
881 (org-maybe-renumber-ordered-list-safe)))
883 (defun org-item-indent-positions ()
884 "Return indentation for plain list items.
885 This returns a list with three values: The current indentation, the
886 parent indentation and the indentation a child should have.
887 Assumes cursor in item line."
888 (let* ((bolpos (point-at-bol))
889 (ind (org-get-indentation))
892 (org-beginning-of-item-list)
893 (skip-chars-backward "\n\r \t")
894 (when (org-in-item-p)
895 (org-beginning-of-item)
896 (setq ind-up
(org-get-indentation))))
900 ((and (condition-case nil
(progn (org-previous-item) t
)
902 (or (forward-char 1) t
)
903 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t
))
904 (setq ind-down
(org-get-indentation)))
905 ((and (goto-char pos
)
907 (goto-char (match-end 0))
908 (skip-chars-forward " \t")
909 (setq ind-down
(current-column)))))
910 (list ind ind-up ind-down
)))
913 ;;; Send and receive lists
915 (defun org-list-parse-list (&optional delete
)
916 "Parse the list at point and maybe DELETE it.
917 Return a list containing first level items as strings and
918 sublevels as a list of strings."
919 (let* ((item-beginning (org-list-item-beginning))
920 (start (car item-beginning
))
921 (end (org-list-end (cdr item-beginning
)))
922 output itemsep ltype
)
923 (while (re-search-forward org-list-beginning-re end t
)
924 (goto-char (match-beginning 3))
926 (cond ((string-match "[0-9]" (match-string 2))
927 (setq itemsep
"[0-9]+\\(?:\\.\\|)\\)"
929 ((string-match "^.*::" (match-string 0))
930 (setq itemsep
"[-+]" ltype
'descriptive
))
931 (t (setq itemsep
"[-+]" ltype
'unordered
))))
932 (let* ((indent1 (match-string 1))
933 (nextitem (save-excursion
935 (or (and (re-search-forward
936 (concat "^" indent1 itemsep
" *?") end t
)
937 (match-beginning 0)) end
))))
938 (item (buffer-substring
940 (or (and (re-search-forward
941 org-list-beginning-re end t
)
942 (goto-char (match-beginning 0)))
944 (nextindent (match-string 1))
945 (item (org-trim item
))
946 (item (if (string-match "^\\[\\([xX ]\\)\\]" item
)
947 (replace-match (if (equal (match-string 1 item
) " ")
953 (when (> (length nextindent
)
955 (narrow-to-region (point) nextitem
)
956 (push (org-list-parse-list) output
)
958 (when delete
(delete-region start end
))
959 (setq output
(nreverse output
))
960 (push ltype output
)))
962 (defun org-list-item-beginning ()
963 "Find the beginning of the list item.
964 Return a cons which car is the beginning position of the item and
965 cdr is the indentation string."
967 (if (not (or (looking-at org-list-beginning-re
)
969 org-list-beginning-re nil t
)))
970 (progn (goto-char (point-min)) (point))
971 (cons (match-beginning 0) (match-string 1)))))
973 (defun org-list-end (indent)
974 "Return the position of the end of the list.
975 INDENT is the indentation of the list."
978 (while (or (looking-at org-list-beginning-re
)
979 (looking-at (concat "^" indent
"[ \t]+\\|^$")))
980 (if (eq (point) (point-max))
981 (throw 'exit
(point-max)))
982 (forward-line 1))) (point)))
984 (defun org-list-insert-radio-list ()
985 "Insert a radio list template appropriate for this major mode."
987 (let* ((e (assq major-mode org-list-radio-list-templates
))
990 (unless e
(error "No radio list setup defined for %s" major-mode
))
991 (setq name
(read-string "List name: "))
992 (while (string-match "%n" txt
)
993 (setq txt
(replace-match name t t txt
)))
994 (or (bolp) (insert "\n"))
999 (defun org-list-send-list (&optional maybe
)
1000 "Send a tranformed version of this list to the receiver position.
1001 With argument MAYBE, fail quietly if no transformation is defined for
1005 (unless (org-at-item-p) (error "Not at a list"))
1007 (goto-char (car (org-list-item-beginning)))
1008 (beginning-of-line 0)
1009 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1012 (error "Don't know how to transform this list"))))
1013 (let* ((name (match-string 1))
1014 (item-beginning (org-list-item-beginning))
1015 (transform (intern (match-string 2)))
1016 (txt (buffer-substring-no-properties
1017 (car item-beginning
)
1018 (org-list-end (cdr item-beginning
))))
1019 (list (org-list-parse-list))
1021 (unless (fboundp transform
)
1022 (error "No such transformation function %s" transform
))
1023 (setq txt
(funcall transform list
))
1024 ;; Find the insertion place
1026 (goto-char (point-min))
1027 (unless (re-search-forward
1028 (concat "BEGIN RECEIVE ORGLST +" name
"\\([ \t]\\|$\\)") nil t
)
1029 (error "Don't know where to insert translated list"))
1030 (goto-char (match-beginning 0))
1031 (beginning-of-line 2)
1033 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
1034 (error "Cannot find end of insertion region"))
1035 (beginning-of-line 1)
1036 (delete-region beg
(point))
1039 (message "List converted and installed at receiver location"))))
1041 (defun org-list-to-generic (list params
)
1042 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1044 Valid parameters PARAMS are
1046 :ustart String to start an unordered list
1047 :uend String to end an unordered list
1049 :ostart String to start an ordered list
1050 :oend String to end an ordered list
1052 :dstart String to start a descriptive list
1053 :dend String to end a descriptive list
1054 :dtstart String to start a descriptive term
1055 :dtend String to end a descriptive term
1056 :ddstart String to start a description
1057 :ddend String to end a description
1059 :splice When set to t, return only list body lines, don't wrap
1060 them into :[u/o]start and :[u/o]end. Default is nil.
1062 :istart String to start a list item
1063 :iend String to end a list item
1064 :isep String to separate items
1065 :lsep String to separate sublists
1067 :cboff String to insert for an unchecked checkbox
1068 :cbon String to insert for a checked checkbox"
1070 (let* ((p params
) sublist
1071 (splicep (plist-get p
:splice
))
1072 (ostart (plist-get p
:ostart
))
1073 (oend (plist-get p
:oend
))
1074 (ustart (plist-get p
:ustart
))
1075 (uend (plist-get p
:uend
))
1076 (dstart (plist-get p
:dstart
))
1077 (dend (plist-get p
:dend
))
1078 (dtstart (plist-get p
:dtstart
))
1079 (dtend (plist-get p
:dtend
))
1080 (ddstart (plist-get p
:ddstart
))
1081 (ddend (plist-get p
:ddend
))
1082 (istart (plist-get p
:istart
))
1083 (iend (plist-get p
:iend
))
1084 (isep (plist-get p
:isep
))
1085 (lsep (plist-get p
:lsep
))
1086 (cbon (plist-get p
:cbon
))
1087 (cboff (plist-get p
:cboff
)))
1089 (cond ((eq (car list
) 'ordered
)
1090 (concat ostart
"\n%s" oend
"\n"))
1091 ((eq (car list
) 'unordered
)
1092 (concat ustart
"\n%s" uend
"\n"))
1093 ((eq (car list
) 'descriptive
)
1094 (concat dstart
"\n%s" dend
"\n"))))
1095 rtn term defstart defend
)
1096 (while (setq sublist
(pop list
))
1097 (cond ((symbolp sublist
) nil
)
1099 (when (string-match "^\\(.*\\) ::" sublist
)
1100 (setq term
(org-trim (format (concat dtstart
"%s" dtend
)
1101 (match-string 1 sublist
))))
1102 (setq sublist
(substring sublist
(1+ (length term
)))))
1103 (if (string-match "\\[CBON\\]" sublist
)
1104 (setq sublist
(replace-match cbon t t sublist
)))
1105 (if (string-match "\\[CBOFF\\]" sublist
)
1106 (setq sublist
(replace-match cboff t t sublist
)))
1107 (setq rtn
(concat rtn istart term ddstart
1108 sublist ddend iend isep
)))
1109 (t (setq rtn
(concat rtn
;; previous list
1110 lsep
;; list separator
1111 (org-list-to-generic sublist p
)
1112 lsep
;; list separator
1114 (format wrapper rtn
))))
1116 (defun org-list-to-latex (list &optional params
)
1117 "Convert LIST into a LaTeX list.
1118 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1119 with overruling parameters for `org-list-to-generic'."
1120 (org-list-to-generic
1123 '(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
1124 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
1125 :dstart
"\\begin{description}" :dend
"\\end{description}"
1126 :dtstart
"[" :dtend
"]"
1127 :ddstart
"" :ddend
""
1128 :istart
"\\item " :iend
""
1129 :isep
"\n" :lsep
"\n"
1130 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
1133 (defun org-list-to-html (list &optional params
)
1134 "Convert LIST into a HTML list.
1135 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1136 with overruling parameters for `org-list-to-generic'."
1137 (org-list-to-generic
1140 '(:splicep nil
:ostart
"<ol>" :oend
"</ol>"
1141 :ustart
"<ul>" :uend
"</ul>"
1142 :dstart
"<dl>" :dend
"</dl>"
1143 :dtstart
"<dt>" :dtend
"</dt>"
1144 :ddstart
"<dd>" :ddend
"</dd>"
1145 :istart
"<li>" :iend
"</li>"
1146 :isep
"\n" :lsep
"\n"
1147 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>")
1150 (defun org-list-to-texinfo (list &optional params
)
1151 "Convert LIST into a Texinfo list.
1152 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1153 with overruling parameters for `org-list-to-generic'."
1154 (org-list-to-generic
1157 '(:splicep nil
:ostart
"@itemize @minus" :oend
"@end itemize"
1158 :ustart
"@enumerate" :uend
"@end enumerate"
1159 :dstart
"@table" :dend
"@end table"
1160 :dtstart
"@item " :dtend
"\n"
1161 :ddstart
"" :ddend
""
1162 :istart
"@item\n" :iend
""
1163 :isep
"\n" :lsep
"\n"
1164 :cbon
"@code{[X]}" :cboff
"@code{[ ]}")
1169 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1170 ;;; org-list.el ends here