annotate lisp/fill.el @ 412:697ef44129c6 r21-2-14

Import from CVS: tag r21-2-14
author cvs
date Mon, 13 Aug 2007 11:20:41 +0200
parents 74fd4e045ea6
children 95016f13131a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1 ;;; fill.el --- fill commands for XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
2
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 1997 Free Software Foundation, Inc.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
4
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
5 ;; Maintainer: XEmacs Development Team
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
6 ;; Keywords: wp, dumped
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
7
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
8 ;; This file is part of XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
9
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
10 ;; XEmacs is free software; you can redistribute it and/or modify it
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
11 ;; under the terms of the GNU General Public License as published by
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
13 ;; any later version.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
14
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
15 ;; XEmacs is distributed in the hope that it will be useful, but
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
18 ;; General Public License for more details.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
19
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
23 ;; 02111-1307, USA.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
24
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
25 ;;; Synched up with: FSF 19.34.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
26
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
27 ;;; Commentary:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
28
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
29 ;; This file is dumped with XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
30
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
31 ;; All the commands for filling text. These are documented in the XEmacs
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
32 ;; Reference Manual.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
33
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
34 ;; 97/3/14 Jareth Hein (jhod@po.iijnet.or.jp) added functions for kinsoku (asian text
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
35 ;; line break processing)
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
36 ;; 97/06/11 Steve Baur (steve@altair.xemacs.org) converted broken
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
37 ;; following-char/preceding-char calls to char-after/char-before.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
38
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
39 ;;; Code:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
40
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
41 (defgroup fill nil
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
42 "Indenting and filling text."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
43 :group 'editing)
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
44
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
45 (defcustom fill-individual-varying-indent nil
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
46 "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
47 Non-nil means changing indent doesn't end a paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
48 That mode can handle paragraphs with extra indentation on the first line,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
49 but it requires separator lines between paragraphs.
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
50 A value of nil means that any change in indentation starts a new paragraph."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
51 :type 'boolean
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
52 :group 'fill)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
53
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
54 (defcustom sentence-end-double-space t
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
55 "*Non-nil means a single space does not end a sentence.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
56 This variable applies only to filling, not motion commands. To
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
57 change the behavior of motion commands, see `sentence-end'."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
58 :type 'boolean
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
59 :group 'fill)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
60
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
61 (defcustom colon-double-space nil
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
62 "*Non-nil means put two spaces after a colon when filling."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
63 :type 'boolean
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
64 :group 'fill)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
65
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
66 (defvar fill-paragraph-function nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
67 "Mode-specific function to fill a paragraph, or nil if there is none.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
68 If the function returns nil, then `fill-paragraph' does its normal work.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
69
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
70 (defun set-fill-prefix ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
71 "Set the fill prefix to the current line up to point.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
72 Filling expects lines to start with the fill prefix and
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
73 reinserts the fill prefix in each resulting line."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
74 (interactive)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
75 (setq fill-prefix (buffer-substring
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
76 (save-excursion (move-to-left-margin) (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
77 (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
78 (if (equal fill-prefix "")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
79 (setq fill-prefix nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
80 (if fill-prefix
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
81 (message "fill-prefix: \"%s\"" fill-prefix)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
82 (message "fill-prefix cancelled")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
83
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
84 (defcustom adaptive-fill-mode t
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
85 "*Non-nil means determine a paragraph's fill prefix from its text."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
86 :type 'boolean
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
87 :group 'fill)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
88
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
89 ;; #### - this is still weak. Yeah, there's filladapt, but this should
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
90 ;; still be better... --Stig
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
91 (defcustom adaptive-fill-regexp (purecopy "[ \t]*\\([#;>*]+ +\\)?")
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
92 "*Regexp to match text at start of line that constitutes indentation.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
93 If Adaptive Fill mode is enabled, whatever text matches this pattern
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
94 on the second line of a paragraph is used as the standard indentation
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
95 for the paragraph. If the paragraph has just one line, the indentation
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
96 is taken from that line."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
97 :type 'regexp
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
98 :group 'fill)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
99
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
100 (defcustom adaptive-fill-function nil
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
101 "*Function to call to choose a fill prefix for a paragraph.
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
102 This function is used when `adaptive-fill-regexp' does not match."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
103 :type 'function
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
104 :group 'fill)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
105
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
106 ;; Added for kinsoku processing. Use this instead of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
107 ;; (skip-chars-backward "^ \t\n")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
108 ;; (skip-chars-backward "^ \n" linebeg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
109 (defun fill-move-backward-to-break-point (regexp &optional lim)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
110 (let ((opoint (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
111 ;; 93.8.23 by kawamoto@ics.es.osaka-u.ac.jp
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
112 ;; case of first 'word' being longer than fill-column
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
113 (if (not (re-search-backward regexp lim 'move))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
114 nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
115 ;; we have skipped backward SPC or WAN (word-across-newline). So move point forward again.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
116 (forward-char)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
117 (if (< opoint (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
118 (forward-char -1)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
119
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
120 ;; Added for kinsoku processing. Use instead of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
121 ;; (re-search-forward "[ \t]" opoint t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
122 ;; (skip-chars-forward "^ \n")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
123 ;; (skip-chars-forward "^ \n")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
124 (defun fill-move-forward-to-break-point (regexp &optional lim)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
125 (let ((opoint (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
126 (if (not (re-search-forward regexp lim 'move))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
127 nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
128 (forward-char -1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
129 (if (< (point) opoint)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
130 (forward-char))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
131 (if (featurep 'mule) (kinsoku-process-extend)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
132
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
133 (defun fill-end-of-sentence-p ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
134 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
135 (skip-chars-backward " ]})\"'")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
136 (memq (char-before (point)) '(?. ?? ?!))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
137
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
138 (defun current-fill-column ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
139 "Return the fill-column to use for this line.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
140 The fill-column to use for a buffer is stored in the variable `fill-column',
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
141 but can be locally modified by the `right-margin' text property, which is
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
142 subtracted from `fill-column'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
143
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
144 The fill column to use for a line is the first column at which the column
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
145 number equals or exceeds the local fill-column - right-margin difference."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
146 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
147 (if fill-column
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
148 (let* ((here (progn (beginning-of-line) (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
149 (here-col 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
150 (eol (progn (end-of-line) (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
151 margin fill-col change col)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
152 ;; Look separately at each region of line with a different right-margin.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
153 (while (and (setq margin (get-text-property here 'right-margin)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
154 fill-col (- fill-column (or margin 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
155 change (text-property-not-all
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
156 here eol 'right-margin margin))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
157 (progn (goto-char (1- change))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
158 (setq col (current-column))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
159 (< col fill-col)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
160 (setq here change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
161 here-col col))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
162 (max here-col fill-col)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
163
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
164 (defun canonically-space-region (beg end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
165 "Remove extra spaces between words in region.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
166 Leave one space between words, two at end of sentences or after colons
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
167 \(depending on values of `sentence-end-double-space' and `colon-double-space').
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
168 Remove indentation from each line."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
169 (interactive "r")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
170 ;;;### 97/3/14 jhod: Do I have to add anything here for kinsoku?
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
171 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
172 (goto-char beg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
173 ;; XEmacs - (ENE/stig from fa-extras.el): Skip the start of a comment.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
174 (and comment-start-skip
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
175 (looking-at comment-start-skip)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
176 (goto-char (match-end 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
177 ;; Nuke tabs; they get screwed up in a fill.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
178 ;; This is quick, but loses when a tab follows the end of a sentence.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
179 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
180 ;; Blame the typist.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
181 (subst-char-in-region beg end ?\t ?\ )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
182 (while (and (< (point) end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
183 (re-search-forward " *" end t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
184 (delete-region
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
185 (+ (match-beginning 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
186 ;; Determine number of spaces to leave:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
187 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
188 (skip-chars-backward " ]})\"'")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
189 (cond ((and sentence-end-double-space
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
190 (memq (char-before (point)) '(?. ?? ?!))) 2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
191 ((and colon-double-space
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
192 (eq (char-before (point)) ?:)) 2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
193 ((char-equal (char-before (point)) ?\n) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
194 (t 1))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
195 (match-end 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
196 ;; Make sure sentences ending at end of line get an extra space.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
197 ;; loses on split abbrevs ("Mr.\nSmith")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
198 (goto-char beg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
199 (while (and (< (point) end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
200 (re-search-forward "[.?!][])}\"']*$" end t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
201 ;; We insert before markers in case a caller such as
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
202 ;; do-auto-fill has done a save-excursion with point at the end
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
203 ;; of the line and wants it to stay at the end of the line.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
204 (insert ? ))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
205 ;; XEmacs: we don't have this function.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
206 ;; (insert-before-markers-and-inherit ? ))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
207
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
208 ;; XEmacs -- added DONT-SKIP-FIRST. Port of older code changes by Stig.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
209 ;; #### probably this junk is broken -- do-auto-fill doesn't actually use
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
210 ;; it. If so, it should be removed.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
211
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
212 (defun fill-context-prefix (from to &optional first-line-regexp
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
213 dont-skip-first)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
214 "Compute a fill prefix from the text between FROM and TO.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
215 This uses the variables `adaptive-fill-prefix' and `adaptive-fill-function'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
216 If FIRST-LINE-REGEXP is non-nil, then when taking a prefix from the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
217 first line, insist it must match FIRST-LINE-REGEXP."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
218 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
219 (goto-char from)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
220 (if (eolp) (forward-line 1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
221 ;; Move to the second line unless there is just one.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
222 (let ((firstline (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
223 ;; Non-nil if we are on the second line.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
224 at-second
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
225 result)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
226 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
227 (if (not dont-skip-first)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
228 (forward-line 1))
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
229 (if (>= (point) to)
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
230 (goto-char firstline)
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
231 (setq at-second t))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
232 (move-to-left-margin)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
233 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
234 (let ((start (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
235 ; jhod: no longer used?
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
236 ;(eol (save-excursion (end-of-line) (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
237 )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
238 (setq result
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
239 (if (not (looking-at paragraph-start))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
240 (cond ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
241 (buffer-substring-no-properties start (match-end 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
242 (adaptive-fill-function (funcall adaptive-fill-function)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
243 (and result
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
244 (or at-second
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
245 (null first-line-regexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
246 (string-match first-line-regexp result))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
247 result)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
248
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
249 ;; XEmacs (stig) - this is pulled out of fill-region-as-paragraph so that it
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
250 ;; can also be called from do-auto-fill
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
251 ;; #### But it's not used there. Chuck pulled it out because it broke things.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
252 (defun maybe-adapt-fill-prefix (&optional from to dont-skip-first)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
253 (if (and adaptive-fill-mode
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
254 (or (null fill-prefix) (string= fill-prefix "")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
255 (setq fill-prefix (fill-context-prefix from to nil dont-skip-first))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
256
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
257 (defun fill-region-as-paragraph (from to &optional justify
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
258 nosqueeze squeeze-after)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
259 "Fill the region as one paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
260 It removes any paragraph breaks in the region and extra newlines at the end,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
261 indents and fills lines between the margins given by the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
262 `current-left-margin' and `current-fill-column' functions.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
263 It leaves point at the beginning of the line following the paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
264
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
265 Normally performs justification according to the `current-justification'
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
266 function, but with a prefix arg, does full justification instead.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
267
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
268 From a program, optional third arg JUSTIFY can specify any type of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
269 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
270 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
271 means don't canonicalize spaces before that position.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
272
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
273 If `sentence-end-double-space' is non-nil, then period followed by one
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
274 space does not end a sentence, so don't break a line there."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
275 (interactive
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
276 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
277 ;; XEmacs addition:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
278 (barf-if-buffer-read-only nil (region-beginning) (region-end))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
279 (list (region-beginning) (region-end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
280 (if current-prefix-arg 'full))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
281 ;; Arrange for undoing the fill to restore point.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
282 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
283 (setq buffer-undo-list (cons (point) buffer-undo-list)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
284
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
285 ;; Make sure "to" is the endpoint.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
286 (goto-char (min from to))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
287 (setq to (max from to))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
288 ;; Ignore blank lines at beginning of region.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
289 (skip-chars-forward " \t\n")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
290
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
291 (let ((from-plus-indent (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
292 (oneleft nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
293
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
294 (beginning-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
295 (setq from (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
296
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
297 ;; Delete all but one soft newline at end of region.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
298 ;; And leave TO before that one.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
299 (goto-char to)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
300 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
301 (if (and oneleft
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
302 (not (and use-hard-newlines
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
303 (get-text-property (1- (point)) 'hard))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
304 (delete-backward-char 1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
305 (backward-char 1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
306 (setq oneleft t)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
307 (setq to (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
308
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
309 ;; If there was no newline, and there is text in the paragraph, then
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
310 ;; create a newline.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
311 (if (and (not oneleft) (> to from-plus-indent))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
312 (newline))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
313 (goto-char from-plus-indent))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
314
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
315 (if (not (> to (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
316 nil ; There is no paragraph, only whitespace: exit now.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
317
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
318 (or justify (setq justify (current-justification)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
319
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
320 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
321 (let ((fill-prefix fill-prefix))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
322 ;; Figure out how this paragraph is indented, if desired.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
323 ;; XEmacs: move some code here to a separate function.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
324 (maybe-adapt-fill-prefix from to t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
325
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
326 (save-restriction
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
327 (goto-char from)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
328 (beginning-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
329 (narrow-to-region (point) to)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
330
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
331 (if (not justify) ; filling disabled: just check indentation
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
332 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
333 (goto-char from)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
334 (while (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
335 (if (and (not (eolp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
336 (< (current-indentation) (current-left-margin)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
337 (indent-to-left-margin))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
338 (forward-line 1)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
339
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
340 (if use-hard-newlines
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
341 (remove-text-properties from (point-max) '(hard nil)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
342 ;; Make sure first line is indented (at least) to left margin...
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
343 (if (or (memq justify '(right center))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
344 (< (current-indentation) (current-left-margin)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
345 (indent-to-left-margin))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
346 ;; Delete the fill prefix from every line except the first.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
347 ;; The first line may not even have a fill prefix.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
348 (goto-char from)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
349 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
350 (concat "[ \t]*"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
351 (regexp-quote fill-prefix)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
352 "[ \t]*"))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
353 (and fpre
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
354 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
355 (if (>= (+ (current-left-margin) (length fill-prefix))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
356 (current-fill-column))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
357 (error "fill-prefix too long for specified width"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
358 (goto-char from)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
359 (forward-line 1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
360 (while (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
361 (if (looking-at fpre)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
362 (delete-region (point) (match-end 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
363 (forward-line 1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
364 (goto-char from)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
365 (if (looking-at fpre)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
366 (goto-char (match-end 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
367 (setq from (point)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
368 ;; Remove indentation from lines other than the first.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
369 (beginning-of-line 2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
370 (indent-region (point) (point-max) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
371 (goto-char from)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
372
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
373 ;; FROM, and point, are now before the text to fill,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
374 ;; but after any fill prefix on the first line.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
375
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
376 ;; Make sure sentences ending at end of line get an extra space.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
377 ;; loses on split abbrevs ("Mr.\nSmith")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
378 (while (re-search-forward "[.?!][])}\"']*$" nil t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
379 ;; XEmacs change (no insert-and-inherit)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
380 (or (eobp) (insert ?\ ?\ )))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
381 (goto-char from)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
382 (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
383 ;; Then change all newlines to spaces.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
384 ;;; 97/3/14 jhod: Kinsoku change
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
385 ;; Spacing is not necessary for charcters of no word-separater.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
386 ;; The regexp word-across-newline is used for this check.
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 371
diff changeset
387 (defvar word-across-newline)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
388 (if (not (and (featurep 'mule)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
389 (stringp word-across-newline)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
390 (subst-char-in-region from (point-max) ?\n ?\ )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
391 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
392 ;; WAN +NL+WAN --> WAN + WAN
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
393 ;; not(WAN)+NL+WAN --> not(WAN) + WAN
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
394 ;; WAN +NL+not(WAN) --> WAN + not(WAN)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
395 ;; SPC +NL+not(WAN) --> SPC + not(WAN)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
396 ;; not(WAN)+NL+not(WAN) --> not(WAN) + SPC + not(WAN)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
397 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
398 (goto-char from)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
399 (end-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
400 (while (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
401 ;; Insert SPC only when point is between nonWAN. Insert
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
402 ;; before deleting to preserve marker if possible.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
403 (if (or (prog2 ; check following char.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
404 (forward-char) ; skip newline
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
405 (or (eobp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
406 (looking-at word-across-newline))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
407 (forward-char -1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
408 (prog2 ; check previous char.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
409 (forward-char -1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
410 (or (eq (char-after (point)) ?\ )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
411 (looking-at word-across-newline))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
412 (forward-char)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
413 nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
414 (insert ?\ ))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
415 (delete-char 1) ; delete newline
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
416 (end-of-line)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
417 ;; end patch
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
418 (goto-char from)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
419 (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
420 (if (and nosqueeze (not (eq justify 'full)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
421 nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
422 (canonically-space-region (or squeeze-after (point)) (point-max))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
423 (goto-char (point-max))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
424 (delete-horizontal-space)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
425 ;; XEmacs change (no insert-and-inherit)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
426 (insert " "))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
427 (goto-char (point-min))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
428
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
429 ;; This is the actual filling loop.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
430 (let ((prefixcol 0) linebeg
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
431 (re-break-point (if (featurep 'mule)
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
432 (concat "[ \n\t]\\|" word-across-newline)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
433 "[ \n\t]")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
434 (while (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
435 (setq linebeg (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
436 (move-to-column (1+ (current-fill-column)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
437 (if (eobp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
438 (or nosqueeze (delete-horizontal-space))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
439 ;; Move back to start of word.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
440 ;; 97/3/14 jhod: Kinsoku
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
441 ;(skip-chars-backward "^ \n" linebeg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
442 (fill-move-backward-to-break-point re-break-point linebeg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
443 ;; end patch
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
444 ;; Don't break after a period followed by just one space.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
445 ;; Move back to the previous place to break.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
446 ;; The reason is that if a period ends up at the end of a line,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
447 ;; further fills will assume it ends a sentence.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
448 ;; If we now know it does not end a sentence,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
449 ;; avoid putting it at the end of the line.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
450 (if sentence-end-double-space
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
451 (while (and (> (point) (+ linebeg 2))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
452 (eq (char-before (point)) ?\ )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
453 (not (eq (char-after (point)) ?\ ))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
454 (eq (char-after (- (point) 2)) ?\.))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
455 (forward-char -2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
456 ;; 97/3/14 jhod: Kinsoku
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
457 ;(skip-chars-backward "^ \n" linebeg)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
458 (fill-move-backward-to-break-point re-break-point linebeg)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
459 (if (featurep 'mule) (kinsoku-process))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
460 ;end patch
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
461
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
462 ;; If the left margin and fill prefix by themselves
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
463 ;; pass the fill-column. or if they are zero
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
464 ;; but we have no room for even one word,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
465 ;; keep at least one word anyway.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
466 ;; This handles ALL BUT the first line of the paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
467 (if (if (zerop prefixcol)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
468 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
469 (skip-chars-backward " \t" linebeg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
470 (bolp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
471 (>= prefixcol (current-column)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
472 ;; Ok, skip at least one word.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
473 ;; Meanwhile, don't stop at a period followed by one space.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
474 (let ((first t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
475 (move-to-column prefixcol)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
476 (while (and (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
477 (or first
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
478 (and (not (bobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
479 sentence-end-double-space
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
480 (save-excursion (forward-char -1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
481 (and (looking-at "\\. ")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
482 (not (looking-at "\\. ")))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
483 (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
484 ;; 94/3/14 jhod: Kinsoku
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
485 ;(skip-chars-forward "^ \n\t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
486 (fill-move-forward-to-break-point re-break-point)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
487 ;; end patch
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
488 (setq first nil)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
489 ;; Normally, move back over the single space between the words.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
490 (if (eq (char-before (point)) ?\ )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
491 (forward-char -1)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
492 ;; If the left margin and fill prefix by themselves
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
493 ;; pass the fill-column, keep at least one word.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
494 ;; This handles the first line of the paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
495 (if (and (zerop prefixcol)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
496 (let ((fill-point (point)) nchars)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
497 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
498 (move-to-left-margin)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
499 (setq nchars (- fill-point (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
500 (or (< nchars 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
501 (and fill-prefix
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
502 (< nchars (length fill-prefix))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
503 (string= (buffer-substring (point) fill-point)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
504 (substring fill-prefix 0 nchars)))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
505 ;; Ok, skip at least one word. But
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
506 ;; don't stop at a period followed by just one space.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
507 (let ((first t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
508 (while (and (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
509 (or first
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
510 (and (not (bobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
511 sentence-end-double-space
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
512 (save-excursion (forward-char -1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
513 (and (looking-at "\\. ")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
514 (not (looking-at "\\. ")))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
515 (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
516 ;; 97/3/14 jhod: Kinsoku
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
517 ;(skip-chars-forward "^ \t\n")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
518 (fill-move-forward-to-break-point re-break-point)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
519 ;; end patch
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
520 (setq first nil))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
521 ;; Check again to see if we got to the end of the paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
522 (if (save-excursion (skip-chars-forward " \t") (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
523 (or nosqueeze (delete-horizontal-space))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
524 ;; Replace whitespace here with one newline, then indent to left
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
525 ;; margin.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
526 (skip-chars-backward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
527 ;; 97/3/14 jhod: More kinsoku stuff
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
528 (if (featurep 'mule)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
529 ;; WAN means chars which match word-across-newline.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
530 ;; (0) | SPC + SPC* <EOB> --> NL
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
531 ;; (1) WAN | SPC + SPC* --> WAN + SPC + NL
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
532 ;; (2) | SPC + SPC* + WAN --> SPC + NL + WAN
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
533 ;; (3) '.' | SPC + nonSPC --> '.' + SPC + NL + nonSPC
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
534 ;; (4) '.' | SPC + SPC --> '.' + NL
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
535 ;; (5) | SPC* --> NL
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
536 (let ((start (point)) ; 92.6.30 by K.Handa
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
537 (ch (char-after (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
538 (if (and (= ch ? )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
539 (progn ; not case (0) -- 92.6.30 by K.Handa
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
540 (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
541 (not (eobp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
542 (or
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
543 (progn ; case (1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
544 (goto-char start)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
545 (forward-char -1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
546 (looking-at word-across-newline))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
547 (progn ; case (2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
548 (goto-char start)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
549 (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
550 (and (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
551 (looking-at word-across-newline)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
552 ;; never leave space after the end of sentence
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
553 (not (fill-end-of-sentence-p))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
554 (progn ; case (3)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
555 (goto-char (1+ start))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
556 (and (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
557 (not (eq (char-after (point)) ? ))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
558 (fill-end-of-sentence-p)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
559 ;; We should keep one SPACE before NEWLINE. (1),(2),(3)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
560 (goto-char (1+ start))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
561 ;; We should delete all SPACES around break point. (4),(5)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
562 (goto-char start))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
563 ;; end of patch
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
564 (insert ?\n)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
565 ;; Give newline the properties of the space(s) it replaces
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
566 (set-text-properties (1- (point)) (point)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
567 (text-properties-at (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
568 (indent-to-left-margin)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
569 ;; Insert the fill prefix after indentation.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
570 ;; Set prefixcol so whitespace in the prefix won't get lost.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
571 (and fill-prefix (not (equal fill-prefix ""))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
572 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
573 (insert fill-prefix)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
574 (setq prefixcol (current-column))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
575 ;; Justify the line just ended, if desired.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
576 (if justify
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
577 (if (save-excursion (skip-chars-forward " \t") (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
578 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
579 (delete-horizontal-space)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
580 (justify-current-line justify t t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
581 (forward-line -1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
582 (justify-current-line justify nil t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
583 (forward-line 1))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
584 ;; Leave point after final newline.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
585 (goto-char (point-max)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
586 (forward-char 1))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
587
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
588 (defun fill-paragraph (arg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
589 "Fill paragraph at or after point. Prefix arg means justify as well.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
590 If `sentence-end-double-space' is non-nil, then period followed by one
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
591 space does not end a sentence, so don't break a line there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
592
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
593 If `fill-paragraph-function' is non-nil, we call it (passing our
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
594 argument to it), and if it returns non-nil, we simply return its value."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
595 (interactive (list (if current-prefix-arg 'full)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
596 (or (and fill-paragraph-function
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
597 (let ((function fill-paragraph-function)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
598 fill-paragraph-function)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
599 (funcall function arg)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
600 (let ((before (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
601 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
602 (forward-paragraph)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
603 (or (bolp) (newline 1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
604 (let ((end (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
605 (beg (progn (backward-paragraph) (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
606 (goto-char before)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
607 (if use-hard-newlines
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
608 ;; Can't use fill-region-as-paragraph, since this paragraph may
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
609 ;; still contain hard newlines. See fill-region.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
610 (fill-region beg end arg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
611 (fill-region-as-paragraph beg end arg)))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
612
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
613 (defun fill-region (from to &optional justify nosqueeze to-eop)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
614 "Fill each of the paragraphs in the region.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
615 Prefix arg (non-nil third arg, if called from program) means justify as well.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
616
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
617 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
618 whitespace other than line breaks untouched, and fifth arg TO-EOP
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
619 non-nil means to keep filling to the end of the paragraph (or next
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
620 hard newline, if `use-hard-newlines' is on).
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
621
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
622 If `sentence-end-double-space' is non-nil, then period followed by one
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
623 space does not end a sentence, so don't break a line there."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
624 (interactive
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
625 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
626 ;; XEmacs addition:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
627 (barf-if-buffer-read-only nil (region-beginning) (region-end))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
628 (list (region-beginning) (region-end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
629 (if current-prefix-arg 'full))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
630 (let (end beg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
631 (save-restriction
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
632 (goto-char (max from to))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
633 (if to-eop
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
634 (progn (skip-chars-backward "\n")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
635 (forward-paragraph)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
636 (setq end (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
637 (goto-char (setq beg (min from to)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
638 (beginning-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
639 (narrow-to-region (point) end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
640 (while (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
641 (let ((initial (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
642 end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
643 ;; If using hard newlines, break at every one for filling
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
644 ;; purposes rather than using paragraph breaks.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
645 (if use-hard-newlines
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
646 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
647 (while (and (setq end (text-property-any (point) (point-max)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
648 'hard t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
649 (not (eq ?\n (char-after end)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
650 (not (= end (point-max))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
651 (goto-char (1+ end)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
652 (setq end (if end (min (point-max) (1+ end)) (point-max)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
653 (goto-char initial))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
654 (forward-paragraph 1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
655 (setq end (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
656 (forward-paragraph -1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
657 (if (< (point) beg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
658 (goto-char beg))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
659 (if (>= (point) initial)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
660 (fill-region-as-paragraph (point) end justify nosqueeze)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
661 (goto-char end)))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
662
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
663 ;; XEmacs addition: from Tim Bradshaw <tfb@edinburgh.ac.uk>
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
664 (defun fill-paragraph-or-region (arg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
665 "Fill the current region, if it's active; otherwise, fill the paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
666 See `fill-paragraph' and `fill-region' for more information."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
667 (interactive "*P")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
668 (if (region-active-p)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
669 (fill-region (point) (mark) arg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
670 (fill-paragraph arg)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
671
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
672
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
673 (defconst default-justification 'left
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
674 "*Method of justifying text not otherwise specified.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
675 Possible values are `left', `right', `full', `center', or `none'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
676 The requested kind of justification is done whenever lines are filled.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
677 The `justification' text-property can locally override this variable.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
678 This variable automatically becomes buffer-local when set in any fashion.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
679 (make-variable-buffer-local 'default-justification)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
680
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
681 (defun current-justification ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
682 "How should we justify this line?
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
683 This returns the value of the text-property `justification',
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
684 or the variable `default-justification' if there is no text-property.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
685 However, it returns nil rather than `none' to mean \"don't justify\"."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
686 (let ((j (or (get-text-property
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
687 ;; Make sure we're looking at paragraph body.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
688 (save-excursion (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
689 (if (and (eobp) (not (bobp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
690 (1- (point)) (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
691 'justification)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
692 default-justification)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
693 (if (eq 'none j)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
694 nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
695 j)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
696
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
697 (defun set-justification (begin end value &optional whole-par)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
698 "Set the region's justification style.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
699 The kind of justification to use is prompted for.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
700 If the mark is not active, this command operates on the current paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
701 If the mark is active, the region is used. However, if the beginning and end
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
702 of the region are not at paragraph breaks, they are moved to the beginning and
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
703 end of the paragraphs they are in.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
704 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
705 breaks.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
706
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
707 When calling from a program, operates just on region between BEGIN and END,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
708 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
709 extended to include entire paragraphs as in the interactive command."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
710 ;; XEmacs change (was mark-active)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
711 (interactive (list (if (region-active-p) (region-beginning) (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
712 (if (region-active-p) (region-end) (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
713 (let ((s (completing-read
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
714 "Set justification to: "
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
715 '(("left") ("right") ("full")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
716 ("center") ("none"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
717 nil t)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
718 (if (equal s "") (error ""))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
719 (intern s))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
720 t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
721 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
722 (save-restriction
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
723 (if whole-par
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
724 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
725 (paragraph-ignore-fill-prefix (if use-hard-newlines t
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
726 paragraph-ignore-fill-prefix)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
727 (goto-char begin)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
728 (while (and (bolp) (not (eobp))) (forward-char 1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
729 (backward-paragraph)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
730 (setq begin (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
731 (goto-char end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
732 (skip-chars-backward " \t\n" begin)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
733 (forward-paragraph)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
734 (setq end (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
735
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
736 (narrow-to-region (point-min) end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
737 (unjustify-region begin (point-max))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
738 (put-text-property begin (point-max) 'justification value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
739 (fill-region begin (point-max) nil t))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
740
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
741 (defun set-justification-none (b e)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
742 "Disable automatic filling for paragraphs in the region.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
743 If the mark is not active, this applies to the current paragraph."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
744 ;; XEmacs change (was mark-active)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
745 (interactive (list (if (region-active-p) (region-beginning) (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
746 (if (region-active-p) (region-end) (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
747 (set-justification b e 'none t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
748
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
749 (defun set-justification-left (b e)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
750 "Make paragraphs in the region left-justified.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
751 This is usually the default, but see the variable `default-justification'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
752 If the mark is not active, this applies to the current paragraph."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
753 ;; XEmacs change (was mark-active)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
754 (interactive (list (if (region-active-p) (region-beginning) (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
755 (if (region-active-p) (region-end) (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
756 (set-justification b e 'left t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
757
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
758 (defun set-justification-right (b e)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
759 "Make paragraphs in the region right-justified:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
760 Flush at the right margin and ragged on the left.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
761 If the mark is not active, this applies to the current paragraph."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
762 ;; XEmacs change (was mark-active)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
763 (interactive (list (if (region-active-p) (region-beginning) (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
764 (if (region-active-p) (region-end) (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
765 (set-justification b e 'right t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
766
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
767 (defun set-justification-full (b e)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
768 "Make paragraphs in the region fully justified:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
769 This makes lines flush on both margins by inserting spaces between words.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
770 If the mark is not active, this applies to the current paragraph."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
771 ;; XEmacs change (was mark-active)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
772 (interactive (list (if (region-active-p) (region-beginning) (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
773 (if (region-active-p) (region-end) (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
774 (set-justification b e 'full t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
775
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
776 (defun set-justification-center (b e)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
777 "Make paragraphs in the region centered.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
778 If the mark is not active, this applies to the current paragraph."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
779 ;; XEmacs change (was mark-active)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
780 (interactive (list (if (region-active-p) (region-beginning) (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
781 (if (region-active-p) (region-end) (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
782 (set-justification b e 'center t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
783
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
784 ;; 97/3/14 jhod: This functions are added for Kinsoku support
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
785 (defun find-space-insertable-point ()
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 371
diff changeset
786 "Search backward for a permissible point for inserting justification spaces"
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
787 (if (boundp 'space-insertable)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
788 (if (re-search-backward space-insertable nil t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
789 (progn (forward-char 1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
790 t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
791 nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
792 (search-backward " " nil t)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
793
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
794 ;; A line has up to six parts:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
795 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
796 ;; >>> hello.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
797 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
798 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
799 ;; "Indent-1" is the left-margin indentation; normally it ends at column
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
800 ;; given by the `current-left-margin' function.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
801 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
802 ;; "Indent-2" is added to justify a line if the `current-justification' is
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
803 ;; `center' or `right'. In `left' and `full' justification regions, any
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
804 ;; whitespace there is part of the line's text, and should not be changed.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
805 ;; Trailing whitespace is not counted as part of the line length when
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
806 ;; center- or right-justifying.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
807 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
808 ;; All parts of the line are optional, although the final newline can
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
809 ;; only be missing on the last line of the buffer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
810
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
811 (defun justify-current-line (&optional how eop nosqueeze)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
812 "Do some kind of justification on this line.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
813 Normally does full justification: adds spaces to the line to make it end at
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
814 the column given by `current-fill-column'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
815 Optional first argument HOW specifies alternate type of justification:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
816 it can be `left', `right', `full', `center', or `none'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
817 If HOW is t, will justify however the `current-justification' function says to.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
818 If HOW is nil or missing, full justification is done by default.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
819 Second arg EOP non-nil means that this is the last line of the paragraph, so
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
820 it will not be stretched by full justification.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
821 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
822 otherwise it is made canonical."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
823 (interactive)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
824 (if (eq t how) (setq how (or (current-justification) 'none))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
825 (if (null how) (setq how 'full)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
826 (or (memq how '(none left right center))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
827 (setq how 'full))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
828 (or (memq how '(none left)) ; No action required for these.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
829 (let ((fc (current-fill-column))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
830 (pos (point-marker))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
831 fp-end ; point at end of fill prefix
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
832 beg ; point at beginning of line's text
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
833 end ; point at end of line's text
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
834 indent ; column of `beg'
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
835 endcol ; column of `end'
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
836 ncols) ; new indent point or offset
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
837 (end-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
838 ;; Check if this is the last line of the paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
839 (if (and use-hard-newlines (null eop)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
840 (get-text-property (point) 'hard))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
841 (setq eop t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
842 (skip-chars-backward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
843 ;; Quick exit if it appears to be properly justified already
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
844 ;; or there is no text.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
845 (if (or (bolp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
846 (and (memq how '(full right))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
847 (= (current-column) fc)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
848 nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
849 (setq end (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
850 (beginning-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
851 (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
852 ;; Skip over fill-prefix.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
853 (if (and fill-prefix
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
854 (not (string-equal fill-prefix ""))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
855 (equal fill-prefix
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
856 (buffer-substring
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
857 (point) (min (point-max) (+ (length fill-prefix)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
858 (point))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
859 (forward-char (length fill-prefix))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
860 (if (and adaptive-fill-mode
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
861 (looking-at adaptive-fill-regexp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
862 (goto-char (match-end 0))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
863 (setq fp-end (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
864 (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
865 ;; This is beginning of the line's text.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
866 (setq indent (current-column))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
867 (setq beg (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
868 (goto-char end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
869 (setq endcol (current-column))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
870
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
871 ;; HOW can't be null or left--we would have exited already
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
872 (cond ((eq 'right how)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
873 (setq ncols (- fc endcol))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
874 (if (< ncols 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
875 ;; Need to remove some indentation
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
876 (delete-region
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
877 (progn (goto-char fp-end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
878 (if (< (current-column) (+ indent ncols))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
879 (move-to-column (+ indent ncols) t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
880 (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
881 (progn (move-to-column indent) (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
882 ;; Need to add some
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
883 (goto-char beg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
884 (indent-to (+ indent ncols))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
885 ;; If point was at beginning of text, keep it there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
886 (if (= beg pos)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
887 (move-marker pos (point)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
888
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
889 ((eq 'center how)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
890 ;; Figure out how much indentation is needed
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
891 (setq ncols (+ (current-left-margin)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
892 (/ (- fc (current-left-margin) ;avail. space
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
893 (- endcol indent)) ;text width
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
894 2)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
895 (if (< ncols indent)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
896 ;; Have too much indentation - remove some
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
897 (delete-region
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
898 (progn (goto-char fp-end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
899 (if (< (current-column) ncols)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
900 (move-to-column ncols t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
901 (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
902 (progn (move-to-column indent) (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
903 ;; Have too little - add some
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
904 (goto-char beg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
905 (indent-to ncols)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
906 ;; If point was at beginning of text, keep it there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
907 (if (= beg pos)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
908 (move-marker pos (point)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
909
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
910 ((eq 'full how)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
911 ;; Insert extra spaces between words to justify line
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
912 (save-restriction
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
913 (narrow-to-region beg end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
914 (or nosqueeze
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
915 (canonically-space-region beg end))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
916 (goto-char (point-max))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
917 (setq ncols (- fc endcol))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
918 ;; Ncols is number of additional spaces needed
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
919 (if (> ncols 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
920 (if (and (not eop)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
921 ;; 97/3/14 jhod: Kinsoku
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
922 (find-space-insertable-point)) ;(search-backward " " nil t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
923 (while (> ncols 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
924 (let ((nmove (+ 3 (random 3))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
925 (while (> nmove 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
926 (or (find-space-insertable-point) ;(search-backward " " nil t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
927 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
928 (goto-char (point-max))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
929 (find-space-insertable-point))) ;(search-backward " ")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
930 (skip-chars-backward " ")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
931 (setq nmove (1- nmove))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
932 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
933 (insert " ")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
934 (skip-chars-backward " ")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
935 (setq ncols (1- ncols)))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
936 (t (error "Unknown justification value"))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
937 (goto-char pos)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
938 (move-marker pos nil)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
939 nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
940
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
941 (defun unjustify-current-line ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
942 "Remove justification whitespace from current line.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
943 If the line is centered or right-justified, this function removes any
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
944 indentation past the left margin. If the line is full-justified, it removes
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
945 extra spaces between words. It does nothing in other justification modes."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
946 (let ((justify (current-justification)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
947 (cond ((eq 'left justify) nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
948 ((eq nil justify) nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
949 ((eq 'full justify) ; full justify: remove extra spaces
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
950 (beginning-of-line-text)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
951 (canonically-space-region
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
952 (point) (save-excursion (end-of-line) (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
953 ((memq justify '(center right))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
954 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
955 (move-to-left-margin nil t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
956 ;; Position ourselves after any fill-prefix.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
957 (if (and fill-prefix
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
958 (not (string-equal fill-prefix ""))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
959 (equal fill-prefix
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
960 (buffer-substring
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
961 (point) (min (point-max) (+ (length fill-prefix)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
962 (point))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
963 (forward-char (length fill-prefix)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
964 (delete-region (point) (progn (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
965 (point))))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
966
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
967 (defun unjustify-region (&optional begin end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
968 "Remove justification whitespace from region.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
969 For centered or right-justified regions, this function removes any indentation
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
970 past the left margin from each line. For full-justified lines, it removes
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
971 extra spaces between words. It does nothing in other justification modes.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
972 Arguments BEGIN and END are optional; default is the whole buffer."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
973 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
974 (save-restriction
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
975 (if end (narrow-to-region (point-min) end))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
976 (goto-char (or begin (point-min)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
977 (while (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
978 (unjustify-current-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
979 (forward-line 1)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
980
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
981
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
982 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
983 "Fill paragraphs within the region, allowing varying indentation within each.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
984 This command divides the region into \"paragraphs\",
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
985 only at paragraph-separator lines, then fills each paragraph
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
986 using as the fill prefix the smallest indentation of any line
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
987 in the paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
988
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
989 When calling from a program, pass range to fill as first two arguments.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
990
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
991 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
992 JUSTIFY to justify paragraphs (prefix arg),
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
993 MAIL-FLAG for a mail message, i. e. don't fill header lines."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
994 (interactive (list (region-beginning) (region-end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
995 (if current-prefix-arg 'full)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
996 (let ((fill-individual-varying-indent t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
997 (fill-individual-paragraphs min max justifyp mailp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
998
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
999 (defun fill-individual-paragraphs (min max &optional justify mailp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1000 "Fill paragraphs of uniform indentation within the region.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1001 This command divides the region into \"paragraphs\",
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1002 treating every change in indentation level as a paragraph boundary,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1003 then fills each paragraph using its indentation level as the fill prefix.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1004
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1005 When calling from a program, pass range to fill as first two arguments.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1006
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1007 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1008 JUSTIFY to justify paragraphs (prefix arg),
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1009 MAIL-FLAG for a mail message, i. e. don't fill header lines."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1010 (interactive (list (region-beginning) (region-end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1011 (if current-prefix-arg 'full)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1012 (save-restriction
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1013 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1014 (goto-char min)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1015 (beginning-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1016 (narrow-to-region (point) max)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1017 (if mailp
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1018 (while (and (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1019 (or (looking-at "[ \t]*[^ \t\n]+:")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1020 (looking-at "[ \t]*$")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1021 (if (looking-at "[ \t]*[^ \t\n]+:")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1022 (search-forward "\n\n" nil 'move)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1023 (forward-line 1))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1024 (narrow-to-region (point) max)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1025 ;; Loop over paragraphs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1026 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1027 (move-to-left-margin)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1028 (let ((start (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1029 fill-prefix fill-prefix-regexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1030 ;; Find end of paragraph, and compute the smallest fill-prefix
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1031 ;; that fits all the lines in this paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1032 (while (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1033 ;; Update the fill-prefix on the first line
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1034 ;; and whenever the prefix good so far is too long.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1035 (if (not (and fill-prefix
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1036 (looking-at fill-prefix-regexp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1037 (setq fill-prefix
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1038 (if (and adaptive-fill-mode adaptive-fill-regexp
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1039 (looking-at adaptive-fill-regexp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1040 (match-string 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1041 (buffer-substring
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1042 (point)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1043 (save-excursion (skip-chars-forward " \t")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1044 (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1045 fill-prefix-regexp (regexp-quote fill-prefix)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1046 (forward-line 1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1047 (if (bolp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1048 ;; If forward-line went past a newline
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1049 ;; move further to the left margin.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1050 (move-to-left-margin))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1051 ;; Now stop the loop if end of paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1052 (and (not (eobp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1053 (if fill-individual-varying-indent
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1054 ;; If this line is a separator line, with or
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1055 ;; without prefix, end the paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1056 (and
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1057 (not (looking-at paragraph-separate))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1058 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1059 (not (and (looking-at fill-prefix-regexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1060 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1061 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1062 (forward-char (length fill-prefix))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1063 (looking-at paragraph-separate))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1064 ;; If this line has more or less indent
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1065 ;; than the fill prefix wants, end the paragraph.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1066 (and (looking-at fill-prefix-regexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1067 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1068 (not
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1069 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1070 (forward-char (length fill-prefix))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1071 (or (looking-at paragraph-separate)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1072 (looking-at paragraph-start))))))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1073 ;; Fill this paragraph, but don't add a newline at the end.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1074 (let ((had-newline (bolp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1075 (fill-region-as-paragraph start (point) justify)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1076 (or had-newline (delete-char -1))))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1077
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1078 ;;; fill.el ends here