annotate lisp/textmodes/fill.el @ 10738:e7d5b119b583

(ps-print-version): Add caveat about version number. (ps-begin-file): Fix format code for /LineHeight.
author Richard M. Stallman <rms@gnu.org>
date Tue, 14 Feb 1995 05:55:53 +0000
parents 6f9d0e697678
children 7f9e55cdc349
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
662
8a533acedb77 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 638
diff changeset
1 ;;; fill.el --- fill commands for Emacs
8a533acedb77 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 638
diff changeset
2
7300
cc7cd83ccf3f Update copyright.
Karl Heuer <kwzh@gnu.org>
parents: 5770
diff changeset
3 ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
846
20674ae6bf52 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 821
diff changeset
4
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 807
diff changeset
5 ;; Keywords: wp
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 732
diff changeset
6
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
7 ;; This file is part of GNU Emacs.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
8
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
10 ;; it under the terms of the GNU General Public License as published by
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 732
diff changeset
11 ;; the Free Software Foundation; either version 2, or (at your option)
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
12 ;; any later version.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
13
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
14 ;; GNU Emacs is distributed in the hope that it will be useful,
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
17 ;; GNU General Public License for more details.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
18
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
19 ;; You should have received a copy of the GNU General Public License
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
22
2307
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2109
diff changeset
23 ;;; Commentary:
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2109
diff changeset
24
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2109
diff changeset
25 ;; All the commands for filling text. These are documented in the Emacs
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2109
diff changeset
26 ;; manual.
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2109
diff changeset
27
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 732
diff changeset
28 ;;; Code:
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
29
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
30 (defconst fill-individual-varying-indent nil
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
31 "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
32 Non-nil means changing indent doesn't end a paragraph.
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
33 That mode can handle paragraphs with extra indentation on the first line,
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
34 but it requires separator lines between paragraphs.
5770
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
35 A value of nil means that any change in indentation starts a new paragraph.")
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
36
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
37 (defconst sentence-end-double-space t
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
38 "*Non-nil means a single space does not end a sentence.")
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
39
10631
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
40 (defvar fill-paragraph-function nil
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
41 "Mode-specific function to fill a paragraph.")
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
42
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
43 (defun set-fill-prefix ()
5770
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
44 "Set the fill prefix to the current line up to point.
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
45 Filling expects lines to start with the fill prefix and
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
46 reinserts the fill prefix in each resulting line."
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
47 (interactive)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
48 (setq fill-prefix (buffer-substring
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
49 (save-excursion (beginning-of-line) (point))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
50 (point)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
51 (if (equal fill-prefix "")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
52 (setq fill-prefix nil))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
53 (if fill-prefix
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
54 (message "fill-prefix: \"%s\"" fill-prefix)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
55 (message "fill-prefix cancelled")))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
56
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
57 (defconst adaptive-fill-mode t
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
58 "*Non-nil means determine a paragraph's fill prefix from its text.")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
59
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
60 (defconst adaptive-fill-regexp "[ \t]*\\([>*] +\\)?"
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
61 "*Regexp to match text at start of line that constitutes indentation.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
62 If Adaptive Fill mode is enabled, whatever text matches this pattern
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
63 on the second line of a paragraph is used as the standard indentation
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
64 for the paragraph.")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
65
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
66 (defun current-fill-column ()
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
67 "Return the fill-column to use for this line.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
68 The fill-column to use for a buffer is stored in the variable `fill-column',
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
69 but can be locally modified by the `right-margin' text property, which is
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
70 subtracted from `fill-column'.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
71
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
72 The fill column to use for a line is the first column at which the column
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
73 number equals or exceeds the local fill-column - right-margin difference."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
74 (save-excursion
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
75 (let* ((here (progn (beginning-of-line) (point)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
76 (here-col 0)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
77 (eol (progn (end-of-line) (point)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
78 margin fill-col change col)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
79 ;; Look separately at each region of line with a different right-margin
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
80 (while (and (setq margin (get-text-property here 'right-margin)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
81 fill-col (- fill-column (or margin 0))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
82 change (text-property-not-all here eol
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
83 'right-margin margin))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
84 (progn (goto-char (1- change))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
85 (setq col (current-column))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
86 (< col fill-col)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
87 (setq here change
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
88 here-col col))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
89 (max here-col fill-col))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
90
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
91 (defun canonically-space-region (beg end)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
92 "Remove extra spaces between words in region.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
93 Puts one space between words in region; two between sentences.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
94 Remove indenation from each line."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
95 (interactive "r")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
96 (save-excursion
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
97 (goto-char beg)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
98 ;; Nuke tabs; they get screwed up in a fill.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
99 ;; This is quick, but loses when a tab follows the end of a sentence.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
100 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
101 ;; Blame the typist.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
102 (subst-char-in-region beg end ?\t ?\ )
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
103 (while (and (< (point) end)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
104 (re-search-forward " *" end t))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
105 (delete-region
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
106 (+ (match-beginning 0)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
107 ;; Determine number of spaces to leave:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
108 (save-excursion
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
109 (skip-chars-backward " ]})\"'")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
110 (cond ((and sentence-end-double-space
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
111 (memq (preceding-char) '(?. ?? ?!))) 2)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
112 ((char-equal (preceding-char) ?\n) 0)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
113 (t 1))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
114 (match-end 0)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
115 ;; Make sure sentences ending at end of line get an extra space.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
116 ;; loses on split abbrevs ("Mr.\nSmith")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
117 (goto-char beg)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
118 (while (and (< (point) end)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
119 (re-search-forward "[.?!][])}\"']*$" end t))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
120 (insert-and-inherit ? ))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
121
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
122 (defun fill-region-as-paragraph (from to &optional justify nosqueeze)
5770
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
123 "Fill region as one paragraph: break lines to fit `fill-column'.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
124 This removes any paragraph breaks in the region.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
125 It performs justification according to the `justification' text-property,
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
126 but a prefix arg can be used to override this and request full justification.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
127
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
128 Optional fourth arg NOSQUEEZE non-nil means to leave whitespace other than line
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
129 breaks untouched. Normally it is made canonical before filling.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
130
5770
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
131 If `sentence-end-double-space' is non-nil, then period followed by one
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
132 space does not end a sentence, so don't break a line there."
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
133 (interactive "r\nP")
1332
955ebdb98095 (fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents: 1067
diff changeset
134 ;; Arrange for undoing the fill to restore point.
955ebdb98095 (fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents: 1067
diff changeset
135 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
955ebdb98095 (fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents: 1067
diff changeset
136 (setq buffer-undo-list (cons (point) buffer-undo-list)))
10472
82ff279a4cea (current-justification): Renamed from current-justification. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents: 10468
diff changeset
137 (or justify (setq justify (current-justification)))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
138
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
139 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
10497
6e00457b6f86 (fill-region-as-paragraph): At end, advance over any newlines
Richard M. Stallman <rms@gnu.org>
parents: 10473
diff changeset
140 (let ((fill-prefix fill-prefix)
6e00457b6f86 (fill-region-as-paragraph): At end, advance over any newlines
Richard M. Stallman <rms@gnu.org>
parents: 10473
diff changeset
141 (skip-after 0))
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
142 ;; Figure out how this paragraph is indented, if desired.
670
bff41708644e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 662
diff changeset
143 (if (and adaptive-fill-mode
bff41708644e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 662
diff changeset
144 (or (null fill-prefix) (string= fill-prefix "")))
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
145 (save-excursion
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
146 (goto-char (min from to))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
147 (if (eolp) (forward-line 1))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
148 (forward-line 1)
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
149 (move-to-left-margin)
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
150 (if (< (point) (max from to))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
151 (let ((start (point)))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
152 (re-search-forward adaptive-fill-regexp)
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
153 (setq fill-prefix (buffer-substring start (point)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
154 (set-text-properties 0 (length fill-prefix) nil fill-prefix))
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
155 (goto-char (min from to))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
156 (if (eolp) (forward-line 1))
7721
b04897534717 (fill-region-as-paragraph): if left-margin is nonzero,
Richard M. Stallman <rms@gnu.org>
parents: 7300
diff changeset
157 ;; If paragraph has only one line, don't assume in general
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
158 ;; that additional lines would have the same starting
729
5d684b81ac6b *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 719
diff changeset
159 ;; decoration. Assume no indentation.
5d684b81ac6b *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 719
diff changeset
160 )))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
161
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
162 (if (not justify) ; filling disabled: just check indentation
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
163 (progn
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
164 (goto-char (min from to))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
165 (setq to (max from to))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
166 (while (< (point) to)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
167 (if (not (eolp))
10473
95868461fc78 (fill-region-as-paragraph): left-margin renamed to current-left-margin.
Richard M. Stallman <rms@gnu.org>
parents: 10472
diff changeset
168 (if (< (current-indentation) (current-left-margin))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
169 (indent-to-left-margin)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
170 (forward-line 1)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
171
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
172 (save-restriction
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
173 (let (beg)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
174 (goto-char (min from to))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
175 (skip-chars-forward "\n")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
176 (setq beg (point))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
177 (goto-char (max from to))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
178 (skip-chars-backward "\n")
10497
6e00457b6f86 (fill-region-as-paragraph): At end, advance over any newlines
Richard M. Stallman <rms@gnu.org>
parents: 10473
diff changeset
179 (setq skip-after (- to (point)))
6e00457b6f86 (fill-region-as-paragraph): At end, advance over any newlines
Richard M. Stallman <rms@gnu.org>
parents: 10473
diff changeset
180 ;; If we omit some final newlines from the end of the narrowing,
6e00457b6f86 (fill-region-as-paragraph): At end, advance over any newlines
Richard M. Stallman <rms@gnu.org>
parents: 10473
diff changeset
181 ;; arrange to advance past them at the end.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
182 (setq to (point)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
183 from beg)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
184 (goto-char from)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
185 (beginning-of-line)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
186 (narrow-to-region (point) to))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
187 (if use-hard-newlines
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
188 (remove-text-properties from to '(hard nil)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
189 ;; Make sure first line is indented (at least) to left margin...
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
190 (if (or (memq justify '(right center))
10473
95868461fc78 (fill-region-as-paragraph): left-margin renamed to current-left-margin.
Richard M. Stallman <rms@gnu.org>
parents: 10472
diff changeset
191 (< (current-indentation) (current-left-margin)))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
192 (indent-to-left-margin))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
193 ;; and remove indentation from other lines.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
194 (beginning-of-line 2)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
195 (indent-region (point) (point-max) 0)
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
196 ;; Delete the fill prefix from every line except the first.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
197 ;; The first line may not even have a fill prefix.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
198 (goto-char from)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
199 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
200 (concat "[ \t]*"
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
201 (regexp-quote fill-prefix)))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
202 (and fpre
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
203 (progn
10473
95868461fc78 (fill-region-as-paragraph): left-margin renamed to current-left-margin.
Richard M. Stallman <rms@gnu.org>
parents: 10472
diff changeset
204 (if (>= (+ (current-left-margin) (length fill-prefix))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
205 (current-fill-column))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
206 (error "fill-prefix too long for specified width"))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
207 (goto-char from)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
208 (forward-line 1)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
209 (while (not (eobp))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
210 (if (looking-at fpre)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
211 (delete-region (point) (match-end 0)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
212 (forward-line 1))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
213 (goto-char from)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
214 (and (looking-at fpre) (goto-char (match-end 0)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
215 (setq from (point)))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
216 ;; "from" is now before the text to fill,
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
217 ;; but after any fill prefix on the first line.
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
218
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
219 ;; Make sure sentences ending at end of line get an extra space.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
220 ;; loses on split abbrevs ("Mr.\nSmith")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
221 (while (re-search-forward "[.?!][])}\"']*$" nil t)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
222 (insert-and-inherit ? ))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
223 (goto-char from)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
224 (skip-chars-forward " \t")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
225 ;; Then change all newlines to spaces.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
226 (subst-char-in-region from (point-max) ?\n ?\ )
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
227 (if (and nosqueeze (not (eq justify 'full)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
228 nil
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
229 (canonically-space-region (point) (point-max))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
230 (goto-char (point-max))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
231 (delete-horizontal-space)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
232 (insert-and-inherit " "))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
233 (goto-char (point-min))
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
234
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
235 ;; This is the actual filling loop.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
236 (let ((prefixcol 0) linebeg)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
237 (while (not (eobp))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
238 (setq linebeg (point))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
239 (move-to-column (1+ (current-fill-column)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
240 (if (eobp)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
241 (or nosqueeze (delete-horizontal-space))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
242 ;; Move back to start of word.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
243 (skip-chars-backward "^ \n" linebeg)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
244 ;; Don't break after a period followed by just one space.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
245 ;; Move back to the previous place to break.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
246 ;; The reason is that if a period ends up at the end of a line,
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
247 ;; further fills will assume it ends a sentence.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
248 ;; If we now know it does not end a sentence,
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
249 ;; avoid putting it at the end of the line.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
250 (if sentence-end-double-space
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
251 (while (and (> (point) (+ linebeg 2))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
252 (eq (preceding-char) ?\ )
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
253 (not (eq (following-char) ?\ ))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
254 (eq (char-after (- (point) 2)) ?\.))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
255 (forward-char -2)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
256 (skip-chars-backward "^ \n" linebeg)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
257 (if (if (zerop prefixcol)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
258 (save-excursion
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
259 (skip-chars-backward " " linebeg)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
260 (bolp))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
261 (>= prefixcol (current-column)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
262 ;; Keep at least one word even if fill prefix exceeds margin.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
263 ;; This handles all but the first line of the paragraph.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
264 ;; Meanwhile, don't stop at a period followed by one space.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
265 (let ((first t))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
266 (move-to-column prefixcol)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
267 (while (and (not (eobp))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
268 (or first
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
269 (and (not (bobp))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
270 sentence-end-double-space
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
271 (save-excursion (forward-char -1)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
272 (and (looking-at "\\. ")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
273 (not (looking-at "\\. ")))))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
274 (skip-chars-forward " ")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
275 (skip-chars-forward "^ \n")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
276 (setq first nil)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
277 ;; Normally, move back over the single space between the words.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
278 (forward-char -1))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
279 (if (and fill-prefix (zerop prefixcol)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
280 (< (- (point) (point-min)) (length fill-prefix))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
281 (string= (buffer-substring (point-min) (point))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
282 (substring fill-prefix 0 (- (point) (point-min)))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
283 ;; Keep at least one word even if fill prefix exceeds margin.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
284 ;; This handles the first line of the paragraph.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
285 ;; Don't stop at a period followed by just one space.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
286 (let ((first t))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
287 (while (and (not (eobp))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
288 (or first
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
289 (and (not (bobp))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
290 sentence-end-double-space
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
291 (save-excursion (forward-char -1)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
292 (and (looking-at "\\. ")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
293 (not (looking-at "\\. ")))))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
294 (skip-chars-forward " ")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
295 (skip-chars-forward "^ \n")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
296 (setq first nil))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
297 ;; Replace whitespace here with one newline, then indent to left
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
298 ;; margin.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
299 (skip-chars-backward " ")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
300 (insert ?\n)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
301 ;; Give newline the properties of the space(s) it replaces
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
302 (set-text-properties (1- (point)) (point)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
303 (text-properties-at (point)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
304 (indent-to-left-margin)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
305 ;; Insert the fill prefix after indentation.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
306 ;; Set prefixcol so whitespace in the prefix won't get lost.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
307 (and fill-prefix (not (equal fill-prefix ""))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
308 (progn
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
309 (insert-and-inherit fill-prefix)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
310 (setq prefixcol (current-column)))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
311 ;; Justify the line just ended, if desired.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
312 (if justify
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
313 (if (eobp)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
314 (justify-current-line justify t t)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
315 (forward-line -1)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
316 (justify-current-line justify nil t)
10497
6e00457b6f86 (fill-region-as-paragraph): At end, advance over any newlines
Richard M. Stallman <rms@gnu.org>
parents: 10473
diff changeset
317 (forward-line 1))))))
6e00457b6f86 (fill-region-as-paragraph): At end, advance over any newlines
Richard M. Stallman <rms@gnu.org>
parents: 10473
diff changeset
318 (forward-char skip-after))))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
319
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
320 (defun fill-paragraph (arg)
5770
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
321 "Fill paragraph at or after point. Prefix arg means justify as well.
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
322 If `sentence-end-double-space' is non-nil, then period followed by one
10631
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
323 space does not end a sentence, so don't break a line there.
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
324
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
325 If `fill-paragraph-function' is non-nil, we call it (passing our
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
326 argument to it), and if it returns non-nil, we simply return its value."
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
327 (interactive "P")
10631
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
328 (or (and fill-paragraph-function
10636
6f9d0e697678 (fill-paragraph): Bind fill-paragraph-function to nil before calling it.
Richard M. Stallman <rms@gnu.org>
parents: 10631
diff changeset
329 (let ((function fill-paragraph-function)
6f9d0e697678 (fill-paragraph): Bind fill-paragraph-function to nil before calling it.
Richard M. Stallman <rms@gnu.org>
parents: 10631
diff changeset
330 fill-paragraph-function)
6f9d0e697678 (fill-paragraph): Bind fill-paragraph-function to nil before calling it.
Richard M. Stallman <rms@gnu.org>
parents: 10631
diff changeset
331 (funcall function arg)))
10631
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
332 (let ((before (point)))
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
333 (save-excursion
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
334 (forward-paragraph)
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
335 (or (bolp) (newline 1))
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
336 (let ((end (point))
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
337 (beg (progn (backward-paragraph) (point))))
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
338 (goto-char before)
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
339 (if use-hard-newlines
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
340 ;; Can't use fill-region-as-paragraph, since this paragraph may
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
341 ;; still contain hard newlines. See fill-region.
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
342 (fill-region beg end arg)
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
343 (fill-region-as-paragraph beg end arg)))))))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
344
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
345 (defun fill-region (from to &optional justify nosqueeze to-eop)
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
346 "Fill each of the paragraphs in the region.
5770
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
347 Prefix arg (non-nil third arg, if called from program) means justify as well.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
348
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
349 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
350 whitespace other than line breaks untouched, and fifth arg TO-EOP
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
351 non-nil means to keep filling to the end of the paragraph (or next
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
352 hard newline, if `use-hard-newlines' is on).
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
353
5770
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
354 If `sentence-end-double-space' is non-nil, then period followed by one
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
355 space does not end a sentence, so don't break a line there."
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
356 (interactive "r\nP")
10423
166aed4ba78b (fill-region-as-paragraph): Don't modify newline
Richard M. Stallman <rms@gnu.org>
parents: 10110
diff changeset
357 ;; If using hard newlines, break at every one for filling purposes rather
166aed4ba78b (fill-region-as-paragraph): Don't modify newline
Richard M. Stallman <rms@gnu.org>
parents: 10110
diff changeset
358 ;; than breaking at normal paragraph breaks.
166aed4ba78b (fill-region-as-paragraph): Don't modify newline
Richard M. Stallman <rms@gnu.org>
parents: 10110
diff changeset
359 (let ((paragraph-start (if use-hard-newlines "^" paragraph-start))
166aed4ba78b (fill-region-as-paragraph): Don't modify newline
Richard M. Stallman <rms@gnu.org>
parents: 10110
diff changeset
360 end beg)
10110
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
361 (save-restriction
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
362 (goto-char (max from to))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
363 (if to-eop
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
364 (progn (skip-chars-backward "\n")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
365 (forward-paragraph)))
10110
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
366 (setq end (point))
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
367 (goto-char (setq beg (min from to)))
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
368 (beginning-of-line)
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
369 (narrow-to-region (point) end)
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
370 (while (not (eobp))
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
371 (let ((initial (point))
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
372 (end (progn
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
373 (forward-paragraph 1) (point))))
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
374 (forward-paragraph -1)
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
375 (if (< (point) beg)
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
376 (goto-char beg))
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
377 (if (>= (point) initial)
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
378 (fill-region-as-paragraph (point) end justify nosqueeze)
10110
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
379 (goto-char end)))))))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
380
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
381
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
382 (defconst default-justification 'left
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
383 "*Method of justifying text not otherwise specified.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
384 Possible values are `left', `right', `full', `center', or `none'.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
385 The requested kind of justification is done whenever lines are filled.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
386 The `justification' text-property can locally override this variable.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
387 This variable automatically becomes buffer-local when set in any fashion.")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
388 (make-variable-buffer-local 'default-justification)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
389
10472
82ff279a4cea (current-justification): Renamed from current-justification. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents: 10468
diff changeset
390 (defun current-justification ()
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
391 "How should we justify this line?
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
392 This returns the value of the text-property `justification',
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
393 or the variable `default-justification' if there is no text-property.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
394 However, it returns nil rather than `none' to mean \"don't justify\"."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
395 (let ((j (or (get-text-property
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
396 ;; Make sure we're looking at paragraph body.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
397 (save-excursion (skip-chars-forward " \t") (point))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
398 'justification)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
399 default-justification)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
400 (if (eq 'none j)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
401 nil
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
402 j)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
403
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
404 (defun set-justification (begin end value)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
405 "Set the region's justification style.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
406 If the mark is not active, this operates on the current line.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
407 In interactive use, if the BEGIN and END points are
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
408 not at line breaks, they are moved outward to the next line break.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
409 If `use-hard-newlines' is true, they are moved to the next hard line breaks.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
410 Noninteractively, the values of BEGIN, END and VALUE are not modified."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
411 (interactive (list (if mark-active (region-beginning) (point))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
412 (if mark-active (region-end) (point))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
413 (let ((s (completing-read
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
414 "Set justification to: "
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
415 '(("left") ("right") ("full") ("center")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
416 ("none"))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
417 nil t)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
418 (if (equal s "")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
419 (error "")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
420 (intern s)))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
421 (let* ((paragraph-start (if use-hard-newlines "^" paragraph-start)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
422 (save-excursion
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
423 (goto-char begin)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
424 (while (bolp) (forward-char 1))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
425 (backward-paragraph)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
426 (setq begin (point))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
427
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
428 (goto-char end)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
429 (skip-chars-backward " \t\n" begin)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
430 (forward-paragraph)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
431 (setq end (point))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
432 (set-mark begin)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
433 (goto-char end)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
434 (y-or-n-p "set-just")))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
435 (put-text-property begin end 'justification value)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
436 (fill-region begin end nil t))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
437
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
438 (defun set-justification-none (b e)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
439 "Disable automatic filling for paragraphs in the region.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
440 If the mark is not active, this applies to the current paragraph."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
441 (interactive "r")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
442 (set-justification b e 'none))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
443
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
444 (defun set-justification-left (b e)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
445 "Make paragraphs in the region left-justified.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
446 This is usually the default, but see `enriched-default-justification'.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
447 If the mark is not active, this applies to the current paragraph."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
448 (interactive "r")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
449 (set-justification b e 'left))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
450
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
451 (defun set-justification-right (b e)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
452 "Make paragraphs in the region right-justified:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
453 Flush at the right margin and ragged on the left.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
454 If the mark is not active, this applies to the current paragraph."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
455 (interactive "r")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
456 (set-justification b e 'right))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
457
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
458 (defun set-justification-full (b e)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
459 "Make paragraphs in the region fully justified:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
460 Flush on both margins.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
461 If the mark is not active, this applies to the current paragraph."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
462 (interactive "r")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
463 (set-justification b e 'both))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
464
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
465 (defun set-justification-center (b e)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
466 "Make paragraphs in the region centered.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
467 If the mark is not active, this applies to the current paragraph."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
468 (interactive "r")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
469 (set-justification b e 'center))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
470
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
471 (defun justify-current-line (&optional how eop nosqueeze)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
472 "Add spaces to line point is in, so it ends at `fill-column'.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
473 Optional first argument HOW specifies alternate type of justification:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
474 it can be `left', `right', `full', `center', or `none'.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
475 If HOW is t, will justify however the `justification' function says.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
476 Any other value, including nil, is taken to mean `full'.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
477 Second arg EOP non-nil means that this is the last line of the paragraph, so
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
478 it will not be stretched by full justification.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
479 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
480 otherwise it is made canonical."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
481 (interactive (list 'full nil nil))
10472
82ff279a4cea (current-justification): Renamed from current-justification. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents: 10468
diff changeset
482 (if (eq t how) (setq how (or (current-justification) 'none)))
10543
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
483 (or (memq how '(none left))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
484 (save-excursion
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
485 (save-restriction
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
486 (let ((fc (current-fill-column))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
487 ncols beg indent end)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
488 (end-of-line)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
489 (if (and use-hard-newlines (null eop)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
490 (get-text-property (point) 'hard))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
491 (setq eop t))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
492 (skip-chars-backward " \t")
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
493 (if (= (current-column) fc)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
494 nil ;; Quick exit if it appears to be properly justified already.
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
495 (setq end (point))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
496 (beginning-of-line)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
497 (skip-chars-forward " \t")
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
498 (if (and fill-prefix
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
499 (equal fill-prefix
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
500 (buffer-substring (point)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
501 (min (point-max)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
502 (+ (point) (length fill-prefix))))))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
503 (forward-char (length fill-prefix)))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
504 (setq indent (current-column))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
505 (setq beg (point))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
506 (goto-char end)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
507 (cond ((or (eq 'none how) (eq 'left how))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
508 nil)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
509 ((eq 'right how)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
510 (setq ncols (- (+ indent (current-fill-column))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
511 (current-column)))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
512 (if (> ncols 0)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
513 (indent-line-to ncols)))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
514 ((eq 'center how)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
515 (setq ncols
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
516 (/ (- (+ indent (current-fill-column)) (current-column))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
517 2))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
518 (if (>= ncols 0)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
519 (indent-line-to ncols)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
520 (message "Line to long to center")))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
521 (t ;; full
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
522 (narrow-to-region beg end)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
523 (or nosqueeze
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
524 (canonically-space-region beg end))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
525 (goto-char (point-max))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
526 (setq ncols (- (current-fill-column) indent (current-column)))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
527 (if (< ncols 0)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
528 (message "Line to long to justify")
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
529 (if (and (not eop)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
530 (search-backward " " nil t))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
531 (while (> ncols 0)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
532 (let ((nmove (+ 3 (random 3))))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
533 (while (> nmove 0)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
534 (or (search-backward " " nil t)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
535 (progn
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
536 (goto-char (point-max))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
537 (search-backward " ")))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
538 (skip-chars-backward " ")
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
539 (setq nmove (1- nmove))))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
540 (insert-and-inherit " ")
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
541 (skip-chars-backward " ")
10543
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
542 (setq ncols (1- ncols))))))))))))
2109
73485780a8f9 * simple.el (yank, yank-pop): Always return nil; don't rely on
Jim Blandy <jimb@redhat.com>
parents: 1332
diff changeset
543 nil)
9026
74fd93bc3568 (justify-current-line): Inherit props when inserting spaces.
Richard M. Stallman <rms@gnu.org>
parents: 8363
diff changeset
544
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
545
2520
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
546 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
547 "Fill paragraphs within the region, allowing varying indentation within each.
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
548 This command divides the region into \"paragraphs\",
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
549 only at paragraph-separator lines, then fills each paragraph
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
550 using as the fill prefix the smallest indentation of any line
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
551 in the paragraph.
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
552
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
553 When calling from a program, pass range to fill as first two arguments.
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
554
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
555 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
556 JUSTIFY to justify paragraphs (prefix arg),
2520
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
557 MAIL-FLAG for a mail message, i. e. don't fill header lines."
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
558 (interactive "r\nP")
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
559 (let ((fill-individual-varying-indent t))
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
560 (fill-individual-paragraphs min max justifyp mailp)))
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
561
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
562 (defun fill-individual-paragraphs (min max &optional justify mailp)
2520
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
563 "Fill paragraphs of uniform indentation within the region.
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
564 This command divides the region into \"paragraphs\",
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
565 treating every change in indentation level as a paragraph boundary,
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
566 then fills each paragraph using its indentation level as the fill prefix.
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
567
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
568 When calling from a program, pass range to fill as first two arguments.
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
569
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
570 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
571 JUSTIFY to justify paragraphs (prefix arg),
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
572 MAIL-FLAG for a mail message, i. e. don't fill header lines."
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
573 (interactive "r\nP")
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
574 (save-restriction
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
575 (save-excursion
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
576 (goto-char min)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
577 (beginning-of-line)
9706
f5f7d07eece8 (fill-individual-paragraphs): Avoid infinite loop
Richard M. Stallman <rms@gnu.org>
parents: 9217
diff changeset
578 (narrow-to-region (point) max)
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
579 (if mailp
9706
f5f7d07eece8 (fill-individual-paragraphs): Avoid infinite loop
Richard M. Stallman <rms@gnu.org>
parents: 9217
diff changeset
580 (while (and (not (eobp))
f5f7d07eece8 (fill-individual-paragraphs): Avoid infinite loop
Richard M. Stallman <rms@gnu.org>
parents: 9217
diff changeset
581 (or (looking-at "[ \t]*[^ \t\n]*:")
f5f7d07eece8 (fill-individual-paragraphs): Avoid infinite loop
Richard M. Stallman <rms@gnu.org>
parents: 9217
diff changeset
582 (looking-at "[ \t]*$")))
4992
8a3293abe1ba (fill-individual-paragraphs): Fix skipping headers of yanked message.
Richard M. Stallman <rms@gnu.org>
parents: 4413
diff changeset
583 (if (looking-at "[ \t]*[^ \t\n]*:")
8a3293abe1ba (fill-individual-paragraphs): Fix skipping headers of yanked message.
Richard M. Stallman <rms@gnu.org>
parents: 4413
diff changeset
584 (search-forward "\n\n" nil 'move)
8a3293abe1ba (fill-individual-paragraphs): Fix skipping headers of yanked message.
Richard M. Stallman <rms@gnu.org>
parents: 4413
diff changeset
585 (forward-line 1))))
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
586 (narrow-to-region (point) max)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
587 ;; Loop over paragraphs.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
588 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
589 (beginning-of-line)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
590 (let ((start (point))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
591 fill-prefix fill-prefix-regexp)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
592 ;; Find end of paragraph, and compute the smallest fill-prefix
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
593 ;; that fits all the lines in this paragraph.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
594 (while (progn
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
595 ;; Update the fill-prefix on the first line
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
596 ;; and whenever the prefix good so far is too long.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
597 (if (not (and fill-prefix
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
598 (looking-at fill-prefix-regexp)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
599 (setq fill-prefix
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
600 (buffer-substring (point)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
601 (save-excursion (skip-chars-forward " \t") (point)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
602 fill-prefix-regexp
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
603 (regexp-quote fill-prefix)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
604 (forward-line 1)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
605 ;; Now stop the loop if end of paragraph.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
606 (and (not (eobp))
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
607 (if fill-individual-varying-indent
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
608 ;; If this line is a separator line, with or
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
609 ;; without prefix, end the paragraph.
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
610 (and
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
611 (not (looking-at paragraph-separate))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
612 (save-excursion
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
613 (not (and (looking-at fill-prefix-regexp)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
614 (progn (forward-char (length fill-prefix))
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
615 (looking-at paragraph-separate))))))
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
616 ;; If this line has more or less indent
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
617 ;; than the fill prefix wants, end the paragraph.
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
618 (and (looking-at fill-prefix-regexp)
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
619 (save-excursion
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
620 (not (progn (forward-char (length fill-prefix))
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
621 (or (looking-at paragraph-separate)
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
622 (looking-at paragraph-start))))))))))
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
623 ;; Fill this paragraph, but don't add a newline at the end.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
624 (let ((had-newline (bolp)))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
625 (fill-region-as-paragraph start (point) justify)
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
626 (or had-newline (delete-char -1))))))))
662
8a533acedb77 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 638
diff changeset
627
732
a8d94735277e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 729
diff changeset
628 ;;; fill.el ends here