Show Longest Line and Lines > or < Specified Column Number
Tuesday, September 9, 2008I guess you could say I write alot of "utility" macros. Short and sweet, but they get the job done. Here are three that come in handy once in a rare while.
// Find longest line in buffer; display line number and length
// It would be good if the macro took you to the longest line.
// I tried a couple of things that didn't work.
// Maybe you can provide the missing code.
_command longest_line() name_info(','READ_ONLY_ARG2)
{
save_pos(p);
top();up();max=0;
while (!down()) {
get_line(line);
len=length(line);
if (len > max) {
max=len;
max_linenum=p_line;
}
}
message('Longest line is ' max_linenum ' with ' max ' bytes');
restore_pos(p);
}
// The following two macros could be modified to display 'greater
// than or equal to" or "less than or equal to" the specified
// column number, if that is more to your liking.
// Show only lines exceeding the column number you specify.
// This command currently provides a selective display, but doesn't
// show the usual "+" and "-" marks that you expect in a
// selective display, to enable you to expand and compress the lines.
// Usage: show_lines_over 128. This will display only lines longer than 128 columns
_command show_lines_over(...) name_info(','VSARG2_REQUIRES_EDITORCTLVSARG2_READ_ONLY)
{
typeless maxlength=arg(1);
if (arg(1)=='' arg(1)<=0) {
message('Error: a column number argument is required');
stop;
}
top();up();
for (;;) {
status=down();
get_line(line);
if (length(line)<=maxlength) {
_lineflags(HIDDEN_LF,HIDDEN_LF);
}
if (status) {
break;
}
}
}
// show lines shorter than specified column length; hide lines longer than that length
_command show_lines_under(...) name_info(','VSARG2_REQUIRES_EDITORCTLVSARG2_READ_ONLY)
{
typeless minlength=arg(1);
if (arg(1)=='' arg(1)<=0) {
message('Error: a column number argument is required');
stop;
}
top();up();
for (;;) {
status=down();
get_line(line);
if (length(line)>minlength) {
_lineflags(HIDDEN_LF,HIDDEN_LF);
}
if (status) {
break;
}
}
}
dh
September 23, 2008 at 3:52 AM
Dave,
Try setting the property p_line to max_linenum at the end, to move to the longest line found. It works for me.
Here's the code, not sure how it will format in here:
_command longest_line() name_info(','READ_ONLY_ARG2)
{
typeless p;
save_pos(p);
top();
up();
int max = 0;
int max_linenum = -1;
while (!down()) {
_str line;
get_line(line);
int len = length(line);
if (len > max) {
max = len;
max_linenum = p_line;
}
}
// message('Longest line is ' max_linenum ' with ' max ' bytes');
restore_pos(p);
if (max_linenum >= 0) {
p_line = max_linenum;
}
}