diff options
Diffstat (limited to 'examples/string-rectangle.qs')
-rw-r--r-- | examples/string-rectangle.qs | 37 |
1 files changed, 37 insertions, 0 deletions
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 + } +} |