annotate lisp/prim/fill.el @ 159:3bb7ccffb0c0 r20-3b6

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