1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

秀丸で編集中のMarkdown表の縦棒「|」をそろえる為に、スペースを挿入する秀丸マクロ

Last updated at Posted at 2018-06-21

作ったきっかけ

タイトルのままです。
秀丸でMarkdownの表を編集中、「|」をそろえたくなりました。

秀丸マクロ用ファイル(***.mac)作成時の注意点

  • Shift-JISで保存。

制作状況

  • 文字列分割のサブルーチンSplit:を拝借させてもらいました。
  • 秀丸マクロってクセが強いですね。
  • 2018年6月22日現在、なんとか動く程度です。

秀丸マクロの本文

gofiletop;	//カーソルをファイル先頭に移動
# lcnt=1;		//行番号変数を「1」行目にセット

while (#lcnt<linecount2+1) {
	golineend;		//カーソルを行の最後に移動
	#end_num = x;//その行の半角文字数を取得

	$seltext[#lcnt] = gettext(0,y,#end_num,y);//カーソルのある行の文字列を選択
	call Split $seltext[#lcnt], "|";//選択した行の文字列を「|」で分割する。

	if(#max_width <= ##return){
		#max_width = ##return;
	}

	//message str(#lcnt) + "行目は "+ str(##return) + "列あり。最大列は" +str(#max_width);

	#i =0;//表の「列」数の初期値
	while ( #i <= ##return) {
		$seltext[#lcnt][#i] = $ReSplit[#i];

		if(#max_strlen[#i] < strlen($ReSplit[#i])){
			#max_strlen[#i] = strlen($ReSplit[#i]);
		}
		#i = #i + 1;
	}
	
	down;//カーソルを次の行に移動(down=下げる)する。
	#lcnt=#lcnt+1;
}

//*****************************************************************

# lcnt=1;		//改めて、行番号変数を「1」行目にセットし直し
while (#lcnt<linecount2+1) {
	#i =0;
	while ( #i <= 15) {
		#j =0;
		$spaces ="";
		while ( #j <= #max_strlen[#i]-strlen($seltext[#lcnt][#i])-1){
			$spaces = $spaces + " ";
			#j = #j + 1;
		}
		$seltext[#lcnt][#i] = $seltext[#lcnt][#i] + $spaces;
		$result_matrix = $result_matrix + $seltext[#lcnt][#i] +"|";

		#i = #i + 1;
	}
	
	$result_matrix = $result_matrix + "\n";
	#lcnt=#lcnt+1;
}
	//message $result_matrix;

	gofileend;	//カーソルをファイルの最後に移動
	insert "\n\n" + "★↓↓↓↓↓出力結果↓↓↓↓↓★\n" + $result_matrix;

endmacro;//以下サブルーチンの為、本文のマクロを終了

//文字列分割のサブルーチン*****************************************************************
Split:
    while ( ##i < strlen($$1) ) {
        if ( midstr($$1, ##i, 1) == $$2 ) {
            ##cnt = ##cnt + 1;
            $ReSplit[##cnt -1] = midstr($$1, ##start, ##i - ##start);
            ##start = ##i + 1;
        }
        ##i = ##i + 1;
    }
    $ReSplit[##cnt] = midstr($$1, ##start);
    return ##cnt;```

秀丸マクロの適用デモ

Qiitaでは文字幅はそろってないですが、画面上では一応そろってます。

もしもしかめよ|かめさんよ
せかいの|うちで
お|まえ|

★↓↓↓↓↓出力結果↓↓↓↓↓★
もしもしかめよ|かめさんよ|||||||||||||||
せかいの      |うちで    |||||||||||||||
お            |まえ      |||||||||||||||

画面キャプチャー

image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?