Mercurial > emacs
comparison lisp/textmodes/paragraphs.el @ 10864:33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
with ^. Doc fix.
(use-hard-newlines): Moved here from cmds.c. Made buffer-local. Doc fix.
(looking-at-hard): Deleted, not needed.
(forward-paragraph): Removes ^ from beginning of regexps, if
required. Look for paragraph-start and paragraph-separate at
left-margin, not BOL. Lines with just left-margin indentation are
treated like blank lines.
author | Boris Goldowsky <boris@gnu.org> |
---|---|
date | Wed, 01 Mar 1995 15:21:11 +0000 |
parents | 765fe4b8deaf |
children | e6bdaaa6ce1b |
comparison
equal
deleted
inserted
replaced
10863:8e6b25e41a99 | 10864:33769cbeb58e |
---|---|
26 ;; This package provides the paragraph-oriented commands documented in the | 26 ;; This package provides the paragraph-oriented commands documented in the |
27 ;; Emacs manual. | 27 ;; Emacs manual. |
28 | 28 |
29 ;;; Code: | 29 ;;; Code: |
30 | 30 |
31 (defconst paragraph-start "^[ \t\n\f]" "\ | 31 (defvar use-hard-newlines nil |
32 "Non-nil means to distinguish hard and soft newlines. | |
33 When this is non-nil, the functions `newline' and `open-line' add the | |
34 text-property `hard' to newlines that they insert. Also, a line is | |
35 only considered as a candidate to match `paragraph-start' or | |
36 `paragraph-separate' if it follows a hard newline. Newlines not | |
37 marked hard are called \"soft\", and are always internal to | |
38 paragraphs. The fill functions always insert soft newlines. | |
39 | |
40 Each buffer has its own value of this variable.") | |
41 (make-variable-buffer-local 'use-hard-newlines) | |
42 | |
43 (defconst paragraph-start "[ \t\n\f]" "\ | |
32 *Regexp for beginning of a line that starts OR separates paragraphs. | 44 *Regexp for beginning of a line that starts OR separates paragraphs. |
33 This regexp should match lines that separate paragraphs | 45 This regexp should match lines that separate paragraphs |
34 and should also match lines that start a paragraph | 46 and should also match lines that start a paragraph |
35 \(and are part of that paragraph). | 47 \(and are part of that paragraph). |
36 | 48 |
49 This is matched against the text at the left margin, which is not necessarily | |
50 the beginning of the line, so it should never use \"^\" as an anchor. This | |
51 ensures that the paragraph functions will work equally well within a region | |
52 of text indented by a margin setting. | |
53 | |
37 The variable `paragraph-separate' specifies how to distinguish | 54 The variable `paragraph-separate' specifies how to distinguish |
38 lines that start paragraphs from lines that separate them. | 55 lines that start paragraphs from lines that separate them. |
39 | 56 |
40 If the variable `use-hard-newlines' is nonnil, then only lines following a | 57 If the variable `use-hard-newlines' is nonnil, then only lines following a |
41 hard newline are considered to match.") | 58 hard newline are considered to match.") |
42 | 59 |
43 (defconst paragraph-separate "^[ \t\f]*$" "\ | 60 ;; paragraph-start requires a hard newline, but paragraph-separate does not: |
61 ;; It is assumed that paragraph-separate is distinctive enough to be believed | |
62 ;; whenever it occurs, while it is reasonable to set paragraph-start to | |
63 ;; something very minimal, even including "." (which makes every hard newline | |
64 ;; start a new paragraph). | |
65 | |
66 (defconst paragraph-separate "[ \t\f]*$" "\ | |
44 *Regexp for beginning of a line that separates paragraphs. | 67 *Regexp for beginning of a line that separates paragraphs. |
45 If you change this, you may have to change paragraph-start also. | 68 If you change this, you may have to change paragraph-start also. |
46 | 69 |
47 If the variable `use-hard-newlines' is nonnil, then only lines following a | 70 This is matched against the text at the left margin, which is not necessarily |
48 hard newline are considered to match.") | 71 the beginning of the line, so it should not use \"^\" as an anchor. This |
72 ensures that the paragraph functions will work equally within a region of | |
73 text indented by a margin setting.") | |
49 | 74 |
50 (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\ | 75 (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\ |
51 *Regexp describing the end of a sentence. | 76 *Regexp describing the end of a sentence. |
52 All paragraph boundaries also end sentences, regardless. | 77 All paragraph boundaries also end sentences, regardless. |
53 | 78 |
59 *Regexp describing line-beginnings that separate pages.") | 84 *Regexp describing line-beginnings that separate pages.") |
60 | 85 |
61 (defvar paragraph-ignore-fill-prefix nil "\ | 86 (defvar paragraph-ignore-fill-prefix nil "\ |
62 Non-nil means the paragraph commands are not affected by `fill-prefix'. | 87 Non-nil means the paragraph commands are not affected by `fill-prefix'. |
63 This is desirable in modes where blank lines are the paragraph delimiters.") | 88 This is desirable in modes where blank lines are the paragraph delimiters.") |
64 | |
65 (defsubst looking-at-hard (re) | |
66 ;; Just for convenience in writing the function below. | |
67 (and (or (null use-hard-newlines) | |
68 (bobp) | |
69 (get-text-property (1- (point)) 'hard)) | |
70 (looking-at re))) | |
71 | 89 |
72 (defun forward-paragraph (&optional arg) | 90 (defun forward-paragraph (&optional arg) |
73 "Move forward to end of paragraph. | 91 "Move forward to end of paragraph. |
74 With arg N, do it N times; negative arg -N means move backward N paragraphs. | 92 With arg N, do it N times; negative arg -N means move backward N paragraphs. |
75 | 93 |
81 (or arg (setq arg 1)) | 99 (or arg (setq arg 1)) |
82 (let* ((fill-prefix-regexp | 100 (let* ((fill-prefix-regexp |
83 (and fill-prefix (not (equal fill-prefix "")) | 101 (and fill-prefix (not (equal fill-prefix "")) |
84 (not paragraph-ignore-fill-prefix) | 102 (not paragraph-ignore-fill-prefix) |
85 (regexp-quote fill-prefix))) | 103 (regexp-quote fill-prefix))) |
104 ;; Remove ^ from paragraph-start and paragraph-sep if they are there. | |
105 ;; These regexps shouldn't be anchored, because we look for them | |
106 ;; starting at the left-margin. This allows paragraph commands to | |
107 ;; work normally with indented text. | |
108 ;; This hack will not find problem cases like "whatever\\|^something". | |
109 (paragraph-start (if (and (not (equal "" paragraph-start)) | |
110 (equal ?^ (aref paragraph-start 0))) | |
111 (substring paragraph-start 1) | |
112 paragraph-start)) | |
113 (paragraph-separate (if (and (not (equal "" paragraph-start)) | |
114 (equal ?^ (aref paragraph-separate 0))) | |
115 (substring paragraph-separate 1) | |
116 paragraph-separate)) | |
86 (paragraph-separate | 117 (paragraph-separate |
87 (if fill-prefix-regexp | 118 (if fill-prefix-regexp |
88 (concat paragraph-separate "\\|^" | 119 (concat paragraph-separate "\\|" |
89 fill-prefix-regexp "[ \t]*$") | 120 fill-prefix-regexp "[ \t]*$") |
90 paragraph-separate))) | 121 paragraph-separate)) |
122 ;; This is used for searching. | |
123 (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)")) | |
124 start) | |
91 (while (and (< arg 0) (not (bobp))) | 125 (while (and (< arg 0) (not (bobp))) |
92 (if (and (not (looking-at-hard paragraph-separate)) | 126 (if (and (not (looking-at paragraph-separate)) |
93 (re-search-backward "^\n" (max (1- (point)) (point-min)) t) | 127 (re-search-backward "^\n" (max (1- (point)) (point-min)) t) |
94 (looking-at-hard paragraph-separate)) | 128 (looking-at paragraph-separate)) |
95 nil | 129 nil |
96 ;; Move back over paragraph-separating lines. | 130 ;; Move back over paragraph-separating lines. |
97 (forward-char -1) (beginning-of-line) | 131 (forward-char -1) (beginning-of-line) |
98 (while (and (not (bobp)) | 132 (while (and (not (bobp)) |
99 (looking-at-hard paragraph-separate)) | 133 (progn (move-to-left-margin) |
100 (forward-line -1)) | 134 (looking-at paragraph-separate))) |
135 (forward-line -1)) | |
101 (if (bobp) | 136 (if (bobp) |
102 nil | 137 nil |
103 ;; Go to end of the previous (non-separating) line. | 138 ;; Go to end of the previous (non-separating) line. |
104 (end-of-line) | 139 (end-of-line) |
105 ;; Search back for line that starts or separates paragraphs. | 140 ;; Search back for line that starts or separates paragraphs. |
106 (if (if fill-prefix-regexp | 141 (if (if fill-prefix-regexp |
107 ;; There is a fill prefix; it overrides paragraph-start. | 142 ;; There is a fill prefix; it overrides paragraph-start. |
108 (progn | 143 (progn |
109 (while (progn (beginning-of-line) | 144 (while (and (progn (beginning-of-line) (not (bobp))) |
110 (and (not (bobp)) | 145 (progn (move-to-left-margin) |
111 (not (looking-at-hard | 146 (not (looking-at paragraph-separate))) |
112 paragraph-separate)) | 147 (looking-at fill-prefix-regexp)) |
113 (looking-at fill-prefix-regexp))) | 148 (forward-line -1)) |
114 (forward-line -1)) | |
115 (not (bobp))) | 149 (not (bobp))) |
116 (while (and (re-search-backward paragraph-start nil 1) | 150 (while (and (re-search-backward sp-paragraph-start nil 1) |
117 use-hard-newlines | 151 ;; Found a candidate, but need to check if it is a |
152 ;; REAL paragraph-start. | |
118 (not (bobp)) | 153 (not (bobp)) |
119 (null (get-text-property (1- (point)) 'hard))) | 154 (progn (setq start (point)) |
120 (if (not (bobp)) (backward-char 1))) | 155 (move-to-left-margin) |
156 (not (looking-at paragraph-separate))) | |
157 (or (not (looking-at paragraph-start)) | |
158 (and use-hard-newlines | |
159 (not (get-text-property (1- start) | |
160 'hard))))) | |
161 (goto-char start)) | |
121 (> (point) (point-min))) | 162 (> (point) (point-min))) |
122 ;; Found one. | 163 ;; Found one. |
123 (progn | 164 (progn |
124 ;; Move forward over paragraph separators. | 165 ;; Move forward over paragraph separators. |
125 ;; We know this cannot reach the place we started | 166 ;; We know this cannot reach the place we started |
126 ;; because we know we moved back over a non-separator. | 167 ;; because we know we moved back over a non-separator. |
127 (while (and (not (eobp)) (looking-at-hard paragraph-separate)) | 168 (while (and (not (eobp)) |
169 (progn (move-to-left-margin) | |
170 (looking-at paragraph-separate))) | |
128 (forward-line 1)) | 171 (forward-line 1)) |
129 (if (eq (char-after (- (point) 2)) ?\n) | 172 ;; If line before paragraph is just margin, back up to there. |
130 (forward-line -1))) | 173 (end-of-line 0) |
174 (if (> (current-column) (current-left-margin)) | |
175 (forward-char 1) | |
176 (skip-chars-backward " \t") | |
177 (if (not (bolp)) | |
178 (forward-line 1)))) | |
131 ;; No starter or separator line => use buffer beg. | 179 ;; No starter or separator line => use buffer beg. |
132 (goto-char (point-min))))) | 180 (goto-char (point-min))))) |
133 (setq arg (1+ arg))) | 181 (setq arg (1+ arg))) |
134 (while (and (> arg 0) (not (eobp))) | 182 (while (and (> arg 0) (not (eobp))) |
135 (beginning-of-line) | |
136 (while (prog1 (and (not (eobp)) | 183 (while (prog1 (and (not (eobp)) |
137 (looking-at-hard paragraph-separate)) | 184 (progn (move-to-left-margin) (not (eobp))) |
185 (looking-at paragraph-separate)) | |
138 (forward-line 1))) | 186 (forward-line 1))) |
139 (if fill-prefix-regexp | 187 (if fill-prefix-regexp |
140 ;; There is a fill prefix; it overrides paragraph-start. | 188 ;; There is a fill prefix; it overrides paragraph-start. |
141 (while (and (not (eobp)) | 189 (while (and (not (eobp)) |
142 (not (looking-at-hard paragraph-separate)) | 190 (progn (move-to-left-margin) (not (eobp))) |
191 (not (looking-at paragraph-separate)) | |
143 (looking-at fill-prefix-regexp)) | 192 (looking-at fill-prefix-regexp)) |
144 (forward-line 1)) | 193 (forward-line 1)) |
145 (while (and (re-search-forward paragraph-start nil 1) | 194 (while (and (re-search-forward sp-paragraph-start nil 1) |
146 (not (eobp)) | 195 (not (eobp)) |
147 use-hard-newlines | 196 (progn (setq start (match-beginning 0)) |
148 (null (get-text-property (1- (match-beginning 0)) 'hard))) | 197 (goto-char start) |
198 (move-to-left-margin) | |
199 (not (looking-at paragraph-separate))) | |
200 (or (not (looking-at paragraph-start)) | |
201 (and use-hard-newlines | |
202 (not (get-text-property (1- start) 'hard))))) | |
149 (forward-char 1)) | 203 (forward-char 1)) |
150 (if (< (point) (point-max)) | 204 (if (< (point) (point-max)) |
151 (goto-char (match-beginning 0)))) | 205 (goto-char start))) |
152 (setq arg (1- arg))))) | 206 (setq arg (1- arg))))) |
153 | 207 |
154 (defun backward-paragraph (&optional arg) | 208 (defun backward-paragraph (&optional arg) |
155 "Move backward to start of paragraph. | 209 "Move backward to start of paragraph. |
156 With arg N, do it N times; negative arg -N means move forward N paragraphs. | 210 With arg N, do it N times; negative arg -N means move forward N paragraphs. |