Vim - 在 Mercurial 中显示提交的差异;

发布于 2024-12-14 02:50:04 字数 1058 浏览 3 评论 0原文

在我的 .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:q! 的临时快捷方式(假设 Mercurial 消息是焦点)。

有什么建议可以让它更有用并且不那么笨重吗?

更新:我发现vim 分割指南 因此用 rightbelow vnew 更改 vnew 会在右侧打开 diff。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆沫 2024-12-21 02:50:04

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

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

更新:按照兰迪·莫里斯的建议我更新了我的代码现在它就像我想要的那样工作了。谢谢!随着时间的推移,还添加了一些额外的功能。

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

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
吻泪 2024-12-21 02:50:04

编辑

嗯,我想这可能不是你第二次阅读时想要的。我知道您想要一个多文件(统一)差异。我真的会使用一个 hg-aware UI 工具和一个单独的 vim 编辑器来处理提交消息。对此感到抱歉。

如果您还不知道 VCSCommand + Hg + Vim,我将保留“原始”响应立场:

我选择的武器是使用

vcscommand.vim : CVS/SVN/SVK/git/hg/bzr 集成插件

你会

:VCSVimDiff

针对存储库版本进行 diffsplit(也使用 Leadercv

:VCSVimDiff <revid>

以与特定修订版本进行比较。

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.

罗罗贝儿 2024-12-21 02:50:04

我的解决方案由三个 vim 文件组成。它不需要 hg 配置更改,并且仅显示您正在提交的文件的差异(如果您使用过 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 <[email protected]>
" 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()

My solution consists of three vim files. It doesn't require hg configuration changes, and only shows the diff for the files you're committing, if you've 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 <[email protected]>
" 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()
心欲静而疯不止 2024-12-21 02:50:04

这是我基于 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

Here's my variation based on Marius Gedminas and JackLeo's 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文