diff --git a/common/magit_common.vim b/common/magit_common.vim new file mode 100644 index 0000000000000000000000000000000000000000..0617dc6809fa3dd8c44dd4227f1cd10dcfa8c17f --- /dev/null +++ b/common/magit_common.vim @@ -0,0 +1,25 @@ +let g:magit_git_status_code = { + \ 'M': 'modified', + \ 'A': 'added', + \ 'D': 'deleted', + \ 'R': 'renamed', + \ 'C': 'copied', + \ 'U': 'updated but unmerged', + \ '?': 'untracked', + \ '!': 'ignored' + \ } + +" Regular expressions used to select blocks +let g:file_re = '^\(' +for status_code in values(g:magit_git_status_code) + let g:file_re .= status_code . '\|' +endfor +let g:file_re .= status_code . 'unknown status\): \(.*\)$' +let g:diff_re = '^diff --git' +let g:stash_re = '^stash@{\d\+}:' +let g:hunk_re = '^@@ -\(\d\+\),\?\(\d*\) +\(\d\+\),\?\(\d*\) @@' +let g:bin_re = '^Binary files ' +let g:title_re = '^&@\%([^&@]\|\s\)\+@&$' +let g:eof_re = '\%$' + + diff --git a/plugin/magit.vim b/plugin/magit.vim index 55a9ccf9e1e8c88ebdab614a61e24950a73fc856..3d6cd025a894274dff4941c71b0cdabb1fa970d5 100644 --- a/plugin/magit.vim +++ b/plugin/magit.vim @@ -10,6 +10,8 @@ scriptencoding utf-8 " if v:version < 703 " endif +execute 'source ' . resolve(expand('<sfile>:p:h')) . '/../common/magit_common.vim' + let g:magit_unstaged_buffer_name = "magit-playground" " s:set: helper function to set user definable variable @@ -242,14 +244,6 @@ function! magit#search_block(start_pattern, end_pattern, upper_limit_pattern) return [0, lines] endfunction -" Regular expressions used to select blocks -let s:diff_re = '^diff --git' -let s:stash_re = '^stash@{\d\+}:' -let s:hunk_re = '^@@ -\(\d\+\),\?\(\d*\) +\(\d\+\),\?\(\d*\) @@' -let s:bin_re = '^Binary files ' -let s:title_re = '^##\%([^#]\|\s\)\+##$' -let s:eof_re = '\%$' - " magit#git_commit: commit staged stuff with message prepared in commit section " param[in] mode: mode to commit " 'CF': don't use commit section, just amend previous commit with staged @@ -286,12 +280,13 @@ endfunction " @[1]: List of lines containing the patch for the whole file function! magit#select_file() return magit#search_block( - \ [s:diff_re, 0], - \ [ [s:diff_re, -1], - \ [s:stash_re, -1], - \ [s:title_re, -2], - \ [s:bin_re, 0], - \ [s:eof_re, 0 ] + \ [g:diff_re, 0], + \ [ [g:diff_re, -1], + \ [g:file_re, -1], + \ [g:stash_re, -1], + \ [g:title_re, -2], + \ [g:bin_re, 0], + \ [g:eof_re, 0 ] \ ], \ "") endfunction @@ -305,8 +300,8 @@ endfunction " @[1]: List of lines containing the diff header function! magit#select_file_header() return magit#search_block( - \ [s:diff_re, 0], - \ [ [s:hunk_re, -1] ], + \ [g:diff_re, 0], + \ [ [g:hunk_re, -1] ], \ "") endfunction @@ -318,14 +313,15 @@ endfunction " @[1]: List of lines containing the hunk function! magit#select_hunk() return magit#search_block( - \ [s:hunk_re, 0], - \ [ [s:hunk_re, -1], - \ [s:diff_re, -1], - \ [s:stash_re, -1], - \ [s:title_re, -2], - \ [ s:eof_re, 0 ] + \ [g:hunk_re, 0], + \ [ [g:hunk_re, -1], + \ [g:diff_re, -1], + \ [g:file_re, -1], + \ [g:stash_re, -1], + \ [g:title_re, -2], + \ [g:eof_re, 0 ] \ ], - \ s:diff_re) + \ g:diff_re) endfunction " magit#git_apply: helper function to stage a selection diff --git a/syntax/gitdiff.vim b/syntax/gitdiff.vim index 576d10a30f14dafad6913fc6b28ade885e0ef438..f0615a0a28949248f2a78a560f93502631af7272 100644 --- a/syntax/gitdiff.vim +++ b/syntax/gitdiff.vim @@ -7,12 +7,14 @@ if exists("b:current_syntax") finish endif +execute 'source ' . resolve(expand('<sfile>:p:h')) . '/../common/magit_common.vim' + syn case match syn sync minlines=50 syn include @diff syntax/diff.vim -syn match titleEntry "^&@\%([^@&]\|\s\)\+@&$" contains=titleSign +execute 'syn match titleEntry "' . g:title_re . '" contains=titleSign' if has("conceal") syn match titleSign contained "\%(&@\|@&\)" conceal else @@ -21,14 +23,22 @@ endif hi def link titleEntry String hi def link titleSign Ignore -syn match stashEntry "stash@{\d\+}:.*$" +execute 'syn match stashEntry "' . g:stash_re . '"' hi def link stashEntry String -syn region gitTitle start=/^$\n^&@\%([^#]\|\s\)\+@&$/ end=/^$/ contains=titleEntry +execute 'syn region gitTitle start=/^$\n' . g:title_re . '/ end=/^$/ contains=titleEntry' + +execute 'syn region gitStash start=/' . g:stash_re . '/ end=/^\%(' . + \ g:stash_re . '\)\@=/ contains=gitDiff,stashEntry fold' + +execute 'syn region gitDiff start=/' . g:diff_re . '/ end=/^\%(' . + \ g:diff_re . '\|' . g:title_re. '\|' . g:stash_re . + \ '\)\@=/ contains=@diff,gitHunk fold' -syn region gitStash start=/^stash@{\d\+}:/ end=/^\%(stash@{\d\+}:\)\@=/ contains=gitDiff,stashEntry fold -syn region gitDiff start=/^diff --git / end=/^\%(diff --\|stash@{\d\+}\|&@\%([^@&]\|\s\)\+@&$\)\@=/ contains=@diff,gitHunk fold -syn region gitHunk start=/^@@ -/ end=/^\%(diff --\|stash@{\d\+}\|@@ -\|&@\%([^@&]\|\s\)\+@&$\)\@=/ contains=@diff fold contained +execute 'syn region gitHunk start=/' . + \ g:hunk_re . '/ end=/^\%(' . + \ g:diff_re . '\|' . g:hunk_re . '\|' . g:title_re. '\|' . g:stash_re . + \ '\)\@=/ contains=@diff fold contained' let b:current_syntax = "gitdiff"