xref: /386bsd/usr/local/lib/emacs/19.25/lisp/paragraphs.el (revision a2142627)
1;;; paragraphs.el --- paragraph and sentence parsing.
2
3;; Copyright (C) 1985, 86, 87, 91, 94 Free Software Foundation, Inc.
4
5;; Maintainer: FSF
6;; Keywords: wp
7
8;; This file is part of GNU Emacs.
9
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.
14
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.
19
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
22;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24;;; Commentary:
25
26;; This package provides the paragraph-oriented commands documented in the
27;; Emacs manual.
28
29;;; Code:
30
31(defconst paragraph-start "^[ \t\n\f]" "\
32*Regexp for beginning of a line that starts OR separates paragraphs.
33This regexp should match lines that separate paragraphs
34and should also match lines that start a paragraph
35\(and are part of that paragraph).
36The variable `paragraph-separate' specifies how to distinguish
37lines that start paragraphs from lines that separate them.")
38
39(defconst paragraph-separate "^[ \t\f]*$" "\
40*Regexp for beginning of a line that separates paragraphs.
41If you change this, you may have to change paragraph-start also.")
42
43(defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\|  \\)[ \t\n]*") "\
44*Regexp describing the end of a sentence.
45All paragraph boundaries also end sentences, regardless.
46
47In order to be recognized as the end of a sentence, the ending period,
48question mark, or exclamation point must be followed by two spaces,
49unless it's inside some sort of quotes or parenthesis.")
50
51(defconst page-delimiter "^\014" "\
52*Regexp describing line-beginnings that separate pages.")
53
54(defvar paragraph-ignore-fill-prefix nil "\
55Non-nil means the paragraph commands are not affected by `fill-prefix'.
56This is desirable in modes where blank lines are the paragraph delimiters.")
57
58
59(defun forward-paragraph (&optional arg)
60  "Move forward to end of paragraph.
61With arg N, do it N times; negative arg -N means move backward N paragraphs.
62
63A line which `paragraph-start' matches either separates paragraphs
64\(if `paragraph-separate' matches it also) or is the first line of a paragraph.
65A paragraph end is the beginning of a line which is not part of the paragraph
66to which the end of the previous line belongs, or the end of the buffer."
67  (interactive "p")
68  (or arg (setq arg 1))
69  (let* ((fill-prefix-regexp
70	  (and fill-prefix (not (equal fill-prefix ""))
71	       (not paragraph-ignore-fill-prefix)
72	       (regexp-quote fill-prefix)))
73	 (paragraph-separate
74	  (if fill-prefix-regexp
75	      (concat paragraph-separate "\\|^"
76		      fill-prefix-regexp "[ \t]*$")
77	    paragraph-separate)))
78    (while (and (< arg 0) (not (bobp)))
79      (if (and (not (looking-at paragraph-separate))
80	       (re-search-backward "^\n" (max (1- (point)) (point-min)) t))
81	  nil
82	;; Move back over paragraph-separating lines.
83	(forward-char -1) (beginning-of-line)
84	(while (and (not (bobp)) (looking-at paragraph-separate))
85	  (forward-line -1))
86	(if (bobp)
87	    nil
88	  ;; Go to end of the previous (non-separating) line.
89	  (end-of-line)
90	  ;; Search back for line that starts or separates paragraphs.
91	  (if (if fill-prefix-regexp
92		  ;; There is a fill prefix; it overrides paragraph-start.
93		  (progn
94		   (while (progn (beginning-of-line)
95				 (and (not (bobp))
96				      (not (looking-at paragraph-separate))
97				      (looking-at fill-prefix-regexp)))
98		     (forward-line -1))
99		   (not (bobp)))
100		(re-search-backward paragraph-start nil t))
101	      ;; Found one.
102	      (progn
103		;; Move forward over paragraph separators.
104		;; We know this cannot reach the place we started
105		;; because we know we moved back over a non-separator.
106		(while (and (not (eobp)) (looking-at paragraph-separate))
107		  (forward-line 1))
108		(if (eq (char-after (- (point) 2)) ?\n)
109		    (forward-line -1)))
110	    ;; No starter or separator line => use buffer beg.
111	    (goto-char (point-min)))))
112      (setq arg (1+ arg)))
113    (while (and (> arg 0) (not (eobp)))
114      (beginning-of-line)
115      (while (prog1 (and (not (eobp))
116			 (looking-at paragraph-separate))
117		    (forward-line 1)))
118      (if fill-prefix-regexp
119	  ;; There is a fill prefix; it overrides paragraph-start.
120	  (while (and (not (eobp))
121		      (not (looking-at paragraph-separate))
122		      (looking-at fill-prefix-regexp))
123	    (forward-line 1))
124	(if (re-search-forward paragraph-start nil t)
125	    (goto-char (match-beginning 0))
126	  (goto-char (point-max))))
127      (setq arg (1- arg)))))
128
129(defun backward-paragraph (&optional arg)
130  "Move backward to start of paragraph.
131With arg N, do it N times; negative arg -N means move forward N paragraphs.
132
133A paragraph start is the beginning of a line which is a
134`first-line-of-paragraph' or which is ordinary text and follows a
135paragraph-separating line; except: if the first real line of a
136paragraph is preceded by a blank line, the paragraph starts at that
137blank line.
138
139See `forward-paragraph' for more information."
140  (interactive "p")
141  (or arg (setq arg 1))
142  (forward-paragraph (- arg)))
143
144(defun mark-paragraph ()
145  "Put point at beginning of this paragraph, mark at end.
146The paragraph marked is the one that contains point or follows point."
147  (interactive)
148  (forward-paragraph 1)
149  (push-mark nil t t)
150  (backward-paragraph 1))
151
152(defun kill-paragraph (arg)
153  "Kill forward to end of paragraph.
154With arg N, kill forward to Nth end of paragraph;
155negative arg -N means kill backward to Nth start of paragraph."
156  (interactive "p")
157  (kill-region (point) (progn (forward-paragraph arg) (point))))
158
159(defun backward-kill-paragraph (arg)
160  "Kill back to start of paragraph.
161With arg N, kill back to Nth start of paragraph;
162negative arg -N means kill forward to Nth end of paragraph."
163  (interactive "p")
164  (kill-region (point) (progn (backward-paragraph arg) (point))))
165
166(defun transpose-paragraphs (arg)
167  "Interchange this (or next) paragraph with previous one."
168  (interactive "*p")
169  (transpose-subr 'forward-paragraph arg))
170
171(defun start-of-paragraph-text ()
172  (let ((opoint (point)) npoint)
173    (forward-paragraph -1)
174    (setq npoint (point))
175    (skip-chars-forward " \t\n")
176    ;; If the range of blank lines found spans the original start point,
177    ;; try again from the beginning of it.
178    ;; Must be careful to avoid infinite loop
179    ;; when following a single return at start of buffer.
180    (if (and (>= (point) opoint) (< npoint opoint))
181	(progn
182	  (goto-char npoint)
183	  (if (> npoint (point-min))
184	      (start-of-paragraph-text))))))
185
186(defun end-of-paragraph-text ()
187  (let ((opoint (point)))
188    (forward-paragraph 1)
189    (if (eq (preceding-char) ?\n) (forward-char -1))
190    (if (<= (point) opoint)
191	(progn
192	  (forward-char 1)
193	  (if (< (point) (point-max))
194	      (end-of-paragraph-text))))))
195
196(defun forward-sentence (&optional arg)
197  "Move forward to next `sentence-end'.  With argument, repeat.
198With negative argument, move backward repeatedly to `sentence-beginning'.
199
200The variable `sentence-end' is a regular expression that matches ends of
201sentences.  Also, every paragraph boundary terminates sentences as well."
202  (interactive "p")
203  (or arg (setq arg 1))
204  (while (< arg 0)
205    (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
206      (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
207	  (goto-char (1- (match-end 0)))
208	(goto-char par-beg)))
209    (setq arg (1+ arg)))
210  (while (> arg 0)
211    (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
212      (if (re-search-forward sentence-end par-end t)
213	  (skip-chars-backward " \t\n")
214	(goto-char par-end)))
215    (setq arg (1- arg))))
216
217(defun backward-sentence (&optional arg)
218  "Move backward to start of sentence.  With arg, do it arg times.
219See `forward-sentence' for more information."
220  (interactive "p")
221  (or arg (setq arg 1))
222  (forward-sentence (- arg)))
223
224(defun kill-sentence (&optional arg)
225  "Kill from point to end of sentence.
226With arg, repeat; negative arg -N means kill back to Nth start of sentence."
227  (interactive "*p")
228  (kill-region (point) (progn (forward-sentence arg) (point))))
229
230(defun backward-kill-sentence (&optional arg)
231  "Kill back from point to start of sentence.
232With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
233  (interactive "*p")
234  (kill-region (point) (progn (backward-sentence arg) (point))))
235
236(defun mark-end-of-sentence (arg)
237  "Put mark at end of sentence.  Arg works as in `forward-sentence'."
238  (interactive "p")
239  (push-mark
240    (save-excursion
241      (forward-sentence arg)
242      (point))
243    nil t))
244
245(defun transpose-sentences (arg)
246  "Interchange this (next) and previous sentence."
247  (interactive "*p")
248  (transpose-subr 'forward-sentence arg))
249
250;;; paragraphs.el ends here
251