aboutsummaryrefslogtreecommitdiffstats
path: root/emacs/.emacs.d/init/oni-css-mode-init.org
blob: 447f9ece4dde0d6fb1a99295dc7baf3a25afb1cc (plain)
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
#+TITLE: CSS

scss-mode is based on css-mode, so any settings for css-mode also
automatically should work for scss-mode.

Enable electric pairing.

#+BEGIN_SRC emacs-lisp
  (add-hook 'css-mode-hook 'electric-pair-local-mode)
#+END_SRC

Enable electric indenting.

#+BEGIN_SRC emacs-lisp
  (add-hook 'css-mode-hook 'electric-indent-local-mode)
#+END_SRC

Enable company mode.

#+BEGIN_SRC emacs-lisp
  (add-hook 'css-mode-hook 'company-mode)
#+END_SRC

Enable rainbow mode to see all the color specifications (well, most)
as pretty colors.

#+BEGIN_SRC emacs-lisp
  (add-hook 'css-mode-hook 'rainbow-mode)
#+END_SRC

Add the scssc compiler's error message output to the compilation error
regexps.

#+BEGIN_SRC emacs-lisp
  (eval-when-compile (require 'compile))

  (with-eval-after-load 'compile
    (defvar oni:scss-error-regexp
      (rx (and bol
               (zero-or-more space) "on line "
               (group (one-or-more digit)) " of "
               (group (one-or-more (or word punct (syntax symbol))))
               eol)))

    (add-to-list 'compilation-error-regexp-alist
                 (list oni:scss-error-regexp 2 1 nil 2 2)))
#+END_SRC

Add a command to toggle the =!important= flag on CSS properties.

#+BEGIN_SRC emacs-lisp
  (defun oni:css-property-important-p ()
    "Return whether or not the current property is important."
    (save-excursion
      (beginning-of-line)
      (re-search-forward "!important" (line-end-position) :noerror)))

  (defun oni:css-add-important ()
    "Add an important flag to the property on the current line."
    (interactive)
    (unless (oni:css-property-important-p)
      (save-excursion
        (end-of-line)
        (when (re-search-backward ";" (line-beginning-position) :noerror)
          (insert " !important")))))

  (defun oni:css-remove-important ()
    "Remove the important flag from the property on the current line."
    (interactive)
    (when (oni:css-property-important-p)
      (save-excursion
        (end-of-line)
        (when (re-search-backward " !important" (line-beginning-position) :noerror)
          (replace-match "")))))

  (defun oni:css-toggle-important ()
    "Toggle the important flag on the property on the current line."
    (interactive)
    (if (oni:css-property-important-p)
        (oni:css-remove-important)
      (oni:css-add-important)))
#+END_SRC

Add a keybinding to toggle the =!important= flag on the current line.

#+BEGIN_SRC emacs-lisp
  (global-set-key (kbd "C-c !") #'oni:css-toggle-important)
#+END_SRC