Showing posts with label Slick-C commands. Show all posts
Showing posts with label Slick-C commands. Show all posts
, , , Saturday, April 12, 2014 1 comments

mark_duplicates command: Marks adjacent duplicate lines in the current buffer by prepending each duplicated line with ">> ", then displays the number of times lines are duplicated.

Two duplicate adjacent lines count as 1 duplicate, 3 duplicate adjacent lines count as 2 duplicates, 4 duplicate adjacent lines count as 3 duplicates, and so on.

This is Slickedit's remove_duplicates command, with a symbol (">> ") marking the duplicates and a count. No lines are removed. A possible useful option to add would be to fold the dupes into a  selective display, showing only the first instance of lines that are duplicated.
_command void mark_duplicates(_str ignore_case='') name_info(','VSARG2_EDITORCTL|VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
{
   int start_line=0;
   int last_line=MAXINT;
   if (select_active2()) {
      if (_select_type()=='CHAR') {
         _select_type("","L","LINE");
      }
      _begin_select();
      start_line=p_line;
      _end_select();
      last_line=p_line;
   }
   _mark_duplicates(ignore_case,start_line,last_line);
}
/**
 * Marks duplicate adjacent lines in the current buffer.
 * Usually the buffer is sorted before calling this function.
 * 
 * @param ignore_case      'i' for case sensitive
 * @param start_line       starting line (inclusive)
 * @param last_line        last line (inclusive)
 * 
 * @appliesTo Edit_Window, Editor_Control
 * 
 * @categories Edit_Window_Methods, Editor_Control_Methods
 */
void _mark_duplicates(_str ignore_case='',int start_line=0,int last_line=MAXINT)
{
   // if this is a list box, then use the more effecient builtin method
   boolean listbox_object = (p_object == OI_LIST_BOX);
   if (listbox_object) {
      _lbremove_duplicates(ignore_case, start_line, last_line-start_line);
      return;
   }

   // convert case argument to boolean
   ignore_case=upcase(ignore_case)=='I';

   // compute the start line
   if (start_line>0) {
      p_line=start_line;
   } else {
      top();
      start_line=1;
   }

   // compute the last line
   if (last_line==MAXINT) {
      last_line=p_Noflines;
   }

   // go through the lines
   int count=last_line-start_line;
   int dupcount=0;
   _str previous_line='', line='';
   get_line(previous_line);

   if (ignore_case) {
      // case insensitive
      for (;count-->0;) {
         down();
         get_line(line);
         if (strieq(line,previous_line)) {
             dupcount=dupcount+1;
            up(); replace_line(">> ":+line);
            down(); replace_line(">> ":+line);
         }
         previous_line=line;
      }
   } else {
      // case sensitive
      for (;count-->0;) {
         down();
         get_line(line);
         if ( line:==previous_line ) {
             dupcount=dupcount+1;
            up(); replace_line(">> ":+line);
            down(); replace_line(">> ":+line);
         }
         previous_line=line;
      }
      message("Duplicate count = " dupcount);
   }
}

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

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