Mercurial > emacs
comparison lisp/textmodes/paragraphs.el @ 36:9697c13298e5
Initial revision
author | Joseph Arceneaux <jla@gnu.org> |
---|---|
date | Tue, 31 Oct 1989 16:00:07 +0000 |
parents | |
children | 7680293d57f3 |
comparison
equal
deleted
inserted
replaced
35:63b375f17a65 | 36:9697c13298e5 |
---|---|
1 ;; Paragraph and sentence parsing. | |
2 ;; Copyright (C) 1985 Free Software Foundation, Inc. | |
3 | |
4 ;; This file is part of GNU Emacs. | |
5 | |
6 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
7 ;; it under the terms of the GNU General Public License as published by | |
8 ;; the Free Software Foundation; either version 1, or (at your option) | |
9 ;; any later version. | |
10 | |
11 ;; GNU Emacs is distributed in the hope that it will be useful, | |
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 ;; GNU General Public License for more details. | |
15 | |
16 ;; You should have received a copy of the GNU General Public License | |
17 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | |
20 | |
21 (defvar paragraph-ignore-fill-prefix nil | |
22 "Non-nil means the paragraph commands are not affected by fill-prefix. | |
23 This is desirable in modes where blank lines are the paragraph delimiters.") | |
24 | |
25 (defun forward-paragraph (&optional arg) | |
26 "Move forward to end of paragraph. | |
27 With arg N, do it N times; negative arg -N means move forward N paragraphs. | |
28 | |
29 A line which `paragraph-start' matches either separates paragraphs | |
30 \(if `paragraph-separate' matches it also) or is the first line of a paragraph. | |
31 A paragraph end is the beginning of a line which is not part of the paragraph | |
32 to which the end of the previous line belongs, or the end of the buffer." | |
33 (interactive "p") | |
34 (or arg (setq arg 1)) | |
35 (let* ((fill-prefix-regexp | |
36 (and fill-prefix (not (equal fill-prefix "")) | |
37 (not paragraph-ignore-fill-prefix) | |
38 (regexp-quote fill-prefix))) | |
39 (paragraph-separate | |
40 (if fill-prefix-regexp | |
41 (concat paragraph-separate "\\|^" | |
42 fill-prefix-regexp "[ \t]*$") | |
43 paragraph-separate))) | |
44 (while (< arg 0) | |
45 (if (and (not (looking-at paragraph-separate)) | |
46 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)) | |
47 nil | |
48 (forward-char -1) (beginning-of-line) | |
49 (while (and (not (bobp)) (looking-at paragraph-separate)) | |
50 (forward-line -1)) | |
51 (end-of-line) | |
52 ;; Search back for line that starts or separates paragraphs. | |
53 (if (if fill-prefix-regexp | |
54 ;; There is a fill prefix; it overrides paragraph-start. | |
55 (progn | |
56 (while (progn (beginning-of-line) | |
57 (and (not (bobp)) | |
58 (not (looking-at paragraph-separate)) | |
59 (looking-at fill-prefix-regexp))) | |
60 (forward-line -1)) | |
61 (not (bobp))) | |
62 (re-search-backward paragraph-start nil t)) | |
63 ;; Found one. | |
64 (progn | |
65 (while (and (not (eobp)) (looking-at paragraph-separate)) | |
66 (forward-line 1)) | |
67 (if (eq (char-after (- (point) 2)) ?\n) | |
68 (forward-line -1))) | |
69 ;; No starter or separator line => use buffer beg. | |
70 (goto-char (point-min)))) | |
71 (setq arg (1+ arg))) | |
72 (while (> arg 0) | |
73 (beginning-of-line) | |
74 (while (prog1 (and (not (eobp)) | |
75 (looking-at paragraph-separate)) | |
76 (forward-line 1))) | |
77 (if fill-prefix-regexp | |
78 ;; There is a fill prefix; it overrides paragraph-start. | |
79 (while (and (not (eobp)) | |
80 (not (looking-at paragraph-separate)) | |
81 (looking-at fill-prefix-regexp)) | |
82 (forward-line 1)) | |
83 (if (re-search-forward paragraph-start nil t) | |
84 (goto-char (match-beginning 0)) | |
85 (goto-char (point-max)))) | |
86 (setq arg (1- arg))))) | |
87 | |
88 (defun backward-paragraph (&optional arg) | |
89 "Move backward to start of paragraph. | |
90 With arg N, do it N times; negative arg -N means move forward N paragraphs. | |
91 | |
92 A paragraph start is the beginning of a line which is a first-line-of-paragraph | |
93 or which is ordinary text and follows a paragraph-separating line; except: | |
94 if the first real line of a paragraph is preceded by a blank line, | |
95 the paragraph starts at that blank line. | |
96 See forward-paragraph for more information." | |
97 (interactive "p") | |
98 (or arg (setq arg 1)) | |
99 (forward-paragraph (- arg))) | |
100 | |
101 (defun mark-paragraph () | |
102 "Put point at beginning of this paragraph, mark at end. | |
103 The paragraph marked is the one that contains point or follows point." | |
104 (interactive) | |
105 (forward-paragraph 1) | |
106 (push-mark nil t) | |
107 (backward-paragraph 1)) | |
108 | |
109 (defun kill-paragraph (arg) | |
110 "Kill forward to end of paragraph. | |
111 With arg N, kill forward to Nth end of paragraph; | |
112 negative arg -N means kill backward to Nth start of paragraph." | |
113 (interactive "*p") | |
114 (kill-region (point) (progn (forward-paragraph arg) (point)))) | |
115 | |
116 (defun backward-kill-paragraph (arg) | |
117 "Kill back to start of paragraph. | |
118 With arg N, kill back to Nth start of paragraph; | |
119 negative arg -N means kill forward to Nth end of paragraph." | |
120 (interactive "*p") | |
121 (kill-region (point) (progn (backward-paragraph arg) (point)))) | |
122 | |
123 (defun transpose-paragraphs (arg) | |
124 "Interchange this (or next) paragraph with previous one." | |
125 (interactive "*p") | |
126 (transpose-subr 'forward-paragraph arg)) | |
127 | |
128 (defun start-of-paragraph-text () | |
129 (let ((opoint (point)) npoint) | |
130 (forward-paragraph -1) | |
131 (setq npoint (point)) | |
132 (skip-chars-forward " \t\n") | |
133 (if (>= (point) opoint) | |
134 (progn | |
135 (goto-char npoint) | |
136 (if (> npoint (point-min)) | |
137 (start-of-paragraph-text)))))) | |
138 | |
139 (defun end-of-paragraph-text () | |
140 (let ((opoint (point))) | |
141 (forward-paragraph 1) | |
142 (if (eq (preceding-char) ?\n) (forward-char -1)) | |
143 (if (<= (point) opoint) | |
144 (progn | |
145 (forward-char 1) | |
146 (if (< (point) (point-max)) | |
147 (end-of-paragraph-text)))))) | |
148 | |
149 (defun forward-sentence (&optional arg) | |
150 "Move forward to next sentence-end. With argument, repeat. | |
151 With negative argument, move backward repeatedly to sentence-beginning. | |
152 | |
153 The variable `sentence-end' is a regular expression that matches ends | |
154 of sentences. Also, every paragraph boundary terminates sentences as | |
155 well." | |
156 (interactive "p") | |
157 (or arg (setq arg 1)) | |
158 (while (< arg 0) | |
159 (let ((par-beg (save-excursion (start-of-paragraph-text) (point)))) | |
160 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t) | |
161 (goto-char (1- (match-end 0))) | |
162 (goto-char par-beg))) | |
163 (setq arg (1+ arg))) | |
164 (while (> arg 0) | |
165 (let ((par-end (save-excursion (end-of-paragraph-text) (point)))) | |
166 (if (re-search-forward sentence-end par-end t) | |
167 (skip-chars-backward " \t\n") | |
168 (goto-char par-end))) | |
169 (setq arg (1- arg)))) | |
170 | |
171 (defun backward-sentence (&optional arg) | |
172 "Move backward to start of sentence. With arg, do it arg times. | |
173 See forward-sentence for more information." | |
174 (interactive "p") | |
175 (or arg (setq arg 1)) | |
176 (forward-sentence (- arg))) | |
177 | |
178 (defun kill-sentence (&optional arg) | |
179 "Kill from point to end of sentence. | |
180 With arg, repeat; negative arg -N means kill back to Nth start of sentence." | |
181 (interactive "*p") | |
182 (let ((beg (point))) | |
183 (forward-sentence arg) | |
184 (kill-region beg (point)))) | |
185 | |
186 (defun backward-kill-sentence (&optional arg) | |
187 "Kill back from point to start of sentence. | |
188 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N." | |
189 (interactive "*p") | |
190 (let ((beg (point))) | |
191 (backward-sentence arg) | |
192 (kill-region beg (point)))) | |
193 | |
194 (defun mark-end-of-sentence (arg) | |
195 "Put mark at end of sentence. Arg works as in forward-sentence." | |
196 (interactive "p") | |
197 (push-mark | |
198 (save-excursion | |
199 (forward-sentence arg) | |
200 (point)))) | |
201 | |
202 (defun transpose-sentences (arg) | |
203 "Interchange this (next) and previous sentence." | |
204 (interactive "*p") | |
205 (transpose-subr 'forward-sentence arg)) |