diff options
| -rw-r--r-- | modules/custom-comments.el | 19 | ||||
| -rw-r--r-- | modules/custom-line-paragraph.el | 9 | ||||
| -rw-r--r-- | modules/custom-ordering.el | 23 | ||||
| -rw-r--r-- | modules/external-open.el | 12 | ||||
| -rw-r--r-- | modules/flyspell-and-abbrev.el | 29 | ||||
| -rw-r--r-- | tests/test-custom-comments-comment-block-banner.el | 2 | ||||
| -rw-r--r-- | tests/test-custom-comments-comment-box.el | 2 | ||||
| -rw-r--r-- | tests/test-custom-comments-comment-heavy-box.el | 2 | ||||
| -rw-r--r-- | tests/test-custom-comments-comment-inline-border.el | 6 | ||||
| -rw-r--r-- | tests/test-custom-comments-comment-padded-divider.el | 2 | ||||
| -rw-r--r-- | tests/test-custom-comments-comment-simple-divider.el | 2 |
11 files changed, 78 insertions, 30 deletions
diff --git a/modules/custom-comments.el b/modules/custom-comments.el index d5419be2d..7e8e14048 100644 --- a/modules/custom-comments.el +++ b/modules/custom-comments.el @@ -85,6 +85,19 @@ ;; ======================== Comment Generation Functions ======================= +(defun cj/--validate-decoration-char (decoration-char) + "Signal `user-error' unless DECORATION-CHAR is a printable single character. +Returns DECORATION-CHAR unchanged on success. Rejects multi-char +strings, empty strings, control chars like newline/tab, and non-string +inputs. Used by all divider / border helpers below." + (unless (and (stringp decoration-char) + (= (length decoration-char) 1) + (string-match-p "[[:print:]]" decoration-char)) + (user-error + "Decoration character must be a single printable character, got: %S" + decoration-char)) + decoration-char) + ;; ----------------------------- Inline Border --------------------------------- (defun cj/--comment-inline-border (cmt-start cmt-end decoration-char text length) @@ -93,6 +106,7 @@ CMT-START and CMT-END are the comment syntax strings. DECORATION-CHAR is the character to use for borders (string). TEXT is the comment text (will be centered). LENGTH is the total width of the line." + (cj/--validate-decoration-char decoration-char) (let* ((current-column-pos (current-column)) (text-length (length text)) (comment-start-len (+ (length cmt-start) @@ -157,6 +171,7 @@ CMT-START and CMT-END are the comment syntax strings. DECORATION-CHAR is the character to use for the divider lines. TEXT is the comment text. LENGTH is the total width of each line." + (cj/--validate-decoration-char decoration-char) (let* ((current-column-pos (current-column)) (min-length (+ current-column-pos (length cmt-start) @@ -233,6 +248,7 @@ DECORATION-CHAR is the character to use for the divider lines. TEXT is the comment text. LENGTH is the total width of each line. PADDING is the number of spaces before the text." + (cj/--validate-decoration-char decoration-char) (when (< padding 0) (error "Padding %d cannot be negative" padding)) (let* ((current-column-pos (current-column)) @@ -314,6 +330,7 @@ CMT-START and CMT-END are the comment syntax strings. DECORATION-CHAR is the character to use for borders. TEXT is the comment text (centered). LENGTH is the total width of each line." + (cj/--validate-decoration-char decoration-char) (let* ((current-column-pos (current-column)) (comment-char (if (equal cmt-start ";") ";;" cmt-start)) (comment-end-char (if (string-empty-p cmt-end) comment-char cmt-end)) @@ -377,6 +394,7 @@ CMT-START and CMT-END are the comment syntax strings. DECORATION-CHAR is the character to use for borders. TEXT is the comment text (centered). LENGTH is the total width of each line." + (cj/--validate-decoration-char decoration-char) (let* ((current-column-pos (current-column)) (comment-char (if (equal cmt-start ";") ";;" cmt-start)) (comment-end-char (if (string-empty-p cmt-end) comment-char cmt-end)) @@ -542,6 +560,7 @@ CMT-END should be the block comment end (e.g., '*/'). DECORATION-CHAR is the character to use for the border line. TEXT is the comment text. LENGTH is the total width of each line." + (cj/--validate-decoration-char decoration-char) (let* ((current-column-pos (current-column)) (min-length (+ current-column-pos (length cmt-start) diff --git a/modules/custom-line-paragraph.el b/modules/custom-line-paragraph.el index 27f24cfe2..0eb1e2a52 100644 --- a/modules/custom-line-paragraph.el +++ b/modules/custom-line-paragraph.el @@ -54,8 +54,15 @@ (defun cj/duplicate-line-or-region (&optional comment) "Duplicate the current line or active region below. -Comment the duplicated text when optional COMMENT is non-nil." +Comment the duplicated text when optional COMMENT is non-nil. +Signal `user-error' when COMMENT is non-nil but the current mode has +no `comment-start' (e.g. `fundamental-mode'), since commenting would +produce malformed output silently." (interactive "P") + (when (and comment (not (and (stringp comment-start) + (> (length comment-start) 0)))) + (user-error + "Cannot comment in %s: no comment syntax defined" major-mode)) (let* ((b (if (region-active-p) (region-beginning) (line-beginning-position))) (e (if (region-active-p) (region-end) (line-end-position))) (lines (split-string (buffer-substring-no-properties b e) "\n"))) diff --git a/modules/custom-ordering.el b/modules/custom-ordering.el index 81490b658..69a2d465c 100644 --- a/modules/custom-ordering.el +++ b/modules/custom-ordering.el @@ -32,13 +32,18 @@ QUOTE specifies the quotation characters to surround each element. Use \"\" for no quotes, \"\\\"\" for double quotes, \"'\" for single quotes. PREFIX is an optional string to prepend to the result (e.g., \"[\" or \"(\"). SUFFIX is an optional string to append to the result (e.g., \"]\" or \")\"). +Preserves a trailing newline if the input region ends with one, so +line-oriented operations on the result behave the same as before. Returns the transformed string without modifying the buffer." (when (> start end) (error "Invalid region: start (%d) is greater than end (%d)" start end)) - (let ((result (mapconcat - (lambda (x) (format "%s%s%s" quote x quote)) - (split-string (buffer-substring start end)) ", "))) - (concat (or prefix "") result (or suffix "")))) + (let* ((raw (buffer-substring start end)) + (trailing-newline (string-suffix-p "\n" raw)) + (result (mapconcat + (lambda (x) (format "%s%s%s" quote x quote)) + (split-string raw) ", "))) + (concat (or prefix "") result (or suffix "") + (if trailing-newline "\n" "")))) (defun cj/arrayify (start end quote) "Convert lines between START and END into quoted, comma-separated strings. @@ -80,12 +85,16 @@ Example: `apple banana cherry' becomes `[\"apple\", \"banana\", \"cherry\"]'." "Internal implementation: Convert comma-separated array to lines. START and END define the region to operate on. Removes quotes (both single and double) and splits by ', '. +Preserves a trailing newline if the input region ends with one. Returns the transformed string without modifying the buffer." (when (> start end) (error "Invalid region: start (%d) is greater than end (%d)" start end)) - (mapconcat - (lambda (x) (replace-regexp-in-string "[\"']" "" x)) - (split-string (buffer-substring start end) ", ") "\n")) + (let* ((raw (buffer-substring start end)) + (trailing-newline (string-suffix-p "\n" raw)) + (result (mapconcat + (lambda (x) (replace-regexp-in-string "[\"']" "" x)) + (split-string raw ", ") "\n"))) + (concat result (if trailing-newline "\n" "")))) (defun cj/unarrayify (start end) "Convert quoted comma-separated strings between START and END to separate lines. diff --git a/modules/external-open.el b/modules/external-open.el index 0d6ec5206..57cffadc8 100644 --- a/modules/external-open.el +++ b/modules/external-open.el @@ -146,9 +146,15 @@ Else call ORIG-FUN with ARGS." (cj/xdg-open file) (apply orig-fun args)))) -;; Make advice idempotent if you reevaluate this form. -(advice-remove 'find-file #'cj/find-file-auto) -(advice-add 'find-file :around #'cj/find-file-auto) +(defun cj/external-open-install-advice () + "Install the `cj/find-file-auto' advice on `find-file'. +Idempotent: re-running removes any prior copy of the advice before +adding it back, so re-evaluating the module updates the advice rather +than stacking it." + (advice-remove 'find-file #'cj/find-file-auto) + (advice-add 'find-file :around #'cj/find-file-auto)) + +(cj/external-open-install-advice) (provide 'external-open) ;;; external-open.el ends here. diff --git a/modules/flyspell-and-abbrev.el b/modules/flyspell-and-abbrev.el index e29ad6e9f..e732ac469 100644 --- a/modules/flyspell-and-abbrev.el +++ b/modules/flyspell-and-abbrev.el @@ -43,6 +43,8 @@ ;;; Code: +(require 'cl-lib) + ;; Forward declarations (eval-when-compile (defvar org-dir)) (defvar flyspell-mode) @@ -111,18 +113,26 @@ ;; ------------------------------ Flyspell Toggle ------------------------------ ;; easy toggling flyspell and also leverage the 'for-buffer-type' functionality. +(defconst cj/--spell-checker-executables + '("aspell" "ispell" "hunspell") + "Spell-checker executables `cj/--require-spell-checker' looks for.") + +(defun cj/--require-spell-checker () + "Signal `user-error' unless a known spell-checker is on PATH. +Looked-up executables: `cj/--spell-checker-executables'. Single point +of change when a new checker (e.g. nuspell) is added." + (unless (cl-some #'executable-find cj/--spell-checker-executables) + (user-error + "No spell checker found. Install one of: %s" + (mapconcat #'identity cj/--spell-checker-executables ", ")))) + (defun cj/flyspell-toggle () "Turn Flyspell on if it is off, or off if it is on. When turning on, it uses `cj/flyspell-on-for-buffer-type' so code-vs-text is handled appropriately." (interactive) - ;; Check if spell checker is available - (unless (or (executable-find "aspell") - (executable-find "ispell") - (executable-find "hunspell")) - (user-error "No spell checker found. Install aspell, ispell, or hunspell")) - + (cj/--require-spell-checker) (if (bound-and-true-p flyspell-mode) (progn ; flyspell is on, turn it off (flyspell-mode -1) @@ -208,12 +218,7 @@ Without prefix argument, it's created in the global abbrev table. Press C-' repeatedly to step through misspellings one at a time." (interactive "P") - ;; Check if spell checker is available - (unless (or (executable-find "aspell") - (executable-find "ispell") - (executable-find "hunspell")) - (user-error "No spell checker found. Install aspell, ispell, or hunspell")) - + (cj/--require-spell-checker) ;; Run flyspell-buffer only if buffer hasn't been checked yet (unless (bound-and-true-p flyspell-mode) (flyspell-buffer)) diff --git a/tests/test-custom-comments-comment-block-banner.el b/tests/test-custom-comments-comment-block-banner.el index 6561ebfab..da0b1fa7f 100644 --- a/tests/test-custom-comments-comment-block-banner.el +++ b/tests/test-custom-comments-comment-block-banner.el @@ -194,7 +194,7 @@ Returns the buffer string for assertions." "Should error when decoration-char is nil." (should-error (test-block-banner-at-column 0 "/*" "*/" nil "Header" 70) - :type 'wrong-type-argument)) + :type 'user-error)) (ert-deftest test-block-banner-c-nil-text () "Should error when text is nil." diff --git a/tests/test-custom-comments-comment-box.el b/tests/test-custom-comments-comment-box.el index 10b1a67d0..d7ee2830c 100644 --- a/tests/test-custom-comments-comment-box.el +++ b/tests/test-custom-comments-comment-box.el @@ -203,7 +203,7 @@ Returns the buffer string for assertions." "Should error when decoration-char is nil." (should-error (test-comment-box-at-column 0 ";;" "" nil "Header" 70) - :type 'wrong-type-argument)) + :type 'user-error)) (ert-deftest test-comment-box-elisp-non-integer-length () "Should error when length is not an integer." diff --git a/tests/test-custom-comments-comment-heavy-box.el b/tests/test-custom-comments-comment-heavy-box.el index 302896252..94d4aaa5f 100644 --- a/tests/test-custom-comments-comment-heavy-box.el +++ b/tests/test-custom-comments-comment-heavy-box.el @@ -207,7 +207,7 @@ Returns the buffer string for assertions." "Should error when decoration-char is nil." (should-error (test-heavy-box-at-column 0 ";;" "" nil "Header" 70) - :type 'wrong-type-argument)) + :type 'user-error)) (ert-deftest test-heavy-box-elisp-nil-text () "Should error when text is nil." diff --git a/tests/test-custom-comments-comment-inline-border.el b/tests/test-custom-comments-comment-inline-border.el index ca2bef06c..78e860353 100644 --- a/tests/test-custom-comments-comment-inline-border.el +++ b/tests/test-custom-comments-comment-inline-border.el @@ -190,10 +190,12 @@ Returns the buffer string for assertions." :type 'error)) (ert-deftest test-inline-border-elisp-nil-decoration () - "Should error when decoration-char is nil." + "Should error when decoration-char is nil. +The decoration-char validator signals `user-error' for any non-printable +or non-single-character input." (should-error (test-inline-border-at-column 0 ";;" "" nil "Header" 70) - :type 'wrong-type-argument)) + :type 'user-error)) (ert-deftest test-inline-border-elisp-non-integer-length () "Should error when length is not an integer." diff --git a/tests/test-custom-comments-comment-padded-divider.el b/tests/test-custom-comments-comment-padded-divider.el index 702a4c675..d4c189052 100644 --- a/tests/test-custom-comments-comment-padded-divider.el +++ b/tests/test-custom-comments-comment-padded-divider.el @@ -200,7 +200,7 @@ Returns the buffer string for assertions." "Should error when decoration-char is nil." (should-error (test-padded-divider-at-column 0 ";;" "" nil "Header" 70 2) - :type 'wrong-type-argument)) + :type 'user-error)) (ert-deftest test-padded-divider-elisp-nil-text () "Should error when text is nil." diff --git a/tests/test-custom-comments-comment-simple-divider.el b/tests/test-custom-comments-comment-simple-divider.el index a61e6b4cf..7979f18b7 100644 --- a/tests/test-custom-comments-comment-simple-divider.el +++ b/tests/test-custom-comments-comment-simple-divider.el @@ -195,7 +195,7 @@ Returns the buffer string for assertions." "Should error when decoration-char is nil." (should-error (test-simple-divider-at-column 0 ";;" "" nil "Header" 70) - :type 'wrong-type-argument)) + :type 'user-error)) (ert-deftest test-simple-divider-elisp-nil-text () "Should error when text is nil." |
