annotate lisp/textmodes/fill.el @ 12429:d6d785d96455

(current-fill-column): If fill-column is nil, return nil.
author Richard M. Stallman <rms@gnu.org>
date Thu, 29 Jun 1995 03:21:26 +0000
parents b0a7e8ff84ad
children 34e9427fe8f5
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
11234
4d2a2fe1d8d7 Update copyright.
Karl Heuer <kwzh@gnu.org>
parents: 11178
diff changeset
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1995 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
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
49 (save-excursion (move-to-left-margin) (point))
75
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
10989
f4693d66f90c (adaptive-fill-regexp): Skip # or ;.
Richard M. Stallman <rms@gnu.org>
parents: 10988
diff changeset
60 (defconst adaptive-fill-regexp "[ \t]*\\([#;>*]+ +\\)?"
218
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
12429
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
75 (if fill-column
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
76 (let* ((here (progn (beginning-of-line) (point)))
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
77 (here-col 0)
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
78 (eol (progn (end-of-line) (point)))
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
79 margin fill-col change col)
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
80 ;; Look separately at each region of line with a different right-margin.
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
81 (while (and (setq margin (get-text-property here 'right-margin)
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
82 fill-col (- fill-column (or margin 0))
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
83 change (text-property-not-all
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
84 here eol 'right-margin margin))
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
85 (progn (goto-char (1- change))
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
86 (setq col (current-column))
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
87 (< col fill-col)))
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
88 (setq here change
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
89 here-col col))
d6d785d96455 (current-fill-column): If fill-column is nil, return nil.
Richard M. Stallman <rms@gnu.org>
parents: 11448
diff changeset
90 (max here-col fill-col)))))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
91
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
92 (defun canonically-space-region (beg end)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
93 "Remove extra spaces between words in region.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
94 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
95 Remove indenation from each line."
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
96 (interactive "r")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
97 (save-excursion
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
98 (goto-char beg)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
99 ;; Nuke tabs; they get screwed up in a fill.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
100 ;; 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
101 ;; 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
102 ;; Blame the typist.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
103 (subst-char-in-region beg end ?\t ?\ )
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
104 (while (and (< (point) end)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
105 (re-search-forward " *" end t))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
106 (delete-region
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
107 (+ (match-beginning 0)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
108 ;; Determine number of spaces to leave:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
109 (save-excursion
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
110 (skip-chars-backward " ]})\"'")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
111 (cond ((and sentence-end-double-space
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
112 (memq (preceding-char) '(?. ?? ?!))) 2)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
113 ((char-equal (preceding-char) ?\n) 0)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
114 (t 1))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
115 (match-end 0)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
116 ;; 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
117 ;; loses on split abbrevs ("Mr.\nSmith")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
118 (goto-char beg)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
119 (while (and (< (point) end)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
120 (re-search-forward "[.?!][])}\"']*$" end t))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
121 (insert-and-inherit ? ))))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
122
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
123 (defun fill-region-as-paragraph (from to &optional justify nosqueeze)
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
124 "Fill the region as one paragraph.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
125 Removes any paragraph breaks in the region and extra newlines at the end,
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
126 indents and fills lines between the margins given by the
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
127 `current-left-margin' and `current-fill-column' functions.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
128
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
129 Normally performs justification according to the `current-justification'
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
130 function, but with a prefix arg, does full justification instead.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
131
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
132 From a program, optional third arg JUSTIFY can specify any type of
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
133 justification, and fourth arg NOSQUEEZE non-nil means not to make spaces
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
134 between words canonical before filling.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
135
5770
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
136 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
137 space does not end a sentence, so don't break a line there."
11356
4be78e93d1be (fill-region-as-paragraph): Undo previous change.
Richard M. Stallman <rms@gnu.org>
parents: 11282
diff changeset
138 (interactive (list (region-beginning) (region-end)
4be78e93d1be (fill-region-as-paragraph): Undo previous change.
Richard M. Stallman <rms@gnu.org>
parents: 11282
diff changeset
139 (if current-prefix-arg 'full)))
1332
955ebdb98095 (fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents: 1067
diff changeset
140 ;; 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
141 (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
142 (setq buffer-undo-list (cons (point) buffer-undo-list)))
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
143
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
144 ;; Make sure "to" is the endpoint. Make sure that we end up there.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
145 (goto-char (min from to))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
146 (setq to (max from to))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
147 (setq from (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
148
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
149 ;; Delete all but one soft newline at end of region.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
150 (goto-char to)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
151 (let ((oneleft nil))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
152 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
153 (if (and oneleft
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
154 (not (and use-hard-newlines
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
155 (get-text-property (1- (point)) 'hard))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
156 (delete-backward-char 1)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
157 (backward-char 1)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
158 (setq oneleft t)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
159 ;; If there was no newline, create one.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
160 (if (and (not oneleft) (> (point) from))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
161 (save-excursion (newline))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
162 (setq to (point))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
163
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
164 ;; Ignore blank lines at beginning of region.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
165 (goto-char from)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
166 (skip-chars-forward " \t\n")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
167 (beginning-of-line)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
168 (setq from (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
169
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
170 (if (>= from to)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
171 nil ; There is no paragraph at all.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
172
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
173 (or justify (setq justify (current-justification)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
174
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
175 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
176 (let ((fill-prefix fill-prefix))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
177 ;; Figure out how this paragraph is indented, if desired.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
178 (if (and adaptive-fill-mode
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
179 (or (null fill-prefix) (string= fill-prefix "")))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
180 (save-excursion
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
181 (goto-char from)
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
182 (if (eolp) (forward-line 1))
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
183 (forward-line 1)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
184 (move-to-left-margin)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
185 (if (< (point) to)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
186 (let ((start (point)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
187 (re-search-forward adaptive-fill-regexp)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
188 (setq fill-prefix (buffer-substring start (point)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
189 (set-text-properties 0 (length fill-prefix) nil
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
190 fill-prefix)))
7721
b04897534717 (fill-region-as-paragraph): if left-margin is nonzero,
Richard M. Stallman <rms@gnu.org>
parents: 7300
diff changeset
191 ;; 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
192 ;; that additional lines would have the same starting
729
5d684b81ac6b *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 719
diff changeset
193 ;; decoration. Assume no indentation.
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
194 ))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
195
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
196 (save-restriction
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
197 (goto-char from)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
198 (beginning-of-line)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
199 (narrow-to-region (point) to)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
200
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
201 (if (not justify) ; filling disabled: just check indentation
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
202 (progn
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
203 (goto-char from)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
204 (while (not (eobp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
205 (if (and (not (eolp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
206 (< (current-indentation) (current-left-margin)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
207 (indent-to-left-margin))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
208 (forward-line 1)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
209
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
210 (if use-hard-newlines
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
211 (remove-text-properties from (point-max) '(hard nil)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
212 ;; Make sure first line is indented (at least) to left margin...
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
213 (if (or (memq justify '(right center))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
214 (< (current-indentation) (current-left-margin)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
215 (indent-to-left-margin))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
216 ;; Delete the fill prefix from every line except the first.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
217 ;; 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
218 (goto-char from)
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
219 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
220 (concat "[ \t]*"
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
221 (regexp-quote fill-prefix)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
222 "[ \t]*"))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
223 (and fpre
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
224 (progn
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
225 (if (>= (+ (current-left-margin) (length fill-prefix))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
226 (current-fill-column))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
227 (error "fill-prefix too long for specified width"))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
228 (goto-char from)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
229 (forward-line 1)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
230 (while (not (eobp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
231 (if (looking-at fpre)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
232 (delete-region (point) (match-end 0)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
233 (forward-line 1))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
234 (goto-char from)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
235 (and (looking-at fpre) (goto-char (match-end 0)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
236 (setq from (point)))))
11178
7d4805feb413 (fill-region-as-paragraph): Remove fill-prefix first, then remove indentation.
Richard M. Stallman <rms@gnu.org>
parents: 10989
diff changeset
237 ;; Remove indentation from lines other than the first.
7d4805feb413 (fill-region-as-paragraph): Remove fill-prefix first, then remove indentation.
Richard M. Stallman <rms@gnu.org>
parents: 10989
diff changeset
238 (beginning-of-line 2)
7d4805feb413 (fill-region-as-paragraph): Remove fill-prefix first, then remove indentation.
Richard M. Stallman <rms@gnu.org>
parents: 10989
diff changeset
239 (indent-region (point) (point-max) 0)
7d4805feb413 (fill-region-as-paragraph): Remove fill-prefix first, then remove indentation.
Richard M. Stallman <rms@gnu.org>
parents: 10989
diff changeset
240 (goto-char from)
7d4805feb413 (fill-region-as-paragraph): Remove fill-prefix first, then remove indentation.
Richard M. Stallman <rms@gnu.org>
parents: 10989
diff changeset
241
7d4805feb413 (fill-region-as-paragraph): Remove fill-prefix first, then remove indentation.
Richard M. Stallman <rms@gnu.org>
parents: 10989
diff changeset
242 ;; FROM, and point, are now before the text to fill,
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
243 ;; but after any fill prefix on the first line.
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
244
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
245 ;; Make sure sentences ending at end of line get an extra space.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
246 ;; loses on split abbrevs ("Mr.\nSmith")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
247 (while (re-search-forward "[.?!][])}\"']*$" nil t)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
248 (insert-and-inherit ? ))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
249 (goto-char from)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
250 (skip-chars-forward " \t")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
251 ;; Then change all newlines to spaces.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
252 (subst-char-in-region from (point-max) ?\n ?\ )
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
253 (if (and nosqueeze (not (eq justify 'full)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
254 nil
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
255 (canonically-space-region (point) (point-max))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
256 (goto-char (point-max))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
257 (delete-horizontal-space)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
258 (insert-and-inherit " "))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
259 (goto-char (point-min))
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
260
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
261 ;; This is the actual filling loop.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
262 (let ((prefixcol 0) linebeg)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
263 (while (not (eobp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
264 (setq linebeg (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
265 (move-to-column (1+ (current-fill-column)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
266 (if (eobp)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
267 (or nosqueeze (delete-horizontal-space))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
268 ;; Move back to start of word.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
269 (skip-chars-backward "^ \n" linebeg)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
270 ;; Don't break after a period followed by just one space.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
271 ;; Move back to the previous place to break.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
272 ;; The reason is that if a period ends up at the end of a line,
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
273 ;; further fills will assume it ends a sentence.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
274 ;; If we now know it does not end a sentence,
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
275 ;; avoid putting it at the end of the line.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
276 (if sentence-end-double-space
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
277 (while (and (> (point) (+ linebeg 2))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
278 (eq (preceding-char) ?\ )
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
279 (not (eq (following-char) ?\ ))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
280 (eq (char-after (- (point) 2)) ?\.))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
281 (forward-char -2)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
282 (skip-chars-backward "^ \n" linebeg)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
283 (if (if (zerop prefixcol)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
284 (save-excursion
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
285 (skip-chars-backward " " linebeg)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
286 (bolp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
287 (>= prefixcol (current-column)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
288 ;; Keep at least one word even if fill prefix exceeds margin.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
289 ;; This handles all but the first line of the paragraph.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
290 ;; Meanwhile, don't stop at a period followed by one space.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
291 (let ((first t))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
292 (move-to-column prefixcol)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
293 (while (and (not (eobp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
294 (or first
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
295 (and (not (bobp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
296 sentence-end-double-space
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
297 (save-excursion (forward-char -1)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
298 (and (looking-at "\\. ")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
299 (not (looking-at "\\. ")))))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
300 (skip-chars-forward " ")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
301 (skip-chars-forward "^ \n")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
302 (setq first nil)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
303 ;; Normally, move back over the single space between the words.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
304 (forward-char -1))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
305 (if (and fill-prefix (zerop prefixcol)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
306 (< (- (point) (point-min)) (length fill-prefix))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
307 (string= (buffer-substring (point-min) (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
308 (substring fill-prefix 0 (- (point) (point-min)))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
309 ;; Keep at least one word even if fill prefix exceeds margin.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
310 ;; This handles the first line of the paragraph.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
311 ;; Don't stop at a period followed by just one space.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
312 (let ((first t))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
313 (while (and (not (eobp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
314 (or first
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
315 (and (not (bobp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
316 sentence-end-double-space
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
317 (save-excursion (forward-char -1)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
318 (and (looking-at "\\. ")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
319 (not (looking-at "\\. ")))))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
320 (skip-chars-forward " ")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
321 (skip-chars-forward "^ \n")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
322 (setq first nil))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
323 ;; Replace whitespace here with one newline, then indent to left
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
324 ;; margin.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
325 (skip-chars-backward " ")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
326 (insert ?\n)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
327 ;; Give newline the properties of the space(s) it replaces
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
328 (set-text-properties (1- (point)) (point)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
329 (text-properties-at (point)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
330 (indent-to-left-margin)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
331 ;; Insert the fill prefix after indentation.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
332 ;; Set prefixcol so whitespace in the prefix won't get lost.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
333 (and fill-prefix (not (equal fill-prefix ""))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
334 (progn
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
335 (insert-and-inherit fill-prefix)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
336 (setq prefixcol (current-column)))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
337 ;; Justify the line just ended, if desired.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
338 (if justify
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
339 (if (eobp)
11356
4be78e93d1be (fill-region-as-paragraph): Undo previous change.
Richard M. Stallman <rms@gnu.org>
parents: 11282
diff changeset
340 (justify-current-line justify t t)
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
341 (forward-line -1)
11356
4be78e93d1be (fill-region-as-paragraph): Undo previous change.
Richard M. Stallman <rms@gnu.org>
parents: 11282
diff changeset
342 (justify-current-line justify nil t)
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
343 (forward-line 1))))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
344 ;; Leave point after final newline.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
345 (goto-char (point-max)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
346 (forward-char 1))))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
347
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
348 (defun fill-paragraph (arg)
5770
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
349 "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
350 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
351 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
352
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
353 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
354 argument to it), and if it returns non-nil, we simply return its value."
11448
b0a7e8ff84ad (fill-paragraph, fill-region, fill-nonuniform-paragraphs)
Richard M. Stallman <rms@gnu.org>
parents: 11356
diff changeset
355 (interactive (list (if current-prefix-arg 'full)))
10631
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
356 (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
357 (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
358 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
359 (funcall function arg)))
10631
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
360 (let ((before (point)))
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
361 (save-excursion
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
362 (forward-paragraph)
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
363 (or (bolp) (newline 1))
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
364 (let ((end (point))
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
365 (beg (progn (backward-paragraph) (point))))
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
366 (goto-char before)
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
367 (if use-hard-newlines
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
368 ;; 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
369 ;; still contain hard newlines. See fill-region.
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
370 (fill-region beg end arg)
dc245bfecc3b (fill-paragraph-function): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 10543
diff changeset
371 (fill-region-as-paragraph beg end arg)))))))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
372
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
373 (defun fill-region (from to &optional justify nosqueeze to-eop)
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
374 "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
375 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
376
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
377 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
378 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
379 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
380 hard newline, if `use-hard-newlines' is on).
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
381
5770
92dd602768d2 (sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 5291
diff changeset
382 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
383 space does not end a sentence, so don't break a line there."
11448
b0a7e8ff84ad (fill-paragraph, fill-region, fill-nonuniform-paragraphs)
Richard M. Stallman <rms@gnu.org>
parents: 11356
diff changeset
384 (interactive (list (region-beginning) (region-end)
b0a7e8ff84ad (fill-paragraph, fill-region, fill-nonuniform-paragraphs)
Richard M. Stallman <rms@gnu.org>
parents: 11356
diff changeset
385 (if current-prefix-arg 'full)))
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
386 (let (end beg)
10110
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
387 (save-restriction
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
388 (goto-char (max from to))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
389 (if to-eop
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
390 (progn (skip-chars-backward "\n")
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
391 (forward-paragraph)))
10110
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
392 (setq end (point))
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
393 (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
394 (beginning-of-line)
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
395 (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
396 (while (not (eobp))
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
397 (let ((initial (point))
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
398 end)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
399 ;; If using hard newlines, break at every one for filling
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
400 ;; purposes rather than using paragraph breaks.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
401 (if use-hard-newlines
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
402 (progn
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
403 (while (and (setq end (text-property-any (point) (point-max)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
404 'hard t))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
405 (not (= ?\n (char-after end)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
406 (not (= end (point-max))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
407 (goto-char (1+ end)))
11282
3da60d3584c4 (fill-region): Avoid error on reaching end of buffer.
Richard M. Stallman <rms@gnu.org>
parents: 11247
diff changeset
408 (setq end (if end (min (point-max) (1+ end)) (point-max)))
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
409 (goto-char initial))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
410 (forward-paragraph 1)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
411 (setq end (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
412 (forward-paragraph -1))
10110
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
413 (if (< (point) beg)
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
414 (goto-char beg))
18d52489f138 (fill-region-as-paragraph): If region starts in mid-line,
Richard M. Stallman <rms@gnu.org>
parents: 9706
diff changeset
415 (if (>= (point) initial)
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
416 (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
417 (goto-char end)))))))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
418
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
419
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
420 (defconst default-justification 'left
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
421 "*Method of justifying text not otherwise specified.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
422 Possible values are `left', `right', `full', `center', or `none'.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
423 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
424 The `justification' text-property can locally override this variable.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
425 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
426 (make-variable-buffer-local 'default-justification)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
427
10472
82ff279a4cea (current-justification): Renamed from current-justification. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents: 10468
diff changeset
428 (defun current-justification ()
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
429 "How should we justify this line?
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
430 This returns the value of the text-property `justification',
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
431 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
432 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
433 (let ((j (or (get-text-property
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
434 ;; Make sure we're looking at paragraph body.
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
435 (save-excursion (skip-chars-forward " \t")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
436 (if (and (eobp) (not (bobp)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
437 (1- (point)) (point)))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
438 'justification)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
439 default-justification)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
440 (if (eq 'none j)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
441 nil
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
442 j)))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
443
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
444 (defun set-justification (begin end value &optional whole-par)
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
445 "Set the region's justification style.
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
446 The kind of justification to use is prompted for.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
447 If the mark is not active, this command operates on the current paragraph.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
448 If the mark is active, the region is used. However, if the beginning and end
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
449 of the region are not at paragraph breaks, they are moved to the beginning and
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
450 end of the paragraphs they are in.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
451 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
452 breaks.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
453
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
454 When calling from a program, operates just on region between BEGIN and END,
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
455 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
456 extended to include entire paragraphs as in the interactive command."
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
457 (interactive (list (if mark-active (region-beginning) (point))
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
458 (if mark-active (region-end) (point))
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
459 (let ((s (completing-read
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
460 "Set justification to: "
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
461 '(("left") ("right") ("full")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
462 ("center") ("none"))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
463 nil t)))
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
464 (if (equal s "") (error ""))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
465 (intern s))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
466 t))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
467 (save-excursion
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
468 (save-restriction
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
469 (if whole-par
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
470 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
471 (paragraph-ignore-fill-prefix (if use-hard-newlines t
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
472 paragraph-ignore-fill-prefix)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
473 (goto-char begin)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
474 (while (and (bolp) (not (eobp))) (forward-char 1))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
475 (backward-paragraph)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
476 (setq begin (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
477 (goto-char end)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
478 (skip-chars-backward " \t\n" begin)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
479 (forward-paragraph)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
480 (setq end (point))))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
481
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
482 (narrow-to-region (point-min) end)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
483 (unjustify-region begin (point-max))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
484 (put-text-property begin (point-max) 'justification value)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
485 (fill-region begin (point-max) nil t))))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
486
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
487 (defun set-justification-none (b e)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
488 "Disable automatic filling for paragraphs in the region.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
489 If the mark is not active, this applies to the current paragraph."
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
490 (interactive (list (if mark-active (region-beginning) (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
491 (if mark-active (region-end) (point))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
492 (set-justification b e 'none t))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
493
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
494 (defun set-justification-left (b e)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
495 "Make paragraphs in the region left-justified.
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
496 This is usually the default, but see the variable `default-justification'.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
497 If the mark is not active, this applies to the current paragraph."
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
498 (interactive (list (if mark-active (region-beginning) (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
499 (if mark-active (region-end) (point))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
500 (set-justification b e 'left t))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
501
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
502 (defun set-justification-right (b e)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
503 "Make paragraphs in the region right-justified:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
504 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
505 If the mark is not active, this applies to the current paragraph."
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
506 (interactive (list (if mark-active (region-beginning) (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
507 (if mark-active (region-end) (point))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
508 (set-justification b e 'right t))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
509
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
510 (defun set-justification-full (b e)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
511 "Make paragraphs in the region fully justified:
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
512 This makes lines flush on both margins by inserting spaces between words.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
513 If the mark is not active, this applies to the current paragraph."
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
514 (interactive (list (if mark-active (region-beginning) (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
515 (if mark-active (region-end) (point))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
516 (set-justification b e 'full t))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
517
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
518 (defun set-justification-center (b e)
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
519 "Make paragraphs in the region centered.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
520 If the mark is not active, this applies to the current paragraph."
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
521 (interactive (list (if mark-active (region-beginning) (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
522 (if mark-active (region-end) (point))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
523 (set-justification b e 'center t))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
524
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
525 ;; A line has up to six parts:
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
526 ;;
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
527 ;; >>> hello.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
528 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
529 ;;
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
530 ;; "Indent-1" is the left-margin indentation; normally it ends at column
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
531 ;; given by the `current-left-margin' function.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
532 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
533 ;; "Indent-2" is added to justify a line if the `current-justification' is
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
534 ;; `center' or `right'. In `left' and `full' justification regions, any
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
535 ;; whitespace there is part of the line's text, and should not be changed.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
536 ;; Trailing whitespace is not counted as part of the line length when
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
537 ;; center- or right-justifying.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
538 ;;
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
539 ;; All parts of the line are optional, although the final newline can
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
540 ;; only be missing on the last line of the buffer.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
541
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
542 (defun justify-current-line (&optional how eop nosqueeze)
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
543 "Do some kind of justification on this line.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
544 Normally does full justification: adds spaces to the line to make it end at
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
545 the column given by `current-fill-column'.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
546 Optional first argument HOW specifies alternate type of justification:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
547 it can be `left', `right', `full', `center', or `none'.
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
548 If HOW is t, will justify however the `current-justification' function says to.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
549 If HOW is nil or missing, full justification is done by default.
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
550 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
551 it will not be stretched by full justification.
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
552 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
553 otherwise it is made canonical."
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
554 (interactive)
10472
82ff279a4cea (current-justification): Renamed from current-justification. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents: 10468
diff changeset
555 (if (eq t how) (setq how (or (current-justification) 'none)))
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
556 (if (null how) (setq how 'full))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
557 (or (memq how '(none left)) ; No action required for these.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
558 (let ((fc (current-fill-column))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
559 (pos (point-marker))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
560 fp-end ; point at end of fill prefix
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
561 beg ; point at beginning of line's text
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
562 end ; point at end of line's text
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
563 indent ; column of `beg'
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
564 endcol ; column of `end'
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
565 ncols) ; new indent point or offset
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
566 (end-of-line)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
567 ;; Check if this is the last line of the paragraph.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
568 (if (and use-hard-newlines (null eop)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
569 (get-text-property (point) 'hard))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
570 (setq eop t))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
571 (skip-chars-backward " \t")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
572 ;; Quick exit if it appears to be properly justified already
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
573 ;; or there is no text.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
574 (if (or (bolp)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
575 (and (memq how '(full right))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
576 (= (current-column) fc)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
577 nil
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
578 (setq end (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
579 (beginning-of-line)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
580 (skip-chars-forward " \t")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
581 ;; Skip over fill-prefix.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
582 (if (and fill-prefix
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
583 (not (string-equal fill-prefix ""))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
584 (equal fill-prefix
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
585 (buffer-substring
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
586 (point) (min (point-max) (+ (length fill-prefix)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
587 (point))))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
588 (forward-char (length fill-prefix))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
589 (if (and adaptive-fill-mode
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
590 (looking-at adaptive-fill-regexp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
591 (goto-char (match-end 0))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
592 (setq fp-end (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
593 (skip-chars-forward " \t")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
594 ;; This is beginning of the line's text.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
595 (setq indent (current-column))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
596 (setq beg (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
597 (goto-char end)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
598 (setq endcol (current-column))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
599
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
600 ;; HOW can't be null or left--we would have exited already
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
601 (cond ((eq 'right how)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
602 (setq ncols (- fc endcol))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
603 (if (< ncols 0)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
604 ;; Need to remove some indentation
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
605 (delete-region
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
606 (progn (goto-char fp-end)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
607 (if (< (current-column) (+ indent ncols))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
608 (move-to-column (+ indent ncols) t))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
609 (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
610 (progn (move-to-column indent) (point)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
611 ;; Need to add some
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
612 (goto-char beg)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
613 (indent-to (+ indent ncols))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
614 ;; If point was at beginning of text, keep it there.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
615 (if (= beg pos)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
616 (move-marker pos (point)))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
617
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
618 ((eq 'center how)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
619 ;; Figure out how much indentation is needed
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
620 (setq ncols (+ (current-left-margin)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
621 (/ (- fc (current-left-margin) ;avail. space
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
622 (- endcol indent)) ;text width
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
623 2)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
624 (if (< ncols indent)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
625 ;; Have too much indentation - remove some
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
626 (delete-region
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
627 (progn (goto-char fp-end)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
628 (if (< (current-column) ncols)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
629 (move-to-column ncols t))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
630 (point))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
631 (progn (move-to-column indent) (point)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
632 ;; Have too little - add some
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
633 (goto-char beg)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
634 (indent-to ncols)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
635 ;; If point was at beginning of text, keep it there.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
636 (if (= beg pos)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
637 (move-marker pos (point)))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
638
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
639 ((eq 'full how)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
640 ;; Insert extra spaces between words to justify line
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
641 (save-restriction
10543
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
642 (narrow-to-region beg end)
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
643 (or nosqueeze
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
644 (canonically-space-region beg end))
3b896847b849 (justify-current-line): Avoid error comparing fill-prefix
Richard M. Stallman <rms@gnu.org>
parents: 10497
diff changeset
645 (goto-char (point-max))
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
646 (setq ncols (- fc endcol))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
647 ;; Ncols is number of additional spaces needed
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
648 (if (> ncols 0)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
649 (if (and (not eop)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
650 (search-backward " " nil t))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
651 (while (> ncols 0)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
652 (let ((nmove (+ 3 (random 3))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
653 (while (> nmove 0)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
654 (or (search-backward " " nil t)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
655 (progn
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
656 (goto-char (point-max))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
657 (search-backward " ")))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
658 (skip-chars-backward " ")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
659 (setq nmove (1- nmove))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
660 (insert-and-inherit " ")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
661 (skip-chars-backward " ")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
662 (setq ncols (1- ncols)))))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
663 (t (error "Unknown justification value"))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
664 (goto-char pos)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
665 (move-marker pos nil)))
2109
73485780a8f9 * simple.el (yank, yank-pop): Always return nil; don't rely on
Jim Blandy <jimb@redhat.com>
parents: 1332
diff changeset
666 nil)
9026
74fd93bc3568 (justify-current-line): Inherit props when inserting spaces.
Richard M. Stallman <rms@gnu.org>
parents: 8363
diff changeset
667
10811
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
668 (defun unjustify-current-line ()
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
669 "Remove justification whitespace from current line.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
670 If the line is centered or right-justified, this function removes any
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
671 indentation past the left margin. If the line is full-jusitified, it removes
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
672 extra spaces between words. It does nothing in other justification modes."
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
673 (let ((justify (current-justification)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
674 (cond ((eq 'left justify) nil)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
675 ((eq nil justify) nil)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
676 ((eq 'full justify) ; full justify: remove extra spaces
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
677 (beginning-of-line-text)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
678 (canonically-space-region
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
679 (point) (save-excursion (end-of-line) (point))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
680 ((memq justify '(center right))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
681 (save-excursion
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
682 (move-to-left-margin nil t)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
683 ;; Position ourselves after any fill-prefix.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
684 (if (and fill-prefix
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
685 (not (string-equal fill-prefix ""))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
686 (equal fill-prefix
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
687 (buffer-substring
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
688 (point) (min (point-max) (+ (length fill-prefix)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
689 (point))))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
690 (forward-char (length fill-prefix)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
691 (delete-region (point) (progn (skip-chars-forward " \t")
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
692 (point))))))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
693
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
694 (defun unjustify-region (&optional begin end)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
695 "Remove justification whitespace from region.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
696 For centered or right-justified regions, this function removes any indentation
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
697 past the left margin from each line. For full-jusitified lines, it removes
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
698 extra spaces between words. It does nothing in other justification modes.
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
699 Arguments BEGIN and END are optional; default is the whole buffer."
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
700 (save-excursion
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
701 (save-restriction
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
702 (if end (narrow-to-region (point-min) end))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
703 (goto-char (or begin (point-min)))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
704 (while (not (eobp))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
705 (unjustify-current-line)
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
706 (forward-line 1)))))
7f9e55cdc349 (set-fill-prefix): start from left-margin.
Boris Goldowsky <boris@gnu.org>
parents: 10636
diff changeset
707
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
708
2520
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
709 (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
710 "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
711 This command divides the region into \"paragraphs\",
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
712 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
713 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
714 in the paragraph.
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
715
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
716 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
717
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
718 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
719 JUSTIFY to justify paragraphs (prefix arg),
2520
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
720 MAIL-FLAG for a mail message, i. e. don't fill header lines."
11448
b0a7e8ff84ad (fill-paragraph, fill-region, fill-nonuniform-paragraphs)
Richard M. Stallman <rms@gnu.org>
parents: 11356
diff changeset
721 (interactive (list (region-beginning) (region-end)
b0a7e8ff84ad (fill-paragraph, fill-region, fill-nonuniform-paragraphs)
Richard M. Stallman <rms@gnu.org>
parents: 11356
diff changeset
722 (if current-prefix-arg 'full)))
2520
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
723 (let ((fill-individual-varying-indent t))
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
724 (fill-individual-paragraphs min max justifyp mailp)))
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
725
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
726 (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
727 "Fill paragraphs of uniform indentation within the region.
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
728 This command divides the region into \"paragraphs\",
ef643dbb7d40 (fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents: 2412
diff changeset
729 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
730 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
731
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
732 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
733
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
734 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
735 JUSTIFY to justify paragraphs (prefix arg),
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
736 MAIL-FLAG for a mail message, i. e. don't fill header lines."
11448
b0a7e8ff84ad (fill-paragraph, fill-region, fill-nonuniform-paragraphs)
Richard M. Stallman <rms@gnu.org>
parents: 11356
diff changeset
737 (interactive (list (region-beginning) (region-end)
b0a7e8ff84ad (fill-paragraph, fill-region, fill-nonuniform-paragraphs)
Richard M. Stallman <rms@gnu.org>
parents: 11356
diff changeset
738 (if current-prefix-arg 'full)))
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
739 (save-restriction
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
740 (save-excursion
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
741 (goto-char min)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
742 (beginning-of-line)
9706
f5f7d07eece8 (fill-individual-paragraphs): Avoid infinite loop
Richard M. Stallman <rms@gnu.org>
parents: 9217
diff changeset
743 (narrow-to-region (point) max)
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
744 (if mailp
9706
f5f7d07eece8 (fill-individual-paragraphs): Avoid infinite loop
Richard M. Stallman <rms@gnu.org>
parents: 9217
diff changeset
745 (while (and (not (eobp))
f5f7d07eece8 (fill-individual-paragraphs): Avoid infinite loop
Richard M. Stallman <rms@gnu.org>
parents: 9217
diff changeset
746 (or (looking-at "[ \t]*[^ \t\n]*:")
f5f7d07eece8 (fill-individual-paragraphs): Avoid infinite loop
Richard M. Stallman <rms@gnu.org>
parents: 9217
diff changeset
747 (looking-at "[ \t]*$")))
4992
8a3293abe1ba (fill-individual-paragraphs): Fix skipping headers of yanked message.
Richard M. Stallman <rms@gnu.org>
parents: 4413
diff changeset
748 (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
749 (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
750 (forward-line 1))))
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
751 (narrow-to-region (point) max)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
752 ;; Loop over paragraphs.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
753 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
754 (beginning-of-line)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
755 (let ((start (point))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
756 fill-prefix fill-prefix-regexp)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
757 ;; Find end of paragraph, and compute the smallest fill-prefix
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
758 ;; that fits all the lines in this paragraph.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
759 (while (progn
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
760 ;; Update the fill-prefix on the first line
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
761 ;; and whenever the prefix good so far is too long.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
762 (if (not (and fill-prefix
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
763 (looking-at fill-prefix-regexp)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
764 (setq fill-prefix
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
765 (buffer-substring (point)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
766 (save-excursion (skip-chars-forward " \t") (point)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
767 fill-prefix-regexp
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
768 (regexp-quote fill-prefix)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
769 (forward-line 1)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
770 ;; Now stop the loop if end of paragraph.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
771 (and (not (eobp))
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
772 (if fill-individual-varying-indent
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
773 ;; If this line is a separator line, with or
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
774 ;; without prefix, end the paragraph.
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
775 (and
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
776 (not (looking-at paragraph-separate))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
777 (save-excursion
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
778 (not (and (looking-at fill-prefix-regexp)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
779 (progn (forward-char (length fill-prefix))
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
780 (looking-at paragraph-separate))))))
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
781 ;; If this line has more or less indent
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
782 ;; than the fill prefix wants, end the paragraph.
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
783 (and (looking-at fill-prefix-regexp)
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
784 (save-excursion
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
785 (not (progn (forward-char (length fill-prefix))
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
786 (or (looking-at paragraph-separate)
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
787 (looking-at paragraph-start))))))))))
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 218
diff changeset
788 ;; 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
789 (let ((had-newline (bolp)))
10468
d0854b6f3216 (set-justification): New function.
Richard M. Stallman <rms@gnu.org>
parents: 10423
diff changeset
790 (fill-region-as-paragraph start (point) justify)
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 474
diff changeset
791 (or had-newline (delete-char -1))))))))
662
8a533acedb77 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 638
diff changeset
792
732
a8d94735277e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 729
diff changeset
793 ;;; fill.el ends here