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
)
39 (defvar org-complex-heading-regexp
)
40 (defvar org-odd-levels-only
)
42 (declare-function org-invisible-p
"org" ())
43 (declare-function org-on-heading-p
"org" (&optional invisible-ok
))
44 (declare-function outline-next-heading
"outline" ())
45 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
46 (declare-function org-back-over-empty-lines
"org" ())
47 (declare-function org-skip-whitespace
"org" ())
48 (declare-function org-trim
"org" (s))
49 (declare-function org-get-indentation
"org" (&optional line
))
50 (declare-function org-timer-item
"org-timer" (&optional arg
))
51 (declare-function org-combine-plists
"org" (&rest plists
))
52 (declare-function org-entry-get
"org" (pom property
&optional inherit
))
53 (declare-function org-narrow-to-subtree
"org" ())
54 (declare-function org-show-subtree
"org" ())
56 (defgroup org-plain-lists nil
57 "Options concerning plain lists in Org-mode."
58 :tag
"Org Plain lists"
59 :group
'org-structure
)
61 (defcustom org-cycle-include-plain-lists t
62 "When t, make TAB cycle visibility on plain list items.
64 Cycling plain lists works only when the cursor is on a plain list
65 item. When the cursor is on an outline heading, plain lists are
66 treated as text. This is the most stable way of handling this,
67 which is why it is the default.
69 When this is the symbol `integrate', then during cycling, plain
70 list items will *temporarily* be interpreted as outline headlines
71 with a level given by 1000+i where i is the indentation of the
72 bullet. This setting can lead to strange effects when switching
73 visibility to `children', because the first \"child\" in a
74 subtree decides what children should be listed. If that first
75 \"child\" is a plain list item with an implied large level
76 number, all true children and grand children of the outline
77 heading will be exposed in a children' view."
78 :group
'org-plain-lists
80 (const :tag
"Never" nil
)
81 (const :tag
"With cursor in plain list (recommended)" t
)
82 (const :tag
"As children of outline headings" integrate
)))
84 (defcustom org-list-demote-modify-bullet nil
85 "Default bullet type installed when demoting an item.
86 This is an association list, for each bullet type, this alist will point
87 to the bulled that should be used when this item is demoted."
88 :group
'org-plain-lists
91 (choice :tag
"If the current bullet is "
97 (choice :tag
"demotion will change it to"
104 (defcustom org-plain-list-ordered-item-terminator t
105 "The character that makes a line with leading number an ordered list item.
106 Valid values are ?. and ?\). To get both terminators, use t. While
107 ?. may look nicer, it creates the danger that a line with leading
108 number may be incorrectly interpreted as an item. ?\) therefore is
110 :group
'org-plain-lists
111 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
112 (const :tag
"paren like in \"2)\"" ?\
))
113 (const :tab
"both" t
)))
115 (defcustom org-list-two-spaces-after-bullet-regexp nil
116 "A regular expression matching bullets that should have 2 spaces after them.
117 When nil, no bullet will have two spaces after them.
118 When a string, it will be used as a regular expression. When the bullet
119 type of a list is changed, the new bullet type will be matched against this
120 regexp. If it matches, there will be two spaces instead of one after
121 the bullet in each item of he list."
122 :group
'org-plain-list
124 (const :tag
"never" nil
)
127 (defcustom org-empty-line-terminates-plain-lists nil
128 "Non-nil means, an empty line ends all plain list levels.
129 This is currently effective only during export. It should also have
130 an effect for indentation and plain list folding, but it does not.
131 When nil, empty lines are part of the preceding item."
132 :group
'org-plain-lists
135 (defcustom org-auto-renumber-ordered-lists t
136 "Non-nil means, automatically renumber ordered plain lists.
137 Renumbering happens when the sequence have been changed with
138 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
139 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
140 :group
'org-plain-lists
143 (defcustom org-provide-checkbox-statistics t
144 "Non-nil means, update checkbox statistics after insert and toggle.
145 When this is set, checkbox statistics is updated each time you
146 either insert a new checkbox with \\[org-insert-todo-heading] or
147 toggle a checkbox with \\[org-ctrl-c-ctrl-c]."
148 :group
'org-plain-lists
151 (defcustom org-hierarchical-checkbox-statistics t
152 "Non-nil means, checkbox statistics counts only the state of direct children.
153 When nil, all boxes below the cookie are counted.
154 This can be set to nil on a per-node basis using a COOKIE_DATA property
155 with the word \"recursive\" in the value."
156 :group
'org-plain-lists
159 (defcustom org-description-max-indent
20
160 "Maximum indentation for the second line of a description list.
161 When the indentation would be larger than this, it will become
162 5 characters instead."
163 :group
'org-plain-lists
166 (defvar org-list-beginning-re
167 "^\\([ \t]*\\)\\([-+]\\|[0-9]+[.)]\\) +\\(.*\\)$")
169 (defcustom org-list-radio-list-templates
170 '((latex-mode "% BEGIN RECEIVE ORGLST %n
171 % END RECEIVE ORGLST %n
173 #+ORGLST: SEND %n org-list-to-latex
176 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
177 @c END RECEIVE ORGLST %n
179 #+ORGLST: SEND %n org-list-to-texinfo
182 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
183 <!-- END RECEIVE ORGLST %n -->
185 #+ORGLST: SEND %n org-list-to-html
188 "Templates for radio lists in different major modes.
189 All occurrences of %n in a template will be replaced with the name of the
190 list, obtained by prompting the user."
191 :group
'org-plain-lists
193 (list (symbol :tag
"Major mode")
194 (string :tag
"Format"))))
196 ;;;; Plain list items, including checkboxes
200 (defun org-at-item-p ()
201 "Is point in a line starting a hand-formatted item?"
202 (let ((llt org-plain-list-ordered-item-terminator
))
204 (goto-char (point-at-bol))
207 ((eq llt t
) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
208 ((= llt ?.
) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
209 ((= llt ?\
)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
210 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
212 (defun org-at-item-bullet-p ()
213 "Is point at the bullet of a plain list item?"
215 (not (member (char-after) '(?\ ?
\t)))
216 (< (point) (match-end 0))))
218 (defun org-in-item-p ()
219 "It the cursor inside a plain list item.
220 Does not have to be the first line."
224 (org-beginning-of-item)
229 (defun org-insert-item (&optional checkbox
)
230 "Insert a new item at the current level.
231 Return t when things worked, nil when we are not in an item."
232 (when (save-excursion
235 (org-beginning-of-item)
237 (if (org-invisible-p) (error "Invisible item"))
240 (let* ((bul (match-string 0))
241 (descp (save-excursion (goto-char (match-beginning 0))
242 (beginning-of-line 1)
244 (and (looking-at "[ \t]*\\(.*?\\) ::")
246 (empty-line-p (save-excursion
247 (goto-char (match-beginning 0))
249 (or (beginning-of-line 0) t
)
251 (looking-at "[ \t]*$")))))
254 (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
256 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
258 (blank-a (if org-empty-line-terminates-plain-lists
260 (cdr (assq 'plain-list-item org-blank-before-new-entry
))))
261 (blank (if (eq blank-a
'auto
) empty-line-p blank-a
))
263 (if descp
(setq checkbox nil
))
265 (progn (org-timer-item) t
)
267 ((and (org-at-item-p) (<= (point) eow
))
269 (beginning-of-line 1)
270 (open-line (if blank
2 1)))
272 (beginning-of-line 1))
274 (unless (org-get-alist-option org-M-RET-may-split-line
'item
)
276 (delete-horizontal-space))
277 (newline (if blank
2 1))))
279 (if checkbox
"[ ]" "")
280 (if descp
(concat (if checkbox
" " "")
281 (read-string "Term: ") " :: ") ""))
285 (unless (= (point) pos
) (just-one-space) (backward-delete-char 1)))
286 (org-maybe-renumber-ordered-list)
287 (and checkbox
(org-update-checkbox-count-maybe))
292 (defun org-at-item-checkbox-p ()
293 "Is point at a line starting a plain-list item with a checklet?"
296 (goto-char (match-end 0))
297 (skip-chars-forward " \t")
298 (looking-at "\\[[- X]\\]"))))
300 (defun org-toggle-checkbox (&optional toggle-presence
)
301 "Toggle the checkbox in the current line.
302 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
303 With double prefix, set checkbox to [-].
304 When there is an active region, toggle status or presence of the checkbox
305 in the first line, and make every item in the region have the same
306 status or presence, respectively.
307 If the cursor is in a headline, apply this to all checkbox items in the
308 text below the heading."
311 (let (beg end status first-present first-status blocked
)
313 ((org-region-active-p)
314 (setq beg
(region-beginning) end
(region-end)))
316 (setq beg
(point) end
(save-excursion (outline-next-heading) (point))))
317 ((org-at-item-checkbox-p)
319 (if (equal toggle-presence
'(4))
322 (goto-char (match-beginning 0))
324 (when (setq blocked
(org-checkbox-blocked-p))
325 (error "Checkbox blocked because of unchecked box in line %d"
328 (cond ((equal toggle-presence
'(16)) "[-]")
329 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
336 (goto-char (match-end 0))
339 (t (error "Not at a checkbox or heading, and no active region")))
340 (setq end
(move-marker (make-marker) end
))
343 (setq first-present
(org-at-item-checkbox-p)
346 (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t
)
347 (equal (match-string 1) "[X]"))))
348 (while (< (point) end
)
351 ((and first-present
(org-at-item-checkbox-p))
354 (goto-char (match-beginning 0))
356 ((and (not first-present
) (not (org-at-item-checkbox-p))
359 (goto-char (match-end 0))
361 (when (org-at-item-checkbox-p)
362 (setq status
(equal (match-string 0) "[X]"))
364 (if first-status
"[ ]" "[X]") t t
)))
365 (beginning-of-line 2)))))
366 (org-update-checkbox-count-maybe))
368 (defun org-reset-checkbox-state-subtree ()
369 "Reset all checkboxes in an entry subtree."
373 (org-narrow-to-subtree)
375 (goto-char (point-min))
376 (let ((end (point-max)))
377 (while (< (point) end
)
378 (when (org-at-item-checkbox-p)
379 (replace-match "[ ]" t t
))
380 (beginning-of-line 2))))
381 (org-update-checkbox-count-maybe)))
383 (defun org-checkbox-blocked-p ()
384 "Is the current checkbox blocked from for being checked now?
385 A checkbox is blocked if all of the following conditions are fulfilled:
387 1. The checkbox is not checked already.
388 2. The current entry has the ORDERED property set.
389 3. There is an unchecked checkbox in this entry before the current line."
393 (unless (org-at-item-checkbox-p) (throw 'exit nil
))
394 (when (equal (match-string 0) "[X]")
395 ;; the box is already checked!
397 (let ((end (point-at-bol)))
398 (condition-case nil
(org-back-to-heading t
)
399 (error (throw 'exit nil
)))
400 (unless (org-entry-get nil
"ORDERED") (throw 'exit nil
))
401 (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t
)
405 (defvar org-checkbox-statistics-hook nil
406 "Hook that is run whenever Org thinks checkbox statistics should be updated.
407 This hook runs even if `org-provide-checkbox-statistics' is nil, to it can
408 be used to implement alternative ways of collecting statistics information.")
410 (defun org-update-checkbox-count-maybe ()
411 "Update checkbox statistics unless turned off by user."
412 (when org-provide-checkbox-statistics
413 (org-update-checkbox-count))
414 (run-hooks 'org-checkbox-statistics-hook
))
416 (defun org-update-checkbox-count (&optional all
)
417 "Update the checkbox statistics in the current section.
418 This will find all statistic cookies like [57%] and [6/12] and update them
419 with the current numbers. With optional prefix argument ALL, do this for
423 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
424 (beg (condition-case nil
425 (progn (org-back-to-heading) (point))
426 (error (point-min))))
427 (end (move-marker (make-marker)
428 (progn (outline-next-heading) (point))))
429 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
430 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
431 (re-find (concat re
"\\|" re-box
))
432 beg-cookie end-cookie is-percent c-on c-off lim new
433 eline curr-ind next-ind continue-from startsearch
435 (or (not org-hierarchical-checkbox-statistics
)
436 (string-match "\\<recursive\\>"
437 (or (org-entry-get nil
"COOKIE_DATA") ""))))
441 (goto-char (point-min))
442 (outline-next-heading)
443 (setq beg
(point) end
(point-max)))
445 ;; find each statistics cookie
446 (while (and (re-search-backward re-find beg t
)
447 (not (save-match-data
448 (and (org-on-heading-p)
449 (string-match "\\<todo\\>"
454 (setq beg-cookie
(match-beginning 1)
455 end-cookie
(match-end 1)
456 cstat
(+ cstat
(if end-cookie
1 0))
457 startsearch
(point-at-eol)
458 continue-from
(match-beginning 0)
459 is-percent
(match-beginning 2)
461 ((org-on-heading-p) (outline-next-heading) (point))
462 ((org-at-item-p) (org-end-of-item) (point))
467 ;; find first checkbox for this cookie and gather
468 ;; statistics from all that are at this indentation level
469 (goto-char startsearch
)
470 (if (re-search-forward re-box lim t
)
472 (org-beginning-of-item)
473 (setq curr-ind
(org-get-indentation))
474 (setq next-ind curr-ind
)
475 (while (and (bolp) (org-at-item-p)
477 (<= curr-ind next-ind
)
478 (= curr-ind next-ind
)))
479 (save-excursion (end-of-line) (setq eline
(point)))
480 (if (re-search-forward re-box eline t
)
481 (if (member (match-string 2) '("[ ]" "[-]"))
482 (setq c-off
(1+ c-off
))
483 (setq c-on
(1+ c-on
))))
487 (when (re-search-forward org-list-beginning-re lim t
)
488 (beginning-of-line)))
489 (setq next-ind
(org-get-indentation)))))
490 (goto-char continue-from
)
493 (setq new
(if is-percent
494 (format "[%d%%]" (/ (* 100 c-on
) (max 1 (+ c-on c-off
))))
495 (format "[%d/%d]" c-on
(+ c-on c-off
))))
496 (goto-char beg-cookie
)
498 (delete-region (point) (+ (point) (- end-cookie beg-cookie
))))
499 ;; update items checkbox if it has one
500 (when (org-at-item-p)
501 (org-beginning-of-item)
502 (when (and (> (+ c-on c-off
) 0)
503 (re-search-forward re-box
(point-at-eol) t
))
504 (setq beg-cookie
(match-beginning 2)
505 end-cookie
(match-end 2))
506 (delete-region beg-cookie end-cookie
)
507 (goto-char beg-cookie
)
508 (cond ((= c-off
0) (insert "[X]"))
509 ((= c-on
0) (insert "[ ]"))
512 (goto-char continue-from
))
513 (when (interactive-p)
514 (message "Checkbox statistics updated %s (%d places)"
515 (if all
"in entire file" "in current outline entry") cstat
)))))
517 (defun org-get-checkbox-statistics-face ()
518 "Select the face for checkbox statistics.
519 The face will be `org-done' when all relevant boxes are checked. Otherwise
520 it will be `org-todo'."
522 (if (equal (match-string 1) "100%")
523 'org-checkbox-statistics-done
524 'org-checkbox-statistics-todo
)
525 (if (and (> (match-end 2) (match-beginning 2))
526 (equal (match-string 2) (match-string 3)))
527 'org-checkbox-statistics-done
528 'org-checkbox-statistics-todo
)))
530 (defun org-beginning-of-item ()
531 "Go to the beginning of the current hand-formatted item.
532 If the cursor is not in an item, throw an error."
535 (limit (save-excursion
538 (org-back-to-heading)
539 (beginning-of-line 2) (point))
540 (error (point-min)))))
541 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
544 (beginning-of-line 1)
545 (beginning-of-line 1)
546 (skip-chars-forward " \t")
547 (setq ind
(current-column))
550 (beginning-of-line 0)
551 (if (or (bobp) (< (point) limit
)) (throw 'exit nil
))
553 (if (looking-at "[ \t]*$")
554 (setq ind1 ind-empty
)
555 (skip-chars-forward " \t")
556 (setq ind1
(current-column)))
558 (progn (beginning-of-line 1) (throw 'exit
(org-at-item-p))))))
561 (error "Not in an item")))))
563 (defun org-end-of-item ()
564 "Go to the end of the current hand-formatted item.
565 If the cursor is not in an item, throw an error."
569 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
570 (limit (save-excursion (outline-next-heading) (point)))
572 (org-beginning-of-item)
573 (skip-chars-forward " \t")
577 (beginning-of-line 2)
578 (if (eobp) (throw 'exit
(point)))
579 (if (>= (point) limit
) (throw 'exit
(point-at-bol)))
580 (if (looking-at "[ \t]*$")
581 (setq ind1 ind-empty
)
582 (skip-chars-forward " \t")
583 (setq ind1
(current-column)))
585 (throw 'exit
(point-at-bol)))))))
589 (error "Not in an item"))))
591 (defun org-next-item ()
592 "Move to the beginning of the next item in the current plain list.
593 Error if not at a plain list, or if this is the last item in the list."
595 (let (ind ind1
(pos (point)))
596 (org-beginning-of-item)
597 (setq ind
(org-get-indentation))
599 (setq ind1
(org-get-indentation))
600 (unless (and (org-at-item-p) (= ind ind1
))
602 (error "On last item"))))
604 (defun org-previous-item ()
605 "Move to the beginning of the previous item in the current plain list.
606 Error if not at a plain list, or if this is the first item in the list."
608 (let (beg ind ind1
(pos (point)))
609 (org-beginning-of-item)
611 (setq ind
(org-get-indentation))
615 (beginning-of-line 0)
616 (if (looking-at "[ \t]*$")
618 (if (<= (setq ind1
(org-get-indentation)) ind
)
620 (if (bobp) (throw 'exit t
))))
622 (if (or (not (org-at-item-p))
625 (org-beginning-of-item))
626 (error (goto-char pos
)
627 (error "On first item")))))
629 (defun org-first-list-item-p ()
630 "Is this heading the first item in a plain list?"
631 (unless (org-at-item-p)
632 (error "Not at a plain list item"))
634 (org-beginning-of-item)
635 (= (point) (save-excursion (org-beginning-of-item-list)))))
637 (defun org-move-item-down ()
638 "Move the plain list item at point down, i.e. swap with following item.
639 Subitems (items with larger indentation) are considered part of the item,
640 so this really moves item trees."
642 (let ((col (current-column))
644 beg beg0 end end0 ind ind1 txt ne-end ne-beg
)
645 (org-beginning-of-item)
648 (setq ne-beg
(org-back-over-empty-lines))
651 (setq ind
(org-get-indentation))
654 (setq ind1
(org-get-indentation))
655 (setq ne-end
(org-back-over-empty-lines))
658 (when (and (org-first-list-item-p) (< ne-end ne-beg
))
659 ;; include less whitespace
662 (forward-line (- ne-beg ne-end
))
665 (if (and (org-at-item-p) (= ind ind1
))
668 (org-back-over-empty-lines)
669 (setq txt
(buffer-substring beg end
))
671 (delete-region beg end
))
674 (goto-char pos
) (org-skip-whitespace)
675 (org-maybe-renumber-ordered-list)
676 (move-to-column col
))
679 (error "Cannot move this item further down"))))
681 (defun org-move-item-up (arg)
682 "Move the plain list item at point up, i.e. swap with previous item.
683 Subitems (items with larger indentation) are considered part of the item,
684 so this really moves item trees."
686 (let ((col (current-column)) (pos (point))
687 beg beg0 end ind ind1 txt
688 ne-beg ne-ins ins-end
)
689 (org-beginning-of-item)
691 (setq ind
(org-get-indentation))
693 (setq ne-beg
(org-back-over-empty-lines))
697 (org-back-over-empty-lines)
702 (beginning-of-line 0)
703 (if (looking-at "[ \t]*$")
704 (if org-empty-line-terminates-plain-lists
707 (error "Cannot move this item further up"))
709 (if (<= (setq ind1
(org-get-indentation)) ind
)
712 (org-beginning-of-item)
713 (error (goto-char beg0
)
715 (error "Cannot move this item further up")))
716 (setq ind1
(org-get-indentation))
717 (if (and (org-at-item-p) (= ind ind1
))
719 (setq ne-ins
(org-back-over-empty-lines))
720 (setq txt
(buffer-substring beg end
))
722 (delete-region beg end
))
725 (setq ins-end
(point))
726 (goto-char pos
) (org-skip-whitespace)
728 (when (and (org-first-list-item-p) (> ne-ins ne-beg
))
729 ;; Move whitespace back to beginning
732 (let ((kill-whole-line t
))
733 (kill-line (- ne-ins ne-beg
)) (point)))
734 (insert (make-string (- ne-ins ne-beg
) ?
\n)))
736 (org-maybe-renumber-ordered-list)
737 (move-to-column col
))
740 (error "Cannot move this item further up"))))
742 (defun org-maybe-renumber-ordered-list ()
743 "Renumber the ordered list at point if setup allows it.
744 This tests the user option `org-auto-renumber-ordered-lists' before
745 doing the renumbering."
747 (when (and org-auto-renumber-ordered-lists
749 (if (match-beginning 3)
750 (org-renumber-ordered-list 1)
751 (org-fix-bullet-type))))
753 (defun org-maybe-renumber-ordered-list-safe ()
756 (org-maybe-renumber-ordered-list))
759 (defun org-cycle-list-bullet (&optional which
)
760 "Cycle through the different itemize/enumerate bullets.
761 This cycle the entire list level through the sequence:
763 `-' -> `+' -> `*' -> `1.' -> `1)'
765 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
766 0 means `-', 1 means `+' etc."
769 (org-beginning-of-item-list)
771 (beginning-of-line 1)
772 (let ((current (match-string 0))
773 (prevp (eq which
'previous
))
776 ((and (numberp which
)
777 (nth (1- which
) '("-" "+" "*" "1." "1)"))))
778 ((string-match "-" current
) (if prevp
"1)" "+"))
779 ((string-match "\\+" current
)
780 (if prevp
"-" (if (looking-at "\\S-") "1." "*")))
781 ((string-match "\\*" current
) (if prevp
"+" "1."))
782 ((string-match "\\." current
)
783 (if prevp
(if (looking-at "\\S-") "+" "*") "1)"))
784 ((string-match ")" current
) (if prevp
"1." "-"))
785 (t (error "This should not happen"))))
786 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
787 (setq old
(match-string 2))
788 (replace-match (concat "\\1" new
)))
789 (org-shift-item-indentation (- (length new
) (length old
)))
790 (org-fix-bullet-type)
791 (org-maybe-renumber-ordered-list))))
793 (defun org-get-string-indentation (s)
794 "What indentation has S due to SPACE and TAB at the beginning of the string?"
795 (let ((n -
1) (i 0) (w tab-width
) c
)
797 (while (< (setq n
(1+ n
)) (length s
))
799 (cond ((= c ?\
) (setq i
(1+ i
)))
800 ((= c ?
\t) (setq i
(* (/ (+ w i
) w
) w
)))
801 (t (throw 'exit t
)))))
804 (defun org-renumber-ordered-list (arg)
805 "Renumber an ordered plain list.
806 Cursor needs to be in the first line of an item, the line that starts
807 with something like \"1.\" or \"2)\"."
809 (unless (and (org-at-item-p)
811 (error "This is not an ordered list"))
812 (let ((line (org-current-line))
813 (col (current-column))
814 (ind (org-get-string-indentation
815 (buffer-substring (point-at-bol) (match-beginning 3))))
816 ;; (term (substring (match-string 3) -1))
818 fmt bobp old new delta
)
819 ;; find where this list begins
820 (org-beginning-of-item-list)
822 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
823 (setq fmt
(concat "%d" (or (match-string 1) ".")))
824 (beginning-of-line 0)
825 ;; walk forward and replace these numbers
829 (if bobp
(setq bobp nil
) (beginning-of-line 2))
830 (if (eobp) (throw 'exit nil
))
831 (if (looking-at "[ \t]*$") (throw 'next nil
))
832 (skip-chars-forward " \t") (setq ind1
(current-column))
833 (if (> ind1 ind
) (throw 'next t
))
834 (if (< ind1 ind
) (throw 'exit t
))
835 (if (not (org-at-item-p)) (throw 'exit nil
))
836 (setq old
(match-string 2))
837 (delete-region (match-beginning 2) (match-end 2))
838 (goto-char (match-beginning 2))
839 (insert (setq new
(format fmt
(setq n
(1+ n
)))))
840 (setq delta
(- (length new
) (length old
)))
841 (org-shift-item-indentation delta
)
842 (if (= (org-current-line) line
) (setq col
(+ col delta
))))))
844 (org-move-to-column col
)))
846 (defvar org-suppress-item-indentation
) ; dynamically scoped parameter
847 (defun org-fix-bullet-type (&optional force-bullet
)
848 "Make sure all items in this list have the same bullet as the first item.
849 Also, fix the indentation."
851 (unless (org-at-item-p) (error "This is not a list"))
852 (let ((line (org-current-line))
853 (col (current-column))
854 (ind (current-indentation))
855 ind1 bullet oldbullet
)
856 ;; find where this list begins
857 (org-beginning-of-item-list)
858 (beginning-of-line 1)
859 ;; find out what the bullet type is
860 (looking-at "[ \t]*\\(\\S-+\\)")
861 (setq bullet
(concat (or force-bullet
(match-string 1)) " "))
862 (if (and org-list-two-spaces-after-bullet-regexp
863 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
864 (setq bullet
(concat bullet
" ")))
865 ;; walk forward and replace these numbers
866 (beginning-of-line 0)
870 (beginning-of-line 2)
871 (if (eobp) (throw 'exit nil
))
872 (if (looking-at "[ \t]*$") (throw 'next nil
))
873 (skip-chars-forward " \t") (setq ind1
(current-column))
874 (if (> ind1 ind
) (throw 'next t
))
875 (if (< ind1 ind
) (throw 'exit t
))
876 (if (not (org-at-item-p)) (throw 'exit nil
))
877 (skip-chars-forward " \t")
878 (looking-at "\\S-+ *")
879 (setq oldbullet
(match-string 0))
880 (unless (equal bullet oldbullet
) (replace-match bullet
))
881 (org-shift-item-indentation (- (length bullet
)
882 (length oldbullet
))))))
884 (org-move-to-column col
)
885 (if (string-match "[0-9]" bullet
)
886 (org-renumber-ordered-list 1))))
888 (defun org-shift-item-indentation (delta)
889 "Shift the indentation in current item by DELTA."
890 (unless (org-bound-and-true-p org-suppress-item-indentation
)
892 (let ((beg (point-at-bol))
893 (end (progn (org-end-of-item) (point)))
896 (beginning-of-line 0)
897 (while (> (point) beg
)
898 (when (looking-at "[ \t]*\\S-")
899 ;; this is not an empty line
900 (setq i
(org-get-indentation))
901 (if (and (> i
0) (> (setq i
(+ i delta
)) 0))
903 (beginning-of-line 0))))))
905 (defun org-beginning-of-item-list ()
906 "Go to the beginning of the current item list.
907 I.e. to the first item in this list."
909 (org-beginning-of-item)
910 (let ((pos (point-at-bol))
911 (ind (org-get-indentation))
913 ;; find where this list begins
917 (beginning-of-line 0)
918 (if (looking-at "[ \t]*$")
919 (throw (if (bobp) 'exit
'next
) t
))
920 (skip-chars-forward " \t") (setq ind1
(current-column))
923 (not (org-at-item-p)))
924 (and (= (point-at-bol) (point-min))
925 (setq pos
(point-min))))
927 (when (org-at-item-p) (setq pos
(point-at-bol)))))))
930 (defun org-end-of-item-list ()
931 "Go to the end of the current item list.
932 I.e. to the text after the last item."
934 (org-beginning-of-item)
935 (let ((pos (point-at-bol))
936 (ind (org-get-indentation))
938 ;; find where this list begins
942 (beginning-of-line 2)
943 (if (looking-at "[ \t]*$")
945 (progn (setq pos
(point)) (throw 'exit t
))
947 (skip-chars-forward " \t") (setq ind1
(current-column))
950 (not (org-at-item-p)))
953 (setq pos
(point-at-bol))
958 (defvar org-last-indent-begin-marker
(make-marker))
959 (defvar org-last-indent-end-marker
(make-marker))
961 (defun org-outdent-item (arg)
962 "Outdent a local list item."
964 (org-indent-item (- arg
)))
966 (defun org-indent-item (arg)
967 "Indent a local list item."
969 (and (org-region-active-p) (org-cursor-to-region-beginning))
970 (unless (org-at-item-p)
971 (error "Not on an item"))
972 (let (beg end ind ind1 ind-bul delta ind-down ind-up firstp
)
973 (setq firstp
(org-first-list-item-p))
975 (setq end
(and (org-region-active-p) (region-end)))
976 (if (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
977 (setq beg org-last-indent-begin-marker
978 end org-last-indent-end-marker
)
979 (org-beginning-of-item)
980 (setq beg
(move-marker org-last-indent-begin-marker
(point)))
982 (setq end
(move-marker org-last-indent-end-marker
(or end
(point)))))
984 (setq ind-bul
(org-item-indent-positions)
986 ind-down
(car (nth 2 ind-bul
))
987 ind-up
(car (nth 1 ind-bul
))
989 (if ind-down
(- ind-down ind
) 2)
990 (if ind-up
(- ind-up ind
) -
2)))
991 (if (< (+ delta ind
) 0) (error "Cannot outdent beyond margin"))
992 (while (< (point) end
)
993 (beginning-of-line 1)
994 (skip-chars-forward " \t") (setq ind1
(current-column))
995 (delete-region (point-at-bol) (point))
996 (or (eolp) (org-indent-to-column (+ ind1 delta
)))
997 (beginning-of-line 2)))
1001 (cdr (assoc (cdr (nth 0 ind-bul
)) org-list-demote-modify-bullet
))))
1002 (org-maybe-renumber-ordered-list-safe)
1004 (beginning-of-line 0)
1005 (condition-case nil
(org-beginning-of-item) (error nil
))
1006 (org-maybe-renumber-ordered-list-safe))))
1008 (defun org-item-indent-positions ()
1009 "Return indentation for plain list items.
1010 This returns a list with three values: The current indentation, the
1011 parent indentation and the indentation a child should have.
1012 Assumes cursor in item line."
1013 (let* ((bolpos (point-at-bol))
1014 (ind (org-get-indentation))
1015 (bullet (org-get-bullet))
1016 ind-down ind-up bullet-up bullet-down pos
)
1018 (org-beginning-of-item-list)
1019 (skip-chars-backward "\n\r \t")
1020 (when (org-in-item-p)
1021 (org-beginning-of-item)
1022 (setq ind-up
(org-get-indentation))
1023 (setq bullet-up
(org-get-bullet))))
1027 ((and (condition-case nil
(progn (org-previous-item) t
)
1029 (or (forward-char 1) t
)
1030 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t
))
1031 (setq ind-down
(org-get-indentation)
1032 bullet-down
(org-get-bullet)))
1033 ((and (goto-char pos
)
1035 (goto-char (match-end 0))
1036 (skip-chars-forward " \t")
1037 (setq ind-down
(current-column)
1038 bullet-down
(org-get-bullet)))))
1039 (if (and bullet-down
(string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-down
))
1040 (setq bullet-down
(concat "1" (match-string 1 bullet-down
))))
1041 (if (and bullet-up
(string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-up
))
1042 (setq bullet-up
(concat "1" (match-string 1 bullet-up
))))
1043 (if (and bullet
(string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet
))
1044 (setq bullet
(concat "1" (match-string 1 bullet
))))
1045 (list (cons ind bullet
)
1046 (cons ind-up bullet-up
)
1047 (cons ind-down bullet-down
))))
1049 (defvar org-tab-ind-state
) ; defined in org.el
1050 (defun org-cycle-item-indentation ()
1051 (let ((org-suppress-item-indentation t
)
1052 (org-adapt-indentation nil
))
1054 ((and (looking-at "[ \t]*$")
1055 (org-looking-back "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[).]\\)[ \t]+"))
1056 (setq this-command
'org-cycle-item-indentation
)
1057 (if (eq last-command
'org-cycle-item-indentation
)
1059 (progn (org-outdent-item 1)
1060 (if (equal org-tab-ind-state
(org-get-indentation))
1061 (org-outdent-item 1))
1065 (while (< (org-get-indentation) org-tab-ind-state
)
1066 (progn (org-indent-item 1) (end-of-line 1)))
1067 (setq this-command
'org-cycle
))))
1068 (setq org-tab-ind-state
(org-get-indentation))
1069 (org-indent-item 1))
1072 (defun org-get-bullet ()
1074 (goto-char (point-at-bol))
1076 "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\(\\*\\)\\)\\( \\|$\\)")
1077 (or (match-string 2) (match-string 4)))))
1079 ;;; Send and receive lists
1081 (defun org-list-parse-list (&optional delete
)
1082 "Parse the list at point and maybe DELETE it.
1083 Return a list containing first level items as strings and
1084 sublevels as a list of strings."
1085 (let* ((item-beginning (org-list-item-beginning))
1086 (start (car item-beginning
))
1087 (end (save-excursion
1088 (goto-char (org-list-end (cdr item-beginning
)))
1089 (org-back-over-empty-lines)
1091 output itemsep ltype
)
1092 (while (re-search-forward org-list-beginning-re end t
)
1093 (goto-char (match-beginning 3))
1095 (cond ((string-match "[0-9]" (match-string 2))
1096 (setq itemsep
"[0-9]+\\(?:\\.\\|)\\)"
1098 ((string-match "^.*::" (match-string 0))
1099 (setq itemsep
"[-+]" ltype
'descriptive
))
1100 (t (setq itemsep
"[-+]" ltype
'unordered
))))
1101 (let* ((indent1 (match-string 1))
1102 (nextitem (save-excursion
1104 (or (and (re-search-forward
1105 (concat "^" indent1 itemsep
" *?") end t
)
1106 (match-beginning 0)) end
))))
1107 (item (buffer-substring
1109 (or (and (re-search-forward
1110 org-list-beginning-re end t
)
1111 (goto-char (match-beginning 0)))
1113 (nextindent (match-string 1))
1114 (item (org-trim item
))
1115 (item (if (string-match "^\\[\\([xX ]\\)\\]" item
)
1116 (replace-match (if (equal (match-string 1 item
) " ")
1122 (when (> (length nextindent
)
1124 (narrow-to-region (point) nextitem
)
1125 (push (org-list-parse-list) output
)
1127 (when delete
(delete-region start end
))
1128 (setq output
(nreverse output
))
1129 (push ltype output
)))
1131 (defun org-list-item-beginning ()
1132 "Find the beginning of the list item.
1133 Return a cons which car is the beginning position of the item and
1134 cdr is the indentation string."
1136 (if (not (or (looking-at org-list-beginning-re
)
1138 org-list-beginning-re nil t
)))
1139 (progn (goto-char (point-min)) (point))
1140 (cons (match-beginning 0) (match-string 1)))))
1142 (defun org-list-goto-true-beginning ()
1143 "Go to the beginning of the list at point."
1144 (beginning-of-line 1)
1145 (while (looking-at org-list-beginning-re
)
1146 (beginning-of-line 0))
1148 (re-search-forward org-list-beginning-re nil t
)
1149 (goto-char (match-beginning 0))))
1151 (defun org-list-make-subtree ()
1152 "Convert the plain list at point into a subtree."
1154 (org-list-goto-true-beginning)
1155 (let ((list (org-list-parse-list t
)) nstars
)
1157 (if (condition-case nil
1158 (org-back-to-heading)
1160 (progn (re-search-forward org-complex-heading-regexp nil t
)
1161 (setq nstars
(length (match-string 1))))
1163 (org-list-make-subtrees list
(1+ nstars
))))
1165 (defun org-list-make-subtrees (list level
)
1166 "Convert LIST into subtrees starting at LEVEL."
1167 (if (symbolp (car list
))
1168 (org-list-make-subtrees (cdr list
) level
)
1169 (mapcar (lambda (item)
1171 (insert (make-string
1172 (if org-odd-levels-only
1173 (1- (* 2 level
)) level
) ?
*) " " item
"\n")
1174 (org-list-make-subtrees item
(1+ level
))))
1177 (defun org-list-end (indent)
1178 "Return the position of the end of the list.
1179 INDENT is the indentation of the list, as a string."
1182 (while (or (looking-at org-list-beginning-re
)
1183 (looking-at (concat "^" indent
"[ \t]+\\|^$"))
1184 (> (or (get-text-property (point) 'original-indentation
) -
1)
1186 (if (eq (point) (point-max))
1187 (throw 'exit
(point-max)))
1191 (defun org-list-insert-radio-list ()
1192 "Insert a radio list template appropriate for this major mode."
1194 (let* ((e (assq major-mode org-list-radio-list-templates
))
1197 (unless e
(error "No radio list setup defined for %s" major-mode
))
1198 (setq name
(read-string "List name: "))
1199 (while (string-match "%n" txt
)
1200 (setq txt
(replace-match name t t txt
)))
1201 (or (bolp) (insert "\n"))
1206 (defun org-list-send-list (&optional maybe
)
1207 "Send a transformed version of this list to the receiver position.
1208 With argument MAYBE, fail quietly if no transformation is defined for
1212 (unless (org-at-item-p) (error "Not at a list"))
1214 (org-list-goto-true-beginning)
1215 (beginning-of-line 0)
1216 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1219 (error "Don't know how to transform this list"))))
1220 (let* ((name (match-string 1))
1221 (transform (intern (match-string 2)))
1222 (item-beginning (org-list-item-beginning))
1223 (txt (buffer-substring-no-properties
1224 (car item-beginning
)
1225 (org-list-end (cdr item-beginning
))))
1226 (list (org-list-parse-list))
1228 (unless (fboundp transform
)
1229 (error "No such transformation function %s" transform
))
1230 (setq txt
(funcall transform list
))
1231 ;; Find the insertion place
1233 (goto-char (point-min))
1234 (unless (re-search-forward
1235 (concat "BEGIN RECEIVE ORGLST +" name
"\\([ \t]\\|$\\)") nil t
)
1236 (error "Don't know where to insert translated list"))
1237 (goto-char (match-beginning 0))
1238 (beginning-of-line 2)
1240 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
1241 (error "Cannot find end of insertion region"))
1242 (beginning-of-line 1)
1243 (delete-region beg
(point))
1246 (message "List converted and installed at receiver location"))))
1248 (defun org-list-to-generic (list params
)
1249 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1251 Valid parameters PARAMS are
1253 :ustart String to start an unordered list
1254 :uend String to end an unordered list
1256 :ostart String to start an ordered list
1257 :oend String to end an ordered list
1259 :dstart String to start a descriptive list
1260 :dend String to end a descriptive list
1261 :dtstart String to start a descriptive term
1262 :dtend String to end a descriptive term
1263 :ddstart String to start a description
1264 :ddend String to end a description
1266 :splice When set to t, return only list body lines, don't wrap
1267 them into :[u/o]start and :[u/o]end. Default is nil.
1269 :istart String to start a list item
1270 :iend String to end a list item
1271 :isep String to separate items
1272 :lsep String to separate sublists
1274 :cboff String to insert for an unchecked checkbox
1275 :cbon String to insert for a checked checkbox"
1277 (let* ((p params
) sublist
1278 (splicep (plist-get p
:splice
))
1279 (ostart (plist-get p
:ostart
))
1280 (oend (plist-get p
:oend
))
1281 (ustart (plist-get p
:ustart
))
1282 (uend (plist-get p
:uend
))
1283 (dstart (plist-get p
:dstart
))
1284 (dend (plist-get p
:dend
))
1285 (dtstart (plist-get p
:dtstart
))
1286 (dtend (plist-get p
:dtend
))
1287 (ddstart (plist-get p
:ddstart
))
1288 (ddend (plist-get p
:ddend
))
1289 (istart (plist-get p
:istart
))
1290 (iend (plist-get p
:iend
))
1291 (isep (plist-get p
:isep
))
1292 (lsep (plist-get p
:lsep
))
1293 (cbon (plist-get p
:cbon
))
1294 (cboff (plist-get p
:cboff
)))
1296 (cond ((eq (car list
) 'ordered
)
1297 (concat ostart
"\n%s" oend
"\n"))
1298 ((eq (car list
) 'unordered
)
1299 (concat ustart
"\n%s" uend
"\n"))
1300 ((eq (car list
) 'descriptive
)
1301 (concat dstart
"\n%s" dend
"\n"))))
1302 rtn term defstart defend
)
1303 (while (setq sublist
(pop list
))
1304 (cond ((symbolp sublist
) nil
)
1306 (when (string-match "^\\(.*\\) ::" sublist
)
1307 (setq term
(org-trim (format (concat dtstart
"%s" dtend
)
1308 (match-string 1 sublist
))))
1309 (setq sublist
(substring sublist
(1+ (length term
)))))
1310 (if (string-match "\\[CBON\\]" sublist
)
1311 (setq sublist
(replace-match cbon t t sublist
)))
1312 (if (string-match "\\[CBOFF\\]" sublist
)
1313 (setq sublist
(replace-match cboff t t sublist
)))
1314 (if (string-match "\\[-\\]" sublist
)
1315 (setq sublist
(replace-match "$\\boxminus$" t t sublist
)))
1316 (setq rtn
(concat rtn istart term ddstart
1317 sublist ddend iend isep
)))
1318 (t (setq rtn
(concat rtn
;; previous list
1319 lsep
;; list separator
1320 (org-list-to-generic sublist p
)
1321 lsep
;; list separator
1323 (format wrapper rtn
))))
1325 (defun org-list-to-latex (list &optional params
)
1326 "Convert LIST into a LaTeX list.
1327 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1328 with overruling parameters for `org-list-to-generic'."
1329 (org-list-to-generic
1332 '(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
1333 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
1334 :dstart
"\\begin{description}" :dend
"\\end{description}"
1335 :dtstart
"[" :dtend
"]"
1336 :ddstart
"" :ddend
""
1337 :istart
"\\item " :iend
""
1338 :isep
"\n" :lsep
"\n"
1339 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
1342 (defun org-list-to-html (list &optional params
)
1343 "Convert LIST into a HTML list.
1344 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1345 with overruling parameters for `org-list-to-generic'."
1346 (org-list-to-generic
1349 '(:splicep nil
:ostart
"<ol>" :oend
"</ol>"
1350 :ustart
"<ul>" :uend
"</ul>"
1351 :dstart
"<dl>" :dend
"</dl>"
1352 :dtstart
"<dt>" :dtend
"</dt>"
1353 :ddstart
"<dd>" :ddend
"</dd>"
1354 :istart
"<li>" :iend
"</li>"
1355 :isep
"\n" :lsep
"\n"
1356 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>")
1359 (defun org-list-to-texinfo (list &optional params
)
1360 "Convert LIST into a Texinfo list.
1361 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1362 with overruling parameters for `org-list-to-generic'."
1363 (org-list-to-generic
1366 '(:splicep nil
:ostart
"@itemize @minus" :oend
"@end itemize"
1367 :ustart
"@enumerate" :uend
"@end enumerate"
1368 :dstart
"@table" :dend
"@end table"
1369 :dtstart
"@item " :dtend
"\n"
1370 :ddstart
"" :ddend
""
1371 :istart
"@item\n" :iend
""
1372 :isep
"\n" :lsep
"\n"
1373 :cbon
"@code{[X]}" :cboff
"@code{[ ]}")
1378 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1379 ;;; org-list.el ends here