summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-16 02:48:18 -0500
committerCraig Jennings <c@cjennings.net>2026-05-16 02:48:18 -0500
commita9a4d8c7148c115a242a7b35d16dd536f9c0c700 (patch)
tree3370202a1fd1a6e8eb7c14984bf602d0d37265d3 /modules
parent1c5a2ebab7c721d795ed9331afdb305fd683e172 (diff)
downloaddotemacs-a9a4d8c7148c115a242a7b35d16dd536f9c0c700.tar.gz
dotemacs-a9a4d8c7148c115a242a7b35d16dd536f9c0c700.zip
refactor(custom-editing): five hygiene fixes from the module-by-module re-review
- Guard `cj/duplicate-line-or-region' when COMMENT is non-nil but the current mode has no `comment-start' (e.g. fundamental-mode). Previously the function silently produced malformed output via `comment-region'; now it signals a clear `user-error'. - Factor the `find-file' advice install in external-open.el into `cj/external-open-install-advice'. Same idempotent shape (remove-then-add) but the intent is named. - Add `cj/--validate-decoration-char' in custom-comments.el and wire it into all six divider / border / box helpers. Rejects multi-char strings, empty strings, and control characters like newline/tab that would corrupt subsequent `M-q' flows. Updated the five nil-decoration ERT tests from `:type 'wrong-type-argument' (the old crash signal from `string-to-char' on nil) to `:type 'user-error', since the validator produces a clear message instead of a deep crash. - Extract `cj/--require-spell-checker' in flyspell-and-abbrev.el. Both `cj/flyspell-toggle' and `cj/flyspell-then-abbrev' now call the shared helper; the checker list lives in `cj/--spell-checker-executables', so adding nuspell or any other checker is a one-line edit. - Preserve trailing newlines in custom-ordering output. Both `cj/--arrayify' and `cj/--unarrayify' now detect a trailing newline on the input region and re-append it to the result, matching the pattern custom-text-enclose.el already uses.
Diffstat (limited to 'modules')
-rw-r--r--modules/custom-comments.el19
-rw-r--r--modules/custom-line-paragraph.el9
-rw-r--r--modules/custom-ordering.el23
-rw-r--r--modules/external-open.el12
-rw-r--r--modules/flyspell-and-abbrev.el29
5 files changed, 69 insertions, 23 deletions
diff --git a/modules/custom-comments.el b/modules/custom-comments.el
index d5419be2..7e8e1404 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 27f24cfe..0eb1e2a5 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 81490b65..69a2d465 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 0d6ec520..57cffadc 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 e29ad6e9..e732ac46 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))