Showkey

Wednesday, September 24, 2008 0 comments

I've used one version or another of the following macro, Showkey, in the macro languages of most of the different editors I've used over the years. Prior to Slickedit my editor was Kedit for a long time. My love of Kedit stemmed from my enjoyment of using Xedit on the IBM mainframe VM/CMS system.

If you asked me now, "What's the usefulness of this Showkey macro?" I don't know that I could remember. I remember that it came in very handy at times. Execute showkey and it will report the exact key or key combination you press. To stop showing your keypresses, press the ESC key.

One example of a use that I had for the showkey macro was to test keystrokes and key combos to make sure the computer understood the keyboard input the same way I thought I was issuing it. For instance, I've had several keyboards that, for some reason, didn't "get" the Ctrl keypress from the Ctrl key on the right side of my keyboard when used in certain key combinations. The Ctrl key on the left side of the keyboard worked fine.

By trying key combinations with the right Ctrl key while showkey was active, I could see that the computer didn't register the right Ctrl key when pressed in conjunction with certain other keys.

Showkey doesn't work on the Print Screen, Pause/Break, or modifier keys (Shift, Ctrl, Alt, or Windows keys) when they are pressed alone. I hope you find a good way to use this macro one day.

// show which key was pressed
_command showkey()
{
message('Reading keystrokes to message line; press ESC to stop');
for (;;) {
binary_key=get_event();
key_name=event2name(binary_key);
if (key_name == 'ESC') {
message('Keypress ' key_name'; reading keystrokes ended');
break;
} else {
message('Keypress: 'key_name);
//return(key_name);
}
}
}
}

Count occurrences matching search_string

, , , , , Sunday, September 21, 2008 0 comments

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);
}

Delall: Delete Visible Lines from a Selective Display

Thursday, September 18, 2008 0 comments

Here are 3 more Slickedit macros I've been using regularly for several years. Actually, the first one is more of a test macro than it is useful. The other two are stalwarts.

The 3 macros are:

  1. count_hidden_lines(): show count of hidden lines
  2. dat(): delete all visible lines in the selective display, without deleting any hidden lines
  3. delall(): delete all lines containing the specified search string
Have fun. Let me know if you find any significant problems or have comments.

dh
// count_hidden_lines

_command count_hidden_lines()
{
NoflinesShown0=((p_Noflines)-p_Nofhidden);
message( NoflinesShown0' visible (selected) lines, p_Noflines= 'p_Noflines'; p_Nofhidden = 'p_Nofhidden);
}


// dat(): delete all visible lines in selective display
// Example: to remove all lines containing "Bookmark this site",
// execute 'all /bookmark this site/' and then execute 'dat.'
// The 'dat' command will delete all of the visible lines and leave the
// rest untouched.

_command dat() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
// get line counts for verification and be sure a selective display is active
NoflinesShown0=((p_Noflines)-p_Nofhidden);
if (NoflinesShown0==p_Noflines) {
_message_box("Error: be sure selective display is active before running ''dat''");
return(0);
}
old_line_cou=p_Noflines;
// make sure nothing is marked--we don't want to delete invisible lines
if (select_active()) {
_message_box('Error: Deselect the marked block');
return(0);
} else {
bottom();
while (p_line>=1) {
hid=_lineflags() & (HIDDEN_LF);
if (hid!=HIDDEN_LF) {
// probably also need to exclude or prompt for

// deletion of pbm "minus" lines. A pbm minus
// line is a hidden line that has been expanded
status=_delete_line();
} else {
cursor_up();
}
}
}
NoflinesShown1=((p_Noflines)-p_Nofhidden);
message(old_line_cou'-'old_line_cou-p_Noflines' lines deleted; 'p_Noflines' remain');
if (p_Noflines!=(old_line_cou-NoflinesShown0)) {
_message_box('Error: Number of lines deleted does not match original number of visible lines');
}
show_all();
}


// delall(): Delete all lines containing the specified search string.
//
Locates lines containing matches and then runs 'dat' to delete
// all matching lines in one step.

// Example: "delall /^Subtotal$/r"
// Delall is a timesaver ... and easily reversible, thanks to Undo.
// You can use any arguments
to 'delall' that you can use with the
// ordinary 'search', 'find', 'all', 'more', or 'less'
commands,
// including those luscious regular expressions we love so much.


_command d,delall(...) name_info(','VSARG2_REQUIRES_EDITORCTLVSARG2_READ_ONLY)
{
_str searchstr=arg(1);
_str delim;
_str sstring;
_str opts;
// strip delimiters
parse searchstr with delim +1 sstring(delim)opts
save_pos(p);
all(sstring,'i':+opts);
dat();
show_all();
restore_pos(p);
}

Show Longest Line and Lines > or < Specified Column Number

Tuesday, September 9, 2008 1 comments

I 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

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