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