rainbow-csv icon indicating copy to clipboard operation
rainbow-csv copied to clipboard

not all cells quoted csv file, not work

Open sawyerzheng opened this issue 2 months ago • 1 comments

Not work example:

Image

Only work example:

Image

This is not a desired behavior, as most csv files will do not quote a cell data if there is not whitespace or a separator char.

sawyerzheng avatar Sep 26 '25 02:09 sawyerzheng

An alternative implementing based on: https://www.emacswiki.org/emacs/CsvMode

(defun csv-highlight (&optional separator)
  (interactive (list (when current-prefix-arg (read-char "Separator: "))))
  (font-lock-mode 1)
  (let* ((separator (or separator ?\,))
         (n (count-matches (string separator) (point-at-bol) (point-at-eol)))
         (available-colors (my/csv-highlight--colors))
         (colors (cl-loop for i from 0 below n
                          collect (nth (mod i (length available-colors)) available-colors))))
    (cl-loop for i from 2 to (1+ n) by 1
             for c in colors
             for r = (format "^\\([^%c\n]+%c\\)\\{%d\\}" separator separator i)
             do (font-lock-add-keywords nil `((,r (1 '(face (:foreground ,c)))))))))



(defun  my/csv-highlight--colors ()
  "List of colors to use."
  (cond
   ((eq 'light (frame-parameter nil 'background-mode))
    '("#333333"
      "#A96329"
      "#233286"
      "#AD66AA"
      "#317CB5"
      "#732301"
      "#4A3F87"
      "#B1364F"
      "#A96329"
      "#0BB8B8"))
   (t
    '("#CCCCCC"
      "#569CD6"
      "#DCCD79"
      "#529955"
      "#CE834A"
      "#8CDCFE"
      "#B5C078"
      "#4EC9B0"
      "#569CD6"
      "#F44747"))))

But this also has a small bug for quoated string cells which share same color as shown in the following picture with green color:

Image

sawyerzheng avatar Sep 26 '25 02:09 sawyerzheng