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