aboutsummaryrefslogtreecommitdiff
path: root/modules/custom-counts.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-06-29 04:41:40 -0400
committerCraig Jennings <c@cjennings.net>2026-06-29 04:41:40 -0400
commit37d53dacf0636ae299b092b54dde78c0658a51c1 (patch)
tree6eb61c1a9220bbbf13bafc931476d87b61c5b27d /modules/custom-counts.el
parent256027866f9ff2cbc3b6155950f21a92b54ea307 (diff)
downloaddotemacs-37d53dacf0636ae299b092b54dde78c0658a51c1.tar.gz
dotemacs-37d53dacf0636ae299b092b54dde78c0658a51c1.zip
refactor: split custom-misc.el into focused modules
custom-misc.el was an incoherent grab-bag, so anything small defaulted to landing there. I split its eight commands by concern. Three moved into new modules: custom-format (region/buffer reformat), custom-counts (word and character counts), and custom-text-transform (fraction glyphs). The other three went to existing homes: the previous-buffer toggle to custom-buffer-file, the delimiter jump to custom-line-paragraph, and the align-regexp space advice with its alignment and fill bindings to custom-whitespace. The C-; bindings, which-key labels, and the six test files moved with their functions, and custom-misc.el is deleted. No behavior change: every command keeps its name and its C-; key.
Diffstat (limited to 'modules/custom-counts.el')
-rw-r--r--modules/custom-counts.el63
1 files changed, 63 insertions, 0 deletions
diff --git a/modules/custom-counts.el b/modules/custom-counts.el
new file mode 100644
index 000000000..792732a40
--- /dev/null
+++ b/modules/custom-counts.el
@@ -0,0 +1,63 @@
+;;; custom-counts.el --- Word and character counts -*- coding: utf-8; lexical-binding: t; -*-
+
+;;; Commentary:
+;;
+;; Layer: 2 (Core UX).
+;; Category: L.
+;; Load shape: eager.
+;; Eager reason: registers its C-; # w and C-; # c command bindings at load.
+;; Top-level side effects: binds the count commands under C-; # w and C-; # c.
+;; Runtime requires: keybindings.
+;; Direct test load: yes (requires keybindings explicitly).
+;;
+;; Count words or characters in the active region, or the whole buffer when no
+;; region is active, and report the total in the minibuffer. Split out of the
+;; former custom-misc.el grab-bag.
+
+;;; Code:
+
+(require 'keybindings) ;; provides cj/register-command
+
+(defun cj/--count-words (start end)
+ "Internal implementation: Count words between START and END.
+START and END define the region to count.
+Returns the word count as an integer."
+ (when (> start end)
+ (error "Invalid region: start (%d) is greater than end (%d)" start end))
+ (count-words start end))
+
+(defun cj/count-words-buffer-or-region ()
+ "Count the number of words in the buffer or region.
+Display the result in the minibuffer."
+ (interactive)
+ (let* ((use-region (use-region-p))
+ (begin (if use-region (region-beginning) (point-min)))
+ (end (if use-region (region-end) (point-max)))
+ (area-type (if use-region "the region" "the buffer"))
+ (word-count (cj/--count-words begin end)))
+ (message "There are %d words in %s." word-count area-type)))
+
+(defun cj/--count-characters (start end)
+ "Internal implementation: Count characters between START and END.
+START and END define the region to count.
+Returns the character count as an integer."
+ (when (> start end)
+ (error "Invalid region: start (%d) is greater than end (%d)" start end))
+ (- end start))
+
+(defun cj/count-characters-buffer-or-region ()
+ "Count the number of characters in the buffer or region.
+Display the result in the minibuffer."
+ (interactive)
+ (let* ((use-region (use-region-p))
+ (begin (if use-region (region-beginning) (point-min)))
+ (end (if use-region (region-end) (point-max)))
+ (area-type (if use-region "the region" "the buffer"))
+ (char-count (cj/--count-characters begin end)))
+ (message "There are %d characters in %s." char-count area-type)))
+
+(cj/register-command "# w" #'cj/count-words-buffer-or-region "count words")
+(cj/register-command "# c" #'cj/count-characters-buffer-or-region "count characters")
+
+(provide 'custom-counts)
+;;; custom-counts.el ends here