From b41e8b4ba6dde7824dd5b637c7ceb81920f54a7b Mon Sep 17 00:00:00 2001 From: "Jesper K. Pedersen" Date: Thu, 18 Apr 2013 07:56:34 +0200 Subject: started an examples subdirectory of useful scripts Change-Id: Ie2db7a8a45fa09d7b41ce93ff09dce8c5bd15374 Reviewed-by: Nicolas Arnaud-Cormos --- examples/string-rectangle.qs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 examples/string-rectangle.qs diff --git a/examples/string-rectangle.qs b/examples/string-rectangle.qs new file mode 100644 index 0000000..50c689f --- /dev/null +++ b/examples/string-rectangle.qs @@ -0,0 +1,37 @@ +// This script is similar to emacs' string-rectangle. +// As an example, select three lines of "var" below, run the script, +// and type "Variable", and see how the "var"'s are replaced with "Variable" + +var text = dialogs.getText("Rectangular Insert", "String to insert") +var editor = editors.current() + +var anchorPos = editor.position(PositionOperation.Anchor) +var anchorColumn = editor.convertPosition(anchorPos).x +var anchorLine = editor.convertPosition(anchorPos).y + +var pointPos = editor.position(PositionOperation.Current) +var pointColumn = editor.convertPosition(pointPos).x +var pointLine = editor.convertPosition(pointPos).y + +var startColumn, endColumn, startLine, endLine +if ( anchorLine < pointLine || (anchorLine == pointLine && anchorColumn < pointColumn)) { + startColumn = anchorColumn + endColumn = pointColumn + startLine = anchorLine + endLine = pointLine +} +else { + startColumn = pointColumn + endColumn = anchorColumn + startLine = pointLine + endLine = anchorLine +} + +if ( startLine != -1 || startColumn != -1) { + var line = startLine + while ( line <= endLine ) { + editor.gotoLine(line,startColumn) + editor.replace(endColumn-startColumn, text) + line = line +1 + } +} -- cgit v1.2.3