Hi everyone!

I share the solution I've found to automatically compile a Lilypond file in Vim. The compilation runs in background after saving the buffer, and errors are added to the quickfix list.
I inspired myself from this script: https://github.com/vim-scripts/makebg

First you must run Vim in server mode.
For this I use an alias in my .bashrc:

|vim_server () {||
|| vim --servername "$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)" "$@||"
||}||
||alias vim='vim_server'|

Then you have to put this in your .vimrc:

|function Makebg(make_cmd, make_target, make_filter)
    execute "silent !makebg" v:servername
                \ shellescape(a:make_cmd, 1)    . " " .
                \ shellescape(a:make_target, 1) . " " .
                \ shellescape(a:make_filter, 1)
    redraw!
endfunction||
||command -nargs=0 Makebg call Makebg(b:make_cmd, b:make_target, b:make_filter)|
||let b:make_cmd = "make"||
||let b:make_target = ""
||||let b:make_filter = "cat"
|
||autocmd Filetype lilypond call SetLilypondOptions()||
||function SetLilypondOptions()||||
||    if filereadable("Makefile")||
||        let b:make_cmd = "make"||
||        let b:make_target = ""||||
||    else||
||        let b:make_cmd = "lilypond"||
||        let b:make_target = expand("%")||
||    endif||
||    let b:make_filter = "lilypond_error_filter"||
||    setlocal efm=%f:%l:%c:%m||
||    autocmd BufWritePost <buffer> Makebg||||
||endfunction|

Finally you must have the two enclosed scripts in your $PATH.

That's all!
Note that you can use the Makebg function for other types of files.

Anton Curl
#!/bin/bash
grep '.*:[0-9]\+:[0-9]\+:.*'
#!/bin/bash
server="$1"
make="$2"
target="$3"
error_filter="$4"
makeef=/tmp/errors.vim

{
        touch "$makeef"
        if [ "$target" == "" ]; then
                make_out="$("$make" 2>&1)"
        else
                make_out="$("$make" "$target" 2>&1)"
        fi
        make_success="$?"
        echo "$make_out" | "$error_filter" >> "$makeef"
        if [ "$make_success" -eq 0 ]; then
                vim --servername "$server" --remote-send \
                        "<Esc>:cgetfile $makeef | redraw! | echomsg \"Build 
successful!\"<CR>"
        else
                vim --servername "$server" --remote-send \
                        "<Esc>:cgetfile $makeef | redraw! | echomsg \"Build 
failed!\"<CR>"
        fi
        sleep 2 && rm "$makeef"
} &
_______________________________________________
lilypond-user mailing list
lilypond-user@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to