;;; lisp file for xyzzy ver0.2.2.233
;;; count-char.l ver0.0.0.1 // crayonzen [2026-03-24]15:47:02(+0900)

;// how-to: load
;//       : write in .xyzzy or siteinit.l (cut a semicolon ;)
;(load-library "count-char")

(provide "count-char")

(in-package "editor")

(export '(count-char
	  ))

(defun count-char ()
  (interactive)
  (if (get-selection-type)
      ;選択範囲があれば narrowing して count-matches 実行
      (selection-start-end (start end)
	(narrow-to-region start end)
	(goto-char (point-min))

	(setq total (count-matches "."));全体
	(setq 1char (count-matches "[\x00-\x7f]"));半角（半角空白、改行、タブ、含む）
	(setq 2char (count-matches "[^\x00-\x7f]"));全角
	(setq nchar (count-matches "[\n]"));改行
	(setq tchar (count-matches "[\t]"));タブ
	(setq HSchar (count-matches "[ ]"));半角空白
	(setq FSchar (count-matches "[　]"));全角空白

	(setq 1char2 ( - 1char nchar tchar HSchar FSchar ));半角文字（表示のみ
	(setq 2char2 ( - 2char FSchar ));全角文字（表示のみ

	(selection-end-of-buffer)
	(widen))
    
    ;選択範囲 がなければ全体
    (save-excursion
      (goto-char (point-min))
      (setq total (count-matches "."));全体
      (setq 1char (count-matches "[\x00-\x7f]"));半角（半角空白、改行、タブ、含む）
      (setq 2char (count-matches "[^\x00-\x7f]"));全角
      (setq nchar (count-matches "[\n]"));改行
      (setq tchar (count-matches "[\t]"));タブ
      (setq HSchar (count-matches "[ ]"));半角空白
      (setq FSchar (count-matches "[　]"));全角空白

      (setq 1char2 ( - 1char nchar tchar HSchar FSchar ));半角文字（表示のみ
      (setq 2char2 ( - 2char FSchar ));全角文字（表示のみ
      ))
  
  (message "ALL:~d ( 1byte:~d/~d  2byte:~d/~d ) CRLF:~d  TAB:~d  HSpace:~d  FSpace:~d" total 1char2 1char 2char2 2char nchar tchar HSchar FSchar)
  )

(add-hook '*init-app-menus-hook*
	  #'(lambda ()
	      (when (and *app-menu*
			 (setq menu (get-menu *app-menu* 'ed::edit)))
		(add-menu-separator menu 'count-chart-sep)
		(add-menu-item menu 'count-char-tag
			       "文字数(&W)" 'count-char)
		)))
