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

Show Selective Display Line Statistics

Sunday, September 7, 2008 0 comments

This discussion involves the commands "all", "allnot", "more", and "less". For the sake of conciseness, I'll call these four commands "the selective display commands", even though I know they aren't the only selective display commands. Additionally, this article assumes you're issuing these commands from the Slickedit command line and that you're using Slickedit 2008 or a comparable Slickedit version.

To show selective display statistics on the message line each time you run one of the four selective display commands, you need to insert 8 lines into your seldisp.e file (make a backup copy first). After you've done this, each time you run one of the four commands cited, the Slickedit message line will tell you:

  • the number of lines matched (lines displayed)
  • the number of lines not matched (lines hidden)
  • the total number of lines in the file
Here's how to do it. I'll use the "all" command as the example. Later you can insert the same 8 lines into the "allnot" and "more" source at the same point in the code as you did with "all". ("Less" doesn't need to be edited because it calls on "allnot" to create a selective display.)

Open seldisp.e from your macros folder and locate the start of the "all" command.

_command void all(...)

Scroll down to the following text near the end of the "all" command:

set_find_next_msg("Find", old_search_string, new_search_options);
restore_pos(p);
//return(status)


Insert the 8 new lines below, between the end of the 3 lines I just cited above and the last brace in the "all" command. The result will look something like:

/* these are the existing lines at the end of the "all" code */

set_find_next_msg("Find", old_search_string, new_search_options);
restore_pos(p);
//return(status)

<-------- insert new code here
}

/* after inserting the 8 new lines */

set_find_next_msg("Find", old_search_string, new_search_options);
restore_pos(p);
//return(status)


/* start of 8 new lines to insert */
// start selective display line statistics, added by [you] on [date/time]
If(p_Nofhidden==0)
{ // "String not found" message will be displayed
} else {
int NoflinesShown=0;
NoflinesShown=(p_Noflines) - p_Nofhidden);
message('Displaying ' NoflinesShown' of 'p_Noflines' lines | 'p_Nofhidden' hidden lines');
}
/* end of 8 new lines to insert */

} // final brace in the existing "all" code

Now if you run the command "all /banks/" on the following sample text, two lines will be displayed and five will be hidden, as is usual.

Sample text:
atlantic regional auto dealer financing aviation b-piece buyers
bad credit balance transfer bank mergers banks in sba programs banks
intl trade issuing credit cards offering lines of credit boats
book distributorships banks book publishing borrowing money for real
estate

Execute "all /banks/" on the sample text.

The resulting selective display is:
+ bad credit balance transfer bank mergers banks in sba programs banks
+ book distributorships banks book publishing borrowing money for real

The plus signs are Slickedit's way of telling us that the lines that didn't contain a match for "banks" are hidden or "folded up" into the lines that did match the text.

Now the message line will read:

Displaying 2 of 5 lines | 3 hidden lines

After you modify the original seldisp.e code and you see these stats a few times, you might find them helpful, especially in large files. You can customize the wording and appearance any way you want. For example, you could add a little more code to get the following result:

Selective Display Statistics: Now displaying 3 instances of "banks" in 2 of 5 lines; 3 lines are hidden

Clearly this is a long message and would be more appropriate if displayed in a message box.

By the way, I have two keys mapped to "show_all" and "toggle selective display" (i.e., expand and collapse visible and hidden lines), respectively. I use them constantly. I added the same 8 lines of code to "show_all" and "toggle selective display". After executing "show_all", the message line reads "All lines displayed". This helps me monitor my file size, search terms and contents continuously.

I hope this helps someone.

NOTE: You'll probably lose the changes to seldisp.e each time you install an update to the editor. You'll then need to re-insert the 8 lines of new code each time seldisp.e is updated. To avoid this, you can keep a modified copy of seldisp.e handy and replace the updated one with the modified version containing the 8 added lines. I keep copies of my modified versions of Slickedit modules in a directory called "dh-modified".

Before swapping the files, be sure to diff them so that you don't lose any important changes in the update to seldisp.e. Another way to avoid losing your modifications is to incorporate the code for "all" etc. into your vusrmacs.e file.

As always, I'm not a professional developer and my code might be a hack. Your mileage might vary. Please feel free to share ideas, bugs, suggestions, corrections, etc.

dh

About myslickeditmacros

, , , , , Thursday, September 4, 2008 1 comments

If this is your first time here, please read the following
important background information.


Here you'll find Slick-C macros of all kinds
, from overly simple to fairly complex. I'm a writer/editor/journalist by trade, not a professional developer. Slick-C programmers should find something 0f interest here, including plenty of code that can be tweaked or fixed.

The purpose of this blog is to address the shortage of Slick-C code on the Web, while stimulating discussion and encouraging code sharing.

I've used Slickedit in my daily work since approximately 1998. Currently I have 388 _command macros in my vusrmacs.e file. I'm sure there are other Slickedit users whose macro folders are wishing to burst free and benefit the larger community.

My professional roles are technical writer; editor and researcher of print and electronic publications; desktop publisher
; journalist. My programming experience has generally been limited to high-level scripting languages. I've spent a lot of time using numerous text editors and macro languages. Of the two kinds of Slickedit users named in the subtitle of this blog, I'm a Wordsmith rather than a Code Maven.

This blog is not meant to replace Slickedit Documentation, Slickedit Community Forums, "Hello World:" The Slickedit Developer Blog or other Slickedit-related sites. There's plenty of good stuff in those places. I'll post a listing of a bunch of good resources soon.

Many of "my" macros--perhaps a third--borrow from, or are based on, code examples or fragments that I've run across in the past 10 years. This includes lots of Slick-C code and ideas plucked from the guts of Slickedit's code modules themselves. And of course, I've borrowed liberally from third party sources.

If I've failed to credit the authors of any of the code on this blog, please contact me. The same goes for any code that duplicates functionality already built into Slickedit. Please call deprecated procedures to my attention. I will gladly remove, change or credit code in response to reasonable requests.

Again, most macros on this site are relatively simple. Some are simply shortcuts for frequently used commands, regular expressions or tedious command-line typing. I hope you find some of them useful.

Wordsmiths and Code Mavens, grab a Jolt and let's get started. Let's all take full advantage of what is probably the best, most complete and most full-featured text editor ever created.


Here's a simple macro I use frequently. It quickly counts the number of paragraphs in the current buffer and displays the result on the command line. This is helpful because I often work with documents containing hundreds of thousands of lines and thousands of blocks of text. If you wish to change the parameters for recognizing a paragraph, you can modify the #defines at the top of the macro.

// count_paragraphs: count number of paragraphs in buffer
_command coupar,count_paragraphs() name_info(','VSARG2_REQUIRES_EDITORCTLVSARG2_READ_ONLY)
{
#define PARAGRAPH_SKIP_CHARS ' \t '
#define PARAGRAPH_SEP_RE ('(^['PARAGRAPH_SKIP_CHARS']*$)')
#define SKIP_PARAGRAPH_SEP_RE ('^~(['PARAGRAPH_SKIP_CHARS']*$)')

if (p_Noflines==0) {
message('Empty file');
stop;
} else {
para_count=1;
}

_save_pos2(p);
top();up();

for (;;) {
_begin_line;
/* skip paragraph separator lines */
status=search(SKIP_PARAGRAPH_SEP_RE,'r');
if ((status==BOTTOM_OF_FILE_RC) (status==STRING_NOT_FOUND_RC)) {

get_line(line);
if (line=='') {
para_count = para_count - 1;
}

message(para_count ' paragraphs');
break;
} else {
/* Search for paragraph separator line. */
status=search(PARAGRAPH_SEP_RE,'r');
if ((status==BOTTOM_OF_FILE_RC) (status==STRING_NOT_FOUND_RC)) {
message(para_count ' paragraphs');
break;
}
++para_count;
}
}
_restore_pos2(p);
}


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