Tapatalk

How to toggle a word

How to toggle a word

20
Basic UserBasic User
20

    Oct 20, 2012#1

    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...

    6,685587
    Grand MasterGrand Master
    6,685587

      Oct 21, 2012#2

      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);
            }
         }
      }

      20
      Basic UserBasic User
      20

        Oct 21, 2012#3

        Thank you very much, works great!
        I wanted to have this for a very long time.

        21
        Basic UserBasic User
        21

          Oct 22, 2012#4

          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);
                    }
                 }
              }