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