Paste with a number existing in clipboard content several times incremented first

Paste with a number existing in clipboard content several times incremented first

2
NewbieNewbie
2

    Jul 05, 2019#1

    Hi all,

    I'm a new user of UltraEdit, after years with Notepad++. Now exploring the power of macros and scripts here. I've searched in the forum and found some similar but not exact topics, so I'm wondering if someone can support me.

    I'd like to do something, I think, simple. Supposing to have some code like

    Code: Select all

    <div class="form-group">
       <label class="control-label" for="input-field3"><?php echo $entry_field3; ?></label>
       <div>[size=50]<input type="text" name="field3" value="<?php echo $field3; ?>" id="input-field3" class="form-control" />[/size][size=50]</div>[/size]
    </div>
    I'd desire to select and copy this part of code and replicate it, increasing the counter. Basically I'd like to have a "Paste" command which manipulates the code before pasting by increasing numbers.

    Expected "Paste Special" result would be so:

    Code: Select all

    <div class="form-group">
       <label class="control-label" for="input-field4"><?php echo $entry_field4; ?></label>
       <div><input type="text" name="field4" value="<?php echo $field4; ?>" id="input-field4" class="form-control" /></div>
    </div>
    Is it something achievable with a macro or script?

    I was beginning to study CountUp macro by Mofi but I'm still a little bit newbie and it is not so easy.

    Thanks in advance,

    Regards, M

    6,686585
    Grand MasterGrand Master
    6,686585

      Jul 05, 2019#2

      UltraEdit macros do not support variables. The CountUp macro was written by me as a replacement for the lack of integer variables in macros before script feature was introduced with UltraEdit for Windows v13.00 many years ago.

      Nowadays a simple UltraEdit script can be used for such a task like this one:

      Code: Select all

      // Search in content of active clipboard case-sensitive for "field" with
      // one or more more digits appended. The function match of JavaScript
      // String object returns a null pointer if no string is found matching
      // the search expression. Otherwise an array is returned by the function
      // match and element 0 of this array contains the found string of interest.
      var asField = UltraEdit.clipboardContent.match(/field\d+/);
      if (asField)
      {
         // Get just the number of found field string copied to a string variable.
         var sFieldNumber = asField[0].substr(5);
         // Convert the decimal number from string to an integer number
         // and increment it by one.
         var nFieldNumber = parseInt(sFieldNumber,10) + 1;
         // Convert the incremented integer number back to a string.
         sFieldNumber = nFieldNumber.toString(10);
         // Replace all field numbers in clipboard by new number.
         UltraEdit.clipboardContent = UltraEdit.clipboardContent.replace(/field\d+/g,"field"+sFieldNumber);
      }
      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         UltraEdit.activeDocument.paste();
      }
      
      What do you know about Smart Templates?

      I think, for such a task the definition of a global or language smart template with the following content would be a good idea:

      Code: Select all

      <div class="form-group">
         <label class="control-label" for="input-field[+number+]"><?php echo $entry_field[+number+]; ?></label>
         <div>[size=50]<input type="text" name="field[+number+]" value="<?php echo $field[+number+]; ?>" id="input-field[+number+]" class="form-control" />[/size][size=50]</div>[/size]
      </div>
      
      The user (you) is prompted for the number on inserting this smart template, and after entering it and pressing RETURN or ENTER or TAB, this block is inserted into active file at current caret position with all occurrences of [+number+] replaced by the entered number.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Jul 05, 2019#3

        Great Mofi! Your script works like a charm. This is the solution suitable for me.

        Regarding smart templates, I don't think they can help because actually my need is to copy different fragments of text with incremented IDs. I don't have a specific template to be replicated.

        Thanks again a lot.

        M