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