[lice @ big huge rearrange. add hanoi. fix extended-command prefix bug.]
[lice.git] / src / lisp / paragraphs.lisp
blob3b23f16a26b5cbccec3f99bb6f8629f2c0c58eae
1 ;;; paragraphs.el --- paragraph and sentence parsing
3 ;; Copyright (C) 1985, 1986, 1987, 1991, 1994, 1995, 1996, 1997, 1999, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; This package provides the paragraph-oriented commands documented in the
29 ;; Emacs manual.
31 ;;; Code:
33 (in-package "LICE")
35 (defgroup paragraphs nil
36 "Paragraph and sentence parsing."
37 :group 'editing)
39 ;; (setf (get 'use-hard-newlines 'permanent-local) t)
40 ;; (define-minor-mode use-hard-newlines
41 ;; "Minor mode to distinguish hard and soft newlines.
42 ;; When active, the functions `newline' and `open-line' add the
43 ;; text-property `hard' to newlines that they insert, and a line is
44 ;; only considered as a candidate to match `paragraph-start' or
45 ;; `paragraph-separate' if it follows a hard newline.
47 ;; Prefix argument says to turn mode on if positive, off if negative.
48 ;; When the mode is turned on, if there are newlines in the buffer but no hard
49 ;; newlines, ask the user whether to mark as hard any newlines preceeding a
50 ;; `paragraph-start' line. From a program, second arg INSERT specifies whether
51 ;; to do this; it can be `never' to change nothing, t or `always' to force
52 ;; marking, `guess' to try to do the right thing with no questions, nil
53 ;; or anything else to ask the user.
55 ;; Newlines not marked hard are called \"soft\", and are always internal
56 ;; to paragraphs. The fill functions insert and delete only soft newlines."
57 ;; :group 'paragraphs
58 ;; :extra-args (insert)
59 ;; (when use-hard-newlines
60 ;; ;; Turn mode on
61 ;; ;; Intuit hard newlines --
62 ;; ;; mark as hard any newlines preceding a paragraph-start line.
63 ;; (if (or (eq insert t) (eq insert 'always)
64 ;; (and (not (eq 'never insert))
65 ;; (not (text-property-any (point-min) (point-max) 'hard t))
66 ;; (save-excursion
67 ;; (goto-char (point-min))
68 ;; (search-forward "\n" nil t))
69 ;; (or (eq insert 'guess)
70 ;; (y-or-n-p "Make newlines between paragraphs hard? "))))
71 ;; (save-excursion
72 ;; (goto-char (point-min))
73 ;; (while (search-forward "\n" nil t)
74 ;; (let ((pos (point)))
75 ;; (move-to-left-margin)
76 ;; (when (looking-at paragraph-start)
77 ;; (set-hard-newline-properties (1- pos) pos))
78 ;; ;; If paragraph-separate, newline after it is hard too.
79 ;; (when (looking-at paragraph-separate)
80 ;; (set-hard-newline-properties (1- pos) pos)
81 ;; (end-of-line)
82 ;; (unless (eobp)
83 ;; (set-hard-newline-properties (point) (1+ (point)))))))))))
85 (defcustom paragraph-start "\f|[ \t]*$" "\
86 Regexp for beginning of a line that starts OR separates paragraphs.
87 This regexp should match lines that separate paragraphs
88 and should also match lines that start a paragraph
89 \(and are part of that paragraph).
91 This is matched against the text at the left margin, which is not necessarily
92 the beginning of the line, so it should never use \"^\" as an anchor. This
93 ensures that the paragraph functions will work equally well within a region
94 of text indented by a margin setting.
96 The variable `paragraph-separate' specifies how to distinguish
97 lines that start paragraphs from lines that separate them.
99 If the variable `use-hard-newlines' is non-nil, then only lines following a
100 hard newline are considered to match."
101 :group 'paragraphs
102 :type 'regexp)
103 ;;;###autoload(put 'paragraph-start 'safe-local-variable 'stringp)
105 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
106 ;; It is assumed that paragraph-separate is distinctive enough to be believed
107 ;; whenever it occurs, while it is reasonable to set paragraph-start to
108 ;; something very minimal, even including "." (which makes every hard newline
109 ;; start a new paragraph).
111 (defcustom paragraph-separate "[ \t\f]*$"
112 "Regexp for beginning of a line that separates paragraphs.
113 If you change this, you may have to change `paragraph-start' also.
115 This is matched against the text at the left margin, which is not necessarily
116 the beginning of the line, so it should not use \"^\" as an anchor. This
117 ensures that the paragraph functions will work equally within a region of
118 text indented by a margin setting."
119 :group 'paragraphs
120 :type 'regexp)
121 ;;;###autoload(put 'paragraph-separate 'safe-local-variable 'stringp)
123 (defcustom sentence-end-double-space t
124 "Non-nil means a single space does not end a sentence.
125 This is relevant for filling. See also `sentence-end-without-period'
126 and `colon-double-space'.
128 This value is used by the function `sentence-end' to construct the
129 regexp describing the end of a sentence, when the value of the variable
130 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
131 :type 'boolean
132 :group 'fill)
133 ;;;###autoload(put 'sentence-end-double-space 'safe-local-variable 'booleanp)
135 (defcustom sentence-end-without-period nil
136 "Non-nil means a sentence will end without a period.
137 For example, a sentence in Thai text ends with double space but
138 without a period.
140 This value is used by the function `sentence-end' to construct the
141 regexp describing the end of a sentence, when the value of the variable
142 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
143 :type 'boolean
144 :group 'fill)
145 ;;;###autoload(put 'sentence-end-without-period 'safe-local-variable 'booleanp)
147 (defcustom sentence-end-without-space
148 "" ;; missing "\e$B!#!%!)!*\e$A!##.#?#!\e$(0!$!%!)!*\e$(G!$!%!)!*\e(B"
149 "String of characters that end sentence without following spaces.
151 This value is used by the function `sentence-end' to construct the
152 regexp describing the end of a sentence, when the value of the variable
153 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
154 :group 'paragraphs
155 :type 'string)
156 ;;;###autoload(put 'sentence-end-without-space 'safe-local-variable 'stringp)
158 (defcustom sentence-end nil
159 "Regexp describing the end of a sentence.
160 The value includes the whitespace following the sentence.
161 All paragraph boundaries also end sentences, regardless.
163 The value nil means to use the default value defined by the
164 function `sentence-end'. You should always use this function
165 to obtain the value of this variable."
166 :group 'paragraphs
167 :type '(choice regexp (const :tag "Use default value" nil)))
168 ;;;###autoload(put 'sentence-end 'safe-local-variable 'string-or-null-p)
170 (defcustom sentence-end-base "[.?!][]\"'\\)}]*" ;; missing \e$B!I\e$,1r}\e(B
171 "Regexp matching the basic end of a sentence, not including following space."
172 :group 'paragraphs
173 :type 'string
174 :version "22.1")
175 ;;;###autoload(put 'sentence-end-base 'safe-local-variable 'stringp)
177 (defun sentence-end ()
178 "Return the regexp describing the end of a sentence.
180 This function returns either the value of the variable `sentence-end'
181 if it is non-nil, or the default value constructed from the
182 variables `sentence-end-base', `sentence-end-double-space',
183 `sentence-end-without-period' and `sentence-end-without-space'.
185 The default value specifies that in order to be recognized as the
186 end of a sentence, the ending period, question mark, or exclamation point
187 must be followed by two spaces, with perhaps some closing delimiters
188 in between. See Info node `(elisp)Standard Regexps'."
189 (or sentence-end
190 (concat (if sentence-end-without-period "\\w |")
192 sentence-end-base
193 (if sentence-end-double-space
194 "($| $|\t| )" "($|[\t ])")
195 ;; FIXME: "|[" sentence-end-without-space "]+"
197 "[ \t\n]*")))
199 (defcustom page-delimiter "^\014"
200 "Regexp describing line-beginnings that separate pages."
201 :group 'paragraphs
202 :type 'regexp)
203 ;;;###autoload(put 'page-delimiter 'safe-local-variable 'stringp)
205 (defcustom paragraph-ignore-fill-prefix nil
206 "Non-nil means the paragraph commands are not affected by `*fill-prefix*'.
207 This is desirable in modes where blank lines are the paragraph delimiters."
208 :group 'paragraphs
209 :type 'boolean)
210 ;;;###autoload(put 'paragraph-ignore-fill-prefix 'safe-local-variable 'booleanp)
212 (defcommand forward-paragraph ((&optional arg)
213 :prefix)
214 "Move forward to end of paragraph.
215 With argument ARG, do it ARG times;
216 a negative argument ARG = -N means move backward N paragraphs.
218 A line which `paragraph-start' matches either separates paragraphs
219 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
220 A paragraph end is the beginning of a line which is not part of the paragraph
221 to which the end of the previous line belongs, or the end of the buffer.
222 Returns the count of paragraphs left to move."
223 (or arg (setq arg 1))
224 (let* ((opoint (point))
225 (fill-prefix-regexp
226 (and *fill-prefix* (not (equal *fill-prefix* ""))
227 (not paragraph-ignore-fill-prefix)
228 (regexp-quote *fill-prefix*)))
229 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
230 ;; These regexps shouldn't be anchored, because we look for them
231 ;; starting at the left-margin. This allows paragraph commands to
232 ;; work normally with indented text.
233 ;; This hack will not find problem cases like "whatever\\|^something".
234 (parstart (if (and (not (equal "" paragraph-start))
235 (equal #\^ (aref paragraph-start 0)))
236 (substring paragraph-start 1)
237 paragraph-start))
238 (parsep (if (and (not (equal "" paragraph-separate))
239 (equal #\^ (aref paragraph-separate 0)))
240 (substring paragraph-separate 1)
241 paragraph-separate))
242 (parsep
243 (if fill-prefix-regexp
244 (concat parsep "|"
245 fill-prefix-regexp "[ \t]*$")
246 parsep))
247 ;; This is used for searching.
248 (sp-parstart (concat "^[ \t]*(?:" parstart "|" parsep ")"))
249 start found-start)
250 (while (and (< arg 0) (not (bobp)))
251 (if (and (not (looking-at parsep))
252 (re-search-backward "^\n" :bound (max (1- (point)) (point-min)) :error nil)
253 ;;(search-backward "\n\n" :bound (max (1- (point)) (point-min)) :error nil)
254 (looking-at parsep))
255 (setq arg (1+ arg))
256 (progn
257 (setq start (point))
258 ;; Move back over paragraph-separating lines.
259 (forward-char -1) (beginning-of-line)
260 (while (and (not (bobp))
261 (progn (move-to-left-margin)
262 (looking-at parsep)))
263 (forward-line -1))
264 (if (bobp)
266 (progn
267 (setq arg (1+ arg))
268 ;; Go to end of the previous (non-separating) line.
269 (end-of-line)
270 ;; Search back for line that starts or separates paragraphs.
271 (if (if fill-prefix-regexp
272 ;; There is a fill prefix; it overrides parstart.
273 (let (multiple-lines)
274 (while (and (progn (beginning-of-line) (not (bobp)))
275 (progn (move-to-left-margin)
276 (not (looking-at parsep)))
277 (looking-at fill-prefix-regexp))
278 (unless (= (point) start)
279 (setq multiple-lines t))
280 (forward-line -1))
281 (move-to-left-margin)
282 ;; This deleted code caused a long hanging-indent line
283 ;; not to be filled together with the following lines.
284 ;; ;; Don't move back over a line before the paragraph
285 ;; ;; which doesn't start with fill-prefix
286 ;; ;; unless that is the only line we've moved over.
287 ;; (and (not (looking-at fill-prefix-regexp))
288 ;; multiple-lines
289 ;; (forward-line 1))
290 (not (bobp)))
291 (progn
292 (while (and (re-search-backward sp-parstart :error 1)
293 (setq found-start t)
294 ;; Found a candidate, but need to check if it is a
295 ;; REAL parstart.
296 (progn (setq start (point))
297 (move-to-left-margin)
298 (not (looking-at parsep)))
299 (not (and (looking-at parstart)
300 (or (not use-hard-newlines)
301 (bobp)
302 (get-text-property
303 (1- start) 'hard)))))
304 (setq found-start nil)
305 (goto-char start))
306 found-start))
307 ;; Found one.
308 (progn
309 ;; Move forward over paragraph separators.
310 ;; We know this cannot reach the place we started
311 ;; because we know we moved back over a non-separator.
312 (while (and (not (eobp))
313 (progn (move-to-left-margin)
314 (looking-at parsep)))
315 (forward-line 1))
316 ;; If line before paragraph is just margin, back up to there.
317 (end-of-line 0)
318 (if (> (current-column) (current-left-margin))
319 (forward-char 1)
320 (progn
321 (skip-chars-backward " \t")
322 (if (not (bolp))
323 (forward-line 1)))))
324 ;; No starter or separator line => use buffer beg.
325 (goto-char (point-min))))))))
327 (while (and (> arg 0) (not (eobp)))
328 ;; Move forward over separator lines...
329 (while (and (not (eobp))
330 (progn (move-to-left-margin) (not (eobp)))
331 (looking-at parsep))
332 (forward-line 1))
333 (unless (eobp) (setq arg (1- arg)))
334 ;; ... and one more line.
335 (forward-line 1)
336 (if fill-prefix-regexp
337 ;; There is a fill prefix; it overrides parstart.
338 (while (and (not (eobp))
339 (progn (move-to-left-margin) (not (eobp)))
340 (not (looking-at parsep))
341 (looking-at fill-prefix-regexp))
342 (forward-line 1))
343 (with-match-data
344 (while (and (re-search-forward sp-parstart :error 1)
345 (progn (setq start (match-beginning 0))
346 (goto-char start)
347 (not (eobp)))
348 (progn (move-to-left-margin)
349 (not (looking-at parsep)))
350 (or (not (looking-at parstart))
351 (and use-hard-newlines
352 (not (get-text-property (1- start) 'hard)))))
353 (forward-char 1))
354 (if (< (point) (point-max))
355 (goto-char start)))))
356 (constrain-to-field nil opoint t)
357 ;; Return the number of steps that could not be done.
358 arg))
360 (defcommand backward-paragraph ((&optional arg)
361 :prefix)
362 "Move backward to start of paragraph.
363 With argument ARG, do it ARG times;
364 a negative argument ARG = -N means move forward N paragraphs.
366 A paragraph start is the beginning of a line which is a
367 `first-line-of-paragraph' or which is ordinary text and follows a
368 paragraph-separating line; except: if the first real line of a
369 paragraph is preceded by a blank line, the paragraph starts at that
370 blank line.
372 See `forward-paragraph' for more information."
373 (or arg (setq arg 1))
374 (forward-paragraph (- arg)))
376 (defcommand mark-paragraph ((&optional arg allow-extend)
377 :prefix :prefix)
378 "Put point at beginning of this paragraph, mark at end.
379 The paragraph marked is the one that contains point or follows point.
381 With argument ARG, puts mark at end of a following paragraph, so that
382 the number of paragraphs marked equals ARG.
384 If ARG is negative, point is put at end of this paragraph, mark is put
385 at beginning of this or a previous paragraph.
387 Interactively, if this command is repeated
388 or (in Transient Mark mode) if the mark is active,
389 it marks the next ARG paragraphs after the ones already marked."
390 ;;(interactive "p\np")
391 (unless arg (setq arg 1))
392 (when (zerop arg)
393 (error "Cannot mark zero paragraphs"))
394 (cond ((and allow-extend
395 (or (and (eq *last-command* *this-command*) (mark t))
396 (and transient-mark-mode mark-active)))
397 (set-mark
398 (save-excursion
399 (goto-char (mark))
400 (forward-paragraph arg)
401 (point))))
403 (forward-paragraph arg)
404 (push-mark nil t t)
405 (backward-paragraph arg))))
407 (defcommand kill-paragraph ((arg)
408 :prefix)
409 "Kill forward to end of paragraph.
410 With arg N, kill forward to Nth end of paragraph;
411 negative arg -N means kill backward to Nth start of paragraph."
412 (kill-region (point) (progn (forward-paragraph arg) (point))))
414 (defcommand backward-kill-paragraph ((arg)
415 :prefix)
416 "Kill back to start of paragraph.
417 With arg N, kill back to Nth start of paragraph;
418 negative arg -N means kill forward to Nth end of paragraph."
419 (kill-region (point) (progn (backward-paragraph arg) (point))))
421 (defcommand transpose-paragraphs ((arg)
422 :prefix)
423 "Interchange this (or next) paragraph with previous one."
424 ;;(interactive "*p")
425 (transpose-subr 'forward-paragraph arg))
427 (defun start-of-paragraph-text ()
428 (let ((opoint (point)) npoint)
429 (forward-paragraph -1)
430 (setq npoint (point))
431 (skip-chars-forward " \t\n")
432 ;; If the range of blank lines found spans the original start point,
433 ;; try again from the beginning of it.
434 ;; Must be careful to avoid infinite loop
435 ;; when following a single return at start of buffer.
436 (if (and (>= (point) opoint) (< npoint opoint))
437 (progn
438 (goto-char npoint)
439 (if (> npoint (point-min))
440 (start-of-paragraph-text))))))
442 (defun end-of-paragraph-text ()
443 (let ((opoint (point)))
444 (forward-paragraph 1)
445 (if (eq (preceding-char) #\Newline) (forward-char -1))
446 (if (<= (point) opoint)
447 (progn
448 (forward-char 1)
449 (if (< (point) (point-max))
450 (end-of-paragraph-text))))))
452 (defcommand forward-sentence ((&optional arg)
453 :prefix)
454 "Move forward to next `sentence-end'. With argument, repeat.
455 With negative argument, move backward repeatedly to `sentence-beginning'.
457 The variable `sentence-end' is a regular expression that matches ends of
458 sentences. Also, every paragraph boundary terminates sentences as well."
459 (with-match-data
460 (or arg (setq arg 1))
461 (let ((opoint (point))
462 (sentence-end (sentence-end)))
463 (while (< arg 0)
464 (let ((pos (point))
465 (par-beg (save-excursion (start-of-paragraph-text) (point))))
466 (if (and (re-search-backward sentence-end :bound par-beg :error nil)
467 (or (< (match-end 0) pos)
468 (re-search-backward sentence-end :bound par-beg :error nil)))
469 (goto-char (match-end 0))
470 (goto-char par-beg)))
471 (setq arg (1+ arg)))
472 (while (> arg 0)
473 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
474 (if (re-search-forward sentence-end :bound par-end :error nil)
475 (skip-chars-backward " \t\n")
476 (goto-char par-end)))
477 (setq arg (1- arg)))
478 (constrain-to-field nil opoint t))))
480 (defcommand repunctuate-sentences ()
481 "Put two spaces at the end of sentences from point to the end of buffer.
482 It works using `query-replace-regexp'."
483 (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
484 "\\1\\2\\3 "))
487 (defcommand backward-sentence ((&optional arg)
488 :raw-prefix)
489 "Move backward to start of sentence. With arg, do it arg times.
490 See `forward-sentence' for more information."
491 (or arg (setq arg 1))
492 (forward-sentence (- arg)))
494 (defcommand kill-sentence ((&optional arg)
495 :prefix)
496 "Kill from point to end of sentence.
497 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
498 (kill-region (point) (progn (forward-sentence arg) (point))))
500 (defcommand backward-kill-sentence ((&optional arg)
501 :prefix)
502 "Kill back from point to start of sentence.
503 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
504 (kill-region (point) (progn (backward-sentence arg) (point))))
506 (defcommand mark-end-of-sentence ((arg)
507 :prefix)
508 "Put mark at end of sentence. Arg works as in `forward-sentence'.
509 If this command is repeated, it marks the next ARG sentences after the
510 ones already marked."
511 (push-mark
512 (save-excursion
513 (if (and (eq *last-command* *this-command*) (mark t))
514 (goto-char (mark)))
515 (forward-sentence arg)
516 (point))
517 nil t))
519 (defcommand transpose-sentences ((arg)
520 :prefix)
521 "Interchange this (next) and previous sentence."
522 ;;(interactive "*p")
523 (transpose-subr 'forward-sentence arg))
525 ;; Local Variables:
526 ;; coding: iso-2022-7bit
527 ;; End:
529 ;; arch-tag: e727eb1a-527a-4464-b9d7-9d3ec0d1a575
530 ;;; paragraphs.el ends here