Skip to content

Commit

Permalink
Add minor mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Dec 10, 2023
1 parent 07984ab commit 754046c
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions rainbow-csv.el
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@
:type 'list
:group 'rainbow-csv)

(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."
(add-hook 'post-self-insert-hook #'rainbow-csv--post-self-insert nil t)
(rainbow-csv-highlight))

(defun rainbow-csv--disable ()
"Disable `rainbow-csv' in current buffer."
(remove-hook 'post-self-insert-hook #'rainbow-csv--post-self-insert t)
(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)))

;;
;; (@* "Util" )
;;

;; Copied from the package `crazy-theme.el'
(defun rainbow-csv--rgb-code (offset limit)
"Generate random rgb code by OFFSET and LIMIT."
Expand All @@ -64,15 +92,23 @@
(let ((code-tri (rainbow-csv--rgb-code 0 256)))
(format "#%02X%02X%02X" (nth 0 code-tri) (nth 1 code-tri) (nth 2 code-tri))))

(defvar rainbow-csv--old-csv-font-lock-keywords nil
"Store the old value for variable `csv-font-lock-keywords'.")
;;
;; (@* "Core" )
;;

(defun rainbow-csv--revert-font-lock-keywords ()
"Revert to default font lock rules."
(unless rainbow-csv--old-csv-font-lock-keywords ; First assign
(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)
"Not documented (SEPARATOR)."
(interactive (list (when current-prefix-arg (read-char "Separator: "))))
Expand All @@ -94,5 +130,15 @@
`((,r (1 '(face (:foreground ,color)) prepend t)))))))
(font-lock-refresh-defaults))

;;
;; (@* "Events" )
;;

(defun rainbow-csv--post-self-insert (&rest _)
"Post insert."
(when (memq last-command-event '(?, ?\"))
(message "update once!")
(rainbow-csv-highlight)))

(provide 'rainbow-csv)
;;; rainbow-csv.el ends here

0 comments on commit 754046c

Please sign in to comment.