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