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 outline-back-to-heading
"outline" (&optional invisible-ok
))
46 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
47 (declare-function org-back-over-empty-lines
"org" ())
48 (declare-function org-skip-whitespace
"org" ())
49 (declare-function org-trim
"org" (s))
50 (declare-function org-get-indentation
"org" (&optional line
))
51 (declare-function org-timer-item
"org-timer" (&optional arg
))
52 (declare-function org-combine-plists
"org" (&rest plists
))
53 (declare-function org-entry-get
"org" (pom property
&optional inherit
))
54 (declare-function org-narrow-to-subtree
"org" ())
55 (declare-function org-show-subtree
"org" ())
57 (defgroup org-plain-lists nil
58 "Options concerning plain lists in Org-mode."
59 :tag
"Org Plain lists"
60 :group
'org-structure
)
62 (defcustom org-cycle-include-plain-lists t
63 "When t, make TAB cycle visibility on plain list items.
65 Cycling plain lists works only when the cursor is on a plain list
66 item. When the cursor is on an outline heading, plain lists are
67 treated as text. This is the most stable way of handling this,
68 which is why it is the default.
70 When this is the symbol `integrate', then during cycling, plain
71 list items will *temporarily* be interpreted as outline headlines
72 with a level given by 1000+i where i is the indentation of the
73 bullet. This setting can lead to strange effects when switching
74 visibility to `children', because the first \"child\" in a
75 subtree decides what children should be listed. If that first
76 \"child\" is a plain list item with an implied large level
77 number, all true children and grand children of the outline
78 heading will be exposed in a children' view."
79 :group
'org-plain-lists
81 (const :tag
"Never" nil
)
82 (const :tag
"With cursor in plain list (recommended)" t
)
83 (const :tag
"As children of outline headings" integrate
)))
85 (defcustom org-list-demote-modify-bullet nil
86 "Default bullet type installed when demoting an item.
87 This is an association list, for each bullet type, this alist will point
88 to the bulled that should be used when this item is demoted."
89 :group
'org-plain-lists
92 (choice :tag
"If the current bullet is "
98 (choice :tag
"demotion will change it to"
105 (defcustom org-plain-list-ordered-item-terminator t
106 "The character that makes a line with leading number an ordered list item.
107 Valid values are ?. and ?\). To get both terminators, use t. While
108 ?. may look nicer, it creates the danger that a line with leading
109 number may be incorrectly interpreted as an item. ?\) therefore is
111 :group
'org-plain-lists
112 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
113 (const :tag
"paren like in \"2)\"" ?\
))
114 (const :tab
"both" t
)))
116 (defcustom org-list-two-spaces-after-bullet-regexp nil
117 "A regular expression matching bullets that should have 2 spaces after them.
118 When nil, no bullet will have two spaces after them.
119 When a string, it will be used as a regular expression. When the bullet
120 type of a list is changed, the new bullet type will be matched against this
121 regexp. If it matches, there will be two spaces instead of one after
122 the bullet in each item of he list."
123 :group
'org-plain-list
125 (const :tag
"never" nil
)
128 (defcustom org-empty-line-terminates-plain-lists nil
129 "Non-nil means, an empty line ends all plain list levels.
130 When nil, empty lines are part of the preceding item."
131 :group
'org-plain-lists
134 (defcustom org-auto-renumber-ordered-lists t
135 "Non-nil means, automatically renumber ordered plain lists.
136 Renumbering happens when the sequence have been changed with
137 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
138 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
139 :group
'org-plain-lists
142 (defcustom org-provide-checkbox-statistics t
143 "Non-nil means, update checkbox statistics after insert and toggle.
144 When this is set, checkbox statistics is updated each time you
145 either insert a new checkbox with \\[org-insert-todo-heading] or
146 toggle a checkbox with \\[org-ctrl-c-ctrl-c]."
147 :group
'org-plain-lists
150 (defcustom org-hierarchical-checkbox-statistics t
151 "Non-nil means, checkbox statistics counts only the state of direct children.
152 When nil, all boxes below the cookie are counted."
153 :group
'org-plain-lists
156 (defcustom org-description-max-indent
20
157 "Maximum indentation for the second line of a description list.
158 When the indentation would be larger than this, it will become
159 5 characters instead."
160 :group
'org-plain-lists
163 (defvar org-list-beginning-re
164 "^\\([ \t]*\\)\\([-+]\\|[0-9]+[.)]\\) +\\(.*\\)$")
166 (defcustom org-list-radio-list-templates
167 '((latex-mode "% BEGIN RECEIVE ORGLST %n
168 % END RECEIVE ORGLST %n
170 #+ORGLST: SEND %n org-list-to-latex
173 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
174 @c END RECEIVE ORGLST %n
176 #+ORGLST: SEND %n org-list-to-texinfo
179 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
180 <!-- END RECEIVE ORGLST %n -->
182 #+ORGLST: SEND %n org-list-to-html
185 "Templates for radio lists in different major modes.
186 All occurrences of %n in a template will be replaced with the name of the
187 list, obtained by prompting the user."
188 :group
'org-plain-lists
190 (list (symbol :tag
"Major mode")
191 (string :tag
"Format"))))
193 ;;;; Plain list items, including checkboxes
197 (defun org-at-item-p ()
198 "Is point in a line starting a hand-formatted item?"
199 (let ((llt org-plain-list-ordered-item-terminator
))
201 (goto-char (point-at-bol))
204 ((eq llt t
) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
205 ((= llt ?.
) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
206 ((= llt ?\
)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
207 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
209 (defun org-at-item-bullet-p ()
210 "Is point at the bullet of a plain list item?"
212 (not (member (char-after) '(?\ ?
\t)))
213 (< (point) (match-end 0))))
215 (defun org-in-item-p ()
216 "It the cursor inside a plain list item.
217 Does not have to be the first line."
221 (org-beginning-of-item)
226 (defun org-insert-item (&optional checkbox
)
227 "Insert a new item at the current level.
228 Return t when things worked, nil when we are not in an item."
229 (when (save-excursion
232 (org-beginning-of-item)
234 (if (org-invisible-p) (error "Invisible item"))
237 (let* ((bul (match-string 0))
238 (descp (save-excursion (goto-char (match-beginning 0))
239 (beginning-of-line 1)
241 (and (looking-at "[ \t]*\\(.*?\\) ::")
243 (empty-line-p (save-excursion
244 (goto-char (match-beginning 0))
246 (or (beginning-of-line 0) t
)
248 (looking-at "[ \t]*$")))))
251 (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
253 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
255 (blank-a (if org-empty-line-terminates-plain-lists
257 (cdr (assq 'plain-list-item org-blank-before-new-entry
))))
258 (blank (if (eq blank-a
'auto
) empty-line-p blank-a
))
260 (if descp
(setq checkbox nil
))
262 (progn (org-timer-item) t
)
264 ((and (org-at-item-p) (<= (point) eow
))
266 (beginning-of-line 1)
267 (open-line (if blank
2 1)))
269 (beginning-of-line 1))
271 (unless (org-get-alist-option org-M-RET-may-split-line
'item
)
273 (delete-horizontal-space))
274 (newline (if blank
2 1))))
276 (if checkbox
"[ ]" "")
277 (if descp
(concat (if checkbox
" " "")
278 (read-string "Term: ") " :: ") ""))
282 (unless (= (point) pos
) (just-one-space) (backward-delete-char 1)))
283 (org-maybe-renumber-ordered-list)
284 (and checkbox
(org-update-checkbox-count-maybe))
289 (defun org-at-item-checkbox-p ()
290 "Is point at a line starting a plain-list item with a checklet?"
293 (goto-char (match-end 0))
294 (skip-chars-forward " \t")
295 (looking-at "\\[[- X]\\]"))))
297 (defun org-toggle-checkbox (&optional toggle-presence
)
298 "Toggle the checkbox in the current line.
299 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
300 With double prefix, set checkbox to [-].
301 When there is an active region, toggle status or presence of the checkbox
302 in the first line, and make every item in the region have the same
303 status or presence, respectively.
304 If the cursor is in a headline, apply this to all checkbox items in the
305 text below the heading."
308 (let (beg end status first-present first-status blocked
)
310 ((org-region-active-p)
311 (setq beg
(region-beginning) end
(region-end)))
313 (setq beg
(point) end
(save-excursion (outline-next-heading) (point))))
314 ((org-at-item-checkbox-p)
316 (if (equal toggle-presence
'(4))
319 (goto-char (match-beginning 0))
321 (when (setq blocked
(org-checkbox-blocked-p))
322 (error "Checkbox blocked because of unchecked box in line %d"
325 (cond ((equal toggle-presence
'(16)) "[-]")
326 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
333 (goto-char (match-end 0))
336 (t (error "Not at a checkbox or heading, and no active region")))
337 (setq end
(move-marker (make-marker) end
))
340 (setq first-present
(org-at-item-checkbox-p)
343 (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t
)
344 (equal (match-string 1) "[X]"))))
345 (while (< (point) end
)
348 ((and first-present
(org-at-item-checkbox-p))
351 (goto-char (match-beginning 0))
353 ((and (not first-present
) (not (org-at-item-checkbox-p))
356 (goto-char (match-end 0))
358 (when (org-at-item-checkbox-p)
359 (setq status
(equal (match-string 0) "[X]"))
361 (if first-status
"[ ]" "[X]") t t
)))
362 (beginning-of-line 2)))))
363 (org-update-checkbox-count-maybe))
365 (defun org-reset-checkbox-state-subtree ()
366 "Reset all checkboxes in an entry subtree."
370 (org-narrow-to-subtree)
372 (goto-char (point-min))
373 (let ((end (point-max)))
374 (while (< (point) end
)
375 (when (org-at-item-checkbox-p)
376 (replace-match "[ ]" t t
))
377 (beginning-of-line 2))))
378 (org-update-checkbox-count-maybe)))
380 (defun org-checkbox-blocked-p ()
381 "Is the current checkbox blocked from for being checked now?
382 A checkbox is blocked if all of the following conditions are fulfilled:
384 1. The checkbox is not checked already.
385 2. The current entry has the ORDERED property set.
386 3. There is an unchecked checkbox in this entry before the current line."
390 (unless (org-at-item-checkbox-p) (throw 'exit nil
))
391 (when (equal (match-string 0) "[X]")
392 ;; the box is already checked!
394 (let ((end (point-at-bol)))
395 (condition-case nil
(org-back-to-heading t
)
396 (error (throw 'exit nil
)))
397 (unless (org-entry-get nil
"ORDERED") (throw 'exit nil
))
398 (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t
)
402 (defvar org-checkbox-statistics-hook nil
403 "Hook that is run whenever Org thinks checkbox statistics should be updated.
404 This hook runs even if `org-provide-checkbox-statistics' is nil, to it can
405 be used to implement alternative ways of collecting statistics information.")
407 (defun org-update-checkbox-count-maybe ()
408 "Update checkbox statistics unless turned off by user."
409 (when org-provide-checkbox-statistics
410 (org-update-checkbox-count))
411 (run-hooks 'org-checkbox-statistics-hook
))
413 (defun org-update-checkbox-count (&optional all
)
414 "Update the checkbox statistics in the current section.
415 This will find all statistic cookies like [57%] and [6/12] and update them
416 with the current numbers. With optional prefix argument ALL, do this for
420 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
421 (beg (condition-case nil
422 (progn (org-back-to-heading) (point))
423 (error (point-min))))
424 (end (move-marker (make-marker)
425 (progn (outline-next-heading) (point))))
426 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
427 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
428 (re-find (concat re
"\\|" re-box
))
429 beg-cookie end-cookie is-percent c-on c-off lim
430 eline curr-ind next-ind continue-from startsearch
432 (or (not org-hierarchical-checkbox-statistics
)
433 (string-match "\\<recursive\\>"
434 (or (org-entry-get nil
"COOKIE_DATA") ""))))
438 (goto-char (point-min))
439 (outline-next-heading)
440 (setq beg
(point) end
(point-max)))
442 ;; find each statistics cookie
443 (while (and (re-search-backward re-find beg t
)
444 (not (save-match-data
445 (and (org-on-heading-p)
446 (string-match "\\<todo\\>"
451 (setq beg-cookie
(match-beginning 1)
452 end-cookie
(match-end 1)
453 cstat
(+ cstat
(if end-cookie
1 0))
454 startsearch
(point-at-eol)
455 continue-from
(match-beginning 0)
456 is-percent
(match-beginning 2)
458 ((org-on-heading-p) (outline-next-heading) (point))
459 ((org-at-item-p) (org-end-of-item) (point))
464 ;; find first checkbox for this cookie and gather
465 ;; statistics from all that are at this indentation level
466 (goto-char startsearch
)
467 (if (re-search-forward re-box lim t
)
469 (org-beginning-of-item)
470 (setq curr-ind
(org-get-indentation))
471 (setq next-ind curr-ind
)
472 (while (and (bolp) (org-at-item-p)
474 (<= curr-ind next-ind
)
475 (= curr-ind next-ind
)))
476 (save-excursion (end-of-line) (setq eline
(point)))
477 (if (re-search-forward re-box eline t
)
478 (if (member (match-string 2) '("[ ]" "[-]"))
479 (setq c-off
(1+ c-off
))
480 (setq c-on
(1+ c-on
))))
484 (when (re-search-forward org-list-beginning-re lim t
)
485 (beginning-of-line)))
486 (setq next-ind
(org-get-indentation)))))
487 (goto-char continue-from
)
490 (delete-region beg-cookie end-cookie
)
491 (goto-char beg-cookie
)
494 (format "[%d%%]" (/ (* 100 c-on
) (max 1 (+ c-on c-off
))))
495 (format "[%d/%d]" c-on
(+ c-on c-off
)))))
496 ;; update items checkbox if it has one
497 (when (org-at-item-p)
498 (org-beginning-of-item)
499 (when (and (> (+ c-on c-off
) 0)
500 (re-search-forward re-box
(point-at-eol) t
))
501 (setq beg-cookie
(match-beginning 2)
502 end-cookie
(match-end 2))
503 (delete-region beg-cookie end-cookie
)
504 (goto-char beg-cookie
)
505 (cond ((= c-off
0) (insert "[X]"))
506 ((= c-on
0) (insert "[ ]"))
509 (goto-char continue-from
))
510 (when (interactive-p)
511 (message "Checkbox statistics updated %s (%d places)"
512 (if all
"in entire file" "in current outline entry") cstat
)))))
514 (defun org-get-checkbox-statistics-face ()
515 "Select the face for checkbox statistics.
516 The face will be `org-done' when all relevant boxes are checked. Otherwise
517 it will be `org-todo'."
519 (if (equal (match-string 1) "100%")
520 'org-checkbox-statistics-done
521 'org-checkbox-statistics-todo
)
522 (if (and (> (match-end 2) (match-beginning 2))
523 (equal (match-string 2) (match-string 3)))
524 'org-checkbox-statistics-done
525 'org-checkbox-statistics-todo
)))
527 (defun org-beginning-of-item ()
528 "Go to the beginning of the current hand-formatted item.
529 If the cursor is not in an item, throw an error."
532 (limit (save-excursion
535 (org-back-to-heading)
536 (beginning-of-line 2) (point))
537 (error (point-min)))))
538 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
541 (beginning-of-line 1)
542 (beginning-of-line 1)
543 (skip-chars-forward " \t")
544 (setq ind
(current-column))
547 (beginning-of-line 0)
548 (if (or (bobp) (< (point) limit
)) (throw 'exit nil
))
550 (if (looking-at "[ \t]*$")
551 (setq ind1 ind-empty
)
552 (skip-chars-forward " \t")
553 (setq ind1
(current-column)))
555 (progn (beginning-of-line 1) (throw 'exit
(org-at-item-p))))))
558 (error "Not in an item")))))
560 (defun org-end-of-item ()
561 "Go to the end of the current hand-formatted item.
562 If the cursor is not in an item, throw an error."
566 (ind-empty (if org-empty-line-terminates-plain-lists
0 10000))
567 (limit (save-excursion (outline-next-heading) (point)))
569 (org-beginning-of-item)
570 (skip-chars-forward " \t")
574 (beginning-of-line 2)
575 (if (eobp) (throw 'exit
(point)))
576 (if (>= (point) limit
) (throw 'exit
(point-at-bol)))
577 (if (looking-at "[ \t]*$")
578 (setq ind1 ind-empty
)
579 (skip-chars-forward " \t")
580 (setq ind1
(current-column)))
582 (throw 'exit
(point-at-bol)))))))
586 (error "Not in an item"))))
588 (defun org-next-item ()
589 "Move to the beginning of the next item in the current plain list.
590 Error if not at a plain list, or if this is the last item in the list."
592 (let (ind ind1
(pos (point)))
593 (org-beginning-of-item)
594 (setq ind
(org-get-indentation))
596 (setq ind1
(org-get-indentation))
597 (unless (and (org-at-item-p) (= ind ind1
))
599 (error "On last item"))))
601 (defun org-previous-item ()
602 "Move to the beginning of the previous item in the current plain list.
603 Error if not at a plain list, or if this is the first item in the list."
605 (let (beg ind ind1
(pos (point)))
606 (org-beginning-of-item)
608 (setq ind
(org-get-indentation))
612 (beginning-of-line 0)
613 (if (looking-at "[ \t]*$")
615 (if (<= (setq ind1
(org-get-indentation)) ind
)
618 (if (or (not (org-at-item-p))
621 (org-beginning-of-item))
622 (error (goto-char pos
)
623 (error "On first item")))))
625 (defun org-first-list-item-p ()
626 "Is this heading the first item in a plain list?"
627 (unless (org-at-item-p)
628 (error "Not at a plain list item"))
630 (org-beginning-of-item)
631 (= (point) (save-excursion (org-beginning-of-item-list)))))
633 (defun org-move-item-down ()
634 "Move the plain list item at point down, i.e. swap with following item.
635 Subitems (items with larger indentation) are considered part of the item,
636 so this really moves item trees."
638 (let ((col (current-column))
640 beg beg0 end end0 ind ind1 txt ne-end ne-beg
)
641 (org-beginning-of-item)
644 (setq ne-beg
(org-back-over-empty-lines))
647 (setq ind
(org-get-indentation))
650 (setq ind1
(org-get-indentation))
651 (setq ne-end
(org-back-over-empty-lines))
654 (when (and (org-first-list-item-p) (< ne-end ne-beg
))
655 ;; include less whitespace
658 (forward-line (- ne-beg ne-end
))
661 (if (and (org-at-item-p) (= ind ind1
))
664 (org-back-over-empty-lines)
665 (setq txt
(buffer-substring beg end
))
667 (delete-region beg end
))
670 (goto-char pos
) (org-skip-whitespace)
671 (org-maybe-renumber-ordered-list)
672 (move-to-column col
))
675 (error "Cannot move this item further down"))))
677 (defun org-move-item-up (arg)
678 "Move the plain list item at point up, i.e. swap with previous item.
679 Subitems (items with larger indentation) are considered part of the item,
680 so this really moves item trees."
682 (let ((col (current-column)) (pos (point))
683 beg beg0 end ind ind1 txt
684 ne-beg ne-ins ins-end
)
685 (org-beginning-of-item)
687 (setq ind
(org-get-indentation))
689 (setq ne-beg
(org-back-over-empty-lines))
693 (org-back-over-empty-lines)
698 (beginning-of-line 0)
699 (if (looking-at "[ \t]*$")
700 (if org-empty-line-terminates-plain-lists
703 (error "Cannot move this item further up"))
705 (if (<= (setq ind1
(org-get-indentation)) ind
)
708 (org-beginning-of-item)
709 (error (goto-char beg0
)
711 (error "Cannot move this item further up")))
712 (setq ind1
(org-get-indentation))
713 (if (and (org-at-item-p) (= ind ind1
))
715 (setq ne-ins
(org-back-over-empty-lines))
716 (setq txt
(buffer-substring beg end
))
718 (delete-region beg end
))
721 (setq ins-end
(point))
722 (goto-char pos
) (org-skip-whitespace)
724 (when (and (org-first-list-item-p) (> ne-ins ne-beg
))
725 ;; Move whitespace back to beginning
728 (let ((kill-whole-line t
))
729 (kill-line (- ne-ins ne-beg
)) (point)))
730 (insert (make-string (- ne-ins ne-beg
) ?
\n)))
732 (org-maybe-renumber-ordered-list)
733 (move-to-column col
))
736 (error "Cannot move this item further up"))))
738 (defun org-maybe-renumber-ordered-list ()
739 "Renumber the ordered list at point if setup allows it.
740 This tests the user option `org-auto-renumber-ordered-lists' before
741 doing the renumbering."
743 (when (and org-auto-renumber-ordered-lists
745 (if (match-beginning 3)
746 (org-renumber-ordered-list 1)
747 (org-fix-bullet-type))))
749 (defun org-maybe-renumber-ordered-list-safe ()
752 (org-maybe-renumber-ordered-list))
755 (defun org-cycle-list-bullet (&optional which
)
756 "Cycle through the different itemize/enumerate bullets.
757 This cycle the entire list level through the sequence:
759 `-' -> `+' -> `*' -> `1.' -> `1)'
761 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
762 0 means `-', 1 means `+' etc."
765 (org-beginning-of-item-list)
767 (beginning-of-line 1)
768 (let ((current (match-string 0))
769 (prevp (eq which
'previous
))
772 ((and (numberp which
)
773 (nth (1- which
) '("-" "+" "*" "1." "1)"))))
774 ((string-match "-" current
) (if prevp
"1)" "+"))
775 ((string-match "\\+" current
)
776 (if prevp
"-" (if (looking-at "\\S-") "1." "*")))
777 ((string-match "\\*" current
) (if prevp
"+" "1."))
778 ((string-match "\\." current
)
779 (if prevp
(if (looking-at "\\S-") "+" "*") "1)"))
780 ((string-match ")" current
) (if prevp
"1." "-"))
781 (t (error "This should not happen"))))
782 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
783 (setq old
(match-string 2))
784 (replace-match (concat "\\1" new
)))
785 (org-shift-item-indentation (- (length new
) (length old
)))
786 (org-fix-bullet-type)
787 (org-maybe-renumber-ordered-list))))
789 (defun org-get-string-indentation (s)
790 "What indentation has S due to SPACE and TAB at the beginning of the string?"
791 (let ((n -
1) (i 0) (w tab-width
) c
)
793 (while (< (setq n
(1+ n
)) (length s
))
795 (cond ((= c ?\
) (setq i
(1+ i
)))
796 ((= c ?
\t) (setq i
(* (/ (+ w i
) w
) w
)))
797 (t (throw 'exit t
)))))
800 (defun org-renumber-ordered-list (arg)
801 "Renumber an ordered plain list.
802 Cursor needs to be in the first line of an item, the line that starts
803 with something like \"1.\" or \"2)\"."
805 (unless (and (org-at-item-p)
807 (error "This is not an ordered list"))
808 (let ((line (org-current-line))
809 (col (current-column))
810 (ind (org-get-string-indentation
811 (buffer-substring (point-at-bol) (match-beginning 3))))
812 ;; (term (substring (match-string 3) -1))
815 ;; find where this list begins
816 (org-beginning-of-item-list)
818 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
819 (setq fmt
(concat "%d" (or (match-string 1) ".")))
820 (beginning-of-line 0)
821 ;; walk forward and replace these numbers
825 (if bobp
(setq bobp nil
) (beginning-of-line 2))
826 (if (eobp) (throw 'exit nil
))
827 (if (looking-at "[ \t]*$") (throw 'next nil
))
828 (skip-chars-forward " \t") (setq ind1
(current-column))
829 (if (> ind1 ind
) (throw 'next t
))
830 (if (< ind1 ind
) (throw 'exit t
))
831 (if (not (org-at-item-p)) (throw 'exit nil
))
832 (setq old
(match-string 2))
833 (delete-region (match-beginning 2) (match-end 2))
834 (goto-char (match-beginning 2))
835 (insert (setq new
(format fmt
(setq n
(1+ n
)))))
836 (org-shift-item-indentation (- (length new
) (length old
))))))
838 (org-move-to-column col
)))
840 (defun org-fix-bullet-type (&optional force-bullet
)
841 "Make sure all items in this list have the same bullet as the first item.
842 Also, fix the indentation."
844 (unless (org-at-item-p) (error "This is not a list"))
845 (let ((line (org-current-line))
846 (col (current-column))
847 (ind (current-indentation))
848 ind1 bullet oldbullet
)
849 ;; find where this list begins
850 (org-beginning-of-item-list)
851 (beginning-of-line 1)
852 ;; find out what the bullet type is
853 (looking-at "[ \t]*\\(\\S-+\\)")
854 (setq bullet
(concat (or force-bullet
(match-string 1)) " "))
855 (if (and org-list-two-spaces-after-bullet-regexp
856 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
857 (setq bullet
(concat bullet
" ")))
858 ;; walk forward and replace these numbers
859 (beginning-of-line 0)
863 (beginning-of-line 2)
864 (if (eobp) (throw 'exit nil
))
865 (if (looking-at "[ \t]*$") (throw 'next nil
))
866 (skip-chars-forward " \t") (setq ind1
(current-column))
867 (if (> ind1 ind
) (throw 'next t
))
868 (if (< ind1 ind
) (throw 'exit t
))
869 (if (not (org-at-item-p)) (throw 'exit nil
))
870 (skip-chars-forward " \t")
871 (looking-at "\\S-+ *")
872 (setq oldbullet
(match-string 0))
873 (unless (equal bullet oldbullet
) (replace-match bullet
))
874 (org-shift-item-indentation (- (length bullet
) (length oldbullet
))))))
876 (org-move-to-column col
)
877 (if (string-match "[0-9]" bullet
)
878 (org-renumber-ordered-list 1))))
880 (defun org-shift-item-indentation (delta)
881 "Shift the indentation in current item by DELTA."
883 (let ((beg (point-at-bol))
884 (end (progn (org-end-of-item) (point)))
887 (beginning-of-line 0)
888 (while (> (point) beg
)
889 (when (looking-at "[ \t]*\\S-")
890 ;; this is not an empty line
891 (setq i
(org-get-indentation))
892 (if (and (> i
0) (> (setq i
(+ i delta
)) 0))
894 (beginning-of-line 0)))))
896 (defun org-beginning-of-item-list ()
897 "Go to the beginning of the current item list.
898 I.e. to the first item in this list."
900 (org-beginning-of-item)
901 (let ((pos (point-at-bol))
902 (ind (org-get-indentation))
904 ;; find where this list begins
908 (beginning-of-line 0)
909 (if (looking-at "[ \t]*$")
910 (throw (if (bobp) 'exit
'next
) t
))
911 (skip-chars-forward " \t") (setq ind1
(current-column))
914 (not (org-at-item-p)))
915 (and (= (point-at-bol) (point-min))
916 (setq pos
(point-min))))
918 (when (org-at-item-p) (setq pos
(point-at-bol)))))))
921 (defun org-end-of-item-list ()
922 "Go to the end of the current item list.
923 I.e. to the text after the last item."
925 (org-beginning-of-item)
926 (let ((pos (point-at-bol))
927 (ind (org-get-indentation))
929 ;; find where this list begins
933 (beginning-of-line 2)
934 (if (looking-at "[ \t]*$")
936 (progn (setq pos
(point)) (throw 'exit t
))
938 (skip-chars-forward " \t") (setq ind1
(current-column))
941 (not (org-at-item-p)))
944 (setq pos
(point-at-bol))
949 (defvar org-last-indent-begin-marker
(make-marker))
950 (defvar org-last-indent-end-marker
(make-marker))
952 (defun org-outdent-item (arg)
953 "Outdent a local list item."
955 (org-indent-item (- arg
)))
957 (defun org-indent-item (arg)
958 "Indent a local list item."
960 (and (org-region-active-p) (org-cursor-to-region-beginning))
961 (unless (org-at-item-p)
962 (error "Not on an item"))
963 (let (beg end ind ind1 ind-bul delta ind-down ind-up firstp
)
964 (setq firstp
(org-first-list-item-p))
966 (setq end
(and (org-region-active-p) (region-end)))
967 (if (memq last-command
'(org-shiftmetaright org-shiftmetaleft
))
968 (setq beg org-last-indent-begin-marker
969 end org-last-indent-end-marker
)
970 (org-beginning-of-item)
971 (setq beg
(move-marker org-last-indent-begin-marker
(point)))
973 (setq end
(move-marker org-last-indent-end-marker
(or end
(point)))))
975 (setq ind-bul
(org-item-indent-positions)
977 ind-down
(car (nth 2 ind-bul
))
978 ind-up
(car (nth 1 ind-bul
))
980 (if ind-down
(- ind-down ind
) 2)
981 (if ind-up
(- ind-up ind
) -
2)))
982 (if (< (+ delta ind
) 0) (error "Cannot outdent beyond margin"))
983 (while (< (point) end
)
984 (beginning-of-line 1)
985 (skip-chars-forward " \t") (setq ind1
(current-column))
986 (delete-region (point-at-bol) (point))
987 (or (eolp) (org-indent-to-column (+ ind1 delta
)))
988 (beginning-of-line 2)))
992 (cdr (assoc (cdr (nth 0 ind-bul
)) org-list-demote-modify-bullet
))))
993 (org-maybe-renumber-ordered-list-safe)
995 (beginning-of-line 0)
996 (condition-case nil
(org-beginning-of-item) (error nil
))
997 (org-maybe-renumber-ordered-list-safe))))
999 (defun org-item-indent-positions ()
1000 "Return indentation for plain list items.
1001 This returns a list with three values: The current indentation, the
1002 parent indentation and the indentation a child should have.
1003 Assumes cursor in item line."
1004 (let* ((bolpos (point-at-bol))
1005 (ind (org-get-indentation))
1006 (bullet (org-get-bullet))
1007 ind-down ind-up bullet-up bullet-down pos
)
1009 (org-beginning-of-item-list)
1010 (skip-chars-backward "\n\r \t")
1011 (when (org-in-item-p)
1012 (org-beginning-of-item)
1013 (setq ind-up
(org-get-indentation))
1014 (setq bullet-up
(org-get-bullet))))
1018 ((and (condition-case nil
(progn (org-previous-item) t
)
1020 (or (forward-char 1) t
)
1021 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t
))
1022 (setq ind-down
(org-get-indentation)
1023 bullet-down
(org-get-bullet)))
1024 ((and (goto-char pos
)
1026 (goto-char (match-end 0))
1027 (skip-chars-forward " \t")
1028 (setq ind-down
(current-column)
1029 bullet-down
(org-get-bullet)))))
1030 (if (and bullet-down
(string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-down
))
1031 (setq bullet-down
(concat "1" (match-string 1 bullet-down
))))
1032 (if (and bullet-up
(string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-up
))
1033 (setq bullet-up
(concat "1" (match-string 1 bullet-up
))))
1034 (if (and bullet
(string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet
))
1035 (setq bullet
(concat "1" (match-string 1 bullet
))))
1036 (list (cons ind bullet
)
1037 (cons ind-up bullet-up
)
1038 (cons ind-down bullet-down
))))
1040 (defun org-get-bullet ()
1042 (goto-char (point-at-bol))
1044 "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\(\\*\\)\\)\\( \\|$\\)")
1045 (or (match-string 2) (match-string 4)))))
1047 ;;; Send and receive lists
1049 (defun org-list-parse-list (&optional delete
)
1050 "Parse the list at point and maybe DELETE it.
1051 Return a list containing first level items as strings and
1052 sublevels as a list of strings."
1053 (let* ((item-beginning (org-list-item-beginning))
1054 (start (car item-beginning
))
1055 (end (org-list-end (cdr item-beginning
)))
1056 output itemsep ltype
)
1057 (while (re-search-forward org-list-beginning-re end t
)
1058 (goto-char (match-beginning 3))
1060 (cond ((string-match "[0-9]" (match-string 2))
1061 (setq itemsep
"[0-9]+\\(?:\\.\\|)\\)"
1063 ((string-match "^.*::" (match-string 0))
1064 (setq itemsep
"[-+]" ltype
'descriptive
))
1065 (t (setq itemsep
"[-+]" ltype
'unordered
))))
1066 (let* ((indent1 (match-string 1))
1067 (nextitem (save-excursion
1069 (or (and (re-search-forward
1070 (concat "^" indent1 itemsep
" *?") end t
)
1071 (match-beginning 0)) end
))))
1072 (item (buffer-substring
1074 (or (and (re-search-forward
1075 org-list-beginning-re end t
)
1076 (goto-char (match-beginning 0)))
1078 (nextindent (match-string 1))
1079 (item (org-trim item
))
1080 (item (if (string-match "^\\[\\([xX ]\\)\\]" item
)
1081 (replace-match (if (equal (match-string 1 item
) " ")
1087 (when (> (length nextindent
)
1089 (narrow-to-region (point) nextitem
)
1090 (push (org-list-parse-list) output
)
1092 (when delete
(delete-region start end
))
1093 (setq output
(nreverse output
))
1094 (push ltype output
)))
1096 (defun org-list-item-beginning ()
1097 "Find the beginning of the list item.
1098 Return a cons which car is the beginning position of the item and
1099 cdr is the indentation string."
1101 (if (not (or (looking-at org-list-beginning-re
)
1103 org-list-beginning-re nil t
)))
1104 (progn (goto-char (point-min)) (point))
1105 (cons (match-beginning 0) (match-string 1)))))
1107 (defun org-list-goto-true-beginning ()
1108 "Go to the beginning of the list at point."
1109 (beginning-of-line 1)
1110 (while (looking-at org-list-beginning-re
)
1111 (beginning-of-line 0))
1113 (re-search-forward org-list-beginning-re nil t
)
1114 (goto-char (match-beginning 0))))
1116 (defun org-list-make-subtree ()
1117 "Convert the plain list at point into a subtree."
1119 (org-list-goto-true-beginning)
1120 (let ((list (org-list-parse-list t
)) nstars
)
1122 (if (condition-case nil
1123 (org-back-to-heading)
1125 (progn (re-search-forward org-complex-heading-regexp nil t
)
1126 (setq nstars
(length (match-string 1))))
1128 (org-list-make-subtrees list
(1+ nstars
))))
1130 (defun org-list-make-subtrees (list level
)
1131 "Convert LIST into subtrees starting at LEVEL."
1132 (if (symbolp (car list
))
1133 (org-list-make-subtrees (cdr list
) level
)
1134 (mapcar (lambda (item)
1136 (insert (make-string
1137 (if org-odd-levels-only
1138 (1- (* 2 level
)) level
) ?
*) " " item
"\n")
1139 (org-list-make-subtrees item
(1+ level
))))
1142 (defun org-list-end (indent)
1143 "Return the position of the end of the list.
1144 INDENT is the indentation of the list, as a string."
1147 (while (or (looking-at org-list-beginning-re
)
1148 (looking-at (concat "^" indent
"[ \t]+\\|^$"))
1149 (> (or (get-text-property (point) 'original-indentation
) -
1)
1151 (if (eq (point) (point-max))
1152 (throw 'exit
(point-max)))
1156 (defun org-list-insert-radio-list ()
1157 "Insert a radio list template appropriate for this major mode."
1159 (let* ((e (assq major-mode org-list-radio-list-templates
))
1162 (unless e
(error "No radio list setup defined for %s" major-mode
))
1163 (setq name
(read-string "List name: "))
1164 (while (string-match "%n" txt
)
1165 (setq txt
(replace-match name t t txt
)))
1166 (or (bolp) (insert "\n"))
1171 (defun org-list-send-list (&optional maybe
)
1172 "Send a tranformed version of this list to the receiver position.
1173 With argument MAYBE, fail quietly if no transformation is defined for
1177 (unless (org-at-item-p) (error "Not at a list"))
1179 (org-list-goto-true-beginning)
1180 (beginning-of-line 0)
1181 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1184 (error "Don't know how to transform this list"))))
1185 (let* ((name (match-string 1))
1186 (item-beginning (org-list-item-beginning))
1187 (transform (intern (match-string 2)))
1188 (txt (buffer-substring-no-properties
1189 (car item-beginning
)
1190 (org-list-end (cdr item-beginning
))))
1191 (list (org-list-parse-list))
1193 (unless (fboundp transform
)
1194 (error "No such transformation function %s" transform
))
1195 (setq txt
(funcall transform list
))
1196 ;; Find the insertion place
1198 (goto-char (point-min))
1199 (unless (re-search-forward
1200 (concat "BEGIN RECEIVE ORGLST +" name
"\\([ \t]\\|$\\)") nil t
)
1201 (error "Don't know where to insert translated list"))
1202 (goto-char (match-beginning 0))
1203 (beginning-of-line 2)
1205 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name
) nil t
)
1206 (error "Cannot find end of insertion region"))
1207 (beginning-of-line 1)
1208 (delete-region beg
(point))
1211 (message "List converted and installed at receiver location"))))
1213 (defun org-list-to-generic (list params
)
1214 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1216 Valid parameters PARAMS are
1218 :ustart String to start an unordered list
1219 :uend String to end an unordered list
1221 :ostart String to start an ordered list
1222 :oend String to end an ordered list
1224 :dstart String to start a descriptive list
1225 :dend String to end a descriptive list
1226 :dtstart String to start a descriptive term
1227 :dtend String to end a descriptive term
1228 :ddstart String to start a description
1229 :ddend String to end a description
1231 :splice When set to t, return only list body lines, don't wrap
1232 them into :[u/o]start and :[u/o]end. Default is nil.
1234 :istart String to start a list item
1235 :iend String to end a list item
1236 :isep String to separate items
1237 :lsep String to separate sublists
1239 :cboff String to insert for an unchecked checkbox
1240 :cbon String to insert for a checked checkbox"
1242 (let* ((p params
) sublist
1243 (splicep (plist-get p
:splice
))
1244 (ostart (plist-get p
:ostart
))
1245 (oend (plist-get p
:oend
))
1246 (ustart (plist-get p
:ustart
))
1247 (uend (plist-get p
:uend
))
1248 (dstart (plist-get p
:dstart
))
1249 (dend (plist-get p
:dend
))
1250 (dtstart (plist-get p
:dtstart
))
1251 (dtend (plist-get p
:dtend
))
1252 (ddstart (plist-get p
:ddstart
))
1253 (ddend (plist-get p
:ddend
))
1254 (istart (plist-get p
:istart
))
1255 (iend (plist-get p
:iend
))
1256 (isep (plist-get p
:isep
))
1257 (lsep (plist-get p
:lsep
))
1258 (cbon (plist-get p
:cbon
))
1259 (cboff (plist-get p
:cboff
)))
1261 (cond ((eq (car list
) 'ordered
)
1262 (concat ostart
"\n%s" oend
"\n"))
1263 ((eq (car list
) 'unordered
)
1264 (concat ustart
"\n%s" uend
"\n"))
1265 ((eq (car list
) 'descriptive
)
1266 (concat dstart
"\n%s" dend
"\n"))))
1267 rtn term defstart defend
)
1268 (while (setq sublist
(pop list
))
1269 (cond ((symbolp sublist
) nil
)
1271 (when (string-match "^\\(.*\\) ::" sublist
)
1272 (setq term
(org-trim (format (concat dtstart
"%s" dtend
)
1273 (match-string 1 sublist
))))
1274 (setq sublist
(substring sublist
(1+ (length term
)))))
1275 (if (string-match "\\[CBON\\]" sublist
)
1276 (setq sublist
(replace-match cbon t t sublist
)))
1277 (if (string-match "\\[CBOFF\\]" sublist
)
1278 (setq sublist
(replace-match cboff t t sublist
)))
1279 (setq rtn
(concat rtn istart term ddstart
1280 sublist ddend iend isep
)))
1281 (t (setq rtn
(concat rtn
;; previous list
1282 lsep
;; list separator
1283 (org-list-to-generic sublist p
)
1284 lsep
;; list separator
1286 (format wrapper rtn
))))
1288 (defun org-list-to-latex (list &optional params
)
1289 "Convert LIST into a LaTeX list.
1290 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1291 with overruling parameters for `org-list-to-generic'."
1292 (org-list-to-generic
1295 '(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
1296 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
1297 :dstart
"\\begin{description}" :dend
"\\end{description}"
1298 :dtstart
"[" :dtend
"]"
1299 :ddstart
"" :ddend
""
1300 :istart
"\\item " :iend
""
1301 :isep
"\n" :lsep
"\n"
1302 :cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
1305 (defun org-list-to-html (list &optional params
)
1306 "Convert LIST into a HTML list.
1307 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1308 with overruling parameters for `org-list-to-generic'."
1309 (org-list-to-generic
1312 '(:splicep nil
:ostart
"<ol>" :oend
"</ol>"
1313 :ustart
"<ul>" :uend
"</ul>"
1314 :dstart
"<dl>" :dend
"</dl>"
1315 :dtstart
"<dt>" :dtend
"</dt>"
1316 :ddstart
"<dd>" :ddend
"</dd>"
1317 :istart
"<li>" :iend
"</li>"
1318 :isep
"\n" :lsep
"\n"
1319 :cbon
"<code>[X]</code>" :cboff
"<code>[ ]</code>")
1322 (defun org-list-to-texinfo (list &optional params
)
1323 "Convert LIST into a Texinfo list.
1324 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1325 with overruling parameters for `org-list-to-generic'."
1326 (org-list-to-generic
1329 '(:splicep nil
:ostart
"@itemize @minus" :oend
"@end itemize"
1330 :ustart
"@enumerate" :uend
"@end enumerate"
1331 :dstart
"@table" :dend
"@end table"
1332 :dtstart
"@item " :dtend
"\n"
1333 :ddstart
"" :ddend
""
1334 :istart
"@item\n" :iend
""
1335 :isep
"\n" :lsep
"\n"
1336 :cbon
"@code{[X]}" :cboff
"@code{[ ]}")
1341 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1342 ;;; org-list.el ends here