Merge branch 'master' of orgmode.org:org-mode
[org-mode.git] / lisp / org-list.el
blobfa2d3f9f7ed753852772e45f199df6892ab7e9b7
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.6
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 ;; The fundamental idea behind lists work is to use structures.
33 ;; A structure is a snapshot of the list, in the shape of data tree
34 ;; (see `org-list-struct').
36 ;; Once the list structure is stored, it is possible to make changes
37 ;; directly on it or get useful information about the list, with the
38 ;; two helper functions, namely `org-list-parents-alist' and
39 ;; `org-list-prevs-alist', and using accessors or methods.
41 ;; Structure is eventually applied to the buffer with
42 ;; `org-list-write-struct'. This function repairs (bullets,
43 ;; indentation, checkboxes) the structure before applying it. It
44 ;; should be called near the end of any function working on
45 ;; structures.
47 ;; Thus, a function applying to lists should usually follow this
48 ;; template:
50 ;; 1. Verify point is in a list and grab item beginning (with the same
51 ;; function `org-in-item-p'). If the function requires the cursor
52 ;; to be at item's bullet, `org-at-item-p' is more selective. If
53 ;; the cursor is amidst the buffer, it is possible to find the
54 ;; closest item with `org-list-search-backward', or
55 ;; `org-list-search-forward', applied to `org-item-beginning-re'.
57 ;; 2. Get list structure with `org-list-struct'.
59 ;; 3. Compute one, or both, helper functions,
60 ;; (`org-list-parents-alist', `org-list-prevs-alist') depending on
61 ;; needed accessors.
63 ;; 4. Proceed with the modifications, using methods and accessors.
65 ;; 5. Verify and apply structure to buffer, using
66 ;; `org-list-write-struct'. Possibly use
67 ;; `org-update-checkbox-count-maybe' if checkboxes might have been
68 ;; modified.
70 ;; Computing a list structure can be a costly operation on huge lists
71 ;; (a few thousand lines long). Thus, code should follow the rule :
72 ;; "collect once, use many". As a corollary, it is usally a bad idea
73 ;; to use directly an interactive function inside the code, as those,
74 ;; being independant entities, read the whole list structure another
75 ;; time.
77 ;;; Code:
79 (eval-when-compile
80 (require 'cl))
81 (require 'org-macs)
82 (require 'org-compat)
84 (defvar org-M-RET-may-split-line)
85 (defvar org-auto-align-tags)
86 (defvar org-blank-before-new-entry)
87 (defvar org-clock-string)
88 (defvar org-closed-string)
89 (defvar org-deadline-string)
90 (defvar org-description-max-indent)
91 (defvar org-drawers)
92 (defvar org-odd-levels-only)
93 (defvar org-scheduled-string)
94 (defvar org-ts-regexp)
95 (defvar org-ts-regexp-both)
97 (declare-function org-at-heading-p "org" (&optional ignored))
98 (declare-function org-before-first-heading-p "org" ())
99 (declare-function org-back-over-empty-lines "org" ())
100 (declare-function org-back-to-heading "org" (&optional invisible-ok))
101 (declare-function org-combine-plists "org" (&rest plists))
102 (declare-function org-count "org" (cl-item cl-seq))
103 (declare-function org-current-level "org" ())
104 (declare-function org-entry-get "org"
105 (pom property &optional inherit literal-nil))
106 (declare-function org-fix-tags-on-the-fly "org" ())
107 (declare-function org-get-indentation "org" (&optional line))
108 (declare-function org-icompleting-read "org" (&rest args))
109 (declare-function org-in-block-p "org" (names))
110 (declare-function org-in-regexp "org" (re &optional nlines visually))
111 (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
112 (declare-function org-inlinetask-goto-end "org-inlinetask" ())
113 (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
114 (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
115 (declare-function org-level-increment "org" ())
116 (declare-function org-narrow-to-subtree "org" ())
117 (declare-function org-on-heading-p "org" (&optional invisible-ok))
118 (declare-function org-previous-line-empty-p "org" ())
119 (declare-function org-remove-if "org" (predicate seq))
120 (declare-function org-reduced-level "org" (L))
121 (declare-function org-show-subtree "org" ())
122 (declare-function org-time-string-to-seconds "org" (s))
123 (declare-function org-timer-hms-to-secs "org-timer" (hms))
124 (declare-function org-timer-item "org-timer" (&optional arg))
125 (declare-function org-trim "org" (s))
126 (declare-function org-uniquify "org" (list))
127 (declare-function outline-invisible-p "outline" (&optional pos))
128 (declare-function outline-flag-region "outline" (from to flag))
129 (declare-function outline-next-heading "outline" ())
130 (declare-function outline-previous-heading "outline" ())
132 ;;; Configuration variables
134 (defgroup org-plain-lists nil
135 "Options concerning plain lists in Org-mode."
136 :tag "Org Plain lists"
137 :group 'org-structure)
139 (defcustom org-cycle-include-plain-lists t
140 "When t, make TAB cycle visibility on plain list items.
141 Cycling plain lists works only when the cursor is on a plain list
142 item. When the cursor is on an outline heading, plain lists are
143 treated as text. This is the most stable way of handling this,
144 which is why it is the default.
146 When this is the symbol `integrate', then during cycling, plain
147 list items will *temporarily* be interpreted as outline headlines
148 with a level given by 1000+i where i is the indentation of the
149 bullet. This setting can lead to strange effects when switching
150 visibility to `children', because the first \"child\" in a
151 subtree decides what children should be listed. If that first
152 \"child\" is a plain list item with an implied large level
153 number, all true children and grand children of the outline
154 heading will be exposed in a children' view."
155 :group 'org-plain-lists
156 :type '(choice
157 (const :tag "Never" nil)
158 (const :tag "With cursor in plain list (recommended)" t)
159 (const :tag "As children of outline headings" integrate)))
161 (defcustom org-list-demote-modify-bullet nil
162 "Default bullet type installed when demoting an item.
163 This is an association list, for each bullet type, this alist will point
164 to the bullet that should be used when this item is demoted.
165 For example,
167 (setq org-list-demote-modify-bullet
168 '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
170 will make
172 + Movies
173 + Silence of the Lambs
174 + My Cousin Vinny
175 + Books
176 + The Hunt for Red October
177 + The Road to Omaha
179 into
181 + Movies
182 - Silence of the Lambs
183 - My Cousin Vinny
184 + Books
185 - The Hunt for Red October
186 - The Road to Omaha"
187 :group 'org-plain-lists
188 :type '(repeat
189 (cons
190 (choice :tag "If the current bullet is "
191 (const "-")
192 (const "+")
193 (const "*")
194 (const "1.")
195 (const "1)"))
196 (choice :tag "demotion will change it to"
197 (const "-")
198 (const "+")
199 (const "*")
200 (const "1.")
201 (const "1)")))))
203 (defcustom org-plain-list-ordered-item-terminator t
204 "The character that makes a line with leading number an ordered list item.
205 Valid values are ?. and ?\). To get both terminators, use t."
206 :group 'org-plain-lists
207 :type '(choice (const :tag "dot like in \"2.\"" ?.)
208 (const :tag "paren like in \"2)\"" ?\))
209 (const :tab "both" t)))
211 (defcustom org-alphabetical-lists nil
212 "Non-nil means single character alphabetical bullets are allowed.
213 Both uppercase and lowercase are handled. Lists with more than
214 26 items will fallback to standard numbering. Alphabetical
215 counters like \"[@c]\" will be recognized."
216 :group 'org-plain-lists
217 :type 'boolean)
219 (defcustom org-list-two-spaces-after-bullet-regexp nil
220 "A regular expression matching bullets that should have 2 spaces after them.
221 When nil, no bullet will have two spaces after them. When
222 a string, it will be used as a regular expression. When the
223 bullet type of a list is changed, the new bullet type will be
224 matched against this regexp. If it matches, there will be two
225 spaces instead of one after the bullet in each item of the list."
226 :group 'org-plain-lists
227 :type '(choice
228 (const :tag "never" nil)
229 (regexp)))
231 (defcustom org-list-ending-method 'both
232 "Determine where plain lists should end.
233 Valid values are: `regexp', `indent' or `both'.
235 When set to `regexp', Org will look into two variables,
236 `org-empty-line-terminates-plain-lists' and the more general
237 `org-list-end-regexp', to determine what will end lists.
239 When set to `indent', a list will end whenever a line following
240 an item, but not starting one, is less or equally indented than
241 the first item of the list.
243 When set to `both', each of the preceding methods is applied to
244 determine lists endings. This is the default method."
245 :group 'org-plain-lists
246 :type '(choice
247 (const :tag "With a regexp defining ending" regexp)
248 (const :tag "With indentation of regular (no bullet) text" indent)
249 (const :tag "With both methods" both)))
251 (defcustom org-empty-line-terminates-plain-lists nil
252 "Non-nil means an empty line ends all plain list levels.
253 This variable only makes sense if `org-list-ending-method' is set
254 to `regexp' or `both'. This is then equivalent to set
255 `org-list-end-regexp' to \"^[ \\t]*$\"."
256 :group 'org-plain-lists
257 :type 'boolean)
259 (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
260 "Regexp matching the end of all plain list levels.
261 It must start with \"^\" and end with \"\\n\". It defaults to 2
262 blank lines. `org-empty-line-terminates-plain-lists' has
263 precedence over it."
264 :group 'org-plain-lists
265 :type 'string)
267 (defcustom org-list-automatic-rules '((bullet . t)
268 (checkbox . t)
269 (indent . t))
270 "Non-nil means apply set of rules when acting on lists.
271 By default, automatic actions are taken when using
272 \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
273 \\[org-shiftmetaright], \\[org-shiftmetaleft],
274 \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
275 \\[org-insert-todo-heading]. You can disable individually these
276 rules by setting them to nil. Valid rules are:
278 bullet when non-nil, cycling bullet do not allow lists at
279 column 0 to have * as a bullet and descriptions lists
280 to be numbered.
281 checkbox when non-nil, checkbox statistics is updated each time
282 you either insert a new checkbox or toggle a checkbox.
283 It also prevents from inserting a checkbox in a
284 description item.
285 indent when non-nil, indenting or outdenting list top-item
286 with its subtree will move the whole list and
287 outdenting a list whose bullet is * to column 0 will
288 change that bullet to \"-\"."
289 :group 'org-plain-lists
290 :type '(alist :tag "Sets of rules"
291 :key-type
292 (choice
293 (const :tag "Bullet" bullet)
294 (const :tag "Checkbox" checkbox)
295 (const :tag "Indent" indent))
296 :value-type
297 (boolean :tag "Activate" :value t)))
299 (defcustom org-list-use-circular-motion nil
300 "Non-nil means commands implying motion in lists should be cyclic.
302 In that case, the item following the last item is the first one,
303 and the item preceding the first item is the last one.
305 This affects the behavior of \\[org-move-item-up],
306 \\[org-move-item-down], \\[org-next-item] and
307 \\[org-previous-item]."
308 :group 'org-plain-lists
309 :type 'boolean)
311 (defvar org-checkbox-statistics-hook nil
312 "Hook that is run whenever Org thinks checkbox statistics should be updated.
313 This hook runs even if checkbox rule in
314 `org-list-automatic-rules' does not apply, so it can be used to
315 implement alternative ways of collecting statistics
316 information.")
318 (defcustom org-hierarchical-checkbox-statistics t
319 "Non-nil means checkbox statistics counts only the state of direct children.
320 When nil, all boxes below the cookie are counted.
321 This can be set to nil on a per-node basis using a COOKIE_DATA property
322 with the word \"recursive\" in the value."
323 :group 'org-plain-lists
324 :type 'boolean)
326 (defcustom org-description-max-indent 20
327 "Maximum indentation for the second line of a description list.
328 When the indentation would be larger than this, it will become
329 5 characters instead."
330 :group 'org-plain-lists
331 :type 'integer)
333 (defcustom org-list-indent-offset 0
334 "Additional indentation for sub-items in a list.
335 By setting this to a small number, usually 1 or 2, one can more
336 clearly distinguish sub-items in a list."
337 :group 'org-plain-lists
338 :type 'integer)
340 (defcustom org-list-radio-list-templates
341 '((latex-mode "% BEGIN RECEIVE ORGLST %n
342 % END RECEIVE ORGLST %n
343 \\begin{comment}
344 #+ORGLST: SEND %n org-list-to-latex
346 \\end{comment}\n")
347 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
348 @c END RECEIVE ORGLST %n
349 @ignore
350 #+ORGLST: SEND %n org-list-to-texinfo
352 @end ignore\n")
353 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
354 <!-- END RECEIVE ORGLST %n -->
355 <!--
356 #+ORGLST: SEND %n org-list-to-html
358 -->\n"))
359 "Templates for radio lists in different major modes.
360 All occurrences of %n in a template will be replaced with the name of the
361 list, obtained by prompting the user."
362 :group 'org-plain-lists
363 :type '(repeat
364 (list (symbol :tag "Major mode")
365 (string :tag "Format"))))
367 (defvar org-list-forbidden-blocks '("example" "verse" "src" "ascii" "beamer"
368 "docbook" "html" "latex" "odt")
369 "Names of blocks where lists are not allowed.
370 Names must be in lower case.")
372 (defvar org-list-export-context '(block inlinetask)
373 "Context types where lists will be interpreted during export.
375 Valid types are `drawer', `inlinetask' and `block'. More
376 specifically, type `block' is determined by the variable
377 `org-list-forbidden-blocks'.")
380 ;;; Predicates and regexps
382 (defconst org-list-end-re (if org-empty-line-terminates-plain-lists
383 "^[ \t]*\n"
384 org-list-end-regexp)
385 "Regex corresponding to the end of a list.
386 It depends on `org-empty-line-terminates-plain-lists'.")
388 (defconst org-list-full-item-re
389 (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\)"
390 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
391 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
392 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
393 "Matches a list item and puts everything into groups:
394 group 1: bullet
395 group 2: counter
396 group 3: checkbox
397 group 4: description tag")
399 (defun org-item-re ()
400 "Return the correct regular expression for plain lists."
401 (let ((term (cond
402 ((eq org-plain-list-ordered-item-terminator t) "[.)]")
403 ((= org-plain-list-ordered-item-terminator ?\)) ")")
404 ((= org-plain-list-ordered-item-terminator ?.) "\\.")
405 (t "[.)]")))
406 (alpha (if org-alphabetical-lists "\\|[A-Za-z]" "")))
407 (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha "\\)" term
408 "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
410 (defsubst org-item-beginning-re ()
411 "Regexp matching the beginning of a plain list item."
412 (concat "^" (org-item-re)))
414 (defun org-list-at-regexp-after-bullet-p (regexp)
415 "Is point at a list item with REGEXP after bullet?"
416 (and (org-at-item-p)
417 (save-excursion
418 (goto-char (match-end 0))
419 (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
420 (if org-alphabetical-lists
421 "\\([0-9]+\\|[A-Za-z]\\)"
422 "[0-9]+")
423 "\\][ \t]*\\)")))
424 ;; Ignore counter if any
425 (when (looking-at counter-re) (goto-char (match-end 0))))
426 (looking-at regexp))))
428 (defun org-list-in-valid-context-p ()
429 "Is point in a context where lists are allowed?"
430 (not (org-in-block-p org-list-forbidden-blocks)))
432 (defun org-in-item-p ()
433 "Return item beginning position when in a plain list, nil otherwise.
434 This checks `org-list-ending-method'."
435 (save-excursion
436 (beginning-of-line)
437 (let* ((case-fold-search t)
438 (context (org-list-context))
439 (lim-up (car context))
440 (drawers-re (concat "^[ \t]*:\\("
441 (mapconcat 'regexp-quote org-drawers "\\|")
442 "\\):[ \t]*$"))
443 (inlinetask-re (and (featurep 'org-inlinetask)
444 (org-inlinetask-outline-regexp)))
445 (item-re (org-item-re))
446 ;; Indentation isn't meaningful when point starts at an empty
447 ;; line or an inline task.
448 (ind-ref (if (or (looking-at "^[ \t]*$")
449 (and inlinetask-re (looking-at inlinetask-re)))
450 10000
451 (org-get-indentation))))
452 (cond
453 ((eq (nth 2 context) 'invalid) nil)
454 ((looking-at item-re) (point))
456 ;; Detect if cursor in amidst `org-list-end-re'. First, count
457 ;; number HL of hard lines it takes, then call `org-in-regexp'
458 ;; to compute its boundaries END-BOUNDS. When point is
459 ;; in-between, move cursor before regexp beginning.
460 (let ((hl 0) (i -1) end-bounds)
461 (when (and (not (eq org-list-ending-method 'indent))
462 (progn
463 (while (setq i (string-match
464 "[\r\n]" org-list-end-re (1+ i)))
465 (setq hl (1+ hl)))
466 (setq end-bounds (org-in-regexp org-list-end-re hl)))
467 (>= (point) (car end-bounds))
468 (< (point) (cdr end-bounds)))
469 (goto-char (car end-bounds))
470 (forward-line -1)))
471 ;; Look for an item, less indented that reference line if
472 ;; `org-list-ending-method' isn't `regexp'.
473 (catch 'exit
474 (while t
475 (let ((ind (org-get-indentation)))
476 (cond
477 ;; This is exactly what we want.
478 ((and (looking-at item-re)
479 (or (< ind ind-ref)
480 (eq org-list-ending-method 'regexp)))
481 (throw 'exit (point)))
482 ;; At upper bound of search or looking at the end of a
483 ;; previous list: search is over.
484 ((<= (point) lim-up) (throw 'exit nil))
485 ((and (not (eq org-list-ending-method 'indent))
486 (looking-at org-list-end-re))
487 (throw 'exit nil))
488 ;; Skip blocks, drawers, inline-tasks, blank lines
489 ((and (looking-at "^[ \t]*#\\+end_")
490 (re-search-backward "^[ \t]*#\\+begin_" lim-up t)))
491 ((and (looking-at "^[ \t]*:END:")
492 (re-search-backward drawers-re lim-up t))
493 (beginning-of-line))
494 ((and inlinetask-re (looking-at inlinetask-re))
495 (org-inlinetask-goto-beginning)
496 (forward-line -1))
497 ((looking-at "^[ \t]*$") (forward-line -1))
498 ;; Text at column 0 cannot belong to a list: stop.
499 ((zerop ind) (throw 'exit nil))
500 ;; Normal text less indented than reference line, take
501 ;; it as new reference.
502 ((< ind ind-ref)
503 (setq ind-ref ind)
504 (forward-line -1))
505 (t (forward-line -1)))))))))))
507 (defun org-at-item-p ()
508 "Is point in a line starting a hand-formatted item?"
509 (save-excursion
510 (beginning-of-line)
511 (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
513 (defun org-at-item-bullet-p ()
514 "Is point at the bullet of a plain list item?"
515 (and (org-at-item-p)
516 (not (member (char-after) '(?\ ?\t)))
517 (< (point) (match-end 0))))
519 (defun org-at-item-timer-p ()
520 "Is point at a line starting a plain list item with a timer?"
521 (org-list-at-regexp-after-bullet-p
522 "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
524 (defun org-at-item-description-p ()
525 "Is point at a description list item?"
526 (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
528 (defun org-at-item-checkbox-p ()
529 "Is point at a line starting a plain-list item with a checklet?"
530 (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
532 (defun org-at-item-counter-p ()
533 "Is point at a line starting a plain-list item with a counter?"
534 (and (org-at-item-p)
535 (looking-at org-list-full-item-re)
536 (match-string 2)))
539 ;;; Structures and helper functions
541 (defun org-list-context ()
542 "Determine context, and its boundaries, around point.
544 Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
545 are boundaries and CONTEXT is a symbol among `drawer', `block',
546 `invalid', `inlinetask' and nil.
548 Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
549 (save-match-data
550 (save-excursion
551 (org-with-limited-levels
552 (beginning-of-line)
553 (let ((case-fold-search t) (pos (point)) beg end context-type
554 ;; Get positions of surrounding headings. This is the
555 ;; default context.
556 (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t))
557 (point)))
558 (point-min)))
559 (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
560 ;; Is point inside a drawer?
561 (let ((end-re "^[ \t]*:END:")
562 ;; Can't use org-drawers-regexp as this function might
563 ;; be called in buffers not in Org mode.
564 (beg-re (concat "^[ \t]*:\\("
565 (mapconcat 'regexp-quote org-drawers "\\|")
566 "\\):[ \t]*$")))
567 (when (save-excursion
568 (and (not (looking-at beg-re))
569 (not (looking-at end-re))
570 (setq beg (and (re-search-backward beg-re lim-up t)
571 (1+ (point-at-eol))))
572 (setq end (or (and (re-search-forward end-re lim-down t)
573 (1- (match-beginning 0)))
574 lim-down))
575 (>= end pos)))
576 (setq lim-up beg lim-down end context-type 'drawer)))
577 ;; Is point strictly in a block, and of which type?
578 (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type)
579 (when (save-excursion
580 (and (not (looking-at block-re))
581 (setq beg (and (re-search-backward block-re lim-up t)
582 (1+ (point-at-eol))))
583 (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
584 (setq type (downcase (match-string 1)))
585 (goto-char beg)
586 (setq end (or (and (re-search-forward block-re lim-down t)
587 (1- (point-at-bol)))
588 lim-down))
589 (>= end pos)
590 (equal (downcase (match-string 1)) "end")))
591 (setq lim-up beg lim-down end
592 context-type (if (member type org-list-forbidden-blocks)
593 'invalid 'block))))
594 ;; Is point in an inlinetask?
595 (when (and (featurep 'org-inlinetask)
596 (save-excursion
597 (let* ((beg-re (org-inlinetask-outline-regexp))
598 (end-re (concat beg-re "END[ \t]*$")))
599 (and (not (looking-at "^\\*+"))
600 (setq beg (and (re-search-backward beg-re lim-up t)
601 (1+ (point-at-eol))))
602 (not (looking-at end-re))
603 (setq end (and (re-search-forward end-re lim-down t)
604 (1- (match-beginning 0))))
605 (> (point) pos)))))
606 (setq lim-up beg lim-down end context-type 'inlinetask))
607 ;; Return context boundaries and type.
608 (list lim-up lim-down context-type))))))
610 (defun org-list-struct ()
611 "Return structure of list at point.
613 A list structure is an alist where key is point at item, and
614 values are:
615 1. indentation,
616 2. bullet with trailing whitespace,
617 3. bullet counter, if any,
618 4. checkbox, if any,
619 5. description tag, if any,
620 6. position at item end.
622 Thus the following list, where numbers in parens are
623 point-at-bol:
625 - [X] first item (1)
626 1. sub-item 1 (18)
627 5. [@5] sub-item 2 (34)
628 some other text belonging to first item (55)
629 - last item (97)
630 + tag :: description (109)
631 (131)
633 will get the following structure:
635 \(\(1 0 \"- \" nil \"[X]\" nil 97\)
636 \(18 2 \"1. \" nil nil nil 34\)
637 \(34 2 \"5. \" \"5\" nil nil 55\)
638 \(97 0 \"- \" nil nil nil 131\)
639 \(109 2 \"+ \" nil nil \"tag\" 131\)
641 Assume point is at an item."
642 (save-excursion
643 (beginning-of-line)
644 (let* ((case-fold-search t)
645 (context (org-list-context))
646 (lim-up (car context))
647 (lim-down (nth 1 context))
648 (text-min-ind 10000)
649 (item-re (org-item-re))
650 (drawers-re (concat "^[ \t]*:\\("
651 (mapconcat 'regexp-quote org-drawers "\\|")
652 "\\):[ \t]*$"))
653 (inlinetask-re (and (featurep 'org-inlinetask)
654 (org-inlinetask-outline-regexp)))
655 (beg-cell (cons (point) (org-get-indentation)))
656 ind itm-lst itm-lst-2 end-lst end-lst-2 struct
657 (assoc-at-point
658 (function
659 ;; Return association at point.
660 (lambda (ind)
661 (looking-at org-list-full-item-re)
662 (list (point)
664 (match-string-no-properties 1) ; bullet
665 (match-string-no-properties 2) ; counter
666 (match-string-no-properties 3) ; checkbox
667 (match-string-no-properties 4))))) ; description tag
668 (end-before-blank
669 (function
670 ;; Ensure list ends at the first blank line.
671 (lambda ()
672 (skip-chars-backward " \r\t\n")
673 (min (1+ (point-at-eol)) lim-down)))))
674 ;; 1. Read list from starting item to its beginning, and save
675 ;; top item position and indentation in BEG-CELL. Also store
676 ;; ending position of items in END-LST.
677 (save-excursion
678 (catch 'exit
679 (while t
680 (let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
681 (org-get-indentation))))
682 (cond
683 ((<= (point) lim-up)
684 ;; At upward limit: if we ended at an item, store it,
685 ;; else dimiss useless data recorded above BEG-CELL.
686 ;; Jump to part 2.
687 (throw 'exit
688 (setq itm-lst
689 (if (or (not (looking-at item-re))
690 (get-text-property (point) 'org-example))
691 (memq (assq (car beg-cell) itm-lst) itm-lst)
692 (setq beg-cell (cons (point) ind))
693 (cons (funcall assoc-at-point ind) itm-lst)))))
694 ;; At a verbatim block, go before its beginning. Move
695 ;; from eol to ensure `previous-single-property-change'
696 ;; will return a value.
697 ((get-text-property (point) 'org-example)
698 (goto-char (previous-single-property-change
699 (point-at-eol) 'org-example nil lim-up))
700 (forward-line -1))
701 ;; Looking at a list ending regexp. Dismiss useless
702 ;; data recorded above BEG-CELL. Jump to part 2.
703 ((and (not (eq org-list-ending-method 'indent))
704 (looking-at org-list-end-re))
705 (throw 'exit
706 (setq itm-lst
707 (memq (assq (car beg-cell) itm-lst) itm-lst))))
708 ;; Point is at an item. Add data to ITM-LST. It may
709 ;; also end a previous item: save it in END-LST. If
710 ;; ind is less or equal than BEG-CELL and there is no
711 ;; end at this ind or lesser, this item becomes the new
712 ;; BEG-CELL.
713 ((looking-at item-re)
714 (push (funcall assoc-at-point ind) itm-lst)
715 (push (cons ind (point)) end-lst)
716 (when (or (and (eq org-list-ending-method 'regexp)
717 (<= ind (cdr beg-cell)))
718 (< ind text-min-ind))
719 (setq beg-cell (cons (point) ind)))
720 (forward-line -1))
721 ;; Skip blocks, drawers, inline tasks, blank lines.
722 ((and (looking-at "^[ \t]*#\\+end_")
723 (re-search-backward "^[ \t]*#\\+begin_" lim-up t)))
724 ((and (looking-at "^[ \t]*:END:")
725 (re-search-backward drawers-re lim-up t))
726 (beginning-of-line))
727 ((and inlinetask-re (looking-at inlinetask-re))
728 (org-inlinetask-goto-beginning)
729 (forward-line -1))
730 ((looking-at "^[ \t]*$")
731 (forward-line -1))
732 ;; From there, point is not at an item. Unless ending
733 ;; method is `regexp', interpret line's indentation:
734 ;; - text at column 0 is necessarily out of any list.
735 ;; Dismiss data recorded above BEG-CELL. Jump to
736 ;; part 2.
737 ;; - any other case may be an ending position for an
738 ;; hypothetical item above. Store it and proceed.
739 ((eq org-list-ending-method 'regexp) (forward-line -1))
740 ((zerop ind)
741 (throw 'exit
742 (setq itm-lst
743 (memq (assq (car beg-cell) itm-lst) itm-lst))))
745 (when (< ind text-min-ind) (setq text-min-ind ind))
746 (push (cons ind (point)) end-lst)
747 (forward-line -1)))))))
748 ;; 2. Read list from starting point to its end, that is until we
749 ;; get out of context, or that a non-item line is less or
750 ;; equally indented than BEG-CELL's cdr. Also, store ending
751 ;; position of items in END-LST-2.
752 (catch 'exit
753 (while t
754 (let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
755 (org-get-indentation))))
756 (cond
757 ((>= (point) lim-down)
758 ;; At downward limit: this is de facto the end of the
759 ;; list. Save point as an ending position, and jump to
760 ;; part 3.
761 (throw 'exit
762 (push (cons 0 (funcall end-before-blank)) end-lst-2)))
763 ;; At a verbatim block, move to its end. Point is at bol
764 ;; and 'org-example property is set by whole lines:
765 ;; `next-single-property-change' always return a value.
766 ((get-text-property (point) 'org-example)
767 (goto-char
768 (next-single-property-change (point) 'org-example nil lim-down)))
769 ;; Looking at a list ending regexp. Save point as an
770 ;; ending position and jump to part 3.
771 ((and (not (eq org-list-ending-method 'indent))
772 (looking-at org-list-end-re))
773 (throw 'exit (push (cons 0 (point)) end-lst-2)))
774 ((looking-at item-re)
775 ;; Point is at an item. Add data to ITM-LST-2. It may
776 ;; also end a previous item, so save it in END-LST-2.
777 (push (funcall assoc-at-point ind) itm-lst-2)
778 (push (cons ind (point)) end-lst-2)
779 (forward-line 1))
780 ;; Skip inline tasks and blank lines along the way
781 ((and inlinetask-re (looking-at inlinetask-re))
782 (org-inlinetask-goto-end))
783 ((looking-at "^[ \t]*$")
784 (forward-line 1))
785 ;; Ind is lesser or equal than BEG-CELL's. The list is
786 ;; over: store point as an ending position and jump to
787 ;; part 3.
788 ((and (not (eq org-list-ending-method 'regexp))
789 (<= ind (cdr beg-cell)))
790 (throw 'exit
791 (push (cons 0 (funcall end-before-blank)) end-lst-2)))
792 ;; Else, if ind is lesser or equal than previous item's,
793 ;; this is an ending position: store it. In any case,
794 ;; skip block or drawer at point, and move to next line.
796 (when (and (not (eq org-list-ending-method 'regexp))
797 (<= ind (nth 1 (car itm-lst-2))))
798 (push (cons ind (point)) end-lst-2))
799 (cond
800 ((and (looking-at "^[ \t]*#\\+begin_")
801 (re-search-forward "^[ \t]*#\\+end_" lim-down t)))
802 ((and (looking-at drawers-re)
803 (re-search-forward "^[ \t]*:END:" lim-down t))))
804 (forward-line 1))))))
805 (setq struct (append itm-lst (cdr (nreverse itm-lst-2)))
806 end-lst (append end-lst (cdr (nreverse end-lst-2))))
807 ;; 3. Correct ill-formed lists by ensuring top item is the least
808 ;; indented.
809 (let ((min-ind (nth 1 (car struct))))
810 (mapc (lambda (item)
811 (let ((ind (nth 1 item))
812 (bul (nth 2 item)))
813 (when (< ind min-ind)
814 (setcar (cdr item) min-ind)
815 ;; Trim bullet so item will be seen as different
816 ;; when compared with repaired version.
817 (setcar (nthcdr 2 item) (org-trim bul)))))
818 struct))
819 ;; 4. Associate each item to its end pos.
820 (org-list-struct-assoc-end struct end-lst)
821 ;; 5. Return STRUCT
822 struct)))
824 (defun org-list-struct-assoc-end (struct end-list)
825 "Associate proper ending point to items in STRUCT.
827 END-LIST is a pseudo-alist where car is indentation and cdr is
828 ending position.
830 This function modifies STRUCT."
831 (let ((endings end-list))
832 (mapc
833 (lambda (elt)
834 (let ((pos (car elt))
835 (ind (nth 1 elt)))
836 ;; Remove end candidates behind current item.
837 (while (or (<= (cdar endings) pos))
838 (pop endings))
839 ;; Add end position to item assoc.
840 (let ((old-end (nthcdr 6 elt))
841 (new-end (assoc-default ind endings '<=)))
842 (if old-end
843 (setcar old-end new-end)
844 (setcdr elt (append (cdr elt) (list new-end)))))))
845 struct)))
847 (defun org-list-prevs-alist (struct)
848 "Return alist between item and previous item in STRUCT."
849 (let ((item-end-alist (mapcar (lambda (e) (cons (car e) (nth 6 e)))
850 struct)))
851 (mapcar (lambda (e)
852 (let ((prev (car (rassq (car e) item-end-alist))))
853 (cons (car e) prev)))
854 struct)))
856 (defun org-list-parents-alist (struct)
857 "Return alist between item and parent in STRUCT."
858 (let ((ind-to-ori (list (list (nth 1 (car struct)))))
859 (prev-pos (list (caar struct))))
860 (cons prev-pos
861 (mapcar (lambda (item)
862 (let ((pos (car item))
863 (ind (nth 1 item))
864 (prev-ind (caar ind-to-ori)))
865 (push pos prev-pos)
866 (cond
867 ((> prev-ind ind)
868 (setq ind-to-ori
869 (member (assq ind ind-to-ori) ind-to-ori))
870 (cons pos (cdar ind-to-ori)))
871 ((< prev-ind ind)
872 (let ((origin (nth 1 prev-pos)))
873 (push (cons ind origin) ind-to-ori)
874 (cons pos origin)))
875 (t (cons pos (cdar ind-to-ori))))))
876 (cdr struct)))))
879 ;;; Accessors
881 (defsubst org-list-get-nth (n key struct)
882 "Return the Nth value of KEY in STRUCT."
883 (nth n (assq key struct)))
885 (defun org-list-set-nth (n key struct new)
886 "Set the Nth value of KEY in STRUCT to NEW.
887 \nThis function modifies STRUCT."
888 (setcar (nthcdr n (assq key struct)) new))
890 (defsubst org-list-get-ind (item struct)
891 "Return indentation of ITEM in STRUCT."
892 (org-list-get-nth 1 item struct))
894 (defun org-list-set-ind (item struct ind)
895 "Set indentation of ITEM in STRUCT to IND.
896 \nThis function modifies STRUCT."
897 (org-list-set-nth 1 item struct ind))
899 (defsubst org-list-get-bullet (item struct)
900 "Return bullet of ITEM in STRUCT."
901 (org-list-get-nth 2 item struct))
903 (defun org-list-set-bullet (item struct bullet)
904 "Set bullet of ITEM in STRUCT to BULLET.
905 \nThis function modifies STRUCT."
906 (org-list-set-nth 2 item struct bullet))
908 (defsubst org-list-get-counter (item struct)
909 "Return counter of ITEM in STRUCT."
910 (org-list-get-nth 3 item struct))
912 (defsubst org-list-get-checkbox (item struct)
913 "Return checkbox of ITEM in STRUCT or nil."
914 (org-list-get-nth 4 item struct))
916 (defun org-list-set-checkbox (item struct checkbox)
917 "Set checkbox of ITEM in STRUCT to CHECKBOX.
918 \nThis function modifies STRUCT."
919 (org-list-set-nth 4 item struct checkbox))
921 (defsubst org-list-get-tag (item struct)
922 "Return end position of ITEM in STRUCT."
923 (org-list-get-nth 5 item struct))
925 (defun org-list-get-item-end (item struct)
926 "Return end position of ITEM in STRUCT."
927 (org-list-get-nth 6 item struct))
929 (defun org-list-get-item-end-before-blank (item struct)
930 "Return point at end of ITEM in STRUCT, before any blank line.
931 Point returned is at end of line."
932 (save-excursion
933 (goto-char (org-list-get-item-end item struct))
934 (skip-chars-backward " \r\t\n")
935 (point-at-eol)))
937 (defun org-list-get-parent (item struct parents)
938 "Return parent of ITEM or nil.
939 STRUCT is the list structure. PARENTS is the alist of parents,
940 as returned by `org-list-parents-alist'."
941 (let ((parents (or parents (org-list-parents-alist struct))))
942 (cdr (assq item parents))))
944 (defun org-list-has-child-p (item struct)
945 "Non-nil if ITEM has a child.
947 STRUCT is the list structure.
949 Value returned is the position of the first child of ITEM."
950 (let ((ind (org-list-get-ind item struct))
951 (child-maybe (car (nth 1 (member (assq item struct) struct)))))
952 (when (and child-maybe
953 (< ind (org-list-get-ind child-maybe struct)))
954 child-maybe)))
956 (defun org-list-get-next-item (item struct prevs)
957 "Return next item in same sub-list as ITEM, or nil.
958 STRUCT is the list structure. PREVS is the alist of previous
959 items, as returned by `org-list-prevs-alist'."
960 (car (rassq item prevs)))
962 (defun org-list-get-prev-item (item struct prevs)
963 "Return previous item in same sub-list as ITEM, or nil.
964 STRUCT is the list structure. PREVS is the alist of previous
965 items, as returned by `org-list-prevs-alist'."
966 (cdr (assq item prevs)))
968 (defun org-list-get-subtree (item struct)
969 "List all items having ITEM as a common ancestor, or nil.
970 STRUCT is the list structure."
971 (let* ((item-end (org-list-get-item-end item struct))
972 (sub-struct (cdr (member (assq item struct) struct)))
973 subtree)
974 (catch 'exit
975 (mapc (lambda (e)
976 (let ((pos (car e)))
977 (if (< pos item-end) (push pos subtree) (throw 'exit nil))))
978 sub-struct))
979 (nreverse subtree)))
981 (defun org-list-get-all-items (item struct prevs)
982 "List all items in the same sub-list as ITEM.
983 STRUCT is the list structure. PREVS is the alist of previous
984 items, as returned by `org-list-prevs-alist'."
985 (let ((prev-item item)
986 (next-item item)
987 before-item after-item)
988 (while (setq prev-item (org-list-get-prev-item prev-item struct prevs))
989 (push prev-item before-item))
990 (while (setq next-item (org-list-get-next-item next-item struct prevs))
991 (push next-item after-item))
992 (append before-item (list item) (nreverse after-item))))
994 (defun org-list-get-children (item struct parents)
995 "List all children of ITEM, or nil.
996 STRUCT is the list structure. PARENTS is the alist of parents, as
997 returned by `org-list-parents-alist'."
998 (let (all child)
999 (while (setq child (car (rassq item parents)))
1000 (setq parents (cdr (member (assq child parents) parents)))
1001 (push child all))
1002 (nreverse all)))
1004 (defun org-list-get-top-point (struct)
1005 "Return point at beginning of list.
1006 STRUCT is the list structure."
1007 (caar struct))
1009 (defun org-list-get-bottom-point (struct)
1010 "Return point at bottom of list.
1011 STRUCT is the list structure."
1012 (apply 'max
1013 (mapcar (lambda (e) (org-list-get-item-end (car e) struct)) struct)))
1015 (defun org-list-get-list-begin (item struct prevs)
1016 "Return point at beginning of sub-list ITEM belongs.
1017 STRUCT is the list structure. PREVS is the alist of previous
1018 items, as returned by `org-list-prevs-alist'."
1019 (let ((first-item item) prev-item)
1020 (while (setq prev-item (org-list-get-prev-item first-item struct prevs))
1021 (setq first-item prev-item))
1022 first-item))
1024 (defalias 'org-list-get-first-item 'org-list-get-list-begin)
1026 (defun org-list-get-last-item (item struct prevs)
1027 "Return point at last item of sub-list ITEM belongs.
1028 STRUCT is the list structure. PREVS is the alist of previous
1029 items, as returned by `org-list-prevs-alist'."
1030 (let ((last-item item) next-item)
1031 (while (setq next-item (org-list-get-next-item last-item struct prevs))
1032 (setq last-item next-item))
1033 last-item))
1035 (defun org-list-get-list-end (item struct prevs)
1036 "Return point at end of sub-list ITEM belongs.
1037 STRUCT is the list structure. PREVS is the alist of previous
1038 items, as returned by `org-list-prevs-alist'."
1039 (org-list-get-item-end (org-list-get-last-item item struct prevs) struct))
1041 (defun org-list-get-list-type (item struct prevs)
1042 "Return the type of the list containing ITEM, as a symbol.
1044 STRUCT is the list structure. PREVS is the alist of previous
1045 items, as returned by `org-list-prevs-alist'.
1047 Possible types are `descriptive', `ordered' and `unordered'. The
1048 type is determined by the first item of the list."
1049 (let ((first (org-list-get-list-begin item struct prevs)))
1050 (cond
1051 ((org-list-get-tag first struct) 'descriptive)
1052 ((string-match "[[:alnum:]]" (org-list-get-bullet first struct)) 'ordered)
1053 (t 'unordered))))
1056 ;;; Searching
1058 (defun org-list-search-generic (search re bound noerr)
1059 "Search a string in valid contexts for lists.
1060 Arguments SEARCH, RE, BOUND and NOERR are similar to those used
1061 in `re-search-forward'."
1062 (catch 'exit
1063 (let ((origin (point)))
1064 (while t
1065 ;; 1. No match: return to origin or bound, depending on NOERR.
1066 (unless (funcall search re bound noerr)
1067 (throw 'exit (and (goto-char (if (memq noerr '(t nil)) origin bound))
1068 nil)))
1069 ;; 2. Match in valid context: return point. Else, continue
1070 ;; searching.
1071 (when (org-list-in-valid-context-p) (throw 'exit (point)))))))
1073 (defun org-list-search-backward (regexp &optional bound noerror)
1074 "Like `re-search-backward' but stop only where lists are recognized.
1075 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1076 `re-search-backward'."
1077 (org-list-search-generic #'re-search-backward
1078 regexp (or bound (point-min)) noerror))
1080 (defun org-list-search-forward (regexp &optional bound noerror)
1081 "Like `re-search-forward' but stop only where lists are recognized.
1082 Arguments REGEXP, BOUND and NOERROR are similar to those used in
1083 `re-search-forward'."
1084 (org-list-search-generic #'re-search-forward
1085 regexp (or bound (point-max)) noerror))
1088 ;;; Methods on structures
1090 (defsubst org-list-bullet-string (bullet)
1091 "Return BULLET with the correct number of whitespaces.
1092 It determines the number of whitespaces to append by looking at
1093 `org-list-two-spaces-after-bullet-regexp'."
1094 (save-match-data
1095 (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
1096 (string-match
1097 org-list-two-spaces-after-bullet-regexp bullet))
1099 " ")))
1100 (string-match "\\S-+\\([ \t]*\\)" bullet)
1101 (replace-match spaces nil nil bullet 1))))
1103 (defun org-list-swap-items (beg-A beg-B struct)
1104 "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
1105 Blank lines at the end of items are left in place. Return the
1106 new structure after the changes.
1108 Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
1109 to the same sub-list.
1111 This function modifies STRUCT."
1112 (save-excursion
1113 (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct))
1114 (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct))
1115 (end-A (org-list-get-item-end beg-A struct))
1116 (end-B (org-list-get-item-end beg-B struct))
1117 (size-A (- end-A-no-blank beg-A))
1118 (size-B (- end-B-no-blank beg-B))
1119 (body-A (buffer-substring beg-A end-A-no-blank))
1120 (body-B (buffer-substring beg-B end-B-no-blank))
1121 (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B))
1122 (sub-A (cons beg-A (org-list-get-subtree beg-A struct)))
1123 (sub-B (cons beg-B (org-list-get-subtree beg-B struct))))
1124 ;; 1. Move effectively items in buffer.
1125 (goto-char beg-A)
1126 (delete-region beg-A end-B-no-blank)
1127 (insert (concat body-B between-A-no-blank-and-B body-A))
1128 ;; 2. Now modify struct. No need to re-read the list, the
1129 ;; transformation is just a shift of positions. Some special
1130 ;; attention is required for items ending at END-A and END-B
1131 ;; as empty spaces are not moved there. In others words,
1132 ;; item BEG-A will end with whitespaces that were at the end
1133 ;; of BEG-B and the same applies to BEG-B.
1134 (mapc (lambda (e)
1135 (let ((pos (car e)))
1136 (cond
1137 ((< pos beg-A))
1138 ((memq pos sub-A)
1139 (let ((end-e (nth 6 e)))
1140 (setcar e (+ pos (- end-B-no-blank end-A-no-blank)))
1141 (setcar (nthcdr 6 e)
1142 (+ end-e (- end-B-no-blank end-A-no-blank)))
1143 (when (= end-e end-A) (setcar (nthcdr 6 e) end-B))))
1144 ((memq pos sub-B)
1145 (let ((end-e (nth 6 e)))
1146 (setcar e (- (+ pos beg-A) beg-B))
1147 (setcar (nthcdr 6 e) (+ end-e (- beg-A beg-B)))
1148 (when (= end-e end-B)
1149 (setcar (nthcdr 6 e)
1150 (+ beg-A size-B (- end-A end-A-no-blank))))))
1151 ((< pos beg-B)
1152 (let ((end-e (nth 6 e)))
1153 (setcar e (+ pos (- size-B size-A)))
1154 (setcar (nthcdr 6 e) (+ end-e (- size-B size-A))))))))
1155 struct)
1156 (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))))
1158 (defun org-list-separating-blank-lines-number (pos struct prevs)
1159 "Return number of blank lines that should separate items in list.
1161 POS is the position of point where `org-list-insert-item' was called.
1163 STRUCT is the list structure. PREVS is the alist of previous
1164 items, as returned by `org-list-prevs-alist'.
1166 Assume point is at item's beginning. If the item is alone, apply
1167 some heuristics to guess the result."
1168 (save-excursion
1169 (let ((item (point))
1170 (insert-blank-p
1171 (cdr (assq 'plain-list-item org-blank-before-new-entry)))
1172 usr-blank)
1173 (cond
1174 ;; Trivial cases where there should be none.
1175 ((or (and (not (eq org-list-ending-method 'indent))
1176 org-empty-line-terminates-plain-lists)
1177 (not insert-blank-p)) 0)
1178 ;; When `org-blank-before-new-entry' says so, it is 1.
1179 ((eq insert-blank-p t) 1)
1180 ;; `plain-list-item' is 'auto. Count blank lines separating
1181 ;; neighbours items in list.
1182 (t (let ((next-p (org-list-get-next-item item struct prevs)))
1183 (cond
1184 ;; Is there a next item?
1185 (next-p (goto-char next-p)
1186 (org-back-over-empty-lines))
1187 ;; Is there a previous item?
1188 ((org-list-get-prev-item item struct prevs)
1189 (org-back-over-empty-lines))
1190 ;; User inserted blank lines, trust him.
1191 ((and (> pos (org-list-get-item-end-before-blank item struct))
1192 (> (save-excursion
1193 (goto-char pos)
1194 (skip-chars-backward " \t")
1195 (setq usr-blank (org-back-over-empty-lines))) 0))
1196 usr-blank)
1197 ;; Are there blank lines inside the list so far?
1198 ((save-excursion
1199 (goto-char (org-list-get-top-point struct))
1200 (org-list-search-forward "^[ \t]*$" item t))
1202 ;; Default choice: no blank line.
1203 (t 0))))))))
1205 (defun org-list-insert-item (pos struct prevs &optional checkbox after-bullet)
1206 "Insert a new list item at POS and return the new structure.
1207 If POS is before first character after bullet of the item, the
1208 new item will be created before the current one.
1210 STRUCT is the list structure. PREVS is the the alist of previous
1211 items, as returned by `org-list-prevs-alist'.
1213 Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
1214 after the bullet. Cursor will be after this text once the
1215 function ends.
1217 This function modifies STRUCT."
1218 (let ((case-fold-search t))
1219 ;; 1. Get information about list: position of point with regards
1220 ;; to item start (BEFOREP), blank lines number separating items
1221 ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
1222 (let* ((item (progn (goto-char pos) (goto-char (org-list-get-item-begin))))
1223 (item-end (org-list-get-item-end item struct))
1224 (item-end-no-blank (org-list-get-item-end-before-blank item struct))
1225 (beforep (and (looking-at org-list-full-item-re)
1226 (<= pos (match-end 0))))
1227 (split-line-p (org-get-alist-option org-M-RET-may-split-line 'item))
1228 (blank-nb (org-list-separating-blank-lines-number
1229 pos struct prevs))
1230 ;; 2. Build the new item to be created. Concatenate same
1231 ;; bullet as item, checkbox, text AFTER-BULLET if
1232 ;; provided, and text cut from point to end of item
1233 ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
1234 ;; BEFOREP and SPLIT-LINE-P. The difference of size
1235 ;; between what was cut and what was inserted in buffer
1236 ;; is stored in SIZE-OFFSET.
1237 (ind (org-list-get-ind item struct))
1238 (ind-size (if indent-tabs-mode
1239 (+ (/ ind tab-width) (mod ind tab-width))
1240 ind))
1241 (bullet (org-list-bullet-string (org-list-get-bullet item struct)))
1242 (box (when checkbox "[ ]"))
1243 (text-cut
1244 (and (not beforep) split-line-p
1245 (progn
1246 (goto-char pos)
1247 ;; If POS is greater than ITEM-END, then point is
1248 ;; in some white lines after the end of the list.
1249 ;; Those must be removed, or they will be left,
1250 ;; stacking up after the list.
1251 (when (< item-end pos)
1252 (delete-region (1- item-end) (point-at-eol)))
1253 (skip-chars-backward " \r\t\n")
1254 (setq pos (point))
1255 (delete-and-extract-region pos item-end-no-blank))))
1256 (body (concat bullet (when box (concat box " ")) after-bullet
1257 (and text-cut
1258 (if (string-match "\\`[ \t]+" text-cut)
1259 (replace-match "" t t text-cut)
1260 text-cut))))
1261 (item-sep (make-string (1+ blank-nb) ?\n))
1262 (item-size (+ ind-size (length body) (length item-sep)))
1263 (size-offset (- item-size (length text-cut))))
1264 ;; 4. Insert effectively item into buffer.
1265 (goto-char item)
1266 (org-indent-to-column ind)
1267 (insert body item-sep)
1268 ;; 5. Add new item to STRUCT.
1269 (mapc (lambda (e)
1270 (let ((p (car e))
1271 (end (nth 6 e)))
1272 (cond
1273 ;; Before inserted item, positions don't change but
1274 ;; an item ending after insertion has its end shifted
1275 ;; by SIZE-OFFSET.
1276 ((< p item)
1277 (when (> end item) (setcar (nthcdr 6 e) (+ end size-offset))))
1278 ;; Trivial cases where current item isn't split in
1279 ;; two. Just shift every item after new one by
1280 ;; ITEM-SIZE.
1281 ((or beforep (not split-line-p))
1282 (setcar e (+ p item-size))
1283 (setcar (nthcdr 6 e) (+ end item-size)))
1284 ;; Item is split in two: elements before POS are just
1285 ;; shifted by ITEM-SIZE. In the case item would end
1286 ;; after split POS, ending is only shifted by
1287 ;; SIZE-OFFSET.
1288 ((< p pos)
1289 (setcar e (+ p item-size))
1290 (if (< end pos)
1291 (setcar (nthcdr 6 e) (+ end item-size))
1292 (setcar (nthcdr 6 e) (+ end size-offset))))
1293 ;; Elements after POS are moved into new item.
1294 ;; Length of ITEM-SEP has to be removed as ITEM-SEP
1295 ;; doesn't appear in buffer yet.
1296 ((< p item-end)
1297 (setcar e (+ p size-offset (- item pos (length item-sep))))
1298 (if (= end item-end)
1299 (setcar (nthcdr 6 e) (+ item item-size))
1300 (setcar (nthcdr 6 e)
1301 (+ end size-offset
1302 (- item pos (length item-sep))))))
1303 ;; Elements at ITEM-END or after are only shifted by
1304 ;; SIZE-OFFSET.
1305 (t (setcar e (+ p size-offset))
1306 (setcar (nthcdr 6 e) (+ end size-offset))))))
1307 struct)
1308 (push (list item ind bullet nil box nil (+ item item-size)) struct)
1309 (setq struct (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))
1310 ;; 6. If not BEFOREP, new item must appear after ITEM, so
1311 ;; exchange ITEM with the next item in list. Position cursor
1312 ;; after bullet, counter, checkbox, and label.
1313 (if beforep
1314 (goto-char item)
1315 (setq struct (org-list-swap-items item (+ item item-size) struct))
1316 (goto-char (org-list-get-next-item
1317 item struct (org-list-prevs-alist struct))))
1318 struct)))
1320 (defun org-list-delete-item (item struct)
1321 "Remove ITEM from the list and return the new structure.
1323 STRUCT is the list structure."
1324 (let* ((end (org-list-get-item-end item struct))
1325 (beg (if (= (org-list-get-bottom-point struct) end)
1326 ;; If ITEM ends with the list, delete blank lines
1327 ;; before it.
1328 (save-excursion
1329 (goto-char item)
1330 (skip-chars-backward " \r\t\n")
1331 (min (1+ (point-at-eol)) (point-max)))
1332 item)))
1333 ;; Remove item from buffer.
1334 (delete-region beg end)
1335 ;; Remove item from structure and shift others items accordingly.
1336 ;; Don't forget to shift also ending position when appropriate.
1337 (let ((size (- end beg)))
1338 (delq nil (mapcar (lambda (e)
1339 (let ((pos (car e)))
1340 (cond
1341 ((< pos item)
1342 (let ((end-e (nth 6 e)))
1343 (cond
1344 ((< end-e item) e)
1345 ((= end-e item)
1346 (append (butlast e) (list beg)))
1348 (append (butlast e) (list (- end-e size)))))))
1349 ((< pos end) nil)
1351 (cons (- pos size)
1352 (append (butlast (cdr e))
1353 (list (- (nth 6 e) size))))))))
1354 struct)))))
1356 (defun org-list-send-item (item dest struct)
1357 "Send ITEM to destination DEST.
1359 STRUCT is the list structure.
1361 DEST can have various values.
1363 If DEST is a buffer position, the function will assume it points
1364 to another item in the same list as ITEM, and will move the
1365 latter just before the former.
1367 If DEST is `begin' \(resp. `end'\), ITEM will be moved at the
1368 beginning \(resp. end\) of the list it belongs to.
1370 If DEST is a string like \"N\", where N is an integer, ITEM will
1371 be moved at the Nth position in the list.
1373 If DEST is `kill', ITEM will be deleted and its body will be
1374 added to the kill-ring.
1376 If DEST is `delete', ITEM will be deleted.
1378 This function returns, destructively, the new list structure."
1379 (let* ((prevs (org-list-prevs-alist struct))
1380 (item-end (org-list-get-item-end item struct))
1381 ;; Grab full item body minus its bullet.
1382 (body (org-trim
1383 (buffer-substring
1384 (save-excursion
1385 (goto-char item)
1386 (looking-at
1387 (concat "[ \t]*"
1388 (regexp-quote (org-list-get-bullet item struct))))
1389 (match-end 0))
1390 item-end)))
1391 ;; Change DEST into a buffer position. A trick is needed
1392 ;; when ITEM is meant to be sent at the end of the list.
1393 ;; Indeed, by setting locally `org-M-RET-may-split-line' to
1394 ;; nil and insertion point (INS-POINT) to the first line's
1395 ;; end of the last item, we ensure the new item will be
1396 ;; inserted after the last item, and not after any of its
1397 ;; hypothetical sub-items.
1398 (ins-point (cond
1399 ((or (eq dest 'kill) (eq dest 'delete)))
1400 ((eq dest 'begin)
1401 (setq dest (org-list-get-list-begin item struct prevs)))
1402 ((eq dest 'end)
1403 (setq dest (org-list-get-list-end item struct prevs))
1404 (save-excursion
1405 (goto-char (org-list-get-last-item item struct prevs))
1406 (point-at-eol)))
1407 ((string-match "\\`[0-9]+\\'" dest)
1408 (let* ((all (org-list-get-all-items item struct prevs))
1409 (len (length all))
1410 (index (mod (string-to-number dest) len)))
1411 (if (not (zerop index))
1412 (setq dest (nth (1- index) all))
1413 ;; Send ITEM at the end of the list.
1414 (setq dest (org-list-get-list-end item struct prevs))
1415 (save-excursion
1416 (goto-char
1417 (org-list-get-last-item item struct prevs))
1418 (point-at-eol)))))
1419 (t dest)))
1420 (org-M-RET-may-split-line nil))
1421 (cond
1422 ((eq dest 'delete) (org-list-delete-item item struct))
1423 ((eq dest 'kill)
1424 (kill-new body)
1425 (org-list-delete-item item struct))
1426 ((and (integerp dest) (/= item ins-point))
1427 (setq item (copy-marker item))
1428 (setq struct (org-list-insert-item ins-point struct prevs nil body))
1429 ;; 1. Structure returned by `org-list-insert-item' may not be
1430 ;; accurate, as it cannot see sub-items included in BODY.
1431 ;; Thus, first compute the real structure so far.
1432 (let ((moved-items
1433 (cons (marker-position item)
1434 (org-list-get-subtree (marker-position item) struct)))
1435 (new-end (org-list-get-item-end (point) struct))
1436 (old-end (org-list-get-item-end (marker-position item) struct))
1437 (new-item (point))
1438 (shift (- (point) item)))
1439 ;; 1.1. Remove the item just created in structure.
1440 (setq struct (delete (assq new-item struct) struct))
1441 ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
1442 (setq struct (sort*
1443 (append
1444 struct
1445 (mapcar (lambda (e)
1446 (let* ((cell (assq e struct))
1447 (pos (car cell))
1448 (end (nth 6 cell)))
1449 (cons (+ pos shift)
1450 (append (butlast (cdr cell))
1451 (list (if (= end old-end)
1452 new-end
1453 (+ end shift)))))))
1454 moved-items))
1455 (lambda (e1 e2) (< (car e1) (car e2))))))
1456 ;; 2. Eventually delete extra copy of the item and clean marker.
1457 (prog1
1458 (org-list-delete-item (marker-position item) struct)
1459 (move-marker item nil)))
1460 (t struct))))
1462 (defun org-list-struct-outdent (start end struct parents)
1463 "Outdent items between positions START and END.
1465 STRUCT is the list structure. PARENTS is the alist of items'
1466 parents, as returned by `org-list-parents-alist'.
1468 START is included, END excluded."
1469 (let* (acc
1470 (out (lambda (cell)
1471 (let* ((item (car cell))
1472 (parent (cdr cell)))
1473 (cond
1474 ;; Item not yet in zone: keep association.
1475 ((< item start) cell)
1476 ;; Item out of zone: follow associations in ACC.
1477 ((>= item end)
1478 (let ((convert (and parent (assq parent acc))))
1479 (if convert (cons item (cdr convert)) cell)))
1480 ;; Item has no parent: error
1481 ((not parent)
1482 (error "Cannot outdent top-level items"))
1483 ;; Parent is outdented: keep association.
1484 ((>= parent start)
1485 (push (cons parent item) acc) cell)
1487 ;; Parent isn't outdented: reparent to grand-parent.
1488 (let ((grand-parent (org-list-get-parent
1489 parent struct parents)))
1490 (push (cons parent item) acc)
1491 (cons item grand-parent))))))))
1492 (mapcar out parents)))
1494 (defun org-list-struct-indent (start end struct parents prevs)
1495 "Indent items between positions START and END.
1497 STRUCT is the list structure. PARENTS is the alist of parents
1498 and PREVS is the alist of previous items, returned by,
1499 respectively, `org-list-parents-alist' and
1500 `org-list-prevs-alist'.
1502 START is included and END excluded.
1504 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1505 bullets between START and END."
1506 (let* (acc
1507 (set-assoc (lambda (cell) (push cell acc) cell))
1508 (change-bullet-maybe
1509 (function
1510 (lambda (item)
1511 (let* ((bul (org-trim (org-list-get-bullet item struct)))
1512 (new-bul-p (cdr (assoc bul org-list-demote-modify-bullet))))
1513 (when new-bul-p (org-list-set-bullet item struct new-bul-p))))))
1514 (ind
1515 (lambda (cell)
1516 (let* ((item (car cell))
1517 (parent (cdr cell)))
1518 (cond
1519 ;; Item not yet in zone: keep association.
1520 ((< item start) cell)
1521 ((>= item end)
1522 ;; Item out of zone: follow associations in ACC.
1523 (let ((convert (assq parent acc)))
1524 (if convert (cons item (cdr convert)) cell)))
1526 ;; Item is in zone...
1527 (let ((prev (org-list-get-prev-item item struct prevs)))
1528 ;; Check if bullet needs to be changed.
1529 (funcall change-bullet-maybe item)
1530 (cond
1531 ;; First item indented but not parent: error
1532 ((and (not prev) (< parent start))
1533 (error "Cannot indent the first item of a list"))
1534 ;; First item and parent indented: keep same
1535 ;; parent.
1536 ((not prev) (funcall set-assoc cell))
1537 ;; Previous item not indented: reparent to it.
1538 ((< prev start) (funcall set-assoc (cons item prev)))
1539 ;; Previous item indented: reparent like it.
1541 (funcall set-assoc
1542 (cons item (cdr (assq prev acc)))))))))))))
1543 (mapcar ind parents)))
1546 ;;; Repairing structures
1548 (defun org-list-use-alpha-bul-p (first struct prevs)
1549 "Non-nil if list starting at FIRST can have alphabetical bullets.
1551 STRUCT is list structure. PREVS is the alist of previous items,
1552 as returned by `org-list-prevs-alist'."
1553 (and org-alphabetical-lists
1554 (catch 'exit
1555 (let ((item first) (ascii 64) (case-fold-search nil))
1556 ;; Pretend that bullets are uppercase and check if alphabet
1557 ;; is sufficient, taking counters into account.
1558 (while item
1559 (let ((bul (org-list-get-bullet item struct))
1560 (count (org-list-get-counter item struct)))
1561 ;; Virtually determine current bullet
1562 (if (and count (string-match "[a-zA-Z]" count))
1563 ;; Counters are not case-sensitive.
1564 (setq ascii (string-to-char (upcase count)))
1565 (setq ascii (1+ ascii)))
1566 ;; Test if bullet would be over z or Z.
1567 (if (> ascii 90)
1568 (throw 'exit nil)
1569 (setq item (org-list-get-next-item item struct prevs)))))
1570 ;; All items checked. All good.
1571 t))))
1573 (defun org-list-inc-bullet-maybe (bullet)
1574 "Increment BULLET if applicable."
1575 (let ((case-fold-search nil))
1576 (cond
1577 ;; Num bullet: increment it.
1578 ((string-match "[0-9]+" bullet)
1579 (replace-match
1580 (number-to-string (1+ (string-to-number (match-string 0 bullet))))
1581 nil nil bullet))
1582 ;; Alpha bullet: increment it.
1583 ((string-match "[A-Za-z]" bullet)
1584 (replace-match
1585 (char-to-string (1+ (string-to-char (match-string 0 bullet))))
1586 nil nil bullet))
1587 ;; Unordered bullet: leave it.
1588 (t bullet))))
1590 (defun org-list-struct-fix-bul (struct prevs)
1591 "Verify and correct bullets in STRUCT.
1592 PREVS is the alist of previous items, as returned by
1593 `org-list-prevs-alist'.
1595 This function modifies STRUCT."
1596 (let ((case-fold-search nil)
1597 (fix-bul
1598 (function
1599 ;; Set bullet of ITEM in STRUCT, depending on the type of
1600 ;; first item of the list, the previous bullet and counter
1601 ;; if any.
1602 (lambda (item)
1603 (let* ((prev (org-list-get-prev-item item struct prevs))
1604 (prev-bul (and prev (org-list-get-bullet prev struct)))
1605 (counter (org-list-get-counter item struct))
1606 (bullet (org-list-get-bullet item struct))
1607 (alphap (and (not prev)
1608 (org-list-use-alpha-bul-p item struct prevs))))
1609 (org-list-set-bullet
1610 item struct
1611 (org-list-bullet-string
1612 (cond
1613 ;; Alpha counter in alpha list: use counter.
1614 ((and prev counter
1615 (string-match "[a-zA-Z]" counter)
1616 (string-match "[a-zA-Z]" prev-bul))
1617 ;; Use cond to be sure `string-match' is used in
1618 ;; both cases.
1619 (let ((real-count
1620 (cond
1621 ((string-match "[a-z]" prev-bul) (downcase counter))
1622 ((string-match "[A-Z]" prev-bul) (upcase counter)))))
1623 (replace-match real-count nil nil prev-bul)))
1624 ;; Num counter in a num list: use counter.
1625 ((and prev counter
1626 (string-match "[0-9]+" counter)
1627 (string-match "[0-9]+" prev-bul))
1628 (replace-match counter nil nil prev-bul))
1629 ;; No counter: increase, if needed, previous bullet.
1630 (prev
1631 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct)))
1632 ;; Alpha counter at first item: use counter.
1633 ((and counter (org-list-use-alpha-bul-p item struct prevs)
1634 (string-match "[A-Za-z]" counter)
1635 (string-match "[A-Za-z]" bullet))
1636 (let ((real-count
1637 (cond
1638 ((string-match "[a-z]" bullet) (downcase counter))
1639 ((string-match "[A-Z]" bullet) (upcase counter)))))
1640 (replace-match real-count nil nil bullet)))
1641 ;; Num counter at first item: use counter.
1642 ((and counter
1643 (string-match "[0-9]+" counter)
1644 (string-match "[0-9]+" bullet))
1645 (replace-match counter nil nil bullet))
1646 ;; First bullet is alpha uppercase: use "A".
1647 ((and alphap (string-match "[A-Z]" bullet))
1648 (replace-match "A" nil nil bullet))
1649 ;; First bullet is alpha lowercase: use "a".
1650 ((and alphap (string-match "[a-z]" bullet))
1651 (replace-match "a" nil nil bullet))
1652 ;; First bullet is num: use "1".
1653 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet)
1654 (replace-match "1" nil nil bullet))
1655 ;; Not an ordered list: keep bullet.
1656 (t bullet)))))))))
1657 (mapc fix-bul (mapcar 'car struct))))
1659 (defun org-list-struct-fix-ind (struct parents &optional bullet-size)
1660 "Verify and correct indentation in STRUCT.
1662 PARENTS is the alist of parents, as returned by
1663 `org-list-parents-alist'.
1665 If numeric optional argument BULLET-SIZE is set, assume all
1666 bullets in list have this length to determine new indentation.
1668 This function modifies STRUCT."
1669 (let* ((ancestor (org-list-get-top-point struct))
1670 (top-ind (org-list-get-ind ancestor struct))
1671 (new-ind
1672 (lambda (item)
1673 (let ((parent (org-list-get-parent item struct parents)))
1674 (if parent
1675 ;; Indent like parent + length of parent's bullet +
1676 ;; sub-list offset.
1677 (org-list-set-ind
1678 item struct (+ (or bullet-size
1679 (length
1680 (org-list-get-bullet parent struct)))
1681 (org-list-get-ind parent struct)
1682 org-list-indent-offset))
1683 ;; If no parent, indent like top-point.
1684 (org-list-set-ind item struct top-ind))))))
1685 (mapc new-ind (mapcar 'car (cdr struct)))))
1687 (defun org-list-struct-fix-box (struct parents prevs &optional ordered)
1688 "Verify and correct checkboxes in STRUCT.
1690 PARENTS is the alist of parents and PREVS is the alist of
1691 previous items, as returned by, respectively,
1692 `org-list-parents-alist' and `org-list-prevs-alist'.
1694 If ORDERED is non-nil, a checkbox can only be checked when every
1695 checkbox before it is checked too. If there was an attempt to
1696 break this rule, the function will return the blocking item. In
1697 all others cases, the return value will be nil.
1699 This function modifies STRUCT."
1700 (let ((all-items (mapcar 'car struct))
1701 (set-parent-box
1702 (function
1703 (lambda (item)
1704 (let* ((box-list
1705 (mapcar (lambda (child)
1706 (org-list-get-checkbox child struct))
1707 (org-list-get-children item struct parents))))
1708 (org-list-set-checkbox
1709 item struct
1710 (cond
1711 ((and (member "[ ]" box-list) (member "[X]" box-list)) "[-]")
1712 ((member "[-]" box-list) "[-]")
1713 ((member "[X]" box-list) "[X]")
1714 ((member "[ ]" box-list) "[ ]")
1715 ;; Parent has no boxed child: leave box as-is.
1716 (t (org-list-get-checkbox item struct))))))))
1717 parent-list)
1718 ;; 1. List all parents with a checkbox.
1719 (mapc
1720 (lambda (e)
1721 (let* ((parent (org-list-get-parent e struct parents))
1722 (parent-box-p (org-list-get-checkbox parent struct)))
1723 (when (and parent-box-p (not (memq parent parent-list)))
1724 (push parent parent-list))))
1725 all-items)
1726 ;; 2. Sort those parents by decreasing indentation.
1727 (setq parent-list (sort parent-list
1728 (lambda (e1 e2)
1729 (> (org-list-get-ind e1 struct)
1730 (org-list-get-ind e2 struct)))))
1731 ;; 3. For each parent, get all children's checkboxes to determine
1732 ;; and set its checkbox accordingly.
1733 (mapc set-parent-box parent-list)
1734 ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
1735 (when ordered
1736 (let* ((box-list
1737 (mapcar (lambda (e) (org-list-get-checkbox e struct)) all-items))
1738 (after-unchecked (member "[ ]" box-list)))
1739 ;; There are boxes checked after an unchecked one: fix that.
1740 (when (member "[X]" after-unchecked)
1741 (let ((index (- (length struct) (length after-unchecked))))
1742 (mapc (lambda (e) (org-list-set-checkbox e struct "[ ]"))
1743 (nthcdr index all-items))
1744 ;; Verify once again the structure, without ORDERED.
1745 (org-list-struct-fix-box struct parents prevs nil)
1746 ;; Return blocking item.
1747 (nth index all-items)))))))
1749 (defun org-list-struct-apply-struct (struct old-struct)
1750 "Apply set-difference between STRUCT and OLD-STRUCT to the buffer.
1752 OLD-STRUCT is the structure before any modifications, and STRUCT
1753 the structure to be applied. The function will only modify parts
1754 of the list which have changed.
1756 Initial position of cursor is restored after the changes."
1757 (let* ((origin (copy-marker (point)))
1758 (inlinetask-re (and (featurep 'org-inlinetask)
1759 (org-inlinetask-outline-regexp)))
1760 (item-re (org-item-re))
1761 (box-rule-p (cdr (assq 'checkbox org-list-automatic-rules)))
1762 (shift-body-ind
1763 (function
1764 ;; Shift the indentation between END and BEG by DELTA.
1765 ;; Start from the line before END.
1766 (lambda (end beg delta)
1767 (goto-char end)
1768 (skip-chars-backward " \r\t\n")
1769 (beginning-of-line)
1770 (while (or (> (point) beg)
1771 (and (= (point) beg)
1772 (not (looking-at item-re))))
1773 (cond
1774 ;; Skip inline tasks.
1775 ((and inlinetask-re (looking-at inlinetask-re))
1776 (org-inlinetask-goto-beginning))
1777 ;; Shift only non-empty lines.
1778 ((org-looking-at-p "^[ \t]*\\S-")
1779 (let ((i (org-get-indentation)))
1780 (org-indent-line-to (+ i delta)))))
1781 (forward-line -1)))))
1782 (modify-item
1783 (function
1784 ;; Replace ITEM first line elements with new elements from
1785 ;; STRUCT, if appropriate.
1786 (lambda (item)
1787 (goto-char item)
1788 (let* ((new-ind (org-list-get-ind item struct))
1789 (old-ind (org-get-indentation))
1790 (new-bul (org-list-bullet-string
1791 (org-list-get-bullet item struct)))
1792 (old-bul (org-list-get-bullet item old-struct))
1793 (new-box (org-list-get-checkbox item struct)))
1794 (looking-at org-list-full-item-re)
1795 ;; a. Replace bullet
1796 (unless (equal old-bul new-bul)
1797 (replace-match new-bul nil nil nil 1))
1798 ;; b. Replace checkbox.
1799 (cond
1800 ((and new-box box-rule-p
1801 (save-match-data (org-at-item-description-p)))
1802 (message "Cannot add a checkbox to a description list item"))
1803 ((equal (match-string 3) new-box))
1804 ((and (match-string 3) new-box)
1805 (replace-match new-box nil nil nil 3))
1806 ((match-string 3)
1807 ;; (goto-char (or (match-end 2) (match-end 1)))
1808 ;; (skip-chars-backward " \t")
1809 (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
1810 (replace-match "" nil nil nil 1))
1811 (t (let ((counterp (match-end 2)))
1812 (goto-char (if counterp (1+ counterp) (match-end 1)))
1813 (insert (concat new-box (unless counterp " "))))))
1814 ;; c. Indent item to appropriate column.
1815 (unless (= new-ind old-ind)
1816 (delete-region (goto-char (point-at-bol))
1817 (progn (skip-chars-forward " \t") (point)))
1818 (indent-to new-ind)))))))
1819 ;; 1. First get list of items and position endings. We maintain
1820 ;; two alists: ITM-SHIFT, determining indentation shift needed
1821 ;; at item, and END-POS, a pseudo-alist where key is ending
1822 ;; position and value point.
1823 (let (end-list acc-end itm-shift all-ends sliced-struct)
1824 (mapc (lambda (e)
1825 (let* ((pos (car e))
1826 (ind-pos (org-list-get-ind pos struct))
1827 (ind-old (org-list-get-ind pos old-struct))
1828 (bul-pos (org-list-get-bullet pos struct))
1829 (bul-old (org-list-get-bullet pos old-struct))
1830 (ind-shift (- (+ ind-pos (length bul-pos))
1831 (+ ind-old (length bul-old))))
1832 (end-pos (org-list-get-item-end pos old-struct)))
1833 (push (cons pos ind-shift) itm-shift)
1834 (unless (assq end-pos old-struct)
1835 ;; To determine real ind of an ending position that
1836 ;; is not at an item, we have to find the item it
1837 ;; belongs to: it is the last item (ITEM-UP), whose
1838 ;; ending is further than the position we're
1839 ;; interested in.
1840 (let ((item-up (assoc-default end-pos acc-end '>)))
1841 (push (cons end-pos item-up) end-list)))
1842 (push (cons end-pos pos) acc-end)))
1843 old-struct)
1844 ;; 2. Slice the items into parts that should be shifted by the
1845 ;; same amount of indentation. The slices are returned in
1846 ;; reverse order so changes modifying buffer do not change
1847 ;; positions they refer to.
1848 (setq all-ends (sort (append (mapcar 'car itm-shift)
1849 (org-uniquify (mapcar 'car end-list)))
1850 '<))
1851 (while (cdr all-ends)
1852 (let* ((up (pop all-ends))
1853 (down (car all-ends))
1854 (ind (if (assq up struct)
1855 (cdr (assq up itm-shift))
1856 (cdr (assq (cdr (assq up end-list)) itm-shift)))))
1857 (push (list down up ind) sliced-struct)))
1858 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1859 ;; end to beginning. Take a special action when beginning is
1860 ;; at item bullet.
1861 (mapc (lambda (e)
1862 (unless (zerop (nth 2 e)) (apply shift-body-ind e))
1863 (let* ((beg (nth 1 e))
1864 (cell (assq beg struct)))
1865 (unless (or (not cell) (equal cell (assq beg old-struct)))
1866 (funcall modify-item beg))))
1867 sliced-struct))
1868 ;; 4. Go back to initial position and clean marker.
1869 (goto-char origin)
1870 (move-marker origin nil)))
1872 (defun org-list-write-struct (struct parents)
1873 "Correct bullets, checkboxes and indentation in list at point.
1874 STRUCT is the list structure. PARENTS is the alist of parents,
1875 as returned by `org-list-parents-alist'."
1876 ;; Order of functions matters here: checkboxes and endings need
1877 ;; correct indentation to be set, and indentation needs correct
1878 ;; bullets.
1880 ;; 0. Save a copy of structure before modifications
1881 (let ((old-struct (copy-tree struct)))
1882 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
1883 ;; order to get items endings and bullets properly
1884 (org-list-struct-fix-ind struct parents 2)
1885 ;; 2. Get pseudo-alist of ending positions and sort it by position.
1886 ;; Then associate them to the structure.
1887 (let (end-list acc-end)
1888 (mapc (lambda (e)
1889 (let* ((pos (car e))
1890 (ind-pos (org-list-get-ind pos struct))
1891 (end-pos (org-list-get-item-end pos struct)))
1892 (unless (assq end-pos struct)
1893 ;; To determine real ind of an ending position that is
1894 ;; not at an item, we have to find the item it belongs
1895 ;; to: it is the last item (ITEM-UP), whose ending is
1896 ;; further than the position we're interested in.
1897 (let ((item-up (assoc-default end-pos acc-end '>)))
1898 (push (cons
1899 ;; Else part is for the bottom point.
1900 (if item-up (+ (org-list-get-ind item-up struct) 2) 0)
1901 end-pos)
1902 end-list)))
1903 (push (cons ind-pos pos) end-list)
1904 (push (cons end-pos pos) acc-end)))
1905 struct)
1906 (setq end-list (sort end-list (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
1907 (org-list-struct-assoc-end struct end-list))
1908 ;; 3. Get bullets right.
1909 (let ((prevs (org-list-prevs-alist struct)))
1910 (org-list-struct-fix-bul struct prevs)
1911 ;; 4. Now get real indentation.
1912 (org-list-struct-fix-ind struct parents)
1913 ;; 5. Eventually fix checkboxes.
1914 (org-list-struct-fix-box struct parents prevs))
1915 ;; 6. Apply structure modifications to buffer.
1916 (org-list-struct-apply-struct struct old-struct)))
1919 ;;; Misc Tools
1921 (defun org-apply-on-list (function init-value &rest args)
1922 "Call FUNCTION on each item of the list at point.
1923 FUNCTION must be called with at least one argument: INIT-VALUE,
1924 that will contain the value returned by the function at the
1925 previous item, plus ARGS extra arguments.
1927 FUNCTION is applied on items in reverse order.
1929 As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
1930 will return the number of items in the current list.
1932 Sublists of the list are skipped. Cursor is always at the
1933 beginning of the item."
1934 (let* ((struct (org-list-struct))
1935 (prevs (org-list-prevs-alist struct))
1936 (item (copy-marker (point-at-bol)))
1937 (all (org-list-get-all-items (marker-position item) struct prevs))
1938 (value init-value))
1939 (mapc (lambda (e)
1940 (goto-char e)
1941 (setq value (apply function value args)))
1942 (nreverse all))
1943 (goto-char item)
1944 (move-marker item nil)
1945 value))
1947 (defun org-list-set-item-visibility (item struct view)
1948 "Set visibility of ITEM in STRUCT to VIEW.
1950 Possible values are: `folded', `children' or `subtree'. See
1951 `org-cycle' for more information."
1952 (cond
1953 ((eq view 'folded)
1954 (let ((item-end (org-list-get-item-end-before-blank item struct)))
1955 ;; Hide from eol
1956 (outline-flag-region (save-excursion (goto-char item) (point-at-eol))
1957 item-end t)))
1958 ((eq view 'children)
1959 ;; First show everything.
1960 (org-list-set-item-visibility item struct 'subtree)
1961 ;; Then fold every child.
1962 (let* ((parents (org-list-parents-alist struct))
1963 (children (org-list-get-children item struct parents)))
1964 (mapc (lambda (e)
1965 (org-list-set-item-visibility e struct 'folded))
1966 children)))
1967 ((eq view 'subtree)
1968 ;; Show everything
1969 (let ((item-end (org-list-get-item-end item struct)))
1970 (outline-flag-region item item-end nil)))))
1972 (defun org-list-item-body-column (item)
1973 "Return column at which body of ITEM should start."
1974 (let (bpos bcol tpos tcol)
1975 (save-excursion
1976 (goto-char item)
1977 (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?[ \t]+")
1978 (setq bpos (match-beginning 1) tpos (match-end 0)
1979 bcol (progn (goto-char bpos) (current-column))
1980 tcol (progn (goto-char tpos) (current-column)))
1981 (when (> tcol (+ bcol org-description-max-indent))
1982 (setq tcol (+ bcol 5))))
1983 tcol))
1986 ;;; Interactive functions
1988 (defalias 'org-list-get-item-begin 'org-in-item-p)
1990 (defun org-beginning-of-item ()
1991 "Go to the beginning of the current item.
1992 Throw an error when not in a list."
1993 (interactive)
1994 (let ((begin (org-in-item-p)))
1995 (if begin (goto-char begin) (error "Not in an item"))))
1997 (defun org-beginning-of-item-list ()
1998 "Go to the beginning item of the current list or sublist.
1999 Throw an error when not in a list."
2000 (interactive)
2001 (let ((begin (org-in-item-p)))
2002 (if (not begin)
2003 (error "Not in an item")
2004 (goto-char begin)
2005 (let* ((struct (org-list-struct))
2006 (prevs (org-list-prevs-alist struct)))
2007 (goto-char (org-list-get-list-begin begin struct prevs))))))
2009 (defun org-end-of-item-list ()
2010 "Go to the end of the current list or sublist.
2011 Throw an error when not in a list."
2012 (interactive)
2013 (let ((begin (org-in-item-p)))
2014 (if (not begin)
2015 (error "Not in an item")
2016 (goto-char begin)
2017 (let* ((struct (org-list-struct))
2018 (prevs (org-list-prevs-alist struct)))
2019 (goto-char (org-list-get-list-end begin struct prevs))))))
2021 (defun org-end-of-item ()
2022 "Go to the end of the current item.
2023 Throw an error when not in a list."
2024 (interactive)
2025 (let ((begin (org-in-item-p)))
2026 (if (not begin)
2027 (error "Not in an item")
2028 (goto-char begin)
2029 (let ((struct (org-list-struct)))
2030 (goto-char (org-list-get-item-end begin struct))))))
2032 (defun org-previous-item ()
2033 "Move to the beginning of the previous item.
2034 Throw an error when not in a list. Also throw an error when at
2035 first item, unless `org-list-use-circular-motion' is non-nil."
2036 (interactive)
2037 (let ((item (org-in-item-p)))
2038 (if (not item)
2039 (error "Not in an item")
2040 (goto-char item)
2041 (let* ((struct (org-list-struct))
2042 (prevs (org-list-prevs-alist struct))
2043 (prevp (org-list-get-prev-item item struct prevs)))
2044 (cond
2045 (prevp (goto-char prevp))
2046 (org-list-use-circular-motion
2047 (goto-char (org-list-get-last-item item struct prevs)))
2048 (t (error "On first item")))))))
2050 (defun org-next-item ()
2051 "Move to the beginning of the next item.
2052 Throw an error when not in a list. Also throw an error when at
2053 last item, unless `org-list-use-circular-motion' is non-nil."
2054 (interactive)
2055 (let ((item (org-in-item-p)))
2056 (if (not item)
2057 (error "Not in an item")
2058 (goto-char item)
2059 (let* ((struct (org-list-struct))
2060 (prevs (org-list-prevs-alist struct))
2061 (prevp (org-list-get-next-item item struct prevs)))
2062 (cond
2063 (prevp (goto-char prevp))
2064 (org-list-use-circular-motion
2065 (goto-char (org-list-get-first-item item struct prevs)))
2066 (t (error "On last item")))))))
2068 (defun org-move-item-down ()
2069 "Move the item at point down, i.e. swap with following item.
2070 Sub-items (items with larger indentation) are considered part of
2071 the item, so this really moves item trees."
2072 (interactive)
2073 (unless (org-at-item-p) (error "Not at an item"))
2074 (let* ((col (current-column))
2075 (item (point-at-bol))
2076 (struct (org-list-struct))
2077 (prevs (org-list-prevs-alist struct))
2078 (next-item (org-list-get-next-item (point-at-bol) struct prevs)))
2079 (unless (or next-item org-list-use-circular-motion)
2080 (error "Cannot move this item further down"))
2081 (if (not next-item)
2082 (setq struct (org-list-send-item item 'begin struct))
2083 (setq struct (org-list-swap-items item next-item struct))
2084 (goto-char
2085 (org-list-get-next-item item struct (org-list-prevs-alist struct))))
2086 (org-list-write-struct struct (org-list-parents-alist struct))
2087 (org-move-to-column col)))
2089 (defun org-move-item-up ()
2090 "Move the item at point up, i.e. swap with previous item.
2091 Sub-items (items with larger indentation) are considered part of
2092 the item, so this really moves item trees."
2093 (interactive)
2094 (unless (org-at-item-p) (error "Not at an item"))
2095 (let* ((col (current-column))
2096 (item (point-at-bol))
2097 (struct (org-list-struct))
2098 (prevs (org-list-prevs-alist struct))
2099 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs)))
2100 (unless (or prev-item org-list-use-circular-motion)
2101 (error "Cannot move this item further up"))
2102 (if (not prev-item)
2103 (setq struct (org-list-send-item item 'end struct))
2104 (setq struct (org-list-swap-items prev-item item struct)))
2105 (org-list-write-struct struct (org-list-parents-alist struct))
2106 (org-move-to-column col)))
2108 (defun org-insert-item (&optional checkbox)
2109 "Insert a new item at the current level.
2110 If cursor is before first character after bullet of the item, the
2111 new item will be created before the current one.
2113 If CHECKBOX is non-nil, add a checkbox next to the bullet.
2115 Return t when things worked, nil when we are not in an item, or
2116 item is invisible."
2117 (let ((itemp (org-in-item-p))
2118 (pos (point)))
2119 ;; If cursor isn't is a list or if list is invisible, return nil.
2120 (unless (or (not itemp)
2121 (save-excursion
2122 (goto-char itemp)
2123 (outline-invisible-p)))
2124 (if (save-excursion
2125 (goto-char itemp)
2126 (org-at-item-timer-p))
2127 ;; Timer list: delegate to `org-timer-item'.
2128 (progn (org-timer-item) t)
2129 (let* ((struct (save-excursion (goto-char itemp)
2130 (org-list-struct)))
2131 (prevs (org-list-prevs-alist struct))
2132 ;; If we're in a description list, ask for the new term.
2133 (desc (when (org-list-get-tag itemp struct)
2134 (concat (read-string "Term: ") " :: ")))
2135 ;; Don't insert a checkbox if checkbox rule is applied
2136 ;; and it is a description item.
2137 (checkp (and checkbox
2138 (or (not desc)
2139 (not (cdr (assq 'checkbox
2140 org-list-automatic-rules)))))))
2141 (setq struct
2142 (org-list-insert-item pos struct prevs checkp desc))
2143 (org-list-write-struct struct (org-list-parents-alist struct))
2144 (when checkp (org-update-checkbox-count-maybe))
2145 (looking-at org-list-full-item-re)
2146 (goto-char (match-end 0))
2147 t)))))
2149 (defun org-list-repair ()
2150 "Fix indentation, bullets and checkboxes is the list at point."
2151 (interactive)
2152 (unless (org-at-item-p) (error "This is not a list"))
2153 (let* ((struct (org-list-struct))
2154 (parents (org-list-parents-alist struct)))
2155 (org-list-write-struct struct parents)))
2157 (defun org-cycle-list-bullet (&optional which)
2158 "Cycle through the different itemize/enumerate bullets.
2159 This cycle the entire list level through the sequence:
2161 `-' -> `+' -> `*' -> `1.' -> `1)'
2163 If WHICH is a valid string, use that as the new bullet. If WHICH
2164 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2165 `previous', cycle backwards."
2166 (interactive "P")
2167 (unless (org-at-item-p) (error "Not at an item"))
2168 (save-excursion
2169 (beginning-of-line)
2170 (let* ((struct (org-list-struct))
2171 (parents (org-list-parents-alist struct))
2172 (prevs (org-list-prevs-alist struct))
2173 (list-beg (org-list-get-first-item (point) struct prevs))
2174 (bullet (org-list-get-bullet list-beg struct))
2175 (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
2176 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs))
2177 (case-fold-search nil)
2178 (current (cond
2179 ((string-match "[a-z]\\." bullet) "a.")
2180 ((string-match "[a-z])" bullet) "a)")
2181 ((string-match "[A-Z]\\." bullet) "A.")
2182 ((string-match "[A-Z])" bullet) "A)")
2183 ((string-match "\\." bullet) "1.")
2184 ((string-match ")" bullet) "1)")
2185 (t (org-trim bullet))))
2186 ;; Compute list of possible bullets, depending on context.
2187 (bullet-list
2188 (append '("-" "+" )
2189 ;; *-bullets are not allowed at column 0.
2190 (unless (and bullet-rule-p
2191 (looking-at "\\S-")) '("*"))
2192 ;; Description items cannot be numbered.
2193 (unless (or (eq org-plain-list-ordered-item-terminator ?\))
2194 (and bullet-rule-p (org-at-item-description-p)))
2195 '("1."))
2196 (unless (or (eq org-plain-list-ordered-item-terminator ?.)
2197 (and bullet-rule-p (org-at-item-description-p)))
2198 '("1)"))
2199 (unless (or (not alpha-p)
2200 (eq org-plain-list-ordered-item-terminator ?\))
2201 (and bullet-rule-p (org-at-item-description-p)))
2202 '("a." "A."))
2203 (unless (or (not alpha-p)
2204 (eq org-plain-list-ordered-item-terminator ?.)
2205 (and bullet-rule-p (org-at-item-description-p)))
2206 '("a)" "A)"))))
2207 (len (length bullet-list))
2208 (item-index (- len (length (member current bullet-list))))
2209 (get-value (lambda (index) (nth (mod index len) bullet-list)))
2210 (new (cond
2211 ((member which bullet-list) which)
2212 ((numberp which) (funcall get-value which))
2213 ((eq 'previous which) (funcall get-value (1- item-index)))
2214 (t (funcall get-value (1+ item-index))))))
2215 ;; Use a short variation of `org-list-write-struct' as there's
2216 ;; no need to go through all the steps.
2217 (let ((old-struct (copy-tree struct)))
2218 (org-list-set-bullet list-beg struct (org-list-bullet-string new))
2219 (org-list-struct-fix-bul struct prevs)
2220 (org-list-struct-fix-ind struct parents)
2221 (org-list-struct-apply-struct struct old-struct)))))
2223 (defun org-toggle-checkbox (&optional toggle-presence)
2224 "Toggle the checkbox in the current line.
2225 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2226 double prefix, set checkbox to [-].
2228 When there is an active region, toggle status or presence of the
2229 first checkbox there, and make every item inside have the same
2230 status or presence, respectively.
2232 If the cursor is in a headline, apply this to all checkbox items
2233 in the text below the heading, taking as reference the first item
2234 in subtree, ignoring drawers."
2235 (interactive "P")
2236 (save-excursion
2237 (let* (singlep
2238 block-item
2239 lim-up
2240 lim-down
2241 (drawer-re (concat "^[ \t]*:\\("
2242 (mapconcat 'regexp-quote org-drawers "\\|")
2243 "\\):[ \t]*$"))
2244 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2245 "\\|" org-deadline-string
2246 "\\|" org-closed-string
2247 "\\|" org-clock-string "\\)"
2248 " *[[<]\\([^]>]+\\)[]>]"))
2249 (orderedp (org-entry-get nil "ORDERED"))
2250 (bounds
2251 ;; In a region, start at first item in region.
2252 (cond
2253 ((org-region-active-p)
2254 (let ((limit (region-end)))
2255 (goto-char (region-beginning))
2256 (if (org-list-search-forward (org-item-beginning-re) limit t)
2257 (setq lim-up (point-at-bol))
2258 (error "No item in region"))
2259 (setq lim-down (copy-marker limit))))
2260 ((org-on-heading-p)
2261 ;; On an heading, start at first item after drawers and
2262 ;; time-stamps (scheduled, etc.).
2263 (let ((limit (save-excursion (outline-next-heading) (point))))
2264 (forward-line 1)
2265 (while (or (looking-at drawer-re) (looking-at keyword-re))
2266 (if (looking-at keyword-re)
2267 (forward-line 1)
2268 (re-search-forward "^[ \t]*:END:" limit nil)))
2269 (if (org-list-search-forward (org-item-beginning-re) limit t)
2270 (setq lim-up (point-at-bol))
2271 (error "No item in subtree"))
2272 (setq lim-down (copy-marker limit))))
2273 ;; Just one item: set SINGLEP flag.
2274 ((org-at-item-p)
2275 (setq singlep t)
2276 (setq lim-up (point-at-bol)
2277 lim-down (point-at-eol)))
2278 (t (error "Not at an item or heading, and no active region"))))
2279 ;; Determine the checkbox going to be applied to all items
2280 ;; within bounds.
2281 (ref-checkbox
2282 (progn
2283 (goto-char lim-up)
2284 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2285 (cond
2286 ((equal toggle-presence '(16)) "[-]")
2287 ((equal toggle-presence '(4))
2288 (unless cbox "[ ]"))
2289 ((equal "[X]" cbox) "[ ]")
2290 (t "[X]"))))))
2291 ;; When an item is found within bounds, grab the full list at
2292 ;; point structure, then: (1) set check-box of all its items
2293 ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
2294 ;; whole list, (3) move point after the list.
2295 (goto-char lim-up)
2296 (while (and (< (point) lim-down)
2297 (org-list-search-forward (org-item-beginning-re)
2298 lim-down 'move))
2299 (let* ((struct (org-list-struct))
2300 (struct-copy (copy-tree struct))
2301 (parents (org-list-parents-alist struct))
2302 (prevs (org-list-prevs-alist struct))
2303 (bottom (copy-marker (org-list-get-bottom-point struct)))
2304 (items-to-toggle (org-remove-if
2305 (lambda (e) (or (< e lim-up) (> e lim-down)))
2306 (mapcar 'car struct))))
2307 (mapc (lambda (e) (org-list-set-checkbox
2308 e struct
2309 ;; If there is no box at item, leave as-is
2310 ;; unless function was called with C-u prefix.
2311 (let ((cur-box (org-list-get-checkbox e struct)))
2312 (if (or cur-box (equal toggle-presence '(4)))
2313 ref-checkbox
2314 cur-box))))
2315 items-to-toggle)
2316 (setq block-item (org-list-struct-fix-box
2317 struct parents prevs orderedp))
2318 ;; Report some problems due to ORDERED status of subtree.
2319 ;; If only one box was being checked, throw an error, else,
2320 ;; only signal problems.
2321 (cond
2322 ((and singlep block-item (> lim-up block-item))
2323 (error
2324 "Checkbox blocked because of unchecked box at line %d"
2325 (org-current-line block-item)))
2326 (block-item
2327 (message
2328 "Checkboxes were removed due to unchecked box at line %d"
2329 (org-current-line block-item))))
2330 (goto-char bottom)
2331 (move-marker lim-down nil)
2332 (move-marker bottom nil)
2333 (org-list-struct-apply-struct struct struct-copy)))))
2334 (org-update-checkbox-count-maybe))
2336 (defun org-reset-checkbox-state-subtree ()
2337 "Reset all checkboxes in an entry subtree."
2338 (interactive "*")
2339 (if (org-before-first-heading-p)
2340 (error "Not inside a tree")
2341 (save-restriction
2342 (save-excursion
2343 (org-narrow-to-subtree)
2344 (org-show-subtree)
2345 (goto-char (point-min))
2346 (let ((end (point-max)))
2347 (while (< (point) end)
2348 (when (org-at-item-checkbox-p)
2349 (replace-match "[ ]" t t nil 1))
2350 (beginning-of-line 2)))
2351 (org-update-checkbox-count-maybe 'all)))))
2353 (defun org-update-checkbox-count (&optional all)
2354 "Update the checkbox statistics in the current section.
2355 This will find all statistic cookies like [57%] and [6/12] and
2356 update them with the current numbers.
2358 With optional prefix argument ALL, do this for the whole buffer."
2359 (interactive "P")
2360 (save-excursion
2361 (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2362 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2363 (recursivep
2364 (or (not org-hierarchical-checkbox-statistics)
2365 (string-match "\\<recursive\\>"
2366 (or (org-entry-get nil "COOKIE_DATA") ""))))
2367 (bounds (if all
2368 (cons (point-min) (point-max))
2369 (cons (or (ignore-errors (org-back-to-heading t) (point))
2370 (point-min))
2371 (save-excursion (outline-next-heading) (point)))))
2372 (count-boxes
2373 (function
2374 ;; Return number of checked boxes and boxes of all types
2375 ;; in all structures in STRUCTS. If RECURSIVEP is
2376 ;; non-nil, also count boxes in sub-lists. If ITEM is
2377 ;; nil, count across the whole structure, else count only
2378 ;; across subtree whose ancestor is ITEM.
2379 (lambda (item structs recursivep)
2380 (let ((c-on 0) (c-all 0))
2381 (mapc
2382 (lambda (s)
2383 (let* ((pre (org-list-prevs-alist s))
2384 (par (org-list-parents-alist s))
2385 (items
2386 (cond
2387 ((and recursivep item) (org-list-get-subtree item s))
2388 (recursivep (mapcar 'car s))
2389 (item (org-list-get-children item s par))
2390 (t (org-list-get-all-items
2391 (org-list-get-top-point s) s pre))))
2392 (cookies (delq nil (mapcar
2393 (lambda (e)
2394 (org-list-get-checkbox e s))
2395 items))))
2396 (setq c-all (+ (length cookies) c-all)
2397 c-on (+ (org-count "[X]" cookies) c-on))))
2398 structs)
2399 (cons c-on c-all)))))
2400 (backup-end 1)
2401 cookies-list structs-bak box-num)
2402 (goto-char (car bounds))
2403 ;; 1. Build an alist for each cookie found within BOUNDS. The
2404 ;; key will be position at beginning of cookie and values
2405 ;; ending position, format of cookie, and a cell whose car is
2406 ;; number of checked boxes to report, and cdr total number of
2407 ;; boxes.
2408 (while (re-search-forward cookie-re (cdr bounds) t)
2409 (catch 'skip
2410 (save-excursion
2411 (push
2412 (list
2413 (match-beginning 1) ; cookie start
2414 (match-end 1) ; cookie end
2415 (match-string 2) ; percent?
2416 (cond ; boxes count
2417 ;; Cookie is at an heading, but specifically for todo,
2418 ;; not for checkboxes: skip it.
2419 ((and (org-on-heading-p)
2420 (string-match "\\<todo\\>"
2421 (downcase
2422 (or (org-entry-get nil "COOKIE_DATA") ""))))
2423 (throw 'skip nil))
2424 ;; Cookie is at an heading, but all lists before next
2425 ;; heading already have been read. Use data collected
2426 ;; in STRUCTS-BAK. This should only happen when
2427 ;; heading has more than one cookie on it.
2428 ((and (org-on-heading-p)
2429 (<= (save-excursion (outline-next-heading) (point))
2430 backup-end))
2431 (funcall count-boxes nil structs-bak recursivep))
2432 ;; Cookie is at a fresh heading. Grab structure of
2433 ;; every list containing a checkbox between point and
2434 ;; next headline, and save them in STRUCTS-BAK.
2435 ((org-on-heading-p)
2436 (setq backup-end (save-excursion
2437 (outline-next-heading) (point))
2438 structs-bak nil)
2439 (while (org-list-search-forward box-re backup-end 'move)
2440 (let* ((struct (org-list-struct))
2441 (bottom (org-list-get-bottom-point struct)))
2442 (push struct structs-bak)
2443 (goto-char bottom)))
2444 (funcall count-boxes nil structs-bak recursivep))
2445 ;; Cookie is at an item, and we already have list
2446 ;; structure stored in STRUCTS-BAK.
2447 ((and (org-at-item-p)
2448 (< (point-at-bol) backup-end)
2449 ;; Only lists in no special context are stored.
2450 (not (nth 2 (org-list-context))))
2451 (funcall count-boxes (point-at-bol) structs-bak recursivep))
2452 ;; Cookie is at an item, but we need to compute list
2453 ;; structure.
2454 ((org-at-item-p)
2455 (let ((struct (org-list-struct)))
2456 (setq backup-end (org-list-get-bottom-point struct)
2457 structs-bak (list struct)))
2458 (funcall count-boxes (point-at-bol) structs-bak recursivep))
2459 ;; Else, cookie found is at a wrong place. Skip it.
2460 (t (throw 'skip nil))))
2461 cookies-list))))
2462 ;; 2. Apply alist to buffer, in reverse order so positions stay
2463 ;; unchanged after cookie modifications.
2464 (mapc (lambda (cookie)
2465 (let* ((beg (car cookie))
2466 (end (nth 1 cookie))
2467 (percentp (nth 2 cookie))
2468 (checked (car (nth 3 cookie)))
2469 (total (cdr (nth 3 cookie)))
2470 (new (if percentp
2471 (format "[%d%%]" (/ (* 100 checked)
2472 (max 1 total)))
2473 (format "[%d/%d]" checked total))))
2474 (goto-char beg)
2475 (insert new)
2476 (delete-region (point) (+ (point) (- end beg)))
2477 (when org-auto-align-tags (org-fix-tags-on-the-fly))))
2478 cookies-list))))
2480 (defun org-get-checkbox-statistics-face ()
2481 "Select the face for checkbox statistics.
2482 The face will be `org-done' when all relevant boxes are checked.
2483 Otherwise it will be `org-todo'."
2484 (if (match-end 1)
2485 (if (equal (match-string 1) "100%")
2486 'org-checkbox-statistics-done
2487 'org-checkbox-statistics-todo)
2488 (if (and (> (match-end 2) (match-beginning 2))
2489 (equal (match-string 2) (match-string 3)))
2490 'org-checkbox-statistics-done
2491 'org-checkbox-statistics-todo)))
2493 (defun org-update-checkbox-count-maybe (&optional all)
2494 "Update checkbox statistics unless turned off by user."
2495 (when (cdr (assq 'checkbox org-list-automatic-rules))
2496 (org-update-checkbox-count all))
2497 (run-hooks 'org-checkbox-statistics-hook))
2499 (defvar org-last-indent-begin-marker (make-marker))
2500 (defvar org-last-indent-end-marker (make-marker))
2501 (defun org-list-indent-item-generic (arg no-subtree struct)
2502 "Indent a local list item including its children.
2503 When number ARG is a negative, item will be outdented, otherwise
2504 it will be indented.
2506 If a region is active, all items inside will be moved.
2508 If NO-SUBTREE is non-nil, only indent the item itself, not its
2509 children.
2511 STRUCT is the list structure.
2513 Return t if successful."
2514 (save-excursion
2515 (beginning-of-line)
2516 (let* ((regionp (org-region-active-p))
2517 (rbeg (and regionp (region-beginning)))
2518 (rend (and regionp (region-end)))
2519 (top (org-list-get-top-point struct))
2520 (parents (org-list-parents-alist struct))
2521 (prevs (org-list-prevs-alist struct))
2522 ;; Are we going to move the whole list?
2523 (specialp
2524 (and (= top (point))
2525 (cdr (assq 'indent org-list-automatic-rules))
2526 (if no-subtree
2527 (error
2528 "First item of list cannot move without its subtree")
2529 t))))
2530 ;; Determine begin and end points of zone to indent. If moving
2531 ;; more than one item, save them for subsequent moves.
2532 (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
2533 (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
2534 (if regionp
2535 (progn
2536 (set-marker org-last-indent-begin-marker rbeg)
2537 (set-marker org-last-indent-end-marker rend))
2538 (set-marker org-last-indent-begin-marker (point))
2539 (set-marker org-last-indent-end-marker
2540 (cond
2541 (specialp (org-list-get-bottom-point struct))
2542 (no-subtree (1+ (point)))
2543 (t (org-list-get-item-end (point) struct))))))
2544 (let* ((beg (marker-position org-last-indent-begin-marker))
2545 (end (marker-position org-last-indent-end-marker)))
2546 (cond
2547 ;; Special case: moving top-item with indent rule.
2548 (specialp
2549 (let* ((level-skip (org-level-increment))
2550 (offset (if (< arg 0) (- level-skip) level-skip))
2551 (top-ind (org-list-get-ind beg struct))
2552 (old-struct (copy-tree struct)))
2553 (if (< (+ top-ind offset) 0)
2554 (error "Cannot outdent beyond margin")
2555 ;; Change bullet if necessary.
2556 (when (and (= (+ top-ind offset) 0)
2557 (string-match "*"
2558 (org-list-get-bullet beg struct)))
2559 (org-list-set-bullet beg struct
2560 (org-list-bullet-string "-")))
2561 ;; Shift every item by OFFSET and fix bullets. Then
2562 ;; apply changes to buffer.
2563 (mapc (lambda (e)
2564 (let ((ind (org-list-get-ind (car e) struct)))
2565 (org-list-set-ind (car e) struct (+ ind offset))))
2566 struct)
2567 (org-list-struct-fix-bul struct prevs)
2568 (org-list-struct-apply-struct struct old-struct))))
2569 ;; Forbidden move:
2570 ((and (< arg 0)
2571 ;; If only one item is moved, it mustn't have a child.
2572 (or (and no-subtree
2573 (not regionp)
2574 (org-list-has-child-p beg struct))
2575 ;; If a subtree or region is moved, the last item
2576 ;; of the subtree mustn't have a child.
2577 (let ((last-item (caar
2578 (reverse
2579 (org-remove-if
2580 (lambda (e) (>= (car e) end))
2581 struct)))))
2582 (org-list-has-child-p last-item struct))))
2583 (error "Cannot outdent an item without its children"))
2584 ;; Normal shifting
2586 (let* ((new-parents
2587 (if (< arg 0)
2588 (org-list-struct-outdent beg end struct parents)
2589 (org-list-struct-indent beg end struct parents prevs))))
2590 (org-list-write-struct struct new-parents))
2591 (org-update-checkbox-count-maybe))))))
2594 (defun org-outdent-item ()
2595 "Outdent a local list item, but not its children.
2596 If a region is active, all items inside will be moved."
2597 (interactive)
2598 (if (org-at-item-p)
2599 (let ((struct (org-list-struct)))
2600 (org-list-indent-item-generic -1 t struct))
2601 (error "Not at an item")))
2603 (defun org-indent-item ()
2604 "Indent a local list item, but not its children.
2605 If a region is active, all items inside will be moved."
2606 (interactive)
2607 (if (org-at-item-p)
2608 (let ((struct (org-list-struct)))
2609 (org-list-indent-item-generic 1 t struct))
2610 (error "Not at an item")))
2612 (defun org-outdent-item-tree ()
2613 "Outdent a local list item including its children.
2614 If a region is active, all items inside will be moved."
2615 (interactive)
2616 (let ((regionp (org-region-active-p)))
2617 (cond
2618 ((or (org-at-item-p)
2619 (and (org-region-active-p)
2620 (goto-char (region-beginning))
2621 (org-at-item-p)))
2622 (let ((struct (org-list-struct)))
2623 (org-list-indent-item-generic -1 nil struct)))
2624 (regionp (error "Region not starting at an item"))
2625 (t (error "Not at an item")))))
2627 (defun org-indent-item-tree ()
2628 "Indent a local list item including its children.
2629 If a region is active, all items inside will be moved."
2630 (interactive)
2631 (let ((regionp (org-region-active-p)))
2632 (cond
2633 ((or (org-at-item-p)
2634 (and (org-region-active-p)
2635 (goto-char (region-beginning))
2636 (org-at-item-p)))
2637 (let ((struct (org-list-struct)))
2638 (org-list-indent-item-generic 1 nil struct)))
2639 (regionp (error "Region not starting at an item"))
2640 (t (error "Not at an item")))))
2642 (defvar org-tab-ind-state)
2643 (defun org-cycle-item-indentation ()
2644 "Cycle levels of indentation of an empty item.
2645 The first run indents the item, if applicable. Subsequents runs
2646 outdent it at meaningful levels in the list. When done, item is
2647 put back at its original position with its original bullet.
2649 Return t at each successful move."
2650 (when (org-at-item-p)
2651 (let* ((org-adapt-indentation nil)
2652 (struct (org-list-struct))
2653 (ind (org-list-get-ind (point-at-bol) struct))
2654 (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
2655 ;; Accept empty items or if cycle has already started.
2656 (when (or (eq last-command 'org-cycle-item-indentation)
2657 (and (save-excursion
2658 (beginning-of-line)
2659 (looking-at org-list-full-item-re))
2660 (>= (match-end 0) (save-excursion
2661 (goto-char (org-list-get-item-end
2662 (point-at-bol) struct))
2663 (skip-chars-backward " \r\t\n")
2664 (point)))))
2665 (setq this-command 'org-cycle-item-indentation)
2666 ;; When in the middle of the cycle, try to outdent first. If
2667 ;; it fails, and point is still at initial position, indent.
2668 ;; Else, re-create it at its original position.
2669 (if (eq last-command 'org-cycle-item-indentation)
2670 (cond
2671 ((ignore-errors (org-list-indent-item-generic -1 t struct)))
2672 ((and (= ind (car org-tab-ind-state))
2673 (ignore-errors (org-list-indent-item-generic 1 t struct))))
2674 (t (delete-region (point-at-bol) (point-at-eol))
2675 (org-indent-to-column (car org-tab-ind-state))
2676 (insert (cdr org-tab-ind-state) " ")
2677 ;; Break cycle
2678 (setq this-command 'identity)))
2679 ;; If a cycle is starting, remember indentation and bullet,
2680 ;; then try to indent. If it fails, try to outdent.
2681 (setq org-tab-ind-state (cons ind bullet))
2682 (cond
2683 ((ignore-errors (org-list-indent-item-generic 1 t struct)))
2684 ((ignore-errors (org-list-indent-item-generic -1 t struct)))
2685 (t (error "Cannot move item"))))
2686 t))))
2688 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
2689 "Sort list items.
2690 The cursor may be at any item of the list that should be sorted.
2691 Sublists are not sorted. Checkboxes, if any, are ignored.
2693 Sorting can be alphabetically, numerically, by date/time as given
2694 by a time stamp, by a property or by priority.
2696 Comparing entries ignores case by default. However, with an
2697 optional argument WITH-CASE, the sorting considers case as well.
2699 The command prompts for the sorting type unless it has been given
2700 to the function through the SORTING-TYPE argument, which needs to
2701 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
2702 meaning of each character:
2704 n Numerically, by converting the beginning of the item to a number.
2705 a Alphabetically. Only the first line of item is checked.
2706 t By date/time, either the first active time stamp in the entry, if
2707 any, or by the first inactive one. In a timer list, sort the timers.
2709 Capital letters will reverse the sort order.
2711 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
2712 a function to be called with point at the beginning of the
2713 record. It must return either a string or a number that should
2714 serve as the sorting key for that record. It will then use
2715 COMPARE-FUNC to compare entries."
2716 (interactive "P")
2717 (let* ((case-func (if with-case 'identity 'downcase))
2718 (struct (org-list-struct))
2719 (prevs (org-list-prevs-alist struct))
2720 (start (org-list-get-list-begin (point-at-bol) struct prevs))
2721 (end (org-list-get-list-end (point-at-bol) struct prevs))
2722 (sorting-type
2723 (progn
2724 (message
2725 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
2726 (read-char-exclusive)))
2727 (getkey-func (and (= (downcase sorting-type) ?f)
2728 (intern (org-icompleting-read "Sort using function: "
2729 obarray 'fboundp t nil nil)))))
2730 (message "Sorting items...")
2731 (save-restriction
2732 (narrow-to-region start end)
2733 (goto-char (point-min))
2734 (let* ((dcst (downcase sorting-type))
2735 (case-fold-search nil)
2736 (now (current-time))
2737 (sort-func (cond
2738 ((= dcst ?a) 'string<)
2739 ((= dcst ?f) compare-func)
2740 ((= dcst ?t) '<)
2741 (t nil)))
2742 (next-record (lambda ()
2743 (skip-chars-forward " \r\t\n")
2744 (beginning-of-line)))
2745 (end-record (lambda ()
2746 (goto-char (org-list-get-item-end-before-blank
2747 (point) struct))))
2748 (value-to-sort
2749 (lambda ()
2750 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2751 (cond
2752 ((= dcst ?n)
2753 (string-to-number (buffer-substring (match-end 0)
2754 (point-at-eol))))
2755 ((= dcst ?a)
2756 (funcall case-func
2757 (buffer-substring (match-end 0) (point-at-eol))))
2758 ((= dcst ?t)
2759 (cond
2760 ;; If it is a timer list, convert timer to seconds
2761 ((org-at-item-timer-p)
2762 (org-timer-hms-to-secs (match-string 1)))
2763 ((or (re-search-forward org-ts-regexp (point-at-eol) t)
2764 (re-search-forward org-ts-regexp-both
2765 (point-at-eol) t))
2766 (org-time-string-to-seconds (match-string 0)))
2767 (t (org-float-time now))))
2768 ((= dcst ?f)
2769 (if getkey-func
2770 (let ((value (funcall getkey-func)))
2771 (if (stringp value)
2772 (funcall case-func value)
2773 value))
2774 (error "Invalid key function `%s'" getkey-func)))
2775 (t (error "Invalid sorting type `%c'" sorting-type)))))))
2776 (sort-subr (/= dcst sorting-type)
2777 next-record
2778 end-record
2779 value-to-sort
2781 sort-func)
2782 ;; Read and fix list again, as `sort-subr' probably destroyed
2783 ;; its structure.
2784 (org-list-repair)
2785 (run-hooks 'org-after-sorting-entries-or-items-hook)
2786 (message "Sorting items...done")))))
2789 ;;; Send and receive lists
2791 (defun org-list-parse-list (&optional delete)
2792 "Parse the list at point and maybe DELETE it.
2794 Return a list whose car is a symbol of list type, among
2795 `ordered', `unordered' and `descriptive'. Then, each item is
2796 a list whose car is counter, and cdr are strings and other
2797 sub-lists. Inside strings, check-boxes are replaced by
2798 \"[CBON]\", \"[CBOFF]\" and \"[CBTRANS]\".
2800 For example, the following list:
2802 1. first item
2803 + sub-item one
2804 + [X] sub-item two
2805 more text in first item
2806 2. [@3] last item
2808 will be parsed as:
2810 \(ordered
2811 \(nil \"first item\"
2812 \(unordered
2813 \(nil \"sub-item one\"\)
2814 \(nil \"[CBON] sub-item two\"\)\)
2815 \"more text in first item\"\)
2816 \(3 \"last item\"\)\)
2818 Point is left at list end."
2819 (let* ((struct (org-list-struct))
2820 (prevs (org-list-prevs-alist struct))
2821 (parents (org-list-parents-alist struct))
2822 (top (org-list-get-top-point struct))
2823 (bottom (org-list-get-bottom-point struct))
2825 parse-item ; for byte-compiler
2826 (get-text
2827 (function
2828 ;; Return text between BEG and END, trimmed, with
2829 ;; checkboxes replaced.
2830 (lambda (beg end)
2831 (let ((text (org-trim (buffer-substring beg end))))
2832 (if (string-match "\\`\\[\\([-X ]\\)\\]" text)
2833 (replace-match
2834 (let ((box (match-string 1 text)))
2835 (cond
2836 ((equal box " ") "CBOFF")
2837 ((equal box "-") "CBTRANS")
2838 (t "CBON")))
2839 t nil text 1)
2840 text)))))
2841 (parse-sublist
2842 (function
2843 ;; Return a list whose car is list type and cdr a list of
2844 ;; items' body.
2845 (lambda (e)
2846 (cons (org-list-get-list-type (car e) struct prevs)
2847 (mapcar parse-item e)))))
2848 (parse-item
2849 (function
2850 ;; Return a list containing counter of item, if any, text
2851 ;; and any sublist inside it.
2852 (lambda (e)
2853 (let ((start (save-excursion
2854 (goto-char e)
2855 (looking-at "[ \t]*\\S-+\\([ \t]+\\[@\\(start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\]\\)?[ \t]*")
2856 (match-end 0)))
2857 ;; Get counter number. For alphabetic counter, get
2858 ;; its position in the alphabet.
2859 (counter (let ((c (org-list-get-counter e struct)))
2860 (cond
2861 ((not c) nil)
2862 ((string-match "[A-Za-z]" c)
2863 (- (string-to-char (upcase (match-string 0 c)))
2864 64))
2865 ((string-match "[0-9]+" c)
2866 (string-to-number (match-string 0 c))))))
2867 (childp (org-list-has-child-p e struct))
2868 (end (org-list-get-item-end e struct)))
2869 ;; If item has a child, store text between bullet and
2870 ;; next child, then recursively parse all sublists. At
2871 ;; the end of each sublist, check for the presence of
2872 ;; text belonging to the original item.
2873 (if childp
2874 (let* ((children (org-list-get-children e struct parents))
2875 (body (list (funcall get-text start childp))))
2876 (while children
2877 (let* ((first (car children))
2878 (sub (org-list-get-all-items first struct prevs))
2879 (last-c (car (last sub)))
2880 (last-end (org-list-get-item-end last-c struct)))
2881 (push (funcall parse-sublist sub) body)
2882 ;; Remove children from the list just parsed.
2883 (setq children (cdr (member last-c children)))
2884 ;; There is a chunk of text belonging to the
2885 ;; item if last child doesn't end where next
2886 ;; child starts or where item ends.
2887 (unless (= (or (car children) end) last-end)
2888 (push (funcall get-text
2889 last-end (or (car children) end))
2890 body))))
2891 (cons counter (nreverse body)))
2892 (list counter (funcall get-text start end))))))))
2893 ;; Store output, take care of cursor position and deletion of
2894 ;; list, then return output.
2895 (setq out (funcall parse-sublist (org-list-get-all-items top struct prevs)))
2896 (goto-char top)
2897 (when delete
2898 (delete-region top bottom)
2899 (when (and (not (eq org-list-ending-method 'indent))
2900 (looking-at org-list-end-re))
2901 (replace-match "\n")))
2902 out))
2904 (defun org-list-make-subtree ()
2905 "Convert the plain list at point into a subtree."
2906 (interactive)
2907 (if (not (ignore-errors (goto-char (org-in-item-p))))
2908 (error "Not in a list")
2909 (let ((list (save-excursion (org-list-parse-list t))))
2910 (insert (org-list-to-subtree list)))))
2912 (defun org-list-insert-radio-list ()
2913 "Insert a radio list template appropriate for this major mode."
2914 (interactive)
2915 (let* ((e (assq major-mode org-list-radio-list-templates))
2916 (txt (nth 1 e))
2917 name pos)
2918 (unless e (error "No radio list setup defined for %s" major-mode))
2919 (setq name (read-string "List name: "))
2920 (while (string-match "%n" txt)
2921 (setq txt (replace-match name t t txt)))
2922 (or (bolp) (insert "\n"))
2923 (setq pos (point))
2924 (insert txt)
2925 (goto-char pos)))
2927 (defun org-list-send-list (&optional maybe)
2928 "Send a transformed version of this list to the receiver position.
2929 With argument MAYBE, fail quietly if no transformation is defined
2930 for this list."
2931 (interactive)
2932 (catch 'exit
2933 (unless (org-at-item-p) (error "Not at a list item"))
2934 (save-excursion
2935 (re-search-backward "#\\+ORGLST" nil t)
2936 (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
2937 (if maybe
2938 (throw 'exit nil)
2939 (error "Don't know how to transform this list"))))
2940 (let* ((name (match-string 1))
2941 (transform (intern (match-string 2)))
2942 (bottom-point
2943 (save-excursion
2944 (re-search-forward
2945 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
2946 (match-beginning 0)))
2947 (top-point
2948 (progn
2949 (re-search-backward "#\\+ORGLST" nil t)
2950 (re-search-forward (org-item-beginning-re) bottom-point t)
2951 (match-beginning 0)))
2952 (list (save-restriction
2953 (narrow-to-region top-point bottom-point)
2954 (org-list-parse-list)))
2955 beg txt)
2956 (unless (fboundp transform)
2957 (error "No such transformation function %s" transform))
2958 (let ((txt (funcall transform list)))
2959 ;; Find the insertion place
2960 (save-excursion
2961 (goto-char (point-min))
2962 (unless (re-search-forward
2963 (concat "BEGIN RECEIVE ORGLST +"
2964 name
2965 "\\([ \t]\\|$\\)") nil t)
2966 (error "Don't know where to insert translated list"))
2967 (goto-char (match-beginning 0))
2968 (beginning-of-line 2)
2969 (setq beg (point))
2970 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
2971 (error "Cannot find end of insertion region"))
2972 (delete-region beg (point-at-bol))
2973 (goto-char beg)
2974 (insert txt "\n")))
2975 (message "List converted and installed at receiver location"))))
2977 (defun org-list-to-generic (list params)
2978 "Convert a LIST parsed through `org-list-parse-list' to other formats.
2979 Valid parameters PARAMS are:
2981 :ustart String to start an unordered list
2982 :uend String to end an unordered list
2984 :ostart String to start an ordered list
2985 :oend String to end an ordered list
2987 :dstart String to start a descriptive list
2988 :dend String to end a descriptive list
2989 :dtstart String to start a descriptive term
2990 :dtend String to end a descriptive term
2991 :ddstart String to start a description
2992 :ddend String to end a description
2994 :splice When set to t, return only list body lines, don't wrap
2995 them into :[u/o]start and :[u/o]end. Default is nil.
2997 :istart String to start a list item.
2998 :icount String to start an item with a counter.
2999 :iend String to end a list item
3000 :isep String to separate items
3001 :lsep String to separate sublists
3002 :csep String to separate text from a sub-list
3004 :cboff String to insert for an unchecked check-box
3005 :cbon String to insert for a checked check-box
3006 :cbtrans String to insert for a check-box in transitional state
3008 Alternatively, each parameter can also be a form returning
3009 a string. These sexp can use keywords `counter' and `depth',
3010 reprensenting respectively counter associated to the current
3011 item, and depth of the current sub-list, starting at 0.
3012 Obviously, `counter' is only available for parameters applying to
3013 items."
3014 (interactive)
3015 (let* ((p params)
3016 (splicep (plist-get p :splice))
3017 (ostart (plist-get p :ostart))
3018 (oend (plist-get p :oend))
3019 (ustart (plist-get p :ustart))
3020 (uend (plist-get p :uend))
3021 (dstart (plist-get p :dstart))
3022 (dend (plist-get p :dend))
3023 (dtstart (plist-get p :dtstart))
3024 (dtend (plist-get p :dtend))
3025 (ddstart (plist-get p :ddstart))
3026 (ddend (plist-get p :ddend))
3027 (istart (plist-get p :istart))
3028 (icount (plist-get p :icount))
3029 (iend (plist-get p :iend))
3030 (isep (plist-get p :isep))
3031 (lsep (plist-get p :lsep))
3032 (csep (plist-get p :csep))
3033 (cbon (plist-get p :cbon))
3034 (cboff (plist-get p :cboff))
3035 (cbtrans (plist-get p :cbtrans))
3036 export-sublist ; for byte-compiler
3037 (export-item
3038 (function
3039 ;; Export an item ITEM of type TYPE, at DEPTH. First
3040 ;; string in item is treated in a special way as it can
3041 ;; bring extra information that needs to be processed.
3042 (lambda (item type depth)
3043 (let* ((counter (pop item))
3044 (fmt (concat
3045 (cond
3046 ((eq type 'descriptive)
3047 ;; Stick DTSTART to ISTART by
3048 ;; left-trimming the latter.
3049 (concat (let ((s (eval istart)))
3050 (or (and (string-match "[ \t\n\r]+\\'" s)
3051 (replace-match "" t t s))
3052 istart))
3053 "%s" (eval ddend)))
3054 ((and counter (eq type 'ordered))
3055 (concat (eval icount) "%s"))
3056 (t (concat (eval istart) "%s")))
3057 (eval iend)))
3058 (first (car item)))
3059 ;; Replace checkbox if any is found.
3060 (cond
3061 ((string-match "\\[CBON\\]" first)
3062 (setq first (replace-match cbon t t first)))
3063 ((string-match "\\[CBOFF\\]" first)
3064 (setq first (replace-match cboff t t first)))
3065 ((string-match "\\[CBTRANS\\]" first)
3066 (setq first (replace-match cbtrans t t first))))
3067 ;; Insert descriptive term if TYPE is `descriptive'.
3068 (when (eq type 'descriptive)
3069 (let* ((complete (string-match "^\\(.*\\)[ \t]+::" first))
3070 (term (if complete
3071 (save-match-data
3072 (org-trim (match-string 1 first)))
3073 "???"))
3074 (desc (if complete
3075 (org-trim (substring first (match-end 0)))
3076 first)))
3077 (setq first (concat (eval dtstart) term (eval dtend)
3078 (eval ddstart) desc))))
3079 (setcar item first)
3080 (format fmt
3081 (mapconcat (lambda (e)
3082 (if (stringp e) e
3083 (funcall export-sublist e (1+ depth))))
3084 item (or (eval csep) "")))))))
3085 (export-sublist
3086 (function
3087 ;; Export sublist SUB at DEPTH.
3088 (lambda (sub depth)
3089 (let* ((type (car sub))
3090 (items (cdr sub))
3091 (fmt (concat (cond
3092 (splicep "%s")
3093 ((eq type 'ordered)
3094 (concat (eval ostart) "%s" (eval oend)))
3095 ((eq type 'descriptive)
3096 (concat (eval dstart) "%s" (eval dend)))
3097 (t (concat (eval ustart) "%s" (eval uend))))
3098 (eval lsep))))
3099 (format fmt (mapconcat (lambda (e)
3100 (funcall export-item e type depth))
3101 items (or (eval isep) ""))))))))
3102 (concat (funcall export-sublist list 0) "\n")))
3104 (defun org-list-to-latex (list &optional params)
3105 "Convert LIST into a LaTeX list.
3106 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3107 with overruling parameters for `org-list-to-generic'."
3108 (org-list-to-generic
3109 list
3110 (org-combine-plists
3111 '(:splice nil :ostart "\\begin{enumerate}\n" :oend "\\end{enumerate}"
3112 :ustart "\\begin{itemize}\n" :uend "\\end{itemize}"
3113 :dstart "\\begin{description}\n" :dend "\\end{description}"
3114 :dtstart "[" :dtend "] "
3115 :istart "\\item " :iend "\n"
3116 :icount (let ((enum (nth depth '("i" "ii" "iii" "iv"))))
3117 (if enum
3118 ;; LaTeX increments counter just before
3119 ;; using it, so set it to the desired
3120 ;; value, minus one.
3121 (format "\\setcounter{enum%s}{%s}\n\\item "
3122 enum (1- counter))
3123 "\\item "))
3124 :csep "\n"
3125 :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}"
3126 :cbtrans "\\texttt{[-]}")
3127 params)))
3129 (defun org-list-to-html (list &optional params)
3130 "Convert LIST into a HTML list.
3131 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3132 with overruling parameters for `org-list-to-generic'."
3133 (org-list-to-generic
3134 list
3135 (org-combine-plists
3136 '(:splice nil :ostart "<ol>\n" :oend "\n</ol>"
3137 :ustart "<ul>\n" :uend "\n</ul>"
3138 :dstart "<dl>\n" :dend "\n</dl>"
3139 :dtstart "<dt>" :dtend "</dt>\n"
3140 :ddstart "<dd>" :ddend "</dd>"
3141 :istart "<li>" :iend "</li>"
3142 :icount (format "<li value=\"%s\">" counter)
3143 :isep "\n" :lsep "\n" :csep "\n"
3144 :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>"
3145 :cbtrans "<code>[-]</code>")
3146 params)))
3148 (defun org-list-to-texinfo (list &optional params)
3149 "Convert LIST into a Texinfo list.
3150 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3151 with overruling parameters for `org-list-to-generic'."
3152 (org-list-to-generic
3153 list
3154 (org-combine-plists
3155 '(:splice nil :ostart "@itemize @minus\n" :oend "@end itemize"
3156 :ustart "@enumerate\n" :uend "@end enumerate"
3157 :dstart "@table @asis\n" :dend "@end table"
3158 :dtstart " " :dtend "\n"
3159 :istart "@item\n" :iend "\n"
3160 :icount "@item\n"
3161 :csep "\n"
3162 :cbon "@code{[X]}" :cboff "@code{[ ]}"
3163 :cbtrans "@code{[-]}")
3164 params)))
3166 (defun org-list-to-subtree (list &optional params)
3167 "Convert LIST into an Org subtree.
3168 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3169 with overruling parameters for `org-list-to-generic'."
3170 (let* ((rule (cdr (assq 'heading org-blank-before-new-entry)))
3171 (level (org-reduced-level (or (org-current-level) 0)))
3172 (blankp (or (eq rule t)
3173 (and (eq rule 'auto)
3174 (save-excursion
3175 (outline-previous-heading)
3176 (org-previous-line-empty-p)))))
3177 (get-stars
3178 (function
3179 ;; Return the string for the heading, depending on depth D
3180 ;; of current sub-list.
3181 (lambda (d)
3182 (let ((oddeven-level (+ level d 1)))
3183 (concat (make-string (if org-odd-levels-only
3184 (1- (* 2 oddeven-level))
3185 oddeven-level)
3187 " "))))))
3188 (org-list-to-generic
3189 list
3190 (org-combine-plists
3191 '(:splice t
3192 :dtstart " " :dtend " "
3193 :istart (funcall get-stars depth)
3194 :icount (funcall get-stars depth)
3195 :isep (if blankp "\n\n" "\n")
3196 :csep (if blankp "\n\n" "\n")
3197 :cbon "DONE" :cboff "TODO" :cbtrans "TODO")
3198 params))))
3200 (provide 'org-list)
3202 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
3203 ;;; org-list.el ends here