Actually,simplicity is not simple
2010-03-22Tool
VIM

给VIM添加jsLint时遇到文件路径有空格时会报错

这问题是我2010年二月份遇到的,也跑到VIM-cn上发贴求助,但无果,整个问题是这样的:

但我不喜欢把东西加到环境变量中,看到下面回复的有修改let g:jslint_command 达到目的的, 就决定使用这种方式,将jsLint下载下来后我放到了VIM程序目录,与VIM、vimfiles并列,但我发现这样使用之后一直报D:\program错误,这应该与它后面带个空格有关。

今天再次蛋痛折腾起这个问题来,终于让我想到种方法了,

  let jsl_command = substitute(g:jslint_command, 'Program Files', 'Progra~1', 'g') . ' ' . g:jslint_command_options . ' ' . current_file

呵呵,就这样,我是直接修改javascriptLint.vim实现的,不知有没有更好的方法,反正是实现了我要的目的,不管了。

另,编码如果是UTF-8会有问题,要转一下:

  if has("win32") && v:lang == 'zh_CN.utf-8'
    let jsl_command = iconv(jsl_command, 'utf-8', 'gbk')
  endif

  if has("win32") && v:lang == 'zh_CN.utf-8'
    let cmd_output = iconv(cmd_output, 'gbk', 'utf-8')
  endif

整个插件修改后的样子:

" File:         javascriptLint.vim
" Author:       Joe Stelmach (joe@zenbe.com)
" Version:      0.3a1
" Description:  javascriptLint.vim allows the JavaScript Lint (jsl) program
"               from http://www.javascriptlint.com/ to be tightly integrated
"               with vim.  The contents of a javascript file will be passed
"               through the jsl program after the file's buffer is saved.
"               Any lint warnings will be placed in the quickfix window.
"               JavaScript Lint must be installed on your system for this
"               plugin to work properly.  This page should get you started:
"               http://www.javascriptlint.com/docs/index.htm
"
"               Modifications were made by smith (nlloyds@gmail.com) to make
"               the program called from a command instead of upon saving.
"               Other minor modifcations were also made
"
" Last Modified: December 28, 2009

if !exists("jslint_command")
  let jslint_command = 'jsl'
endif

if !exists("jslint_command_options")
  let jslint_command_options = '-nofilelisting -nocontext -nosummary -nologo -process'
endif

if !exists("jslint_highlight_color")
  let jslint_highlight_color = 'DarkMagenta'
endif

" set up auto commands
" Commented out by smith
"autocmd BufWritePost,FileWritePost *.js call JavascriptLint()
"autocmd BufWinLeave * call s:MaybeClearCursorLineColor()

" set up commands
command! JavaScriptLint call JavascriptLint()
command! JavaScriptLintClear call s:ClearCursorLineColor()

" Runs the current file through javascript lint and 
" opens a quickfix window with any warnings
" TODO: Make this work with ranges
function! JavascriptLint() 
  " run javascript lint on the current file
  let current_file = shellescape(expand('%:p'))

  if has("win32")
      let g:jslint_command = substitute(g:jslint_command, 'Program Files', 'Progra~1', 'g')
  endif
  let jsl_command =  g:jslint_command . ' ' . g:jslint_command_options . ' ' . current_file
  " echo jsl_command

  if has("win32") && v:lang == 'zh_CN.utf-8'
    let jsl_command = iconv(jsl_command, 'utf-8', 'gbk')
  endif

  let cmd_output = system(jsl_command)

  if has("win32") && v:lang == 'zh_CN.utf-8'
    let cmd_output = iconv(cmd_output, 'gbk', 'utf-8')
  endif

  " if some warnings were found, we process them
  if strlen(cmd_output) > 0

    " ensure proper error format
    let s:errorformat = "%f(%l):\%m^M"

    " write quickfix errors to a temp file 
    let quickfix_tmpfile_name = tempname()
    exe "redir! > " . quickfix_tmpfile_name
      silent echon cmd_output
    redir END

    " read in the errors temp file 
    execute "silent! cfile " . quickfix_tmpfile_name

    " change the cursor line to something hard to miss 
    call s:SetCursorLineColor()

    " open the quicfix window
    botright copen
    let s:qfix_buffer = bufnr("$")

    " delete the temp file
    call delete(quickfix_tmpfile_name)

  " if no javascript warnings are found, we revert the cursorline color
  " and close the quick fix window
  else 
    call s:ClearCursorLineColor()
    if(exists("s:qfix_buffer"))
      cclose
      unlet s:qfix_buffer
    endif
  endif
endfunction

" sets the cursor line highlight color to the error highlight color 
" FIXME: This doesn't work for me
function! s:SetCursorLineColor() 
  " check for disabled cursor line
  if(!exists("g:jslint_highlight_color") || strlen(g:jslint_highlight_color) == 0) 
    return 
  endif

  call s:ClearCursorLineColor()
  let s:highlight_on = 1 

  " find the current cursor line highlight info 
  redir => l:highlight_info
    silent highlight CursorLine
  redir END

  " find the guibg property within the highlight info (if it exists)
  let l:start_index = match(l:highlight_info, "guibg")
  if(l:start_index > 0)
    let s:previous_cursor_guibg = strpart(l:highlight_info, l:start_index)

  elseif(exists("s:previous_cursor_guibg")) 
    unlet s:previous_cursor_guibg
  endif

  execute "highlight CursorLine guibg=" . g:jslint_highlight_color
endfunction

" Conditionally reverts the cursor line color based on the presence
" of the quickfix window
function! s:MaybeClearCursorLineColor()
  if(exists("s:qfix_buffer") && s:qfix_buffer == bufnr("%"))
    call s:ClearCursorLineColor()
  endif
endfunction

" Reverts the cursor line color
function! s:ClearCursorLineColor()
  " only revert if our highlight is currently enabled
  if(exists("s:highlight_on") && s:highlight_on) 
    let s:highlight_on = 0 

    " if a previous cursor guibg color was recorded, we use it
    if(exists("s:previous_cursor_guibg")) 
      execute "highlight CursorLine " . s:previous_cursor_guibg
      unlet s:previous_cursor_guibg

    " otherwise, we clear the curor line highlight entirely
    else
      highlight clear CursorLine 
    endif
  endif
endfunction

现在使用这个插件就不用在环境变量里设置了,可以在_vimrc中这样写:

let g:jslint_command = $VIM.'/jsLint/jsl.exe' 

注:我是把jsLint放到了VIM程序的同一目录下

日志信息 »

该日志于2010-03-22 19:14由 asins 发表在Tool分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过评论 RSS订阅这个日志的所有评论。

相关日志 »

1 条评论

  1. aben aben November 5th, 2010 at 04:14 pm

    THX!
    今天按照你的方法修改,老是有问题- -!

    最后绝望的时候直接复制粘贴,竟然...
    Done!

    ps.你的验证码好变态- -

发表评论 »

captcha
请输入验证码