kinsoku.el 6.2 KB

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