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