Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

安装 changeColorScheme

按顺序/随机选取配色,可以删除被选择的配色
http://www.vim.org/scripts/script.php?script_id=870
  • Loading branch information...
commit b3aecf3f630daed373c3bcc33c8216d8c493d99f 1 parent 9770c30
依云 authored

Showing 1 changed file with 262 additions and 0 deletions. Show diff stats Hide diff stats

  1. +262 0 plugin/changeColorScheme.vim
262 plugin/changeColorScheme.vim
... ... @@ -0,0 +1,262 @@
  1 +"------------------------------------------------------------------------------
  2 +" File: changeColorScheme.vim
  3 +" vimscript #870
  4 +" URL on VIM: http://www.vim.org/scripts/script.php?script_id=870
  5 +"------------------------------------------------------------------------------
  6 +" Got ideas from following tips and scripts
  7 +" vimtip #341, vimtip #358, vimscript #668, vimscript #109
  8 +"------------------------------------------------------------------------------
  9 +" Author: Hosup Chung <hosup.chung@gmail.com>
  10 +"
  11 +" Created: 2003 December 31
  12 +" Last Updated: 2010 July 23
  13 +"
  14 +" Version: 0.4
  15 +" 0.4: added RemoveCurrentColorScheme() function (requested by auto didakto)
  16 +"
  17 +" 0.3: updated code to handle file not found error due to color schemes
  18 +" were removed
  19 +"
  20 +" 0.2: updated RandomColorScheme()
  21 +"
  22 +" 0.12: I finally tested this plugin on OS X and Linux
  23 +" When searching color schemes, substituting eol didn't work on Linux.
  24 +" I'm using "\n" instead of '\n' now, and it seems working.
  25 +"
  26 +" 0.11: fixed some typo in Usage description
  27 +"
  28 +" 0.1: initial upload
  29 +"------------------------------------------------------------------------------
  30 +" Install:
  31 +" Copy this script in your plugin directory
  32 +"------------------------------------------------------------------------------
  33 +" Usage:
  34 +" When this script is loaded, it will populate an array with each of color
  35 +" scheme's file path. You can then load different color schemes using
  36 +" NextColorScheme(), PreviousColorScheme() or RandomColorScheme().
  37 +"
  38 +" Or if you don't like current color scheme you can call
  39 +" RemoveCurrentColorScheme() to remove the file.
  40 +"
  41 +" There are 4 main functions
  42 +" 1. You can either call them directly
  43 +" :call NextColorScheme()
  44 +" :call PreviousColorScheme()
  45 +" :call RandomColorScheme()
  46 +" :call RemoveCurrentColorScheme()
  47 +"
  48 +" 2. You can map and save them in your [._]gvimrc.
  49 +" map <F4> :call NextColorScheme()<CR>
  50 +" map <S-F4> :call PreviousColorScheme()<CR>
  51 +" map <C-F4> :call RandomColorScheme()<CR>
  52 +" map <F3> :call RemoveCurrentColorScheme()<CR>
  53 +"
  54 +" 3. You can also start each vim session with random color scheme by
  55 +" adding following line in your [._]gvimrc
  56 +" call RandomColorScheme()
  57 +"
  58 +"------------------------------------------------------------------------------
  59 +" Tip:
  60 +" You can change your rulerformat in your [._]vimrc to display the name of
  61 +" current color scheme on status line.
  62 +"
  63 +" First, add this line to display status information
  64 +" set ruler
  65 +"
  66 +" And add %{g:colors_name} in rulerformat to display name of current color
  67 +" scheme. For example,
  68 +" set rulerformat=%55(:%{g:colors_name}:\ %5l,%-6(%c%V%)\ %P%)
  69 +"
  70 +" However, you will see an error message if you didn't load color scheme at
  71 +" startup. So you might want to add %{GetColorSyntaxName()} instead.
  72 +" set rulerformat=%55(:%{GetColorSyntaxName()}:\ %5l,%-6(%c%V%)\ %P%)
  73 +"
  74 +" GetColorSyntaxName() function is included in this script. It returns
  75 +" the value of g:colors_name if it exists, otherwise an empty string. If you
  76 +" are using a console version, then you might have to copy
  77 +" GetColorSyntaxName() into .vimrc, because I think the plugin files get
  78 +" loaded after evaluating .vimrc.
  79 +"------------------------------------------------------------------------------
  80 +
  81 +if exists("g:change_color_scheme")
  82 + finish
  83 +endif
  84 +
  85 +let g:change_color_scheme="0.3"
  86 +
  87 +if 1
  88 + let s:save_cpo = &cpoptions
  89 +endif
  90 +set cpo&vim
  91 +
  92 +" You can pick any sep character that will mark between array elements.
  93 +" However, because each array element is a file path, it would be better
  94 +" to pick an invalid file character.
  95 +let s:sep='?'
  96 +
  97 +" Just in case when some of color scheme files are removed, I need to keep track
  98 +" of which direction the user was moving in color scheme array.
  99 +let s:direction='NEXT'
  100 +
  101 +" I got an idea from lightWeightArray.vim, and came up ElementAt.
  102 +" array is actually a string. Each element in the array is separated by sep
  103 +" character.
  104 +" The function will returns the array_index element or -1 for not found
  105 +function! ElementAt (array, sep, array_index)
  106 + " if array is empty or array_index is negative then return -1
  107 + if strlen(a:array) == 0 || a:array_index < 0
  108 + return -1
  109 + endif
  110 +
  111 + let char_pos = 0 " current character position within array
  112 + let i = 0 " current array index position
  113 +
  114 + " Search the array element on a:array_index position.
  115 + while i != a:array_index
  116 + let char_pos = match(a:array, a:sep, char_pos)
  117 + if char_pos == -1
  118 + return -1 " couldn't find it
  119 + endif
  120 +
  121 + let char_pos = char_pos + 1
  122 + let i = i + 1
  123 + endwhile
  124 +
  125 + " then find where the current array element ends
  126 + let array_element_endpos = match(a:array, a:sep, char_pos)
  127 + if array_element_endpos == -1
  128 + " must be the last array element
  129 + let array_element_endpos = strlen(a:array)
  130 + endif
  131 +
  132 + let color_scheme_path = strpart(a:array,char_pos,(array_element_endpos-char_pos))
  133 +
  134 + " if path is not found, probably some of color scheme files are
  135 + " removed. Reinitialize the array and call ElementAt again.
  136 + if filereadable(color_scheme_path) == 0
  137 + call InitializeVariables()
  138 +
  139 + if ((a:array_index == 0 || a:array_index >= s:total_schemes-1) && s:direction == 'NEXT')
  140 + let s:scheme_index = 0
  141 + else
  142 + let s:scheme_index = s:total_schemes-1
  143 + endif
  144 + return ElementAt(s:color_schemes, s:sep, s:scheme_index)
  145 + endif
  146 +
  147 + " return the color scheme file path in a:array_index position
  148 + return color_scheme_path
  149 +endfunction " ElementAt
  150 +
  151 +" If g:colors_name is defined, return the name of current color syntax name.
  152 +" Otherwise return an empty string
  153 +function! GetColorSyntaxName()
  154 + if exists('g:colors_name')
  155 + return g:colors_name
  156 + else
  157 + return ''
  158 + endif
  159 +endfunction "GetColorSyntaxName
  160 +
  161 +function! InitializeVariables()
  162 + " get all color scheme file path and save it to s:color_schemes. It will be
  163 + " treated as a string array which elements are separated by sep character.
  164 + let s:color_schemes = substitute(globpath(&runtimepath,"colors/*.vim"), "\n", s:sep, 'g')
  165 + let s:total_schemes = 0
  166 + let s:scheme_index = 0
  167 +
  168 + if (strlen(s:color_schemes) > 0)
  169 + " determine the total number of color schemes by counting sep
  170 + " character from color_schemes string. Unless there's no color
  171 + " scheme, total number of color scheme is 1 bigger than number
  172 + " of sep characters
  173 + let found = 0
  174 + while found != -1
  175 + let found = match(s:color_schemes, s:sep, found+1)
  176 + let s:total_schemes = s:total_schemes + 1
  177 + endwhile
  178 + endif
  179 +endfunction
  180 +
  181 +" load next color schemes.
  182 +function! NextColorScheme()
  183 + let s:scheme_index = s:scheme_index + 1
  184 + let s:direction = 'NEXT'
  185 + call LoadColorScheme()
  186 +endfunction
  187 +
  188 +" load previous color schemes.
  189 +function! PreviousColorScheme()
  190 + let s:scheme_index = s:scheme_index - 1
  191 + let s:direction = 'PREVIOUS'
  192 + call LoadColorScheme()
  193 +endfunction
  194 +
  195 +" load randomly chosen color scheme.
  196 +" vim still doesn't have a function that returns millisecons. As a result,
  197 +" it's difficult to generate a random number sequence. My previous attempt was
  198 +" to use just localtime(). But since it only returns seconds, quickly calling
  199 +" series of RandomColorScheme() is just same as NextColorScheme() except the
  200 +" first call. Now I found another function getfsize(), which returns the file
  201 +" size. I think adding localtime() and current color scheme's filesize seems
  202 +" random enough for this script.
  203 +function! RandomColorScheme()
  204 + let s:current_scheme_fsize = getfsize(ElementAt(s:color_schemes, s:sep, s:scheme_index))
  205 + " set a random scheme_index from the range (0 ... total_schemes-1).
  206 + let s:scheme_index = (localtime()+s:current_scheme_fsize) % s:total_schemes
  207 + call LoadColorScheme()
  208 +endfunction
  209 +
  210 +" load a color scheme
  211 +function! LoadColorScheme()
  212 + " quit if there's no color scheme
  213 + if s:total_schemes == 0
  214 + return 0
  215 + endif
  216 +
  217 + " wrap around scheme_index for either direction
  218 + if s:scheme_index < 0
  219 + let s:scheme_index = s:total_schemes-1
  220 + elseif s:scheme_index >= s:total_schemes
  221 + let s:scheme_index = 0
  222 + endif
  223 +
  224 + " ElementAt returns the name of color scheme on scheme_index position in
  225 + " color_schemes array. Then we will load (source) the scheme.
  226 + exe "source " ElementAt(s:color_schemes, s:sep, s:scheme_index)
  227 +endfunction
  228 +
  229 +function! RemoveCurrentColorScheme()
  230 + let s:current_scheme_path = ElementAt(s:color_schemes, s:sep, s:scheme_index)
  231 + let s:isFilewritable = filewritable(s:current_scheme_path)
  232 +
  233 + if s:isFilewritable == 1
  234 + let s:response = input("Are you sure to remove current color scheme (" . s:current_scheme_path . ") file? [y/n] ")
  235 +
  236 + if (s:response == "y" || s:response == "Y")
  237 + let s:returnValue = delete(s:current_scheme_path)
  238 + if s:returnValue == 0
  239 + call InitializeVariables()
  240 + call RandomColorScheme()
  241 + redraw | echo s:current_scheme_path . " was removed"
  242 + else
  243 + echo 'Could not remove current color scheme file'
  244 + endif
  245 + else
  246 + echo 'Removing current color scheme cancelled'
  247 + endif
  248 + elseif filereadable(s:current_scheme_path) == 1
  249 + echo 'Could not remove current color scheme file'
  250 + else
  251 + echo 'Could not read the current color scheme file'
  252 + endif
  253 +endfunction
  254 +
  255 +call InitializeVariables()
  256 +
  257 +" restore 'cpoptions'
  258 +set cpo&
  259 +if 1
  260 + let &cpoptions = s:save_cpo
  261 + unlet s:save_cpo
  262 +endif

0 comments on commit b3aecf3

Please sign in to comment.
Something went wrong with that request. Please try again.