⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.c

📁 该内容是《》一书的光盘内容
💻 C
📖 第 1 页 / 共 4 页
字号:
         col = AbortCommand;
   } while (col != AbortCommand  &&  c != 'A'  &&  c != 'a'  &&
            c != 'D'  &&  c != 'd');
   switch ( c ) {
      case 'A' :
      case 'a' :
         sort.direction = ASCENDING;
         break;
      case 'D' :
      case 'd' :
         sort.direction = DESCENDING;
         break;
      default  :
         col = AbortCommand;
         break;
   }
   restore_screen_line( 0, window->bottom_line, line_buff );
   return( col == AbortCommand ? ERROR : OK );
}



int  get_replace_direction( WINDOW *window )
{
register int c;
int  col;
char line_buff[(MAX_COLS+1)*2];         

   save_screen_line( 0, window->bottom_line, line_buff );
 
   s_output( utils5, window->bottom_line, 0, g_display.message_color );
   c = strlen( utils5 );
   eol_clear( c, window->bottom_line, g_display.text_color );
   xygoto( c, window->bottom_line );
   do {
      c = getkey( );
      col = getfunc( c );
      if (c == ESC)
         col = AbortCommand;
   } while (col != AbortCommand  &&  c != 'F'  &&  c != 'f'  &&
            c != 'B'  &&  c != 'b');
   switch ( c ) {
      case 'F' :
      case 'f' :
         c = FORWARD;
         break;
      case 'B' :
      case 'b' :
         c = BACKWARD;
         break;
      default  :
         c = ERROR;
   }
   restore_screen_line( 0, window->bottom_line, line_buff );
   return( col == AbortCommand ? ERROR : c );
}



int  get_yn( void )
{
int  c;                 
register int rc;        

   do {
      c = getkey( );
      rc = getfunc( c );
      if (c== ESC)
         rc = AbortCommand;
   } while (rc != AbortCommand  &&  c != 'Y'  &&  c != 'y'  &&
            c != 'N'  &&  c != 'n');
   if (rc == AbortCommand || c == ESC)
      rc = ERROR;
   else {
      switch ( c ) {
         case 'Y' :
         case 'y' :
            rc = A_YES;
            break;
         case 'N' :
         case 'n' :
            rc = A_NO;
            break;
      }
   }
   return( rc );
}



int  get_lr( void )
{
int  c;                 
register int rc;        

   for (rc=OK; rc == OK;) {
      c = getkey( );
      if (getfunc( c ) == AbortCommand || c == ESC)
         rc = ERROR;
      else {
         switch ( c ) {
            case 'L' :
            case 'l' :
               rc = LEFT;
               break;
            case 'R' :
            case 'r' :
               rc = RIGHT;
               break;
         }
      }
   }
   return( rc );
}



int  get_bc( void )
{
int  c;                 
register int rc;        

   for (rc=OK; rc == OK;) {
      c = getkey( );
      if (getfunc( c ) == AbortCommand || c == ESC)
         rc = ERROR;
      else {
         switch ( c ) {
            case 'B' :
            case 'b' :
               rc = BEGINNING;
               break;
            case 'C' :
            case 'c' :
               rc = CURRENT;
               break;
         }
      }
   }
   return( rc );
}



int  get_oa( void )
{
int  c;                 
register int rc;        
int  func;

   rc = 0;
   while (rc != AbortCommand && rc != A_OVERWRITE && rc != A_APPEND) {
      c = getkey( );
      func = getfunc( c );
      if (func == AbortCommand || c == ESC)
         rc = AbortCommand;
      switch ( c ) {
         case 'O' :
         case 'o' :
            rc = A_OVERWRITE;
            break;
         case 'A' :
         case 'a' :
            rc = A_APPEND;
            break;
      }
   }
   return( rc );
}


void show_eof( WINDOW *window )
{
register int color;
char temp[MAX_COLS+2];

   assert( strlen( mode.eof ) < MAX_COLS );

   strcpy( temp, mode.eof );
   color = window->end_col + 1 - window->start_col;
   if (strlen( temp ) > (unsigned)color)
      temp[color] = '\0';
   color = g_display.eof_color;
   window_eol_clear( window, color );
   s_output( temp, window->cline, window->start_col, color );
}



void display_current_window( WINDOW *window )
{
int  count;     
int  number;    
register int i; 
WINDOW w;       
int  curl;      
int  eof;

  
   number = window->bottom_line - ((window->top_line + window->ruler) - 1);
   count  = window->cline - (window->top_line + window->ruler);
   dup_window_info( &w, window );

   w.cline -= count;
   w.rline -= count;
   for (eof=count; eof > 0; eof--)
      w.ll = w.ll->prev;


   
   eof = FALSE;
   curl = window->cline;
   for (i=number; i>0; i--) {
      if (w.ll->len != EOF) {
       
         if (w.cline != curl)
            update_line( &w );
         w.ll = w.ll->next;
      } else if (eof == FALSE) {
         show_eof( &w );
         eof = TRUE;
      } else
         window_eol_clear( &w, COLOR_TEXT );
      ++w.cline;
      ++w.rline;
   }
   show_asterisk( window );
   show_curl_line( window );
}



int  redraw_screen( WINDOW *window )
{
register WINDOW *above;        
register WINDOW *below;        

   cls( );
  
   redraw_current_window( window );

  
   above = below = window;
   while (above->prev || below->next) {
      if (above->prev) {
         above = above->prev;
         redraw_current_window( above );
      }
      if (below->next) {
         below = below->next;
         redraw_current_window( below );
      }
   }
   window->file_info->dirty = FALSE;
   show_modes( );
   return( OK );
}



void redraw_current_window( WINDOW *window )
{

  
   if (window->visible) {
      display_current_window( window );
      show_window_header( window );
      show_ruler( window );
      show_ruler_pointer( window );
      if (window->vertical)
         show_vertical_separator( window );
   }
}



void show_changed_line( WINDOW *window )
{
WINDOW *above;                  
WINDOW *below;                  
WINDOW w;                       
long changed_line;              
long top_line, bottom_line;     
int  line_on_screen;            
file_infos *file;               

   file = window->file_info;
   if ((file->dirty == LOCAL || file->dirty == GLOBAL) && window->visible)
      show_curl_line( window );
   changed_line = window->rline;

  
   if (file->dirty != LOCAL) {
      above = below = window;
      while (above->prev || below->next) {
         if (above->prev) {
            above = above->prev;
            dup_window_info( &w, above );
         } else if (below->next) {
            below = below->next;
            dup_window_info( &w, below );
         }

        
         if (w.file_info == file && w.visible) {

           
            line_on_screen = FALSE;
            top_line = w.rline - (w.cline - (w.top_line + w.ruler));
            bottom_line = w.rline + (w.bottom_line - w.cline);
            if (changed_line == w.rline)
               line_on_screen = CURLINE;
            else if (changed_line < w.rline && changed_line >= top_line) {
               line_on_screen = NOTCURLINE;
               while (w.rline > changed_line) {
                  w.ll = w.ll->prev;
                  --w.rline;
                  --w.cline;
               }
            } else if (changed_line > w.rline && changed_line <= bottom_line) {
               line_on_screen = NOTCURLINE;
               while (w.rline < changed_line) {
                  w.ll = w.ll->next;
                  ++w.rline;
                  ++w.cline;
               }
            }

          
            if (line_on_screen == NOTCURLINE)
               update_line( &w );
            else if (line_on_screen == CURLINE)
               show_curl_line( &w );
         }
      }
   }
   file->dirty = FALSE;
}



void show_curl_line( WINDOW *window )
{
int  text_color;
int  dirty_color;

   if (window->visible  &&  g_status.screen_display) {
      text_color = g_display.text_color;
      dirty_color = g_display.dirty_color;
      g_display.dirty_color = g_display.text_color = g_display.curl_color;
      update_line( window );
      g_display.text_color = text_color;
      g_display.dirty_color = dirty_color;
   }
}



void dup_window_info( WINDOW *dw, WINDOW *sw )
{
   memcpy( dw, sw, sizeof( WINDOW ) );
}



void adjust_windows_cursor( WINDOW *window, long line_change )
{
register WINDOW *next;
long i;
file_infos *file;
MARKER *marker;
long length;

   file = window->file_info;
   length = file->length;
   next = g_status.window_list;
   while (next != NULL) {
      if (next != window) {
         if (next->file_info == file) {
            if (next->rline > length + 1) {
               next->rline = length;
               next->ll    = file->line_list_end;
               file->dirty = NOT_LOCAL;
            } else if (next->rline < 1) {
               next->rline = 1;
               next->cline = next->top_line + next->ruler;
               next->ll    = file->line_list;
               next->bin_offset = 0;
               file->dirty = NOT_LOCAL;
            }
            if (next->rline > window->rline  &&  line_change) {
               file->dirty = NOT_LOCAL;
               if (line_change < 0) {
                  for (i=line_change; i < 0 && next->ll->next != NULL; i++) {
                     next->bin_offset += next->ll->len;
                     next->ll = next->ll->next;
                  }
               } else if (line_change > 0) {
                  for (i=line_change; i > 0 && next->ll->prev != NULL; i--) {
                     next->ll = next->ll->prev;
                     next->bin_offset -= next->ll->len;
                  }
               }
            }
            if (next->rline < (next->cline -(next->top_line+next->ruler-1))) {
               next->cline = (int)next->rline+(next->top_line+next->ruler)-1;
               file->dirty = NOT_LOCAL;
            }
         }
      }
      next = next->next;
   }

   
   for (i=0; i<3; i++) {
      marker = &file->marker[ (int) i ];
      if (marker->rline > window->rline) {
         marker->rline += line_change;
         if (marker->rline < 1L)
            marker->rline = 1L;
         else if (marker->rline > length)
            marker->rline = length;
      }
   }
}



int  first_non_blank( text_ptr s, int len )
{
register int count = 0;

   if (s != NULL) {
      if (mode.inflate_tabs) {
         for (; len > 0 && (*s == ' ' || *s == '\t'); s++, len--) {
            if (*s != '\t')
               ++count;
            else
               count += mode.ptab_size - (count % mode.ptab_size);
         }
      } else {
         while (len-- > 0  &&  *s++ == ' ')
           ++count;
      }
   }
   return( count );
}


int  find_end( text_ptr s, int len )
{
register int count = 0;

   if (s != NULL) {
      if (mode.inflate_tabs) {
         for (;len > 0; s++, len--) {
            if (*s == '\t')
               count += mode.ptab_size - (count % mode.ptab_size);
            else
               ++count;
         }
      } else
         count = len;
   }
   return( count );
}



int is_line_blank( text_ptr s, int len )
{
   if (s != NULL) {
      if (mode.inflate_tabs) {
        while (len > 0  &&  (*s == ' ' || *s == '\t')) {
           ++s;
           --len;
        }
      } else {
         while (len > 0  &&  *s == ' ') {
            ++s;
            --len;
         }
      }
   } else
      len = 0;
   return( len == 0 );
}



int  page_up( WINDOW *window )
{
int  i;                 
int  rc = OK;           
register WINDOW *win;   
long number;
long len;

   win = window;
   entab_linebuff( );
   if (un_copy_line( win->ll, win, TRUE ) == ERROR)
      return( ERROR );
   if (win->rline != (win->cline - (win->top_line + win->ruler - 1))) {
      i = win->cline - (win->top_line + win->ruler - 1);
      number = win->rline;
      if (( win->rline - i) < win->page)
         win->rline = (win->cline-(win->top_line + win->ruler -1)) + win->page;
      win->rline -= win->page;
      for (len =0, i=(int)(number - win->rline); i>0; i--)
         if (win->ll->prev != NULL) {
            win->ll = win->ll->prev;
            len -= win->ll->len;
         }
      win->file_info->dirty = LOCAL;
      win->bin_offset += len;
   } else
      rc = ERROR;
   sync( win );
   return( rc );
}



int  page_down( WINDOW *window )
{
int  i;                 
int  k;
int  rc = OK;
long len;
register WINDOW *win;  
line_list_ptr p;

   win = window;
   entab_linebuff( );
   if (un_copy_line( win->ll, win, TRUE ) == ERROR)
      return( ERROR );
   p = win->ll;
   k = win->cline - (win->top_line + win->ruler);
   for (len=i=0; i < win->page && p->next != NULL; i++, k++, p=p->next)
      if (p->len != EOF)
         len += p->len;
   if (k >= win->page) {
      win->rline += i;
      win->cline = win->cline + i - win->page;
      win->bin_offset += len;
      win->ll = p;
      win->file_info->dirty = LOCAL;
   } else
      rc = ERROR;
   sync( win );
   return( rc );
}



int  scroll_down( WINDOW *window )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -