2008-09-23 06:03:13 +00:00
|
|
|
;;; org-list.el --- Plain lists for Org-mode
|
|
|
|
;;
|
2010-04-07 14:26:10 +00:00
|
|
|
;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
|
2009-01-26 08:42:33 +00:00
|
|
|
;; Free Software Foundation, Inc.
|
2008-09-23 06:03:13 +00:00
|
|
|
;;
|
|
|
|
;; Author: Carsten Dominik <carsten at orgmode dot org>
|
2008-12-16 16:17:15 +00:00
|
|
|
;; Bastien Guerry <bzg AT altern DOT org>
|
2008-09-23 06:03:13 +00:00
|
|
|
;; Keywords: outlines, hypermedia, calendar, wp
|
|
|
|
;; Homepage: http://orgmode.org
|
2010-12-10 17:43:10 +00:00
|
|
|
;; Version: 7.5
|
2008-09-23 06:03:13 +00: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 16:17:15 +00:00
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-09-23 06:03:13 +00: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.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2010-05-13 16:02:05 +00:00
|
|
|
(eval-when-compile
|
|
|
|
(require 'cl))
|
2008-09-23 06:03:13 +00:00
|
|
|
(require 'org-macs)
|
|
|
|
(require 'org-compat)
|
|
|
|
|
|
|
|
(defvar org-blank-before-new-entry)
|
|
|
|
(defvar org-M-RET-may-split-line)
|
2009-08-03 11:28:24 +00:00
|
|
|
(defvar org-complex-heading-regexp)
|
|
|
|
(defvar org-odd-levels-only)
|
2010-09-05 15:58:30 +00:00
|
|
|
(defvar org-outline-regexp)
|
|
|
|
(defvar org-ts-regexp)
|
|
|
|
(defvar org-ts-regexp-both)
|
2008-09-23 06:03:13 +00:00
|
|
|
|
|
|
|
(declare-function org-invisible-p "org" ())
|
|
|
|
(declare-function org-on-heading-p "org" (&optional invisible-ok))
|
2008-11-08 21:00:03 +00:00
|
|
|
(declare-function outline-next-heading "outline" ())
|
2008-09-23 06:03:13 +00:00
|
|
|
(declare-function org-back-to-heading "org" (&optional invisible-ok))
|
|
|
|
(declare-function org-back-over-empty-lines "org" ())
|
|
|
|
(declare-function org-trim "org" (s))
|
|
|
|
(declare-function org-get-indentation "org" (&optional line))
|
2008-11-25 19:42:06 +00:00
|
|
|
(declare-function org-timer-item "org-timer" (&optional arg))
|
2010-08-17 20:21:24 +00:00
|
|
|
(declare-function org-timer-hms-to-secs "org-timer" (hms))
|
2009-01-06 16:23:36 +00:00
|
|
|
(declare-function org-combine-plists "org" (&rest plists))
|
2010-08-27 20:35:09 +00:00
|
|
|
(declare-function org-entry-get "org"
|
|
|
|
(pom property &optional inherit literal-nil))
|
2009-05-05 08:33:12 +00:00
|
|
|
(declare-function org-narrow-to-subtree "org" ())
|
|
|
|
(declare-function org-show-subtree "org" ())
|
2010-09-05 15:58:30 +00:00
|
|
|
(declare-function org-in-regexps-block-p "org"
|
|
|
|
(start-re end-re &optional bound))
|
|
|
|
(declare-function org-level-increment "org" ())
|
|
|
|
(declare-function org-at-heading-p "org" (&optional ignored))
|
|
|
|
(declare-function outline-previous-heading "outline" ())
|
|
|
|
(declare-function org-icompleting-read "org" (&rest args))
|
|
|
|
(declare-function org-time-string-to-seconds "org" (s))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
|
|
|
(defgroup org-plain-lists nil
|
|
|
|
"Options concerning plain lists in Org-mode."
|
|
|
|
:tag "Org Plain lists"
|
|
|
|
:group 'org-structure)
|
|
|
|
|
2009-07-01 07:34:13 +00: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 06:03:13 +00:00
|
|
|
:group 'org-plain-lists
|
2009-07-01 07:34:13 +00:00
|
|
|
:type '(choice
|
|
|
|
(const :tag "Never" nil)
|
2009-07-01 07:42:24 +00:00
|
|
|
(const :tag "With cursor in plain list (recommended)" t)
|
|
|
|
(const :tag "As children of outline headings" integrate)))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2009-07-06 06:53:50 +00: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 05:11:01 +00: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 06:53:50 +00: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 06:03:13 +00: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 05:54:46 +00: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 22:45:34 +00:00
|
|
|
When a string, it will be used as a regular expression. When the
|
2010-07-10 15:02:58 +00: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 21:00:22 +00:00
|
|
|
spaces instead of one after the bullet in each item of the list."
|
2010-04-21 07:51:30 +00:00
|
|
|
:group 'org-plain-lists
|
2008-11-21 05:54:46 +00:00
|
|
|
:type '(choice
|
|
|
|
(const :tag "never" nil)
|
|
|
|
(regexp)))
|
|
|
|
|
2010-08-22 17:34:46 +00:00
|
|
|
(defcustom org-list-ending-method 'both
|
2010-08-19 14:09:45 +00:00
|
|
|
"Determine where plain lists should end.
|
2010-08-20 09:35:56 +00:00
|
|
|
Valid values are: `regexp', `indent' or `both'.
|
2010-08-19 14:09:45 +00:00
|
|
|
|
2010-08-20 09:35:56 +00:00
|
|
|
When set to `regexp', Org will look into two variables,
|
2010-08-19 14:09:45 +00:00
|
|
|
`org-empty-line-terminates-plain-lists' and the more general
|
2010-08-23 11:32:00 +00:00
|
|
|
`org-list-end-regexp', to determine what will end lists. This is
|
|
|
|
the fastest method.
|
2010-08-19 14:09:45 +00:00
|
|
|
|
2010-08-22 17:34:46 +00: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
|
|
|
|
it.
|
2010-08-19 14:09:45 +00:00
|
|
|
|
2010-08-23 11:32:00 +00:00
|
|
|
When set to `both', each of the preceding methods is applied to
|
|
|
|
determine lists endings. This is the default method."
|
2010-08-19 14:09:45 +00:00
|
|
|
:group 'org-plain-lists
|
|
|
|
:type '(choice
|
2010-08-23 11:32:00 +00:00
|
|
|
(const :tag "With a regexp defining ending" regexp)
|
|
|
|
(const :tag "With indentation of regular (no bullet) text" indent)
|
2010-08-19 14:09:45 +00:00
|
|
|
(const :tag "With both methods" both)))
|
|
|
|
|
2008-09-23 06:03:13 +00:00
|
|
|
(defcustom org-empty-line-terminates-plain-lists nil
|
2010-01-21 15:15:40 +00:00
|
|
|
"Non-nil means an empty line ends all plain list levels.
|
2010-08-19 14:09:45 +00:00
|
|
|
This variable only makes sense if `org-list-ending-method' is set
|
2010-08-28 10:53:19 +00:00
|
|
|
to `regexp' or `both'. This is then equivalent to set
|
2010-08-19 23:58:46 +00:00
|
|
|
`org-list-end-regexp' to \"^[ \\t]*$\"."
|
2008-09-23 06:03:13 +00:00
|
|
|
:group 'org-plain-lists
|
|
|
|
:type 'boolean)
|
|
|
|
|
2010-07-14 07:31:12 +00:00
|
|
|
(defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
|
2010-07-13 13:52:11 +00:00
|
|
|
"Regexp matching the end of all plain list levels.
|
2010-07-30 22:45:34 +00:00
|
|
|
It must start with \"^\" and end with \"\\n\". It defaults to 2
|
2010-07-14 07:31:12 +00:00
|
|
|
blank lines. `org-empty-line-terminates-plain-lists' has
|
2010-07-13 13:52:11 +00:00
|
|
|
precedence over it."
|
|
|
|
:group 'org-plain-lists
|
|
|
|
:type 'string)
|
|
|
|
|
2010-07-23 18:36:42 +00:00
|
|
|
(defcustom org-list-automatic-rules '((bullet . t)
|
|
|
|
(checkbox . t)
|
|
|
|
(indent . t)
|
2010-08-20 09:35:56 +00:00
|
|
|
(insert . t))
|
2010-07-23 18:36:42 +00:00
|
|
|
"Non-nil means apply set of rules when acting on lists.
|
|
|
|
By default, automatic actions are taken when using
|
2010-09-14 16:30:03 +00: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 18:36:42 +00: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 14:51:28 +00:00
|
|
|
It also prevents from inserting a checkbox in a
|
|
|
|
description item.
|
2010-08-20 09:35:56 +00: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
|
|
|
|
change that bullet to -
|
2010-07-23 18:36:42 +00:00
|
|
|
insert when non-nil, trying to insert an item inside a block
|
|
|
|
will insert it right before the block instead of
|
2010-08-20 09:35:56 +00:00
|
|
|
throwing an error."
|
2010-07-23 18:36:42 +00: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 09:35:56 +00:00
|
|
|
(const :tag "Insert" insert))
|
2010-07-23 18:36:42 +00:00
|
|
|
:value-type
|
|
|
|
(boolean :tag "Activate" :value t)))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2009-05-08 15:34:40 +00:00
|
|
|
(defcustom org-hierarchical-checkbox-statistics t
|
2010-01-21 15:15:40 +00:00
|
|
|
"Non-nil means checkbox statistics counts only the state of direct children.
|
2009-08-18 14:43:55 +00:00
|
|
|
When nil, all boxes below the cookie are counted.
|
2009-11-12 12:39:29 +00:00
|
|
|
This can be set to nil on a per-node basis using a COOKIE_DATA property
|
2009-08-18 14:43:55 +00:00
|
|
|
with the word \"recursive\" in the value."
|
2009-05-08 15:34:40 +00:00
|
|
|
:group 'org-plain-lists
|
|
|
|
:type 'boolean)
|
|
|
|
|
2008-09-23 06:03:13 +00: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 12:44:51 +00:00
|
|
|
-
|
2008-09-23 06:03:13 +00: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 12:44:51 +00:00
|
|
|
-
|
2008-09-23 06:03:13 +00:00
|
|
|
@end ignore\n")
|
|
|
|
(html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
|
|
|
|
<!-- END RECEIVE ORGLST %n -->
|
|
|
|
<!--
|
|
|
|
#+ORGLST: SEND %n org-list-to-html
|
2010-07-12 12:44:51 +00:00
|
|
|
-
|
2008-09-23 06:03:13 +00: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"))))
|
|
|
|
|
2010-07-10 15:02:58 +00:00
|
|
|
;;; Internal functions
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-07-10 15:02:58 +00: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 13:52:11 +00:00
|
|
|
"^[ \t]*\n"
|
|
|
|
org-list-end-regexp))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-04-21 13:52:01 +00: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 18:52:30 +00:00
|
|
|
"\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
|
2010-04-21 13:52:01 +00:00
|
|
|
((= org-plain-list-ordered-item-terminator ?.)
|
2010-07-27 18:52:30 +00:00
|
|
|
"\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
|
2010-04-21 13:52:01 +00:00
|
|
|
((= org-plain-list-ordered-item-terminator ?\))
|
2010-07-27 18:52:30 +00:00
|
|
|
"\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
|
2010-04-21 13:52:01 +00:00
|
|
|
(t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
|
|
|
|
|
2010-07-20 19:13:37 +00:00
|
|
|
(defconst org-item-beginning-re (concat "^" (org-item-re))
|
|
|
|
"Regexp matching the beginning of a plain list item.")
|
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-list-ending-between (min max &optional firstp)
|
|
|
|
"Find the position of a list ending between MIN and MAX, or nil.
|
2010-07-27 22:05:42 +00:00
|
|
|
This function looks for `org-list-end-re' outside a block.
|
2010-07-10 15:02:58 +00:00
|
|
|
|
|
|
|
If FIRSTP in non-nil, return the point at the beginning of the
|
2010-08-20 09:35:56 +00:00
|
|
|
nearest valid terminator from MIN. Otherwise, return the point at
|
|
|
|
the end of the nearest terminator from MAX."
|
2010-07-10 15:02:58 +00:00
|
|
|
(save-excursion
|
|
|
|
(let* ((start (if firstp min max))
|
|
|
|
(end (if firstp max min))
|
|
|
|
(search-fun (if firstp
|
|
|
|
#'org-search-forward-unenclosed
|
|
|
|
#'org-search-backward-unenclosed))
|
|
|
|
(list-end-p (progn
|
|
|
|
(goto-char start)
|
2010-07-17 09:27:58 +00:00
|
|
|
(funcall search-fun (org-list-end-re) end t))))
|
2010-08-28 10:53:19 +00:00
|
|
|
;; Is there a valid list ending somewhere ?
|
2010-07-10 15:02:58 +00:00
|
|
|
(and list-end-p
|
|
|
|
;; we want to be on the first line of the list ender
|
|
|
|
(match-beginning 0)))))
|
|
|
|
|
2010-08-19 14:09:45 +00:00
|
|
|
(defun org-list-maybe-skip-block (search limit)
|
|
|
|
"Return non-nil value if point is in a block, skipping it on the way.
|
2010-08-21 11:32:33 +00:00
|
|
|
It looks for the boundary of the block in SEARCH direction,
|
|
|
|
stopping at LIMIT."
|
2010-08-19 14:09:45 +00:00
|
|
|
(save-match-data
|
|
|
|
(let ((case-fold-search t)
|
|
|
|
(boundary (if (eq search 're-search-forward) 3 5)))
|
|
|
|
(when (save-excursion
|
|
|
|
(and (funcall search "^[ \t]*#\\+\\(begin\\|end\\)_" limit t)
|
|
|
|
(= (length (match-string 1)) boundary)))
|
|
|
|
;; We're in a block: get out of it
|
|
|
|
(goto-char (match-beginning 0))))))
|
|
|
|
|
|
|
|
(defun org-list-search-unenclosed-generic (search re bound noerr)
|
2010-08-21 11:32:33 +00:00
|
|
|
"Search a string outside blocks and protected places.
|
|
|
|
Arguments SEARCH, RE, BOUND and NOERR are similar to those in
|
|
|
|
`search-forward', `search-backward', `re-search-forward' and
|
|
|
|
`re-search-backward'."
|
2010-08-19 14:09:45 +00:00
|
|
|
(catch 'exit
|
|
|
|
(let ((origin (point)))
|
|
|
|
(while t
|
2010-08-19 23:58:46 +00:00
|
|
|
;; 1. No match: return to origin or bound, depending on NOERR.
|
2010-08-19 14:09:45 +00:00
|
|
|
(unless (funcall search re bound noerr)
|
2010-10-17 16:35:02 +00:00
|
|
|
(throw 'exit (and (goto-char (if (memq noerr '(t nil)) origin bound))
|
2010-08-19 14:09:45 +00:00
|
|
|
nil)))
|
2010-08-19 23:58:46 +00:00
|
|
|
;; 2. Match not in block or protected: return point. Else
|
|
|
|
;; skip the block and carry on.
|
2010-08-19 14:09:45 +00:00
|
|
|
(unless (or (get-text-property (match-beginning 0) 'org-protected)
|
|
|
|
(org-list-maybe-skip-block search bound))
|
|
|
|
(throw 'exit (point)))))))
|
2010-07-12 08:48:17 +00:00
|
|
|
|
2010-07-17 09:27:58 +00:00
|
|
|
(defun org-search-backward-unenclosed (regexp &optional bound noerror)
|
2010-08-21 11:32:33 +00:00
|
|
|
"Like `re-search-backward' but don't stop inside blocks or protected places.
|
|
|
|
Arguments REGEXP, BOUND and NOERROR are similar to those used in
|
|
|
|
`re-search-backward'."
|
2010-08-13 08:52:27 +00:00
|
|
|
(org-list-search-unenclosed-generic
|
2010-08-19 14:09:45 +00:00
|
|
|
#'re-search-backward regexp (or bound (point-min)) noerror))
|
2010-07-12 08:48:17 +00:00
|
|
|
|
2010-07-17 09:27:58 +00:00
|
|
|
(defun org-search-forward-unenclosed (regexp &optional bound noerror)
|
2010-08-21 11:32:33 +00:00
|
|
|
"Like `re-search-forward' but don't stop inside blocks or protected places.
|
|
|
|
Arguments REGEXP, BOUND and NOERROR are similar to those used in
|
|
|
|
`re-search-forward'."
|
2010-08-13 08:52:27 +00:00
|
|
|
(org-list-search-unenclosed-generic
|
2010-08-19 14:09:45 +00:00
|
|
|
#'re-search-forward regexp (or bound (point-max)) noerror))
|
|
|
|
|
|
|
|
(defun org-list-in-item-p-with-indent (limit)
|
|
|
|
"Is the cursor inside a plain list?
|
|
|
|
Plain lists are considered ending when a non-blank line is less
|
2010-08-28 10:53:19 +00:00
|
|
|
indented than the previous item within LIMIT."
|
2010-08-19 14:09:45 +00:00
|
|
|
(save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(beginning-of-line)
|
2010-08-21 02:32:48 +00:00
|
|
|
(cond
|
|
|
|
;; do not start searching inside a block...
|
|
|
|
((org-list-maybe-skip-block #'re-search-backward limit))
|
|
|
|
;; ... or at a blank line
|
|
|
|
((looking-at "^[ \t]*$")
|
2010-08-19 14:09:45 +00:00
|
|
|
(skip-chars-backward " \r\t\n")
|
2010-08-21 02:32:48 +00:00
|
|
|
(beginning-of-line)))
|
|
|
|
(beginning-of-line)
|
2010-08-28 10:53:19 +00:00
|
|
|
(or (org-at-item-p)
|
2010-08-21 13:23:58 +00:00
|
|
|
(let* ((case-fold-search t)
|
|
|
|
(ind-ref (org-get-indentation))
|
2010-08-28 10:53:19 +00:00
|
|
|
;; Ensure there is at least an item above
|
2010-08-21 13:23:58 +00:00
|
|
|
(up-item-p (save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-search-backward-unenclosed
|
|
|
|
org-item-beginning-re limit t))))
|
2010-08-21 13:23:58 +00:00
|
|
|
(and up-item-p
|
2010-08-21 02:32:48 +00:00
|
|
|
(catch 'exit
|
|
|
|
(while t
|
2010-08-21 13:23:58 +00:00
|
|
|
(cond
|
2010-08-28 10:53:19 +00:00
|
|
|
((org-at-item-p)
|
|
|
|
(throw 'exit (< (org-get-indentation) ind-ref)))
|
2010-08-21 13:23:58 +00:00
|
|
|
((looking-at "^[ \t]*$")
|
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(beginning-of-line))
|
|
|
|
((looking-at "^[ \t]*#\\+end_")
|
|
|
|
(re-search-backward "^[ \t]*#\\+begin_"))
|
|
|
|
(t
|
|
|
|
(setq ind-ref (min (org-get-indentation) ind-ref))
|
|
|
|
(forward-line -1))))))))))
|
2010-08-19 14:09:45 +00:00
|
|
|
|
|
|
|
(defun org-list-in-item-p-with-regexp (limit)
|
|
|
|
"Is the cursor inside a plain list?
|
|
|
|
Plain lists end when `org-list-end-regexp' is matched, or at a
|
2010-08-21 11:32:33 +00:00
|
|
|
blank line if `org-empty-line-terminates-plain-lists' is true.
|
|
|
|
|
|
|
|
Argument LIMIT specifies the upper-bound of the search."
|
2010-08-19 14:09:45 +00:00
|
|
|
(save-excursion
|
|
|
|
(let* ((actual-pos (goto-char (point-at-eol)))
|
|
|
|
;; Moved to eol so current line can be matched by
|
|
|
|
;; `org-item-re'.
|
|
|
|
(last-item-start (save-excursion
|
|
|
|
(org-search-backward-unenclosed
|
|
|
|
org-item-beginning-re limit t)))
|
2010-08-28 10:53:19 +00:00
|
|
|
(list-ender (org-list-ending-between
|
2010-08-19 14:09:45 +00:00
|
|
|
last-item-start actual-pos)))
|
|
|
|
;; We are in a list when we are on an item line or when we can
|
|
|
|
;; find an item before point and there is no valid list ender
|
|
|
|
;; between it and the point.
|
2010-08-28 10:53:19 +00:00
|
|
|
(and last-item-start (not list-ender)))))
|
2010-08-19 14:09:45 +00:00
|
|
|
|
|
|
|
(defun org-list-top-point-with-regexp (limit)
|
2010-08-28 10:53:19 +00:00
|
|
|
"Return point at the top level item in a list.
|
2010-08-21 11:32:33 +00:00
|
|
|
Argument LIMIT specifies the upper-bound of the search.
|
|
|
|
|
2010-08-19 14:09:45 +00:00
|
|
|
List ending is determined by regexp. See
|
|
|
|
`org-list-ending-method'. for more information."
|
|
|
|
(save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(let ((pos (point-at-eol)))
|
|
|
|
;; Is there some list above this one ? If so, go to its ending.
|
|
|
|
;; Otherwise, go back to the heading above or bob.
|
|
|
|
(goto-char (or (org-list-ending-between limit pos) limit))
|
|
|
|
;; From there, search down our list.
|
|
|
|
(org-search-forward-unenclosed org-item-beginning-re pos t)
|
|
|
|
(point-at-bol))))
|
2010-08-19 14:09:45 +00:00
|
|
|
|
|
|
|
(defun org-list-bottom-point-with-regexp (limit)
|
2010-08-28 10:53:19 +00:00
|
|
|
"Return point just before list ending.
|
2010-08-21 11:32:33 +00:00
|
|
|
Argument LIMIT specifies the lower-bound of the search.
|
|
|
|
|
2010-08-19 14:09:45 +00:00
|
|
|
List ending is determined by regexp. See
|
|
|
|
`org-list-ending-method'. for more information."
|
|
|
|
(save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(let ((pos (org-get-item-beginning)))
|
|
|
|
;; The list ending is either first point matching
|
|
|
|
;; `org-list-end-re', point at first white-line before next
|
|
|
|
;; heading, or eob.
|
|
|
|
(or (org-list-ending-between (min pos limit) limit t) limit))))
|
2010-08-19 14:09:45 +00:00
|
|
|
|
|
|
|
(defun org-list-top-point-with-indent (limit)
|
2010-08-28 10:53:19 +00:00
|
|
|
"Return point at the top level in a list.
|
2010-08-21 11:32:33 +00:00
|
|
|
Argument LIMIT specifies the upper-bound of the search.
|
|
|
|
|
2010-08-19 14:09:45 +00:00
|
|
|
List ending is determined by indentation of text. See
|
|
|
|
`org-list-ending-method'. for more information."
|
|
|
|
(save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(let ((case-fold-search t))
|
|
|
|
(let ((item-ref (goto-char (org-get-item-beginning)))
|
|
|
|
(ind-ref 10000))
|
|
|
|
(forward-line -1)
|
|
|
|
(catch 'exit
|
|
|
|
(while t
|
2010-12-09 08:54:42 +00:00
|
|
|
(let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
|
|
|
|
(org-get-indentation))))
|
2010-08-28 10:53:19 +00:00
|
|
|
(cond
|
|
|
|
((looking-at "^[ \t]*:END:")
|
|
|
|
(throw 'exit item-ref))
|
|
|
|
((<= (point) limit)
|
|
|
|
(throw 'exit
|
|
|
|
(if (and (org-at-item-p) (< ind ind-ref))
|
|
|
|
(point-at-bol)
|
|
|
|
item-ref)))
|
|
|
|
((looking-at "^[ \t]*$")
|
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(beginning-of-line))
|
|
|
|
((looking-at "^[ \t]*#\\+end_")
|
|
|
|
(re-search-backward "^[ \t]*#\\+begin_"))
|
|
|
|
((not (org-at-item-p))
|
|
|
|
(setq ind-ref (min ind ind-ref))
|
|
|
|
(forward-line -1))
|
|
|
|
((>= ind ind-ref)
|
|
|
|
(throw 'exit item-ref))
|
|
|
|
(t
|
|
|
|
(setq item-ref (point-at-bol) ind-ref 10000)
|
|
|
|
(forward-line -1))))))))))
|
2010-08-19 14:09:45 +00:00
|
|
|
|
|
|
|
(defun org-list-bottom-point-with-indent (limit)
|
|
|
|
"Return point just before list ending or nil if not in a list.
|
2010-08-21 11:32:33 +00:00
|
|
|
Argument LIMIT specifies the lower-bound of the search.
|
|
|
|
|
2010-08-19 14:09:45 +00:00
|
|
|
List ending is determined by the indentation of text. See
|
|
|
|
`org-list-ending-method' for more information."
|
|
|
|
(save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(let ((ind-ref (progn
|
|
|
|
(goto-char (org-get-item-beginning))
|
|
|
|
(org-get-indentation)))
|
|
|
|
(case-fold-search t))
|
2010-08-21 02:32:48 +00:00
|
|
|
;; do not start inside a block
|
|
|
|
(org-list-maybe-skip-block #'re-search-forward limit)
|
|
|
|
(beginning-of-line)
|
2010-08-28 10:53:19 +00:00
|
|
|
(catch 'exit
|
|
|
|
(while t
|
2010-10-30 12:26:40 +00:00
|
|
|
(skip-chars-forward " \t")
|
2010-12-09 08:54:42 +00:00
|
|
|
(let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
|
|
|
|
(org-get-indentation))))
|
2010-08-28 10:53:19 +00:00
|
|
|
(cond
|
|
|
|
((or (>= (point) limit)
|
|
|
|
(looking-at ":END:"))
|
|
|
|
(throw 'exit (progn
|
|
|
|
;; Ensure bottom is just after a
|
|
|
|
;; non-blank line.
|
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(min (point-max) (1+ (point-at-eol))))))
|
|
|
|
((= (point) (point-at-eol))
|
|
|
|
(skip-chars-forward " \r\t\n")
|
|
|
|
(beginning-of-line))
|
|
|
|
((org-at-item-p)
|
2010-09-18 17:39:04 +00:00
|
|
|
(setq ind-ref ind)
|
2010-08-28 10:53:19 +00:00
|
|
|
(forward-line 1))
|
|
|
|
((<= ind ind-ref)
|
2010-11-28 15:51:14 +00:00
|
|
|
(throw 'exit (progn
|
|
|
|
;; Again, ensure bottom is just after a
|
|
|
|
;; non-blank line.
|
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(min (point-max) (1+ (point-at-eol))))))
|
2010-08-28 10:53:19 +00:00
|
|
|
((looking-at "#\\+begin_")
|
|
|
|
(re-search-forward "[ \t]*#\\+end_")
|
|
|
|
(forward-line 1))
|
|
|
|
(t (forward-line 1)))))))))
|
2010-07-10 15:02:58 +00:00
|
|
|
|
2010-07-19 09:21:22 +00: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 13:53:37 +00:00
|
|
|
;; Ignore counter if any
|
2010-08-20 11:05:38 +00:00
|
|
|
(when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
|
2010-07-28 13:53:37 +00:00
|
|
|
(goto-char (match-end 0)))
|
2010-07-19 09:21:22 +00:00
|
|
|
(looking-at regexp))))
|
|
|
|
|
2010-07-19 09:33:20 +00:00
|
|
|
(defun org-list-get-item-same-level (search-fun pos limit pre-move)
|
2010-07-10 15:02:58 +00:00
|
|
|
"Return point at the beginning of next item at the same level.
|
|
|
|
Search items using function SEARCH-FUN, from POS to LIMIT. It
|
2010-07-20 13:58:48 +00:00
|
|
|
uses PRE-MOVE before search. Return nil if no item was found."
|
2010-07-10 15:02:58 +00:00
|
|
|
(save-excursion
|
2010-07-20 13:58:48 +00:00
|
|
|
(goto-char pos)
|
2010-08-28 10:53:19 +00:00
|
|
|
(let* ((start (org-get-item-beginning))
|
|
|
|
(ind (progn (goto-char start) (org-get-indentation))))
|
2010-07-20 13:58:48 +00:00
|
|
|
;; We don't want to match the current line.
|
2010-07-10 15:02:58 +00:00
|
|
|
(funcall pre-move)
|
2010-07-16 09:43:40 +00:00
|
|
|
;; Skip any sublist on the way
|
2010-07-20 19:13:37 +00:00
|
|
|
(while (and (funcall search-fun org-item-beginning-re limit t)
|
2010-07-20 13:58:48 +00:00
|
|
|
(> (org-get-indentation) ind)))
|
2010-07-10 15:02:58 +00:00
|
|
|
(when (and (/= (point-at-bol) start) ; Have we moved ?
|
|
|
|
(= (org-get-indentation) ind))
|
|
|
|
(point-at-bol)))))
|
|
|
|
|
2010-08-30 06:36:13 +00:00
|
|
|
(defun org-list-separating-blank-lines-number (pos top bottom)
|
2010-08-28 10:53:19 +00:00
|
|
|
"Return number of blank lines that should separate items in list.
|
2010-08-30 06:36:13 +00:00
|
|
|
POS is the position of point to be considered.
|
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
TOP and BOTTOM are respectively position of list beginning and
|
|
|
|
list ending.
|
|
|
|
|
|
|
|
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.
|
|
|
|
(t (let ((next-p (org-get-next-item (point) bottom)))
|
|
|
|
(cond
|
|
|
|
;; Is there a next item?
|
|
|
|
(next-p (goto-char next-p)
|
|
|
|
(org-back-over-empty-lines))
|
|
|
|
;; Is there a previous item?
|
|
|
|
((org-get-previous-item (point) top)
|
|
|
|
(org-back-over-empty-lines))
|
|
|
|
;; User inserted blank lines, trust him
|
2010-08-30 06:36:13 +00:00
|
|
|
((and (> pos (org-end-of-item-before-blank bottom))
|
2010-08-28 10:53:19 +00:00
|
|
|
(> (save-excursion
|
2010-08-30 06:36:13 +00:00
|
|
|
(goto-char pos)
|
2010-08-28 10:53:19 +00: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
|
|
|
|
(org-search-forward-unenclosed
|
|
|
|
"^[ \t]*$" (org-end-of-item-before-blank bottom) t)) 1)
|
|
|
|
;; No parent: no blank line.
|
|
|
|
(t 0))))))))
|
|
|
|
|
2010-07-19 09:33:20 +00:00
|
|
|
(defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
|
|
|
|
"Insert a new list item at POS.
|
2010-07-15 16:28:01 +00: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 09:04:15 +00:00
|
|
|
function ends."
|
2010-07-15 16:28:01 +00:00
|
|
|
(goto-char pos)
|
2010-07-23 18:36:42 +00:00
|
|
|
;; Is point in a special block?
|
2010-07-15 16:28:01 +00:00
|
|
|
(when (org-in-regexps-block-p
|
|
|
|
"^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
|
|
|
|
'(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
|
2010-07-23 18:36:42 +00:00
|
|
|
(if (not (cdr (assq 'insert org-list-automatic-rules)))
|
2010-07-27 22:05:42 +00:00
|
|
|
;; Rule in `org-list-automatic-rules' forbids insertion.
|
2010-08-21 11:32:33 +00:00
|
|
|
(error "Cannot insert item inside a block")
|
2010-07-23 18:36:42 +00:00
|
|
|
;; Else, move before it prior to add a new item.
|
|
|
|
(end-of-line)
|
|
|
|
(re-search-backward "^[ \t]*#\\+\\(begin\\|BEGIN\\)_" nil t)
|
|
|
|
(end-of-line 0)))
|
2010-07-15 16:28:01 +00:00
|
|
|
(let* ((true-pos (point))
|
2010-08-28 10:53:19 +00:00
|
|
|
(top (org-list-top-point))
|
|
|
|
(bottom (copy-marker (org-list-bottom-point)))
|
|
|
|
(bullet (and (goto-char (org-get-item-beginning))
|
2010-07-27 18:52:30 +00:00
|
|
|
(org-list-bullet-string (org-get-bullet))))
|
|
|
|
(ind (org-get-indentation))
|
2010-07-15 16:28:01 +00:00
|
|
|
(before-p (progn
|
2010-07-19 09:21:22 +00:00
|
|
|
;; Description item: text starts after colons.
|
2010-07-27 22:05:42 +00:00
|
|
|
(or (org-at-item-description-p)
|
2010-07-16 09:43:40 +00:00
|
|
|
;; At a checkbox: text starts after it.
|
2010-07-15 16:28:01 +00:00
|
|
|
(org-at-item-checkbox-p)
|
2010-07-16 09:43:40 +00:00
|
|
|
;; Otherwise, text starts after bullet.
|
2010-07-15 16:28:01 +00:00
|
|
|
(org-at-item-p))
|
2010-07-17 08:05:05 +00:00
|
|
|
(<= true-pos (match-end 0))))
|
2010-08-30 06:36:13 +00:00
|
|
|
(blank-lines-nb (org-list-separating-blank-lines-number
|
|
|
|
true-pos top bottom))
|
2010-07-15 16:28:01 +00:00
|
|
|
(insert-fun
|
2010-07-17 21:00:22 +00:00
|
|
|
(lambda (text)
|
2010-07-16 09:43:40 +00:00
|
|
|
;; insert bullet above item in order to avoid bothering
|
|
|
|
;; with possible blank lines ending last item.
|
2010-08-28 10:53:19 +00:00
|
|
|
(goto-char (org-get-item-beginning))
|
2010-11-14 07:58:41 +00:00
|
|
|
(org-indent-to-column ind)
|
2010-07-20 13:58:48 +00:00
|
|
|
(insert (concat bullet (when checkbox "[ ] ") after-bullet))
|
2010-07-17 08:05:05 +00:00
|
|
|
;; Stay between after-bullet and before text.
|
2010-07-15 16:28:01 +00:00
|
|
|
(save-excursion
|
|
|
|
(insert (concat text (make-string (1+ blank-lines-nb) ?\n))))
|
2010-08-28 10:53:19 +00:00
|
|
|
(unless before-p
|
|
|
|
;; store bottom: exchanging items doesn't change list
|
|
|
|
;; bottom point but will modify marker anyway
|
|
|
|
(setq bottom (marker-position bottom))
|
|
|
|
(let ((col (current-column)))
|
|
|
|
(org-list-exchange-items
|
|
|
|
(org-get-item-beginning) (org-get-next-item (point) bottom)
|
|
|
|
bottom)
|
|
|
|
;; recompute next-item: last sexp modified list
|
|
|
|
(goto-char (org-get-next-item (point) bottom))
|
|
|
|
(org-move-to-column col)))
|
2010-09-07 16:55:46 +00:00
|
|
|
;; checkbox update might modify bottom point, so use a
|
|
|
|
;; marker here
|
|
|
|
(setq bottom (copy-marker bottom))
|
2010-08-28 10:53:19 +00:00
|
|
|
(when checkbox (org-update-checkbox-count-maybe))
|
|
|
|
(org-list-repair nil top bottom))))
|
2010-07-15 16:28:01 +00:00
|
|
|
(goto-char true-pos)
|
|
|
|
(cond
|
2010-08-28 10:53:19 +00:00
|
|
|
(before-p (funcall insert-fun nil) t)
|
2010-07-16 09:43:40 +00:00
|
|
|
;; Can't split item: insert bullet at the end of item.
|
2010-07-15 16:28:01 +00:00
|
|
|
((not (org-get-alist-option org-M-RET-may-split-line 'item))
|
2010-07-17 21:00:22 +00:00
|
|
|
(funcall insert-fun nil) t)
|
2010-07-15 16:28:01 +00:00
|
|
|
;; else, insert a new bullet along with everything from point
|
2010-07-16 09:43:40 +00:00
|
|
|
;; down to last non-blank line of item.
|
2010-07-15 16:28:01 +00:00
|
|
|
(t
|
|
|
|
(delete-horizontal-space)
|
2010-07-16 09:43:40 +00:00
|
|
|
;; Get pos again in case previous command modified line.
|
2010-07-15 16:28:01 +00:00
|
|
|
(let* ((pos (point))
|
2010-08-28 10:53:19 +00:00
|
|
|
(end-before-blank (org-end-of-item-before-blank bottom))
|
2010-07-17 21:00:22 +00:00
|
|
|
(after-text
|
|
|
|
(when (< pos end-before-blank)
|
|
|
|
(prog1
|
2010-07-25 09:27:28 +00:00
|
|
|
(delete-and-extract-region pos end-before-blank)
|
2010-07-17 21:00:22 +00:00
|
|
|
;; delete any blank line at and before point.
|
|
|
|
(beginning-of-line)
|
|
|
|
(while (looking-at "^[ \t]*$")
|
|
|
|
(delete-region (point-at-bol) (1+ (point-at-eol)))
|
2010-07-18 09:04:15 +00:00
|
|
|
(beginning-of-line 0))))))
|
2010-07-15 16:28:01 +00:00
|
|
|
(funcall insert-fun after-text) t)))))
|
|
|
|
|
2010-08-21 23:10:58 +00:00
|
|
|
(defvar org-last-indent-begin-marker (make-marker))
|
|
|
|
(defvar org-last-indent-end-marker (make-marker))
|
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-list-indent-item-generic (arg no-subtree top bottom)
|
2010-08-21 23:10:58 +00: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 09:24:57 +00:00
|
|
|
If a region is active, all items inside will be moved.
|
2010-08-22 18:01:33 +00:00
|
|
|
|
|
|
|
If NO-SUBTREE is non-nil, only indent the item itself, not its
|
2010-08-21 23:10:58 +00:00
|
|
|
children.
|
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
TOP and BOTTOM are respectively position at item beginning and at
|
|
|
|
item ending.
|
|
|
|
|
2010-08-21 23:10:58 +00:00
|
|
|
Return t if successful."
|
2010-08-23 09:47:52 +00:00
|
|
|
(let* ((regionp (org-region-active-p))
|
|
|
|
(rbeg (and regionp (region-beginning)))
|
|
|
|
(rend (and regionp (region-end))))
|
|
|
|
(cond
|
|
|
|
((and regionp
|
|
|
|
(goto-char rbeg)
|
|
|
|
(not (org-search-forward-unenclosed org-item-beginning-re rend t)))
|
|
|
|
(error "No item in region"))
|
|
|
|
((not (org-at-item-p))
|
|
|
|
(error "Not on an item"))
|
|
|
|
(t
|
|
|
|
;; Are we going to move the whole list?
|
2010-08-28 10:53:19 +00:00
|
|
|
(let* ((specialp (and (cdr (assq 'indent org-list-automatic-rules))
|
2010-08-23 09:47:52 +00:00
|
|
|
(not no-subtree)
|
|
|
|
(= top (point-at-bol)))))
|
|
|
|
;; Determine begin and end points of zone to indent. If moving
|
|
|
|
;; more than one item, ensure we keep them on 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-at-bol))
|
|
|
|
(set-marker org-last-indent-end-marker
|
|
|
|
(save-excursion
|
|
|
|
(cond
|
2010-08-28 10:53:19 +00:00
|
|
|
(specialp bottom)
|
|
|
|
(no-subtree (org-end-of-item-or-at-child bottom))
|
|
|
|
(t (org-get-end-of-item bottom)))))))
|
2010-08-23 09:47:52 +00:00
|
|
|
;; Get everything ready
|
|
|
|
(let* ((beg (marker-position org-last-indent-begin-marker))
|
|
|
|
(end (marker-position org-last-indent-end-marker))
|
2010-08-28 10:53:19 +00:00
|
|
|
(struct (org-list-struct
|
|
|
|
beg end top (if specialp end bottom) (< arg 0)))
|
2010-08-23 09:47:52 +00:00
|
|
|
(origins (org-list-struct-origins struct))
|
|
|
|
(beg-item (assq beg struct)))
|
|
|
|
(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 (nth 1 beg-item)))
|
|
|
|
(if (< (+ top-ind offset) 0)
|
|
|
|
(error "Cannot outdent beyond margin")
|
|
|
|
;; Change bullet if necessary
|
|
|
|
(when (and (= (+ top-ind offset) 0)
|
|
|
|
(string-match "*" (nth 2 beg-item)))
|
|
|
|
(setcdr beg-item (list (nth 1 beg-item)
|
|
|
|
(org-list-bullet-string "-"))))
|
|
|
|
;; Shift ancestor
|
|
|
|
(let ((anc (car struct)))
|
|
|
|
(setcdr anc (list (+ (nth 1 anc) offset) "" nil)))
|
|
|
|
(org-list-struct-fix-struct struct origins)
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-list-struct-apply-struct struct end))))
|
2010-08-23 09:47:52 +00:00
|
|
|
;; Forbidden move
|
|
|
|
((and (< arg 0)
|
|
|
|
(or (and no-subtree
|
|
|
|
(not regionp)
|
|
|
|
(org-list-struct-get-child beg-item struct))
|
|
|
|
(let ((last-item (save-excursion
|
|
|
|
(goto-char end)
|
|
|
|
(skip-chars-backward " \r\t\n")
|
2010-08-28 10:53:19 +00:00
|
|
|
(goto-char (org-get-item-beginning))
|
2010-08-23 09:47:52 +00:00
|
|
|
(org-list-struct-assoc-at-point))))
|
|
|
|
(org-list-struct-get-child last-item struct))))
|
|
|
|
(error "Cannot outdent an item without its children"))
|
|
|
|
;; Normal shifting
|
|
|
|
(t
|
|
|
|
(let* ((shifted-ori (if (< arg 0)
|
|
|
|
(org-list-struct-outdent beg end origins)
|
|
|
|
(org-list-struct-indent beg end origins struct))))
|
|
|
|
(org-list-struct-fix-struct struct shifted-ori)
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-list-struct-apply-struct struct bottom))))))))))
|
2010-08-21 23:10:58 +00:00
|
|
|
|
2010-07-10 15:02:58 +00:00
|
|
|
;;; Predicates
|
|
|
|
|
|
|
|
(defun org-in-item-p ()
|
2010-08-19 14:09:45 +00:00
|
|
|
"Is the cursor inside a plain list?
|
|
|
|
This checks `org-list-ending-method'."
|
2010-07-20 13:58:48 +00:00
|
|
|
(unless (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
|
2010-08-21 13:23:58 +00:00
|
|
|
(let* ((prev-head (save-excursion (outline-previous-heading)))
|
|
|
|
(bound (if prev-head
|
|
|
|
(or (save-excursion
|
|
|
|
(let ((case-fold-search t))
|
|
|
|
(re-search-backward "^[ \t]*:END:" prev-head t)))
|
|
|
|
prev-head)
|
|
|
|
(point-min))))
|
2010-08-19 14:09:45 +00:00
|
|
|
(cond
|
2010-08-22 17:34:46 +00:00
|
|
|
((eq org-list-ending-method 'regexp)
|
|
|
|
(org-list-in-item-p-with-regexp bound))
|
2010-08-19 14:09:45 +00:00
|
|
|
((eq org-list-ending-method 'indent)
|
|
|
|
(org-list-in-item-p-with-indent bound))
|
2010-08-22 17:34:46 +00:00
|
|
|
(t (and (org-list-in-item-p-with-regexp bound)
|
|
|
|
(org-list-in-item-p-with-indent bound)))))))
|
2010-07-10 15:02:58 +00:00
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-list-first-item-p (top)
|
2010-08-12 12:14:37 +00:00
|
|
|
"Is this item the first item in a plain list?
|
2010-08-28 10:53:19 +00:00
|
|
|
Assume point is at an item.
|
|
|
|
|
|
|
|
TOP is the position of list's top-item."
|
2010-07-10 15:02:58 +00:00
|
|
|
(save-excursion
|
2010-08-12 12:14:37 +00:00
|
|
|
(beginning-of-line)
|
|
|
|
(let ((ind (org-get-indentation)))
|
2010-08-28 10:53:19 +00:00
|
|
|
(or (not (org-search-backward-unenclosed org-item-beginning-re top t))
|
2010-08-12 12:14:37 +00:00
|
|
|
(< (org-get-indentation) ind)))))
|
2010-07-10 15:02:58 +00:00
|
|
|
|
2008-09-23 06:03:13 +00:00
|
|
|
(defun org-at-item-p ()
|
|
|
|
"Is point in a line starting a hand-formatted item?"
|
2010-04-21 13:52:01 +00:00
|
|
|
(save-excursion
|
2010-07-28 13:53:37 +00:00
|
|
|
(beginning-of-line) (looking-at org-item-beginning-re)))
|
2008-09-23 06:03:13 +00: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 15:23:44 +00: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 13:17:38 +00:00
|
|
|
(defun org-at-item-timer-p ()
|
2010-07-19 08:50:15 +00:00
|
|
|
"Is point at a line starting a plain list item with a timer?"
|
2010-08-19 14:09:45 +00:00
|
|
|
(org-list-at-regexp-after-bullet-p
|
|
|
|
"\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
|
2010-07-19 09:21:22 +00:00
|
|
|
|
2010-07-27 22:05:42 +00:00
|
|
|
(defun org-at-item-description-p ()
|
2010-07-19 09:21:22 +00:00
|
|
|
"Is point at a description list item?"
|
2010-07-19 10:38:14 +00:00
|
|
|
(org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
|
2010-07-17 13:17:38 +00:00
|
|
|
|
2008-09-23 06:03:13 +00:00
|
|
|
(defun org-at-item-checkbox-p ()
|
|
|
|
"Is point at a line starting a plain-list item with a checklet?"
|
2010-07-19 15:02:58 +00:00
|
|
|
(org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2009-02-19 08:13:08 +00:00
|
|
|
(defun org-checkbox-blocked-p ()
|
|
|
|
"Is the current checkbox blocked from for being checked now?
|
|
|
|
A checkbox is blocked if all of the following conditions are fulfilled:
|
|
|
|
|
|
|
|
1. The checkbox is not checked already.
|
|
|
|
2. The current entry has the ORDERED property set.
|
|
|
|
3. There is an unchecked checkbox in this entry before the current line."
|
|
|
|
(catch 'exit
|
|
|
|
(save-match-data
|
|
|
|
(save-excursion
|
|
|
|
(unless (org-at-item-checkbox-p) (throw 'exit nil))
|
2010-07-19 15:02:58 +00:00
|
|
|
(when (equal (match-string 1) "[X]")
|
2009-02-19 08:13:08 +00:00
|
|
|
;; the box is already checked!
|
|
|
|
(throw 'exit nil))
|
|
|
|
(let ((end (point-at-bol)))
|
|
|
|
(condition-case nil (org-back-to-heading t)
|
|
|
|
(error (throw 'exit nil)))
|
|
|
|
(unless (org-entry-get nil "ORDERED") (throw 'exit nil))
|
2010-07-28 13:53:37 +00:00
|
|
|
(when (org-search-forward-unenclosed
|
2010-08-20 11:05:38 +00:00
|
|
|
"^[ \t]*[-+*0-9.)]+[ \t]+\\(\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[[- ]\\]" end t)
|
2010-07-17 09:27:58 +00:00
|
|
|
(org-current-line)))))))
|
2009-02-19 08:13:08 +00:00
|
|
|
|
2010-07-10 15:02:58 +00:00
|
|
|
;;; Navigate
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
;; Every interactive navigation function is derived from a
|
|
|
|
;; non-interactive one, which doesn't move point, assumes point is
|
|
|
|
;; already in a list and doesn't compute list boundaries.
|
|
|
|
|
|
|
|
;; If you plan to use more than one org-list function is some code,
|
2010-09-14 16:30:03 +00:00
|
|
|
;; you should therefore first check if point is in a list with
|
|
|
|
;; `org-in-item-p' or `org-at-item-p', then compute list boundaries
|
|
|
|
;; with `org-list-top-point' and `org-list-bottom-point', and make use
|
|
|
|
;; of non-interactive forms.
|
2010-08-28 10:53:19 +00:00
|
|
|
|
2010-07-10 15:02:58 +00:00
|
|
|
(defun org-list-top-point ()
|
2010-08-28 10:53:19 +00:00
|
|
|
"Return point at the top level in a list.
|
|
|
|
Assume point is in a list."
|
2010-08-21 13:23:58 +00:00
|
|
|
(let* ((prev-head (save-excursion (outline-previous-heading)))
|
|
|
|
(bound (if prev-head
|
|
|
|
(or (save-excursion
|
|
|
|
(let ((case-fold-search t))
|
|
|
|
(re-search-backward "^[ \t]*:END:" prev-head t)))
|
|
|
|
prev-head)
|
|
|
|
(point-min))))
|
2010-08-19 14:09:45 +00:00
|
|
|
(cond
|
2010-08-22 17:34:46 +00:00
|
|
|
((eq org-list-ending-method 'regexp)
|
|
|
|
(org-list-top-point-with-regexp bound))
|
2010-08-19 14:09:45 +00:00
|
|
|
((eq org-list-ending-method 'indent)
|
2010-08-21 13:23:58 +00:00
|
|
|
(org-list-top-point-with-indent bound))
|
2010-08-23 11:06:04 +00:00
|
|
|
(t (let ((top-re (org-list-top-point-with-regexp bound)))
|
|
|
|
(org-list-top-point-with-indent (or top-re bound)))))))
|
2010-07-10 15:02:58 +00:00
|
|
|
|
|
|
|
(defun org-list-bottom-point ()
|
2010-08-28 10:53:19 +00:00
|
|
|
"Return point just before list ending.
|
|
|
|
Assume point is in a list."
|
2010-08-21 13:23:58 +00:00
|
|
|
(let* ((next-head (save-excursion
|
|
|
|
(and (let ((outline-regexp org-outline-regexp))
|
|
|
|
;; Use default regexp because folding
|
|
|
|
;; changes OUTLINE-REGEXP.
|
2010-08-25 13:45:05 +00:00
|
|
|
(outline-next-heading)))))
|
2010-08-21 13:23:58 +00:00
|
|
|
(limit (or (save-excursion
|
|
|
|
(and (re-search-forward "^[ \t]*:END:" next-head t)
|
|
|
|
(point-at-bol)))
|
|
|
|
next-head
|
2010-08-28 10:53:19 +00:00
|
|
|
(point-max))))
|
|
|
|
(cond
|
|
|
|
((eq org-list-ending-method 'regexp)
|
|
|
|
(org-list-bottom-point-with-regexp limit))
|
|
|
|
((eq org-list-ending-method 'indent)
|
|
|
|
(org-list-bottom-point-with-indent limit))
|
|
|
|
(t (let ((bottom-re (org-list-bottom-point-with-regexp limit)))
|
|
|
|
(org-list-bottom-point-with-indent (or bottom-re limit)))))))
|
|
|
|
|
|
|
|
(defun org-get-item-beginning ()
|
|
|
|
"Return position of current item beginning."
|
|
|
|
(save-excursion
|
|
|
|
;; possibly match current line
|
|
|
|
(end-of-line)
|
|
|
|
(org-search-backward-unenclosed org-item-beginning-re nil t)
|
|
|
|
(point-at-bol)))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
|
|
|
(defun org-beginning-of-item ()
|
|
|
|
"Go to the beginning of the current hand-formatted item.
|
2010-08-28 10:53:19 +00:00
|
|
|
If the cursor is not in an item, throw an error."
|
2008-09-23 06:03:13 +00:00
|
|
|
(interactive)
|
2010-08-28 10:53:19 +00:00
|
|
|
(if (org-in-item-p)
|
|
|
|
(goto-char (org-get-item-beginning))
|
|
|
|
(error "Not in an item")))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-get-beginning-of-list (top)
|
|
|
|
"Return position of the first item of the current list or sublist.
|
|
|
|
TOP is the position at list beginning."
|
|
|
|
(save-excursion
|
|
|
|
(let (prev-p)
|
|
|
|
(while (setq prev-p (org-get-previous-item (point) top))
|
|
|
|
(goto-char prev-p))
|
|
|
|
(point-at-bol))))
|
|
|
|
|
|
|
|
(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 06:03:13 +00:00
|
|
|
(interactive)
|
2010-08-28 10:53:19 +00:00
|
|
|
(if (org-in-item-p)
|
|
|
|
(goto-char (org-get-beginning-of-list (org-list-top-point)))
|
|
|
|
(error "Not in an item")))
|
2010-07-27 22:05:42 +00:00
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-get-end-of-list (bottom)
|
|
|
|
"Return position at the end of the current list or sublist.
|
|
|
|
BOTTOM is the position at list ending."
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (org-get-item-beginning))
|
|
|
|
(let ((ind (org-get-indentation)))
|
|
|
|
(while (and (/= (point) bottom)
|
|
|
|
(>= (org-get-indentation) ind))
|
|
|
|
(org-search-forward-unenclosed org-item-beginning-re bottom 'move))
|
|
|
|
(if (= (point) bottom) bottom (point-at-bol)))))
|
2010-07-10 15:02:58 +00:00
|
|
|
|
2010-08-28 10:53:19 +00: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)
|
|
|
|
(if (org-in-item-p)
|
|
|
|
(goto-char (org-get-end-of-list (org-list-bottom-point)))
|
|
|
|
(error "Not in an item")))
|
|
|
|
|
|
|
|
(defun org-get-end-of-item (bottom)
|
|
|
|
"Return position at the end of the current item.
|
|
|
|
BOTTOM is the position at list ending."
|
2010-09-14 16:30:03 +00:00
|
|
|
(or (org-get-next-item (point) bottom)
|
|
|
|
(org-get-end-of-list bottom)))
|
2010-08-28 10:53:19 +00: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)
|
|
|
|
(if (org-in-item-p)
|
|
|
|
(goto-char (org-get-end-of-item (org-list-bottom-point)))
|
|
|
|
(error "Not in an item")))
|
|
|
|
|
|
|
|
(defun org-end-of-item-or-at-child (bottom)
|
|
|
|
"Move to the end of the item, stops before the first child if any.
|
|
|
|
BOTTOM is the position at list ending."
|
|
|
|
(end-of-line)
|
|
|
|
(goto-char
|
|
|
|
(if (org-search-forward-unenclosed org-item-beginning-re bottom t)
|
|
|
|
(point-at-bol)
|
|
|
|
(org-get-end-of-item bottom))))
|
|
|
|
|
|
|
|
(defun org-end-of-item-before-blank (bottom)
|
2010-07-10 15:02:58 +00:00
|
|
|
"Return point at end of item, before any blank line.
|
2010-08-28 10:53:19 +00:00
|
|
|
Point returned is at eol.
|
|
|
|
|
|
|
|
BOTTOM is the position at list ending."
|
2010-07-10 15:02:58 +00:00
|
|
|
(save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(goto-char (org-get-end-of-item bottom))
|
2010-07-10 15:02:58 +00:00
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(point-at-eol)))
|
|
|
|
|
|
|
|
(defun org-get-previous-item (pos limit)
|
2010-08-28 10:53:19 +00:00
|
|
|
"Return point of the previous item at the same level as POS.
|
|
|
|
Stop searching at LIMIT. Return nil if no item is found."
|
2010-07-19 09:33:20 +00:00
|
|
|
(org-list-get-item-same-level
|
2010-07-19 09:43:33 +00:00
|
|
|
#'org-search-backward-unenclosed pos limit #'beginning-of-line))
|
2010-04-21 13:52:01 +00:00
|
|
|
|
2008-09-23 06:03:13 +00:00
|
|
|
(defun org-previous-item ()
|
2010-07-10 15:02:58 +00: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 06:03:13 +00:00
|
|
|
(interactive)
|
2010-08-28 10:53:19 +00:00
|
|
|
(if (not (org-in-item-p))
|
|
|
|
(error "Not in an item")
|
|
|
|
(let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
|
|
|
|
(if prev-p (goto-char prev-p) (error "On first item")))))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-get-next-item (pos limit)
|
|
|
|
"Return point of the next item at the same level as POS.
|
|
|
|
Stop searching at LIMIT. Return nil if no item is found."
|
|
|
|
(org-list-get-item-same-level
|
|
|
|
#'org-search-forward-unenclosed pos limit #'end-of-line))
|
2010-07-10 15:02:58 +00:00
|
|
|
|
2010-08-28 10:53:19 +00: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 15:02:58 +00:00
|
|
|
(interactive)
|
2010-08-28 10:53:19 +00:00
|
|
|
(if (not (org-in-item-p))
|
|
|
|
(error "Not in an item")
|
|
|
|
(let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
|
|
|
|
(if next-p (goto-char next-p) (error "On last item")))))
|
2010-07-10 15:02:58 +00:00
|
|
|
|
|
|
|
;;; Manipulate
|
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-list-exchange-items (beg-A beg-B bottom)
|
2010-07-10 15:02:58 +00:00
|
|
|
"Swap item starting at BEG-A with item starting at BEG-B.
|
2010-08-28 10:53:19 +00:00
|
|
|
Blank lines at the end of items are left in place. Assume BEG-A
|
|
|
|
is lesser than BEG-B.
|
|
|
|
|
|
|
|
BOTTOM is the position at list ending."
|
2009-07-06 06:53:50 +00:00
|
|
|
(save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(let* ((end-of-item-no-blank
|
|
|
|
(lambda (pos)
|
|
|
|
(goto-char pos)
|
|
|
|
(goto-char (org-end-of-item-before-blank bottom))))
|
2010-07-10 15:02:58 +00:00
|
|
|
(end-A-no-blank (funcall end-of-item-no-blank beg-A))
|
|
|
|
(end-B-no-blank (funcall end-of-item-no-blank beg-B))
|
|
|
|
(body-A (buffer-substring beg-A end-A-no-blank))
|
|
|
|
(body-B (buffer-substring beg-B end-B-no-blank))
|
|
|
|
(between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
|
|
|
|
(goto-char beg-A)
|
|
|
|
(delete-region beg-A end-B-no-blank)
|
|
|
|
(insert (concat body-B between-A-no-blank-and-B body-A)))))
|
2008-09-23 06:03:13 +00: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-08-28 10:53:19 +00:00
|
|
|
(if (not (org-at-item-p))
|
|
|
|
(error "Not at an item")
|
|
|
|
(let* ((pos (point))
|
|
|
|
(col (current-column))
|
|
|
|
(bottom (org-list-bottom-point))
|
|
|
|
(actual-item (goto-char (org-get-item-beginning)))
|
|
|
|
(next-item (org-get-next-item (point) bottom)))
|
|
|
|
(if (not next-item)
|
|
|
|
(progn
|
|
|
|
(goto-char pos)
|
|
|
|
(error "Cannot move this item further down"))
|
|
|
|
(org-list-exchange-items actual-item next-item bottom)
|
|
|
|
(org-list-repair nil nil bottom)
|
|
|
|
(goto-char (org-get-next-item (point) bottom))
|
2010-11-14 07:58:41 +00:00
|
|
|
(org-move-to-column col)))))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-07-10 15:02:58 +00:00
|
|
|
(defun org-move-item-up ()
|
2008-09-23 06:03:13 +00: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-08-28 10:53:19 +00:00
|
|
|
(if (not (org-at-item-p))
|
|
|
|
(error "Not at an item")
|
|
|
|
(let* ((pos (point))
|
|
|
|
(col (current-column))
|
|
|
|
(top (org-list-top-point))
|
|
|
|
(bottom (org-list-bottom-point))
|
|
|
|
(actual-item (goto-char (org-get-item-beginning)))
|
|
|
|
(prev-item (org-get-previous-item (point) top)))
|
|
|
|
(if (not prev-item)
|
|
|
|
(progn
|
|
|
|
(goto-char pos)
|
|
|
|
(error "Cannot move this item further up"))
|
|
|
|
(org-list-exchange-items prev-item actual-item bottom)
|
|
|
|
(org-list-repair nil top bottom)
|
2010-11-14 07:58:41 +00:00
|
|
|
(org-move-to-column col)))))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-07-10 15:02:58 +00: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 11:32:33 +00: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-07-10 15:02:58 +00:00
|
|
|
(unless (or (not (org-in-item-p))
|
2010-09-07 17:06:03 +00:00
|
|
|
(save-excursion
|
|
|
|
(goto-char (org-get-item-beginning))
|
|
|
|
(org-invisible-p)))
|
2010-07-17 21:00:22 +00:00
|
|
|
(if (save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(goto-char (org-get-item-beginning))
|
2010-07-17 21:00:22 +00:00
|
|
|
(org-at-item-timer-p))
|
2010-07-17 13:17:38 +00:00
|
|
|
;; Timer list: delegate to `org-timer-item'.
|
2010-07-15 14:57:37 +00:00
|
|
|
(progn (org-timer-item) t)
|
2010-07-15 16:28:01 +00:00
|
|
|
;; if we're in a description list, ask for the new term.
|
|
|
|
(let ((desc-text (when (save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(and (goto-char (org-get-item-beginning))
|
2010-07-27 22:05:42 +00:00
|
|
|
(org-at-item-description-p)))
|
2010-07-15 16:28:01 +00:00
|
|
|
(concat (read-string "Term: ") " :: "))))
|
2010-07-28 14:51:28 +00:00
|
|
|
;; Don't insert a checkbox if checkbox rule is applied and it
|
|
|
|
;; is a description item.
|
2010-07-19 09:33:20 +00:00
|
|
|
(org-list-insert-item-generic
|
2010-07-28 14:51:28 +00:00
|
|
|
(point) (and checkbox
|
|
|
|
(or (not desc-text)
|
|
|
|
(not (cdr (assq 'checkbox org-list-automatic-rules)))))
|
|
|
|
desc-text)))))
|
2010-07-10 15:02:58 +00:00
|
|
|
|
2010-07-30 22:45:34 +00:00
|
|
|
;;; Structures
|
|
|
|
|
|
|
|
;; The idea behind structures is to avoid moving back and forth in the
|
|
|
|
;; buffer on costly operations like indenting or fixing bullets.
|
|
|
|
|
|
|
|
;; It achieves this by taking a snapshot of an interesting part of the
|
2010-08-28 10:53:19 +00:00
|
|
|
;; list, in the shape of an alist, using `org-list-struct'.
|
2010-07-30 22:45:34 +00:00
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
;; It then proceeds to changes directly on the alist, with the help of
|
|
|
|
;; and `org-list-struct-origins'. When those are done,
|
|
|
|
;; `org-list-struct-apply-struct' applies the changes to the buffer.
|
2010-07-30 22:45:34 +00:00
|
|
|
|
|
|
|
(defun org-list-struct-assoc-at-point ()
|
|
|
|
"Return the structure association at point.
|
|
|
|
It is a cons-cell whose key is point and values are indentation,
|
|
|
|
bullet string and bullet counter, if any."
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(list (point-at-bol)
|
|
|
|
(org-get-indentation)
|
|
|
|
(progn
|
|
|
|
(looking-at "^[ \t]*\\([-+*0-9.)]+[ \t]+\\)")
|
|
|
|
(match-string 1))
|
|
|
|
(progn
|
|
|
|
(goto-char (match-end 0))
|
2010-08-20 11:05:38 +00:00
|
|
|
(and (looking-at "\\[@\\(?:start:\\)?\\([0-9]+\\)\\]")
|
2010-07-30 22:45:34 +00:00
|
|
|
(match-string 1))))))
|
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-list-struct (begin end top bottom &optional outdent)
|
2010-07-30 22:45:34 +00:00
|
|
|
"Return the structure containing the list between BEGIN and END.
|
|
|
|
A structure is an alist where key is point of item and values
|
|
|
|
are, in that order, indentation, bullet string and value of
|
2010-08-08 20:28:53 +00:00
|
|
|
counter, if any. A structure contains every list and sublist that
|
|
|
|
has items between BEGIN and END along with their common ancestor.
|
|
|
|
If no such ancestor can be found, the function will add a virtual
|
|
|
|
ancestor at position 0.
|
2010-07-30 22:45:34 +00:00
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
TOP and BOTTOM are respectively the position of list beginning
|
|
|
|
and list ending.
|
|
|
|
|
2010-07-30 22:45:34 +00:00
|
|
|
If OUTDENT is non-nil, it will also grab all of the parent list
|
|
|
|
and the grand-parent. Setting OUTDENT to t is mandatory when next
|
2010-08-28 10:53:19 +00:00
|
|
|
change is an outdent."
|
2010-07-30 22:45:34 +00:00
|
|
|
(save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(let* (struct
|
2010-07-30 22:45:34 +00:00
|
|
|
(extend
|
|
|
|
(lambda (struct)
|
|
|
|
(let* ((ind-min (apply 'min (mapcar 'cadr struct)))
|
|
|
|
(begin (caar struct))
|
|
|
|
(end (caar (last struct)))
|
|
|
|
pre-list post-list)
|
|
|
|
(goto-char begin)
|
|
|
|
;; Find beginning of most outdented list (min list)
|
2010-08-19 14:40:21 +00:00
|
|
|
(while (and (org-search-backward-unenclosed
|
|
|
|
org-item-beginning-re top t)
|
2010-07-30 22:45:34 +00:00
|
|
|
(>= (org-get-indentation) ind-min))
|
2010-08-19 14:40:21 +00:00
|
|
|
(setq pre-list (cons (org-list-struct-assoc-at-point)
|
|
|
|
pre-list)))
|
2010-08-08 20:28:53 +00:00
|
|
|
;; Now get the parent. If none, add a virtual ancestor
|
2010-07-30 22:45:34 +00:00
|
|
|
(if (< (org-get-indentation) ind-min)
|
2010-08-19 14:40:21 +00:00
|
|
|
(setq pre-list (cons (org-list-struct-assoc-at-point)
|
|
|
|
pre-list))
|
|
|
|
(setq pre-list (cons (list 0 (org-get-indentation) "" nil)
|
|
|
|
pre-list)))
|
2010-07-30 22:45:34 +00:00
|
|
|
;; Find end of min list
|
|
|
|
(goto-char end)
|
|
|
|
(end-of-line)
|
2010-08-19 14:40:21 +00:00
|
|
|
(while (and (org-search-forward-unenclosed
|
2010-08-19 15:59:55 +00:00
|
|
|
org-item-beginning-re bottom 'move)
|
2010-07-30 22:45:34 +00:00
|
|
|
(>= (org-get-indentation) ind-min))
|
2010-08-19 14:40:21 +00:00
|
|
|
(setq post-list (cons (org-list-struct-assoc-at-point)
|
|
|
|
post-list)))
|
2010-08-19 23:58:46 +00:00
|
|
|
;; Is list is malformed? If some items are less
|
|
|
|
;; indented that top-item, add them anyhow.
|
2010-08-22 19:48:33 +00:00
|
|
|
(when (and (= (caar pre-list) 0) (< (point) bottom))
|
|
|
|
(beginning-of-line)
|
2010-08-19 15:59:55 +00:00
|
|
|
(while (org-search-forward-unenclosed
|
|
|
|
org-item-beginning-re bottom t)
|
|
|
|
(setq post-list (cons (org-list-struct-assoc-at-point)
|
|
|
|
post-list))))
|
2010-07-30 22:45:34 +00:00
|
|
|
(append pre-list struct (reverse post-list))))))
|
|
|
|
;; Here we start: first get the core zone...
|
|
|
|
(goto-char end)
|
|
|
|
(while (org-search-backward-unenclosed org-item-beginning-re begin t)
|
|
|
|
(setq struct (cons (org-list-struct-assoc-at-point) struct)))
|
|
|
|
;; ... then, extend it to make it a structure...
|
|
|
|
(let ((extended (funcall extend struct)))
|
|
|
|
;; ... twice when OUTDENT is non-nil and struct still can be
|
|
|
|
;; extended
|
|
|
|
(if (and outdent (> (caar extended) 0))
|
|
|
|
(funcall extend extended)
|
|
|
|
extended)))))
|
|
|
|
|
|
|
|
(defun org-list-struct-origins (struct)
|
2010-08-21 11:32:33 +00:00
|
|
|
"Return an alist where key is item's position and value parent's.
|
|
|
|
STRUCT is the list's structure looked up."
|
2010-07-30 22:45:34 +00:00
|
|
|
(let* ((struct-rev (reverse struct))
|
2010-08-17 20:21:24 +00:00
|
|
|
(acc (list (cons (nth 1 (car struct)) 0)))
|
2010-08-19 14:40:21 +00:00
|
|
|
(prev-item (lambda (item)
|
|
|
|
(car (nth 1 (member (assq item struct) struct-rev)))))
|
2010-07-30 22:45:34 +00:00
|
|
|
(get-origins
|
|
|
|
(lambda (item)
|
|
|
|
(let* ((item-pos (car item))
|
|
|
|
(ind (nth 1 item))
|
|
|
|
(prev-ind (caar acc)))
|
|
|
|
(cond
|
|
|
|
;; List closing.
|
|
|
|
((> prev-ind ind)
|
2010-08-19 15:59:55 +00:00
|
|
|
(let ((current-origin (or (member (assq ind acc) acc)
|
|
|
|
;; needed if top-point is
|
|
|
|
;; not the most outdented
|
|
|
|
(last acc))))
|
|
|
|
(setq acc current-origin)
|
|
|
|
(cons item-pos (cdar acc))))
|
2010-07-30 22:45:34 +00:00
|
|
|
;; New list
|
|
|
|
((< prev-ind ind)
|
|
|
|
(let ((origin (funcall prev-item item-pos)))
|
|
|
|
(setq acc (cons (cons ind origin) acc))
|
|
|
|
(cons item-pos origin)))
|
|
|
|
;; Current list going on
|
2010-08-17 20:21:24 +00:00
|
|
|
(t (cons item-pos (cdar acc))))))))
|
2010-07-30 22:45:34 +00:00
|
|
|
(cons '(0 . 0) (mapcar get-origins (cdr struct)))))
|
|
|
|
|
|
|
|
(defun org-list-struct-get-parent (item struct origins)
|
2010-08-21 11:32:33 +00:00
|
|
|
"Return parent association of ITEM in STRUCT or nil.
|
|
|
|
ORIGINS is the alist of parents. See `org-list-struct-origins'."
|
2010-07-30 22:45:34 +00:00
|
|
|
(let* ((parent-pos (cdr (assq (car item) origins))))
|
|
|
|
(when (> parent-pos 0) (assq parent-pos struct))))
|
|
|
|
|
|
|
|
(defun org-list-struct-get-child (item struct)
|
|
|
|
"Return child association of ITEM in STRUCT or nil."
|
|
|
|
(let ((ind (nth 1 item))
|
|
|
|
(next-item (cadr (member item struct))))
|
|
|
|
(when (and next-item (> (nth 1 next-item) ind)) next-item)))
|
|
|
|
|
|
|
|
(defun org-list-struct-fix-bul (struct origins)
|
|
|
|
"Verify and correct bullets for every association in STRUCT.
|
2010-08-21 11:32:33 +00:00
|
|
|
ORIGINS is the alist of parents. See `org-list-struct-origins'.
|
|
|
|
|
2010-07-30 22:45:34 +00:00
|
|
|
This function modifies STRUCT."
|
2010-08-17 20:21:24 +00:00
|
|
|
(let* (acc
|
|
|
|
(init-bul (lambda (item)
|
2010-07-30 22:45:34 +00:00
|
|
|
(let ((counter (nth 3 item))
|
|
|
|
(bullet (org-list-bullet-string (nth 2 item))))
|
|
|
|
(cond
|
|
|
|
((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)))))
|
|
|
|
(set-bul (lambda (item bullet)
|
|
|
|
(setcdr item (list (nth 1 item) bullet (nth 3 item)))))
|
|
|
|
(get-bul (lambda (item bullet)
|
|
|
|
(let* ((counter (nth 3 item)))
|
|
|
|
(if (and counter (string-match "[0-9]+" bullet))
|
|
|
|
(replace-match counter nil nil bullet)
|
|
|
|
bullet))))
|
|
|
|
(fix-bul
|
|
|
|
(lambda (item) struct
|
|
|
|
(let* ((parent (cdr (assq (car item) origins)))
|
|
|
|
(orig-ref (assq parent acc)))
|
|
|
|
(if orig-ref
|
|
|
|
;; Continuing previous list
|
|
|
|
(let* ((prev-bul (cdr orig-ref))
|
|
|
|
(new-bul (funcall get-bul item prev-bul)))
|
|
|
|
(setcdr orig-ref (org-list-inc-bullet-maybe new-bul))
|
|
|
|
(funcall set-bul item new-bul))
|
|
|
|
;; A new list is starting
|
|
|
|
(let ((new-bul (funcall init-bul item)))
|
|
|
|
(funcall set-bul item new-bul)
|
2010-08-19 14:40:21 +00:00
|
|
|
(setq acc (cons (cons parent
|
|
|
|
(org-list-inc-bullet-maybe new-bul))
|
|
|
|
acc))))))))
|
2010-07-30 22:45:34 +00:00
|
|
|
(mapc fix-bul (cdr struct))))
|
|
|
|
|
|
|
|
(defun org-list-struct-fix-ind (struct origins)
|
|
|
|
"Verify and correct indentation for every association in STRUCT.
|
2010-08-21 11:32:33 +00:00
|
|
|
ORIGINS is the alist of parents. See `org-list-struct-origins'.
|
|
|
|
|
2010-07-30 22:45:34 +00:00
|
|
|
This function modifies STRUCT."
|
|
|
|
(let* ((headless (cdr struct))
|
|
|
|
(ancestor (car struct))
|
|
|
|
(top-ind (+ (nth 1 ancestor) (length (nth 2 ancestor))))
|
|
|
|
(new-ind
|
|
|
|
(lambda (item)
|
|
|
|
(let* ((parent (org-list-struct-get-parent item headless origins)))
|
|
|
|
(if parent
|
|
|
|
;; Indent like parent + length of parent's bullet
|
2010-08-19 14:40:21 +00:00
|
|
|
(setcdr item (cons (+ (length (nth 2 parent)) (nth 1 parent))
|
|
|
|
(cddr item)))
|
2010-07-30 22:45:34 +00:00
|
|
|
;; If no parent, indent like top-point
|
|
|
|
(setcdr item (cons top-ind (cddr item))))))))
|
|
|
|
(mapc new-ind headless)))
|
|
|
|
|
|
|
|
(defun org-list-struct-fix-struct (struct origins)
|
|
|
|
"Return STRUCT with correct bullets and indentation.
|
2010-08-21 11:32:33 +00:00
|
|
|
ORIGINS is the alist of parents. See `org-list-struct-origins'.
|
|
|
|
|
2010-07-30 22:45:34 +00:00
|
|
|
Only elements of STRUCT that have changed are returned."
|
2010-08-12 12:14:37 +00:00
|
|
|
(let ((old (copy-alist struct)))
|
2010-07-30 22:45:34 +00:00
|
|
|
(org-list-struct-fix-bul struct origins)
|
|
|
|
(org-list-struct-fix-ind struct origins)
|
2010-08-12 12:14:37 +00:00
|
|
|
(delq nil (mapcar (lambda (e) (when (not (equal (pop old) e)) e)) struct))))
|
2010-07-30 22:45:34 +00:00
|
|
|
|
|
|
|
(defun org-list-struct-outdent (start end origins)
|
2010-08-21 11:32:33 +00:00
|
|
|
"Outdent items in a structure.
|
|
|
|
Items are indented when their key is between START, included, and
|
|
|
|
END, excluded.
|
|
|
|
|
|
|
|
ORIGINS is the alist of parents. See `org-list-struct-origins'.
|
|
|
|
|
|
|
|
STRUCT is the concerned structure."
|
2010-08-17 20:21:24 +00: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)
|
|
|
|
(let ((convert (assq parent acc)))
|
|
|
|
(if convert (cons item (cdr convert)) cell)))
|
|
|
|
;; Item has no parent: error
|
|
|
|
((<= parent 0)
|
|
|
|
(error "Cannot outdent top-level items"))
|
|
|
|
;; Parent is outdented: keep association
|
|
|
|
((>= parent start)
|
|
|
|
(setq acc (cons (cons parent item) acc)) cell)
|
|
|
|
(t
|
|
|
|
;; Parent isn't outdented: reparent to grand-parent
|
|
|
|
(let ((grand-parent (cdr (assq parent origins))))
|
|
|
|
(setq acc (cons (cons parent item) acc))
|
|
|
|
(cons item grand-parent))))))))
|
2010-07-30 22:45:34 +00:00
|
|
|
(mapcar out origins)))
|
|
|
|
|
2010-08-11 11:08:48 +00:00
|
|
|
(defun org-list-struct-indent (start end origins struct)
|
2010-08-21 11:32:33 +00:00
|
|
|
"Indent items in a structure.
|
|
|
|
Items are indented when their key is between START, included, and
|
|
|
|
END, excluded.
|
|
|
|
|
|
|
|
ORIGINS is the alist of parents. See `org-list-struct-origins'.
|
2010-08-11 11:08:48 +00:00
|
|
|
|
2010-08-21 11:32:33 +00:00
|
|
|
STRUCT is the concerned structure. It may be modified if
|
|
|
|
`org-list-demote-modify-bullet' matches bullets between START and
|
|
|
|
END."
|
2010-08-17 20:21:24 +00:00
|
|
|
(let* (acc
|
|
|
|
(orig-rev (reverse origins))
|
2010-08-11 11:08:48 +00:00
|
|
|
(get-prev-item
|
|
|
|
(lambda (cell parent)
|
|
|
|
(car (rassq parent (cdr (memq cell orig-rev))))))
|
|
|
|
(set-assoc
|
|
|
|
(lambda (cell)
|
|
|
|
(setq acc (cons cell acc)) cell))
|
|
|
|
(change-bullet-maybe
|
|
|
|
(lambda (item)
|
|
|
|
(let* ((full-item (assq item struct))
|
|
|
|
(item-bul (org-trim (nth 2 full-item)))
|
|
|
|
(new-bul-p (cdr (assoc item-bul org-list-demote-modify-bullet))))
|
|
|
|
(when new-bul-p
|
|
|
|
;; new bullet is stored without space to ensure item
|
|
|
|
;; will be modified
|
|
|
|
(setcdr full-item
|
2010-08-19 14:40:21 +00:00
|
|
|
(list (nth 1 full-item)
|
|
|
|
new-bul-p
|
|
|
|
(nth 3 full-item)))))))
|
2010-07-30 22:45:34 +00: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...
|
|
|
|
(let ((prev (funcall get-prev-item cell parent)))
|
2010-08-11 11:08:48 +00:00
|
|
|
;; Check if bullet needs to be changed
|
|
|
|
(funcall change-bullet-maybe item)
|
2010-07-30 22:45:34 +00:00
|
|
|
(cond
|
|
|
|
;; First item indented but not parent: error
|
|
|
|
((and (or (not prev) (= prev 0)) (< parent start))
|
|
|
|
(error "Cannot indent the first item of a list"))
|
|
|
|
;; First item and parent indented: keep same parent
|
|
|
|
((or (not prev) (= prev 0))
|
|
|
|
(funcall set-assoc cell))
|
|
|
|
;; Previous item not indented: reparent to it
|
|
|
|
((< prev start)
|
|
|
|
(funcall set-assoc (cons item prev)))
|
|
|
|
;; Previous item indented: reparent like it
|
|
|
|
(t
|
2010-08-19 14:40:21 +00:00
|
|
|
(funcall set-assoc (cons item
|
|
|
|
(cdr (assq prev acc)))))))))))))
|
2010-07-30 22:45:34 +00:00
|
|
|
(mapcar ind origins)))
|
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-list-struct-apply-struct (struct bottom)
|
2010-07-30 22:45:34 +00:00
|
|
|
"Apply modifications to list so it mirrors STRUCT.
|
2010-08-28 10:53:19 +00:00
|
|
|
BOTTOM is position at list ending.
|
|
|
|
|
2010-07-30 22:45:34 +00:00
|
|
|
Initial position is restored after the changes."
|
|
|
|
(let* ((pos (copy-marker (point)))
|
2010-08-07 16:35:29 +00:00
|
|
|
(ancestor (caar struct))
|
2010-07-30 22:45:34 +00:00
|
|
|
(modify
|
|
|
|
(lambda (item)
|
2010-08-14 22:21:15 +00:00
|
|
|
(goto-char (car item))
|
|
|
|
(let* ((new-ind (nth 1 item))
|
|
|
|
(new-bul (org-list-bullet-string (nth 2 item)))
|
|
|
|
(old-ind (org-get-indentation))
|
|
|
|
(old-bul (progn
|
|
|
|
(looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
|
|
|
|
(match-string 1)))
|
|
|
|
(old-body-ind (+ (length old-bul) old-ind))
|
|
|
|
(new-body-ind (+ (length new-bul) new-ind)))
|
2010-08-19 14:09:45 +00:00
|
|
|
;; 1. Shift item's body
|
|
|
|
(unless (= old-body-ind new-body-ind)
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-shift-item-indentation
|
|
|
|
(- new-body-ind old-body-ind) bottom))
|
2010-08-19 14:09:45 +00:00
|
|
|
;; 2. Replace bullet
|
2010-08-14 22:21:15 +00:00
|
|
|
(unless (equal new-bul old-bul)
|
2010-08-19 14:09:45 +00:00
|
|
|
(save-excursion
|
|
|
|
(looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
|
|
|
|
(replace-match new-bul nil nil nil 1)))
|
|
|
|
;; 3. Indent item to appropriate column
|
2010-08-14 22:21:15 +00:00
|
|
|
(unless (= new-ind old-ind)
|
2010-08-19 14:09:45 +00:00
|
|
|
(delete-region (point-at-bol)
|
|
|
|
(progn
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
(point)))
|
|
|
|
(indent-to new-ind)))))
|
2010-07-30 22:45:34 +00:00
|
|
|
;; Remove ancestor if it is left.
|
2010-08-19 14:40:21 +00:00
|
|
|
(struct-to-apply (if (or (not ancestor) (= 0 ancestor))
|
|
|
|
(cdr struct)
|
|
|
|
struct)))
|
2010-07-30 22:45:34 +00:00
|
|
|
;; Apply changes from bottom to top
|
|
|
|
(mapc modify (nreverse struct-to-apply))
|
|
|
|
(goto-char pos)))
|
|
|
|
|
2010-07-10 15:02:58 +00:00
|
|
|
;;; Indentation
|
2008-09-23 06:03:13 +00:00
|
|
|
|
|
|
|
(defun org-get-string-indentation (s)
|
|
|
|
"What indentation has S due to SPACE and TAB at the beginning of the string?"
|
|
|
|
(let ((n -1) (i 0) (w tab-width) c)
|
|
|
|
(catch 'exit
|
|
|
|
(while (< (setq n (1+ n)) (length s))
|
|
|
|
(setq c (aref s n))
|
|
|
|
(cond ((= c ?\ ) (setq i (1+ i)))
|
|
|
|
((= c ?\t) (setq i (* (/ (+ w i) w) w)))
|
|
|
|
(t (throw 'exit t)))))
|
|
|
|
i))
|
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-shift-item-indentation (delta bottom)
|
2010-07-30 22:45:34 +00:00
|
|
|
"Shift the indentation in current item by DELTA.
|
2010-08-28 10:53:19 +00:00
|
|
|
Sub-items are not moved.
|
|
|
|
|
|
|
|
BOTTOM is position at list ending."
|
2010-07-29 17:11:06 +00:00
|
|
|
(save-excursion
|
|
|
|
(let ((beg (point-at-bol))
|
2010-08-28 10:53:19 +00:00
|
|
|
(end (org-end-of-item-or-at-child bottom)))
|
2010-07-30 22:45:34 +00:00
|
|
|
(beginning-of-line (unless (eolp) 0))
|
2010-07-29 17:11:06 +00:00
|
|
|
(while (> (point) beg)
|
|
|
|
(when (looking-at "[ \t]*\\S-")
|
|
|
|
;; this is not an empty line
|
|
|
|
(let ((i (org-get-indentation)))
|
|
|
|
(when (and (> i 0) (> (+ i delta) 0))
|
2010-11-14 07:58:41 +00:00
|
|
|
(org-indent-line-to (+ i delta)))))
|
2010-07-29 17:11:06 +00:00
|
|
|
(beginning-of-line 0)))))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-08-21 23:10:58 +00:00
|
|
|
(defun org-outdent-item ()
|
2010-08-28 10:53:19 +00:00
|
|
|
"Outdent a local list item, but not its children.
|
|
|
|
If a region is active, all items inside will be moved."
|
2010-08-21 23:10:58 +00:00
|
|
|
(interactive)
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-list-indent-item-generic
|
|
|
|
-1 t (org-list-top-point) (org-list-bottom-point)))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-08-21 23:10:58 +00:00
|
|
|
(defun org-indent-item ()
|
2010-08-28 10:53:19 +00:00
|
|
|
"Indent a local list item, but not its children.
|
|
|
|
If a region is active, all items inside will be moved."
|
2010-08-21 23:10:58 +00:00
|
|
|
(interactive)
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-list-indent-item-generic
|
|
|
|
1 t (org-list-top-point) (org-list-bottom-point)))
|
2010-08-21 11:32:33 +00:00
|
|
|
|
2010-08-21 23:10:58 +00:00
|
|
|
(defun org-outdent-item-tree ()
|
2010-08-28 10:53:19 +00:00
|
|
|
"Outdent a local list item including its children.
|
|
|
|
If a region is active, all items inside will be moved."
|
2010-08-21 23:10:58 +00:00
|
|
|
(interactive)
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-list-indent-item-generic
|
|
|
|
-1 nil (org-list-top-point) (org-list-bottom-point)))
|
2010-08-21 11:32:33 +00:00
|
|
|
|
2010-08-21 23:10:58 +00:00
|
|
|
(defun org-indent-item-tree ()
|
2010-08-28 10:53:19 +00:00
|
|
|
"Indent a local list item including its children.
|
|
|
|
If a region is active, all items inside will be moved."
|
2010-08-21 23:10:58 +00:00
|
|
|
(interactive)
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-list-indent-item-generic
|
|
|
|
1 nil (org-list-top-point) (org-list-bottom-point)))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-07-27 22:18:33 +00:00
|
|
|
(defvar org-tab-ind-state)
|
2009-11-02 21:01:38 +00:00
|
|
|
(defun org-cycle-item-indentation ()
|
2010-08-21 11:32:33 +00:00
|
|
|
"Cycle levels of indentation of an empty item.
|
2010-09-14 16:30:03 +00:00
|
|
|
The first run indent the item, if applicable. Subsequents runs
|
|
|
|
outdent it at meaningful levels in the list. When done, item is
|
2010-08-21 11:32:33 +00:00
|
|
|
put back at its original position with its original bullet.
|
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
Return t at each successful move."
|
|
|
|
(let ((org-adapt-indentation nil)
|
|
|
|
(ind (org-get-indentation))
|
|
|
|
(bottom (and (org-at-item-p) (org-list-bottom-point))))
|
2010-08-19 14:40:21 +00:00
|
|
|
(when (and (or (org-at-item-description-p)
|
|
|
|
(org-at-item-checkbox-p)
|
|
|
|
(org-at-item-p))
|
2010-08-28 10:53:19 +00:00
|
|
|
;; Check that item is really empty
|
2010-07-25 18:16:26 +00:00
|
|
|
(>= (match-end 0) (save-excursion
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-end-of-item-or-at-child bottom)
|
2010-07-25 18:16:26 +00:00
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(point))))
|
2009-11-02 21:01:38 +00:00
|
|
|
(setq this-command 'org-cycle-item-indentation)
|
2010-08-28 10:53:19 +00:00
|
|
|
(let ((top (org-list-top-point)))
|
|
|
|
;; 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
|
|
|
|
((ignore-errors (org-list-indent-item-generic -1 t top bottom)))
|
|
|
|
((and (= (org-get-indentation) (car org-tab-ind-state))
|
|
|
|
(ignore-errors
|
|
|
|
(org-list-indent-item-generic 1 t top bottom))))
|
|
|
|
(t (back-to-indentation)
|
2010-11-14 07:58:41 +00:00
|
|
|
(org-indent-to-column (car org-tab-ind-state))
|
2010-08-28 10:53:19 +00:00
|
|
|
(end-of-line)
|
|
|
|
(org-list-repair (cdr org-tab-ind-state))
|
|
|
|
;; 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 21:05:03 +00:00
|
|
|
(cond
|
2010-08-28 10:53:19 +00:00
|
|
|
((ignore-errors (org-list-indent-item-generic 1 t top bottom)))
|
|
|
|
((ignore-errors (org-list-indent-item-generic -1 t top bottom)))
|
|
|
|
(t (error "Cannot move item")))))
|
2010-07-22 07:47:11 +00:00
|
|
|
t)))
|
2009-11-02 21:01:38 +00:00
|
|
|
|
2010-07-10 15:02:58 +00:00
|
|
|
;;; Bullets
|
|
|
|
|
2009-07-06 06:53:50 +00:00
|
|
|
(defun org-get-bullet ()
|
2010-08-02 20:07:46 +00:00
|
|
|
"Return the bullet of the item at point.
|
|
|
|
Assume cursor is at an item."
|
2010-08-27 09:48:51 +00:00
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(and (looking-at "[ \t]*\\(\\S-+\\)") (match-string 1))))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2010-07-27 18:52:30 +00:00
|
|
|
(defun org-list-bullet-string (bullet)
|
2010-07-30 22:45:34 +00:00
|
|
|
"Return BULLET with the correct number of whitespaces.
|
2010-07-27 18:52:30 +00:00
|
|
|
It determines the number of whitespaces to append by looking at
|
|
|
|
`org-list-two-spaces-after-bullet-regexp'."
|
2010-07-29 09:44:54 +00:00
|
|
|
(save-match-data
|
2010-07-30 22:45:34 +00: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 11:32:33 +00:00
|
|
|
"Increment BULLET if applicable."
|
2010-07-30 22:45:34 +00:00
|
|
|
(if (string-match "[0-9]+" bullet)
|
|
|
|
(replace-match
|
2010-08-19 14:40:21 +00:00
|
|
|
(number-to-string (1+ (string-to-number (match-string 0 bullet))))
|
|
|
|
nil nil bullet)
|
2010-07-30 22:45:34 +00:00
|
|
|
bullet))
|
2010-07-27 18:52:30 +00:00
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
(defun org-list-repair (&optional force-bullet top bottom)
|
2010-08-17 19:29:49 +00:00
|
|
|
"Make sure all items are correctly indented, with the right bullet.
|
|
|
|
This function scans the list at point, along with any sublist.
|
|
|
|
|
2010-08-20 09:35:56 +00:00
|
|
|
If FORCE-BULLET is a string, ensure all items in list share this
|
|
|
|
bullet, or a logical successor in the case of an ordered list.
|
2010-08-17 19:29:49 +00:00
|
|
|
|
2010-08-28 10:53:19 +00:00
|
|
|
When non-nil, TOP and BOTTOM specify respectively position of
|
|
|
|
list beginning and list ending.
|
|
|
|
|
2010-08-17 19:29:49 +00:00
|
|
|
Item's body is not indented, only shifted with the bullet."
|
2010-07-10 15:02:58 +00:00
|
|
|
(interactive)
|
|
|
|
(unless (org-at-item-p) (error "This is not a list"))
|
2010-08-28 10:53:19 +00:00
|
|
|
(let* ((bottom (or bottom (org-list-bottom-point)))
|
|
|
|
(struct (org-list-struct
|
|
|
|
(point-at-bol) (point-at-eol)
|
|
|
|
(or top (org-list-top-point)) bottom))
|
2010-07-30 22:45:34 +00:00
|
|
|
(origins (org-list-struct-origins struct))
|
|
|
|
fixed-struct)
|
2010-08-20 09:35:56 +00:00
|
|
|
(if (stringp force-bullet)
|
2010-07-30 22:45:34 +00:00
|
|
|
(let ((begin (nth 1 struct)))
|
2010-08-19 14:40:21 +00:00
|
|
|
(setcdr begin (list (nth 1 begin)
|
|
|
|
(org-list-bullet-string force-bullet)
|
|
|
|
(nth 3 begin)))
|
|
|
|
(setq fixed-struct
|
|
|
|
(cons begin (org-list-struct-fix-struct struct origins))))
|
2010-07-30 22:45:34 +00:00
|
|
|
(setq fixed-struct (org-list-struct-fix-struct struct origins)))
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-list-struct-apply-struct fixed-struct bottom)))
|
2010-07-30 22:45:34 +00:00
|
|
|
|
2010-07-10 15:02:58 +00:00
|
|
|
(defun org-cycle-list-bullet (&optional which)
|
|
|
|
"Cycle through the different itemize/enumerate bullets.
|
2010-07-20 10:36:01 +00:00
|
|
|
This cycle the entire list level through the sequence:
|
2010-07-10 15:02:58 +00:00
|
|
|
|
2010-07-20 09:06:35 +00:00
|
|
|
`-' -> `+' -> `*' -> `1.' -> `1)'
|
2010-07-10 15:02:58 +00:00
|
|
|
|
2010-07-20 09:06:35 +00:00
|
|
|
If WHICH is a valid string, use that as the new bullet. If WHICH
|
2010-07-20 10:36:01 +00:00
|
|
|
is an integer, 0 means `-', 1 means `+' etc. If WHICH is
|
|
|
|
'previous, cycle backwards."
|
2010-07-10 15:02:58 +00:00
|
|
|
(interactive "P")
|
2010-11-19 22:47:02 +00:00
|
|
|
(save-excursion
|
|
|
|
(let* ((top (org-list-top-point))
|
|
|
|
(bullet (progn
|
|
|
|
(goto-char (org-get-beginning-of-list top))
|
|
|
|
(org-get-bullet)))
|
|
|
|
(current (cond
|
|
|
|
((string-match "\\." bullet) "1.")
|
|
|
|
((string-match ")" bullet) "1)")
|
|
|
|
(t bullet)))
|
|
|
|
(bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
|
|
|
|
(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))))))
|
|
|
|
(org-list-repair new top))))
|
2010-07-10 15:02:58 +00:00
|
|
|
|
|
|
|
;;; Checkboxes
|
|
|
|
|
|
|
|
(defun org-toggle-checkbox (&optional toggle-presence)
|
|
|
|
"Toggle the checkbox in the current line.
|
2010-07-28 13:53:37 +00: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 08:52:05 +00:00
|
|
|
first checkbox there, and make every item inside have the
|
|
|
|
same status or presence, respectively.
|
2010-07-28 13:53:37 +00: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 08:52:05 +00:00
|
|
|
in subtree, ignoring drawers."
|
2010-07-10 15:02:58 +00:00
|
|
|
(interactive "P")
|
2010-07-28 13:53:37 +00:00
|
|
|
;; Bounds is a list of type (beg end single-p) where single-p is t
|
|
|
|
;; when `org-toggle-checkbox' is applied to a single item. Only
|
|
|
|
;; toggles on single items will return errors.
|
|
|
|
(let* ((bounds
|
|
|
|
(cond
|
|
|
|
((org-region-active-p)
|
2010-09-04 08:52:05 +00:00
|
|
|
(let ((rbeg (region-beginning))
|
|
|
|
(rend (region-end)))
|
|
|
|
(save-excursion
|
|
|
|
(goto-char rbeg)
|
|
|
|
(if (org-search-forward-unenclosed org-item-beginning-re rend 'move)
|
|
|
|
(list (point-at-bol) rend nil)
|
|
|
|
(error "No item in region")))))
|
2010-07-28 13:53:37 +00:00
|
|
|
((org-on-heading-p)
|
2010-09-04 08:52:05 +00:00
|
|
|
;; In this case, reference line is the first item in
|
|
|
|
;; subtree outside drawers
|
|
|
|
(let ((pos (point))
|
|
|
|
(limit (save-excursion (outline-next-heading) (point))))
|
2010-07-28 13:53:37 +00:00
|
|
|
(save-excursion
|
2010-09-04 08:52:05 +00:00
|
|
|
(goto-char limit)
|
|
|
|
(org-search-backward-unenclosed ":END:" pos 'move)
|
2010-08-19 14:40:21 +00:00
|
|
|
(org-search-forward-unenclosed
|
|
|
|
org-item-beginning-re limit 'move)
|
2010-07-28 13:53:37 +00:00
|
|
|
(list (point) limit nil))))
|
|
|
|
((org-at-item-p)
|
2010-09-30 19:55:45 +00:00
|
|
|
(list (point-at-bol) (1+ (point-at-eol)) t))
|
2010-07-28 13:53:37 +00:00
|
|
|
(t (error "Not at an item or heading, and no active region"))))
|
2010-09-04 08:52:05 +00:00
|
|
|
(beg (car bounds))
|
|
|
|
;; marker is needed because deleting or inserting checkboxes
|
|
|
|
;; will change bottom point
|
2010-07-28 13:53:37 +00:00
|
|
|
(end (copy-marker (nth 1 bounds)))
|
|
|
|
(single-p (nth 2 bounds))
|
2010-08-19 14:40:21 +00:00
|
|
|
(ref-presence (save-excursion
|
2010-09-04 08:52:05 +00:00
|
|
|
(goto-char beg)
|
2010-08-19 14:40:21 +00:00
|
|
|
(org-at-item-checkbox-p)))
|
2010-07-28 13:53:37 +00:00
|
|
|
(ref-status (equal (match-string 1) "[X]"))
|
|
|
|
(act-on-item
|
|
|
|
(lambda (ref-pres ref-stat)
|
|
|
|
(if (equal toggle-presence '(4))
|
|
|
|
(cond
|
|
|
|
((and ref-pres (org-at-item-checkbox-p))
|
|
|
|
(replace-match ""))
|
|
|
|
((and (not ref-pres)
|
|
|
|
(not (org-at-item-checkbox-p))
|
|
|
|
(org-at-item-p))
|
|
|
|
(goto-char (match-end 0))
|
|
|
|
;; Ignore counter, if any
|
2010-08-20 11:05:38 +00:00
|
|
|
(when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
|
2010-07-28 13:53:37 +00:00
|
|
|
(goto-char (match-end 0)))
|
|
|
|
(let ((desc-p (and (org-at-item-description-p)
|
|
|
|
(cdr (assq 'checkbox org-list-automatic-rules)))))
|
|
|
|
(cond
|
|
|
|
((and single-p desc-p)
|
|
|
|
(error "Cannot add a checkbox in a description list"))
|
|
|
|
((not desc-p) (insert "[ ] "))))))
|
|
|
|
(let ((blocked (org-checkbox-blocked-p)))
|
|
|
|
(cond
|
|
|
|
((and blocked single-p)
|
|
|
|
(error "Checkbox blocked because of unchecked box in line %d" blocked))
|
|
|
|
(blocked nil)
|
|
|
|
((org-at-item-checkbox-p)
|
|
|
|
(replace-match
|
|
|
|
(cond ((equal toggle-presence '(16)) "[-]")
|
|
|
|
(ref-stat "[ ]")
|
|
|
|
(t "[X]"))
|
|
|
|
t t nil 1))))))))
|
|
|
|
(save-excursion
|
2010-09-04 08:52:05 +00:00
|
|
|
(goto-char beg)
|
2010-07-28 13:53:37 +00:00
|
|
|
(while (< (point) end)
|
|
|
|
(funcall act-on-item ref-presence ref-status)
|
|
|
|
(org-search-forward-unenclosed org-item-beginning-re end 'move)))
|
|
|
|
(org-update-checkbox-count-maybe)))
|
2010-07-10 15:02:58 +00: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 15:02:58 +00:00
|
|
|
(replace-match "[ ]" t t nil 1))
|
2010-07-10 15:02:58 +00: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 09:35:56 +00: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 18:36:42 +00:00
|
|
|
implement alternative ways of collecting statistics
|
|
|
|
information.")
|
2010-07-10 15:02:58 +00:00
|
|
|
|
|
|
|
(defun org-update-checkbox-count-maybe ()
|
|
|
|
"Update checkbox statistics unless turned off by user."
|
2010-07-23 18:36:42 +00:00
|
|
|
(when (cdr (assq 'checkbox org-list-automatic-rules))
|
2010-07-10 15:02:58 +00: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.
|
|
|
|
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."
|
|
|
|
(interactive "P")
|
|
|
|
(save-excursion
|
2010-09-04 08:52:05 +00:00
|
|
|
(let ((cstat 0))
|
|
|
|
(catch 'exit
|
|
|
|
(while t
|
|
|
|
(let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
|
|
|
|
(beg (condition-case nil
|
|
|
|
(progn (org-back-to-heading) (point))
|
|
|
|
(error (point-min))))
|
|
|
|
(end (copy-marker (save-excursion
|
|
|
|
(outline-next-heading) (point))))
|
|
|
|
(re-cookie "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
|
|
|
|
(re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
|
|
|
|
beg-cookie end-cookie is-percent c-on c-off lim new
|
|
|
|
curr-ind next-ind continue-from startsearch list-beg list-end
|
|
|
|
(recursive
|
|
|
|
(or (not org-hierarchical-checkbox-statistics)
|
|
|
|
(string-match "\\<recursive\\>"
|
|
|
|
(or (ignore-errors
|
|
|
|
(org-entry-get nil "COOKIE_DATA"))
|
|
|
|
"")))))
|
|
|
|
(goto-char end)
|
|
|
|
;; find each statistics cookie
|
|
|
|
(while (and (org-search-backward-unenclosed re-cookie beg 'move)
|
|
|
|
(not (save-match-data
|
|
|
|
(and (org-on-heading-p)
|
|
|
|
(string-match "\\<todo\\>"
|
|
|
|
(downcase
|
|
|
|
(or (org-entry-get
|
|
|
|
nil "COOKIE_DATA")
|
|
|
|
"")))))))
|
|
|
|
(setq beg-cookie (match-beginning 1)
|
|
|
|
end-cookie (match-end 1)
|
|
|
|
cstat (+ cstat (if end-cookie 1 0))
|
|
|
|
startsearch (point-at-eol)
|
|
|
|
continue-from (match-beginning 0)
|
|
|
|
is-percent (match-beginning 2)
|
|
|
|
lim (cond
|
|
|
|
((org-on-heading-p) (outline-next-heading) (point))
|
|
|
|
;; Ensure many cookies in the same list won't imply
|
|
|
|
;; computing list boundaries as many times.
|
|
|
|
((org-at-item-p)
|
|
|
|
(unless (and list-beg (>= (point) list-beg))
|
|
|
|
(setq list-beg (org-list-top-point)
|
|
|
|
list-end (copy-marker
|
|
|
|
(org-list-bottom-point))))
|
|
|
|
(org-get-end-of-item list-end))
|
|
|
|
(t nil))
|
|
|
|
c-on 0
|
|
|
|
c-off 0)
|
|
|
|
(when lim
|
|
|
|
;; find first checkbox for this cookie and gather
|
|
|
|
;; statistics from all that are at this indentation level
|
|
|
|
(goto-char startsearch)
|
|
|
|
(if (org-search-forward-unenclosed re-box lim t)
|
|
|
|
(progn
|
|
|
|
(beginning-of-line)
|
|
|
|
(setq curr-ind (org-get-indentation))
|
|
|
|
(setq next-ind curr-ind)
|
|
|
|
(while (and (bolp) (org-at-item-p)
|
|
|
|
(if recursive
|
|
|
|
(<= curr-ind next-ind)
|
|
|
|
(= curr-ind next-ind)))
|
|
|
|
(when (org-at-item-checkbox-p)
|
|
|
|
(if (member (match-string 1) '("[ ]" "[-]"))
|
|
|
|
(setq c-off (1+ c-off))
|
|
|
|
(setq c-on (1+ c-on))))
|
|
|
|
(if (not recursive)
|
|
|
|
;; org-get-next-item goes through list-enders
|
|
|
|
;; with proper limit.
|
|
|
|
(goto-char (or (org-get-next-item (point) lim) lim))
|
|
|
|
(end-of-line)
|
|
|
|
(when (org-search-forward-unenclosed
|
|
|
|
org-item-beginning-re lim t)
|
|
|
|
(beginning-of-line)))
|
|
|
|
(setq next-ind (org-get-indentation)))))
|
|
|
|
(goto-char continue-from)
|
|
|
|
;; update cookie
|
|
|
|
(when end-cookie
|
|
|
|
(setq new (if is-percent
|
|
|
|
(format "[%d%%]" (/ (* 100 c-on)
|
|
|
|
(max 1 (+ c-on c-off))))
|
|
|
|
(format "[%d/%d]" c-on (+ c-on c-off))))
|
|
|
|
(goto-char beg-cookie)
|
|
|
|
(insert new)
|
|
|
|
(delete-region (point) (+ (point) (- end-cookie beg-cookie))))
|
|
|
|
;; update items checkbox if it has one
|
|
|
|
(when (and (org-at-item-checkbox-p)
|
|
|
|
(> (+ c-on c-off) 0))
|
|
|
|
(setq beg-cookie (match-beginning 1)
|
|
|
|
end-cookie (match-end 1))
|
|
|
|
(delete-region beg-cookie end-cookie)
|
|
|
|
(goto-char beg-cookie)
|
|
|
|
(cond ((= c-off 0) (insert "[X]"))
|
|
|
|
((= c-on 0) (insert "[ ]"))
|
|
|
|
(t (insert "[-]")))))
|
|
|
|
(goto-char continue-from)))
|
|
|
|
(unless (and all (outline-next-heading)) (throw 'exit nil))))
|
2010-07-10 15:02:58 +00:00
|
|
|
(when (interactive-p)
|
2010-09-04 08:52:05 +00:00
|
|
|
(message "Checkbox statistics updated %s (%d places)"
|
|
|
|
(if all "in entire file" "in current outline entry") cstat)))))
|
2010-07-10 15:02:58 +00: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 15:40:00 +00:00
|
|
|
(defun org-apply-on-list (function init-value &rest args)
|
2010-09-14 16:30:03 +00:00
|
|
|
"Call FUNCTION on each item of the list at point.
|
2010-08-21 11:32:33 +00: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 15:02:58 +00:00
|
|
|
|
2010-07-12 15:40:00 +00: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 16:30:03 +00:00
|
|
|
Sublists of the list are skipped. Cursor is always at the
|
2010-07-12 15:40:00 +00:00
|
|
|
beginning of the item."
|
2010-08-17 20:21:24 +00:00
|
|
|
(let* ((pos (copy-marker (point)))
|
|
|
|
(end (copy-marker (org-list-bottom-point)))
|
2010-08-28 10:53:19 +00:00
|
|
|
(next-p (copy-marker (org-get-beginning-of-list (org-list-top-point))))
|
2010-08-17 20:21:24 +00:00
|
|
|
(value init-value))
|
|
|
|
(while (< next-p end)
|
|
|
|
(goto-char next-p)
|
|
|
|
(set-marker next-p (or (org-get-next-item (point) end) end))
|
|
|
|
(setq value (apply function value args)))
|
|
|
|
(goto-char pos)
|
|
|
|
value))
|
2010-07-10 15:02:58 +00: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 11:55:44 +00:00
|
|
|
Sublists are not sorted. Checkboxes, if any, are ignored.
|
2010-07-10 15:02:58 +00:00
|
|
|
|
|
|
|
Sorting can be alphabetically, numerically, by date/time as given by
|
|
|
|
a time stamp, by a property or by priority.
|
|
|
|
|
2010-08-21 11:32:33 +00:00
|
|
|
Comparing entries ignores case by default. However, with an
|
|
|
|
optional argument WITH-CASE, the sorting considers case as well.
|
|
|
|
|
2010-07-17 11:56:11 +00: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 09:19:08 +00:00
|
|
|
be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
|
2010-07-17 11:56:11 +00:00
|
|
|
meaning of each character:
|
2010-07-10 15:02:58 +00:00
|
|
|
|
2010-07-17 11:56:11 +00:00
|
|
|
n Numerically, by converting the beginning of the item to a number.
|
2010-07-28 09:19:08 +00:00
|
|
|
a Alphabetically. Only the first line of item is checked.
|
2010-07-17 11:56:11 +00:00
|
|
|
t By date/time, either the first active time stamp in the entry, if
|
2010-07-28 09:19:08 +00:00
|
|
|
any, or by the first inactive one. In a timer list, sort the timers.
|
2010-07-10 15:02:58 +00: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 11:32:33 +00:00
|
|
|
the sorting key for that record. It will then use COMPARE-FUNC to
|
|
|
|
compare entries."
|
2010-07-10 15:02:58 +00:00
|
|
|
(interactive "P")
|
|
|
|
(let* ((case-func (if with-case 'identity 'downcase))
|
2010-08-28 10:53:19 +00:00
|
|
|
(top (org-list-top-point))
|
|
|
|
(bottom (org-list-bottom-point))
|
|
|
|
(start (org-get-beginning-of-list top))
|
|
|
|
(end (org-get-end-of-list bottom))
|
2010-07-10 15:02:58 +00: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 12:26:40 +00:00
|
|
|
(goto-char (point-min))
|
2010-07-10 15:02:58 +00: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 11:56:11 +00:00
|
|
|
((= dcst ?t) '<)
|
2010-07-10 15:02:58 +00:00
|
|
|
(t nil)))
|
|
|
|
(begin-record (lambda ()
|
2010-07-12 12:31:20 +00:00
|
|
|
(skip-chars-forward " \r\t\n")
|
|
|
|
(beginning-of-line)))
|
2010-07-10 15:02:58 +00:00
|
|
|
(end-record (lambda ()
|
2010-08-28 10:53:19 +00:00
|
|
|
(goto-char (org-end-of-item-before-blank end))))
|
2010-07-17 11:56:11 +00: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 13:17:38 +00:00
|
|
|
((org-at-item-timer-p)
|
2010-07-17 11:56:11 +00:00
|
|
|
(org-timer-hms-to-secs (match-string 1)))
|
|
|
|
((or (org-search-forward-unenclosed org-ts-regexp
|
|
|
|
(point-at-eol) t)
|
|
|
|
(org-search-forward-unenclosed org-ts-regexp-both
|
|
|
|
(point-at-eol) t))
|
|
|
|
(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 14:40:21 +00:00
|
|
|
(sort-subr (/= dcst sorting-type)
|
|
|
|
begin-record
|
|
|
|
end-record
|
|
|
|
value-to-sort
|
|
|
|
nil
|
|
|
|
sort-func)
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-list-repair nil top bottom)
|
2010-07-10 15:02:58 +00:00
|
|
|
(run-hooks 'org-after-sorting-entries-or-items-hook)
|
|
|
|
(message "Sorting items...done")))))
|
|
|
|
|
2008-09-23 06:13:32 +00:00
|
|
|
;;; Send and receive lists
|
2008-09-23 06:03:13 +00:00
|
|
|
|
|
|
|
(defun org-list-parse-list (&optional delete)
|
|
|
|
"Parse the list at point and maybe DELETE it.
|
|
|
|
Return a list containing first level items as strings and
|
|
|
|
sublevels as a list of strings."
|
2010-07-10 15:02:58 +00:00
|
|
|
(let* ((start (goto-char (org-list-top-point)))
|
2010-07-11 17:24:35 +00:00
|
|
|
(end (org-list-bottom-point))
|
2008-12-16 16:17:15 +00:00
|
|
|
output itemsep ltype)
|
2010-07-20 19:13:37 +00:00
|
|
|
(while (org-search-forward-unenclosed org-item-beginning-re end t)
|
2010-07-10 15:02:58 +00:00
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
2010-11-03 22:16:37 +00:00
|
|
|
(setq ltype (cond ((org-looking-at-p "^[ \t]*[0-9]") 'ordered)
|
2010-07-27 22:05:42 +00:00
|
|
|
((org-at-item-description-p) 'descriptive)
|
2010-07-19 23:20:56 +00:00
|
|
|
(t 'unordered))))
|
2010-07-10 15:02:58 +00:00
|
|
|
(let* ((indent1 (org-get-indentation))
|
|
|
|
(nextitem (or (org-get-next-item (point) end) end))
|
2010-08-19 14:40:21 +00:00
|
|
|
(item (org-trim (buffer-substring (point)
|
2010-08-28 10:53:19 +00:00
|
|
|
(org-end-of-item-or-at-child end))))
|
2010-07-13 12:40:39 +00:00
|
|
|
(nextindent (if (= (point) end) 0 (org-get-indentation)))
|
2010-08-19 14:40:21 +00:00
|
|
|
(item (if (string-match
|
2010-08-20 11:05:38 +00:00
|
|
|
"^\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[\\([xX ]\\)\\]"
|
2010-08-19 14:40:21 +00:00
|
|
|
item)
|
2009-01-06 16:23:36 +00:00
|
|
|
(replace-match (if (equal (match-string 1 item) " ")
|
2010-07-28 13:53:37 +00:00
|
|
|
"CBOFF"
|
|
|
|
"CBON")
|
|
|
|
t nil item 1)
|
2009-01-06 16:23:36 +00:00
|
|
|
item)))
|
2008-09-23 06:03:13 +00:00
|
|
|
(push item output)
|
2010-07-10 15:02:58 +00:00
|
|
|
(when (> nextindent indent1)
|
2010-07-10 21:56:41 +00:00
|
|
|
(save-restriction
|
|
|
|
(narrow-to-region (point) nextitem)
|
|
|
|
(push (org-list-parse-list) output)))))
|
2010-07-11 13:39:49 +00:00
|
|
|
(when delete
|
|
|
|
(delete-region start end)
|
2010-07-11 17:24:35 +00:00
|
|
|
(save-match-data
|
2010-08-19 14:09:45 +00:00
|
|
|
(when (and (not (eq org-list-ending-method 'indent))
|
|
|
|
(looking-at (org-list-end-re)))
|
2010-07-11 17:24:35 +00:00
|
|
|
(replace-match "\n"))))
|
2008-09-23 06:03:13 +00:00
|
|
|
(setq output (nreverse output))
|
|
|
|
(push ltype output)))
|
|
|
|
|
2009-07-25 20:48:32 +00:00
|
|
|
(defun org-list-make-subtree ()
|
|
|
|
"Convert the plain list at point into a subtree."
|
|
|
|
(interactive)
|
2010-07-20 09:47:54 +00:00
|
|
|
(if (not (org-in-item-p))
|
2010-08-21 11:32:33 +00:00
|
|
|
(error "Not in a list")
|
2010-07-20 09:47:54 +00:00
|
|
|
(let ((list (org-list-parse-list t)) nstars)
|
|
|
|
(save-excursion
|
|
|
|
(if (ignore-errors
|
|
|
|
(org-back-to-heading))
|
2010-08-23 09:53:08 +00:00
|
|
|
(progn (looking-at org-complex-heading-regexp)
|
2010-07-20 09:47:54 +00:00
|
|
|
(setq nstars (length (match-string 1))))
|
|
|
|
(setq nstars 0)))
|
|
|
|
(org-list-make-subtrees list (1+ nstars)))))
|
2009-07-25 20:48:32 +00: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 15:30:30 +00:00
|
|
|
(insert (make-string
|
|
|
|
(if org-odd-levels-only
|
2009-07-25 20:48:32 +00:00
|
|
|
(1- (* 2 level)) level) ?*) " " item "\n")
|
|
|
|
(org-list-make-subtrees item (1+ level))))
|
|
|
|
list)))
|
|
|
|
|
2008-09-23 06:03:13 +00: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 12:39:29 +00:00
|
|
|
"Send a transformed version of this list to the receiver position.
|
2008-09-23 06:03:13 +00:00
|
|
|
With argument MAYBE, fail quietly if no transformation is defined for
|
|
|
|
this list."
|
|
|
|
(interactive)
|
|
|
|
(catch 'exit
|
2010-08-28 10:53:19 +00:00
|
|
|
(unless (org-at-item-p) (error "Not at a list item"))
|
2008-09-23 06:03:13 +00:00
|
|
|
(save-excursion
|
2010-07-14 09:11:10 +00:00
|
|
|
(re-search-backward "#\\+ORGLST" nil t)
|
2010-07-12 09:16:42 +00:00
|
|
|
(unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
|
2008-09-23 06:03:13 +00: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 21:56:41 +00:00
|
|
|
(bottom-point
|
|
|
|
(save-excursion
|
2010-08-19 14:40:21 +00:00
|
|
|
(re-search-forward
|
|
|
|
"\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
|
2010-07-14 09:11:10 +00:00
|
|
|
(match-beginning 0)))
|
|
|
|
(top-point
|
|
|
|
(progn
|
|
|
|
(re-search-backward "#\\+ORGLST" nil t)
|
2010-07-20 19:13:37 +00:00
|
|
|
(re-search-forward org-item-beginning-re bottom-point t)
|
2010-07-14 09:11:10 +00:00
|
|
|
(match-beginning 0)))
|
2010-07-10 21:56:41 +00:00
|
|
|
(list (save-restriction
|
|
|
|
(narrow-to-region top-point bottom-point)
|
|
|
|
(org-list-parse-list)))
|
|
|
|
beg txt)
|
2008-09-23 06:03:13 +00:00
|
|
|
(unless (fboundp transform)
|
|
|
|
(error "No such transformation function %s" transform))
|
2010-07-16 06:49:37 +00:00
|
|
|
(let ((txt (funcall transform list)))
|
|
|
|
;; Find the insertion place
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(unless (re-search-forward
|
2010-08-19 14:40:21 +00:00
|
|
|
(concat "BEGIN RECEIVE ORGLST +"
|
|
|
|
name
|
|
|
|
"\\([ \t]\\|$\\)") nil t)
|
2010-07-16 06:49:37 +00: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 07:16:13 +00:00
|
|
|
(delete-region beg (point-at-bol))
|
2010-07-16 06:49:37 +00:00
|
|
|
(goto-char beg)
|
|
|
|
(insert txt "\n")))
|
2008-09-23 06:03:13 +00: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 16:17:15 +00:00
|
|
|
:ustart String to start an unordered list
|
|
|
|
:uend String to end an unordered list
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2008-12-16 16:17:15 +00:00
|
|
|
:ostart String to start an ordered list
|
|
|
|
:oend String to end an ordered list
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2008-12-16 16:17:15 +00:00
|
|
|
:dstart String to start a descriptive list
|
|
|
|
:dend String to end a descriptive list
|
2008-09-23 06:03:13 +00:00
|
|
|
:dtstart String to start a descriptive term
|
2008-12-16 16:17:15 +00:00
|
|
|
:dtend String to end a descriptive term
|
2008-09-23 06:03:13 +00:00
|
|
|
:ddstart String to start a description
|
2008-12-16 16:17:15 +00:00
|
|
|
:ddend String to end a description
|
2008-09-23 06:03:13 +00:00
|
|
|
|
2008-12-16 16:17:15 +00: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 06:03:13 +00:00
|
|
|
|
2008-12-16 16:17:15 +00:00
|
|
|
:istart String to start a list item
|
|
|
|
:iend String to end a list item
|
|
|
|
:isep String to separate items
|
2009-01-06 16:23:36 +00: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 06:03:13 +00:00
|
|
|
(interactive)
|
|
|
|
(let* ((p params) sublist
|
|
|
|
(splicep (plist-get p :splice))
|
2010-07-10 15:02:58 +00: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))
|
2009-01-06 16:23:36 +00:00
|
|
|
(cboff (plist-get p :cboff)))
|
2008-09-23 06:03:13 +00:00
|
|
|
(let ((wrapper
|
|
|
|
(cond ((eq (car list) 'ordered)
|
|
|
|
(concat ostart "\n%s" oend "\n"))
|
|
|
|
((eq (car list) 'unordered)
|
|
|
|
(concat ustart "\n%s" uend "\n"))
|
|
|
|
((eq (car list) 'descriptive)
|
|
|
|
(concat dstart "\n%s" dend "\n"))))
|
|
|
|
rtn term defstart defend)
|
|
|
|
(while (setq sublist (pop list))
|
|
|
|
(cond ((symbolp sublist) nil)
|
|
|
|
((stringp sublist)
|
2010-09-09 13:35:46 +00:00
|
|
|
(when (string-match "^\\(.*\\)[ \t]+::" sublist)
|
2008-12-16 16:17:15 +00:00
|
|
|
(setq term (org-trim (format (concat dtstart "%s" dtend)
|
|
|
|
(match-string 1 sublist))))
|
2010-07-10 15:02:58 +00:00
|
|
|
(setq sublist (concat ddstart
|
2010-08-19 14:40:21 +00:00
|
|
|
(org-trim (substring sublist
|
|
|
|
(match-end 0)))
|
2010-07-10 15:02:58 +00:00
|
|
|
ddend)))
|
2009-01-06 16:23:36 +00:00
|
|
|
(if (string-match "\\[CBON\\]" sublist)
|
|
|
|
(setq sublist (replace-match cbon t t sublist)))
|
|
|
|
(if (string-match "\\[CBOFF\\]" sublist)
|
|
|
|
(setq sublist (replace-match cboff t t sublist)))
|
2009-11-06 16:11:44 +00:00
|
|
|
(if (string-match "\\[-\\]" sublist)
|
|
|
|
(setq sublist (replace-match "$\\boxminus$" t t sublist)))
|
2010-07-10 15:02:58 +00:00
|
|
|
(setq rtn (concat rtn istart term sublist iend isep)))
|
|
|
|
(t (setq rtn (concat rtn ;; previous list
|
|
|
|
lsep ;; list separator
|
2008-12-16 16:17:15 +00:00
|
|
|
(org-list-to-generic sublist p)
|
2010-07-10 15:02:58 +00:00
|
|
|
lsep ;; list separator
|
2008-12-16 16:17:15 +00:00
|
|
|
)))))
|
2008-09-23 06:03:13 +00:00
|
|
|
(format wrapper rtn))))
|
|
|
|
|
2009-01-06 16:23:36 +00:00
|
|
|
(defun org-list-to-latex (list &optional params)
|
|
|
|
"Convert LIST into a LaTeX list.
|
2010-07-16 21:22:01 +00:00
|
|
|
LIST is as returned by `org-list-parse-list'. PARAMS is a property list
|
2009-01-06 16:23:36 +00:00
|
|
|
with overruling parameters for `org-list-to-generic'."
|
2008-09-23 06:03:13 +00:00
|
|
|
(org-list-to-generic
|
2009-01-06 16:23:36 +00: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}"
|
|
|
|
:dtstart "[" :dtend "]"
|
|
|
|
: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 21:22:01 +00:00
|
|
|
LIST is as returned by `org-list-parse-list'. PARAMS is a property list
|
2009-01-06 16:23:36 +00:00
|
|
|
with overruling parameters for `org-list-to-generic'."
|
2008-09-23 06:03:13 +00:00
|
|
|
(org-list-to-generic
|
2009-01-06 16:23:36 +00: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 21:22:01 +00:00
|
|
|
LIST is as returned by `org-list-parse-list'. PARAMS is a property list
|
2009-01-06 16:23:36 +00:00
|
|
|
with overruling parameters for `org-list-to-generic'."
|
2008-09-23 06:03:13 +00:00
|
|
|
(org-list-to-generic
|
2009-06-08 06:00:46 +00:00
|
|
|
list
|
2009-01-06 16:23:36 +00:00
|
|
|
(org-combine-plists
|
|
|
|
'(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
|
|
|
|
:ustart "@enumerate" :uend "@end enumerate"
|
|
|
|
:dstart "@table" :dend "@end table"
|
|
|
|
:dtstart "@item " :dtend "\n"
|
|
|
|
:ddstart "" :ddend ""
|
|
|
|
:istart "@item\n" :iend ""
|
|
|
|
:isep "\n" :lsep "\n"
|
|
|
|
:cbon "@code{[X]}" :cboff "@code{[ ]}")
|
|
|
|
params)))
|
2008-09-23 06:03:13 +00:00
|
|
|
|
|
|
|
(provide 'org-list)
|
|
|
|
|
2008-10-15 08:20:24 +00:00
|
|
|
;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
|
2008-09-23 06:03:13 +00:00
|
|
|
;;; org-list.el ends here
|