|
|
1.1 root 1: ;; Paragraph and sentence parsing.
2: ;; Copyright (C) 1985 Richard M. Stallman.
3:
4: ;; This file is part of GNU Emacs.
5:
6: ;; GNU Emacs is distributed in the hope that it will be useful,
7: ;; but WITHOUT ANY WARRANTY. No author or distributor
8: ;; accepts responsibility to anyone for the consequences of using it
9: ;; or for whether it serves any particular purpose or works at all,
10: ;; unless he says so in writing. Refer to the GNU Emacs General Public
11: ;; License for full details.
12:
13: ;; Everyone is granted permission to copy, modify and redistribute
14: ;; GNU Emacs, but only under the conditions described in the
15: ;; GNU Emacs General Public License. A copy of this license is
16: ;; supposed to have been given to you along with GNU Emacs so you
17: ;; can know your rights and responsibilities. It should be in a
18: ;; file named COPYING. Among other things, the copyright notice
19: ;; and this notice must be preserved on all copies.
20:
21:
22: (defun forward-paragraph (&optional arg)
23: "Move forward to end of paragraph. With arg, do it arg times.
24: A line which paragraph-start matches either separates paragraphs
25: \(if paragraph-separate matches it also) or is the first line of a paragraph.
26: A paragraph end is the beginning of a line which is not part of the paragraph
27: to which the end of the previous line belongs, or the end of the buffer."
28: (interactive "p")
29: (or arg (setq arg 1))
30: (let* ((fill-prefix-regexp
31: (and fill-prefix (not (equal fill-prefix ""))
32: (regexp-quote fill-prefix)))
33: (paragraph-separate
34: (if fill-prefix-regexp
35: (concat paragraph-separate "\\|^"
36: fill-prefix-regexp "[ \t]*$")
37: paragraph-separate)))
38: (while (< arg 0)
39: (if (and (not (looking-at paragraph-separate))
40: (re-search-backward "^\n" (max (1- (point)) (point-min)) t))
41: nil
42: (forward-char -1) (beginning-of-line)
43: (while (and (not (bobp)) (looking-at paragraph-separate))
44: (forward-line -1))
45: (end-of-line)
46: ;; Saerch back for line that starts or separates paragraphs.
47: (if (if (and fill-prefix (not (equal fill-prefix "")))
48: ;; There is a fill prefix; it overrides paragraph-start.
49: (progn
50: (while (progn (beginning-of-line)
51: (and (not (bobp))
52: (not (looking-at paragraph-separate))
53: (looking-at fill-prefix-regexp)))
54: (forward-line -1))
55: (not (bobp)))
56: (re-search-backward paragraph-start nil t))
57: ;; Found one.
58: (progn
59: (while (looking-at paragraph-separate)
60: (forward-line 1))
61: (if (eq (char-after (- (point) 2)) ?\n)
62: (forward-line -1)))
63: ;; No starter or separator line => use buffer beg.
64: (goto-char (point-min))))
65: (setq arg (1+ arg)))
66: (while (> arg 0)
67: (beginning-of-line)
68: (while (prog1 (and (not (eobp))
69: (looking-at paragraph-separate))
70: (forward-line 1)))
71: (if (and fill-prefix (not (equal fill-prefix "")))
72: ;; There is a fill prefix; it overrides paragraph-start.
73: (while (and (not (eobp))
74: (not (looking-at paragraph-separate))
75: (looking-at fill-prefix-regexp))
76: (forward-line 1))
77: (if (re-search-forward paragraph-start nil t)
78: (goto-char (match-beginning 0))
79: (goto-char (point-max))))
80: (setq arg (1- arg)))))
81:
82: (defun backward-paragraph (&optional arg)
83: "Move backward to start of paragraph. With arg, do it arg times.
84: A paragraph start is the beginning of a line which is a first-line-of-paragraph
85: or which is ordinary text and follows a paragraph-separating line; except:
86: if the first real line of a paragraph is preceded by a blank line,
87: the paragraph starts at that blank line.
88: See forward-paragraph for more information."
89: (interactive "p")
90: (or arg (setq arg 1))
91: (forward-paragraph (- arg)))
92:
93: (defun mark-paragraph ()
94: "Put point at beginning of this paragraph, mark at end."
95: (interactive)
96: (forward-paragraph 1)
97: (set-mark (point))
98: (backward-paragraph 1))
99:
100: (defun kill-paragraph (arg)
101: "Kill to end of paragraph."
102: (interactive "p")
103: (kill-region (point) (progn (forward-paragraph arg) (point))))
104:
105: (defun backward-kill-paragraph (arg)
106: "Kill back to start of paragraph."
107: (interactive "p")
108: (kill-region (point) (progn (backward-paragraph arg) (point))))
109:
110: (defun transpose-paragraphs (arg)
111: "Interchange this (or next) paragraph with previous one."
112: (interactive "p")
113: (transpose-subr 'forward-paragraph arg))
114:
115: (defun start-of-paragraph-text ()
116: (let ((opoint (point)) npoint)
117: (forward-paragraph -1)
118: (setq npoint (point))
119: (skip-chars-forward " \t\n")
120: (if (>= (point) opoint)
121: (progn
122: (goto-char npoint)
123: (if (> npoint (point-min))
124: (start-of-paragraph-text))))))
125:
126: (defun end-of-paragraph-text ()
127: (let ((opoint (point)))
128: (forward-paragraph 1)
129: (if (eq (preceding-char) ?\n) (forward-char -1))
130: (if (<= (point) opoint)
131: (progn
132: (forward-char 1)
133: (if (< (point) (point-max))
134: (end-of-paragraph-text))))))
135:
136: (defun forward-sentence (&optional arg)
137: "Move forward to next sentence-end. With argument, repeat.
138: With negative argument, move backward repeatedly to sentence-beginning.
139: Sentence ends are identified by the value of sentence-end
140: treated as a regular expression. Also, every paragraph boundary
141: terminates sentences as well."
142: (interactive "p")
143: (or arg (setq arg 1))
144: (while (< arg 0)
145: (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
146: (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
147: (goto-char (1- (match-end 0)))
148: (goto-char par-beg)))
149: (setq arg (1+ arg)))
150: (while (> arg 0)
151: (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
152: (if (re-search-forward sentence-end par-end t)
153: (skip-chars-backward " \t\n")
154: (goto-char par-end)))
155: (setq arg (1- arg))))
156:
157: (defun backward-sentence (&optional arg)
158: "Move backward to start of sentence. With arg, do it arg times.
159: See forward-sentence for more information."
160: (interactive "p")
161: (or arg (setq arg 1))
162: (forward-sentence (- arg)))
163:
164: (defun kill-sentence (&optional arg)
165: "Kill from point to end of sentence.
166: With arg, repeat, or backward if negative arg."
167: (interactive "p")
168: (let ((beg (point)))
169: (forward-sentence arg)
170: (kill-region beg (point))))
171:
172: (defun backward-kill-sentence (&optional arg)
173: "Kill back from point to start of sentence.
174: With arg, repeat, or forward if negative arg."
175: (interactive "p")
176: (let ((beg (point)))
177: (backward-sentence arg)
178: (kill-region beg (point))))
179:
180: (defun mark-end-of-sentence (arg)
181: "Put mark at end of sentence. Arg works as in forward-sentence."
182: (interactive "p")
183: (push-mark
184: (save-excursion
185: (forward-sentence arg)
186: (point))))
187:
188: (defun transpose-sentences (arg)
189: "Interchange this (next) and previous sentence."
190: (interactive "p")
191: (transpose-subr 'forward-sentence arg))
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.