python3-ly reformat in vim

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

python3-ly reformat in vim

Andrew Bernard
For those who use vim, I'm posting this as result of my discovery of python3-ly reformat thanks to David Wright in the hope that others may find it useful.

This is for Debian or Ubuntu, and assumes the Python package python3-ly is installed.

Add this code to ~/.vimrc:

" lilypond reformat (acb)
" reformat buffer, and return cursor to about the same place.
fun DoLilypondReformat()
    let l = line(".")
    let c = col(".")
    :%!ly reformat
    call cursor(l, c)
endfun

au Filetype lilypond nmap <F2> :call DoLilypondReformat()<CR>

Pressing F2 will reformat the current buffer. Note that, as with all code tidying programs, you can only return the cursor to approximately the same position, as lines may be added of deleted. But better than nothing. Without that repositioning code, the cursor is repositioned at line 1 after a filter operation in vim.

Andrew



_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: python3-ly reformat in vim

Werner LEMBERG

> [...]  Note that, as with all code tidying programs, you can only
> return the cursor to approximately the same position, as lines may
> be added of deleted.

Are you sure about that?  In Emacs, I can set a marker property that
`sticks' to a given character.  As long as this character is not
deleted, it is possible to return to exactly that character,
regardless of its new position in the buffer after text manipulation.
I guess vim has a similar functionality...


    Werner

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: python3-ly reformat in vim

Andrew Bernard
Hi Werner,

Interesting. I'll have some investigation. But close enough is good enough for me. In the majority of cases in my work, no lines are added or deleted, so I end up back very close.

Thanks!

Andrew


On Fri, 10 May 2019 at 15:32, Werner LEMBERG <[hidden email]> wrote:

> [...]  Note that, as with all code tidying programs, you can only
> return the cursor to approximately the same position, as lines may
> be added of deleted.

Are you sure about that?  In Emacs, I can set a marker property that
`sticks' to a given character.  As long as this character is not
deleted, it is possible to return to exactly that character,
regardless of its new position in the buffer after text manipulation.
I guess vim has a similar functionality...


_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: python3-ly reformat in vim

Martin Tarenskeen
In reply to this post by Andrew Bernard


On Fri, 10 May 2019, Andrew Bernard wrote:

> Add this code to ~/.vimrc:
>
> " lilypond reformat (acb)
> " reformat buffer, and return cursor to about the same place.
> fun DoLilypondReformat()
>     let l = line(".")
>     let c = col(".")
>     :%!ly reformat
>     call cursor(l, c)
> endfun
>
> au Filetype lilypond nmap <F2> :call DoLilypondReformat()<CR>
Great! That's very useful! But the indentation of the code between fun
and endfun gave errors on my system. After unindenting these lines your
code worked perfectly.

--

MT

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: python3-ly reformat in vim

Andrew Bernard
Hi Martin,

That's probably from pasting into email.

Andrew


On Fri, 10 May 2019 at 16:27, Martin Tarenskeen <[hidden email]> wrote:


On Fri, 10 May 2019, Andrew Bernard wrote:

> Add this code to ~/.vimrc:
>
> " lilypond reformat (acb)
> " reformat buffer, and return cursor to about the same place.
> fun DoLilypondReformat()
>     let l = line(".")
>     let c = col(".")
>     :%!ly reformat
>     call cursor(l, c)
> endfun
>
> au Filetype lilypond nmap <F2> :call DoLilypondReformat()<CR>

Great! That's very useful! But the indentation of the code between fun
and endfun gave errors on my system. After unindenting these lines your
code worked perfectly.


_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user