(kinsoku-longer): Check eolp, not eobp.
[bpt/emacs.git] / lisp / international / kinsoku.el
CommitLineData
60370d40 1;;; kinsoku.el --- `Kinsoku' processing funcs -*- coding: iso-2022-7bit; -*-
4ed46869 2
2fd125a3
KH
3;; Copyright (C) 1997, 2003 Free Software Foundation, Inc.
4;; Copyright (C) 1995, 1997, 1998
5;; National Institute of Advanced Industrial Science and Technology (AIST)
6;; Registration Number H14PRO021
4ed46869 7
fa526c4a 8;; Keywords: mule, kinsoku
4ed46869
KH
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
369314dc 23;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
4ed46869
KH
26
27;;; Commentary:
28
29;; `Kinsoku' processing is to prohibit specific characters to be
30;; placed at beginning of line or at end of line. Characters not to
31;; be placed at beginning and end of line have character category `>'
32;; and `<' respectively. This restriction is dissolved by making a
33;; line longer or shorter.
34;;
35;; `Kinsoku' is a Japanese word which originally means ordering to
36;; stay in one place, and is used for the text processing described
37;; above in the context of text formatting.
38
39;;; Code:
40
41(defvar kinsoku-limit 4
42 "How many more columns we can make lines longer by `kinsoku' processing.
43The value 0 means there's no limitation.")
44
45;; Setting character category `>' for characters which should not be
46;; placed at beginning of line.
47(let* ((kinsoku-bol
48 (concat
49 ;; ASCII
50 "!)-_~}]:;',.?"
a09244e6 51 ;; Latin JISX0201
e8dd0160 52 ;; Instead of putting Latin JISX0201 string directly, we
a09244e6
KH
53 ;; generate the string as below to avoid character
54 ;; unification problem.
55 (let* ((str1 "!)-_~}]:;',.?")
56 (len (length str1))
57 (idx 0)
58 (str2 "")
59 ch)
60 (while (< idx len)
61 (setq ch (make-char 'latin-jisx0201 (aref str1 idx))
62 str2 (concat str2 (char-to-string ch))
63 idx (1+ idx)))
64 str2)
65 ;; Katakana JISX0201
66 "\e(I!#'()*+,-./0^_\e(B"
4ed46869
KH
67 ;; Japanese JISX0208
68 "\e$B!"!#!$!%!&!'!(!)!*!+!,!-!.!/!0!1!2!3!4!5!6!7!8!9!:!;!<!=!>\e(B\
69\e$B!?!@!A!B!C!D!E!G!I!K!M!O!Q!S!U!W!Y![!k!l!m!n\e(B\
70\e$B$!$#$%$'$)$C$c$e$g$n%!%#%%%'%)%C%c%e%g%n%u%v\e(B"
71 ;; Chinese GB2312
72 "\e$A!"!##.#,!$!%!&!'!(!)!*!+!,!-!/!1#)!3!5!7!9!;!=\e(B\
73\e$A!?#;#:#?#!!@!A!B!C!c!d!e!f#/#\#"#_#~#|(e\e(B"
74 ;; Chinese BIG5
75 "\e$(0!"!#!$!%!&!'!(!)!*!+!,!-!.!/!0!1!2\e(B\
76\e$(0!3!4!5!6!7!8!9!:!;!<!=!?!A!C!E!G!I!K\e(B\
4c0f6426 77\e$(0!M!O!Q!S!U!W!Y![!]!_!a!c!e!g!i!k!q\e(B\
4ed46869
KH
78\e$(0"#"$"%"&"'"(")"*"+","2"3"4"j"k"l"x%7\e(B"))
79 (len (length kinsoku-bol))
80 (idx 0)
81 ch)
82 (while (< idx len)
4a027a0d
KH
83 (setq ch (aref kinsoku-bol idx)
84 idx (1+ idx))
4ed46869
KH
85 (modify-category-entry ch ?>)))
86
87;; Setting character category `<' for characters which should not be
88;; placed at end of line.
89(let* ((kinsoku-eol
90 (concat
91 ;; ASCII
92 "({[`"
a09244e6
KH
93 ;; Latin JISX0201
94 ;; See the comment above.
95 (let* ((str1 "({[`")
96 (len (length str1))
97 (idx 0)
98 (str2 "")
99 ch)
100 (while (< idx len)
101 (setq ch (make-char 'latin-jisx0201 (aref str1 idx))
102 str2 (concat str2 (char-to-string ch))
103 idx (1+ idx)))
104 str2)
105 ;; JISX0201 Katakana
106 "\e(I"\e(B"
4ed46869
KH
107 ;; Japanese JISX0208
108 "\e$B!F!H!J!L!N!P!R!T!V!X!Z!k!l!m!n!w!x\e(B\
109\e$A!.!0#"#(!2!4!6!8!:!<!>!c!d!e#@!f!l\e(B"
110 ;; Chinese GB2312
111 "\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\
522d8129 112\\e$(0!>!@!B!D!F!H!J!L!N!P!R!T!V!X!Z!\!^!`!b\e(B"
4ed46869
KH
113 ;; Chinese BIG5
114 "\e$(0!d!f!h!j!k!q!p"i"j"k"n"x$u$v$w$x$y$z${\e(B\
115\e$(0$|$}$~%!%"%#%$%%%&%'%(%)%*%+%:\e(B"))
116 (len (length kinsoku-eol))
117 (idx 0)
118 ch)
119 (while (< idx len)
4a027a0d
KH
120 (setq ch (aref kinsoku-eol idx)
121 idx (1+ idx))
4ed46869
KH
122 (modify-category-entry ch ?<)))
123
124;; Try to resolve `kinsoku' restriction by making the current line longer.
125(defun kinsoku-longer ()
acafeff7
KH
126 (let ((pos-and-column
127 (save-excursion
128 (forward-char 1)
522d8129 129 (while (and (not (eolp))
acafeff7
KH
130 (or (aref (char-category-set (following-char)) ?>)
131 ;; protect non-kinsoku words
132 (not (or (eq (preceding-char) ? )
133 (aref (char-category-set (preceding-char))
134 ?|)))))
135 (forward-char 1))
136 (cons (point) (current-column)))))
4ed46869
KH
137 (if (or (<= kinsoku-limit 0)
138 (< (cdr pos-and-column) (+ (current-fill-column) kinsoku-limit)))
139 (goto-char (car pos-and-column)))))
140
141;; Try to resolve `kinsoku' restriction by making the current line shorter.
a09244e6 142;; The line can't be broken before the buffer position LINEBEG.
4ed46869
KH
143(defun kinsoku-shorter (linebeg)
144 (let ((pos (save-excursion
145 (forward-char -1)
acafeff7
KH
146 (while (and
147 (< linebeg (point))
148 (or (aref (char-category-set (preceding-char)) ?<)
149 (aref (char-category-set (following-char)) ?>)
150 ;; protect non-kinsoku words
151 (not (or (eq (preceding-char) ? )
152 (aref (char-category-set (preceding-char))
153 ?|)))))
4ed46869
KH
154 (forward-char -1))
155 (point))))
156 (if (< linebeg pos)
157 (goto-char pos))))
158
159;;;###autoload
160(defun kinsoku (linebeg)
161 "Go to a line breaking position near point by doing `kinsoku' processing.
162LINEBEG is a buffer position we can't break a line before.
163
164`Kinsoku' processing is to prohibit specific characters to be placed
165at beginning of line or at end of line. Characters not to be placed
166at beginning and end of line have character category `>' and `<'
167respectively. This restriction is dissolved by making a line longer or
168shorter.
169
170`Kinsoku' is a Japanese word which originally means ordering to stay
171in one place, and is used for the text processing described above in
172the context of text formatting."
11e60d92
KH
173 (if enable-kinsoku
174 (if (or (and
175 ;; The character after point can't be placed at beginning
a1506d29 176 ;; of line.
11e60d92
KH
177 (aref (char-category-set (following-char)) ?>)
178 ;; We at first try to dissolve this situation by making a
179 ;; line longer. If it fails, then try making a line
180 ;; shorter.
181 (not (kinsoku-longer)))
182 ;; The character before point can't be placed at end of line.
183 (aref (char-category-set (preceding-char)) ?<))
184 (kinsoku-shorter linebeg))))
4ed46869 185
ab5796a9 186;;; arch-tag: e6b036bc-9e5b-4e9f-a22c-4ed04e37777e
60370d40 187;;; kinsoku.el ends here