📄 slsmg.c
字号:
/* SLang Screen management routines */
/* Copyright (c) 1992, 1995 John E. Davis
* All rights reserved.
*
* You may distribute under the terms of either the GNU General Public
* License or the Perl Artistic License.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "slang.h"
#include "_slang.h"
typedef struct Screen_Type
{
int n; /* number of chars written last time */
int flags; /* line untouched, etc... */
unsigned short *old, *neew;
#ifndef pc_system
unsigned long old_hash, new_hash;
#endif
}
Screen_Type;
#define TOUCHED 0x1
#define TRASHED 0x2
#ifndef pc_system
#define MAX_SCREEN_SIZE 120
#else
#define MAX_SCREEN_SIZE 75
#endif
Screen_Type SL_Screen[MAX_SCREEN_SIZE];
static int Start_Col, Start_Row;
static int Screen_Cols, Screen_Rows;
static int This_Row, This_Col;
static int This_Color; /* only the first 8 bits of this
* are used. The highest bit is used
* to indicate an alternate character
* set. This leaves 127 userdefineable
* color combination.
*/
#ifndef pc_system
#define ALT_CHAR_FLAG 0x80
#else
#define ALT_CHAR_FLAG 0x00
#endif
int SLsmg_Newline_Moves = 0;
int SLsmg_Backspace_Moves = 0;
static void blank_line (unsigned short *p, int n, unsigned char ch)
{
register unsigned short *pmax = p + n;
register unsigned short color_ch;
color_ch = (This_Color << 8) | (unsigned short) ch;
while (p < pmax)
{
*p++ = color_ch;
}
}
static void clear_region (int row, int n)
{
int i;
int imax = row + n;
if (imax > Screen_Rows) imax = Screen_Rows;
for (i = row; i < imax; i++)
{
if (i >= 0)
{
blank_line (SL_Screen[i].neew, Screen_Cols, ' ');
SL_Screen[i].flags |= TOUCHED;
}
}
}
void SLsmg_erase_eol (void)
{
int r, c;
c = This_Col - Start_Col;
r = This_Row - Start_Row;
if ((r < 0) || (r >= Screen_Rows)) return;
if (c < 0) c = 0; else if (c >= Screen_Cols) return;
blank_line (SL_Screen[This_Row].neew + c , Screen_Cols - c, ' ');
SL_Screen[This_Row].flags |= TOUCHED;
}
static void scroll_up (void)
{
unsigned int i, imax;
unsigned short *neew;
neew = SL_Screen[0].neew;
imax = Screen_Rows - 1;
for (i = 0; i < imax; i++)
{
SL_Screen[i].neew = SL_Screen[i + 1].neew;
SL_Screen[i].flags |= TOUCHED;
}
SL_Screen[i].neew = neew;
SL_Screen[i].flags |= TOUCHED;
blank_line (neew, Screen_Cols, ' ');
This_Row--;
}
void SLsmg_gotorc (int r, int c)
{
This_Row = r;
This_Col = c;
}
int SLsmg_get_row (void)
{
return This_Row;
}
int SLsmg_get_column (void)
{
return This_Col;
}
void SLsmg_erase_eos (void)
{
SLsmg_erase_eol ();
clear_region (This_Row + 1, Screen_Rows);
}
static int This_Alt_Char;
#ifndef pc_system
void SLsmg_set_char_set (int i)
{
if (SLtt_Use_Blink_For_ACS) return; /* alt chars not used and the alt bit
* is used to indicate a blink.
*/
if (i) This_Alt_Char = ALT_CHAR_FLAG;
else This_Alt_Char = 0;
This_Color &= 0x7F;
This_Color |= This_Alt_Char;
}
#endif
void SLsmg_set_color (int color)
{
if (color < 0) return;
This_Color = color | This_Alt_Char;
}
void SLsmg_reverse_video (void)
{
SLsmg_set_color (1);
}
void SLsmg_normal_video (void)
{
This_Color = This_Alt_Char; /* reset video but NOT char set. */
}
static int point_visible (int col_too)
{
return ((This_Row >= Start_Row) && (This_Row < Start_Row + Screen_Rows)
&& ((col_too == 0)
|| ((This_Col >= Start_Col)
&& (This_Col < Start_Col + Screen_Cols))));
}
void SLsmg_printf (char *fmt, ...)
{
char p[1000];
va_list ap;
va_start(ap, fmt);
(void) vsprintf(p, fmt, ap);
va_end(ap);
SLsmg_write_string (p);
}
void SLsmg_write_string (char *str)
{
SLsmg_write_nchars (str, strlen (str));
}
void SLsmg_write_nstring (char *str, int n)
{
int width;
char blank = ' ';
if (str == NULL) width = 0;
else
{
width = strlen (str);
if (width > n) width = n;
SLsmg_write_nchars (str, width);
}
while (width++ < n) SLsmg_write_nchars (&blank, 1);
}
void SLsmg_write_wrapped_string (char *s, int r, int c, int dr, int dc, int fill)
{
register char ch, *p;
int maxc = dc;
if ((dr == 0) || (dc == 0)) return;
p = s;
dc = 0;
while (1)
{
ch = *p++;
if ((ch == 0) || (ch == '\n'))
{
int diff;
diff = maxc - dc;
SLsmg_gotorc (r, c);
SLsmg_write_nchars (s, dc);
if (fill && (diff > 0))
{
while (diff--) SLsmg_write_char (' ');
}
if ((ch == 0) || (dr == 1)) break;
r++;
dc = 0;
dr--;
s = p;
}
else if (dc == maxc)
{
SLsmg_gotorc (r, c);
SLsmg_write_nchars (s, dc + 1);
if (dr == 1) break;
r++;
dc = 0;
dr--;
s = p;
}
else dc++;
}
}
int SLsmg_Tab_Width = 8;
/* Minimum value for which eight bit char is displayed as is. */
#ifndef pc_system
int SLsmg_Display_Eight_Bit = 160;
static unsigned char Alt_Char_Set[129];/* 129th is used as a flag */
#else
int SLsmg_Display_Eight_Bit = 128;
#endif
void SLsmg_write_nchars (char *str, int n)
{
register unsigned short *p, old, neew, color;
unsigned char ch;
unsigned int flags;
int len, start_len, max_len;
char *str_max;
int newline_flag;
#ifndef pc_system
int alt_char_set_flag;
alt_char_set_flag = ((SLtt_Use_Blink_For_ACS == 0)
&& (This_Color & ALT_CHAR_FLAG));
#endif
str_max = str + n;
color = This_Color << 8;
top: /* get here only on newline */
newline_flag = 0;
start_len = Start_Col;
if (point_visible (0) == 0) return;
len = This_Col;
max_len = start_len + Screen_Cols;
p = SL_Screen[This_Row].neew;
if (len > start_len) p += (len - start_len);
flags = SL_Screen[This_Row].flags;
while ((len < max_len) && (str < str_max))
{
ch = (unsigned char) *str++;
#ifndef pc_system
if (alt_char_set_flag)
ch = Alt_Char_Set [ch & 0x7F];
#endif
if (((ch >= ' ') && (ch < 127))
|| (ch >= (unsigned char) SLsmg_Display_Eight_Bit)
#ifndef pc_system
|| alt_char_set_flag
#endif
)
{
len += 1;
if (len > start_len)
{
old = *p;
neew = color | (unsigned short) ch;
if (old != neew)
{
flags |= TOUCHED;
*p = neew;
}
p++;
}
}
else if ((ch == '\t') && (SLsmg_Tab_Width > 0))
{
n = len;
n += SLsmg_Tab_Width;
n = SLsmg_Tab_Width - (n % SLsmg_Tab_Width);
if (len + n > max_len) n = max_len - len;
neew = color | (unsigned short) ' ';
while (n--)
{
len += 1;
if (len > start_len)
{
if (*p != neew)
{
flags |= TOUCHED;
*p = neew;
}
p++;
}
}
}
else if (ch == '\n')
{
newline_flag = 1;
break;
}
else if ((ch == 0x8) && SLsmg_Backspace_Moves)
{
if (len != 0) len--;
}
else
{
if (ch & 0x80)
{
neew = color | (unsigned short) '~';
len += 1;
if (len > start_len)
{
if (*p != neew)
{
*p = neew;
flags |= TOUCHED;
}
p++;
if (len == max_len) break;
ch &= 0x7F;
}
}
len += 1;
if (len > start_len)
{
neew = color | (unsigned short) '^';
if (*p != neew)
{
*p = neew;
flags |= TOUCHED;
}
p++;
if (len == max_len) break;
}
if (ch == 127) ch = '?'; else ch = ch + '@';
len++;
if (len > start_len)
{
neew = color | (unsigned short) ch;
if (*p != neew)
{
*p = neew;
flags |= TOUCHED;
}
p++;
}
}
}
SL_Screen[This_Row].flags = flags;
This_Col = len;
if (SLsmg_Newline_Moves == 0)
return;
if (newline_flag == 0)
{
while (str < str_max)
{
if (*str == '\n') break;
str++;
}
if (str == str_max) return;
str++;
}
This_Row++;
This_Col = 0;
if (This_Row == Start_Row + Screen_Rows)
{
if (SLsmg_Newline_Moves > 0) scroll_up ();
}
goto top;
}
void SLsmg_write_char (char ch)
{
SLsmg_write_nchars (&ch, 1);
}
static int Cls_Flag;
void SLsmg_cls (void)
{
This_Color = 0;
clear_region (0, Screen_Rows);
This_Color = This_Alt_Char;
Cls_Flag = 1;
}
#if 0
static void do_copy (unsigned short *a, unsigned short *b)
{
unsigned short *amax = a + Screen_Cols;
while (a < amax) *a++ = *b++;
}
#endif
#ifndef pc_system
int SLsmg_Scroll_Hash_Border = 0;
static unsigned long compute_hash (unsigned short *s, int n)
{
register unsigned long h = 0, g;
register unsigned long sum = 0;
register unsigned short *smax, ch;
int is_blank = 2;
s += SLsmg_Scroll_Hash_Border;
smax = s + (n - SLsmg_Scroll_Hash_Border);
while (s < smax)
{
ch = *s++;
if (is_blank && ((ch & 0xFF) != 32)) is_blank--;
sum += ch;
h = sum + (h << 3);
if ((g = h & 0xE0000000UL) != 0)
{
h = h ^ (g >> 24);
h = h ^ g;
}
}
if (is_blank) return 0;
return h;
}
static unsigned long Blank_Hash;
static void try_scroll (void)
{
int i, j, di, r1, r2, rmin, rmax;
unsigned long hash;
int color, did_scroll = 0;
unsigned short *tmp;
int ignore;
/* find region limits. */
for (rmax = Screen_Rows - 1; rmax > 0; rmax--)
{
if (SL_Screen[rmax].new_hash != SL_Screen[rmax].old_hash)
{
r1 = rmax - 1;
if ((r1 == 0)
|| (SL_Screen[r1].new_hash != SL_Screen[r1].old_hash))
break;
rmax = r1;
}
}
for (rmin = 0; rmin < rmax; rmin++)
{
if (SL_Screen[rmin].new_hash != SL_Screen[rmin].old_hash)
{
r1 = rmin + 1;
if ((r1 == rmax)
|| (SL_Screen[r1].new_hash != SL_Screen[r1].old_hash))
break;
rmin = r1;
}
}
for (i = rmax; i > rmin; i--)
{
hash = SL_Screen[i].new_hash;
if (hash == Blank_Hash) continue;
if ((hash == SL_Screen[i].old_hash)
|| ((i + 1 < Screen_Rows) && (hash == SL_Screen[i + 1].old_hash))
|| ((i - 1 > rmin) && (SL_Screen[i].old_hash == SL_Screen[i - 1].new_hash)))
continue;
for (j = i - 1; j >= rmin; j--)
{
if (hash == SL_Screen[j].old_hash) break;
}
if (j < rmin) continue;
r2 = i; /* end scroll region */
di = i - j;
j--;
ignore = 0;
while ((j >= rmin) && (SL_Screen[j].old_hash == SL_Screen[j + di].new_hash))
{
if (SL_Screen[j].old_hash == Blank_Hash) ignore++;
j--;
}
r1 = j + 1;
/* If this scroll only scrolls this line into place, don't do it.
*/
if ((di > 1) && (r1 + di + ignore == r2)) continue;
/* If there is anything in the scrolling region that is ok, abort the
* scroll.
*/
for (j = r1; j <= r2; j++)
{
if ((SL_Screen[j].old_hash != Blank_Hash)
&& (SL_Screen[j].old_hash == SL_Screen[j].new_hash))
{
/* See if the scroll is happens to scroll this one into place. */
if ((j + di > r2) || (SL_Screen[j].old_hash != SL_Screen[j + di].new_hash))
break;
}
}
if (j <= r2) continue;
color = This_Color; This_Color = 0;
did_scroll = 1;
SLtt_normal_video ();
SLtt_set_scroll_region (r1, r2);
SLtt_goto_rc (0, 0);
SLtt_reverse_index (di);
SLtt_reset_scroll_region ();
/* Now we have a hole in the screen. Make the virtual screen look
* like it.
*/
for (j = r1; j <= r2; j++) SL_Screen[j].flags = TOUCHED;
while (di--)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -