1 ;;; paragraphs.el --- paragraph and sentence parsing.
3 ;; Copyright (C) 1985, 86, 87, 91, 94, 95, 97 Free Software Foundation, Inc.
6 ;; Keywords: wp, dumped
8 ;; This file is part of XEmacs.
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)
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.
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
25 ;;; Synched up with: FSF 19.34.
29 ;; This file is dumped with XEmacs.
31 ;; This package provides the paragraph-oriented commands documented in the
32 ;; XEmacs Reference Manual.
34 ;; 06/11/1997 - Use char-(after|before) instead of
35 ;; (following|preceding)-char. -slb
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.
48 Each buffer has its own value of this variable.")
49 (make-variable-buffer-local 'use-hard-newlines)
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.
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.
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)))
72 (setq use-hard-newlines nil)
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))
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? "))))
86 (goto-char (point-min))
87 (while (search-forward "\n" nil t)
90 (if (looking-at paragraph-start)
92 (set-hard-newline-properties (1- pos) pos)
93 ;; If paragraph-separate, newline after it is hard too.
94 (if (looking-at paragraph-separate)
98 (set-hard-newline-properties
99 (point) (1+ (point))))))))))))
100 (setq use-hard-newlines t)))
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).
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.
114 The variable `paragraph-separate' specifies how to distinguish
115 lines that start paragraphs from lines that separate them.
117 If the variable `use-hard-newlines' is non-nil, then only lines following a
118 hard newline are considered to match.")
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).
126 ;; XEmacs -- use purecopy
127 (defconst paragraph-separate (purecopy "[ \t\f]*$") "\
128 *Regexp for beginning of a line that separates paragraphs.
129 If you change this, you may have to change paragraph-start also.
131 This is matched against the text at the left margin, which is not necessarily
132 the beginning of the line, so it should not use \"^\" as an anchor. This
133 ensures that the paragraph functions will work equally within a region of
134 text indented by a margin setting.")
136 ;; XEmacs -- use purecopy
137 (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
138 *Regexp describing the end of a sentence.
139 All paragraph boundaries also end sentences, regardless.
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.")
145 ;; XEmacs -- use purecopy
146 (defconst page-delimiter (purecopy "^\014") "\
147 *Regexp describing line-beginnings that separate pages.")
149 (defvar paragraph-ignore-fill-prefix nil "\
150 Non-nil means the paragraph commands are not affected by `fill-prefix'.
151 This is desirable in modes where blank lines are the paragraph delimiters.")
153 (defun forward-paragraph (&optional arg)
154 "Move forward to end of paragraph.
155 With arg N, do it N times; negative arg -N means move backward N paragraphs.
157 A line which `paragraph-start' matches either separates paragraphs
158 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
159 A paragraph end is the beginning of a line which is not part of the paragraph
160 to which the end of the previous line belongs, or the end of the buffer."
161 (interactive "_p") ; XEmacs
162 (or arg (setq arg 1))
163 (let* ((fill-prefix-regexp
164 (and fill-prefix (not (equal fill-prefix ""))
165 (not paragraph-ignore-fill-prefix)
166 (regexp-quote fill-prefix)))
167 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
168 ;; These regexps shouldn't be anchored, because we look for them
169 ;; starting at the left-margin. This allows paragraph commands to
170 ;; work normally with indented text.
171 ;; This hack will not find problem cases like "whatever\\|^something".
172 (paragraph-start (if (and (not (equal "" paragraph-start))
173 (equal ?^ (aref paragraph-start 0)))
174 (substring paragraph-start 1)
176 (paragraph-separate (if (and (not (equal "" paragraph-start))
177 (equal ?^ (aref paragraph-separate 0)))
178 (substring paragraph-separate 1)
181 (if fill-prefix-regexp
182 (concat paragraph-separate "\\|"
183 fill-prefix-regexp "[ \t]*$")
185 ;; This is used for searching.
186 (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)"))
188 (while (and (< arg 0) (not (bobp)))
189 (if (and (not (looking-at paragraph-separate))
190 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
191 (looking-at paragraph-separate))
194 ;; Move back over paragraph-separating lines.
195 (forward-char -1) (beginning-of-line)
196 (while (and (not (bobp))
197 (progn (move-to-left-margin)
198 (looking-at paragraph-separate)))
202 ;; Go to end of the previous (non-separating) line.
204 ;; Search back for line that starts or separates paragraphs.
205 (if (if fill-prefix-regexp
206 ;; There is a fill prefix; it overrides paragraph-start.
207 (let (multiple-lines)
208 (while (and (progn (beginning-of-line) (not (bobp)))
209 (progn (move-to-left-margin)
210 (not (looking-at paragraph-separate)))
211 (looking-at fill-prefix-regexp))
212 (if (not (= (point) start))
213 (setq multiple-lines t))
215 (move-to-left-margin)
216 ;; Don't move back over a line before the paragraph
217 ;; which doesn't start with fill-prefix
218 ;; unless that is the only line we've moved over.
219 (and (not (looking-at fill-prefix-regexp))
223 (while (and (re-search-backward sp-paragraph-start nil 1)
224 ;; Found a candidate, but need to check if it is a
225 ;; REAL paragraph-start.
227 (progn (setq start (point))
228 (move-to-left-margin)
229 (not (looking-at paragraph-separate)))
230 (or (not (looking-at paragraph-start))
231 (and use-hard-newlines
232 (not (get-text-property (1- start)
235 (> (point) (point-min)))
238 ;; Move forward over paragraph separators.
239 ;; We know this cannot reach the place we started
240 ;; because we know we moved back over a non-separator.
241 (while (and (not (eobp))
242 (progn (move-to-left-margin)
243 (looking-at paragraph-separate)))
245 ;; If line before paragraph is just margin, back up to there.
247 (if (> (current-column) (current-left-margin))
249 (skip-chars-backward " \t")
252 ;; No starter or separator line => use buffer beg.
253 (goto-char (point-min)))))
255 (while (and (> arg 0) (not (eobp)))
256 ;; Move forward over separator lines, and one more line.
257 (while (prog1 (and (not (eobp))
258 (progn (move-to-left-margin) (not (eobp)))
259 (looking-at paragraph-separate))
261 (if fill-prefix-regexp
262 ;; There is a fill prefix; it overrides paragraph-start.
263 (while (and (not (eobp))
264 (progn (move-to-left-margin) (not (eobp)))
265 (not (looking-at paragraph-separate))
266 (looking-at fill-prefix-regexp))
268 (while (and (re-search-forward sp-paragraph-start nil 1)
269 (progn (setq start (match-beginning 0))
272 (progn (move-to-left-margin)
273 (not (looking-at paragraph-separate)))
274 (or (not (looking-at paragraph-start))
275 (and use-hard-newlines
276 (not (get-text-property (1- start) 'hard)))))
278 (if (< (point) (point-max))
280 (setq arg (1- arg)))))
282 (defun backward-paragraph (&optional arg)
283 "Move backward to start of paragraph.
284 With arg N, do it N times; negative arg -N means move forward N paragraphs.
286 A paragraph start is the beginning of a line which is a
287 `first-line-of-paragraph' or which is ordinary text and follows a
288 paragraph-separating line; except: if the first real line of a
289 paragraph is preceded by a blank line, the paragraph starts at that
292 See `forward-paragraph' for more information."
293 (interactive "_p") ; XEmacs
294 (or arg (setq arg 1))
295 (forward-paragraph (- arg)))
297 (defun mark-paragraph ()
298 "Put point at beginning of this paragraph, mark at end.
299 The paragraph marked is the one that contains point or follows point."
301 (forward-paragraph 1)
303 (backward-paragraph 1))
305 (defun kill-paragraph (arg)
306 "Kill forward to end of paragraph.
307 With arg N, kill forward to Nth end of paragraph;
308 negative arg -N means kill backward to Nth start of paragraph."
309 (interactive "*p") ; XEmacs
310 (kill-region (point) (progn (forward-paragraph arg) (point))))
312 (defun backward-kill-paragraph (arg)
313 "Kill back to start of paragraph.
314 With arg N, kill back to Nth start of paragraph;
315 negative arg -N means kill forward to Nth end of paragraph."
316 (interactive "*p") ; XEmacs
317 (kill-region (point) (progn (backward-paragraph arg) (point))))
319 (defun transpose-paragraphs (arg)
320 "Interchange this (or next) paragraph with previous one."
322 (transpose-subr 'forward-paragraph arg))
324 (defun start-of-paragraph-text ()
325 (let ((opoint (point)) npoint)
326 (forward-paragraph -1)
327 (setq npoint (point))
328 (skip-chars-forward " \t\n")
329 ;; If the range of blank lines found spans the original start point,
330 ;; try again from the beginning of it.
331 ;; Must be careful to avoid infinite loop
332 ;; when following a single return at start of buffer.
333 (if (and (>= (point) opoint) (< npoint opoint))
336 (if (> npoint (point-min))
337 (start-of-paragraph-text))))))
339 (defun end-of-paragraph-text ()
340 (let ((opoint (point)))
341 (forward-paragraph 1)
342 (if (eq (char-before (point)) ?\n) (forward-char -1))
343 (if (<= (point) opoint)
346 (if (< (point) (point-max))
347 (end-of-paragraph-text))))))
349 (defun forward-sentence (&optional arg)
350 "Move forward to next `sentence-end'. With argument, repeat.
351 With negative argument, move backward repeatedly to `sentence-beginning'.
353 The variable `sentence-end' is a regular expression that matches ends of
354 sentences. Also, every paragraph boundary terminates sentences as well."
355 (interactive "_p") ; XEmacs
356 (or arg (setq arg 1))
358 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
359 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
360 (goto-char (1- (match-end 0)))
361 (goto-char par-beg)))
364 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
365 (if (re-search-forward sentence-end par-end t)
366 (skip-chars-backward " \t\n")
367 (goto-char par-end)))
368 (setq arg (1- arg))))
370 (defun backward-sentence (&optional arg)
371 "Move backward to start of sentence. With arg, do it arg times.
372 See `forward-sentence' for more information."
373 (interactive "_p") ; XEmacs
374 (or arg (setq arg 1))
375 (forward-sentence (- arg)))
377 (defun kill-sentence (&optional arg)
378 "Kill from point to end of sentence.
379 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
380 (interactive "*p") ; XEmacs
381 (kill-region (point) (progn (forward-sentence arg) (point))))
383 (defun backward-kill-sentence (&optional arg)
384 "Kill back from point to start of sentence.
385 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
386 (interactive "*p") ; XEmacs
387 (kill-region (point) (progn (backward-sentence arg) (point))))
389 (defun mark-end-of-sentence (arg)
390 "Put mark at end of sentence. Arg works as in `forward-sentence'."
395 ; (forward-sentence arg)
398 (mark-something 'mark-end-of-sentence 'forward-sentence arg))
400 (defun mark-end-of-line (arg)
401 "Put mark at end of line. Arg works as in `end-of-line'."
403 (mark-something 'mark-end-of-line 'end-of-line arg))
406 (defun transpose-sentences (arg)
407 "Interchange this (next) and previous sentence."
409 (transpose-subr 'forward-sentence arg))
411 ;;; paragraphs.el ends here