|
|
1.1 root 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. With arg, do it arg times.
27: A line which paragraph-start matches either separates paragraphs
28: \(if paragraph-separate matches it also) or is the first line of a paragraph.
29: A paragraph end is the beginning of a line which is not part of the paragraph
30: to which the end of the previous line belongs, or the end of the buffer."
31: (interactive "p")
32: (or arg (setq arg 1))
33: (let* ((fill-prefix-regexp
34: (and fill-prefix (not (equal fill-prefix ""))
35: (not paragraph-ignore-fill-prefix)
36: (regexp-quote fill-prefix)))
37: (paragraph-separate
38: (if fill-prefix-regexp
39: (concat paragraph-separate "\\|^"
40: fill-prefix-regexp "[ \t]*$")
41: paragraph-separate)))
42: (while (< arg 0)
43: (if (and (not (looking-at paragraph-separate))
44: (re-search-backward "^\n" (max (1- (point)) (point-min)) t))
45: nil
46: (forward-char -1) (beginning-of-line)
47: (while (and (not (bobp)) (looking-at paragraph-separate))
48: (forward-line -1))
49: (end-of-line)
50: ;; Search back for line that starts or separates paragraphs.
51: (if (if fill-prefix-regexp
52: ;; There is a fill prefix; it overrides paragraph-start.
53: (progn
54: (while (progn (beginning-of-line)
55: (and (not (bobp))
56: (not (looking-at paragraph-separate))
57: (looking-at fill-prefix-regexp)))
58: (forward-line -1))
59: (not (bobp)))
60: (re-search-backward paragraph-start nil t))
61: ;; Found one.
62: (progn
63: (while (and (not (eobp)) (looking-at paragraph-separate))
64: (forward-line 1))
65: (if (eq (char-after (- (point) 2)) ?\n)
66: (forward-line -1)))
67: ;; No starter or separator line => use buffer beg.
68: (goto-char (point-min))))
69: (setq arg (1+ arg)))
70: (while (> arg 0)
71: (beginning-of-line)
72: (while (prog1 (and (not (eobp))
73: (looking-at paragraph-separate))
74: (forward-line 1)))
75: (if fill-prefix-regexp
76: ;; There is a fill prefix; it overrides paragraph-start.
77: (while (and (not (eobp))
78: (not (looking-at paragraph-separate))
79: (looking-at fill-prefix-regexp))
80: (forward-line 1))
81: (if (re-search-forward paragraph-start nil t)
82: (goto-char (match-beginning 0))
83: (goto-char (point-max))))
84: (setq arg (1- arg)))))
85:
86: (defun backward-paragraph (&optional arg)
87: "Move backward to start of paragraph. With arg, do it arg times.
88: A paragraph start is the beginning of a line which is a first-line-of-paragraph
89: or which is ordinary text and follows a paragraph-separating line; except:
90: if the first real line of a paragraph is preceded by a blank line,
91: the paragraph starts at that blank line.
92: See forward-paragraph for more information."
93: (interactive "p")
94: (or arg (setq arg 1))
95: (forward-paragraph (- arg)))
96:
97: (defun mark-paragraph ()
98: "Put point at beginning of this paragraph, mark at end."
99: (interactive)
100: (forward-paragraph 1)
101: (push-mark nil t)
102: (backward-paragraph 1))
103:
104: (defun kill-paragraph (arg)
105: "Kill to end of paragraph."
106: (interactive "*p")
107: (kill-region (point) (progn (forward-paragraph arg) (point))))
108:
109: (defun backward-kill-paragraph (arg)
110: "Kill back to start of paragraph."
111: (interactive "*p")
112: (kill-region (point) (progn (backward-paragraph arg) (point))))
113:
114: (defun transpose-paragraphs (arg)
115: "Interchange this (or next) paragraph with previous one."
116: (interactive "*p")
117: (transpose-subr 'forward-paragraph arg))
118:
119: (defun start-of-paragraph-text ()
120: (let ((opoint (point)) npoint)
121: (forward-paragraph -1)
122: (setq npoint (point))
123: (skip-chars-forward " \t\n")
124: ;; If the range of blank lines found spans the original start point,
125: ;; try again from the beginning of it.
126: ;; Must be careful to avoid infinite loop
127: ;; when following a single return at start of buffer.
128: (if (and (>= (point) opoint) (< npoint opoint))
129: (progn
130: (goto-char npoint)
131: (if (> npoint (point-min))
132: (start-of-paragraph-text))))))
133:
134: (defun end-of-paragraph-text ()
135: (let ((opoint (point)))
136: (forward-paragraph 1)
137: (if (eq (preceding-char) ?\n) (forward-char -1))
138: (if (<= (point) opoint)
139: (progn
140: (forward-char 1)
141: (if (< (point) (point-max))
142: (end-of-paragraph-text))))))
143:
144: (defun forward-sentence (&optional arg)
145: "Move forward to next sentence-end. With argument, repeat.
146: With negative argument, move backward repeatedly to sentence-beginning.
147: Sentence ends are identified by the value of sentence-end
148: treated as a regular expression. Also, every paragraph boundary
149: terminates sentences as well."
150: (interactive "p")
151: (or arg (setq arg 1))
152: (while (< arg 0)
153: (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
154: (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
155: (goto-char (1- (match-end 0)))
156: (goto-char par-beg)))
157: (setq arg (1+ arg)))
158: (while (> arg 0)
159: (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
160: (if (re-search-forward sentence-end par-end t)
161: (skip-chars-backward " \t\n")
162: (goto-char par-end)))
163: (setq arg (1- arg))))
164:
165: (defun backward-sentence (&optional arg)
166: "Move backward to start of sentence. With arg, do it arg times.
167: See forward-sentence for more information."
168: (interactive "p")
169: (or arg (setq arg 1))
170: (forward-sentence (- arg)))
171:
172: (defun kill-sentence (&optional arg)
173: "Kill from point to end of sentence.
174: With arg, repeat, or backward if negative arg."
175: (interactive "*p")
176: (let ((beg (point)))
177: (forward-sentence arg)
178: (kill-region beg (point))))
179:
180: (defun backward-kill-sentence (&optional arg)
181: "Kill back from point to start of sentence.
182: With arg, repeat, or forward if negative arg."
183: (interactive "*p")
184: (let ((beg (point)))
185: (backward-sentence arg)
186: (kill-region beg (point))))
187:
188: (defun mark-end-of-sentence (arg)
189: "Put mark at end of sentence. Arg works as in forward-sentence."
190: (interactive "p")
191: (push-mark
192: (save-excursion
193: (forward-sentence arg)
194: (point))))
195:
196: (defun transpose-sentences (arg)
197: "Interchange this (next) and previous sentence."
198: (interactive "*p")
199: (transpose-subr 'forward-sentence arg))
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.