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