Debug message fix
[sxemacs] / lisp / paragraphs.el
1 ;;; paragraphs.el --- paragraph and sentence parsing.
2
3 ;; Copyright (C) 1985, 86, 87, 91, 94, 95, 97 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: wp, dumped
7
8 ;; This file is part of SXEmacs.
9
10 ;; SXEmacs 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 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; SXEmacs 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 this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Synched up with: FSF 19.34.
24
25 ;;; Commentary:
26
27 ;; This file is dumped with SXEmacs.
28
29 ;; This package provides the paragraph-oriented commands documented in the
30 ;; XEmacs Reference Manual.
31
32 ;; 06/11/1997 - Use char-(after|before) instead of
33 ;;  (following|preceding)-char. -slb
34
35 ;;; Code:
36
37 (defvar use-hard-newlines nil
38     "Non-nil means to distinguish hard and soft newlines.
39 When this is non-nil, the functions `newline' and `open-line' add the
40 text-property `hard' to newlines that they insert.  Also, a line is
41 only considered as a candidate to match `paragraph-start' or
42 `paragraph-separate' if it follows a hard newline.  Newlines not
43 marked hard are called \"soft\", and are always internal to
44 paragraphs.  The fill functions always insert soft newlines.
45
46 Each buffer has its own value of this variable.")
47 (make-variable-buffer-local 'use-hard-newlines)
48
49 (defun use-hard-newlines (&optional arg insert)
50   "Minor mode to distinguish hard and soft newlines.
51 When active, the functions `newline' and `open-line' add the
52 text-property `hard' to newlines that they insert, and a line is
53 only considered as a candidate to match `paragraph-start' or
54 `paragraph-separate' if it follows a hard newline.
55
56 Prefix argument says to turn mode on if positive, off if negative.
57 When the mode is turned on, if there are newlines in the buffer but no hard
58 newlines, ask the user whether to mark as hard any newlines preceding a
59 `paragraph-start' line.  From a program, second arg INSERT specifies whether
60 to do this; it can be `never' to change nothing, t or `always' to force
61 marking, `guess' to try to do the right thing with no questions, nil
62 or anything else to ask the user.
63
64 Newlines not marked hard are called \"soft\", and are always internal
65 to paragraphs.  The fill functions insert and delete only soft newlines."
66   (interactive (list current-prefix-arg nil))
67   (if (or (<= (prefix-numeric-value arg) 0)
68           (and use-hard-newlines (null arg)))
69       ;; Turn mode off
70       (setq use-hard-newlines nil)
71     ;; Turn mode on
72     ;; Intuit hard newlines --
73     ;;   mark as hard any newlines preceding a paragraph-start line.
74     (if (or (eq insert t) (eq insert 'always)
75             (and (not (eq 'never insert))
76                  (not use-hard-newlines)
77                  (not (text-property-any (point-min) (point-max) 'hard t))
78                  (save-excursion
79                    (goto-char (point-min))
80                    (search-forward "\n" nil t))
81                  (or (eq insert 'guess)
82                      (y-or-n-p "Make newlines between paragraphs hard? "))))
83         (save-excursion
84           (goto-char (point-min))
85           (while (search-forward "\n" nil t)
86             (let ((pos (point)))
87               (move-to-left-margin)
88               (if (looking-at paragraph-start)
89                   (progn
90                     (set-hard-newline-properties (1- pos) pos)
91                     ;; If paragraph-separate, newline after it is hard too.
92                     (if (looking-at paragraph-separate)
93                         (progn
94                           (end-of-line)
95                           (if (not (eobp))
96                               (set-hard-newline-properties
97                                (point) (1+ (point))))))))))))
98     (setq use-hard-newlines t)))
99
100 (defconst paragraph-start "[ \t\n\f]"
101   "*Regexp for beginning of a line that starts OR separates paragraphs.
102 This regexp should match lines that separate paragraphs
103 and should also match lines that start a paragraph
104 \(and are part of that paragraph).
105
106 This is matched against the text at the left margin, which is not necessarily
107 the beginning of the line, so it should never use \"^\" as an anchor.  This
108 ensures that the paragraph functions will work equally well within a region
109 of text indented by a margin setting.
110
111 The variable `paragraph-separate' specifies how to distinguish
112 lines that start paragraphs from lines that separate them.
113
114 If the variable `use-hard-newlines' is non-nil, then only lines following a
115 hard newline are considered to match.")
116
117 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
118 ;; It is assumed that paragraph-separate is distinctive enough to be believed
119 ;; whenever it occurs, while it is reasonable to set paragraph-start to
120 ;; something very minimal, even including "." (which makes every hard newline
121 ;; start a new paragraph).
122
123 (defconst paragraph-separate "[ \t\f]*$"
124   "*Regexp for beginning of a line that separates paragraphs.
125 If you change this, you may have to change `paragraph-start' also.
126
127 A line matching this is not part of any paragraph.
128
129 This is matched against the text at the left margin, which is not necessarily
130 the beginning of the line, so it should not use \"^\" as an anchor.  This
131 ensures that the paragraph functions will work equally within a region of
132 text indented by a margin setting.")
133
134 (defconst sentence-end "[.?!][]\"')}]*\\($\\| $\\|\t\\|  \\)[ \t\n]*"
135   "*Regexp describing the end of a sentence.
136 All paragraph boundaries also end sentences, regardless.
137
138 In order to be recognized as the end of a sentence, the ending period,
139 question mark, or exclamation point must be followed by two spaces,
140 unless it's inside some sort of quotes or parenthesis.")
141
142 (defconst page-delimiter "^\014"
143   "*Regexp describing line-beginnings that separate pages.")
144
145 (defvar paragraph-ignore-fill-prefix nil
146   "Non-nil means the paragraph commands are not affected by `fill-prefix'.
147 This is desirable in modes where blank lines are the paragraph delimiters.")
148
149 (defun forward-paragraph (&optional arg)
150   "Move forward to end of paragraph.
151 With arg N, do it N times; negative arg -N means move backward N paragraphs.
152
153 A line which `paragraph-start' matches either separates paragraphs
154 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
155 A paragraph end is the beginning of a line which is not part of the paragraph
156 to which the end of the previous line belongs, or the end of the buffer."
157   (interactive "_p") ; XEmacs
158   (or arg (setq arg 1))
159   (let* ((fill-prefix-regexp
160           (and fill-prefix (not (equal fill-prefix ""))
161                (not paragraph-ignore-fill-prefix)
162                (regexp-quote fill-prefix)))
163          ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
164          ;; These regexps shouldn't be anchored, because we look for them
165          ;; starting at the left-margin.  This allows paragraph commands to
166          ;; work normally with indented text.
167          ;; This hack will not find problem cases like "whatever\\|^something".
168          (paragraph-start (if (and (not (equal "" paragraph-start))
169                                    (equal ?^ (aref paragraph-start 0)))
170                               (substring paragraph-start 1)
171                             paragraph-start))
172          (paragraph-separate (if (and (not (equal "" paragraph-start))
173                                       (equal ?^ (aref paragraph-separate 0)))
174                               (substring paragraph-separate 1)
175                             paragraph-separate))
176          (paragraph-separate
177           (if fill-prefix-regexp
178               (concat paragraph-separate "\\|"
179                       fill-prefix-regexp "[ \t]*$")
180             paragraph-separate))
181          ;; This is used for searching.
182          (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)"))
183          start)
184     (while (and (< arg 0) (not (bobp)))
185       (if (and (not (looking-at paragraph-separate))
186                (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
187                (looking-at paragraph-separate))
188           nil
189         (setq start (point))
190         ;; Move back over paragraph-separating lines.
191         (backward-char 1) (beginning-of-line)
192         (while (and (not (bobp))
193                     (progn (move-to-left-margin)
194                            (looking-at paragraph-separate)))
195           (forward-line -1))
196         (if (bobp)
197             nil
198           ;; Go to end of the previous (non-separating) line.
199           (end-of-line)
200           ;; Search back for line that starts or separates paragraphs.
201           (if (if fill-prefix-regexp
202                   ;; There is a fill prefix; it overrides paragraph-start.
203                   (let (multiple-lines)
204                     (while (and (progn (beginning-of-line) (not (bobp)))
205                                 (progn (move-to-left-margin)
206                                        (not (looking-at paragraph-separate)))
207                                 (looking-at fill-prefix-regexp))
208                       (if (not (= (point) start))
209                           (setq multiple-lines t))
210                       (forward-line -1))
211                     (move-to-left-margin)
212                     ;; Don't move back over a line before the paragraph
213                     ;; which doesn't start with fill-prefix
214                     ;; unless that is the only line we've moved over.
215                     (and (not (looking-at fill-prefix-regexp))
216                          multiple-lines
217                          (forward-line 1))
218                     (not (bobp)))
219                 (while (and (re-search-backward sp-paragraph-start nil 1)
220                             ;; Found a candidate, but need to check if it is a
221                             ;; REAL paragraph-start.
222                             (not (bobp))
223                             (progn (setq start (point))
224                                    (move-to-left-margin)
225                                    (not (looking-at paragraph-separate)))
226                             (or (not (looking-at paragraph-start))
227                                 (and use-hard-newlines
228                                      (not (get-text-property (1- start)
229                                                              'hard)))))
230                   (goto-char start))
231                 (> (point) (point-min)))
232               ;; Found one.
233               (progn
234                 ;; Move forward over paragraph separators.
235                 ;; We know this cannot reach the place we started
236                 ;; because we know we moved back over a non-separator.
237                 (while (and (not (eobp))
238                             (progn (move-to-left-margin)
239                                    (looking-at paragraph-separate)))
240                   (forward-line 1))
241                 ;; If line before paragraph is just margin, back up to there.
242                 (end-of-line 0)
243                 (if (> (current-column) (current-left-margin))
244                     (forward-char 1)
245                   (skip-chars-backward " \t")
246                   (if (not (bolp))
247                       (forward-line 1))))
248             ;; No starter or separator line => use buffer beg.
249             (goto-char (point-min)))))
250       (setq arg (1+ arg)))
251     (while (and (> arg 0) (not (eobp)))
252       ;; Move forward over separator lines, and one more line.
253       (while (prog1 (and (not (eobp))
254                          (progn (move-to-left-margin) (not (eobp)))
255                          (looking-at paragraph-separate))
256                (forward-line 1)))
257       (if fill-prefix-regexp
258           ;; There is a fill prefix; it overrides paragraph-start.
259           (while (and (not (eobp))
260                       (progn (move-to-left-margin) (not (eobp)))
261                       (not (looking-at paragraph-separate))
262                       (looking-at fill-prefix-regexp))
263             (forward-line 1))
264         (while (and (re-search-forward sp-paragraph-start nil 1)
265                     (progn (setq start (match-beginning 0))
266                            (goto-char start)
267                            (not (eobp)))
268                     (progn (move-to-left-margin)
269                            (not (looking-at paragraph-separate)))
270                     (or (not (looking-at paragraph-start))
271                         (and use-hard-newlines
272                              (not (get-text-property (1- start) 'hard)))))
273           (forward-char 1))
274         (if (< (point) (point-max))
275             (goto-char start)))
276       (setq arg (1- arg)))))
277
278 (defun backward-paragraph (&optional arg)
279   "Move backward to start of paragraph.
280 With arg N, do it N times; negative arg -N means move forward N paragraphs.
281
282 A paragraph start is the beginning of a line which is a
283 `first-line-of-paragraph' or which is ordinary text and follows a
284 paragraph-separating line; except: if the first real line of a
285 paragraph is preceded by a blank line, the paragraph starts at that
286 blank line.
287
288 See `forward-paragraph' for more information."
289   (interactive "_p") ; XEmacs
290   (or arg (setq arg 1))
291   (forward-paragraph (- arg)))
292
293 (defun mark-paragraph ()
294   "Put point at beginning of this paragraph, mark at end.
295 The paragraph marked is the one that contains point or follows point."
296   (interactive)
297   (forward-paragraph 1)
298   (push-mark nil t t)
299   (backward-paragraph 1))
300
301 (defun kill-paragraph (arg)
302   "Kill forward to end of paragraph.
303 With arg N, kill forward to Nth end of paragraph;
304 negative arg -N means kill backward to Nth start of paragraph."
305   (interactive "*p") ; XEmacs
306   (kill-region (point) (progn (forward-paragraph arg) (point))))
307
308 (defun backward-kill-paragraph (arg)
309   "Kill back to start of paragraph.
310 With arg N, kill back to Nth start of paragraph;
311 negative arg -N means kill forward to Nth end of paragraph."
312   (interactive "*p") ; XEmacs
313   (kill-region (point) (progn (backward-paragraph arg) (point))))
314
315 (defun transpose-paragraphs (arg)
316   "Interchange this (or next) paragraph with previous one."
317   (interactive "*p")
318   (transpose-subr 'forward-paragraph arg))
319
320 (defun start-of-paragraph-text ()
321   (let ((opoint (point)) npoint)
322     (forward-paragraph -1)
323     (setq npoint (point))
324     (skip-chars-forward " \t\n")
325     ;; If the range of blank lines found spans the original start point,
326     ;; try again from the beginning of it.
327     ;; Must be careful to avoid infinite loop
328     ;; when following a single return at start of buffer.
329     (if (and (>= (point) opoint) (< npoint opoint))
330         (progn
331           (goto-char npoint)
332           (if (> npoint (point-min))
333               (start-of-paragraph-text))))))
334
335 (defun end-of-paragraph-text ()
336   (let ((opoint (point)))
337     (forward-paragraph 1)
338     (if (eq (char-before (point)) ?\n) (backward-char 1))
339     (if (<= (point) opoint)
340         (progn
341           (forward-char 1)
342           (if (< (point) (point-max))
343               (end-of-paragraph-text))))))
344
345 (defun forward-sentence (&optional arg)
346   "Move forward to next `sentence-end'.  With argument, repeat.
347 With negative argument, move backward repeatedly to `sentence-beginning'.
348
349 The variable `sentence-end' is a regular expression that matches ends of
350 sentences.  A paragraph boundary also terminates a sentence."
351   (interactive "_p") ; XEmacs
352   (or arg (setq arg 1))
353   (while (< arg 0)
354     (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
355       (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
356           (goto-char (1- (match-end 0)))
357         (goto-char par-beg)))
358     (setq arg (1+ arg)))
359   (while (> arg 0)
360     (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
361       (if (re-search-forward sentence-end par-end t)
362           (skip-chars-backward " \t\n")
363         (goto-char par-end)))
364     (setq arg (1- arg))))
365
366 (defun backward-sentence (&optional arg)
367   "Move backward to start of sentence.  With arg, do it arg times.
368 See `forward-sentence' for more information."
369   (interactive "_p") ; XEmacs
370   (or arg (setq arg 1))
371   (forward-sentence (- arg)))
372
373 (defun kill-sentence (&optional arg)
374   "Kill from point to end of sentence.
375 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
376   (interactive "*p") ; XEmacs
377   (kill-region (point) (progn (forward-sentence arg) (point))))
378
379 (defun backward-kill-sentence (&optional arg)
380   "Kill back from point to start of sentence.
381 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
382   (interactive "*p") ; XEmacs
383   (kill-region (point) (progn (backward-sentence arg) (point))))
384
385 (defun mark-end-of-sentence (arg)
386   "Put mark at end of sentence.  Arg works as in `forward-sentence'."
387   (interactive "p")
388   ;; FSF Version:
389 ;  (push-mark
390 ;   (save-excursion
391 ;     (forward-sentence arg)
392 ;     (point))
393 ;   nil t))
394   (mark-something 'mark-end-of-sentence 'forward-sentence arg))
395
396 (defun mark-end-of-line (arg)
397   "Put mark at end of line.  Arg works as in `end-of-line'."
398   (interactive "p")
399   (mark-something 'mark-end-of-line 'end-of-line arg))
400
401
402 (defun transpose-sentences (arg)
403   "Interchange this (next) and previous sentence."
404   (interactive "*p")
405   (transpose-subr 'forward-sentence arg))
406
407 ;;; paragraphs.el ends here