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