PATCH: default installation directories

Doug Kearns dougkearns at gmail.com
Mon Aug 15 10:52:14 EDT 2005


This is just a quick patch to use $VIM/vimfiles and
$HOME/{.vim,vimfiles} rather than $VIMRUNTIME as the two default
installation directories offered by vim-ruby-install.rb.

$VIM is determined by:
1. checking for an env variable VIM or if this is not found - as in the default case
2. by parsing vim --version on UNIX or reading the 'path' key under
   HKEY_LOCAL_MACHINE\Software\Vim\Gvim on Windows

Perhaps $VIMRUNTIME should be a fall-back but I think there are some
pretty significant problems if we can't find $VIM.

It could undoubtedly be improved...

Regards,
Doug

PS. Apologies for the incorrect threading of the two previous messages.
I stuck them in my drafts folder late one night and forgot to edit the
In-Reply-To header...*sigh* I know how this stuff really annoys some
people. ;-)


Index: README
===================================================================
RCS file: /var/cvs/vim-ruby/vim-ruby/README,v
retrieving revision 1.5
diff -u -r1.5 README
--- README	6 Nov 2003 10:17:33 -0000	1.5
+++ README	15 Aug 2005 14:39:33 -0000
@@ -26,10 +26,10 @@
   - Thank you very much for taking an interest.
 
 Contents of the project:
-  - The compiler, ftplugin, indent and syntax directories contain the ruby.vim
-    files that are to be copied somewhere Vim can see them.
-  - install.rb performs this copying.
-  - build.rb creates a tarball for easy distribution.
+  - The compiler, ftdetect, ftplugin, indent and syntax directories contain
+    the ruby.vim files that are to be copied to a location somewhere in the Vim
+    'runtimepath'.
+  - vim-ruby-install.rb performs this copying.
 
 How you get these files into Vim:
   - By downloading the project via a snapshot or anonymous CVS, you can keep
@@ -37,8 +37,9 @@
     directory.
   - By downloading one of the tarballs, you can easily install the latest
     stable or development version wherever you like on your machine.  No
-    README, no install.rb, just Vim files.  You would typically install these
-    wherever Vim is installed, in the (for example) "vim62" directory.
+    README, no vim-ruby-install.rb, just Vim files.  You would typically
+    install these into either $VIM/vimfiles, for system-wide use, or $HOME/.vim
+    ($HOME/vimfiles on Windows) for personal use.
   - Remember that when you install Vim in the first place, all of these files
     are present.  The purpose of downloading and installing them from
     RubyForge is to get the latest version of them.
@@ -70,7 +71,8 @@
     (gsinclair at soyabean.com.au).
   - To ask about the contents of the configuration files, ask on the mailing
     list, as different people maintain the different files.  Gavin knows
-    nothing about the syntax file, for instance.
+    nothing about the syntax file, for instance. (Come to think of it, nor does
+    Doug - djk)
 
 Project gossip:
   - Two of the configuration file maintainers, Doug Kearns and Gavin Sinclair,
Index: bin/vim-ruby-install.rb
===================================================================
RCS file: /var/cvs/vim-ruby/vim-ruby/bin/vim-ruby-install.rb,v
retrieving revision 1.4
diff -u -r1.4 vim-ruby-install.rb
--- bin/vim-ruby-install.rb	11 Aug 2005 13:51:28 -0000	1.4
+++ bin/vim-ruby-install.rb	15 Aug 2005 14:39:33 -0000
@@ -2,10 +2,10 @@
 
 # vim-ruby-install: install the Vim config files for Ruby editing
 #
-#  * scope out the target directry and get user to confirm
+#  * scope out the target directory and get user to confirm
 #    * if no directory found, ask user
 #    * allow user to force a search for a Windows gvim installation
-#  * find source files from gem or from current directory
+#  * find source files from gem or from top level directory
 #  * copy to target directory, taking account of
 #    * line endings (NL for Unix-ish; CRLF for Windows)
 #    * permissions (755 for directories; 644 for files)
@@ -26,11 +26,11 @@
   syntax/eruby.vim
   syntax/ruby.vim
 }
-# XXX: what do we do with 'filetype/filetype.vim' ???
-                  
+#FIXME: ftdetect/ruby.vim - vim 6.3+ only? This won't cause problems for
+#       earlier versions; it just won't work!
 
   #
-  # Miscelleneous functions in the user's environment.
+  # Miscellaneous functions in the user's environment.
   #
 class Env
     #
@@ -47,18 +47,15 @@
 
     #
     # Returns the path to the directory where the vim configuration files will be copied from.
-    # The first preference is the current directory.  If that fails, we look for the RubyGems
-    # package 'vim-ruby'.  Failing that, we return +nil+.
+    # The first preference is the directory above this script.  If that fails, we look for the
+    # RubyGems package 'vim-ruby'.  Failing that, we return +nil+.
     #
   def Env.determine_source_directory
-      # 1. Try the current directory.
-    if SOURCE_FILES.all? { |path| FileTest.file?(path) }
-      return '.'
-      # 2. Try the directory above this installation script.
-    elsif SOURCE_FILES.map { |f| File.join(File.dirname($0), '..', f) }.all? { |path| FileTest.file?(path) }
-      return File.join(File.dirname($0), '..')
+      # 1. Try the directory above this installation script.
+    if SOURCE_FILES.map { |f| File.join(File.dirname($0), '..', f) }.all? { |path| FileTest.file?(path) }
+      return File.expand_path(File.join(File.dirname($0), '..'))
     end
-      # 3. Try the gem 'vim-ruby'.
+      # 2. Try the gem 'vim-ruby'.
     begin
       require 'rubygems'
       raise "Need RubyGems 0.8+" if Gem::RubyGemsPackageVersion < '0.8'
@@ -77,6 +74,29 @@
     return nil
   end
 
+    # Returns the Vim installation directory ($VIM).
+    # FIXME: print warning if vim command not in PATH or appropriate key not in registry?
+  def Env.determine_vim_dir
+    installation_dir = ENV['VIM'] ||
+    case Env.determine_target_os
+    when :UNIX
+      IO.popen('vim --version 2>/dev/null') do |version|
+	dir = version.read[/fall-back for \$VIM: "(.*)"/, 1]
+      end
+    when :WINDOWS
+      require 'win32/registry'
+      path = ''
+      Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Vim\Gvim') do |reg|
+	path = reg['path', Win32::Registry::REG_SZ]
+      end
+      # FIXME: Does Registry#[] ever return nil? Exceptions?
+      unless path.empty? or path.nil?
+	dir = path.sub(/\\vim\d\d\\gvim.exe/i, '')
+      end
+    end
+    return installation_dir
+  end
+
   def Env.ask_user(message)
     print message
     gets.strip
@@ -158,21 +178,14 @@
   # user options; but is ultimately created with one in mind.
   #
 class TargetDirectory::Finder
-  POTENTIAL_DIRECTORIES = {
-    :UNIX => [
-      "/usr/local/share/vim",
-      "/usr/local/vim",
-      "/usr/share/vim",
-      "/usr/vim",
-      "/opt/share/vim",
-      "/opt/vim"
-    ],
-    :WINDOWS => [ File.join(ENV['PROGRAMFILES'], 'vim') ]
-  }
 
     # Guides the user through a selection process, ending in a chosen directory. 
   def find_target_directory
-      # 1. Try the potentials (if there are any).
+      # 1. Was a directory specified using the --directory option?
+    if option_dir = $options[:target_dir]
+      return option_dir
+    end
+      # 2. Try the potentials (if there are any).
     if dirs = _potential_directories and not dirs.empty?
       puts
       puts "Possible Vim installation directories:"
@@ -186,23 +199,15 @@
         return chosen_directory
       end
     end 
-      # 2. We didn't find any, or the user wants to enter another.
+      # 3. We didn't find any, or the user wants to enter another.
     if dirs.empty?
       puts
       puts "Couldn't find any Vim installation directories."
     end
-    loop do
-      dir = Env.ask_user "Please enter the full path to your Vim installation directory: "
-      dir = File.expand_path(dir)
-      if FileTest.directory?(dir)
-        entered_directory = dir
-        puts
-        return entered_directory
-      else
-        puts " *** That directory doesn't exist!"
-      end
-    end
-      # 3. We don't get here; every path contains a return statement.
+    entered_directory = Env.ask_user "Please enter the full path to your Vim installation directory: "
+    entered_directory = File.expand_path(entered_directory)
+    return entered_directory
+      # 4. We don't get here; every path contains a return statement.
   end
   
  private 
@@ -210,17 +215,26 @@
     # Return an array of _potential_ directories (i.e. they exist).  Take the options into
     # account.
   def _potential_directories
-    dirs = POTENTIAL_DIRECTORIES[Env.determine_target_os].select { |d| FileTest.directory?(d) }
-    dirs.map { |d| _vim_runtime_directory(d) }
-  end
-
-    # Given a directory like '/usr/share/vim', returns a directory like '/usr/share/vim/vim63'.
-  def _vim_runtime_directory(dir)
-    if File.basename(dir) =~ /vim\d+/
-      dir
-    else
-      Dir.glob("#{dir}/vim*").select { |d| d =~ /vim\d+/ }.sort.last
-    end
+    dirs = []
+    dirs << _vim_user_dir
+    dirs << _vim_system_dir
+    return dirs.compact.map { |dir| File.expand_path(dir) }
+  end
+
+    # Return the Vim system preferences directory
+  def _vim_system_dir
+    dir = ENV['VIM'] || Env.determine_vim_dir
+    system_dir = dir + "/vimfiles" if dir
+    return system_dir
+  end
+
+    # Return the Vim user preferences directory
+  def _vim_user_dir
+    extension = { :UNIX => "/.vim", :WINDOWS => "/vimfiles" }
+    platform = Env.determine_target_os
+    dir = ENV['HOME']
+    user_dir = dir + extension[platform] if dir
+    return user_dir
   end
 
 end  # class TargetDirectory::Finder
@@ -331,7 +345,7 @@
      vim-ruby-install.rb: Install the vim-ruby configuration files
 
       About:
-        * Detects the Vim runtime directory
+        * Detects the Vim user and system-wide preferences directories
           * User to confirm before proceeding
           * User may specify other directory
         * Takes config files from current directory or from vim-ruby gem
@@ -368,15 +382,30 @@
           Cygwin or MinGW.
     
         * This installer is quite new (2004-09-20).  Please report bugs to
-          gsinclair at soyabea.com.au.
+          gsinclair at soyabean.com.au.
   }.gsub(/^    /, '')
 end
 op.parse!(ARGV)
 
 source_dir = Env.determine_source_directory
-if source_dir.nil? then raise "Can't find source directory"; end
-target_dir = $options[:target_dir] || TargetDirectory.finder.find_target_directory
-VimRubyInstaller.new(source_dir, target_dir).install
+if source_dir.nil?
+  raise "Can't find source directory"
+end
 
-# vim: ft=ruby
+target_dir = TargetDirectory.finder.find_target_directory
+if not File.directory?(target_dir)
+  puts
+  puts "Target directory '#{target_dir}' does not exist."
+  response = Env.ask_user "Do you want to create it? [Yn] "
+  if response.strip =~ /^y(es)?$/i
+    FileUtils.mkdir_p(target_dir, :verbose => true)
+  else
+    puts
+    puts "Installation aborted."
+    exit
+  end
+end
+
+VimRubyInstaller.new(source_dir, target_dir).install
 
+# vim: ft=ruby sw=2 sts=2 ts=8:


More information about the vim-ruby-devel mailing list