文字列のセンタリング
Posted feedbacks - 秀丸マクロ
文字列として取得して整形。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | begingroupundo;
if ( getconfig( "AutoAdjustOrikaeshi" ) == 2 ) {
#width = windowwidth;
} else {
#width = getconfig( "Orikaeshi" );
}
if ( !selecting ) {
selectall;
} else {
#l1 = seltoplineno;
#l2 = selendlineno;
movetolineno 1, #l1;
beginsel;
movetolineno 1, #l2;
movetolineno linelen2 + 1, #l2;
endsel;
}
$text = gettext( seltopx, seltopy, selendx, selendy, 1 );
delete;
call center $text, #width;
insert $$return;
endgroupundo;
endmacro;
center:
$$result = "";
while( $$1 != "" ) {
##lineLen = strstr( $$1, "\x0d\x0a" );
##isLastLine = ##lineLen == -1;
if ( ##isLastLine ) {
##lineLen = strlen( $$1 );
}
$$line = leftstr( $$1, ##lineLen );
call center_line $$line, ##2;
$$result = $$result + $$return;
$$1 = rightstr( $$1, strlen( $$1 ) - ##lineLen );
if ( !##isLastLine ) {
$$1 = rightstr( $$1, strlen( $$1 ) - strlen( "\x0d\x0a" ) );
$$result = $$result + "\n";
}
}
return $$result;
center_line:
##margin = ##2 - strlen( $$1 );
if ( ##margin >= 0 ) {
$$result = "";
##i = ##margin / 2;
while( ##i > 0 ) {
$$result = $$result + " ";
##i = ##i - 1;
}
$$result = $$result + $$1;
} else {
##idx = -##margin / 2;
call midstr2, $$1, ##idx, ##2;
$$result = $$return;
// ±1バイトとる範囲をずらして、その中から一番長いテキストをとる
if ( strlen( $$result ) < ##2 ) {
call midstr2, $$1, ##idx + 1, ##2;
if ( strlen( $$return ) > strlen( $$result ) ) {
$$result = $$return;
}
if ( strlen( $$result ) < ##2 ) {
call midstr2, $$1, ##idx - 1, ##2;
if ( strlen( $$return ) > strlen( $$result ) ) {
$$result = $$return;
}
}
}
}
return $$result;
midstr2: // マルチバイトを考慮した midstr
$$src = $$1;
##idx = ##2;
##len = ##3;
if ( ##idx < 0 ) {
##idx = 0;
}
if ( ##idx > strlen( $$src ) ) {
##idx = strlen( $$src );
}
if ( ##len < 0 ) {
##len = 0;
}
if ( ##len > strlen( $$src ) - ##idx ) {
##len = strlen( $$src ) - ##idx ;
}
##end = ##idx + ##len;
##i = 0;
$$dest = "";
while( ##i < ##end ) {
##char = ascii( rightstr( $$src, strlen( $$src ) - ##i ) );
##byte = strlen( char( ##char ) );
if ( ( ##i >= ##idx ) && ( ##i + ##byte <= ##end ) ) {
$$dest = $$dest + char( ##char );
}
##i = ##i + ##byte;
}
return $$dest;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | begingroupundo;
if ( getconfig( "AutoAdjustOrikaeshi" ) == 2 ) {
#width = windowwidth;
} else {
#width = getconfig( "Orikaeshi" );
}
if ( !selecting ) {
#top = 1;
#end = linecount2;
} else {
#top = seltoplineno;
#end = selendlineno;
escape;
}
#i = #top;
while( #i <= #end ) {
movetolineno 1, #i;
#margin = #width - linelen2;
if ( #margin >= 0 ) {
#j = #margin / 2;
$margin = "";
while( #j > 0 ) {
$margin = $margin + " ";
#j = #j - 1;
}
insert $margin;
} else {
beginsel;
movetolineno linelen2 + 1, #i;
$text = gettext( seltopx, seltopy, selendx, selendy, 1 );
delete;
call cutstr, $text, #width;
insert $$return;
}
#i = #i + 1;
}
endgroupundo;
endmacro;
cutstr:
$$src = $$1;
##width = ##2;
if ( ##width < 0 ) {
##width = 0;
}
if ( ##width > strlen( $$src ) ) {
##width = strlen( $$src );
}
##start = strlen( $$src ) - ##width;
##end = strlen( $$src ) + ##width;
##i = 0;
$$dest = "";
$$left = "";
$$right = "";
while( ##i * 2 < ##end ) {
##char = ascii( $$src );
##bytes = strlen( char( ##char ) );
if ( ##i * 2 < ##start ) {
$$left = char( ##char );
}
if ( ( ( ##i + ##bytes ) * 2 > ##end ) && ( $$right == "" ) ) {
$$right = char( ##char );
}
if ( ( ##i * 2 >= ##start ) && ( ( ##i + ##bytes ) * 2 <= ##end ) ) {
$$dest = $$dest + char( ##char );
}
$$src = rightstr( $$src, strlen( $$src ) - ##bytes );
##i = ##i + ##bytes;
}
if ( $$right == "" ) {
$$right = char( ascii( $$src ) );
}
if ( strlen( $$dest ) >= ##width ) {
return $$dest;
}
if ( ( strlen( $$left + $$dest ) <= ##width ) && ( $$left != "" ) ) {
return $$left + $$dest;
}
if ( ( strlen( $$dest + $$right ) <= ##width ) && ( $$right != "" ) ) {
return $$dest + $$right;
}
return $$dest;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | if ( getconfig( "AutoAdjustOrikaeshi" ) == 2 ) {
#width = windowwidth;
} else {
#width = getconfig( "Orikaeshi" );
}
disabledraw;
begingroupundo;
if ( !selecting ) {
selectall;
}
#i = seltoplineno;
#end = selendlineno;
escape;
while( #i <= #end ) {
movetolineno 1, #i;
#margin = #width - linelen2;
if ( #margin >= 0 ) {
#j = #margin / 2;
while( #j > 0 ) {
insert " ";
#j = #j - 1;
}
} else {
while( linelen2 > #width ) {
golineend2;
backspace;
if ( !linelen2 > #width ) {
break;
}
golinetop2
delete;
}
}
#i = #i + 1;
}
endgroupundo;
enabledraw;
|



nobsun
#4089()
Rating1/3=0.33
文字列を指定のカラム幅にセンタリング配置する関数を示してください。文字列の長さが指定した幅より長い場合には文字列の両端をできるだけ均等に切り落して指定幅に収めてください。1文字は1カラムに収まるものと仮定してかまいません。
[ reply ]