Return value of `org-cycle-item-indentation' was broken.
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-list.el
blob17f51910fa530c59f1a0334e406131cb716b964d
1 ;;; org-list.el --- Plain lists for Org-mode
2 ;;
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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: 7.01trans
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 (eval-when-compile
35 (require 'cl))
36 (require 'org-macs)
37 (require 'org-compat)
39 (defvar org-blank-before-new-entry)
40 (defvar org-M-RET-may-split-line)
41 (defvar org-complex-heading-regexp)
42 (defvar org-odd-levels-only)
44 (declare-function org-invisible-p "org" ())
45 (declare-function org-on-heading-p "org" (&optional invisible-ok))
46 (declare-function outline-next-heading "outline" ())
47 (declare-function org-back-to-heading "org" (&optional invisible-ok))
48 (declare-function org-back-over-empty-lines "org" ())
49 (declare-function org-skip-whitespace "org" ())
50 (declare-function org-trim "org" (s))
51 (declare-function org-get-indentation "org" (&optional line))
52 (declare-function org-timer-item "org-timer" (&optional arg))
53 (declare-function org-combine-plists "org" (&rest plists))
54 (declare-function org-entry-get "org"
55 (pom property &optional inherit literal-nil))
56 (declare-function org-narrow-to-subtree "org" ())
57 (declare-function org-show-subtree "org" ())
59 (defgroup org-plain-lists nil
60 "Options concerning plain lists in Org-mode."
61 :tag "Org Plain lists"
62 :group 'org-structure)
64 (defcustom org-cycle-include-plain-lists t
65 "When t, make TAB cycle visibility on plain list items.
67 Cycling plain lists works only when the cursor is on a plain list
68 item. When the cursor is on an outline heading, plain lists are
69 treated as text. This is the most stable way of handling this,
70 which is why it is the default.
72 When this is the symbol `integrate', then during cycling, plain
73 list items will *temporarily* be interpreted as outline headlines
74 with a level given by 1000+i where i is the indentation of the
75 bullet. This setting can lead to strange effects when switching
76 visibility to `children', because the first \"child\" in a
77 subtree decides what children should be listed. If that first
78 \"child\" is a plain list item with an implied large level
79 number, all true children and grand children of the outline
80 heading will be exposed in a children' view."
81 :group 'org-plain-lists
82 :type '(choice
83 (const :tag "Never" nil)
84 (const :tag "With cursor in plain list (recommended)" t)
85 (const :tag "As children of outline headings" integrate)))
87 (defcustom org-list-demote-modify-bullet nil
88 "Default bullet type installed when demoting an item.
89 This is an association list, for each bullet type, this alist will point
90 to the bullet that should be used when this item is demoted.
91 For example,
93 (setq org-list-demote-modify-bullet
94 '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
96 will make
98 + Movies
99 + Silence of the Lambs
100 + My Cousin Vinny
101 + Books
102 + The Hunt for Red October
103 + The Road to Omaha
105 into
107 + Movies
108 - Silence of the Lambs
109 - My Cousin Vinny
110 + Books
111 - The Hunt for Red October
112 - The Road to Omaha"
113 :group 'org-plain-lists
114 :type '(repeat
115 (cons
116 (choice :tag "If the current bullet is "
117 (const "-")
118 (const "+")
119 (const "*")
120 (const "1.")
121 (const "1)"))
122 (choice :tag "demotion will change it to"
123 (const "-")
124 (const "+")
125 (const "*")
126 (const "1.")
127 (const "1)")))))
129 (defcustom org-plain-list-ordered-item-terminator t
130 "The character that makes a line with leading number an ordered list item.
131 Valid values are ?. and ?\). To get both terminators, use t. While
132 ?. may look nicer, it creates the danger that a line with leading
133 number may be incorrectly interpreted as an item. ?\) therefore is
134 the safe choice."
135 :group 'org-plain-lists
136 :type '(choice (const :tag "dot like in \"2.\"" ?.)
137 (const :tag "paren like in \"2)\"" ?\))
138 (const :tab "both" t)))
140 (defcustom org-list-two-spaces-after-bullet-regexp nil
141 "A regular expression matching bullets that should have 2 spaces after them.
142 When nil, no bullet will have two spaces after them.
143 When a string, it will be used as a regular expression. When the
144 bullet type of a list is changed, the new bullet type will be
145 matched against this regexp. If it matches, there will be two
146 spaces instead of one after the bullet in each item of the list."
147 :group 'org-plain-lists
148 :type '(choice
149 (const :tag "never" nil)
150 (regexp)))
152 (defcustom org-empty-line-terminates-plain-lists nil
153 "Non-nil means an empty line ends all plain list levels.
154 Otherwise, look for `org-list-end-regexp'."
156 :group 'org-plain-lists
157 :type 'boolean)
159 (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
160 "Regexp matching the end of all plain list levels.
161 It must start with \"^\" and end with \"\\n\". It defaults to 2
162 blank lines. `org-empty-line-terminates-plain-lists' has
163 precedence over it."
164 :group 'org-plain-lists
165 :type 'string)
167 (defcustom org-auto-renumber-ordered-lists t
168 "Non-nil means automatically renumber ordered plain lists.
169 Renumbering happens when the sequence have been changed with
170 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing
171 commands, use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
172 :group 'org-plain-lists
173 :type 'boolean)
175 (defcustom org-provide-checkbox-statistics t
176 "Non-nil means update checkbox statistics after insert and toggle.
177 When this is set, checkbox statistics is updated each time you
178 either insert a new checkbox with \\[org-insert-todo-heading] or
179 toggle a checkbox with \\[org-ctrl-c-ctrl-c]."
180 :group 'org-plain-lists
181 :type 'boolean)
183 (defcustom org-hierarchical-checkbox-statistics t
184 "Non-nil means checkbox statistics counts only the state of direct children.
185 When nil, all boxes below the cookie are counted.
186 This can be set to nil on a per-node basis using a COOKIE_DATA property
187 with the word \"recursive\" in the value."
188 :group 'org-plain-lists
189 :type 'boolean)
191 (defcustom org-description-max-indent 20
192 "Maximum indentation for the second line of a description list.
193 When the indentation would be larger than this, it will become
194 5 characters instead."
195 :group 'org-plain-lists
196 :type 'integer)
198 (defcustom org-list-radio-list-templates
199 '((latex-mode "% BEGIN RECEIVE ORGLST %n
200 % END RECEIVE ORGLST %n
201 \\begin{comment}
202 #+ORGLST: SEND %n org-list-to-latex
204 \\end{comment}\n")
205 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
206 @c END RECEIVE ORGLST %n
207 @ignore
208 #+ORGLST: SEND %n org-list-to-texinfo
210 @end ignore\n")
211 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
212 <!-- END RECEIVE ORGLST %n -->
213 <!--
214 #+ORGLST: SEND %n org-list-to-html
216 -->\n"))
217 "Templates for radio lists in different major modes.
218 All occurrences of %n in a template will be replaced with the name of the
219 list, obtained by prompting the user."
220 :group 'org-plain-lists
221 :type '(repeat
222 (list (symbol :tag "Major mode")
223 (string :tag "Format"))))
225 ;;; Internal functions
227 (defun org-list-end-re ()
228 "Return the regex corresponding to the end of a list.
229 It depends on `org-empty-line-terminates-plain-lists'."
230 (if org-empty-line-terminates-plain-lists
231 "^[ \t]*\n"
232 org-list-end-regexp))
234 (defun org-item-re (&optional general)
235 "Return the correct regular expression for plain lists.
236 If GENERAL is non-nil, return the general regexp independent of the value
237 of `org-plain-list-ordered-item-terminator'."
238 (cond
239 ((or general (eq org-plain-list-ordered-item-terminator t))
240 "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
241 ((= org-plain-list-ordered-item-terminator ?.)
242 "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
243 ((= org-plain-list-ordered-item-terminator ?\))
244 "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
245 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
247 (defconst org-item-beginning-re (concat "^" (org-item-re))
248 "Regexp matching the beginning of a plain list item.")
250 (defun org-list-terminator-between (min max &optional firstp)
251 "Find the position of a list ender between MIN and MAX, or nil.
252 This function looks for `org-list-end-re' not matching a block.
254 If FIRSTP in non-nil, return the point at the beginning of the
255 nearest valid terminator from min. Otherwise, return the point at
256 the end of the nearest terminator from max."
257 (save-excursion
258 (let* ((start (if firstp min max))
259 (end (if firstp max min))
260 (search-fun (if firstp
261 #'org-search-forward-unenclosed
262 #'org-search-backward-unenclosed))
263 (list-end-p (progn
264 (goto-char start)
265 (funcall search-fun (org-list-end-re) end t))))
266 ;; Is there a valid list terminator somewhere ?
267 (and list-end-p
268 ;; we want to be on the first line of the list ender
269 (match-beginning 0)))))
271 (defun org-list-search-unenclosed-generic (search-fun regexp bound noerror count)
272 "Search for REGEXP with SEARCH-FUN but don't stop inside blocks or at protected places."
273 (let ((origin (point)))
274 (cond
275 ;; nothing found: return nil
276 ((not (funcall search-fun regexp bound noerror count)) nil)
277 ((or (save-match-data
278 (org-in-regexps-block-p "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
279 '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2))))
280 (get-text-property (match-beginning 0) 'org-protected))
281 ;; match is enclosed or protected: start again, searching one
282 ;; occurrence away.
283 (goto-char origin)
284 (org-list-search-unenclosed-generic search-fun regexp bound noerror (1+ count)))
285 ;; else return point.
286 (t (point)))))
288 (defun org-search-backward-unenclosed (regexp &optional bound noerror)
289 "Like `re-search-backward' but don't stop inside blocks or at protected places."
290 (org-list-search-unenclosed-generic #'re-search-backward regexp bound noerror 1))
292 (defun org-search-forward-unenclosed (regexp &optional bound noerror)
293 "Like `re-search-forward' but don't stop inside blocks or at protected places."
294 (org-list-search-unenclosed-generic #'re-search-forward regexp bound noerror 1))
296 (defun org-list-at-regexp-after-bullet-p (regexp)
297 "Is point at a list item with REGEXP after bullet?"
298 (and (org-at-item-p)
299 (save-excursion
300 (goto-char (match-end 0))
301 (skip-chars-forward " \t")
302 (looking-at regexp))))
304 (defun org-list-get-item-same-level (search-fun pos limit pre-move)
305 "Return point at the beginning of next item at the same level.
306 Search items using function SEARCH-FUN, from POS to LIMIT. It
307 uses PRE-MOVE before search. Return nil if no item was found."
308 (save-excursion
309 (goto-char pos)
310 (let ((ind (progn
311 (org-beginning-of-item)
312 (org-get-indentation)))
313 (start (point-at-bol)))
314 ;; We don't want to match the current line.
315 (funcall pre-move)
316 ;; Skip any sublist on the way
317 (while (and (funcall search-fun org-item-beginning-re limit t)
318 (> (org-get-indentation) ind)))
319 (when (and (/= (point-at-bol) start) ; Have we moved ?
320 (= (org-get-indentation) ind))
321 (point-at-bol)))))
323 (defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
324 "Insert a new list item at POS.
326 If POS is before first character after bullet of the item, the
327 new item will be created before the current one.
329 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
330 after the bullet. Cursor will be after this text once the
331 function ends."
332 (goto-char pos)
333 ;; Point in a special block: move before it prior to add a new item.
334 (when (org-in-regexps-block-p
335 "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
336 '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
337 ;; in case we're on the #+begin line
338 (end-of-line)
339 (re-search-backward "^[ \t]*#\\+\\(begin\\|BEGIN\\)_" nil t)
340 (end-of-line 0))
341 (let* ((true-pos (point))
342 (bullet (and (org-beginning-of-item)
343 (looking-at org-item-beginning-re)
344 (match-string 0)))
345 (before-p (progn
346 ;; Description item: text starts after colons.
347 (or (org-at-description-p)
348 ;; At a checkbox: text starts after it.
349 (org-at-item-checkbox-p)
350 ;; Otherwise, text starts after bullet.
351 (org-at-item-p))
352 (<= true-pos (match-end 0))))
353 ;; Guess number of blank lines used to separate items.
354 (blank-lines-nb
355 (let ((insert-blank-p
356 (cdr (assq 'plain-list-item org-blank-before-new-entry))))
357 (cond
358 ;; Trivial cases where there should be none.
359 ((or org-empty-line-terminates-plain-lists
360 (not insert-blank-p)) 0)
361 ;; When `org-blank-before-new-entry' says so, or item is
362 ;; alone in the whole list, it is 1.
363 ((or (eq insert-blank-p t)
364 (save-excursion
365 (goto-char (org-list-top-point))
366 (end-of-line)
367 (not (org-search-forward-unenclosed
368 org-item-beginning-re (org-list-bottom-point) t)))) 1)
369 ;; plain-list-item is 'auto. Count blank lines separating
370 ;; neighbours items in list.
371 (t (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
372 (cond
373 ;; Is there a next item?
374 (next-p (goto-char next-p)
375 (org-back-over-empty-lines))
376 ;; Is there a previous item?
377 ((not (org-first-list-item-p)) (org-back-over-empty-lines))
378 ;; Local search failed: search globally.
379 ((and (goto-char (org-list-bottom-point))
380 (beginning-of-line 0)
381 (org-search-backward-unenclosed "^[ \t]*$" (org-list-top-point) t))
382 (1+ (org-back-over-empty-lines)))
383 ;; No blank line found in the whole list.
384 (t 0)))))))
385 (insert-fun
386 (lambda (text)
387 ;; insert bullet above item in order to avoid bothering
388 ;; with possible blank lines ending last item.
389 (org-beginning-of-item)
390 (insert (concat bullet (when checkbox "[ ] ") after-bullet))
391 ;; Stay between after-bullet and before text.
392 (save-excursion
393 (insert (concat text (make-string (1+ blank-lines-nb) ?\n))))
394 (unless before-p (org-move-item-down))
395 (when checkbox (org-update-checkbox-count-maybe)))))
396 (goto-char true-pos)
397 (cond
398 (before-p (funcall insert-fun nil)
399 ;; Not taking advantage of renumbering while moving
400 ;; down. Need to call it directly.
401 (org-maybe-renumber-ordered-list) t)
402 ;; Can't split item: insert bullet at the end of item.
403 ((not (org-get-alist-option org-M-RET-may-split-line 'item))
404 (funcall insert-fun nil) t)
405 ;; else, insert a new bullet along with everything from point
406 ;; down to last non-blank line of item.
408 (delete-horizontal-space)
409 ;; Get pos again in case previous command modified line.
410 (let* ((pos (point))
411 (end-before-blank (org-end-of-item-before-blank))
412 (after-text
413 (when (< pos end-before-blank)
414 (prog1
415 (buffer-substring pos end-before-blank)
416 (delete-region pos end-before-blank)
417 ;; delete any blank line at and before point.
418 (beginning-of-line)
419 (while (looking-at "^[ \t]*$")
420 (delete-region (point-at-bol) (1+ (point-at-eol)))
421 (beginning-of-line 0))))))
422 (funcall insert-fun after-text) t)))))
424 ;;; Predicates
426 (defun org-in-item-p ()
427 "Is the cursor inside a plain list ?"
428 (unless (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
429 (save-excursion
430 (let* ((limit (save-excursion (outline-previous-heading)))
431 ;; Move to eol so current line can be matched by `org-item-re'.
432 (actual-pos (goto-char (point-at-eol)))
433 (last-item-start (save-excursion
434 (org-search-backward-unenclosed org-item-beginning-re limit t)))
435 (list-ender (org-list-terminator-between last-item-start actual-pos)))
436 ;; We are in a list when we are on an item line or when we can
437 ;; find an item before point and there is no valid list ender
438 ;; between it and the point.
439 (and last-item-start
440 (not list-ender))))))
442 (defun org-first-list-item-p ()
443 "Is this heading the first item in a plain list?"
444 (unless (org-at-item-p)
445 (error "Not at a plain list item"))
446 (save-excursion
447 (= (save-excursion (org-beginning-of-item)) (org-beginning-of-item-list))))
449 (defun org-at-item-p ()
450 "Is point in a line starting a hand-formatted item?"
451 (save-excursion
452 (goto-char (point-at-bol))
453 (looking-at org-item-beginning-re)))
455 (defun org-at-item-bullet-p ()
456 "Is point at the bullet of a plain list item?"
457 (and (org-at-item-p)
458 (not (member (char-after) '(?\ ?\t)))
459 (< (point) (match-end 0))))
461 (defun org-at-item-timer-p ()
462 "Is point at a line starting a plain list item with a timer?"
463 (org-list-at-regexp-after-bullet-p "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
465 (defun org-at-description-p ()
466 "Is point at a description list item?"
467 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
469 (defun org-at-item-checkbox-p ()
470 "Is point at a line starting a plain-list item with a checklet?"
471 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
473 (defun org-checkbox-blocked-p ()
474 "Is the current checkbox blocked from for being checked now?
475 A checkbox is blocked if all of the following conditions are fulfilled:
477 1. The checkbox is not checked already.
478 2. The current entry has the ORDERED property set.
479 3. There is an unchecked checkbox in this entry before the current line."
480 (catch 'exit
481 (save-match-data
482 (save-excursion
483 (unless (org-at-item-checkbox-p) (throw 'exit nil))
484 (when (equal (match-string 1) "[X]")
485 ;; the box is already checked!
486 (throw 'exit nil))
487 (let ((end (point-at-bol)))
488 (condition-case nil (org-back-to-heading t)
489 (error (throw 'exit nil)))
490 (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
491 (when (org-search-forward-unenclosed "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t)
492 (org-current-line)))))))
494 ;;; Navigate
496 (defun org-list-top-point ()
497 "Return point at the top level item in a list, or nil if not in a list."
498 (save-excursion
499 (and (org-in-item-p)
500 (let ((pos (point-at-eol))
501 (bound (or (outline-previous-heading) (point-min))))
502 ;; Is there some list above this one ? If so, go to its ending.
503 ;; Otherwise, go back to the heading above or bob.
504 (goto-char (or (org-list-terminator-between bound pos) bound))
505 ;; From there, search down our list.
506 (org-search-forward-unenclosed org-item-beginning-re pos t)
507 (point-at-bol)))))
509 (defun org-list-bottom-point ()
510 "Return point just before list ending or nil if not in a list."
511 (save-excursion
512 (and (org-in-item-p)
513 (let ((pos (org-beginning-of-item))
514 (bound (or (and (let ((outline-regexp org-outline-regexp))
515 ;; Use default regexp because folding
516 ;; changes OUTLINE-REGEXP.
517 (outline-next-heading))
518 (skip-chars-backward " \t\r\n")
519 (1+ (point-at-eol)))
520 (point-max))))
521 ;; The list ending is either first point matching
522 ;; `org-list-end-re', point at first white-line before next
523 ;; heading, or eob.
524 (or (org-list-terminator-between pos bound t) bound)))))
526 (defun org-beginning-of-item ()
527 "Go to the beginning of the current hand-formatted item.
528 If the cursor is not in an item, throw an error. Return point."
529 (interactive)
530 (if (not (org-in-item-p))
531 (error "Not in an item")
532 ;; Possibly match the current line.
533 (end-of-line)
534 (org-search-backward-unenclosed org-item-beginning-re nil t)
535 (goto-char (point-at-bol))))
537 (defun org-end-of-item ()
538 "Go to the end of the current hand-formatted item.
539 If the cursor is not in an item, throw an error."
540 (interactive)
541 (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
542 (cond ((not (org-in-item-p))
543 (error "Not in an item"))
544 (next-p
545 (goto-char next-p))
547 (org-end-of-item-list)))))
549 (defun org-end-of-item-text-before-children ()
550 "Move to the end of the item text, stops before the first child if any.
551 Assumes that the cursor is in the first line of an item."
552 (let ((limit (org-list-bottom-point)))
553 (end-of-line)
554 (goto-char
555 (if (org-search-forward-unenclosed org-item-beginning-re limit t)
556 (point-at-bol)
557 limit))))
559 (defun org-end-of-item-before-blank ()
560 "Return point at end of item, before any blank line.
561 Point returned is at eol."
562 (save-excursion
563 (org-end-of-item)
564 (skip-chars-backward " \r\t\n")
565 (point-at-eol)))
567 (defun org-get-next-item (pos limit)
568 "Get the point of the next item at the same level as POS.
569 Stop searching at LIMIT. Return nil if no item is found. This
570 function does not move point."
571 (org-list-get-item-same-level
572 #'org-search-forward-unenclosed pos limit #'end-of-line))
574 (defun org-get-previous-item (pos limit)
575 "Get the point of the previous item at the same level as POS.
576 Stop searching at LIMIT. Return nil if no item is found. This
577 function does not move point."
578 (org-list-get-item-same-level
579 #'org-search-backward-unenclosed pos limit #'beginning-of-line))
581 (defun org-next-item ()
582 "Move to the beginning of the next item.
583 Item is at the same level in the current plain list. Error if not
584 in a plain list, or if this is the last item in the list."
585 (interactive)
586 (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
587 (if next-p
588 (goto-char next-p)
589 (error "On last item"))))
591 (defun org-previous-item ()
592 "Move to the beginning of the previous item.
593 Item is at the same level in the current plain list. Error if not
594 in a plain list, or if this is the first item in the list."
595 (interactive)
596 (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
597 (if prev-p
598 (goto-char prev-p)
599 (error "On first item"))))
601 (defun org-beginning-of-item-list ()
602 "Go to the beginning item of the current list or sublist.
603 Return point."
604 (interactive)
605 (let ((limit (org-list-top-point))
606 (move-up (lambda (pos bound)
607 ;; prev-p: any item of same level before ?
608 (let ((prev-p (org-get-previous-item pos bound)))
609 ;; recurse until no more item of the same level
610 ;; can be found.
611 (if prev-p (funcall move-up prev-p bound) pos)))))
612 ;; Go to the last item found and at bol in case we didn't move
613 (goto-char (funcall move-up (point) limit))
614 (goto-char (point-at-bol))))
616 (defun org-end-of-item-list ()
617 "Go to the end of the current list or sublist.
618 Return point."
619 (interactive)
620 (org-beginning-of-item)
621 (let ((limit (org-list-bottom-point))
622 (ind (org-get-indentation))
623 (get-last-item (lambda (pos bound)
624 ;; next-p: any item of same level after ?
625 (let ((next-p (org-get-next-item pos bound)))
626 ;; recurse until no more item of the same level
627 ;; can be found.
628 (if next-p (funcall get-last-item next-p bound) pos)))))
629 ;; Move to the last item of every list or sublist encountered, and
630 ;; down to bol of a higher-level item, or limit.
631 (while (and (/= (point) limit)
632 (>= (org-get-indentation) ind))
633 (goto-char (funcall get-last-item (point) limit))
634 (end-of-line)
635 (when (org-search-forward-unenclosed org-item-beginning-re limit 'move)
636 (beginning-of-line)))
637 (point)))
639 ;;; Manipulate
641 (defun org-list-exchange-items (beg-A beg-B)
642 "Swap item starting at BEG-A with item starting at BEG-B.
643 Blank lines at the end of items are left in place. Assumes
644 BEG-A is lesser than BEG-B."
645 (save-excursion
646 (let* ((end-of-item-no-blank (lambda (pos)
647 (goto-char pos)
648 (goto-char (org-end-of-item-before-blank))))
649 (end-A-no-blank (funcall end-of-item-no-blank beg-A))
650 (end-B-no-blank (funcall end-of-item-no-blank beg-B))
651 (body-A (buffer-substring beg-A end-A-no-blank))
652 (body-B (buffer-substring beg-B end-B-no-blank))
653 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
654 (goto-char beg-A)
655 (delete-region beg-A end-B-no-blank)
656 (insert (concat body-B between-A-no-blank-and-B body-A)))))
658 (defun org-move-item-down ()
659 "Move the plain list item at point down, i.e. swap with following item.
660 Subitems (items with larger indentation) are considered part of the item,
661 so this really moves item trees."
662 (interactive)
663 (let ((pos (point))
664 (col (current-column))
665 (actual-item (org-beginning-of-item))
666 (next-item (org-get-next-item (point) (save-excursion (org-end-of-item-list)))))
667 (if (not next-item)
668 (progn
669 (goto-char pos)
670 (error "Cannot move this item further down"))
671 (org-list-exchange-items actual-item next-item)
672 (org-maybe-renumber-ordered-list)
673 (org-next-item)
674 (move-to-column col))))
676 (defun org-move-item-up ()
677 "Move the plain list item at point up, i.e. swap with previous item.
678 Subitems (items with larger indentation) are considered part of the item,
679 so this really moves item trees."
680 (interactive)
681 (let ((pos (point))
682 (col (current-column))
683 (actual-item (org-beginning-of-item))
684 (prev-item (org-get-previous-item (point) (save-excursion (org-beginning-of-item-list)))))
685 (if (not prev-item)
686 (progn
687 (goto-char pos)
688 (error "Cannot move this item further up"))
689 (org-list-exchange-items prev-item actual-item)
690 (org-maybe-renumber-ordered-list)
691 (move-to-column col))))
693 (defun org-insert-item (&optional checkbox)
694 "Insert a new item at the current level.
696 If cursor is before first character after bullet of the item, the
697 new item will be created before the current one. Return t when
698 things worked, nil when we are not in an item, or item is
699 invisible."
700 (unless (or (not (org-in-item-p))
701 (org-invisible-p))
702 (if (save-excursion
703 (org-beginning-of-item)
704 (org-at-item-timer-p))
705 ;; Timer list: delegate to `org-timer-item'.
706 (progn (org-timer-item) t)
707 ;; if we're in a description list, ask for the new term.
708 (let ((desc-text (when (save-excursion
709 (and (org-beginning-of-item)
710 (org-at-description-p)))
711 (concat (read-string "Term: ") " :: "))))
712 (org-list-insert-item-generic
713 (point) (and checkbox (not desc-text)) desc-text)))))
715 ;;; Indentation
717 (defun org-get-string-indentation (s)
718 "What indentation has S due to SPACE and TAB at the beginning of the string?"
719 (let ((n -1) (i 0) (w tab-width) c)
720 (catch 'exit
721 (while (< (setq n (1+ n)) (length s))
722 (setq c (aref s n))
723 (cond ((= c ?\ ) (setq i (1+ i)))
724 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
725 (t (throw 'exit t)))))
728 (defvar org-suppress-item-indentation) ; dynamically scoped parameter
730 (defun org-shift-item-indentation (delta)
731 "Shift the indentation in current item by DELTA."
732 (unless (org-bound-and-true-p org-suppress-item-indentation)
733 (save-excursion
734 (let ((beg (point-at-bol))
735 (end (org-end-of-item)))
736 (beginning-of-line 0)
737 (while (> (point) beg)
738 (when (looking-at "[ \t]*\\S-")
739 ;; this is not an empty line
740 (let ((i (org-get-indentation)))
741 (when (and (> i 0) (> (+ i delta) 0))
742 (indent-line-to (+ i delta)))))
743 (beginning-of-line 0))))))
746 (defvar org-last-indent-begin-marker (make-marker))
747 (defvar org-last-indent-end-marker (make-marker))
749 (defun org-outdent-item (arg)
750 "Outdent a local list item, but not its children."
751 (interactive "p")
752 (org-indent-item-tree (- arg) 'no-subtree))
754 (defun org-indent-item (arg)
755 "Indent a local list item, but not its children."
756 (interactive "p")
757 (org-indent-item-tree arg 'no-subtree))
759 (defun org-outdent-item-tree (arg &optional no-subtree)
760 "Outdent a local list item including its children.
761 If NO-SUBTREE is set, only outdent the item itself, not its children."
762 (interactive "p")
763 (org-indent-item-tree (- arg) no-subtree))
765 (defun org-indent-item-tree (arg &optional no-subtree)
766 "Indent a local list item including its children.
767 If NO-SUBTREE is set, only indent the item itself, not its
768 children. Return t if sucessful."
769 (interactive "p")
770 (and (org-region-active-p) (org-cursor-to-region-beginning))
771 (unless (org-at-item-p)
772 (error "Not on an item"))
773 (let ((origin-ind (save-excursion
774 (goto-char (org-list-top-point))
775 (org-get-indentation)))
776 beg end ind ind1 ind-pos bullet delta ind-down ind-up firstp)
777 (setq firstp (org-first-list-item-p))
778 (save-excursion
779 (setq end (and (org-region-active-p) (region-end)))
780 ;; If moving a subtree, don't drain other items on the way.
781 (if (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
782 (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
783 (setq beg org-last-indent-begin-marker
784 end org-last-indent-end-marker)
785 (org-beginning-of-item)
786 (setq beg (move-marker org-last-indent-begin-marker (point)))
787 (cond
788 ;; Top-item: reindent all down to end of list.
789 ((= (point-at-bol) (org-list-top-point)) (goto-char (org-list-bottom-point)))
790 ;; No-subtree: reindent down to next children, if any.
791 (no-subtree (org-end-of-item-text-before-children))
792 ;; Else: reindent down to next item.
793 (t (org-end-of-item)))
794 (setq end (move-marker org-last-indent-end-marker (or end (point)))))
795 (goto-char beg)
796 (setq ind-pos (org-item-indent-positions)
797 bullet (cdr (car ind-pos))
798 ind (caar ind-pos)
799 ind-down (car (nth 2 ind-pos))
800 ind-up (car (nth 1 ind-pos))
801 delta (if (> arg 0)
802 (if ind-down (- ind-down ind) 2)
803 (if ind-up (- ind-up ind) -2)))
804 (cond
805 ;; Going to a negative column is nonsensical.
806 ((< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
807 ;; Apply indent rules if activated.
808 ((cdr (assq 'indent org-list-automatic-rules))
809 (cond
810 ;; 1. If at top-point move the whole list. Moreover, if
811 ;; *-list is going to column 0, change bullet to "-".
812 ((= (point-at-bol) (org-list-top-point))
813 (when (and (= (+ delta ind) 0) (equal bullet "*")) (org-fix-bullet-type "-"))
814 (setq end (set-marker org-last-indent-end-marker (org-list-bottom-point))))
815 ;; 2. Do not indent before top-item.
816 ((< (+ delta ind) origin-ind)
817 (error "Cannot outdent beyond top level item"))
818 ;; 3. Do not indent the first item of a list.
819 ((and firstp (> delta 0))
820 (error "Cannot indent the beginning of a sublist"))
821 ;; 4. Do not outdent item that has children without moving.
822 ;; In the case of a subtree, make sure the check applies to
823 ;; its last item.
824 ((and (save-excursion
825 (goto-char (1- end))
826 (/= (save-excursion (org-end-of-item-text-before-children))
827 (save-excursion (org-end-of-item))))
828 (< delta 0))
829 (error "Cannot outdent an item having children")))))
830 ;; Proceed to reindentation.
831 (while (< (point) end)
832 (beginning-of-line)
833 (skip-chars-forward " \t") (setq ind1 (current-column))
834 (delete-region (point-at-bol) (point))
835 (or (eolp) (org-indent-to-column (+ ind1 delta)))
836 (beginning-of-line 2)))
837 (org-fix-bullet-type
838 (and (> arg 0)
839 (cdr (assoc bullet org-list-demote-modify-bullet))))
840 ;; Reorder lists that might have changed
841 (save-excursion
842 (beginning-of-line 0)
843 (ignore-errors (org-beginning-of-item))
844 (org-maybe-renumber-ordered-list))
845 (save-excursion
846 (org-end-of-item-text-before-children)
847 (org-maybe-renumber-ordered-list))
848 (save-excursion
849 (org-end-of-item-list)
850 (org-maybe-renumber-ordered-list))
853 (defun org-item-indent-positions ()
854 "Return indentation for plain list items.
855 This returns a list with three values: The current indentation, the
856 parent indentation and the indentation a child should have.
857 Assumes cursor in item line."
858 (let* ((bolpos (point-at-bol))
859 (ind (org-get-indentation))
860 (bullet (org-get-bullet))
861 ind-down ind-up bullet-up bullet-down pos)
862 (save-excursion
863 (org-beginning-of-item-list)
864 (skip-chars-backward "\n\r \t")
865 (when (org-in-item-p)
866 (org-beginning-of-item)
867 (let ((prev-indent (org-get-indentation)))
868 (when (< prev-indent ind)
869 (setq ind-up prev-indent)
870 (setq bullet-up (org-get-bullet))))))
871 (setq pos (point))
872 (save-excursion
873 (cond
874 ((and (ignore-errors (progn (org-previous-item) t))
875 (or (end-of-line) t)
876 (org-search-forward-unenclosed org-item-beginning-re bolpos t))
877 (setq ind-down (org-get-indentation)
878 bullet-down (org-get-bullet)))
879 ((and (goto-char pos)
880 (org-at-item-p))
881 (goto-char (match-end 0))
882 (skip-chars-forward " \t")
883 (setq ind-down (current-column)
884 bullet-down (org-get-bullet)))))
885 (if (and bullet-down (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-down))
886 (setq bullet-down (concat "1" (match-string 1 bullet-down))))
887 (if (and bullet-up (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-up))
888 (setq bullet-up (concat "1" (match-string 1 bullet-up))))
889 (if (and bullet (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet))
890 (setq bullet (concat "1" (match-string 1 bullet))))
891 (list (cons ind bullet)
892 (cons ind-up bullet-up)
893 (cons ind-down bullet-down))))
895 (defvar org-tab-ind-state) ; defined in org.el
897 (defun org-cycle-item-indentation ()
898 (let ((org-suppress-item-indentation t)
899 (org-adapt-indentation nil))
900 (when (and (looking-at "[ \t]*$")
901 (org-looking-back (concat org-item-beginning-re "[ \t]*")))
902 (setq this-command 'org-cycle-item-indentation)
903 ;; When in the middle of the cycle, try to outdent first. If it
904 ;; fails, and point is still at initial position, indent. Else,
905 ;; go back to original position.
906 (if (eq last-command 'org-cycle-item-indentation)
907 (cond
908 ((ignore-errors (org-indent-item -1)))
909 ((and (= (org-get-indentation) org-tab-ind-state)
910 (ignore-errors (org-indent-item 1))))
911 (t (back-to-indentation)
912 (org-indent-to-column org-tab-ind-state)
913 (end-of-line)
914 (org-maybe-renumber-ordered-list)
915 ;; Break cycle
916 (setq this-command 'identity)))
917 ;; If a cycle has just started, try to indent first. If it
918 ;; fails, try to outdent.
919 (setq org-tab-ind-state (org-get-indentation))
920 (cond
921 ((ignore-errors (org-indent-item 1)))
922 ((ignore-errors (org-indent-item -1)))
923 (t (error "Cannot move item"))))
924 t)))
926 ;;; Bullets
928 (defun org-get-bullet ()
929 (and (org-at-item-p)
930 (org-trim (match-string 1))))
932 (defun org-fix-bullet-type (&optional force-bullet)
933 "Make sure all items in this list have the same bullet as the first item.
934 Also, fix the indentation."
935 (interactive)
936 (unless (org-at-item-p) (error "This is not a list"))
937 (org-preserve-lc
938 (let* ((ini-bul (progn (org-beginning-of-item-list) (org-get-bullet)))
939 (bullet
940 (progn
941 (concat
942 (or force-bullet ini-bul) " "
943 ;; Do we need to concat another white space ?
944 (when (and org-list-two-spaces-after-bullet-regexp
945 (string-match org-list-two-spaces-after-bullet-regexp ini-bul))
946 " "))))
947 (replace-bullet
948 (lambda (result bullet)
949 (let* ((old (progn
950 (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
951 (match-string 1))))
952 (unless (equal bullet old)
953 (replace-match bullet nil nil nil 1)
954 ;; When bullet lengths are differents, move the whole
955 ;; sublist accordingly
956 (org-shift-item-indentation (- (length bullet) (length old))))))))
957 (org-apply-on-list replace-bullet nil bullet)
958 (org-maybe-renumber-ordered-list))))
960 (defun org-renumber-ordered-list (&optional arg)
961 "Renumber an ordered plain list.
962 Cursor needs to be in the first line of an item, the line that starts
963 with something like \"1.\" or \"2)\". Start to count at ARG or 1."
964 (interactive "p")
965 (unless (and (org-at-item-p)
966 (match-beginning 3))
967 (error "This is not an ordered list"))
968 (org-preserve-lc
969 (let* ((offset (progn
970 (org-beginning-of-item)
971 (or (and (looking-at "[ \t]*\\[@start:\\([0-9]+\\)")
972 (string-to-number (match-string 1)))
974 1)))
975 (item-fmt (progn
976 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
977 (concat "%d" (or (match-string 1) "."))))
978 ;; Here is the function applied at each item of the list.
979 (renumber-item (lambda (counter off fmt)
980 (let* ((new (format fmt (+ counter off)))
981 (old (progn
982 (looking-at org-item-beginning-re)
983 (match-string 2)))
984 (begin (match-beginning 2))
985 (end (match-end 2)))
986 (delete-region begin end)
987 (goto-char begin)
988 (insert new)
989 ;; In case item number went from 9. to 10.
990 ;; or the other way.
991 (org-shift-item-indentation (- (length new) (length old)))
992 (1+ counter)))))
993 (org-apply-on-list renumber-item 0 offset item-fmt))))
995 (defun org-maybe-renumber-ordered-list ()
996 "Renumber the ordered list at point if setup allows it.
997 This tests the user option `org-auto-renumber-ordered-lists' before
998 doing the renumbering. Do not throw error on failure."
999 (interactive)
1000 (when org-auto-renumber-ordered-lists
1001 (ignore-errors (org-renumber-ordered-list))))
1003 (defun org-cycle-list-bullet (&optional which)
1004 "Cycle through the different itemize/enumerate bullets.
1005 This cycle the entire list level through the sequence:
1007 `-' -> `+' -> `*' -> `1.' -> `1)'
1009 If WHICH is a valid string, use that as the new bullet. If WHICH
1010 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
1011 'previous, cycle backwards."
1012 (interactive "P")
1013 (org-preserve-lc
1014 (let* ((bullet (progn (org-beginning-of-item-list)
1015 (org-get-bullet)))
1016 (current (cond
1017 ((string-match "\\." bullet) "1.")
1018 ((string-match ")" bullet) "1)")
1019 (t bullet)))
1020 (bullet-list (append '("-" "+" )
1021 ;; *-bullets are not allowed at column 0
1022 (unless (looking-at "\\S-") '("*"))
1023 ;; Description items cannot be numbered
1024 (unless (org-at-description-p) '("1." "1)"))))
1025 (len (length bullet-list))
1026 (item-index (- len (length (member current bullet-list))))
1027 (get-value (lambda (index) (nth (mod index len) bullet-list)))
1028 (new (cond
1029 ((member which bullet-list) which)
1030 ((numberp which) (funcall get-value which))
1031 ((eq 'previous which) (funcall get-value (1- item-index)))
1032 (t (funcall get-value (1+ item-index))))))
1033 (org-fix-bullet-type new))))
1035 ;;; Checkboxes
1037 (defun org-toggle-checkbox (&optional toggle-presence)
1038 "Toggle the checkbox in the current line.
1039 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
1040 With double prefix, set checkbox to [-].
1041 When there is an active region, toggle status or presence of the checkbox
1042 in the first line, and make every item in the region have the same
1043 status or presence, respectively.
1044 If the cursor is in a headline, apply this to all checkbox items in the
1045 text below the heading."
1046 (interactive "P")
1047 (catch 'exit
1048 (let (beg end status first-present first-status blocked)
1049 (cond
1050 ((org-region-active-p)
1051 (setq beg (region-beginning) end (region-end)))
1052 ((org-on-heading-p)
1053 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
1054 ((org-at-item-checkbox-p)
1055 (save-excursion
1056 (if (equal toggle-presence '(4))
1057 (progn
1058 (replace-match "" nil nil nil 1)
1059 (goto-char (match-beginning 0))
1060 (just-one-space))
1061 (when (setq blocked (org-checkbox-blocked-p))
1062 (error "Checkbox blocked because of unchecked box in line %d"
1063 blocked))
1064 (replace-match
1065 (cond ((equal toggle-presence '(16)) "[-]")
1066 ((member (match-string 1) '("[ ]" "[-]")) "[X]")
1067 (t "[ ]"))
1068 t t nil 1)))
1069 (throw 'exit t))
1070 ((org-at-item-p)
1071 ;; add a checkbox
1072 (save-excursion
1073 (goto-char (match-end 0))
1074 (insert "[ ] "))
1075 (throw 'exit t))
1076 (t (error "Not at a checkbox or heading, and no active region")))
1077 (setq end (move-marker (make-marker) end))
1078 (save-excursion
1079 (goto-char beg)
1080 (setq first-present (org-at-item-checkbox-p)
1081 first-status
1082 (save-excursion
1083 (and (org-search-forward-unenclosed "[ \t]\\(\\[[ X]\\]\\)" end t)
1084 (equal (match-string 0) "[X]"))))
1085 (while (< (point) end)
1086 (if toggle-presence
1087 (cond
1088 ((and first-present (org-at-item-checkbox-p))
1089 (save-excursion
1090 (replace-match "")
1091 (goto-char (match-beginning 0))
1092 (just-one-space)))
1093 ((and (not first-present) (not (org-at-item-checkbox-p))
1094 (org-at-item-p))
1095 (save-excursion
1096 (goto-char (match-end 0))
1097 (insert "[ ] "))))
1098 (when (org-at-item-checkbox-p)
1099 (setq status (equal (match-string 1) "[X]"))
1100 (replace-match
1101 (if first-status "[ ]" "[X]") t t nil 1)))
1102 (beginning-of-line 2)))))
1103 (org-update-checkbox-count-maybe))
1105 (defun org-reset-checkbox-state-subtree ()
1106 "Reset all checkboxes in an entry subtree."
1107 (interactive "*")
1108 (save-restriction
1109 (save-excursion
1110 (org-narrow-to-subtree)
1111 (org-show-subtree)
1112 (goto-char (point-min))
1113 (let ((end (point-max)))
1114 (while (< (point) end)
1115 (when (org-at-item-checkbox-p)
1116 (replace-match "[ ]" t t nil 1))
1117 (beginning-of-line 2))))
1118 (org-update-checkbox-count-maybe)))
1120 (defvar org-checkbox-statistics-hook nil
1121 "Hook that is run whenever Org thinks checkbox statistics should be updated.
1122 This hook runs even if `org-provide-checkbox-statistics' is nil, to it can
1123 be used to implement alternative ways of collecting statistics information.")
1125 (defun org-update-checkbox-count-maybe ()
1126 "Update checkbox statistics unless turned off by user."
1127 (when org-provide-checkbox-statistics
1128 (org-update-checkbox-count))
1129 (run-hooks 'org-checkbox-statistics-hook))
1131 (defun org-update-checkbox-count (&optional all)
1132 "Update the checkbox statistics in the current section.
1133 This will find all statistic cookies like [57%] and [6/12] and update them
1134 with the current numbers. With optional prefix argument ALL, do this for
1135 the whole buffer."
1136 (interactive "P")
1137 (save-excursion
1138 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
1139 (beg (condition-case nil
1140 (progn (org-back-to-heading) (point))
1141 (error (point-min))))
1142 (end (move-marker (make-marker)
1143 (progn (outline-next-heading) (point))))
1144 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
1145 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
1146 (re-find (concat re "\\|" re-box))
1147 beg-cookie end-cookie is-percent c-on c-off lim new
1148 eline curr-ind next-ind continue-from startsearch
1149 (recursive
1150 (or (not org-hierarchical-checkbox-statistics)
1151 (string-match "\\<recursive\\>"
1152 (or (ignore-errors
1153 (org-entry-get nil "COOKIE_DATA"))
1154 ""))))
1155 (cstat 0)
1157 (when all
1158 (goto-char (point-min))
1159 (outline-next-heading)
1160 (setq beg (point) end (point-max)))
1161 (goto-char end)
1162 ;; find each statistics cookie
1163 (while (and (org-search-backward-unenclosed re-find beg t)
1164 (not (save-match-data
1165 (and (org-on-heading-p)
1166 (string-match "\\<todo\\>"
1167 (downcase
1168 (or (org-entry-get
1169 nil "COOKIE_DATA")
1170 "")))))))
1171 (setq beg-cookie (match-beginning 1)
1172 end-cookie (match-end 1)
1173 cstat (+ cstat (if end-cookie 1 0))
1174 startsearch (point-at-eol)
1175 continue-from (match-beginning 0)
1176 is-percent (match-beginning 2)
1177 lim (cond
1178 ((org-on-heading-p) (outline-next-heading) (point))
1179 ((org-at-item-p) (org-end-of-item) (point))
1180 (t nil))
1181 c-on 0
1182 c-off 0)
1183 (when lim
1184 ;; find first checkbox for this cookie and gather
1185 ;; statistics from all that are at this indentation level
1186 (goto-char startsearch)
1187 (if (org-search-forward-unenclosed re-box lim t)
1188 (progn
1189 (org-beginning-of-item)
1190 (setq curr-ind (org-get-indentation))
1191 (setq next-ind curr-ind)
1192 (while (and (bolp) (org-at-item-p)
1193 (if recursive
1194 (<= curr-ind next-ind)
1195 (= curr-ind next-ind)))
1196 (setq eline (point-at-eol))
1197 (if (org-search-forward-unenclosed re-box eline t)
1198 (if (member (match-string 2) '("[ ]" "[-]"))
1199 (setq c-off (1+ c-off))
1200 (setq c-on (1+ c-on))))
1201 (if (not recursive)
1202 ;; org-get-next-item goes through list-enders
1203 ;; with proper limit.
1204 (goto-char (or (org-get-next-item (point) lim) lim))
1205 (end-of-line)
1206 (when (org-search-forward-unenclosed org-item-beginning-re lim t)
1207 (beginning-of-line)))
1208 (setq next-ind (org-get-indentation)))))
1209 (goto-char continue-from)
1210 ;; update cookie
1211 (when end-cookie
1212 (setq new (if is-percent
1213 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
1214 (format "[%d/%d]" c-on (+ c-on c-off))))
1215 (goto-char beg-cookie)
1216 (insert new)
1217 (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
1218 ;; update items checkbox if it has one
1219 (when (org-at-item-p)
1220 (org-beginning-of-item)
1221 (when (and (> (+ c-on c-off) 0)
1222 (org-search-forward-unenclosed re-box (point-at-eol) t))
1223 (setq beg-cookie (match-beginning 2)
1224 end-cookie (match-end 2))
1225 (delete-region beg-cookie end-cookie)
1226 (goto-char beg-cookie)
1227 (cond ((= c-off 0) (insert "[X]"))
1228 ((= c-on 0) (insert "[ ]"))
1229 (t (insert "[-]")))
1231 (goto-char continue-from))
1232 (when (interactive-p)
1233 (message "Checkbox statistics updated %s (%d places)"
1234 (if all "in entire file" "in current outline entry") cstat)))))
1236 (defun org-get-checkbox-statistics-face ()
1237 "Select the face for checkbox statistics.
1238 The face will be `org-done' when all relevant boxes are checked.
1239 Otherwise it will be `org-todo'."
1240 (if (match-end 1)
1241 (if (equal (match-string 1) "100%")
1242 'org-checkbox-statistics-done
1243 'org-checkbox-statistics-todo)
1244 (if (and (> (match-end 2) (match-beginning 2))
1245 (equal (match-string 2) (match-string 3)))
1246 'org-checkbox-statistics-done
1247 'org-checkbox-statistics-todo)))
1249 ;;; Misc Tools
1251 (defun org-apply-on-list (function init-value &rest args)
1252 "Call FUNCTION for each item of a the list under point.
1254 FUNCTION must be called with at least one argument : a return
1255 value that will contain the value returned by the function at
1256 the previous item, plus ARGS extra arguments. INIT-VALUE will be
1257 the value passed to the function at the first item of the list.
1259 As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
1260 will return the number of items in the current list.
1262 Sublists of the list are skipped. Cursor is always at the
1263 beginning of the item."
1264 (save-excursion
1265 (let ((end (copy-marker (save-excursion (org-end-of-item-list))))
1266 (next-p (make-marker))
1267 (move-down-action
1268 (lambda (pos value &rest args)
1269 (goto-char pos)
1270 (set-marker next-p (org-get-next-item pos end))
1271 (let ((return-value (apply function value args)))
1272 (if (marker-position next-p)
1273 (apply move-down-action next-p return-value args)
1274 return-value)))))
1275 (apply move-down-action (org-beginning-of-item-list) init-value args))))
1277 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
1278 "Sort plain list items.
1279 The cursor may be at any item of the list that should be sorted.
1280 Sublists are not sorted. Checkboxes, if any, are ignored.
1282 Sorting can be alphabetically, numerically, by date/time as given by
1283 a time stamp, by a property or by priority.
1285 The command prompts for the sorting type unless it has been given
1286 to the function through the SORTING-TYPE argument, which needs to
1287 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
1288 meaning of each character:
1290 n Numerically, by converting the beginning of the item to a number.
1291 a Alphabetically.
1292 t By date/time, either the first active time stamp in the entry, if
1293 any, or by the first inactive one. In a timer list, sorts the timers.
1294 Only the first line of item is checked.
1296 Capital letters will reverse the sort order.
1298 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
1299 function to be called with point at the beginning of the record.
1300 It must return either a string or a number that should serve as
1301 the sorting key for that record.
1303 Comparing entries ignores case by default. However, with an
1304 optional argument WITH-CASE, the sorting considers case as well."
1305 (interactive "P")
1306 (let* ((case-func (if with-case 'identity 'downcase))
1307 (start (org-beginning-of-item-list))
1308 (end (save-excursion (org-end-of-item-list)))
1309 (sorting-type
1310 (progn
1311 (message
1312 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
1313 (read-char-exclusive)))
1314 (getkey-func (and (= (downcase sorting-type) ?f)
1315 (org-icompleting-read "Sort using function: "
1316 obarray 'fboundp t nil nil)
1317 (intern getkey-func))))
1318 (message "Sorting items...")
1319 (save-restriction
1320 (narrow-to-region start end)
1321 (let* ((dcst (downcase sorting-type))
1322 (case-fold-search nil)
1323 (now (current-time))
1324 (sort-func (cond
1325 ((= dcst ?a) 'string<)
1326 ((= dcst ?f) compare-func)
1327 ((= dcst ?t) '<)
1328 (t nil)))
1329 (begin-record (lambda ()
1330 (skip-chars-forward " \r\t\n")
1331 (beginning-of-line)))
1332 (end-record (lambda ()
1333 (goto-char (org-end-of-item-before-blank))))
1334 (value-to-sort
1335 (lambda ()
1336 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
1337 (cond
1338 ((= dcst ?n)
1339 (string-to-number (buffer-substring (match-end 0)
1340 (point-at-eol))))
1341 ((= dcst ?a)
1342 (buffer-substring (match-end 0) (point-at-eol)))
1343 ((= dcst ?t)
1344 (cond
1345 ;; If it is a timer list, convert timer to seconds
1346 ((org-at-item-timer-p)
1347 (org-timer-hms-to-secs (match-string 1)))
1348 ((or (org-search-forward-unenclosed org-ts-regexp
1349 (point-at-eol) t)
1350 (org-search-forward-unenclosed org-ts-regexp-both
1351 (point-at-eol) t))
1352 (org-time-string-to-seconds (match-string 0)))
1353 (t (org-float-time now))))
1354 ((= dcst ?f)
1355 (if getkey-func
1356 (let ((value (funcall getkey-func)))
1357 (if (stringp value)
1358 (funcall case-func value)
1359 value))
1360 (error "Invalid key function `%s'" getkey-func)))
1361 (t (error "Invalid sorting type `%c'" sorting-type)))))))
1362 (sort-subr (/= dcst sorting-type) begin-record end-record value-to-sort nil sort-func)
1363 (org-maybe-renumber-ordered-list)
1364 (run-hooks 'org-after-sorting-entries-or-items-hook)
1365 (message "Sorting items...done")))))
1367 ;;; Send and receive lists
1369 (defun org-list-parse-list (&optional delete)
1370 "Parse the list at point and maybe DELETE it.
1371 Return a list containing first level items as strings and
1372 sublevels as a list of strings."
1373 (let* ((start (goto-char (org-list-top-point)))
1374 (end (org-list-bottom-point))
1375 output itemsep ltype)
1376 (while (org-search-forward-unenclosed org-item-beginning-re end t)
1377 (save-excursion
1378 (beginning-of-line)
1379 (setq ltype (cond ((looking-at-p "^[ \t]*[0-9]") 'ordered)
1380 ((org-at-description-p) 'descriptive)
1381 (t 'unordered))))
1382 (let* ((indent1 (org-get-indentation))
1383 (nextitem (or (org-get-next-item (point) end) end))
1384 (item (org-trim (buffer-substring (point) (org-end-of-item-text-before-children))))
1385 (nextindent (if (= (point) end) 0 (org-get-indentation)))
1386 (item (if (string-match "^\\[\\([xX ]\\)\\]" item)
1387 (replace-match (if (equal (match-string 1 item) " ")
1388 "[CBOFF]"
1389 "[CBON]")
1390 t nil item)
1391 item)))
1392 (push item output)
1393 (when (> nextindent indent1)
1394 (save-restriction
1395 (narrow-to-region (point) nextitem)
1396 (push (org-list-parse-list) output)))))
1397 (when delete
1398 (delete-region start end)
1399 (save-match-data
1400 (when (looking-at (org-list-end-re))
1401 (replace-match "\n"))))
1402 (setq output (nreverse output))
1403 (push ltype output)))
1405 (defun org-list-make-subtree ()
1406 "Convert the plain list at point into a subtree."
1407 (interactive)
1408 (if (not (org-in-item-p))
1409 (error "Not in a list.")
1410 (goto-char (org-list-top-point))
1411 (let ((list (org-list-parse-list t)) nstars)
1412 (save-excursion
1413 (if (ignore-errors
1414 (org-back-to-heading))
1415 (progn (org-search-forward-unenclosed org-complex-heading-regexp nil t)
1416 (setq nstars (length (match-string 1))))
1417 (setq nstars 0)))
1418 (org-list-make-subtrees list (1+ nstars)))))
1420 (defun org-list-make-subtrees (list level)
1421 "Convert LIST into subtrees starting at LEVEL."
1422 (if (symbolp (car list))
1423 (org-list-make-subtrees (cdr list) level)
1424 (mapcar (lambda (item)
1425 (if (stringp item)
1426 (insert (make-string
1427 (if org-odd-levels-only
1428 (1- (* 2 level)) level) ?*) " " item "\n")
1429 (org-list-make-subtrees item (1+ level))))
1430 list)))
1432 (defun org-list-insert-radio-list ()
1433 "Insert a radio list template appropriate for this major mode."
1434 (interactive)
1435 (let* ((e (assq major-mode org-list-radio-list-templates))
1436 (txt (nth 1 e))
1437 name pos)
1438 (unless e (error "No radio list setup defined for %s" major-mode))
1439 (setq name (read-string "List name: "))
1440 (while (string-match "%n" txt)
1441 (setq txt (replace-match name t t txt)))
1442 (or (bolp) (insert "\n"))
1443 (setq pos (point))
1444 (insert txt)
1445 (goto-char pos)))
1447 (defun org-list-send-list (&optional maybe)
1448 "Send a transformed version of this list to the receiver position.
1449 With argument MAYBE, fail quietly if no transformation is defined for
1450 this list."
1451 (interactive)
1452 (catch 'exit
1453 (unless (org-at-item-p) (error "Not at a list"))
1454 (save-excursion
1455 (re-search-backward "#\\+ORGLST" nil t)
1456 (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
1457 (if maybe
1458 (throw 'exit nil)
1459 (error "Don't know how to transform this list"))))
1460 (let* ((name (match-string 1))
1461 (transform (intern (match-string 2)))
1462 (bottom-point
1463 (save-excursion
1464 (re-search-forward "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
1465 (match-beginning 0)))
1466 (top-point
1467 (progn
1468 (re-search-backward "#\\+ORGLST" nil t)
1469 (re-search-forward org-item-beginning-re bottom-point t)
1470 (match-beginning 0)))
1471 (list (save-restriction
1472 (narrow-to-region top-point bottom-point)
1473 (org-list-parse-list)))
1474 beg txt)
1475 (unless (fboundp transform)
1476 (error "No such transformation function %s" transform))
1477 (let ((txt (funcall transform list)))
1478 ;; Find the insertion place
1479 (save-excursion
1480 (goto-char (point-min))
1481 (unless (re-search-forward
1482 (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
1483 (error "Don't know where to insert translated list"))
1484 (goto-char (match-beginning 0))
1485 (beginning-of-line 2)
1486 (setq beg (point))
1487 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
1488 (error "Cannot find end of insertion region"))
1489 (delete-region beg (point-at-bol))
1490 (goto-char beg)
1491 (insert txt "\n")))
1492 (message "List converted and installed at receiver location"))))
1494 (defun org-list-to-generic (list params)
1495 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1497 Valid parameters PARAMS are
1499 :ustart String to start an unordered list
1500 :uend String to end an unordered list
1502 :ostart String to start an ordered list
1503 :oend String to end an ordered list
1505 :dstart String to start a descriptive list
1506 :dend String to end a descriptive list
1507 :dtstart String to start a descriptive term
1508 :dtend String to end a descriptive term
1509 :ddstart String to start a description
1510 :ddend String to end a description
1512 :splice When set to t, return only list body lines, don't wrap
1513 them into :[u/o]start and :[u/o]end. Default is nil.
1515 :istart String to start a list item
1516 :iend String to end a list item
1517 :isep String to separate items
1518 :lsep String to separate sublists
1520 :cboff String to insert for an unchecked checkbox
1521 :cbon String to insert for a checked checkbox"
1522 (interactive)
1523 (let* ((p params) sublist
1524 (splicep (plist-get p :splice))
1525 (ostart (plist-get p :ostart))
1526 (oend (plist-get p :oend))
1527 (ustart (plist-get p :ustart))
1528 (uend (plist-get p :uend))
1529 (dstart (plist-get p :dstart))
1530 (dend (plist-get p :dend))
1531 (dtstart (plist-get p :dtstart))
1532 (dtend (plist-get p :dtend))
1533 (ddstart (plist-get p :ddstart))
1534 (ddend (plist-get p :ddend))
1535 (istart (plist-get p :istart))
1536 (iend (plist-get p :iend))
1537 (isep (plist-get p :isep))
1538 (lsep (plist-get p :lsep))
1539 (cbon (plist-get p :cbon))
1540 (cboff (plist-get p :cboff)))
1541 (let ((wrapper
1542 (cond ((eq (car list) 'ordered)
1543 (concat ostart "\n%s" oend "\n"))
1544 ((eq (car list) 'unordered)
1545 (concat ustart "\n%s" uend "\n"))
1546 ((eq (car list) 'descriptive)
1547 (concat dstart "\n%s" dend "\n"))))
1548 rtn term defstart defend)
1549 (while (setq sublist (pop list))
1550 (cond ((symbolp sublist) nil)
1551 ((stringp sublist)
1552 (when (string-match "^\\(\\S-+\\)[ \t]+::" sublist)
1553 (setq term (org-trim (format (concat dtstart "%s" dtend)
1554 (match-string 1 sublist))))
1555 (setq sublist (concat ddstart
1556 (org-trim (substring sublist (match-end 0)))
1557 ddend)))
1558 (if (string-match "\\[CBON\\]" sublist)
1559 (setq sublist (replace-match cbon t t sublist)))
1560 (if (string-match "\\[CBOFF\\]" sublist)
1561 (setq sublist (replace-match cboff t t sublist)))
1562 (if (string-match "\\[-\\]" sublist)
1563 (setq sublist (replace-match "$\\boxminus$" t t sublist)))
1564 (setq rtn (concat rtn istart term sublist iend isep)))
1565 (t (setq rtn (concat rtn ;; previous list
1566 lsep ;; list separator
1567 (org-list-to-generic sublist p)
1568 lsep ;; list separator
1569 )))))
1570 (format wrapper rtn))))
1572 (defun org-list-to-latex (list &optional params)
1573 "Convert LIST into a LaTeX list.
1574 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
1575 with overruling parameters for `org-list-to-generic'."
1576 (org-list-to-generic
1577 list
1578 (org-combine-plists
1579 '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
1580 :ustart "\\begin{itemize}" :uend "\\end{itemize}"
1581 :dstart "\\begin{description}" :dend "\\end{description}"
1582 :dtstart "[" :dtend "]"
1583 :ddstart "" :ddend ""
1584 :istart "\\item " :iend ""
1585 :isep "\n" :lsep "\n"
1586 :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
1587 params)))
1589 (defun org-list-to-html (list &optional params)
1590 "Convert LIST into a HTML list.
1591 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
1592 with overruling parameters for `org-list-to-generic'."
1593 (org-list-to-generic
1594 list
1595 (org-combine-plists
1596 '(:splicep nil :ostart "<ol>" :oend "</ol>"
1597 :ustart "<ul>" :uend "</ul>"
1598 :dstart "<dl>" :dend "</dl>"
1599 :dtstart "<dt>" :dtend "</dt>"
1600 :ddstart "<dd>" :ddend "</dd>"
1601 :istart "<li>" :iend "</li>"
1602 :isep "\n" :lsep "\n"
1603 :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
1604 params)))
1606 (defun org-list-to-texinfo (list &optional params)
1607 "Convert LIST into a Texinfo list.
1608 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
1609 with overruling parameters for `org-list-to-generic'."
1610 (org-list-to-generic
1611 list
1612 (org-combine-plists
1613 '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
1614 :ustart "@enumerate" :uend "@end enumerate"
1615 :dstart "@table" :dend "@end table"
1616 :dtstart "@item " :dtend "\n"
1617 :ddstart "" :ddend ""
1618 :istart "@item\n" :iend ""
1619 :isep "\n" :lsep "\n"
1620 :cbon "@code{[X]}" :cboff "@code{[ ]}")
1621 params)))
1623 (provide 'org-list)
1625 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
1626 ;;; org-list.el ends here