組み込みメソッド
組み込みメソッドは ViViScript があらかじめ用意しているメソッドです。
メソッドとは特定のオブジェクトのみが実行できる関数のようなものです。たとえば writeln(文字列) は、
console オブジェクのメソッドで文字列をコンソールに表示します。このメソッドは console に対してのみ有効で、
他のオブジェクトに対し実行することは出来ません。
console.writeln("hello, world"); // これはOK
var foo;
foo.writeln("hello, world"); // foo は console でないのでNG
注意:以下の説明で、"Method of" とあるのは、
そのメソッドがどのオブジェクトまたはクラスのメソッドであるかを示すものです。
■ charAt(index)
文字列の index で示される位置の文字を取得します。指定位置がダブルバイト文字の一部である場合、
ダブルバイト文字1文字が値として返されます。
Method of
文字列
サンプルプログラム:
var str = "abcあい";
for(var ix=0;ix<str.length();++ix)
console.writeln(str.charAt(ix));
実行結果:
a
b
c
あ
あ
い
い
■ charCode(index)
文字列の index で示される位置の文字のSJISコードを取得します。
文字コードはMS漢字(SJIS)によるものです。
Method of
文字列
サンプルプログラム:
var str = "abcあい";
for(var ix=0;ix<str.length();++ix)
console.writeln(str.charCode(ix));
実行結果:
97
98
99
130
160
130
162
■ charType(index)
文字列の index で示される位置の文字コードが1バイト文字か2バイト文字かを判別します。
return:
0 1バイト文字(または this が文字列で無い)
1 2バイト文字の1バイト目
2 2バイト文字の2バイト目
Method of
文字列
サンプルプログラム:
var str = "abcあい";
for(var ix=0;ix<str.length();++ix)
console.writeln(str.charType(ix));
実行結果:
0
0
0
1
2
1
2
■ clear()
コンソールウィンドウに表示されているすべての文字列をを消去します。
Method of
console
サンプルプログラム:
console.clear();
■ clearSelected()
選択モードを解除します。
Method of ビュー
サンプルプログラム:
■ close()
コンソールウィンドウをクローズします。
Method of
console
サンプルプログラム:
console.open();
var regexp = conple.input();
console.close();
■ compare( string )
文字列のコード順による比較を行います。
return:
−1 this のほうが引数の string より小さい場合
0 this と引数の string が等しい場合
1 this のほうが引数の string より大きい場合
Method of
文字列
サンプルプログラム:
var str1 = "はっしー";
console.writeln(str1.compare("じゅんこ"));
console.writeln(str1.compare("はっしー"));
console.writeln(str1.compare("まゆみ"));
実行結果:
1
0
-1
■ copy()
選択範囲をクリップボードへ転送する。
Method of
ビュー
■ curLeft( cnt )
■ curRight( cnt )
■ curUp( cnt )
■ curDown( cnt )
カーソルを移動します。引数で移動量を指定します。省略した場合は1です。
Method of
ビュー
■ cut()
選択範囲をカットし、クリップボードへ転送する。
Method of
ビュー
■ find( string )
■ find( code )
文字列または文字コードを検索し、そのインッデクスを返します。マッチしない場合は−1を返します。
Method of
文字列
サンプルプログラム:
var tmp = "abcあい";
console.writeln(tmp.find("b"));
console.writeln(tmp.find("あ"));
console.writeln(tmp.find("は"));
console.writeln(tmp.find(0x63));
実行結果:
1
3
-1
2
■ findOneOf( string )
文字列から string に含まれる文字を検索し、そのインッデクスを返します。マッチしない場合は−1を返します。
Method of
文字列
サンプルプログラム:
var tmp = "abc123xyz";
console.writeln(tmp.findOneOf("0123456789"));
console.writeln(tmp.findOneOf("xyz"));
実行結果:
3
6
■ getCount()
オブジェクトのメンバ数を返します。
Method of
任意のオブジェクト
サンプルプログラム:
var obj;
console.writeln(obj.getCount());
obj.foo = 1;
obj.bar = "うふふ";
console.writeln(obj.getCount());
実行結果:
0
2
■ getCount()
アウトラインビューのアイテム数を返します。アウトライン解析が行われていない場合は0を返します。
Method of
アウトラインビュー
■ getCursorPos()
現在のカーソル位置を返します。リターンオブジェクトの line, offset プロパティの値が、
行数、オフセットを示します。
Method of
ビュー
サンプルプログラム:
現在のカーソル位置をステータスバーに表示します。
var pos = thisView.getCursorPos();
var tmp = "(" + pos.line + ", " + pos.offset + ")";
statusBar.write(tmp);
■ getDay()
日(1〜31)を返します。
Method of
日付・時刻
■ getHour()
時(0〜23)を返します。
Method of
日付・時刻
■ getLineCount()
ドキュメントが保持するテキストの(論理)行数を返します。
Method of
ドキュメント
サンプルプログラム:
var doc = openDocument("vi.cpp"); // vi.cpp をオープン
var lineCount = doc.getLineCount(); // 行数を取得
console.writeln(lineCount); // 行数を表示
doc.close(); // クローズ
実行結果:
3055
■ getLineString( line )
ドキュメントが保持するテキストの line 行目の文字列を返します。行番号は0オリジンです。
Method of
ドキュメント
サンプルプログラム:
var doc = openDocument("doc2.vvs");
var cnt = doc.getLineCount();
for(var line = cnt - 1; line >= 0; --line) // 行を逆順に表示
console.writeln(format("%6d: ", line + 1) + doc.getLineString(line));
doc.close();
実行結果:
6:
5: doc.close();
4: console.writeln(format("%6d: ", line + 1) + doc.getLineString(line));
3: for(var line = cnt - 1; line >= 0; --line)
2: var cnt = doc.getLineCount();
1: var doc = openDocument("doc2.vvs");
■ getLineString( line )
ビューに対応するドキュメントが保持するテキストの line 行目(表示行番号による)の文字列を返します。
行番号は0オリジンです。
Method of
ビュー
サンプルプログラム:
var pos = thisView.getCursorPos(); // カーソル位置を取得
var string = thisView.getLineString( pos.line); // カーソル行の文字列を取得
■ getMinute()
分(0〜59)を返します。
Method of
日付・時刻
■ getMonth()
月(1〜12)を返します。
Method of
日付・時刻
■ getOutlineView()
ドキュメントに関連付けられているアウトラインビューを取得します。
Method of
ビュー
■ getPathName()
ドキュメントのフルパス名を返します。
Method of
ドキュメント
■ getRootItem()
アウトラインのルートアイテムを返します。
Method of
アウトラインビュー
■ getSecond()
秒(0〜59)を返します。
Method of
日付・時刻
■ getSelectedRange()
ビューの選択範囲を返します。
範囲はオブジェクトで返され、line1, offset1 メンバに始点位置、line2, offset2 メンバに終点位置が入ります。
Method of
ビュー
■ getVSplitiew()
ビューが分割されている場合は、もう一方のビューを返します。
Method of
ビュー
■ getTitle()
ドキュメントのタイトル文字列を返します。
Method of
ドキュメント
■ getView()
ドキュメントに関連付けられている最初のビューを取得します。
Method of
ドキュメント
サンプルプログラム:
"test.txt" というファイルをオープンし、最初に "test" という行を挿入します。
var doc = openDocument("test.txt"); // ドキュメントをオープンする
var view = doc.getView(); // そのビューを取得する
view.insertText("test\n"); // 文字列を挿入する
■ getYear()
年号(西暦)を返します。
Method of
日付・時刻
■ input()
コンソールにプロンプト("> ")を表示し、1行入力を行います。
Method of
console
サンプルプログラム:
var tmp = console.input();
console.writeln(tmp);
実行結果:
> aaa
aaa
■ insertText( string )
ビューの現在のカーソル位置に文字列を挿入します。
Method of
ビュー
サンプルプログラム:
■ isalpha( index )
ANSIの英字かどうかを判定します。
Method of
文字列
■ iscsym( index )
ANSIの英字、アンダーバー、数字かどうかを判定します。
Method of
文字列
■ iscsymf( index )
ANSIの英字、アンダーバーかどうかを判定します。
Method of
文字列
■ isdigit( index )
ANSIの数字かどうかを判定します。
Method of
文字列
■ isExpanded( item )
アイテムが展開されているかどうかを判定します
Method of
アウトラインビュー
■ isKindOf( kind )
変数の種別を判定します。引数の値は以下のとおりです。
1 整数
2 文字列
3 オブジェクト
4 関数
5 組み込み関数
6 ビュー
7 ドキュメント
8 日付・時刻
9 アウトラインビュー
Method of
任意のオブジェクト
サンプルプログラム:
function printType(v) // 引数のタイプを表示する関数
{
if( v.isKindOf(1) )
console.writeln("整数");
else if( v.isKindOf(2) )
console.writeln("文字列");
else if( v.isKindOf(3) )
console.writeln("オブジェクト");
else if( v.isKindOf(4) )
console.writeln("関数");
else if( v.isKindOf(5) )
console.writeln("組み込み関数");
else if( v.isKindOf(6) )
console.writeln("ビュー");
else if( v.isKindOf(7) )
console.writeln("ドキュメント");
else if( v.isKindOf(8) )
console.writeln("日付・時刻");
else
console.writeln("???");
}
function main()
{
var a;
printType(a);
a = 1;
printType(a);
a = "foo";
printType(a);
a.prop = 0;
printType(a);
}
実行結果:
???
整数
文字列
オブジェクト
■ isMatched()
以前に実行した検索がマッチしたかどうかを返します。
Method of
ビュー
サンプルプログラム:
// 正規表現にマッチする行を削除するスクリプト
console.open();
console.writeln("正規表現を入力してください。");
var regexp = console.input();
console.close();
if( regexp != "" ) {
thisView.viCommand("/" + regexp + "\n");
while( thisView.isMatched() ) {
thisView.viCommand("dd");
thisView.viCommand("n");
}
}
■ isMember( string )
プロパティ名がオブジェクトのメンバかどうかを判定します。
Method of
任意のオブジェクト
サンプルプログラム:
var obj;
obj.foo = 1;
obj.bar = "うふふ";
console.writeln(obj.isMember("boo")); // これは FALSE になる
console.writeln(obj.isMember("bar")); // これは TRUE になる
実行結果:
0
1
■ layoutRetToLogicalRet()
選択範囲のレイアウト改行を本物の改行に変換します。
Method of
ビュー
サンプルプログラム:
setSelect();
layoutRetToLogicalRet();
■ layoutedToLogical(lineNumber)
レイアウト行番号(0オリジン)を論理行番号(0オリジン)に変換します。
Method of
ビュー
■ left( n )
文字列の先頭から n バイトの部分文字列を返します。
ダブルバイト文字の途中の場合は、その文字も含めます。
Method of
文字列
サンプルプログラム:
var tmp = "abcあい";
console.writeln(tmp.left(3));
console.writeln(tmp.left(4));
console.writeln(tmp.left(5));
実行結果:
abc
abcあ
abcあ
■ length()
文字列長(バイト数)を返します。
Method of
文字列
サンプルプログラム:
var tmp = "abcAbc";
var len = tmp.length(); // tmp の文字列長を取得
console.writeln(len);
実行結果:
6
■ logicalToLayouted(lineNumber)
論理行番号(0オリジン)をレイアウト行番号(0オリジン)に変換します。
Method of
ビュー
■ mid(index, n)
文字列の index バイト目から n バイトの部分文字列を返します。
第2引数を省略した場合、index バイト目以降の文字列を取得します。
ダブルバイト文字の途中の場合は、その文字も含めます。
substring とは引数の意味が異なるので注意してください。
Method of
文字列
サンプルプログラム:
var tmp = "abcあい";
console.writeln(tmp.mid(1));
console.writeln(tmp.mid(3));
console.writeln(tmp.mid(1, 4));
console.writeln(tmp.mid(1, 5));
実行結果:
bcあい
あい
bcあ
bcあい
■ open()
コンソール(アウトプットウィンドウ)がオープンされていない場合は、オープンします。
Method of
コンソール
サンプルプログラム:
console.open();
var regexp = conple.input();
console.close();
実行結果:
■ paste()
クリップボードのテキストをカーソル位置に挿入する。
Method of
ビュー
■ regexpFind( regexp )
正規表現による検索を行い、そのインッデクスを返します。マッチしない場合は−1を返します。
Method of
文字列
サンプルプログラム:
var tmp = "abc123";
var tmp2 = tmp + " ";
console.writeln(tmp.regexpFind("[0-9]+"));
console.writeln(tmp.regexpFind("^[0-9]+"));
console.writeln(tmp.regexpFind("[0-9]+$"));
console.writeln(tmp2.regexpFind("[0-9]+$"));
実行結果:
3
-1
3
-1
■ repeat( n )
文字列をn回繰り返します。
Method of
文字列
サンプルプログラム:
for(var i=0;i<10;++i)
console.writeln("vi".repeat(i));
実行結果:
vi
vivi
vivivi
vivivivi
vivivivivi
vivivivivivi
vivivivivivivi
vivivivivivivivi
vivivivivivivivivi
■ right( n )
文字列の末尾から n バイトの部分文字列を返します。
ダブルバイト文字の途中の場合は、その文字も含めます。
Method of
文字列
サンプルプログラム:
var tmp = "abcあい";
console.writeln(tmp.right(3));
console.writeln(tmp.right(4));
console.writeln(tmp.right(5));
実行結果:
あい
あい
cあい
■ strftime( format )
日付・時刻オブジェクトの値を書式にしたがって文字列に変換します。
以下に書式指定コードを示します。
|