annotate lisp/prim/paragraphs.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children b82b59fe008d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; paragraphs.el --- paragraph and sentence parsing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1985, 1986, 1987, 1991, 1993, 1994, 1995
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Maintainer: FSF
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; Keywords: wp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; Synched up with: FSF 19.30.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; This package provides the paragraph-oriented commands documented in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; XEmacs Reference Manual.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 (defvar use-hard-newlines nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 "Non-nil means to distinguish hard and soft newlines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 When this is non-nil, the functions `newline' and `open-line' add the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 text-property `hard' to newlines that they insert. Also, a line is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 only considered as a candidate to match `paragraph-start' or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 `paragraph-separate' if it follows a hard newline. Newlines not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 marked hard are called \"soft\", and are always internal to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 paragraphs. The fill functions always insert soft newlines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 Each buffer has its own value of this variable.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 (make-variable-buffer-local 'use-hard-newlines)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (defconst paragraph-start (purecopy "[ \t\n\f]") "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 *Regexp for beginning of a line that starts OR separates paragraphs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 This regexp should match lines that separate paragraphs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 and should also match lines that start a paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 \(and are part of that paragraph).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 This is matched against the text at the left margin, which is not necessarily
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 the beginning of the line, so it should never use \"^\" as an anchor. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ensures that the paragraph functions will work equally well within a region
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 of text indented by a margin setting.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 The variable `paragraph-separate' specifies how to distinguish
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 lines that start paragraphs from lines that separate them.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 If the variable `use-hard-newlines' is nonnil, then only lines following a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 hard newline are considered to match.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 ;; It is assumed that paragraph-separate is distinctive enough to be believed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ;; whenever it occurs, while it is reasonable to set paragraph-start to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 ;; something very minimal, even including "." (which makes every hard newline
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ;; start a new paragraph).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (defconst paragraph-separate (purecopy "[ \t\f]*$") "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 *Regexp for beginning of a line that separates paragraphs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 If you change this, you may have to change paragraph-start also.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 This is matched against the text at the left margin, which is not necessarily
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 the beginning of the line, so it should not use \"^\" as an anchor. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 ensures that the paragraph functions will work equally within a region of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 text indented by a margin setting.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 *Regexp describing the end of a sentence.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 All paragraph boundaries also end sentences, regardless.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 In order to be recognized as the end of a sentence, the ending period,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 question mark, or exclamation point must be followed by two spaces,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 unless it's inside some sort of quotes or parenthesis.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (defconst page-delimiter (purecopy "^\014") "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 *Regexp describing line-beginnings that separate pages.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (defvar paragraph-ignore-fill-prefix nil "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 Non-nil means the paragraph commands are not affected by `fill-prefix'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 This is desirable in modes where blank lines are the paragraph delimiters.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (defun forward-paragraph (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 "Move forward to end of paragraph.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 With arg N, do it N times; negative arg -N means move backward N paragraphs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 A line which `paragraph-start' matches either separates paragraphs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 A paragraph end is the beginning of a line which is not part of the paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 to which the end of the previous line belongs, or the end of the buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (interactive "_p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (or arg (setq arg 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (let* ((fill-prefix-regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (and fill-prefix (not (equal fill-prefix ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (not paragraph-ignore-fill-prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (regexp-quote fill-prefix)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 ;; These regexps shouldn't be anchored, because we look for them
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 ;; starting at the left-margin. This allows paragraph commands to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 ;; work normally with indented text.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 ;; This hack will not find problem cases like "whatever\\|^something".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (paragraph-start (if (and (not (equal "" paragraph-start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (equal ?^ (aref paragraph-start 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (substring paragraph-start 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 paragraph-start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (paragraph-separate (if (and (not (equal "" paragraph-start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (equal ?^ (aref paragraph-separate 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (substring paragraph-separate 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 paragraph-separate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (if fill-prefix-regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (concat paragraph-separate "\\|"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 fill-prefix-regexp "[ \t]*$")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 paragraph-separate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 ;; This is used for searching.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (while (and (< arg 0) (not (bobp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (if (and (not (looking-at paragraph-separate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (looking-at paragraph-separate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (setq start (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 ;; Move back over paragraph-separating lines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (forward-char -1) (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (while (and (not (bobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (progn (move-to-left-margin)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (looking-at paragraph-separate)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (forward-line -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (if (bobp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 ;; Go to end of the previous (non-separating) line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 ;; Search back for line that starts or separates paragraphs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (if (if fill-prefix-regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 ;; There is a fill prefix; it overrides paragraph-start.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (let (multiple-lines)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (while (and (progn (beginning-of-line) (not (bobp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (progn (move-to-left-margin)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (not (looking-at paragraph-separate)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (looking-at fill-prefix-regexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (if (not (= (point) start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (setq multiple-lines t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (forward-line -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (move-to-left-margin)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 ;; Don't move back over a line before the paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 ;; which doesn't start with fill-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 ;; unless that is the only line we've moved over.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (and (not (looking-at fill-prefix-regexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 multiple-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (not (bobp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (while (and (re-search-backward sp-paragraph-start nil 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 ;; Found a candidate, but need to check if it is a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 ;; REAL paragraph-start.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (not (bobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (progn (setq start (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (move-to-left-margin)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (not (looking-at paragraph-separate)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (or (not (looking-at paragraph-start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (and use-hard-newlines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (not (get-text-property (1- start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 'hard)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (goto-char start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (> (point) (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 ;; Found one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 ;; Move forward over paragraph separators.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 ;; We know this cannot reach the place we started
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 ;; because we know we moved back over a non-separator.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (while (and (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 (progn (move-to-left-margin)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 (looking-at paragraph-separate)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 ;; If line before paragraph is just margin, back up to there.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (end-of-line 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (if (> (current-column) (current-left-margin))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (forward-char 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 (if (not (bolp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (forward-line 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 ;; No starter or separator line => use buffer beg.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (goto-char (point-min))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 (setq arg (1+ arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 (while (and (> arg 0) (not (eobp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 (while (prog1 (and (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (progn (move-to-left-margin) (not (eobp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (looking-at paragraph-separate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 (forward-line 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (if fill-prefix-regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 ;; There is a fill prefix; it overrides paragraph-start.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 (while (and (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 (progn (move-to-left-margin) (not (eobp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (not (looking-at paragraph-separate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (looking-at fill-prefix-regexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (while (and (re-search-forward sp-paragraph-start nil 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 (progn (setq start (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (goto-char start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (not (eobp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 (progn (move-to-left-margin)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (not (looking-at paragraph-separate)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (or (not (looking-at paragraph-start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 (and use-hard-newlines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (not (get-text-property (1- start) 'hard)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (forward-char 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 (if (< (point) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (goto-char start)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (setq arg (1- arg)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (defun backward-paragraph (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 "Move backward to start of paragraph.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 With arg N, do it N times; negative arg -N means move forward N paragraphs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 A paragraph start is the beginning of a line which is a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 `first-line-of-paragraph' or which is ordinary text and follows a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 paragraph-separating line; except: if the first real line of a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 paragraph is preceded by a blank line, the paragraph starts at that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 blank line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 See `forward-paragraph' for more information."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 (interactive "_p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (or arg (setq arg 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (forward-paragraph (- arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 (defun mark-paragraph ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 "Put point at beginning of this paragraph, mark at end.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 The paragraph marked is the one that contains point or follows point."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (forward-paragraph 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (push-mark nil t t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (backward-paragraph 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (defun kill-paragraph (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 "Kill forward to end of paragraph.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 With arg N, kill forward to Nth end of paragraph;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 negative arg -N means kill backward to Nth start of paragraph."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 (interactive "*p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (kill-region (point) (progn (forward-paragraph arg) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (defun backward-kill-paragraph (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 "Kill back to start of paragraph.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 With arg N, kill back to Nth start of paragraph;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 negative arg -N means kill forward to Nth end of paragraph."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (interactive "*p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (kill-region (point) (progn (backward-paragraph arg) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (defun transpose-paragraphs (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 "Interchange this (or next) paragraph with previous one."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 (interactive "*p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 (transpose-subr 'forward-paragraph arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (defun start-of-paragraph-text ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (let ((opoint (point)) npoint)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (forward-paragraph -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 (setq npoint (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (skip-chars-forward " \t\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 ;; If the range of blank lines found spans the original start point,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 ;; try again from the beginning of it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 ;; Must be careful to avoid infinite loop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 ;; when following a single return at start of buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (if (and (>= (point) opoint) (< npoint opoint))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 (goto-char npoint)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (if (> npoint (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (start-of-paragraph-text))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 (defun end-of-paragraph-text ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 (let ((opoint (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (forward-paragraph 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (if (eq (preceding-char) ?\n) (forward-char -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 (if (<= (point) opoint)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 (forward-char 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 (if (< (point) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (end-of-paragraph-text))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 (defun forward-sentence (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 "Move forward to next `sentence-end'. With argument, repeat.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 With negative argument, move backward repeatedly to `sentence-beginning'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 The variable `sentence-end' is a regular expression that matches ends
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 of sentences. Also, every paragraph boundary terminates sentences as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 well."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (interactive "_p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 (or arg (setq arg 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (while (< arg 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 (goto-char (1- (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 (goto-char par-beg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 (setq arg (1+ arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 (while (> arg 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 (if (re-search-forward sentence-end par-end t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 (skip-chars-backward " \t\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 (goto-char par-end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 (setq arg (1- arg))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 (defun backward-sentence (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 "Move backward to start of sentence. With arg, do it arg times.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 See `forward-sentence' for more information."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 (interactive "_p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 (or arg (setq arg 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 (forward-sentence (- arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 (defun kill-sentence (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 "Kill from point to end of sentence.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 (kill-region (point) (progn (forward-sentence arg) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (defun backward-kill-sentence (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 "Kill back from point to start of sentence.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 (kill-region (point) (progn (backward-sentence arg) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 (defun mark-end-of-sentence (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 "Put mark at end of sentence. Arg works as in `forward-sentence'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (mark-something 'mark-end-of-sentence 'forward-sentence arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 (defun transpose-sentences (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 "Interchange this (next) and previous sentence."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (interactive "*p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (transpose-subr 'forward-sentence arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 ;;; paragraphs.el ends here
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341