ひらがな/カタカナ変換
ナビゲーションに移動
検索に移動
ひらがな/カタカナ トグル変換
実行するごとに「ひらがな ⇔ カタカナ」の相互変換をします。
- ダウンロード >> 「ファイル:ひらがな/カタカナ変換.zip」(アイコン入り)
#title = "ひらがな/カタカナ変換"
#tooltip = "ひらがな/全角カタカナ のトグル変換"
#icon = "文字_あア[1].ico"
// #icon = "Mery用 マテリアルデザインっぽいアイコン.icl", 324
// ---------- ▼ 設定項目 ▼ ----------
// ■ 選択範囲がないとき
var autoSelection = 1;
/**
* 0: なにもしない
* 1: カーソル位置の単語を自動選択
* 2: カーソル位置の行全体(表示行)を自動選択
* 3: カーソル位置の行全体(論理行)を自動選択
* 4: 文書全体を自動選択
*/
// ---------- ▲ 設定項目 ▲ ----------
var d = editor.ActiveDocument, s = d.selection;
var isEmpty = s.IsEmpty;
if ( d.ReadOnly ) {
Status = " このドキュメントは書き換え禁止です。";
}
else if ( isEmpty && autoSelection == 0 ) {
Status = " 選択範囲がありません。";
}
else {
Redraw = false;
var sx = ScrollX, sy = ScrollY;
var ay = s.GetActivePointY( mePosLogical );
var pos = ( isEmpty ) ? s.GetActivePos() : -1;
// 非選択時には自動選択
if ( isEmpty ) {
if ( autoSelection == 1 ) {
s.SelectWord();
if ( s.GetActivePointY( mePosLogical ) != ay ) {
s.SetActivePos( pos );
}
}
else if ( autoSelection == 2 || autoSelection == 3 ) {
var posMode = autoSelection - 1;
s.SetAnchorPoint( posMode, 1, s.GetActivePointY ( posMode ) );
s.EndOfLine( true, posMode );
}
else if ( autoSelection == 4 ) { s.SelectAll(); }
}
var st = s.Text;
if ( st ) {
var anc = s.GetAnchorPos(), act = s.GetActivePos();
var tp = Math.min( anc, act );
var tmp;
// ひらがな → 全角カタカナ変換 「ゔ ゕ ゖ → ヴ ヵ ヶ」含む
tmp = st.replace( /う[゛゙u\3099]/g, "ヴ" )
.replace( /[\u3041-\u3096\u309D\u309E]/g, function( chr ) {
return String.fromCharCode( chr.charCodeAt( 0 ) + 0x60 );
} );
// ※ unicode 文字に対応できるときは「ヷ ヸ ヹ ヺ」に変換
if ( d.Encoding >= 65000 ) {
tmp = tmp.replace( /ワ[゛゙u\3099]/g, "ヷ" ).replace( /ヰ[゛゙u\3099]/g, "ヸ" )
.replace( /ヱ[゛゙u\3099]/g, "ヹ" ).replace( /ヲ[゛゙u\3099]/g, "ヺ" );
}
if ( st == tmp ) {
// 全角カタカナ → ひらがな変換
// ※ 「ヷ ヸ ヹ ヺ」は「かな+濁点」に分解する
tmp = st.replace( /\u30F7/g, "わ゛" ).replace( /\u30F8/g, "ゐ゛" )
.replace( /\u30F9/g, "ゑ゛" ).replace( /\u30FA/g, "を゛" )
.replace( /[\u30A1-\u30F3\u30FD\u30FE]/g, function( chr ) {
return String.fromCharCode( chr.charCodeAt( 0 ) - 0x60 );
} );
// ※ unicode 文字に対応できるときは「ヴ ヵ ヶ → ゔ ゕ ゖ」に変換
if ( d.Encoding >= 65000 ) {
tmp = tmp.replace( /[\u30F4-\u30F6]/g, function( chr ) {
return String.fromCharCode( chr.charCodeAt( 0 ) - 0x60 );
} );
}
// ※ Shift_JIS などでは「ヴ ヵ ヶ → う゛ ヵ ヶ」
else {
tmp = tmp.replace( /\u30F4/g, "う゛" );
}
}
if ( st != tmp ) {
// ブックマークを保存
var bmArray = [];
s.SetActivePoint( mePosLogical, 1, 2 );
if ( s.PreviousBookmark() ) {
bmArray.push( s.GetActivePointY( mePosLogical ) );
}
s.SetActivePos( 0 );
while ( s.NextBookmark() ) {
bmArray.push( s.GetActivePointY( mePosLogical ) );
}
s.SetActivePos( anc ); s.SetActivePos( act, true );
ScrollX = sx; ScrollY = sy;
// 選択範囲を変換
s.Text = tmp;
act = s.GetActivePos();
// ブックマークを復元
var bmCount = bmArray.length;
if ( bmCount ) {
editor.ExecuteCommandByID( 2129 ); // ブックマークをクリア
for ( var i = 0; i < bmCount; i ++ ) {
s.SetActivePoint( mePosLogical, 1, bmArray[i], false );
editor.ExecuteCommandByID( 2126 ); // ブックマークを設定/解除(設定)
}
}
s.SetActivePos( tp ); s.SetActivePos( act, true );
}
else {
Status = " ひらがな/カタカナ がありません。";
if ( pos >= 0 ) { s.SetActivePos( pos ); }
}
}
ScrollX = sx; ScrollY = sy;
Redraw = true;
}
以下、参照 >> 『テキスト整形』マクロ
ひらがな → カタカナ変換
#title = "ひらがな → カタカナ変換"
// e.g. 「あいうえお」 → 「アイウエオ」
var s = document.selection;
if( s.Text ) {
s.Text = s.Text.replace( /[\u3041-\u3096\u309D\u309E]/g, function( t ) {
return String.fromCharCode( t.charCodeAt( 0 ) + 0x60 )
});
}
カタカナ → ひらがな変換
#title = "全角カタカナ → ひらがな変換"
// e.g. 「アイウエオ」 → 「あいうえお」
var s = document.selection;
if( s.Text ) {
s.Text = s.Text.replace( /[\u30A1-\u30F3\u30FD\u30FE]/g, function( t ) {
return String.fromCharCode( t.charCodeAt( 0 ) - 0x60 )
});
}
ひらがな/カタカナ マルチカーソル対応版 (トグル変換未対応) by Kuro
Mery Ver 3.0.0 のマルチカーソルに対応する例として sukemaru さんのソースコードをもとにカスタマイズしてみました。
変換処理の部分は手を加えずメソッド化しただけです。
マクロからマルチカーソルの位置を取得することはできるのですが、マルチカーソルを設置する機能がないので複数選択の状態を復元することができないためトグル変換に対応できていません。
複数の選択範囲に対して変換処理を行うときに、ひらがな・カタカナ変換のように行数が変化しないものはこのようにシンプルに書けますが、変換前と変換後で行数が変わる場合は複数選択の位置を再計算してやる必要があるのでもう少し大変だと思います。
#title = "ひらがな/カタカナ変換"
#tooltip = "ひらがな/全角カタカナ のトグル変換"
#icon = "文字_あア[1].ico"
// #icon = "Mery用 マテリアルデザインっぽいアイコン.icl", 324
// ---------- ▼ 設定項目 ▼ ----------
// ■ 選択範囲がないとき
var autoSelection = 1;
/**
* 0: なにもしない
* 1: カーソル位置の単語を自動選択
* 2: カーソル位置の行全体(表示行)を自動選択
* 3: カーソル位置の行全体(論理行)を自動選択
* 4: 文書全体を自動選択
*/
// ---------- ▲ 設定項目 ▲ ----------
var d = editor.ActiveDocument, s = d.selection;
var isEmpty = s.IsEmpty;
if ( d.ReadOnly ) {
Status = " このドキュメントは書き換え禁止です。";
}
else if ( isEmpty && autoSelection == 0 ) {
Status = " 選択範囲がありません。";
}
else {
Redraw = false;
var sx = ScrollX, sy = ScrollY;
// 以下、変更した箇所
// ---------------------------------ここから---------------------------------
// もともとここにあった処理は下部の Convert 関数にそのまま移動
// 複数選択を保持しておくための配列 (SetActivePoint を使うと複数選択が解除されるため、事前に選択箇所を格納しておく必要がある)
var selections = [];
// マルチカーソルの場合、document.selection.Count は 0 より大きい値が返る
if (document.selection.Count > 0) {
for (var i = 0; i < document.selection.Count; i++) {
// GetTopPointX、GetTopPointY、GetBottomPointX、GetBottomPointY のみ、第二引数に 0 以上の値を指定すれば複数選択の箇所が取得できる
selections.push({
tx: s.GetTopPointX( mePosLogical, i ),
ty: s.GetTopPointY( mePosLogical, i ),
bx: s.GetBottomPointX( mePosLogical, i ),
by: s.GetBottomPointY( mePosLogical, i )
});
}
// マルチカーソルではない場合
} else {
// GetTopPointX、GetTopPointY、GetBottomPointX、GetBottomPointY は第二引数を省略するか -1 を指定すれば現在のカーソル位置が取得できる
selections.push({
tx: s.GetTopPointX( mePosLogical ),
ty: s.GetTopPointY( mePosLogical ),
bx: s.GetBottomPointX( mePosLogical ),
by: s.GetBottomPointY( mePosLogical )
});
}
BeginUndoGroup();
try {
for (var i = 0; i < selections.length; i++) {
// 複数選択の箇所に現在のカーソルを設定する (選択範囲の開始と終了が逆になることがあるけど気にしない方向で…)
s.SetActivePoint( mePosLogical, selections[i].bx, selections[i].by, false );
s.SetActivePoint( mePosLogical, selections[i].tx, selections[i].ty, true );
Convert(s.IsEmpty, s.GetActivePointY( mePosLogical ), ( isEmpty ) ? s.GetActivePos() : -1);
}
} finally {
EndUndoGroup();
}
// 今のところマクロから複数選択を設定する仕組みがないので複数の選択範囲を復元することはできません
// ---------------------------------ここまで---------------------------------
ScrollX = sx; ScrollY = sy;
Redraw = true;
}
// Convert メソッドの中身はもとのソースから変更なし
// isEmpty、ay、pos は選択範囲に応じて更新する必要があるため引数で渡す
function Convert(isEmpty, ay, pos) {
// 非選択時には自動選択
if ( isEmpty ) {
if ( autoSelection == 1 ) {
s.SelectWord();
if ( s.GetActivePointY( mePosLogical ) != ay ) {
s.SetActivePos( pos );
}
}
else if ( autoSelection == 2 || autoSelection == 3 ) {
var posMode = autoSelection - 1;
s.SetAnchorPoint( posMode, 1, s.GetActivePointY ( posMode ) );
s.EndOfLine( true, posMode );
}
else if ( autoSelection == 4 ) { s.SelectAll(); }
}
var st = s.Text;
if ( st ) {
var anc = s.GetAnchorPos(), act = s.GetActivePos();
var tp = Math.min( anc, act );
var tmp;
// ひらがな → 全角カタカナ変換 「ゔ ゕ ゖ → ヴ ヵ ヶ」含む
tmp = st.replace( /う[゛゙u\3099]/g, "ヴ" )
.replace( /[\u3041-\u3096\u309D\u309E]/g, function( chr ) {
return String.fromCharCode( chr.charCodeAt( 0 ) + 0x60 );
} );
// ※ unicode 文字に対応できるときは「ヷ ヸ ヹ ヺ」に変換
if ( d.Encoding >= 65000 ) {
tmp = tmp.replace( /ワ[゛゙u\3099]/g, "ヷ" ).replace( /ヰ[゛゙u\3099]/g, "ヸ" )
.replace( /ヱ[゛゙u\3099]/g, "ヹ" ).replace( /ヲ[゛゙u\3099]/g, "ヺ" );
}
if ( st == tmp ) {
// 全角カタカナ → ひらがな変換
// ※ 「ヷ ヸ ヹ ヺ」は「かな+濁点」に分解する
tmp = st.replace( /\u30F7/g, "わ゛" ).replace( /\u30F8/g, "ゐ゛" )
.replace( /\u30F9/g, "ゑ゛" ).replace( /\u30FA/g, "を゛" )
.replace( /[\u30A1-\u30F3\u30FD\u30FE]/g, function( chr ) {
return String.fromCharCode( chr.charCodeAt( 0 ) - 0x60 );
} );
// ※ unicode 文字に対応できるときは「ヴ ヵ ヶ → ゔ ゕ ゖ」に変換
if ( d.Encoding >= 65000 ) {
tmp = tmp.replace( /[\u30F4-\u30F6]/g, function( chr ) {
return String.fromCharCode( chr.charCodeAt( 0 ) - 0x60 );
} );
}
// ※ Shift_JIS などでは「ヴ ヵ ヶ → う゛ ヵ ヶ」
else {
tmp = tmp.replace( /\u30F4/g, "う゛" );
}
}
if ( st != tmp ) {
// ブックマークを保存
var bmArray = [];
s.SetActivePoint( mePosLogical, 1, 2 );
if ( s.PreviousBookmark() ) {
bmArray.push( s.GetActivePointY( mePosLogical ) );
}
s.SetActivePos( 0 );
while ( s.NextBookmark() ) {
bmArray.push( s.GetActivePointY( mePosLogical ) );
}
s.SetActivePos( anc ); s.SetActivePos( act, true );
ScrollX = sx; ScrollY = sy;
// 選択範囲を変換
s.Text = tmp;
act = s.GetActivePos();
// ブックマークを復元
var bmCount = bmArray.length;
if ( bmCount ) {
editor.ExecuteCommandByID( 2129 ); // ブックマークをクリア
for ( var i = 0; i < bmCount; i ++ ) {
s.SetActivePoint( mePosLogical, 1, bmArray[i], false );
editor.ExecuteCommandByID( 2126 ); // ブックマークを設定/解除(設定)
}
}
s.SetActivePos( tp ); s.SetActivePos( act, true );
}
else {
Status = " ひらがな/カタカナ がありません。";
if ( pos >= 0 ) { s.SetActivePos( pos ); }
}
}
}
スポンサーリンク