字下げ・字上げ

概要 編集

論理行単位で字下げ・字上げします。

インデントには任意の文字列を設定できます。※初期設定は半角空白2個です。

 

変更履歴 編集

  • 1.0.3 (2019-06-01)
    • 字下げ・字上げした次の行に変更行の印がつかないようにした
      • 末尾の改行(次の行頭)を範囲に含めず書き換えるように変更
    • with 文を使用せず変数化した(他の自作マクロに合わせた)
  • 1.0.2 (2019-04-12)
    • (字下げ) Quit() → break ラベル文に変更
  • 1.0.1 (2015-02-14)
    • (字上げ) 「インデントの定義」に正規表現の特殊文字があると誤動作する不具合を修正
    • 矩形選択(始点or終点が左下かつ論理行頭)時、下端の行が範囲から漏れる不具合を修正
  • 1.0.0 (2014-02-09)
    • 初版公開

字下げ (インデント) 編集

  • 複数行選択時 (選択範囲が2行以上) は字下げします。
    単一行選択時 (選択範囲が1行以内) または非選択時はタブ文字を挿入します。
    ※選択範囲の行数はステータスバーの「xx文字 (xx行) 選択」の部分で判断してください。
  • 空行 (改行のみの行) は初期状態では字下げしません。
  • 「Tab」キーなどに割り当ててご利用ください。

ソースコード 編集

//■字下げ(インデント)
// 2014-02-09 - 2019-06-01
//・論理行単位で字下げする。空行は字下げしない。非選択時/単行選択時はタブ挿入。

//■インデントの定義 ●初期値="  "
var INDENT = "  ";

quit: {
var Sel = Document.Selection;
if (Sel.GetTopPointY(mePosView)===Sel.GetBottomPointY(mePosView)) {
  Sel.Text = "\t"; break quit; //非選択時/単行選択時はタブ挿入して終了
}
var ty = Sel.GetTopPointY(mePosLogical);
var by = Sel.GetBottomPointY(mePosLogical);
var bx = Sel.GetBottomPointX(mePosLogical);
var nn =(Sel.Text.match(/\n/g)||[]).length;
if (bx===1 && nn<=by-ty && nn) by--; //末尾改行対策
Sel.SetActivePoint(mePosLogical, 1, by);
Sel.EndOfLine(false, mePosLogical);
Sel.SetAnchorPoint(mePosLogical, 1, ty);
Sel.Text = Sel.Text.replace(/^(?!$)/gm,INDENT);
Sel.SetActivePoint(mePosLogical, 1, by+1);
Sel.SetAnchorPoint(mePosLogical, 1, ty);
}

字上げ (アンインデント) 編集

  • 単一行選択時 (選択範囲が1行以内) または非選択時でも字上げします。
  • 「Shift + Tab」キーなどに割り当ててご利用ください。

ソースコード 編集

//■字上げ(アンインデント)
// 2014-02-09 - 2019-06-01
//・論理行単位で字上げする。非選択時/単行選択時でも字上げする。

//■インデントの定義 ●初期値="  "
var INDENT = "  ";

var Sel = Document.Selection;
var ty = Sel.GetTopPointY(mePosLogical);
var by = Sel.GetBottomPointY(mePosLogical);
var bx = Sel.GetBottomPointX(mePosLogical);
var nn =(Sel.Text.match(/\n/g)||[]).length;
if (bx===1 && nn<=by-ty && nn) by--; //末尾改行対策
Sel.SetActivePoint(mePosLogical, 1, by);
Sel.EndOfLine(false, mePosLogical);
Sel.SetAnchorPoint(mePosLogical, 1, ty);
var pat = "^"+ INDENT.replace(/[$()*+.?\[\\\]^{|}]/g,"\\$&");
Sel.Text = Sel.Text.replace(new RegExp(pat,"gm"),"");
Sel.SetActivePoint(mePosLogical, 1, by+1);
Sel.SetAnchorPoint(mePosLogical, 1, ty);
スポンサーリンク