all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 8a981d67b93840581b792d3042795c0d3d3df63f 9767 bytes (raw)
name: lisp/textmodes/less-css-mode.el 	 # note: path name is non-authoritative(*)

  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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
 
;;; less-css-mode.el --- Major mode for editing LESS CSS files (lesscss.org)
;;
;; Copyright (C) 2011-2014 Steve Purcell
;;
;; Author: Steve Purcell <steve@sanityinc.com>
;; URL: https://github.com/purcell/less-css-mode
;; Keywords: less css mode
;; Version: DEV
;;
;; 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:
;;
;; This mode provides syntax highlighting for LESS CSS files, plus
;; optional support for compilation of .less files to .css files at
;; the time they are saved: use `less-css-compile-at-save' to enable
;; this.
;;
;; Command line utility "lessc" is required if setting
;; `less-css-compile-at-save' to t.  To install "lessc" using the
;; Node.js package manager, run "npm install less"
;;
;; Also make sure the "lessc" executable is in Emacs' PATH, example:
;; (setq exec-path (cons (expand-file-name "~/.gem/ruby/1.8/bin") exec-path))
;; or customize `less-css-lessc-command' to point to your "lessc" executable.
;;
;; We target lessc >= 1.4.0, and thus use the `--no-color' flag by
;; default.  You may want to adjust `less-css-lessc-options' for
;; compatibility with older versions.
;;
;; `less-css-mode' is derived from `css-mode', and indentation of
;; nested blocks may not work correctly with versions of `css-mode'
;; other than that bundled with recent Emacs.
;;
;; You can specify per-file values for `less-css-compile-at-save',
;; `less-css-output-file-name' or `less-css-output-directory' using a
;; variables header at the top of your .less file, e.g.:
;;
;; // -*- less-css-compile-at-save: t; less-css-output-directory: "../css" -*-
;;
;; Alternatively, you can use directory local variables to set the
;; default value of `less-css-output-directory' for your project.
;;
;; In the case of files which are included in other .less files, you
;; may want to trigger the compilation of a "master" .less file on
;; save: you can accomplish this with `less-css-input-file-name',
;; which is probably best set using directory local variables.
;;
;; If you don't need CSS output but would like to be warned of any
;; syntax errors in your .less source, consider using `flymake-less':
;; https://github.com/purcell/flymake-less
;;
;;; Credits
;;
;; The original code for this mode was, in large part, written using
;; Anton Johansson's scss-mode as a template -- thanks Anton!
;; https://github.com/antonj
;;
;;; Code:

(require 'derived)
(require 'compile)

;; There are at least three css-mode.el implementations, but we need
;; the right one in order to work as expected, not the versions by
;; Landström or Garshol

(require 'css-mode)
(unless (or (boundp 'css-navigation-syntax-table)
            (functionp 'css-smie-rules))
  (error "Wrong css-mode.el: please use the version by Stefan Monnier, bundled with Emacs >= 23"))

(defgroup less-css nil
  "Less-css mode"
  :prefix "less-css-"
  :group 'css)

;;;###autoload
(defcustom less-css-lessc-command "lessc"
  "Command used to compile LESS files.
Should be lessc or the complete path to your lessc executable,
  e.g.: \"~/.gem/ruby/1.8/bin/lessc\""
  :type 'file
  :group 'less-css
  :safe 'stringp)

;;;###autoload
(defcustom less-css-compile-at-save nil
  "If non-nil, the LESS buffers will be compiled to CSS after each save."
  :type 'boolean
  :group 'less-css
  :safe 'booleanp)

;;;###autoload
(defcustom less-css-lessc-options '("--no-color")
  "Command line options for less executable.

Use \"-x\" to minify output."
  :type '(repeat string)
  :group 'less-css
  :safe t)

;;;###autoload
(defcustom less-css-output-directory nil
  "Directory in which to save CSS, or nil to use the LESS file's directory.

This path is expanded relative to the directory of the LESS file
using `expand-file-name', so both relative and absolute paths
will work as expected."
  :type 'directory
  :group 'less-css
  :safe 'stringp)

;;;###autoload
(defcustom less-css-output-file-name nil
  "File name in which to save CSS, or nil to use <name>.css for <name>.less.

This can be also be set to a full path, or a relative path.  If
the path is relative, it will be relative to the value of
`less-css-output-dir', if set, or the current directory by
default."
  :type 'file
  :group 'less-css
  :safe 'stringp)
(make-variable-buffer-local 'less-css-output-file-name)

;;;###autoload
(defcustom less-css-input-file-name nil
  "File name which will be compiled to CSS.

When the current buffer is saved `less-css-input-file-name' file
will be compiled to css instead of the current file.

Set this in order to trigger compilation of a \"master\" .less
file which includes the current file.  The best way to set this
variable in most cases is likely to be via directory local
variables.

This can be also be set to a full path, or a relative path. If
the path is relative, it will be relative to the the current directory by
default."
  :type 'file
  :group 'less-css
  :safe 'stringp)
(make-variable-buffer-local 'less-css-input-file-name)

(defconst less-css-default-error-regex
  "^\\(?:\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\\)?")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Compilation to CSS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(add-to-list 'compilation-error-regexp-alist-alist
             (list 'less-css less-css-default-error-regex 2 3 4 nil 1))
(add-to-list 'compilation-error-regexp-alist 'less-css)


(defun less-css-compile-maybe ()
  "Run `less-css-compile' if `less-css-compile-at-save' is non-nil."
  (if less-css-compile-at-save
      (less-css-compile)))

(defun less-css--output-path ()
  "Calculate the path for the compiled CSS file created by `less-css-compile'."
  (expand-file-name (or less-css-output-file-name
                        (concat (file-name-nondirectory (file-name-sans-extension buffer-file-name)) ".css"))
                    (or less-css-output-directory default-directory)))

(defun less-css--maybe-shell-quote-command (command)
  "Selectively shell-quote COMMAND appropriately for `system-type'."
  (funcall (if (eq system-type 'windows-nt)
               'identity
             'shell-quote-argument) command))

;;;###autoload
(defun less-css-compile ()
  "Compiles the current buffer to css using `less-css-lessc-command'."
  (interactive)
  (message "Compiling less to css")
  (let ((compilation-buffer-name-function (lambda (mode-name) "*less-css-compilation*")))
    (save-window-excursion
      (with-current-buffer
          (compile
           (mapconcat 'identity
                      (append (list (less-css--maybe-shell-quote-command less-css-lessc-command))
                              (mapcar 'shell-quote-argument less-css-lessc-options)
                              (list (shell-quote-argument
                                     (or less-css-input-file-name buffer-file-name))
                                    (shell-quote-argument (less-css--output-path))))
                      " "))
        (add-hook 'compilation-finish-functions
                  (lambda (buf msg)
                    (unless (string-match-p "^finished" msg)
                      (display-buffer buf)))
                  nil
                  t)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Minor mode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; TODO: interpolation ("@{val}"), escaped values (~"..."), JS eval (~`...`), custom faces
(defconst less-css-font-lock-keywords
  '(;; Variables
    ("@[a-z_-][a-z-_0-9]*" . font-lock-constant-face)
    ("&" . font-lock-preprocessor-face)
    ;; Mixins
    ("\\(?:[ \t{;]\\|^\\)\\(\\.[a-z_-][a-z-_0-9]*\\)[ \t]*;" . (1 font-lock-keyword-face)))
  )

;;;###autoload
(define-derived-mode less-css-mode css-mode "LESS"
  "Major mode for editing LESS files, http://lesscss.org/
Special commands:
\\{less-css-mode-map}"
  (font-lock-add-keywords nil less-css-font-lock-keywords)
  ;; cpp-style comments
  (modify-syntax-entry ?/ ". 124b" less-css-mode-syntax-table)
  (modify-syntax-entry ?* ". 23" less-css-mode-syntax-table)
  (modify-syntax-entry ?\n "> b" less-css-mode-syntax-table)
  ;; Special chars that sometimes come at the beginning of words.
  (modify-syntax-entry ?. "'" less-css-mode-syntax-table)

  (set (make-local-variable 'comment-start) "//")
  (set (make-local-variable 'comment-end) "")
  (set (make-local-variable 'indent-line-function) 'less-css-indent-line)
  (when (functionp 'css-smie-rules)
    (smie-setup css-smie-grammar #'css-smie-rules
                :forward-token #'css-smie--forward-token
                :backward-token #'css-smie--backward-token))

  (add-hook 'after-save-hook 'less-css-compile-maybe nil t))

(define-key less-css-mode-map "\C-c\C-c" 'less-css-compile)

(defun less-css-indent-line ()
  "Indent current line according to LESS CSS indentation rules."
  (let ((css-navigation-syntax-table less-css-mode-syntax-table))
    (if (fboundp 'css-indent-line)
        (css-indent-line)
      (smie-indent-line))))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.less\\'" . less-css-mode))


(provide 'less-css-mode)
;;; less-css-mode.el ends here

debug log:

solving 8a981d67b9 ...
found 8a981d67b9 in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.