annotate lisp/prim/fill.el @ 126:1370575f1259 xemacs-20-1p1

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