update.
[chise/xemacs-chise.git.1] / lisp / mule / kinsoku.el
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 ;;;     "\e$B!!!"!#!$!%!&!'!'!(!)!*!+!,!-!.!/\e(B"
28 ;;;   "\e$B!0!1!2!3!4!5!6!7!8!9!:!;!<!=!>!?\e(B"
29 ;;;   "\e$B!@!A!B!C!D!E!F!G!H!I!J!K!L!M!N!O\e(B"
30 ;;;   "\e$B!P!Q!R!S!T!U!V!W!X!Y!Z![!\!]!^!_\e(B"
31 ;;;   "\e$B!`!a!b!c!d!e!f!g!h!i!j!k!l!m!n!o\e(B"
32 ;;;   "\e$B!p!q!r!s!t!u!v!w!x!y!z!{!|!}!~\e(B"
33 ;;;     "\e$B"!"""#"$"%"&"'"(")"*"+","-".\e(B "
34 ;;;     "\e$B&!&"&#&$&%&&&'&(&)&*&+&,&-&.&/\e(B"
35 ;;;   "\e$B&0&1&2&3&4&5&6&7&8\e(B"
36 ;;;     "\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"W"X"Y"Z"["\"]"^"_\e(B
109 ;;;\e$(0"`"a"b"c"d"e"f"g"h"i"j"k"l"m"n"o\e(B
110 ;;;\e$(0"p"q"r"s"t"u"v"w"x"y"z"{"|"}"~\e(B
111 ;;;  \e$(0#!#"###$#%#&#'#(#)#*#+#,#-#.#/\e(B
112 ;;;\e$(0#0#1#2#3#4#5#6#7#8#9#:#;#<#=#>#?\e(B
113 ;;;\e$(0#@#A#B#C#D#E#F#G#H#I#J#K#L#M#N#O\e(B
114 ;;;\e$(0#P#Q#R#S#T#U#V#W#X#Y#Z#[#\#]#^#_\e(B
115 ;;;\e$(0#`#a#b#c#d#e#f#g#h#i#j#k#l#m#n#o\e(B
116 ;;;\e$(0#p#q#r#s#t#u#v#w#x#y#z#{#|#}#~\e(B
117 ;;;  \e$(0$!$"$#$$$%$&$'$($)$*$+$,$-$.$/\e(B
118 ;;;\e$(0$0$1$2$3$4$5$6$7$8$9$:$;$<$=$>$?\e(B
119 ;;;\e$(0$@$A$B$C$D$E$F$G$H$I$J$K$L$M$N$O\e(B
120 ;;;\e$(0$P$Q$R$S$T$U$V$W$X$Y$Z$[$\$]$^$_\e(B
121 ;;;\e$(0$`$a$b$c$d$e$f$g$h$i$j$k$l$m$n$o\e(B
122 ;;;\e$(0$p$q$r$s$t$u$v$w$x$y$z${$|$}$~\e(B
123 ;;;  \e$(0%!%"%#%$%%%&%'%(%)%*%+%,%-%.%/\e(B
124 ;;;\e$(0%0%1%2%3%4%5%6%7%8%9%:%;%<%=%>%?\e(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  "\e$B!"!#!$!%!&!'!(!)!*!+!,!-!.!/!0!1!2!3!4!5!6!7!8!9!:!;!<!=!>\e(B"
143            "\e$B!?!@!A!B!C!D!E!G!I!K!M!O!Q!S!U!W!Y![!k!l!m!n\e(B"
144            "\e$B$!$#$%$'$)$C$c$e$g$n%!%#%%%'%)%C%c%e%g%n%u%v\e(B")
145   "BOL kinsoku for JISX0208.")
146 (defvar kinsoku-jis-eol
147   "\e$B!F!H!J!L!N!P!R!T!V!X!Z!k!l!m!n!w!x\e(B"
148   "EOL kinsoku for JISX0208.")
149 (defvar kinsoku-gb-bol
150   (concat  "\e$A!"!##.#,!$!%!&!'!(!)!*!+!,!-!/!1#)!3!5!7!9!;!=\e(B"
151            "\e$A!?#;#:#?#!!@!A!B!C!c!d!e!f#/#\#"#_#~#|(e\e(B")
152   "BOL kinsoku for GB2312.")
153 (defvar kinsoku-gb-eol
154   (concat "\e$A!.!0#"#(!2!4!6!8!:!<!>!c!d!e#@!f!l\e(B"
155           "\e$A(E(F(G(H(I(J(K(L(M(N(O(P(Q(R(S(T(U(V(W(X(Y(h\e(B")
156   "EOL kinsoku for GB2312.")
157 (defvar kinsoku-big5-bol
158   (concat  "\e$(0!"!#!$!%!&!'!(!)!*!+!,!-!.!/!0!1!2\e(B"
159            "\e$(0!3!4!5!6!7!8!9!:!;!<!=!?!A!C!E!G!I!K\e(B"
160            "\e$(0!M!O!Q\e(B       \e$(0!S!U!W!Y![!]!_!a!c!e!g!i!k!q\e(B"
161            "\e$(0"#"$"%"&"'"(")"*"+","2"3"4"j"k"l"x%7\e(B")
162   "BOL kinsoku for BIG5.")
163 (defvar kinsoku-big5-eol
164   (concat "\e$(0!>!@!B!D!F!H!J!L!N!P!R!T!V!X!Z!\!^!`!b\e(B"
165           "\e$(0!d!f!h!j!k!q!p"i"j"k"n"x$u$v$w$x$y$z${\e(B"
166           "\e$(0$|$}$~%!%"%#%$%%%&%'%(%)%*%+%:\e(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 (string-to-char-list 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 (string-to-char-list kinsoku-gb-bol)
184       do (modify-category-entry char ?s))
185 (loop for char in (string-to-char-list kinsoku-gb-eol)
186       do (modify-category-entry char ?e))
187 ;; kinsoku-big5
188 (loop for char in (string-to-char-list kinsoku-big5-bol)
189       do (modify-category-entry char ?s))
190 (loop for char in (string-to-char-list 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\e$B$G2~9T$9$k$H9TF,6XB'$K?($l$k$+$I$&$+$r$+$($9!#\e(B
197 \e$B9TF,6XB'J8;z$O\e(B\'s\'\e$B$N\e(Bcategory\e$B$G;XDj$9$k!#\e(B"
198   (let ((before (char-before))
199         (after (char-after)))
200     (if (and after
201              (or
202               (and kinsoku-ascii (char-in-category-p after ?a))
203               (and kinsoku-jis (or (char-in-category-p after ?j)
204                                    (and before
205                                         (char-in-category-p before ?j))))
206               (and kinsoku-gb (or (char-in-category-p after ?c)
207                                   (and before
208                                        (char-in-category-p before ?c))))
209               (and kinsoku-big5 (or (char-in-category-p after ?t)
210                                     (and before
211                                          (char-in-category-p before ?t))))))
212         (char-in-category-p after ?s)
213       nil)))
214
215 (defun kinsoku-eol-p ()
216   "Check if point would break forbidden end-of-line rules
217 Uses category \'e\' to check.
218 point\e$B$G2~9T$9$k$H9TKv6XB'$K?($l$k$+$I$&$+$r$+$($9!#\e(B
219 \e$B9TKv6XB'J8;z$O\e(B\'s\'\e$B$N\e(Bcategory\e$B$G;XDj$9$k!#\e(B"
220   (let ((before (char-before))
221         (after (char-after)))
222     (if (and before
223              (or
224               (and kinsoku-ascii (char-in-category-p before ?a))
225               (and kinsoku-jis (or (char-in-category-p before ?j)
226                                    (and after
227                                         (char-in-category-p after ?j))))
228               (and kinsoku-gb (or (char-in-category-p before ?c)
229                                   (and after
230                                        (char-in-category-p after ?c))))
231               (and kinsoku-big5 (or (char-in-category-p before ?t)
232                                     (and after
233                                          (char-in-category-p after ?t))))))
234         (char-in-category-p before ?e)
235       nil)))
236
237 (defvar kinsoku-extend-limit nil
238   "Defines how many characters kinsoku will search forward before giving up.
239 A value of nil equates to infinity.
240 \e$B6XB'=hM}$G9T$r?-$P$7$FNI$$H>3QJ8;z?t$r;XDj$9$k!#\e(B
241 \e$BHsIi@0?t0J30$N>l9g$OL58BBg$r0UL#$9$k!#\e(B")
242
243 (defun kinsoku-process ()
244   "Move to a point that will not break forbidden line break rules.
245 \e$B6XB'$K?($l$J$$E@$X0\F0$9$k!#\e(B
246 point\e$B$,9TF,6XB'$K?($l$k>l9g$O9T$r?-$P$7$F!"6XB'$K?($l$J$$E@$rC5$9!#\e(B
247 point\e$B$,9TKv6XB'$K?($l$k>l9g$O9T$r=L$a$F!"6XB'$K?($l$J$$E@$rC5$9!#\e(B
248 \e$B$?$@$7!"9T?-$P$7H>3QJ8;z?t$,\e(Bkinsoku-extend-limit\e$B$r1[$($k$H!"\e(B
249 \e$B9T$r=L$a$F6XB'$K?($l$J$$E@$rC5$9!#\e(B"
250   (let ((bol-kin nil) (eol-kin nil))
251     (if (and (not (bolp))
252              (not (eolp))
253              (or (setq bol-kin (kinsoku-bol-p))
254                  (setq eol-kin (kinsoku-eol-p))))
255         (cond(bol-kin (kinsoku-process-extend))
256              (eol-kin (kinsoku-process-shrink))))))
257
258 (defun kinsoku-process-extend ()
259   "Move point forward to a permissable for line-breaking.
260 \e$B9T$r?-$P$7$F6XB'$K?($l$J$$E@$X0\F0$9$k!#\e(B"
261   (let ((max-column (+ fill-column 
262                        (if (and (numberp kinsoku-extend-limit)
263                                 (>= kinsoku-extend-limit 0))
264                            kinsoku-extend-limit
265                          10000)))  ;;; 10000 is deliberatly unreasonably large
266         ch1 ch2)
267     (while (and (setq ch1 (char-after))
268                 (<= (+ (current-column)
269                        (char-width ch1 ))
270                     max-column)
271                 (not (bolp))
272                 (not (eolp))
273                 (or (kinsoku-eol-p)
274                     (kinsoku-bol-p)
275                     ;;; don't break in the middle of an English word
276                     (and (char-in-category-p ch1 ?a)
277                          (setq ch2 (char-before))
278                          (char-in-category-p ch2 ?a)
279                          (= ?w (char-syntax ch2))
280                          (= ?w (char-syntax ch1)))))
281       (forward-char))
282     (if (or (kinsoku-eol-p) (kinsoku-bol-p))
283         (kinsoku-process-shrink))))
284
285 (defun kinsoku-process-shrink ()
286   "Move point backward to a point permissable for line-breaking.
287 \e$B9T$r=L$a$F6XB'$K?($l$J$$E@$X0\F0$9$k!#\e(B"
288   (let (ch1 ch2)
289     (while (and (not (bolp))
290                 (not (eolp))
291                 (or (kinsoku-bol-p)
292                     (kinsoku-eol-p)
293                 ;;; don't break in the middle of an English word
294                     (and
295                      (char-in-category-p (setq ch1 (following-char)) ?a)
296                      (char-in-category-p (setq ch2 (preceding-char)) ?a)
297                      (= ?w (char-syntax ch2))
298                      (= ?w (char-syntax ch1)))))
299       (backward-char))))