annotate lisp/prim/paragraphs.el @ 108:360340f9fd5f r20-1b6

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