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