Skip to content

Instantly share code, notes, and snippets.

@kiyohara
Created February 19, 2012 14:24
Show Gist options
  • Save kiyohara/1864051 to your computer and use it in GitHub Desktop.
Save kiyohara/1864051 to your computer and use it in GitHub Desktop.
Colorscheme switching script for sudo.vim ( http://www.vim.org/scripts/script.php?script_id=729 )
let g:sudo_colorscheme = "evening"
let g:sudo_colorscheme_gui = "morning"
" ---
augroup SudoVimColor
autocmd!
autocmd BufEnter * call SudoVimUpdateColor()
augroup END
function! s:GetColorscheme()
let has_gui = has("gui_running")
let has_ts = exists("g:sudo_colorscheme")
let has_gs = exists("g:sudo_colorscheme_gui")
if (has_ts && has_gs)
return has_gui ? g:sudo_colorscheme_gui : g:sudo_colorscheme
elseif (has_ts)
return g:sudo_colorscheme
elseif (has_gs)
return g:sudo_colorscheme_gui
endif
return ""
endfunction
function! s:HasSetting()
let setting = s:GetColorscheme()
return (strlen(setting) > 0)
endfunction
function! SudoVimUpdateColor()
" check definition
if (!s:HasSetting())
return
endif
if (expand("%") =~# "^sudo:.\\+")
call SudoVimEnableColor()
else
call SudoVimDisableColor()
endif
endfunction
function! SudoVimEnableColor()
" check definition
if (!s:HasSetting())
return
endif
" check current state
if exists("g:sudo_is_enabled")
" is enabled already
return
else
" set as enabled
let g:sudo_is_enabled=1
endif
" save current colorscheme
if exists("g:colors_name")
let g:sudo_colorscheme_saved = g:colors_name
else
let g:sudo_colorscheme_saved = "default"
endif
" save background setting
if exists("&background")
let g:sudo_background_saved = &background
endif
" update colorscheme
exec "colorscheme" s:GetColorscheme()
endfunction
function! SudoVimDisableColor()
" check definition
if (!s:HasSetting())
return
endif
" check current state
if ! exists("g:sudo_is_enabled")
" is disabled already
return
else
" set as disabled
unlet g:sudo_is_enabled
endif
" restore colorscheme
if exists("g:sudo_colorscheme_saved")
exec "colorscheme" g:sudo_colorscheme_saved
unlet g:sudo_colorscheme_saved
endif
" restore background
if exists("g:sudo_background_saved")
exec "set" "background=".g:sudo_background_saved
unlet g:sudo_background_saved
endif
endfunction
@kiyohara
Copy link
Author

sudo.vim 用のスクリプト。
sudo:* で開いたバッファにフォーカスした際に、colorscheme を変更する。
g:sudo_colorscheme に sudo:* バッファにフォーカスした際に利用する colorscheme を記載する。
.vimrc に記載するか、別ファイルに保存して .vimrc から so して利用する。

実装方法について
au の pat 指定部は、以下の様に出来ると見通しが良いと思うのだけど、 not を指定する方法はなさそう。

au BufEnter sudo:*,sudo:*/* call enable_func()
au BufEnter ! (sudo:*,sudo:*/*) call disable_func()

機能について
現状では window すべての colorscheme が変更されるので、いまいち見にくい。
バッファ毎に colorscheme が指定出来るようになれば、もう少し見やすくなると思う。
それが出来るようになると、BufEnter 毎にトリガリングしなくても sudo.vim 自身が フッキングしているように BufReadCmd と FileReadCmd だけ見ればよくなり、負荷も下がる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment