Count occurrences matching search_string

, , , , , Sunday, September 21, 2008

I'm sure this command could be improved, but it has served my needs just fine
for years. If you find problems or can suggest improvements, please post them.
Remember I'm not a professional programmer, so please take that into consideration.

dh

// Searches current buffer for search_string specified on the command line
// and returns a
total count of occurrences and a count of the number of lines
// containing the occurrences. Will accept all command line arguments that
// the "search" command accepts, including regular expressions
// in search_string. Can be used on visible lines in a selective display.
// Case insensitive search by default.

_command int cou,count(...) name_info(','VSARG2_REQUIRES_EDITORCTL)
{
int strcount;
int linecount;
_str search_string=arg(1);
parse search_string with delim +1 sstring(delim)opts

save_pos(p);
top(); up();

status=search(sstring, '@i>':+opts);
if (!status) {
strcount=1;
curline=p_line;
linecount=1;
} else {
restore_pos(p);
message get_message(status);
stop;
}

for (;;) {
status=search(sstring, '@i>':+opts);
if (status !=0) {
break
} else {
++strcount;

if (!(curline==p_line)) {
++linecount;
curline=p_line;
}

}
}
restore_pos(p);
message(strcount' occurrence(s) in ' linecount ' lines');
return(strcount);
}

0 comments:

Post a Comment

GlossyBlue Blogger by Black Quanta. Theme & Icons by N.Design Studio
Entries RSS Comments RSS