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