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

📄 utils.c

📁 该内容是《》一书的光盘内容
💻 C
📖 第 1 页 / 共 4 页
字号:


#include "tdestr.h"
#include "common.h"
#include "define.h"
#include "tdefunc.h"



int  myiswhitespc( int c )
{
   return( c == ' ' || (ispunct( c ) && c != '_') || iscntrl( c ) );
}



void check_virtual_col( WINDOW *window, int rcol, int ccol )
{
register int bcol;
int  start_col;
int  end_col;
file_infos *file;

   file      = window->file_info;
   bcol      = window->bcol;
   start_col = window->start_col;
   end_col   = window->end_col;

 
   if (ccol > end_col) {
 2;  */
      ccol = end_col;
      bcol = rcol - (ccol - start_col);
      file->dirty = LOCAL;
   }

  
   if (ccol < start_col) {
      if (bcol >= (start_col - ccol))
         bcol -= (start_col - ccol);
      ccol = start_col;
      file->dirty = LOCAL;
   }

  
   if (rcol < bcol) {
      ccol = rcol + start_col;
      bcol = 0;
      if (ccol > end_col) {
         bcol = rcol;
         ccol = start_col;
      }
      file->dirty = LOCAL;
   }

  
   if ((ccol - start_col) + bcol != rcol) {
      if (bcol < 0 || bcol > rcol) {
         bcol = rcol;
         file->dirty = LOCAL;
      }
      ccol = rcol - bcol + start_col;
      if (ccol > end_col) {
         bcol = rcol;
         ccol = start_col;
         file->dirty = LOCAL;
      }
   }

  
   if (rcol < 0) {
      rcol = bcol = 0;
      ccol = start_col;
      file->dirty = LOCAL;
   }

   if (rcol >= MAX_LINE_LENGTH) {
      rcol = MAX_LINE_LENGTH - 1;
      bcol = rcol - (ccol - start_col);
   }

   assert( rcol >= 0 );
   assert( rcol < MAX_LINE_LENGTH );
   assert( bcol >= 0 );
   assert( bcol < MAX_LINE_LENGTH );
   assert( ccol >= start_col );
   assert( ccol <= end_col );

   window->bcol = bcol;
   window->ccol = ccol;
   window->rcol = rcol;
}



void copy_line( line_list_ptr ll )
{
register unsigned int len;
text_ptr text_line;

   if (g_status.copied == FALSE  &&  ll->len != EOF) {

      assert( ll != NULL );

      len = ll->len;
      text_line = ll->line;
      g_status.buff_node = ll;

      assert( len < MAX_LINE_LENGTH );

      if (text_line != NULL)
         _fmemcpy( g_status.line_buff, text_line, len );

      g_status.line_buff_len = len;
      g_status.copied = TRUE;
   }
}



int  un_copy_line( line_list_ptr ll, WINDOW *window, int del_trailing )
{
text_ptr p;
size_t len;     
size_t ll_len;  
int  net_change;
int  rc;
char c;
file_infos *file;
WINDOW *wp;

   rc = OK;
   if (mode.do_backups == TRUE)
      rc = backup_file( window );

   if (g_status.copied == TRUE  &&  ll->len != EOF) {

      file = window->file_info;

      if (g_status.command == DeleteLine)
         del_trailing = FALSE;

      if (del_trailing  &&  mode.trailing  &&  file->crlf != BINARY) {
         len = g_status.line_buff_len;
         for (p=(text_ptr)(g_status.line_buff+len); len > 0; len--, p--) {
            c = *(p - 1);
            if (c != ' '  &&  c != '\t')
               break;
            if (!mode.inflate_tabs && c == '\t')
               break;
         }
         g_status.line_buff_len = len;
         file->dirty = GLOBAL;
         if (window->visible == TRUE)
            show_changed_line( window );
      }
      len = g_status.line_buff_len;
      ll_len =  (ll->line == NULL) ? 0 : ll->len;


      assert( len < MAX_LINE_LENGTH );
      assert( ll_len < MAX_LINE_LENGTH );

      net_change = len - ll_len;

      if (ll_len != len  ||  ll->line == NULL) {
         
         p = my_malloc( len, &rc );
         if (rc == ERROR)
            error( WARNING, window->bottom_line, main4 );

        
         if (rc != ERROR  &&  ll->line != NULL)
            my_free( ll->line );
      } else
         p = ll->line;

      if (rc != ERROR) {
         if (len > 0)
            _fmemcpy( p, g_status.line_buff, len );
         ll->line = p;
         ll->len = len;

         if (net_change != 0) {
            for (wp=g_status.window_list; wp != NULL; wp=wp->next) {
               if (wp->file_info == file && wp != window)
                  if (wp->rline > window->rline)
                     wp->bin_offset += net_change;
            }
         }

         file->modified = TRUE;
         show_avail_mem( );
      }
   }
   g_status.copied = FALSE;
   return( rc );
}



int  un_copy_tab_buffer( line_list_ptr ll, WINDOW *window )
{
text_ptr p;
int  len;               
int  net_change;
int  rc;
file_infos *file;
WINDOW *wp;

   rc = OK;
   file = window->file_info;
  
   if (mode.do_backups == TRUE) {
      window->file_info->modified = TRUE;
      rc = backup_file( window );
   }

   len = g_status.tabout_buff_len;

   assert( len >= 0 );
   assert( len < MAX_LINE_LENGTH );
   assert( ll->len >= 0 );
   assert( ll->len < MAX_LINE_LENGTH );

  
   p = my_malloc( len, &rc );
   if (rc == ERROR)
      error( WARNING, window->bottom_line, main4 );

   if (rc == OK) {
      net_change = len - ll->len;

      if (ll->line != NULL)
         my_free( ll->line );
      if (len > 0)
         _fmemcpy( p, g_status.line_buff, len );
      ll->line = p;
      ll->len  = len;

      if (net_change != 0) {
         for (wp=g_status.window_list; wp != NULL; wp=wp->next) {
            if (wp->file_info == file && wp != window)
               if (wp->rline > window->rline)
                  wp->bin_offset += net_change;
         }
      }

      file->modified = TRUE;
   }
   return( rc );
}



void load_undo_buffer( file_infos *file, text_ptr line_to_undo, int len )
{
int  rc;
text_ptr l;
line_list_ptr temp_ll;

   rc = OK;
   if (file->undo_count >= mode.undo_max) {
      --file->undo_count;
      temp_ll = file->undo_bot->prev;
      temp_ll->prev->next = file->undo_bot;
      file->undo_bot->prev = temp_ll->prev;
      if (temp_ll->line != NULL)
         my_free( temp_ll->line );
   } else
      temp_ll = (line_list_ptr)my_malloc( sizeof(line_list_struc), &rc );

   assert( len >= 0 );
   assert( len < MAX_LINE_LENGTH );

   l = my_malloc( len, &rc );

   if (rc == ERROR) {
      if (l != NULL)
         my_free( l );
      if (temp_ll != NULL)
         my_free( temp_ll );
   } else {
      if (len > 0)
         _fmemcpy( l, line_to_undo, len );
      temp_ll->line  = l;
      temp_ll->len   = len;
      temp_ll->dirty = TRUE;

      temp_ll->prev = NULL;
      temp_ll->next = file->undo_top;
      file->undo_top->prev = temp_ll;
      file->undo_top = temp_ll;

      ++file->undo_count;
   }
}



void set_prompt( char *prompt, int line )
{
register int prompt_col;

  
   prompt_col = strlen( prompt );

   assert( prompt_col <= MAX_COLS );

  
   s_output( prompt, line, 0, g_display.message_color );
   eol_clear( prompt_col, line, g_display.message_color );

  
   xygoto( prompt_col, line );
}



int  get_name( char *prompt, int line, char *name, int color )
{
int  col;               
int  c;                 
char *cp;               
char *answer;           
int first = TRUE;       
register int len;       
int  plen;              
int  func;              
int  stop;              
char *p;                
char buffer[MAX_COLS+2];
char line_buff[(MAX_COLS+1)*2]; 
int  normal;
int  local_macro = FALSE;
int  next;
int  regx_help_on;
char **pp;
int  i;

  
   assert( strlen( prompt ) < MAX_COLS );
   assert( strlen( name )   < MAX_COLS );

   strcpy( buffer, prompt );
   plen = strlen( prompt );
   answer = buffer + plen;
   strcpy( answer, name );

   regx_help_on = FALSE;
   len = strlen( answer );
   col = strlen( buffer );
   g_status.prompt_line = line;
   g_status.prompt_col = col;
   cp = answer + len;
   normal = g_display.text_color;
   save_screen_line( 0, line, line_buff );
   s_output( buffer, line, 0, color );
   eol_clear( col, line, normal );
   for (stop = FALSE; stop == FALSE;) {
      if (regx_help_on == TRUE)
         xygoto( -1, -1 );
      else
         xygoto( col, line );
      if (g_status.macro_executing) {
         next = g_status.macro_next;
         g_status.macro_next = macro.strokes[g_status.macro_next].next;
         if (g_status.macro_next != -1) {
            c = macro.strokes[g_status.macro_next].key;
            func = getfunc( c );
            if (func == PlayBack) {
               stop = TRUE;
               g_status.macro_next = next;
            }
         } else {
            c = 0x100;
            func = AbortCommand;
            stop = TRUE;
         }
      } else {
         if (local_macro == FALSE) {
            c = getkey( );
            func = getfunc( c );

           
            if (c == RTURN)
               func = Rturn;
            else if (c == ESC)
               func = AbortCommand;

            if (func == PlayBack) {
               local_macro = TRUE;
               next = macro.first_stroke[ c-256 ];
               c = macro.strokes[next].key;
               func = getfunc( c );
               next = macro.strokes[next].next;
            } else {
               g_status.key_pressed = c;
               record_keys( line );
            }
         } else {
            if (next != -1) {
               c = macro.strokes[next].key;
               next = macro.strokes[next].next;
            } else {
               local_macro = FALSE;
               c = 0x100;
            }
            func = getfunc( c );
         }
      }
      if (c == _F1)
         func = Help;
      if (regx_help_on == TRUE  &&  g_status.current_window != NULL) {
         redraw_screen( g_status.current_window );
         s_output( buffer, line, 0, color );
         eol_clear( col, line, normal );
         s_output( cp, line, col, color );
         regx_help_on = FALSE;
      } else {
         switch (func) {
            case Help :
               if ((g_status.command == FindRegX  ||
                    g_status.command == RepeatFindRegX) &&
                    regx_help_on == FALSE) {
                  regx_help_on = TRUE;
                  for (i=3,pp=regx_help; *pp != NULL; pp++, i++)
                     s_output( *pp, i, 12, g_display.help_color );
               }
               break;
            case ToggleSearchCase :
               mode.search_case = mode.search_case == IGNORE ? MATCH : IGNORE;
               build_boyer_array( );
               show_search_case( );
               break;
            case Rturn       :
            case NextLine    :
            case BegNextLine :
               answer[len] = '\0';
               assert( strlen( answer ) < MAX_COLS );
               strcpy( name, answer );
              
               stop = TRUE;
               break;
            case BackSpace :
             
               if (cp > answer) {
                  for (p=cp-1; p < answer+len; p++) {
                     *p = *(p+1);
                  }
                  --len;
                  --col;
                  --cp;
                  c_output( ' ', plen+len, line, normal );
                  s_output( cp, line, col, color );
                  *(answer + len) = '\0';
               }
               break;
            case DeleteChar :
            
               if (*cp) {
                  for (p=cp; p < answer+len; p++) {
                     *p = *(p+1);
                  }
                  --len;
                  c_output( ' ', plen+len, line, normal );
                  s_output( cp, line, col, color );
                  *(answer + len) = '\0';
               }
               break;
            case DeleteLine :
              
               col = plen;
               cp = answer;
               *cp = '\0';
               len = 0;
               eol_clear( col, line, normal );
               break;
            case AbortCommand :
               stop = TRUE;
               break;
            case CharLeft :
            
               if (cp > answer) {
                  col--;
                  cp--;
               }
               break;
            case CharRight :
              
               if (*cp) {
                  col++;
                  cp++;
                }
                break;
            case BegOfLine :
             
               col = plen;
               cp = answer;
               break;
            case EndOfLine :
             
               col = plen + len;
               cp = answer + len;
               break;
            default :
               if (c < 0x100) {
                
                  if (first) {
                    
                     col = plen;
                     cp = answer;
                     *cp = '\0';
                     len = 0;
                     eol_clear( col, line, normal );
                  }

                 
                  if (col < g_display.ncols-1) {
                     if (*cp == '\0') {
                        ++len;
                        *(answer + len) = '\0';
                     }
                     *cp = (char)c;
                     c_output( c, col, line, color );
                     ++cp;
                     ++col;
                  }
               }
               break;
         }
      }
      first = FALSE;
   }
   restore_screen_line( 0, line, line_buff );
   return( func == AbortCommand ? ERROR : OK );
}



int  get_sort_order( 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( utils4, window->bottom_line, 0, g_display.message_color );
   c = strlen( utils4 );
   eol_clear( c, window->bottom_line, g_display.text_color );
   xygoto( c, window->bottom_line );
   do {
      c = getkey( );
      col = getfunc( c );
      if (c == ESC)

⌨️ 快捷键说明

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