Subject: Re: bug#71042: 29.3; csv-mode: csv-guess-set-separator chokes on comment lines

From: "Peder O. Klingenberg" <[email-masked]>
Date: Fri, 26 Jul 2024 12:07:00 +0200

Tags: patch

On Sat, 2024-05-18 12:14:21 +0200, Marco Emilio Poleggi wrote:

> Comment lines seem well supported by csv-mode. However, function
> `csv-guess-set-separator` has troubles with them.

Easily fixed, but needs someone with commit privileges to apply the patch

>From 6bcdef0ed429dfe88a607478c504eb52186e639e Mon Sep 17 00:00:00 2001
From: "Peder O. Klingenberg" <[email-masked]>
Date: Fri, 26 Jul 2024 10:13:24 +0200
Subject: [PATCH] Disregard comments when guessing separators

* csv-mode.el (csv--separator-candidates): Ignore
csv-comment-start when looking for potential separators.

* csv-mode-tests.el (csv-tests-guess-separator-avoid-comment):
Previously failing test case guessing the comment start as
separator char.

(Bug#71042 fixed)
---
 csv-mode-tests.el | 10 ++++++++++
 csv-mode.el       |  7 ++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/csv-mode-tests.el b/csv-mode-tests.el
index 12e009ecf9..213fd033b2 100644
--- a/csv-mode-tests.el
+++ b/csv-mode-tests.el
@@ -170,5 +170,15 @@
   (should (equal (csv--unquote-value "|Hello, World|")
                  "|Hello, World|")))
 
+(ert-deftest csv-tests-guess-separator-avoid-comment ()
+  ;; bug#71042
+  (let ((testdata "###
+###
+Foo;Bar;Quux
+123;456;blah, blah
+"))
+    (message "Guessed separator: %c" (csv-guess-separator testdata))
+    (should-not (equal (csv-guess-separator testdata) ?#))))
+
 (provide 'csv-mode-tests)
 ;;; csv-mode-tests.el ends here
diff --git a/csv-mode.el b/csv-mode.el
index 1e0f99ef92..6bdfcafbbb 100644
--- a/csv-mode.el
+++ b/csv-mode.el
@@ -4,7 +4,7 @@
 
 ;; Author: "Francis J. Wright" <[email-masked]>
 ;; Maintainer: [email-masked]
-;; Version: 1.25
+;; Version: 1.26
 ;; Package-Requires: ((emacs "27.1") (cl-lib "0.5"))
 ;; Keywords: convenience
 
@@ -107,6 +107,10 @@
 
 ;;; News:
 
+;; Since 1.26:
+;; - `csv-guess-separator' will no longer guess the comment-start
+;;    character as a potential separator character.
+
 ;; Since 1.25:
 ;; - The ASCII control character 31 Unit Separator can now be
 ;;   recognized as a CSV separator by `csv-guess-separator'.
@@ -1902,6 +1906,7 @@ When CUTOFF is passed, look only at the first CUTOFF number of characters."
                  (or (= c ?\t)
                      (= c ?\C-_)
                      (and (not (member c '(?. ?/ ?\" ?')))
+                          (not (= c (string-to-char csv-comment-start)))
                           (not (member (get-char-code-property c 'general-category)
                                        '(Lu Ll Lt Lm Lo Nd Nl No Ps Pe Cc Co))))))
         (puthash c t chars)))
-- 
2.25.1