333
|
1 ;; kinsoku.el -- Kinsoku (line wrap) processing for XEmacs/Mule
|
|
2
|
|
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
|
|
4 ;; This file is part of Mule (MULtilingual Enhancement of XEmacs).
|
|
5 ;; This file contains Japanese and Chinese characters.
|
|
6
|
|
7 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
8 ;; under the terms of the GNU General Public License as published by
|
|
9 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
10 ;; any later version.
|
|
11
|
|
12 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 ;; General Public License for more details.
|
|
16
|
|
17 ;; You should have received a copy of the GNU General Public License
|
|
18 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 ;; Boston, MA 02111-1307, USA.
|
|
21
|
|
22 ;; Written by Jareth Hein (jhod@po.iijnet.or.jp) based off of
|
|
23 ;; code by S.Tomura, Electrotechnical Lab. (tomura@etl.go.jp) from
|
|
24 ;; Mule-2.3
|
|
25
|
|
26 ;;; Special characters for JIS code
|
|
27 ;;; "$B!!!"!#!$!%!&!'!'!(!)!*!+!,!-!.!/(B"
|
|
28 ;;; "$B!0!1!2!3!4!5!6!7!8!9!:!;!<!=!>!?(B"
|
|
29 ;;; "$B!@!A!B!C!D!E!F!G!H!I!J!K!L!M!N!O(B"
|
|
30 ;;; "$B!P!Q!R!S!T!U!V!W!X!Y!Z![!\!]!^!_(B"
|
|
31 ;;; "$B!`!a!b!c!d!e!f!g!h!i!j!k!l!m!n!o(B"
|
|
32 ;;; "$B!p!q!r!s!t!u!v!w!x!y!z!{!|!}!~(B"
|
|
33 ;;; "$B"!"""#"$"%"&"'"(")"*"+","-".(B "
|
|
34 ;;; "$B&!&"&#&$&%&&&'&(&)&*&+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"A"B"C"D"E"F"G"H"I"J"K"L"M"N"O(B
|
|
108 ;;;$(0"P"Q"R"S"T"U"V"W"X"Y"Z"["\"]"^"_(B
|
|
109 ;;;$(0"`"a"b"c"d"e"f"g"h"i"j"k"l"m"n"o(B
|
|
110 ;;;$(0"p"q"r"s"t"u"v"w"x"y"z"{"|"}"~(B
|
|
111 ;;; $(0#!#"###$#%#&#'#(#)#*#+#,#-#.#/(B
|
|
112 ;;;$(0#0#1#2#3#4#5#6#7#8#9#:#;#<#=#>#?(B
|
|
113 ;;;$(0#@#A#B#C#D#E#F#G#H#I#J#K#L#M#N#O(B
|
|
114 ;;;$(0#P#Q#R#S#T#U#V#W#X#Y#Z#[#\#]#^#_(B
|
|
115 ;;;$(0#`#a#b#c#d#e#f#g#h#i#j#k#l#m#n#o(B
|
|
116 ;;;$(0#p#q#r#s#t#u#v#w#x#y#z#{#|#}#~(B
|
|
117 ;;; $(0$!$"$#$$$%$&$'$($)$*$+$,$-$.$/(B
|
|
118 ;;;$(0$0$1$2$3$4$5$6$7$8$9$:$;$<$=$>$?(B
|
|
119 ;;;$(0$@$A$B$C$D$E$F$G$H$I$J$K$L$M$N$O(B
|
|
120 ;;;$(0$P$Q$R$S$T$U$V$W$X$Y$Z$[$\$]$^$_(B
|
|
121 ;;;$(0$`$a$b$c$d$e$f$g$h$i$j$k$l$m$n$o(B
|
|
122 ;;;$(0$p$q$r$s$t$u$v$w$x$y$z${$|$}$~(B
|
|
123 ;;; $(0%!%"%#%$%%%&%'%(%)%*%+%,%-%.%/(B
|
|
124 ;;;$(0%0%1%2%3%4%5%6%7%8%9%:%;%<%=%>%?(B
|
|
125
|
|
126 (defvar kinsoku-ascii nil "Do kinsoku-processing for ASCII.")
|
|
127 (make-variable-buffer-local 'kinsoku-ascii)
|
|
128 (set-default 'kinsoku-ascii nil)
|
|
129 (defvar kinsoku-jis t "Do kinsoku-processing for JISX0208.")
|
|
130 (make-variable-buffer-local 'kinsoku-jis)
|
|
131 (set-default 'kinsoku-jis t)
|
|
132 (defvar kinsoku-gb t "Do kinsoku-processing for GB2312.")
|
|
133 (make-variable-buffer-local 'kinsoku-gb)
|
|
134 (set-default 'kinsoku-gb t)
|
|
135 (defvar kinsoku-big5 t "Do kinsoku-processing for Big5..")
|
|
136 (make-variable-buffer-local 'kinsoku-big5)
|
|
137 (set-default 'kinsoku-big5 t)
|
|
138
|
|
139 (defvar kinsoku-ascii-bol "!)-_~}]:;',.?" "BOL kinsoku for ASCII.")
|
|
140 (defvar kinsoku-ascii-eol "({[" "EOL kinsoku for ASCII.")
|
|
141 (defvar kinsoku-jis-bol
|
|
142 (concat "$B!"!#!$!%!&!'!(!)!*!+!,!-!.!/!0!1!2!3!4!5!6!7!8!9!:!;!<!=!>(B"
|
|
143 "$B!?!@!A!B!C!D!E!G!I!K!M!O!Q!S!U!W!Y![!k!l!m!n(B"
|
|
144 "$B$!$#$%$'$)$C$c$e$g$n%!%#%%%'%)%C%c%e%g%n%u%v(B")
|
|
145 "BOL kinsoku for JISX0208.")
|
|
146 (defvar kinsoku-jis-eol
|
|
147 "$B!F!H!J!L!N!P!R!T!V!X!Z!k!l!m!n!w!x(B"
|
|
148 "EOL kinsoku for JISX0208.")
|
|
149 (defvar kinsoku-gb-bol
|
|
150 (concat "$A!"!##.#,!$!%!&!'!(!)!*!+!,!-!/!1#)!3!5!7!9!;!=(B"
|
|
151 "$A!?#;#:#?#!!@!A!B!C!c!d!e!f#/#\#"#_#~#|(e(B")
|
|
152 "BOL kinsoku for GB2312.")
|
|
153 (defvar kinsoku-gb-eol
|
|
154 (concat "$A!.!0#"#(!2!4!6!8!:!<!>!c!d!e#@!f!l(B"
|
|
155 "$A(E(F(G(H(I(J(K(L(M(N(O(P(Q(R(S(T(U(V(W(X(Y(h(B")
|
|
156 "EOL kinsoku for GB2312.")
|
|
157 (defvar kinsoku-big5-bol
|
|
158 (concat "$(0!"!#!$!%!&!'!(!)!*!+!,!-!.!/!0!1!2(B"
|
|
159 "$(0!3!4!5!6!7!8!9!:!;!<!=!?!A!C!E!G!I!K(B"
|
|
160 "$(0!M!O!Q(B $(0!S!U!W!Y![!]!_!a!c!e!g!i!k!q(B"
|
|
161 "$(0"#"$"%"&"'"(")"*"+","2"3"4"j"k"l"x%7(B")
|
|
162 "BOL kinsoku for BIG5.")
|
|
163 (defvar kinsoku-big5-eol
|
|
164 (concat "$(0!>!@!B!D!F!H!J!L!N!P!R!T!V!X!Z!\!^!`!b(B"
|
|
165 "$(0!d!f!h!j!k!q!p"i"j"k"n"x$u$v$w$x$y$z${(B"
|
|
166 "$(0$|$}$~%!%"%#%$%%%&%'%(%)%*%+%:(B")
|
|
167 "EOL kinsoku for BIG5.")
|
|
168
|
|
169 (define-category ?s "Kinsoku forbidden start of line characters")
|
|
170 (define-category ?e "Kinsoku forbidden end of line characters")
|
|
171
|
|
172 ;; kinsoku ascii
|
|
173 (loop for char in (string-to-char-list kinsoku-ascii-bol)
|
|
174 do (modify-category-entry char ?s))
|
|
175 (loop for char in kinsoku-ascii-eol
|
|
176 do (modify-category-entry char ?e))
|
|
177 ;; kinsoku-jis
|
|
178 (loop for char in (string-to-char-list kinsoku-jis-bol)
|
|
179 do (modify-category-entry char ?s))
|
|
180 (loop for char in (string-to-char-list kinsoku-jis-eol)
|
|
181 do (modify-category-entry char ?e))
|
|
182 ;; kinsoku-gb
|
|
183 (loop for char in kinsoku-gb-bol
|
|
184 do (modify-category-entry char ?s))
|
|
185 (loop for char in kinsoku-gb-eol
|
|
186 do (modify-category-entry char ?e))
|
|
187 ;; kinsoku-big5
|
|
188 (loop for char in kinsoku-big5-bol
|
|
189 do (modify-category-entry char ?s))
|
|
190 (loop for char in kinsoku-big5-eol
|
|
191 do (modify-category-entry char ?e))
|
|
192
|
|
193 (defun kinsoku-bol-p ()
|
|
194 "Check if point would break forbidden beginning-of-line rules
|
|
195 Uses category \'s\' to check.
|
|
196 point$B$G2~9T$9$k$H9TF,6XB'$K?($l$k$+$I$&$+$r$+$($9!#(B
|
|
197 $B9TF,6XB'J8;z$O(B\'s\'$B$N(Bcategory$B$G;XDj$9$k!#(B"
|
|
198 (let ((ch (char-after)))
|
|
199 (if (and ch
|
|
200 (or
|
|
201 (and kinsoku-ascii (char-in-category-p ch ?a))
|
|
202 (and kinsoku-jis (char-in-category-p ch ?j))
|
|
203 (and kinsoku-gb (char-in-category-p ch ?c))
|
|
204 (and kinsoku-big5 (char-in-category-p ch ?t))))
|
|
205 (char-in-category-p ch ?s)
|
|
206 nil)))
|
|
207
|
|
208 (defun kinsoku-eol-p ()
|
|
209 "Check if point would break forbidden end-of-line rules
|
|
210 Uses category \'e\' to check.
|
|
211 point$B$G2~9T$9$k$H9TKv6XB'$K?($l$k$+$I$&$+$r$+$($9!#(B
|
|
212 $B9TKv6XB'J8;z$O(B\'s\'$B$N(Bcategory$B$G;XDj$9$k!#(B"
|
|
213 (let ((ch (char-before)))
|
|
214 (if (and ch
|
|
215 (or
|
|
216 (and kinsoku-ascii (char-in-category-p ch ?a))
|
|
217 (and kinsoku-jis (char-in-category-p ch ?j))
|
|
218 (and kinsoku-gb (char-in-category-p ch ?c))
|
|
219 (and kinsoku-big5 (char-in-category-p ch ?t))))
|
|
220 (char-in-category-p ch ?e)
|
|
221 nil)))
|
|
222
|
|
223 (defvar kinsoku-extend-limit nil
|
|
224 "Defines how many characters kinsoku will search forward before giving up.
|
|
225 A value of nil equates to infinity.
|
|
226 $B6XB'=hM}$G9T$r?-$P$7$FNI$$H>3QJ8;z?t$r;XDj$9$k!#(B
|
|
227 $BHsIi@0?t0J30$N>l9g$OL58BBg$r0UL#$9$k!#(B")
|
|
228
|
|
229 (defun kinsoku-process ()
|
|
230 "Move to a point that will not break forbidden line break rules.
|
|
231 $B6XB'$K?($l$J$$E@$X0\F0$9$k!#(B
|
|
232 point$B$,9TF,6XB'$K?($l$k>l9g$O9T$r?-$P$7$F!"6XB'$K?($l$J$$E@$rC5$9!#(B
|
|
233 point$B$,9TKv6XB'$K?($l$k>l9g$O9T$r=L$a$F!"6XB'$K?($l$J$$E@$rC5$9!#(B
|
|
234 $B$?$@$7!"9T?-$P$7H>3QJ8;z?t$,(Bkinsoku-extend-limit$B$r1[$($k$H!"(B
|
|
235 $B9T$r=L$a$F6XB'$K?($l$J$$E@$rC5$9!#(B"
|
|
236 (let ((bol-kin nil) (eol-kin nil))
|
|
237 (if (and (not (bolp))
|
|
238 (not (eolp))
|
|
239 (or (setq bol-kin (kinsoku-bol-p))
|
|
240 (setq eol-kin (kinsoku-eol-p))))
|
|
241 (cond(bol-kin (kinsoku-process-extend))
|
|
242 (eol-kin (kinsoku-process-shrink))))))
|
|
243
|
|
244 (defun kinsoku-process-extend ()
|
|
245 "Move point forward to a permissable for line-breaking.
|
|
246 $B9T$r?-$P$7$F6XB'$K?($l$J$$E@$X0\F0$9$k!#(B"
|
|
247 (let ((max-column (+ fill-column
|
|
248 (if (and (numberp kinsoku-extend-limit)
|
|
249 (>= kinsoku-extend-limit 0))
|
|
250 kinsoku-extend-limit
|
|
251 10000))) ;;; 10000 is deliberatly unreasonably large
|
|
252 ch1 ch2)
|
|
253 (while (and (setq ch1 (char-after))
|
|
254 (<= (+ (current-column)
|
|
255 (char-width ch1 ))
|
|
256 max-column)
|
|
257 (not (bolp))
|
|
258 (not (eolp))
|
|
259 (or (kinsoku-eol-p)
|
|
260 (kinsoku-bol-p)
|
|
261 ;;; don't break in the middle of an English word
|
|
262 (and (char-in-category-p ch1 ?a)
|
|
263 (setq ch2 (char-before))
|
|
264 (char-in-category-p ch2 ?a)
|
|
265 (= ?w (char-syntax ch2))
|
|
266 (= ?w (char-syntax ch1)))))
|
|
267 (forward-char))
|
|
268 (if (or (kinsoku-eol-p) (kinsoku-bol-p))
|
|
269 (kinsoku-process-shrink))))
|
|
270
|
|
271 (defun kinsoku-process-shrink ()
|
|
272 "Move point backward to a point permissable for line-breaking.
|
|
273 $B9T$r=L$a$F6XB'$K?($l$J$$E@$X0\F0$9$k!#(B"
|
|
274 (let (ch1 ch2)
|
|
275 (while (and (not (bolp))
|
|
276 (not (eolp))
|
|
277 (or (kinsoku-bol-p)
|
|
278 (kinsoku-eol-p)
|
|
279 ;;; don't break in the middle of an English word
|
|
280 (and
|
|
281 (char-in-category-p (setq ch1 (following-char)) ?a)
|
|
282 (char-in-category-p (setq ch2 (preceding-char)) ?a)
|
|
283 (= ?w (char-syntax ch2))
|
|
284 (= ?w (char-syntax ch1)))))
|
|
285 (backward-char))))
|