"
" }}}

" Terminals that support italics
if has("gui_running") || ( has("unix") && system("tput sitm") == "\033[3m" )
    let s:terminal_italic=1
else
    let s:terminal_italic=0
endif


let g:solarized_termtrans = 0
let g:solarized_degrade = 0
let g:solarized_bold = 1
let g:solarized_underline = 1
" note that we need to override this later if the terminal doesn't support
let g:solarized_italic = 1
let g:solarized_termcolors = 16
let g:solarized_contrast = "normal"
let g:solarized_visibility = "normal"
let g:solarized_diffmode = "normal"
let g:solarized_hitrail = 0
let g:solarized_menu = 1

" Colorscheme initialization "{{{
" ---------------------------------------------------------------------
highlight clear
if exists("syntax_on")
  syntax reset
endif
let colors_name = "bsolarized"

"}}}
" GUI & CSApprox hexadecimal palettes"{{{
" ---------------------------------------------------------------------
"
let s:vmode       = "cterm"
let s:base03      = "8"
let s:base02      = "0"
let s:base01      = "10"
let s:base00      = "11"
let s:base0       = "12"
let s:base1       = "14"
let s:base2       = "7"
let s:base3       = "15"
let s:yellow      = "3"
let s:orange      = "9"
let s:red         = "1"
let s:magenta     = "5"
let s:violet      = "13"
let s:blue        = "4"
let s:cyan        = "6"
let s:green       = "2"
let s:none        = "NONE"
let s:none        = "NONE"
let s:t_none      = "NONE"
let s:n           = "NONE"
let s:b           = ",bold"
let s:bb          = ""
let s:u           = ",underline"
let s:i           = ",italic"
let s:c           = ",undercurl"
let s:r           = ",reverse"
let s:s           = ",standout"
let s:ou          = ""
let s:ob          = ""

if (has("gui_running") || g:solarized_termtrans == 0)
    let s:back        = s:base03
else
    let s:back        = "NONE"
endif

let s:bg_none      = ' ctermbg=NONE guibg=NONE'
let s:bg_back      = ' ctermbg=NONE guibg=#002b36'
let s:bg_base03    = ' ctermbg=8    guibg=#002b36'
let s:bg_base02    = ' ctermbg=0    guibg=#073642'
let s:bg_base01    = ' ctermbg=10   guibg=#586e75'
let s:bg_base00    = ' ctermbg=11   guibg=#657b83'
let s:bg_base0     = ' ctermbg=12   guibg=#839496'
let s:bg_base1     = ' ctermbg=14   guibg=#93a1a1'
let s:bg_base2     = ' ctermbg=7    guibg=#eee8d5'
let s:bg_base3     = ' ctermbg=15   guibg=#fdf6e3'
let s:bg_green     = ' ctermbg=2    guibg=#859900'
let s:bg_yellow    = ' ctermbg=3    guibg=#b58900'
let s:bg_orange    = ' ctermbg=9    guibg=#cb4b16'
let s:bg_red       = ' ctermbg=1    guibg=#dc322f'
let s:bg_magenta   = ' ctermbg=5    guibg=#d33682'
let s:bg_violet    = ' ctermbg=13   guibg=#6c71c4'
let s:bg_blue      = ' ctermbg=4    guibg=#268bd2'
let s:bg_cyan      = ' ctermbg=6    guibg=#2aa198'

let s:fg_none      = ' ctermfg=NONE guifg=NONE'
let s:fg_back      = ' ctermfg=NONE guifg=#002b36'
let s:fg_base03    = ' ctermfg=8    guifg=#002b36'
let s:fg_base02    = ' ctermfg=0    guifg=#073642'
let s:fg_base01    = ' ctermfg=10   guifg=#586e75'
let s:fg_base00    = ' ctermfg=11   guifg=#657b83'
let s:fg_base0     = ' ctermfg=12   guifg=#839496'
let s:fg_base1     = ' ctermfg=14   guifg=#93a1a1'
let s:fg_base2     = ' ctermfg=7    guifg=#eee8d5'
let s:fg_base3     = ' ctermfg=15   guifg=#fdf6e3'
let s:fg_green     = ' ctermfg=2    guifg=#859900'
let s:fg_yellow    = ' ctermfg=3    guifg=#b58900'
let s:fg_orange    = ' ctermfg=9    guifg=#cb4b16'
let s:fg_red       = ' ctermfg=1    guifg=#dc322f'
let s:fg_magenta   = ' ctermfg=5    guifg=#d33682'
let s:fg_violet    = ' ctermfg=13   guifg=#6c71c4'
let s:fg_blue      = ' ctermfg=4    guifg=#268bd2'
let s:fg_cyan      = ' ctermfg=6    guifg=#2aa198'

let s:fmt_none     = ' cterm=NONE              gui=NONE'
let s:fmt_bold     = ' cterm=bold              gui=bold'
let s:fmt_bldi     = ' cterm=bold,italic       gui=bold,italic'
let s:fmt_undr     = ' cterm=underline         gui=underline'
let s:fmt_undb     = ' cterm=underline,bold    gui=underline,bold'
let s:fmt_undi     = ' cterm=underline,italic  gui=underline,italic'
let s:fmt_uopt     = ' cterm=NONE              gui=NONE'
let s:fmt_curl     = ' cterm=undercurl         gui=undercurl'
let s:fmt_ital     = ' cterm=italic            gui=italic'
let s:fmt_stnd     = ' cterm=standout          gui=standout'
let s:fmt_revr     = ' cterm=reverse           gui=reverse'
let s:fmt_revb     = ' cterm=reverse,bold      gui=reverse,bold'
let s:fmt_revbb    = ' cterm=reverse           gui=reverse'
let s:fmt_revbbu   = ' cterm=reverse,underline gui=reverse,underline'

let s:sp_none      = ' guisp=NONE         '
let s:sp_back      = ' guisp=8            '
let s:sp_base03    = ' guisp=8            '
let s:sp_base02    = ' guisp=0            '
let s:sp_base01    = ' guisp=10           '
let s:sp_base00    = ' guisp=11           '
let s:sp_base0     = ' guisp=12           '
let s:sp_base1     = ' guisp=14           '
let s:sp_base2     = ' guisp=7            '
let s:sp_base3     = ' guisp=15           '
let s:sp_green     = ' guisp=2            '
let s:sp_yellow    = ' guisp=3            '
let s:sp_orange    = ' guisp=9            '
let s:sp_red       = ' guisp=1            '
let s:sp_magenta   = ' guisp=5            '
let s:sp_violet    = ' guisp=13           '
let s:sp_blue      = ' guisp=4            '
let s:sp_cyan      = ' guisp=6            '


hi! link diffAdded Statement
hi! link diffLine Identifier
"}}}
" git & gitcommit highlighting "{{{
"git
"exe "hi! gitDateHeader"
"exe "hi! gitIdentityHeader"
"exe "hi! gitIdentityKeyword"
"exe "hi! gitNotesHeader"
"exe "hi! gitReflogHeader"
"exe "hi! gitKeyword"
"exe "hi! gitIdentity"
"exe "hi! gitEmailDelimiter"
"exe "hi! gitEmail"
"exe "hi! gitDate"
"exe "hi! gitMode"
"exe "hi! gitHashAbbrev"
"exe "hi! gitHash"
"exe "hi! gitReflogMiddle"
"exe "hi! gitReference"
"exe "hi! gitStage"
"exe "hi! gitType"
"exe "hi! gitDiffAdded"
"exe "hi! gitDiffRemoved"
"gitcommit
"exe "hi! gitcommitSummary"      
exe "hi! gitcommitComment"      .s:fmt_ital     .s:fg_base01    .s:bg_none
hi! link gitcommitUntracked gitcommitComment
hi! link gitcommitDiscarded gitcommitComment
hi! link gitcommitSelected  gitcommitComment
exe "hi! gitcommitUnmerged"     .s:fmt_bold     .s:fg_green     .s:bg_none
exe "hi! gitcommitOnBranch"     .s:fmt_bold     .s:fg_base01    .s:bg_none
exe "hi! gitcommitBranch"       .s:fmt_bold     .s:fg_magenta   .s:bg_none
hi! link gitcommitNoBranch gitcommitBranch
exe "hi! gitcommitDiscardedType".s:fmt_none     .s:fg_red       .s:bg_none
exe "hi! gitcommitSelectedType" .s:fmt_none     .s:fg_green     .s:bg_none
"exe "hi! gitcommitUnmergedType"
"exe "hi! gitcommitType"
"exe "hi! gitcommitNoChanges"
"exe "hi! gitcommitHeader"
exe "hi! gitcommitHeader"       .s:fmt_none     .s:fg_base01    .s:bg_none
exe "hi! gitcommitUntrackedFile".s:fmt_bold     .s:fg_cyan      .s:bg_none
exe "hi! gitcommitDiscardedFile".s:fmt_bold     .s:fg_red       .s:bg_none
exe "hi! gitcommitSelectedFile" .s:fmt_bold     .s:fg_green     .s:bg_none
exe "hi! gitcommitUnmergedFile" .s:fmt_bold     .s:fg_yellow    .s:bg_none
exe "hi! gitcommitFile"         .s:fmt_bold     .s:fg_base0     .s:bg_none
hi! link gitcommitDiscardedArrow gitcommitDiscardedFile
hi! link gitcommitSelectedArrow  gitcommitSelectedFile
hi! link gitcommitUnmergedArrow  gitcommitUnmergedFile
"exe "hi! gitcommitArrow"
"exe "hi! gitcommitOverflow"
"exe "hi! gitcommitBlank"
" }}}
" Utility autocommand "{{{
" ---------------------------------------------------------------------
" In cases where Solarized is initialized inside a terminal vim session and 
" then transferred to a gui session via the command `:gui`, the gui vim process 
" does not re-read the colorscheme (or .vimrc for that matter) so any `has_gui` 
" related code that sets gui specific values isn't executed.
"
" Currently, Solarized sets only the cterm or gui values for the colorscheme 
" depending on gui or terminal mode. It's possible that, if the following 
" autocommand method is deemed excessively poor form, that approach will be 
" used again and the autocommand below will be dropped.
"
" However it seems relatively benign in this case to include the autocommand 
" here. It fires only in cases where vim is transferring from terminal to gui 
" mode (detected with the script scope s:vmode variable). It also allows for 
" other potential terminal customizations that might make gui mode suboptimal.
"
autocmd GUIEnter * if (s:vmode != "gui") | exe "colorscheme " . g:colors_name | endif
"}}}
" Highlight Trailing Space {{{
" Experimental: Different highlight when on cursorline
function! s:SolarizedHiTrail()
    if g:solarized_hitrail==0
        hi! clear solarizedTrailingSpace
    else
        syn match solarizedTrailingSpace "\s*$"
        exe "hi! solarizedTrailingSpace " .s:fmt_undr .s:fg_red .s:bg_none .s:sp_red
    endif
endfunction  
augroup SolarizedHiTrail
    autocmd!
    if g:solarized_hitrail==1
        autocmd! Syntax * call s:SolarizedHiTrail()
        autocmd! ColorScheme * if g:colors_name == "solarized" | call s:SolarizedHiTrail() | else | augroup! s:SolarizedHiTrail | endif
    endif
augroup END
" }}}
" Menus "{{{
" ---------------------------------------------------------------------
" Turn off Solarized menu by including the following assignment in your .vimrc:
"
"    let g:solarized_menu=0

function! s:SolarizedOptions()
    new "new buffer
    setf vim "vim filetype
    let failed = append(0, s:defaults_list)
    let failed = append(0, s:colorscheme_list)
    let failed = append(0, s:options_list)
    let failed = append(0, s:lazycat_list)
    0 "jump back to the top
endfunction
if !exists(":SolarizedOptions")
    command SolarizedOptions :call s:SolarizedOptions()
endif

function! SolarizedMenu()
    if exists("g:loaded_solarized_menu")
        try
            silent! aunmenu Solarized
        endtry
    endif
    let g:loaded_solarized_menu = 1

    if g:colors_name == "solarized" && g:solarized_menu != 0

        amenu &Solarized.&Contrast.&Low\ Contrast        :let g:solarized_contrast="low"       \| colorscheme solarized<CR>
        amenu &Solarized.&Contrast.&Normal\ Contrast     :let g:solarized_contrast="normal"    \| colorscheme solarized<CR>
        amenu &Solarized.&Contrast.&High\ Contrast       :let g:solarized_contrast="high"      \| colorscheme solarized<CR>
        an    &Solarized.&Contrast.-sep-                 <Nop>
        amenu &Solarized.&Contrast.&Help:\ Contrast      :help 'solarized_contrast'<CR>

        amenu &Solarized.&Visibility.&Low\ Visibility    :let g:solarized_visibility="low"     \| colorscheme solarized<CR>
        amenu &Solarized.&Visibility.&Normal\ Visibility :let g:solarized_visibility="normal"  \| colorscheme solarized<CR>
        amenu &Solarized.&Visibility.&High\ Visibility   :let g:solarized_visibility="high"    \| colorscheme solarized<CR>
        an    &Solarized.&Visibility.-sep-                 <Nop>
        amenu &Solarized.&Visibility.&Help:\ Visibility    :help 'solarized_visibility'<CR>

        amenu &Solarized.&Background.&Toggle\ Background :ToggleBG<CR>
        amenu &Solarized.&Background.&Dark\ Background   :set background=dark  \| colorscheme solarized<CR>
        amenu &Solarized.&Background.&Light\ Background  :set background=light \| colorscheme solarized<CR>
        an    &Solarized.&Background.-sep-               <Nop>
        amenu &Solarized.&Background.&Help:\ ToggleBG     :help togglebg<CR>

        if g:solarized_bold==0 | let l:boldswitch="On" | else | let l:boldswitch="Off" | endif
        exe "amenu &Solarized.&Styling.&Turn\\ Bold\\ ".l:boldswitch." :let g:solarized_bold=(abs(g:solarized_bold-1)) \\| colorscheme solarized<CR>"
        if g:solarized_italic==0 | let l:italicswitch="On" | else | let l:italicswitch="Off" | endif
        exe "amenu &Solarized.&Styling.&Turn\\ Italic\\ ".l:italicswitch." :let g:solarized_italic=(abs(g:solarized_italic-1)) \\| colorscheme solarized<CR>"
        if g:solarized_underline==0 | let l:underlineswitch="On" | else | let l:underlineswitch="Off" | endif
        exe "amenu &Solarized.&Styling.&Turn\\ Underline\\ ".l:underlineswitch." :let g:solarized_underline=(abs(g:solarized_underline-1)) \\| colorscheme solarized<CR>"

        amenu &Solarized.&Diff\ Mode.&Low\ Diff\ Mode    :let g:solarized_diffmode="low"     \| colorscheme solarized<CR>
        amenu &Solarized.&Diff\ Mode.&Normal\ Diff\ Mode :let g:solarized_diffmode="normal"  \| colorscheme solarized<CR>
        amenu &Solarized.&Diff\ Mode.&High\ Diff\ Mode   :let g:solarized_diffmode="high"    \| colorscheme solarized<CR>

        if g:solarized_hitrail==0 | let l:hitrailswitch="On" | else | let l:hitrailswitch="Off" | endif
        exe "amenu &Solarized.&Experimental.&Turn\\ Highlight\\ Trailing\\ Spaces\\ ".l:hitrailswitch." :let g:solarized_hitrail=(abs(g:solarized_hitrail-1)) \\| colorscheme solarized<CR>"
        an    &Solarized.&Experimental.-sep-               <Nop>
        amenu &Solarized.&Experimental.&Help:\ HiTrail    :help 'solarized_hitrail'<CR>

        an    &Solarized.-sep1-                          <Nop>

        amenu &Solarized.&Autogenerate\ options          :SolarizedOptions<CR>

        an    &Solarized.-sep2-                          <Nop>

        amenu &Solarized.&Help.&Solarized\ Help          :help solarized<CR>
        amenu &Solarized.&Help.&Toggle\ Background\ Help :help togglebg<CR>
        amenu &Solarized.&Help.&Removing\ This\ Menu     :help solarized-menu<CR>

        an 9999.77 &Help.&Solarized\ Colorscheme         :help solarized<CR>
        an 9999.78 &Help.&Toggle\ Background             :help togglebg<CR>
        an 9999.79 &Help.-sep3-                          <Nop>

    endif
endfunction

autocmd ColorScheme * if g:colors_name != "solarized" | silent! aunmenu Solarized | else | call SolarizedMenu() | endif

"}}}
