2006/07/09
# [xyzzy] CSVデータに色付け
CSV / TSV データの列ごとの表示色を変えてみようと,試みる。1 行目のデータのみを参照し,区切り文字と列数をテキトーに判定。
(defun csv-coloring ()
(interactive)
(make-local-variable 'regexp-keyword-list)
(let* ((col '((:color 4 0) (:color 9 0) (:color 10 0)))
(reg "^")
(str (save-excursion
(buffer-substring (progn (goto-char (point-min)) (point))
(progn (goto-eol) (point)))))
(sep (if (string-match (string #\TAB) str) #\TAB #\,))
(sepstr (string sep))
(len (list-length (split-string str sep)))
collst colnum)
(setq colnum (1- (list-length col)))
(dotimes (i len)
(let ((j (1+ i))
(c (nth colnum col)))
(when (= j 10) (return nil))
(when (< (decf colnum) 0)
(setq colnum (1- (list-length col))))
(setq reg (concat reg "\\([^" sepstr "]*\\)" (if (< j len) sepstr "$")))
(setq collst (acons j c collst))))
(setq regexp-keyword-list
(compile-regexp-keyword-list `((,reg t ,collst))))))
但し,色付けは,9 列目までしか行えない。それ以上色付けを行おうとすると,Bad reference number: 10 といわれてしまうため,なんともならん感じ。また,ダブルクォートでくくられていようと,カンマはカンマで区切る。といった中途半端で手抜きな仕様。
コメント
トラックバック -
http://hie.s64.xrea.com/murmur/tb.cgi/20060709
