Checkboxes: Make C-u C-c C-c C-c set the box to "[-]"
[org-mode.git] / lisp / org-list.el
blob0440644d6dbad60bcefc8c942bbf888567edf1e5
1 ;;; org-list.el --- Plain lists for Org-mode
2 ;;
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
5 ;;
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
10 ;; Version: 6.22trans
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; This file contains the code dealing with plain lists in Org-mode.
32 ;;; Code:
34 (require 'org-macs)
35 (require 'org-compat)
37 (defvar org-blank-before-new-entry)
38 (defvar org-M-RET-may-split-line)
40 (declare-function org-invisible-p "org" ())
41 (declare-function org-on-heading-p "org" (&optional invisible-ok))
42 (declare-function outline-next-heading "outline" ())
43 (declare-function outline-back-to-heading "outline" (&optional invisible-ok))
44 (declare-function org-back-to-heading "org" (&optional invisible-ok))
45 (declare-function org-back-over-empty-lines "org" ())
46 (declare-function org-skip-whitespace "org" ())
47 (declare-function org-trim "org" (s))
48 (declare-function org-get-indentation "org" (&optional line))
49 (declare-function org-timer-item "org-timer" (&optional arg))
50 (declare-function org-combine-plists "org" (&rest plists))
52 (defgroup org-plain-lists nil
53 "Options concerning plain lists in Org-mode."
54 :tag "Org Plain lists"
55 :group 'org-structure)
57 (defcustom org-cycle-include-plain-lists nil
58 "Non-nil means, include plain lists into visibility cycling.
59 This means that during cycling, plain list items will *temporarily* be
60 interpreted as outline headlines with a level given by 1000+i where i is the
61 indentation of the bullet. In all other operations, plain list items are
62 not seen as headlines. For example, you cannot assign a TODO keyword to
63 such an item."
64 :group 'org-plain-lists
65 :type 'boolean)
67 (defcustom org-plain-list-ordered-item-terminator t
68 "The character that makes a line with leading number an ordered list item.
69 Valid values are ?. and ?\). To get both terminators, use t. While
70 ?. may look nicer, it creates the danger that a line with leading
71 number may be incorrectly interpreted as an item. ?\) therefore is
72 the safe choice."
73 :group 'org-plain-lists
74 :type '(choice (const :tag "dot like in \"2.\"" ?.)
75 (const :tag "paren like in \"2)\"" ?\))
76 (const :tab "both" t)))
78 (defcustom org-list-two-spaces-after-bullet-regexp nil
79 "A regular expression matching bullets that should have 2 spaces after them.
80 When nil, no bullet will have two spaces after them.
81 When a string, it will be used as a regular expression. When the bullet
82 type of a list is changed, the new bullet type will be matched against this
83 regexp. If it matches, there will be two spaces instead of one after
84 the bullet in each item of he list."
85 :group 'org-plain-list
86 :type '(choice
87 (const :tag "never" nil)
88 (regexp)))
90 (defcustom org-empty-line-terminates-plain-lists nil
91 "Non-nil means, an empty line ends all plain list levels.
92 When nil, empty lines are part of the preceding item."
93 :group 'org-plain-lists
94 :type 'boolean)
96 (defcustom org-auto-renumber-ordered-lists t
97 "Non-nil means, automatically renumber ordered plain lists.
98 Renumbering happens when the sequence have been changed with
99 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
100 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
101 :group 'org-plain-lists
102 :type 'boolean)
104 (defcustom org-provide-checkbox-statistics t
105 "Non-nil means, update checkbox statistics after insert and toggle.
106 When this is set, checkbox statistics is updated each time you either insert
107 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
108 with \\[org-ctrl-c-ctrl-c\\]."
109 :group 'org-plain-lists
110 :type 'boolean)
112 (defcustom org-description-max-indent 20
113 "Maximum indentation for the second line of a description list.
114 When the indentation would be larger than this, it will become
115 5 characters instead."
116 :group 'org-plain-lists
117 :type 'integer)
119 (defvar org-list-beginning-re
120 "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +\\(.*\\)$")
122 (defcustom org-list-radio-list-templates
123 '((latex-mode "% BEGIN RECEIVE ORGLST %n
124 % END RECEIVE ORGLST %n
125 \\begin{comment}
126 #+ORGLST: SEND %n org-list-to-latex
127 | | |
128 \\end{comment}\n")
129 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
130 @c END RECEIVE ORGLST %n
131 @ignore
132 #+ORGLST: SEND %n org-list-to-texinfo
133 | | |
134 @end ignore\n")
135 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
136 <!-- END RECEIVE ORGLST %n -->
137 <!--
138 #+ORGLST: SEND %n org-list-to-html
139 | | |
140 -->\n"))
141 "Templates for radio lists in different major modes.
142 All occurrences of %n in a template will be replaced with the name of the
143 list, obtained by prompting the user."
144 :group 'org-plain-lists
145 :type '(repeat
146 (list (symbol :tag "Major mode")
147 (string :tag "Format"))))
149 ;;;; Plain list items, including checkboxes
151 ;;; Plain list items
153 (defun org-at-item-p ()
154 "Is point in a line starting a hand-formatted item?"
155 (let ((llt org-plain-list-ordered-item-terminator))
156 (save-excursion
157 (goto-char (point-at-bol))
158 (looking-at
159 (cond
160 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
161 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
162 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
163 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
165 (defun org-at-item-bullet-p ()
166 "Is point at the bullet of a plain list item?"
167 (and (org-at-item-p)
168 (not (member (char-after) '(?\ ?\t)))
169 (< (point) (match-end 0))))
171 (defun org-in-item-p ()
172 "It the cursor inside a plain list item.
173 Does not have to be the first line."
174 (save-excursion
175 (condition-case nil
176 (progn
177 (org-beginning-of-item)
178 (org-at-item-p)
180 (error nil))))
182 (defun org-insert-item (&optional checkbox)
183 "Insert a new item at the current level.
184 Return t when things worked, nil when we are not in an item."
185 (when (save-excursion
186 (condition-case nil
187 (progn
188 (org-beginning-of-item)
189 (org-at-item-p)
190 (if (org-invisible-p) (error "Invisible item"))
192 (error nil)))
193 (let* ((bul (match-string 0))
194 (descp (save-excursion (goto-char (match-beginning 0))
195 (beginning-of-line 1)
196 (save-match-data
197 (and (looking-at "[ \t]*\\(.*?\\) ::")
198 (match-string 1)))))
199 (empty-line-p (save-excursion
200 (goto-char (match-beginning 0))
201 (and (not (bobp))
202 (or (beginning-of-line 0) t)
203 (save-match-data
204 (looking-at "[ \t]*$")))))
205 (timerp (and descp
206 (save-match-data
207 (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
208 descp))))
209 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
210 (match-end 0)))
211 (blank-a (cdr (assq 'plain-list-item org-blank-before-new-entry)))
212 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
213 pos)
214 (if descp (setq checkbox nil))
215 (if timerp
216 (progn (org-timer-item) t)
217 (cond
218 ((and (org-at-item-p) (<= (point) eow))
219 ;; before the bullet
220 (beginning-of-line 1)
221 (open-line (if blank 2 1)))
222 ((<= (point) eow)
223 (beginning-of-line 1))
225 (unless (org-get-alist-option org-M-RET-may-split-line 'item)
226 (end-of-line 1)
227 (delete-horizontal-space))
228 (newline (if blank 2 1))))
229 (insert bul
230 (if checkbox "[ ]" "")
231 (if descp (concat (if checkbox " " "")
232 (read-string "Term: ") " :: ") ""))
233 (just-one-space)
234 (setq pos (point))
235 (end-of-line 1)
236 (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
237 (org-maybe-renumber-ordered-list)
238 (and checkbox (org-update-checkbox-count-maybe))
239 t)))
241 ;;; Checkboxes
243 (defun org-at-item-checkbox-p ()
244 "Is point at a line starting a plain-list item with a checklet?"
245 (and (org-at-item-p)
246 (save-excursion
247 (goto-char (match-end 0))
248 (skip-chars-forward " \t")
249 (looking-at "\\[[- X]\\]"))))
251 (defun org-toggle-checkbox (&optional toggle-presence)
252 "Toggle the checkbox in the current line.
253 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
254 With double prefix, set checkbox to [-].
255 When there is an active region, toggle status or presence of the checkbox
256 in the first line, and make every item in the region have the same
257 status or presence, respectively.
258 If the cursor is in a headline, apply this to all checkbox items in the
259 text below the heading."
260 (interactive "P")
261 (catch 'exit
262 (let (beg end status first-present first-status)
263 (cond
264 ((org-region-active-p)
265 (setq beg (region-beginning) end (region-end)))
266 ((org-on-heading-p)
267 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
268 ((org-at-item-checkbox-p)
269 (let ((pos (point)))
270 (if (equal toggle-presence '(4))
271 (progn
272 (replace-match "")
273 (goto-char (match-beginning 0))
274 (just-one-space))
275 (replace-match
276 (cond ((equal toggle-presence '(16)) "[-]")
277 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
278 (t "[ ]"))
279 t t))
280 (goto-char pos))
281 (throw 'exit t))
282 ((org-at-item-p)
283 ;; add a checkbox
284 (save-excursion
285 (goto-char (match-end 0))
286 (insert "[ ] "))
287 (throw 'exit t))
288 (t (error "Not at a checkbox or heading, and no active region")))
289 (setq end (move-marker (make-marker) end))
290 (save-excursion
291 (goto-char beg)
292 (setq first-present (org-at-item-checkbox-p)
293 first-status
294 (save-excursion
295 (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t)
296 (equal (match-string 1) "[X]"))))
297 (while (< (point) end)
298 (if toggle-presence
299 (cond
300 ((and first-present (org-at-item-checkbox-p))
301 (save-excursion
302 (replace-match "")
303 (goto-char (match-beginning 0))
304 (just-one-space)))
305 ((and (not first-present) (not (org-at-item-checkbox-p))
306 (org-at-item-p))
307 (save-excursion
308 (goto-char (match-end 0))
309 (insert "[ ] "))))
310 (when (org-at-item-checkbox-p)
311 (setq status (equal (match-string 0) "[X]"))
312 (replace-match
313 (if first-status "[ ]" "[X]") t t)))
314 (beginning-of-line 2)))))
315 (org-update-checkbox-count-maybe))
317 (defun org-update-checkbox-count-maybe ()
318 "Update checkbox statistics unless turned off by user."
319 (when org-provide-checkbox-statistics
320 (org-update-checkbox-count)))
322 (defun org-update-checkbox-count (&optional all)
323 "Update the checkbox statistics in the current section.
324 This will find all statistic cookies like [57%] and [6/12] and update them
325 with the current numbers. With optional prefix argument ALL, do this for
326 the whole buffer."
327 (interactive "P")
328 (save-excursion
329 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
330 (beg (condition-case nil
331 (progn (org-back-to-heading) (point))
332 (error (point-min))))
333 (end (move-marker (make-marker)
334 (progn (outline-next-heading) (point))))
335 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
336 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
337 (re-find (concat re "\\|" re-box))
338 beg-cookie end-cookie is-percent c-on c-off lim
339 eline curr-ind next-ind continue-from startsearch
340 (cstat 0)
342 (when all
343 (goto-char (point-min))
344 (outline-next-heading)
345 (setq beg (point) end (point-max)))
346 (goto-char end)
347 ;; find each statistic cookie
348 (while (re-search-backward re-find beg t)
349 (setq beg-cookie (match-beginning 1)
350 end-cookie (match-end 1)
351 cstat (+ cstat (if end-cookie 1 0))
352 startsearch (point-at-eol)
353 continue-from (match-beginning 0)
354 is-percent (match-beginning 2)
355 lim (cond
356 ((org-on-heading-p) (outline-next-heading) (point))
357 ((org-at-item-p) (org-end-of-item) (point))
358 (t nil))
359 c-on 0
360 c-off 0)
361 (when lim
362 ;; find first checkbox for this cookie and gather
363 ;; statistics from all that are at this indentation level
364 (goto-char startsearch)
365 (if (re-search-forward re-box lim t)
366 (progn
367 (org-beginning-of-item)
368 (setq curr-ind (org-get-indentation))
369 (setq next-ind curr-ind)
370 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
371 (save-excursion (end-of-line) (setq eline (point)))
372 (if (re-search-forward re-box eline t)
373 (if (member (match-string 2) '("[ ]" "[-]"))
374 (setq c-off (1+ c-off))
375 (setq c-on (1+ c-on))
378 (org-end-of-item)
379 (setq next-ind (org-get-indentation))
381 (goto-char continue-from)
382 ;; update cookie
383 (when end-cookie
384 (delete-region beg-cookie end-cookie)
385 (goto-char beg-cookie)
386 (insert
387 (if is-percent
388 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
389 (format "[%d/%d]" c-on (+ c-on c-off)))))
390 ;; update items checkbox if it has one
391 (when (org-at-item-p)
392 (org-beginning-of-item)
393 (when (and (> (+ c-on c-off) 0)
394 (re-search-forward re-box (point-at-eol) t))
395 (setq beg-cookie (match-beginning 2)
396 end-cookie (match-end 2))
397 (delete-region beg-cookie end-cookie)
398 (goto-char beg-cookie)
399 (cond ((= c-off 0) (insert "[X]"))
400 ((= c-on 0) (insert "[ ]"))
401 (t (insert "[-]")))
403 (goto-char continue-from))
404 (when (interactive-p)
405 (message "Checkbox statistics updated %s (%d places)"
406 (if all "in entire file" "in current outline entry") cstat)))))
408 (defun org-get-checkbox-statistics-face ()
409 "Select the face for checkbox statistics.
410 The face will be `org-done' when all relevant boxes are checked. Otherwise
411 it will be `org-todo'."
412 (if (match-end 1)
413 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
414 (if (and (> (match-end 2) (match-beginning 2))
415 (equal (match-string 2) (match-string 3)))
416 'org-done
417 'org-todo)))
419 (defun org-beginning-of-item ()
420 "Go to the beginning of the current hand-formatted item.
421 If the cursor is not in an item, throw an error."
422 (interactive)
423 (let ((pos (point))
424 (limit (save-excursion
425 (condition-case nil
426 (progn
427 (org-back-to-heading)
428 (beginning-of-line 2) (point))
429 (error (point-min)))))
430 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
431 ind ind1)
432 (if (org-at-item-p)
433 (beginning-of-line 1)
434 (beginning-of-line 1)
435 (skip-chars-forward " \t")
436 (setq ind (current-column))
437 (if (catch 'exit
438 (while t
439 (beginning-of-line 0)
440 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
442 (if (looking-at "[ \t]*$")
443 (setq ind1 ind-empty)
444 (skip-chars-forward " \t")
445 (setq ind1 (current-column)))
446 (if (< ind1 ind)
447 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
449 (goto-char pos)
450 (error "Not in an item")))))
452 (defun org-end-of-item ()
453 "Go to the end of the current hand-formatted item.
454 If the cursor is not in an item, throw an error."
455 (interactive)
456 (let* ((pos (point))
457 ind1
458 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
459 (limit (save-excursion (outline-next-heading) (point)))
460 (ind (save-excursion
461 (org-beginning-of-item)
462 (skip-chars-forward " \t")
463 (current-column)))
464 (end (catch 'exit
465 (while t
466 (beginning-of-line 2)
467 (if (eobp) (throw 'exit (point)))
468 (if (>= (point) limit) (throw 'exit (point-at-bol)))
469 (if (looking-at "[ \t]*$")
470 (setq ind1 ind-empty)
471 (skip-chars-forward " \t")
472 (setq ind1 (current-column)))
473 (if (<= ind1 ind)
474 (throw 'exit (point-at-bol)))))))
475 (if end
476 (goto-char end)
477 (goto-char pos)
478 (error "Not in an item"))))
480 (defun org-next-item ()
481 "Move to the beginning of the next item in the current plain list.
482 Error if not at a plain list, or if this is the last item in the list."
483 (interactive)
484 (let (ind ind1 (pos (point)))
485 (org-beginning-of-item)
486 (setq ind (org-get-indentation))
487 (org-end-of-item)
488 (setq ind1 (org-get-indentation))
489 (unless (and (org-at-item-p) (= ind ind1))
490 (goto-char pos)
491 (error "On last item"))))
493 (defun org-previous-item ()
494 "Move to the beginning of the previous item in the current plain list.
495 Error if not at a plain list, or if this is the first item in the list."
496 (interactive)
497 (let (beg ind ind1 (pos (point)))
498 (org-beginning-of-item)
499 (setq beg (point))
500 (setq ind (org-get-indentation))
501 (goto-char beg)
502 (catch 'exit
503 (while t
504 (beginning-of-line 0)
505 (if (looking-at "[ \t]*$")
507 (if (<= (setq ind1 (org-get-indentation)) ind)
508 (throw 'exit t)))))
509 (condition-case nil
510 (if (or (not (org-at-item-p))
511 (< ind1 (1- ind)))
512 (error "")
513 (org-beginning-of-item))
514 (error (goto-char pos)
515 (error "On first item")))))
517 (defun org-first-list-item-p ()
518 "Is this heading the item in a plain list?"
519 (unless (org-at-item-p)
520 (error "Not at a plain list item"))
521 (org-beginning-of-item)
522 (= (point) (save-excursion (org-beginning-of-item-list))))
524 (defun org-move-item-down ()
525 "Move the plain list item at point down, i.e. swap with following item.
526 Subitems (items with larger indentation) are considered part of the item,
527 so this really moves item trees."
528 (interactive)
529 (let ((col (current-column))
530 (pos (point))
531 beg beg0 end end0 ind ind1 txt ne-end ne-beg)
532 (org-beginning-of-item)
533 (setq beg0 (point))
534 (save-excursion
535 (setq ne-beg (org-back-over-empty-lines))
536 (setq beg (point)))
537 (goto-char beg0)
538 (setq ind (org-get-indentation))
539 (org-end-of-item)
540 (setq end0 (point))
541 (setq ind1 (org-get-indentation))
542 (setq ne-end (org-back-over-empty-lines))
543 (setq end (point))
544 (goto-char beg0)
545 (when (and (org-first-list-item-p) (< ne-end ne-beg))
546 ;; include less whitespace
547 (save-excursion
548 (goto-char beg)
549 (forward-line (- ne-beg ne-end))
550 (setq beg (point))))
551 (goto-char end0)
552 (if (and (org-at-item-p) (= ind ind1))
553 (progn
554 (org-end-of-item)
555 (org-back-over-empty-lines)
556 (setq txt (buffer-substring beg end))
557 (save-excursion
558 (delete-region beg end))
559 (setq pos (point))
560 (insert txt)
561 (goto-char pos) (org-skip-whitespace)
562 (org-maybe-renumber-ordered-list)
563 (move-to-column col))
564 (goto-char pos)
565 (move-to-column col)
566 (error "Cannot move this item further down"))))
568 (defun org-move-item-up (arg)
569 "Move the plain list item at point up, i.e. swap with previous item.
570 Subitems (items with larger indentation) are considered part of the item,
571 so this really moves item trees."
572 (interactive "p")
573 (let ((col (current-column)) (pos (point))
574 beg beg0 end ind ind1 txt
575 ne-beg ne-ins ins-end)
576 (org-beginning-of-item)
577 (setq beg0 (point))
578 (setq ind (org-get-indentation))
579 (save-excursion
580 (setq ne-beg (org-back-over-empty-lines))
581 (setq beg (point)))
582 (goto-char beg0)
583 (org-end-of-item)
584 (org-back-over-empty-lines)
585 (setq end (point))
586 (goto-char beg0)
587 (catch 'exit
588 (while t
589 (beginning-of-line 0)
590 (if (looking-at "[ \t]*$")
591 (if org-empty-line-terminates-plain-lists
592 (progn
593 (goto-char pos)
594 (error "Cannot move this item further up"))
595 nil)
596 (if (<= (setq ind1 (org-get-indentation)) ind)
597 (throw 'exit t)))))
598 (condition-case nil
599 (org-beginning-of-item)
600 (error (goto-char beg0)
601 (move-to-column col)
602 (error "Cannot move this item further up")))
603 (setq ind1 (org-get-indentation))
604 (if (and (org-at-item-p) (= ind ind1))
605 (progn
606 (setq ne-ins (org-back-over-empty-lines))
607 (setq txt (buffer-substring beg end))
608 (save-excursion
609 (delete-region beg end))
610 (setq pos (point))
611 (insert txt)
612 (setq ins-end (point))
613 (goto-char pos) (org-skip-whitespace)
615 (when (and (org-first-list-item-p) (> ne-ins ne-beg))
616 ;; Move whitespace back to beginning
617 (save-excursion
618 (goto-char ins-end)
619 (let ((kill-whole-line t))
620 (kill-line (- ne-ins ne-beg)) (point)))
621 (insert (make-string (- ne-ins ne-beg) ?\n)))
623 (org-maybe-renumber-ordered-list)
624 (move-to-column col))
625 (goto-char pos)
626 (move-to-column col)
627 (error "Cannot move this item further up"))))
629 (defun org-maybe-renumber-ordered-list ()
630 "Renumber the ordered list at point if setup allows it.
631 This tests the user option `org-auto-renumber-ordered-lists' before
632 doing the renumbering."
633 (interactive)
634 (when (and org-auto-renumber-ordered-lists
635 (org-at-item-p))
636 (if (match-beginning 3)
637 (org-renumber-ordered-list 1)
638 (org-fix-bullet-type))))
640 (defun org-maybe-renumber-ordered-list-safe ()
641 (condition-case nil
642 (save-excursion
643 (org-maybe-renumber-ordered-list))
644 (error nil)))
646 (defun org-cycle-list-bullet (&optional which)
647 "Cycle through the different itemize/enumerate bullets.
648 This cycle the entire list level through the sequence:
650 `-' -> `+' -> `*' -> `1.' -> `1)'
652 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
653 0 means `-', 1 means `+' etc."
654 (interactive "P")
655 (org-preserve-lc
656 (org-beginning-of-item-list)
657 (org-at-item-p)
658 (beginning-of-line 1)
659 (let ((current (match-string 0))
660 (prevp (eq which 'previous))
661 new old)
662 (setq new (cond
663 ((and (numberp which)
664 (nth (1- which) '("-" "+" "*" "1." "1)"))))
665 ((string-match "-" current) (if prevp "1)" "+"))
666 ((string-match "\\+" current)
667 (if prevp "-" (if (looking-at "\\S-") "1." "*")))
668 ((string-match "\\*" current) (if prevp "+" "1."))
669 ((string-match "\\." current)
670 (if prevp (if (looking-at "\\S-") "+" "*") "1)"))
671 ((string-match ")" current) (if prevp "1." "-"))
672 (t (error "This should not happen"))))
673 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
674 (setq old (match-string 2))
675 (replace-match (concat "\\1" new)))
676 (org-shift-item-indentation (- (length new) (length old)))
677 (org-fix-bullet-type)
678 (org-maybe-renumber-ordered-list))))
680 (defun org-get-string-indentation (s)
681 "What indentation has S due to SPACE and TAB at the beginning of the string?"
682 (let ((n -1) (i 0) (w tab-width) c)
683 (catch 'exit
684 (while (< (setq n (1+ n)) (length s))
685 (setq c (aref s n))
686 (cond ((= c ?\ ) (setq i (1+ i)))
687 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
688 (t (throw 'exit t)))))
691 (defun org-renumber-ordered-list (arg)
692 "Renumber an ordered plain list.
693 Cursor needs to be in the first line of an item, the line that starts
694 with something like \"1.\" or \"2)\"."
695 (interactive "p")
696 (unless (and (org-at-item-p)
697 (match-beginning 3))
698 (error "This is not an ordered list"))
699 (let ((line (org-current-line))
700 (col (current-column))
701 (ind (org-get-string-indentation
702 (buffer-substring (point-at-bol) (match-beginning 3))))
703 ;; (term (substring (match-string 3) -1))
704 ind1 (n (1- arg))
705 fmt bobp old new)
706 ;; find where this list begins
707 (org-beginning-of-item-list)
708 (setq bobp (bobp))
709 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
710 (setq fmt (concat "%d" (match-string 1)))
711 (beginning-of-line 0)
712 ;; walk forward and replace these numbers
713 (catch 'exit
714 (while t
715 (catch 'next
716 (if bobp (setq bobp nil) (beginning-of-line 2))
717 (if (eobp) (throw 'exit nil))
718 (if (looking-at "[ \t]*$") (throw 'next nil))
719 (skip-chars-forward " \t") (setq ind1 (current-column))
720 (if (> ind1 ind) (throw 'next t))
721 (if (< ind1 ind) (throw 'exit t))
722 (if (not (org-at-item-p)) (throw 'exit nil))
723 (setq old (match-string 2))
724 (delete-region (match-beginning 2) (match-end 2))
725 (goto-char (match-beginning 2))
726 (insert (setq new (format fmt (setq n (1+ n)))))
727 (org-shift-item-indentation (- (length new) (length old))))))
728 (goto-line line)
729 (org-move-to-column col)))
731 (defun org-fix-bullet-type ()
732 "Make sure all items in this list have the same bullet as the first item.
733 Also, fix the indentation."
734 (interactive)
735 (unless (org-at-item-p) (error "This is not a list"))
736 (let ((line (org-current-line))
737 (col (current-column))
738 (ind (current-indentation))
739 ind1 bullet oldbullet)
740 ;; find where this list begins
741 (org-beginning-of-item-list)
742 (beginning-of-line 1)
743 ;; find out what the bullet type is
744 (looking-at "[ \t]*\\(\\S-+\\)")
745 (setq bullet (concat (match-string 1) " "))
746 (if (and org-list-two-spaces-after-bullet-regexp
747 (string-match org-list-two-spaces-after-bullet-regexp bullet))
748 (setq bullet (concat bullet " ")))
749 ;; walk forward and replace these numbers
750 (beginning-of-line 0)
751 (catch 'exit
752 (while t
753 (catch 'next
754 (beginning-of-line 2)
755 (if (eobp) (throw 'exit nil))
756 (if (looking-at "[ \t]*$") (throw 'next nil))
757 (skip-chars-forward " \t") (setq ind1 (current-column))
758 (if (> ind1 ind) (throw 'next t))
759 (if (< ind1 ind) (throw 'exit t))
760 (if (not (org-at-item-p)) (throw 'exit nil))
761 (skip-chars-forward " \t")
762 (looking-at "\\S-+ *")
763 (setq oldbullet (match-string 0))
764 (replace-match bullet)
765 (org-shift-item-indentation (- (length bullet) (length oldbullet))))))
766 (goto-line line)
767 (org-move-to-column col)
768 (if (string-match "[0-9]" bullet)
769 (org-renumber-ordered-list 1))))
771 (defun org-shift-item-indentation (delta)
772 "Shift the indentation in current item by DELTA."
773 (save-excursion
774 (let ((beg (point-at-bol))
775 (end (progn (org-end-of-item) (point)))
777 (goto-char end)
778 (beginning-of-line 0)
779 (while (> (point) beg)
780 (when (looking-at "[ \t]*\\S-")
781 ;; this is not an empty line
782 (setq i (org-get-indentation))
783 (if (and (> i 0) (> (setq i (+ i delta)) 0))
784 (indent-line-to i)))
785 (beginning-of-line 0)))))
787 (defun org-beginning-of-item-list ()
788 "Go to the beginning of the current item list.
789 I.e. to the first item in this list."
790 (interactive)
791 (org-beginning-of-item)
792 (let ((pos (point-at-bol))
793 (ind (org-get-indentation))
794 ind1)
795 ;; find where this list begins
796 (catch 'exit
797 (while t
798 (catch 'next
799 (beginning-of-line 0)
800 (if (looking-at "[ \t]*$")
801 (throw (if (bobp) 'exit 'next) t))
802 (skip-chars-forward " \t") (setq ind1 (current-column))
803 (if (or (< ind1 ind)
804 (and (= ind1 ind)
805 (not (org-at-item-p)))
806 (and (= (point-at-bol) (point-min))
807 (setq pos (point-min))))
808 (throw 'exit t)
809 (when (org-at-item-p) (setq pos (point-at-bol)))))))
810 (goto-char pos)))
813 (defun org-end-of-item-list ()
814 "Go to the end of the current item list.
815 I.e. to the text after the last item."
816 (interactive)
817 (org-beginning-of-item)
818 (let ((pos (point-at-bol))
819 (ind (org-get-indentation))
820 ind1)
821 ;; find where this list begins
822 (catch 'exit
823 (while t
824 (catch 'next
825 (beginning-of-line 2)
826 (if (looking-at "[ \t]*$")
827 (throw (if (eobp) 'exit 'next) t))
828 (skip-chars-forward " \t") (setq ind1 (current-column))
829 (if (or (< ind1 ind)
830 (and (= ind1 ind)
831 (not (org-at-item-p)))
832 (eobp))
833 (progn
834 (setq pos (point-at-bol))
835 (throw 'exit t))))))
836 (goto-char pos)))
839 (defvar org-last-indent-begin-marker (make-marker))
840 (defvar org-last-indent-end-marker (make-marker))
842 (defun org-outdent-item (arg)
843 "Outdent a local list item."
844 (interactive "p")
845 (org-indent-item (- arg)))
847 (defun org-indent-item (arg)
848 "Indent a local list item."
849 (interactive "p")
850 (unless (org-at-item-p)
851 (error "Not on an item"))
852 (save-excursion
853 (let (beg end ind ind1 tmp delta ind-down ind-up)
854 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
855 (setq beg org-last-indent-begin-marker
856 end org-last-indent-end-marker)
857 (org-beginning-of-item)
858 (setq beg (move-marker org-last-indent-begin-marker (point)))
859 (org-end-of-item)
860 (setq end (move-marker org-last-indent-end-marker (point))))
861 (goto-char beg)
862 (setq tmp (org-item-indent-positions)
863 ind (car tmp)
864 ind-down (nth 2 tmp)
865 ind-up (nth 1 tmp)
866 delta (if (> arg 0)
867 (if ind-down (- ind-down ind) 2)
868 (if ind-up (- ind-up ind) -2)))
869 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
870 (while (< (point) end)
871 (beginning-of-line 1)
872 (skip-chars-forward " \t") (setq ind1 (current-column))
873 (delete-region (point-at-bol) (point))
874 (or (eolp) (org-indent-to-column (+ ind1 delta)))
875 (beginning-of-line 2))))
876 (org-fix-bullet-type)
877 (org-maybe-renumber-ordered-list-safe)
878 (save-excursion
879 (beginning-of-line 0)
880 (condition-case nil (org-beginning-of-item) (error nil))
881 (org-maybe-renumber-ordered-list-safe)))
883 (defun org-item-indent-positions ()
884 "Return indentation for plain list items.
885 This returns a list with three values: The current indentation, the
886 parent indentation and the indentation a child should have.
887 Assumes cursor in item line."
888 (let* ((bolpos (point-at-bol))
889 (ind (org-get-indentation))
890 ind-down ind-up pos)
891 (save-excursion
892 (org-beginning-of-item-list)
893 (skip-chars-backward "\n\r \t")
894 (when (org-in-item-p)
895 (org-beginning-of-item)
896 (setq ind-up (org-get-indentation))))
897 (setq pos (point))
898 (save-excursion
899 (cond
900 ((and (condition-case nil (progn (org-previous-item) t)
901 (error nil))
902 (or (forward-char 1) t)
903 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
904 (setq ind-down (org-get-indentation)))
905 ((and (goto-char pos)
906 (org-at-item-p))
907 (goto-char (match-end 0))
908 (skip-chars-forward " \t")
909 (setq ind-down (current-column)))))
910 (list ind ind-up ind-down)))
913 ;;; Send and receive lists
915 (defun org-list-parse-list (&optional delete)
916 "Parse the list at point and maybe DELETE it.
917 Return a list containing first level items as strings and
918 sublevels as a list of strings."
919 (let* ((item-beginning (org-list-item-beginning))
920 (start (car item-beginning))
921 (end (org-list-end (cdr item-beginning)))
922 output itemsep ltype)
923 (while (re-search-forward org-list-beginning-re end t)
924 (goto-char (match-beginning 3))
925 (save-match-data
926 (cond ((string-match "[0-9]" (match-string 2))
927 (setq itemsep "[0-9]+\\(?:\\.\\|)\\)"
928 ltype 'ordered))
929 ((string-match "^.*::" (match-string 0))
930 (setq itemsep "[-+]" ltype 'descriptive))
931 (t (setq itemsep "[-+]" ltype 'unordered))))
932 (let* ((indent1 (match-string 1))
933 (nextitem (save-excursion
934 (save-match-data
935 (or (and (re-search-forward
936 (concat "^" indent1 itemsep " *?") end t)
937 (match-beginning 0)) end))))
938 (item (buffer-substring
939 (point)
940 (or (and (re-search-forward
941 org-list-beginning-re end t)
942 (goto-char (match-beginning 0)))
943 (goto-char end))))
944 (nextindent (match-string 1))
945 (item (org-trim item))
946 (item (if (string-match "^\\[\\([xX ]\\)\\]" item)
947 (replace-match (if (equal (match-string 1 item) " ")
948 "[CBOFF]"
949 "[CBON]")
950 t nil item)
951 item)))
952 (push item output)
953 (when (> (length nextindent)
954 (length indent1))
955 (narrow-to-region (point) nextitem)
956 (push (org-list-parse-list) output)
957 (widen))))
958 (when delete (delete-region start end))
959 (setq output (nreverse output))
960 (push ltype output)))
962 (defun org-list-item-beginning ()
963 "Find the beginning of the list item.
964 Return a cons which car is the beginning position of the item and
965 cdr is the indentation string."
966 (save-excursion
967 (if (not (or (looking-at org-list-beginning-re)
968 (re-search-backward
969 org-list-beginning-re nil t)))
970 (progn (goto-char (point-min)) (point))
971 (cons (match-beginning 0) (match-string 1)))))
973 (defun org-list-end (indent)
974 "Return the position of the end of the list.
975 INDENT is the indentation of the list."
976 (save-excursion
977 (catch 'exit
978 (while (or (looking-at org-list-beginning-re)
979 (looking-at (concat "^" indent "[ \t]+\\|^$")))
980 (if (eq (point) (point-max))
981 (throw 'exit (point-max)))
982 (forward-line 1))) (point)))
984 (defun org-list-insert-radio-list ()
985 "Insert a radio list template appropriate for this major mode."
986 (interactive)
987 (let* ((e (assq major-mode org-list-radio-list-templates))
988 (txt (nth 1 e))
989 name pos)
990 (unless e (error "No radio list setup defined for %s" major-mode))
991 (setq name (read-string "List name: "))
992 (while (string-match "%n" txt)
993 (setq txt (replace-match name t t txt)))
994 (or (bolp) (insert "\n"))
995 (setq pos (point))
996 (insert txt)
997 (goto-char pos)))
999 (defun org-list-send-list (&optional maybe)
1000 "Send a tranformed version of this list to the receiver position.
1001 With argument MAYBE, fail quietly if no transformation is defined for
1002 this list."
1003 (interactive)
1004 (catch 'exit
1005 (unless (org-at-item-p) (error "Not at a list"))
1006 (save-excursion
1007 (goto-char (car (org-list-item-beginning)))
1008 (beginning-of-line 0)
1009 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1010 (if maybe
1011 (throw 'exit nil)
1012 (error "Don't know how to transform this list"))))
1013 (let* ((name (match-string 1))
1014 (item-beginning (org-list-item-beginning))
1015 (transform (intern (match-string 2)))
1016 (txt (buffer-substring-no-properties
1017 (car item-beginning)
1018 (org-list-end (cdr item-beginning))))
1019 (list (org-list-parse-list))
1020 beg)
1021 (unless (fboundp transform)
1022 (error "No such transformation function %s" transform))
1023 (setq txt (funcall transform list))
1024 ;; Find the insertion place
1025 (save-excursion
1026 (goto-char (point-min))
1027 (unless (re-search-forward
1028 (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
1029 (error "Don't know where to insert translated list"))
1030 (goto-char (match-beginning 0))
1031 (beginning-of-line 2)
1032 (setq beg (point))
1033 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
1034 (error "Cannot find end of insertion region"))
1035 (beginning-of-line 1)
1036 (delete-region beg (point))
1037 (goto-char beg)
1038 (insert txt "\n"))
1039 (message "List converted and installed at receiver location"))))
1041 (defun org-list-to-generic (list params)
1042 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1044 Valid parameters PARAMS are
1046 :ustart String to start an unordered list
1047 :uend String to end an unordered list
1049 :ostart String to start an ordered list
1050 :oend String to end an ordered list
1052 :dstart String to start a descriptive list
1053 :dend String to end a descriptive list
1054 :dtstart String to start a descriptive term
1055 :dtend String to end a descriptive term
1056 :ddstart String to start a description
1057 :ddend String to end a description
1059 :splice When set to t, return only list body lines, don't wrap
1060 them into :[u/o]start and :[u/o]end. Default is nil.
1062 :istart String to start a list item
1063 :iend String to end a list item
1064 :isep String to separate items
1065 :lsep String to separate sublists
1067 :cboff String to insert for an unchecked checkbox
1068 :cbon String to insert for a checked checkbox"
1069 (interactive)
1070 (let* ((p params) sublist
1071 (splicep (plist-get p :splice))
1072 (ostart (plist-get p :ostart))
1073 (oend (plist-get p :oend))
1074 (ustart (plist-get p :ustart))
1075 (uend (plist-get p :uend))
1076 (dstart (plist-get p :dstart))
1077 (dend (plist-get p :dend))
1078 (dtstart (plist-get p :dtstart))
1079 (dtend (plist-get p :dtend))
1080 (ddstart (plist-get p :ddstart))
1081 (ddend (plist-get p :ddend))
1082 (istart (plist-get p :istart))
1083 (iend (plist-get p :iend))
1084 (isep (plist-get p :isep))
1085 (lsep (plist-get p :lsep))
1086 (cbon (plist-get p :cbon))
1087 (cboff (plist-get p :cboff)))
1088 (let ((wrapper
1089 (cond ((eq (car list) 'ordered)
1090 (concat ostart "\n%s" oend "\n"))
1091 ((eq (car list) 'unordered)
1092 (concat ustart "\n%s" uend "\n"))
1093 ((eq (car list) 'descriptive)
1094 (concat dstart "\n%s" dend "\n"))))
1095 rtn term defstart defend)
1096 (while (setq sublist (pop list))
1097 (cond ((symbolp sublist) nil)
1098 ((stringp sublist)
1099 (when (string-match "^\\(.*\\) ::" sublist)
1100 (setq term (org-trim (format (concat dtstart "%s" dtend)
1101 (match-string 1 sublist))))
1102 (setq sublist (substring sublist (1+ (length term)))))
1103 (if (string-match "\\[CBON\\]" sublist)
1104 (setq sublist (replace-match cbon t t sublist)))
1105 (if (string-match "\\[CBOFF\\]" sublist)
1106 (setq sublist (replace-match cboff t t sublist)))
1107 (setq rtn (concat rtn istart term ddstart
1108 sublist ddend iend isep)))
1109 (t (setq rtn (concat rtn ;; previous list
1110 lsep ;; list separator
1111 (org-list-to-generic sublist p)
1112 lsep ;; list separator
1113 )))))
1114 (format wrapper rtn))))
1116 (defun org-list-to-latex (list &optional params)
1117 "Convert LIST into a LaTeX list.
1118 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1119 with overruling parameters for `org-list-to-generic'."
1120 (org-list-to-generic
1121 list
1122 (org-combine-plists
1123 '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
1124 :ustart "\\begin{itemize}" :uend "\\end{itemize}"
1125 :dstart "\\begin{description}" :dend "\\end{description}"
1126 :dtstart "[" :dtend "]"
1127 :ddstart "" :ddend ""
1128 :istart "\\item " :iend ""
1129 :isep "\n" :lsep "\n"
1130 :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
1131 params)))
1133 (defun org-list-to-html (list &optional params)
1134 "Convert LIST into a HTML list.
1135 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1136 with overruling parameters for `org-list-to-generic'."
1137 (org-list-to-generic
1138 list
1139 (org-combine-plists
1140 '(:splicep nil :ostart "<ol>" :oend "</ol>"
1141 :ustart "<ul>" :uend "</ul>"
1142 :dstart "<dl>" :dend "</dl>"
1143 :dtstart "<dt>" :dtend "</dt>"
1144 :ddstart "<dd>" :ddend "</dd>"
1145 :istart "<li>" :iend "</li>"
1146 :isep "\n" :lsep "\n"
1147 :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
1148 params)))
1150 (defun org-list-to-texinfo (list &optional params)
1151 "Convert LIST into a Texinfo list.
1152 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1153 with overruling parameters for `org-list-to-generic'."
1154 (org-list-to-generic
1155 list
1156 (org-combine-plists
1157 '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
1158 :ustart "@enumerate" :uend "@end enumerate"
1159 :dstart "@table" :dend "@end table"
1160 :dtstart "@item " :dtend "\n"
1161 :ddstart "" :ddend ""
1162 :istart "@item\n" :iend ""
1163 :isep "\n" :lsep "\n"
1164 :cbon "@code{[X]}" :cboff "@code{[ ]}")
1165 params)))
1167 (provide 'org-list)
1169 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1170 ;;; org-list.el ends here