1 ;; kinsoku.el -- Kinsoku (line wrap) processing for XEmacs/Mule
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.
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)
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.
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.
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
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 ;;; "
\e$B&A&B&C&D&E&F&G&H&I&J&K&L&M&N&O
\e(B"
37 ;;; "
\e$B&P&Q&R&S&T&U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"_
\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
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)
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.")
169 (define-category ?s "Kinsoku forbidden start of line characters")
170 (define-category ?e "Kinsoku forbidden end of line characters")
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))
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))
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))
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))
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)))
202 (and kinsoku-ascii (char-in-category-p after ?a))
203 (and kinsoku-jis (or (char-in-category-p after ?j)
205 (char-in-category-p before ?j))))
206 (and kinsoku-gb (or (char-in-category-p after ?c)
208 (char-in-category-p before ?c))))
209 (and kinsoku-big5 (or (char-in-category-p after ?t)
211 (char-in-category-p before ?t))))))
212 (char-in-category-p after ?s)
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)))
224 (and kinsoku-ascii (char-in-category-p before ?a))
225 (and kinsoku-jis (or (char-in-category-p before ?j)
227 (char-in-category-p after ?j))))
228 (and kinsoku-gb (or (char-in-category-p before ?c)
230 (char-in-category-p after ?c))))
231 (and kinsoku-big5 (or (char-in-category-p before ?t)
233 (char-in-category-p after ?t))))))
234 (char-in-category-p before ?e)
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")
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))
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))))))
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))
265 10000))) ;;; 10000 is deliberatly unreasonably large
267 (while (and (setq ch1 (char-after))
268 (<= (+ (current-column)
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)))))
282 (if (or (kinsoku-eol-p) (kinsoku-bol-p))
283 (kinsoku-process-shrink))))
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"
289 (while (and (not (bolp))
293 ;;; don't break in the middle of an English word
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)))))