Revisiting the Longest_line Macro

, Saturday, February 7, 2009 0 comments

If anyone can tell me how to prevent the macro text
from truncating on the right, please drop me a line.
I know I can insert line breaks manually, but there
must be a better way.


Thank you, fellow Slick editors John Hurst and Mark
Robbins, for providing useful suggestions and
revisions to the
longest_line macro.

Below are (1) the original version of the macro
(called longest_line0),
(2) John's improved version
(longest_line1), and (3) Mark's expanded and
enhanced
version (longest_line2).

This macro still needs the ability to
identify and
report on multiple lines matching the length of the
first of the longest lines found.
The macro could
further be easily modified to show the document's
longest lines in a selective display.

Anywho, I'll let you decide the differences in each
of these three macros. Maybe you'll find a good use
for them.

// Find longest line in buffer; display line number and length

_command longest_line0,longln() 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);
}

// with modifications by John Hurst
_command longest_line1() 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;
}
}

// modified and enhanced by Mark Robbins
_command longest_line2() name_info(','READ_ONLY_ARG2)
{
save_pos(p);
top();up();max=0;
while (!down()) {
get_line(lin);
len=length(lin);
if (len > max) {
max=len;
max_linenum=p_line;
}
}
//message('Longest line is ' max_linenum ' with ' max ' bytes');
s='Line 'max_linenum ' is longest with ' max ' bytes';
rv=(_message_box(s,'Goto Line', MB_DEFBUTTON2|MB_YESNO|MB_ICONQUESTION)==IDYES);
if (rv==1) {
goto_line(max_linenum);
}else{
restore_pos(p);
}
}

/*
And here is a simplified msg box:

Usage:
rv=msg_box_ask(‘Question?’,‘yN’); // default btn is No, returns 1 if yes, 0 if no
rv=msg_box_ask(‘Question?’,‘ynC’); // default btn is Cancel, returns 1 if yes, 2 if no, 3 if cancel
rv=msg_box_ask(‘Question?’,‘yN’,’h’); // default btn is No, Icon is hand, returns 1 if yes, 0 if no
rv=msg_box_ask(‘Question?’,‘yN’,’h’,’title’,’’); // default btn is No, Icon is hand, returns ‘yes’ if yes, ‘no’ if no
very tricky now….

rv=msg_box_ask(‘Question?’,‘yN’,’h’,’title’,’yes’); // default btn is No, Icon is hand, returns true if yes, else false
*/

int msg_box(_str string, _str title="Visual SlickEdit", int mb_flags=MB_OK|MB_ICONEXCLAMATION)
{
return _message_box(string,title,mb_flags);
}


typeless msg_box_ask(_str string, _str buttons='', _str icon='', _str title="Visual SlickEdit",typeless retcode=0)
{
//buttons
// o,ok,'';c,oc;a,ari,i;y,n,yn;ync;r,rc
// icons
// h,q,e,i,s,n
but=0;
b=lowcase(buttons);
if (b==''||b=='o') {but=MB_OK;}
if (b=='oc'||b=='c') {but=MB_OKCANCEL;}
if (b=='ari'||b=='a'||b=='i') {but=MB_ABORTRETRYIGNORE;}
if (b=='y'||b=='n'||b=='yn') {but=MB_YESNO;}
if (b=='ync') {but=MB_YESNOCANCEL;}
if (b=='r'||b=='rc') {but=MB_RETRYCANCEL;}
b=buttons;
d=0;
if (pos('C',b)==2) {d=MB_DEFBUTTON2;}
if (pos('C',b)==3) {d=MB_DEFBUTTON3;}
if (pos('R',b)==2) {d=MB_DEFBUTTON2;}
if (pos('R',b)==1) {d=MB_DEFBUTTON1;}
if (pos('I',b)==3||pos('I',b)==1) {d=MB_DEFBUTTON3;}
if (pos('N',b)==2) {d=MB_DEFBUTTON2;}
i=MB_ICONNONE;
// h,q,e,i,s,n
if (icon=='n') {i=MB_ICONNONE;}
if (icon=='h') {i=MB_ICONHAND;}
if (icon=='q') {i=MB_ICONQUESTION;}
if (icon=='e') {i=MB_ICONEXCLAMATION;}
if (icon=='i') {i=MB_ICONINFORMATION;}
if (icon=='s') {i=MB_ICONSTOP;}
if (icon=='n') {i=MB_ICONNONE;}
if (icon=='') {
if (but==MB_OK) {i=MB_ICONNONE;}
if (but==MB_OKCANCEL) {i=MB_ICONQUESTION;}
if (but==MB_ABORTRETRYIGNORE) {i=MB_ICONEXCLAMATION;}
if (but==MB_YESNO) {i=MB_ICONQUESTION;}
if (but==MB_YESNOCANCEL) {i=MB_ICONQUESTION;}
if (but==MB_RETRYCANCEL) {i=MB_ICONSTOP;}
}
v=msg_box(string,title,but|d|i);
if (retcode==0&&retcode._varformat()==VF_INT) {
if (but==MB_OK) {
if (v==IDOK) {return 1;}else{return 0;}
}
if (but==MB_OKCANCEL) {
if (v==IDOK) {return 1;}else{return 0;}
}
if (but==MB_ABORTRETRYIGNORE) {
if (v==IDABORT) {return 1;}
if (v==IDRETRY) {return 2;}
if (v==IDIGNORE) {return 3;}
}
if (but==MB_YESNO) {
if (v==IDYES) {return 1;}else{return 0;}
}
if (but==MB_YESNOCANCEL) {
if (v==IDYES) {return 1;}
if (v==IDNO) {return 2;}
if (v==IDCANCEL) {return 3;}
}
if (but==MB_RETRYCANCEL) {
if (v==IDRETRY) {return 1;}else{return 0;}
}
return(0);
}
if (retcode==1&&retcode._varformat()==VF_INT) {return v;}

if (retcode._varformat()==VF_LSTR) {
if (retcode=='') {
if (but==MB_OK) {
if (v==IDOK) {return 'ok';}
}
if (but==MB_OKCANCEL) {
if (v==IDOK) {return 'ok';}else{return 'cancel';}
}
if (but==MB_ABORTRETRYIGNORE) {
if (v==IDABORT) {return 'abort';}
if (v==IDRETRY) {return 'retry';}
if (v==IDIGNORE) {return 'ignore';}
}
if (but==MB_YESNO) {
if (v==IDYES) {return 'yes';}else{return 'no';}
}
if (but==MB_YESNOCANCEL) {
if (v==IDYES) {return 'yes';}
if (v==IDNO) {return 'no';}
if (v==IDCANCEL) {return 'cancel';}
}
if (but==MB_RETRYCANCEL) {
if (v==IDRETRY) {return 'retry'}else{return 'cancel';}
}
return'fail';
}else{
r=retcode;

if (but==MB_OK) {
if (v==IDOK) {return ('ok'==r);}
}
if (but==MB_OKCANCEL) {
if (v==IDOK) {return ('ok'==r);}else{return ('cancel'==r);}
}
if (but==MB_ABORTRETRYIGNORE) {
if (v==IDABORT) {return ('abort'==r);}
if (v==IDRETRY) {return ('retry'==r);}
if (v==IDIGNORE) {return ('ignore'==r);}
}
if (but==MB_YESNO) {
if (v==IDYES) {return ('yes'==r);}else{return ('no'==r);}
}
if (but==MB_YESNOCANCEL) {
if (v==IDYES) {return ('yes'==r);}
if (v==IDNO) {return ('no'==r);}
if (v==IDCANCEL) {return ('cancel'==r);}
}
if (but==MB_RETRYCANCEL) {
if (v==IDRETRY) {return ('retry'==r)}else{return ('cancel'==r);}
}
return false;
}
}
return(-1);
//return (msg_box(string,title,MB_YESNO|MB_DEFBUTTON2|mb_flags2)==IDYES);
}


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