Wrap it! script

Ask questions about how to create a script or swap scripts with other users.
Post Reply
kbieb86
Posts: 41
Joined: 26 Aug 2016 13:49

Wrap it! script

Post by kbieb86 »

This script opens up a dialog box with eight buttons, one each for "", '', (), {}, [], <>, /**/, <!---->. Pressing one wraps the selected text in that set of symbols. I have this assigned to a shortcut key and I "page through" the buttons with the arrow keys and hit return for the one I want.

Code: Select all

function wrapSelection(e) {
	myForm.Close;
	switch(e.Name) {
		case "doubleQuote": {
			Document.SelText = "\"" + Document.SelText + "\"";
			break;
		}
		case "singleQuote": {
			Document.SelText = "'" + Document.SelText + "'";
			break;
		}
		case "parentheses": {
			Document.SelText = "(" + Document.SelText + ")";
			break;
		}
		case "curlyBraces": {
			Document.SelText = "{" + Document.SelText + "}";
			break;
		}
		case "squareBracket": {
			Document.SelText = "[" + Document.SelText + "]";
			break;
		}
		case "angleBracket": {
			Document.SelText = "<" + Document.SelText + ">";
			break;
		}
		case "jsComment": {
			Document.SelText = "/*" + Document.SelText + "*/";
			break;
		}
		case "htmlComment": {
			Document.SelText = "<!--" + Document.SelText + "-->";
			break;
		}
	}
}
TForm myForm = new TForm(nil);
myForm.Caption = "Wrap It!";
myForm.BorderStyle = bsDialog;
myForm.Position = poScreenCenter;
var buttonTopIncrement = 35;
var buttonTop = 5;
var buttonLeft = 5;
var buttonWidth = 90;
myForm.Width = buttonWidth + 25;
TButton wrapWithDoubleQuote = TButton.Create(myForm);
wrapWithDoubleQuote.Parent = myForm;
wrapWithDoubleQuote.Name = "doubleQuote";
wrapWithDoubleQuote.Caption = "\"selection\"";
wrapWithDoubleQuote.Width = buttonWidth;
wrapWithDoubleQuote.Top = buttonTop;
wrapWithDoubleQuote.Left = buttonLeft;
wrapWithDoubleQuote.OnClick = "wrapSelection";
TButton wrapWithSingleQuote = TButton.Create(myForm);
wrapWithSingleQuote.Parent = myForm;
wrapWithSingleQuote.Name = "singleQuote";
wrapWithSingleQuote.Caption = "'selection'";
wrapWithSingleQuote.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithSingleQuote.Top = buttonTop;
wrapWithSingleQuote.Left = buttonLeft;
wrapWithSingleQuote.OnClick = "wrapSelection";
TButton wrapWithParentheses = TButton.Create(myForm);
wrapWithParentheses.Parent = myForm;
wrapWithParentheses.Name = "parentheses";
wrapWithParentheses.Caption = "(selection)";
wrapWithParentheses.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithParentheses.Top = buttonTop;
wrapWithParentheses.Left = buttonLeft;
wrapWithParentheses.OnClick = "wrapSelection";
TButton wrapWithCurlyBraces = TButton.Create(myForm);
wrapWithCurlyBraces.Parent = myForm;
wrapWithCurlyBraces.Name = "curlyBraces";
wrapWithCurlyBraces.Caption = "{selection}";
wrapWithCurlyBraces.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithCurlyBraces.Top = buttonTop;
wrapWithCurlyBraces.Left = buttonLeft;
wrapWithCurlyBraces.OnClick = "wrapSelection";
TButton wrapWithSquareBracket = TButton.Create(myForm);
wrapWithSquareBracket.Parent = myForm;
wrapWithSquareBracket.Name = "squareBracket";
wrapWithSquareBracket.Caption = "[selection]";
wrapWithSquareBracket.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithSquareBracket.Top = buttonTop;
wrapWithSquareBracket.Left = buttonLeft;
wrapWithSquareBracket.OnClick = "wrapSelection";
TButton wrapWithAngleBracket = TButton.Create(myForm);
wrapWithAngleBracket.Parent = myForm;
wrapWithAngleBracket.Name = "angleBracket";
wrapWithAngleBracket.Caption = "<selection>";
wrapWithAngleBracket.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithAngleBracket.Top = buttonTop;
wrapWithAngleBracket.Left = buttonLeft;
wrapWithAngleBracket.OnClick = "wrapSelection";
TButton wrapWithJSComment = TButton.Create(myForm);
wrapWithJSComment.Parent = myForm;
wrapWithJSComment.Name = "jsComment";
wrapWithJSComment.Caption = "/*selection*/";
wrapWithJSComment.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithJSComment.Top = buttonTop;
wrapWithJSComment.Left = buttonLeft;
wrapWithJSComment.OnClick = "wrapSelection";
TButton wrapWithHTMLComment = TButton.Create(myForm);
wrapWithHTMLComment.Parent = myForm;
wrapWithHTMLComment.Name = "htmlComment";
wrapWithHTMLComment.Caption = "<!--selection-->";
wrapWithHTMLComment.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithHTMLComment.Top = buttonTop;
wrapWithHTMLComment.Left = buttonLeft;
wrapWithHTMLComment.OnClick = "wrapSelection";
myForm.Height = buttonTop + 65;
myForm.ShowModal;
myForm.Free;
09/20/2016 edit: added comment wrapping
10/11/2016 edit: minor coding edit, no functionality change
Nierewa
Posts: 3
Joined: 21 Feb 2019 09:37

Re: Wrap it! script

Post by Nierewa »

How can I add this script to Rj TextEd?
I can't figure out how to work with scripts.
User avatar
Rickard Johansson
Site Admin
Posts: 6577
Joined: 19 Jul 2006 14:29

Re: Wrap it! script

Post by Rickard Johansson »

Copy and paste the code to a new file and save it as e.g. "Wrap It.jsfs".

Add the script as a tool (Tools -> Configure tools...). Just remember to select script in the drop down box. The default is "Executable (.exe|.com|.bat)".
Nierewa
Posts: 3
Joined: 21 Feb 2019 09:37

Re: Wrap it! script

Post by Nierewa »

Cool.
Thanks :-D

And this can be done the same way for all scripts?
User avatar
rjbill
Posts: 874
Joined: 13 Jun 2011 06:36

Re: Wrap it! script

Post by rjbill »

I enhanced, renamed, and cleaned up the code to make it more readable and understandable.
Added some SPACE characters and a Cancel button.

Added/Modified: ('_' is a SPACE here)
  • jsphpMultiLineComment /*_selection_*/
    jsphpSingLineComment //_selection
    phpHashComment #_selection
    htmlComment <!--_selection_-->
    phpCode <?php_selection_?>

Code: Select all

function wrapSelection (peEvent)
{
   frmWrapText.Close;

   switch (peEvent.Name) {
      case "doubleQuote": {
         Document.SelText = "\"" + Document.SelText + "\"";
         break;
      }

      case "singleQuote": {
         Document.SelText = "'" + Document.SelText + "'";
         break;
      }

      case "parentheses": {
         Document.SelText = "(" + Document.SelText + ")";
         break;
      }

      case "curlyBraces": {
         Document.SelText = "{" + Document.SelText + "}";
         break;
      }

      case "squareBrackets": {
         Document.SelText = "[" + Document.SelText + "]";
         break;
      }

      case "angleBrackets": {
         Document.SelText = "<" + Document.SelText + ">";
         break;
      }

      case "jsphpMultiLineComment": {
         Document.SelText = "/* " + Document.SelText + " */";
         break;
      }

      case "jsphpSingLineComment": {
         Document.SelText = "// " + Document.SelText;
         break;
      }

      case "phpHashComment": {
         Document.SelText = "# " + Document.SelText;
         break;
      }

      case "htmlComment": {
         Document.SelText = "<!-- " + Document.SelText + " -->";
         break;
      }

      case "phpCode": {
         Document.SelText = "<" + "?php " + Document.SelText + " ?" + ">";
         break;
      }

   } // switch
} // wrapSelection


TForm frmWrapText = new TForm (nil);

frmWrapText.Caption = "Wrap Text";
frmWrapText.BorderStyle = bsDialog;
frmWrapText.Position = poScreenCenter;

var iButtonTopIncrement = 35;
var iButtonTop = 5;
var iButtonLeft = 5;
var iButtonWidth = 115;

frmWrapText.Width = iButtonWidth + 25;

TButton btnWrapWithDoubleQuote = TButton.Create (frmWrapText);
btnWrapWithDoubleQuote.Parent = frmWrapText;
btnWrapWithDoubleQuote.Name = "doubleQuote";
btnWrapWithDoubleQuote.Caption = "\"selection\"";
btnWrapWithDoubleQuote.Width = iButtonWidth;
btnWrapWithDoubleQuote.Top = iButtonTop;
btnWrapWithDoubleQuote.Left = iButtonLeft;
btnWrapWithDoubleQuote.OnClick = "wrapSelection";

TButton btnWrapWithSingleQuote = TButton.Create (frmWrapText);
btnWrapWithSingleQuote.Parent = frmWrapText;
btnWrapWithSingleQuote.Name = "singleQuote";
btnWrapWithSingleQuote.Caption = "'selection'";
btnWrapWithSingleQuote.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement;
btnWrapWithSingleQuote.Top = iButtonTop;
btnWrapWithSingleQuote.Left = iButtonLeft;
btnWrapWithSingleQuote.OnClick = "wrapSelection";

TButton btnWrapWithParentheses = TButton.Create (frmWrapText);
btnWrapWithParentheses.Parent = frmWrapText;
btnWrapWithParentheses.Name = "parentheses";
btnWrapWithParentheses.Caption = "(selection)";
btnWrapWithParentheses.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement;
btnWrapWithParentheses.Top = iButtonTop;
btnWrapWithParentheses.Left = iButtonLeft;
btnWrapWithParentheses.OnClick = "wrapSelection";

TButton btnWrapWithCurlyBraces = TButton.Create (frmWrapText);
btnWrapWithCurlyBraces.Parent = frmWrapText;
btnWrapWithCurlyBraces.Name = "curlyBraces";
btnWrapWithCurlyBraces.Caption = "{selection}";
btnWrapWithCurlyBraces.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement;
btnWrapWithCurlyBraces.Top = iButtonTop;
btnWrapWithCurlyBraces.Left = iButtonLeft;
btnWrapWithCurlyBraces.OnClick = "wrapSelection";

TButton btnWrapWithsquareBrackets = TButton.Create (frmWrapText);
btnWrapWithsquareBrackets.Parent = frmWrapText;
btnWrapWithsquareBrackets.Name = "squareBrackets";
btnWrapWithsquareBrackets.Caption = "[selection]";
btnWrapWithsquareBrackets.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement;
btnWrapWithsquareBrackets.Top = iButtonTop;
btnWrapWithsquareBrackets.Left = iButtonLeft;
btnWrapWithsquareBrackets.OnClick = "wrapSelection";

TButton btnWrapWithangleBrackets = TButton.Create (frmWrapText);
btnWrapWithangleBrackets.Parent = frmWrapText;
btnWrapWithangleBrackets.Name = "angleBrackets";
btnWrapWithangleBrackets.Caption = "<selection>";
btnWrapWithangleBrackets.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement;
btnWrapWithangleBrackets.Top = iButtonTop;
btnWrapWithangleBrackets.Left = iButtonLeft;
btnWrapWithangleBrackets.OnClick = "wrapSelection";

TButton btnWrapWithjsphpMultiLineComment = TButton.Create (frmWrapText);
btnWrapWithjsphpMultiLineComment.Parent = frmWrapText;
btnWrapWithjsphpMultiLineComment.Name = "jsphpMultiLineComment";
btnWrapWithjsphpMultiLineComment.Caption = "/* selection */";
btnWrapWithjsphpMultiLineComment.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement;
btnWrapWithjsphpMultiLineComment.Top = iButtonTop;
btnWrapWithjsphpMultiLineComment.Left = iButtonLeft;
btnWrapWithjsphpMultiLineComment.OnClick = "wrapSelection";

TButton btnWrapWithjsphpSingLineComment = TButton.Create (frmWrapText);
btnWrapWithjsphpSingLineComment.Parent = frmWrapText;
btnWrapWithjsphpSingLineComment.Name = "jsphpSingLineComment";
btnWrapWithjsphpSingLineComment.Caption = "// selection";
btnWrapWithjsphpSingLineComment.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement;
btnWrapWithjsphpSingLineComment.Top = iButtonTop;
btnWrapWithjsphpSingLineComment.Left = iButtonLeft;
btnWrapWithjsphpSingLineComment.OnClick = "wrapSelection";

TButton btnWrapWithphpHashComment = TButton.Create (frmWrapText);
btnWrapWithphpHashComment.Parent = frmWrapText;
btnWrapWithphpHashComment.Name = "phpHashComment";
btnWrapWithphpHashComment.Caption = "# selection";
btnWrapWithphpHashComment.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement;
btnWrapWithphpHashComment.Top = iButtonTop;
btnWrapWithphpHashComment.Left = iButtonLeft;
btnWrapWithphpHashComment.OnClick = "wrapSelection";

TButton btnWrapWithHTMLComment = TButton.Create (frmWrapText);
btnWrapWithHTMLComment.Parent = frmWrapText;
btnWrapWithHTMLComment.Name = "htmlComment";
btnWrapWithHTMLComment.Caption = "<!-- selection -->";
btnWrapWithHTMLComment.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement;
btnWrapWithHTMLComment.Top = iButtonTop;
btnWrapWithHTMLComment.Left = iButtonLeft;
btnWrapWithHTMLComment.OnClick = "wrapSelection";

TButton btnWrapWithphpCode = TButton.Create (frmWrapText);
btnWrapWithphpCode.Parent = frmWrapText;
btnWrapWithphpCode.Name = "phpCode";
btnWrapWithphpCode.Caption = "<" + "?php selection ?" + ">";
btnWrapWithphpCode.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement;
btnWrapWithphpCode.Top = iButtonTop;
btnWrapWithphpCode.Left = iButtonLeft;
btnWrapWithphpCode.OnClick = "wrapSelection";


TButton btnCancel = TButton.Create (frmWrapText);
btnCancel.Parent = frmWrapText;
btnCancel.Name = "btnCancel";
btnCancel.Caption = "Cancel";
btnCancel.Width = iButtonWidth;
iButtonTop = iButtonTop + iButtonTopIncrement + 10;
btnCancel.Top = iButtonTop;
btnCancel.Left = iButtonLeft;
btnCancel.OnClick = "wrapSelection";

frmWrapText.Height = iButtonTop + 65;
frmWrapText.ShowModal;
frmWrapText.Free;
RJTE version 16.15 (actual?) - 64-bit
Win 10 Pro 64-bit 8 GB RAM Intel Core i7-6700 3.40 GHz SCSI Hard Drive 1 TB

Note: The signature is dynamic, not static,
so it may not show the correct version above
that was in use at the time of the post.
Post Reply