2008-09-23 02:03:13 -04:00
|
|
|
;;; org-list.el --- Plain lists for Org-mode
|
|
|
|
;;
|
2010-04-07 10:26:10 -04:00
|
|
|
;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
|
2009-01-26 03:42:33 -05:00
|
|
|
;; Free Software Foundation, Inc.
|
2008-09-23 02:03:13 -04:00
|
|
|
;;
|
|
|
|
;; Author: Carsten Dominik <carsten at orgmode dot org>
|
2008-12-16 11:17:15 -05:00
|
|
|
;; Bastien Guerry <bzg AT altern DOT org>
|
2008-09-23 02:03:13 -04:00
|
|
|
;; Keywords: outlines, hypermedia, calendar, wp
|
|
|
|
;; Homepage: http://orgmode.org
|
2010-12-10 12:49:40 -05:00
|
|
|
;; Version: 7.4
|
2008-09-23 02:03:13 -04:00
|
|
|
;;
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-12-16 11:17:15 -05:00
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-09-23 02:03:13 -04:00
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This file contains the code dealing with plain lists in Org-mode.
|
|
|
|
|
2010-12-29 14:16:00 -05:00
|
|
|
;; The fundamental idea behind lists work is to use structures. A
|
|
|
|
;; structure is a snapshot of the list, in the shape of data tree (see
|
|
|
|
;; `org-list-struct').
|
|
|
|
|
|
|
|
;; Once the list structure is stored, it is possible to make changes
|
|
|
|
;; directly on it or get useful information on the list, with helper
|
|
|
|
;; functions `org-list-struct-parent-alist' and
|
|
|
|
;; `org-list-struct-prev-alist', and using accessors provided in the
|
|
|
|
;; file.
|
|
|
|
|
|
|
|
;; Structure is repaired with `org-list-struct-fix-struct'. Then
|
|
|
|
;; changes are applied to buffer with `org-list-struct-apply-struct'.
|
|
|
|
|
|
|
|
;; So any function working on plain lists should follow this template:
|
|
|
|
;; 1. Verify point is in a list and grab item beginning (with the same
|
|
|
|
;; function `org-in-item-p') ;
|
|
|
|
;; 2. Get list structure ;
|
|
|
|
;; 3. Compute one, or both, helper functions depending on required
|
|
|
|
;; accessors ;
|
|
|
|
;; 4. Proceed with the modifications ;
|
|
|
|
;; 5. Then fix the structure one last time and apply it on buffer.
|
|
|
|
|
|
|
|
;; It is usally a bad idea to use directly an interactive function
|
|
|
|
;; inside a function, as those read the whole list structure another
|
|
|
|
;; time.
|
|
|
|
|
2008-09-23 02:03:13 -04:00
|
|
|
;;; Code:
|
|
|
|
|
2010-05-13 12:02:05 -04:00
|
|
|
(eval-when-compile
|
|
|
|
(require 'cl))
|
2008-09-23 02:03:13 -04:00
|
|
|
(require 'org-macs)
|
|
|
|
(require 'org-compat)
|
|
|
|
|
|
|
|
(defvar org-blank-before-new-entry)
|
|
|
|
(defvar org-M-RET-may-split-line)
|
2009-08-03 07:28:24 -04:00
|
|
|
(defvar org-complex-heading-regexp)
|
|
|
|
(defvar org-odd-levels-only)
|
2010-09-05 11:58:30 -04:00
|
|
|
(defvar org-outline-regexp)
|
|
|
|
(defvar org-ts-regexp)
|
|
|
|
(defvar org-ts-regexp-both)
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2010-12-23 16:02:05 -05:00
|
|
|
(declare-function org-at-heading-p "org" (&optional ignored))
|
2008-09-23 02:03:13 -04:00
|
|
|
(declare-function org-back-over-empty-lines "org" ())
|
2010-12-23 16:02:05 -05:00
|
|
|
(declare-function org-back-to-heading "org" (&optional invisible-ok))
|
2009-01-06 11:23:36 -05:00
|
|
|
(declare-function org-combine-plists "org" (&rest plists))
|
2010-08-27 16:35:09 -04:00
|
|
|
(declare-function org-entry-get "org"
|
|
|
|
(pom property &optional inherit literal-nil))
|
2010-12-23 16:02:05 -05:00
|
|
|
(declare-function org-get-indentation "org" (&optional line))
|
|
|
|
(declare-function org-icompleting-read "org" (&rest args))
|
2010-09-05 11:58:30 -04:00
|
|
|
(declare-function org-in-regexps-block-p "org"
|
|
|
|
(start-re end-re &optional bound))
|
2010-12-23 12:54:24 -05:00
|
|
|
(declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
|
|
|
|
(declare-function org-inlinetask-goto-end "org-inlinetask" ())
|
2010-12-23 16:02:05 -05:00
|
|
|
(declare-function org-inlinetask-in-task-p "org-inlinetask" ())
|
|
|
|
(declare-function org-invisible-p "org" ())
|
|
|
|
(declare-function org-level-increment "org" ())
|
|
|
|
(declare-function org-narrow-to-subtree "org" ())
|
|
|
|
(declare-function org-on-heading-p "org" (&optional invisible-ok))
|
|
|
|
(declare-function org-remove-if "org" (predicate seq))
|
|
|
|
(declare-function org-show-subtree "org" ())
|
|
|
|
(declare-function org-time-string-to-seconds "org" (s))
|
|
|
|
(declare-function org-timer-hms-to-secs "org-timer" (hms))
|
|
|
|
(declare-function org-timer-item "org-timer" (&optional arg))
|
|
|
|
(declare-function org-trim "org" (s))
|
|
|
|
(declare-function org-uniquify "org" (list))
|
|
|
|
(declare-function outline-next-heading "outline" ())
|
|
|
|
(declare-function outline-previous-heading "outline" ())
|
2008-09-23 02:03:13 -04:00
|
|
|
|
|
|
|
(defgroup org-plain-lists nil
|
|
|
|
"Options concerning plain lists in Org-mode."
|
|
|
|
:tag "Org Plain lists"
|
|
|
|
:group 'org-structure)
|
|
|
|
|
2009-07-01 03:34:13 -04:00
|
|
|
(defcustom org-cycle-include-plain-lists t
|
|
|
|
"When t, make TAB cycle visibility on plain list items.
|
|
|
|
Cycling plain lists works only when the cursor is on a plain list
|
|
|
|
item. When the cursor is on an outline heading, plain lists are
|
|
|
|
treated as text. This is the most stable way of handling this,
|
|
|
|
which is why it is the default.
|
|
|
|
|
|
|
|
When this is the symbol `integrate', then during cycling, plain
|
|
|
|
list items will *temporarily* be interpreted as outline headlines
|
|
|
|
with a level given by 1000+i where i is the indentation of the
|
|
|
|
bullet. This setting can lead to strange effects when switching
|
|
|
|
visibility to `children', because the first \"child\" in a
|
|
|
|
subtree decides what children should be listed. If that first
|
|
|
|
\"child\" is a plain list item with an implied large level
|
|
|
|
number, all true children and grand children of the outline
|
|
|
|
heading will be exposed in a children' view."
|
2008-09-23 02:03:13 -04:00
|
|
|
:group 'org-plain-lists
|
2009-07-01 03:34:13 -04:00
|
|
|
:type '(choice
|
|
|
|
(const :tag "Never" nil)
|
2009-07-01 03:42:24 -04:00
|
|
|
(const :tag "With cursor in plain list (recommended)" t)
|
|
|
|
(const :tag "As children of outline headings" integrate)))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2009-07-06 02:53:50 -04:00
|
|
|
(defcustom org-list-demote-modify-bullet nil
|
|
|
|
"Default bullet type installed when demoting an item.
|
|
|
|
This is an association list, for each bullet type, this alist will point
|
2010-05-23 01:11:01 -04:00
|
|
|
to the bullet that should be used when this item is demoted.
|
|
|
|
For example,
|
|
|
|
|
|
|
|
(setq org-list-demote-modify-bullet
|
|
|
|
'((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
|
|
|
|
|
|
|
|
will make
|
|
|
|
|
|
|
|
+ Movies
|
|
|
|
+ Silence of the Lambs
|
|
|
|
+ My Cousin Vinny
|
|
|
|
+ Books
|
|
|
|
+ The Hunt for Red October
|
|
|
|
+ The Road to Omaha
|
|
|
|
|
|
|
|
into
|
|
|
|
|
|
|
|
+ Movies
|
|
|
|
- Silence of the Lambs
|
|
|
|
- My Cousin Vinny
|
|
|
|
+ Books
|
|
|
|
- The Hunt for Red October
|
|
|
|
- The Road to Omaha"
|
2009-07-06 02:53:50 -04:00
|
|
|
:group 'org-plain-lists
|
|
|
|
:type '(repeat
|
|
|
|
(cons
|
|
|
|
(choice :tag "If the current bullet is "
|
|
|
|
(const "-")
|
|
|
|
(const "+")
|
|
|
|
(const "*")
|
|
|
|
(const "1.")
|
|
|
|
(const "1)"))
|
|
|
|
(choice :tag "demotion will change it to"
|
|
|
|
(const "-")
|
|
|
|
(const "+")
|
|
|
|
(const "*")
|
|
|
|
(const "1.")
|
|
|
|
(const "1)")))))
|
|
|
|
|
2008-09-23 02:03:13 -04:00
|
|
|
(defcustom org-plain-list-ordered-item-terminator t
|
|
|
|
"The character that makes a line with leading number an ordered list item.
|
|
|
|
Valid values are ?. and ?\). To get both terminators, use t. While
|
|
|
|
?. may look nicer, it creates the danger that a line with leading
|
|
|
|
number may be incorrectly interpreted as an item. ?\) therefore is
|
|
|
|
the safe choice."
|
|
|
|
:group 'org-plain-lists
|
|
|
|
:type '(choice (const :tag "dot like in \"2.\"" ?.)
|
|
|
|
(const :tag "paren like in \"2)\"" ?\))
|
|
|
|
(const :tab "both" t)))
|
|
|
|
|
2008-11-21 00:54:46 -05:00
|
|
|
(defcustom org-list-two-spaces-after-bullet-regexp nil
|
|
|
|
"A regular expression matching bullets that should have 2 spaces after them.
|
|
|
|
When nil, no bullet will have two spaces after them.
|
2010-07-30 18:45:34 -04:00
|
|
|
When a string, it will be used as a regular expression. When the
|
2010-07-10 11:02:58 -04:00
|
|
|
bullet type of a list is changed, the new bullet type will be
|
|
|
|
matched against this regexp. If it matches, there will be two
|
2010-07-17 17:00:22 -04:00
|
|
|
spaces instead of one after the bullet in each item of the list."
|
2010-04-21 03:51:30 -04:00
|
|
|
:group 'org-plain-lists
|
2008-11-21 00:54:46 -05:00
|
|
|
:type '(choice
|
|
|
|
(const :tag "never" nil)
|
|
|
|
(regexp)))
|
|
|
|
|
2010-08-22 13:34:46 -04:00
|
|
|
(defcustom org-list-ending-method 'both
|
2010-08-19 10:09:45 -04:00
|
|
|
"Determine where plain lists should end.
|
2010-08-20 05:35:56 -04:00
|
|
|
Valid values are: `regexp', `indent' or `both'.
|
2010-08-19 10:09:45 -04:00
|
|
|
|
2010-08-20 05:35:56 -04:00
|
|
|
When set to `regexp', Org will look into two variables,
|
2010-08-19 10:09:45 -04:00
|
|
|
`org-empty-line-terminates-plain-lists' and the more general
|
2010-12-29 14:16:00 -05:00
|
|
|
`org-list-end-regexp', to determine what will end lists.
|
2010-08-19 10:09:45 -04:00
|
|
|
|
2010-08-22 13:34:46 -04:00
|
|
|
When set to `indent', a list will end whenever a line following
|
|
|
|
an item, but not starting one, is less or equally indented than
|
2010-12-29 14:16:00 -05:00
|
|
|
the first item of the list.
|
2010-08-19 10:09:45 -04:00
|
|
|
|
2010-08-23 07:32:00 -04:00
|
|
|
When set to `both', each of the preceding methods is applied to
|
|
|
|
determine lists endings. This is the default method."
|
2010-08-19 10:09:45 -04:00
|
|
|
:group 'org-plain-lists
|
|
|
|
:type '(choice
|
2010-08-23 07:32:00 -04:00
|
|
|
(const :tag "With a regexp defining ending" regexp)
|
|
|
|
(const :tag "With indentation of regular (no bullet) text" indent)
|
2010-08-19 10:09:45 -04:00
|
|
|
(const :tag "With both methods" both)))
|
|
|
|
|
2008-09-23 02:03:13 -04:00
|
|
|
(defcustom org-empty-line-terminates-plain-lists nil
|
2010-01-21 10:15:40 -05:00
|
|
|
"Non-nil means an empty line ends all plain list levels.
|
2010-08-19 10:09:45 -04:00
|
|
|
This variable only makes sense if `org-list-ending-method' is set
|
2010-08-28 06:53:19 -04:00
|
|
|
to `regexp' or `both'. This is then equivalent to set
|
2010-08-19 19:58:46 -04:00
|
|
|
`org-list-end-regexp' to \"^[ \\t]*$\"."
|
2008-09-23 02:03:13 -04:00
|
|
|
:group 'org-plain-lists
|
|
|
|
:type 'boolean)
|
|
|
|
|
2010-07-14 03:31:12 -04:00
|
|
|
(defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
|
2010-07-13 09:52:11 -04:00
|
|
|
"Regexp matching the end of all plain list levels.
|
2010-07-30 18:45:34 -04:00
|
|
|
It must start with \"^\" and end with \"\\n\". It defaults to 2
|
2010-07-14 03:31:12 -04:00
|
|
|
blank lines. `org-empty-line-terminates-plain-lists' has
|
2010-07-13 09:52:11 -04:00
|
|
|
precedence over it."
|
|
|
|
:group 'org-plain-lists
|
|
|
|
:type 'string)
|
|
|
|
|
2010-07-23 14:36:42 -04:00
|
|
|
(defcustom org-list-automatic-rules '((bullet . t)
|
|
|
|
(checkbox . t)
|
2011-01-06 12:54:30 -05:00
|
|
|
(indent . t))
|
2010-07-23 14:36:42 -04:00
|
|
|
"Non-nil means apply set of rules when acting on lists.
|
|
|
|
By default, automatic actions are taken when using
|
2010-09-14 12:30:03 -04:00
|
|
|
\\[org-meta-return], \\[org-metaright], \\[org-metaleft],
|
|
|
|
\\[org-shiftmetaright], \\[org-shiftmetaleft],
|
|
|
|
\\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
|
|
|
|
\\[org-insert-todo-heading]. You can disable individually these
|
|
|
|
rules by setting them to nil. Valid rules are:
|
2010-07-23 14:36:42 -04:00
|
|
|
|
|
|
|
bullet when non-nil, cycling bullet do not allow lists at
|
|
|
|
column 0 to have * as a bullet and descriptions lists
|
|
|
|
to be numbered.
|
|
|
|
checkbox when non-nil, checkbox statistics is updated each time
|
|
|
|
you either insert a new checkbox or toggle a checkbox.
|
2010-07-28 10:51:28 -04:00
|
|
|
It also prevents from inserting a checkbox in a
|
|
|
|
description item.
|
2010-08-20 05:35:56 -04:00
|
|
|
indent when non-nil, indenting or outdenting list top-item
|
|
|
|
with its subtree will move the whole list and
|
|
|
|
outdenting a list whose bullet is * to column 0 will
|
2011-01-06 12:54:30 -05:00
|
|
|
change that bullet to \"-\"."
|
2010-07-23 14:36:42 -04:00
|
|
|
:group 'org-plain-lists
|
|
|
|
:type '(alist :tag "Sets of rules"
|
|
|
|
:key-type
|
|
|
|
(choice
|
|
|
|
(const :tag "Bullet" bullet)
|
|
|
|
(const :tag "Checkbox" checkbox)
|
|
|
|
(const :tag "Indent" indent)
|
2010-08-20 05:35:56 -04:00
|
|
|
(const :tag "Insert" insert))
|
2010-07-23 14:36:42 -04:00
|
|
|
:value-type
|
|
|
|
(boolean :tag "Activate" :value t)))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2009-05-08 11:34:40 -04:00
|
|
|
(defcustom org-hierarchical-checkbox-statistics t
|
2010-01-21 10:15:40 -05:00
|
|
|
"Non-nil means checkbox statistics counts only the state of direct children.
|
2009-08-18 10:43:55 -04:00
|
|
|
When nil, all boxes below the cookie are counted.
|
2009-11-12 07:39:29 -05:00
|
|
|
This can be set to nil on a per-node basis using a COOKIE_DATA property
|
2009-08-18 10:43:55 -04:00
|
|
|
with the word \"recursive\" in the value."
|
2009-05-08 11:34:40 -04:00
|
|
|
:group 'org-plain-lists
|
|
|
|
:type 'boolean)
|
|
|
|
|
2008-09-23 02:03:13 -04:00
|
|
|
(defcustom org-description-max-indent 20
|
|
|
|
"Maximum indentation for the second line of a description list.
|
|
|
|
When the indentation would be larger than this, it will become
|
|
|
|
5 characters instead."
|
|
|
|
:group 'org-plain-lists
|
|
|
|
:type 'integer)
|
|
|
|
|
|
|
|
(defcustom org-list-radio-list-templates
|
|
|
|
'((latex-mode "% BEGIN RECEIVE ORGLST %n
|
|
|
|
% END RECEIVE ORGLST %n
|
|
|
|
\\begin{comment}
|
|
|
|
#+ORGLST: SEND %n org-list-to-latex
|
2010-07-12 08:44:51 -04:00
|
|
|
-
|
2008-09-23 02:03:13 -04:00
|
|
|
\\end{comment}\n")
|
|
|
|
(texinfo-mode "@c BEGIN RECEIVE ORGLST %n
|
|
|
|
@c END RECEIVE ORGLST %n
|
|
|
|
@ignore
|
|
|
|
#+ORGLST: SEND %n org-list-to-texinfo
|
2010-07-12 08:44:51 -04:00
|
|
|
-
|
2008-09-23 02:03:13 -04:00
|
|
|
@end ignore\n")
|
|
|
|
(html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
|
|
|
|
<!-- END RECEIVE ORGLST %n -->
|
|
|
|
<!--
|
|
|
|
#+ORGLST: SEND %n org-list-to-html
|
2010-07-12 08:44:51 -04:00
|
|
|
-
|
2008-09-23 02:03:13 -04:00
|
|
|
-->\n"))
|
|
|
|
"Templates for radio lists in different major modes.
|
|
|
|
All occurrences of %n in a template will be replaced with the name of the
|
|
|
|
list, obtained by prompting the user."
|
|
|
|
:group 'org-plain-lists
|
|
|
|
:type '(repeat
|
|
|
|
(list (symbol :tag "Major mode")
|
|
|
|
(string :tag "Format"))))
|
|
|
|
|
2011-01-02 09:57:15 -05:00
|
|
|
(defvar org-list-forbidden-blocks '("example" "verse" "src")
|
|
|
|
"Names of blocks where lists are not allowed.
|
|
|
|
Names must be in lower case.")
|
|
|
|
|
|
|
|
(defvar org-list-export-context '(block inlinetask)
|
|
|
|
"Context types where lists will be interpreted during export.
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2011-01-02 09:57:15 -05:00
|
|
|
Valid types are `drawer', `inlinetask' and `block'. More
|
|
|
|
specifically, type `block' is determined by the variable
|
|
|
|
`org-list-forbidden-blocks'.")
|
|
|
|
|
|
|
|
;;; Internal functions
|
2010-12-23 12:54:24 -05:00
|
|
|
|
2010-07-10 11:02:58 -04:00
|
|
|
(defun org-list-end-re ()
|
|
|
|
"Return the regex corresponding to the end of a list.
|
|
|
|
It depends on `org-empty-line-terminates-plain-lists'."
|
|
|
|
(if org-empty-line-terminates-plain-lists
|
2010-07-13 09:52:11 -04:00
|
|
|
"^[ \t]*\n"
|
|
|
|
org-list-end-regexp))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2010-04-21 09:52:01 -04:00
|
|
|
(defun org-item-re (&optional general)
|
|
|
|
"Return the correct regular expression for plain lists.
|
|
|
|
If GENERAL is non-nil, return the general regexp independent of the value
|
|
|
|
of `org-plain-list-ordered-item-terminator'."
|
|
|
|
(cond
|
|
|
|
((or general (eq org-plain-list-ordered-item-terminator t))
|
2010-07-27 14:52:30 -04:00
|
|
|
"\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
|
2010-04-21 09:52:01 -04:00
|
|
|
((= org-plain-list-ordered-item-terminator ?.)
|
2010-07-27 14:52:30 -04:00
|
|
|
"\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
|
2010-04-21 09:52:01 -04:00
|
|
|
((= org-plain-list-ordered-item-terminator ?\))
|
2010-07-27 14:52:30 -04:00
|
|
|
"\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
|
2010-04-21 09:52:01 -04:00
|
|
|
(t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
|
|
|
|
|
2010-07-20 15:13:37 -04:00
|
|
|
(defconst org-item-beginning-re (concat "^" (org-item-re))
|
|
|
|
"Regexp matching the beginning of a plain list item.")
|
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
(defconst org-list-full-item-re
|
2010-12-30 12:39:36 -05:00
|
|
|
(concat "^[ \t]*\\(\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\)"
|
2010-12-23 12:54:24 -05:00
|
|
|
"\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\)\\]\\)?"
|
2010-12-30 12:39:36 -05:00
|
|
|
"\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
|
|
|
|
"\\(?:\\(.*\\)[ \t]+::[ \t]+\\)?")
|
2010-12-23 12:54:24 -05:00
|
|
|
"Matches a list item and puts everything into groups:
|
2010-12-30 12:39:36 -05:00
|
|
|
group 1: bullet
|
|
|
|
group 2: counter
|
|
|
|
group 3: checkbox
|
|
|
|
group 4: description tag")
|
2010-12-23 12:54:24 -05:00
|
|
|
|
|
|
|
(defun org-list-context ()
|
|
|
|
"Determine context, and its boundaries, around point.
|
|
|
|
|
|
|
|
Context will be an alist like (MIN MAX CONTEXT) where MIN and MAX
|
2011-01-01 12:27:31 -05:00
|
|
|
are boundaries and CONTEXT is a symbol among `drawer', `block',
|
|
|
|
`invalid', `inlinetask' and nil.
|
2010-12-23 12:54:24 -05:00
|
|
|
|
2011-01-02 09:57:15 -05:00
|
|
|
Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
|
2010-12-23 12:54:24 -05:00
|
|
|
(save-match-data
|
2011-01-01 12:27:31 -05:00
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(let* ((outline-regexp (org-get-limited-outline-regexp))
|
|
|
|
;; can't use org-drawers-regexp as this function might be
|
|
|
|
;; called in buffers not in Org mode
|
|
|
|
(drawers-re (concat "^[ \t]*:\\("
|
|
|
|
(mapconcat 'regexp-quote org-drawers "\\|")
|
|
|
|
"\\):[ \t]*$"))
|
|
|
|
(case-fold-search t)
|
|
|
|
;; compute position of surrounding headings. this is the
|
|
|
|
;; default context.
|
|
|
|
(heading
|
|
|
|
(save-excursion
|
|
|
|
(list
|
|
|
|
(or (and (org-at-heading-p) (point-at-bol))
|
|
|
|
(outline-previous-heading)
|
|
|
|
(point-min))
|
|
|
|
(or (outline-next-heading)
|
|
|
|
(point-max))
|
|
|
|
nil)))
|
|
|
|
(prev-head (car heading))
|
|
|
|
(next-head (nth 1 heading))
|
|
|
|
;; Are we strictly inside a drawer?
|
|
|
|
(drawerp
|
|
|
|
(when (and (org-in-regexps-block-p
|
|
|
|
drawers-re "^[ \t]*:END:" prev-head)
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(and (not (looking-at drawers-re))
|
|
|
|
(not (looking-at "^[ \t]*:END:")))))
|
|
|
|
(save-excursion
|
|
|
|
(list
|
|
|
|
(progn
|
|
|
|
(re-search-backward drawers-re prev-head t)
|
|
|
|
(1+ (point-at-eol)))
|
|
|
|
(if (re-search-forward "^[ \t]*:END:" next-head t)
|
|
|
|
(1- (point-at-bol))
|
|
|
|
next-head)
|
|
|
|
'drawer))))
|
|
|
|
;; Are we strictly in a block, and of which type?
|
|
|
|
(blockp
|
|
|
|
(save-excursion
|
|
|
|
(when (and (org-in-regexps-block-p
|
|
|
|
"^[ \t]*#\\+begin_" "^[ \t]*#\\+end_" prev-head)
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(not (looking-at
|
|
|
|
"^[ \t]*#\\+\\(begin\\|end\\)_"))))
|
|
|
|
(list
|
|
|
|
(progn
|
|
|
|
(re-search-backward
|
|
|
|
"^[ \t]*#\\+begin_\\(\\S-+\\)" prev-head t)
|
|
|
|
(1+ (point-at-eol)))
|
|
|
|
(save-match-data
|
|
|
|
(if (re-search-forward "^[ \t]*#\\+end_" next-head t)
|
|
|
|
(1- (point-at-bol))
|
|
|
|
next-head))
|
2011-01-02 09:57:15 -05:00
|
|
|
(if (member (downcase (match-string 1))
|
|
|
|
org-list-forbidden-blocks)
|
2011-01-01 12:27:31 -05:00
|
|
|
'invalid
|
|
|
|
'block)))))
|
|
|
|
;; Are we in an inlinetask?
|
|
|
|
(inlinetaskp
|
|
|
|
(when (and (featurep 'org-inlinetask)
|
|
|
|
(org-inlinetask-in-task-p)
|
|
|
|
(not (looking-at "^\\*+")))
|
|
|
|
(save-excursion
|
|
|
|
(list
|
|
|
|
(progn (org-inlinetask-goto-beginning)
|
|
|
|
(1+ (point-at-eol)))
|
|
|
|
(progn
|
|
|
|
(org-inlinetask-goto-end)
|
|
|
|
(forward-line -1)
|
|
|
|
(1- (point-at-bol)))
|
|
|
|
'inlinetask))))
|
|
|
|
;; list actual candidates
|
|
|
|
(context-list
|
|
|
|
(delq nil (list heading drawerp blockp inlinetaskp))))
|
|
|
|
;; Return the closest context around
|
|
|
|
(assq (apply 'max (mapcar 'car context-list)) context-list)))))
|
2010-12-23 12:54:24 -05:00
|
|
|
|
2011-01-01 18:26:37 -05:00
|
|
|
(defun org-list-search-generic (search re bound noerr)
|
|
|
|
"Search a string in valid contexts for lists.
|
2010-08-21 07:32:33 -04:00
|
|
|
Arguments SEARCH, RE, BOUND and NOERR are similar to those in
|
2011-01-01 18:26:37 -05:00
|
|
|
`re-search-forward'."
|
2010-08-19 10:09:45 -04:00
|
|
|
(catch 'exit
|
|
|
|
(let ((origin (point)))
|
|
|
|
(while t
|
2010-08-19 19:58:46 -04:00
|
|
|
;; 1. No match: return to origin or bound, depending on NOERR.
|
2010-08-19 10:09:45 -04:00
|
|
|
(unless (funcall search re bound noerr)
|
2010-10-17 12:35:02 -04:00
|
|
|
(throw 'exit (and (goto-char (if (memq noerr '(t nil)) origin bound))
|
2010-08-19 10:09:45 -04:00
|
|
|
nil)))
|
2011-01-01 18:26:37 -05:00
|
|
|
;; 2. Match in an `invalid' context: continue searching. Else,
|
|
|
|
;; return point.
|
|
|
|
(unless (eq (org-list-context) 'invalid) (throw 'exit (point)))))))
|
|
|
|
|
|
|
|
(defun org-list-search-backward (regexp &optional bound noerror)
|
|
|
|
"Like `re-search-backward' but stop only where lists are recognized.
|
2010-08-21 07:32:33 -04:00
|
|
|
Arguments REGEXP, BOUND and NOERROR are similar to those used in
|
|
|
|
`re-search-backward'."
|
2011-01-01 18:26:37 -05:00
|
|
|
(org-list-search-generic #'re-search-backward
|
|
|
|
regexp (or bound (point-min)) noerror))
|
2010-07-12 04:48:17 -04:00
|
|
|
|
2011-01-01 18:26:37 -05:00
|
|
|
(defun org-list-search-forward (regexp &optional bound noerror)
|
|
|
|
"Like `re-search-forward' but stop only where lists are recognized.
|
2010-08-21 07:32:33 -04:00
|
|
|
Arguments REGEXP, BOUND and NOERROR are similar to those used in
|
|
|
|
`re-search-forward'."
|
2011-01-01 18:26:37 -05:00
|
|
|
(org-list-search-generic #'re-search-forward
|
|
|
|
regexp (or bound (point-max)) noerror))
|
2010-08-19 10:09:45 -04:00
|
|
|
|
2010-07-19 05:21:22 -04:00
|
|
|
(defun org-list-at-regexp-after-bullet-p (regexp)
|
|
|
|
"Is point at a list item with REGEXP after bullet?"
|
|
|
|
(and (org-at-item-p)
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (match-end 0))
|
2010-07-28 09:53:37 -04:00
|
|
|
;; Ignore counter if any
|
2010-08-20 07:05:38 -04:00
|
|
|
(when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
|
2010-07-28 09:53:37 -04:00
|
|
|
(goto-char (match-end 0)))
|
2010-07-19 05:21:22 -04:00
|
|
|
(looking-at regexp))))
|
|
|
|
|
2010-12-24 07:25:37 -05:00
|
|
|
(defun org-list-separating-blank-lines-number (pos struct prevs)
|
2010-08-28 06:53:19 -04:00
|
|
|
"Return number of blank lines that should separate items in list.
|
2010-12-29 14:16:00 -05:00
|
|
|
POS is the position at item beginning to be considered. STRUCT is
|
|
|
|
the list structure. PREVS is the alist of previous items. See
|
|
|
|
`org-list-struct-prev-alist'.
|
2010-08-28 06:53:19 -04:00
|
|
|
|
|
|
|
Assume point is at item's beginning. If the item is alone, apply
|
|
|
|
some heuristics to guess the result."
|
|
|
|
(save-excursion
|
|
|
|
(let ((insert-blank-p
|
|
|
|
(cdr (assq 'plain-list-item org-blank-before-new-entry)))
|
|
|
|
usr-blank)
|
|
|
|
(cond
|
|
|
|
;; Trivial cases where there should be none.
|
|
|
|
((or (and (not (eq org-list-ending-method 'indent))
|
|
|
|
org-empty-line-terminates-plain-lists)
|
|
|
|
(not insert-blank-p)) 0)
|
|
|
|
;; When `org-blank-before-new-entry' says so, it is 1.
|
|
|
|
((eq insert-blank-p t) 1)
|
|
|
|
;; plain-list-item is 'auto. Count blank lines separating
|
|
|
|
;; neighbours items in list.
|
2010-12-24 07:25:37 -05:00
|
|
|
(t (let ((next-p (org-list-get-next-item (point) struct prevs)))
|
2010-08-28 06:53:19 -04:00
|
|
|
(cond
|
|
|
|
;; Is there a next item?
|
|
|
|
(next-p (goto-char next-p)
|
|
|
|
(org-back-over-empty-lines))
|
|
|
|
;; Is there a previous item?
|
2010-12-24 07:25:37 -05:00
|
|
|
((org-list-get-prev-item (point) struct prevs)
|
2010-08-28 06:53:19 -04:00
|
|
|
(org-back-over-empty-lines))
|
|
|
|
;; User inserted blank lines, trust him
|
2010-12-24 07:25:37 -05:00
|
|
|
((and (> pos (org-list-get-item-end-before-blank pos struct))
|
2010-08-28 06:53:19 -04:00
|
|
|
(> (save-excursion
|
2010-08-30 02:36:13 -04:00
|
|
|
(goto-char pos)
|
2010-08-28 06:53:19 -04:00
|
|
|
(skip-chars-backward " \t")
|
|
|
|
(setq usr-blank (org-back-over-empty-lines))) 0))
|
|
|
|
usr-blank)
|
|
|
|
;; Are there blank lines inside the item ?
|
|
|
|
((save-excursion
|
2011-01-01 18:26:37 -05:00
|
|
|
(org-list-search-forward
|
2010-12-24 07:25:37 -05:00
|
|
|
"^[ \t]*$" (org-list-get-item-end-before-blank pos struct) t))
|
|
|
|
1)
|
2010-08-28 06:53:19 -04:00
|
|
|
;; No parent: no blank line.
|
|
|
|
(t 0))))))))
|
|
|
|
|
2010-07-19 05:33:20 -04:00
|
|
|
(defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
|
|
|
|
"Insert a new list item at POS.
|
2010-07-15 12:28:01 -04:00
|
|
|
If POS is before first character after bullet of the item, the
|
|
|
|
new item will be created before the current one.
|
|
|
|
|
|
|
|
Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
|
|
|
|
after the bullet. Cursor will be after this text once the
|
2010-07-18 05:04:15 -04:00
|
|
|
function ends."
|
2010-12-24 07:25:37 -05:00
|
|
|
(let ((case-fold-search t))
|
2011-01-06 12:54:30 -05:00
|
|
|
;; 1. Get information about list: structure, usual helper
|
2010-12-24 07:25:37 -05:00
|
|
|
;; functions, position of point with regards to item start
|
|
|
|
;; (BEFOREP), blank lines number separating items (BLANK-NB),
|
|
|
|
;; position of split (POS) if we're allowed to (SPLIT-LINE-P).
|
|
|
|
(let* ((pos (point))
|
2011-01-02 09:16:16 -05:00
|
|
|
(item (goto-char (org-list-get-item-begin)))
|
2010-12-24 07:25:37 -05:00
|
|
|
(struct (org-list-struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(item-end (org-list-get-item-end item struct))
|
|
|
|
(item-end-no-blank (org-list-get-item-end-before-blank item struct))
|
2010-12-30 12:39:36 -05:00
|
|
|
(beforep (and (looking-at org-list-full-item-re)
|
2010-12-24 07:25:37 -05:00
|
|
|
(<= pos (match-end 0))))
|
|
|
|
(split-line-p (org-get-alist-option org-M-RET-may-split-line 'item))
|
|
|
|
(blank-nb (org-list-separating-blank-lines-number
|
|
|
|
item struct prevs))
|
2011-01-06 12:54:30 -05:00
|
|
|
;; 2. Build the new item to be created. Concatenate same
|
2010-12-24 07:25:37 -05:00
|
|
|
;; bullet as item, checkbox, text AFTER-BULLET if
|
|
|
|
;; provided, and text cut from point to end of item
|
|
|
|
;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
|
|
|
|
;; BEFOREP and SPLIT-LINE-P. The difference of size
|
|
|
|
;; between what was cut and what was inserted in buffer
|
|
|
|
;; is stored in SIZE-OFFSET.
|
|
|
|
(ind (org-list-get-ind item struct))
|
|
|
|
(bullet (org-list-bullet-string (org-list-get-bullet item struct)))
|
|
|
|
(box (when checkbox "[ ]"))
|
|
|
|
(text-cut
|
|
|
|
(and (not beforep) split-line-p
|
|
|
|
(progn
|
|
|
|
(goto-char pos)
|
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(setq pos (point))
|
|
|
|
(delete-and-extract-region pos item-end-no-blank))))
|
|
|
|
(body (concat bullet (when box (concat box " ")) after-bullet
|
|
|
|
(or (and text-cut
|
|
|
|
(if (string-match "\\`[ \t]+" text-cut)
|
|
|
|
(replace-match "" t t text-cut)
|
|
|
|
text-cut))
|
|
|
|
"")))
|
|
|
|
(item-sep (make-string (1+ blank-nb) ?\n))
|
|
|
|
(item-size (+ ind (length body) (length item-sep)))
|
|
|
|
(size-offset (- item-size (length text-cut))))
|
|
|
|
;; 4. Insert effectively item into buffer
|
|
|
|
(goto-char item)
|
|
|
|
(org-indent-to-column ind)
|
|
|
|
(insert body)
|
|
|
|
(insert item-sep)
|
|
|
|
;; 5. Add new item to STRUCT.
|
|
|
|
(mapc (lambda (e)
|
|
|
|
(let ((p (car e))
|
2010-12-30 12:39:36 -05:00
|
|
|
(end (nth 6 e)))
|
2010-12-24 07:25:37 -05:00
|
|
|
(cond
|
|
|
|
;; Before inserted item, positions don't change but
|
|
|
|
;; an item ending after insertion has its end shifted
|
|
|
|
;; by SIZE-OFFSET.
|
|
|
|
((< p item)
|
2010-12-30 12:39:36 -05:00
|
|
|
(when (> end item) (setcar (nthcdr 6 e) (+ end size-offset))))
|
2010-12-24 07:25:37 -05:00
|
|
|
;; Trivial cases where current item isn't split in
|
|
|
|
;; two. Just shift every item after new one by
|
|
|
|
;; ITEM-SIZE.
|
|
|
|
((or beforep (not split-line-p))
|
|
|
|
(setcar e (+ p item-size))
|
2010-12-30 12:39:36 -05:00
|
|
|
(setcar (nthcdr 6 e) (+ end item-size)))
|
2010-12-24 07:25:37 -05:00
|
|
|
;; Item is split in two: elements before POS are just
|
|
|
|
;; shifted by ITEM-SIZE. In the case item would end
|
|
|
|
;; after split POS, ending is only shifted by
|
|
|
|
;; SIZE-OFFSET.
|
|
|
|
((< p pos)
|
|
|
|
(setcar e (+ p item-size))
|
|
|
|
(if (< end pos)
|
2010-12-30 12:39:36 -05:00
|
|
|
(setcar (nthcdr 6 e) (+ end item-size))
|
|
|
|
(setcar (nthcdr 6 e) (+ end size-offset))))
|
2010-12-24 07:25:37 -05:00
|
|
|
;; Elements after POS are moved into new item. Length
|
|
|
|
;; of ITEM-SEP has to be removed as ITEM-SEP
|
|
|
|
;; doesn't appear in buffer yet.
|
|
|
|
((< p item-end)
|
|
|
|
(setcar e (+ p size-offset (- item pos (length item-sep))))
|
|
|
|
(if (= end item-end)
|
2010-12-30 12:39:36 -05:00
|
|
|
(setcar (nthcdr 6 e) (+ item item-size))
|
|
|
|
(setcar (nthcdr 6 e)
|
2010-12-24 07:25:37 -05:00
|
|
|
(+ end size-offset
|
|
|
|
(- item pos (length item-sep))))))
|
|
|
|
;; Elements at ITEM-END or after are only shifted by
|
|
|
|
;; SIZE-OFFSET.
|
|
|
|
(t (setcar e (+ p size-offset))
|
2010-12-30 12:39:36 -05:00
|
|
|
(setcar (nthcdr 6 e) (+ end size-offset))))))
|
2010-12-24 07:25:37 -05:00
|
|
|
struct)
|
|
|
|
(setq struct (sort
|
2010-12-30 16:11:27 -05:00
|
|
|
(cons (list item ind bullet nil box nil (+ item item-size))
|
2010-12-24 07:25:37 -05:00
|
|
|
struct)
|
|
|
|
(lambda (e1 e2) (< (car e1) (car e2)))))
|
|
|
|
;; 6. If not BEFOREP, new item must appear after ITEM, so
|
|
|
|
;; exchange ITEM with the next item in list. Position cursor
|
|
|
|
;; after bullet, counter, checkbox, and label.
|
|
|
|
(if beforep
|
|
|
|
(goto-char item)
|
|
|
|
(setq struct (org-list-exchange-items item (+ item item-size) struct))
|
|
|
|
(goto-char (org-list-get-next-item
|
|
|
|
item struct (org-list-struct-prev-alist struct))))
|
|
|
|
(org-list-struct-fix-struct struct (org-list-struct-parent-alist struct))
|
|
|
|
(when checkbox (org-update-checkbox-count-maybe))
|
2010-12-30 12:39:36 -05:00
|
|
|
(looking-at org-list-full-item-re)
|
2010-12-24 07:25:37 -05:00
|
|
|
(goto-char (match-end 0))
|
|
|
|
t)))
|
2010-07-15 12:28:01 -04:00
|
|
|
|
2010-08-21 19:10:58 -04:00
|
|
|
(defvar org-last-indent-begin-marker (make-marker))
|
|
|
|
(defvar org-last-indent-end-marker (make-marker))
|
|
|
|
|
2010-12-23 18:53:30 -05:00
|
|
|
(defun org-list-indent-item-generic (arg no-subtree struct)
|
2010-08-21 19:10:58 -04:00
|
|
|
"Indent a local list item including its children.
|
|
|
|
When number ARG is a negative, item will be outdented, otherwise
|
|
|
|
it will be indented.
|
|
|
|
|
2010-08-23 05:24:57 -04:00
|
|
|
If a region is active, all items inside will be moved.
|
2010-08-22 14:01:33 -04:00
|
|
|
|
|
|
|
If NO-SUBTREE is non-nil, only indent the item itself, not its
|
2010-08-21 19:10:58 -04:00
|
|
|
children.
|
|
|
|
|
2010-12-29 14:16:00 -05:00
|
|
|
STRUCT is the list structure. Return t if successful."
|
2010-12-23 18:53:30 -05:00
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(let* ((regionp (org-region-active-p))
|
|
|
|
(rbeg (and regionp (region-beginning)))
|
2010-12-29 12:24:50 -05:00
|
|
|
(rend (and regionp (region-end)))
|
|
|
|
(top (org-list-get-top-point struct))
|
|
|
|
(parents (org-list-struct-parent-alist struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
;; Are we going to move the whole list?
|
2010-12-30 16:23:51 -05:00
|
|
|
(specialp
|
|
|
|
(and (= top (point))
|
|
|
|
(cdr (assq 'indent org-list-automatic-rules))
|
|
|
|
(if no-subtree
|
|
|
|
(error
|
|
|
|
"First item of list cannot move without its subtree")
|
|
|
|
t))))
|
2010-12-29 12:24:50 -05:00
|
|
|
;; Determine begin and end points of zone to indent. If moving
|
|
|
|
;; more than one item, save them for subsequent moves.
|
|
|
|
(unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
|
|
|
|
(memq this-command '(org-shiftmetaright org-shiftmetaleft)))
|
|
|
|
(if regionp
|
|
|
|
(progn
|
|
|
|
(set-marker org-last-indent-begin-marker rbeg)
|
|
|
|
(set-marker org-last-indent-end-marker rend))
|
|
|
|
(set-marker org-last-indent-begin-marker (point))
|
|
|
|
(set-marker org-last-indent-end-marker
|
|
|
|
(cond
|
|
|
|
(specialp (org-list-get-bottom-point struct))
|
|
|
|
(no-subtree (1+ (point)))
|
|
|
|
(t (org-list-get-item-end (point) struct))))))
|
|
|
|
(let* ((beg (marker-position org-last-indent-begin-marker))
|
|
|
|
(end (marker-position org-last-indent-end-marker)))
|
|
|
|
(cond
|
|
|
|
;; Special case: moving top-item with indent rule
|
|
|
|
(specialp
|
|
|
|
(let* ((level-skip (org-level-increment))
|
|
|
|
(offset (if (< arg 0) (- level-skip) level-skip))
|
|
|
|
(top-ind (org-list-get-ind beg struct))
|
|
|
|
(old-struct (mapcar (lambda (e) (copy-alist e)) struct)))
|
|
|
|
(if (< (+ top-ind offset) 0)
|
|
|
|
(error "Cannot outdent beyond margin")
|
|
|
|
;; Change bullet if necessary
|
|
|
|
(when (and (= (+ top-ind offset) 0)
|
|
|
|
(string-match "*"
|
|
|
|
(org-list-get-bullet beg struct)))
|
|
|
|
(org-list-set-bullet beg struct
|
|
|
|
(org-list-bullet-string "-")))
|
|
|
|
;; Shift every item by OFFSET and fix bullets. Then
|
|
|
|
;; apply changes to buffer.
|
|
|
|
(mapc (lambda (e)
|
|
|
|
(let ((ind (org-list-get-ind (car e) struct)))
|
|
|
|
(org-list-set-ind (car e) struct (+ ind offset))))
|
|
|
|
struct)
|
|
|
|
(org-list-struct-fix-bul struct prevs)
|
|
|
|
(org-list-struct-apply-struct struct old-struct))))
|
|
|
|
;; Forbidden move:
|
|
|
|
((and (< arg 0)
|
|
|
|
;; If only one item is moved, it mustn't have a child
|
|
|
|
(or (and no-subtree
|
|
|
|
(not regionp)
|
|
|
|
(org-list-has-child-p beg struct))
|
|
|
|
;; If a subtree or region is moved, the last item
|
|
|
|
;; of the subtree mustn't have a child
|
|
|
|
(let ((last-item (caar
|
|
|
|
(reverse
|
|
|
|
(org-remove-if
|
|
|
|
(lambda (e) (>= (car e) end))
|
|
|
|
struct)))))
|
|
|
|
(org-list-has-child-p last-item struct))))
|
|
|
|
(error "Cannot outdent an item without its children"))
|
|
|
|
;; Normal shifting
|
|
|
|
(t
|
|
|
|
(let* ((new-parents
|
|
|
|
(if (< arg 0)
|
|
|
|
(org-list-struct-outdent beg end struct parents)
|
|
|
|
(org-list-struct-indent beg end struct parents prevs))))
|
|
|
|
(org-list-struct-fix-struct struct new-parents))
|
|
|
|
(org-update-checkbox-count-maybe))))))
|
2010-12-23 18:53:30 -05:00
|
|
|
t)
|
2010-08-21 19:10:58 -04:00
|
|
|
|
2010-07-10 11:02:58 -04:00
|
|
|
;;; Predicates
|
|
|
|
|
|
|
|
(defun org-in-item-p ()
|
2010-12-29 14:16:00 -05:00
|
|
|
"Return item beginning position when in a plain list, nil otherwise.
|
2010-08-19 10:09:45 -04:00
|
|
|
This checks `org-list-ending-method'."
|
2010-07-10 11:02:58 -04:00
|
|
|
(save-excursion
|
2010-08-12 08:14:37 -04:00
|
|
|
(beginning-of-line)
|
2010-12-23 18:54:48 -05:00
|
|
|
(unless (or (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
|
|
|
|
(and (not (eq org-list-ending-method 'indent))
|
|
|
|
(looking-at (org-list-end-re))
|
|
|
|
(progn (forward-line -1) (looking-at (org-list-end-re)))))
|
|
|
|
(or (and (org-at-item-p) (point-at-bol))
|
|
|
|
(let* ((case-fold-search t)
|
|
|
|
(context (org-list-context))
|
|
|
|
(lim-up (car context))
|
|
|
|
(inlinetask-re (and (featurep 'org-inlinetask)
|
|
|
|
(org-inlinetask-outline-regexp)))
|
|
|
|
(ind-ref (if (looking-at "^[ \t]*$")
|
|
|
|
10000
|
|
|
|
(org-get-indentation))))
|
|
|
|
(catch 'exit
|
|
|
|
(while t
|
|
|
|
(let ((ind (org-get-indentation)))
|
|
|
|
(cond
|
|
|
|
((<= (point) lim-up)
|
2010-12-28 19:43:20 -05:00
|
|
|
(throw 'exit (and (org-at-item-p) (< ind ind-ref) (point))))
|
2010-12-23 18:54:48 -05:00
|
|
|
((and (not (eq org-list-ending-method 'indent))
|
|
|
|
(looking-at (org-list-end-re)))
|
|
|
|
(throw 'exit nil))
|
|
|
|
;; Skip blocks, drawers, inline-tasks, blank lines
|
|
|
|
((looking-at "^[ \t]*#\\+end_")
|
|
|
|
(re-search-backward "^[ \t]*#\\+begin_" nil t))
|
|
|
|
((looking-at "^[ \t]*:END:")
|
|
|
|
(re-search-backward org-drawer-regexp nil t)
|
|
|
|
(beginning-of-line))
|
|
|
|
((and inlinetask-re (looking-at inlinetask-re))
|
|
|
|
(org-inlinetask-goto-beginning)
|
|
|
|
(forward-line -1))
|
|
|
|
((looking-at "^[ \t]*$")
|
|
|
|
(forward-line -1))
|
|
|
|
((< ind ind-ref)
|
|
|
|
(if (org-at-item-p)
|
|
|
|
(throw 'exit (point))
|
|
|
|
(setq ind-ref ind)
|
|
|
|
(forward-line -1)))
|
|
|
|
(t (if (and (eq org-list-ending-method 'regexp)
|
|
|
|
(org-at-item-p))
|
|
|
|
(throw 'exit (point))
|
|
|
|
(forward-line -1))))))))))))
|
2010-07-10 11:02:58 -04:00
|
|
|
|
2008-09-23 02:03:13 -04:00
|
|
|
(defun org-at-item-p ()
|
|
|
|
"Is point in a line starting a hand-formatted item?"
|
2010-04-21 09:52:01 -04:00
|
|
|
(save-excursion
|
2010-07-28 09:53:37 -04:00
|
|
|
(beginning-of-line) (looking-at org-item-beginning-re)))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
Support for Shift Selection
Emacs 23 introduces `shift-selection-mode', and even turns it on by
default. Therefore, it is fair to assume that users of Emacs 23 will
expect to be able to select a region by holding down shift will moving
the cursor. This conflicts with the use of shifted cursor keys in
Org-mode for other purposes, in particular to "change the item under
the cursor", like it is done for time stamps, item bullet types, TODO
keywords and priorities.
This commit tries to support `shift-selection-mode' as much as possible,
while retaining these valuable commands keys for Org-mode. The
following things are changed:
1. The range of contexts where Org's commands do act is reduced.
- S-up and S-down no longer jump to the previous and next plain list
item - you can use the paragraph commands C-up and C-down for this
quite well.
- S-left and S-right only change the bullet type of a plain list
item when the cursor is *exactly* on the bullet, not just anywhere
in the item line.
2. When a S-cursor key is used outside a special context, a region is
started or extended.
3. When a region has already been started, it will be continue to
extend even if the S-cursor keys move the cursor into a special
context.
4. If you want S-cursor selection to work in headlines as well, you
can set the variable `org-support-shift-select' to
`even-in-headlines'. Then you need to rely on `C-c C-t' to set
TODO keywords, and on `C-c ,' to set priorities.
If you want everything to behave the way it was, set
`org-support-shift-select' to nil.
2009-01-26 10:23:44 -05:00
|
|
|
(defun org-at-item-bullet-p ()
|
|
|
|
"Is point at the bullet of a plain list item?"
|
|
|
|
(and (org-at-item-p)
|
|
|
|
(not (member (char-after) '(?\ ?\t)))
|
|
|
|
(< (point) (match-end 0))))
|
|
|
|
|
2010-07-17 09:17:38 -04:00
|
|
|
(defun org-at-item-timer-p ()
|
2010-07-19 04:50:15 -04:00
|
|
|
"Is point at a line starting a plain list item with a timer?"
|
2010-08-19 10:09:45 -04:00
|
|
|
(org-list-at-regexp-after-bullet-p
|
|
|
|
"\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
|
2010-07-19 05:21:22 -04:00
|
|
|
|
2010-07-27 18:05:42 -04:00
|
|
|
(defun org-at-item-description-p ()
|
2010-07-19 05:21:22 -04:00
|
|
|
"Is point at a description list item?"
|
2010-07-19 06:38:14 -04:00
|
|
|
(org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
|
2010-07-17 09:17:38 -04:00
|
|
|
|
2008-09-23 02:03:13 -04:00
|
|
|
(defun org-at-item-checkbox-p ()
|
|
|
|
"Is point at a line starting a plain-list item with a checklet?"
|
2010-07-19 11:02:58 -04:00
|
|
|
(org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2010-07-10 11:02:58 -04:00
|
|
|
;;; Navigate
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2010-12-23 18:54:48 -05:00
|
|
|
(defalias 'org-list-get-item-begin 'org-in-item-p)
|
2008-09-23 02:03:13 -04:00
|
|
|
|
|
|
|
(defun org-beginning-of-item ()
|
|
|
|
"Go to the beginning of the current hand-formatted item.
|
2010-08-28 06:53:19 -04:00
|
|
|
If the cursor is not in an item, throw an error."
|
2008-09-23 02:03:13 -04:00
|
|
|
(interactive)
|
2010-12-23 18:54:48 -05:00
|
|
|
(let ((begin (org-in-item-p)))
|
|
|
|
(if begin (goto-char begin) (error "Not in an item"))))
|
2010-08-28 06:53:19 -04:00
|
|
|
|
|
|
|
(defun org-beginning-of-item-list ()
|
|
|
|
"Go to the beginning item of the current list or sublist.
|
|
|
|
Return an error if not in a list."
|
2008-09-23 02:03:13 -04:00
|
|
|
(interactive)
|
2010-12-23 18:54:48 -05:00
|
|
|
(let ((begin (org-in-item-p)))
|
|
|
|
(if (not begin)
|
|
|
|
(error "Not in an item")
|
|
|
|
(goto-char begin)
|
|
|
|
(let ((struct (org-list-struct)))
|
|
|
|
(goto-char (org-list-get-list-begin begin (org-list-struct)))))))
|
2010-07-10 11:02:58 -04:00
|
|
|
|
2010-08-28 06:53:19 -04:00
|
|
|
(defun org-end-of-item-list ()
|
|
|
|
"Go to the end of the current list or sublist.
|
|
|
|
If the cursor in not in an item, throw an error."
|
|
|
|
(interactive)
|
2010-12-23 18:54:48 -05:00
|
|
|
(let ((begin (org-in-item-p)))
|
|
|
|
(if (not begin)
|
|
|
|
(error "Not in an item")
|
|
|
|
(goto-char begin)
|
|
|
|
(let ((struct (org-list-struct)))
|
|
|
|
(goto-char (org-list-get-list-end begin (org-list-struct)))))))
|
2010-08-28 06:53:19 -04:00
|
|
|
|
|
|
|
(defun org-end-of-item ()
|
|
|
|
"Go to the end of the current hand-formatted item.
|
|
|
|
If the cursor is not in an item, throw an error."
|
|
|
|
(interactive)
|
2010-12-23 18:54:48 -05:00
|
|
|
(let ((begin (org-in-item-p)))
|
|
|
|
(if (not begin)
|
|
|
|
(error "Not in an item")
|
|
|
|
(goto-char begin)
|
|
|
|
(let ((struct (org-list-struct)))
|
|
|
|
(goto-char (org-list-get-item-end begin struct))))))
|
2010-04-21 09:52:01 -04:00
|
|
|
|
2008-09-23 02:03:13 -04:00
|
|
|
(defun org-previous-item ()
|
2010-07-10 11:02:58 -04:00
|
|
|
"Move to the beginning of the previous item.
|
|
|
|
Item is at the same level in the current plain list. Error if not
|
|
|
|
in a plain list, or if this is the first item in the list."
|
2008-09-23 02:03:13 -04:00
|
|
|
(interactive)
|
2010-12-23 18:54:48 -05:00
|
|
|
(let ((begin (org-in-item-p)))
|
|
|
|
(if (not begin)
|
|
|
|
(error "Not in an item")
|
|
|
|
(goto-char begin)
|
|
|
|
(let* ((struct (org-list-struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(prevp (org-list-get-prev-item begin struct prevs)))
|
|
|
|
(if prevp (goto-char prevp) (error "On first item"))))))
|
2010-07-10 11:02:58 -04:00
|
|
|
|
2010-08-28 06:53:19 -04:00
|
|
|
(defun org-next-item ()
|
|
|
|
"Move to the beginning of the next item.
|
|
|
|
Item is at the same level in the current plain list. Error if not
|
|
|
|
in a plain list, or if this is the last item in the list."
|
2010-07-10 11:02:58 -04:00
|
|
|
(interactive)
|
2010-12-23 18:54:48 -05:00
|
|
|
(let ((begin (org-in-item-p)))
|
|
|
|
(if (not begin)
|
|
|
|
(error "Not in an item")
|
|
|
|
(goto-char begin)
|
|
|
|
(let* ((struct (org-list-struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(prevp (org-list-get-next-item begin struct prevs)))
|
|
|
|
(if prevp (goto-char prevp) (error "On last item"))))))
|
2010-07-10 11:02:58 -04:00
|
|
|
|
|
|
|
;;; Manipulate
|
|
|
|
|
2010-12-23 18:53:30 -05:00
|
|
|
(defun org-list-exchange-items (beg-A beg-B struct)
|
2010-12-23 18:54:48 -05:00
|
|
|
"Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
|
2010-12-24 07:25:37 -05:00
|
|
|
Blank lines at the end of items are left in place. Return the new
|
|
|
|
structure after the changes.
|
2010-12-23 18:54:48 -05:00
|
|
|
|
2010-12-24 07:25:37 -05:00
|
|
|
Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
|
|
|
|
to the same sub-list.
|
2010-12-23 18:54:48 -05:00
|
|
|
|
|
|
|
This function modifies STRUCT."
|
2009-07-06 02:53:50 -04:00
|
|
|
(save-excursion
|
2010-12-24 07:25:37 -05:00
|
|
|
(let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct))
|
|
|
|
(end-B-no-blank (org-list-get-item-end-before-blank beg-B struct))
|
|
|
|
(end-A (org-list-get-item-end beg-A struct))
|
|
|
|
(end-B (org-list-get-item-end beg-B struct))
|
|
|
|
(size-A (- end-A-no-blank beg-A))
|
|
|
|
(size-B (- end-B-no-blank beg-B))
|
2010-07-10 11:02:58 -04:00
|
|
|
(body-A (buffer-substring beg-A end-A-no-blank))
|
|
|
|
(body-B (buffer-substring beg-B end-B-no-blank))
|
2010-12-24 07:25:37 -05:00
|
|
|
(between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B))
|
|
|
|
(sub-A (cons beg-A (org-list-get-subtree beg-A struct)))
|
|
|
|
(sub-B (cons beg-B (org-list-get-subtree beg-B struct))))
|
|
|
|
;; 1. Move effectively items in buffer.
|
2010-07-10 11:02:58 -04:00
|
|
|
(goto-char beg-A)
|
|
|
|
(delete-region beg-A end-B-no-blank)
|
2010-12-23 18:54:48 -05:00
|
|
|
(insert (concat body-B between-A-no-blank-and-B body-A))
|
2010-12-24 07:25:37 -05:00
|
|
|
;; 2. Now modify struct. No need to re-read the list, the
|
|
|
|
;; transformation is just a shift of positions. Some special
|
|
|
|
;; attention is required for items ending at END-A and END-B
|
|
|
|
;; as empty spaces are not moved there. In others words, item
|
|
|
|
;; BEG-A will end with whitespaces that were at the end of
|
|
|
|
;; BEG-B and the same applies to BEG-B.
|
|
|
|
(mapc (lambda (e)
|
|
|
|
(let ((pos (car e)))
|
|
|
|
(cond
|
|
|
|
((< pos beg-A))
|
|
|
|
((memq pos sub-A)
|
2010-12-30 12:39:36 -05:00
|
|
|
(let ((end-e (nth 6 e)))
|
2010-12-24 07:25:37 -05:00
|
|
|
(setcar e (+ pos (- end-B-no-blank end-A-no-blank)))
|
2010-12-30 12:39:36 -05:00
|
|
|
(setcar (nthcdr 6 e)
|
2010-12-24 07:25:37 -05:00
|
|
|
(+ end-e (- end-B-no-blank end-A-no-blank)))
|
2010-12-30 12:39:36 -05:00
|
|
|
(when (= end-e end-A) (setcar (nthcdr 6 e) end-B))))
|
2010-12-24 07:25:37 -05:00
|
|
|
((memq pos sub-B)
|
2010-12-30 12:39:36 -05:00
|
|
|
(let ((end-e (nth 6 e)))
|
2010-12-24 07:25:37 -05:00
|
|
|
(setcar e (- (+ pos beg-A) beg-B))
|
2010-12-30 12:39:36 -05:00
|
|
|
(setcar (nthcdr 6 e) (+ end-e (- beg-A beg-B)))
|
2010-12-24 07:25:37 -05:00
|
|
|
(when (= end-e end-B)
|
2010-12-30 12:39:36 -05:00
|
|
|
(setcar (nthcdr 6 e)
|
2010-12-24 07:25:37 -05:00
|
|
|
(+ beg-A size-B (- end-A end-A-no-blank))))))
|
|
|
|
((< pos beg-B)
|
2010-12-30 12:39:36 -05:00
|
|
|
(let ((end-e (nth 6 e)))
|
2010-12-24 07:25:37 -05:00
|
|
|
(setcar e (+ pos (- size-B size-A)))
|
2010-12-30 12:39:36 -05:00
|
|
|
(setcar (nthcdr 6 e) (+ end-e (- size-B size-A))))))))
|
2010-12-24 07:25:37 -05:00
|
|
|
struct)
|
|
|
|
(sort struct (lambda (e1 e2) (< (car e1) (car e2)))))))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
|
|
|
(defun org-move-item-down ()
|
|
|
|
"Move the plain list item at point down, i.e. swap with following item.
|
|
|
|
Subitems (items with larger indentation) are considered part of the item,
|
|
|
|
so this really moves item trees."
|
|
|
|
(interactive)
|
2010-12-23 18:53:30 -05:00
|
|
|
(unless (org-at-item-p) (error "Not at an item"))
|
|
|
|
(let* ((pos (point))
|
|
|
|
(col (current-column))
|
|
|
|
(actual-item (point-at-bol))
|
|
|
|
(struct (org-list-struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(next-item (org-list-get-next-item (point-at-bol) struct prevs)))
|
|
|
|
(if (not next-item)
|
|
|
|
(progn
|
|
|
|
(goto-char pos)
|
|
|
|
(error "Cannot move this item further down"))
|
2010-12-24 07:25:37 -05:00
|
|
|
(setq struct
|
|
|
|
(org-list-exchange-items actual-item next-item struct))
|
2010-12-23 18:54:48 -05:00
|
|
|
;; Use a short variation of `org-list-struct-fix-struct' as
|
|
|
|
;; there's no need to go through all the steps.
|
|
|
|
(let ((old-struct (mapcar (lambda (e) (copy-alist e)) struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(parents (org-list-struct-parent-alist struct)))
|
|
|
|
(org-list-struct-fix-bul struct prevs)
|
|
|
|
(org-list-struct-fix-ind struct parents)
|
|
|
|
(org-list-struct-apply-struct struct old-struct)
|
|
|
|
(goto-char (org-list-get-next-item (point-at-bol) struct prevs)))
|
|
|
|
(org-move-to-column col))))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2010-07-10 11:02:58 -04:00
|
|
|
(defun org-move-item-up ()
|
2008-09-23 02:03:13 -04:00
|
|
|
"Move the plain list item at point up, i.e. swap with previous item.
|
|
|
|
Subitems (items with larger indentation) are considered part of the item,
|
|
|
|
so this really moves item trees."
|
|
|
|
(interactive)
|
2010-12-23 18:53:30 -05:00
|
|
|
(unless (org-at-item-p) (error "Not at an item"))
|
|
|
|
(let* ((pos (point))
|
|
|
|
(col (current-column))
|
|
|
|
(actual-item (point-at-bol))
|
|
|
|
(struct (org-list-struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(prev-item (org-list-get-prev-item (point-at-bol) struct prevs)))
|
|
|
|
(if (not prev-item)
|
|
|
|
(progn
|
|
|
|
(goto-char pos)
|
|
|
|
(error "Cannot move this item further up"))
|
2010-12-24 07:25:37 -05:00
|
|
|
(setq struct
|
|
|
|
(org-list-exchange-items prev-item actual-item struct))
|
2010-12-23 18:54:48 -05:00
|
|
|
;; Use a short variation of `org-list-struct-fix-struct' as
|
|
|
|
;; there's no need to go through all the steps.
|
|
|
|
(let ((old-struct (mapcar (lambda (e) (copy-alist e)) struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(parents (org-list-struct-parent-alist struct)))
|
|
|
|
(org-list-struct-fix-bul struct prevs)
|
|
|
|
(org-list-struct-fix-ind struct parents)
|
|
|
|
(org-list-struct-apply-struct struct old-struct))
|
2010-12-23 18:53:30 -05:00
|
|
|
(org-move-to-column col))))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2010-07-10 11:02:58 -04:00
|
|
|
(defun org-insert-item (&optional checkbox)
|
|
|
|
"Insert a new item at the current level.
|
|
|
|
If cursor is before first character after bullet of the item, the
|
2010-08-21 07:32:33 -04:00
|
|
|
new item will be created before the current one.
|
|
|
|
|
|
|
|
If CHECKBOX is non-nil, add a checkbox next to the bullet.
|
|
|
|
|
|
|
|
Return t when things worked, nil when we are not in an item, or
|
|
|
|
item is invisible."
|
2010-12-24 07:25:37 -05:00
|
|
|
(let ((itemp (org-in-item-p)))
|
|
|
|
(unless (or (not itemp)
|
|
|
|
(save-excursion
|
|
|
|
(goto-char itemp)
|
|
|
|
(org-invisible-p)))
|
|
|
|
(if (save-excursion
|
|
|
|
(goto-char itemp)
|
|
|
|
(org-at-item-timer-p))
|
|
|
|
;; Timer list: delegate to `org-timer-item'.
|
|
|
|
(progn (org-timer-item) t)
|
|
|
|
;; if we're in a description list, ask for the new term.
|
|
|
|
(let ((desc-text (when (save-excursion
|
|
|
|
(and (goto-char itemp)
|
|
|
|
(org-at-item-description-p)))
|
|
|
|
(concat (read-string "Term: ") " :: "))))
|
|
|
|
;; Don't insert a checkbox if checkbox rule is applied and it
|
|
|
|
;; is a description item.
|
|
|
|
(org-list-insert-item-generic
|
|
|
|
(point) (and checkbox
|
|
|
|
(or (not desc-text)
|
|
|
|
(not (cdr (assq 'checkbox org-list-automatic-rules)))))
|
|
|
|
desc-text))))))
|
|
|
|
|
2010-07-10 11:02:58 -04:00
|
|
|
|
2010-07-30 18:45:34 -04:00
|
|
|
;;; Structures
|
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
(defun org-list-struct ()
|
|
|
|
"Return structure of list at point.
|
|
|
|
|
|
|
|
A list structure is an alist where keys is point at item, and
|
|
|
|
values are:
|
|
|
|
1. indentation,
|
|
|
|
2. bullet with trailing whitespace,
|
|
|
|
3. bullet counter, if any,
|
|
|
|
4. checkbox, if any,
|
2010-12-30 12:39:36 -05:00
|
|
|
5. position at item end,
|
|
|
|
6. description tag, if any.
|
2010-12-23 12:54:24 -05:00
|
|
|
|
2010-12-29 14:16:00 -05:00
|
|
|
Thus the following list, where numbers in parens are
|
|
|
|
point-at-bol:
|
|
|
|
|
|
|
|
- [X] first item (1)
|
|
|
|
1. sub-item 1 (18)
|
|
|
|
5. [@5] sub-item 2 (34)
|
|
|
|
some other text belonging to first item (55)
|
|
|
|
- last item (97)
|
2010-12-30 12:39:36 -05:00
|
|
|
+ tag :: description (109)
|
|
|
|
(131)
|
2010-12-29 14:16:00 -05:00
|
|
|
|
|
|
|
will get the following structure:
|
|
|
|
|
2010-12-30 12:39:36 -05:00
|
|
|
\(\(1 0 \"- \" nil [X] nil 97)
|
|
|
|
\(18 2 \"1. \" nil nil nil 34\)
|
|
|
|
\(34 2 \"5. \" \"5\" nil nil 55\)
|
|
|
|
\(97 0 \"- \" nil nil nil 131\)
|
|
|
|
\(109 2 \"+ \" nil nil \"tag\" 131\)
|
2010-12-29 14:16:00 -05:00
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
Assume point is at an item."
|
2010-07-30 18:45:34 -04:00
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
2010-12-23 12:54:24 -05:00
|
|
|
(let* ((case-fold-search t)
|
|
|
|
(context (org-list-context))
|
|
|
|
(lim-up (car context))
|
|
|
|
(lim-down (nth 1 context))
|
|
|
|
(text-min-ind 10000)
|
|
|
|
(drawers-re (concat "^[ \t]*:\\("
|
|
|
|
(mapconcat 'regexp-quote org-drawers "\\|")
|
|
|
|
"\\):[ \t]*$"))
|
|
|
|
(inlinetask-re (and (featurep 'org-inlinetask)
|
|
|
|
(org-inlinetask-outline-regexp)))
|
|
|
|
(beg-cell (cons (point) (org-get-indentation)))
|
|
|
|
ind itm-lst itm-lst-2 end-lst end-lst-2 struct
|
|
|
|
(assoc-at-point
|
|
|
|
;; Return an association whose key is point and values are
|
|
|
|
;; indentation, bullet string, bullet counter, and
|
|
|
|
;; checkbox.
|
|
|
|
(function
|
|
|
|
(lambda (ind)
|
|
|
|
(looking-at org-list-full-item-re)
|
|
|
|
(list (point)
|
|
|
|
ind
|
2010-12-30 12:39:36 -05:00
|
|
|
(match-string-no-properties 1) ; bullet
|
|
|
|
(match-string-no-properties 2) ; counter
|
|
|
|
(match-string-no-properties 3) ; checkbox
|
|
|
|
(match-string-no-properties 4))))) ; description tag
|
2010-12-23 12:54:24 -05:00
|
|
|
(end-before-blank
|
|
|
|
;; Ensure list ends at the first blank line.
|
|
|
|
(function
|
|
|
|
(lambda ()
|
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(min (1+ (point-at-eol)) lim-down)))))
|
|
|
|
;; 1. Read list from starting item to its beginning, and save
|
|
|
|
;; top item position and indentation in BEG-CELL. Also store
|
|
|
|
;; ending position of items in END-LST.
|
|
|
|
(save-excursion
|
|
|
|
(catch 'exit
|
|
|
|
(while t
|
2011-01-01 18:26:37 -05:00
|
|
|
(let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
|
|
|
|
(org-get-indentation))))
|
2010-07-30 18:45:34 -04:00
|
|
|
(cond
|
2010-12-23 12:54:24 -05:00
|
|
|
((<= (point) lim-up)
|
|
|
|
;; At upward limit: if we ended at an item, store it,
|
|
|
|
;; else dimiss useless data recorded above BEG-CELL.
|
|
|
|
;; Jump to part 2.
|
|
|
|
(throw 'exit
|
|
|
|
(setq itm-lst
|
|
|
|
(if (not (org-at-item-p))
|
|
|
|
(memq (assq (car beg-cell) itm-lst) itm-lst)
|
|
|
|
(setq beg-cell (cons (point) ind))
|
|
|
|
(cons (funcall assoc-at-point ind) itm-lst)))))
|
|
|
|
((and (not (eq org-list-ending-method 'indent))
|
|
|
|
(looking-at (org-list-end-re)))
|
|
|
|
;; Looking at a list ending regexp. Dismiss useless
|
|
|
|
;; data recorded above BEG-CELL. Jump to part 2.
|
|
|
|
(throw 'exit
|
|
|
|
(setq itm-lst
|
|
|
|
(memq (assq (car beg-cell) itm-lst) itm-lst))))
|
|
|
|
;; Skip blocks, drawers, inline tasks, blank lines
|
|
|
|
;; along the way.
|
|
|
|
((looking-at "^[ \t]*#\\+end_")
|
|
|
|
(re-search-backward "^[ \t]*#\\+begin_" nil t))
|
|
|
|
((looking-at "^[ \t]*:END:")
|
|
|
|
(re-search-backward drawers-re nil t)
|
|
|
|
(beginning-of-line))
|
|
|
|
((and inlinetask-re (looking-at inlinetask-re))
|
|
|
|
(org-inlinetask-goto-beginning)
|
|
|
|
(forward-line -1))
|
|
|
|
((looking-at "^[ \t]*$")
|
|
|
|
(forward-line -1))
|
|
|
|
((org-at-item-p)
|
|
|
|
;; Point is at an item. Add data to ITM-LST. It may
|
|
|
|
;; also end a previous item: save it in END-LST. If
|
|
|
|
;; ind is less or equal than BEG-CELL and there is no
|
|
|
|
;; end at this ind or lesser, this item becomes the
|
|
|
|
;; new BEG-CELL.
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (funcall assoc-at-point ind) itm-lst)
|
|
|
|
(push (cons ind (point-at-bol)) end-lst)
|
2010-12-23 12:54:24 -05:00
|
|
|
(when (or (and (eq org-list-ending-method 'regexp)
|
|
|
|
(<= ind (cdr beg-cell)))
|
|
|
|
(< ind text-min-ind))
|
|
|
|
(setq beg-cell (cons (point-at-bol) ind)))
|
|
|
|
(forward-line -1))
|
|
|
|
(t
|
|
|
|
;; Point is not at an item. Unless ending method is
|
|
|
|
;; `regexp', interpret line's indentation:
|
|
|
|
;;
|
|
|
|
;; - text at column 0 is necessarily out of any list.
|
|
|
|
;; Dismiss data recorded above BEG-CELL. Jump to
|
|
|
|
;; part 2.
|
|
|
|
;;
|
|
|
|
;; - any other case, it can possibly be an ending
|
|
|
|
;; position for an item above. Save it and proceed.
|
|
|
|
(cond
|
|
|
|
((eq org-list-ending-method 'regexp))
|
|
|
|
((= ind 0)
|
|
|
|
(throw 'exit
|
|
|
|
(setq itm-lst
|
|
|
|
(memq (assq (car beg-cell) itm-lst) itm-lst))))
|
|
|
|
(t
|
|
|
|
(when (< ind text-min-ind) (setq text-min-ind ind))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (cons ind (point-at-bol)) end-lst)))
|
2010-12-23 12:54:24 -05:00
|
|
|
(forward-line -1)))))))
|
|
|
|
;; 2. Read list from starting point to its end, that is until we
|
|
|
|
;; get out of context, or a non-item line is less or equally
|
|
|
|
;; indented that BEG-CELL's cdr. Also store ending position
|
|
|
|
;; of items in END-LST-2.
|
|
|
|
(catch 'exit
|
|
|
|
(while t
|
2011-01-01 18:26:37 -05:00
|
|
|
(let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
|
|
|
|
(org-get-indentation))))
|
2010-12-23 12:54:24 -05:00
|
|
|
(cond
|
|
|
|
((>= (point) lim-down)
|
|
|
|
;; At downward limit: this is de facto the end of the
|
|
|
|
;; list. Save point as an ending position, and jump to
|
|
|
|
;; part 3.
|
|
|
|
(throw 'exit
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (cons 0 (funcall end-before-blank)) end-lst-2)))
|
2011-01-09 09:26:34 -05:00
|
|
|
((and (not (eq org-list-ending-method 'indent))
|
2010-12-23 12:54:24 -05:00
|
|
|
(looking-at (org-list-end-re)))
|
|
|
|
;; Looking at a list ending regexp. Save point as an
|
|
|
|
;; ending position and jump to part 3.
|
2011-01-09 09:26:34 -05:00
|
|
|
(throw 'exit (push (cons 0 (point-at-bol)) end-lst-2)))
|
2010-12-23 12:54:24 -05:00
|
|
|
;; Skip blocks, drawers, inline tasks and blank lines
|
|
|
|
;; along the way
|
|
|
|
((looking-at "^[ \t]*#\\+begin_")
|
|
|
|
(re-search-forward "^[ \t]*#\\+end_")
|
|
|
|
(forward-line 1))
|
|
|
|
((looking-at drawers-re)
|
|
|
|
(re-search-forward "^[ \t]*:END:" nil t)
|
|
|
|
(forward-line 1))
|
|
|
|
((and inlinetask-re (looking-at inlinetask-re))
|
|
|
|
(org-inlinetask-goto-end)
|
|
|
|
(forward-line 1))
|
|
|
|
((looking-at "^[ \t]*$")
|
|
|
|
(forward-line 1))
|
|
|
|
((org-at-item-p)
|
|
|
|
;; Point is at an item. Add data to ITM-LST-2. It may also
|
|
|
|
;; end a previous item, so save it in END-LST-2.
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (funcall assoc-at-point ind) itm-lst-2)
|
|
|
|
(push (cons ind (point-at-bol)) end-lst-2)
|
2010-12-23 12:54:24 -05:00
|
|
|
(forward-line 1))
|
|
|
|
(t
|
|
|
|
;; Point is not at an item. If ending method is not
|
|
|
|
;; `regexp', two situations are of interest:
|
|
|
|
;;
|
|
|
|
;; - ind is lesser or equal than BEG-CELL's. The list is
|
|
|
|
;; over. Store point as an ending position and jump to
|
|
|
|
;; part 3.
|
|
|
|
;;
|
|
|
|
;; - ind is lesser or equal than previous item's. This
|
|
|
|
;; is an ending position. Store it and proceed.
|
|
|
|
(cond
|
|
|
|
((eq org-list-ending-method 'regexp))
|
|
|
|
((<= ind (cdr beg-cell))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (cons ind (funcall end-before-blank)) end-lst-2)
|
2010-12-23 12:54:24 -05:00
|
|
|
(throw 'exit nil))
|
|
|
|
((<= ind (nth 1 (car itm-lst-2)))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (cons ind (point-at-bol)) end-lst-2)))
|
2010-12-23 12:54:24 -05:00
|
|
|
(forward-line 1))))))
|
|
|
|
(setq struct (append itm-lst (cdr (nreverse itm-lst-2))))
|
|
|
|
(setq end-lst (append end-lst (cdr (nreverse end-lst-2))))
|
|
|
|
;; 3. Correct ill-formed lists by making sure top item has the
|
|
|
|
;; least indentation of the list
|
|
|
|
(let ((min-ind (nth 1 (car struct))))
|
|
|
|
(mapc (lambda (item)
|
|
|
|
(let ((ind (nth 1 item)))
|
|
|
|
(when (< ind min-ind) (setcar (cdr item) min-ind))))
|
|
|
|
struct))
|
|
|
|
;; 4. Associate each item to its end pos.
|
|
|
|
(org-list-struct-assoc-end struct end-lst)
|
|
|
|
;; 5. Return STRUCT
|
|
|
|
struct)))
|
|
|
|
|
|
|
|
(defun org-list-struct-assoc-end (struct end-list)
|
|
|
|
"Associate proper ending point to items in STRUCT.
|
|
|
|
|
|
|
|
END-LIST is a pseudo-alist where car is indentation and cdr is
|
|
|
|
ending position.
|
|
|
|
|
|
|
|
This function modifies STRUCT."
|
|
|
|
(let ((endings end-list))
|
|
|
|
(mapc
|
|
|
|
(lambda (elt)
|
|
|
|
(let ((pos (car elt))
|
|
|
|
(ind (nth 1 elt)))
|
|
|
|
;; remove end candidates behind current item
|
|
|
|
(while (or (<= (cdar endings) pos))
|
|
|
|
(pop endings))
|
|
|
|
;; add end position to item assoc
|
2010-12-30 12:39:36 -05:00
|
|
|
(let ((old-end (nthcdr 6 elt))
|
2010-12-23 12:54:24 -05:00
|
|
|
(new-end (assoc-default ind endings '<=)))
|
|
|
|
(if old-end
|
|
|
|
(setcar old-end new-end)
|
|
|
|
(setcdr elt (append (cdr elt) (list new-end)))))))
|
|
|
|
struct)))
|
|
|
|
|
|
|
|
(defun org-list-struct-prev-alist (struct)
|
|
|
|
"Return alist between item and previous item in STRUCT."
|
2010-12-30 12:39:36 -05:00
|
|
|
(let ((item-end-alist (mapcar (lambda (e) (cons (car e) (nth 6 e)))
|
2010-12-23 12:54:24 -05:00
|
|
|
struct)))
|
|
|
|
(mapcar (lambda (e)
|
|
|
|
(let ((prev (car (rassq (car e) item-end-alist))))
|
|
|
|
(cons (car e) prev)))
|
|
|
|
struct)))
|
|
|
|
|
|
|
|
(defun org-list-struct-parent-alist (struct)
|
|
|
|
"Return alist between item and parent in STRUCT."
|
|
|
|
(let ((ind-to-ori (list (list (nth 1 (car struct)))))
|
|
|
|
(prev-pos (list (caar struct))))
|
|
|
|
(cons prev-pos
|
|
|
|
(mapcar (lambda (item)
|
|
|
|
(let ((pos (car item))
|
|
|
|
(ind (nth 1 item))
|
|
|
|
(prev-ind (caar ind-to-ori)))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push pos prev-pos)
|
2010-12-23 12:54:24 -05:00
|
|
|
(cond
|
|
|
|
((> prev-ind ind)
|
|
|
|
(setq ind-to-ori
|
|
|
|
(member (assq ind ind-to-ori) ind-to-ori))
|
|
|
|
(cons pos (cdar ind-to-ori)))
|
|
|
|
((< prev-ind ind)
|
|
|
|
(let ((origin (nth 1 prev-pos)))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (cons ind origin) ind-to-ori)
|
2010-12-23 12:54:24 -05:00
|
|
|
(cons pos origin)))
|
|
|
|
(t (cons pos (cdar ind-to-ori))))))
|
|
|
|
(cdr struct)))))
|
|
|
|
|
2010-12-28 19:43:20 -05:00
|
|
|
(defun org-list-get-parent (item struct parents)
|
2010-12-23 12:54:24 -05:00
|
|
|
"Return parent of ITEM in STRUCT, or nil.
|
2010-12-28 19:43:20 -05:00
|
|
|
PARENTS is the alist of items' parent. See
|
2010-12-23 12:54:24 -05:00
|
|
|
`org-list-struct-parent-alist'."
|
|
|
|
(let ((parents (or parents (org-list-struct-parent-alist struct))))
|
|
|
|
(cdr (assq item parents))))
|
|
|
|
|
2010-12-28 19:43:20 -05:00
|
|
|
(defun org-list-has-child-p (item struct)
|
|
|
|
"Return a non-nil value if ITEM in STRUCT has a child.
|
2010-12-29 14:16:00 -05:00
|
|
|
Value returned is the position of the first child of ITEM."
|
2010-12-23 12:54:24 -05:00
|
|
|
(let ((ind (org-list-get-ind item struct))
|
|
|
|
(child-maybe (car (nth 1 (member (assq item struct) struct)))))
|
2010-12-23 18:53:30 -05:00
|
|
|
(when (and child-maybe
|
|
|
|
(< ind (org-list-get-ind child-maybe struct)))
|
|
|
|
child-maybe)))
|
2010-12-23 12:54:24 -05:00
|
|
|
|
|
|
|
(defun org-list-get-next-item (item struct prevs)
|
|
|
|
"Return next item in same sub-list as ITEM in STRUCT, or nil.
|
|
|
|
PREVS is the alist of previous items. See
|
|
|
|
`org-list-struct-prev-alist'."
|
|
|
|
(car (rassq item prevs)))
|
|
|
|
|
|
|
|
(defun org-list-get-prev-item (item struct prevs)
|
|
|
|
"Return previous item in same sub-list as ITEM in STRUCT, or nil.
|
|
|
|
PREVS is the alist of previous items. See
|
|
|
|
`org-list-struct-prev-alist'."
|
|
|
|
(cdr (assq item prevs)))
|
|
|
|
|
|
|
|
(defun org-list-get-subtree (item struct)
|
2010-12-19 16:03:05 -05:00
|
|
|
"Return all items with ITEM as a common ancestor or nil.
|
2010-12-29 14:16:00 -05:00
|
|
|
STRUCT is the list structure considered."
|
2010-12-23 12:54:24 -05:00
|
|
|
(let* ((item-end (org-list-get-item-end item struct))
|
|
|
|
(sub-struct (cdr (member (assq item struct) struct)))
|
|
|
|
subtree)
|
|
|
|
(catch 'exit
|
2011-01-01 12:27:31 -05:00
|
|
|
(mapc (lambda (e)
|
|
|
|
(let ((pos (car e)))
|
|
|
|
(if (< pos item-end) (push pos subtree) (throw 'exit nil))))
|
2010-12-23 12:54:24 -05:00
|
|
|
sub-struct))
|
|
|
|
(nreverse subtree)))
|
|
|
|
|
|
|
|
(defun org-list-get-all-items (item struct prevs)
|
|
|
|
"List of items in the same sub-list as ITEM in STRUCT.
|
|
|
|
PREVS, when provided, is the alist of previous items. See
|
|
|
|
`org-list-struct-prev-alist'."
|
|
|
|
(let ((prev-item item)
|
|
|
|
(next-item item)
|
|
|
|
before-item after-item)
|
|
|
|
(while (setq prev-item (org-list-get-prev-item prev-item struct prevs))
|
2010-12-28 19:43:20 -05:00
|
|
|
(push prev-item before-item))
|
2010-12-23 12:54:24 -05:00
|
|
|
(while (setq next-item (org-list-get-next-item next-item struct prevs))
|
2010-12-28 19:43:20 -05:00
|
|
|
(push next-item after-item))
|
2010-12-23 12:54:24 -05:00
|
|
|
(append before-item (list item) (nreverse after-item))))
|
|
|
|
|
2010-12-28 19:43:20 -05:00
|
|
|
(defun org-list-get-children (item struct parents)
|
2010-12-23 12:54:24 -05:00
|
|
|
"List all children of ITEM in STRUCT, or nil.
|
2010-12-28 19:43:20 -05:00
|
|
|
PARENTS is the alist of items' parent. See
|
|
|
|
`org-list-struct-parent-alist'."
|
|
|
|
(let (all)
|
|
|
|
(while (setq child (car (rassq item parents)))
|
2011-01-01 12:27:31 -05:00
|
|
|
(setq parents (cdr (member (assq child parents) parents)))
|
|
|
|
(push child all))
|
2010-12-28 19:43:20 -05:00
|
|
|
(nreverse all)))
|
2010-12-23 12:54:24 -05:00
|
|
|
|
|
|
|
(defun org-list-get-top-point (struct)
|
|
|
|
"Return point at beginning of list.
|
|
|
|
STRUCT is the structure of the list."
|
|
|
|
(caar struct))
|
|
|
|
|
|
|
|
(defun org-list-get-bottom-point (struct)
|
|
|
|
"Return point at bottom of list.
|
|
|
|
STRUCT is the structure of the list."
|
|
|
|
(apply 'max
|
|
|
|
(mapcar (lambda (e) (org-list-get-item-end (car e) struct)) struct)))
|
|
|
|
|
|
|
|
(defun org-list-get-list-begin (item struct prevs)
|
|
|
|
"Return point at beginning of sub-list ITEM belongs.
|
|
|
|
STRUCT is the structure of the list. PREVS is the alist of
|
|
|
|
previous items. See `org-list-struct-prev-alist'."
|
2010-12-28 19:43:20 -05:00
|
|
|
(let ((first-item item) prev-item)
|
|
|
|
(while (setq prev-item (org-list-get-prev-item first-item struct prevs))
|
2010-12-23 12:54:24 -05:00
|
|
|
(setq first-item prev-item))
|
|
|
|
first-item))
|
|
|
|
|
2010-12-30 12:41:19 -05:00
|
|
|
(defalias 'org-list-get-first-item 'org-list-get-list-begin)
|
|
|
|
|
|
|
|
(defun org-list-get-last-item (item struct prevs)
|
|
|
|
"Return point at last item of sub-list ITEM belongs.
|
2010-12-23 12:54:24 -05:00
|
|
|
STRUCT is the structure of the list. PREVS is the alist of
|
|
|
|
previous items. See `org-list-struct-prev-alist'."
|
2010-12-28 19:43:20 -05:00
|
|
|
(let ((last-item item) next-item)
|
|
|
|
(while (setq next-item (org-list-get-next-item last-item struct prevs))
|
2010-12-23 12:54:24 -05:00
|
|
|
(setq last-item next-item))
|
2010-12-30 12:41:19 -05:00
|
|
|
last-item))
|
|
|
|
|
|
|
|
(defun org-list-get-list-end (item struct prevs)
|
|
|
|
"Return point at end of sub-list ITEM belongs.
|
|
|
|
STRUCT is the structure of the list. PREVS is the alist of
|
|
|
|
previous items. See `org-list-struct-prev-alist'."
|
2011-01-02 13:07:58 -05:00
|
|
|
(org-list-get-item-end (org-list-get-last-item item struct prevs) struct))
|
2010-12-19 16:03:05 -05:00
|
|
|
|
|
|
|
(defun org-list-get-nth (n key struct)
|
|
|
|
"Return the Nth value of KEY in STRUCT."
|
|
|
|
(nth n (assq key struct)))
|
|
|
|
|
|
|
|
(defun org-list-set-nth (n key struct new)
|
|
|
|
"Set the Nth value of KEY in STRUCT to NEW.
|
|
|
|
\nThis function modifies STRUCT."
|
|
|
|
(setcar (nthcdr n (assq key struct)) new))
|
|
|
|
|
|
|
|
(defun org-list-get-ind (item struct)
|
|
|
|
"Return indentation of ITEM in STRUCT."
|
|
|
|
(org-list-get-nth 1 item struct))
|
|
|
|
|
|
|
|
(defun org-list-set-ind (item struct ind)
|
|
|
|
"Set indentation of ITEM in STRUCT to IND.
|
|
|
|
\nThis function modifies STRUCT."
|
|
|
|
(org-list-set-nth 1 item struct ind))
|
|
|
|
|
|
|
|
(defun org-list-get-bullet (item struct)
|
|
|
|
"Return bullet of ITEM in STRUCT."
|
|
|
|
(org-list-get-nth 2 item struct))
|
|
|
|
|
|
|
|
(defun org-list-set-bullet (item struct bullet)
|
|
|
|
"Set bullet of ITEM in STRUCT to BULLET.
|
|
|
|
\nThis function modifies STRUCT."
|
|
|
|
(org-list-set-nth 2 item struct bullet))
|
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
(defun org-list-get-counter (item struct)
|
|
|
|
"Return counter of ITEM in STRUCT."
|
|
|
|
(org-list-get-nth 3 item struct))
|
|
|
|
|
2010-12-19 16:03:05 -05:00
|
|
|
(defun org-list-get-checkbox (item struct)
|
|
|
|
"Return checkbox of ITEM in STRUCT or nil."
|
|
|
|
(org-list-get-nth 4 item struct))
|
|
|
|
|
|
|
|
(defun org-list-set-checkbox (item struct checkbox)
|
|
|
|
"Set checkbox of ITEM in STRUCT to CHECKBOX.
|
|
|
|
\nThis function modifies STRUCT."
|
|
|
|
(org-list-set-nth 4 item struct checkbox))
|
2010-07-30 18:45:34 -04:00
|
|
|
|
2010-12-30 12:39:36 -05:00
|
|
|
(defun org-list-get-tag (item struct)
|
2010-12-23 12:54:24 -05:00
|
|
|
"Return end position of ITEM in STRUCT."
|
|
|
|
(org-list-get-nth 5 item struct))
|
2010-08-21 07:32:33 -04:00
|
|
|
|
2010-12-30 12:39:36 -05:00
|
|
|
(defun org-list-get-item-end (item struct)
|
|
|
|
"Return end position of ITEM in STRUCT."
|
|
|
|
(org-list-get-nth 6 item struct))
|
|
|
|
|
2010-12-23 18:54:48 -05:00
|
|
|
(defun org-list-get-item-end-before-blank (item struct)
|
2010-12-29 14:16:00 -05:00
|
|
|
"Return point at end of ITEM in STRUCT, before any blank line.
|
2010-12-23 18:54:48 -05:00
|
|
|
Point returned is at end of line."
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (org-list-get-item-end item struct))
|
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(point-at-eol)))
|
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
(defun org-list-struct-fix-bul (struct prevs)
|
|
|
|
"Verify and correct bullets for every association in STRUCT.
|
2010-12-29 14:16:00 -05:00
|
|
|
PREVS is the alist of previous items. See
|
|
|
|
`org-list-struct-prev-alist'.
|
|
|
|
|
|
|
|
This function modifies STRUCT."
|
2010-12-23 12:54:24 -05:00
|
|
|
(let ((fix-bul
|
|
|
|
(function
|
2010-12-19 16:04:12 -05:00
|
|
|
(lambda (item)
|
2010-12-23 12:54:24 -05:00
|
|
|
(let* ((prev (org-list-get-prev-item item struct prevs))
|
|
|
|
(prev-bul (and prev (org-list-get-bullet prev struct)))
|
|
|
|
(counter (org-list-get-counter item struct))
|
|
|
|
(bullet (org-list-get-bullet item struct)))
|
|
|
|
(org-list-set-bullet
|
|
|
|
item struct
|
|
|
|
(org-list-bullet-string
|
|
|
|
(cond
|
|
|
|
((and prev (string-match "[0-9]+" prev-bul) counter)
|
|
|
|
(replace-match counter nil nil prev-bul))
|
|
|
|
(prev
|
|
|
|
(org-list-inc-bullet-maybe (org-list-get-bullet prev struct)))
|
|
|
|
((and (string-match "[0-9]+" bullet) counter)
|
|
|
|
(replace-match counter nil nil bullet))
|
|
|
|
((string-match "[0-9]+" bullet)
|
|
|
|
(replace-match "1" nil nil bullet))
|
|
|
|
(t bullet)))))))))
|
|
|
|
(mapc fix-bul (mapcar 'car struct))))
|
|
|
|
|
|
|
|
(defun org-list-struct-fix-ind (struct parents &optional bullet-size)
|
2010-07-30 18:45:34 -04:00
|
|
|
"Verify and correct indentation for every association in STRUCT.
|
2010-12-23 12:54:24 -05:00
|
|
|
|
2010-12-29 14:16:00 -05:00
|
|
|
PARENTS is the alist of items' parents. See
|
|
|
|
`org-list-struct-parent-alist'.
|
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
If numeric optional argument BULLET-SIZE is set, assume all
|
|
|
|
bullets in list have this length to determine new indentation.
|
2010-08-21 07:32:33 -04:00
|
|
|
|
2010-07-30 18:45:34 -04:00
|
|
|
This function modifies STRUCT."
|
2010-12-23 12:54:24 -05:00
|
|
|
(let* ((ancestor (org-list-get-top-point struct))
|
|
|
|
(top-ind (org-list-get-ind ancestor struct))
|
2010-07-30 18:45:34 -04:00
|
|
|
(new-ind
|
|
|
|
(lambda (item)
|
2010-12-23 12:54:24 -05:00
|
|
|
(let ((parent (org-list-get-parent item struct parents)))
|
2010-07-30 18:45:34 -04:00
|
|
|
(if parent
|
|
|
|
;; Indent like parent + length of parent's bullet
|
2010-12-23 12:54:24 -05:00
|
|
|
(org-list-set-ind
|
|
|
|
item struct (+ (or bullet-size
|
|
|
|
(length
|
|
|
|
(org-list-get-bullet parent struct)))
|
|
|
|
(org-list-get-ind parent struct)))
|
2010-07-30 18:45:34 -04:00
|
|
|
;; If no parent, indent like top-point
|
2010-12-19 16:03:05 -05:00
|
|
|
(org-list-set-ind item struct top-ind))))))
|
|
|
|
(mapc new-ind (mapcar 'car (cdr struct)))))
|
2010-07-30 18:45:34 -04:00
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
(defun org-list-struct-fix-box (struct parents prevs &optional ordered)
|
2010-12-19 16:04:12 -05:00
|
|
|
"Verify and correct checkboxes for every association in STRUCT.
|
|
|
|
|
2010-12-29 14:16:00 -05:00
|
|
|
PARENTS is the alist of items' parents. See
|
|
|
|
`org-list-struct-parent-alist'. PREVS is the alist of previous
|
|
|
|
items. See `org-list-struct-prev-alist.
|
|
|
|
|
2010-12-19 16:04:12 -05:00
|
|
|
If ORDERED is non-nil, a checkbox can only be checked when every
|
|
|
|
checkbox before it is checked too. If there was an attempt to
|
|
|
|
break this rule, the function will return the blocking item. In
|
2010-12-29 14:16:00 -05:00
|
|
|
all others cases, the return value will be nil.
|
2010-12-19 16:04:12 -05:00
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
This function modifies STRUCT."
|
|
|
|
(let ((all-items (mapcar 'car struct))
|
2010-12-19 16:04:12 -05:00
|
|
|
(set-parent-box
|
|
|
|
(function
|
|
|
|
(lambda (item)
|
2010-12-23 12:54:24 -05:00
|
|
|
(let* ((box-list
|
|
|
|
(mapcar (lambda (child)
|
|
|
|
(org-list-get-checkbox child struct))
|
2010-12-28 19:43:20 -05:00
|
|
|
(org-list-get-children item struct parents))))
|
2010-12-19 16:04:12 -05:00
|
|
|
(org-list-set-checkbox
|
|
|
|
item struct
|
|
|
|
(cond
|
|
|
|
((and (member "[ ]" box-list) (member "[X]" box-list)) "[-]")
|
|
|
|
((member "[-]" box-list) "[-]")
|
|
|
|
((member "[X]" box-list) "[X]")
|
|
|
|
((member "[ ]" box-list) "[ ]")
|
|
|
|
;; parent has no boxed child: leave box as-is
|
|
|
|
(t (org-list-get-checkbox item struct))))))))
|
|
|
|
parent-list)
|
2010-12-23 12:54:24 -05:00
|
|
|
;; 1. List all parents with a checkbox
|
2010-12-19 16:04:12 -05:00
|
|
|
(mapc
|
2010-12-23 12:54:24 -05:00
|
|
|
(lambda (e)
|
|
|
|
(let* ((parent (org-list-get-parent e struct parents))
|
2010-12-19 16:04:12 -05:00
|
|
|
(parent-box-p (org-list-get-checkbox parent struct)))
|
|
|
|
(when (and parent-box-p (not (memq parent parent-list)))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push parent parent-list))))
|
2010-12-23 12:54:24 -05:00
|
|
|
all-items)
|
|
|
|
;; 2. Sort those parents by decreasing indentation
|
2010-12-19 16:04:12 -05:00
|
|
|
(setq parent-list (sort parent-list
|
|
|
|
(lambda (e1 e2)
|
|
|
|
(> (org-list-get-ind e1 struct)
|
|
|
|
(org-list-get-ind e2 struct)))))
|
2010-12-23 12:54:24 -05:00
|
|
|
;; 3. For each parent, get all children's checkboxes to determine
|
|
|
|
;; and set its checkbox accordingly
|
2010-12-19 16:04:12 -05:00
|
|
|
(mapc set-parent-box parent-list)
|
2010-12-23 12:54:24 -05:00
|
|
|
;; 4. If ORDERED is set, see if we need to uncheck some boxes
|
2010-12-19 16:04:12 -05:00
|
|
|
(when ordered
|
2010-12-23 12:54:24 -05:00
|
|
|
(let* ((box-list
|
2010-12-19 16:04:12 -05:00
|
|
|
(mapcar (lambda (e) (org-list-get-checkbox e struct)) all-items))
|
|
|
|
(after-unchecked (member "[ ]" box-list)))
|
|
|
|
;; there are boxes checked after an unchecked one: fix that
|
|
|
|
(when (member "[X]" after-unchecked)
|
|
|
|
(let ((index (- (length struct) (length after-unchecked))))
|
|
|
|
(mapc (lambda (e) (org-list-set-checkbox e struct "[ ]"))
|
|
|
|
(nthcdr index all-items))
|
|
|
|
;; Verify once again the structure, without ORDERED
|
2010-12-23 18:53:30 -05:00
|
|
|
(org-list-struct-fix-box struct parents prevs nil)
|
2010-12-19 16:04:12 -05:00
|
|
|
;; return blocking item
|
|
|
|
(nth index all-items)))))))
|
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
(defun org-list-struct-fix-struct (struct parents)
|
2010-12-29 14:16:00 -05:00
|
|
|
"Return STRUCT with correct bullets and indentation.
|
|
|
|
PARENTS is the alist of items' parents. See
|
|
|
|
`org-list-struct-parent-alist'."
|
2010-12-23 12:54:24 -05:00
|
|
|
;; Order of functions matters here: checkboxes and endings need
|
|
|
|
;; correct indentation to be set, and indentation needs correct
|
|
|
|
;; bullets.
|
|
|
|
;;
|
|
|
|
;; 0. Save a copy of structure before modifications
|
|
|
|
(let ((old-struct (mapcar (lambda (e) (copy-alist e)) struct)))
|
|
|
|
;; 1. Set a temporary, but coherent with PARENTS, indentation in
|
|
|
|
;; order to get items endings and bullets properly
|
|
|
|
(org-list-struct-fix-ind struct parents 2)
|
|
|
|
;; 2. Get pseudo-alist of ending positions and sort it by position.
|
|
|
|
;; Then associate them to the structure.
|
|
|
|
(let (end-list acc-end)
|
|
|
|
(mapc (lambda (e)
|
|
|
|
(let* ((pos (car e))
|
|
|
|
(ind-pos (org-list-get-ind pos struct))
|
|
|
|
(end-pos (org-list-get-item-end pos struct)))
|
|
|
|
(unless (assq end-pos struct)
|
|
|
|
;; to determine real ind of an ending position that is
|
|
|
|
;; not at an item, we have to find the item it belongs
|
|
|
|
;; to: it is the last item (ITEM-UP), whose ending is
|
|
|
|
;; further than the position we're interested in.
|
|
|
|
(let ((item-up (assoc-default end-pos acc-end '>)))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (cons
|
|
|
|
;; else part is for the bottom point
|
|
|
|
(if item-up (+ (org-list-get-ind item-up struct) 2) 0)
|
|
|
|
end-pos)
|
|
|
|
end-list)))
|
|
|
|
(push (cons ind-pos pos) end-list)
|
|
|
|
(push (cons end-pos pos) acc-end)))
|
2010-12-23 12:54:24 -05:00
|
|
|
struct)
|
|
|
|
(setq end-list (sort end-list (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
|
|
|
|
(org-list-struct-assoc-end struct end-list))
|
|
|
|
;; 3. Get bullets right
|
|
|
|
(let ((prevs (org-list-struct-prev-alist struct)))
|
|
|
|
(org-list-struct-fix-bul struct prevs)
|
|
|
|
;; 4. Now get real indentation
|
|
|
|
(org-list-struct-fix-ind struct parents)
|
|
|
|
;; 5. Eventually fix checkboxes
|
|
|
|
(org-list-struct-fix-box struct parents prevs))
|
|
|
|
;; 6. Apply structure modifications to buffer
|
|
|
|
(org-list-struct-apply-struct struct old-struct)))
|
|
|
|
|
|
|
|
(defun org-list-struct-outdent (start end struct parents)
|
2010-12-29 14:16:00 -05:00
|
|
|
"Outdent items between START and END in structure STRUCT.
|
|
|
|
|
|
|
|
PARENTS is the alist of items' parents. See
|
|
|
|
`org-list-struct-parent-alist'.
|
|
|
|
|
|
|
|
START is included, END excluded."
|
2010-08-17 16:21:24 -04:00
|
|
|
(let* (acc
|
|
|
|
(out (lambda (cell)
|
|
|
|
(let* ((item (car cell))
|
|
|
|
(parent (cdr cell)))
|
|
|
|
(cond
|
|
|
|
;; Item not yet in zone: keep association
|
|
|
|
((< item start) cell)
|
|
|
|
;; Item out of zone: follow associations in acc
|
|
|
|
((>= item end)
|
2010-12-23 12:54:24 -05:00
|
|
|
(let ((convert (and parent (assq parent acc))))
|
2010-08-17 16:21:24 -04:00
|
|
|
(if convert (cons item (cdr convert)) cell)))
|
|
|
|
;; Item has no parent: error
|
2010-12-23 12:54:24 -05:00
|
|
|
((not parent)
|
2010-08-17 16:21:24 -04:00
|
|
|
(error "Cannot outdent top-level items"))
|
|
|
|
;; Parent is outdented: keep association
|
|
|
|
((>= parent start)
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (cons parent item) acc) cell)
|
2010-08-17 16:21:24 -04:00
|
|
|
(t
|
|
|
|
;; Parent isn't outdented: reparent to grand-parent
|
2010-12-23 12:54:24 -05:00
|
|
|
(let ((grand-parent (org-list-get-parent
|
|
|
|
parent struct parents)))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (cons parent item) acc)
|
2010-08-17 16:21:24 -04:00
|
|
|
(cons item grand-parent))))))))
|
2010-12-23 18:53:30 -05:00
|
|
|
(mapcar out parents)))
|
2010-07-30 18:45:34 -04:00
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
(defun org-list-struct-indent (start end struct parents prevs)
|
2010-12-29 14:16:00 -05:00
|
|
|
"Indent items between START and END in structure STRUCT.
|
2010-08-21 07:32:33 -04:00
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
PARENTS is the alist of parents. See
|
|
|
|
`org-list-struct-parent-alist'. PREVS is the alist of previous
|
|
|
|
items. See `org-list-struct-prev-alist'.
|
2010-08-11 07:08:48 -04:00
|
|
|
|
2010-12-29 14:16:00 -05:00
|
|
|
START is included and END excluded.
|
|
|
|
|
|
|
|
STRUCT may be modified if `org-list-demote-modify-bullet' matches
|
|
|
|
bullets between START and END."
|
2010-08-17 16:21:24 -04:00
|
|
|
(let* (acc
|
2011-01-01 12:27:31 -05:00
|
|
|
(set-assoc (lambda (cell) (push cell acc) cell))
|
2010-08-11 07:08:48 -04:00
|
|
|
(change-bullet-maybe
|
2010-12-23 12:54:24 -05:00
|
|
|
(function
|
|
|
|
(lambda (item)
|
|
|
|
(let* ((bul (org-trim (org-list-get-bullet item struct)))
|
|
|
|
(new-bul-p (cdr (assoc bul org-list-demote-modify-bullet))))
|
|
|
|
(when new-bul-p (org-list-set-bullet item struct new-bul-p))))))
|
2010-07-30 18:45:34 -04:00
|
|
|
(ind
|
|
|
|
(lambda (cell)
|
|
|
|
(let* ((item (car cell))
|
|
|
|
(parent (cdr cell)))
|
|
|
|
(cond
|
|
|
|
;; Item not yet in zone: keep association
|
|
|
|
((< item start) cell)
|
|
|
|
((>= item end)
|
|
|
|
;; Item out of zone: follow associations in acc
|
|
|
|
(let ((convert (assq parent acc)))
|
|
|
|
(if convert (cons item (cdr convert)) cell)))
|
|
|
|
(t
|
|
|
|
;; Item is in zone...
|
2010-12-23 12:54:24 -05:00
|
|
|
(let ((prev (org-list-get-prev-item item struct prevs)))
|
2010-08-11 07:08:48 -04:00
|
|
|
;; Check if bullet needs to be changed
|
|
|
|
(funcall change-bullet-maybe item)
|
2010-07-30 18:45:34 -04:00
|
|
|
(cond
|
|
|
|
;; First item indented but not parent: error
|
2010-12-23 12:54:24 -05:00
|
|
|
((and (not prev) (< parent start))
|
2010-07-30 18:45:34 -04:00
|
|
|
(error "Cannot indent the first item of a list"))
|
|
|
|
;; First item and parent indented: keep same parent
|
2010-12-23 12:54:24 -05:00
|
|
|
((not prev) (funcall set-assoc cell))
|
2010-07-30 18:45:34 -04:00
|
|
|
;; Previous item not indented: reparent to it
|
2010-12-23 12:54:24 -05:00
|
|
|
((< prev start) (funcall set-assoc (cons item prev)))
|
2010-07-30 18:45:34 -04:00
|
|
|
;; Previous item indented: reparent like it
|
|
|
|
(t
|
2011-01-01 12:27:31 -05:00
|
|
|
(funcall set-assoc
|
|
|
|
(cons item (cdr (assq prev acc)))))))))))))
|
2010-12-23 12:54:24 -05:00
|
|
|
(mapcar ind parents)))
|
2010-07-30 18:45:34 -04:00
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
(defun org-list-struct-apply-struct (struct old-struct)
|
2010-07-30 18:45:34 -04:00
|
|
|
"Apply modifications to list so it mirrors STRUCT.
|
2010-08-28 06:53:19 -04:00
|
|
|
|
2010-12-23 12:54:24 -05:00
|
|
|
OLD-STRUCT is the structure before any modifications. Thus, the
|
|
|
|
function is smart enough to modify only parts of buffer which
|
|
|
|
have changed.
|
|
|
|
|
|
|
|
Initial position of cursor is restored after the changes."
|
2010-07-30 18:45:34 -04:00
|
|
|
(let* ((pos (copy-marker (point)))
|
2010-12-23 12:54:24 -05:00
|
|
|
(shift-body-ind
|
|
|
|
(function
|
|
|
|
;; Shift the indentation between END and BEG by DELTA.
|
|
|
|
;; Start from the line before END.
|
|
|
|
(lambda (end beg delta)
|
|
|
|
(unless (= delta 0)
|
|
|
|
(goto-char end)
|
|
|
|
(forward-line -1)
|
|
|
|
(while (or (> (point) beg)
|
|
|
|
(and (= (point) beg) (not (org-at-item-p))))
|
|
|
|
(when (org-looking-at-p "^[ \t]*\\S-")
|
|
|
|
(let ((i (org-get-indentation)))
|
|
|
|
(org-indent-line-to (+ i delta))))
|
|
|
|
(forward-line -1))))))
|
|
|
|
(modify-item
|
|
|
|
(function
|
|
|
|
;; Replace item first line elements with new elements from
|
|
|
|
;; STRUCT, if appropriate.
|
|
|
|
(lambda (item)
|
|
|
|
(goto-char item)
|
|
|
|
(let* ((new-ind (org-list-get-ind item struct))
|
|
|
|
(old-ind (org-list-get-ind item old-struct))
|
|
|
|
(new-bul (org-list-bullet-string
|
|
|
|
(org-list-get-bullet item struct)))
|
|
|
|
(old-bul (org-list-get-bullet item old-struct))
|
|
|
|
(new-box (org-list-get-checkbox item struct)))
|
|
|
|
(looking-at org-list-full-item-re)
|
|
|
|
;; a. Replace bullet
|
|
|
|
(unless (equal old-bul new-bul)
|
|
|
|
(replace-match new-bul nil nil nil 1))
|
|
|
|
;; b. Replace checkbox
|
|
|
|
(cond
|
|
|
|
((and new-box
|
|
|
|
(save-match-data (org-at-item-description-p))
|
|
|
|
(cdr (assq 'checkbox org-list-automatic-rules)))
|
|
|
|
(message "Cannot add a checkbox to a description list item"))
|
|
|
|
((equal (match-string 3) new-box))
|
|
|
|
((and (match-string 3) new-box)
|
|
|
|
(replace-match new-box nil nil nil 3))
|
|
|
|
((match-string 3)
|
|
|
|
(goto-char (or (match-end 2) (match-end 1)))
|
|
|
|
(looking-at "\\[[ X-]\\][ \t]+")
|
|
|
|
(replace-match ""))
|
|
|
|
(t (goto-char (or (match-end 2) (match-end 1)))
|
|
|
|
(insert (concat new-box " "))))
|
|
|
|
;; c. Indent item to appropriate column
|
|
|
|
(unless (= new-ind old-ind)
|
|
|
|
(delete-region (goto-char (point-at-bol))
|
|
|
|
(progn (skip-chars-forward " \t") (point)))
|
|
|
|
(indent-to new-ind)))))))
|
|
|
|
;; 1. First get list of items and position endings. We maintain
|
|
|
|
;; two alists: ITM-SHIFT, determining indentation shift needed
|
|
|
|
;; at item, and END-POS, a pseudo-alist where key is ending
|
|
|
|
;; position and value point
|
|
|
|
(let (end-list acc-end itm-shift all-ends sliced-struct)
|
|
|
|
(mapc (lambda (e)
|
|
|
|
(let* ((pos (car e))
|
|
|
|
(ind-pos (org-list-get-ind pos struct))
|
|
|
|
(ind-old (org-list-get-ind pos old-struct))
|
|
|
|
(bul-pos (org-list-get-bullet pos struct))
|
|
|
|
(bul-old (org-list-get-bullet pos old-struct))
|
|
|
|
(ind-shift (- (+ ind-pos (length bul-pos))
|
|
|
|
(+ ind-old (length bul-old))))
|
|
|
|
(end-pos (org-list-get-item-end pos old-struct)))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (cons pos ind-shift) itm-shift)
|
2010-12-23 12:54:24 -05:00
|
|
|
(unless (assq end-pos old-struct)
|
|
|
|
;; To determine real ind of an ending position that is
|
|
|
|
;; not at an item, we have to find the item it belongs
|
|
|
|
;; to: it is the last item (ITEM-UP), whose ending is
|
|
|
|
;; further than the position we're interested in.
|
|
|
|
(let ((item-up (assoc-default end-pos acc-end '>)))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (cons end-pos item-up) end-list)))
|
|
|
|
(push (cons end-pos pos) acc-end)))
|
2010-12-23 12:54:24 -05:00
|
|
|
old-struct)
|
|
|
|
;; 2. Slice the items into parts that should be shifted by the
|
|
|
|
;; same amount of indentation. The slices are returned in
|
|
|
|
;; reverse order so changes modifying buffer do not change
|
|
|
|
;; positions they refer to.
|
|
|
|
(setq all-ends (sort (append (mapcar 'car itm-shift)
|
|
|
|
(org-uniquify (mapcar 'car end-list)))
|
|
|
|
'<))
|
|
|
|
(while (cdr all-ends)
|
|
|
|
(let* ((up (pop all-ends))
|
|
|
|
(down (car all-ends))
|
|
|
|
(ind (if (assq up struct)
|
|
|
|
(cdr (assq up itm-shift))
|
|
|
|
(cdr (assq (cdr (assq up end-list)) itm-shift)))))
|
2011-01-01 12:27:31 -05:00
|
|
|
(push (list down up ind) sliced-struct)))
|
2010-12-23 12:54:24 -05:00
|
|
|
;; 3. Modify each slice in buffer, from end to beginning, with a
|
|
|
|
;; special action when beginning is at item start.
|
|
|
|
(mapc (lambda (e)
|
|
|
|
(apply shift-body-ind e)
|
|
|
|
(let ((beg (nth 1 e)))
|
|
|
|
(when (assq beg struct)
|
|
|
|
(funcall modify-item beg))))
|
|
|
|
sliced-struct))
|
|
|
|
;; 4. Go back to initial position
|
2010-07-30 18:45:34 -04:00
|
|
|
(goto-char pos)))
|
|
|
|
|
2010-07-10 11:02:58 -04:00
|
|
|
;;; Indentation
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2010-08-21 19:10:58 -04:00
|
|
|
(defun org-outdent-item ()
|
2010-08-28 06:53:19 -04:00
|
|
|
"Outdent a local list item, but not its children.
|
|
|
|
If a region is active, all items inside will be moved."
|
2010-08-21 19:10:58 -04:00
|
|
|
(interactive)
|
2010-12-29 12:24:50 -05:00
|
|
|
(if (org-at-item-p)
|
|
|
|
(let ((struct (org-list-struct)))
|
|
|
|
(org-list-indent-item-generic -1 t struct))
|
|
|
|
(error "Not at an item")))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2010-08-21 19:10:58 -04:00
|
|
|
(defun org-indent-item ()
|
2010-08-28 06:53:19 -04:00
|
|
|
"Indent a local list item, but not its children.
|
|
|
|
If a region is active, all items inside will be moved."
|
2010-08-21 19:10:58 -04:00
|
|
|
(interactive)
|
2010-12-29 12:24:50 -05:00
|
|
|
(if (org-at-item-p)
|
|
|
|
(let ((struct (org-list-struct)))
|
|
|
|
(org-list-indent-item-generic 1 t struct))
|
|
|
|
(error "Not at an item")))
|
2010-08-21 07:32:33 -04:00
|
|
|
|
2010-08-21 19:10:58 -04:00
|
|
|
(defun org-outdent-item-tree ()
|
2010-08-28 06:53:19 -04:00
|
|
|
"Outdent a local list item including its children.
|
|
|
|
If a region is active, all items inside will be moved."
|
2010-08-21 19:10:58 -04:00
|
|
|
(interactive)
|
2010-12-29 12:24:50 -05:00
|
|
|
(let ((regionp (org-region-active-p)))
|
|
|
|
(cond
|
|
|
|
((or (org-at-item-p)
|
|
|
|
(and (org-region-active-p)
|
|
|
|
(goto-char (region-beginning))
|
|
|
|
(org-at-item-p)))
|
|
|
|
(let ((struct (org-list-struct)))
|
|
|
|
(org-list-indent-item-generic -1 nil struct)))
|
|
|
|
(regionp (error "Region not starting at an item"))
|
|
|
|
(t (error "Not at an item")))))
|
2010-08-21 07:32:33 -04:00
|
|
|
|
2010-08-21 19:10:58 -04:00
|
|
|
(defun org-indent-item-tree ()
|
2010-08-28 06:53:19 -04:00
|
|
|
"Indent a local list item including its children.
|
|
|
|
If a region is active, all items inside will be moved."
|
2010-08-21 19:10:58 -04:00
|
|
|
(interactive)
|
2010-12-29 12:24:50 -05:00
|
|
|
(interactive)
|
|
|
|
(let ((regionp (org-region-active-p)))
|
|
|
|
(cond
|
|
|
|
((or (org-at-item-p)
|
|
|
|
(and (org-region-active-p)
|
|
|
|
(goto-char (region-beginning))
|
|
|
|
(org-at-item-p)))
|
|
|
|
(let ((struct (org-list-struct)))
|
|
|
|
(org-list-indent-item-generic 1 nil struct)))
|
|
|
|
(regionp (error "Region not starting at an item"))
|
|
|
|
(t (error "Not at an item")))))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2010-07-27 18:18:33 -04:00
|
|
|
(defvar org-tab-ind-state)
|
2009-11-02 16:01:38 -05:00
|
|
|
(defun org-cycle-item-indentation ()
|
2010-08-21 07:32:33 -04:00
|
|
|
"Cycle levels of indentation of an empty item.
|
2010-12-23 18:53:30 -05:00
|
|
|
The first run indents the item, if applicable. Subsequents runs
|
2010-09-14 12:30:03 -04:00
|
|
|
outdent it at meaningful levels in the list. When done, item is
|
2010-08-21 07:32:33 -04:00
|
|
|
put back at its original position with its original bullet.
|
|
|
|
|
2010-08-28 06:53:19 -04:00
|
|
|
Return t at each successful move."
|
2010-12-23 18:53:30 -05:00
|
|
|
(when (org-at-item-p)
|
|
|
|
(let* ((org-adapt-indentation nil)
|
|
|
|
(struct (org-list-struct))
|
|
|
|
(ind (org-list-get-ind (point-at-bol) struct)))
|
|
|
|
;; Check that item is really empty
|
2010-12-30 12:39:36 -05:00
|
|
|
(when (and (save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(looking-at org-list-full-item-re))
|
2010-12-23 18:53:30 -05:00
|
|
|
(>= (match-end 0) (save-excursion
|
|
|
|
(goto-char (org-list-get-item-end
|
|
|
|
(point-at-bol) struct))
|
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(point))))
|
|
|
|
(setq this-command 'org-cycle-item-indentation)
|
2010-08-28 06:53:19 -04:00
|
|
|
;; When in the middle of the cycle, try to outdent first. If it
|
|
|
|
;; fails, and point is still at initial position, indent. Else,
|
|
|
|
;; go back to original position.
|
|
|
|
(if (eq last-command 'org-cycle-item-indentation)
|
|
|
|
(cond
|
2010-12-23 18:53:30 -05:00
|
|
|
((ignore-errors (org-list-indent-item-generic -1 t struct)))
|
|
|
|
((and (= ind (car org-tab-ind-state))
|
|
|
|
(ignore-errors (org-list-indent-item-generic 1 t struct))))
|
2010-08-28 06:53:19 -04:00
|
|
|
(t (back-to-indentation)
|
2010-11-14 02:58:41 -05:00
|
|
|
(org-indent-to-column (car org-tab-ind-state))
|
2010-12-23 18:53:30 -05:00
|
|
|
(looking-at "\\S-+")
|
|
|
|
(replace-match (cdr org-tab-ind-state))
|
2010-08-28 06:53:19 -04:00
|
|
|
(end-of-line)
|
|
|
|
;; Break cycle
|
|
|
|
(setq this-command 'identity)))
|
|
|
|
;; If a cycle is starting, remember indentation and bullet,
|
|
|
|
;; then try to indent. If it fails, try to outdent.
|
|
|
|
(setq org-tab-ind-state (cons ind (org-get-bullet)))
|
2010-07-21 17:05:03 -04:00
|
|
|
(cond
|
2010-12-23 18:53:30 -05:00
|
|
|
((ignore-errors (org-list-indent-item-generic 1 t struct)))
|
|
|
|
((ignore-errors (org-list-indent-item-generic -1 t struct)))
|
|
|
|
(t (error "Cannot move item"))))
|
|
|
|
t))))
|
2009-11-02 16:01:38 -05:00
|
|
|
|
2010-07-10 11:02:58 -04:00
|
|
|
;;; Bullets
|
|
|
|
|
2009-07-06 02:53:50 -04:00
|
|
|
(defun org-get-bullet ()
|
2010-08-02 16:07:46 -04:00
|
|
|
"Return the bullet of the item at point.
|
|
|
|
Assume cursor is at an item."
|
2010-08-27 05:48:51 -04:00
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(and (looking-at "[ \t]*\\(\\S-+\\)") (match-string 1))))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2010-07-27 14:52:30 -04:00
|
|
|
(defun org-list-bullet-string (bullet)
|
2010-07-30 18:45:34 -04:00
|
|
|
"Return BULLET with the correct number of whitespaces.
|
2010-07-27 14:52:30 -04:00
|
|
|
It determines the number of whitespaces to append by looking at
|
|
|
|
`org-list-two-spaces-after-bullet-regexp'."
|
2010-07-29 05:44:54 -04:00
|
|
|
(save-match-data
|
2010-07-30 18:45:34 -04:00
|
|
|
(string-match "\\S-+\\([ \t]*\\)" bullet)
|
|
|
|
(replace-match
|
|
|
|
(save-match-data
|
|
|
|
(concat
|
|
|
|
" "
|
|
|
|
;; Do we need to concat another white space ?
|
|
|
|
(when (and org-list-two-spaces-after-bullet-regexp
|
|
|
|
(string-match org-list-two-spaces-after-bullet-regexp bullet))
|
|
|
|
" ")))
|
|
|
|
nil nil bullet 1)))
|
|
|
|
|
|
|
|
(defun org-list-inc-bullet-maybe (bullet)
|
2010-08-21 07:32:33 -04:00
|
|
|
"Increment BULLET if applicable."
|
2010-07-30 18:45:34 -04:00
|
|
|
(if (string-match "[0-9]+" bullet)
|
|
|
|
(replace-match
|
2010-08-19 10:40:21 -04:00
|
|
|
(number-to-string (1+ (string-to-number (match-string 0 bullet))))
|
|
|
|
nil nil bullet)
|
2010-07-30 18:45:34 -04:00
|
|
|
bullet))
|
2010-07-27 14:52:30 -04:00
|
|
|
|
2010-12-23 18:53:30 -05:00
|
|
|
(defun org-list-repair ()
|
2010-08-17 15:29:49 -04:00
|
|
|
"Make sure all items are correctly indented, with the right bullet.
|
2010-12-23 18:53:30 -05:00
|
|
|
This function scans the list at point, along with any sublist."
|
2010-07-10 11:02:58 -04:00
|
|
|
(interactive)
|
|
|
|
(unless (org-at-item-p) (error "This is not a list"))
|
2010-12-23 18:53:30 -05:00
|
|
|
(let* ((struct (org-list-struct))
|
|
|
|
(parents (org-list-struct-parent-alist struct)))
|
|
|
|
(org-list-struct-fix-struct struct parents)))
|
2010-07-30 18:45:34 -04:00
|
|
|
|
2010-07-10 11:02:58 -04:00
|
|
|
(defun org-cycle-list-bullet (&optional which)
|
|
|
|
"Cycle through the different itemize/enumerate bullets.
|
2010-07-20 06:36:01 -04:00
|
|
|
This cycle the entire list level through the sequence:
|
2010-07-10 11:02:58 -04:00
|
|
|
|
2010-07-20 05:06:35 -04:00
|
|
|
`-' -> `+' -> `*' -> `1.' -> `1)'
|
2010-07-10 11:02:58 -04:00
|
|
|
|
2010-07-20 05:06:35 -04:00
|
|
|
If WHICH is a valid string, use that as the new bullet. If WHICH
|
2010-07-20 06:36:01 -04:00
|
|
|
is an integer, 0 means `-', 1 means `+' etc. If WHICH is
|
2010-12-23 18:53:30 -05:00
|
|
|
`previous', cycle backwards."
|
2010-07-10 11:02:58 -04:00
|
|
|
(interactive "P")
|
2010-12-23 18:53:30 -05:00
|
|
|
(unless (org-at-item-p) (error "This is not a list"))
|
2010-11-19 17:47:02 -05:00
|
|
|
(save-excursion
|
2010-12-23 18:53:30 -05:00
|
|
|
(beginning-of-line)
|
|
|
|
(let* ((struct (org-list-struct))
|
|
|
|
(parents (org-list-struct-parent-alist struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(list-beg (org-list-get-list-begin (point) struct prevs))
|
|
|
|
(bullet (org-list-get-bullet list-beg struct))
|
2010-11-19 17:47:02 -05:00
|
|
|
(current (cond
|
|
|
|
((string-match "\\." bullet) "1.")
|
|
|
|
((string-match ")" bullet) "1)")
|
2010-12-23 18:53:30 -05:00
|
|
|
(t (org-trim bullet))))
|
2010-11-19 17:47:02 -05:00
|
|
|
(bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
|
2010-12-23 18:53:30 -05:00
|
|
|
;; Compute list of possible bullets, depending on context
|
2010-11-19 17:47:02 -05:00
|
|
|
(bullet-list (append '("-" "+" )
|
|
|
|
;; *-bullets are not allowed at column 0
|
|
|
|
(unless (and bullet-rule-p
|
|
|
|
(looking-at "\\S-")) '("*"))
|
|
|
|
;; Description items cannot be numbered
|
|
|
|
(unless (and bullet-rule-p
|
|
|
|
(or (eq org-plain-list-ordered-item-terminator ?\))
|
|
|
|
(org-at-item-description-p))) '("1."))
|
|
|
|
(unless (and bullet-rule-p
|
|
|
|
(or (eq org-plain-list-ordered-item-terminator ?.)
|
|
|
|
(org-at-item-description-p))) '("1)"))))
|
|
|
|
(len (length bullet-list))
|
|
|
|
(item-index (- len (length (member current bullet-list))))
|
|
|
|
(get-value (lambda (index) (nth (mod index len) bullet-list)))
|
|
|
|
(new (cond
|
|
|
|
((member which bullet-list) which)
|
|
|
|
((numberp which) (funcall get-value which))
|
|
|
|
((eq 'previous which) (funcall get-value (1- item-index)))
|
|
|
|
(t (funcall get-value (1+ item-index))))))
|
2010-12-23 18:53:30 -05:00
|
|
|
;; Use a short variation of `org-list-struct-fix-struct' as
|
|
|
|
;; there's no need to go through all the steps.
|
|
|
|
(let ((old-struct (mapcar (lambda (e) (copy-alist e)) struct)))
|
|
|
|
(org-list-set-bullet list-beg struct (org-list-bullet-string new))
|
|
|
|
(org-list-struct-fix-bul struct prevs)
|
|
|
|
(org-list-struct-fix-ind struct parents)
|
|
|
|
(org-list-struct-apply-struct struct old-struct)))))
|
2010-07-10 11:02:58 -04:00
|
|
|
|
|
|
|
;;; Checkboxes
|
|
|
|
|
|
|
|
(defun org-toggle-checkbox (&optional toggle-presence)
|
|
|
|
"Toggle the checkbox in the current line.
|
2010-07-28 09:53:37 -04:00
|
|
|
With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
|
|
|
|
double prefix, set checkbox to [-].
|
|
|
|
|
|
|
|
When there is an active region, toggle status or presence of the
|
2010-09-04 04:52:05 -04:00
|
|
|
first checkbox there, and make every item inside have the
|
|
|
|
same status or presence, respectively.
|
2010-07-28 09:53:37 -04:00
|
|
|
|
|
|
|
If the cursor is in a headline, apply this to all checkbox items
|
|
|
|
in the text below the heading, taking as reference the first item
|
2010-09-04 04:52:05 -04:00
|
|
|
in subtree, ignoring drawers."
|
2010-07-10 11:02:58 -04:00
|
|
|
(interactive "P")
|
2010-12-17 13:54:25 -05:00
|
|
|
(save-excursion
|
|
|
|
(let* (singlep
|
|
|
|
block-item
|
|
|
|
lim-up
|
|
|
|
lim-down
|
|
|
|
(orderedp (ignore-errors (org-entry-get nil "ORDERED")))
|
|
|
|
(bounds
|
|
|
|
;; In a region, start at first item in region
|
|
|
|
(cond
|
|
|
|
((org-region-active-p)
|
|
|
|
(let ((limit (region-end)))
|
|
|
|
(goto-char (region-beginning))
|
2011-01-01 18:26:37 -05:00
|
|
|
(if (org-list-search-forward org-item-beginning-re limit t)
|
2010-12-17 13:54:25 -05:00
|
|
|
(setq lim-up (point-at-bol))
|
|
|
|
(error "No item in region"))
|
|
|
|
(setq lim-down (copy-marker limit))))
|
|
|
|
((org-on-heading-p)
|
|
|
|
;; On an heading, start at first item after drawers
|
|
|
|
(let ((limit (save-excursion (outline-next-heading) (point))))
|
|
|
|
(forward-line 1)
|
|
|
|
(when (looking-at org-drawer-regexp)
|
|
|
|
(re-search-forward "^[ \t]*:END:" limit nil))
|
2011-01-01 18:26:37 -05:00
|
|
|
(if (org-list-search-forward org-item-beginning-re limit t)
|
2010-12-17 13:54:25 -05:00
|
|
|
(setq lim-up (point-at-bol))
|
|
|
|
(error "No item in subtree"))
|
|
|
|
(setq lim-down (copy-marker limit))))
|
|
|
|
;; Just one item: set singlep flag
|
|
|
|
((org-at-item-p)
|
|
|
|
(setq singlep t)
|
|
|
|
(setq lim-up (point-at-bol)
|
|
|
|
lim-down (point-at-eol)))
|
|
|
|
(t (error "Not at an item or heading, and no active region"))))
|
|
|
|
;; determine the checkbox going to be applied to all items
|
|
|
|
;; within bounds
|
|
|
|
(ref-checkbox
|
|
|
|
(progn
|
|
|
|
(goto-char lim-up)
|
|
|
|
(let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
|
|
|
|
(cond
|
|
|
|
((equal toggle-presence '(16)) "[-]")
|
|
|
|
((equal toggle-presence '(4))
|
|
|
|
(unless cbox "[ ]"))
|
|
|
|
((equal "[ ]" cbox) "[X]")
|
|
|
|
(t "[ ]"))))))
|
|
|
|
;; When an item is found within bounds, grab the full list at
|
|
|
|
;; point structure, then: 1. set checkbox of all its items
|
|
|
|
;; within bounds to ref-checkbox; 2. fix checkboxes of the whole
|
|
|
|
;; list; 3. move point after the list.
|
|
|
|
(goto-char lim-up)
|
|
|
|
(while (and (< (point) lim-down)
|
2011-01-01 18:26:37 -05:00
|
|
|
(org-list-search-forward org-item-beginning-re
|
|
|
|
lim-down 'move))
|
2010-12-17 13:54:25 -05:00
|
|
|
(let* ((struct (org-list-struct))
|
|
|
|
(struct-copy (mapcar (lambda (e) (copy-alist e)) struct))
|
|
|
|
(parents (org-list-struct-parent-alist struct))
|
2010-12-23 18:53:30 -05:00
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
2010-12-17 13:54:25 -05:00
|
|
|
(bottom (copy-marker (org-list-get-bottom-point struct)))
|
|
|
|
(items-to-toggle (org-remove-if
|
|
|
|
(lambda (e) (or (< e lim-up) (> e lim-down)))
|
|
|
|
(mapcar 'car (cdr struct)))))
|
|
|
|
(mapc (lambda (e) (org-list-set-checkbox
|
|
|
|
e struct
|
|
|
|
;; if there is no box at item, leave as-is
|
|
|
|
;; unless function was called with C-u prefix
|
|
|
|
(let ((cur-box (org-list-get-checkbox e struct)))
|
|
|
|
(if (or cur-box (equal toggle-presence '(4)))
|
|
|
|
ref-checkbox
|
|
|
|
cur-box))))
|
|
|
|
items-to-toggle)
|
2010-12-23 18:53:30 -05:00
|
|
|
(setq block-item (org-list-struct-fix-box
|
|
|
|
struct parents prevs orderedp))
|
2010-12-17 13:54:25 -05:00
|
|
|
;; Report some problems due to ORDERED status of subtree. If
|
|
|
|
;; only one box was being checked, throw an error, else,
|
|
|
|
;; only signal problems.
|
|
|
|
(cond
|
|
|
|
((and singlep block-item (> lim-up block-item))
|
|
|
|
(error
|
|
|
|
"Checkbox blocked because of unchecked box at line %d"
|
|
|
|
(org-current-line block-item)))
|
|
|
|
(block-item
|
|
|
|
(message
|
|
|
|
"Checkboxes were removed due to unchecked box at line %d"
|
|
|
|
(org-current-line block-item))))
|
|
|
|
(goto-char bottom)
|
|
|
|
(org-list-struct-apply-struct struct struct-copy))))
|
2010-07-28 09:53:37 -04:00
|
|
|
(org-update-checkbox-count-maybe)))
|
2010-07-10 11:02:58 -04:00
|
|
|
|
|
|
|
(defun org-reset-checkbox-state-subtree ()
|
|
|
|
"Reset all checkboxes in an entry subtree."
|
|
|
|
(interactive "*")
|
|
|
|
(save-restriction
|
|
|
|
(save-excursion
|
|
|
|
(org-narrow-to-subtree)
|
|
|
|
(org-show-subtree)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(let ((end (point-max)))
|
|
|
|
(while (< (point) end)
|
|
|
|
(when (org-at-item-checkbox-p)
|
2010-07-19 11:02:58 -04:00
|
|
|
(replace-match "[ ]" t t nil 1))
|
2010-07-10 11:02:58 -04:00
|
|
|
(beginning-of-line 2))))
|
|
|
|
(org-update-checkbox-count-maybe)))
|
|
|
|
|
|
|
|
(defvar org-checkbox-statistics-hook nil
|
|
|
|
"Hook that is run whenever Org thinks checkbox statistics should be updated.
|
2010-08-20 05:35:56 -04:00
|
|
|
This hook runs even if checkbox rule in
|
|
|
|
`org-list-automatic-rules' does not apply, so it can be used to
|
2010-07-23 14:36:42 -04:00
|
|
|
implement alternative ways of collecting statistics
|
|
|
|
information.")
|
2010-07-10 11:02:58 -04:00
|
|
|
|
|
|
|
(defun org-update-checkbox-count-maybe ()
|
|
|
|
"Update checkbox statistics unless turned off by user."
|
2010-07-23 14:36:42 -04:00
|
|
|
(when (cdr (assq 'checkbox org-list-automatic-rules))
|
2010-07-10 11:02:58 -04:00
|
|
|
(org-update-checkbox-count))
|
|
|
|
(run-hooks 'org-checkbox-statistics-hook))
|
|
|
|
|
|
|
|
(defun org-update-checkbox-count (&optional all)
|
|
|
|
"Update the checkbox statistics in the current section.
|
2010-12-17 13:54:25 -05:00
|
|
|
This will find all statistic cookies like [57%] and [6/12] and
|
|
|
|
update them with the current numbers.
|
|
|
|
|
|
|
|
With optional prefix argument ALL, do this for the whole buffer."
|
2010-07-10 11:02:58 -04:00
|
|
|
(interactive "P")
|
|
|
|
(save-excursion
|
2010-12-17 13:54:25 -05:00
|
|
|
(let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
|
|
|
|
(box-re "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
|
|
|
|
(recursivep
|
|
|
|
(or (not org-hierarchical-checkbox-statistics)
|
|
|
|
(string-match "\\<recursive\\>"
|
|
|
|
(or (ignore-errors
|
|
|
|
(org-entry-get nil "COOKIE_DATA"))
|
|
|
|
""))))
|
|
|
|
(bounds (if all
|
|
|
|
(cons (point-min) (point-max))
|
|
|
|
(cons (or (ignore-errors (org-back-to-heading) (point))
|
|
|
|
(point-min))
|
|
|
|
(save-excursion (outline-next-heading) (point)))))
|
|
|
|
(count-boxes
|
|
|
|
(function
|
|
|
|
;; add checked boxes and boxes of all types in all
|
|
|
|
;; structures in STRUCTS to c-on and c-all, respectively.
|
|
|
|
;; This looks at RECURSIVEP value. If ITEM is nil, count
|
|
|
|
;; across the whole structure, else count only across
|
|
|
|
;; subtree whose ancestor is ITEM.
|
|
|
|
(lambda (item structs)
|
|
|
|
(mapc
|
|
|
|
(lambda (s)
|
|
|
|
(let* ((pre (org-list-struct-prev-alist s))
|
2010-12-28 19:43:20 -05:00
|
|
|
(par (org-list-struct-parent-alist s))
|
2010-12-17 13:54:25 -05:00
|
|
|
(items
|
2011-01-01 12:27:31 -05:00
|
|
|
(cond
|
|
|
|
((and recursivep item) (org-list-get-subtree item s))
|
|
|
|
(recursivep (mapcar 'car s))
|
|
|
|
(item (org-list-get-children item s par))
|
|
|
|
(t (org-list-get-all-items
|
|
|
|
(org-list-get-top-point s) s pre))))
|
2010-12-17 13:54:25 -05:00
|
|
|
(cookies (delq nil (mapcar
|
|
|
|
(lambda (e)
|
|
|
|
(org-list-get-checkbox e s))
|
|
|
|
items))))
|
|
|
|
(setq c-all (+ (length cookies) c-all)
|
|
|
|
c-on (+ (org-count "[X]" cookies) c-on))))
|
|
|
|
structs))))
|
2011-01-04 18:04:11 -05:00
|
|
|
(backup-end 1)
|
|
|
|
cookies-list structs-backup)
|
2010-12-17 13:54:25 -05:00
|
|
|
(goto-char (car bounds))
|
|
|
|
;; 1. Build an alist for each cookie found within BOUNDS. The
|
|
|
|
;; key will be position at beginning of cookie and values
|
|
|
|
;; ending position, format of cookie, number of checked boxes
|
|
|
|
;; to report, and total number of boxes.
|
|
|
|
(while (re-search-forward cookie-re (cdr bounds) t)
|
2011-01-04 18:04:11 -05:00
|
|
|
(catch 'skip
|
|
|
|
(save-excursion
|
|
|
|
(let ((c-on 0) (c-all 0))
|
|
|
|
(save-match-data
|
|
|
|
;; There are two types of cookies: those at headings and those
|
|
|
|
;; at list items.
|
|
|
|
(cond
|
|
|
|
;; Cookie is at an heading, but specifically for todo,
|
|
|
|
;; not for checkboxes: skip it.
|
|
|
|
((and (org-on-heading-p)
|
|
|
|
(string-match "\\<todo\\>"
|
|
|
|
(downcase
|
|
|
|
(or (org-entry-get nil "COOKIE_DATA") ""))))
|
|
|
|
(throw 'skip nil))
|
|
|
|
;; Cookie is at an heading, but all lists before next
|
|
|
|
;; heading already have been read. Use data collected
|
|
|
|
;; in STRUCTS-BACKUP. This should only happen when
|
|
|
|
;; heading has more than one cookie on it.
|
|
|
|
((and (org-on-heading-p)
|
|
|
|
(<= (save-excursion (outline-next-heading) (point))
|
|
|
|
backup-end))
|
|
|
|
(funcall count-boxes nil structs-backup))
|
|
|
|
;; Cookie is at a fresh heading. Grab structure of
|
|
|
|
;; every list containing a checkbox between point and
|
|
|
|
;; next headline, and save them in STRUCTS-BACKUP.
|
|
|
|
((org-on-heading-p)
|
|
|
|
(setq backup-end (save-excursion
|
|
|
|
(outline-next-heading) (point)))
|
|
|
|
(while (org-list-search-forward box-re backup-end 'move)
|
|
|
|
(let* ((struct (org-list-struct))
|
|
|
|
(bottom (org-list-get-bottom-point struct)))
|
|
|
|
(push struct structs-backup)
|
|
|
|
(goto-char bottom)))
|
|
|
|
(funcall count-boxes nil structs-backup))
|
|
|
|
;; Cookie is at an item, and we already list structure
|
|
|
|
;; stored in STRUCTS-BACKUP.
|
|
|
|
((and (org-at-item-p)
|
|
|
|
(< (point-at-bol) backup-end))
|
|
|
|
(funcall count-boxes (point-at-bol) structs-backup))
|
|
|
|
;; Cookie is at an item, but we need to compute list
|
|
|
|
;; structure.
|
|
|
|
((org-at-item-p)
|
|
|
|
(let ((struct (org-list-struct)))
|
|
|
|
(setq backup-end (org-list-get-bottom-point struct)
|
|
|
|
structs-backup (list struct)))
|
|
|
|
(funcall count-boxes item structs-backup))
|
|
|
|
;; Else, cookie found is at a wrong place. Skip it.
|
|
|
|
(t (throw 'skip nil))))
|
|
|
|
;; Build the cookies list, with appropriate information
|
|
|
|
(push (list (match-beginning 1) ; cookie start
|
|
|
|
(match-end 1) ; cookie end
|
|
|
|
(match-string 2) ; percent?
|
|
|
|
c-on ; checked boxes
|
|
|
|
c-all) ; total boxes
|
|
|
|
cookies-list)))))
|
2010-12-17 13:54:25 -05:00
|
|
|
;; 2. Apply alist to buffer, in reverse order so positions stay
|
|
|
|
;; unchanged after cookie modifications.
|
|
|
|
(mapc (lambda (cookie)
|
|
|
|
(let* ((beg (car cookie))
|
|
|
|
(end (nth 1 cookie))
|
|
|
|
(percentp (nth 2 cookie))
|
|
|
|
(checked (nth 3 cookie))
|
|
|
|
(total (nth 4 cookie))
|
|
|
|
(new (if percentp
|
|
|
|
(format "[%d%%]" (/ (* 100 checked)
|
|
|
|
(max 1 total)))
|
|
|
|
(format "[%d/%d]" checked total))))
|
|
|
|
(goto-char beg)
|
|
|
|
(insert new)
|
|
|
|
(delete-region (point) (+ (point) (- end beg)))))
|
|
|
|
cookies-list))))
|
2010-07-10 11:02:58 -04:00
|
|
|
|
|
|
|
(defun org-get-checkbox-statistics-face ()
|
|
|
|
"Select the face for checkbox statistics.
|
|
|
|
The face will be `org-done' when all relevant boxes are checked.
|
|
|
|
Otherwise it will be `org-todo'."
|
|
|
|
(if (match-end 1)
|
|
|
|
(if (equal (match-string 1) "100%")
|
|
|
|
'org-checkbox-statistics-done
|
|
|
|
'org-checkbox-statistics-todo)
|
|
|
|
(if (and (> (match-end 2) (match-beginning 2))
|
|
|
|
(equal (match-string 2) (match-string 3)))
|
|
|
|
'org-checkbox-statistics-done
|
|
|
|
'org-checkbox-statistics-todo)))
|
|
|
|
|
|
|
|
;;; Misc Tools
|
|
|
|
|
2010-07-12 11:40:00 -04:00
|
|
|
(defun org-apply-on-list (function init-value &rest args)
|
2010-09-14 12:30:03 -04:00
|
|
|
"Call FUNCTION on each item of the list at point.
|
2010-08-21 07:32:33 -04:00
|
|
|
FUNCTION must be called with at least one argument: INIT-VALUE,
|
|
|
|
that will contain the value returned by the function at the
|
|
|
|
previous item, plus ARGS extra arguments.
|
2010-07-10 11:02:58 -04:00
|
|
|
|
2010-12-29 11:58:58 -05:00
|
|
|
FUNCTION is applied on items in reverse order.
|
|
|
|
|
2010-07-12 11:40:00 -04:00
|
|
|
As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
|
|
|
|
will return the number of items in the current list.
|
|
|
|
|
2010-09-14 12:30:03 -04:00
|
|
|
Sublists of the list are skipped. Cursor is always at the
|
2010-07-12 11:40:00 -04:00
|
|
|
beginning of the item."
|
2010-12-29 11:58:58 -05:00
|
|
|
(let* ((struct (org-list-struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(item (copy-marker (point-at-bol)))
|
|
|
|
(all (org-list-get-all-items (marker-position item) struct prevs))
|
2010-08-17 16:21:24 -04:00
|
|
|
(value init-value))
|
2010-12-29 11:58:58 -05:00
|
|
|
(mapc (lambda (e)
|
|
|
|
(goto-char e)
|
|
|
|
(setq value (apply function value args)))
|
|
|
|
(nreverse all))
|
|
|
|
(goto-char item)
|
2010-08-17 16:21:24 -04:00
|
|
|
value))
|
2010-07-10 11:02:58 -04:00
|
|
|
|
|
|
|
(defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
|
|
|
|
"Sort plain list items.
|
|
|
|
The cursor may be at any item of the list that should be sorted.
|
2010-08-30 07:55:44 -04:00
|
|
|
Sublists are not sorted. Checkboxes, if any, are ignored.
|
2010-07-10 11:02:58 -04:00
|
|
|
|
|
|
|
Sorting can be alphabetically, numerically, by date/time as given by
|
|
|
|
a time stamp, by a property or by priority.
|
|
|
|
|
2010-08-21 07:32:33 -04:00
|
|
|
Comparing entries ignores case by default. However, with an
|
|
|
|
optional argument WITH-CASE, the sorting considers case as well.
|
|
|
|
|
2010-07-17 07:56:11 -04:00
|
|
|
The command prompts for the sorting type unless it has been given
|
|
|
|
to the function through the SORTING-TYPE argument, which needs to
|
2010-07-28 05:19:08 -04:00
|
|
|
be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
|
2010-07-17 07:56:11 -04:00
|
|
|
meaning of each character:
|
2010-07-10 11:02:58 -04:00
|
|
|
|
2010-07-17 07:56:11 -04:00
|
|
|
n Numerically, by converting the beginning of the item to a number.
|
2010-07-28 05:19:08 -04:00
|
|
|
a Alphabetically. Only the first line of item is checked.
|
2010-07-17 07:56:11 -04:00
|
|
|
t By date/time, either the first active time stamp in the entry, if
|
2010-07-28 05:19:08 -04:00
|
|
|
any, or by the first inactive one. In a timer list, sort the timers.
|
2010-07-10 11:02:58 -04:00
|
|
|
|
|
|
|
Capital letters will reverse the sort order.
|
|
|
|
|
|
|
|
If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
|
|
|
|
function to be called with point at the beginning of the record.
|
|
|
|
It must return either a string or a number that should serve as
|
2010-08-21 07:32:33 -04:00
|
|
|
the sorting key for that record. It will then use COMPARE-FUNC to
|
|
|
|
compare entries."
|
2010-07-10 11:02:58 -04:00
|
|
|
(interactive "P")
|
|
|
|
(let* ((case-func (if with-case 'identity 'downcase))
|
2010-12-23 18:53:30 -05:00
|
|
|
(struct (org-list-struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(start (org-list-get-list-begin (point-at-bol) struct prevs))
|
|
|
|
(end (org-list-get-list-end (point-at-bol) struct prevs))
|
2010-07-10 11:02:58 -04:00
|
|
|
(sorting-type
|
|
|
|
(progn
|
|
|
|
(message
|
|
|
|
"Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
|
|
|
|
(read-char-exclusive)))
|
|
|
|
(getkey-func (and (= (downcase sorting-type) ?f)
|
|
|
|
(org-icompleting-read "Sort using function: "
|
|
|
|
obarray 'fboundp t nil nil)
|
|
|
|
(intern getkey-func))))
|
|
|
|
(message "Sorting items...")
|
|
|
|
(save-restriction
|
|
|
|
(narrow-to-region start end)
|
2010-10-30 08:26:40 -04:00
|
|
|
(goto-char (point-min))
|
2010-07-10 11:02:58 -04:00
|
|
|
(let* ((dcst (downcase sorting-type))
|
|
|
|
(case-fold-search nil)
|
|
|
|
(now (current-time))
|
|
|
|
(sort-func (cond
|
|
|
|
((= dcst ?a) 'string<)
|
|
|
|
((= dcst ?f) compare-func)
|
2010-07-17 07:56:11 -04:00
|
|
|
((= dcst ?t) '<)
|
2010-07-10 11:02:58 -04:00
|
|
|
(t nil)))
|
2010-12-23 18:53:30 -05:00
|
|
|
(next-record (lambda ()
|
2010-07-12 08:31:20 -04:00
|
|
|
(skip-chars-forward " \r\t\n")
|
|
|
|
(beginning-of-line)))
|
2010-07-10 11:02:58 -04:00
|
|
|
(end-record (lambda ()
|
2011-01-02 13:25:52 -05:00
|
|
|
(goto-char (org-list-get-item-end-before-blank
|
|
|
|
(point) struct))))
|
2010-07-17 07:56:11 -04:00
|
|
|
(value-to-sort
|
|
|
|
(lambda ()
|
|
|
|
(when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
|
|
|
|
(cond
|
|
|
|
((= dcst ?n)
|
|
|
|
(string-to-number (buffer-substring (match-end 0)
|
|
|
|
(point-at-eol))))
|
|
|
|
((= dcst ?a)
|
|
|
|
(buffer-substring (match-end 0) (point-at-eol)))
|
|
|
|
((= dcst ?t)
|
|
|
|
(cond
|
|
|
|
;; If it is a timer list, convert timer to seconds
|
2010-07-17 09:17:38 -04:00
|
|
|
((org-at-item-timer-p)
|
2010-07-17 07:56:11 -04:00
|
|
|
(org-timer-hms-to-secs (match-string 1)))
|
2011-01-01 18:26:37 -05:00
|
|
|
((or (re-search-forward org-ts-regexp (point-at-eol) t)
|
|
|
|
(re-search-forward org-ts-regexp-both
|
|
|
|
(point-at-eol) t))
|
2010-07-17 07:56:11 -04:00
|
|
|
(org-time-string-to-seconds (match-string 0)))
|
|
|
|
(t (org-float-time now))))
|
|
|
|
((= dcst ?f)
|
|
|
|
(if getkey-func
|
|
|
|
(let ((value (funcall getkey-func)))
|
|
|
|
(if (stringp value)
|
|
|
|
(funcall case-func value)
|
|
|
|
value))
|
|
|
|
(error "Invalid key function `%s'" getkey-func)))
|
|
|
|
(t (error "Invalid sorting type `%c'" sorting-type)))))))
|
2010-08-19 10:40:21 -04:00
|
|
|
(sort-subr (/= dcst sorting-type)
|
2010-12-23 18:53:30 -05:00
|
|
|
next-record
|
2010-08-19 10:40:21 -04:00
|
|
|
end-record
|
|
|
|
value-to-sort
|
|
|
|
nil
|
|
|
|
sort-func)
|
2011-01-02 13:25:52 -05:00
|
|
|
;; Read and fix list again, as `sort-subr' probably destroyed
|
|
|
|
;; its structure.
|
|
|
|
(org-list-repair)
|
2010-07-10 11:02:58 -04:00
|
|
|
(run-hooks 'org-after-sorting-entries-or-items-hook)
|
|
|
|
(message "Sorting items...done")))))
|
|
|
|
|
2008-09-23 02:13:32 -04:00
|
|
|
;;; Send and receive lists
|
2008-09-23 02:03:13 -04:00
|
|
|
(defun org-list-parse-list (&optional delete)
|
|
|
|
"Parse the list at point and maybe DELETE it.
|
2010-12-28 19:43:42 -05:00
|
|
|
|
|
|
|
Return a list whose car is a symbol of list type, among
|
|
|
|
`ordered', `unordered' and `descriptive'. Then, each item is a
|
|
|
|
list whose elements are strings and other sub-lists. Inside
|
|
|
|
strings, checkboxes are replaced by \"[CBON]\" and \"[CBOFF]\".
|
|
|
|
|
|
|
|
For example, the following list:
|
|
|
|
|
|
|
|
1. first item
|
|
|
|
+ sub-item one
|
|
|
|
+ [X] sub-item two
|
|
|
|
more text in first item
|
|
|
|
2. last item
|
|
|
|
|
|
|
|
will be parsed as:
|
|
|
|
|
|
|
|
\(ordered \(\"first item\"
|
|
|
|
\(unordered \(\"sub-item one\"\) \(\"[CBON] sub-item two\"\)\)
|
|
|
|
\"more text in first item\"\)
|
|
|
|
\(\"last item\"\)\)
|
|
|
|
|
|
|
|
Point is left at list end."
|
|
|
|
(let* ((struct (org-list-struct))
|
|
|
|
(prevs (org-list-struct-prev-alist struct))
|
|
|
|
(parents (org-list-struct-parent-alist struct))
|
|
|
|
(top (org-list-get-top-point struct))
|
|
|
|
(bottom (org-list-get-bottom-point struct))
|
|
|
|
out
|
|
|
|
(get-list-type
|
|
|
|
(function
|
2010-12-30 12:39:36 -05:00
|
|
|
;; determine type of list by getting info on item POS in
|
|
|
|
;; STRUCT.
|
|
|
|
(lambda (pos struct)
|
|
|
|
(cond ((string-match "[0-9]" (org-list-get-bullet pos struct))
|
|
|
|
'ordered)
|
|
|
|
((org-list-get-tag pos struct) 'descriptive)
|
|
|
|
(t 'unordered)))))
|
2010-12-28 19:43:42 -05:00
|
|
|
(parse-sublist
|
|
|
|
(function
|
|
|
|
;; return a list whose car is list type and cdr a list of
|
|
|
|
;; items' body.
|
|
|
|
(lambda (e)
|
2010-12-30 12:39:36 -05:00
|
|
|
(cons (funcall get-list-type (car e) struct)
|
2010-12-28 19:43:42 -05:00
|
|
|
(mapcar parse-item e)))))
|
|
|
|
(parse-item
|
|
|
|
(function
|
|
|
|
;; return a list containing text and any sublist inside
|
|
|
|
;; item.
|
|
|
|
(lambda (e)
|
|
|
|
(let ((start (save-excursion
|
|
|
|
(goto-char e)
|
|
|
|
(looking-at org-item-beginning-re)
|
|
|
|
(match-end 0)))
|
|
|
|
(childp (org-list-has-child-p e struct))
|
|
|
|
(end (org-list-get-item-end e struct)))
|
|
|
|
(if childp
|
|
|
|
(let* ((children (org-list-get-children e struct parents))
|
|
|
|
(body (list (funcall get-text start childp t))))
|
|
|
|
(while children
|
|
|
|
(let* ((first (car children))
|
|
|
|
(sub (org-list-get-all-items first struct prevs))
|
|
|
|
(last-c (car (last sub)))
|
|
|
|
(last-end (org-list-get-item-end last-c struct)))
|
|
|
|
(push (funcall parse-sublist sub) body)
|
|
|
|
(setq children (cdr (member last-c children)))
|
|
|
|
(unless (= (or (car children) end) last-end)
|
|
|
|
(push (funcall get-text last-end (or (car children) end) nil)
|
|
|
|
body))))
|
|
|
|
(nreverse body))
|
|
|
|
(list (funcall get-text start end t)))))))
|
|
|
|
(get-text
|
|
|
|
(function
|
|
|
|
;; return text between BEG and END, trimmed, with
|
|
|
|
;; checkboxes replaced if BOX is true.
|
|
|
|
(lambda (beg end box)
|
|
|
|
(let ((text (org-trim (buffer-substring beg end))))
|
|
|
|
(if (and box
|
|
|
|
(string-match
|
|
|
|
"^\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[\\([xX ]\\)\\]"
|
|
|
|
text))
|
|
|
|
(replace-match
|
|
|
|
(if (equal (match-string 1 text) " ") "CBOFF" "CBON")
|
|
|
|
t nil text 1)
|
|
|
|
text))))))
|
|
|
|
;; store output, take care of cursor position and deletion of
|
|
|
|
;; list, then return output.
|
|
|
|
(setq out (funcall parse-sublist (org-list-get-all-items top struct prevs)))
|
2011-01-02 07:52:16 -05:00
|
|
|
(goto-char top)
|
2010-07-11 09:39:49 -04:00
|
|
|
(when delete
|
2010-12-28 19:43:42 -05:00
|
|
|
(delete-region top bottom)
|
2011-01-02 07:52:16 -05:00
|
|
|
(when (and (not (eq org-list-ending-method 'indent))
|
|
|
|
(looking-at (org-list-end-re)))
|
|
|
|
(replace-match "\n")))
|
2010-12-28 19:43:42 -05:00
|
|
|
out))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2009-07-25 16:48:32 -04:00
|
|
|
(defun org-list-make-subtree ()
|
|
|
|
"Convert the plain list at point into a subtree."
|
|
|
|
(interactive)
|
2010-07-20 05:47:54 -04:00
|
|
|
(if (not (org-in-item-p))
|
2010-08-21 07:32:33 -04:00
|
|
|
(error "Not in a list")
|
2010-07-20 05:47:54 -04:00
|
|
|
(let ((list (org-list-parse-list t)) nstars)
|
|
|
|
(save-excursion
|
|
|
|
(if (ignore-errors
|
|
|
|
(org-back-to-heading))
|
2010-08-23 05:53:08 -04:00
|
|
|
(progn (looking-at org-complex-heading-regexp)
|
2010-07-20 05:47:54 -04:00
|
|
|
(setq nstars (length (match-string 1))))
|
|
|
|
(setq nstars 0)))
|
|
|
|
(org-list-make-subtrees list (1+ nstars)))))
|
2009-07-25 16:48:32 -04:00
|
|
|
|
|
|
|
(defun org-list-make-subtrees (list level)
|
|
|
|
"Convert LIST into subtrees starting at LEVEL."
|
|
|
|
(if (symbolp (car list))
|
|
|
|
(org-list-make-subtrees (cdr list) level)
|
|
|
|
(mapcar (lambda (item)
|
|
|
|
(if (stringp item)
|
2009-08-03 11:30:30 -04:00
|
|
|
(insert (make-string
|
|
|
|
(if org-odd-levels-only
|
2009-07-25 16:48:32 -04:00
|
|
|
(1- (* 2 level)) level) ?*) " " item "\n")
|
|
|
|
(org-list-make-subtrees item (1+ level))))
|
|
|
|
list)))
|
|
|
|
|
2008-09-23 02:03:13 -04:00
|
|
|
(defun org-list-insert-radio-list ()
|
|
|
|
"Insert a radio list template appropriate for this major mode."
|
|
|
|
(interactive)
|
|
|
|
(let* ((e (assq major-mode org-list-radio-list-templates))
|
|
|
|
(txt (nth 1 e))
|
|
|
|
name pos)
|
|
|
|
(unless e (error "No radio list setup defined for %s" major-mode))
|
|
|
|
(setq name (read-string "List name: "))
|
|
|
|
(while (string-match "%n" txt)
|
|
|
|
(setq txt (replace-match name t t txt)))
|
|
|
|
(or (bolp) (insert "\n"))
|
|
|
|
(setq pos (point))
|
|
|
|
(insert txt)
|
|
|
|
(goto-char pos)))
|
|
|
|
|
|
|
|
(defun org-list-send-list (&optional maybe)
|
2009-11-12 07:39:29 -05:00
|
|
|
"Send a transformed version of this list to the receiver position.
|
2008-09-23 02:03:13 -04:00
|
|
|
With argument MAYBE, fail quietly if no transformation is defined for
|
|
|
|
this list."
|
|
|
|
(interactive)
|
|
|
|
(catch 'exit
|
2010-08-28 06:53:19 -04:00
|
|
|
(unless (org-at-item-p) (error "Not at a list item"))
|
2008-09-23 02:03:13 -04:00
|
|
|
(save-excursion
|
2010-07-14 05:11:10 -04:00
|
|
|
(re-search-backward "#\\+ORGLST" nil t)
|
2010-07-12 05:16:42 -04:00
|
|
|
(unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
|
2008-09-23 02:03:13 -04:00
|
|
|
(if maybe
|
|
|
|
(throw 'exit nil)
|
|
|
|
(error "Don't know how to transform this list"))))
|
|
|
|
(let* ((name (match-string 1))
|
|
|
|
(transform (intern (match-string 2)))
|
2010-07-10 17:56:41 -04:00
|
|
|
(bottom-point
|
|
|
|
(save-excursion
|
2010-08-19 10:40:21 -04:00
|
|
|
(re-search-forward
|
|
|
|
"\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
|
2010-07-14 05:11:10 -04:00
|
|
|
(match-beginning 0)))
|
|
|
|
(top-point
|
|
|
|
(progn
|
|
|
|
(re-search-backward "#\\+ORGLST" nil t)
|
2010-07-20 15:13:37 -04:00
|
|
|
(re-search-forward org-item-beginning-re bottom-point t)
|
2010-07-14 05:11:10 -04:00
|
|
|
(match-beginning 0)))
|
2010-07-10 17:56:41 -04:00
|
|
|
(list (save-restriction
|
|
|
|
(narrow-to-region top-point bottom-point)
|
|
|
|
(org-list-parse-list)))
|
|
|
|
beg txt)
|
2008-09-23 02:03:13 -04:00
|
|
|
(unless (fboundp transform)
|
|
|
|
(error "No such transformation function %s" transform))
|
2010-07-16 02:49:37 -04:00
|
|
|
(let ((txt (funcall transform list)))
|
|
|
|
;; Find the insertion place
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(unless (re-search-forward
|
2010-08-19 10:40:21 -04:00
|
|
|
(concat "BEGIN RECEIVE ORGLST +"
|
|
|
|
name
|
|
|
|
"\\([ \t]\\|$\\)") nil t)
|
2010-07-16 02:49:37 -04:00
|
|
|
(error "Don't know where to insert translated list"))
|
|
|
|
(goto-char (match-beginning 0))
|
|
|
|
(beginning-of-line 2)
|
|
|
|
(setq beg (point))
|
|
|
|
(unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
|
|
|
|
(error "Cannot find end of insertion region"))
|
2010-07-19 03:16:13 -04:00
|
|
|
(delete-region beg (point-at-bol))
|
2010-07-16 02:49:37 -04:00
|
|
|
(goto-char beg)
|
|
|
|
(insert txt "\n")))
|
2008-09-23 02:03:13 -04:00
|
|
|
(message "List converted and installed at receiver location"))))
|
|
|
|
|
|
|
|
(defun org-list-to-generic (list params)
|
|
|
|
"Convert a LIST parsed through `org-list-parse-list' to other formats.
|
|
|
|
Valid parameters PARAMS are
|
|
|
|
|
2008-12-16 11:17:15 -05:00
|
|
|
:ustart String to start an unordered list
|
|
|
|
:uend String to end an unordered list
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2008-12-16 11:17:15 -05:00
|
|
|
:ostart String to start an ordered list
|
|
|
|
:oend String to end an ordered list
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2008-12-16 11:17:15 -05:00
|
|
|
:dstart String to start a descriptive list
|
|
|
|
:dend String to end a descriptive list
|
2008-09-23 02:03:13 -04:00
|
|
|
:dtstart String to start a descriptive term
|
2008-12-16 11:17:15 -05:00
|
|
|
:dtend String to end a descriptive term
|
2008-09-23 02:03:13 -04:00
|
|
|
:ddstart String to start a description
|
2008-12-16 11:17:15 -05:00
|
|
|
:ddend String to end a description
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2008-12-16 11:17:15 -05:00
|
|
|
:splice When set to t, return only list body lines, don't wrap
|
|
|
|
them into :[u/o]start and :[u/o]end. Default is nil.
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2008-12-16 11:17:15 -05:00
|
|
|
:istart String to start a list item
|
|
|
|
:iend String to end a list item
|
|
|
|
:isep String to separate items
|
2009-01-06 11:23:36 -05:00
|
|
|
:lsep String to separate sublists
|
|
|
|
|
|
|
|
:cboff String to insert for an unchecked checkbox
|
|
|
|
:cbon String to insert for a checked checkbox"
|
2008-09-23 02:03:13 -04:00
|
|
|
(interactive)
|
|
|
|
(let* ((p params) sublist
|
|
|
|
(splicep (plist-get p :splice))
|
2010-07-10 11:02:58 -04:00
|
|
|
(ostart (plist-get p :ostart))
|
|
|
|
(oend (plist-get p :oend))
|
|
|
|
(ustart (plist-get p :ustart))
|
|
|
|
(uend (plist-get p :uend))
|
|
|
|
(dstart (plist-get p :dstart))
|
|
|
|
(dend (plist-get p :dend))
|
|
|
|
(dtstart (plist-get p :dtstart))
|
|
|
|
(dtend (plist-get p :dtend))
|
|
|
|
(ddstart (plist-get p :ddstart))
|
|
|
|
(ddend (plist-get p :ddend))
|
|
|
|
(istart (plist-get p :istart))
|
|
|
|
(iend (plist-get p :iend))
|
|
|
|
(isep (plist-get p :isep))
|
|
|
|
(lsep (plist-get p :lsep))
|
|
|
|
(cbon (plist-get p :cbon))
|
2010-12-28 19:43:42 -05:00
|
|
|
(cboff (plist-get p :cboff))
|
|
|
|
(export-item
|
|
|
|
(function
|
|
|
|
;; Export an item ITEM of type TYPE. First string in item
|
|
|
|
;; is treated in a special way as it can bring extra
|
|
|
|
;; information that needs to be processed.
|
|
|
|
(lambda (item type)
|
|
|
|
(let ((fmt (if (eq type 'descriptive)
|
|
|
|
(concat (org-trim istart) "%s" ddend iend isep)
|
|
|
|
(concat istart "%s" iend isep)))
|
|
|
|
(first (car item)))
|
|
|
|
;; Replace checkbox if any is found.
|
|
|
|
(cond
|
|
|
|
((string-match "\\[CBON\\]" first)
|
|
|
|
(setq first (replace-match cbon t t first)))
|
|
|
|
((string-match "\\[CBOFF\\]" first)
|
|
|
|
(setq first (replace-match cboff t t first)))
|
|
|
|
((string-match "\\[-\\]" first)
|
|
|
|
(setq first (replace-match "$\\boxminus$" t t first))))
|
|
|
|
;; Insert descriptive term if TYPE is `descriptive'.
|
|
|
|
(when (and (eq type 'descriptive)
|
|
|
|
(string-match "^\\(.*\\)[ \t]+::" first))
|
|
|
|
(setq first (concat
|
|
|
|
dtstart (org-trim (match-string 1 first)) dtend
|
|
|
|
ddstart (org-trim (substring first (match-end 0))))))
|
|
|
|
(setcar item first)
|
|
|
|
(format fmt (mapconcat
|
|
|
|
(lambda (e)
|
|
|
|
(if (stringp e) e (funcall export-sublist e)))
|
|
|
|
item isep))))))
|
|
|
|
(export-sublist
|
|
|
|
(function
|
|
|
|
;; Export sublist SUB
|
|
|
|
(lambda (sub)
|
|
|
|
(let* ((type (car sub))
|
|
|
|
(items (cdr sub))
|
|
|
|
(fmt (cond
|
|
|
|
(splicep "%s")
|
|
|
|
((eq type 'ordered)
|
|
|
|
(concat ostart "\n%s" oend))
|
|
|
|
((eq type 'descriptive)
|
|
|
|
(concat dstart "\n%s" dend))
|
|
|
|
(t (concat ustart "\n%s" uend)))))
|
|
|
|
(format fmt (mapconcat
|
|
|
|
(lambda (e) (funcall export-item e type))
|
|
|
|
items lsep)))))))
|
|
|
|
(concat (funcall export-sublist list) "\n")))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
2009-01-06 11:23:36 -05:00
|
|
|
(defun org-list-to-latex (list &optional params)
|
|
|
|
"Convert LIST into a LaTeX list.
|
2010-07-16 17:22:01 -04:00
|
|
|
LIST is as returned by `org-list-parse-list'. PARAMS is a property list
|
2009-01-06 11:23:36 -05:00
|
|
|
with overruling parameters for `org-list-to-generic'."
|
2008-09-23 02:03:13 -04:00
|
|
|
(org-list-to-generic
|
2009-01-06 11:23:36 -05:00
|
|
|
list
|
|
|
|
(org-combine-plists
|
|
|
|
'(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
|
|
|
|
:ustart "\\begin{itemize}" :uend "\\end{itemize}"
|
|
|
|
:dstart "\\begin{description}" :dend "\\end{description}"
|
2010-12-28 19:43:42 -05:00
|
|
|
:dtstart "[" :dtend "] "
|
2009-01-06 11:23:36 -05:00
|
|
|
:ddstart "" :ddend ""
|
|
|
|
:istart "\\item " :iend ""
|
|
|
|
:isep "\n" :lsep "\n"
|
|
|
|
:cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
|
|
|
|
params)))
|
|
|
|
|
|
|
|
(defun org-list-to-html (list &optional params)
|
|
|
|
"Convert LIST into a HTML list.
|
2010-07-16 17:22:01 -04:00
|
|
|
LIST is as returned by `org-list-parse-list'. PARAMS is a property list
|
2009-01-06 11:23:36 -05:00
|
|
|
with overruling parameters for `org-list-to-generic'."
|
2008-09-23 02:03:13 -04:00
|
|
|
(org-list-to-generic
|
2009-01-06 11:23:36 -05:00
|
|
|
list
|
|
|
|
(org-combine-plists
|
|
|
|
'(:splicep nil :ostart "<ol>" :oend "</ol>"
|
|
|
|
:ustart "<ul>" :uend "</ul>"
|
|
|
|
:dstart "<dl>" :dend "</dl>"
|
|
|
|
:dtstart "<dt>" :dtend "</dt>"
|
|
|
|
:ddstart "<dd>" :ddend "</dd>"
|
|
|
|
:istart "<li>" :iend "</li>"
|
|
|
|
:isep "\n" :lsep "\n"
|
|
|
|
:cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
|
|
|
|
params)))
|
|
|
|
|
|
|
|
(defun org-list-to-texinfo (list &optional params)
|
|
|
|
"Convert LIST into a Texinfo list.
|
2010-07-16 17:22:01 -04:00
|
|
|
LIST is as returned by `org-list-parse-list'. PARAMS is a property list
|
2009-01-06 11:23:36 -05:00
|
|
|
with overruling parameters for `org-list-to-generic'."
|
2008-09-23 02:03:13 -04:00
|
|
|
(org-list-to-generic
|
2009-06-08 02:00:46 -04:00
|
|
|
list
|
2009-01-06 11:23:36 -05:00
|
|
|
(org-combine-plists
|
|
|
|
'(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
|
|
|
|
:ustart "@enumerate" :uend "@end enumerate"
|
2010-12-28 19:43:42 -05:00
|
|
|
:dstart "@table @asis" :dend "@end table"
|
|
|
|
:dtstart " " :dtend "\n"
|
2009-01-06 11:23:36 -05:00
|
|
|
:ddstart "" :ddend ""
|
|
|
|
:istart "@item\n" :iend ""
|
|
|
|
:isep "\n" :lsep "\n"
|
|
|
|
:cbon "@code{[X]}" :cboff "@code{[ ]}")
|
|
|
|
params)))
|
2008-09-23 02:03:13 -04:00
|
|
|
|
|
|
|
(provide 'org-list)
|
|
|
|
|
2008-10-15 04:20:24 -04:00
|
|
|
;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
|
2008-09-23 02:03:13 -04:00
|
|
|
;;; org-list.el ends here
|