]> Dogcows Code - chaz/vimcoder/blobdiff - src/com/dogcows/Editor.java
now loads template files from storage directory
[chaz/vimcoder] / src / com / dogcows / Editor.java
index 3bc81d2b3e1b2e51b2c947d1dbb4809ce24eddaf..15dad35a8f5712c70c21017377e30e73dbea181f 100644 (file)
@@ -74,7 +74,7 @@ public class Editor
         this.name = component.getClassName();
         
         // Make sure the top-level vimcoder directory exists.
-        File topDir = new File(System.getProperty("user.home"), ".vimcoder");
+        File topDir = VimCoder.getStorageDirectory();
         if (!topDir.isDirectory())
         {
             if (!topDir.mkdirs()) throw new IOException(topDir.getPath());
@@ -125,7 +125,7 @@ public class Editor
         sourceFile = new File(directory, name + "." + ext);
         if (!sourceFile.canRead())
         {
-            String text = Util.expandTemplate(Util.readResource(lang + "Template"),
+            String text = Util.expandTemplate(readTemplate(lang + "Template"),
                                               terms);
             FileWriter writer = new FileWriter(sourceFile);
             writer.write(text);
@@ -136,8 +136,8 @@ public class Editor
         File driverFile = new File(directory, "driver." + ext);
         if (!driverFile.canRead())
         {
-            String text = Util.expandTemplate(Util.readResource(lang + "Driver"),
-                                                 terms);
+            String text = Util.expandTemplate(readTemplate(lang + "Driver"),
+                                              terms);
             FileWriter writer = new FileWriter(driverFile);
             writer.write(text);
             writer.close();
@@ -168,8 +168,8 @@ public class Editor
         // Finally, expand the Makefile template and write it.
         File makeFile = new File(directory, "Makefile");
         {
-            String text = Util.expandTemplate(Util.readResource(lang + "Makefile"),
-                                         terms);
+            String text = Util.expandTemplate(readTemplate(lang + "Makefile"),
+                                              terms);
             FileWriter writer = new FileWriter(makeFile);
             writer.write(text);
             writer.close();
@@ -224,12 +224,14 @@ public class Editor
      * @param argument Arguments for the remote command.
      * @throws Exception If the command could not be sent.
      */
-    private void sendVimCommand(String command, 
+    private void sendVimCommand(String command,
                                 String[] arguments) throws Exception
     {
-        String[] exec = {"gvim", "--servername", "VimCoder" + id, command};
-        exec = Util.concat(exec, arguments);
-        Process child = Runtime.getRuntime().exec(exec, null, directory);
+        String[] vimCommand = VimCoder.getVimCommand().split("\\s");
+        String[] flags = {"--servername", "VimCoder" + id, command};
+        vimCommand = Util.concat(vimCommand, flags);
+        vimCommand = Util.concat(vimCommand, arguments);
+        Process child = Runtime.getRuntime().exec(vimCommand, null, directory);
         
         /* FIXME: This is a hack with a magic number.  The problem is the Vim
          * process doesn't fork to the background on some systems, so we can't
@@ -239,8 +241,7 @@ public class Editor
          * get the return code from the child if we can.  The workaround here is
          * to stall the thread for a little while or until we know the child
          * does exit.  If the child never exits before the timeout, we will
-         * assume it is not backgrounding and that everything worked.  This all
-         * works very well in practice, but perhaps there's a better way... */
+         * assume it is not backgrounding and that everything worked. */
         long expire = System.currentTimeMillis() + 250;
         while (System.currentTimeMillis() < expire)
         {
@@ -258,6 +259,28 @@ public class Editor
         }
     }
     
+    
+    /**
+     * Read a template.  We first look in the storage directory.  If we can't
+     * find one, we look among the resources.
+     * @param tName The name of the template.
+     * @return The contents of the template file, or an empty string.
+     */
+    private String readTemplate(String tName)
+    {
+        File templateFile = new File(VimCoder.getStorageDirectory(), tName);
+        try
+        {
+            if (templateFile.canRead()) return Util.readFile(templateFile);
+            return Util.readResource(tName);
+        }
+        catch (IOException exception)
+        {
+            return "";
+        }
+    }
+    
+    
     /**
      * Convert an array of data types to an array of strings according to a
      * given language.
This page took 0.024455 seconds and 4 git commands to generate.