Count Lines Traversed

, , , , , , , , , , , Tuesday, March 17, 2009 0 comments

// Using the cursor-up or cursor-down keys,
// display on the message line the total number
// of lines the cursor has moved.


// Assign
a key combination to the line_counter
// command. Press the key combo to start the counter.
// It initializes with a value of 0. Press ESC to stop.
//
// Move your cursor up or down to reach your target.
// The target is typically a certain number of lines you
// want to traverse, or some specific text you want to
// reach. With each cursor-up/down keystroke the displayed
// count will decrement or increment by 1.
Moving the
// cursor up
will decrement the number of lines;
// moving the cursor down increments the line count.
// Change this if you want.
//
// This macro can actually be valuable once in a while.
// But I must plead ignorant for now; I've forgotten what I
// use it for!

_command line_counter() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
lines=0;
message('Number of lines traversed='lines)

for (;;) {
key = get_event();
keyname=event2name(key);

if (keyname=='DOWN') {
cursor_down();
++lines;
}

if (keyname=='UP') {
cursor_up();
--lines;
}
message('Linecount='lines)

if (keyname=='ESC') {
message('Linectr cancelled')
break;
}
}
}

I repeat myself when under stress, I repeat myself when under stress . . .

, , , , , , Thursday, March 12, 2009 0 comments

// repeat_n [how many times] [command name]
// Repeats a command the number of times specified.

// Args are "n command", where n is the number of times
// to execute the command, and command is the name of
// the command to run.
//
// Usage example: repeat_n 15 cursor_down
// This will execute the cursor-down command 15 times, with the
// expected results.

// Source: Professional Slickedit by John Hurst
// Copyright 2007 Wiley Publishing Inc, Indianapolis, Indiana, USA.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0

_command void repeat_n(_str args = null) \\
name_info(','VSARG2_EDITORCTL)
{
if (args == null) {
message("Specify number of times and " \\
"command to run."
);
return;
}
_str n;
_str command;
parse args with n command;
if (n <= 0 || command == null) {
message("Specify number of times and " \\
"command to run."
);
return;
}
int i;
for (i = 0; i < n; i++) {
execute(command);
}
}

// repeat_n_last_macro [how many times]
// Repeats the last recorded macro the number of times specified
// Usage example: repeat_n_last_macro 25
// Executes the last recorded macro 25 times

//
Source: Professional Slickedit by John Hurst
// Copyright 2007 Wiley Publishing Inc, Indianapolis, Indiana, USA.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0

_command void repeat_n_last_macro(_str args = null) \\
name_info
(','VSARG2_EDITORCTL)
{
repeat_n(args :+ " last_macro");
}


Circuitous Logic

, , , , , 0 comments

// (1) Get and set your margin settings at desired increments.
// Assign this macro to a key combination.
// Stick your favorite margin presets into the macro code.
// I like to use 1 80 1, 1 128 1, 1 256 1, 1 512 1 and 1 1024 1.

// The same "cycling" method can be used on almost any setting.
// For example, you can easily modify this macro to take a text
// selection and cycle the text through all caps, initial caps,
// all lower case, and so on.

// Currently the macro will automatically increment to the next
// setting each time the key combo is pressed (including the
// first press). If the margins are currently 1 256 1, executing
// the command will increment the setting to 1 512 1. If 512
// is not the setting you want, just press the key combo again
// to set your desired value.
//

// (2) Cycle through display modes: hex, line hex, normal edit.
//


_command cycle_right_margin() name_info(','VSARG2_REQUIRES_EDITORCTL, VSARG2_READ_ONLY)
{
parse p_margins with left_ma right_ma new_para_ma;
// message("Current margin settings "left_ma' 'right_ma' 'new_para_ma);
switch (right_ma) {
case 80:
p_margins=left_ma' '128' 'new_para_ma;
message('Margins 'p_margins);
break;
case 128:
p_margins=left_ma' '256' 'new_para_ma;
message('Margins 'p_margins);
break;
case 256:
p_margins=left_ma' '512' 'new_para_ma;
message('Margins 'p_margins);
break;
case 512:
p_margins=left_ma' '1024' 'new_para_ma;
message('Margins 'p_margins);
break;
case 1024:
p_margins=left_ma' '80' 'new_para_ma;
message('Margins 'p_margins);
break;
default:
p_margins=left_ma' '80' 'new_para_ma;
message('Margins 'p_margins);
break;
}
}


// (2) Cycle through display modes: hex, line hex, normal edit.
// From Thom Little & Associates, www.tlanet.net


#define TLA_DISPLAY_EDIT 0
#define TLA_DISPLAY_HEX 1
#define TLA_DISPLAY_LINE_HEX 2

_command void tlaViewCycle( ) name_info( ',' VSARG2_ICON | VSARG2_REQUIRES_MDI_EDITORCTL )
{
switch ( p_hex_mode )
{
case TLA_DISPLAY_EDIT :
p_hex_mode = TLA_DISPLAY_HEX ;
break ;
case TLA_DISPLAY_HEX :
p_hex_mode = TLA_DISPLAY_LINE_HEX ;
break ;
case TLA_DISPLAY_LINE_HEX :
p_hex_mode = TLA_DISPLAY_EDIT ;
break ;
default :
tlaError( "tlaViewCycle", "Unknown view state" );
break ;
}
return ;
}

Change current file's file extension

Wednesday, March 11, 2009 0 comments

// change fext or prompt for fully qualified buffer name
_command fe, fext() name_info(','VSARG2_EDITORCTL|VSARG2_CMDLINE)
{
fid=p_buf_name;
new_fext=arg(1);
if (new_fext=='') {
name();
return(0);
}
buf_nofext=strip_filename(fid,'E');
new_fid=buf_nofext:+'.':+new_fext;
status=name(new_fid);
if (status) {
message('Fext was not changed');
return(0);
} else {
save_as(new_fid);
return(1);
}
}

Remove "no-save" and "current line" lineflags

Friday, February 27, 2009 0 comments

The purpose of this macro is to remove certain 
lineflags placed in the text manually be the user.
These particular lineflags were explored in "Fun
with Lineflags." That post can be found elsewhere
in this blog.

The use of the "no-save"(NLF) and "current line"
(CLF) lineflags were bastardizations, in that the
flags were not meant to used in ways I showed.
One thing you can do with these lineflags is to
use them to tag special lines, much like using a
bookmark. It doesn't have any great uses.

When flagged with either of these lineflags, small
icons will be displayed in the left margin of the
selected lines. You can also use the lineflags to
show a background color of you choice for each
flagged line. This can help to distinguish the
specially lineflagged lines from other selective
display lines. You can create selective displays
using color backgrounds of your choice for the
selected lines to display all lines with those
particular bits.

I have some simple macros named "all_nl" and
"all_clf" and others created to be used for having
fun with lineflags. At the bottom of this post is
a tiny macro for toggling a lineflag on a line.
In this case it deals with "CLFs" only.

// turn NLF & CLF OFF for ALL lines

_command untag_all_nlfclf()
{
if (p_mode_name=='Fileman') {
deselect_all();
} else {
save_pos(p)
top();up();
_lineflags(0,CURLINEBITMAP_LF);
_lineflags(0,NOSAVE_LF);
for (;;) {
if (down()) break;
_lineflags(0,CURLINEBITMAP_LF);
_lineflags(0,NOSAVE_LF);
}
restore_pos(p);
}
}

------------------------

// toggle CLF for current line
_command clf_toggle()
{
if (!(_lineflags() & CURLINEBITMAP_LF)) {
_lineflags(CURLINEBITMAP_LF,CURLINEBITMAP_LF);
} else {
_lineflags(0,CURLINEBITMAP_LF);
}
down();
}



arg_show_lines (...)

, , , , , , , , , Sunday, February 22, 2009 0 comments

// command arg_show_lines(...)

// In a selective display, show a range of lines by line number
// The main goal here is to open a range of hidden lines to view.
// If the range contains already-displayed lines, they'll be
// displayed along with the newly displayed hidden lines
//
// Usage: arg_show_lines 352 396
//
// Note: if a line with a "minus bitmap" is displayed before
// executing this macro, the minus bitmap will be removed; this
// makes some sense because the macro is meant to remove all
// minus and plusbitmap lines as it unhides all lines in the
// selected range
//
_command void arg_show_lines(...) name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
parse arg(1) with first_line last_line // get line numbers from the
p_line=first_line; // command-line arguments
up();
if (p_line<=1) { // check the lineflags in the p_line=0; // range, get seldisp level new_level=0; } else { new_level=_lineflags()&LEVEL_LF; if (!up()) { new_level=_lineflags()&LEVEL_LF; down(); } } for (;;) { // go through all lines in the if (p_line>last_line) break; // specified range and bust
level=(_lineflags() & LEVEL_LF); // them out of hidden state
_lineflags(new_level,HIDDEN_LF|LEVEL_LF|PLUSBITMAP_LF|MINUSBITMAP_LF);
status=down();
if (status) {
break;
}
if (p_line>last_line) {
break;
}
}
p_line=last_line;
}

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