テキストエディタEmEditorのマクロを作成。
EmEditor上で、開いたテキストを選択した状態から本マクロを使用すると、クリップボードに論理行を付加してコピーされるという仕組み。以下にソースコード&例を示す。
実行前の状態
// 開始・終了位置
var startX = document.selection.GetTopPointX(eePosLogical);
var startY = document.selection.GetTopPointY(eePosLogical);
var endX = document.selection.GetBottomPointX(eePosLogical);
var endY = document.selection.GetBottomPointY(eePosLogical);
// 指定幅、指定文字で桁揃え
Object.prototype.pad = function(width, padChar) {
var strLength = this.toString().length;
if (strLength >= width || padChar == null) return this;
var outputStr = "";
var padLength = width - strLength;
for (var i = 0; i < padLength; i++){
outputStr += padChar;
}
return outputStr + this;
}
// 出力文字列
var outputText = "";
// 初期位置設定
document.selection.SetActivePoint(eePosLogical, startX, startY, true);
// 選択解除
document.selection.Collapse();
// 選択範囲を行ごとに処理
for (var i = startY; i <= endY; i++) {
var left = document.selection.StartOfLine(false, eeLineLogical); // 1行選択範囲の左位置
var right = document.selection.EndOfLine(false, eeLineLogical); // 1行選択範囲の右位置
// 1行選択
document.selection.SelectLine();
// 選択開始行
if (i == startY) {
left = startX;
document.selection.SetAnchorPoint(eePosLogical, left, i);
}
// 選択終了行
if (i == endY) {
right = endX;
document.selection.SetActivePoint(eePosLogical, right, i, true);
// 最終行が空文字の場合処理を終了
if (document.selection.Text.length == 0) break;
}
// 出力テキスト設定
outputText +=
i.pad(PAD_WIDTH, PAD_CHAR) +
SEPARATE_CHAR +
document.selection.Text;
document.selection.Collapse();
}
// クリップボードにコピー
clipboardData.setData("Text", outputText);
実行後のクリップボード中身
1 : // 開始・終了位置
2 : var startX = document.selection.GetTopPointX(eePosLogical);
3 : var startY = document.selection.GetTopPointY(eePosLogical);
4 : var endX = document.selection.GetBottomPointX(eePosLogical);
5 : var endY = document.selection.GetBottomPointY(eePosLogical);
6 :
7 : // 指定幅、指定文字で桁揃え
8 : Object.prototype.pad = function(width, padChar) {
9 : var strLength = this.toString().length;
10 : if (strLength >= width || padChar == null) return this;
11 :
12 : var outputStr = "";
13 : var padLength = width - strLength;
14 : for (var i = 0; i < padLength; i++){
15 : outputStr += padChar;
16 : }
17 :
18 : return outputStr + this;
19 : }
20 :
21 :
22 :
23 : // 出力文字列
24 : var outputText = "";
25 :
26 : // 初期位置設定
27 : document.selection.SetActivePoint(eePosLogical, startX, startY, true);
28 : // 選択解除
29 : document.selection.Collapse();
30 :
31 : // 選択範囲を行ごとに処理
32 : for (var i = startY; i <= endY; i++) {
33 : var left = document.selection.StartOfLine(false, eeLineLogical); // 1行選択範囲の左位置
34 : var right = document.selection.EndOfLine(false, eeLineLogical); // 1行選択範囲の右位置
35 :
36 : // 1行選択
37 : document.selection.SelectLine();
38 :
39 : // 選択開始行
40 : if (i == startY) {
41 : left = startX;
42 : document.selection.SetAnchorPoint(eePosLogical, left, i);
43 : }
44 : // 選択終了行
45 : if (i == endY) {
46 : right = endX;
47 : document.selection.SetActivePoint(eePosLogical, right, i, true);
48 : // 最終行が空文字の場合処理を終了
49 : if (document.selection.Text.length == 0) break;
50 : }
51 :
52 : // 出力テキスト設定
53 : outputText +=
54 : i.pad(PAD_WIDTH, PAD_CHAR) +
55 : SEPARATE_CHAR +
56 : document.selection.Text;
57 :
58 : document.selection.Collapse();
59 : }
60 :
61 : // クリップボードにコピー
62 : clipboardData.setData("Text", outputText);
それだけ。EmEditorの標準機能にあるかと思ってたら、どうにも見あたらなかったので作ってしまった。簡単だと思ってたら、私の技術力では大変に時間がかかりました…。
ちなみに、表示行には未対応。だって使い道が無いんだもの。
なお、
var PAD_WIDTH = 3; // 行番号の最小桁数。 var PAD_CHAR = " "; // 最小桁以下の場合、行番号の先頭を埋める文字。 var SEPARATE_CHAR = " : "; // 行番号と内容の間に入れる文字。
この辺をいじると多少カスタマイズができる。
追記
Number"がNunber"になってた…馬鹿。
Melog: Works > Junk > EmEditor マクロ
こちらに移動。
コメントを残す