Showing posts with label line count. Show all posts
Showing posts with label line count. Show all posts

Character, Word and Line Count for Current File

, , , Thursday, March 19, 2009 3 comments

// command wc(): Display on the message line,
// the total numbers of characters, words and
// lines
in the current file.
//
// This ubiquitous procedure
has been done ad
// infinitum by many developers. It's almost
// as common
as Hello World! I don't presume
// to hold any claim to it.
//
// On a large file of several thousand lines
// or more, it can take a few seconds to
// display its results

_command wc() {
cc = 0; // character count
wc = 0; // word count
lc = 0; // line count
item = 0;
_save_pos2(p);
top_of_buffer();
for (;;) {
get_line line;
lc++;
for (i = 1; i < length(line); i++) {
cc++;
if (isalnum(substr(line,i,1))) {
item = 1;
} else if (item && !isalnum(substr(line,i,1))) {
wc++;
item = 0;
}
}
status=down();
if (status == BOTTOM_OF_FILE_RC) {
message("chars: " cc " / words: " wc " / lines: " lc);
break;
}
}
_restore_pos2(p);
}

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

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