Release 6.21a
[org-mode/org-tableheadings.git] / lisp / org-list.el
blob83385b43b7e8fe551af0a0bbcbebfa6b5032ff62
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.21a
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 When there is an active region, toggle status or presence of the checkbox
255 in the first line, and make every item in the region have the same
256 status or presence, respectively.
257 If the cursor is in a headline, apply this to all checkbox items in the
258 text below the heading."
259 (interactive "P")
260 (catch 'exit
261 (let (beg end status first-present first-status)
262 (cond
263 ((org-region-active-p)
264 (setq beg (region-beginning) end (region-end)))
265 ((org-on-heading-p)
266 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
267 ((org-at-item-checkbox-p)
268 (let ((pos (point)))
269 (if toggle-presence
270 (progn
271 (replace-match "")
272 (goto-char (match-beginning 0))
273 (just-one-space))
274 (replace-match
275 (cond ((member (match-string 0) '("[ ]" "[-]")) "[X]")
276 (t "[ ]"))
277 t t))
278 (goto-char pos))
279 (throw 'exit t))
280 ((org-at-item-p)
281 ;; add a checkbox
282 (save-excursion
283 (goto-char (match-end 0))
284 (insert "[ ] "))
285 (throw 'exit t))
286 (t (error "Not at a checkbox or heading, and no active region")))
287 (setq end (move-marker (make-marker) end))
288 (save-excursion
289 (goto-char beg)
290 (setq first-present (org-at-item-checkbox-p)
291 first-status
292 (save-excursion
293 (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t)
294 (equal (match-string 1) "[X]"))))
295 (while (< (point) end)
296 (if toggle-presence
297 (cond
298 ((and first-present (org-at-item-checkbox-p))
299 (save-excursion
300 (replace-match "")
301 (goto-char (match-beginning 0))
302 (just-one-space)))
303 ((and (not first-present) (not (org-at-item-checkbox-p))
304 (org-at-item-p))
305 (save-excursion
306 (goto-char (match-end 0))
307 (insert "[ ] "))))
308 (when (org-at-item-checkbox-p)
309 (setq status (equal (match-string 0) "[X]"))
310 (replace-match
311 (if first-status "[ ]" "[X]") t t)))
312 (beginning-of-line 2)))))
313 (org-update-checkbox-count-maybe))
315 (defun org-update-checkbox-count-maybe ()
316 "Update checkbox statistics unless turned off by user."
317 (when org-provide-checkbox-statistics
318 (org-update-checkbox-count)))
320 (defun org-update-checkbox-count (&optional all)
321 "Update the checkbox statistics in the current section.
322 This will find all statistic cookies like [57%] and [6/12] and update them
323 with the current numbers. With optional prefix argument ALL, do this for
324 the whole buffer."
325 (interactive "P")
326 (save-excursion
327 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
328 (beg (condition-case nil
329 (progn (org-back-to-heading) (point))
330 (error (point-min))))
331 (end (move-marker (make-marker)
332 (progn (outline-next-heading) (point))))
333 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
334 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
335 (re-find (concat re "\\|" re-box))
336 beg-cookie end-cookie is-percent c-on c-off lim
337 eline curr-ind next-ind continue-from startsearch
338 (cstat 0)
340 (when all
341 (goto-char (point-min))
342 (outline-next-heading)
343 (setq beg (point) end (point-max)))
344 (goto-char end)
345 ;; find each statistic cookie
346 (while (re-search-backward re-find beg t)
347 (setq beg-cookie (match-beginning 1)
348 end-cookie (match-end 1)
349 cstat (+ cstat (if end-cookie 1 0))
350 startsearch (point-at-eol)
351 continue-from (match-beginning 0)
352 is-percent (match-beginning 2)
353 lim (cond
354 ((org-on-heading-p) (outline-next-heading) (point))
355 ((org-at-item-p) (org-end-of-item) (point))
356 (t nil))
357 c-on 0
358 c-off 0)
359 (when lim
360 ;; find first checkbox for this cookie and gather
361 ;; statistics from all that are at this indentation level
362 (goto-char startsearch)
363 (if (re-search-forward re-box lim t)
364 (progn
365 (org-beginning-of-item)
366 (setq curr-ind (org-get-indentation))
367 (setq next-ind curr-ind)
368 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
369 (save-excursion (end-of-line) (setq eline (point)))
370 (if (re-search-forward re-box eline t)
371 (if (member (match-string 2) '("[ ]" "[-]"))
372 (setq c-off (1+ c-off))
373 (setq c-on (1+ c-on))
376 (org-end-of-item)
377 (setq next-ind (org-get-indentation))
379 (goto-char continue-from)
380 ;; update cookie
381 (when end-cookie
382 (delete-region beg-cookie end-cookie)
383 (goto-char beg-cookie)
384 (insert
385 (if is-percent
386 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
387 (format "[%d/%d]" c-on (+ c-on c-off)))))
388 ;; update items checkbox if it has one
389 (when (org-at-item-p)
390 (org-beginning-of-item)
391 (when (and (> (+ c-on c-off) 0)
392 (re-search-forward re-box (point-at-eol) t))
393 (setq beg-cookie (match-beginning 2)
394 end-cookie (match-end 2))
395 (delete-region beg-cookie end-cookie)
396 (goto-char beg-cookie)
397 (cond ((= c-off 0) (insert "[X]"))
398 ((= c-on 0) (insert "[ ]"))
399 (t (insert "[-]")))
401 (goto-char continue-from))
402 (when (interactive-p)
403 (message "Checkbox statistics updated %s (%d places)"
404 (if all "in entire file" "in current outline entry") cstat)))))
406 (defun org-get-checkbox-statistics-face ()
407 "Select the face for checkbox statistics.
408 The face will be `org-done' when all relevant boxes are checked. Otherwise
409 it will be `org-todo'."
410 (if (match-end 1)
411 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
412 (if (and (> (match-end 2) (match-beginning 2))
413 (equal (match-string 2) (match-string 3)))
414 'org-done
415 'org-todo)))
417 (defun org-beginning-of-item ()
418 "Go to the beginning of the current hand-formatted item.
419 If the cursor is not in an item, throw an error."
420 (interactive)
421 (let ((pos (point))
422 (limit (save-excursion
423 (condition-case nil
424 (progn
425 (org-back-to-heading)
426 (beginning-of-line 2) (point))
427 (error (point-min)))))
428 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
429 ind ind1)
430 (if (org-at-item-p)
431 (beginning-of-line 1)
432 (beginning-of-line 1)
433 (skip-chars-forward " \t")
434 (setq ind (current-column))
435 (if (catch 'exit
436 (while t
437 (beginning-of-line 0)
438 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
440 (if (looking-at "[ \t]*$")
441 (setq ind1 ind-empty)
442 (skip-chars-forward " \t")
443 (setq ind1 (current-column)))
444 (if (< ind1 ind)
445 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
447 (goto-char pos)
448 (error "Not in an item")))))
450 (defun org-end-of-item ()
451 "Go to the end of the current hand-formatted item.
452 If the cursor is not in an item, throw an error."
453 (interactive)
454 (let* ((pos (point))
455 ind1
456 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
457 (limit (save-excursion (outline-next-heading) (point)))
458 (ind (save-excursion
459 (org-beginning-of-item)
460 (skip-chars-forward " \t")
461 (current-column)))
462 (end (catch 'exit
463 (while t
464 (beginning-of-line 2)
465 (if (eobp) (throw 'exit (point)))
466 (if (>= (point) limit) (throw 'exit (point-at-bol)))
467 (if (looking-at "[ \t]*$")
468 (setq ind1 ind-empty)
469 (skip-chars-forward " \t")
470 (setq ind1 (current-column)))
471 (if (<= ind1 ind)
472 (throw 'exit (point-at-bol)))))))
473 (if end
474 (goto-char end)
475 (goto-char pos)
476 (error "Not in an item"))))
478 (defun org-next-item ()
479 "Move to the beginning of the next item in the current plain list.
480 Error if not at a plain list, or if this is the last item in the list."
481 (interactive)
482 (let (ind ind1 (pos (point)))
483 (org-beginning-of-item)
484 (setq ind (org-get-indentation))
485 (org-end-of-item)
486 (setq ind1 (org-get-indentation))
487 (unless (and (org-at-item-p) (= ind ind1))
488 (goto-char pos)
489 (error "On last item"))))
491 (defun org-previous-item ()
492 "Move to the beginning of the previous item in the current plain list.
493 Error if not at a plain list, or if this is the first item in the list."
494 (interactive)
495 (let (beg ind ind1 (pos (point)))
496 (org-beginning-of-item)
497 (setq beg (point))
498 (setq ind (org-get-indentation))
499 (goto-char beg)
500 (catch 'exit
501 (while t
502 (beginning-of-line 0)
503 (if (looking-at "[ \t]*$")
505 (if (<= (setq ind1 (org-get-indentation)) ind)
506 (throw 'exit t)))))
507 (condition-case nil
508 (if (or (not (org-at-item-p))
509 (< ind1 (1- ind)))
510 (error "")
511 (org-beginning-of-item))
512 (error (goto-char pos)
513 (error "On first item")))))
515 (defun org-first-list-item-p ()
516 "Is this heading the item in a plain list?"
517 (unless (org-at-item-p)
518 (error "Not at a plain list item"))
519 (org-beginning-of-item)
520 (= (point) (save-excursion (org-beginning-of-item-list))))
522 (defun org-move-item-down ()
523 "Move the plain list item at point down, i.e. swap with following item.
524 Subitems (items with larger indentation) are considered part of the item,
525 so this really moves item trees."
526 (interactive)
527 (let ((col (current-column))
528 (pos (point))
529 beg beg0 end end0 ind ind1 txt ne-end ne-beg)
530 (org-beginning-of-item)
531 (setq beg0 (point))
532 (save-excursion
533 (setq ne-beg (org-back-over-empty-lines))
534 (setq beg (point)))
535 (goto-char beg0)
536 (setq ind (org-get-indentation))
537 (org-end-of-item)
538 (setq end0 (point))
539 (setq ind1 (org-get-indentation))
540 (setq ne-end (org-back-over-empty-lines))
541 (setq end (point))
542 (goto-char beg0)
543 (when (and (org-first-list-item-p) (< ne-end ne-beg))
544 ;; include less whitespace
545 (save-excursion
546 (goto-char beg)
547 (forward-line (- ne-beg ne-end))
548 (setq beg (point))))
549 (goto-char end0)
550 (if (and (org-at-item-p) (= ind ind1))
551 (progn
552 (org-end-of-item)
553 (org-back-over-empty-lines)
554 (setq txt (buffer-substring beg end))
555 (save-excursion
556 (delete-region beg end))
557 (setq pos (point))
558 (insert txt)
559 (goto-char pos) (org-skip-whitespace)
560 (org-maybe-renumber-ordered-list)
561 (move-to-column col))
562 (goto-char pos)
563 (move-to-column col)
564 (error "Cannot move this item further down"))))
566 (defun org-move-item-up (arg)
567 "Move the plain list item at point up, i.e. swap with previous item.
568 Subitems (items with larger indentation) are considered part of the item,
569 so this really moves item trees."
570 (interactive "p")
571 (let ((col (current-column)) (pos (point))
572 beg beg0 end ind ind1 txt
573 ne-beg ne-ins ins-end)
574 (org-beginning-of-item)
575 (setq beg0 (point))
576 (setq ind (org-get-indentation))
577 (save-excursion
578 (setq ne-beg (org-back-over-empty-lines))
579 (setq beg (point)))
580 (goto-char beg0)
581 (org-end-of-item)
582 (org-back-over-empty-lines)
583 (setq end (point))
584 (goto-char beg0)
585 (catch 'exit
586 (while t
587 (beginning-of-line 0)
588 (if (looking-at "[ \t]*$")
589 (if org-empty-line-terminates-plain-lists
590 (progn
591 (goto-char pos)
592 (error "Cannot move this item further up"))
593 nil)
594 (if (<= (setq ind1 (org-get-indentation)) ind)
595 (throw 'exit t)))))
596 (condition-case nil
597 (org-beginning-of-item)
598 (error (goto-char beg0)
599 (move-to-column col)
600 (error "Cannot move this item further up")))
601 (setq ind1 (org-get-indentation))
602 (if (and (org-at-item-p) (= ind ind1))
603 (progn
604 (setq ne-ins (org-back-over-empty-lines))
605 (setq txt (buffer-substring beg end))
606 (save-excursion
607 (delete-region beg end))
608 (setq pos (point))
609 (insert txt)
610 (setq ins-end (point))
611 (goto-char pos) (org-skip-whitespace)
613 (when (and (org-first-list-item-p) (> ne-ins ne-beg))
614 ;; Move whitespace back to beginning
615 (save-excursion
616 (goto-char ins-end)
617 (let ((kill-whole-line t))
618 (kill-line (- ne-ins ne-beg)) (point)))
619 (insert (make-string (- ne-ins ne-beg) ?\n)))
621 (org-maybe-renumber-ordered-list)
622 (move-to-column col))
623 (goto-char pos)
624 (move-to-column col)
625 (error "Cannot move this item further up"))))
627 (defun org-maybe-renumber-ordered-list ()
628 "Renumber the ordered list at point if setup allows it.
629 This tests the user option `org-auto-renumber-ordered-lists' before
630 doing the renumbering."
631 (interactive)
632 (when (and org-auto-renumber-ordered-lists
633 (org-at-item-p))
634 (if (match-beginning 3)
635 (org-renumber-ordered-list 1)
636 (org-fix-bullet-type))))
638 (defun org-maybe-renumber-ordered-list-safe ()
639 (condition-case nil
640 (save-excursion
641 (org-maybe-renumber-ordered-list))
642 (error nil)))
644 (defun org-cycle-list-bullet (&optional which)
645 "Cycle through the different itemize/enumerate bullets.
646 This cycle the entire list level through the sequence:
648 `-' -> `+' -> `*' -> `1.' -> `1)'
650 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
651 0 means `-', 1 means `+' etc."
652 (interactive "P")
653 (org-preserve-lc
654 (org-beginning-of-item-list)
655 (org-at-item-p)
656 (beginning-of-line 1)
657 (let ((current (match-string 0))
658 (prevp (eq which 'previous))
659 new old)
660 (setq new (cond
661 ((and (numberp which)
662 (nth (1- which) '("-" "+" "*" "1." "1)"))))
663 ((string-match "-" current) (if prevp "1)" "+"))
664 ((string-match "\\+" current)
665 (if prevp "-" (if (looking-at "\\S-") "1." "*")))
666 ((string-match "\\*" current) (if prevp "+" "1."))
667 ((string-match "\\." current)
668 (if prevp (if (looking-at "\\S-") "+" "*") "1)"))
669 ((string-match ")" current) (if prevp "1." "-"))
670 (t (error "This should not happen"))))
671 (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
672 (setq old (match-string 2))
673 (replace-match (concat "\\1" new)))
674 (org-shift-item-indentation (- (length new) (length old)))
675 (org-fix-bullet-type)
676 (org-maybe-renumber-ordered-list))))
678 (defun org-get-string-indentation (s)
679 "What indentation has S due to SPACE and TAB at the beginning of the string?"
680 (let ((n -1) (i 0) (w tab-width) c)
681 (catch 'exit
682 (while (< (setq n (1+ n)) (length s))
683 (setq c (aref s n))
684 (cond ((= c ?\ ) (setq i (1+ i)))
685 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
686 (t (throw 'exit t)))))
689 (defun org-renumber-ordered-list (arg)
690 "Renumber an ordered plain list.
691 Cursor needs to be in the first line of an item, the line that starts
692 with something like \"1.\" or \"2)\"."
693 (interactive "p")
694 (unless (and (org-at-item-p)
695 (match-beginning 3))
696 (error "This is not an ordered list"))
697 (let ((line (org-current-line))
698 (col (current-column))
699 (ind (org-get-string-indentation
700 (buffer-substring (point-at-bol) (match-beginning 3))))
701 ;; (term (substring (match-string 3) -1))
702 ind1 (n (1- arg))
703 fmt bobp old new)
704 ;; find where this list begins
705 (org-beginning-of-item-list)
706 (setq bobp (bobp))
707 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
708 (setq fmt (concat "%d" (match-string 1)))
709 (beginning-of-line 0)
710 ;; walk forward and replace these numbers
711 (catch 'exit
712 (while t
713 (catch 'next
714 (if bobp (setq bobp nil) (beginning-of-line 2))
715 (if (eobp) (throw 'exit nil))
716 (if (looking-at "[ \t]*$") (throw 'next nil))
717 (skip-chars-forward " \t") (setq ind1 (current-column))
718 (if (> ind1 ind) (throw 'next t))
719 (if (< ind1 ind) (throw 'exit t))
720 (if (not (org-at-item-p)) (throw 'exit nil))
721 (setq old (match-string 2))
722 (delete-region (match-beginning 2) (match-end 2))
723 (goto-char (match-beginning 2))
724 (insert (setq new (format fmt (setq n (1+ n)))))
725 (org-shift-item-indentation (- (length new) (length old))))))
726 (goto-line line)
727 (org-move-to-column col)))
729 (defun org-fix-bullet-type ()
730 "Make sure all items in this list have the same bullet as the first item.
731 Also, fix the indentation."
732 (interactive)
733 (unless (org-at-item-p) (error "This is not a list"))
734 (let ((line (org-current-line))
735 (col (current-column))
736 (ind (current-indentation))
737 ind1 bullet oldbullet)
738 ;; find where this list begins
739 (org-beginning-of-item-list)
740 (beginning-of-line 1)
741 ;; find out what the bullet type is
742 (looking-at "[ \t]*\\(\\S-+\\)")
743 (setq bullet (concat (match-string 1) " "))
744 (if (and org-list-two-spaces-after-bullet-regexp
745 (string-match org-list-two-spaces-after-bullet-regexp bullet))
746 (setq bullet (concat bullet " ")))
747 ;; walk forward and replace these numbers
748 (beginning-of-line 0)
749 (catch 'exit
750 (while t
751 (catch 'next
752 (beginning-of-line 2)
753 (if (eobp) (throw 'exit nil))
754 (if (looking-at "[ \t]*$") (throw 'next nil))
755 (skip-chars-forward " \t") (setq ind1 (current-column))
756 (if (> ind1 ind) (throw 'next t))
757 (if (< ind1 ind) (throw 'exit t))
758 (if (not (org-at-item-p)) (throw 'exit nil))
759 (skip-chars-forward " \t")
760 (looking-at "\\S-+ *")
761 (setq oldbullet (match-string 0))
762 (replace-match bullet)
763 (org-shift-item-indentation (- (length bullet) (length oldbullet))))))
764 (goto-line line)
765 (org-move-to-column col)
766 (if (string-match "[0-9]" bullet)
767 (org-renumber-ordered-list 1))))
769 (defun org-shift-item-indentation (delta)
770 "Shift the indentation in current item by DELTA."
771 (save-excursion
772 (let ((beg (point-at-bol))
773 (end (progn (org-end-of-item) (point)))
775 (goto-char end)
776 (beginning-of-line 0)
777 (while (> (point) beg)
778 (when (looking-at "[ \t]*\\S-")
779 ;; this is not an empty line
780 (setq i (org-get-indentation))
781 (if (and (> i 0) (> (setq i (+ i delta)) 0))
782 (indent-line-to i)))
783 (beginning-of-line 0)))))
785 (defun org-beginning-of-item-list ()
786 "Go to the beginning of the current item list.
787 I.e. to the first item in this list."
788 (interactive)
789 (org-beginning-of-item)
790 (let ((pos (point-at-bol))
791 (ind (org-get-indentation))
792 ind1)
793 ;; find where this list begins
794 (catch 'exit
795 (while t
796 (catch 'next
797 (beginning-of-line 0)
798 (if (looking-at "[ \t]*$")
799 (throw (if (bobp) 'exit 'next) t))
800 (skip-chars-forward " \t") (setq ind1 (current-column))
801 (if (or (< ind1 ind)
802 (and (= ind1 ind)
803 (not (org-at-item-p)))
804 (and (= (point-at-bol) (point-min))
805 (setq pos (point-min))))
806 (throw 'exit t)
807 (when (org-at-item-p) (setq pos (point-at-bol)))))))
808 (goto-char pos)))
811 (defun org-end-of-item-list ()
812 "Go to the end of the current item list.
813 I.e. to the text after the last item."
814 (interactive)
815 (org-beginning-of-item)
816 (let ((pos (point-at-bol))
817 (ind (org-get-indentation))
818 ind1)
819 ;; find where this list begins
820 (catch 'exit
821 (while t
822 (catch 'next
823 (beginning-of-line 2)
824 (if (looking-at "[ \t]*$")
825 (throw (if (eobp) 'exit 'next) t))
826 (skip-chars-forward " \t") (setq ind1 (current-column))
827 (if (or (< ind1 ind)
828 (and (= ind1 ind)
829 (not (org-at-item-p)))
830 (eobp))
831 (progn
832 (setq pos (point-at-bol))
833 (throw 'exit t))))))
834 (goto-char pos)))
837 (defvar org-last-indent-begin-marker (make-marker))
838 (defvar org-last-indent-end-marker (make-marker))
840 (defun org-outdent-item (arg)
841 "Outdent a local list item."
842 (interactive "p")
843 (org-indent-item (- arg)))
845 (defun org-indent-item (arg)
846 "Indent a local list item."
847 (interactive "p")
848 (unless (org-at-item-p)
849 (error "Not on an item"))
850 (save-excursion
851 (let (beg end ind ind1 tmp delta ind-down ind-up)
852 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
853 (setq beg org-last-indent-begin-marker
854 end org-last-indent-end-marker)
855 (org-beginning-of-item)
856 (setq beg (move-marker org-last-indent-begin-marker (point)))
857 (org-end-of-item)
858 (setq end (move-marker org-last-indent-end-marker (point))))
859 (goto-char beg)
860 (setq tmp (org-item-indent-positions)
861 ind (car tmp)
862 ind-down (nth 2 tmp)
863 ind-up (nth 1 tmp)
864 delta (if (> arg 0)
865 (if ind-down (- ind-down ind) 2)
866 (if ind-up (- ind-up ind) -2)))
867 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
868 (while (< (point) end)
869 (beginning-of-line 1)
870 (skip-chars-forward " \t") (setq ind1 (current-column))
871 (delete-region (point-at-bol) (point))
872 (or (eolp) (org-indent-to-column (+ ind1 delta)))
873 (beginning-of-line 2))))
874 (org-fix-bullet-type)
875 (org-maybe-renumber-ordered-list-safe)
876 (save-excursion
877 (beginning-of-line 0)
878 (condition-case nil (org-beginning-of-item) (error nil))
879 (org-maybe-renumber-ordered-list-safe)))
881 (defun org-item-indent-positions ()
882 "Return indentation for plain list items.
883 This returns a list with three values: The current indentation, the
884 parent indentation and the indentation a child should have.
885 Assumes cursor in item line."
886 (let* ((bolpos (point-at-bol))
887 (ind (org-get-indentation))
888 ind-down ind-up pos)
889 (save-excursion
890 (org-beginning-of-item-list)
891 (skip-chars-backward "\n\r \t")
892 (when (org-in-item-p)
893 (org-beginning-of-item)
894 (setq ind-up (org-get-indentation))))
895 (setq pos (point))
896 (save-excursion
897 (cond
898 ((and (condition-case nil (progn (org-previous-item) t)
899 (error nil))
900 (or (forward-char 1) t)
901 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
902 (setq ind-down (org-get-indentation)))
903 ((and (goto-char pos)
904 (org-at-item-p))
905 (goto-char (match-end 0))
906 (skip-chars-forward " \t")
907 (setq ind-down (current-column)))))
908 (list ind ind-up ind-down)))
911 ;;; Send and receive lists
913 (defun org-list-parse-list (&optional delete)
914 "Parse the list at point and maybe DELETE it.
915 Return a list containing first level items as strings and
916 sublevels as a list of strings."
917 (let* ((item-beginning (org-list-item-beginning))
918 (start (car item-beginning))
919 (end (org-list-end (cdr item-beginning)))
920 output itemsep ltype)
921 (while (re-search-forward org-list-beginning-re end t)
922 (goto-char (match-beginning 3))
923 (save-match-data
924 (cond ((string-match "[0-9]" (match-string 2))
925 (setq itemsep "[0-9]+\\(?:\\.\\|)\\)"
926 ltype 'ordered))
927 ((string-match "^.*::" (match-string 0))
928 (setq itemsep "[-+]" ltype 'descriptive))
929 (t (setq itemsep "[-+]" ltype 'unordered))))
930 (let* ((indent1 (match-string 1))
931 (nextitem (save-excursion
932 (save-match-data
933 (or (and (re-search-forward
934 (concat "^" indent1 itemsep " *?") end t)
935 (match-beginning 0)) end))))
936 (item (buffer-substring
937 (point)
938 (or (and (re-search-forward
939 org-list-beginning-re end t)
940 (goto-char (match-beginning 0)))
941 (goto-char end))))
942 (nextindent (match-string 1))
943 (item (org-trim item))
944 (item (if (string-match "^\\[\\([xX ]\\)\\]" item)
945 (replace-match (if (equal (match-string 1 item) " ")
946 "[CBOFF]"
947 "[CBON]")
948 t nil item)
949 item)))
950 (push item output)
951 (when (> (length nextindent)
952 (length indent1))
953 (narrow-to-region (point) nextitem)
954 (push (org-list-parse-list) output)
955 (widen))))
956 (when delete (delete-region start end))
957 (setq output (nreverse output))
958 (push ltype output)))
960 (defun org-list-item-beginning ()
961 "Find the beginning of the list item.
962 Return a cons which car is the beginning position of the item and
963 cdr is the indentation string."
964 (save-excursion
965 (if (not (or (looking-at org-list-beginning-re)
966 (re-search-backward
967 org-list-beginning-re nil t)))
968 (progn (goto-char (point-min)) (point))
969 (cons (match-beginning 0) (match-string 1)))))
971 (defun org-list-end (indent)
972 "Return the position of the end of the list.
973 INDENT is the indentation of the list."
974 (save-excursion
975 (catch 'exit
976 (while (or (looking-at org-list-beginning-re)
977 (looking-at (concat "^" indent "[ \t]+\\|^$")))
978 (if (eq (point) (point-max))
979 (throw 'exit (point-max)))
980 (forward-line 1))) (point)))
982 (defun org-list-insert-radio-list ()
983 "Insert a radio list template appropriate for this major mode."
984 (interactive)
985 (let* ((e (assq major-mode org-list-radio-list-templates))
986 (txt (nth 1 e))
987 name pos)
988 (unless e (error "No radio list setup defined for %s" major-mode))
989 (setq name (read-string "List name: "))
990 (while (string-match "%n" txt)
991 (setq txt (replace-match name t t txt)))
992 (or (bolp) (insert "\n"))
993 (setq pos (point))
994 (insert txt)
995 (goto-char pos)))
997 (defun org-list-send-list (&optional maybe)
998 "Send a tranformed version of this list to the receiver position.
999 With argument MAYBE, fail quietly if no transformation is defined for
1000 this list."
1001 (interactive)
1002 (catch 'exit
1003 (unless (org-at-item-p) (error "Not at a list"))
1004 (save-excursion
1005 (goto-char (car (org-list-item-beginning)))
1006 (beginning-of-line 0)
1007 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1008 (if maybe
1009 (throw 'exit nil)
1010 (error "Don't know how to transform this list"))))
1011 (let* ((name (match-string 1))
1012 (item-beginning (org-list-item-beginning))
1013 (transform (intern (match-string 2)))
1014 (txt (buffer-substring-no-properties
1015 (car item-beginning)
1016 (org-list-end (cdr item-beginning))))
1017 (list (org-list-parse-list))
1018 beg)
1019 (unless (fboundp transform)
1020 (error "No such transformation function %s" transform))
1021 (setq txt (funcall transform list))
1022 ;; Find the insertion place
1023 (save-excursion
1024 (goto-char (point-min))
1025 (unless (re-search-forward
1026 (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
1027 (error "Don't know where to insert translated list"))
1028 (goto-char (match-beginning 0))
1029 (beginning-of-line 2)
1030 (setq beg (point))
1031 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
1032 (error "Cannot find end of insertion region"))
1033 (beginning-of-line 1)
1034 (delete-region beg (point))
1035 (goto-char beg)
1036 (insert txt "\n"))
1037 (message "List converted and installed at receiver location"))))
1039 (defun org-list-to-generic (list params)
1040 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1042 Valid parameters PARAMS are
1044 :ustart String to start an unordered list
1045 :uend String to end an unordered list
1047 :ostart String to start an ordered list
1048 :oend String to end an ordered list
1050 :dstart String to start a descriptive list
1051 :dend String to end a descriptive list
1052 :dtstart String to start a descriptive term
1053 :dtend String to end a descriptive term
1054 :ddstart String to start a description
1055 :ddend String to end a description
1057 :splice When set to t, return only list body lines, don't wrap
1058 them into :[u/o]start and :[u/o]end. Default is nil.
1060 :istart String to start a list item
1061 :iend String to end a list item
1062 :isep String to separate items
1063 :lsep String to separate sublists
1065 :cboff String to insert for an unchecked checkbox
1066 :cbon String to insert for a checked checkbox"
1067 (interactive)
1068 (let* ((p params) sublist
1069 (splicep (plist-get p :splice))
1070 (ostart (plist-get p :ostart))
1071 (oend (plist-get p :oend))
1072 (ustart (plist-get p :ustart))
1073 (uend (plist-get p :uend))
1074 (dstart (plist-get p :dstart))
1075 (dend (plist-get p :dend))
1076 (dtstart (plist-get p :dtstart))
1077 (dtend (plist-get p :dtend))
1078 (ddstart (plist-get p :ddstart))
1079 (ddend (plist-get p :ddend))
1080 (istart (plist-get p :istart))
1081 (iend (plist-get p :iend))
1082 (isep (plist-get p :isep))
1083 (lsep (plist-get p :lsep))
1084 (cbon (plist-get p :cbon))
1085 (cboff (plist-get p :cboff)))
1086 (let ((wrapper
1087 (cond ((eq (car list) 'ordered)
1088 (concat ostart "\n%s" oend "\n"))
1089 ((eq (car list) 'unordered)
1090 (concat ustart "\n%s" uend "\n"))
1091 ((eq (car list) 'descriptive)
1092 (concat dstart "\n%s" dend "\n"))))
1093 rtn term defstart defend)
1094 (while (setq sublist (pop list))
1095 (cond ((symbolp sublist) nil)
1096 ((stringp sublist)
1097 (when (string-match "^\\(.*\\) ::" sublist)
1098 (setq term (org-trim (format (concat dtstart "%s" dtend)
1099 (match-string 1 sublist))))
1100 (setq sublist (substring sublist (1+ (length term)))))
1101 (if (string-match "\\[CBON\\]" sublist)
1102 (setq sublist (replace-match cbon t t sublist)))
1103 (if (string-match "\\[CBOFF\\]" sublist)
1104 (setq sublist (replace-match cboff t t sublist)))
1105 (setq rtn (concat rtn istart term ddstart
1106 sublist ddend iend isep)))
1107 (t (setq rtn (concat rtn ;; previous list
1108 lsep ;; list separator
1109 (org-list-to-generic sublist p)
1110 lsep ;; list separator
1111 )))))
1112 (format wrapper rtn))))
1114 (defun org-list-to-latex (list &optional params)
1115 "Convert LIST into a LaTeX list.
1116 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1117 with overruling parameters for `org-list-to-generic'."
1118 (org-list-to-generic
1119 list
1120 (org-combine-plists
1121 '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
1122 :ustart "\\begin{itemize}" :uend "\\end{itemize}"
1123 :dstart "\\begin{description}" :dend "\\end{description}"
1124 :dtstart "[" :dtend "]"
1125 :ddstart "" :ddend ""
1126 :istart "\\item " :iend ""
1127 :isep "\n" :lsep "\n"
1128 :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
1129 params)))
1131 (defun org-list-to-html (list &optional params)
1132 "Convert LIST into a HTML list.
1133 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1134 with overruling parameters for `org-list-to-generic'."
1135 (org-list-to-generic
1136 list
1137 (org-combine-plists
1138 '(:splicep nil :ostart "<ol>" :oend "</ol>"
1139 :ustart "<ul>" :uend "</ul>"
1140 :dstart "<dl>" :dend "</dl>"
1141 :dtstart "<dt>" :dtend "</dt>"
1142 :ddstart "<dd>" :ddend "</dd>"
1143 :istart "<li>" :iend "</li>"
1144 :isep "\n" :lsep "\n"
1145 :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
1146 params)))
1148 (defun org-list-to-texinfo (list &optional params)
1149 "Convert LIST into a Texinfo list.
1150 LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
1151 with overruling parameters for `org-list-to-generic'."
1152 (org-list-to-generic
1153 list
1154 (org-combine-plists
1155 '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
1156 :ustart "@enumerate" :uend "@end enumerate"
1157 :dstart "@table" :dend "@end table"
1158 :dtstart "@item " :dtend "\n"
1159 :ddstart "" :ddend ""
1160 :istart "@item\n" :iend ""
1161 :isep "\n" :lsep "\n"
1162 :cbon "@code{[X]}" :cboff "@code{[ ]}")
1163 params)))
1165 (provide 'org-list)
1167 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1168 ;;; org-list.el ends here