diff options
| -rw-r--r-- | modules/custom-text-enclose.el | 45 | ||||
| -rw-r--r-- | tests/test-custom-text-enclose--region-or-buffer-bounds.el | 43 |
2 files changed, 64 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) diff --git a/tests/test-custom-text-enclose--region-or-buffer-bounds.el b/tests/test-custom-text-enclose--region-or-buffer-bounds.el new file mode 100644 index 00000000..98992af8 --- /dev/null +++ b/tests/test-custom-text-enclose--region-or-buffer-bounds.el @@ -0,0 +1,43 @@ +;;; test-custom-text-enclose--region-or-buffer-bounds.el --- Tests for bounds helper -*- lexical-binding: t; -*- + +;;; Commentary: +;; Unit tests for cj/--region-or-buffer-bounds, the single source of the +;; "operate on the active region, else the whole buffer" contract shared by +;; the *-in-region-or-buffer editing commands. + +;;; Code: + +(require 'ert) +(require 'cl-lib) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'custom-text-enclose) + +;;; Normal Cases + +(ert-deftest test-ctenc-bounds-uses-region-when-active () + "Normal: with an active region, returns its beginning and end." + (cl-letf (((symbol-function 'use-region-p) (lambda () t)) + ((symbol-function 'region-beginning) (lambda () 3)) + ((symbol-function 'region-end) (lambda () 8))) + (should (equal '(3 . 8) (cj/--region-or-buffer-bounds))))) + +(ert-deftest test-ctenc-bounds-uses-whole-buffer-when-no-region () + "Normal: with no active region, returns the whole buffer's bounds." + (with-temp-buffer + (insert "line one\nline two\n") + (cl-letf (((symbol-function 'use-region-p) (lambda () nil))) + (should (equal (cons (point-min) (point-max)) + (cj/--region-or-buffer-bounds)))))) + +;;; Boundary Cases + +(ert-deftest test-ctenc-bounds-empty-buffer-no-region () + "Boundary: an empty buffer with no region yields equal start and end." + (with-temp-buffer + (cl-letf (((symbol-function 'use-region-p) (lambda () nil))) + (let ((b (cj/--region-or-buffer-bounds))) + (should (= (car b) (cdr b))))))) + +(provide 'test-custom-text-enclose--region-or-buffer-bounds) +;;; test-custom-text-enclose--region-or-buffer-bounds.el ends here |
