1 ;;; less-css-mode.el --- Major mode for editing Less CSS files -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2018 Free Software Foundation, Inc.
5 ;; Author: Steve Purcell <steve@sanityinc.com>
6 ;; Maintainer: Simen Heggestøyl <simenheg@gmail.com>
7 ;; Keywords: hypermedia
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;; This mode provides syntax highlighting for Less CSS files
27 ;; (http://lesscss.org/), plus optional support for compilation of
28 ;; .less files to .css files at the time they are saved: use
29 ;; `less-css-compile-at-save' to enable this.
31 ;; Command line utility "lessc" is required if setting
32 ;; `less-css-compile-at-save' to t. To install "lessc" using the
33 ;; Node.js package manager, run "npm install less".
35 ;; Also make sure the "lessc" executable is in Emacs' PATH, example:
36 ;; (push (expand-file-name "~/.gem/ruby/1.8/bin") exec-path)
37 ;; or customize `less-css-lessc-command' to point to your "lessc"
40 ;; We target lessc >= 1.4.0, and thus use the `--no-color' flag by
41 ;; default. You may want to adjust `less-css-lessc-options' for
42 ;; compatibility with older versions.
44 ;; `less-css-mode' is derived from `css-mode', and indentation of
45 ;; nested blocks may not work correctly with versions of `css-mode'
46 ;; other than that bundled with recent Emacs.
48 ;; You can specify per-file values for `less-css-compile-at-save',
49 ;; `less-css-output-file-name' or `less-css-output-directory' using a
50 ;; variables header at the top of your .less file, e.g.:
52 ;; // -*- less-css-compile-at-save: t; less-css-output-directory: "../css" -*-
54 ;; Alternatively, you can use directory local variables to set the
55 ;; default value of `less-css-output-directory' for your project.
57 ;; In the case of files which are included in other .less files, you
58 ;; may want to trigger the compilation of a "master" .less file on
59 ;; save: you can accomplish this with `less-css-input-file-name',
60 ;; which is probably best set using directory local variables.
62 ;; If you don't need CSS output but would like to be warned of any
63 ;; syntax errors in your .less source, consider using `flymake-less':
64 ;; https://github.com/purcell/flymake-less.
68 ;; The original code for this mode was, in large part, written using
69 ;; Anton Johansson's scss-mode as a template -- thanks Anton!
70 ;; https://github.com/antonj
77 (eval-when-compile (require 'subr-x
))
79 (defgroup less-css nil
85 (defcustom less-css-lessc-command
"lessc"
86 "Command used to compile Less files.
87 Should be \"lessc\" or the complete path to your lessc
88 executable, e.g.: \"~/.gem/ruby/1.8/bin/lessc\"."
91 (defcustom less-css-compile-at-save nil
92 "If non-nil, Less buffers are compiled to CSS after each save."
95 (put 'less-css-compile-at-save
'safe-local-variable
'booleanp
)
97 (defcustom less-css-lessc-options
'("--no-color")
98 "Command line options for Less executable.
99 Use \"-x\" to minify output."
100 :type
'(repeat string
))
102 (put 'less-css-lessc-options
'safe-local-variable t
)
104 (defcustom less-css-output-directory nil
105 "Directory in which to save CSS, or nil to use the Less file's directory.
106 This path is expanded relative to the directory of the Less file
107 using `expand-file-name', so both relative and absolute paths
108 will work as expected."
109 :type
'(choice (const :tag
"Same as Less file" nil
) directory
))
111 (put 'less-css-output-directory
'safe-local-variable
'stringp
)
113 (defcustom less-css-output-file-name nil
114 "File name in which to save CSS, or nil to use <name>.css for <name>.less.
115 This can be also be set to a full path, or a relative path. If
116 the path is relative, it will be relative to the value of
117 `less-css-output-dir', if set, or the current directory by
119 :type
'(choice (const :tag
"Default" nil
) file
))
120 (make-variable-buffer-local 'less-css-output-file-name
)
122 (defcustom less-css-input-file-name nil
123 "File name which will be compiled to CSS.
124 When the current buffer is saved `less-css-input-file-name' file
125 will be compiled to CSS instead of the current file.
127 Set this in order to trigger compilation of a \"master\" .less
128 file which includes the current file. The best way to set this
129 variable in most cases is likely to be via directory local
132 This can be also be set to a full path, or a relative path. If
133 the path is relative, it will be relative to the current
134 directory by default."
135 :type
'(choice (const nil
) file
))
137 (put 'less-css-input-file-name
'safe-local-variable
'stringp
)
138 (make-variable-buffer-local 'less-css-input-file-name
)
140 (defconst less-css-default-error-regex
141 "^\\(?:\e\\[31m\\)?\\([^\e\n]*\\|FileError:.*\n\\)\\(?:\e\\[39m\e\\[31m\\)? in \\(?:\e\\[39m\\)?\\([^ \r\n\t\e]+\\)\\(?:\e\\[90m\\)?\\(?::\\| on line \\)\\([0-9]+\\)\\(?::\\|, column \\)\\([0-9]+\\):?\\(?:\e\\[39m\\)?")
143 ;;; Compilation to CSS
145 (add-to-list 'compilation-error-regexp-alist-alist
146 (list 'less-css less-css-default-error-regex
2 3 4 nil
1))
147 (add-to-list 'compilation-error-regexp-alist
'less-css
)
149 (defun less-css-compile-maybe ()
150 "Run `less-css-compile' if `less-css-compile-at-save' is non-nil."
151 (when less-css-compile-at-save
154 (defun less-css--output-path ()
155 "Return the path to use for the compiled CSS file."
157 (or less-css-output-file-name
159 (file-name-nondirectory
160 (file-name-sans-extension buffer-file-name
))
162 (or less-css-output-directory default-directory
)))
164 (defun less-css-compile ()
165 "Compile the current buffer to CSS using `less-css-lessc-command'."
167 (message "Compiling Less to CSS")
168 (let ((compilation-buffer-name-function
169 (lambda (_) "*less-css-compilation*")))
170 (save-window-excursion
175 (list less-css-lessc-command
)
176 (mapcar #'shell-quote-argument less-css-lessc-options
)
177 (list (shell-quote-argument
178 (or less-css-input-file-name buffer-file-name
))
179 (shell-quote-argument (less-css--output-path))))
181 (add-hook 'compilation-finish-functions
183 (unless (string-match-p "^finished" msg
)
184 (display-buffer buf
)))
191 ;; - interpolation ("@{val}")
192 ;; - escaped values (~"...")
193 ;; - JS eval (~`...`)
195 (defconst less-css-font-lock-keywords
197 ("@[a-z_-][a-z-_0-9]*" . font-lock-variable-name-face
)
198 ("&" . font-lock-preprocessor-face
)
200 ("\\(?:[ \t{;]\\|^\\)\\(\\.[a-z_-][a-z-_0-9]*\\)[ \t]*;" .
201 (1 font-lock-keyword-face
))))
203 (defvar less-css-mode-syntax-table
204 (let ((st (make-syntax-table css-mode-syntax-table
)))
205 ;; C++-style comments.
206 (modify-syntax-entry ?
/ ". 124b" st
)
207 (modify-syntax-entry ?
* ". 23" st
)
208 (modify-syntax-entry ?
\n "> b" st
)
209 ;; Special chars that sometimes come at the beginning of words.
210 (modify-syntax-entry ?.
"'" st
)
213 (defvar less-css-mode-map
214 (let ((map (make-sparse-keymap)))
215 (define-key map
"\C-c\C-c" 'less-css-compile
)
218 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.less\\'" . less-css-mode))
220 (define-derived-mode less-css-mode css-mode
"Less"
221 "Major mode for editing Less files (http://lesscss.org/).
223 \\{less-css-mode-map}"
224 (font-lock-add-keywords nil less-css-font-lock-keywords
)
225 (setq-local comment-start
"//")
226 (setq-local comment-end
"")
227 (setq-local comment-continue
" *")
228 (setq-local comment-start-skip
"/[*/]+[ \t]*")
229 (setq-local comment-end-skip
"[ \t]*\\(?:\n\\|\\*+/\\)")
230 (add-hook 'after-save-hook
'less-css-compile-maybe nil t
))
232 (provide 'less-css-mode
)
233 ;;; less-css-mode.el ends here