home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
new
/
util
/
edit
/
jade
/
src
/
edit.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-10-04
|
19KB
|
783 lines
/* edit.c -- Editing buffers
Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
This file is part of Jade.
Jade is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Jade is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Jade; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "jade.h"
#include "jade_protos.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef NEED_MEMORY_H
# include <memory.h>
#endif
_PR bool clear_line_list(TX *);
_PR void kill_line_list(TX *);
_PR LINE *resize_line_list(TX *, long, long);
_PR bool insert_gap(TX *, long, const POS *);
_PR bool insert_str(TX *, const u_char *, POS *);
_PR bool insert_str_n(TX *, const u_char *, long, POS *);
_PR bool insert_string(TX *, const u_char *, long, POS *);
_PR bool delete_chars(TX *, const POS *, long);
_PR bool delete_lines(TX *, long, long);
_PR bool delete_section(TX *, POS *, POS *);
_PR bool split_line(TX *, POS *);
static bool join_lines(TX *, POS *);
_PR bool pad_pos(TX *, POS *);
_PR bool pad_cursor(VW *);
_PR void order_pos(POS *, POS *);
_PR bool check_section(TX *, POS *, POS *);
_PR void check_pos(TX *, POS *);
_PR bool check_line(TX *, POS *);
_PR long section_length(TX *, POS *, POS *);
_PR void copy_section(TX *, POS *, POS *, u_char *);
_PR bool pos_in_block(VW *, long, long);
_PR bool cursor_in_block(VW *);
_PR bool page_in_block(VW *);
_PR short line_in_block(VW *, long);
_PR void order_block(VW *);
_PR void set_block_refresh(VW *);
_PR bool read_only(TX *);
#define ALLOC_LL(n) mymalloc(sizeof(LINE) * (n))
#define FREE_LL(l) myfree(l)
/*
* This copies a line list (or part of one).
* d = destination
* s = source
* n = number of LINE's to copy.
*/
#define MOV_LL(d,s,n) memcpy((d), (s), (n) * sizeof(LINE))
/*
* void
* MOV_LL(LINE *dst, LINE *src, long number)
* {
* memcpy(dst, src, number * sizeof(LINE));
* }
*/
/*
* Makes file empty (null string in first line)
*/
bool
clear_line_list(TX *tx)
{
if(tx->tx_Lines)
kill_line_list(tx);
tx->tx_Lines = ALLOC_LL(1);
if(tx->tx_Lines)
{
tx->tx_Lines[0].ln_Line = str_dupn("", 0);
if(tx->tx_Lines[0].ln_Line)
tx->tx_Lines[0].ln_Strlen = 1;
else
tx->tx_Lines[0].ln_Strlen = 0;
tx->tx_NumLines = 1;
return(TRUE);
}
return(FALSE);
}
/*
* deallocates all lines and their list
*/
void
kill_line_list(TX *tx)
{
if(tx->tx_Lines)
{
LINE *line;
long i;
for(i = 0, line = tx->tx_Lines; i < tx->tx_NumLines; i++, line++)
{
if(line->ln_Strlen)
str_free(line->ln_Line);
}
FREE_LL(tx->tx_Lines);
tx->tx_Lines = NULL;
tx->tx_NumLines = 0;
}
}
/*
* deallocates some lines (but not the list)
*/
static void
kill_some_lines(TX *tx, long start, long number)
{
LINE *line = tx->tx_Lines + start;
long i;
for(i = 0; i < number; i++, line++)
{
if(line->ln_Strlen)
{
str_free(line->ln_Line);
line->ln_Strlen = 0;
line->ln_Line = NULL;
}
}
}
/*
* Creates blank entries or removes existing lines starting from line 'where'
* 'change' is the number of lines to insert, negative numbers mean delete
* that number of lines starting at the cursor line.
* If lines are deleted the actual text is also deleted.
* NOTE: You can't have a line list of zero lines.
*/
LINE *
resize_line_list(TX *tx, long change, long where)
{
LINE *newlines;
long newsize = tx->tx_NumLines + change;
if((newsize > 0) && (newlines = ALLOC_LL(newsize)))
{
if(tx->tx_Lines)
{
if(change > 0)
{
MOV_LL(newlines, tx->tx_Lines, where);
MOV_LL(newlines + where + change, tx->tx_Lines + where, tx->tx_NumLines - where);
}
else
{
MOV_LL(newlines, tx->tx_Lines, where);
MOV_LL(newlines + where, tx->tx_Lines + where - change, tx->tx_NumLines - where + change);
kill_some_lines(tx, where, -change);
}
FREE_LL(tx->tx_Lines);
}
if(change > 0)
bzero(newlines + where, change * sizeof(LINE));
tx->tx_Lines = newlines;
tx->tx_NumLines = newsize;
return(newlines);
}
return(FALSE);
}
#if 0
/*
* Pastes a line into the current view at line num.
* a LINE should have been made if it is wanted.
* text is not copied, it should have been mymalloc()'ed (or savestring()'ed)
*/
bool
stuffline(TX *tx, u_char *text, long lineNum)
{
LINE *line = tx->tx_Lines + lineNum;
if(line->ln_Strlen)
str_free(line->ln_Line);
line->ln_Strlen = strlen(text) + 1;
line->ln_Line = text;
return(TRUE);
}
#endif
/*
* Inserts some `space' at pos. The gap will be filled with random garbage.
* pos is *not* altered.
*/
bool
insert_gap(TX *tx, long len, const POS *pos)
{
LINE *line = tx->tx_Lines + pos->pos_Line;
u_char *newline = str_alloc(len + line->ln_Strlen);
if(newline)
{
if(line->ln_Strlen)
{
memcpy(newline, line->ln_Line, pos->pos_Col);
memcpy(newline + pos->pos_Col + len, line->ln_Line + pos->pos_Col,
line->ln_Strlen - pos->pos_Col);
str_free(line->ln_Line);
}
else
newline[len] = 0;
line->ln_Line = newline;
line->ln_Strlen += len;
adjust_marks_add_x(tx, len, pos->pos_Col, pos->pos_Line);
return(TRUE);
}
mem_error();
return(FALSE);
}
/*
* Inserts a string into the current line at the cursor pos
*
* IMPORTANT: For any of the next functions which insert text the pos
* argument must not be one which will be fiddled with by the keeppos*()
* functions (specifically a direct pointer to vw_CursorPos).
*/
bool
insert_str(TX *tx, const u_char *text, POS *pos)
{
return(insert_str_n(tx, text, strlen(text), pos));
}
bool
insert_str_n(TX *tx, const u_char *text, long textLen, POS *pos)
{
LINE *line = tx->tx_Lines + pos->pos_Line;
if(insert_gap(tx, textLen, pos))
{
memcpy(line->ln_Line + pos->pos_Col, text, textLen);
pos->pos_Col += textLen;
return(TRUE);
}
return(FALSE);
}
/*
* Inserts a null teminated string, this routine acts on any '\n' characters
* that it finds. I expect that this routine will be incredibly slow.
*/
bool
insert_string(TX *tx, const u_char *text, long textLen, POS *pos)
{
const u_char *eol;
POS orig = *pos;
while((eol = memchr(text, '\n', textLen)))
{
long len = eol - text;
if(pos->pos_Col)
{
if(len > 0)
{
if(!insert_str_n(tx, text, len, pos))
goto abort;
}
if(!split_line(tx, pos))
goto abort;
pos->pos_Col = 0;
}
else
{
u_char *copy;
if(!resize_line_list(tx, +1, pos->pos_Line))
goto abort;
if(!(copy = str_dupn(text, len)))
goto abort;
tx->tx_Lines[pos->pos_Line].ln_Strlen = len + 1;
tx->tx_Lines[pos->pos_Line].ln_Line = copy;
adjust_marks_add_y(tx, +1, pos->pos_Line);
}
textLen -= len + 1;
text = eol + 1;
pos->pos_Line++;
}
if(textLen > 0)
{
if(!insert_str_n(tx, text, textLen, pos))
{
abort:
return(FALSE);
}
}
undo_record_insertion(tx, &orig, pos);
flag_insertion(tx, &orig, pos);
return(TRUE);
}
/*
* Deletes some text (this line only)
*/
bool
delete_chars(TX *tx, const POS *pos, long size)
{
LINE *line = tx->tx_Lines + pos->pos_Line;
if(line->ln_Strlen)
{
u_char *newline;
if(size >= line->ln_Strlen)
size = line->ln_Strlen - 1;
newline = str_alloc(line->ln_Strlen - size);
if(newline)
{
#if 1
memcpy(newline, line->ln_Line, pos->pos_Col);
memcpy(newline + pos->pos_Col, line->ln_Line + pos->pos_Col + size,
line->ln_Strlen - pos->pos_Col - size);
#else
strncpy(newline, line->ln_Line, pos->pos_Col);
strcpy(newline + pos->pos_Col, line->ln_Line + pos->pos_Col + size);
#endif
str_free(line->ln_Line);
line->ln_Strlen -= size;
line->ln_Line = newline;
ad