How to toggle a word

Help with writing and running scripts

How to toggle a word

Postby aleksazr » Sat Oct 20, 2012 2:12 pm

Is it possible, in any way, to toggle a word with UE?

Say my cursor is on the word TRUE, and I want it toggled into FALSE.
If I toggle it again, it should read TRUE again.

I have a list of paired words I would like to toggle:
TRUE <> FALSE,
rising_edge <> falling_edge,
signal <> variable,
and so on...
aleksazr
Basic User
Basic User
 
Posts: 20
Joined: Sun Dec 18, 2011 7:16 pm

Re: How to toggle a word

Postby Mofi » Sun Oct 21, 2012 4:16 am

Sure, this is no problem with a small script added to Scripting - Script List and with a hotkey or chord assigned to this script for fast execution. Here is an example of such a script written for UE v17.20 or UES v11.20 and later versions.

Code: Select all
// Is at least 1 file opened?
if (UltraEdit.document.length > 0) {

   // Define the word pairs.
   var asWords1 = new Array("TRUE","rising_edge","signal");
   var asWords2 = new Array("FALSE","falling_edge","variable");

   // Get position of caret in active file.
   var nLine = UltraEdit.activeDocument.currentLineNum;
   var nColumn = UltraEdit.activeDocument.currentColumnNum;

   // Select the word at caret position.
   UltraEdit.activeDocument.selectWord();
   if (UltraEdit.activeDocument.isSel()) {

      // Compare the selected word against the words in the 2 arrays.
      for (var nWordIndex = 0; nWordIndex < asWords1.length; nWordIndex++) {

         // Is selected word present in first array?
         if (UltraEdit.activeDocument.selection == asWords1[nWordIndex]) {
            UltraEdit.activeDocument.write(asWords2[nWordIndex]);
            break;
         }
         // Is selected word present in second array?
         if (UltraEdit.activeDocument.selection == asWords2[nWordIndex]) {
            UltraEdit.activeDocument.write(asWords1[nWordIndex]);
            break;
         }
      }
      // Discard the selection and restore caret if nothing changed.
      if (nWordIndex == asWords1.length) {
         UltraEdit.activeDocument.cancelSelect();
         UltraEdit.activeDocument.gotoLine(nLine,nColumn);
      }
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to toggle a word

Postby aleksazr » Sun Oct 21, 2012 5:32 am

Thank you very much, works great!
I wanted to have this for a very long time.
aleksazr
Basic User
Basic User
 
Posts: 20
Joined: Sun Dec 18, 2011 7:16 pm

Re: How to toggle a word

Postby Jaretin » Mon Oct 22, 2012 2:28 am

To be faster.
Code: Select all
    // Is at least 1 file opened?
    if (UltraEdit.document.length > 0) {

       // Define the word pairs.
       var asWords = {};
       asWords["TRUE"] = "FALSE";
       asWords["FALSE"] = "TRUE";
       asWords["rising_edge"] = "falling_edge";
       asWords["falling_edge"] = "rising_edge";
       asWords["signal"] = "variable";
       asWords["variable"] = "signal";

       // Get position of caret in active file.
       var nLine = UltraEdit.activeDocument.currentLineNum;
       var nColumn = UltraEdit.activeDocument.currentColumnNum;

       // Select the word at caret position.
       UltraEdit.activeDocument.selectWord();
       if (UltraEdit.activeDocument.isSel()) {
             if ( asWords[UltraEdit.activeDocument.selection] != null )
              UltraEdit.activeDocument.write(asWords[UltraEdit.activeDocument.selection]);
          else {
             UltraEdit.activeDocument.cancelSelect();
             UltraEdit.activeDocument.gotoLine(nLine,nColumn);
          }
       }
    }
User avatar
Jaretin
Basic User
Basic User
 
Posts: 19
Joined: Sun Mar 25, 2007 11:00 pm


Return to Scripts