Vim - show diff on fixation in mercury;

In my .hgrc I can provide an editor or command to start the editor with commit parameters.

I want to write a method or alias that runs $ hg ci , it will not only open the message in Vim, but will also split the window and print $ hg diff .

I know that I can provide options for vim using the +{command} option. Thus, running $ vim "+vsplit" does the splitting, but any other parameters go to the first open window. Therefore, I assume that I need a specific function, but I have no experience writing my own Vim scripts.

The script should:

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

I wrote a function like this:

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

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

This seems to work, but I would like the broken window to be on the right side (now it opens on the left), and the mercurial message will be a focused window. In addition :wq can be set as a temporary shortcut to :wq<CR>:q! (with the assumption that the mercury message is focused).

Any suggestions to make this a little more useful and less short?

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

+4
source share
4 answers

So, I have 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 \<cw>w" endfunction 

But he missed the mapping :wq as :wqa , but using :wqa not that difficult.

The sources of my vimrc are here: http://hg.jackleo.info/vim-configs/src/08df5cb9d143/vimrc
The sources of my hgrc are here: http://hg.jackleo.info/home-configs/src/22f5fb47a7d2/.hgrc

Update: as suggested by Randy Morris. I updated my code and now it works the way I wanted. Thank you Also added a few additional features over time.

 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 
+5
source

Edit

Hmm, I think this may not be the way you are after your second reading. I understand that you need a multi-file (unified) diff. I would really use the hg-aware UI tool and a separate vim editor for the commit message. Excuse me.

I will leave the original return stand if you do not already know VCSCommand + Hg + Vim:

<sub>

My weapon of choice is to distract it all with

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

You would

 :VCSVimDiff 

for diffsplit vs repo version (also with Leader c v )

 :VCSVimDiff <revid> 

for comparison with a specific version.

sub>

+3
source

My solution consists of three vim files. It does not require hg configuration changes and only shows diff for the files you commit if you used hg commit file1 file2 :

~ / .vim / ftdetect / hg.vim

 au BufRead,BufNewFile /tmp/hg-editor-*.txt set filetype=hg 

~ / .vim / syntax / hg.vim

 " Vim syntax file " Language: hg commit file " Maintainer: Marius Gedminas < marius@gedmin.as > " Filenames: /tmp/hg-editor-*.txt " Last Change: 2012 July 8 " Based on gitcommit.vim by Tim Pope if exists("b:current_syntax") finish endif syn case match syn sync minlines=50 if has("spell") syn spell toplevel endif syn match hgComment "^HG: .*" hi def link hgComment Comment let b:current_syntax = "hg" 

~ / .vim / ftplugin / hg.vim

 " Show diff while editing a Mercurial commit message " Inspired by http://stackoverflow.com/questions/8009333/vim-show-diff-on-commit-in-mercurial " and Michael Scherer' svn.vim function! HgCiDiff() let i = 0 let list_of_files = '' while i <= line('$') && getline(i) != 'HG: --' let i = i + 1 endwhile while i <= line('$') let line = getline(i) if line =~ '^HG: \(added\|changed\)' let file = substitute(line, '^HG: \(added\|changed\) ', '', '') let file = "'".substitute(file, "'", "'\''", '')."'" let list_of_files = list_of_files . ' '.file endif let i = i + 1 endwhile if list_of_files == "" return endif pclose new setlocal ft=diff previewwindow bufhidden=delete nobackup noswf nobuflisted nowrap buftype=nofile silent exec ':0r!hg diff ' . list_of_files setlocal nomodifiable goto 1 redraw! " nooo idea why I have to do this syn enable endfunction call HgCiDiff() 
+1
source

Here are my variations based on the Marius Gedminas and JackLeo versions:

 function! HgCiDiff() " find files that were changed (not interested in added or deleted) let changed_files = [] let pattern = '\vHG: changed \zs(.+)\ze' while search("HG: changed", "W") > 0 let line_text = getline(line(".")) call add(changed_files, matchstr(line_text, pattern)) endwhile let diff_cmd = "hg diff " . join(changed_files, " ") " Reset cursor to beginning of the buffer call cursor(1, 1) rightbelow vnew setlocal buftype=nofile let diff_output = system(diff_cmd) call append(0, split(diff_output, "\n")) " Reset cursor to beginning of the buffer call cursor(1, 1) setlocal ft=diff wincmd p setlocal spell spelllang=en_us cnoremap wq wqa cnoremap q qa! startinsert endfunction 
+1
source

Source: https://habr.com/ru/post/1379643/


All Articles