diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-24 07:26:20 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-24 07:26:20 -0500 |
| commit | a7cc89482cc931f60d4b43d5c7b875542cbe2538 (patch) | |
| tree | 8f313e84bfcefd4ed4f83e36aa9c4e8b25c180db /modules | |
| parent | 9135298c45f2f03ab92903edb242c8f5f94396d5 (diff) | |
| download | dotemacs-a7cc89482cc931f60d4b43d5c7b875542cbe2538.tar.gz dotemacs-a7cc89482cc931f60d4b43d5c7b875542cbe2538.zip | |
refactor(text-enclose): extract shared region-or-buffer bounds helper
The append/prepend/indent/dedent *-in-region-or-buffer commands each inlined the same (if (use-region-p) (region-beginning) (point-min)) / (region-end)/(point-max) block — four copies of the "operate on the region, else the whole buffer" contract. Extracted cj/--region-or-buffer-bounds as the single source of that decision and routed all four through it. Behavior is unchanged; the public-wrapper tests still pass.
This was the "extract a shared helper that decides the target range" option from the reconcile task. The sibling custom-ordering.el helpers (cj/--arrayify, cj/--unarrayify) already document an explicit (start end) contract accurately and are region-required by design, so they needed no docstring change — each pair now has one clear, consistent contract. Tests cover the helper for the region case, the no-region whole-buffer case, and an empty buffer.
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/custom-text-enclose.el | 45 |
1 files changed, 21 insertions, 24 deletions
diff --git a/modules/custom-text-enclose.el b/modules/custom-text-enclose.el index ebfb44e9..e1b735a0 100644 --- a/modules/custom-text-enclose.el +++ b/modules/custom-text-enclose.el @@ -132,15 +132,21 @@ Returns the transformed string without modifying the buffer." (mapconcat (lambda (line) (concat line suffix)) lines-to-process "\n") (if has-trailing-newline "\n" "")))) +(defun cj/--region-or-buffer-bounds () + "Return a cons (START . END) for the active region, else the whole buffer. +The single source of the region-or-buffer contract shared by the +`*-in-region-or-buffer' commands: operate on the active region when one is +active, otherwise the entire buffer." + (if (use-region-p) + (cons (region-beginning) (region-end)) + (cons (point-min) (point-max)))) + (defun cj/append-to-lines-in-region-or-buffer (str) "Append STR to the end of each line in the region or entire buffer." (interactive "sEnter string to append: ") - (let* ((start-pos (if (use-region-p) - (region-beginning) - (point-min))) - (end-pos (if (use-region-p) - (region-end) - (point-max))) + (let* ((bounds (cj/--region-or-buffer-bounds)) + (start-pos (car bounds)) + (end-pos (cdr bounds)) (text (buffer-substring start-pos end-pos)) (insertion (cj/--append-to-lines text str))) (delete-region start-pos end-pos) @@ -167,12 +173,9 @@ Returns the transformed string without modifying the buffer." (defun cj/prepend-to-lines-in-region-or-buffer (str) "Prepend STR to the beginning of each line in the region or entire buffer." (interactive "sEnter string to prepend: ") - (let* ((start-pos (if (use-region-p) - (region-beginning) - (point-min))) - (end-pos (if (use-region-p) - (region-end) - (point-max))) + (let* ((bounds (cj/--region-or-buffer-bounds)) + (start-pos (car bounds)) + (end-pos (cdr bounds)) (text (buffer-substring start-pos end-pos)) (insertion (cj/--prepend-to-lines text str))) (delete-region start-pos end-pos) @@ -195,12 +198,9 @@ Returns the indented text without modifying the buffer." COUNT is the number of characters to indent (default 4). USE-TABS when non-nil (prefix argument) uses tabs instead of spaces." (interactive "p\nP") - (let* ((start-pos (if (use-region-p) - (region-beginning) - (point-min))) - (end-pos (if (use-region-p) - (region-end) - (point-max))) + (let* ((bounds (cj/--region-or-buffer-bounds)) + (start-pos (car bounds)) + (end-pos (cdr bounds)) (text (buffer-substring start-pos end-pos)) (insertion (cj/--indent-lines text count use-tabs))) (delete-region start-pos end-pos) @@ -242,12 +242,9 @@ Returns the dedented text without modifying the buffer." COUNT is the number of characters to remove (default 4). Works on region if active, otherwise entire buffer." (interactive "p") - (let* ((start-pos (if (use-region-p) - (region-beginning) - (point-min))) - (end-pos (if (use-region-p) - (region-end) - (point-max))) + (let* ((bounds (cj/--region-or-buffer-bounds)) + (start-pos (car bounds)) + (end-pos (cdr bounds)) (text (buffer-substring start-pos end-pos)) (insertion (cj/--dedent-lines text count))) (delete-region start-pos end-pos) |
