annotate lisp/prim/fill.el @ 207:e45d5e7c476e r20-4b2

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