* frames.texi (Pop-Up Menus): Document where menu title comes from (Bug#7684).
[emacs.git] / lisp / international / kinsoku.el
blob835f991957b434f472f8e7743c3ca26602f81eaf
1 ;;; kinsoku.el --- `Kinsoku' processing funcs -*- coding: iso-2022-7bit; -*-
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
10 ;; Keywords: mule, kinsoku
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
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.
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.
39 ;;; Code:
41 (defvar kinsoku-limit 4
42 "How many more columns we can make lines longer by `kinsoku' processing.
43 The value 0 means there's no limitation.")
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 "!)-_~}]:;',.?"
51 ;; Latin JISX0201
52 ;; Instead of putting Latin JISX0201 string directly, we
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"
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\
77 \e$(0!M!O!Q!S!U!W!Y![!]!_!a!c!e!g!i!k!q\e(B\
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)
83 (setq ch (aref kinsoku-bol idx)
84 idx (1+ idx))
85 (modify-category-entry ch ?>)))
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 "({[`"
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"
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\
112 \\e$(0!>!@!B!D!F!H!J!L!N!P!R!T!V!X!Z!\!^!`!b\e(B"
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)
119 (while (< idx len)
120 (setq ch (aref kinsoku-eol idx)
121 idx (1+ idx))
122 (modify-category-entry ch ?<)))
124 ;; Try to resolve `kinsoku' restriction by making the current line longer.
125 (defun kinsoku-longer ()
126 (let ((pos-and-column
127 (save-excursion
128 (forward-char 1)
129 (while (and (not (eolp))
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)))))
137 (if (or (<= kinsoku-limit 0)
138 (< (cdr pos-and-column) (+ (current-fill-column) kinsoku-limit)))
139 (goto-char (car pos-and-column)))))
141 ;; Try to resolve `kinsoku' restriction by making the current line shorter.
142 ;; The line can't be broken before the buffer position LINEBEG.
143 (defun kinsoku-shorter (linebeg)
144 (let ((pos (save-excursion
145 (forward-char -1)
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 ?|)))))
154 (forward-char -1))
155 (point))))
156 (if (< linebeg pos)
157 (goto-char pos))))
159 ;;;###autoload
160 (defun kinsoku (linebeg)
161 "Go to a line breaking position near point by doing `kinsoku' processing.
162 LINEBEG is a buffer position we can't break a line before.
164 `Kinsoku' processing is to prohibit specific characters to be placed
165 at beginning of line or at end of line. Characters not to be placed
166 at beginning and end of line have character category `>' and `<'
167 respectively. This restriction is dissolved by making a line longer or
168 shorter.
170 `Kinsoku' is a Japanese word which originally means ordering to stay
171 in one place, and is used for the text processing described above in
172 the context of text formatting."
173 (if enable-kinsoku
174 (if (or (and
175 ;; The character after point can't be placed at beginning
176 ;; of line.
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))))
186 ;; arch-tag: e6b036bc-9e5b-4e9f-a22c-4ed04e37777e
187 ;;; kinsoku.el ends here