📄 block.c
字号:
dup_window_info( &s_w, source_window );
dup_window_info( &d_w, window );
s_w.rline = br;
s_w.ll = source;
s_w.visible = FALSE;
d_w.rline = rline;
d_w.ll = dest;
d_w.visible = FALSE;
block_buff = (char *)calloc( BUFF_SIZE + 2, sizeof(char) );
swap_buff = (char *)calloc( BUFF_SIZE + 2, sizeof(char) );
if (block_buff == NULL || swap_buff == NULL) {
error( WARNING, window->bottom_line, block4 );
*rc = ERROR;
}
if (*rc == OK && (action == OVERLAY || action == SWAP) &&
same && rline > br && rline <= er) {
dest_add = rline - br;
if (dest_add + er > window->file_info->length) {
dest_add = dest_add - (window->file_info->length - er);
p = dest_file->line_list_end->prev;
for (; dest_add > 0 && *rc == OK; dest_add--)
*rc = pad_dest_line( window, dest_file, p );
padded_file = TRUE;
}
for (li=er-br; li > 0; li--) {
load_undo_buffer( dest_file, dest->line, dest->len );
dest = dest->next;
++d_w.rline;
source = source->next;
++s_w.rline;
}
for (li=er; *rc == OK && li >= br && !g_status.control_break;
li--, s_w.rline--, d_w.rline--) {
lens = find_end( source->line, source->len );
lend = find_end( dest->line, dest->len );
if (lens != 0 || lend != 0) {
load_box_buff( block_buff, source, bc, ec, ' ' );
if (action == SWAP)
load_box_buff( swap_buff, dest, rcol, rcol+block_len, ' ' );
*rc = copy_buff_2file( &d_w, block_buff, dest, rcol,
block_len, action );
dest->dirty = TRUE;
if (action == SWAP) {
add = 0;
*rc = copy_buff_2file( &s_w, swap_buff, source, bc,
block_len, action );
source->dirty = TRUE;
}
}
source = source->prev;
dest = dest->prev;
}
} else {
if (action == FILL)
block_fill( block_buff, fill_char, block_len );
for (li=br; *rc == OK && li <= er && !g_status.control_break;
li++, s_w.rline++, d_w.rline++) {
lens = find_end( source->line, source->len );
lend = find_end( dest->line, dest->len );
switch (action) {
case FILL :
case NUMBER :
case DELETE :
case MOVE :
load_undo_buffer( source_file, source->line, source->len );
break;
case COPY :
case KOPY :
case OVERLAY :
load_undo_buffer( dest_file, dest->line, dest->len );
break;
}
if (action == FILL || action == NUMBER) {
if (action == NUMBER) {
number_block_buff( block_buff, block_len, block_num, block_just );
block_num += block_inc;
}
*rc = copy_buff_2file( &s_w, block_buff, source, rcol,
block_len, action );
source->dirty = TRUE;
} else if (lens != 0 || lend != 0) {
if (action == MOVE || action == COPY || action == KOPY ||
action == OVERLAY || action == SWAP) {
xbc = bc;
xec = ec;
if (action != OVERLAY && action != SWAP && same) {
if (rcol < bc && rline > br && rline <=er)
if (li >= rline) {
xbc = bc + block_len;
xec = ec + block_len;
}
}
load_box_buff( block_buff, source, xbc, xec, ' ' );
if (action == SWAP)
load_box_buff( swap_buff, dest, rcol, rcol+block_len, ' ' );
*rc = copy_buff_2file( &d_w, block_buff, dest, rcol,
block_len, action );
dest->dirty = TRUE;
if (action == SWAP && *rc == OK) {
*rc = copy_buff_2file( &s_w, swap_buff, source, xbc,
block_len, action );
source->dirty = TRUE;
}
}
if (action == MOVE || action == DELETE) {
lens = find_end( source->line, source->len );
if (lens >= (bc + 1)) {
source->dirty = TRUE;
add = block_len;
xbc = bc;
if (lens <= (ec + 1))
add = lens - bc;
if (same && action == MOVE) {
if (rcol < bc && rline >= br && rline <=er)
if (li >= rline) {
xbc = bc + block_len;
if (lens <= (ec + block_len + 1))
add = lens - xbc;
}
}
if (add > 0)
*rc = delete_box_block( &s_w, source, xbc, add );
}
}
}
source = source->next;
if (action != DELETE && action != FILL && action != NUMBER) {
p = dest->next;
if (p->len != EOF)
dest = p;
else if (li < er) {
padded_file = TRUE;
pad_dest_line( window, dest_file, p );
dest = dest->next;
}
}
}
}
if (block_buff != NULL)
free( block_buff );
if (swap_buff != NULL)
free( swap_buff );
if (padded_file) {
w = g_status.window_list;
while (w != NULL) {
if (w->file_info == dest_file && w->visible)
show_size( w );
w = w->next;
}
}
show_avail_mem( );
}
void load_box_buff( char *block_buff, line_list_ptr ll, int bc, int ec,
char filler )
{
int len;
int avlen;
register int i;
register char *bb;
text_ptr s;
assert( bc >= 0 );
assert( ec >= bc );
assert( ec < MAX_LINE_LENGTH );
bb = block_buff;
len = ll->len;
s = detab_a_line( ll->line, &len );
if (len < ec + 1) {
assert( ec + 1 - bc >= 0 );
memset( block_buff, filler, (ec + 1) - bc );
if (len >= bc) {
avlen = len - bc;
s += bc;
for (i=avlen; i>0; i--)
*bb++ = *s++;
}
} else {
avlen = (ec + 1) - bc;
s = s + bc;
for (i=avlen; i>0; i--)
*bb++ = *s++;
}
}
int copy_buff_2file( WINDOW *window, char *block_buff, line_list_ptr dest,
int rcol, int block_len, int action )
{
char *s;
char *d;
int len;
int pad;
int add;
copy_line( dest );
if (mode.inflate_tabs)
detab_linebuff( );
len = g_status.line_buff_len;
assert( len >= 0 );
assert( len < MAX_LINE_LENGTH );
assert( rcol >= 0 );
assert( rcol < MAX_LINE_LENGTH );
assert( block_len >= 0 );
assert( block_len < BUFF_SIZE );
if (rcol > len) {
pad = rcol - len;
assert( pad >= 0 );
assert( pad < MAX_LINE_LENGTH );
memset( g_status.line_buff + len, ' ', pad );
len += pad;
}
s = g_status.line_buff + rcol;
if (action == FILL || action == OVERLAY || action == NUMBER || action == SWAP) {
add = len - rcol;
if (add < block_len) {
pad = block_len - add;
assert( pad >= 0 );
assert( pad < MAX_LINE_LENGTH );
memset( g_status.line_buff + len, ' ', pad );
len += pad;
}
} else {
d = s + block_len;
add = len - rcol;
assert( add >= 0 );
assert( add < MAX_LINE_LENGTH );
memmove( d, s, add );
len += block_len;
}
assert( rcol + block_len <= len );
assert( len >= 0 );
assert( len < MAX_LINE_LENGTH );
memmove( s, block_buff, block_len );
g_status.line_buff_len = len;
if (mode.inflate_tabs)
entab_linebuff( );
return( un_copy_line( dest, window, TRUE ) );
}
void block_fill( char *block_buff, int fill_char, int block_len )
{
assert( block_len >= 0 );
assert( block_len < BUFF_SIZE );
assert( block_buff != NULL );
memset( block_buff, fill_char, block_len );
}
void number_block_buff( char *block_buff, int block_len, long block_num,
int just )
{
int len;
int i;
char temp[MAX_COLS];
assert( block_len >= 0 );
assert( block_len < BUFF_SIZE );
block_fill( block_buff, ' ', block_len );
len = strlen( ltoa( block_num, temp, 10 ) );
if (just == RIGHT) {
block_len--;
len--;
for (;block_len >= 0 && len >= 0; block_len--, len--)
block_buff[block_len] = temp[len];
} else {
for (i=0; block_len > 0 && i < len; block_len--, i++)
block_buff[i] = temp[i];
}
}
void restore_cursors( file_infos *file )
{
register WINDOW *window;
line_list_ptr ll;
long n;
assert( file != NULL );
window = g_status.window_list;
while (window != NULL) {
if (window->file_info == file) {
window->bin_offset = 0;
if (window->rline < 1L)
window->rline = 1L;
if (window->rline > file->length)
window->rline = file->length;
ll = file->line_list;
n = 1L;
for (; n < window->rline; n++) {
window->bin_offset += ll->len;
ll = ll->next;
}
window->ll = ll;
if (window->rline < (window->cline - (window->top_line+window->ruler-1)))
window->cline = (int)window->rline + window->top_line+window->ruler-1;
if (window->cline < window->top_line + window->ruler)
window->cline = window->top_line + window->ruler;
if (window->visible)
show_size( window );
}
window = window->next;
}
}
int delete_box_block( WINDOW *s_w, line_list_ptr source, int bc, int add )
{
char *s;
int number;
assert( s_w != NULL );
assert( source != NULL );
assert( bc >= 0 );
assert( bc < MAX_LINE_LENGTH );
assert( add >= 0 );
assert( add < MAX_LINE_LENGTH );
copy_line( source );
detab_linebuff( );
number = g_status.line_buff_len - bc;
s = g_status.line_buff + bc + add;
assert( number >= 0 );
assert( number < MAX_LINE_LENGTH );
assert( bc + add >= 0 );
assert( bc + add < MAX_LINE_LENGTH );
assert( add <= g_status.line_buff_len );
memmove( s - add, s, number );
g_status.line_buff_len -= add;
entab_linebuff( );
return( un_copy_line( source, s_w, TRUE ) );
}
void check_block( void )
{
register file_infos *file;
WINDOW filler;
file = g_status.marked_file;
if (file == NULL || file->block_br > file->length)
unmark_block( &filler );
else {
if (file->block_er > file->length)
file->block_er = file->length;
find_begblock( file );
find_endblock( file );
}
}
void find_begblock( file_infos *file )
{
line_list_ptr ll;
long li;
assert( file != NULL );
assert( file->line_list != NULL );
ll = file->line_list;
for (li=1; li<file->block_br && ll->next != NULL; li++)
ll = ll->next;
file->block_start = ll;
}
void find_endblock( file_infos *file )
{
line_list_ptr ll;
long i;
register file_infos *fp;
assert( file != NULL );
assert( file->block_start != NULL );
fp = file;
ll = fp->block_start;
if (ll != NULL) {
for (i=fp->block_br; i < fp->block_er && ll->next != NULL; i++)
ll = ll->next;
if (ll != NULL)
fp->block_end = ll;
else {
fp->block_end = fp->line_list_end->prev;
fp->block_er = fp->length;
}
}
}
int block_write( WINDOW *window )
{
int prompt_line;
int rc;
char buff[MAX_COLS+2];
char line_buff[(MAX_COLS+1)*2];
file_infos *file;
int block_type;
int fattr;
entab_linebuff( );
rc = un_copy_line( window->ll, window, TRUE );
check_block( );
if (rc == OK && g_status.marked == TRUE) {
prompt_line = window->bottom_line;
file = g_status.marked_file;
assert( file != NULL );
block_type = file->block_type;
save_screen_line( 0, prompt_line, line_buff );
*g_status.rw_name = '\0';
if (get_name( block6, prompt_line, g_status.rw_name,
g_display.message_color ) == OK) {
rc = get_fattr( g_status.rw_name, &fattr );
if (rc == OK) {
set_prompt( block7, prompt_line );
switch (get_oa( )) {
case A_OVERWRITE :
change_mode( g_status.rw_name, prompt_line );
combine_strings( buff, block8, g_status.rw_name, "'" );
s_output( buff, prompt_line, 0, g_display.message_color );
rc = hw_save( g_status.rw_name, file, file->block_br,
file->block_er, block_type );
if (rc == ERROR)
error( WARNING, prompt_line, block9 );
break;
case A_APPEND :
combine_strings( buff, block10, g_status.rw_name, "'" );
s_output( buff, prompt_line, 0, g_display.message_color );
rc = hw_append( g_status.rw_name, file, file->block_br,
file->block_er, block_type );
if (rc == ERROR)
error( WARNING, prompt_line, block11 );
break;
case AbortCommand :
default :
rc = ERROR;
break;
}
} else if (rc != ERROR) {
combine_strings( buff, block12, g_status.rw_name, "'" );
s_output( buff, prompt_line, 0, g_display.message_color );
if (hw_save( g_status.rw_name, file, file->block_br, file->block_er,
block_type ) == ERROR) {
error( WARNING, prompt_line, block9 );
rc = ERROR;
}
}
}
restore_screen_line( 0, prompt_line, line_buff );
} else
rc = ERROR;
return( rc );
}
int block_print( WINDOW *window )
{
char answer[MAX_COLS];
char line_buff[(MAX_COLS+1)*2];
int col;
int func;
int prompt_line;
line_list_ptr block_start;
file_infos *file;
int block_type;
char *p;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -