1;;; less-css-mode.el --- Major mode for editing Less CSS files  -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2011-2021 Free Software Foundation, Inc.
4
5;; Author: Steve Purcell <steve@sanityinc.com>
6;; Maintainer: Simen Heggestøyl <simenheg@gmail.com>
7;; Keywords: hypermedia
8
9;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24;;; Commentary:
25
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.
30;;
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".
34;;
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"
38;; executable.
39;;
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.
43;;
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.
47;;
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.:
51;;
52;; // -*- less-css-compile-at-save: t; less-css-output-directory: "../css" -*-
53;;
54;; Alternatively, you can use directory local variables to set the
55;; default value of `less-css-output-directory' for your project.
56;;
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.
61;;
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.
65
66;;; Credits
67
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
71
72;;; Code:
73
74(require 'compile)
75(require 'css-mode)
76(require 'derived)
77(eval-when-compile (require 'subr-x))
78
79(defgroup less-css nil
80  "Less CSS mode."
81  :version "26.1"
82  :prefix "less-css-"
83  :group 'css)
84
85(defcustom less-css-lessc-command "lessc"
86  "Command used to compile Less files.
87Should be \"lessc\" or the complete path to your lessc
88executable, e.g.: \"~/.gem/ruby/1.8/bin/lessc\"."
89  :type 'file)
90
91(defcustom less-css-compile-at-save nil
92  "If non-nil, Less buffers are compiled to CSS after each save."
93  :type 'boolean)
94;;;###autoload
95(put 'less-css-compile-at-save 'safe-local-variable 'booleanp)
96
97(defcustom less-css-lessc-options '("--no-color")
98  "Command line options for Less executable.
99Use \"-x\" to minify output."
100  :type '(repeat string))
101;;;###autoload
102(put 'less-css-lessc-options 'safe-local-variable t)
103
104(defcustom less-css-output-directory nil
105  "Directory in which to save CSS, or nil to use the Less file's directory.
106This path is expanded relative to the directory of the Less file
107using `expand-file-name', so both relative and absolute paths
108will work as expected."
109  :type '(choice (const :tag "Same as Less file" nil) directory))
110;;;###autoload
111(put 'less-css-output-directory 'safe-local-variable 'stringp)
112
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.
115This can be also be set to a full path, or a relative path.  If
116the path is relative, it will be relative to the value of
117`less-css-output-dir', if set, or the current directory by
118default."
119  :type '(choice (const :tag "Default" nil) file))
120(make-variable-buffer-local 'less-css-output-file-name)
121
122(defcustom less-css-input-file-name nil
123  "File name which will be compiled to CSS.
124When the current buffer is saved `less-css-input-file-name' file
125will be compiled to CSS instead of the current file.
126
127Set this in order to trigger compilation of a \"master\" .less
128file which includes the current file.  The best way to set this
129variable in most cases is likely to be via directory local
130variables.
131
132This can be also be set to a full path, or a relative path.  If
133the path is relative, it will be relative to the current
134directory by default."
135  :type '(choice (const nil) file))
136;;;###autoload
137(put 'less-css-input-file-name 'safe-local-variable 'stringp)
138(make-variable-buffer-local 'less-css-input-file-name)
139
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\\)?")
142
143;;; Compilation to CSS
144
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)
148
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
152    (less-css-compile)))
153
154(defun less-css--output-path ()
155  "Return the path to use for the compiled CSS file."
156  (expand-file-name
157   (or less-css-output-file-name
158       (concat
159        (file-name-nondirectory
160         (file-name-sans-extension buffer-file-name))
161        ".css"))
162   (or less-css-output-directory default-directory)))
163
164(defun less-css-compile ()
165  "Compile the current buffer to CSS using `less-css-lessc-command'."
166  (interactive)
167  (message "Compiling Less to CSS")
168  (let ((compilation-buffer-name-function
169         (lambda (_) "*less-css-compilation*")))
170    (save-window-excursion
171      (with-current-buffer
172          (compile
173           (string-join
174            (append
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))))
180            " "))
181        (add-hook 'compilation-finish-functions
182                  (lambda (buf msg)
183                    (unless (string-match-p "^finished" msg)
184                      (display-buffer buf)))
185                  nil
186                  t)))))
187
188;;; Major mode
189
190;; TODO:
191;; - interpolation ("@{val}")
192;; - escaped values (~"...")
193;; - JS eval (~`...`)
194;; - custom faces.
195(defconst less-css-font-lock-keywords
196  '(;; Variables
197    ("@[a-z_-][a-z_0-9-]*" . font-lock-variable-name-face)
198    ("&" . font-lock-preprocessor-face)
199    ;; Mixins
200    ("\\(?:[ \t{;]\\|^\\)\\(\\.[a-z_-][a-z_0-9-]*\\)[ \t]*;" .
201     (1 font-lock-keyword-face))))
202
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)
211    st))
212
213(defvar less-css-mode-map
214  (let ((map (make-sparse-keymap)))
215    (define-key map "\C-c\C-c" 'less-css-compile)
216    map))
217
218;;;###autoload (add-to-list 'auto-mode-alist '("\\.less\\'" . less-css-mode))
219;;;###autoload
220(define-derived-mode less-css-mode css-mode "Less"
221  "Major mode for editing Less files (http://lesscss.org/).
222Special commands:
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))
231
232(provide 'less-css-mode)
233;;; less-css-mode.el ends here
234