Count Paragraphs

, , , , , , , , , Tuesday, December 30, 2008 0 comments

COUNT PARAGRAPHS in a document

// count number of paragraphs in buffer
// FYI: the macro counts the first paragraph properly
// regardless of whether it is proceeded by a blank line
// at the top of file

// count number of paragraphs in buffer or range

_command coupar,count_paragraphs() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_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);
}

Childlike in Its Simplicity

, , , Sunday, December 21, 2008 0 comments

Over many years, I've used different versions of the "jump_srch" macro as a productivity enhancer.

The example of jump_srch code below is rudimentary and only involves quick searching between two MDI windows. I plan to add more complete versions in the near future. This macro can be made much more versatile and powerful, incorporating such tasks as automatic insertion, deletion, checking and indexing of text between windows. For now, you're getting it in its bare bones flavor. Please don't laugh too loud when you see the actual macro. We all have to start somewhere!

The base use of the macro is to employ find and next_window commands between two visible Slickedit MDI windows. The two windows can contain the same buffer (aka "file") or two different buffers. I haven't used this macro in more than two windows at a time, although I
suppose you could cycle the cursor through however many windows you want. FYI: I always have "one file per window" turned OFF. I don't know if the macro would be adversely affected by having it turned ON.

How it works: with your cursor on a text string in Window A, you press the key binding for jump_srch and the cursor jumps to Window B and locates the next matching search string, if any, in Window B. You can, if you like, continue searching for the same string in Window B or you can return to Window A and go on your merry way.

I use the binding "Ctrl-G" to find the next instance of a search term within a single file. To move my cursor back to Window A from Window B, I use a simple key binding for next_window. In my case, next_window is bound to "Ctrl-Tab." In other words, you use jump_srch to seek the string in the other window, and then you use next_window to return to the first window. This is handy when you need to locate or cross-check certain words or phrases which appear in both files or both windows.

Here's an example of tasks for which this macro can be useful. Please forgive the use of jargon from the book publishing industry, which is where I've used this macro most often.

/*---------------------------------------------------------------*/

How to Automate Cross-checking of References and Citations
in Publication Drafts

There are two basic goals in cross-checking literary references;
these are:

1) to verify that every reference appearing in the main section of the
document (body text, tables, figures, etc.) has a corresponding
citation in the section designated to hold a comprehensive listing
of citations (this is usually titled References, and appears as a
component of the back matter)

2) to verify that every citation in the References section has a
corresponding reference in the main section of the document

These two sections are typically maintained in two separate text files.
This facilitates creation of a system for automating
the process of checking crossreferences . . .
/*---------------------------------------------------------------*/

jump_srch, barebones version:
// put the current word into a variable, jump to the next open window, and find the text in that window
// todo etc.
// make it work with any selected current word or phrase or line

// whether the cursor is in or at the start of a word; selected or not
// allow for omore than 2 windows, etc
// pass cmdline parameters to search command

_command void jump_search(...) name_info(',')
{
_str keywd=cur_word(stuff);
message(something);
next_window();
search(something);
}

dh_Fileinfo

, , , , , , , , , , , , , , , , Monday, December 1, 2008 0 comments

The dh_fileinfo macro displays an informational dialog box with
various
details about the file currently being edited. I cobbled the macro
together from code available on the Slickedit Web site and elsewhere.
I've since forgotten where everything came from. This macro can be
greatly improved. You might notice, for example, that the value of some
of the variables, such as 'cc' (character count) are not yet included in the
message box.

I invite your improvements and suggestions, including ideas on stats to
add to the existing dialog.


The dh_fileinfo dialog lists:
  • fully qualified filename and path
  • file's current date and time
  • file size in bytes
  • file attributes
  • total number of lines in file
  • current margin setting
  • file's mode (e.g., Slick-C, HTML, C, etc.)
  • number of windows open in the editor
  • number of buffers open
  • number of the current View id
  • decimal and hex values of character under the cursor
// detailed fileinfo message box
_command dh_fileinfo() name_info(FILE_ARG'*,'READ_ONLY_ARG2)
{
_str filename=arg(1)
if (filename=='') {
filename=p_buf_name;
}
filename=absolute(filename);
_str filedate=file_date(filename);
_str filetime=file_time(filename);
_str fileattr=file_list_field(filename,DIR_ATTR_COL,DIR_ATTR_WIDTH);
int filelen=p_Noflines;
int bufcou=_Nofbuffers();
int wincou=Nofwindows();
get_view_id view_id
parse p_margins with left_ma right_ma new_para_ma;

// info on current character
get_line(line);
char=substr(line,p_col,1);
chr_num=_asc(get_text());

cc = 0; /* chars */
wc = 0; /* words */
lc = 0; /* lines */
dc = 0; /* displayed lines */
hc = 0; /* hidden lines */

int item = 0;
save_pos(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();
bytesize = fsize();
dc = (p_Noflines - p_NofSelDispBitmaps);
hc = p_Nofhidden;
sc = p_NofSelDispBitmaps;
pc = ( filelen intdiv 55 );
_message_box(filename \r filedate' 'filetime \r bytesize ' bytes; ' fileattr \r\
'Lines: 'filelen' '\r\
'Margins: 'left_ma' 'right_ma' 'new_para_ma \r\
'Mode: 'p_mode_name \r 'Windows: 'wincou', Buffers: 'bufcou \r\
'Viewid 'view_id \r\
'Ascii char "'char'" dec='chr_num', hex='dec2hex(chr_num),' ':+
'FileInfo: '
p_buf_name, MB_ICONINFORMATION);

restore_pos(p);
break;
}
}

make_anchor

, , , Sunday, November 30, 2008 0 comments

// make_anchor
// This nifty macro comes from John Hurst's book, Professional Slickedit,
// published by Wrox, a subsidiary of Wiley Publishing (Indianapolis),
// 2008, paperback, 472 pages. List price $49.99. The Slickedit macro code
// for make_anchor and many other macros in from Professional Slickedit
// can be downloaded freely from http://www.wrox.com/WileyCDA/WroxTitle/
// Professional-SlickEdit.productCd-0470122153,descCd-DOWNLOAD.html

// make_anchor creates an HTML anchor tag around the selected text.
// For example: if the selected text is "www.wrox.com", highlighting
// "www.wrox.com" and then executing make_anchor will produce the following:

<a href="http://www.wrox.com" www.wrox.com<a>

// This would, of course, be rendered to the user as the hyperlink
// www.wrox.com.

// Some editing software and blog editing programs
// nowadays incorporate similar functionality,
// but if you're editing plain text files in Slickedit, this macro could be
// a time saver.

GETL: Variation on GET Displays Number of Lines "Gotten"

Tuesday, November 25, 2008 0 comments

// Here's another of my rinky-dink macros:
// GETL: same as 'get' but displays the number of lines imported into the
// current buffer. I missed having this functionality when I switched over from
// Mansfield's Kedit. It acts as a visual confirmation of the number of
// lines imported and tells you how much bigger you're making the current
// (i.e., destination) file.

_command getl(...) name_info(FILE_ARG'*,'VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
filename=arg(1)
old_noflines=p_noflines;
get(filename);
new_noflines=p_noflines;
noflines_added=(new_noflines - old_noflines)
message(noflines_added' lines added');
}

// direct complaints, suggestions or improvements to me

Fun with Line Flags!

, , , , , , , , Sunday, October 26, 2008 0 comments

Lineflags, as defined below and in the macros that follow, are an important
element driving the attributes, behavior and display of specific types of lines
of text in a Slickedit editing session. Lineflags are used in such modules as
mouse.e, seldisp.e, util.e, builtins.e, surround.e, markfilt.e, guireplace.e,
javadoc.e and many more.

Some of the more common examples of commands that use lineflags include 'all',
'less', 'hide_all_comments', 'plusminus', 'show_braces', and 'show_paragraphs.'
A few of the uses of lineflags include the setting of levels of nested comments
and selectively displayed lines, the setting of 'no save' lines in Diffzilla,
the 'plus' or 'minus' bitmaps shown in the left margins of selective displays,
and even in the color or box appearance of the current line.

Additionally, lineflags account for the differently colored labels used to
identify 'inserted' and 'modified' lines in the lefthand margins or linenumbers
area of speacific lines in a file. Some types of lineflags set the visible
appearance of lines and some are used unseen in the background.

The basic function for manipulating lineflags, taken from Slickedit Help, is
'_lineflags', as follows. (For added flavor, I've included the bit values in
hex and decimal that are set by a few of the lineflags. I gathered these bits
from a source file in which the constants were set, in an earlier version of
Slickedit. I haven't been able to find them in the current Slickedit 2008 and
recent versions.)

Following this Help information, I present several of my 'Fun With Lineflags'
macros.)

int _lineflags(int newflags=0, int mask=newflags)

Gets or sets the line status flags for the current line. If the flags argument
is given, the line status flags for the current line are modified. mask
defaults to the same value as flags if it is not specified. The mask indicates
which bits will be set according to flags.

MLCOMMENTINDEX_LF
Indicates which multi-line comment. Only two are allowed. Must know which
multi-line comment we are in so we know what will terminate it.

MLCOMMENTLEVEL_LF
Indicates multi-line comment nest level.
Used by Difference Editor and Merge Editor. Lines with the NOSAVE_LF flag set
are not saved in the file. VIMARK_LF Used by VI emulation to mark lines.

MODIFY_LF
Line has been modified.

INSERTED_LINE_LF
Line was inserted.

HIDDEN_LF 0x00001000 4096
Indicates that this line should not be displayed.

PLUSBITMAP_LF 0x00004000 16384
Display "+" bitmap to left of this line.

MINUSBITMAP_LF 0x00002000 8192
Display "-" bitmap to left of this line.

CURLINEBITMAP_LF
Display current line bitmap.

LEVEL_LF 0x001F8000 2064384
Bits used to store selective display nest level.

NEXTLEVEL_LF 0x00008000 32768

The MLCOMMENT flags can not be modified.

Returns: int
The new current line status flags for the current line.


Examples:

if (_lineflags() & INSERTED_LINE_LF) {
messageNwait("This line was inserted");
}
if (_lineflags() & MODIFY_LF) {
messageNwait("This line was modified");
}
// Turn on hidden flag
_lineflags(HIDDEN_LF,HIDDEN_LF);
if (_lineflags() & HIDDEN_LF) {
messageNwait("HIDDEN flag is on");

}
// Turn off HIDDEN flag
_lineflags(0,HIDDEN_LF);
if (!(_lineflags() & HIDDEN_LF)) {
messageNwait("hidden flag is off");
}

Applies To:
Edit Window, Editor Control

Categories:
CursorMovement Functions, Edit Window Methods, Editor Control Methods


FUN WITH LINEFLAGS--GETTING DOWN TO BUSINESS

Below are several macros I created to explore possible novel uses of lineflags.
I've refrained from including a couple related commands because they are not
ready for prime time.

Try these on sample files that you can afford to mess up. Generally the
operations are easily reversible (as in the toggle commands), but be
particularly careful applying the NOSAVE_LF. In some situations NOSAVE Lines
may be deleted permanently when you close a file containing them.

// clf_toggle: this command toggles the 'current line' flag on any line you
// choose. 'clf' in the command name means 'current line flag.' In this
// particular implementation, the 'current line' flag appears as a green triangle
// in the left margin. You can toggle the current line flag on and off easily if
// you bind the command to a key. Each time the command is issued, the existing
// flag is toggled and the cursor moves down one line.

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


// turn ON ALL CLF indicators and flags in a file
// turn CLF ON for ALL lines
_command clf_all_lines()
{
if (p_mode_name=='Fileman') {
deselect_all();
} else {
save_pos(p)
top();up();
_lineflags(CURLINEBITMAP_LF,CURLINEBITMAP_LF);
for (;;) {
if (down()) break;
_lineflags(CURLINEBITMAP_LF,CURLINEBITMAP_LF);
}
restore_pos(p);
}
}


// turn OFF ALL CLF indicators and flags in a file
// turn CLF OFF for ALL lines
_command clf_no_lines()
{
if (p_mode_name=='Fileman') {
deselect_all();
} else {
save_pos(p)
top();up();
_lineflags(0,CURLINEBITMAP_LF);
for (;;) {
if (down()) break;
_lineflags(0,CURLINEBITMAP_LF);
}
restore_pos(p);
}
}

// turn CLF ON for lines matching search string in the current buffer
// for example, 'all_clf /VSARG2/' will add the current line flag to all
// lines containg 'VSARG2'--like the 'all' command. You might want to change the
// command name, since it can be confused with 'clf_all_lines.
_command all_clf(...) name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
int strcount;
int linecount;
_str searchstr=arg(1);
parse searchstr 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;
_lineflags(CURLINEBITMAP_LF,CURLINEBITMAP_LF);
} else {
restore_pos(p);
message get_message(status);
stop;
}

for (;;) {
status=search(sstring, 'i>':+opts);
if (status !=0) {
break
} else {
_lineflags(CURLINEBITMAP_LF,CURLINEBITMAP_LF);
++strcount;

if (!(curline==p_line)) {
++linecount;
curline=p_line;
}

}
}
restore_pos(p);
message(strcount' occurrence(s) in ' linecount ' lines');
}


// 'NO-SAVE LINE' FLAG (displys the Slickedit default or user-set background
// color for 'no save' lines. 'NLF' means NOSAVE lineflag.)
// toggle NLF for current line
_command nlf_toggle() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
if (!(_lineflags() & 2)) {
_lineflags(NOSAVE_LF,NOSAVE_LF);
} else {
_lineflags(0,NOSAVE_LF);
}
cursor_down();
}

// turn NLF ON for ALL lines
_command nlf_all_lines()
{
if (p_mode_name=='Fileman') {
deselect_all();
} else {
save_pos(p)
top();up();
_lineflags(NOSAVE_LF,NOSAVE_LF);
for (;;) {
if (down()) break;
_lineflags(NOSAVE_LF,NOSAVE_LF);
}
restore_pos(p);
}
}

// turn NLF OFF for ALL lines
_command nlf_no_lines()
{
if (p_mode_name=='Fileman') {
deselect_all();
} else {
save_pos(p)
top();up();
_lineflags(0,NOSAVE_LF);
for (;;) {
if (down()) break;
_lineflags(0,NOSAVE_LF);
}
restore_pos(p);
}
}

// turn NLF ON for lines matching search string in current buffer
_command all_nlf(...) name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
int strcount;
int linecount;
_str searchstr=arg(1);
parse searchstr 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;
_lineflags(NOSAVE_LF,NOSAVE_LF);
} else {
restore_pos(p);
message get_message(status);
stop;
}

for (;;) {
status=search(sstring, 'i>':+opts);
if (status !=0) {
break
} else {
_lineflags(NOSAVE_LF,NOSAVE_LF);
++strcount;

if (!(curline==p_line)) {
++linecount;
curline=p_line;
}

}
}
restore_pos(p);
message(strcount' occurrence(s) in ' linecount ' lines');
}

// turn NLF AND 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);
}
}

// Setting PLUS and MINUS BITMAPS

// If a 'plus' and 'minus' bitmap is shown in left margin of the current line,
// running this command on that line will remove the bitmap and its associated
// lineflag.
_command set_plusminus_lf_off()
name_info(','VSARG2_REQUIRES_EDITORCTL) {
if (_lineflags() & (PLUSBITMAP_LF|MINUSBITMAP_LF)) {
_lineflags(0,PLUSBITMAP_LF|MINUSBITMAP_LF);
cursor_down();
}
}

// sets hidden lineflag on and hides the line
_command set_hidden_lf_on() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
_lineflags(HIDDEN_LF,HIDDEN_LF);
if (!(_lineflags() & HIDDEN_LF)) {
messageNwait("hidden flag is off");
}
}

// hides lines in the numeric range specified in the argument
// For example, 'hide_lines 30 50' will hide file lines 30-50 and
// indicate them by showing a 'plus' mark (collapsed/hidden lines) in the margin

_command hide_lines(...) name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY|VSARG2_CMDLINE)
{
parse arg(1) with first_line last_line

if (first_line>last_line) {
p_line=last_line;
return(1);
}
p_line=first_line;
// IF we are on line#0 and we are not displaying tof line
if (_on_line0() && !_default_option('t')) {
++first_line;
status=down();
if (status || first_line>=last_line) {
p_line=last_line;
return(-1);
}
}
cur_lineflags=_lineflags();
up();
prev_lineflags=_lineflags();
// IF this is case 1 or 3
if (!(prev_lineflags & (PLUSBITMAP_LF|MINUSBITMAP_LF)) &&
(prev_lineflags & LEVEL_LF)==(cur_lineflags & LEVEL_LF)) {
new_level=(prev_lineflags& LEVEL_LF) + NEXTLEVEL_LF;
// messageNwait("hide_selection: case 1 or 3");
// IF this is case 2
} else if ((prev_lineflags & (PLUSBITMAP_LF|MINUSBITMAP_LF)) &&
!(cur_lineflags & (PLUSBITMAP_LF|MINUSBITMAP_LF))
) {
p_line=last_line;
return(0);
// IF this is case 4 or 5
} else {
p_line=last_line;
return(0);
}

_lineflags(PLUSBITMAP_LF,PLUSBITMAP_LF|MINUSBITMAP_LF);
down();
start_level=new_level-NEXTLEVEL_LF;
doEndLastLevel=true;
for (;;) {
level=(_lineflags() & LEVEL_LF);
if (levellast_line) {
break;
}
}
if (doEndLastLevel) {
for (;;) {
new_level= (_lineflags() & LEVEL_LF)+NEXTLEVEL_LF;
if (new_level /*(_lineflags() & LEVEL_LF)*/<=start_level+NEXTLEVEL_LF) { break; } _lineflags(HIDDEN_LF|new_level,HIDDEN_LF|LEVEL_LF); status=down(); if (status) break; } } p_line=last_line;
}


Flush Left All Lines in Buffer

Tuesday, October 14, 2008 0 comments

I use this dinky little macro all the time. Offered for your consideration ... and, as always, perhaps you can build on it

// flush left all lines in current buffer
// TODO: Add option to affect line, selection, or all
// depending on select_active, etc

_command fl,flush_left_all() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
save_pos(p);
top();
search('^:b','r*','');
restore_pos(p);
}

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