Vim - 在mercurial中提交时显示差异;

时间:2011-11-04 12:43:34

标签: vim mercurial diff hook commit

在我的.hgrc我可以提供一个编辑器或命令来启动一个包含提交选项的编辑器。

我想编写一个启动$ hg ci的方法或别名,它不仅会在Vim中打开消息,还会分割窗口并打印出$ hg diff

我知道我可以使用+{command}选项为vim提供参数。因此启动$ vim "+vsplit"会进行拆分,但任何其他选项都会转到第一个打开的窗口。所以我假设我需要一个特定的功能,但我没有编写自己的Vim脚本的经验。

脚本应该:

  • 使用空缓冲区打开新的垂直拆分(可能vnew
  • 在空缓冲区启动:.!hg diff
  • 将空缓冲区文件类型设置为diff :set ft=diff

我写过这样的功能:

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

.hgrc我添加了选项:editor = vim "+HgCiDiff()"

它有点工作,但我希望分裂窗口在右侧(现在它在左侧打开)并且mercurial消息将是聚焦窗口。此外,:wq可以设置为:wq<CR>:q!的临时快捷方式(假设有重点消息是专注的)。

有什么建议可以让它更有用而且不那么厚实吗?

更新:我找到了vim split guide,因此vnew更改为rightbelow vnew会在右侧打开差异。

4 个答案:

答案 0 :(得分:5)

所以我扩展了自己的代码:

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

然而,它错过:wq映射为:wqa,但使用:wqa并不是那么难。

我的vimrc来源位于:http://hg.jackleo.info/vim-configs/src/08df5cb9d143/vimrc
我的hgrc的来源位于:http://hg.jackleo.info/home-configs/src/22f5fb47a7d2/.hgrc

按照Randy Morris的建议

更新:我更新了我的代码,现在它正常工作。谢谢!随着时间的推移,还增加了一些额外的功能。

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

答案 1 :(得分:3)

修改

嗯,我想这可能不是你二读后的样子。我知道你想要一个多文件(统一)差异。我真的使用hg-aware UI工具和单独的vim编辑器来提交消息。对不起。

如果您还不了解VCSCommand + Hg + Vim,我会留下'原创'回复立场:

<子>

我选择的武器是用

将它抽象出来

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

你会

:VCSVimDiff

对repo版本进行diffsplit(同时使用 Leader c v

:VCSVimDiff <revid>

与特定版本进行比较。

答案 2 :(得分:1)

我的解决方案包含三个vim文件。它不需要hg配置更改,只显示您提交的文件的差异,如果您使用hg commit file1 file2

<强>〜/的.vim / ftdetect / hg.vim

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

<强>〜/的.vim /语法/ 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 /文件类型插件/ 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()

答案 3 :(得分:1)

这是基于Marius Gedminas和JackLeo版本的变体:

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