カーソル行と、上の行を行入れ替え
- ページ: Macro/投稿
- 作者: sai
- 作者: sai, jj
- カテゴリー: js
- 投稿日: 2014-02-11 (火) 06:40:00
メッセージ
カーソルのある行と、上の行を入れ替えます。
キー割り当てはVC++に合わせ Alt+Shift+T とか。
ところで、行入れ替え後のカーソル位置はどこがよいでしょう?
Meadow(Emacs)の transpose-lines (C-x C-t)ではカーソル行の下の行。
VC++6.0ではカーソル行。
このマクロはカーソル行の上の行だったりします。
0
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
| |
(function(){
var s = Editor.ExpandParameter("${R?r$:$:$}"); if (s == "r") return;
var y = parseInt( Editor.ExpandParameter("$y") ); if (y <= 1) return; if (y >= Editor.GetLineCount(0)) {
s = Editor.GetLineStr(y);
}
Editor.CancelMode();
var cnt = 0;
if (s.match(/[^\r\n]$/)) { var newLine = "\r\n";
var lineCode = Editor.GetLineCode();
if (lineCode == 0) {
newLine = "\r\n";
} else if (lineCode == 1) {
newLine = "\r";
} else if (lineCode == 2) {
newLine = "\n";
}
s = Editor.GetLineStr( y ) + newLine + Editor.GetLineStr( y - 1 ).replace(/[\r\n]/g, "");
cnt--;
Editor.GoLineTop(9);
Editor.Up();
Editor.GoLineTop(9);
Editor.BeginSelect();
while ( parseInt( Editor.ExpandParameter("$y") ) <= y ) {
cnt++;
Editor.GoLineEnd();
Editor.Down();
if ( parseInt( Editor.ExpandParameter("$y") ) == y ) {
cnt++;
Editor.GoLineEnd();
break;
}
}
} else {
s = Editor.GetLineStr( y ) + Editor.GetLineStr( y - 1 );
Editor.GoLineTop(9);
Editor.Up();
Editor.GoLineTop(9);
Editor.BeginSelect();
while ( parseInt( Editor.ExpandParameter("$y") ) <= y ) {
cnt++;
Editor.Down();
}
}
Editor.InsText( s );
for (var i = 0; i < cnt; i++) {
Editor.Up();
Editor.GoLineTop(9);
}
Editor.ReDraw();
})();
|
0
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
| | // (Tab=8)
// [概要]
// カーソルのある行と、上の行を入れ替えます。
// 行入れ替え後のカーソル位置は、上の行の先頭に移動します。
// (emacsの transpose-lines (C-x C-t)、
// VCの LineTranspose (Alt+Shift+T)の様な機能です。)
// [変更履歴]
// 2009/04/17 範囲選択中なら取り消し。
// 2004/08/24 新規
var
y : Integer;
s : String;
begin
s := S_ExpandParameter('${R?r$:$:$}'); // '${R?読み取り専用$:上書き禁止$:いずれでもない$}'
if s = 'r' then Exit; // 読み取り専用
//y := S_ExpandParameter('$y'); // (これでもOKだったが)
y := StrToInt( S_ExpandParameter('$y') ); // カーソル行(改行単位)
if y <= 1 then Exit; // カーソル行が、1行目
if y >= S_GetLineCount(0) then begin
s := S_GetLineStr(y);
if (Pos(#13, s) = 0) and (Pos(#10, s) = 0) then Exit;
// カーソル行に、改行が無い。(EOF行)
end;
S_CancelMode(); // 範囲選択中なら取り消し。
// 入れ替えた文字列
s := S_GetLineStr( y ) + S_GetLineStr( y - 1 );
// 上の行(改行単位)へ
S_GoLineTop(9);
S_Up();
S_GoLineTop(9);
// 上の行から、2行選択
S_BeginSelect();
while( StrToInt(S_ExpandParameter('$y')) <= y ) do begin
S_Down();
end;
// 入れ替えた文字列を挿入
S_InsText( s );
// 上の行(改行単位)へ
S_Up();
S_GoLineTop(9);
S_Up();
S_GoLineTop(9);
// S_InsText() の後は、
// 再描画しないと更新状態にならない。([ファイル名(更新)] のようにならない。)
// カーソル位置も更新されない。
S_ReDraw();
end;
|