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

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