4

I'm a big fan of rainbow-delimiters. Even when I do not code in Lisp I can quickly and easily find matching bracket just looking at the code. My method works better on dark backgrounds, but can be tweaked to work on light backgrounds as well. The thing that still needs doing is automatic change of bracket colours after the theme change.

My code so far:

(require 'color)
(defun hsl-to-hex (h s l)
  "Convert H S L to hex colours."
  (let (rgb)
    (setq rgb (color-hsl-to-rgb h s l))
    (color-rgb-to-hex (nth 0 rgb)
                      (nth 1 rgb)
                      (nth 2 rgb))))

(defun bracket-colors ()
  "Calculate the bracket colours based on background."
  (let (hexcolors lightvals)
    (if (>= (color-distance  "white"
                             (face-attribute 'default :background))
            (color-distance  "black"
                             (face-attribute 'default :background)))
        (setq lightvals (list 0.65 0.55))
      (setq lightvals (list 0.35 0.30)))

    (concatenate 'list
                 (dolist (n'(.71 .3 .11 .01))
                   (push (hsl-to-hex (+ n 0.0) 1.0 (nth 0 lightvals)) hexcolors))
                 (dolist (n '(.81 .49 .17 .05))
                   (push (hsl-to-hex (+ n 0.0) 1.0 (nth 1 lightvals)) hexcolors)))
    (reverse hexcolors)))


(defun colorise-brackets ()
  "Apply my own colours to rainbow delimiters."
  (require 'rainbow-delimiters)
  (custom-set-faces
   ;; or use (list-colors-display)
   `(rainbow-delimiters-depth-1-face ((t (:foreground "#888"))))
   `(rainbow-delimiters-depth-2-face ((t (:foreground ,(nth 0 (bracket-colors))))))
   `(rainbow-delimiters-depth-3-face ((t (:foreground ,(nth 1 (bracket-colors))))))
   `(rainbow-delimiters-depth-4-face ((t (:foreground ,(nth 2 (bracket-colors))))))
   `(rainbow-delimiters-depth-5-face ((t (:foreground ,(nth 3 (bracket-colors))))))
   `(rainbow-delimiters-depth-6-face ((t (:foreground ,(nth 4 (bracket-colors))))))
   `(rainbow-delimiters-depth-7-face ((t (:foreground ,(nth 5 (bracket-colors))))))
   `(rainbow-delimiters-depth-8-face ((t (:foreground ,(nth 6 (bracket-colors))))))
   `(rainbow-delimiters-depth-9-face ((t (:foreground ,(nth 7 (bracket-colors))))))
   `(rainbow-delimiters-unmatched-face ((t (:foreground "white" :background "red"))))
   `(highlight ((t (:foreground "#ff0000" :background "#888"))))
   ))

(colorise-brackets)

After changing from dark to light theme, or vice versa I need to manually eval following in the scratch buffer:

(colorise-brackets)

How do I make it automatic? What other improvements would you suggest?

3
  • How about just creating a new function that changes the theme and then calls your own function (essentially a wrapper around the theme changing function that finishes by calling your own custom function)? You can always evaluate an expression with M-: and just type it into the minibuffer, instead of typing into the *scratch* buffer. You could also make your function for colorise-brackets interactive and add it to a keyboard shortcut. – lawlist Mar 30 '16 at 0:44
  • @lawlist that's one of the options, but i wonder if there is any callback to be ran after changing theme, so that It could happen automatically. – ruby_object Mar 30 '16 at 1:09
  • 1
    It seems to me that there ought to be a hook at the tail end of all three of these: enable-theme, load-theme, disable-theme. I don't use themes, but if I did, I would probably submit an enhancement request to the Emacs development team. I don't see it in the functions, and I don't see it in the manual: gnu.org/software/emacs/manual/html_node/emacs/… – lawlist Mar 30 '16 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.