Skip to content

Commit

Permalink
initialize repository
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbra committed Feb 25, 2015
0 parents commit e0bc9a6
Show file tree
Hide file tree
Showing 21 changed files with 654 additions and 0 deletions.
161 changes: 161 additions & 0 deletions EnhancedDiff.vmb
@@ -0,0 +1,161 @@
" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.
UseVimball
finish
plugin/EnhancedDiff.vim [[[1
152
" EnhancedDiff.vim - Enhanced Diff functions for Vim
" -------------------------------------------------------------
" Version: 0.1
" Maintainer: Christian Brabandt <cb@256bit.org>
" Last Change: Thu, 15 Jan 2015 20:52:29 +0100
" Script: http://www.vim.org/scripts/script.php?script_id=
" 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: XXX 1 :AutoInstall: EnhancedDiff.vim
"
" Functions {{{1
function! s:DiffInit(...) "{{{2
let s:diffcmd=exists("a:1") ? a:1 : 'diff'
let s:diffargs=[]
let diffopt=split(&diffopt, ',')
let special_args = {'icase': '-i', 'iwhite': '-b'}
let git_default = get(g:, 'enhanced_diff_default_git',
\ '--no-index --no-color --no-ext-diff')
let diff_default = get(g:, 'enhanced_diff_default_diff', '--binary')
let default_args = (exists("a:2") ? a:2 : ''). ' '.
\ get(g:, 'enhanced_diff_default_args', '-U0')

if !executable(split(s:diffcmd)[0])
throw "no executable"
else
" Try to use git diff command, allows for more customizations
if split(s:diffcmd)[0] is# 'git' && !exists("s:git_version")
let s:git_version = substitute(split(system('git --version'))[-1], '\.', '', 'g') + 0
endif
endif
let s:diffargs += split(default_args)
if exists("{s:diffcmd}_default")
let s:diffargs += split({s:diffcmd}_default)
endif

for [i,j] in items(special_args)
if match(diffopt, i) > -1
call add(g:diffargs, j)
endif
endfor

" Add file arguments, should be last!
call add(s:diffargs, v:fname_in)
call add(s:diffargs, v:fname_new)
" v:fname_out Will be written later
endfu
function! s:Warn(msg) "{{{2
echohl WarningMsg
unsilent echo "EnhancedDiff: ". a:msg
echohl Normal
endfu
function! EnhancedDiff(...) "{{{2
let cmd=(exists("a:1") ? a:1 : '')
let arg=(exists("a:2") ? a:2 : '')
try
call s:DiffInit(cmd, arg)
catch
" no-op
" error occured, reset diffexpr
set diffexpr=
call s:Warn(cmd. ' not found in path, aborting!')
return
endtry
let difflist=systemlist(s:diffcmd. ' '. join(s:diffargs, ' '))
if v:shell_error < 0 || v:shell_error > 1
" An error occured
set diffexpr=
call s:Warn(cmd. ' Error executing "'. s:diffcmd. ' '.join(s:diffargs, ' ').'"')
call s:Warn(difflist[0])
return
endif
" if unified diff...
" do some processing here
if !empty(difflist) && difflist[0] !~# '^\%(\d\+\)\%(,\d\+\)\?[acd]\%(\d\+\)\%(,\d\+\)\?'
" transform into normal diff
let difflist=s:ConvertToNormalDiff(difflist)
endif
call writefile(difflist, v:fname_out)
if get(g:, 'enhanced_diff_debug', 0)
call writefile(difflist, 'EnhancedDiff_normal.txt')
" Also write default diff
let opt = "-a --binary "
if &diffopt =~ "icase"
let opt .= "-i "
endif
if &diffopt =~ "iwhite"
let opt .= "-b "
endif
silent execute "!diff " . opt . v:fname_in . " " . v:fname_new . " > EnhancedDiff_default.txt"
endif
endfunction
function! s:ConvertToNormalDiff(list) "{{{2
let result=[]
let start=1
let hunk_start = '^@@ -\(\d\+\)\%(,\(\d\+\)\)\? +\(\d\+\)\%(,\(\d\+\)\)\? @@.*$'
let last = ''
for line in a:list
if start && line !~# '^@@'
continue
else
let start=0
endif
if line =~? '^+'
if last is# 'old'
call add(result, '---')
let last='new'
endif
call add(result, substitute(line, '^+', '> ', ''))
elseif line =~? '^-'
let last='old'
call add(result, substitute(line, '^-', '< ', ''))
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 = ''

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

if (old_len)
let before_end = printf(',%s', old_start + old_len - 1)
endif
if (new_len)
let 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:CustomDiffAlgComplete(A,L,P)
return "myers\nminimal\ndefault\npatience\nhistogram"
endfu
" public interface {{{1
"set diffexpr=EnhancedDiff('','')

com! -nargs=1 -complete=custom,s:CustomDiffAlgComplete CustomDiff :let &diffexpr='EnhancedDiff("git diff", "--diff-algorithm=<args>")'
com! PatienceDiff :CustomDiff patience
com! -nargs=? DefaultDiff :set diffexpr=
autoload/EnhancedDiff.vim [[[1
0
doc/EnhancedDiff.txt [[[1
0
48 changes: 48 additions & 0 deletions Makefile
@@ -0,0 +1,48 @@
SCRIPT=$(wildcard plugin/*.vim)
AUTOL =$(wildcard autoload/*.vim)
DOC=$(wildcard doc/*.txt)
PLUGIN=$(shell basename "$$PWD")
VERSION=$(shell sed -n '/Version:/{s/^.*\(\S\.\S\+\)$$/\1/;p}' $(SCRIPT))

.PHONY: $(PLUGIN).vmb README test

all: uninstall vimball install

vimball: $(PLUGIN).vmb

test:
cd test && ./test.sh && find . -type f -name "EnhancedDiff_*.txt" -delete

clean:
find . -type f \( -name "*.vba" -o -name "*.orig" -o -name "*.~*" \
-o -name ".VimballRecord" -o -name ".*.un~" -o -name "*.sw*" -o \
-name tags -o -name "*.vmb" -o -name "EnhancedDiff_*.txt" \) -delete

dist-clean: clean

install:
vim -N -i NONE -u NONE -c 'ru! plugin/vimballPlugin.vim' -c':so %' -c':q!' $(PLUGIN)-$(VERSION).vmb

uninstall:
vim -N -i NONE -u NONE -c 'ru! plugin/vimballPlugin.vim' -c':RmVimball' -c':q!' $(PLUGIN)-$(VERSION).vmb

undo:
for i in */*.orig; do mv -f "$$i" "$${i%.*}"; done

README:
cp -f $(DOC) README

$(PLUGIN).vmb:
rm -f $(PLUGIN)-$(VERSION).vmb
vim -N -i NONE -u NONE -c 'ru! plugin/vimballPlugin.vim' -c ':call append("0", [ "$(SCRIPT)", "$(AUTOL)", "$(DOC)"])' -c '$$d' -c ":%MkVimball $(PLUGIN)-$(VERSION) ." -c':q!'
ln -f $(PLUGIN)-$(VERSION).vmb $(PLUGIN).vmb

release: version all README

version:
perl -i.orig -pne 'if (/Version:/) {s/\.(\d*)/sprintf(".%d", 1+$$1)/e}' ${SCRIPT} ${AUTOL}
perl -i -pne 'if (/GetLatestVimScripts:/) {s/(\d+)\s+:AutoInstall:/sprintf("%d :AutoInstall:", 1+$$1)/e}' ${SCRIPT} ${AUTOL}
#perl -i -pne 'if (/Last Change:/) {s/\d+\.\d+\.\d\+$$/sprintf("%s", `date -R`)/e}' ${SCRIPT}
perl -i -pne 'if (/Last Change:/) {s/(:\s+).*\n/sprintf(": %s", `date -R`)/e}' ${SCRIPT} ${AUTOL}
perl -i.orig -pne 'if (/Version:/) {s/\.(\d+).*\n/sprintf(".%d %s", 1+$$1, `date -R`)/e}' ${DOC}
VERSION=$(shell sed -n '/Version:/{s/^.*\(\S\.\S\+\)$$/\1/;p}' $(SCRIPT))
152 changes: 152 additions & 0 deletions plugin/EnhancedDiff.vim
@@ -0,0 +1,152 @@
" EnhancedDiff.vim - Enhanced Diff functions for Vim
" -------------------------------------------------------------
" Version: 0.1
" Maintainer: Christian Brabandt <cb@256bit.org>
" Last Change: Thu, 15 Jan 2015 20:52:29 +0100
" Script: http://www.vim.org/scripts/script.php?script_id=
" 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: XXX 1 :AutoInstall: EnhancedDiff.vim
"
" Functions {{{1
function! s:DiffInit(...) "{{{2
let s:diffcmd=exists("a:1") ? a:1 : 'diff'
let s:diffargs=[]
let diffopt=split(&diffopt, ',')
let special_args = {'icase': '-i', 'iwhite': '-b'}
let git_default = get(g:, 'enhanced_diff_default_git',
\ '--no-index --no-color --no-ext-diff')
let diff_default = get(g:, 'enhanced_diff_default_diff', '--binary')
let default_args = (exists("a:2") ? a:2 : ''). ' '.
\ get(g:, 'enhanced_diff_default_args', '-U0')

if !executable(split(s:diffcmd)[0])
throw "no executable"
else
" Try to use git diff command, allows for more customizations
if split(s:diffcmd)[0] is# 'git' && !exists("s:git_version")
let s:git_version = substitute(split(system('git --version'))[-1], '\.', '', 'g') + 0
endif
endif
let s:diffargs += split(default_args)
if exists("{s:diffcmd}_default")
let s:diffargs += split({s:diffcmd}_default)
endif

for [i,j] in items(special_args)
if match(diffopt, i) > -1
call add(g:diffargs, j)
endif
endfor

" Add file arguments, should be last!
call add(s:diffargs, v:fname_in)
call add(s:diffargs, v:fname_new)
" v:fname_out will be written later
endfu
function! s:Warn(msg) "{{{2
echohl WarningMsg
unsilent echo "EnhancedDiff: ". a:msg
echohl Normal
endfu
function! EnhancedDiff(...) "{{{2
let cmd=(exists("a:1") ? a:1 : '')
let arg=(exists("a:2") ? a:2 : '')
try
call s:DiffInit(cmd, arg)
catch
" no-op
" error occured, reset diffexpr
set diffexpr=
call s:Warn(cmd. ' not found in path, aborting!')
return
endtry
let difflist=systemlist(s:diffcmd. ' '. join(s:diffargs, ' '))
if v:shell_error < 0 || v:shell_error > 1
" An error occured
set diffexpr=
call s:Warn(cmd. ' Error executing "'. s:diffcmd. ' '.join(s:diffargs, ' ').'"')
call s:Warn(difflist[0])
return
endif
" if unified diff...
" do some processing here
if !empty(difflist) && difflist[0] !~# '^\%(\d\+\)\%(,\d\+\)\?[acd]\%(\d\+\)\%(,\d\+\)\?'
" transform into normal diff
let difflist=s:ConvertToNormalDiff(difflist)
endif
call writefile(difflist, v:fname_out)
if get(g:, 'enhanced_diff_debug', 0)
call writefile(difflist, 'EnhancedDiff_normal.txt')
" Also write default diff
let opt = "-a --binary "
if &diffopt =~ "icase"
let opt .= "-i "
endif
if &diffopt =~ "iwhite"
let opt .= "-b "
endif
silent execute "!diff " . opt . v:fname_in . " " . v:fname_new . " > EnhancedDiff_default.txt"
endif
endfunction
function! s:ConvertToNormalDiff(list) "{{{2
let result=[]
let start=1
let hunk_start = '^@@ -\(\d\+\)\%(,\(\d\+\)\)\? +\(\d\+\)\%(,\(\d\+\)\)\? @@.*$'
let last = ''
for line in a:list
if start && line !~# '^@@'
continue
else
let start=0
endif
if line =~? '^+'
if last is# 'old'
call add(result, '---')
let last='new'
endif
call add(result, substitute(line, '^+', '> ', ''))
elseif line =~? '^-'
let last='old'
call add(result, substitute(line, '^-', '< ', ''))
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 = ''

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

if (old_len)
let before_end = printf(',%s', old_start + old_len - 1)
endif
if (new_len)
let 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:CustomDiffAlgComplete(A,L,P)
return "myers\nminimal\ndefault\npatience\nhistogram"
endfu
" public interface {{{1
"set diffexpr=EnhancedDiff('','')

com! -nargs=1 -complete=custom,s:CustomDiffAlgComplete CustomDiff :let &diffexpr='EnhancedDiff("git diff", "--diff-algorithm=<args>")'
com! PatienceDiff :CustomDiff patience
com! -nargs=? DefaultDiff :set diffexpr=
1 change: 1 addition & 0 deletions test/1/diff.txt
@@ -0,0 +1 @@
default
1 change: 1 addition & 0 deletions test/1/file1
@@ -0,0 +1 @@
line1
1 change: 1 addition & 0 deletions test/1/file2
@@ -0,0 +1 @@
line2
4 changes: 4 additions & 0 deletions test/1/normal_diff.ok
@@ -0,0 +1,4 @@
1c1
< line1
---
> line2
1 change: 1 addition & 0 deletions test/2/diff.txt
@@ -0,0 +1 @@
default

0 comments on commit e0bc9a6

Please sign in to comment.