Document prefix argument for the bulk scatter command
[org-mode.git] / lisp / org-list.el
blobabbaa5f98a7553c6ee40bec4e3c437a8cdc65605
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.3
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)
43 (defvar org-outline-regexp)
44 (defvar org-ts-regexp)
45 (defvar org-ts-regexp-both)
47 (declare-function org-invisible-p "org" ())
48 (declare-function org-on-heading-p "org" (&optional invisible-ok))
49 (declare-function outline-next-heading "outline" ())
50 (declare-function org-back-to-heading "org" (&optional invisible-ok))
51 (declare-function org-back-over-empty-lines "org" ())
52 (declare-function org-trim "org" (s))
53 (declare-function org-get-indentation "org" (&optional line))
54 (declare-function org-timer-item "org-timer" (&optional arg))
55 (declare-function org-timer-hms-to-secs "org-timer" (hms))
56 (declare-function org-combine-plists "org" (&rest plists))
57 (declare-function org-entry-get "org"
58 (pom property &optional inherit literal-nil))
59 (declare-function org-narrow-to-subtree "org" ())
60 (declare-function org-show-subtree "org" ())
61 (declare-function org-in-regexps-block-p "org"
62 (start-re end-re &optional bound))
63 (declare-function org-level-increment "org" ())
64 (declare-function org-at-heading-p "org" (&optional ignored))
65 (declare-function outline-previous-heading "outline" ())
66 (declare-function org-icompleting-read "org" (&rest args))
67 (declare-function org-time-string-to-seconds "org" (s))
69 (defgroup org-plain-lists nil
70 "Options concerning plain lists in Org-mode."
71 :tag "Org Plain lists"
72 :group 'org-structure)
74 (defcustom org-cycle-include-plain-lists t
75 "When t, make TAB cycle visibility on plain list items.
76 Cycling plain lists works only when the cursor is on a plain list
77 item. When the cursor is on an outline heading, plain lists are
78 treated as text. This is the most stable way of handling this,
79 which is why it is the default.
81 When this is the symbol `integrate', then during cycling, plain
82 list items will *temporarily* be interpreted as outline headlines
83 with a level given by 1000+i where i is the indentation of the
84 bullet. This setting can lead to strange effects when switching
85 visibility to `children', because the first \"child\" in a
86 subtree decides what children should be listed. If that first
87 \"child\" is a plain list item with an implied large level
88 number, all true children and grand children of the outline
89 heading will be exposed in a children' view."
90 :group 'org-plain-lists
91 :type '(choice
92 (const :tag "Never" nil)
93 (const :tag "With cursor in plain list (recommended)" t)
94 (const :tag "As children of outline headings" integrate)))
96 (defcustom org-list-demote-modify-bullet nil
97 "Default bullet type installed when demoting an item.
98 This is an association list, for each bullet type, this alist will point
99 to the bullet that should be used when this item is demoted.
100 For example,
102 (setq org-list-demote-modify-bullet
103 '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
105 will make
107 + Movies
108 + Silence of the Lambs
109 + My Cousin Vinny
110 + Books
111 + The Hunt for Red October
112 + The Road to Omaha
114 into
116 + Movies
117 - Silence of the Lambs
118 - My Cousin Vinny
119 + Books
120 - The Hunt for Red October
121 - The Road to Omaha"
122 :group 'org-plain-lists
123 :type '(repeat
124 (cons
125 (choice :tag "If the current bullet is "
126 (const "-")
127 (const "+")
128 (const "*")
129 (const "1.")
130 (const "1)"))
131 (choice :tag "demotion will change it to"
132 (const "-")
133 (const "+")
134 (const "*")
135 (const "1.")
136 (const "1)")))))
138 (defcustom org-plain-list-ordered-item-terminator t
139 "The character that makes a line with leading number an ordered list item.
140 Valid values are ?. and ?\). To get both terminators, use t. While
141 ?. may look nicer, it creates the danger that a line with leading
142 number may be incorrectly interpreted as an item. ?\) therefore is
143 the safe choice."
144 :group 'org-plain-lists
145 :type '(choice (const :tag "dot like in \"2.\"" ?.)
146 (const :tag "paren like in \"2)\"" ?\))
147 (const :tab "both" t)))
149 (defcustom org-list-two-spaces-after-bullet-regexp nil
150 "A regular expression matching bullets that should have 2 spaces after them.
151 When nil, no bullet will have two spaces after them.
152 When a string, it will be used as a regular expression. When the
153 bullet type of a list is changed, the new bullet type will be
154 matched against this regexp. If it matches, there will be two
155 spaces instead of one after the bullet in each item of the list."
156 :group 'org-plain-lists
157 :type '(choice
158 (const :tag "never" nil)
159 (regexp)))
161 (defcustom org-list-ending-method 'both
162 "Determine where plain lists should end.
163 Valid values are: `regexp', `indent' or `both'.
165 When set to `regexp', Org will look into two variables,
166 `org-empty-line-terminates-plain-lists' and the more general
167 `org-list-end-regexp', to determine what will end lists. This is
168 the fastest method.
170 When set to `indent', a list will end whenever a line following
171 an item, but not starting one, is less or equally indented than
174 When set to `both', each of the preceding methods is applied to
175 determine lists endings. This is the default method."
176 :group 'org-plain-lists
177 :type '(choice
178 (const :tag "With a regexp defining ending" regexp)
179 (const :tag "With indentation of regular (no bullet) text" indent)
180 (const :tag "With both methods" both)))
182 (defcustom org-empty-line-terminates-plain-lists nil
183 "Non-nil means an empty line ends all plain list levels.
184 This variable only makes sense if `org-list-ending-method' is set
185 to `regexp' or `both'. This is then equivalent to set
186 `org-list-end-regexp' to \"^[ \\t]*$\"."
187 :group 'org-plain-lists
188 :type 'boolean)
190 (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
191 "Regexp matching the end of all plain list levels.
192 It must start with \"^\" and end with \"\\n\". It defaults to 2
193 blank lines. `org-empty-line-terminates-plain-lists' has
194 precedence over it."
195 :group 'org-plain-lists
196 :type 'string)
198 (defcustom org-list-automatic-rules '((bullet . t)
199 (checkbox . t)
200 (indent . t)
201 (insert . t))
202 "Non-nil means apply set of rules when acting on lists.
203 By default, automatic actions are taken when using
204 \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
205 \\[org-shiftmetaright], \\[org-shiftmetaleft],
206 \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
207 \\[org-insert-todo-heading]. You can disable individually these
208 rules by setting them to nil. Valid rules are:
210 bullet when non-nil, cycling bullet do not allow lists at
211 column 0 to have * as a bullet and descriptions lists
212 to be numbered.
213 checkbox when non-nil, checkbox statistics is updated each time
214 you either insert a new checkbox or toggle a checkbox.
215 It also prevents from inserting a checkbox in a
216 description item.
217 indent when non-nil, indenting or outdenting list top-item
218 with its subtree will move the whole list and
219 outdenting a list whose bullet is * to column 0 will
220 change that bullet to -
221 insert when non-nil, trying to insert an item inside a block
222 will insert it right before the block instead of
223 throwing an error."
224 :group 'org-plain-lists
225 :type '(alist :tag "Sets of rules"
226 :key-type
227 (choice
228 (const :tag "Bullet" bullet)
229 (const :tag "Checkbox" checkbox)
230 (const :tag "Indent" indent)
231 (const :tag "Insert" insert))
232 :value-type
233 (boolean :tag "Activate" :value t)))
235 (defcustom org-hierarchical-checkbox-statistics t
236 "Non-nil means checkbox statistics counts only the state of direct children.
237 When nil, all boxes below the cookie are counted.
238 This can be set to nil on a per-node basis using a COOKIE_DATA property
239 with the word \"recursive\" in the value."
240 :group 'org-plain-lists
241 :type 'boolean)
243 (defcustom org-description-max-indent 20
244 "Maximum indentation for the second line of a description list.
245 When the indentation would be larger than this, it will become
246 5 characters instead."
247 :group 'org-plain-lists
248 :type 'integer)
250 (defcustom org-list-radio-list-templates
251 '((latex-mode "% BEGIN RECEIVE ORGLST %n
252 % END RECEIVE ORGLST %n
253 \\begin{comment}
254 #+ORGLST: SEND %n org-list-to-latex
256 \\end{comment}\n")
257 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
258 @c END RECEIVE ORGLST %n
259 @ignore
260 #+ORGLST: SEND %n org-list-to-texinfo
262 @end ignore\n")
263 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
264 <!-- END RECEIVE ORGLST %n -->
265 <!--
266 #+ORGLST: SEND %n org-list-to-html
268 -->\n"))
269 "Templates for radio lists in different major modes.
270 All occurrences of %n in a template will be replaced with the name of the
271 list, obtained by prompting the user."
272 :group 'org-plain-lists
273 :type '(repeat
274 (list (symbol :tag "Major mode")
275 (string :tag "Format"))))
277 ;;; Internal functions
279 (defun org-list-end-re ()
280 "Return the regex corresponding to the end of a list.
281 It depends on `org-empty-line-terminates-plain-lists'."
282 (if org-empty-line-terminates-plain-lists
283 "^[ \t]*\n"
284 org-list-end-regexp))
286 (defun org-item-re (&optional general)
287 "Return the correct regular expression for plain lists.
288 If GENERAL is non-nil, return the general regexp independent of the value
289 of `org-plain-list-ordered-item-terminator'."
290 (cond
291 ((or general (eq org-plain-list-ordered-item-terminator t))
292 "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
293 ((= org-plain-list-ordered-item-terminator ?.)
294 "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
295 ((= org-plain-list-ordered-item-terminator ?\))
296 "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
297 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
299 (defconst org-item-beginning-re (concat "^" (org-item-re))
300 "Regexp matching the beginning of a plain list item.")
302 (defun org-list-ending-between (min max &optional firstp)
303 "Find the position of a list ending between MIN and MAX, or nil.
304 This function looks for `org-list-end-re' outside a block.
306 If FIRSTP in non-nil, return the point at the beginning of the
307 nearest valid terminator from MIN. Otherwise, return the point at
308 the end of the nearest terminator from MAX."
309 (save-excursion
310 (let* ((start (if firstp min max))
311 (end (if firstp max min))
312 (search-fun (if firstp
313 #'org-search-forward-unenclosed
314 #'org-search-backward-unenclosed))
315 (list-end-p (progn
316 (goto-char start)
317 (funcall search-fun (org-list-end-re) end t))))
318 ;; Is there a valid list ending somewhere ?
319 (and list-end-p
320 ;; we want to be on the first line of the list ender
321 (match-beginning 0)))))
323 (defun org-list-maybe-skip-block (search limit)
324 "Return non-nil value if point is in a block, skipping it on the way.
325 It looks for the boundary of the block in SEARCH direction,
326 stopping at LIMIT."
327 (save-match-data
328 (let ((case-fold-search t)
329 (boundary (if (eq search 're-search-forward) 3 5)))
330 (when (save-excursion
331 (and (funcall search "^[ \t]*#\\+\\(begin\\|end\\)_" limit t)
332 (= (length (match-string 1)) boundary)))
333 ;; We're in a block: get out of it
334 (goto-char (match-beginning 0))))))
336 (defun org-list-search-unenclosed-generic (search re bound noerr)
337 "Search a string outside blocks and protected places.
338 Arguments SEARCH, RE, BOUND and NOERR are similar to those in
339 `search-forward', `search-backward', `re-search-forward' and
340 `re-search-backward'."
341 (catch 'exit
342 (let ((origin (point)))
343 (while t
344 ;; 1. No match: return to origin or bound, depending on NOERR.
345 (unless (funcall search re bound noerr)
346 (throw 'exit (and (goto-char (if (memq noerr '(t nil)) origin bound))
347 nil)))
348 ;; 2. Match not in block or protected: return point. Else
349 ;; skip the block and carry on.
350 (unless (or (get-text-property (match-beginning 0) 'org-protected)
351 (org-list-maybe-skip-block search bound))
352 (throw 'exit (point)))))))
354 (defun org-search-backward-unenclosed (regexp &optional bound noerror)
355 "Like `re-search-backward' but don't stop inside blocks or protected places.
356 Arguments REGEXP, BOUND and NOERROR are similar to those used in
357 `re-search-backward'."
358 (org-list-search-unenclosed-generic
359 #'re-search-backward regexp (or bound (point-min)) noerror))
361 (defun org-search-forward-unenclosed (regexp &optional bound noerror)
362 "Like `re-search-forward' but don't stop inside blocks or protected places.
363 Arguments REGEXP, BOUND and NOERROR are similar to those used in
364 `re-search-forward'."
365 (org-list-search-unenclosed-generic
366 #'re-search-forward regexp (or bound (point-max)) noerror))
368 (defun org-list-in-item-p-with-indent (limit)
369 "Is the cursor inside a plain list?
370 Plain lists are considered ending when a non-blank line is less
371 indented than the previous item within LIMIT."
372 (save-excursion
373 (beginning-of-line)
374 (cond
375 ;; do not start searching inside a block...
376 ((org-list-maybe-skip-block #'re-search-backward limit))
377 ;; ... or at a blank line
378 ((looking-at "^[ \t]*$")
379 (skip-chars-backward " \r\t\n")
380 (beginning-of-line)))
381 (beginning-of-line)
382 (or (org-at-item-p)
383 (let* ((case-fold-search t)
384 (ind-ref (org-get-indentation))
385 ;; Ensure there is at least an item above
386 (up-item-p (save-excursion
387 (org-search-backward-unenclosed
388 org-item-beginning-re limit t))))
389 (and up-item-p
390 (catch 'exit
391 (while t
392 (cond
393 ((org-at-item-p)
394 (throw 'exit (< (org-get-indentation) ind-ref)))
395 ((looking-at "^[ \t]*$")
396 (skip-chars-backward " \r\t\n")
397 (beginning-of-line))
398 ((looking-at "^[ \t]*#\\+end_")
399 (re-search-backward "^[ \t]*#\\+begin_"))
401 (setq ind-ref (min (org-get-indentation) ind-ref))
402 (forward-line -1))))))))))
404 (defun org-list-in-item-p-with-regexp (limit)
405 "Is the cursor inside a plain list?
406 Plain lists end when `org-list-end-regexp' is matched, or at a
407 blank line if `org-empty-line-terminates-plain-lists' is true.
409 Argument LIMIT specifies the upper-bound of the search."
410 (save-excursion
411 (let* ((actual-pos (goto-char (point-at-eol)))
412 ;; Moved to eol so current line can be matched by
413 ;; `org-item-re'.
414 (last-item-start (save-excursion
415 (org-search-backward-unenclosed
416 org-item-beginning-re limit t)))
417 (list-ender (org-list-ending-between
418 last-item-start actual-pos)))
419 ;; We are in a list when we are on an item line or when we can
420 ;; find an item before point and there is no valid list ender
421 ;; between it and the point.
422 (and last-item-start (not list-ender)))))
424 (defun org-list-top-point-with-regexp (limit)
425 "Return point at the top level item in a list.
426 Argument LIMIT specifies the upper-bound of the search.
428 List ending is determined by regexp. See
429 `org-list-ending-method'. for more information."
430 (save-excursion
431 (let ((pos (point-at-eol)))
432 ;; Is there some list above this one ? If so, go to its ending.
433 ;; Otherwise, go back to the heading above or bob.
434 (goto-char (or (org-list-ending-between limit pos) limit))
435 ;; From there, search down our list.
436 (org-search-forward-unenclosed org-item-beginning-re pos t)
437 (point-at-bol))))
439 (defun org-list-bottom-point-with-regexp (limit)
440 "Return point just before list ending.
441 Argument LIMIT specifies the lower-bound of the search.
443 List ending is determined by regexp. See
444 `org-list-ending-method'. for more information."
445 (save-excursion
446 (let ((pos (org-get-item-beginning)))
447 ;; The list ending is either first point matching
448 ;; `org-list-end-re', point at first white-line before next
449 ;; heading, or eob.
450 (or (org-list-ending-between (min pos limit) limit t) limit))))
452 (defun org-list-top-point-with-indent (limit)
453 "Return point at the top level in a list.
454 Argument LIMIT specifies the upper-bound of the search.
456 List ending is determined by indentation of text. See
457 `org-list-ending-method'. for more information."
458 (save-excursion
459 (let ((case-fold-search t))
460 (let ((item-ref (goto-char (org-get-item-beginning)))
461 (ind-ref 10000))
462 (forward-line -1)
463 (catch 'exit
464 (while t
465 (let ((ind (org-get-indentation)))
466 (cond
467 ((looking-at "^[ \t]*:END:")
468 (throw 'exit item-ref))
469 ((<= (point) limit)
470 (throw 'exit
471 (if (and (org-at-item-p) (< ind ind-ref))
472 (point-at-bol)
473 item-ref)))
474 ((looking-at "^[ \t]*$")
475 (skip-chars-backward " \r\t\n")
476 (beginning-of-line))
477 ((looking-at "^[ \t]*#\\+end_")
478 (re-search-backward "^[ \t]*#\\+begin_"))
479 ((not (org-at-item-p))
480 (setq ind-ref (min ind ind-ref))
481 (forward-line -1))
482 ((>= ind ind-ref)
483 (throw 'exit item-ref))
485 (setq item-ref (point-at-bol) ind-ref 10000)
486 (forward-line -1))))))))))
488 (defun org-list-bottom-point-with-indent (limit)
489 "Return point just before list ending or nil if not in a list.
490 Argument LIMIT specifies the lower-bound of the search.
492 List ending is determined by the indentation of text. See
493 `org-list-ending-method' for more information."
494 (save-excursion
495 (let ((ind-ref (progn
496 (goto-char (org-get-item-beginning))
497 (org-get-indentation)))
498 (case-fold-search t))
499 ;; do not start inside a block
500 (org-list-maybe-skip-block #'re-search-forward limit)
501 (beginning-of-line)
502 (catch 'exit
503 (while t
504 (skip-chars-forward " \t")
505 (let ((ind (org-get-indentation)))
506 (cond
507 ((or (>= (point) limit)
508 (looking-at ":END:"))
509 (throw 'exit (progn
510 ;; Ensure bottom is just after a
511 ;; non-blank line.
512 (skip-chars-backward " \r\t\n")
513 (min (point-max) (1+ (point-at-eol))))))
514 ((= (point) (point-at-eol))
515 (skip-chars-forward " \r\t\n")
516 (beginning-of-line))
517 ((org-at-item-p)
518 (setq ind-ref ind)
519 (forward-line 1))
520 ((<= ind ind-ref)
521 (throw 'exit (progn
522 ;; Again, ensure bottom is just after a
523 ;; non-blank line.
524 (skip-chars-backward " \r\t\n")
525 (min (point-max) (1+ (point-at-eol))))))
526 ((looking-at "#\\+begin_")
527 (re-search-forward "[ \t]*#\\+end_")
528 (forward-line 1))
529 (t (forward-line 1)))))))))
531 (defun org-list-at-regexp-after-bullet-p (regexp)
532 "Is point at a list item with REGEXP after bullet?"
533 (and (org-at-item-p)
534 (save-excursion
535 (goto-char (match-end 0))
536 ;; Ignore counter if any
537 (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
538 (goto-char (match-end 0)))
539 (looking-at regexp))))
541 (defun org-list-get-item-same-level (search-fun pos limit pre-move)
542 "Return point at the beginning of next item at the same level.
543 Search items using function SEARCH-FUN, from POS to LIMIT. It
544 uses PRE-MOVE before search. Return nil if no item was found."
545 (save-excursion
546 (goto-char pos)
547 (let* ((start (org-get-item-beginning))
548 (ind (progn (goto-char start) (org-get-indentation))))
549 ;; We don't want to match the current line.
550 (funcall pre-move)
551 ;; Skip any sublist on the way
552 (while (and (funcall search-fun org-item-beginning-re limit t)
553 (> (org-get-indentation) ind)))
554 (when (and (/= (point-at-bol) start) ; Have we moved ?
555 (= (org-get-indentation) ind))
556 (point-at-bol)))))
558 (defun org-list-separating-blank-lines-number (pos top bottom)
559 "Return number of blank lines that should separate items in list.
560 POS is the position of point to be considered.
562 TOP and BOTTOM are respectively position of list beginning and
563 list ending.
565 Assume point is at item's beginning. If the item is alone, apply
566 some heuristics to guess the result."
567 (save-excursion
568 (let ((insert-blank-p
569 (cdr (assq 'plain-list-item org-blank-before-new-entry)))
570 usr-blank)
571 (cond
572 ;; Trivial cases where there should be none.
573 ((or (and (not (eq org-list-ending-method 'indent))
574 org-empty-line-terminates-plain-lists)
575 (not insert-blank-p)) 0)
576 ;; When `org-blank-before-new-entry' says so, it is 1.
577 ((eq insert-blank-p t) 1)
578 ;; plain-list-item is 'auto. Count blank lines separating
579 ;; neighbours items in list.
580 (t (let ((next-p (org-get-next-item (point) bottom)))
581 (cond
582 ;; Is there a next item?
583 (next-p (goto-char next-p)
584 (org-back-over-empty-lines))
585 ;; Is there a previous item?
586 ((org-get-previous-item (point) top)
587 (org-back-over-empty-lines))
588 ;; User inserted blank lines, trust him
589 ((and (> pos (org-end-of-item-before-blank bottom))
590 (> (save-excursion
591 (goto-char pos)
592 (skip-chars-backward " \t")
593 (setq usr-blank (org-back-over-empty-lines))) 0))
594 usr-blank)
595 ;; Are there blank lines inside the item ?
596 ((save-excursion
597 (org-search-forward-unenclosed
598 "^[ \t]*$" (org-end-of-item-before-blank bottom) t)) 1)
599 ;; No parent: no blank line.
600 (t 0))))))))
602 (defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
603 "Insert a new list item at POS.
604 If POS is before first character after bullet of the item, the
605 new item will be created before the current one.
607 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
608 after the bullet. Cursor will be after this text once the
609 function ends."
610 (goto-char pos)
611 ;; Is point in a special block?
612 (when (org-in-regexps-block-p
613 "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
614 '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
615 (if (not (cdr (assq 'insert org-list-automatic-rules)))
616 ;; Rule in `org-list-automatic-rules' forbids insertion.
617 (error "Cannot insert item inside a block")
618 ;; Else, move before it prior to add a new item.
619 (end-of-line)
620 (re-search-backward "^[ \t]*#\\+\\(begin\\|BEGIN\\)_" nil t)
621 (end-of-line 0)))
622 (let* ((true-pos (point))
623 (top (org-list-top-point))
624 (bottom (copy-marker (org-list-bottom-point)))
625 (bullet (and (goto-char (org-get-item-beginning))
626 (org-list-bullet-string (org-get-bullet))))
627 (ind (org-get-indentation))
628 (before-p (progn
629 ;; Description item: text starts after colons.
630 (or (org-at-item-description-p)
631 ;; At a checkbox: text starts after it.
632 (org-at-item-checkbox-p)
633 ;; Otherwise, text starts after bullet.
634 (org-at-item-p))
635 (<= true-pos (match-end 0))))
636 (blank-lines-nb (org-list-separating-blank-lines-number
637 true-pos top bottom))
638 (insert-fun
639 (lambda (text)
640 ;; insert bullet above item in order to avoid bothering
641 ;; with possible blank lines ending last item.
642 (goto-char (org-get-item-beginning))
643 (org-indent-to-column ind)
644 (insert (concat bullet (when checkbox "[ ] ") after-bullet))
645 ;; Stay between after-bullet and before text.
646 (save-excursion
647 (insert (concat text (make-string (1+ blank-lines-nb) ?\n))))
648 (unless before-p
649 ;; store bottom: exchanging items doesn't change list
650 ;; bottom point but will modify marker anyway
651 (setq bottom (marker-position bottom))
652 (let ((col (current-column)))
653 (org-list-exchange-items
654 (org-get-item-beginning) (org-get-next-item (point) bottom)
655 bottom)
656 ;; recompute next-item: last sexp modified list
657 (goto-char (org-get-next-item (point) bottom))
658 (org-move-to-column col)))
659 ;; checkbox update might modify bottom point, so use a
660 ;; marker here
661 (setq bottom (copy-marker bottom))
662 (when checkbox (org-update-checkbox-count-maybe))
663 (org-list-repair nil top bottom))))
664 (goto-char true-pos)
665 (cond
666 (before-p (funcall insert-fun nil) t)
667 ;; Can't split item: insert bullet at the end of item.
668 ((not (org-get-alist-option org-M-RET-may-split-line 'item))
669 (funcall insert-fun nil) t)
670 ;; else, insert a new bullet along with everything from point
671 ;; down to last non-blank line of item.
673 (delete-horizontal-space)
674 ;; Get pos again in case previous command modified line.
675 (let* ((pos (point))
676 (end-before-blank (org-end-of-item-before-blank bottom))
677 (after-text
678 (when (< pos end-before-blank)
679 (prog1
680 (delete-and-extract-region pos end-before-blank)
681 ;; delete any blank line at and before point.
682 (beginning-of-line)
683 (while (looking-at "^[ \t]*$")
684 (delete-region (point-at-bol) (1+ (point-at-eol)))
685 (beginning-of-line 0))))))
686 (funcall insert-fun after-text) t)))))
688 (defvar org-last-indent-begin-marker (make-marker))
689 (defvar org-last-indent-end-marker (make-marker))
691 (defun org-list-indent-item-generic (arg no-subtree top bottom)
692 "Indent a local list item including its children.
693 When number ARG is a negative, item will be outdented, otherwise
694 it will be indented.
696 If a region is active, all items inside will be moved.
698 If NO-SUBTREE is non-nil, only indent the item itself, not its
699 children.
701 TOP and BOTTOM are respectively position at item beginning and at
702 item ending.
704 Return t if successful."
705 (let* ((regionp (org-region-active-p))
706 (rbeg (and regionp (region-beginning)))
707 (rend (and regionp (region-end))))
708 (cond
709 ((and regionp
710 (goto-char rbeg)
711 (not (org-search-forward-unenclosed org-item-beginning-re rend t)))
712 (error "No item in region"))
713 ((not (org-at-item-p))
714 (error "Not on an item"))
716 ;; Are we going to move the whole list?
717 (let* ((specialp (and (cdr (assq 'indent org-list-automatic-rules))
718 (not no-subtree)
719 (= top (point-at-bol)))))
720 ;; Determine begin and end points of zone to indent. If moving
721 ;; more than one item, ensure we keep them on subsequent moves.
722 (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
723 (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
724 (if regionp
725 (progn
726 (set-marker org-last-indent-begin-marker rbeg)
727 (set-marker org-last-indent-end-marker rend))
728 (set-marker org-last-indent-begin-marker (point-at-bol))
729 (set-marker org-last-indent-end-marker
730 (save-excursion
731 (cond
732 (specialp bottom)
733 (no-subtree (org-end-of-item-or-at-child bottom))
734 (t (org-get-end-of-item bottom)))))))
735 ;; Get everything ready
736 (let* ((beg (marker-position org-last-indent-begin-marker))
737 (end (marker-position org-last-indent-end-marker))
738 (struct (org-list-struct
739 beg end top (if specialp end bottom) (< arg 0)))
740 (origins (org-list-struct-origins struct))
741 (beg-item (assq beg struct)))
742 (cond
743 ;; Special case: moving top-item with indent rule
744 (specialp
745 (let* ((level-skip (org-level-increment))
746 (offset (if (< arg 0) (- level-skip) level-skip))
747 (top-ind (nth 1 beg-item)))
748 (if (< (+ top-ind offset) 0)
749 (error "Cannot outdent beyond margin")
750 ;; Change bullet if necessary
751 (when (and (= (+ top-ind offset) 0)
752 (string-match "*" (nth 2 beg-item)))
753 (setcdr beg-item (list (nth 1 beg-item)
754 (org-list-bullet-string "-"))))
755 ;; Shift ancestor
756 (let ((anc (car struct)))
757 (setcdr anc (list (+ (nth 1 anc) offset) "" nil)))
758 (org-list-struct-fix-struct struct origins)
759 (org-list-struct-apply-struct struct end))))
760 ;; Forbidden move
761 ((and (< arg 0)
762 (or (and no-subtree
763 (not regionp)
764 (org-list-struct-get-child beg-item struct))
765 (let ((last-item (save-excursion
766 (goto-char end)
767 (skip-chars-backward " \r\t\n")
768 (goto-char (org-get-item-beginning))
769 (org-list-struct-assoc-at-point))))
770 (org-list-struct-get-child last-item struct))))
771 (error "Cannot outdent an item without its children"))
772 ;; Normal shifting
774 (let* ((shifted-ori (if (< arg 0)
775 (org-list-struct-outdent beg end origins)
776 (org-list-struct-indent beg end origins struct))))
777 (org-list-struct-fix-struct struct shifted-ori)
778 (org-list-struct-apply-struct struct bottom))))))))))
780 ;;; Predicates
782 (defun org-in-item-p ()
783 "Is the cursor inside a plain list?
784 This checks `org-list-ending-method'."
785 (unless (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
786 (let* ((prev-head (save-excursion (outline-previous-heading)))
787 (bound (if prev-head
788 (or (save-excursion
789 (let ((case-fold-search t))
790 (re-search-backward "^[ \t]*:END:" prev-head t)))
791 prev-head)
792 (point-min))))
793 (cond
794 ((eq org-list-ending-method 'regexp)
795 (org-list-in-item-p-with-regexp bound))
796 ((eq org-list-ending-method 'indent)
797 (org-list-in-item-p-with-indent bound))
798 (t (and (org-list-in-item-p-with-regexp bound)
799 (org-list-in-item-p-with-indent bound)))))))
801 (defun org-list-first-item-p (top)
802 "Is this item the first item in a plain list?
803 Assume point is at an item.
805 TOP is the position of list's top-item."
806 (save-excursion
807 (beginning-of-line)
808 (let ((ind (org-get-indentation)))
809 (or (not (org-search-backward-unenclosed org-item-beginning-re top t))
810 (< (org-get-indentation) ind)))))
812 (defun org-at-item-p ()
813 "Is point in a line starting a hand-formatted item?"
814 (save-excursion
815 (beginning-of-line) (looking-at org-item-beginning-re)))
817 (defun org-at-item-bullet-p ()
818 "Is point at the bullet of a plain list item?"
819 (and (org-at-item-p)
820 (not (member (char-after) '(?\ ?\t)))
821 (< (point) (match-end 0))))
823 (defun org-at-item-timer-p ()
824 "Is point at a line starting a plain list item with a timer?"
825 (org-list-at-regexp-after-bullet-p
826 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
828 (defun org-at-item-description-p ()
829 "Is point at a description list item?"
830 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
832 (defun org-at-item-checkbox-p ()
833 "Is point at a line starting a plain-list item with a checklet?"
834 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
836 (defun org-checkbox-blocked-p ()
837 "Is the current checkbox blocked from for being checked now?
838 A checkbox is blocked if all of the following conditions are fulfilled:
840 1. The checkbox is not checked already.
841 2. The current entry has the ORDERED property set.
842 3. There is an unchecked checkbox in this entry before the current line."
843 (catch 'exit
844 (save-match-data
845 (save-excursion
846 (unless (org-at-item-checkbox-p) (throw 'exit nil))
847 (when (equal (match-string 1) "[X]")
848 ;; the box is already checked!
849 (throw 'exit nil))
850 (let ((end (point-at-bol)))
851 (condition-case nil (org-back-to-heading t)
852 (error (throw 'exit nil)))
853 (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
854 (when (org-search-forward-unenclosed
855 "^[ \t]*[-+*0-9.)]+[ \t]+\\(\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[[- ]\\]" end t)
856 (org-current-line)))))))
858 ;;; Navigate
860 ;; Every interactive navigation function is derived from a
861 ;; non-interactive one, which doesn't move point, assumes point is
862 ;; already in a list and doesn't compute list boundaries.
864 ;; If you plan to use more than one org-list function is some code,
865 ;; you should therefore first check if point is in a list with
866 ;; `org-in-item-p' or `org-at-item-p', then compute list boundaries
867 ;; with `org-list-top-point' and `org-list-bottom-point', and make use
868 ;; of non-interactive forms.
870 (defun org-list-top-point ()
871 "Return point at the top level in a list.
872 Assume point is in a list."
873 (let* ((prev-head (save-excursion (outline-previous-heading)))
874 (bound (if prev-head
875 (or (save-excursion
876 (let ((case-fold-search t))
877 (re-search-backward "^[ \t]*:END:" prev-head t)))
878 prev-head)
879 (point-min))))
880 (cond
881 ((eq org-list-ending-method 'regexp)
882 (org-list-top-point-with-regexp bound))
883 ((eq org-list-ending-method 'indent)
884 (org-list-top-point-with-indent bound))
885 (t (let ((top-re (org-list-top-point-with-regexp bound)))
886 (org-list-top-point-with-indent (or top-re bound)))))))
888 (defun org-list-bottom-point ()
889 "Return point just before list ending.
890 Assume point is in a list."
891 (let* ((next-head (save-excursion
892 (and (let ((outline-regexp org-outline-regexp))
893 ;; Use default regexp because folding
894 ;; changes OUTLINE-REGEXP.
895 (outline-next-heading)))))
896 (limit (or (save-excursion
897 (and (re-search-forward "^[ \t]*:END:" next-head t)
898 (point-at-bol)))
899 next-head
900 (point-max))))
901 (cond
902 ((eq org-list-ending-method 'regexp)
903 (org-list-bottom-point-with-regexp limit))
904 ((eq org-list-ending-method 'indent)
905 (org-list-bottom-point-with-indent limit))
906 (t (let ((bottom-re (org-list-bottom-point-with-regexp limit)))
907 (org-list-bottom-point-with-indent (or bottom-re limit)))))))
909 (defun org-get-item-beginning ()
910 "Return position of current item beginning."
911 (save-excursion
912 ;; possibly match current line
913 (end-of-line)
914 (org-search-backward-unenclosed org-item-beginning-re nil t)
915 (point-at-bol)))
917 (defun org-beginning-of-item ()
918 "Go to the beginning of the current hand-formatted item.
919 If the cursor is not in an item, throw an error."
920 (interactive)
921 (if (org-in-item-p)
922 (goto-char (org-get-item-beginning))
923 (error "Not in an item")))
925 (defun org-get-beginning-of-list (top)
926 "Return position of the first item of the current list or sublist.
927 TOP is the position at list beginning."
928 (save-excursion
929 (let (prev-p)
930 (while (setq prev-p (org-get-previous-item (point) top))
931 (goto-char prev-p))
932 (point-at-bol))))
934 (defun org-beginning-of-item-list ()
935 "Go to the beginning item of the current list or sublist.
936 Return an error if not in a list."
937 (interactive)
938 (if (org-in-item-p)
939 (goto-char (org-get-beginning-of-list (org-list-top-point)))
940 (error "Not in an item")))
942 (defun org-get-end-of-list (bottom)
943 "Return position at the end of the current list or sublist.
944 BOTTOM is the position at list ending."
945 (save-excursion
946 (goto-char (org-get-item-beginning))
947 (let ((ind (org-get-indentation)))
948 (while (and (/= (point) bottom)
949 (>= (org-get-indentation) ind))
950 (org-search-forward-unenclosed org-item-beginning-re bottom 'move))
951 (if (= (point) bottom) bottom (point-at-bol)))))
953 (defun org-end-of-item-list ()
954 "Go to the end of the current list or sublist.
955 If the cursor in not in an item, throw an error."
956 (interactive)
957 (if (org-in-item-p)
958 (goto-char (org-get-end-of-list (org-list-bottom-point)))
959 (error "Not in an item")))
961 (defun org-get-end-of-item (bottom)
962 "Return position at the end of the current item.
963 BOTTOM is the position at list ending."
964 (or (org-get-next-item (point) bottom)
965 (org-get-end-of-list bottom)))
967 (defun org-end-of-item ()
968 "Go to the end of the current hand-formatted item.
969 If the cursor is not in an item, throw an error."
970 (interactive)
971 (if (org-in-item-p)
972 (goto-char (org-get-end-of-item (org-list-bottom-point)))
973 (error "Not in an item")))
975 (defun org-end-of-item-or-at-child (bottom)
976 "Move to the end of the item, stops before the first child if any.
977 BOTTOM is the position at list ending."
978 (end-of-line)
979 (goto-char
980 (if (org-search-forward-unenclosed org-item-beginning-re bottom t)
981 (point-at-bol)
982 (org-get-end-of-item bottom))))
984 (defun org-end-of-item-before-blank (bottom)
985 "Return point at end of item, before any blank line.
986 Point returned is at eol.
988 BOTTOM is the position at list ending."
989 (save-excursion
990 (goto-char (org-get-end-of-item bottom))
991 (skip-chars-backward " \r\t\n")
992 (point-at-eol)))
994 (defun org-get-previous-item (pos limit)
995 "Return point of the previous item at the same level as POS.
996 Stop searching at LIMIT. Return nil if no item is found."
997 (org-list-get-item-same-level
998 #'org-search-backward-unenclosed pos limit #'beginning-of-line))
1000 (defun org-previous-item ()
1001 "Move to the beginning of the previous item.
1002 Item is at the same level in the current plain list. Error if not
1003 in a plain list, or if this is the first item in the list."
1004 (interactive)
1005 (if (not (org-in-item-p))
1006 (error "Not in an item")
1007 (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
1008 (if prev-p (goto-char prev-p) (error "On first item")))))
1010 (defun org-get-next-item (pos limit)
1011 "Return point of the next item at the same level as POS.
1012 Stop searching at LIMIT. Return nil if no item is found."
1013 (org-list-get-item-same-level
1014 #'org-search-forward-unenclosed pos limit #'end-of-line))
1016 (defun org-next-item ()
1017 "Move to the beginning of the next item.
1018 Item is at the same level in the current plain list. Error if not
1019 in a plain list, or if this is the last item in the list."
1020 (interactive)
1021 (if (not (org-in-item-p))
1022 (error "Not in an item")
1023 (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
1024 (if next-p (goto-char next-p) (error "On last item")))))
1026 ;;; Manipulate
1028 (defun org-list-exchange-items (beg-A beg-B bottom)
1029 "Swap item starting at BEG-A with item starting at BEG-B.
1030 Blank lines at the end of items are left in place. Assume BEG-A
1031 is lesser than BEG-B.
1033 BOTTOM is the position at list ending."
1034 (save-excursion
1035 (let* ((end-of-item-no-blank
1036 (lambda (pos)
1037 (goto-char pos)
1038 (goto-char (org-end-of-item-before-blank bottom))))
1039 (end-A-no-blank (funcall end-of-item-no-blank beg-A))
1040 (end-B-no-blank (funcall end-of-item-no-blank beg-B))
1041 (body-A (buffer-substring beg-A end-A-no-blank))
1042 (body-B (buffer-substring beg-B end-B-no-blank))
1043 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
1044 (goto-char beg-A)
1045 (delete-region beg-A end-B-no-blank)
1046 (insert (concat body-B between-A-no-blank-and-B body-A)))))
1048 (defun org-move-item-down ()
1049 "Move the plain list item at point down, i.e. swap with following item.
1050 Subitems (items with larger indentation) are considered part of the item,
1051 so this really moves item trees."
1052 (interactive)
1053 (if (not (org-at-item-p))
1054 (error "Not at an item")
1055 (let* ((pos (point))
1056 (col (current-column))
1057 (bottom (org-list-bottom-point))
1058 (actual-item (goto-char (org-get-item-beginning)))
1059 (next-item (org-get-next-item (point) bottom)))
1060 (if (not next-item)
1061 (progn
1062 (goto-char pos)
1063 (error "Cannot move this item further down"))
1064 (org-list-exchange-items actual-item next-item bottom)
1065 (org-list-repair nil nil bottom)
1066 (goto-char (org-get-next-item (point) bottom))
1067 (org-move-to-column col)))))
1069 (defun org-move-item-up ()
1070 "Move the plain list item at point up, i.e. swap with previous item.
1071 Subitems (items with larger indentation) are considered part of the item,
1072 so this really moves item trees."
1073 (interactive)
1074 (if (not (org-at-item-p))
1075 (error "Not at an item")
1076 (let* ((pos (point))
1077 (col (current-column))
1078 (top (org-list-top-point))
1079 (bottom (org-list-bottom-point))
1080 (actual-item (goto-char (org-get-item-beginning)))
1081 (prev-item (org-get-previous-item (point) top)))
1082 (if (not prev-item)
1083 (progn
1084 (goto-char pos)
1085 (error "Cannot move this item further up"))
1086 (org-list-exchange-items prev-item actual-item bottom)
1087 (org-list-repair nil top bottom)
1088 (org-move-to-column col)))))
1090 (defun org-insert-item (&optional checkbox)
1091 "Insert a new item at the current level.
1092 If cursor is before first character after bullet of the item, the
1093 new item will be created before the current one.
1095 If CHECKBOX is non-nil, add a checkbox next to the bullet.
1097 Return t when things worked, nil when we are not in an item, or
1098 item is invisible."
1099 (unless (or (not (org-in-item-p))
1100 (save-excursion
1101 (goto-char (org-get-item-beginning))
1102 (org-invisible-p)))
1103 (if (save-excursion
1104 (goto-char (org-get-item-beginning))
1105 (org-at-item-timer-p))
1106 ;; Timer list: delegate to `org-timer-item'.
1107 (progn (org-timer-item) t)
1108 ;; if we're in a description list, ask for the new term.
1109 (let ((desc-text (when (save-excursion
1110 (and (goto-char (org-get-item-beginning))
1111 (org-at-item-description-p)))
1112 (concat (read-string "Term: ") " :: "))))
1113 ;; Don't insert a checkbox if checkbox rule is applied and it
1114 ;; is a description item.
1115 (org-list-insert-item-generic
1116 (point) (and checkbox
1117 (or (not desc-text)
1118 (not (cdr (assq 'checkbox org-list-automatic-rules)))))
1119 desc-text)))))
1121 ;;; Structures
1123 ;; The idea behind structures is to avoid moving back and forth in the
1124 ;; buffer on costly operations like indenting or fixing bullets.
1126 ;; It achieves this by taking a snapshot of an interesting part of the
1127 ;; list, in the shape of an alist, using `org-list-struct'.
1129 ;; It then proceeds to changes directly on the alist, with the help of
1130 ;; and `org-list-struct-origins'. When those are done,
1131 ;; `org-list-struct-apply-struct' applies the changes to the buffer.
1133 (defun org-list-struct-assoc-at-point ()
1134 "Return the structure association at point.
1135 It is a cons-cell whose key is point and values are indentation,
1136 bullet string and bullet counter, if any."
1137 (save-excursion
1138 (beginning-of-line)
1139 (list (point-at-bol)
1140 (org-get-indentation)
1141 (progn
1142 (looking-at "^[ \t]*\\([-+*0-9.)]+[ \t]+\\)")
1143 (match-string 1))
1144 (progn
1145 (goto-char (match-end 0))
1146 (and (looking-at "\\[@\\(?:start:\\)?\\([0-9]+\\)\\]")
1147 (match-string 1))))))
1149 (defun org-list-struct (begin end top bottom &optional outdent)
1150 "Return the structure containing the list between BEGIN and END.
1151 A structure is an alist where key is point of item and values
1152 are, in that order, indentation, bullet string and value of
1153 counter, if any. A structure contains every list and sublist that
1154 has items between BEGIN and END along with their common ancestor.
1155 If no such ancestor can be found, the function will add a virtual
1156 ancestor at position 0.
1158 TOP and BOTTOM are respectively the position of list beginning
1159 and list ending.
1161 If OUTDENT is non-nil, it will also grab all of the parent list
1162 and the grand-parent. Setting OUTDENT to t is mandatory when next
1163 change is an outdent."
1164 (save-excursion
1165 (let* (struct
1166 (extend
1167 (lambda (struct)
1168 (let* ((ind-min (apply 'min (mapcar 'cadr struct)))
1169 (begin (caar struct))
1170 (end (caar (last struct)))
1171 pre-list post-list)
1172 (goto-char begin)
1173 ;; Find beginning of most outdented list (min list)
1174 (while (and (org-search-backward-unenclosed
1175 org-item-beginning-re top t)
1176 (>= (org-get-indentation) ind-min))
1177 (setq pre-list (cons (org-list-struct-assoc-at-point)
1178 pre-list)))
1179 ;; Now get the parent. If none, add a virtual ancestor
1180 (if (< (org-get-indentation) ind-min)
1181 (setq pre-list (cons (org-list-struct-assoc-at-point)
1182 pre-list))
1183 (setq pre-list (cons (list 0 (org-get-indentation) "" nil)
1184 pre-list)))
1185 ;; Find end of min list
1186 (goto-char end)
1187 (end-of-line)
1188 (while (and (org-search-forward-unenclosed
1189 org-item-beginning-re bottom 'move)
1190 (>= (org-get-indentation) ind-min))
1191 (setq post-list (cons (org-list-struct-assoc-at-point)
1192 post-list)))
1193 ;; Is list is malformed? If some items are less
1194 ;; indented that top-item, add them anyhow.
1195 (when (and (= (caar pre-list) 0) (< (point) bottom))
1196 (beginning-of-line)
1197 (while (org-search-forward-unenclosed
1198 org-item-beginning-re bottom t)
1199 (setq post-list (cons (org-list-struct-assoc-at-point)
1200 post-list))))
1201 (append pre-list struct (reverse post-list))))))
1202 ;; Here we start: first get the core zone...
1203 (goto-char end)
1204 (while (org-search-backward-unenclosed org-item-beginning-re begin t)
1205 (setq struct (cons (org-list-struct-assoc-at-point) struct)))
1206 ;; ... then, extend it to make it a structure...
1207 (let ((extended (funcall extend struct)))
1208 ;; ... twice when OUTDENT is non-nil and struct still can be
1209 ;; extended
1210 (if (and outdent (> (caar extended) 0))
1211 (funcall extend extended)
1212 extended)))))
1214 (defun org-list-struct-origins (struct)
1215 "Return an alist where key is item's position and value parent's.
1216 STRUCT is the list's structure looked up."
1217 (let* ((struct-rev (reverse struct))
1218 (acc (list (cons (nth 1 (car struct)) 0)))
1219 (prev-item (lambda (item)
1220 (car (nth 1 (member (assq item struct) struct-rev)))))
1221 (get-origins
1222 (lambda (item)
1223 (let* ((item-pos (car item))
1224 (ind (nth 1 item))
1225 (prev-ind (caar acc)))
1226 (cond
1227 ;; List closing.
1228 ((> prev-ind ind)
1229 (let ((current-origin (or (member (assq ind acc) acc)
1230 ;; needed if top-point is
1231 ;; not the most outdented
1232 (last acc))))
1233 (setq acc current-origin)
1234 (cons item-pos (cdar acc))))
1235 ;; New list
1236 ((< prev-ind ind)
1237 (let ((origin (funcall prev-item item-pos)))
1238 (setq acc (cons (cons ind origin) acc))
1239 (cons item-pos origin)))
1240 ;; Current list going on
1241 (t (cons item-pos (cdar acc))))))))
1242 (cons '(0 . 0) (mapcar get-origins (cdr struct)))))
1244 (defun org-list-struct-get-parent (item struct origins)
1245 "Return parent association of ITEM in STRUCT or nil.
1246 ORIGINS is the alist of parents. See `org-list-struct-origins'."
1247 (let* ((parent-pos (cdr (assq (car item) origins))))
1248 (when (> parent-pos 0) (assq parent-pos struct))))
1250 (defun org-list-struct-get-child (item struct)
1251 "Return child association of ITEM in STRUCT or nil."
1252 (let ((ind (nth 1 item))
1253 (next-item (cadr (member item struct))))
1254 (when (and next-item (> (nth 1 next-item) ind)) next-item)))
1256 (defun org-list-struct-fix-bul (struct origins)
1257 "Verify and correct bullets for every association in STRUCT.
1258 ORIGINS is the alist of parents. See `org-list-struct-origins'.
1260 This function modifies STRUCT."
1261 (let* (acc
1262 (init-bul (lambda (item)
1263 (let ((counter (nth 3 item))
1264 (bullet (org-list-bullet-string (nth 2 item))))
1265 (cond
1266 ((and (string-match "[0-9]+" bullet) counter)
1267 (replace-match counter nil nil bullet))
1268 ((string-match "[0-9]+" bullet)
1269 (replace-match "1" nil nil bullet))
1270 (t bullet)))))
1271 (set-bul (lambda (item bullet)
1272 (setcdr item (list (nth 1 item) bullet (nth 3 item)))))
1273 (get-bul (lambda (item bullet)
1274 (let* ((counter (nth 3 item)))
1275 (if (and counter (string-match "[0-9]+" bullet))
1276 (replace-match counter nil nil bullet)
1277 bullet))))
1278 (fix-bul
1279 (lambda (item) struct
1280 (let* ((parent (cdr (assq (car item) origins)))
1281 (orig-ref (assq parent acc)))
1282 (if orig-ref
1283 ;; Continuing previous list
1284 (let* ((prev-bul (cdr orig-ref))
1285 (new-bul (funcall get-bul item prev-bul)))
1286 (setcdr orig-ref (org-list-inc-bullet-maybe new-bul))
1287 (funcall set-bul item new-bul))
1288 ;; A new list is starting
1289 (let ((new-bul (funcall init-bul item)))
1290 (funcall set-bul item new-bul)
1291 (setq acc (cons (cons parent
1292 (org-list-inc-bullet-maybe new-bul))
1293 acc))))))))
1294 (mapc fix-bul (cdr struct))))
1296 (defun org-list-struct-fix-ind (struct origins)
1297 "Verify and correct indentation for every association in STRUCT.
1298 ORIGINS is the alist of parents. See `org-list-struct-origins'.
1300 This function modifies STRUCT."
1301 (let* ((headless (cdr struct))
1302 (ancestor (car struct))
1303 (top-ind (+ (nth 1 ancestor) (length (nth 2 ancestor))))
1304 (new-ind
1305 (lambda (item)
1306 (let* ((parent (org-list-struct-get-parent item headless origins)))
1307 (if parent
1308 ;; Indent like parent + length of parent's bullet
1309 (setcdr item (cons (+ (length (nth 2 parent)) (nth 1 parent))
1310 (cddr item)))
1311 ;; If no parent, indent like top-point
1312 (setcdr item (cons top-ind (cddr item))))))))
1313 (mapc new-ind headless)))
1315 (defun org-list-struct-fix-struct (struct origins)
1316 "Return STRUCT with correct bullets and indentation.
1317 ORIGINS is the alist of parents. See `org-list-struct-origins'.
1319 Only elements of STRUCT that have changed are returned."
1320 (let ((old (copy-alist struct)))
1321 (org-list-struct-fix-bul struct origins)
1322 (org-list-struct-fix-ind struct origins)
1323 (delq nil (mapcar (lambda (e) (when (not (equal (pop old) e)) e)) struct))))
1325 (defun org-list-struct-outdent (start end origins)
1326 "Outdent items in a structure.
1327 Items are indented when their key is between START, included, and
1328 END, excluded.
1330 ORIGINS is the alist of parents. See `org-list-struct-origins'.
1332 STRUCT is the concerned structure."
1333 (let* (acc
1334 (out (lambda (cell)
1335 (let* ((item (car cell))
1336 (parent (cdr cell)))
1337 (cond
1338 ;; Item not yet in zone: keep association
1339 ((< item start) cell)
1340 ;; Item out of zone: follow associations in acc
1341 ((>= item end)
1342 (let ((convert (assq parent acc)))
1343 (if convert (cons item (cdr convert)) cell)))
1344 ;; Item has no parent: error
1345 ((<= parent 0)
1346 (error "Cannot outdent top-level items"))
1347 ;; Parent is outdented: keep association
1348 ((>= parent start)
1349 (setq acc (cons (cons parent item) acc)) cell)
1351 ;; Parent isn't outdented: reparent to grand-parent
1352 (let ((grand-parent (cdr (assq parent origins))))
1353 (setq acc (cons (cons parent item) acc))
1354 (cons item grand-parent))))))))
1355 (mapcar out origins)))
1357 (defun org-list-struct-indent (start end origins struct)
1358 "Indent items in a structure.
1359 Items are indented when their key is between START, included, and
1360 END, excluded.
1362 ORIGINS is the alist of parents. See `org-list-struct-origins'.
1364 STRUCT is the concerned structure. It may be modified if
1365 `org-list-demote-modify-bullet' matches bullets between START and
1366 END."
1367 (let* (acc
1368 (orig-rev (reverse origins))
1369 (get-prev-item
1370 (lambda (cell parent)
1371 (car (rassq parent (cdr (memq cell orig-rev))))))
1372 (set-assoc
1373 (lambda (cell)
1374 (setq acc (cons cell acc)) cell))
1375 (change-bullet-maybe
1376 (lambda (item)
1377 (let* ((full-item (assq item struct))
1378 (item-bul (org-trim (nth 2 full-item)))
1379 (new-bul-p (cdr (assoc item-bul org-list-demote-modify-bullet))))
1380 (when new-bul-p
1381 ;; new bullet is stored without space to ensure item
1382 ;; will be modified
1383 (setcdr full-item
1384 (list (nth 1 full-item)
1385 new-bul-p
1386 (nth 3 full-item)))))))
1387 (ind
1388 (lambda (cell)
1389 (let* ((item (car cell))
1390 (parent (cdr cell)))
1391 (cond
1392 ;; Item not yet in zone: keep association
1393 ((< item start) cell)
1394 ((>= item end)
1395 ;; Item out of zone: follow associations in acc
1396 (let ((convert (assq parent acc)))
1397 (if convert (cons item (cdr convert)) cell)))
1399 ;; Item is in zone...
1400 (let ((prev (funcall get-prev-item cell parent)))
1401 ;; Check if bullet needs to be changed
1402 (funcall change-bullet-maybe item)
1403 (cond
1404 ;; First item indented but not parent: error
1405 ((and (or (not prev) (= prev 0)) (< parent start))
1406 (error "Cannot indent the first item of a list"))
1407 ;; First item and parent indented: keep same parent
1408 ((or (not prev) (= prev 0))
1409 (funcall set-assoc cell))
1410 ;; Previous item not indented: reparent to it
1411 ((< prev start)
1412 (funcall set-assoc (cons item prev)))
1413 ;; Previous item indented: reparent like it
1415 (funcall set-assoc (cons item
1416 (cdr (assq prev acc)))))))))))))
1417 (mapcar ind origins)))
1419 (defun org-list-struct-apply-struct (struct bottom)
1420 "Apply modifications to list so it mirrors STRUCT.
1421 BOTTOM is position at list ending.
1423 Initial position is restored after the changes."
1424 (let* ((pos (copy-marker (point)))
1425 (ancestor (caar struct))
1426 (modify
1427 (lambda (item)
1428 (goto-char (car item))
1429 (let* ((new-ind (nth 1 item))
1430 (new-bul (org-list-bullet-string (nth 2 item)))
1431 (old-ind (org-get-indentation))
1432 (old-bul (progn
1433 (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
1434 (match-string 1)))
1435 (old-body-ind (+ (length old-bul) old-ind))
1436 (new-body-ind (+ (length new-bul) new-ind)))
1437 ;; 1. Shift item's body
1438 (unless (= old-body-ind new-body-ind)
1439 (org-shift-item-indentation
1440 (- new-body-ind old-body-ind) bottom))
1441 ;; 2. Replace bullet
1442 (unless (equal new-bul old-bul)
1443 (save-excursion
1444 (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
1445 (replace-match new-bul nil nil nil 1)))
1446 ;; 3. Indent item to appropriate column
1447 (unless (= new-ind old-ind)
1448 (delete-region (point-at-bol)
1449 (progn
1450 (skip-chars-forward " \t")
1451 (point)))
1452 (indent-to new-ind)))))
1453 ;; Remove ancestor if it is left.
1454 (struct-to-apply (if (or (not ancestor) (= 0 ancestor))
1455 (cdr struct)
1456 struct)))
1457 ;; Apply changes from bottom to top
1458 (mapc modify (nreverse struct-to-apply))
1459 (goto-char pos)))
1461 ;;; Indentation
1463 (defun org-get-string-indentation (s)
1464 "What indentation has S due to SPACE and TAB at the beginning of the string?"
1465 (let ((n -1) (i 0) (w tab-width) c)
1466 (catch 'exit
1467 (while (< (setq n (1+ n)) (length s))
1468 (setq c (aref s n))
1469 (cond ((= c ?\ ) (setq i (1+ i)))
1470 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
1471 (t (throw 'exit t)))))
1474 (defun org-shift-item-indentation (delta bottom)
1475 "Shift the indentation in current item by DELTA.
1476 Sub-items are not moved.
1478 BOTTOM is position at list ending."
1479 (save-excursion
1480 (let ((beg (point-at-bol))
1481 (end (org-end-of-item-or-at-child bottom)))
1482 (beginning-of-line (unless (eolp) 0))
1483 (while (> (point) beg)
1484 (when (looking-at "[ \t]*\\S-")
1485 ;; this is not an empty line
1486 (let ((i (org-get-indentation)))
1487 (when (and (> i 0) (> (+ i delta) 0))
1488 (org-indent-line-to (+ i delta)))))
1489 (beginning-of-line 0)))))
1491 (defun org-outdent-item ()
1492 "Outdent a local list item, but not its children.
1493 If a region is active, all items inside will be moved."
1494 (interactive)
1495 (org-list-indent-item-generic
1496 -1 t (org-list-top-point) (org-list-bottom-point)))
1498 (defun org-indent-item ()
1499 "Indent a local list item, but not its children.
1500 If a region is active, all items inside will be moved."
1501 (interactive)
1502 (org-list-indent-item-generic
1503 1 t (org-list-top-point) (org-list-bottom-point)))
1505 (defun org-outdent-item-tree ()
1506 "Outdent a local list item including its children.
1507 If a region is active, all items inside will be moved."
1508 (interactive)
1509 (org-list-indent-item-generic
1510 -1 nil (org-list-top-point) (org-list-bottom-point)))
1512 (defun org-indent-item-tree ()
1513 "Indent a local list item including its children.
1514 If a region is active, all items inside will be moved."
1515 (interactive)
1516 (org-list-indent-item-generic
1517 1 nil (org-list-top-point) (org-list-bottom-point)))
1519 (defvar org-tab-ind-state)
1520 (defun org-cycle-item-indentation ()
1521 "Cycle levels of indentation of an empty item.
1522 The first run indent the item, if applicable. Subsequents runs
1523 outdent it at meaningful levels in the list. When done, item is
1524 put back at its original position with its original bullet.
1526 Return t at each successful move."
1527 (let ((org-adapt-indentation nil)
1528 (ind (org-get-indentation))
1529 (bottom (and (org-at-item-p) (org-list-bottom-point))))
1530 (when (and (or (org-at-item-description-p)
1531 (org-at-item-checkbox-p)
1532 (org-at-item-p))
1533 ;; Check that item is really empty
1534 (>= (match-end 0) (save-excursion
1535 (org-end-of-item-or-at-child bottom)
1536 (skip-chars-backward " \r\t\n")
1537 (point))))
1538 (setq this-command 'org-cycle-item-indentation)
1539 (let ((top (org-list-top-point)))
1540 ;; When in the middle of the cycle, try to outdent first. If it
1541 ;; fails, and point is still at initial position, indent. Else,
1542 ;; go back to original position.
1543 (if (eq last-command 'org-cycle-item-indentation)
1544 (cond
1545 ((ignore-errors (org-list-indent-item-generic -1 t top bottom)))
1546 ((and (= (org-get-indentation) (car org-tab-ind-state))
1547 (ignore-errors
1548 (org-list-indent-item-generic 1 t top bottom))))
1549 (t (back-to-indentation)
1550 (org-indent-to-column (car org-tab-ind-state))
1551 (end-of-line)
1552 (org-list-repair (cdr org-tab-ind-state))
1553 ;; Break cycle
1554 (setq this-command 'identity)))
1555 ;; If a cycle is starting, remember indentation and bullet,
1556 ;; then try to indent. If it fails, try to outdent.
1557 (setq org-tab-ind-state (cons ind (org-get-bullet)))
1558 (cond
1559 ((ignore-errors (org-list-indent-item-generic 1 t top bottom)))
1560 ((ignore-errors (org-list-indent-item-generic -1 t top bottom)))
1561 (t (error "Cannot move item")))))
1562 t)))
1564 ;;; Bullets
1566 (defun org-get-bullet ()
1567 "Return the bullet of the item at point.
1568 Assume cursor is at an item."
1569 (save-excursion
1570 (beginning-of-line)
1571 (and (looking-at "[ \t]*\\(\\S-+\\)") (match-string 1))))
1573 (defun org-list-bullet-string (bullet)
1574 "Return BULLET with the correct number of whitespaces.
1575 It determines the number of whitespaces to append by looking at
1576 `org-list-two-spaces-after-bullet-regexp'."
1577 (save-match-data
1578 (string-match "\\S-+\\([ \t]*\\)" bullet)
1579 (replace-match
1580 (save-match-data
1581 (concat
1583 ;; Do we need to concat another white space ?
1584 (when (and org-list-two-spaces-after-bullet-regexp
1585 (string-match org-list-two-spaces-after-bullet-regexp bullet))
1586 " ")))
1587 nil nil bullet 1)))
1589 (defun org-list-inc-bullet-maybe (bullet)
1590 "Increment BULLET if applicable."
1591 (if (string-match "[0-9]+" bullet)
1592 (replace-match
1593 (number-to-string (1+ (string-to-number (match-string 0 bullet))))
1594 nil nil bullet)
1595 bullet))
1597 (defun org-list-repair (&optional force-bullet top bottom)
1598 "Make sure all items are correctly indented, with the right bullet.
1599 This function scans the list at point, along with any sublist.
1601 If FORCE-BULLET is a string, ensure all items in list share this
1602 bullet, or a logical successor in the case of an ordered list.
1604 When non-nil, TOP and BOTTOM specify respectively position of
1605 list beginning and list ending.
1607 Item's body is not indented, only shifted with the bullet."
1608 (interactive)
1609 (unless (org-at-item-p) (error "This is not a list"))
1610 (let* ((bottom (or bottom (org-list-bottom-point)))
1611 (struct (org-list-struct
1612 (point-at-bol) (point-at-eol)
1613 (or top (org-list-top-point)) bottom))
1614 (origins (org-list-struct-origins struct))
1615 fixed-struct)
1616 (if (stringp force-bullet)
1617 (let ((begin (nth 1 struct)))
1618 (setcdr begin (list (nth 1 begin)
1619 (org-list-bullet-string force-bullet)
1620 (nth 3 begin)))
1621 (setq fixed-struct
1622 (cons begin (org-list-struct-fix-struct struct origins))))
1623 (setq fixed-struct (org-list-struct-fix-struct struct origins)))
1624 (org-list-struct-apply-struct fixed-struct bottom)))
1626 (defun org-cycle-list-bullet (&optional which)
1627 "Cycle through the different itemize/enumerate bullets.
1628 This cycle the entire list level through the sequence:
1630 `-' -> `+' -> `*' -> `1.' -> `1)'
1632 If WHICH is a valid string, use that as the new bullet. If WHICH
1633 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
1634 'previous, cycle backwards."
1635 (interactive "P")
1636 (save-excursion
1637 (let* ((top (org-list-top-point))
1638 (bullet (progn
1639 (goto-char (org-get-beginning-of-list top))
1640 (org-get-bullet)))
1641 (current (cond
1642 ((string-match "\\." bullet) "1.")
1643 ((string-match ")" bullet) "1)")
1644 (t bullet)))
1645 (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
1646 (bullet-list (append '("-" "+" )
1647 ;; *-bullets are not allowed at column 0
1648 (unless (and bullet-rule-p
1649 (looking-at "\\S-")) '("*"))
1650 ;; Description items cannot be numbered
1651 (unless (and bullet-rule-p
1652 (or (eq org-plain-list-ordered-item-terminator ?\))
1653 (org-at-item-description-p))) '("1."))
1654 (unless (and bullet-rule-p
1655 (or (eq org-plain-list-ordered-item-terminator ?.)
1656 (org-at-item-description-p))) '("1)"))))
1657 (len (length bullet-list))
1658 (item-index (- len (length (member current bullet-list))))
1659 (get-value (lambda (index) (nth (mod index len) bullet-list)))
1660 (new (cond
1661 ((member which bullet-list) which)
1662 ((numberp which) (funcall get-value which))
1663 ((eq 'previous which) (funcall get-value (1- item-index)))
1664 (t (funcall get-value (1+ item-index))))))
1665 (org-list-repair new top))))
1667 ;;; Checkboxes
1669 (defun org-toggle-checkbox (&optional toggle-presence)
1670 "Toggle the checkbox in the current line.
1671 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
1672 double prefix, set checkbox to [-].
1674 When there is an active region, toggle status or presence of the
1675 first checkbox there, and make every item inside have the
1676 same status or presence, respectively.
1678 If the cursor is in a headline, apply this to all checkbox items
1679 in the text below the heading, taking as reference the first item
1680 in subtree, ignoring drawers."
1681 (interactive "P")
1682 ;; Bounds is a list of type (beg end single-p) where single-p is t
1683 ;; when `org-toggle-checkbox' is applied to a single item. Only
1684 ;; toggles on single items will return errors.
1685 (let* ((bounds
1686 (cond
1687 ((org-region-active-p)
1688 (let ((rbeg (region-beginning))
1689 (rend (region-end)))
1690 (save-excursion
1691 (goto-char rbeg)
1692 (if (org-search-forward-unenclosed org-item-beginning-re rend 'move)
1693 (list (point-at-bol) rend nil)
1694 (error "No item in region")))))
1695 ((org-on-heading-p)
1696 ;; In this case, reference line is the first item in
1697 ;; subtree outside drawers
1698 (let ((pos (point))
1699 (limit (save-excursion (outline-next-heading) (point))))
1700 (save-excursion
1701 (goto-char limit)
1702 (org-search-backward-unenclosed ":END:" pos 'move)
1703 (org-search-forward-unenclosed
1704 org-item-beginning-re limit 'move)
1705 (list (point) limit nil))))
1706 ((org-at-item-p)
1707 (list (point-at-bol) (1+ (point-at-eol)) t))
1708 (t (error "Not at an item or heading, and no active region"))))
1709 (beg (car bounds))
1710 ;; marker is needed because deleting or inserting checkboxes
1711 ;; will change bottom point
1712 (end (copy-marker (nth 1 bounds)))
1713 (single-p (nth 2 bounds))
1714 (ref-presence (save-excursion
1715 (goto-char beg)
1716 (org-at-item-checkbox-p)))
1717 (ref-status (equal (match-string 1) "[X]"))
1718 (act-on-item
1719 (lambda (ref-pres ref-stat)
1720 (if (equal toggle-presence '(4))
1721 (cond
1722 ((and ref-pres (org-at-item-checkbox-p))
1723 (replace-match ""))
1724 ((and (not ref-pres)
1725 (not (org-at-item-checkbox-p))
1726 (org-at-item-p))
1727 (goto-char (match-end 0))
1728 ;; Ignore counter, if any
1729 (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
1730 (goto-char (match-end 0)))
1731 (let ((desc-p (and (org-at-item-description-p)
1732 (cdr (assq 'checkbox org-list-automatic-rules)))))
1733 (cond
1734 ((and single-p desc-p)
1735 (error "Cannot add a checkbox in a description list"))
1736 ((not desc-p) (insert "[ ] "))))))
1737 (let ((blocked (org-checkbox-blocked-p)))
1738 (cond
1739 ((and blocked single-p)
1740 (error "Checkbox blocked because of unchecked box in line %d" blocked))
1741 (blocked nil)
1742 ((org-at-item-checkbox-p)
1743 (replace-match
1744 (cond ((equal toggle-presence '(16)) "[-]")
1745 (ref-stat "[ ]")
1746 (t "[X]"))
1747 t t nil 1))))))))
1748 (save-excursion
1749 (goto-char beg)
1750 (while (< (point) end)
1751 (funcall act-on-item ref-presence ref-status)
1752 (org-search-forward-unenclosed org-item-beginning-re end 'move)))
1753 (org-update-checkbox-count-maybe)))
1755 (defun org-reset-checkbox-state-subtree ()
1756 "Reset all checkboxes in an entry subtree."
1757 (interactive "*")
1758 (save-restriction
1759 (save-excursion
1760 (org-narrow-to-subtree)
1761 (org-show-subtree)
1762 (goto-char (point-min))
1763 (let ((end (point-max)))
1764 (while (< (point) end)
1765 (when (org-at-item-checkbox-p)
1766 (replace-match "[ ]" t t nil 1))
1767 (beginning-of-line 2))))
1768 (org-update-checkbox-count-maybe)))
1770 (defvar org-checkbox-statistics-hook nil
1771 "Hook that is run whenever Org thinks checkbox statistics should be updated.
1772 This hook runs even if checkbox rule in
1773 `org-list-automatic-rules' does not apply, so it can be used to
1774 implement alternative ways of collecting statistics
1775 information.")
1777 (defun org-update-checkbox-count-maybe ()
1778 "Update checkbox statistics unless turned off by user."
1779 (when (cdr (assq 'checkbox org-list-automatic-rules))
1780 (org-update-checkbox-count))
1781 (run-hooks 'org-checkbox-statistics-hook))
1783 (defun org-update-checkbox-count (&optional all)
1784 "Update the checkbox statistics in the current section.
1785 This will find all statistic cookies like [57%] and [6/12] and update them
1786 with the current numbers. With optional prefix argument ALL, do this for
1787 the whole buffer."
1788 (interactive "P")
1789 (save-excursion
1790 (let ((cstat 0))
1791 (catch 'exit
1792 (while t
1793 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
1794 (beg (condition-case nil
1795 (progn (org-back-to-heading) (point))
1796 (error (point-min))))
1797 (end (copy-marker (save-excursion
1798 (outline-next-heading) (point))))
1799 (re-cookie "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
1800 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
1801 beg-cookie end-cookie is-percent c-on c-off lim new
1802 curr-ind next-ind continue-from startsearch list-beg list-end
1803 (recursive
1804 (or (not org-hierarchical-checkbox-statistics)
1805 (string-match "\\<recursive\\>"
1806 (or (ignore-errors
1807 (org-entry-get nil "COOKIE_DATA"))
1808 "")))))
1809 (goto-char end)
1810 ;; find each statistics cookie
1811 (while (and (org-search-backward-unenclosed re-cookie beg 'move)
1812 (not (save-match-data
1813 (and (org-on-heading-p)
1814 (string-match "\\<todo\\>"
1815 (downcase
1816 (or (org-entry-get
1817 nil "COOKIE_DATA")
1818 "")))))))
1819 (setq beg-cookie (match-beginning 1)
1820 end-cookie (match-end 1)
1821 cstat (+ cstat (if end-cookie 1 0))
1822 startsearch (point-at-eol)
1823 continue-from (match-beginning 0)
1824 is-percent (match-beginning 2)
1825 lim (cond
1826 ((org-on-heading-p) (outline-next-heading) (point))
1827 ;; Ensure many cookies in the same list won't imply
1828 ;; computing list boundaries as many times.
1829 ((org-at-item-p)
1830 (unless (and list-beg (>= (point) list-beg))
1831 (setq list-beg (org-list-top-point)
1832 list-end (copy-marker
1833 (org-list-bottom-point))))
1834 (org-get-end-of-item list-end))
1835 (t nil))
1836 c-on 0
1837 c-off 0)
1838 (when lim
1839 ;; find first checkbox for this cookie and gather
1840 ;; statistics from all that are at this indentation level
1841 (goto-char startsearch)
1842 (if (org-search-forward-unenclosed re-box lim t)
1843 (progn
1844 (beginning-of-line)
1845 (setq curr-ind (org-get-indentation))
1846 (setq next-ind curr-ind)
1847 (while (and (bolp) (org-at-item-p)
1848 (if recursive
1849 (<= curr-ind next-ind)
1850 (= curr-ind next-ind)))
1851 (when (org-at-item-checkbox-p)
1852 (if (member (match-string 1) '("[ ]" "[-]"))
1853 (setq c-off (1+ c-off))
1854 (setq c-on (1+ c-on))))
1855 (if (not recursive)
1856 ;; org-get-next-item goes through list-enders
1857 ;; with proper limit.
1858 (goto-char (or (org-get-next-item (point) lim) lim))
1859 (end-of-line)
1860 (when (org-search-forward-unenclosed
1861 org-item-beginning-re lim t)
1862 (beginning-of-line)))
1863 (setq next-ind (org-get-indentation)))))
1864 (goto-char continue-from)
1865 ;; update cookie
1866 (when end-cookie
1867 (setq new (if is-percent
1868 (format "[%d%%]" (/ (* 100 c-on)
1869 (max 1 (+ c-on c-off))))
1870 (format "[%d/%d]" c-on (+ c-on c-off))))
1871 (goto-char beg-cookie)
1872 (insert new)
1873 (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
1874 ;; update items checkbox if it has one
1875 (when (and (org-at-item-checkbox-p)
1876 (> (+ c-on c-off) 0))
1877 (setq beg-cookie (match-beginning 1)
1878 end-cookie (match-end 1))
1879 (delete-region beg-cookie end-cookie)
1880 (goto-char beg-cookie)
1881 (cond ((= c-off 0) (insert "[X]"))
1882 ((= c-on 0) (insert "[ ]"))
1883 (t (insert "[-]")))))
1884 (goto-char continue-from)))
1885 (unless (and all (outline-next-heading)) (throw 'exit nil))))
1886 (when (interactive-p)
1887 (message "Checkbox statistics updated %s (%d places)"
1888 (if all "in entire file" "in current outline entry") cstat)))))
1890 (defun org-get-checkbox-statistics-face ()
1891 "Select the face for checkbox statistics.
1892 The face will be `org-done' when all relevant boxes are checked.
1893 Otherwise it will be `org-todo'."
1894 (if (match-end 1)
1895 (if (equal (match-string 1) "100%")
1896 'org-checkbox-statistics-done
1897 'org-checkbox-statistics-todo)
1898 (if (and (> (match-end 2) (match-beginning 2))
1899 (equal (match-string 2) (match-string 3)))
1900 'org-checkbox-statistics-done
1901 'org-checkbox-statistics-todo)))
1903 ;;; Misc Tools
1905 (defun org-apply-on-list (function init-value &rest args)
1906 "Call FUNCTION on each item of the list at point.
1907 FUNCTION must be called with at least one argument: INIT-VALUE,
1908 that will contain the value returned by the function at the
1909 previous item, plus ARGS extra arguments.
1911 As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
1912 will return the number of items in the current list.
1914 Sublists of the list are skipped. Cursor is always at the
1915 beginning of the item."
1916 (let* ((pos (copy-marker (point)))
1917 (end (copy-marker (org-list-bottom-point)))
1918 (next-p (copy-marker (org-get-beginning-of-list (org-list-top-point))))
1919 (value init-value))
1920 (while (< next-p end)
1921 (goto-char next-p)
1922 (set-marker next-p (or (org-get-next-item (point) end) end))
1923 (setq value (apply function value args)))
1924 (goto-char pos)
1925 value))
1927 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
1928 "Sort plain list items.
1929 The cursor may be at any item of the list that should be sorted.
1930 Sublists are not sorted. Checkboxes, if any, are ignored.
1932 Sorting can be alphabetically, numerically, by date/time as given by
1933 a time stamp, by a property or by priority.
1935 Comparing entries ignores case by default. However, with an
1936 optional argument WITH-CASE, the sorting considers case as well.
1938 The command prompts for the sorting type unless it has been given
1939 to the function through the SORTING-TYPE argument, which needs to
1940 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
1941 meaning of each character:
1943 n Numerically, by converting the beginning of the item to a number.
1944 a Alphabetically. Only the first line of item is checked.
1945 t By date/time, either the first active time stamp in the entry, if
1946 any, or by the first inactive one. In a timer list, sort the timers.
1948 Capital letters will reverse the sort order.
1950 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
1951 function to be called with point at the beginning of the record.
1952 It must return either a string or a number that should serve as
1953 the sorting key for that record. It will then use COMPARE-FUNC to
1954 compare entries."
1955 (interactive "P")
1956 (let* ((case-func (if with-case 'identity 'downcase))
1957 (top (org-list-top-point))
1958 (bottom (org-list-bottom-point))
1959 (start (org-get-beginning-of-list top))
1960 (end (org-get-end-of-list bottom))
1961 (sorting-type
1962 (progn
1963 (message
1964 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
1965 (read-char-exclusive)))
1966 (getkey-func (and (= (downcase sorting-type) ?f)
1967 (org-icompleting-read "Sort using function: "
1968 obarray 'fboundp t nil nil)
1969 (intern getkey-func))))
1970 (message "Sorting items...")
1971 (save-restriction
1972 (narrow-to-region start end)
1973 (goto-char (point-min))
1974 (let* ((dcst (downcase sorting-type))
1975 (case-fold-search nil)
1976 (now (current-time))
1977 (sort-func (cond
1978 ((= dcst ?a) 'string<)
1979 ((= dcst ?f) compare-func)
1980 ((= dcst ?t) '<)
1981 (t nil)))
1982 (begin-record (lambda ()
1983 (skip-chars-forward " \r\t\n")
1984 (beginning-of-line)))
1985 (end-record (lambda ()
1986 (goto-char (org-end-of-item-before-blank end))))
1987 (value-to-sort
1988 (lambda ()
1989 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
1990 (cond
1991 ((= dcst ?n)
1992 (string-to-number (buffer-substring (match-end 0)
1993 (point-at-eol))))
1994 ((= dcst ?a)
1995 (buffer-substring (match-end 0) (point-at-eol)))
1996 ((= dcst ?t)
1997 (cond
1998 ;; If it is a timer list, convert timer to seconds
1999 ((org-at-item-timer-p)
2000 (org-timer-hms-to-secs (match-string 1)))
2001 ((or (org-search-forward-unenclosed org-ts-regexp
2002 (point-at-eol) t)
2003 (org-search-forward-unenclosed org-ts-regexp-both
2004 (point-at-eol) t))
2005 (org-time-string-to-seconds (match-string 0)))
2006 (t (org-float-time now))))
2007 ((= dcst ?f)
2008 (if getkey-func
2009 (let ((value (funcall getkey-func)))
2010 (if (stringp value)
2011 (funcall case-func value)
2012 value))
2013 (error "Invalid key function `%s'" getkey-func)))
2014 (t (error "Invalid sorting type `%c'" sorting-type)))))))
2015 (sort-subr (/= dcst sorting-type)
2016 begin-record
2017 end-record
2018 value-to-sort
2020 sort-func)
2021 (org-list-repair nil top bottom)
2022 (run-hooks 'org-after-sorting-entries-or-items-hook)
2023 (message "Sorting items...done")))))
2025 ;;; Send and receive lists
2027 (defun org-list-parse-list (&optional delete)
2028 "Parse the list at point and maybe DELETE it.
2029 Return a list containing first level items as strings and
2030 sublevels as a list of strings."
2031 (let* ((start (goto-char (org-list-top-point)))
2032 (end (org-list-bottom-point))
2033 output itemsep ltype)
2034 (while (org-search-forward-unenclosed org-item-beginning-re end t)
2035 (save-excursion
2036 (beginning-of-line)
2037 (setq ltype (cond ((org-looking-at-p "^[ \t]*[0-9]") 'ordered)
2038 ((org-at-item-description-p) 'descriptive)
2039 (t 'unordered))))
2040 (let* ((indent1 (org-get-indentation))
2041 (nextitem (or (org-get-next-item (point) end) end))
2042 (item (org-trim (buffer-substring (point)
2043 (org-end-of-item-or-at-child end))))
2044 (nextindent (if (= (point) end) 0 (org-get-indentation)))
2045 (item (if (string-match
2046 "^\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[\\([xX ]\\)\\]"
2047 item)
2048 (replace-match (if (equal (match-string 1 item) " ")
2049 "CBOFF"
2050 "CBON")
2051 t nil item 1)
2052 item)))
2053 (push item output)
2054 (when (> nextindent indent1)
2055 (save-restriction
2056 (narrow-to-region (point) nextitem)
2057 (push (org-list-parse-list) output)))))
2058 (when delete
2059 (delete-region start end)
2060 (save-match-data
2061 (when (and (not (eq org-list-ending-method 'indent))
2062 (looking-at (org-list-end-re)))
2063 (replace-match "\n"))))
2064 (setq output (nreverse output))
2065 (push ltype output)))
2067 (defun org-list-make-subtree ()
2068 "Convert the plain list at point into a subtree."
2069 (interactive)
2070 (if (not (org-in-item-p))
2071 (error "Not in a list")
2072 (let ((list (org-list-parse-list t)) nstars)
2073 (save-excursion
2074 (if (ignore-errors
2075 (org-back-to-heading))
2076 (progn (looking-at org-complex-heading-regexp)
2077 (setq nstars (length (match-string 1))))
2078 (setq nstars 0)))
2079 (org-list-make-subtrees list (1+ nstars)))))
2081 (defun org-list-make-subtrees (list level)
2082 "Convert LIST into subtrees starting at LEVEL."
2083 (if (symbolp (car list))
2084 (org-list-make-subtrees (cdr list) level)
2085 (mapcar (lambda (item)
2086 (if (stringp item)
2087 (insert (make-string
2088 (if org-odd-levels-only
2089 (1- (* 2 level)) level) ?*) " " item "\n")
2090 (org-list-make-subtrees item (1+ level))))
2091 list)))
2093 (defun org-list-insert-radio-list ()
2094 "Insert a radio list template appropriate for this major mode."
2095 (interactive)
2096 (let* ((e (assq major-mode org-list-radio-list-templates))
2097 (txt (nth 1 e))
2098 name pos)
2099 (unless e (error "No radio list setup defined for %s" major-mode))
2100 (setq name (read-string "List name: "))
2101 (while (string-match "%n" txt)
2102 (setq txt (replace-match name t t txt)))
2103 (or (bolp) (insert "\n"))
2104 (setq pos (point))
2105 (insert txt)
2106 (goto-char pos)))
2108 (defun org-list-send-list (&optional maybe)
2109 "Send a transformed version of this list to the receiver position.
2110 With argument MAYBE, fail quietly if no transformation is defined for
2111 this list."
2112 (interactive)
2113 (catch 'exit
2114 (unless (org-at-item-p) (error "Not at a list item"))
2115 (save-excursion
2116 (re-search-backward "#\\+ORGLST" nil t)
2117 (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
2118 (if maybe
2119 (throw 'exit nil)
2120 (error "Don't know how to transform this list"))))
2121 (let* ((name (match-string 1))
2122 (transform (intern (match-string 2)))
2123 (bottom-point
2124 (save-excursion
2125 (re-search-forward
2126 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
2127 (match-beginning 0)))
2128 (top-point
2129 (progn
2130 (re-search-backward "#\\+ORGLST" nil t)
2131 (re-search-forward org-item-beginning-re bottom-point t)
2132 (match-beginning 0)))
2133 (list (save-restriction
2134 (narrow-to-region top-point bottom-point)
2135 (org-list-parse-list)))
2136 beg txt)
2137 (unless (fboundp transform)
2138 (error "No such transformation function %s" transform))
2139 (let ((txt (funcall transform list)))
2140 ;; Find the insertion place
2141 (save-excursion
2142 (goto-char (point-min))
2143 (unless (re-search-forward
2144 (concat "BEGIN RECEIVE ORGLST +"
2145 name
2146 "\\([ \t]\\|$\\)") nil t)
2147 (error "Don't know where to insert translated list"))
2148 (goto-char (match-beginning 0))
2149 (beginning-of-line 2)
2150 (setq beg (point))
2151 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
2152 (error "Cannot find end of insertion region"))
2153 (delete-region beg (point-at-bol))
2154 (goto-char beg)
2155 (insert txt "\n")))
2156 (message "List converted and installed at receiver location"))))
2158 (defun org-list-to-generic (list params)
2159 "Convert a LIST parsed through `org-list-parse-list' to other formats.
2160 Valid parameters PARAMS are
2162 :ustart String to start an unordered list
2163 :uend String to end an unordered list
2165 :ostart String to start an ordered list
2166 :oend String to end an ordered list
2168 :dstart String to start a descriptive list
2169 :dend String to end a descriptive list
2170 :dtstart String to start a descriptive term
2171 :dtend String to end a descriptive term
2172 :ddstart String to start a description
2173 :ddend String to end a description
2175 :splice When set to t, return only list body lines, don't wrap
2176 them into :[u/o]start and :[u/o]end. Default is nil.
2178 :istart String to start a list item
2179 :iend String to end a list item
2180 :isep String to separate items
2181 :lsep String to separate sublists
2183 :cboff String to insert for an unchecked checkbox
2184 :cbon String to insert for a checked checkbox"
2185 (interactive)
2186 (let* ((p params) sublist
2187 (splicep (plist-get p :splice))
2188 (ostart (plist-get p :ostart))
2189 (oend (plist-get p :oend))
2190 (ustart (plist-get p :ustart))
2191 (uend (plist-get p :uend))
2192 (dstart (plist-get p :dstart))
2193 (dend (plist-get p :dend))
2194 (dtstart (plist-get p :dtstart))
2195 (dtend (plist-get p :dtend))
2196 (ddstart (plist-get p :ddstart))
2197 (ddend (plist-get p :ddend))
2198 (istart (plist-get p :istart))
2199 (iend (plist-get p :iend))
2200 (isep (plist-get p :isep))
2201 (lsep (plist-get p :lsep))
2202 (cbon (plist-get p :cbon))
2203 (cboff (plist-get p :cboff)))
2204 (let ((wrapper
2205 (cond ((eq (car list) 'ordered)
2206 (concat ostart "\n%s" oend "\n"))
2207 ((eq (car list) 'unordered)
2208 (concat ustart "\n%s" uend "\n"))
2209 ((eq (car list) 'descriptive)
2210 (concat dstart "\n%s" dend "\n"))))
2211 rtn term defstart defend)
2212 (while (setq sublist (pop list))
2213 (cond ((symbolp sublist) nil)
2214 ((stringp sublist)
2215 (when (string-match "^\\(.*\\)[ \t]+::" sublist)
2216 (setq term (org-trim (format (concat dtstart "%s" dtend)
2217 (match-string 1 sublist))))
2218 (setq sublist (concat ddstart
2219 (org-trim (substring sublist
2220 (match-end 0)))
2221 ddend)))
2222 (if (string-match "\\[CBON\\]" sublist)
2223 (setq sublist (replace-match cbon t t sublist)))
2224 (if (string-match "\\[CBOFF\\]" sublist)
2225 (setq sublist (replace-match cboff t t sublist)))
2226 (if (string-match "\\[-\\]" sublist)
2227 (setq sublist (replace-match "$\\boxminus$" t t sublist)))
2228 (setq rtn (concat rtn istart term sublist iend isep)))
2229 (t (setq rtn (concat rtn ;; previous list
2230 lsep ;; list separator
2231 (org-list-to-generic sublist p)
2232 lsep ;; list separator
2233 )))))
2234 (format wrapper rtn))))
2236 (defun org-list-to-latex (list &optional params)
2237 "Convert LIST into a LaTeX list.
2238 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
2239 with overruling parameters for `org-list-to-generic'."
2240 (org-list-to-generic
2241 list
2242 (org-combine-plists
2243 '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
2244 :ustart "\\begin{itemize}" :uend "\\end{itemize}"
2245 :dstart "\\begin{description}" :dend "\\end{description}"
2246 :dtstart "[" :dtend "]"
2247 :ddstart "" :ddend ""
2248 :istart "\\item " :iend ""
2249 :isep "\n" :lsep "\n"
2250 :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
2251 params)))
2253 (defun org-list-to-html (list &optional params)
2254 "Convert LIST into a HTML list.
2255 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
2256 with overruling parameters for `org-list-to-generic'."
2257 (org-list-to-generic
2258 list
2259 (org-combine-plists
2260 '(:splicep nil :ostart "<ol>" :oend "</ol>"
2261 :ustart "<ul>" :uend "</ul>"
2262 :dstart "<dl>" :dend "</dl>"
2263 :dtstart "<dt>" :dtend "</dt>"
2264 :ddstart "<dd>" :ddend "</dd>"
2265 :istart "<li>" :iend "</li>"
2266 :isep "\n" :lsep "\n"
2267 :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
2268 params)))
2270 (defun org-list-to-texinfo (list &optional params)
2271 "Convert LIST into a Texinfo list.
2272 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
2273 with overruling parameters for `org-list-to-generic'."
2274 (org-list-to-generic
2275 list
2276 (org-combine-plists
2277 '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
2278 :ustart "@enumerate" :uend "@end enumerate"
2279 :dstart "@table" :dend "@end table"
2280 :dtstart "@item " :dtend "\n"
2281 :ddstart "" :ddend ""
2282 :istart "@item\n" :iend ""
2283 :isep "\n" :lsep "\n"
2284 :cbon "@code{[X]}" :cboff "@code{[ ]}")
2285 params)))
2287 (provide 'org-list)
2289 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
2290 ;;; org-list.el ends here