Wrap it! script
Posted: 16 Sep 2016 19:15
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.
09/20/2016 edit: added comment wrapping
10/11/2016 edit: minor coding edit, no functionality change
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;
10/11/2016 edit: minor coding edit, no functionality change