* calendar/todo-mode.el (todo-set-top-priorities): Fix logic to
[bpt/emacs.git] / lisp / international / kinsoku.el
CommitLineData
60370d40 1;;; kinsoku.el --- `Kinsoku' processing funcs -*- coding: iso-2022-7bit; -*-
4ed46869 2
ba318903 3;; Copyright (C) 1997, 2001-2014 Free Software Foundation, Inc.
7976eda0 4;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5df4f04c 5;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
2fd125a3
KH
6;; National Institute of Advanced Industrial Science and Technology (AIST)
7;; Registration Number H14PRO021
4ed46869 8
fa526c4a 9;; Keywords: mule, kinsoku
4ed46869
KH
10
11;; This file is part of GNU Emacs.
12
4936186e 13;; GNU Emacs is free software: you can redistribute it and/or modify
4ed46869 14;; it under the terms of the GNU General Public License as published by
4936186e
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
4ed46869
KH
17
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.
22
23;; You should have received a copy of the GNU General Public License
4936186e 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
4ed46869
KH
25
26;;; Commentary:
27
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.
33;;
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.
37
38;;; Code:
39
40(defvar kinsoku-limit 4
41 "How many more columns we can make lines longer by `kinsoku' processing.
42The value 0 means there's no limitation.")
43
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 "!)-_~}]:;',.?"
a09244e6 50 ;; Latin JISX0201
e8dd0160 51 ;; Instead of putting Latin JISX0201 string directly, we
a09244e6
KH
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"
4ed46869
KH
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\
4c0f6426 76\e$(0!M!O!Q!S!U!W!Y![!]!_!a!c!e!g!i!k!q\e(B\
4ed46869
KH
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)
4a027a0d
KH
82 (setq ch (aref kinsoku-bol idx)
83 idx (1+ idx))
4ed46869
KH
84 (modify-category-entry ch ?>)))
85
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 "({[`"
a09244e6
KH
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"
4ed46869
KH
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\
522d8129 111\\e$(0!>!@!B!D!F!H!J!L!N!P!R!T!V!X!Z!\!^!`!b\e(B"
4ed46869
KH
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)
117 ch)
118 (while (< idx len)
4a027a0d
KH
119 (setq ch (aref kinsoku-eol idx)
120 idx (1+ idx))
4ed46869
KH
121 (modify-category-entry ch ?<)))
122
123;; Try to resolve `kinsoku' restriction by making the current line longer.
124(defun kinsoku-longer ()
acafeff7
KH
125 (let ((pos-and-column
126 (save-excursion
127 (forward-char 1)
522d8129 128 (while (and (not (eolp))
acafeff7
KH
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)))))
4ed46869
KH
136 (if (or (<= kinsoku-limit 0)
137 (< (cdr pos-and-column) (+ (current-fill-column) kinsoku-limit)))
138 (goto-char (car pos-and-column)))))
139
140;; Try to resolve `kinsoku' restriction by making the current line shorter.
a09244e6 141;; The line can't be broken before the buffer position LINEBEG.
4ed46869
KH
142(defun kinsoku-shorter (linebeg)
143 (let ((pos (save-excursion
144 (forward-char -1)
acafeff7
KH
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 ?|)))))
4ed46869
KH
153 (forward-char -1))
154 (point))))
155 (if (< linebeg pos)
156 (goto-char pos))))
157
158;;;###autoload
159(defun kinsoku (linebeg)
160 "Go to a line breaking position near point by doing `kinsoku' processing.
161LINEBEG is a buffer position we can't break a line before.
162
163`Kinsoku' processing is to prohibit specific characters to be placed
164at beginning of line or at end of line. Characters not to be placed
165at beginning and end of line have character category `>' and `<'
166respectively. This restriction is dissolved by making a line longer or
167shorter.
168
169`Kinsoku' is a Japanese word which originally means ordering to stay
170in one place, and is used for the text processing described above in
171the context of text formatting."
11e60d92
KH
172 (if enable-kinsoku
173 (if (or (and
174 ;; The character after point can't be placed at beginning
a1506d29 175 ;; of line.
11e60d92
KH
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))))
4ed46869 184
60370d40 185;;; kinsoku.el ends here