Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: chrisbra/vim-diff-enhanced
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: niva-xx/vim9-diff-enhanced
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 11 commits
  • 2 files changed
  • 1 contributor

Commits on Jan 13, 2022

  1. Update EnhancedDiff.vim

    port in vim9script
    niva-xx authored Jan 13, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    b5c756e View commit details
  2. Update EnhancedDiff.vim

    port in vim9script
    niva-xx authored Jan 13, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a6162ba View commit details
  3. Update EnhancedDiff.vim

    niva-xx authored Jan 13, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    e2581ed View commit details
  4. Update EnhancedDiff.vim

    niva-xx authored Jan 13, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    71b1fc5 View commit details
  5. Update EnhancedDiff.vim

    niva-xx authored Jan 13, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    3195750 View commit details
  6. Update EnhancedDiff.vim

    niva-xx authored Jan 13, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    2b1e416 View commit details

Commits on Jan 14, 2022

  1. Update EnhancedDiff.vim

    niva-xx authored Jan 14, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    4db5e7b View commit details
  2. Update EnhancedDiff.vim

    niva-xx authored Jan 14, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    900bbd6 View commit details
  3. Update EnhancedDiff.vim

    niva-xx authored Jan 14, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    38a1dd7 View commit details
  4. Update EnhancedDiff.vim

    niva-xx authored Jan 14, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8e052ad View commit details
  5. Update EnhancedDiff.vim

    niva-xx authored Jan 14, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    fa7714d View commit details
Showing with 229 additions and 154 deletions.
  1. +144 −74 autoload/EnhancedDiff.vim
  2. +85 −80 plugin/EnhancedDiff.vim
218 changes: 144 additions & 74 deletions autoload/EnhancedDiff.vim
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
" EnhancedDiff.vim - Enhanced Diff functions for Vim
" -------------------------------------------------------------
" Version: 0.3
" Maintainer: Christian Brabandt <cb@256bit.org>
" Last Change: Thu, 05 Mar 2015 08:11:46 +0100
" Script: http://www.vim.org/scripts/script.php?script_id=5121
" Copyright: (c) 2009-2015 by Christian Brabandt
" The VIM LICENSE applies to EnhancedDifff.vim
" (see |copyright|) except use "EnhancedDiff.vim"
" instead of "Vim".
" No warranty, express or implied.
" *** *** Use At-Your-Own-Risk! *** ***
" GetLatestVimScripts: 5121 3 :AutoInstall: EnhancedDiff.vim
function! s:DiffInit(...) "{{{2
vim9script

# EnhancedDiff.vim - Enhanced Diff functions for Vim
# -------------------------------------------------------------
# Version: 0.3
## SubMaintainer: NiVa
## SubLast Change: Fri, 14 Jan 2022
# Maintainer: Christian Brabandt <cb@256bit.org>
# Last Change: Thu, 05 Mar 2015 08:11:46 +0100
# Script: http://www.vim.org/scripts/script.php?script_id=5121
# Copyright: (c) 2009-2015 by Christian Brabandt
# The VIM LICENSE applies to EnhancedDifff.vim
# (see |copyright|) except use "EnhancedDiff.vim"
# instead of "Vim".
# No warranty, express or implied.
# *** *** Use At-Your-Own-Risk! *** ***
# GetLatestVimScripts: 5121 3 :AutoInstall: EnhancedDiff.vim
function s:DiffInit(...) #{{{2
let s:diffcmd=exists("a:1") ? a:1 : 'diff'
let s:diffargs=[]
let diffopt=split(&diffopt, ',')
@@ -24,7 +28,7 @@ function! s:DiffInit(...) "{{{2
if exists("g:enhanced_diff_default_{diff_cmd}")
let {diff_cmd}_default = g:enhanced_diff_default_{diff_cmd}
endif
" need to get first word of the diff command
# need to get first word of the diff command

if !executable(diff_cmd)
throw "no executable"
@@ -37,28 +41,28 @@ function! s:DiffInit(...) "{{{2
for [i,j] in items(special_args)
if match(diffopt, '\m\C'.i) > -1
if diff_cmd is# 'git' && i is# 'icase'
" git diff does not support -i option!
# git diff does not support -i option!
call s:Warn("git does not support -i/icase option")
continue
endif
call add(s:diffargs, j)
endif
endfor

" Add file arguments, should be last!
# Add file arguments, should be last!
call add(s:diffargs, s:ModifyPathAndCD(v:fname_in))
call add(s:diffargs, s:ModifyPathAndCD(v:fname_new))
" v:fname_out will be written later
endfu
function! s:ModifyDiffFiles() "{{{2
" replace provided pattern by 'XXX' so it will be ignored when diffing
# v:fname_out will be written later
endfunction
def s:ModifyDiffFiles(): void #{{{2
# replace provided pattern by 'XXX' so it will be ignored when diffing
for expr in get(g:, 'enhanced_diff_ignore_pat', []) + get(b:, 'enhanced_diff_ignore_pat', [])
if !exists("cnt1")
let cnt1 = readfile(v:fname_in)
let cnt2 = readfile(v:fname_new)
var cnt1: string = readfile(v:fname_in)
var cnt2: string = readfile(v:fname_new)
endif
" do not modify the test diff, that always comes
" before the actual diff is evaluated
# do not modify the test diff, that always comes
# before the actual diff is evaluated
if cnt1 ==# ['line1'] && cnt2 ==# ['line2']
return
endif
@@ -69,95 +73,96 @@ function! s:ModifyDiffFiles() "{{{2
call writefile(cnt1, v:fname_in)
call writefile(cnt2, v:fname_new)
endif
endfu
function! s:Warn(msg) "{{{2
echohl WarningMsg
unsilent echomsg "EnhancedDiff: ". a:msg
enddef
def s:Warn( msg: string )
echohl WarningMsg ' unsilent echomsg "EnhancedDiff: ' .. msg
echohl Normal
endfu
function! s:ModifyPathAndCD(file) "{{{2
enddef
def s:ModifyPathAndCD(file: string): string #{{{2
if has("win32") || has("win64")
" avoid a problem with Windows and cygwins path (issue #3)
if a:file is# '-'
" cd back into the previous directory
# avoid a problem with Windows and cygwins path (issue #3)
if file is# '-'
# cd back into the previous directory
cd -
return
return ''
endif
let path = fnamemodify(a:file, ':p:h')
var path = fnamemodify(file, ':p:h')
if getcwd() isnot# path
exe 'sil :cd' fnameescape(path)
endif
return fnameescape(fnamemodify(a:file, ':p:.'))
return fnameescape(fnamemodify(file, ':p:.'))
endif
return fnameescape(a:file)
endfunction
function! EnhancedDiff#ConvertToNormalDiff(list) "{{{2
" Convert unified diff into normal diff
let result=[]
let start=1
let hunk_start = '^@@ -\(\d\+\)\%(,\(\d\+\)\)\? +\(\d\+\)\%(,\(\d\+\)\)\? @@.*$'
let last = ''
for line in a:list
return fnameescape(file)
enddef
def EnhancedDiff#ConvertToNormalDiff(list: list<string>) #{{{2
# Convert unified diff into normal diff
var result=[]
var start=1
var hunk_start = '^@@ -\(\d\+\)\%(,\(\d\+\)\)\? +\(\d\+\)\%(,\(\d\+\)\)\? @@.*$'
var last = ''
for line in list
if start && line !~# '^@@'
continue
else
let start=0
var start=0
endif
if line =~? '^+'
if last is# 'old'
call add(result, '---')
let last='new'
var last='new'
endif
call add(result, substitute(line, '^+', '> ', ''))
elseif line =~? '^-'
let last='old'
var last='old'
call add(result, substitute(line, '^-', '< ', ''))
elseif line =~? '^ ' " skip context lines
continue
elseif line =~? hunk_start
let list = matchlist(line, hunk_start)
let old_start = list[1] + 0
let old_len = list[2] + 0
let new_start = list[3] + 0
let new_len = list[4] + 0
let action = 'c'
let before_end= ''
let after_end = ''
let last = ''
var list = matchlist(line, hunk_start)
var old_start = list[1] + 0
var old_len = list[2] + 0
var new_start = list[3] + 0
var new_len = list[4] + 0
var action = 'c'
var before_end= ''
var after_end = ''
var last = ''

if list[2] is# '0'
let action = 'a'
var action = 'a'
elseif list[4] is# '0'
let action = 'd'
var action = 'd'
endif

if (old_len)
let before_end = printf(',%s', old_start + old_len - 1)
var before_end = printf(',%s', old_start + old_len - 1)
endif
if (new_len)
let after_end = printf(',%s', new_start + new_len - 1)
var after_end = printf(',%s', new_start + new_len - 1)
endif
call add(result, old_start.before_end.action.new_start.after_end)
endif
endfor
return result
endfunction
function! s:SysList(cmd)
enddef
def s:SysList(cmd: string): string
if exists('*systemlist')
return systemlist(a:cmd)
return systemlist(cmd)
endif
return split(system(a:cmd), '\n')
endfunction
function! EnhancedDiff#Diff(...) "{{{2
let cmd=(exists("a:1") ? a:1 : '')
let arg=(exists("a:2") ? a:2 : '')
return split(system(cmd), '\n')
enddef
function EnhancedDiff#Diff(...)
let cmd = (exists("a:1") ? a:1 : '')
let arg = (exists("a:2") ? a:2 : '')

" no-op
" error occured, reset diffexpr
try
call s:DiffInit(cmd, arg)
catch
" no-op
" error occured, reset diffexpr
set diffexpr=
call s:Warn(cmd. ' not found in path, aborting!')
" call s:Warn( cmd . ' not found in path, aborting!' )
call s:Warn( ' not found in path, aborting ' )
return
endtry
call s:ModifyDiffFiles()
@@ -195,4 +200,69 @@ function! EnhancedDiff#Diff(...) "{{{2
redraw!
endif
endfunction
" vim: ts=2 sts=-1 sw=0 et fdm=marker com+=l\:\"


export def EnhancedDiffLayout() #{{{


exec 'sleep ' .. g:EnhancedDiffLayout_Options.timemsecbefore .. 'm'

echomsg 'EnhancedDiffLayout :: autocmd called on CursorHold event.'
g:EnhancedDirDiffDone = {} # cursor hold event locker to do the job

if ( &ft == 'dirdiff' ) && exists( 'b:currentDiff' )

var memo_currentDiff: number = b:currentDiff

# maximize Vim if not
if &columns < 200
set columns=999
set lines=999
endif

# minimize dirdiff buffer
resize 4

# don't do the job if already done
if has_key(g:EnhancedDirDiffDone, memo_currentDiff)
if g:EnhancedDirDiffDone[memo_currentDiff]
return
endif
endif


windo if &diff | setl nofoldenable | endif

exec ':1,2 windo vertical resize ' .. (&columns / 2)
exec ':1,2 windo setl nofoldenable'
exec ':1 windo exec '':3'''
exec ':1 windo norm ]c'

g:EnhancedDirDiffDone[memo_currentDiff] = true

endif

enddef #}}}
export def EnhancedDiffLayout_Toggle() #{{{

g:EnhancedDiffLayout_Options.autocmd_enabled = !g:EnhancedDiffLayout_Options.autocmd_enabled

if !g:EnhancedDiffLayout_Options.autocmd_enabled

echomsg "EnhancedDiffLayout_autocmd disabled"

augroup difflayout
autocmd!
augroup END

else

echomsg "EnhancedDiffLayout_autocmd enabled"
augroup difflayout
au!
autocmd CursorHold *.tmp call EnhancedDiffLayout()
augroup END

endif
enddef #}}}
# vim: set ft=vim ff=dos fdm=marker ts=4 :expandtab:
165 changes: 85 additions & 80 deletions plugin/EnhancedDiff.vim
Original file line number Diff line number Diff line change
@@ -1,89 +1,94 @@
" EnhancedDiff.vim - Enhanced Diff functions for Vim
" -------------------------------------------------------------
" Version: 0.3
" Maintainer: Christian Brabandt <cb@256bit.org>
" Last Change: Thu, 05 Mar 2015 08:11:46 +0100
" Script: http://www.vim.org/scripts/script.php?script_id=5121
" Copyright: (c) 2009-2015 by Christian Brabandt
" The VIM LICENSE applies to EnhancedDifff.vim
" (see |copyright|) except use "EnhancedDiff.vim"
" instead of "Vim".
" No warranty, express or implied.
" *** *** Use At-Your-Own-Risk! *** ***
" GetLatestVimScripts: 5121 3 :AutoInstall: EnhancedDiff.vim
"
" Init: {{{1
let s:cpo= &cpo
if exists("g:loaded_enhanced_diff") || &cp
finish
endif
set cpo&vim
let g:loaded_enhanced_diff = 1
vim9script
# EnhancedDiffLayout.vim - Enhanced Diff Layout functions for Vim
# -------------------------------------------------------------
# Version: 0.3
# Maintainer: Christian Brabandt <cb@256bit.org>
## SubMaintainer: NiVa
## SubLast Change: Sat, 14 Jan 2022
# Script:
# Copyright: (c) 2009-2015 by Christian Brabandt
# The VIM LICENSE applies to EnhancedDiffLayout.vim
# (see |copyright|) except use "EnhancedDiffLayout.vim"
# instead of "Vim".
# No warranty, express or implied.
# *** *** Use At-Your-Own-Risk! *** ***
#
import '../autoload/EnhancedDiff.vim' as DiffEnhancedLayout

" Functions {{{1
function! s:OldGitVersion() "{{{2
if !exists('g:enhanced_diff_old_git')
silent let git_version = matchlist(system("git --version"),'\vgit version (\d+)\.(\d+)\.(\d+)')
let major = git_version[1]
let middle = git_version[2]
let minor = git_version[3]
let g:enhanced_diff_old_git = (major < 1) || (major == 1 && (middle < 8 || (middle == 8 && minor < 2)))
endif
return g:enhanced_diff_old_git
endfu
function! s:CustomDiffAlgComplete(A,L,P) "{{{2
if s:OldGitVersion()
return "myers\ndefault\npatience"
g:EnhancedDiffLayout_Options = { 'timemsecbefore': 100,
'autocmd_enabled': true }

silent! echomsg 'EnhancedDiffLayout'

augroup difflayout
au!
autocmd CursorHold *.tmp call DiffEnhancedLayout.EnhancedDiffLayout()
augroup END

command! -nargs=0 EnhancedDiffLayoutToggle call DiffEnhancedLayout.EnhancedDiffLayout_Toggle()
# Functions {{{1
def s:OldGitVersion(): bool #{{{2
if !exists('g:enhanced_diff_old_git')
silent var git_version: list<string> = matchlist(system("git --version"), '\vgit version (\d+)\.(\d+)\.(\d+)')
var major: string = git_version[1]
var middle: string = git_version[2]
var minor: string = git_version[3]

g:enhanced_diff_old_git = ( major->str2nr() < 1 )
|| ( major->str2nr() == 1 &&
( middle->str2nr() < 8
|| ( ( middle->str2nr() == 8) && ( minor->str2nr() < 2 ) ) ) )
endif
return g:enhanced_diff_old_git
enddef
function s:CustomDiffAlgComplete(A,L,P) #{{{2
if s:OldGitVersion()
return "myers\ndefault\npatience"
else
return "myers\nminimal\ndefault\npatience\nhistogram"
endif
endfunc
function s:CustomIgnorePat(bang, ...) #{{{2
if a:bang
if a:bang && a:0 && a:1 == '-buffer'
let b:enhanced_diff_ignore_pat=[]
else
return "myers\nminimal\ndefault\npatience\nhistogram"
let g:enhanced_diff_ignore_pat=[]
endif
endfu
function! s:CustomIgnorePat(bang, ...) "{{{2
if a:bang
if a:bang && a:0 && a:1 == '-buffer'
let b:enhanced_diff_ignore_pat=[]
else
let g:enhanced_diff_ignore_pat=[]
endif
endif
if !exists("g:enhanced_diff_ignore_pat")
let g:enhanced_diff_ignore_pat=[]
endif

if a:0
local = 0
replace = 'XXX'
if a:0 == 3 && a:1 == '-buffer'
let local=1
if !exists("b:enhanced_diff_ignore_pat"))
let b:enhanced_diff_ignore_pat=[]
endif
endif
if !exists("g:enhanced_diff_ignore_pat")
let g:enhanced_diff_ignore_pat=[]
let pat = local ? a:2 : a:1
if a:0 == 2
let replace = local ? a:3 : a:2
endif

if a:0
let local = 0
let replace = 'XXX'
if a:0 == 3 && a:1 == '-buffer'
let local=1
if !exists("b:enhanced_diff_ignore_pat"))
let b:enhanced_diff_ignore_pat=[]
endif
endif
let pat = local ? a:2 : a:1
if a:0 == 2
let replace = local ? a:3 : a:2
endif
if local
call add(b:enhanced_diff_ignore_pat, [pat, replace])
else
call add(g:enhanced_diff_ignore_pat, [pat, replace])
endif
if local
call add(b:enhanced_diff_ignore_pat, [pat, replace])
else
call add(g:enhanced_diff_ignore_pat, [pat, replace])
endif
endfu
function s:EnhancedDiffExpr(algo)
return printf('EnhancedDiff#Diff("git diff","%s")',
\ s:OldGitVersion()
\ ? (a:algo == "patience" ? "--patience":"")
\ : "--diff-algorithm=".a:algo)
endfu
" public interface {{{1
com! -nargs=1 -complete=custom,s:CustomDiffAlgComplete EnhancedDiff :let &diffexpr=s:EnhancedDiffExpr("<args>")|:diffupdate
endif
endfunc
def s:EnhancedDiffExpr(algo: string): string
return printf('EnhancedDiff#Diff("git diff", "%s")',
\ s:OldGitVersion()
\ ? (algo == "patience" ? "--patience" : "")
\ : "--diff-algorithm = " .. algo)
enddef
# public interface {{{1
com! -nargs=1 -complete=custom,s:CustomDiffAlgComplete EnhancedDiff :exe '&diffexpr = s:EnhancedDiffExpr("<args>") | diffupdate'
com! PatienceDiff :EnhancedDiff patience
com! EnhancedDiffDisable :set diffexpr=
"com! -nargs=1 -bang EnhancedDiffIgnorePat if <q-bang> | :let g:enhanced_diff_ignore_pat = [<q-args>] | else | :let g:enhanced_diff_ignore_pat=get(g:, 'enhanced_diff_ignore_pat', []) + [<q-args>] |endif
com! -nargs=* -bang EnhancedDiffIgnorePat call s:CustomIgnorePat(<bang>0, <f-args>)

" Restore: "{{{1
let &cpo=s:cpo
unlet s:cpo
" vim: ts=4 sts=4 sw=4 et fdm=marker com+=l\:\"
# vim: set ft=vim ff=dos fdm=marker ts=4 :expandtab: