aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/custom-ordering.el41
-rw-r--r--tests/test-custom-ordering-number-lines.el5
-rw-r--r--tests/test-custom-ordering-reverse-lines.el6
3 files changed, 35 insertions, 17 deletions
diff --git a/modules/custom-ordering.el b/modules/custom-ordering.el
index 4dc5bff8..71477948 100644
--- a/modules/custom-ordering.el
+++ b/modules/custom-ordering.el
@@ -145,8 +145,15 @@ START and END identify the active region."
START and END define the region to operate on.
Returns the transformed string without modifying the buffer."
(cj/--ordering-validate-region start end)
- (let ((lines (split-string (buffer-substring start end) "\n")))
- (mapconcat #'identity (nreverse lines) "\n")))
+ ;; Strip a trailing newline before splitting so it doesn't become a spurious
+ ;; empty line (which reversing would float to the top); reattach it after.
+ ;; Internal blank lines are preserved.
+ (let* ((raw (buffer-substring start end))
+ (trailing-newline (string-suffix-p "\n" raw))
+ (body (if trailing-newline (substring raw 0 -1) raw))
+ (lines (split-string body "\n")))
+ (concat (mapconcat #'identity (nreverse lines) "\n")
+ (if trailing-newline "\n" ""))))
(defun cj/reverse-lines (start end)
"Reverse the order of lines in region between START and END.
@@ -164,20 +171,28 @@ ZERO-PAD when non-nil pads numbers with zeros for alignment.
Example with 100 lines: \"001\", \"002\", ..., \"100\".
Returns the transformed string without modifying the buffer."
(cj/--ordering-validate-region start end)
- (let* ((lines (split-string (buffer-substring start end) "\n"))
+ ;; Strip a trailing newline before splitting so it doesn't become a spurious
+ ;; extra numbered empty line; reattach it after. Internal blank lines are
+ ;; preserved and numbered.
+ (let* ((raw (buffer-substring start end))
+ (trailing-newline (string-suffix-p "\n" raw))
+ (body (if trailing-newline (substring raw 0 -1) raw))
+ (lines (split-string body "\n"))
(line-count (length lines))
(width (if zero-pad (length (number-to-string line-count)) 1))
(format-spec (if zero-pad (format "%%0%dd" width) "%d")))
- (mapconcat
- (lambda (pair)
- (let* ((num (car pair))
- (line (cdr pair))
- (num-str (format format-spec num)))
- (concat (replace-regexp-in-string "N" num-str format-string) line)))
- (cl-loop for line in lines
- for i from 1
- collect (cons i line))
- "\n")))
+ (concat
+ (mapconcat
+ (lambda (pair)
+ (let* ((num (car pair))
+ (line (cdr pair))
+ (num-str (format format-spec num)))
+ (concat (replace-regexp-in-string "N" num-str format-string) line)))
+ (cl-loop for line in lines
+ for i from 1
+ collect (cons i line))
+ "\n")
+ (if trailing-newline "\n" ""))))
(defun cj/number-lines (start end format-string zero-pad)
"Number lines in region between START and END with custom format.
diff --git a/tests/test-custom-ordering-number-lines.el b/tests/test-custom-ordering-number-lines.el
index adda84f0..142e5561 100644
--- a/tests/test-custom-ordering-number-lines.el
+++ b/tests/test-custom-ordering-number-lines.el
@@ -122,9 +122,10 @@ Returns the transformed string."
(should (string= result "1. "))))
(ert-deftest test-number-lines-empty-lines ()
- "Should number empty lines."
+ "Should number empty lines, treating the final newline as a terminator.
+The old split counted the trailing newline as a spurious third line."
(let ((result (test-number-lines "\n\n" "N. " nil)))
- (should (string= result "1. \n2. \n3. "))))
+ (should (string= result "1. \n2. \n"))))
(ert-deftest test-number-lines-with-existing-numbers ()
"Should number lines that already have content."
diff --git a/tests/test-custom-ordering-reverse-lines.el b/tests/test-custom-ordering-reverse-lines.el
index 3c71362d..5b8c01ac 100644
--- a/tests/test-custom-ordering-reverse-lines.el
+++ b/tests/test-custom-ordering-reverse-lines.el
@@ -86,9 +86,11 @@ Returns the transformed string."
(should (string= result "b\n\na"))))
(ert-deftest test-reverse-lines-trailing-newline ()
- "Should handle trailing newline."
+ "Should reverse the lines and preserve the trailing newline.
+The old split dropped the trailing newline into a leading empty line,
+producing \"\\nline2\\nline1\"."
(let ((result (test-reverse-lines "line1\nline2\n")))
- (should (string= result "\nline2\nline1"))))
+ (should (string= result "line2\nline1\n"))))
(ert-deftest test-reverse-lines-only-newlines ()
"Should reverse lines that are only newlines."