emacs-在nano-modeline右下角添加当前buffer中英文字数统计
1. 加载第三方包
(require 'advance-words-count)
https://github.com/LdBeth/advance-words-count.el
这个包提供了CJK字符、英文单词及其他字符的统计功能。
2. 覆盖右下角数据(旧)
(defun nano-modeline-cursor-position (&optional format)
"Cursor position using given FORMAT."
(let ((format (or format "%l:%c ")))
(propertize (format-mode-line '( (:eval (format "%s字" (+ (words-count (point-min) (point-max) words-count-rule-ansci) (words-count (point-min) (point-max) words-count-rule-CJK)))) " "))
'face (nano-modeline-face 'secondary))
))
其中 words-count-rule-ansci
统计英文字符; words-count-rule-CJK
统计CJK字符。
3. 覆盖右下角数据(新)
(defvar my/buffer-words 0
"The number of characters in the current buffer.")
;;变成buffer-local变量,确保另一个buffer的modeline不会受到影响
(make-variable-buffer-local 'my/buffer-words)
(defun my/buffer-words-count ()
"Return the number of characters in the current buffer."
(setq my/buffer-words
(+ (words-count (point-min) (point-max) words-count-rule-CJK)
(words-count (point-min) (point-max) words-count-rule-ansci )))
(force-mode-line-update))
;;窗口切换后
(add-hook 'window-configuration-change-hook 'my/buffer-words-count)
;; buffer调整后
(add-hook 'buffer-list-update-hook 'my/buffer-words-count)
;; 空闲时(针对scratch)
(run-with-idle-timer 2 t 'my/buffer-words-count)
(defun nano-modeline-cursor-position (&optional format)
"Cursor position using given FORMAT."
(let ((format (or format "%l:%c ")))
;; (propertize (format-mode-line '(" "))
;; 'face (nano-modeline-face 'secondary))
(propertize (format-mode-line '((:eval (format "%s字" my/buffer-words
)) " "))
'face (nano-modeline-face 'secondary))
))
通过buffer-local的变量异步更新当前buffer数值。