-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rainbow-csv.el
146 lines (123 loc) · 4.66 KB
/
rainbow-csv.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;;; rainbow-csv.el --- Highlight CSV and TSV files in different rainbow colors -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2025 Shen, Jen-Chieh
;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
;; Maintainer: Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/emacs-vs/rainbow-csv
;; Version: 0.1.0
;; Package-Requires: ((emacs "27.1") (csv-mode "1.22"))
;; Keywords: convenience csv
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Highlight CSV and TSV files in different rainbow colors.
;;
;;; Code:
(require 'csv-mode)
(defgroup rainbow-csv nil
"Highlight CSV and TSV spreadsheet files in different rainbow colors."
:prefix "rainbow-csv-"
:group 'convenience
:link '(url-link :tag "Repository" "https://github.com/emacs-vs/rainbow-csv"))
(defcustom rainbow-csv-colors
(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")))
"List of colors to use."
:type '(list color)
:group 'rainbow-csv)
(defcustom rainbow-csv-separators nil
"Alist map mode to separator."
:type '(list (cons symbol character))
:group 'rainbow-csv)
(make-obsolete 'rainbow-csv-separators 'csv-separators "1.0.0")
(defvar rainbow-csv--old-csv-font-lock-keywords nil
"Store the old value for variable `csv-font-lock-keywords'.")
;;
;; (@* "Entry" )
;;
(defun rainbow-csv--enable ()
"Enable `rainbow-csv' in current buffer."
(rainbow-csv-highlight))
(defun rainbow-csv--disable ()
"Disable `rainbow-csv' in current buffer."
(rainbow-csv-revert-font-lock))
;;;###autoload
(define-minor-mode rainbow-csv-mode
"Minor mode `rainbow-csv-mode'."
:lighter " RainbowCSV"
:group rainbow-csv
(if rainbow-csv-mode (rainbow-csv--enable) (rainbow-csv--disable)))
;;
;; (@* "Core" )
;;
(defun rainbow-csv--revert-font-lock-keywords ()
"Revert to default font lock rules."
(unless rainbow-csv--old-csv-font-lock-keywords
(setq rainbow-csv--old-csv-font-lock-keywords csv-font-lock-keywords))
(setq csv-font-lock-keywords rainbow-csv--old-csv-font-lock-keywords))
(defun rainbow-csv-revert-font-lock ()
"Revert to default font lock rules interactively."
(interactive)
(rainbow-csv--revert-font-lock-keywords)
(font-lock-refresh-defaults))
;;;###autoload
(defun rainbow-csv-highlight (&optional separator)
"Highlight CSV with rainbow colors, optionally select SEPARATOR."
(interactive (list (when current-prefix-arg (read-char "Separator: "))))
(rainbow-csv--revert-font-lock-keywords)
(font-lock-mode 1)
(let* ((separator (or separator
(csv-guess-separator ; From `csv-guess-set-separator'
(buffer-substring-no-properties
(point-min) (min 8192 (point-max)))
2048)))
(fields (save-excursion
(goto-char (point-min))
(csv--collect-fields (line-end-position))))
;; Is the first field quoted?
(quote-char (string-match-p (format "^\\([%s]\\).*\\1$" (string-join csv-field-quotes)) (car fields)))
(quote-char (and quote-char (substring (car fields) nil 1)))) ; Get the first char
(dotimes (i (length fields))
(let* ((r (if quote-char
(format "^\\(%s[^%s]*%s[%c\n]\\)\\{%d\\}"
quote-char quote-char quote-char separator (1+ i))
(format "^\\([^%c\n]*[%c\n]\\)\\{%d\\}"
separator separator (1+ i))))
(len (length rainbow-csv-colors))
(color (nth (% i len) rainbow-csv-colors)))
(setq csv-font-lock-keywords
(append csv-font-lock-keywords
`((,r (1 '(face (:foreground ,color)) prepend t))))))))
(font-lock-refresh-defaults))
(provide 'rainbow-csv)
;;; rainbow-csv.el ends here