Skip to content

Commit

Permalink
first commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
KohPoll committed Aug 3, 2012
0 parents commit 27d3495
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 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
46 changes: 46 additions & 0 deletions 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()
12 changes: 12 additions & 0 deletions 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
82 changes: 82 additions & 0 deletions 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

0 comments on commit 27d3495

Please sign in to comment.