org-crypt: make org-decrypt disable auto-save-mode (configurable)
[org-mode/org-kjn.git] / lisp / org-list.el
blobb0bdc9c7dcbe8d088a20ac2263ed88a019f12e2c
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. A
33 ;; structure is a snapshot of the list, in the shape of data tree (see
34 ;; `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 26
218 items will fallback to standard numbering. Alphabetical counters
219 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.
226 When 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
554 ;; might 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 ind
701 ;; is less or equal than BEG-CELL and there is no end
702 ;; 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 also
767 ;; 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, as
931 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. Length
1226 ;; 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 new
1255 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, item
1281 ;; BEG-A will end with whitespaces that were at the end of
1282 ;; 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 and
1343 PREVS is the alist of previous items, returned by, respectively,
1344 `org-list-parents-alist' and `org-list-prevs-alist'.
1346 START is included and END excluded.
1348 STRUCT may be modified if `org-list-demote-modify-bullet' matches
1349 bullets between START and END."
1350 (let* (acc
1351 (set-assoc (lambda (cell) (push cell acc) cell))
1352 (change-bullet-maybe
1353 (function
1354 (lambda (item)
1355 (let* ((bul (org-trim (org-list-get-bullet item struct)))
1356 (new-bul-p (cdr (assoc bul org-list-demote-modify-bullet))))
1357 (when new-bul-p (org-list-set-bullet item struct new-bul-p))))))
1358 (ind
1359 (lambda (cell)
1360 (let* ((item (car cell))
1361 (parent (cdr cell)))
1362 (cond
1363 ;; Item not yet in zone: keep association
1364 ((< item start) cell)
1365 ((>= item end)
1366 ;; Item out of zone: follow associations in acc
1367 (let ((convert (assq parent acc)))
1368 (if convert (cons item (cdr convert)) cell)))
1370 ;; Item is in zone...
1371 (let ((prev (org-list-get-prev-item item struct prevs)))
1372 ;; Check if bullet needs to be changed
1373 (funcall change-bullet-maybe item)
1374 (cond
1375 ;; First item indented but not parent: error
1376 ((and (not prev) (< parent start))
1377 (error "Cannot indent the first item of a list"))
1378 ;; First item and parent indented: keep same parent
1379 ((not prev) (funcall set-assoc cell))
1380 ;; Previous item not indented: reparent to it
1381 ((< prev start) (funcall set-assoc (cons item prev)))
1382 ;; Previous item indented: reparent like it
1384 (funcall set-assoc
1385 (cons item (cdr (assq prev acc)))))))))))))
1386 (mapcar ind parents)))
1389 ;;; Repairing structures
1391 (defun org-list-use-alpha-bul-p (first struct prevs)
1392 "Non-nil if list starting at FIRST can have alphabetical bullets.
1394 STRUCT is list structure. PREVS is the alist of previous items,
1395 as returned by `org-list-prevs-alist'."
1396 (and org-alphabetical-lists
1397 (catch 'exit
1398 (let ((item first) (ascii 64) (case-fold-search nil))
1399 ;; Pretend that bullets are uppercase and check if alphabet
1400 ;; is sufficient, taking counters into account.
1401 (while item
1402 (let ((bul (org-list-get-bullet item struct))
1403 (count (org-list-get-counter item struct)))
1404 ;; Virtually determine current bullet
1405 (if (and count (string-match "[a-zA-Z]" count))
1406 ;; Counters are not case-sensitive.
1407 (setq ascii (string-to-char (upcase count)))
1408 (setq ascii (1+ ascii)))
1409 ;; Test if bullet would be over z or Z.
1410 (if (> ascii 90)
1411 (throw 'exit nil)
1412 (setq item (org-list-get-next-item item struct prevs)))))
1413 ;; All items checked. All good.
1414 t))))
1416 (defun org-list-inc-bullet-maybe (bullet)
1417 "Increment BULLET if applicable."
1418 (let ((case-fold-search nil))
1419 (cond
1420 ;; Num bullet: increment it.
1421 ((string-match "[0-9]+" bullet)
1422 (replace-match
1423 (number-to-string (1+ (string-to-number (match-string 0 bullet))))
1424 nil nil bullet))
1425 ;; Alpha bullet: increment it.
1426 ((string-match "[A-Za-z]" bullet)
1427 (replace-match
1428 (char-to-string (1+ (string-to-char (match-string 0 bullet))))
1429 nil nil bullet))
1430 ;; Unordered bullet: leave it.
1431 (t bullet))))
1433 (defun org-list-struct-fix-bul (struct prevs)
1434 "Verify and correct bullets in STRUCT.
1435 PREVS is the alist of previous items, as returned by
1436 `org-list-prevs-alist'.
1438 This function modifies STRUCT."
1439 (let ((case-fold-search nil)
1440 (fix-bul
1441 (function
1442 ;; Set bullet of ITEM in STRUCT, depending on the type of
1443 ;; first item of the list, the previous bullet and counter
1444 ;; if any.
1445 (lambda (item)
1446 (let* ((prev (org-list-get-prev-item item struct prevs))
1447 (prev-bul (and prev (org-list-get-bullet prev struct)))
1448 (counter (org-list-get-counter item struct))
1449 (bullet (org-list-get-bullet item struct))
1450 (alphap (and (not prev)
1451 (org-list-use-alpha-bul-p item struct prevs))))
1452 (org-list-set-bullet
1453 item struct
1454 (org-list-bullet-string
1455 (cond
1456 ;; Alpha counter in alpha list: use counter.
1457 ((and prev counter
1458 (string-match "[a-zA-Z]" counter)
1459 (string-match "[a-zA-Z]" prev-bul))
1460 ;; Use cond to be sure `string-match' is used in
1461 ;; both cases.
1462 (let ((real-count
1463 (cond
1464 ((string-match "[a-z]" prev-bul) (downcase counter))
1465 ((string-match "[A-Z]" prev-bul) (upcase counter)))))
1466 (replace-match real-count nil nil prev-bul)))
1467 ;; Num counter in a num list: use counter.
1468 ((and prev counter
1469 (string-match "[0-9]+" counter)
1470 (string-match "[0-9]+" prev-bul))
1471 (replace-match counter nil nil prev-bul))
1472 ;; No counter: increase, if needed, previous bullet.
1473 (prev
1474 (org-list-inc-bullet-maybe (org-list-get-bullet prev struct)))
1475 ;; Alpha counter at first item: use counter.
1476 ((and counter (org-list-use-alpha-bul-p item struct prevs)
1477 (string-match "[A-Za-z]" counter)
1478 (string-match "[A-Za-z]" bullet))
1479 (let ((real-count
1480 (cond
1481 ((string-match "[a-z]" bullet) (downcase counter))
1482 ((string-match "[A-Z]" bullet) (upcase counter)))))
1483 (replace-match real-count nil nil bullet)))
1484 ;; Num counter at first item: use counter.
1485 ((and counter
1486 (string-match "[0-9]+" counter)
1487 (string-match "[0-9]+" bullet))
1488 (replace-match counter nil nil bullet))
1489 ;; First bullet is alpha uppercase: use "A".
1490 ((and alphap (string-match "[A-Z]" bullet))
1491 (replace-match "A" nil nil bullet))
1492 ;; First bullet is alpha lowercase: use "a".
1493 ((and alphap (string-match "[a-z]" bullet))
1494 (replace-match "a" nil nil bullet))
1495 ;; First bullet is num: use "1".
1496 ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet)
1497 (replace-match "1" nil nil bullet))
1498 ;; Not an ordered list: keep bullet.
1499 (t bullet)))))))))
1500 (mapc fix-bul (mapcar 'car struct))))
1502 (defun org-list-struct-fix-ind (struct parents &optional bullet-size)
1503 "Verify and correct indentation in STRUCT.
1505 PARENTS is the alist of parents, as returned by
1506 `org-list-parents-alist'.
1508 If numeric optional argument BULLET-SIZE is set, assume all
1509 bullets in list have this length to determine new indentation.
1511 This function modifies STRUCT."
1512 (let* ((ancestor (org-list-get-top-point struct))
1513 (top-ind (org-list-get-ind ancestor struct))
1514 (new-ind
1515 (lambda (item)
1516 (let ((parent (org-list-get-parent item struct parents)))
1517 (if parent
1518 ;; Indent like parent + length of parent's bullet
1519 (org-list-set-ind
1520 item struct (+ (or bullet-size
1521 (length
1522 (org-list-get-bullet parent struct)))
1523 (org-list-get-ind parent struct)))
1524 ;; If no parent, indent like top-point
1525 (org-list-set-ind item struct top-ind))))))
1526 (mapc new-ind (mapcar 'car (cdr struct)))))
1528 (defun org-list-struct-fix-box (struct parents prevs &optional ordered)
1529 "Verify and correct checkboxes in STRUCT.
1531 PARENTS is the alist of parents and PREVS is the alist of
1532 previous items, as returned by, respectively,
1533 `org-list-parents-alist' and `org-list-prevs-alist'.
1535 If ORDERED is non-nil, a checkbox can only be checked when every
1536 checkbox before it is checked too. If there was an attempt to
1537 break this rule, the function will return the blocking item. In
1538 all others cases, the return value will be nil.
1540 This function modifies STRUCT."
1541 (let ((all-items (mapcar 'car struct))
1542 (set-parent-box
1543 (function
1544 (lambda (item)
1545 (let* ((box-list
1546 (mapcar (lambda (child)
1547 (org-list-get-checkbox child struct))
1548 (org-list-get-children item struct parents))))
1549 (org-list-set-checkbox
1550 item struct
1551 (cond
1552 ((and (member "[ ]" box-list) (member "[X]" box-list)) "[-]")
1553 ((member "[-]" box-list) "[-]")
1554 ((member "[X]" box-list) "[X]")
1555 ((member "[ ]" box-list) "[ ]")
1556 ;; parent has no boxed child: leave box as-is
1557 (t (org-list-get-checkbox item struct))))))))
1558 parent-list)
1559 ;; 1. List all parents with a checkbox
1560 (mapc
1561 (lambda (e)
1562 (let* ((parent (org-list-get-parent e struct parents))
1563 (parent-box-p (org-list-get-checkbox parent struct)))
1564 (when (and parent-box-p (not (memq parent parent-list)))
1565 (push parent parent-list))))
1566 all-items)
1567 ;; 2. Sort those parents by decreasing indentation
1568 (setq parent-list (sort parent-list
1569 (lambda (e1 e2)
1570 (> (org-list-get-ind e1 struct)
1571 (org-list-get-ind e2 struct)))))
1572 ;; 3. For each parent, get all children's checkboxes to determine
1573 ;; and set its checkbox accordingly
1574 (mapc set-parent-box parent-list)
1575 ;; 4. If ORDERED is set, see if we need to uncheck some boxes
1576 (when ordered
1577 (let* ((box-list
1578 (mapcar (lambda (e) (org-list-get-checkbox e struct)) all-items))
1579 (after-unchecked (member "[ ]" box-list)))
1580 ;; there are boxes checked after an unchecked one: fix that
1581 (when (member "[X]" after-unchecked)
1582 (let ((index (- (length struct) (length after-unchecked))))
1583 (mapc (lambda (e) (org-list-set-checkbox e struct "[ ]"))
1584 (nthcdr index all-items))
1585 ;; Verify once again the structure, without ORDERED
1586 (org-list-struct-fix-box struct parents prevs nil)
1587 ;; return blocking item
1588 (nth index all-items)))))))
1590 (defun org-list-struct-apply-struct (struct old-struct)
1591 "Apply set-difference between STRUCT and OLD-STRUCT to the buffer.
1593 OLD-STRUCT is the structure before any modifications, and STRUCT
1594 the structure to be applied. The function will only modify parts
1595 of the list which have changed.
1597 Initial position of cursor is restored after the changes."
1598 (let* ((pos (copy-marker (point)))
1599 (inlinetask-re (and (featurep 'org-inlinetask)
1600 (org-inlinetask-outline-regexp)))
1601 (item-re (org-item-re))
1602 (box-rule-p (cdr (assq 'checkbox org-list-automatic-rules)))
1603 (shift-body-ind
1604 (function
1605 ;; Shift the indentation between END and BEG by DELTA.
1606 ;; Start from the line before END.
1607 (lambda (end beg delta)
1608 (goto-char end)
1609 (skip-chars-backward " \r\t\n")
1610 (beginning-of-line)
1611 (while (or (> (point) beg)
1612 (and (= (point) beg)
1613 (not (looking-at item-re))))
1614 (cond
1615 ;; Skip inline tasks
1616 ((and inlinetask-re (looking-at inlinetask-re))
1617 (org-inlinetask-goto-beginning))
1618 ;; Shift only non-empty lines
1619 ((org-looking-at-p "^[ \t]*\\S-")
1620 (let ((i (org-get-indentation)))
1621 (org-indent-line-to (+ i delta)))))
1622 (forward-line -1)))))
1623 (modify-item
1624 (function
1625 ;; Replace ITEM first line elements with new elements from
1626 ;; STRUCT, if appropriate.
1627 (lambda (item)
1628 (goto-char item)
1629 (let* ((new-ind (org-list-get-ind item struct))
1630 (old-ind (org-get-indentation))
1631 (new-bul (org-list-bullet-string
1632 (org-list-get-bullet item struct)))
1633 (old-bul (org-list-get-bullet item old-struct))
1634 (new-box (org-list-get-checkbox item struct)))
1635 (looking-at org-list-full-item-re)
1636 ;; a. Replace bullet
1637 (unless (equal old-bul new-bul)
1638 (replace-match new-bul nil nil nil 1))
1639 ;; b. Replace checkbox
1640 (cond
1641 ((and new-box box-rule-p
1642 (save-match-data (org-at-item-description-p)))
1643 (message "Cannot add a checkbox to a description list item"))
1644 ((equal (match-string 3) new-box))
1645 ((and (match-string 3) new-box)
1646 (replace-match new-box nil nil nil 3))
1647 ((match-string 3)
1648 (goto-char (or (match-end 2) (match-end 1)))
1649 (looking-at "\\[[ X-]\\][ \t]+")
1650 (replace-match ""))
1651 (t (goto-char (or (match-end 2) (match-end 1)))
1652 (insert (concat new-box " "))))
1653 ;; c. Indent item to appropriate column
1654 (unless (= new-ind old-ind)
1655 (delete-region (goto-char (point-at-bol))
1656 (progn (skip-chars-forward " \t") (point)))
1657 (indent-to new-ind)))))))
1658 ;; 1. First get list of items and position endings. We maintain
1659 ;; two alists: ITM-SHIFT, determining indentation shift needed
1660 ;; at item, and END-POS, a pseudo-alist where key is ending
1661 ;; position and value point
1662 (let (end-list acc-end itm-shift all-ends sliced-struct)
1663 (mapc (lambda (e)
1664 (let* ((pos (car e))
1665 (ind-pos (org-list-get-ind pos struct))
1666 (ind-old (org-list-get-ind pos old-struct))
1667 (bul-pos (org-list-get-bullet pos struct))
1668 (bul-old (org-list-get-bullet pos old-struct))
1669 (ind-shift (- (+ ind-pos (length bul-pos))
1670 (+ ind-old (length bul-old))))
1671 (end-pos (org-list-get-item-end pos old-struct)))
1672 (push (cons pos ind-shift) itm-shift)
1673 (unless (assq end-pos old-struct)
1674 ;; To determine real ind of an ending position that is
1675 ;; not at an item, we have to find the item it belongs
1676 ;; to: it is the last item (ITEM-UP), whose ending is
1677 ;; further than the position we're interested in.
1678 (let ((item-up (assoc-default end-pos acc-end '>)))
1679 (push (cons end-pos item-up) end-list)))
1680 (push (cons end-pos pos) acc-end)))
1681 old-struct)
1682 ;; 2. Slice the items into parts that should be shifted by the
1683 ;; same amount of indentation. The slices are returned in
1684 ;; reverse order so changes modifying buffer do not change
1685 ;; positions they refer to.
1686 (setq all-ends (sort (append (mapcar 'car itm-shift)
1687 (org-uniquify (mapcar 'car end-list)))
1688 '<))
1689 (while (cdr all-ends)
1690 (let* ((up (pop all-ends))
1691 (down (car all-ends))
1692 (ind (if (assq up struct)
1693 (cdr (assq up itm-shift))
1694 (cdr (assq (cdr (assq up end-list)) itm-shift)))))
1695 (push (list down up ind) sliced-struct)))
1696 ;; 3. Shift each slice in buffer, provided delta isn't 0, from
1697 ;; end to beginning. Take a special action when beginning is
1698 ;; at item bullet.
1699 (mapc (lambda (e)
1700 (unless (zerop (nth 2 e)) (apply shift-body-ind e))
1701 (let* ((beg (nth 1 e))
1702 (cell (assq beg struct)))
1703 (unless (or (not cell) (equal cell (assq beg old-struct)))
1704 (funcall modify-item beg))))
1705 sliced-struct))
1706 ;; 4. Go back to initial position
1707 (goto-char pos)))
1709 (defun org-list-write-struct (struct parents)
1710 "Correct bullets, checkboxes and indentation in list at point.
1711 STRUCT is the list structure. PARENTS is the alist of parents, as
1712 returned by `org-list-parents-alist'."
1713 ;; Order of functions matters here: checkboxes and endings need
1714 ;; correct indentation to be set, and indentation needs correct
1715 ;; bullets.
1717 ;; 0. Save a copy of structure before modifications
1718 (let ((old-struct (copy-tree struct)))
1719 ;; 1. Set a temporary, but coherent with PARENTS, indentation in
1720 ;; order to get items endings and bullets properly
1721 (org-list-struct-fix-ind struct parents 2)
1722 ;; 2. Get pseudo-alist of ending positions and sort it by position.
1723 ;; Then associate them to the structure.
1724 (let (end-list acc-end)
1725 (mapc (lambda (e)
1726 (let* ((pos (car e))
1727 (ind-pos (org-list-get-ind pos struct))
1728 (end-pos (org-list-get-item-end pos struct)))
1729 (unless (assq end-pos struct)
1730 ;; To determine real ind of an ending position that is
1731 ;; not at an item, we have to find the item it belongs
1732 ;; to: it is the last item (ITEM-UP), whose ending is
1733 ;; further than the position we're interested in.
1734 (let ((item-up (assoc-default end-pos acc-end '>)))
1735 (push (cons
1736 ;; Else part is for the bottom point
1737 (if item-up (+ (org-list-get-ind item-up struct) 2) 0)
1738 end-pos)
1739 end-list)))
1740 (push (cons ind-pos pos) end-list)
1741 (push (cons end-pos pos) acc-end)))
1742 struct)
1743 (setq end-list (sort end-list (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
1744 (org-list-struct-assoc-end struct end-list))
1745 ;; 3. Get bullets right
1746 (let ((prevs (org-list-prevs-alist struct)))
1747 (org-list-struct-fix-bul struct prevs)
1748 ;; 4. Now get real indentation
1749 (org-list-struct-fix-ind struct parents)
1750 ;; 5. Eventually fix checkboxes
1751 (org-list-struct-fix-box struct parents prevs))
1752 ;; 6. Apply structure modifications to buffer
1753 (org-list-struct-apply-struct struct old-struct)))
1756 ;;; Misc Tools
1758 (defun org-apply-on-list (function init-value &rest args)
1759 "Call FUNCTION on each item of the list at point.
1760 FUNCTION must be called with at least one argument: INIT-VALUE,
1761 that will contain the value returned by the function at the
1762 previous item, plus ARGS extra arguments.
1764 FUNCTION is applied on items in reverse order.
1766 As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
1767 will return the number of items in the current list.
1769 Sublists of the list are skipped. Cursor is always at the
1770 beginning of the item."
1771 (let* ((struct (org-list-struct))
1772 (prevs (org-list-prevs-alist struct))
1773 (item (copy-marker (point-at-bol)))
1774 (all (org-list-get-all-items (marker-position item) struct prevs))
1775 (value init-value))
1776 (mapc (lambda (e)
1777 (goto-char e)
1778 (setq value (apply function value args)))
1779 (nreverse all))
1780 (goto-char item)
1781 value))
1783 (defun org-list-set-item-visibility (item struct view)
1784 "Set visibility of ITEM in STRUCT to VIEW.
1786 Possible values are: `folded', `children' or `subtree'. See
1787 `org-cycle' for more information."
1788 (cond
1789 ((eq view 'folded)
1790 (let ((item-end (org-list-get-item-end-before-blank item struct)))
1791 ;; Hide from eol
1792 (outline-flag-region (save-excursion (goto-char item) (point-at-eol))
1793 item-end t)))
1794 ((eq view 'children)
1795 ;; First show everything.
1796 (org-list-set-item-visibility item struct 'subtree)
1797 ;; Then fold every child.
1798 (let* ((parents (org-list-parents-alist struct))
1799 (children (org-list-get-children item struct parents)))
1800 (mapc (lambda (e)
1801 (org-list-set-item-visibility e struct 'folded))
1802 children)))
1803 ((eq view 'subtree)
1804 ;; Show everything
1805 (let ((item-end (org-list-get-item-end item struct)))
1806 (outline-flag-region item item-end nil)))))
1808 (defun org-list-item-body-column (item)
1809 "Return column at which body of ITEM should start."
1810 (let (bpos bcol tpos tcol)
1811 (save-excursion
1812 (goto-char item)
1813 (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?[ \t]+")
1814 (setq bpos (match-beginning 1) tpos (match-end 0)
1815 bcol (progn (goto-char bpos) (current-column))
1816 tcol (progn (goto-char tpos) (current-column)))
1817 (when (> tcol (+ bcol org-description-max-indent))
1818 (setq tcol (+ bcol 5))))
1819 tcol))
1822 ;;; Interactive functions
1824 (defalias 'org-list-get-item-begin 'org-in-item-p)
1826 (defun org-beginning-of-item ()
1827 "Go to the beginning of the current item.
1828 Throw an error when not in a list."
1829 (interactive)
1830 (let ((begin (org-in-item-p)))
1831 (if begin (goto-char begin) (error "Not in an item"))))
1833 (defun org-beginning-of-item-list ()
1834 "Go to the beginning item of the current list or sublist.
1835 Throw an error when not in a list."
1836 (interactive)
1837 (let ((begin (org-in-item-p)))
1838 (if (not begin)
1839 (error "Not in an item")
1840 (goto-char begin)
1841 (let* ((struct (org-list-struct))
1842 (prevs (org-list-prevs-alist struct)))
1843 (goto-char (org-list-get-list-begin begin struct prevs))))))
1845 (defun org-end-of-item-list ()
1846 "Go to the end of the current list or sublist.
1847 Throw an error when not in a list."
1848 (interactive)
1849 (let ((begin (org-in-item-p)))
1850 (if (not begin)
1851 (error "Not in an item")
1852 (goto-char begin)
1853 (let* ((struct (org-list-struct))
1854 (prevs (org-list-prevs-alist struct)))
1855 (goto-char (org-list-get-list-end begin struct prevs))))))
1857 (defun org-end-of-item ()
1858 "Go to the end of the current item.
1859 Throw an error when not in a list."
1860 (interactive)
1861 (let ((begin (org-in-item-p)))
1862 (if (not begin)
1863 (error "Not in an item")
1864 (goto-char begin)
1865 (let ((struct (org-list-struct)))
1866 (goto-char (org-list-get-item-end begin struct))))))
1868 (defun org-previous-item ()
1869 "Move to the beginning of the previous item.
1870 Throw an error when not in a list, or at first item."
1871 (interactive)
1872 (let ((begin (org-in-item-p)))
1873 (if (not begin)
1874 (error "Not in an item")
1875 (goto-char begin)
1876 (let* ((struct (org-list-struct))
1877 (prevs (org-list-prevs-alist struct))
1878 (prevp (org-list-get-prev-item begin struct prevs)))
1879 (if prevp (goto-char prevp) (error "On first item"))))))
1881 (defun org-next-item ()
1882 "Move to the beginning of the next item.
1883 Throw an error when not in a plain list, or at last item."
1884 (interactive)
1885 (let ((begin (org-in-item-p)))
1886 (if (not begin)
1887 (error "Not in an item")
1888 (goto-char begin)
1889 (let* ((struct (org-list-struct))
1890 (prevs (org-list-prevs-alist struct))
1891 (prevp (org-list-get-next-item begin struct prevs)))
1892 (if prevp (goto-char prevp) (error "On last item"))))))
1894 (defun org-move-item-down ()
1895 "Move the item at point down, i.e. swap with following item.
1896 Subitems (items with larger indentation) are considered part of
1897 the item, so this really moves item trees."
1898 (interactive)
1899 (unless (org-at-item-p) (error "Not at an item"))
1900 (let* ((pos (point))
1901 (col (current-column))
1902 (actual-item (point-at-bol))
1903 (struct (org-list-struct))
1904 (prevs (org-list-prevs-alist struct))
1905 (next-item (org-list-get-next-item (point-at-bol) struct prevs)))
1906 (if (not next-item)
1907 (progn
1908 (goto-char pos)
1909 (error "Cannot move this item further down"))
1910 (setq struct
1911 (org-list-exchange-items actual-item next-item struct))
1912 ;; Use a short variation of `org-list-write-struct' as there's
1913 ;; no need to go through all the steps.
1914 (let ((old-struct (copy-tree struct))
1915 (prevs (org-list-prevs-alist struct))
1916 (parents (org-list-parents-alist struct)))
1917 (org-list-struct-fix-bul struct prevs)
1918 (org-list-struct-fix-ind struct parents)
1919 (org-list-struct-apply-struct struct old-struct)
1920 (goto-char (org-list-get-next-item (point-at-bol) struct prevs)))
1921 (org-move-to-column col))))
1923 (defun org-move-item-up ()
1924 "Move the item at point up, i.e. swap with previous item.
1925 Subitems (items with larger indentation) are considered part of
1926 the item, so this really moves item trees."
1927 (interactive)
1928 (unless (org-at-item-p) (error "Not at an item"))
1929 (let* ((pos (point))
1930 (col (current-column))
1931 (actual-item (point-at-bol))
1932 (struct (org-list-struct))
1933 (prevs (org-list-prevs-alist struct))
1934 (prev-item (org-list-get-prev-item (point-at-bol) struct prevs)))
1935 (if (not prev-item)
1936 (progn
1937 (goto-char pos)
1938 (error "Cannot move this item further up"))
1939 (setq struct
1940 (org-list-exchange-items prev-item actual-item struct))
1941 ;; Use a short variation of `org-list-write-struct' as there's
1942 ;; no need to go through all the steps.
1943 (let ((old-struct (copy-tree struct))
1944 (prevs (org-list-prevs-alist struct))
1945 (parents (org-list-parents-alist struct)))
1946 (org-list-struct-fix-bul struct prevs)
1947 (org-list-struct-fix-ind struct parents)
1948 (org-list-struct-apply-struct struct old-struct))
1949 (org-move-to-column col))))
1951 (defun org-insert-item (&optional checkbox)
1952 "Insert a new item at the current level.
1953 If cursor is before first character after bullet of the item, the
1954 new item will be created before the current one.
1956 If CHECKBOX is non-nil, add a checkbox next to the bullet.
1958 Return t when things worked, nil when we are not in an item, or
1959 item is invisible."
1960 (let ((itemp (org-in-item-p))
1961 (pos (point)))
1962 ;; If cursor isn't is a list or if list is invisible, return nil.
1963 (unless (or (not itemp)
1964 (save-excursion
1965 (goto-char itemp)
1966 (outline-invisible-p)))
1967 (if (save-excursion
1968 (goto-char itemp)
1969 (org-at-item-timer-p))
1970 ;; Timer list: delegate to `org-timer-item'.
1971 (progn (org-timer-item) t)
1972 (goto-char itemp)
1973 (let* ((struct (org-list-struct))
1974 (prevs (org-list-prevs-alist struct))
1975 ;; If we're in a description list, ask for the new term.
1976 (desc (when (org-list-get-tag itemp struct)
1977 (concat (read-string "Term: ") " :: ")))
1978 ;; Don't insert a checkbox if checkbox rule is applied
1979 ;; and it is a description item.
1980 (checkp (and checkbox
1981 (or (not desc)
1982 (not (cdr (assq 'checkbox
1983 org-list-automatic-rules)))))))
1984 (setq struct
1985 (org-list-insert-item pos struct prevs checkp desc))
1986 (org-list-write-struct struct (org-list-parents-alist struct))
1987 (when checkp (org-update-checkbox-count-maybe))
1988 (looking-at org-list-full-item-re)
1989 (goto-char (match-end 0))
1990 t)))))
1992 (defun org-list-repair ()
1993 "Fix indentation, bullets and checkboxes is the list at point."
1994 (interactive)
1995 (unless (org-at-item-p) (error "This is not a list"))
1996 (let* ((struct (org-list-struct))
1997 (parents (org-list-parents-alist struct)))
1998 (org-list-write-struct struct parents)))
2000 (defun org-cycle-list-bullet (&optional which)
2001 "Cycle through the different itemize/enumerate bullets.
2002 This cycle the entire list level through the sequence:
2004 `-' -> `+' -> `*' -> `1.' -> `1)'
2006 If WHICH is a valid string, use that as the new bullet. If WHICH
2007 is an integer, 0 means `-', 1 means `+' etc. If WHICH is
2008 `previous', cycle backwards."
2009 (interactive "P")
2010 (unless (org-at-item-p) (error "Not at an item"))
2011 (save-excursion
2012 (beginning-of-line)
2013 (let* ((struct (org-list-struct))
2014 (parents (org-list-parents-alist struct))
2015 (prevs (org-list-prevs-alist struct))
2016 (list-beg (org-list-get-first-item (point) struct prevs))
2017 (bullet (org-list-get-bullet list-beg struct))
2018 (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
2019 (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs))
2020 (case-fold-search nil)
2021 (current (cond
2022 ((string-match "[a-z]\\." bullet) "a.")
2023 ((string-match "[a-z])" bullet) "a)")
2024 ((string-match "[A-Z]\\." bullet) "A.")
2025 ((string-match "[A-Z])" bullet) "A)")
2026 ((string-match "\\." bullet) "1.")
2027 ((string-match ")" bullet) "1)")
2028 (t (org-trim bullet))))
2029 ;; Compute list of possible bullets, depending on context
2030 (bullet-list
2031 (append '("-" "+" )
2032 ;; *-bullets are not allowed at column 0
2033 (unless (and bullet-rule-p
2034 (looking-at "\\S-")) '("*"))
2035 ;; Description items cannot be numbered
2036 (unless (or (eq org-plain-list-ordered-item-terminator ?\))
2037 (and bullet-rule-p (org-at-item-description-p)))
2038 '("1."))
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 (not alpha-p)
2043 (eq org-plain-list-ordered-item-terminator ?\))
2044 (and bullet-rule-p (org-at-item-description-p)))
2045 '("a." "A."))
2046 (unless (or (not alpha-p)
2047 (eq org-plain-list-ordered-item-terminator ?.)
2048 (and bullet-rule-p (org-at-item-description-p)))
2049 '("a)" "A)"))))
2050 (len (length bullet-list))
2051 (item-index (- len (length (member current bullet-list))))
2052 (get-value (lambda (index) (nth (mod index len) bullet-list)))
2053 (new (cond
2054 ((member which bullet-list) which)
2055 ((numberp which) (funcall get-value which))
2056 ((eq 'previous which) (funcall get-value (1- item-index)))
2057 (t (funcall get-value (1+ item-index))))))
2058 ;; Use a short variation of `org-list-write-struct' as there's
2059 ;; no need to go through all the steps.
2060 (let ((old-struct (copy-tree struct)))
2061 (org-list-set-bullet list-beg struct (org-list-bullet-string new))
2062 (org-list-struct-fix-bul struct prevs)
2063 (org-list-struct-fix-ind struct parents)
2064 (org-list-struct-apply-struct struct old-struct)))))
2066 (defun org-toggle-checkbox (&optional toggle-presence)
2067 "Toggle the checkbox in the current line.
2068 With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
2069 double prefix, set checkbox to [-].
2071 When there is an active region, toggle status or presence of the
2072 first checkbox there, and make every item inside have the same
2073 status or presence, respectively.
2075 If the cursor is in a headline, apply this to all checkbox items
2076 in the text below the heading, taking as reference the first item
2077 in subtree, ignoring drawers."
2078 (interactive "P")
2079 (save-excursion
2080 (let* (singlep
2081 block-item
2082 lim-up
2083 lim-down
2084 (drawer-re (concat "^[ \t]*:\\("
2085 (mapconcat 'regexp-quote org-drawers "\\|")
2086 "\\):[ \t]*$"))
2087 (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
2088 "\\|" org-deadline-string
2089 "\\|" org-closed-string
2090 "\\|" org-clock-string "\\)"
2091 " *[[<]\\([^]>]+\\)[]>]"))
2092 (orderedp (org-entry-get nil "ORDERED"))
2093 (bounds
2094 ;; In a region, start at first item in region
2095 (cond
2096 ((org-region-active-p)
2097 (let ((limit (region-end)))
2098 (goto-char (region-beginning))
2099 (if (org-list-search-forward (org-item-beginning-re) limit t)
2100 (setq lim-up (point-at-bol))
2101 (error "No item in region"))
2102 (setq lim-down (copy-marker limit))))
2103 ((org-on-heading-p)
2104 ;; On an heading, start at first item after drawers and
2105 ;; time-stamps (scheduled, etc.)
2106 (let ((limit (save-excursion (outline-next-heading) (point))))
2107 (forward-line 1)
2108 (while (or (looking-at drawer-re) (looking-at keyword-re))
2109 (if (looking-at keyword-re)
2110 (forward-line 1)
2111 (re-search-forward "^[ \t]*:END:" limit nil)))
2112 (if (org-list-search-forward (org-item-beginning-re) limit t)
2113 (setq lim-up (point-at-bol))
2114 (error "No item in subtree"))
2115 (setq lim-down (copy-marker limit))))
2116 ;; Just one item: set singlep flag
2117 ((org-at-item-p)
2118 (setq singlep t)
2119 (setq lim-up (point-at-bol)
2120 lim-down (point-at-eol)))
2121 (t (error "Not at an item or heading, and no active region"))))
2122 ;; Determine the checkbox going to be applied to all items
2123 ;; within bounds
2124 (ref-checkbox
2125 (progn
2126 (goto-char lim-up)
2127 (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
2128 (cond
2129 ((equal toggle-presence '(16)) "[-]")
2130 ((equal toggle-presence '(4))
2131 (unless cbox "[ ]"))
2132 ((equal "[X]" cbox) "[ ]")
2133 (t "[X]"))))))
2134 ;; When an item is found within bounds, grab the full list at
2135 ;; point structure, then: 1. set checkbox of all its items
2136 ;; within bounds to ref-checkbox; 2. fix checkboxes of the whole
2137 ;; list; 3. move point after the list.
2138 (goto-char lim-up)
2139 (while (and (< (point) lim-down)
2140 (org-list-search-forward (org-item-beginning-re)
2141 lim-down 'move))
2142 (let* ((struct (org-list-struct))
2143 (struct-copy (copy-tree struct))
2144 (parents (org-list-parents-alist struct))
2145 (prevs (org-list-prevs-alist struct))
2146 (bottom (copy-marker (org-list-get-bottom-point struct)))
2147 (items-to-toggle (org-remove-if
2148 (lambda (e) (or (< e lim-up) (> e lim-down)))
2149 (mapcar 'car struct))))
2150 (mapc (lambda (e) (org-list-set-checkbox
2151 e struct
2152 ;; if there is no box at item, leave as-is
2153 ;; unless function was called with C-u prefix
2154 (let ((cur-box (org-list-get-checkbox e struct)))
2155 (if (or cur-box (equal toggle-presence '(4)))
2156 ref-checkbox
2157 cur-box))))
2158 items-to-toggle)
2159 (setq block-item (org-list-struct-fix-box
2160 struct parents prevs orderedp))
2161 ;; Report some problems due to ORDERED status of subtree. If
2162 ;; only one box was being checked, throw an error, else,
2163 ;; only signal problems.
2164 (cond
2165 ((and singlep block-item (> lim-up block-item))
2166 (error
2167 "Checkbox blocked because of unchecked box at line %d"
2168 (org-current-line block-item)))
2169 (block-item
2170 (message
2171 "Checkboxes were removed due to unchecked box at line %d"
2172 (org-current-line block-item))))
2173 (goto-char bottom)
2174 (org-list-struct-apply-struct struct struct-copy)))))
2175 (org-update-checkbox-count-maybe))
2177 (defun org-reset-checkbox-state-subtree ()
2178 "Reset all checkboxes in an entry subtree."
2179 (interactive "*")
2180 (if (org-before-first-heading-p)
2181 (error "Not inside a tree")
2182 (save-restriction
2183 (save-excursion
2184 (org-narrow-to-subtree)
2185 (org-show-subtree)
2186 (goto-char (point-min))
2187 (let ((end (point-max)))
2188 (while (< (point) end)
2189 (when (org-at-item-checkbox-p)
2190 (replace-match "[ ]" t t nil 1))
2191 (beginning-of-line 2)))
2192 (org-update-checkbox-count-maybe 'all)))))
2194 (defun org-update-checkbox-count (&optional all)
2195 "Update the checkbox statistics in the current section.
2196 This will find all statistic cookies like [57%] and [6/12] and
2197 update them with the current numbers.
2199 With optional prefix argument ALL, do this for the whole buffer."
2200 (interactive "P")
2201 (save-excursion
2202 (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
2203 (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
2204 (recursivep
2205 (or (not org-hierarchical-checkbox-statistics)
2206 (string-match "\\<recursive\\>"
2207 (or (org-entry-get nil "COOKIE_DATA") ""))))
2208 (bounds (if all
2209 (cons (point-min) (point-max))
2210 (cons (or (ignore-errors (org-back-to-heading t) (point))
2211 (point-min))
2212 (save-excursion (outline-next-heading) (point)))))
2213 (count-boxes
2214 (function
2215 ;; Return number of checked boxes and boxes of all types
2216 ;; in all structures in STRUCTS. If RECURSIVEP is non-nil,
2217 ;; also count boxes in sub-lists. If ITEM is nil, count
2218 ;; across the whole structure, else count only across
2219 ;; subtree whose ancestor is ITEM.
2220 (lambda (item structs recursivep)
2221 (let ((c-on 0) (c-all 0))
2222 (mapc
2223 (lambda (s)
2224 (let* ((pre (org-list-prevs-alist s))
2225 (par (org-list-parents-alist s))
2226 (items
2227 (cond
2228 ((and recursivep item) (org-list-get-subtree item s))
2229 (recursivep (mapcar 'car s))
2230 (item (org-list-get-children item s par))
2231 (t (org-list-get-all-items
2232 (org-list-get-top-point s) s pre))))
2233 (cookies (delq nil (mapcar
2234 (lambda (e)
2235 (org-list-get-checkbox e s))
2236 items))))
2237 (setq c-all (+ (length cookies) c-all)
2238 c-on (+ (org-count "[X]" cookies) c-on))))
2239 structs)
2240 (cons c-on c-all)))))
2241 (backup-end 1)
2242 cookies-list structs-bak box-num)
2243 (goto-char (car bounds))
2244 ;; 1. Build an alist for each cookie found within BOUNDS. The
2245 ;; key will be position at beginning of cookie and values
2246 ;; ending position, format of cookie, and a cell whose car is
2247 ;; number of checked boxes to report, and cdr total number of
2248 ;; boxes.
2249 (while (re-search-forward cookie-re (cdr bounds) t)
2250 (catch 'skip
2251 (save-excursion
2252 (push
2253 (list
2254 (match-beginning 1) ; cookie start
2255 (match-end 1) ; cookie end
2256 (match-string 2) ; percent?
2257 (cond ; boxes count
2258 ;; Cookie is at an heading, but specifically for todo,
2259 ;; not for checkboxes: skip it.
2260 ((and (org-on-heading-p)
2261 (string-match "\\<todo\\>"
2262 (downcase
2263 (or (org-entry-get nil "COOKIE_DATA") ""))))
2264 (throw 'skip nil))
2265 ;; Cookie is at an heading, but all lists before next
2266 ;; heading already have been read. Use data collected
2267 ;; in STRUCTS-BAK. This should only happen when heading
2268 ;; has more than one cookie on it.
2269 ((and (org-on-heading-p)
2270 (<= (save-excursion (outline-next-heading) (point))
2271 backup-end))
2272 (funcall count-boxes nil structs-bak recursivep))
2273 ;; Cookie is at a fresh heading. Grab structure of
2274 ;; every list containing a checkbox between point and
2275 ;; next headline, and save them in STRUCTS-BAK.
2276 ((org-on-heading-p)
2277 (setq backup-end (save-excursion
2278 (outline-next-heading) (point))
2279 structs-bak nil)
2280 (while (org-list-search-forward box-re backup-end 'move)
2281 (let* ((struct (org-list-struct))
2282 (bottom (org-list-get-bottom-point struct)))
2283 (push struct structs-bak)
2284 (goto-char bottom)))
2285 (funcall count-boxes nil structs-bak recursivep))
2286 ;; Cookie is at an item, and we already have list
2287 ;; structure stored in STRUCTS-BAK.
2288 ((and (org-at-item-p)
2289 (< (point-at-bol) backup-end)
2290 ;; Only lists in no special context are stored.
2291 (not (nth 2 (org-list-context))))
2292 (funcall count-boxes (point-at-bol) structs-bak recursivep))
2293 ;; Cookie is at an item, but we need to compute list
2294 ;; structure.
2295 ((org-at-item-p)
2296 (let ((struct (org-list-struct)))
2297 (setq backup-end (org-list-get-bottom-point struct)
2298 structs-bak (list struct)))
2299 (funcall count-boxes (point-at-bol) structs-bak recursivep))
2300 ;; Else, cookie found is at a wrong place. Skip it.
2301 (t (throw 'skip nil))))
2302 cookies-list))))
2303 ;; 2. Apply alist to buffer, in reverse order so positions stay
2304 ;; unchanged after cookie modifications.
2305 (mapc (lambda (cookie)
2306 (let* ((beg (car cookie))
2307 (end (nth 1 cookie))
2308 (percentp (nth 2 cookie))
2309 (checked (car (nth 3 cookie)))
2310 (total (cdr (nth 3 cookie)))
2311 (new (if percentp
2312 (format "[%d%%]" (/ (* 100 checked)
2313 (max 1 total)))
2314 (format "[%d/%d]" checked total))))
2315 (goto-char beg)
2316 (insert new)
2317 (delete-region (point) (+ (point) (- end beg)))
2318 (when org-auto-align-tags (org-fix-tags-on-the-fly))))
2319 cookies-list))))
2321 (defun org-get-checkbox-statistics-face ()
2322 "Select the face for checkbox statistics.
2323 The face will be `org-done' when all relevant boxes are checked.
2324 Otherwise it will be `org-todo'."
2325 (if (match-end 1)
2326 (if (equal (match-string 1) "100%")
2327 'org-checkbox-statistics-done
2328 'org-checkbox-statistics-todo)
2329 (if (and (> (match-end 2) (match-beginning 2))
2330 (equal (match-string 2) (match-string 3)))
2331 'org-checkbox-statistics-done
2332 'org-checkbox-statistics-todo)))
2334 (defun org-update-checkbox-count-maybe (&optional all)
2335 "Update checkbox statistics unless turned off by user."
2336 (when (cdr (assq 'checkbox org-list-automatic-rules))
2337 (org-update-checkbox-count all))
2338 (run-hooks 'org-checkbox-statistics-hook))
2340 (defvar org-last-indent-begin-marker (make-marker))
2341 (defvar org-last-indent-end-marker (make-marker))
2342 (defun org-list-indent-item-generic (arg no-subtree struct)
2343 "Indent a local list item including its children.
2344 When number ARG is a negative, item will be outdented, otherwise
2345 it will be indented.
2347 If a region is active, all items inside will be moved.
2349 If NO-SUBTREE is non-nil, only indent the item itself, not its
2350 children.
2352 STRUCT is the list structure.
2354 Return t if successful."
2355 (save-excursion
2356 (beginning-of-line)
2357 (let* ((regionp (org-region-active-p))
2358 (rbeg (and regionp (region-beginning)))
2359 (rend (and regionp (region-end)))
2360 (top (org-list-get-top-point struct))
2361 (parents (org-list-parents-alist struct))
2362 (prevs (org-list-prevs-alist struct))
2363 ;; Are we going to move the whole list?
2364 (specialp
2365 (and (= top (point))
2366 (cdr (assq 'indent org-list-automatic-rules))
2367 (if no-subtree
2368 (error
2369 "First item of list cannot move without its subtree")
2370 t))))
2371 ;; Determine begin and end points of zone to indent. If moving
2372 ;; more than one item, save them for subsequent moves.
2373 (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
2374 (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
2375 (if regionp
2376 (progn
2377 (set-marker org-last-indent-begin-marker rbeg)
2378 (set-marker org-last-indent-end-marker rend))
2379 (set-marker org-last-indent-begin-marker (point))
2380 (set-marker org-last-indent-end-marker
2381 (cond
2382 (specialp (org-list-get-bottom-point struct))
2383 (no-subtree (1+ (point)))
2384 (t (org-list-get-item-end (point) struct))))))
2385 (let* ((beg (marker-position org-last-indent-begin-marker))
2386 (end (marker-position org-last-indent-end-marker)))
2387 (cond
2388 ;; Special case: moving top-item with indent rule
2389 (specialp
2390 (let* ((level-skip (org-level-increment))
2391 (offset (if (< arg 0) (- level-skip) level-skip))
2392 (top-ind (org-list-get-ind beg struct))
2393 (old-struct (copy-tree struct)))
2394 (if (< (+ top-ind offset) 0)
2395 (error "Cannot outdent beyond margin")
2396 ;; Change bullet if necessary
2397 (when (and (= (+ top-ind offset) 0)
2398 (string-match "*"
2399 (org-list-get-bullet beg struct)))
2400 (org-list-set-bullet beg struct
2401 (org-list-bullet-string "-")))
2402 ;; Shift every item by OFFSET and fix bullets. Then
2403 ;; apply changes to buffer.
2404 (mapc (lambda (e)
2405 (let ((ind (org-list-get-ind (car e) struct)))
2406 (org-list-set-ind (car e) struct (+ ind offset))))
2407 struct)
2408 (org-list-struct-fix-bul struct prevs)
2409 (org-list-struct-apply-struct struct old-struct))))
2410 ;; Forbidden move:
2411 ((and (< arg 0)
2412 ;; If only one item is moved, it mustn't have a child
2413 (or (and no-subtree
2414 (not regionp)
2415 (org-list-has-child-p beg struct))
2416 ;; If a subtree or region is moved, the last item
2417 ;; of the subtree mustn't have a child
2418 (let ((last-item (caar
2419 (reverse
2420 (org-remove-if
2421 (lambda (e) (>= (car e) end))
2422 struct)))))
2423 (org-list-has-child-p last-item struct))))
2424 (error "Cannot outdent an item without its children"))
2425 ;; Normal shifting
2427 (let* ((new-parents
2428 (if (< arg 0)
2429 (org-list-struct-outdent beg end struct parents)
2430 (org-list-struct-indent beg end struct parents prevs))))
2431 (org-list-write-struct struct new-parents))
2432 (org-update-checkbox-count-maybe))))))
2435 (defun org-outdent-item ()
2436 "Outdent a local list item, but not its children.
2437 If a region is active, all items inside will be moved."
2438 (interactive)
2439 (if (org-at-item-p)
2440 (let ((struct (org-list-struct)))
2441 (org-list-indent-item-generic -1 t struct))
2442 (error "Not at an item")))
2444 (defun org-indent-item ()
2445 "Indent a local list item, but not its children.
2446 If a region is active, all items inside will be moved."
2447 (interactive)
2448 (if (org-at-item-p)
2449 (let ((struct (org-list-struct)))
2450 (org-list-indent-item-generic 1 t struct))
2451 (error "Not at an item")))
2453 (defun org-outdent-item-tree ()
2454 "Outdent a local list item including its children.
2455 If a region is active, all items inside will be moved."
2456 (interactive)
2457 (let ((regionp (org-region-active-p)))
2458 (cond
2459 ((or (org-at-item-p)
2460 (and (org-region-active-p)
2461 (goto-char (region-beginning))
2462 (org-at-item-p)))
2463 (let ((struct (org-list-struct)))
2464 (org-list-indent-item-generic -1 nil struct)))
2465 (regionp (error "Region not starting at an item"))
2466 (t (error "Not at an item")))))
2468 (defun org-indent-item-tree ()
2469 "Indent a local list item including its children.
2470 If a region is active, all items inside will be moved."
2471 (interactive)
2472 (let ((regionp (org-region-active-p)))
2473 (cond
2474 ((or (org-at-item-p)
2475 (and (org-region-active-p)
2476 (goto-char (region-beginning))
2477 (org-at-item-p)))
2478 (let ((struct (org-list-struct)))
2479 (org-list-indent-item-generic 1 nil struct)))
2480 (regionp (error "Region not starting at an item"))
2481 (t (error "Not at an item")))))
2483 (defvar org-tab-ind-state)
2484 (defun org-cycle-item-indentation ()
2485 "Cycle levels of indentation of an empty item.
2486 The first run indents the item, if applicable. Subsequents runs
2487 outdent it at meaningful levels in the list. When done, item is
2488 put back at its original position with its original bullet.
2490 Return t at each successful move."
2491 (when (org-at-item-p)
2492 (let* ((org-adapt-indentation nil)
2493 (struct (org-list-struct))
2494 (ind (org-list-get-ind (point-at-bol) struct))
2495 (bullet (org-list-get-bullet (point-at-bol) struct)))
2496 ;; Accept empty items or if cycle has already started.
2497 (when (or (eq last-command 'org-cycle-item-indentation)
2498 (and (save-excursion
2499 (beginning-of-line)
2500 (looking-at org-list-full-item-re))
2501 (>= (match-end 0) (save-excursion
2502 (goto-char (org-list-get-item-end
2503 (point-at-bol) struct))
2504 (skip-chars-backward " \r\t\n")
2505 (point)))))
2506 (setq this-command 'org-cycle-item-indentation)
2507 ;; When in the middle of the cycle, try to outdent first. If it
2508 ;; fails, and point is still at initial position, indent. Else,
2509 ;; go back to original position.
2510 (if (eq last-command 'org-cycle-item-indentation)
2511 (cond
2512 ((ignore-errors (org-list-indent-item-generic -1 t struct)))
2513 ((and (= ind (car org-tab-ind-state))
2514 (ignore-errors (org-list-indent-item-generic 1 t struct))))
2515 (t (delete-region (point-at-bol) (point-at-eol))
2516 (org-indent-to-column (car org-tab-ind-state))
2517 (insert (cdr org-tab-ind-state))
2518 ;; Break cycle
2519 (setq this-command 'identity)))
2520 ;; If a cycle is starting, remember indentation and bullet,
2521 ;; then try to indent. If it fails, try to outdent.
2522 (setq org-tab-ind-state (cons ind bullet))
2523 (cond
2524 ((ignore-errors (org-list-indent-item-generic 1 t struct)))
2525 ((ignore-errors (org-list-indent-item-generic -1 t struct)))
2526 (t (error "Cannot move item"))))
2527 t))))
2529 (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
2530 "Sort list items.
2531 The cursor may be at any item of the list that should be sorted.
2532 Sublists are not sorted. Checkboxes, if any, are ignored.
2534 Sorting can be alphabetically, numerically, by date/time as given by
2535 a time stamp, by a property or by priority.
2537 Comparing entries ignores case by default. However, with an
2538 optional argument WITH-CASE, the sorting considers case as well.
2540 The command prompts for the sorting type unless it has been given
2541 to the function through the SORTING-TYPE argument, which needs to
2542 be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
2543 meaning of each character:
2545 n Numerically, by converting the beginning of the item to a number.
2546 a Alphabetically. Only the first line of item is checked.
2547 t By date/time, either the first active time stamp in the entry, if
2548 any, or by the first inactive one. In a timer list, sort the timers.
2550 Capital letters will reverse the sort order.
2552 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
2553 function to be called with point at the beginning of the record.
2554 It must return either a string or a number that should serve as
2555 the sorting key for that record. It will then use COMPARE-FUNC to
2556 compare entries."
2557 (interactive "P")
2558 (let* ((case-func (if with-case 'identity 'downcase))
2559 (struct (org-list-struct))
2560 (prevs (org-list-prevs-alist struct))
2561 (start (org-list-get-list-begin (point-at-bol) struct prevs))
2562 (end (org-list-get-list-end (point-at-bol) struct prevs))
2563 (sorting-type
2564 (progn
2565 (message
2566 "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
2567 (read-char-exclusive)))
2568 (getkey-func (and (= (downcase sorting-type) ?f)
2569 (intern (org-icompleting-read "Sort using function: "
2570 obarray 'fboundp t nil nil)))))
2571 (message "Sorting items...")
2572 (save-restriction
2573 (narrow-to-region start end)
2574 (goto-char (point-min))
2575 (let* ((dcst (downcase sorting-type))
2576 (case-fold-search nil)
2577 (now (current-time))
2578 (sort-func (cond
2579 ((= dcst ?a) 'string<)
2580 ((= dcst ?f) compare-func)
2581 ((= dcst ?t) '<)
2582 (t nil)))
2583 (next-record (lambda ()
2584 (skip-chars-forward " \r\t\n")
2585 (beginning-of-line)))
2586 (end-record (lambda ()
2587 (goto-char (org-list-get-item-end-before-blank
2588 (point) struct))))
2589 (value-to-sort
2590 (lambda ()
2591 (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
2592 (cond
2593 ((= dcst ?n)
2594 (string-to-number (buffer-substring (match-end 0)
2595 (point-at-eol))))
2596 ((= dcst ?a)
2597 (funcall case-func
2598 (buffer-substring (match-end 0) (point-at-eol))))
2599 ((= dcst ?t)
2600 (cond
2601 ;; If it is a timer list, convert timer to seconds
2602 ((org-at-item-timer-p)
2603 (org-timer-hms-to-secs (match-string 1)))
2604 ((or (re-search-forward org-ts-regexp (point-at-eol) t)
2605 (re-search-forward org-ts-regexp-both
2606 (point-at-eol) t))
2607 (org-time-string-to-seconds (match-string 0)))
2608 (t (org-float-time now))))
2609 ((= dcst ?f)
2610 (if getkey-func
2611 (let ((value (funcall getkey-func)))
2612 (if (stringp value)
2613 (funcall case-func value)
2614 value))
2615 (error "Invalid key function `%s'" getkey-func)))
2616 (t (error "Invalid sorting type `%c'" sorting-type)))))))
2617 (sort-subr (/= dcst sorting-type)
2618 next-record
2619 end-record
2620 value-to-sort
2622 sort-func)
2623 ;; Read and fix list again, as `sort-subr' probably destroyed
2624 ;; its structure.
2625 (org-list-repair)
2626 (run-hooks 'org-after-sorting-entries-or-items-hook)
2627 (message "Sorting items...done")))))
2630 ;;; Send and receive lists
2632 (defun org-list-parse-list (&optional delete)
2633 "Parse the list at point and maybe DELETE it.
2635 Return a list whose car is a symbol of list type, among
2636 `ordered', `unordered' and `descriptive'. Then, each item is
2637 a list whose car is counter, and cdr are strings and other
2638 sub-lists. Inside strings, check-boxes are replaced by
2639 \"[CBON]\", \"[CBOFF]\" and \"[CBTRANS]\".
2641 For example, the following list:
2643 1. first item
2644 + sub-item one
2645 + [X] sub-item two
2646 more text in first item
2647 2. [@3] last item
2649 will be parsed as:
2651 \(ordered
2652 \(nil \"first item\"
2653 \(unordered
2654 \(nil \"sub-item one\"\)
2655 \(nil \"[CBON] sub-item two\"\)\)
2656 \"more text in first item\"\)
2657 \(3 \"last item\"\)\)
2659 Point is left at list end."
2660 (let* ((struct (org-list-struct))
2661 (prevs (org-list-prevs-alist struct))
2662 (parents (org-list-parents-alist struct))
2663 (top (org-list-get-top-point struct))
2664 (bottom (org-list-get-bottom-point struct))
2666 parse-item ; for byte-compiler
2667 (get-text
2668 (function
2669 ;; Return text between BEG and END, trimmed, with
2670 ;; checkboxes replaced.
2671 (lambda (beg end)
2672 (let ((text (org-trim (buffer-substring beg end))))
2673 (if (string-match "\\`\\[\\([-X ]\\)\\]" text)
2674 (replace-match
2675 (let ((box (match-string 1 text)))
2676 (cond
2677 ((equal box " ") "CBOFF")
2678 ((equal box "-") "CBTRANS")
2679 (t "CBON")))
2680 t nil text 1)
2681 text)))))
2682 (parse-sublist
2683 (function
2684 ;; Return a list whose car is list type and cdr a list of
2685 ;; items' body.
2686 (lambda (e)
2687 (cons (org-list-get-list-type (car e) struct prevs)
2688 (mapcar parse-item e)))))
2689 (parse-item
2690 (function
2691 ;; Return a list containing counter of item, if any, text
2692 ;; and any sublist inside it.
2693 (lambda (e)
2694 (let ((start (save-excursion
2695 (goto-char e)
2696 (looking-at "[ \t]*\\S-+\\([ \t]+\\[@\\(start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\]\\)?[ \t]*")
2697 (match-end 0)))
2698 ;; Get counter number. For alphabetic counter, get
2699 ;; its position in the alphabet.
2700 (counter (let ((c (org-list-get-counter e struct)))
2701 (cond
2702 ((not c) nil)
2703 ((string-match "[A-Za-z]" c)
2704 (- (string-to-char (upcase (match-string 0 c)))
2705 64))
2706 ((string-match "[0-9]+" c)
2707 (string-to-number (match-string 0 c))))))
2708 (childp (org-list-has-child-p e struct))
2709 (end (org-list-get-item-end e struct)))
2710 ;; If item has a child, store text between bullet and
2711 ;; next child, then recursively parse all sublists. At
2712 ;; the end of each sublist, check for the presence of
2713 ;; text belonging to the original item.
2714 (if childp
2715 (let* ((children (org-list-get-children e struct parents))
2716 (body (list (funcall get-text start childp))))
2717 (while children
2718 (let* ((first (car children))
2719 (sub (org-list-get-all-items first struct prevs))
2720 (last-c (car (last sub)))
2721 (last-end (org-list-get-item-end last-c struct)))
2722 (push (funcall parse-sublist sub) body)
2723 ;; Remove children from the list just parsed.
2724 (setq children (cdr (member last-c children)))
2725 ;; There is a chunk of text belonging to the
2726 ;; item if last child doesn't end where next
2727 ;; child starts or where item ends.
2728 (unless (= (or (car children) end) last-end)
2729 (push (funcall get-text
2730 last-end (or (car children) end))
2731 body))))
2732 (cons counter (nreverse body)))
2733 (list counter (funcall get-text start end))))))))
2734 ;; Store output, take care of cursor position and deletion of
2735 ;; list, then return output.
2736 (setq out (funcall parse-sublist (org-list-get-all-items top struct prevs)))
2737 (goto-char top)
2738 (when delete
2739 (delete-region top bottom)
2740 (when (and (not (eq org-list-ending-method 'indent))
2741 (looking-at org-list-end-re))
2742 (replace-match "\n")))
2743 out))
2745 (defun org-list-make-subtree ()
2746 "Convert the plain list at point into a subtree."
2747 (interactive)
2748 (if (not (ignore-errors (goto-char (org-in-item-p))))
2749 (error "Not in a list")
2750 (let ((list (save-excursion (org-list-parse-list t))))
2751 (insert (org-list-to-subtree list)))))
2753 (defun org-list-insert-radio-list ()
2754 "Insert a radio list template appropriate for this major mode."
2755 (interactive)
2756 (let* ((e (assq major-mode org-list-radio-list-templates))
2757 (txt (nth 1 e))
2758 name pos)
2759 (unless e (error "No radio list setup defined for %s" major-mode))
2760 (setq name (read-string "List name: "))
2761 (while (string-match "%n" txt)
2762 (setq txt (replace-match name t t txt)))
2763 (or (bolp) (insert "\n"))
2764 (setq pos (point))
2765 (insert txt)
2766 (goto-char pos)))
2768 (defun org-list-send-list (&optional maybe)
2769 "Send a transformed version of this list to the receiver position.
2770 With argument MAYBE, fail quietly if no transformation is defined for
2771 this list."
2772 (interactive)
2773 (catch 'exit
2774 (unless (org-at-item-p) (error "Not at a list item"))
2775 (save-excursion
2776 (re-search-backward "#\\+ORGLST" nil t)
2777 (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
2778 (if maybe
2779 (throw 'exit nil)
2780 (error "Don't know how to transform this list"))))
2781 (let* ((name (match-string 1))
2782 (transform (intern (match-string 2)))
2783 (bottom-point
2784 (save-excursion
2785 (re-search-forward
2786 "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
2787 (match-beginning 0)))
2788 (top-point
2789 (progn
2790 (re-search-backward "#\\+ORGLST" nil t)
2791 (re-search-forward (org-item-beginning-re) bottom-point t)
2792 (match-beginning 0)))
2793 (list (save-restriction
2794 (narrow-to-region top-point bottom-point)
2795 (org-list-parse-list)))
2796 beg txt)
2797 (unless (fboundp transform)
2798 (error "No such transformation function %s" transform))
2799 (let ((txt (funcall transform list)))
2800 ;; Find the insertion place
2801 (save-excursion
2802 (goto-char (point-min))
2803 (unless (re-search-forward
2804 (concat "BEGIN RECEIVE ORGLST +"
2805 name
2806 "\\([ \t]\\|$\\)") nil t)
2807 (error "Don't know where to insert translated list"))
2808 (goto-char (match-beginning 0))
2809 (beginning-of-line 2)
2810 (setq beg (point))
2811 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
2812 (error "Cannot find end of insertion region"))
2813 (delete-region beg (point-at-bol))
2814 (goto-char beg)
2815 (insert txt "\n")))
2816 (message "List converted and installed at receiver location"))))
2818 (defun org-list-to-generic (list params)
2819 "Convert a LIST parsed through `org-list-parse-list' to other formats.
2820 Valid parameters PARAMS are
2822 :ustart String to start an unordered list
2823 :uend String to end an unordered list
2825 :ostart String to start an ordered list
2826 :oend String to end an ordered list
2828 :dstart String to start a descriptive list
2829 :dend String to end a descriptive list
2830 :dtstart String to start a descriptive term
2831 :dtend String to end a descriptive term
2832 :ddstart String to start a description
2833 :ddend String to end a description
2835 :splice When set to t, return only list body lines, don't wrap
2836 them into :[u/o]start and :[u/o]end. Default is nil.
2838 :istart String to start a list item.
2839 :icount String to start an item with a counter.
2840 :iend String to end a list item
2841 :isep String to separate items
2842 :lsep String to separate sublists
2843 :csep String to separate text from a sub-list
2845 :cboff String to insert for an unchecked check-box
2846 :cbon String to insert for a checked check-box
2847 :cbtrans String to insert for a check-box in transitional state
2849 Alternatively, each parameter can also be a form returning a
2850 string. These sexp can use keywords `counter' and `depth',
2851 reprensenting respectively counter associated to the current
2852 item, and depth of the current sub-list, starting at 0.
2853 Obviously, `counter' is only available for parameters applying to
2854 items."
2855 (interactive)
2856 (let* ((p params)
2857 (splicep (plist-get p :splice))
2858 (ostart (plist-get p :ostart))
2859 (oend (plist-get p :oend))
2860 (ustart (plist-get p :ustart))
2861 (uend (plist-get p :uend))
2862 (dstart (plist-get p :dstart))
2863 (dend (plist-get p :dend))
2864 (dtstart (plist-get p :dtstart))
2865 (dtend (plist-get p :dtend))
2866 (ddstart (plist-get p :ddstart))
2867 (ddend (plist-get p :ddend))
2868 (istart (plist-get p :istart))
2869 (icount (plist-get p :icount))
2870 (iend (plist-get p :iend))
2871 (isep (plist-get p :isep))
2872 (lsep (plist-get p :lsep))
2873 (csep (plist-get p :csep))
2874 (cbon (plist-get p :cbon))
2875 (cboff (plist-get p :cboff))
2876 (cbtrans (plist-get p :cbtrans))
2877 export-sublist ; for byte-compiler
2878 (export-item
2879 (function
2880 ;; Export an item ITEM of type TYPE, at DEPTH. First string
2881 ;; in item is treated in a special way as it can bring
2882 ;; extra information that needs to be processed.
2883 (lambda (item type depth)
2884 (let* ((counter (pop item))
2885 (fmt (concat
2886 (cond
2887 ((eq type 'descriptive)
2888 ;; Stick DTSTART to ISTART by
2889 ;; left-trimming the latter.
2890 (concat (let ((s (eval istart)))
2891 (or (and (string-match "[ \t\n\r]+\\'" s)
2892 (replace-match "" t t s))
2893 istart))
2894 "%s" (eval ddend)))
2895 ((and counter (eq type 'ordered))
2896 (concat (eval icount) "%s"))
2897 (t (concat (eval istart) "%s")))
2898 (eval iend)))
2899 (first (car item)))
2900 ;; Replace checkbox if any is found.
2901 (cond
2902 ((string-match "\\[CBON\\]" first)
2903 (setq first (replace-match cbon t t first)))
2904 ((string-match "\\[CBOFF\\]" first)
2905 (setq first (replace-match cboff t t first)))
2906 ((string-match "\\[CBTRANS\\]" first)
2907 (setq first (replace-match cbtrans t t first))))
2908 ;; Insert descriptive term if TYPE is `descriptive'.
2909 (when (eq type 'descriptive)
2910 (let* ((complete (string-match "^\\(.*\\)[ \t]+::" first))
2911 (term (if complete
2912 (save-match-data
2913 (org-trim (match-string 1 first)))
2914 "???"))
2915 (desc (if complete
2916 (org-trim (substring first (match-end 0)))
2917 first)))
2918 (setq first (concat (eval dtstart) term (eval dtend)
2919 (eval ddstart) desc))))
2920 (setcar item first)
2921 (format fmt
2922 (mapconcat (lambda (e)
2923 (if (stringp e) e
2924 (funcall export-sublist e (1+ depth))))
2925 item (or (eval csep) "")))))))
2926 (export-sublist
2927 (function
2928 ;; Export sublist SUB at DEPTH
2929 (lambda (sub depth)
2930 (let* ((type (car sub))
2931 (items (cdr sub))
2932 (fmt (concat (cond
2933 (splicep "%s")
2934 ((eq type 'ordered)
2935 (concat (eval ostart) "%s" (eval oend)))
2936 ((eq type 'descriptive)
2937 (concat (eval dstart) "%s" (eval dend)))
2938 (t (concat (eval ustart) "%s" (eval uend))))
2939 (eval lsep))))
2940 (format fmt (mapconcat (lambda (e)
2941 (funcall export-item e type depth))
2942 items (or (eval isep) ""))))))))
2943 (concat (funcall export-sublist list 0) "\n")))
2945 (defun org-list-to-latex (list &optional params)
2946 "Convert LIST into a LaTeX list.
2947 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
2948 with overruling parameters for `org-list-to-generic'."
2949 (org-list-to-generic
2950 list
2951 (org-combine-plists
2952 '(:splice nil :ostart "\\begin{enumerate}\n" :oend "\\end{enumerate}"
2953 :ustart "\\begin{itemize}\n" :uend "\\end{itemize}"
2954 :dstart "\\begin{description}\n" :dend "\\end{description}"
2955 :dtstart "[" :dtend "] "
2956 :istart "\\item " :iend "\n"
2957 :icount (let ((enum (nth depth '("i" "ii" "iii" "iv"))))
2958 (if enum
2959 ;; LaTeX increments counter just before
2960 ;; using it, so set it to the desired
2961 ;; value, minus one.
2962 (format "\\setcounter{enum%s}{%s}\n\\item "
2963 enum (1- counter))
2964 "\\item "))
2965 :csep "\n"
2966 :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}"
2967 :cbtrans "$\\boxminus$")
2968 params)))
2970 (defun org-list-to-html (list &optional params)
2971 "Convert LIST into a HTML list.
2972 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
2973 with overruling parameters for `org-list-to-generic'."
2974 (org-list-to-generic
2975 list
2976 (org-combine-plists
2977 '(:splice nil :ostart "<ol>\n" :oend "\n</ol>"
2978 :ustart "<ul>\n" :uend "\n</ul>"
2979 :dstart "<dl>\n" :dend "\n</dl>"
2980 :dtstart "<dt>" :dtend "</dt>\n"
2981 :ddstart "<dd>" :ddend "</dd>"
2982 :istart "<li>" :iend "</li>"
2983 :icount (format "<li value=\"%s\">" counter)
2984 :isep "\n" :lsep "\n" :csep "\n"
2985 :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>"
2986 :cbtrans "<code>[-]</code>")
2987 params)))
2989 (defun org-list-to-texinfo (list &optional params)
2990 "Convert LIST into a Texinfo list.
2991 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
2992 with overruling parameters for `org-list-to-generic'."
2993 (org-list-to-generic
2994 list
2995 (org-combine-plists
2996 '(:splice nil :ostart "@itemize @minus\n" :oend "@end itemize"
2997 :ustart "@enumerate\n" :uend "@end enumerate"
2998 :dstart "@table @asis\n" :dend "@end table"
2999 :dtstart " " :dtend "\n"
3000 :istart "@item\n" :iend "\n"
3001 :icount "@item\n"
3002 :csep "\n"
3003 :cbon "@code{[X]}" :cboff "@code{[ ]}"
3004 :cbtrans "@code{[-]}")
3005 params)))
3007 (defun org-list-to-subtree (list &optional params)
3008 "Convert LIST into an Org subtree.
3009 LIST is as returned by `org-list-parse-list'. PARAMS is a property list
3010 with overruling parameters for `org-list-to-generic'."
3011 (let* ((rule (cdr (assq 'heading org-blank-before-new-entry)))
3012 (level (org-reduced-level (or (org-current-level) 0)))
3013 (blankp (or (eq rule t)
3014 (and (eq rule 'auto)
3015 (save-excursion
3016 (outline-previous-heading)
3017 (org-previous-line-empty-p)))))
3018 (get-stars
3019 (function
3020 ;; Return the string for the heading, depending on depth D
3021 ;; of current sub-list.
3022 (lambda (d)
3023 (let ((oddeven-level (+ level d 1)))
3024 (concat (make-string (if org-odd-levels-only
3025 (1- (* 2 oddeven-level))
3026 oddeven-level)
3028 " "))))))
3029 (org-list-to-generic
3030 list
3031 (org-combine-plists
3032 '(:splice t
3033 :dtstart " " :dtend " "
3034 :istart (funcall get-stars depth)
3035 :icount (funcall get-stars depth)
3036 :isep (if blankp "\n\n" "\n")
3037 :csep (if blankp "\n\n" "\n")
3038 :cbon "DONE" :cboff "TODO" :cbtrans "TODO")
3039 params))))
3041 (provide 'org-list)
3043 ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
3044 ;;; org-list.el ends here