ひらがな/カタカナ変換

提供: MeryWiki
2020年1月3日 (金) 09:29時点におけるSukemaru (トーク | 投稿記録)による版
ナビゲーションに移動 検索に移動

ひらがな/カタカナ トグル変換

実行するごとに「ひらがな ⇔ カタカナ」の相互変換をします。

ダウンロード >> 「ファイル:ひらがな/カタカナ変換.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 )
  });
}
スポンサーリンク