forked from antonj/scss-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscss-mode.el
186 lines (156 loc) · 6.62 KB
/
scss-mode.el
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
;;; scss-mode.el --- Major mode for editing SCSS files
;;
;; Author: Anton Johansson <[email protected]> - http://antonj.se
;; URL: https://github.com/antonj/scss-mode
;; Created: Sep 1 23:11:26 2010
;; Version: 0.5.0
;; Keywords: scss css mode
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE. See the GNU General Public License for more details.
;;
;;; Commentary:
;;
;; Command line utility sass is required, see http://sass-lang.com/
;; To install sass:
;; gem install sass
;;
;; Also make sure sass location is in emacs PATH, example:
;; (setq exec-path (cons (expand-file-name "~/.gem/ruby/1.8/bin") exec-path))
;; or customize `scss-sass-command' to point to your sass executable.
;;
;;; Code:
(require 'derived)
(require 'compile)
(require 'flymake)
(require 'css-mode) ; For `css-mode-syntax-table'.
(require 'cc-cmds) ; For `c-fill-paragraph', `c-indent-new-comment-line'.
(eval-and-compile
(require 'cc-styles) ; For `c-setup-paragraph-variables'.
(require 'cc-engine)) ; For `c-paragraph-start' et. al.
(defgroup scss nil
"Scss mode"
:prefix "scss-"
:group 'css)
(defcustom scss-sass-command "sass"
"Command used to compile SCSS files, should be sass or the
complete path to your sass runnable example:
\"~/.gem/ruby/1.8/bin/sass\""
:group 'scss)
(defcustom scss-compile-at-save nil
"If not nil the SCSS buffers will be compiled after each save"
:type 'boolean
:group 'scss)
(defcustom scss-sass-options '()
"Command line Options for sass executable, for example:
'(\"--cache-location\" \"'/tmp/.sass-cache'\")"
:group 'scss)
(defcustom scss-output-directory nil
"Output directory for compiled files, for example:
\"../css\""
:group 'scss)
(defcustom scss-compile-error-regex '("\\(Syntax error:\s*.*\\)\n\s*on line\s*\\([0-9]+\\) of \\([^, \n]+\\)" 3 2 nil nil 1)
"Regex for finding line number file and error message in
compilation buffers, syntax from
`compilation-error-regexp-alist' (REGEXP FILE LINE COLUMN TYPE
HYPERLINK HIGHLIGHT)"
:group 'scss)
(defconst scss-font-lock-keywords
;; Variables
'(("$[a-z_-][a-z-_0-9]*" . font-lock-constant-face)))
(defconst scss-comment-start-skip
"\\(//+\\|/\\*+\\)\\s *")
(defconst scss-comment-prefix-regexp
"//+\\|\\**")
(defconst scss-comment-start-regexp
"/[*/]\\|\\s|")
(defconst scss-paragraph-start
"\\(@[[:alpha:]]+\\>\\|$\\)")
(defconst scss-syntactic-ws-start
"\\s \\|/[*/]\\|[\n\r]\\|\\\\[\n\r]\\|\\s!\\|<!--\\|^\\s-*-->")
(defconst scss-syntactic-ws-end
"\\s \\|[\n\r/]\\|\\s!")
(defconst scss-syntactic-eol
(concat "\\s *\\(/\\*[^*\n\r]*"
"\\(\\*+[^*\n\r/][^*\n\r]*\\)*"
"\\*+/\\s *\\)*"
"\\(//\\|/\\*[^*\n\r]*"
"\\(\\*+[^*\n\r/][^*\n\r]*\\)*$"
"\\|\\\\$\\|$\\)"))
(defun scss-compile-maybe()
"Runs `scss-compile' on if `scss-compile-at-save' is t"
(if scss-compile-at-save
(scss-compile)))
(defun scss-compile()
"Compiles the directory belonging to the current buffer, using the --update option"
(interactive)
(compile (concat scss-sass-command " " (mapconcat 'identity scss-sass-options " ") " --update "
(when (string-match ".*/" buffer-file-name)
(concat "'" (match-string 0 buffer-file-name) "'"))
(when scss-output-directory
(concat ":'" scss-output-directory "'")))))
;;;###autoload
(define-derived-mode scss-mode css-mode "SCSS"
"Major mode for editing SCSS files, http://sass-lang.com/
Special commands:
\\{scss-mode-map}"
(font-lock-add-keywords nil scss-font-lock-keywords)
;; Add the single-line comment syntax ('//', ends with newline)
;; as comment style 'b' (see "Syntax Flags" in elisp manual)
(modify-syntax-entry ?/ ". 124" css-mode-syntax-table)
(modify-syntax-entry ?* ". 23b" css-mode-syntax-table)
(modify-syntax-entry ?\n ">" css-mode-syntax-table)
;; Prefer single-line comments for `comment-region'. These variables are also
;; used by `fill-paragraph' for single-line comments.
(setq-local comment-start "//")
(setq-local comment-end "")
(setq-local comment-start-skip scss-comment-start-skip)
(setq-local comment-end-skip nil) ; Override `css-mode'.
;; Some variables needed by `cc-engine' for `fill-paragraph', etc. Copied
;; from `js2-mode'.
(setq-local fill-paragraph-function 'c-fill-paragraph)
(setq-local comment-line-break-function 'c-indent-new-comment-line)
(setq-local c-comment-prefix-regexp scss-comment-prefix-regexp)
(setq c-comment-start-regexp scss-comment-start-regexp)
(setq c-line-comment-starter "//")
(setq c-paragraph-start scss-paragraph-start)
(setq c-paragraph-separate "$")
(setq c-syntactic-ws-start scss-syntactic-ws-start)
(setq c-syntactic-ws-end scss-syntactic-ws-end)
(setq c-syntactic-eol scss-syntactic-eol)
(let ((c-buffer-is-cc-mode t))
;; Copied from `js-mode'. Also see Bug#6071.
(make-local-variable 'paragraph-start)
(make-local-variable 'paragraph-separate)
(make-local-variable 'paragraph-ignore-fill-prefix)
(make-local-variable 'adaptive-fill-mode)
(make-local-variable 'adaptive-fill-regexp)
(c-setup-paragraph-variables))
(add-to-list 'compilation-error-regexp-alist scss-compile-error-regex)
(add-hook 'after-save-hook 'scss-compile-maybe nil t))
(define-key scss-mode-map "\C-c\C-c" 'scss-compile)
;; Weirdly this is still needed when `comment-line-break-function' is bound.
(define-key scss-mode-map (kbd "M-j") 'c-indent-new-comment-line)
(defun flymake-scss-init ()
"Flymake support for SCSS files"
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
(list scss-sass-command (append scss-sass-options (list "--scss" "--check" local-file)))))
(push '(".+\\.scss$" flymake-scss-init) flymake-allowed-file-name-masks)
;;;; TODO: Not possible to use multiline regexs flymake? flymake-err-[line]-patterns
;; '("Syntax error:\s*\\(.*\\)\n\s*on line\s*\\([0-9]+\\) of \\([^ ]+\\)$" 3 2 nil 1)
(push '("on line \\([0-9]+\\) of \\([^ ]+\\)$" 2 1 nil 2) flymake-err-line-patterns)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.scss\\'" . scss-mode))
(provide 'scss-mode)
;;; scss-mode.el ends here