Permalink
Browse files

it actually works

  • Loading branch information...
0 parents commit 6c649526706aafc770a9dbf23be9c16feba51e8d @mislav committed Jun 22, 2011
Showing with 246 additions and 0 deletions.
  1. +41 −0 Rakefile
  2. +79 −0 plugin/stylish.vim
  3. +126 −0 ruby/stylish.rb
@@ -0,0 +1,41 @@
+require 'fileutils'
+
+task :default => :install
+
+vimfiles = if ENV['VIMFILES']
+ ENV['VIMFILES']
+ elsif RUBY_PLATFORM =~ /(win|w)32$/
+ File.expand_path("~/vimfiles")
+ else
+ File.expand_path("~/.vim")
+ end
+
+files = lambda do
+ Dir['*/*'].map do |file|
+ [file, File.join(vimfiles, file)]
+ end
+end
+
+desc "Install to #{vimfiles.sub(ENV['HOME'], '~')}"
+task :install do
+ files[].map do |file, target_file|
+ FileUtils.mkdir_p(File.dirname(target_file))
+ FileUtils.cp(file, target_file, :verbose => true)
+ end
+end
+
+desc "Install by symlinking"
+task :symlink do
+ pwd = Dir.pwd
+ files[].map do |file, target_file|
+ FileUtils.mkdir_p(File.dirname(target_file))
+ FileUtils.ln_sf(File.expand_path(file, pwd), target_file, :verbose => true)
+ end
+end
+
+desc "Uninstall"
+task :uninstall do
+ files[].map do |file, target_file|
+ FileUtils.rm(target_file, :verbose => true)
+ end
+end
@@ -0,0 +1,79 @@
+if exists("g:stylish_loaded")
+ finish
+endif
+let g:stylish_loaded = 1
+
+command Stylish call <SID>StylishSetup()
+
+function s:StylishRubyWarning()
+ echohl WarningMsg
+ echo "stylish.vim requires Vim to be compiled with Ruby support"
+ echohl none
+endfunction
+
+function s:StylishSetup()
+ if has('ruby')
+ nmap <Leader>c :StylishNext<CR>
+ vmap <Leader>c :StylishNext<CR>
+ nmap <Leader>cp :StylishPrev<CR>
+ vmap <Leader>cp :StylishPrev<CR>
+ nmap <Leader>r :StylishRand<CR>
+ vmap <Leader>r :StylishRand<CR>
+ nmap <Leader>f :StylishFont<CR>
+ vmap <Leader>f :StylishFont<CR>
+ else
+ call s:StylishRubyWarning()
+ endif
+endfunction
+
+if !has('ruby')
+ finish
+endif
+
+function s:StylishInit()
+ ruby $stylish ||= Stylish.new
+endfunction
+
+command StylishNext call <SID>StylishNext()
+
+function s:StylishNext()
+ call s:StylishInit()
+ ruby $stylish.next!
+endfunction
+
+command StylishPrev call <SID>StylishPrev()
+
+function s:StylishPrev()
+ call s:StylishInit()
+ ruby $stylish.previous!
+endfunction
+
+command StylishRand call <SID>StylishRand()
+
+function s:StylishRand()
+ call s:StylishInit()
+ ruby $stylish.rand!
+endfunction
+
+command StylishFont call <SID>StylishFont()
+
+function s:StylishFont()
+ call s:StylishInit()
+ ruby $stylish.next_font!
+endfunction
+
+ruby << EOF
+ begin
+ require 'stylish'
+ rescue LoadError
+ load_path_modified = false
+ ::VIM::evaluate('&runtimepath').to_s.split(',').each do |path|
+ lib = "#{path}/ruby"
+ if !$LOAD_PATH.include?(lib) and File.directory?(lib)
+ $LOAD_PATH << lib
+ load_path_modified = true
+ end
+ end
+ retry if load_path_modified
+ end
+EOF
@@ -0,0 +1,126 @@
+# yay Stylish!
+class Stylish
+ attr_reader :current_scheme
+
+ def initialize
+ @current_scheme = vim('g:colors_name').to_s.downcase
+ @current_font, font_size = vim('&guifont').to_s.split(':h')
+ @font_size = font_size ? font_size.to_i : 11
+ end
+
+ def available_schemes
+ @available_schemes ||= vim('&runtimepath').to_s.split(',').map do |path|
+ dir = File.join(path, 'colors')
+ Dir.glob File.join(dir, '*.vim') if File.directory? dir
+ end.compact.flatten.
+ map { |file| File.basename(file, '.vim') }.sort.uniq
+ end
+
+ # TODO: support for other than OS X
+ def available_fonts
+ @available_fonts ||= begin
+ prefixes = ["", "/System", ENV['HOME']]
+ Dir[*prefixes.map {|p| "#{p}/Library/Fonts/*" }].map do |font|
+ File.basename(font).sub(/\.\w{2,4}$/, '')
+ end.sort.uniq
+ end
+ end
+
+ FONTS = [ "Andale Mono", "Anonymous Pro", "Courier New",
+ "DejaVu Sans Mono", "Inconsolata", "Menlo", "Monaco" ]
+
+ # available monospace fonts on the system
+ def monospace_fonts
+ @monospace_fonts ||= FONTS.select do |name|
+ re = Regexp.new(name.gsub(' ', ' ?'), 'i')
+ available_fonts.grep(re).any?
+ end
+ end
+
+ def next_scheme(offset = 1)
+ idx = offset_index(available_schemes, @current_scheme, offset)
+ available_schemes[idx]
+ end
+
+ def set_scheme(scheme)
+ @current_scheme = scheme
+ vim_cmd ":color #{scheme}"
+ vim_echo "color scheme: #{scheme}"
+ end
+
+ def next!
+ set_scheme next_scheme
+ end
+
+ def previous!
+ set_scheme next_scheme(-1)
+ end
+
+ def rand!
+ if !defined?(@visited_schemes) or @visited_schemes.size >= available_schemes.size
+ @visited_schemes = [@current_scheme]
+ end
+ begin
+ idx = Kernel.rand available_schemes.size
+ scheme = available_schemes[idx]
+ end while @visited_schemes.include? scheme
+ @visited_schemes << scheme
+ set_scheme scheme
+ end
+
+ def next_font(offset = 1)
+ idx = offset_index(monospace_fonts, @current_font, offset)
+ monospace_fonts[idx]
+ end
+
+ def next_font!
+ set_font next_font
+ end
+
+ def previous_font!
+ set_font next_font(-1)
+ end
+
+ def set_guifont(name, size)
+ vim_cmd ":set guifont=#{name.gsub(' ', '\ ')}:h#{size}"
+ end
+
+ def set_font(name)
+ @current_font = name
+ set_guifont name, @font_size
+ vim_echo "font: #{name}"
+ end
+
+ def set_font_size(size)
+ @font_size = size.to_i
+ set_guifont @current_font, @font_size
+ end
+
+ private
+
+ def vim(expression)
+ ::VIM.evaluate(expression)
+ end
+
+ def vim_cmd(cmd)
+ ::VIM.command(cmd)
+ end
+
+ def vim_echo(msg)
+ vim_cmd ":redraw | echo #{msg.inspect}"
+ end
+
+ # Finds the index of `item` in `array` and offsets it by the given `offset`,
+ # wrapping the number to the other end of the array if needed.
+ def offset_index(array, item, offset)
+ if idx = array.index(item)
+ idx += offset
+ if idx >= array.size then idx = idx - array.size
+ elsif idx < 0 then idx = array.size + idx
+ end
+ idx
+ else
+ 0
+ end
+ end
+end

0 comments on commit 6c64952

Please sign in to comment.