summaryrefslogtreecommitdiffstats
path: root/examples/string-rectangle.qs
blob: 50c689ffb47277b9e569d845eff856ba14001e9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
    }
}