From 2a7c0e542a0bdba9aa0df4618357f60932bffcf2 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Wed, 1 Jan 2025 11:19:01 +0800 Subject: count: make value buffer-local, font-changing commands. * count.el (count-mode-value): Make this variable buffer-local, so that we can have different buffers count different values. (counting-font-step): Add a variable that controls the font-size scaling factor within the counting buffers. I shall consider if I need to make this variable buffer-local as well. (count-mode, count-refresh): Refactor some logic into the function `count-refresh`. (count-mode-map): Add bindings for font-size changing commands. (count-make-pad-string): Account for the font-size scaling variable in the calculation of the size of the numbers to be centered. (count-enlarge, count-shrink): Commands for changing the font-scaling variable, so as to achieve the effect of enlarging and shrinking the texts within the counting buffers. --- count.el | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) (limited to 'count.el') diff --git a/count.el b/count.el index 0b20005..4b2e25d 100644 --- a/count.el +++ b/count.el @@ -27,20 +27,26 @@ (defvar counting-buffer-name "*Count*" "The name of the buffer within which to count.") -(defvar count-mode-value 0 +(defvar counting-font-step 10 + "The proportion by which the counting value font size is +increased.") + +(defvar-local count-mode-value 0 "The value of the current count.") (define-derived-mode count-mode special-mode "Count" "Major mode for counting." - (setq text-scale-mode-amount 10) - (text-scale-mode 1) (count-refresh) (set 'cursor-type nil)) -(define-key count-mode-map (vector 'up) #'count-up) -(define-key count-mode-map (vector 'down) #'count-down) -(define-key count-mode-map (vector ?r) #'count-refresh) -(define-key count-mode-map (vector ?o) #'count-reset) +(let ((map count-mode-map)) + (define-key map (vector 'up) #'count-up) + (define-key map (vector 'down) #'count-down) + (define-key map (vector ?r) #'count-refresh) + (define-key map (vector ?o) #'count-reset) + (define-key map (vector ?=) #'count-enlarge) + (define-key map (vector ?-) #'count-shrink)) + (defun count-start-or-switch () "Start counting in an old buffer if possible." @@ -65,7 +71,9 @@ (let* ((number-str (format "%d" number)) (number-str-width (* (string-pixel-width number-str) ;; account for the remapping - (expt 1.2 10))) + (expt + text-scale-mode-step + counting-font-step))) (space-width (string-pixel-width (string #x20))) (padding (/ (- (window-pixel-width) (* 2 space-width) number-str-width) @@ -85,6 +93,8 @@ (cond ((derived-mode-p 'count-mode) (let ((inhibit-read-only t)) + (setq text-scale-mode-amount counting-font-step) + (text-scale-mode 1) (delete-region (point-min) (point-max)) (insert (count-make-pad-string count-mode-value)))) ((user-error @@ -110,5 +120,21 @@ (setq count-mode-value 0) (count-refresh)) +(defun count-enlarge (&optional arg) + "Increase the font size by ARG steps. +The size of one step is defined by `text-scale-mode-step'." + (interactive "p") + (setq arg (cond ((null arg) 1) ((prefix-numeric-value arg)))) + (setq counting-font-step (+ counting-font-step arg)) + (count-refresh)) + +(defun count-shrink (&optional arg) + "Decrease the font size by ARG steps. +The size of one step is defined by `text-scale-mode-step'." + (interactive "p") + (setq arg (cond ((null arg) 1) ((prefix-numeric-value arg)))) + (setq counting-font-step (- counting-font-step arg)) + (count-refresh)) + (provide 'count) ;;; count.el ends here -- cgit v1.2.3-18-g5258