Tuesday, August 16, 2011

Re: functions in vim does't respond to % or %<

On Aug 16, 5:29 am, zhufeng <iwantcompu...@gmail.com> wrote:
> hey guys, I want to write a function in _vimrc to compile some source
> code file .
>
> For example, I want to use "set makeprg=javac\ %" to compile .java
> file, and if there is no error I wanna vim to continue run the "java
> %<", and if there are any errors , I wanna vim to open the quickfix
> windows automatically.  So , the problem is how to judge whether there
> is error after compile . I wanna use vim's function filereadable() to
> judge whether there is any error (if there is error , javac will not
> create a class file ), but when I try to use this function, I found
> that the function filereadable() can't receive arguments like % and
> %<,  the result is , ":echo filereadable("test.class")" is 1, but
> ":echo filereadable("%<.class")" is 0 .The current file I am editing
> is test.java, so this confused me a lot .
>
> I wonder whether fuctions like filereadable() can receive arguments
> like % or %<, and anyone who can help me to write my own function ?
> Thanks a lot .
>
> sorry for my poor English , I hope you guys can understand what I
> mean .

You can use the expand() function in conjunction with filereadable().

Note that %:r is preferred over %< which is only included for
backwards compatibility (see :help extension-removal, an extension
of :help filename-modifiers).

However, if you have your 'errorformat' option set properly, you can
use Vim to detect when there were errors. Simply use :cwindow which
will only open the error list if there are any errors. Then you can
either check that the window is open, or more robustly, you can
probably check that getqflist() returns an empty list (I have not
tried this).

There is a 'javac' compiler plugin distributed with Vim which may set
both the makeprg and errorformat for you automatically. You should
check it out. Just issue the command, :compiler! javac to use it.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: