209
|
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
|
215
|
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 preceeding 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
|
209
|
102 ;; XEmacs - use purecopy
|
|
103 (defconst paragraph-start (purecopy "[ \t\n\f]") "\
|
|
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
|
|
126 ;; XEmacs -- use purecopy
|
|
127 (defconst paragraph-separate (purecopy "[ \t\f]*$") "\
|
|
128 *Regexp for beginning of a line that separates paragraphs.
|
367
|
129 If you change this, you may have to change `paragraph-start' also.
|
|
130
|
|
131 A line matching this is not part of any paragraph.
|
209
|
132
|
|
133 This is matched against the text at the left margin, which is not necessarily
|
|
134 the beginning of the line, so it should not use \"^\" as an anchor. This
|
|
135 ensures that the paragraph functions will work equally within a region of
|
|
136 text indented by a margin setting.")
|
|
137
|
|
138 ;; XEmacs -- use purecopy
|
|
139 (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
|
|
140 *Regexp describing the end of a sentence.
|
|
141 All paragraph boundaries also end sentences, regardless.
|
|
142
|
|
143 In order to be recognized as the end of a sentence, the ending period,
|
|
144 question mark, or exclamation point must be followed by two spaces,
|
|
145 unless it's inside some sort of quotes or parenthesis.")
|
|
146
|
|
147 ;; XEmacs -- use purecopy
|
|
148 (defconst page-delimiter (purecopy "^\014") "\
|
|
149 *Regexp describing line-beginnings that separate pages.")
|
|
150
|
|
151 (defvar paragraph-ignore-fill-prefix nil "\
|
|
152 Non-nil means the paragraph commands are not affected by `fill-prefix'.
|
|
153 This is desirable in modes where blank lines are the paragraph delimiters.")
|
|
154
|
|
155 (defun forward-paragraph (&optional arg)
|
|
156 "Move forward to end of paragraph.
|
|
157 With arg N, do it N times; negative arg -N means move backward N paragraphs.
|
|
158
|
|
159 A line which `paragraph-start' matches either separates paragraphs
|
|
160 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
|
|
161 A paragraph end is the beginning of a line which is not part of the paragraph
|
|
162 to which the end of the previous line belongs, or the end of the buffer."
|
|
163 (interactive "_p") ; XEmacs
|
|
164 (or arg (setq arg 1))
|
|
165 (let* ((fill-prefix-regexp
|
|
166 (and fill-prefix (not (equal fill-prefix ""))
|
|
167 (not paragraph-ignore-fill-prefix)
|
|
168 (regexp-quote fill-prefix)))
|
|
169 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
|
|
170 ;; These regexps shouldn't be anchored, because we look for them
|
|
171 ;; starting at the left-margin. This allows paragraph commands to
|
|
172 ;; work normally with indented text.
|
|
173 ;; This hack will not find problem cases like "whatever\\|^something".
|
|
174 (paragraph-start (if (and (not (equal "" paragraph-start))
|
|
175 (equal ?^ (aref paragraph-start 0)))
|
|
176 (substring paragraph-start 1)
|
|
177 paragraph-start))
|
|
178 (paragraph-separate (if (and (not (equal "" paragraph-start))
|
|
179 (equal ?^ (aref paragraph-separate 0)))
|
|
180 (substring paragraph-separate 1)
|
|
181 paragraph-separate))
|
|
182 (paragraph-separate
|
|
183 (if fill-prefix-regexp
|
|
184 (concat paragraph-separate "\\|"
|
|
185 fill-prefix-regexp "[ \t]*$")
|
|
186 paragraph-separate))
|
|
187 ;; This is used for searching.
|
|
188 (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)"))
|
|
189 start)
|
|
190 (while (and (< arg 0) (not (bobp)))
|
|
191 (if (and (not (looking-at paragraph-separate))
|
|
192 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
|
|
193 (looking-at paragraph-separate))
|
|
194 nil
|
|
195 (setq start (point))
|
|
196 ;; Move back over paragraph-separating lines.
|
|
197 (forward-char -1) (beginning-of-line)
|
|
198 (while (and (not (bobp))
|
|
199 (progn (move-to-left-margin)
|
|
200 (looking-at paragraph-separate)))
|
|
201 (forward-line -1))
|
|
202 (if (bobp)
|
|
203 nil
|
|
204 ;; Go to end of the previous (non-separating) line.
|
|
205 (end-of-line)
|
|
206 ;; Search back for line that starts or separates paragraphs.
|
|
207 (if (if fill-prefix-regexp
|
|
208 ;; There is a fill prefix; it overrides paragraph-start.
|
|
209 (let (multiple-lines)
|
|
210 (while (and (progn (beginning-of-line) (not (bobp)))
|
|
211 (progn (move-to-left-margin)
|
|
212 (not (looking-at paragraph-separate)))
|
|
213 (looking-at fill-prefix-regexp))
|
|
214 (if (not (= (point) start))
|
|
215 (setq multiple-lines t))
|
|
216 (forward-line -1))
|
|
217 (move-to-left-margin)
|
|
218 ;; Don't move back over a line before the paragraph
|
|
219 ;; which doesn't start with fill-prefix
|
|
220 ;; unless that is the only line we've moved over.
|
|
221 (and (not (looking-at fill-prefix-regexp))
|
|
222 multiple-lines
|
|
223 (forward-line 1))
|
|
224 (not (bobp)))
|
|
225 (while (and (re-search-backward sp-paragraph-start nil 1)
|
|
226 ;; Found a candidate, but need to check if it is a
|
|
227 ;; REAL paragraph-start.
|
|
228 (not (bobp))
|
|
229 (progn (setq start (point))
|
|
230 (move-to-left-margin)
|
|
231 (not (looking-at paragraph-separate)))
|
|
232 (or (not (looking-at paragraph-start))
|
|
233 (and use-hard-newlines
|
|
234 (not (get-text-property (1- start)
|
|
235 'hard)))))
|
|
236 (goto-char start))
|
|
237 (> (point) (point-min)))
|
|
238 ;; Found one.
|
|
239 (progn
|
|
240 ;; Move forward over paragraph separators.
|
|
241 ;; We know this cannot reach the place we started
|
|
242 ;; because we know we moved back over a non-separator.
|
|
243 (while (and (not (eobp))
|
|
244 (progn (move-to-left-margin)
|
|
245 (looking-at paragraph-separate)))
|
|
246 (forward-line 1))
|
|
247 ;; If line before paragraph is just margin, back up to there.
|
|
248 (end-of-line 0)
|
|
249 (if (> (current-column) (current-left-margin))
|
|
250 (forward-char 1)
|
|
251 (skip-chars-backward " \t")
|
|
252 (if (not (bolp))
|
|
253 (forward-line 1))))
|
|
254 ;; No starter or separator line => use buffer beg.
|
|
255 (goto-char (point-min)))))
|
|
256 (setq arg (1+ arg)))
|
|
257 (while (and (> arg 0) (not (eobp)))
|
|
258 ;; Move forward over separator lines, and one more line.
|
|
259 (while (prog1 (and (not (eobp))
|
|
260 (progn (move-to-left-margin) (not (eobp)))
|
|
261 (looking-at paragraph-separate))
|
|
262 (forward-line 1)))
|
|
263 (if fill-prefix-regexp
|
|
264 ;; There is a fill prefix; it overrides paragraph-start.
|
|
265 (while (and (not (eobp))
|
|
266 (progn (move-to-left-margin) (not (eobp)))
|
|
267 (not (looking-at paragraph-separate))
|
|
268 (looking-at fill-prefix-regexp))
|
|
269 (forward-line 1))
|
|
270 (while (and (re-search-forward sp-paragraph-start nil 1)
|
|
271 (progn (setq start (match-beginning 0))
|
|
272 (goto-char start)
|
|
273 (not (eobp)))
|
|
274 (progn (move-to-left-margin)
|
|
275 (not (looking-at paragraph-separate)))
|
|
276 (or (not (looking-at paragraph-start))
|
|
277 (and use-hard-newlines
|
|
278 (not (get-text-property (1- start) 'hard)))))
|
|
279 (forward-char 1))
|
|
280 (if (< (point) (point-max))
|
|
281 (goto-char start)))
|
|
282 (setq arg (1- arg)))))
|
|
283
|
|
284 (defun backward-paragraph (&optional arg)
|
|
285 "Move backward to start of paragraph.
|
|
286 With arg N, do it N times; negative arg -N means move forward N paragraphs.
|
|
287
|
|
288 A paragraph start is the beginning of a line which is a
|
|
289 `first-line-of-paragraph' or which is ordinary text and follows a
|
|
290 paragraph-separating line; except: if the first real line of a
|
|
291 paragraph is preceded by a blank line, the paragraph starts at that
|
|
292 blank line.
|
|
293
|
|
294 See `forward-paragraph' for more information."
|
|
295 (interactive "_p") ; XEmacs
|
|
296 (or arg (setq arg 1))
|
|
297 (forward-paragraph (- arg)))
|
|
298
|
|
299 (defun mark-paragraph ()
|
|
300 "Put point at beginning of this paragraph, mark at end.
|
|
301 The paragraph marked is the one that contains point or follows point."
|
|
302 (interactive)
|
|
303 (forward-paragraph 1)
|
|
304 (push-mark nil t t)
|
|
305 (backward-paragraph 1))
|
|
306
|
|
307 (defun kill-paragraph (arg)
|
|
308 "Kill forward to end of paragraph.
|
|
309 With arg N, kill forward to Nth end of paragraph;
|
|
310 negative arg -N means kill backward to Nth start of paragraph."
|
|
311 (interactive "*p") ; XEmacs
|
|
312 (kill-region (point) (progn (forward-paragraph arg) (point))))
|
|
313
|
|
314 (defun backward-kill-paragraph (arg)
|
|
315 "Kill back to start of paragraph.
|
|
316 With arg N, kill back to Nth start of paragraph;
|
|
317 negative arg -N means kill forward to Nth end of paragraph."
|
|
318 (interactive "*p") ; XEmacs
|
|
319 (kill-region (point) (progn (backward-paragraph arg) (point))))
|
|
320
|
|
321 (defun transpose-paragraphs (arg)
|
|
322 "Interchange this (or next) paragraph with previous one."
|
|
323 (interactive "*p")
|
|
324 (transpose-subr 'forward-paragraph arg))
|
|
325
|
|
326 (defun start-of-paragraph-text ()
|
|
327 (let ((opoint (point)) npoint)
|
|
328 (forward-paragraph -1)
|
|
329 (setq npoint (point))
|
|
330 (skip-chars-forward " \t\n")
|
|
331 ;; If the range of blank lines found spans the original start point,
|
|
332 ;; try again from the beginning of it.
|
|
333 ;; Must be careful to avoid infinite loop
|
|
334 ;; when following a single return at start of buffer.
|
|
335 (if (and (>= (point) opoint) (< npoint opoint))
|
|
336 (progn
|
|
337 (goto-char npoint)
|
|
338 (if (> npoint (point-min))
|
|
339 (start-of-paragraph-text))))))
|
|
340
|
|
341 (defun end-of-paragraph-text ()
|
|
342 (let ((opoint (point)))
|
|
343 (forward-paragraph 1)
|
|
344 (if (eq (char-before (point)) ?\n) (forward-char -1))
|
|
345 (if (<= (point) opoint)
|
|
346 (progn
|
|
347 (forward-char 1)
|
|
348 (if (< (point) (point-max))
|
|
349 (end-of-paragraph-text))))))
|
|
350
|
|
351 (defun forward-sentence (&optional arg)
|
|
352 "Move forward to next `sentence-end'. With argument, repeat.
|
|
353 With negative argument, move backward repeatedly to `sentence-beginning'.
|
|
354
|
|
355 The variable `sentence-end' is a regular expression that matches ends of
|
367
|
356 sentences. A paragraph boundary also terminates a sentence."
|
209
|
357 (interactive "_p") ; XEmacs
|
|
358 (or arg (setq arg 1))
|
|
359 (while (< arg 0)
|
|
360 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
|
|
361 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
|
|
362 (goto-char (1- (match-end 0)))
|
|
363 (goto-char par-beg)))
|
|
364 (setq arg (1+ arg)))
|
|
365 (while (> arg 0)
|
|
366 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
|
|
367 (if (re-search-forward sentence-end par-end t)
|
|
368 (skip-chars-backward " \t\n")
|
|
369 (goto-char par-end)))
|
|
370 (setq arg (1- arg))))
|
|
371
|
|
372 (defun backward-sentence (&optional arg)
|
|
373 "Move backward to start of sentence. With arg, do it arg times.
|
|
374 See `forward-sentence' for more information."
|
|
375 (interactive "_p") ; XEmacs
|
|
376 (or arg (setq arg 1))
|
|
377 (forward-sentence (- arg)))
|
|
378
|
|
379 (defun kill-sentence (&optional arg)
|
|
380 "Kill from point to end of sentence.
|
|
381 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
|
|
382 (interactive "*p") ; XEmacs
|
|
383 (kill-region (point) (progn (forward-sentence arg) (point))))
|
|
384
|
|
385 (defun backward-kill-sentence (&optional arg)
|
|
386 "Kill back from point to start of sentence.
|
|
387 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
|
|
388 (interactive "*p") ; XEmacs
|
|
389 (kill-region (point) (progn (backward-sentence arg) (point))))
|
|
390
|
|
391 (defun mark-end-of-sentence (arg)
|
|
392 "Put mark at end of sentence. Arg works as in `forward-sentence'."
|
|
393 (interactive "p")
|
|
394 ;; FSF Version:
|
|
395 ; (push-mark
|
|
396 ; (save-excursion
|
|
397 ; (forward-sentence arg)
|
|
398 ; (point))
|
|
399 ; nil t))
|
|
400 (mark-something 'mark-end-of-sentence 'forward-sentence arg))
|
|
401
|
211
|
402 (defun mark-end-of-line (arg)
|
|
403 "Put mark at end of line. Arg works as in `end-of-line'."
|
|
404 (interactive "p")
|
|
405 (mark-something 'mark-end-of-line 'end-of-line arg))
|
|
406
|
|
407
|
209
|
408 (defun transpose-sentences (arg)
|
|
409 "Interchange this (next) and previous sentence."
|
|
410 (interactive "*p")
|
|
411 (transpose-subr 'forward-sentence arg))
|
|
412
|
|
413 ;;; paragraphs.el ends here
|