1 ;;; org-list.el --- Plain lists for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Bastien Guerry <bzg AT altern DOT org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; This file contains the code dealing with plain lists in Org-mode.
37 (defvar org-blank-before-new-entry
)
38 (defvar org-M-RET-may-split-line
)
40 (declare-function org-invisible-p
"org" ())
41 (declare-function org-on-heading-p
"org" (&optional invisible-ok
))
42 (declare-function outline-next-heading
"outline" ())
43 (declare-function outline-back-to-heading
"outline" (&optional invisible-ok
))
44 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
45 (declare-function org-back-over-empty-lines
"org" ())
46 (declare-function org-skip-whitespace
"org" ())
47 (declare-function org-trim
"org" (s))
48 (declare-function org-get-indentation
"org" (&optional line
))
49 (declare-function org-timer-item
"org-timer" (&optional arg
))
50 (declare-function org-combine-plists
"org" (&rest plists
))
51 (declare-function org-entry-get
"org" (pom property
&optional inherit
))
52 (declare-function org-narrow-to-subtree
"org" ())
53 (declare-function org-show-subtree
"org" ())
55 (defgroup org-plain-lists nil
56 "Options concerning plain lists in Org-mode."
57 :tag
"Org Plain lists"
58 :group
'org-structure
)
60 (defcustom org-cycle-include-plain-lists nil
61 "Non-nil means, include plain lists into visibility cycling.
62 This means that during cycling, plain list items will *temporarily* be
63 interpreted as outline headlines with a level given by 1000+i where i is the
64 indentation of the bullet. In all other operations, plain list items are
65 not seen as headlines. For example, you cannot assign a TODO keyword to
67 :group
'org-plain-lists
70 (defcustom org-plain-list-ordered-item-terminator t
71 "The character that makes a line with leading number an ordered list item.
72 Valid values are ?. and ?\). To get both terminators, use t. While
73 ?. may look nicer, it creates the danger that a line with leading
74 number may be incorrectly interpreted as an item. ?\) therefore is
76 :group
'org-plain-lists
77 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
78 (const :tag
"paren like in \"2)\"" ?\
))
79 (const :tab
"both" t
)))
81 (defcustom org-list-two-spaces-after-bullet-regexp nil
82 "A regular expression matching bullets that should have 2 spaces after them.
83 When nil, no bullet will have two spaces after them.
84 When a string, it will be used as a regular expression. When the bullet
85 type of a list is changed, the new bullet type will be matched against this
86 regexp. If it matches, there will be two spaces instead of one after
87 the bullet in each item of he list."
88 :group
'org-plain-list
90 (const :tag
"never" nil
)
93 (defcustom org-empty-line-terminates-plain-lists nil
94 "Non-nil means, an empty line ends all plain list levels.
95 When nil, empty lines are part of the preceding item."
96 :group
'org-plain-lists
99 (defcustom org-auto-renumber-ordered-lists t
100 "Non-nil means, automatically renumber ordered plain lists.
101 Renumbering happens when the sequence have been changed with
102 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
103 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
104 :group
'org-plain-lists
107 (defcustom org-provide-checkbox-statistics t
108 "Non-nil means, update checkbox statistics after insert and toggle.
109 When this is set, checkbox statistics is updated each time you either insert
110 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
111 with \\[org-ctrl-c-ctrl-c\\]."
112 :group
'org-plain-lists
115 (defcustom org-hierarchical-checkbox-statistics t
116 "Non-nil means, checkbox statistics counts only the state of direct children.
117 When nil, all boxes below the cookie are counted."
118 :group
'org-plain-lists
121 (defcustom org-description-max-indent
20
122 "Maximum indentation for the second line of a description list.
123 When the indentation would be larger than this, it will become
124 5 characters instead."
125 :group
'org-plain-lists
128 (defvar org-list-beginning-re
129 "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +\\(.*\\)$")
131 (defcustom org-list-radio-list-templates
132 '((latex-mode "% BEGIN RECEIVE ORGLST %n
133 % END RECEIVE ORGLST %n
135 #+ORGLST: SEND %n org-list-to-latex
138 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
139 @c END RECEIVE ORGLST %n
141 #+ORGLST: SEND %n org-list-to-texinfo
144 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
145 <!-- END RECEIVE ORGLST %n -->
147 #+ORGLST: SEND %n org-list-to-html
150 "Templates for radio lists in different major modes.
151 All occurrences of %n in a template will be replaced with the name of the
152 list, obtained by prompting the user."
153 :group
'org-plain-lists
155 (list (symbol :tag
"Major mode")
156 (string :tag
"Format"))))
158 ;;;; Plain list items, including checkboxes
162 (defun org-at-item-p ()
163 "Is point in a line starting a hand-formatted item?"
164 (let ((llt org-plain-list-ordered-item-terminator
))
166 (goto-char (point-at-bol))
169 ((eq llt t
) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
170 ((= llt ?.
) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
171 ((= llt ?\
)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
172 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
174 (defun org-at-item-bullet-p ()
175 "Is point at the bullet of a plain list item?"
177 (not (member (char-after) '(?\ ?
\t)))
178 (< (point) (match-end 0))))
180 (defun org-in-item-p ()
181 "It the cursor inside a plain list item.
182 Does not have to be the first line."
186 (org-beginning-of-item)
191 (defun org-insert-item (&optional checkbox
)
192 "Insert a new item at the current level.
193 Return t when things worked, nil when we are not in an item."
194 (when (save-excursion
197 (org-beginning-of-item)
199 (if (org-invisible-p) (error "Invisible item"))
202 (let* ((bul (match-string 0))
203 (descp (save-excursion (goto-char (match-beginning 0))
204 (beginning-of-line 1)
206 (and (looking-at "[ \t]*\\(.*?\\) ::")
208 (empty-line-p (save-excursion
209 (goto-char (match-beginning 0))
211 (or (beginning-of-line 0) t
)
213 (looking-at "[ \t]*$")))))
216 (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
218 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
220 (blank-a (if org-empty-line-terminates-plain-lists
222 (cdr (assq 'plain-list-item org-blank-before-new-entry
))))
223 (blank (if (eq blank-a
'auto
) empty-line-p blank-a
))
225 (if descp
(setq checkbox nil
))
227 (progn (org-timer-item) t
)
229 ((and (org-at-item-p) (<= (point) eow
))
231 (beginning-of-line 1)
232 (open-line (if blank
2 1)))
234 (beginning-of-line 1))
236 (unless (org-get-alist-option org-M-RET-may-split-line
'item
)
238 (delete-horizontal-space))
239 (newline (if blank
2 1))))
241 (if checkbox
"[ ]" "")
242 (if descp
(concat (if checkbox
" " "")
243 (read-string "Term: ") " :: ") ""))
247 (unless (= (point) pos
) (just-one-space) (backward-delete-char 1)))
248 (org-maybe-renumber-ordered-list)
249 (and checkbox
(org-update-checkbox-count-maybe))
254 (defun org-at-item-checkbox-p ()
255 "Is point at a line starting a plain-list item with a checklet?"
258 (goto-char (match-end 0))
259 (skip-chars-forward " \t")
260 (looking-at "\\[[- X]\\]"))))
262 (defun org-toggle-checkbox (&optional toggle-presence
)
263 "Toggle the checkbox in the current line.
264 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
265 With double prefix, set checkbox to [-].
266 When there is an active region, toggle status or presence of the checkbox
267 in the first line, and make every item in the region have the same
268 status or presence, respectively.
269 If the cursor is in a headline, apply this to all checkbox items in the
270 text below the heading."
273 (let (beg end status first-present first-status blocked
)
275 ((org-region-active-p)
276 (setq beg
(region-beginning) end
(region-end)))
278 (setq beg
(point) end
(save-excursion (outline-next-heading) (point))))
279 ((org-at-item-checkbox-p)
281 (if (equal toggle-presence
'(4))
284 (goto-char (match-beginning 0))
286 (when (setq blocked
(org-checkbox-blocked-p))
287 (error "Checkbox blocked because of unchecked box in line %d"
290 (cond ((equal toggle-presence
'(16)) "[-]")
291 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
298 (goto-char (match-end 0))
301 (t (error "Not at a checkbox or heading, and no active region")))
302 (setq end
(move-marker (make-marker) end
))
305 (setq first-present
(org-at-item-checkbox-p)
308 (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t
)
309 (equal (match-string 1) "[X]"))))
310 (while (< (point) end
)
313 ((and first-present
(org-at-item-checkbox-p))
316 (goto-char (match-beginning 0))
318 ((and (not first-present
) (not (org-at-item-checkbox-p))
321 (goto-char (match-end 0))
323 (when (org-at-item-checkbox-p)
324 (setq status
(equal (match-string 0) "[X]"))
326 (if first-status
"[ ]" "[X]") t t
)))
327 (beginning-of-line 2)))))
328 (org-update-checkbox-count-maybe))
330 (defun org-reset-checkbox-state-subtree ()
331 "Reset all checkboxes in an entry subtree."
335 (org-narrow-to-subtree)
337 (goto-char (point-min))
338 (let ((end (point-max)))
339 (while (< (point) end
)
340 (when (org-at-item-checkbox-p)
341 (replace-match "[ ]" t t
))
342 (beginning-of-line 2))))
343 (org-update-checkbox-count-maybe)))
345 (defun org-checkbox-blocked-p ()
346 "Is the current checkbox blocked from for being checked now?
347 A checkbox is blocked if all of the following conditions are fulfilled:
349 1. The checkbox is not checked already.
350 2. The current entry has the ORDERED property set.
351 3. There is an unchecked checkbox in this entry before the current line."
355 (unless (org-at-item-checkbox-p) (throw 'exit nil
))
356 (when (equal (match-string 0) "[X]")
357 ;; the box is already checked!
359 (let ((end (point-at-bol)))
360 (condition-case nil
(org-back-to-heading t
)
361 (error (throw 'exit nil
)))
362 (unless (org-entry-get nil
"ORDERED") (throw 'exit nil
))
363 (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t
)
367 (defvar org-checkbox-statistics-hook nil
368 "Hook that is run whenever Org thinks checkbox statistics should be updated.
369 This hook runs even if `org-provide-checkbox-statistics' is nil, to it can
370 be used to implement alternative ways of collecting statistics information.")
372 (defun org-update-checkbox-count-maybe ()
373 "Update checkbox statistics unless turned off by user."
374 (when org-provide-checkbox-statistics
375 (org-update-checkbox-count))
376 (run-hooks 'org-checkbox-statistics-hook
))
378 (defun org-update-checkbox-count (&optional all
)
379 "Update the checkbox statistics in the current section.
380 This will find all statistic cookies like [57%] and [6/12] and update them
381 with the current numbers. With optional prefix argument ALL, do this for
385 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
386 (beg (condition-case nil
387 (progn (org-back-to-heading) (point))
388 (error (point-min))))
389 (end (move-marker (make-marker)
390 (progn (outline-next-heading) (point))))
391 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
392 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
393 (re-find (concat re
"\\|" re-box
))
394 beg-cookie end-cookie is-percent c-on c-off lim
395 eline curr-ind next-ind continue-from startsearch
397 (or (not org-hierarchical-checkbox-statistics
)
398 (string-match "\\<recursive\\>"
399 (or (org-entry-get nil
"COOKIE_DATA") ""))))
403 (goto-char (point-min))
404 (outline-next-heading)
405 (setq beg
(point) end
(point-max)))
407 ;; find each statistics cookie
408 (while (and (re-search-backward re-find beg t
)
409 (not (save-match-data
410 (and (org-on-heading-p)
411 (string-match "\\<todo\\>"
416 (setq beg-cookie
(match-beginning 1)
417 end-cookie
(match-end 1)
418 cstat
(+ cstat
(if end-cookie
1 0))
419 startsearch
(point-at-eol)
420 continue-from
(match-beginning 0)
421 is-percent
(match-beginning 2)
423 ((org-on-heading-p) (outline-next-heading) (point))
424 ((org-at-item-p) (org-end-of-item) (point))
429 ;; find first checkbox for this cookie and gather
430 ;; statistics from all that are at this indentation level
431 (goto-char startsearch
)
432 (if (re-search-forward re-box lim t
)
434 (org-beginning-of-item)
435 (setq curr-ind
(org-get-indentation))
436 (setq next-ind curr-ind
)
437 (while (and (bolp) (org-at-item-p)
439 (<= curr-ind next-ind
)
440 (= curr-ind next-ind
)))
441 (save-excursion (end-of-line) (setq eline
(point)))
442 (if (re-search-forward re-box eline t
)
443 (if (member (match-string 2) '("[ ]" "[-]"))
444 (setq c-off
(1+ c-off
))
445 (setq c-on
(1+ c-on
))
451 (when (re-search-forward org-list-beginning-re lim t
)
452 (beginning-of-line)))
453 (setq next-ind
(org-get-indentation))
455 (goto-char continue-from
)
458 (delete-region beg-cookie end-cookie
)
459 (goto-char beg-cookie
)
462 (format "[%d%%]" (/ (* 100 c-on
) (max 1 (+ c-on c-off
))))
463 (format "[%d/%d]" c-on
(+ c-on c-off
)))))
464 ;; update items checkbox if it has one
465 (when (org-at-item-p)
466 (org-beginning-of-item)
467 (when (and (> (+ c-on c-off
) 0)
468 (re-search-forward re-box
(point-at-eol) t
))
469 (setq beg-cookie
(match-beginning 2)
470 end-cookie
(match-end 2))
471 (delete-region beg-cookie end-cookie
)
472 (goto-char beg-cookie
)
473 (cond ((= c-off
0) (insert "[X]"))
474 ((= c-on
0) (insert "[ ]"))
477 (goto-char continue-from
))
478 (when (interactive-p)
479 (message "Checkbox statistics updated %s (%d places)"
480 (if all
"in entire file" "in current outline entry") cstat
)))))
482 (defun org-get-checkbox-statistics-face ()
483 "Select the face for checkbox statistics.
484 The face will be `org-done' when all relevant boxes are checked. Otherwise
485 it will be `org-todo'."
487 (if (equal (match-string 1) "100%")
488 'org-checkbox-statistics-done
489 'org-checkbox-statistics-todo
)
490 (if (and (> (match-end 2) (match-beginning 2))
491 (equal (match-string 2) (match-string 3)))
492 'org-checkbox-statistics-done
493 'org-checkbox-statistics-todo
)))
495 (defun org-beginning-of-item ()
496 "Go to the beginning of the current hand-formatted item.
497 If the cursor is not in an item, throw an error."
500 (limit (save-excursion
503 (org-back-to-heading)
504 (beginning-of-line 2) (point))
505 (error (point-min)))))
506 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
509 (beginning-of-line 1)
510 (beginning-of-line 1)
511 (skip-chars-forward " \t")
512 (setq ind
(current-column))
515 (beginning-of-line 0)
516 (if (or (bobp) (< (point) limit
)) (throw 'exit nil
))
518 (if (looking-at "[ \t]*$")
519 (setq ind1 ind-empty
)
520 (skip-chars-forward " \t")
521 (setq ind1
(current-column)))
523 (progn (beginning-of-line 1) (throw 'exit
(org-at-item-p))))))
526 (error "Not in an item")))))
528 (defun org-end-of-item ()
529 "Go to the end of the current hand-formatted item.
530 If the cursor is not in an item, throw an error."
534 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
535 (limit (save-excursion (outline-next-heading) (point)))
537 (org-beginning-of-item)
538 (skip-chars-forward " \t")
542 (beginning-of-line 2)
543 (if (eobp) (throw 'exit
(point)))
544 (if (>= (point) limit
) (throw 'exit
(point-at-bol)))
545 (if (looking-at "[ \t]*$")
546 (setq ind1 ind-empty
)
547 (skip-chars-forward " \t")
548 (setq ind1
(current-column)))
550 (throw 'exit
(point-at-bol)))))))
554 (error "Not in an item"))))
556 (defun org-next-item ()
557 "Move to the beginning of the next item in the current plain list.
558 Error if not at a plain list, or if this is the last item in the list."
560 (let (ind ind1
(pos (point)))
561 (org-beginning-of-item)
562 (setq ind
(org-get-indentation))
564 (setq ind1
(org-get-indentation))
565 (unless (and (org-at-item-p) (= ind ind1
))
567 (error "On last item"))))
569 (defun org-previous-item ()
570 "Move to the beginning of the previous item in the current plain list.
571 Error if not at a plain list, or if this is the first item in the list."
573 (let (beg ind ind1
(pos (point)))
574 (org-beginning-of-item)
576 (setq ind
(org-get-indentation))
580 (beginning-of-line 0)
581 (if (looking-at "[ \t]*$")
583 (if (<= (setq ind1
(org-get-indentation)) ind
)
586 (if (or (not (org-at-item-p))
589 (org-beginning-of-item))
590 (error (goto-char pos
)
591 (error "On first item")))))
593 (defun org-first-list-item-p ()
594 "Is this heading the item in a plain list?"
595 (unless (org-at-item-p)
596 (error "Not at a plain list item"))
597 (org-beginning-of-item)
598 (= (point) (save-excursion (org-beginning-of-item-list))))
600 (defun org-move-item-down ()
601 "Move the plain list item at point down, i.e. swap with following item.
602 Subitems (items with larger indentation) are considered part of the item,
603 so this really moves item trees."
605 (let ((col (current-column))
607 beg beg0 end end0 ind ind1 txt ne-end ne-beg
)
608 (org-beginning-of-item)
611 (setq ne-beg
(org-back-over-empty-lines))
614 (setq ind
(org-get-indentation))
617 (setq ind1
(org-get-indentation))
618 (setq ne-end
(org-back-over-empty-lines))
621 (when (and (org-first-list-item-p) (< ne-end ne-beg
))
622 ;; include less whitespace
625 (forward-line (- ne-beg ne-end
))
628 (if (and (org-at-item-p) (= ind ind1
))
631 (org-back-over-empty-lines)
632 (setq txt
(buffer-substring beg end
))
634 (delete-region beg end
))
637 (goto-char pos
) (org-skip-whitespace)
638 (org-maybe-renumber-ordered-list)
639 (move-to-column col
))
642 (error "Cannot move this item further down"))))
644 (defun org-move-item-up (arg)
645 "Move the plain list item at point up, i.e. swap with previous item.
646 Subitems (items with larger indentation) are considered part of the item,
647 so this really moves item trees."
649 (let ((col (current-column)) (pos (point))
650 beg beg0 end ind ind1 txt
651 ne-beg ne-ins ins-end
)
652 (org-beginning-of-item)
654 (setq ind
(org-get-indentation))
656 (setq ne-beg
(org-back-over-empty-lines))
660 (org-back-over-empty-lines)
665 (beginning-of-line 0)
666 (if (looking-at "[ \t]*$")
667 (if org-empty-line-terminates-plain-lists
670 (error "Cannot move this item further up"))
672 (if (<= (setq ind1
(org-get-indentation)) ind
)
675 (org-beginning-of-item)
676 (error (goto-char beg0
)
678 (error "Cannot move this item further up")))
679 (setq ind1
(org-get-indentation))
680 (if (and (org-at-item-p) (= ind ind1
))
682 (setq ne-ins
(org-back-over-empty-lines))
683 (setq txt
(buffer-substring beg end
))
685 (delete-region beg end
))
688 (setq ins-end
(point))
689 (goto-char pos
) (org-skip-whitespace)
691 (when (and (org-first-list-item-p) (> ne-ins ne-beg
))
692 ;; Move whitespace back to beginning
695 (let ((kill-whole-line t
))
696 (kill-line (- ne-ins ne-beg
)) (point)))
697 (insert (make-string (- ne-ins ne-beg
) ?
\n)))
699 (org-maybe-renumber-ordered-list)
700 (move-to-column col
))
703 (error "Cannot move this item further up"))))
705 (defun org-maybe-renumber-ordered-list ()
706 "Renumber the ordered list at point if setup allows it.
707 This tests the user option `org-auto-renumber-ordered-lists' before
708 doing the renumbering."
710 (when (and org-auto-renumber-ordered-lists
712 (if (match-beginning 3)
713 (org-renumber-ordered-list 1)
714 (org-fix-bullet-type))))
716 (defun org-maybe-renumber-ordered-list-safe ()
719 (org-maybe-renumber-ordered-list))
722 (defun org-cycle-list-bullet (&optional which
)
723 "Cycle through the different itemize/enumerate bullets.
724 This cycle the entire list level through the sequence:
726 `-' -> `+' -> `*' -> `1.' -> `1)'
728 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
729 0 means `-', 1 means `+' etc."
732 (org-beginning-of-item-list)
734 (beginning-of-line 1)
735 (let ((current (match-string 0))
736 (prevp (eq which
'previous
))
739 ((and (numberp which
)
740 (nth (1- which
) '("-" "+" "*" "1." "1)"))))
741 ((string-match "-" current
) (if prevp
"1)" "+"))
742 ((string-match "\\+" current
)
743 (if prevp
"-" (if (looking-at "\\S-") "1." "*")))
744 ((string-match "\\*" current
) (if prevp
"+" "1."))
745 ((string-match "\\." current
)
746 (if prevp
(if (looking-at "\\S-") "+" "*") "1)"))
747 ((string-match ")" current
) (if prevp
"1." "-"))
748 (t (error "This should not happen"))))
749 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
750 (setq old
(match-string 2))
751 (replace-match (concat "\\1" new
)))
752 (org-shift-item-indentation (- (length new
) (length old
)))
753 (org-fix-bullet-type)
754 (org-maybe-renumber-ordered-list))))
756 (defun org-get-string-indentation (s)
757 "What indentation has S due to SPACE and TAB at the beginning of the string?"
758 (let ((n -
1) (i 0) (w tab-width
) c
)
760 (while (< (setq n
(1+ n
)) (length s
))
762 (cond ((= c ?\
) (setq i
(1+ i
)))
763 ((= c ?
\t) (setq i
(* (/ (+ w i
) w
) w
)))
764 (t (throw 'exit t
)))))
767 (defun org-renumber-ordered-list (arg)
768 "Renumber an ordered plain list.
769 Cursor needs to be in the first line of an item, the line that starts
770 with something like \"1.\" or \"2)\"."
772 (unless (and (org-at-item-p)
774 (error "This is not an ordered list"))
775 (let ((line (org-current-line))
776 (col (current-column))
777 (ind (org-get-string-indentation
778 (buffer-substring (point-at-bol) (match-beginning 3))))
779 ;; (term (substring (match-string 3) -1))
782 ;; find where this list begins
783 (org-beginning-of-item-list)
785 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
786 (setq fmt
(concat "%d" (or (match-string 1) ".")))
787 (beginning-of-line 0)
788 ;; walk forward and replace these numbers
792 (if bobp
(setq bobp nil
) (beginning-of-line 2))
793 (if (eobp) (throw 'exit nil
))
794 (if (looking-at "[ \t]*$") (throw 'next nil
))
795 (skip-chars-forward " \t") (setq ind1
(current-column))
796 (if (> ind1 ind
) (throw 'next t
))
797 (if (< ind1 ind
) (throw 'exit t
))
798 (if (not (org-at-item-p)) (throw 'exit nil
))
799 (setq old
(match-string 2))
800 (delete-region (match-beginning 2) (match-end 2))
801 (goto-char (match-beginning 2))
802 (insert (setq new
(format fmt
(setq n
(1+ n
)))))
803 (org-shift-item-indentation (- (length new
) (length old
))))))
805 (org-move-to-column col
)))
807 (defun org-fix-bullet-type ()
808 "Make sure all items in this list have the same bullet as the first item.
809 Also, fix the indentation."
811 (unless (org-at-item-p) (error "This is not a list"))
812 (let ((line (org-current-line))
813 (col (current-column))
814 (ind (current-indentation))
815 ind1 bullet oldbullet
)
816 ;; find where this list begins
817 (org-beginning-of-item-list)
818 (beginning-of-line 1)
819 ;; find out what the bullet type is
820 (looking-at "[ \t]*\\(\\S-+\\)")
821 (setq bullet
(concat (match-string 1) " "))
822 (if (and org-list-two-spaces-after-bullet-regexp
823 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
824 (setq bullet
(concat bullet
" ")))
825 ;; walk forward and replace these numbers
826 (beginning-of-line 0)
830 (beginning-of-line 2)
831 (if (eobp) (throw 'exit nil
))
832 (if (looking-at "[ \t]*$") (throw 'next nil
))
833 (skip-chars-forward " \t") (setq ind1
(current-column))
834 (if (> ind1 ind
) (throw 'next t
))
835 (if (< ind1 ind
) (throw 'exit t
))
836 (if (not (org-at-item-p)) (throw 'exit nil
))
837 (skip-chars-forward " \t")
838 (looking-at "\\S-+ *")
839 (setq oldbullet
(match-string 0))
840 (unless (equal bullet oldbullet
) (replace-match bullet
))
841 (org-shift-item-indentation (- (length bullet
) (length oldbullet
))))))
843 (org-move-to-column col
)
844 (if (string-match "[0-9]" bullet
)
845 (org-renumber-ordered-list 1))))
847 (defun org-shift-item-indentation (delta)
848 "Shift the indentation in current item by DELTA."
850 (let ((beg (point-at-bol))
851 (end (progn (org-end-of-item) (point)))
854 (beginning-of-line 0)
855 (while (> (point) beg
)
856 (when (looking-at "[ \t]*\\S-")
857 ;; this is not an empty line
858 (setq i
(org-get-indentation))
859 (if (and (> i
0) (> (setq i
(+ i delta
)) 0))
861 (beginning-of-line 0)))))
863 (defun org-beginning-of-item-list ()
864 "Go to the beginning of the current item list.
865 I.e. to the first item in this list."
867 (org-beginning-of-item)
868 (let ((pos (point-at-bol))
869 (ind (org-get-indentation))
871 ;; find where this list begins
875 (beginning-of-line 0)
876 (if (looking-at "[ \t]*$")
877 (throw (if (bobp) 'exit
'next
) t
))
878 (skip-chars-forward " \t") (setq ind1
(current-column))
881 (not (org-at-item-p)))
882 (and (= (point-at-bol) (point-min))
883 (setq pos
(point-min))))
885 (when (org-at-item-p) (setq pos
(point-at-bol)))))))
889 (defun org-end-of-item-list ()
890 "Go to the end of the current item list.
891 I.e. to the text after the last item."
893 (org-beginning-of-item)
894 (let ((pos (point-at-bol))
895 (ind (org-get-indentation))
897 ;; find where this list begins
901 (beginning-of-line 2)
902 (if (looking-at "[ \t]*$")
904 (progn (setq pos
(point)) (throw 'exit t
))
906 (skip-chars-forward " \t") (setq ind1
(current-column))
909 (not (org-at-item-p)))
912 (setq pos
(point-at-bol))
917 (defvar org-last-indent-begin-marker
(make-marker))
918 (defvar org-last-indent-end-marker
(make-marker))
920 (defun org-outdent-item (arg)
921 "Outdent a local list item."
923 (org-indent-item (- arg
)))
925 (defun org-indent-item (arg)
926 "Indent a local list item."
928 (and (org-region-active-p) (org-cursor-to-region-beginning))
929 (unless (org-at-item-p)
930 (error "Not on an item"))
932 (let (beg end ind ind1 tmp delta ind-down ind-up
)
933 (setq end
(and (org-region-active-p) (region-end)))
934 (if (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
935 (setq beg org-last-indent-begin-marker
936 end org-last-indent-end-marker
)
937 (org-beginning-of-item)
938 (setq beg
(move-marker org-last-indent-begin-marker
(point)))
940 (setq end
(move-marker org-last-indent-end-marker
(or end
(point)))))
942 (setq tmp
(org-item-indent-positions)
947 (if ind-down
(- ind-down ind
) 2)
948 (if ind-up
(- ind-up ind
) -
2)))
949 (if (< (+ delta ind
) 0) (error "Cannot outdent beyond margin"))
950 (while (< (point) end
)
951 (beginning-of-line 1)
952 (skip-chars-forward " \t") (setq ind1
(current-column))
953 (delete-region (point-at-bol) (point))
954 (or (eolp) (org-indent-to-column (+ ind1 delta
)))
955 (beginning-of-line 2))))
956 (org-fix-bullet-type)
957 (org-maybe-renumber-ordered-list-safe)
959 (beginning-of-line 0)
960 (condition-case nil
(org-beginning-of-item) (error nil
))
961 (org-maybe-renumber-ordered-list-safe)))
963 (defun org-item-indent-positions ()
964 "Return indentation for plain list items.
965 This returns a list with three values: The current indentation, the
966 parent indentation and the indentation a child should have.
967 Assumes cursor in item line."
968 (let* ((bolpos (point-at-bol))
969 (ind (org-get-indentation))
972 (org-beginning-of-item-list)
973 (skip-chars-backward "\n\r \t")
974 (when (org-in-item-p)
975 (org-beginning-of-item)
976 (setq ind-up
(org-get-indentation))))
980 ((and (condition-case nil
(progn (org-previous-item) t
)
982 (or (forward-char 1) t
)
983 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t
))
984 (setq ind-down
(org-get-indentation)))
985 ((and (goto-char pos
)
987 (goto-char (match-end 0))
988 (skip-chars-forward " \t")
989 (setq ind-down
(current-column)))))
990 (list ind ind-up ind-down
)))
993 ;;; Send and receive lists
995 (defun org-list-parse-list (&optional delete
)
996 "Parse the list at point and maybe DELETE it.
997 Return a list containing first level items as strings and
998 sublevels as a list of strings."
999 (let* ((item-beginning (org-list-item-beginning))
1000 (start (car item-beginning
))
1001 (end (org-list-end (cdr item-beginning
)))
1002 output itemsep ltype
)
1003 (while (re-search-forward org-list-beginning-re end t
)
1004 (goto-char (match-beginning 3))
1006 (cond ((string-match "[0-9]" (match-string 2))
1007 (setq itemsep
"[0-9]+\\(?:\\.\\|)\\)"
1009 ((string-match "^.*::" (match-string 0))
1010 (setq itemsep
"[-+]" ltype
'descriptive
))
1011 (t (setq itemsep
"[-+]" ltype
'unordered
))))
1012 (let* ((indent1 (match-string 1))
1013 (nextitem (save-excursion
1015 (or (and (re-search-forward
1016 (concat "^" indent1 itemsep
" *?") end t
)
1017 (match-beginning 0)) end
))))
1018 (item (buffer-substring
1020 (or (and (re-search-forward
1021 org-list-beginning-re end t
)
1022 (goto-char (match-beginning 0)))
1024 (nextindent (match-string 1))
1025 (item (org-trim item
))
1026 (item (if (string-match "^\\[\\([xX ]\\)\\]" item
)
1027 (replace-match (if (equal (match-string 1 item
) " ")
1033 (when (> (length nextindent
)
1035 (narrow-to-region (point) nextitem
)
1036 (push (org-list-parse-list) output
)
1038 (when delete
(delete-region start end
))
1039 (setq output
(nreverse output
))
1040 (push ltype output
)))
1042 (defun org-list-item-beginning ()
1043 "Find the beginning of the list item.
1044 Return a cons which car is the beginning position of the item and
1045 cdr is the indentation string."
1047 (if (not (or (looking-at org-list-beginning-re
)
1049 org-list-beginning-re nil t
)))
1050 (progn (goto-char (point-min)) (point))
1051 (cons (match-beginning 0) (match-string 1)))))
1053 (defun org-list-end (indent)
1054 "Return the position of the end of the list.
1055 INDENT is the indentation of the list, as a string."
1058 (while (or (looking-at org-list-beginning-re
)
1059 (looking-at (concat "^" indent
"[ \t]+\\|^$"))
1060 (> (or (get-text-property (point) 'original-indentation
) -
1)
1062 (if (eq (point) (point-max))
1063 (throw 'exit
(point-max)))
1067 (defun org-list-insert-radio-list ()
1068 "Insert a radio list template appropriate for this major mode."
1070 (let* ((e (assq major-mode org-list-radio-list-templates
))
1073 (unless e
(error "No radio list setup defined for %s" major-mode
))
1074 (setq name
(read-string "List name: "))
1075 (while (string-match "%n" txt
)
1076 (setq txt
(replace-match name t t txt
)))
1077 (or (bolp) (insert "\n"))
1082 (defun org-list-send-list (&optional maybe
)
1083 "Send a tranformed version of this list to the receiver position.
1084 With argument MAYBE, fail quietly if no transformation is defined for
1088 (unless (org-at-item-p) (error "Not at a list"))
1090 (goto-char (car (org-list-item-beginning)))
1091 (beginning-of-line 0)
1092 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1095 (error "Don't know how to transform this list"))))
1096 (let* ((name (match-string 1))
1097 (item-beginning (org-list-item-beginning))
1098 (transform (intern (match-string 2)))
1099 (txt (buffer-substring-no-properties
1100 (car item-beginning
)
1101 (org-list-end (cdr item-beginning
))))
1102 (list (org-list-parse-list))
1104 (unless (fboundp transform
)
1105 (error "No such transformation function %s" transform
))
1106 (setq txt
(funcall transform list
))
1107 ;; Find the insertion place
1109 (goto-char (point-min))
1110 (unless (re-search-forward
1111 (concat "BEGIN RECEIVE ORGLST +" name
"\\([ \t]\\|$\\)") nil t
)
1112 (error "Don't know where to insert translated list"))
1113 (goto-char (match-beginning 0))
1114 (beginning-of-line 2)
1116 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
1117 (error "Cannot find end of insertion region"))
1118 (beginning-of-line 1)
1119 (delete-region beg
(point))
1122 (message "List converted and installed at receiver location"))))
1124 (defun org-list-to-generic (list params
)
1125 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1127 Valid parameters PARAMS are
1129 :ustart String to start an unordered list
1130 :uend String to end an unordered list
1132 :ostart String to start an ordered list
1133 :oend String to end an ordered list
1135 :dstart String to start a descriptive list
1136 :dend String to end a descriptive list
1137 :dtstart String to start a descriptive term
1138 :dtend String to end a descriptive term
1139 :ddstart String to start a description
1140 :ddend String to end a description
1142 :splice When set to t, return only list body lines, don't wrap
1143 them into :[u/o]start and :[u/o]end. Default is nil.
1145 :istart String to start a list item
1146 :iend String to end a list item
1147 :isep String to separate items
1148 :lsep String to separate sublists
1150 :cboff String to insert for an unchecked checkbox
1151 :cbon String to insert for a checked checkbox"
1153 (let* ((p params
) sublist
1154 (splicep (plist-get p
:splice
))
1155 (ostart (plist-get p
:ostart
))
1156 (oend (plist-get p
:oend
))
1157 (ustart (plist-get p
:ustart
))
1158 (uend (plist-get p
:uend
))
1159 (dstart (plist-get p
:dstart
))
1160 (dend (plist-get p
:dend
))
1161 (dtstart (plist-get p
:dtstart
))
1162 (dtend (plist-get p
:dtend
))
1163 (ddstart (plist-get p
:ddstart
))
1164 (ddend (plist-get p
:ddend
))
1165 (istart (plist-get p
:istart
))
1166 (iend (plist-get p
:iend
))
1167 (isep (plist-get p
:isep
))
1168 (lsep (plist-get p
:lsep
))
1169 (cbon (plist-get p
:cbon
))
1170 (cboff (plist-get p
:cboff
)))
1172 (cond ((eq (car list
) 'ordered
)
1173 (concat ostart
"\n%s" oend
"\n"))
1174 ((eq (car list
) 'unordered
)
1175 (concat ustart
"\n%s" uend
"\n"))
1176 ((eq (car list
) 'descriptive
)
1177 (concat dstart
"\n%s" dend
"\n"))))
1178 rtn term defstart defend
)
1179 (while (setq sublist
(pop list
))
1180 (cond ((symbolp sublist
) nil
)
1182 (when (string-match "^\\(.*\\) ::" sublist
)
1183 (setq term
(org-trim (format (concat dtstart
"%s" dtend
)
1184 (match-string 1 sublist
))))
1185 (setq sublist
(substring sublist
(1+ (length term
)))))
1186 (if (string-match "\\[CBON\\]" sublist
)
1187 (setq sublist
(replace-match cbon t t sublist
)))
1188 (if (string-match "\\[CBOFF\\]" sublist
)
1189 (setq sublist
(replace-match cboff t t sublist
)))
1190 (setq rtn
(concat rtn istart term ddstart
1191 sublist ddend iend isep
)))
1192 (t (setq rtn
(concat rtn
;; previous list
1193 lsep
;; list separator
1194 (org-list-to-generic sublist p
)
1195 lsep
;; list separator
1197 (format wrapper rtn
))))
1199 (defun org-list-to-latex (list &optional params
)
1200 "Convert LIST into a LaTeX list.
1201 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1202 with overruling parameters for `org-list-to-generic'."
1203 (org-list-to-generic
1206 '(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
1207 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
1208 :dstart
"\\begin{description}" :dend
"\\end{description}"
1209 :dtstart
"[" :dtend
"]"
1210 :ddstart
"" :ddend
""
1211 :istart
"\\item " :iend
""
1212 :isep
"\n" :lsep
"\n"
1213 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
1216 (defun org-list-to-html (list &optional params
)
1217 "Convert LIST into a HTML list.
1218 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1219 with overruling parameters for `org-list-to-generic'."
1220 (org-list-to-generic
1223 '(:splicep nil
:ostart
"<ol>" :oend
"</ol>"
1224 :ustart
"<ul>" :uend
"</ul>"
1225 :dstart
"<dl>" :dend
"</dl>"
1226 :dtstart
"<dt>" :dtend
"</dt>"
1227 :ddstart
"<dd>" :ddend
"</dd>"
1228 :istart
"<li>" :iend
"</li>"
1229 :isep
"\n" :lsep
"\n"
1230 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>")
1233 (defun org-list-to-texinfo (list &optional params
)
1234 "Convert LIST into a Texinfo list.
1235 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1236 with overruling parameters for `org-list-to-generic'."
1237 (org-list-to-generic
1240 '(:splicep nil
:ostart
"@itemize @minus" :oend
"@end itemize"
1241 :ustart
"@enumerate" :uend
"@end enumerate"
1242 :dstart
"@table" :dend
"@end table"
1243 :dtstart
"@item " :dtend
"\n"
1244 :ddstart
"" :ddend
""
1245 :istart
"@item\n" :iend
""
1246 :isep
"\n" :lsep
"\n"
1247 :cbon
"@code{[X]}" :cboff
"@code{[ ]}")
1252 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1253 ;;; org-list.el ends here