From 6bcdef0ed429dfe88a607478c504eb52186e639e Mon Sep 17 00:00:00 2001 From: "Peder O. Klingenberg" 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" ;; Maintainer: emacs-devel@gnu.org -;; 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