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