diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-16 02:48:18 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-16 02:48:18 -0500 |
| commit | a9a4d8c7148c115a242a7b35d16dd536f9c0c700 (patch) | |
| tree | 3370202a1fd1a6e8eb7c14984bf602d0d37265d3 /modules/custom-ordering.el | |
| parent | 1c5a2ebab7c721d795ed9331afdb305fd683e172 (diff) | |
| download | dotemacs-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/custom-ordering.el')
| -rw-r--r-- | modules/custom-ordering.el | 23 |
1 files changed, 16 insertions, 7 deletions
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. |
