Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - show diff on commit in mercurial;

In my .hgrc I can provide an editor or a command to launch an editor with options on commit.

I want to write a method or alias that launches $ hg ci, it would not only open up message in Vim, but also would split window and there print out $ hg diff.

I know that I can give parameters to vim by using +{command} option. So launching $ vim "+vsplit" does the split but any other options goes to first opened window. So I assume i need a specific function, yet I have no experience in writing my own Vim scripts.

The script should:

  • Open new vertical split with empty buffer (with vnew possibly)
  • In empty buffer launch :.!hg diff
  • Set empty buffer file type as diff :set ft=diff

I've written such function:

function! HgCiDiff()
    vnew
    :.!hg diff
    set ft=diff
endfunction

And in .hgrc I've added option: editor = vim "+HgCiDiff()"

It kind of works, but I would like that splited window would be in right side (now it opens up in left) and mercurial message would be focused window. Also :wq could be setted as temporary shortcut to :wq<CR>:q! (having an assumption that mercurial message is is focused).

Any suggestions to make this a bit more useful and less chunky?

UPDATE: I found vim split guide so changing vnew with rightbelow vnew opens up diff on the right side.

like image 608
JackLeo Avatar asked Dec 28 '22 11:12

JackLeo


2 Answers

So I expanded my own code:

function! HgCiDiff()
    "In .hgrc editor option I call vim "+HgCiDiff()"
    "It opens new split with diff inside
    rightbelow  vnew
    :.!hg diff
    set ft=diff
    saveas! /tmp/hgdiff.txt
    execute "normal \<c-w>w"
endfunction

Yet It missed :wq mapping as :wqa, yet using :wqa is not that hard.

Sources of my vimrc is located here: http://hg.jackleo.info/vim-configs/src/08df5cb9d143/vimrc
Sources of my hgrc is located here: http://hg.jackleo.info/home-configs/src/22f5fb47a7d2/.hgrc

Update: as suggested by Randy Morris I updated my code and now it works just as I wanted. Thanks! Also added few extra features as the time went by.

function! HgCiDiff()
    "In .hgrc editor option I call vim "+HgCiDiff()"
    "It opens new split with diff inside
    rightbelow  vnew
    setlocal buftype=nofile
    :.!hg diff
    setlocal ft=diff
    wincmd p
    setlocal spell spelllang=en_us
    cnoremap wq wqa
    cnoremap q qa
    start
endfunction
like image 193
JackLeo Avatar answered Jan 04 '23 08:01

JackLeo


Edit

Hmm I think this might not be what you are after on second reading. I understand you want a multi-file (unified) diff. I'd really use a hg-aware UI tool and a separate vim editor for the commit message. Sorry about that.

I'll leave the 'original' response stand in case you didn't know VCSCommand + Hg + Vim yet:

My weapon of choice is to abstract it all away with

vcscommand.vim : CVS/SVN/SVK/git/hg/bzr integration plugin

You would

:VCSVimDiff

to diffsplit against the repo version (also with Leadercv)

:VCSVimDiff <revid>

to compare against a specific revision.

like image 40
sehe Avatar answered Jan 04 '23 06:01

sehe