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