Tapatalk

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

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

1
NewbieNewbie
1

    Jul 20, 2009#1

    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

    6,685587
    Grand MasterGrand Master
    6,685587

      Jul 20, 2009#2

      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);
            }
         }
      }
      Best regards from an UC/UE/UES for Windows user from Austria