;; Kinsoku shori for Egg
;; Coded by S.Tomura, Electrotechnical Lab. (tomura@etl.go.jp)

;; This file is part of Egg on Nemacs (Japanese Environment)

;; Egg is distributed in the forms of patches to GNU
;; Emacs under the terms of the GNU EMACS GENERAL PUBLIC
;; LICENSE which is distributed along with GNU Emacs by the
;; Free Software Foundation.

;; Egg is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.  See the GNU EMACS GENERAL PUBLIC LICENSE for
;; more details.

;; You should have received a copy of the GNU EMACS GENERAL
;; PUBLIC LICENSE along with Nemacs; see the file COPYING.
;; If not, write to the Free Software Foundation, 675 Mass
;; Ave, Cambridge, MA 02139, USA.

;;; Nemacs 3.2 created by S. Tomura 89-Nov-15
;;; Ver. 3.2  3.2 対応に変更
;;; Nemacs 3.0 created by S. Tomura 89-Mar-17
;;; Ver. 2.1a modified by S. Tomura 88-Nov-17
;;;           wordの途中で分割しないように修正した。
;;; Ver. 2.1  modified by S. Tomura 88-Jun-24
;;;           kinsoku-shori moves the point <= fill-column + kinsoku-nobashi
;;; Nemacs V.2.1
;;; Ver. 1.1  modified by S. Tomura 88-Feb-29
;;;           Bug fix:  regexp-quote is used.
;;; Ver. 1.0  Created by S. Tomura
;;;           禁則処理機能を提供する。
;;;

(defvar kanji-kinsoku-version "3.21")
;;; Last modified date: Wed Nov 15 11:59:00 1989

;;; The followings must be merged into kanji.el
;;; patched by S.Tomura 87-Dec-7
;;;    JIS codeの特殊文字の一覧表です。（戸村）
;;;;     "　、。，．・：：；？！゛゜´｀¨"
;;;;   "＾￣＿ヽヾゝゞ〃仝々〆〇ー―‐／"
;;;;   "＼〜‖｜…‥‘’“”（）〔〕［］"
;;;;   "｛｝〈〉《》「」『』【】＋−±×"
;;;;   "÷＝≠＜＞≦≧∞∴♂♀°′″℃￥"
;;;;   "＄¢£％＃＆＊＠§☆★○●◎◇"
;;;;     "◆□■△▲▽▼※〒→←↑↓〓 "
;;;;     "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟ"
;;;;   "ΠΡΣΤΥΦΧΨΩ"
;;;;     "αβγδεζηθικλμνξο"
;;;;   "πρστυφχψω"
;;;;     "АБВГДЕЁЖЗИЙКЛМН"
;;;;   "ОПРСТУФХЦЧШЩЪЫЬЭ"
;;;;   "ЮЯ"
;;;;     "абвгдеёжзийклмн”
;;;;   "опрстуфхцчшщъыьэ"
;;;;   "юя"
;;;;    ０１２３４５６７８９ＡＢＣＤＥＦ
;;;;   "ぁぃぅぇぉっゃゅょゎ"
;;;;   "ァィゥェォッャュョヮヵヶ"

(defvar kinsoku-bol-chars 
  (concat  "!)-_~}]:;',.?"
	   "、。，．・：；？！゛゜´｀¨＾￣＿ヽヾゝゞ〃仝々〆〇ー―‐"
	   "／＼〜‖｜…‥’”）〕］｝〉》」』】°′″℃"
	   "ぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵヶ"
	   )
  "行頭禁則を行なう文字を指定する。
（EUCのコードの文字列を使用する。）"
   )

(defvar  kinsoku-eol-chars
   "({[‘“（〔［｛〈《「『【°′″℃＠§"
  "行末禁則を行なう文字を指定する。
（EUCのコードの文字列を使用する。）"
  )

;;;
;;; Buffers for kinsoku-shori
;;;
(defvar $kinsoku-buff1$ " "  "禁則処理のための半角文字用作業領域")
(defvar $kinsoku-buff2$ "  " "禁則処理のための全角文字用作業領域")

(defun kinsoku-bol-p ()
  "pointで改行すると行頭禁則に触れるかどうかをかえす。
行頭禁則文字はkinsoku-bol-charsで指定する。"
  (string-match "" "") ;;;これはregex compのリセットです。
  (string-match 
   (if (<= 128 (following-char)) 
       (progn
	 (aset $kinsoku-buff2$ 0 (following-char))
	 (aset $kinsoku-buff2$ 1 (char-after (1+ (point))))
	 $kinsoku-buff2$)
     (progn
       (aset $kinsoku-buff1$ 0 (following-char))
       (regexp-quote $kinsoku-buff1$)))
   kinsoku-bol-chars))

(defun kinsoku-eol-p ()
    "pointで改行すると行末禁則に触れるかどうかをかえす。
行末禁則文字はkinsoku-eol-charsで指定する。"
    (string-match "" "") ;;;これはregex compのリセットです。
    (string-match
     (if (<= 128 (preceding-char))
	 (progn
	   (aset $kinsoku-buff2$ 0 (char-after (- (point) 2)))
	   (aset $kinsoku-buff2$ 1 (preceding-char))
	   $kinsoku-buff2$)
       (progn
	(aset $kinsoku-buff1$ 0 (preceding-char))
	(regexp-quote $kinsoku-buff1$)))
     kinsoku-eol-chars))

(defvar kinsoku-nobashi-limit nil
  "禁則処理で行を伸ばして良い半角文字数を指定する。
非負整数以外の場合は無限大を意味する。")

(defun kinsoku-shori ()
  "禁則に触れない点へ移動する。
pointが行頭禁則に触れる場合は行を伸ばして、禁則に触れない点を探す。
pointが行末禁則に触れる場合は行を縮めて、禁則に触れない点を探す。
ただし、行伸ばし半角文字数がkinsoku-nobashi-limitを越えると、
行を縮めて禁則に触れない点を探す。"

  (let ((bol-kin nil) (eol-kin nil))
    (if (and (not (bolp))
	     (not (eolp))
	     (or (setq bol-kin (kinsoku-bol-p))
		 (setq eol-kin (kinsoku-eol-p))))
	(cond(bol-kin (kinsoku-shori-nobashi))
	     (eol-kin (kinsoku-shori-chizime))))))

(defun kinsoku-shori-nobashi ()
  "行を伸ばして禁則に触れない点へ移動する。"
  (let ((max-column (+ fill-column 
		       (if (and (numberp kinsoku-nobashi-limit)
				(>= kinsoku-nobashi-limit 0))
			   kinsoku-nobashi-limit
			 10000)))) ;;; 10000は無限大のつもりです。
    (while (and (<= (+ (current-column)
		       (if (<= 128 (following-char)) 1 2))
		    max-column)
		(not (bolp))
		(not (eolp))
		(or (kinsoku-eol-p)
		    (kinsoku-bol-p)
	            ;;; English word の途中では分割しない。
		    (and (= ?w (char-syntax (preceding-char)))
			 (= ?w (char-syntax (following-char))))))
      (forward-char))
    (if (or (kinsoku-eol-p) (kinsoku-bol-p))
	(kinsoku-shori-chizime))))

(defun kinsoku-shori-chizime ()
  "行を縮めて禁則に触れない点へ移動する。"
    (while (and (not (bolp))
		(not (eolp))
		(or (kinsoku-bol-p)
		    (kinsoku-eol-p)
		;;; word の途中では分割しない。
		    (and (= ?w (char-syntax (preceding-char)))
			 (= ?w (char-syntax (following-char))))))
      (backward-char)))


