;;; lisp file for xyzzy ver0.2.2.234
;;; mend-encoding.l ver0.1.1.1 // for text-mode or else mode // crayonzen [2023-03-16]21:48:06(+0900)

(provide "mend-encoding")

(in-package "editor")

(export '(*mend-encoding-popup-menu*
		  *mend-encoding-menu*
		  *mend-encoding*
		  *mend-eol*
		  mend-buffer-ANSI
		  mend-buffer-UTF-16LE-BOM
		  mend-buffer-UTF-16BE-BOM
		  mend-buffer-UTF-8
		  mend-buffer-UTF-8-BOM
		  mend-buffer-Auto
		  mend-buffer-JIS
		  mend-buffer-EUC-JP
		  mend-buffer-Binary
		  mend-eol-lf
		  mend-eol-crlf
		  mend-eol-cr
		  mend-encoding-eol
		  init-mend-encoding-menu
		  ))

(defvar *mend-encoding-popup-menu* nil)
(defvar *mend-encoding-menu* nil)

(defvar *mend-encoding* nil)
(defvar *mend-eol* nil)

;;エンコーディング文字コード
(defun mend-buffer-ANSI ()
  (interactive)
  (setq *mend-encoding* *encoding-sjis*)
  (setq buffer (selected-buffer))
  (mend-encoding-eol buffer *encoding-sjis* *mend-eol*)
  )

(defun mend-buffer-UTF-16LE-BOM ()
  (interactive)
  (setq *mend-encoding* *encoding-utf16le-bom*)
  (setq buffer (selected-buffer))
  (mend-encoding-eol buffer *encoding-utf16le-bom* *mend-eol*)
  )

(defun mend-buffer-UTF-16BE-BOM ()
  (interactive)
  (setq *mend-encoding* *encoding-utf16be-bom*)
  (setq buffer (selected-buffer))
  (mend-encoding-eol buffer *encoding-utf16be-bom* *mend-eol*)
  )

(defun mend-buffer-UTF-8 ()
  (interactive)
  (setq *mend-encoding* *encoding-utf8n*)
  (setq buffer (selected-buffer))
  (mend-encoding-eol buffer *encoding-utf8n* *mend-eol*)
  )

(defun mend-buffer-UTF-8-BOM ()
  (interactive)
  (setq *mend-encoding* *encoding-utf8*)
  (setq buffer (selected-buffer))
  (mend-encoding-eol buffer *encoding-utf8* *mend-eol*)
  )

(defun mend-buffer-Auto ()
  (interactive)
  (setq *mend-encoding* *encoding-auto*)
  (setq buffer (selected-buffer))
  (mend-encoding-eol buffer *encoding-auto* *mend-eol*)
  )

(defun mend-buffer-JIS ()
  (interactive)
  (setq *mend-encoding* *encoding-jis*)
  (setq buffer (selected-buffer))
  (mend-encoding-eol buffer *encoding-jis* *mend-eol*)
  )

(defun mend-buffer-EUC-JP ()
  (interactive)
  (setq *mend-encoding* *encoding-euc-jp*)
  (setq buffer (selected-buffer))
  (mend-encoding-eol buffer *encoding-euc-jp* *mend-eol*)
  )

(defun mend-buffer-Binary ()
  (interactive)
  (setq *mend-encoding* *encoding-binary*)
  (setq buffer (selected-buffer))
  (mend-encoding-eol buffer *encoding-binary* *mend-eol*)
  )

;;EOL改行コード
(defun mend-eol-lf (&optional buffer)
  (interactive)
  (setq *mend-eol* *eol-lf*)
  (or buffer (setq buffer (selected-buffer)))
  (mend-encoding-eol buffer *mend-encoding* *eol-lf*)
  )

(defun mend-eol-crlf (&optional buffer)
  (interactive)
  (setq *mend-eol* *eol-crlf*)
  (or buffer (setq buffer (selected-buffer)))
  (mend-encoding-eol buffer *mend-encoding* *eol-crlf*)
  )

(defun mend-eol-cr (&optional buffer)
  (interactive)
  (setq *mend-eol* *eol-cr*)
  (or buffer (setq buffer (selected-buffer)))
  (mend-encoding-eol buffer *mend-encoding* *eol-cr*)
  )

(defun mend-encoding-eol (&optional buffer encoding eol-code)
  (or buffer (setq buffer (selected-buffer)))
  (or encoding *mend-encoding* (setq encoding *expected-fileio-encoding*))
  (or eol-code *mend-eol* (setq eol-code *expected-eol-code*))
  
  ;;save-excursionしてもダメなんだっけ？
  (set-buffer buffer);;カレント対象（現在の）だが複数用に残す
  ;;(set-buffer-fileio-encoding encoding);;自動選択でエラー。自動を外すか、判断も欲しいのでコメントアウト
  ;;(set-buffer-eol-code eol-code);下記のletに追加。バイナリは？
  ;;(save-buffer)

  ;;指定の文字コード改行コードで読み直す
  (let ((*expected-fileio-encoding* encoding)
		(*expected-eol-code* eol-code))
	(declare (special *expected-fileio-encoding* *expected-eol-code*))
	(revert-buffer))
  )

;;メニュー
(defun init-mend-encoding-menu ()

  (or *mend-encoding-popup-menu*
      (setq *mend-encoding-popup-menu* (create-popup-menu ':mend-encoding-popup)))
  (or *mend-encoding-menu*
	  (setq *mend-encoding-menu* (create-popup-menu ':mend-encoding)))

  ;;クリップボードエンコーディングのリストを流用
  ;;メインの (setq *mend-encoding-menu* より頭にもってきて作っておく
  (setq *mend-encoding-popup-menu*
		(make-char-encoding-popup-menu
		 #'(lambda (encoding)
			 (interactive)
			 (continue-pre-selection)
			 (revert-buffer encoding)
			 (setq *mend-encoding-popup-menu* encoding)
			 )
		 #'(lambda (encoding)
			 (if (or (equal (buffer-fileio-encoding (selected-buffer)) encoding)
					 (eq *mend-encoding-popup-menu* encoding)) :check)
			 )
		 *clipboard-char-encoding-list*))

  (setq *mend-encoding-menu*
		(define-popup-menu
		  (:item :tag-command "ANSI [Shift_JIS](&S)"
		   'mend-buffer-ANSI
		   #'(lambda () (if (equal (buffer-fileio-encoding (selected-buffer)) *encoding-sjis*) :check)))
		  (:item :tag-command "UTF-16LE-BOM(&L)"
		   'mend-buffer-UTF-16LE-BOM
		   #'(lambda () (if (equal (buffer-fileio-encoding (selected-buffer)) *encoding-utf16le-bom*) :check)))
		  (:item :tag-command "UTF-16BE-BOM(&B)"
		   'mend-buffer-UTF-16BE-BOM
		   #'(lambda () (if (equal (buffer-fileio-encoding (selected-buffer)) *encoding-utf16be-bom*) :check)))
		  (:item :tag-command "UTF-8(&U)"
		   'mend-buffer-UTF-8
		   #'(lambda () (if (equal (buffer-fileio-encoding (selected-buffer)) *encoding-utf8n*) :check)))
		  (:item :tag-command "UTF-8-BOM(&I)"
		   'mend-buffer-UTF-8-BOM
		   #'(lambda () (if (equal (buffer-fileio-encoding (selected-buffer)) *encoding-utf8*) :check)))
		  :sep
		  (:item :tag-command "■自動(&A)"
		   'mend-buffer-Auto)
		  :sep
		  (:item :tag-command "JIS(&J)"
		   'mend-buffer-JIS
		   #'(lambda () (if (equal (buffer-fileio-encoding (selected-buffer)) *encoding-jis*) :check)))
		  (:item :tag-command "EUC-JP(&C)"
		   'mend-buffer-EUC-JP
		   #'(lambda () (if (equal (buffer-fileio-encoding (selected-buffer)) *encoding-euc-jp*) :check)))
		  (:item :tag-command "Binary(&Z)"
		   'mend-buffer-Binary
		   #'(lambda () (if (equal (buffer-fileio-encoding (selected-buffer)) *encoding-binary*) :check)))
		  (:menu nil "その他エンコーディング(&M)"
		   *mend-encoding-popup-menu*)
		  :sep
		  (:item nil "＜改行＞" nil)
		  ;;(:item nil "＜改行＞" 'dummy)
		  (:item :tag-command "LF [linux,macx](&Q)"
		   'mend-eol-lf
		   #'(lambda () (if (equal (buffer-eol-code (selected-buffer)) *eol-lf*) :check)))
		  (:item :tag-command "CRLF [win](&W)"
		   'mend-eol-crlf
		   #'(lambda () (if (equal (buffer-eol-code (selected-buffer)) *eol-crlf*) :check)))
		  (:item :tag-command "CR [mac9](&E)"
		   'mend-eol-cr
		   #'(lambda () (if (equal (buffer-eol-code (selected-buffer)) *eol-cr*) :check)))
		  )
		)
  )



;;ファイルのポップアップ「開く」の下に追加
(add-hook '*init-app-menus-hook*
		  #'(lambda ()
			  (if (init-mend-encoding-menu)
				  (if (setq ip (get-menu-position *app-menu* 'ed::open-file-dialog))
					  (progn
						(setq ip2 (+ ip 1))
						(let ((file (get-menu *app-menu* 'ed::file)))
						  (insert-popup-menu file ip2 *mend-encoding-menu* "開き直す(&M)")
						  (insert-menu-separator file (+ ip2 1))
						  )
						)
					(let ((file (get-menu *app-menu* 'ed::file)))
					  (insert-popup-menu file 2 *mend-encoding-menu* "開き直す(&M)"))
					)
				)
			  ))

#|
;;ファイルのポップアップに追加
(add-hook '*init-app-menus-hook*
		  #'(lambda ()
			  (if (init-mend-encoding-menu)
				  (let ((file (get-menu *app-menu* 'ed::file)))
					(insert-popup-menu file 2 *mend-encoding-menu* "開き直す(&M)")
					(insert-menu-separator file 3))
				)))
|#

#|
;;メニューバーに追加
(add-hook '*init-app-menus-hook*
		  #'(lambda ()
			  (if (init-mend-encoding-menu)
				  (insert-popup-menu *app-menu* 2 *mend-encoding-menu* "開き直す(&M)")
				)))
|#

#|
(add-hook '*init-app-menus-hook*
		  #'(lambda ()
			  (if (init-mend-encoding-menu)
				  (if (setq ip (get-menu-position *app-menu* 'ed::open-file-dialog))
					  (insert-popup-menu *app-menu* ip *mend-encoding-menu* "開き直す(&M)")
					(add-popup-menu *app-menu* *mend-encoding-menu* "開き直す(&M)")
					)
				)
			  ))
|#

#|
(add-hook 'editor::*init-app-menus-hook*
		  #'(lambda ()
			  (let ((file (get-menu *app-menu* 'ed::file)))
				(insert-popup-menu file 2 *mend-encoding-menu* "開き直す(&M)")
				(insert-menu-separator file 3)
				)))
|#

(global-set-key '(#\C-c #\m) #'(lambda () (interactive)
				 (track-popup-menu *mend-encoding-menu*)))
