0
$ ls
Makefile          html-page/        page-generator.m4
Run               includes/

除了Makefile,我还有一个脚本Run,只有在make没有错误的情况下才会执行。我已经设法在我的文件中使用以下内容实现了这一点,如果需要,它还会在父目录中.vimrc查找。Makefile

" Before the 'make' quickfix command, run my quickfix pre-commands
autocmd QuickfixCmdPre make call MyQuickfixCmdPre()

" After the 'make' quickfix command, run my quickfix post-commands
autocmd QuickfixCmdPost make call MyQuickfixCmdPost()

function! MyQuickfixCmdPre()
    " Save current buffer, but only if it's been modified
    update

    " (h)ead of (p)ath of % (current buffer), i.e. path of current file
    let l:dir = expand('%:p:h')

    " Remove final / and smack a /Makefile on the end, glob gives empty if file doesn't exist
    while empty(glob(substitute(l:dir, '/$', '', '') . '/Makefile'))
        " There's no Makefile here. Are we at the root dir?
        if l:dir ==# "/"
            " Just use dir of current file then
            let l:dir = '.'
            break
        else
            " Try the parent dir. Get (h)ead of dir, i.e. remove rightmost dir name from it
            let l:dir = fnamemodify(l:dir, ':h')
        endif
    endwhile
    " Makefile is in this dir, so local-cd (only this window) to the dir
    execute "lcd " . l:dir
endfunction

function! MyQuickfixCmdPost()
    " Get number of valid quickfix entries, i.e. number of errors reported,
    " using filter to check the 'valid' flag
    let l:err_count = len(filter(getqflist(), 'v:val.valid'))

    if l:err_count ==# 0
        " The make succeeded. Execute the Run script expected in the same dir as Makefile
        call system('./Run')
        redraw!
    endif
endfunction

有了这个,在输入:makvim 后,代码就生成并运行了……有两种可能的结果:

  1. 如果在 期间出现错误make,vim 会在之后将这些错误呈现出来Press ENTER or type command to continue,这一切都很好。
  2. 但是,如果make成功且没有错误,Run则执行我的脚本,以测试我的代码(在本例中为浏览器中显示的 html 文件),但是当我切换回 vim 时,我必须按下enter以删除来自 vim 的消息我不需要阅读,因为它没有告诉我错误。这条消息过去看起来像这样:
"includes/m4includes/subs.m4" 34L, 759B written
:!make  2>&1| tee /var/folders/zk/0bsgbxne3pe5c86jsbgdt27f3333yd/T/vkbxFyd/255
m4 -I includes/m4includes page-generator.m4 >html-page/mypage.html
(1 of 1): m4 -I includes/m4includes page-generator.m4 >html-page/mypage.html
Press ENTER or type command to continue

但在引入redraw!in后,MyQuickfixCmdPost()现在简化为:

(1 of 1): m4 -I includes/m4includes page-generator.m4 >html-page/mypage.html
Press ENTER or type command to continue

但仍然需要按下enter

编译成功后,我们如何避免enter每次返回 vim 时都必须按下?有任何想法吗?

注意:vim 有一个-silent命令行选项,但据我所知,这会使所有Press ENTERs 静音,这里的目标是仅在成功后避免它们make

4

1 回答 1

2

Just add call feedkeys("\<CR>") afterwards. There are not many places you need feedkeys() (often normal! or similar commands will do), and there are subtle effects (look at the flags it takes carefully). Fortunately this is one place it is useful.

于 2021-04-12T21:13:23.450 回答