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