Script to open/activate header file for source file and vice versa

Help with writing and running scripts

Script to open/activate header file for source file and vice versa

Postby vzse » Mon Jul 20, 2009 12:09 am

Hi,

I'd like to have/write a macro that flips between a c++ header and body files. I was thinking something like, for example if my current open document is Foo.hpp than it opens Foo.cpp (Assuming they are at the same location) or if it is already open it simply changes to it. And the other way around too.
Cheers
vzse
Newbie
 
Posts: 1
Joined: Mon Jul 20, 2009 12:04 am

Re: Script to open/activate header file for source file and vice versa

Postby Mofi » Mon Jul 20, 2009 3:15 am

UEStudio supports that natively. In UltraEdit you have to use a script. A macro is not good because it does not support variables and that would make the macro very complicated. Here is a quickly written script for your need. It may not work on Linux because of case-sensitive file names and I have not tested it with FTP files. The whole script is not fully tested. You need the function GetFileExt from FileNameFunctions.js

Code: Select all
/* Insert here the function GetFileExt from FileNameFunctions.js !!! */

if (UltraEdit.document.length > 0) {  // Is any file open?

   var sFileExt = GetFileExt(-1);     // Get file extension of active file.

   if (sFileExt.length > 0) {         // Has the file an extension?
      // Get full name of active file and convert file extension to lowercase.
      var sFileName = UltraEdit.activeDocument.path.toLowerCase();
      var sExtension = sFileExt.toLowerCase();
      var sFileToOpen = "";

      switch (sExtension) { // Is this a known file extension?
         case "c":   sFileToOpen = sFileName.replace(/\.c$/,".h");
                     break;
         case "cpp": sFileToOpen = sFileName.replace(/\.cpp$/,".hpp");
                     break;
         case "h":   sFileToOpen = sFileName.replace(/\.h$/,".c");
                     break;
         case "hpp": sFileToOpen = sFileName.replace(/\.hpp$/,".cpp");
                     break;
      }

      if (sFileToOpen.length > 0) {  // Is there a header or source file for the active file?

         var nDocIndex = 0; // Look if this file is already open.
         do {
            sFileName = UltraEdit.document[nDocIndex].path.toLowerCase();
            if (sFileName == sFileToOpen) {
               UltraEdit.document[nDocIndex].setActive();
               break;
            }
         }
         while (++nDocIndex < UltraEdit.document.length)

         if (nDocIndex >= UltraEdit.document.length) UltraEdit.open(sFileToOpen);
      }
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts