public
Description: A mac-oriented emacs configuration bundle with batteries included: check out as ~/.emacs.d
Homepage: http://www.sanityinc.com/
Clone URL: git://github.com/purcell/emacs.d.git
emacs.d / init-css.el
100644 76 lines (63 sloc) 2.439 kb
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
(autoload 'css-mode "css-mode" "Mode for editing CSS files" t)
(setq cssm-indent-function #'cssm-c-style-indenter)
(add-auto-mode 'css-mode "\\.css$")
 
(eval-after-load "mmm-vars"
  '(progn
     (mmm-add-group
      'html-css
      '((css-cdata
         :submode css-mode
         :face mmm-code-submode-face
         :front "<style[^>]*>[ \t\n]*\\(//\\)?<!\\[CDATA\\[[ \t]*\n?"
         :back "[ \t]*\\(//\\)?]]>[ \t\n]*</style>"
         :insert ((?j js-tag nil @ "<style type=\"text/css\">"
                      @ "\n" _ "\n" @ "</script>" @)))
        (css
         :submode css-mode
         :face mmm-code-submode-face
         :front "<style[^>]*>[ \t]*\n?"
         :back "[ \t]*</style>"
         :insert ((?j js-tag nil @ "<style type=\"text/css\">"
                      @ "\n" _ "\n" @ "</style>" @)))
        (css-inline
         :submode css-mode
         :face mmm-code-submode-face
         :front "style=\""
         :back "\"")))
     (dolist (mode (list 'html-mode 'nxml-mode))
       (mmm-add-mode-ext-class mode "\\.r?html\\(\\.erb\\)?$" 'html-css))))
 
 
 
;; Colourise CSS colour literals
;; Inspired by http://xahlee.org/emacs/emacs_html.html, but much extended
 
(require 'hexl)
(defun choose-contrasting-colour (hex-colour)
  (if (> (hexl-hex-string-to-integer (substring hex-colour 1))
         (hexl-hex-string-to-integer "777777"))
      "#000000"
    "#ffffff"))
 
(defun three-hex-colour-to-six (three)
  (let ((six "#"))
    (progn
      (dolist (c (string-to-list (substring three 1)))
        (setq six (concat six (string c c))))
      six)))
 
(defun hexcolour-font-lock-face (hexcolour)
  (list :background hexcolour :foreground (choose-contrasting-colour hexcolour)))
 
(setq hexcolour-keywords
  '(("#[abcdef[:digit:]]\\{3\\}"
     (0 (put-text-property
         (match-beginning 0)
         (match-end 0)
         'face (hexcolour-font-lock-face
                (three-hex-colour-to-six (match-string-no-properties 0))))))
    ("#[abcdef[:digit:]]\\{6\\}"
     (0 (put-text-property
         (match-beginning 0)
         (match-end 0)
         'face (hexcolour-font-lock-face
                (match-string-no-properties 0)))))))
 
(defun hexcolour-add-to-font-lock ()
  (font-lock-add-keywords nil hexcolour-keywords))
 
(add-hook 'css-mode-hook 'hexcolour-add-to-font-lock)
(add-hook 'html-mode-hook 'hexcolour-add-to-font-lock)
(add-hook 'sass-mode-hook 'hexcolour-add-to-font-lock)
 
 
(provide 'init-css)