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

📄 tab.c

📁 一个开源著名的TDE编辑器源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
                        *to++ = ' ';                        last_col++;                     } else {                        *to++ = '\t';                        last_col += space;                     }                     rc = TRUE;                  } else                     do {                        *to++ = ' ';                        last_col++;                     } while (last_col < col);               } while (last_col < col);            }            /*             * when we see a quote character, stop entabbing.             */            if (*s == '\"' || *s == '\'' || ind) {               my_memcpy( to, s, len );               to += len;               last_col = col;               break;            }            *to++ = *s;            ++col;            last_col = col;         }         s++;      } while (--len != 0);      if (col != last_col) {         do {            space = tab_size - (last_col % tab_size);            if (last_col + space <= col) {               if (space == 1) {                  *to++ = ' ';                  last_col++;               } else {                  *to++ = '\t';                  last_col += space;               }               rc = TRUE;            } else               do {                  *to++ = ' ';                  last_col++;               } while (last_col < col);         } while (last_col < col);      }      g_status.line_buff_len = (int)(to - g_status.line_buff);   } else      g_status.line_buff_len = 0;   return( rc );}/* * Name:    detab_linebuff * Purpose: To expand tabs in line buffer so we can handle editing functions * Date:    October 31, 1992 * Notes:   before we do any editing function that modifies text, let's *            expand any tabs to space. *          if inflate_tabs is FALSE, then nothing is done. */void detab_linebuff( int inflate_tabs, int tab_size ){int  len;   if (inflate_tabs  &&  g_status.copied) {      len = g_status.line_buff_len;      tabout( g_status.line_buff, &len, tab_size, 0 );      assert( len >= 0 );      assert( len < MAX_LINE_LENGTH );      memmove( g_status.line_buff, g_status.tabout_buff, len );      g_status.line_buff_len = len;   }}/* * Name:    entab_linebuff * Purpose: To compress space in line buffer * Date:    October 31, 1992 * Notes:   compress spaces back to tabs in the line buffer, if *             inflate_tabs == TRUE * * jmh 991203: modified to suit the changed entab() function. */void entab_linebuff( int inflate_tabs, int tab_size ){   if (inflate_tabs  &&  g_status.copied)      entab( g_status.line_buff, g_status.line_buff_len, tab_size, FALSE );}/* * Name:     detab_a_line * Purpose:  To inflate tabs in any line in the file * Date:     October 31, 1992 * Modified: August 20, 1997, Jason Hood - test for NULL explicitly * Returns:  pointer to text or tabout_buff * Notes:    expand an arbitrary line in the file. */text_ptr detab_a_line( text_ptr s, int *len, int inflate_tabs, int tab_size ){   if (inflate_tabs && s != NULL) {      assert( *len >= 0 );      assert( *len < MAX_LINE_LENGTH );      s = tabout( s, len, tab_size, 0 );   }   return( s );}/* * Name:    tabout * Purpose: Expand tabs in display line * Date:    October 31, 1992 * Passed:  s:  string pointer *          len:  pointer to current length of string * Notes:   Expand tabs in the display line according to current tab. *          If eol display is on, let's display tabs, too. * * jmh 990501: added tab_size as a parameter. * jmh 991126: added tab display (mode.show_eol) as a parameter. */text_ptr tabout( text_ptr s, int *len, int tab_size, int display ){text_ptr to;int  i;int  tab_len;char show_tab;#if !defined( __DOS16__ )int  col;int  space;#endif   if (*len > 0) {      i = tab_len = *len;      show_tab = (display == 1) ? '\t' : ' ';      to = g_status.tabout_buff;#if !defined( __DOS16__ )      /*       * for right now, no assembly in unix or djgpp or win32       */      col = 0;      do {         if (*s != '\t') {            *to++ = *s;            ++col;         } else {            space = tab_size - (col % tab_size);            col += space;            if (col > MAX_LINE_LENGTH) {               space = col - MAX_LINE_LENGTH;               col = MAX_LINE_LENGTH;            }            memset( to, ' ', space );            *to = show_tab;            to += space;            tab_len += space - 1;         }         ++s;      } while (--i != 0  &&  col < MAX_LINE_LENGTH);#else   ASSEMBLE {        push    si        push    di        push    ds        mov     bx, WORD PTR tab_size   /* keep tab_size in bx */        xor     cx, cx                  /* keep col in cx */        les     di, to                  /* es:di = to or the destination */        lds     si, s                   /* ds:si = s  or the source */        push    di                      /* preserve to */        mov     ax, 0x2020              /* two spaces */        mov     cx, MAX_LINE_LENGTH / 2 /* assume even */        rep     stosw                   /* memset( to, ' ', MAX_LINE_LENGTH ) */        pop     di                      /* restore to */   }top:   ASSEMBLE {        lodsb                           /* al = BYTE PTR ds:si */        cmp     al, 0x09                /* is this a tab character? */        jne     not_tab        mov     al, BYTE PTR show_tab   /* tab or space */        stosb                           /* store in es:di and incr di */        mov     ax, cx        xor     dx, dx                  /* set up dx:ax for IDIV */        div     bx                      /* dx = col % tab_size */        mov     ax, bx                  /* put tab_size in ax */        sub     ax, dx                  /* ax = tab_size - (col % tab_size) */        add     cx, ax                  /* col += space */        dec     ax                      /* --space */        add     di, ax                  /* to += space */        add     WORD PTR tab_len, ax    /* add spaces to string length */        jmp     SHORT next_char   }not_tab:   ASSEMBLE {        stosb                           /* store character in es:di inc di */        inc     cx                      /* increment col counter */  }next_char:   ASSEMBLE {        dec     WORD PTR i              /* at end of string? */        jz      get_out        cmp     cx, MAX_LINE_LENGTH     /* at end of tabout buffer? */        jl      top}get_out:   ASSEMBLE {        pop     ds        pop     di        pop     si   }#endif      if (tab_len > MAX_LINE_LENGTH)         tab_len = MAX_LINE_LENGTH;      *len = g_status.tabout_buff_len = tab_len;   }   return( g_status.tabout_buff );}/* * Name:    detab_adjust_rcol * Purpose: given rcol in a line, find virtual column * Date:    October 31, 1992 * Modified: August 20, 1997, Jason Hood - test for NULL like entab_adjust_rcol * Passed:  s:  string pointer * Notes:   without expanding tabs, calculate the display column according *            to current tab stop. */int  detab_adjust_rcol( text_ptr s, int rcol, int tab_size ){register int col;   assert( rcol >= EOF );   assert( rcol < MAX_LINE_LENGTH );   assert( tab_size != 0 );   if (s != NULL && rcol > 0) {      col = 0;      do {         if (*s++ == '\t')            col += tab_size - (col % tab_size);         else            col++;      } while (--rcol != 0);   } else      col = rcol;   assert( col >= 0 );   assert( col < MAX_LINE_LENGTH );   return( col );}/* * Name:    entab_adjust_rcol * Purpose: given virtual rcol in a line, find real column * Date:    October 31, 1992 * Passed:  s:     string pointer *          len:   length of string *          rcol:  virtual real column *          tab_size: size of tabs * Notes:   without expanding tabs, calculate which col the real cursor *            is referencing. * * jmh:     October 31, 1997 - added "|| len == EOF" to the first assertion, to *           prevent it activating when the cursor is at EOF. * jmh 990915: keep relative position beyond EOL. */int  entab_adjust_rcol( text_ptr s, int len, int rcol, int tab_size ){register int col;register int last_col;   assert( len >= 0 || len == EOF );   assert( len < MAX_LINE_LENGTH );   assert( rcol >= EOF );   assert( rcol < MAX_LINE_LENGTH );   assert( tab_size != 0 );   if (len > 0 && rcol > 0) {      last_col = col = 0;      do {         if (*s++ != '\t')            ++col;         else {            col += tab_size - (col % tab_size);            if (col > rcol)               break;         }         ++last_col;      } while (col < rcol  &&  --len != 0);      if (col < rcol)         last_col += rcol - col;   } else      last_col = rcol;   assert( last_col >= 0 );   assert( last_col < MAX_LINE_LENGTH );   return( last_col );}/* * Name:    block_expand_tabs * Purpose: Expand tabs in a marked block. * Date:    June 5, 1991 * Passed:  window:  pointer to current window * Notes:   Tabs are expanded using the current tab interval. *          Lines are checked to make sure they are not too long. * * jmh 991202: return ERROR if no block. */int  block_expand_tabs( TDE_WIN *window ){int  len;int  tab_size;int  dirty;register int spaces;line_list_ptr p;                /* pointer to block line */file_infos *file;TDE_WIN *sw, s_w;long er;int  i;int  rc;text_ptr b, t;   /*    * make sure block is marked OK and that this is a LINE block    */   rc = un_copy_line( window->ll, window, TRUE, TRUE );   check_block( &sw );   file = g_status.marked_file;   if (rc == ERROR  ||  g_status.marked == FALSE  ||  file->read_only)      return( ERROR );   if (file->block_type != LINE) {      /*       * can only expand tabs in line blocks       */      error( WARNING, window->bottom_line, block20 );      return( ERROR );   }   /*    * initialize everything    */   dirty = FALSE;   tab_size = file->ptab_size;   dup_window_info( &s_w, sw );   p  = file->block_start;   er = file->block_er;   s_w.rline = file->block_br;   s_w.visible = FALSE;   for (; s_w.rline <= er  &&  !g_status.control_break  &&  rc == OK;        s_w.rline++) {      /*       * use the line buffer to expand LINE blocks.       */      t = memchr( p->line, '\t', p->len );      if (t != NULL) {         g_status.line_buff_len = (int)(t - p->line);         memcpy( g_status.line_buff, p->line, g_status.line_buff_len );         b = g_status.line_buff + g_status.line_buff_len;         len = p->len - g_status.line_buff_len;         for (i = g_status.line_buff_len + 1; len > 0  &&  rc == OK;              t++, b++, len--) {            /*             * each line in the LINE block is copied to the g_status.line_buff.             *  look at the text in the buffer and expand tabs.             */            if (*t == '\t') {               spaces = i % tab_size;               if (spaces)                  spaces = tab_size - spaces;               memset( b, ' ', spaces+1 );               b += spaces;               i += spaces;               g_status.line_buff_len += spaces;               assert( g_status.line_buff_len < MAX_LINE_LENGTH );            } else {               *b = *t;            }            ++i;            ++g_status.line_buff_len;         }         g_status.copied = TRUE;         rc = un_copy_line( p, &s_w, TRUE, FALSE );         dirty = TRUE;      }      p = p->next;   }   /*    * IMPORTANT:  we need to reset the copied flag because the cursor may    * not necessarily be on the last line of the block.    */   g_status.copied = FALSE;   if (dirty)      file->dirty = GLOBAL;   return( rc );}/* * Name:    block_compress_tabs * Purpose: Compress tabs in a marked block. * Date:    October 31, 1992 * Passed:  window:  pointer to current window * Notes:   Tabs are compressed using the current tab setting. */int  block_compress_tabs( TDE_WIN *window ){int  rc;int  tab_size;int  dirty;line_list_ptr p;                /* pointer to block line */file_infos *file;TDE_WIN *sw, s_w;long er;int  indent_only;   /*    * make sure block is marked OK and that this is a LINE block    */   rc = un_copy_line( window->ll, window, TRUE, TRUE );   check_block( &sw );   file = g_status.marked_file;   if (rc == ERROR  ||  g_status.marked == FALSE  ||  file->read_only)      return( ERROR );   if (file->block_type != LINE) {      /*       * can only compress tabs in line blocks       */      error( WARNING, window->bottom_line, block20a );      return( ERROR );   }   /*    * initialize everything    */   indent_only = (g_status.command == BlockIndentTabs);   dirty = FALSE;   tab_size = file->ptab_size;   dup_window_info( &s_w, sw );   s_w.visible = FALSE;   p  = file->block_start;   er = file->block_er;   s_w.rline = file->block_br;   for (; s_w.rline <= er  &&  !g_status.control_break  &&  rc == OK;        s_w.rline++) {      /*       * if any tabs were found, write g_status.line_buff to file.       */      if (entab( p->line, p->len, tab_size, indent_only )) {         g_status.copied = TRUE;         rc = un_copy_line( p, &s_w, TRUE, FALSE );         dirty = TRUE;      }      p = p->next;   }   /*    * IMPORTANT:  we need to reset the copied flag because the cursor may    * not necessarily be on the last line of the block.    */   g_status.copied = FALSE;   if (dirty)      file->dirty = GLOBAL;   return( rc );}

⌨️ 快捷键说明

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