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