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