From 27d3495108ce909374b4b4b22232f5d52464c72f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=96=E9=82=80?= Date: Fri, 3 Aug 2012 16:40:14 +0800 Subject: [PATCH] first commit. --- ftdetect/less.vim | 7 ++++ ftplugin/less.vim | 46 ++++++++++++++++++++++++++ indent/less.vim | 12 +++++++ syntax/less.vim | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 ftdetect/less.vim create mode 100644 ftplugin/less.vim create mode 100644 indent/less.vim create mode 100644 syntax/less.vim diff --git a/ftdetect/less.vim b/ftdetect/less.vim new file mode 100644 index 0000000..dcefe90 --- /dev/null +++ b/ftdetect/less.vim @@ -0,0 +1,7 @@ +" Vim ftdetect fle +" Language: Less +" Author: Kohpoll (http://www.cnblogs.com/kohpoll/) +" Licensed under MIT. +" Last Modified: 2012-08-03 + +autocmd BufNewFile,BufRead *.less setfiletype less diff --git a/ftplugin/less.vim b/ftplugin/less.vim new file mode 100644 index 0000000..cf6167d --- /dev/null +++ b/ftplugin/less.vim @@ -0,0 +1,46 @@ +" Vim file type fle +" Language: Less +" Author: Kohpoll (http://www.cnblogs.com/kohpoll/) +" Licensed under MIT. +" Last Modified: 2012-08-03 + +if exists("b:did_ftplugin") + finish +endif + +let b:did_ftplugin = 1 + +" Enable LessMake if it won't overwrite any settings. +" if !len(&l:makeprg) +" compiler less +" endif + +" FIXME: can not se the correct errorformat for lessc produce the error string +" with escape character like(echo in console with color): +" \033[31mParseError: missing closing `}` \033[39m\033[31m in \033[39mD:\PlayGround\x.less\033[90m:152:1\033[39m +" \033[90m151 }\033[39m\033[0m +" so, do not use the compiler, I just substitute all the escape string with empty string and echo it. = = + +" compile the curren less fileļ¼Œsave it as css with the same file name,echo the error. +func! s:CompileLess() + let l:input = fnameescape(expand("%:p")) + let l:output = fnameescape(expand("%:p:r") . ".css") + + let l:cmd = "lessc " . l:input . " " . l:output + + let l:errs = system(l:cmd) + + if (!empty(l:errs)) + " replace the escape string(\%oxxx match the octal character). e.g: \033[33m + let l:errs = substitute(l:errs, "\\%o033[\\d\\+m", "", "g") + " replace the blank lines + let l:errs = substitute(l:errs, "^$", "", "g") + " we jsut need the error message + let l:errs = split(l:errs, "\\n")[0] + + echo l:errs + endif +endfunc + +" compile less when saving. +autocmd! BufWritePost,FileWritePost *.less call s:CompileLess() diff --git a/indent/less.vim b/indent/less.vim new file mode 100644 index 0000000..fca4475 --- /dev/null +++ b/indent/less.vim @@ -0,0 +1,12 @@ +" Vim indent fle +" Language: Less +" Author: Kohpoll (http://www.cnblogs.com/kohpoll/) +" Licensed under MIT. +" Last Modified: 2012-08-03 + +if exists("b:did_indent") + finish +endif + +" use css indent is enough +runtime! indent/css.vim diff --git a/syntax/less.vim b/syntax/less.vim new file mode 100644 index 0000000..7707225 --- /dev/null +++ b/syntax/less.vim @@ -0,0 +1,82 @@ +" Vim syntax fle +" Language: Less +" Author: Kohpoll (http://www.cnblogs.com/kohpoll/) +" Inspired by the syntax files of scss and css. +" Licensed under MIT. +" Last Modified: 2012-08-03 + +if exists("b:current_syntax") && b:current_syntax == "less" + finish +endif + +" use the css syntax and then enhance it.>.< +runtime! syntax/css.vim + +syn case ignore + +" css props and attrs +syn cluster lessCssProperties contains=css.*Prop +syn cluster lessCssAttributes contains=css.*Attr,cssValue.*,cssColor,cssURL,cssImportant,cssErr,cssStringQ,cssStringQQ,lessComment + +syn region lessDefinition matchgroup=cssBraces start='{' end='}' contains=TOP + +" less props (contain in less definition) +" (?<=[{};]\s*|^\s*)([\w-])+\s*: +syn match lessProperty "\%([{};]\s*\|^\s*\)\@<=\%([[:alnum:]-]\)\+\s*:" contains=@lessCssProperties skipwhite nextgroup=lessAttribute contained containedin=lessDefinition + +" less attrs (contains all the css attr, less variable, less functinos, less string interpolation) +" ([^{};])* +syn match lessAttribute "\%([^{};]\)*" contained contains=@lessCssAttributes,lessVariable,lessFunction,lessInterpolation + +" variable: @variable-name: variable-value +syn match lessVariable "@\{1,2}[[:alnum:]_-]\+" +" (?<=@{1,2}[\w_-]+\s*): +syn match lessVariableAssignment "\%(@\{1,2}[[:alnum:]_-]\+\s*\)\@<=:" nextgroup=lessAttribute skipwhite +hi def link lessVariable Identifier + +" mixin: .mixin (arguments) when (condition) +syn match lessMixin "\.[[:alnum:]_-]\+" skipwhite nextgroup=lessMixinArguments +syn match lessMixinArguments "([^)]*)" contained contains=lessAttribute skipwhite nextgroup=lessMixinWhen +syn match lessMixinWhen "when" contained nextgroup=lessMixinGuard skipwhite +syn match lessMixinGuard "([^)]*)" contained contains=lessAttribute skipwhite nextgroup=lessDefinition +hi def link lessMixin Statement +" FIXME: can not highlight `when`... why? = = +hi def link lessMixinWhen Identifier + +" & syntax +syn match lessAmpersand "&" +hi def link lessAmpersand Character + +" include +" me=e-1 means the last char of the match does not highlighted.(i.e the ';') +" me: match end +syn region lessInclude start="@import" end=";\|$"me=e-1 contains=lessComment,cssStringQ,cssStringQQ,cssURL,cssUnicodeEscape,cssMediaType +hi def link lessInclude Include + +" string interpolation +" @{variable-name} +syn region lessInterpolation matchgroup=lessInterpolationDelimiter start="@{" end="}" contains=@lessCssAttributes containedin=cssStringQ,cssStringQQ +" %("xxxxx", varialble-a, variable-b) +syn region lessInterpolation matchgroup=lessInterpolationDelimiter start="%(" end=")" contains=@lessCssAttributes,lessVariable,cssStringQ,cssStringQQ +hi def link lessInterpolationDelimiter Constant + +" functions +syn keyword lessFunction lighten darken saturate desaturate fadein fadeout fade mix spin hsl hue saturation lightness contained +syn keyword lessFunction round ceil floor percentage contained +syn keyword lessFunction iscolor isnumber isstring iskeyword isurl ispixel ispercentage isem contained +hi def link lessFunction Function + +" comments +" TODO: well~ taken from sass syntax file, I can not understand it now. >.< +" pattern\@! => (?!pattern), \%(pattern\) => (?:pattern) +syn keyword lessTodo FIXME NOTE TODO OPTIMIZE XXX contained +syn region lessComment start="^\z(\s*\)//" end="^\%(\z1 \)\@!" contains=lessTodo,@Spell +syn region lessCssComment start="^\z(\s*\)/\*" end="^\%(\z1 \)\@!" contains=lessTodo,@Spell +hi def link lessCssComment lessComment +hi def link lessComment Comment +hi def link lessTodo Todo + + +if !exists("b:current_syntax") + let b:current_syntax = "less" +endif