home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
CSSRC
/
FNTED.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-10-19
|
9KB
|
408 lines
/*
fnted.c
% ted_funcs virtual field funcs for text editing, limited size
C-scape 3.2
Copyright (c) 1988, 1989 by Oakland Group, Inc.
ALL RIGHTS RESERVED.
Revision History:
-----------------
8/22/88 jdc Created
9/10/88 jmd Preened
9/12/88 jdc Preened even more
9/17/88 jmd added std_ funcs
9/22/88 jmd added ted_fenter
10/09/88 jmd added SED_ABORT support
10/14/88 jdc added var_size element to field_funcs_struct
11/05/88 jmd removed menu_Close
2/07/89 jmd added char * cast
4/19/89 jdc fixed checking for sed_GetTB() and sed_SetTB()
6/07/89 jmd added test for mouse code (later removed)
3/28/90 jmd ansi-fied
4/25/90 jmd the cut buffer is now stored in the field 2nd data pointer
6/13/90 jmd ted_sexit now resets the field data pointer (not the sed 's)
6/14/90 jdc now Sets, not Gets
6/26/90 jdc fixed cutbuf check for cut & copy
9/13/90 jmd made #defines for all text strings
10/19/90 mla added mouse support to pop_Search
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "cscape.h"
#include "scancode.h"
#include "teddecl.h"
OGLOBAL field_funcs_struct ted_funcs = {
ted_fenter,
std_fexit,
ted_fkey,
ted_senter,
ted_sexit,
VAR_TED
};
OSTATIC int pop_Search(boolean mode, char *srch, char *repl);
/* strings used by ted_funcs */
#define MSG_SEARCH "Search"
#define MSG_REPLACE "Replace"
#define MSG_SEARCHING "Searching..."
#define MSG_REPLACING "Replacing..."
#define MSG_NOTFOUND "Pattern not found."
#define MSG_REPLACEYNG "Replace? Yes, No, Global : "
#define MSG_ENDOFTEXT "Searching for end of text."
/* -------------------------------------------------------------------------- */
void ted_fenter(sed_type sed)
{
std_fenter(sed);
ted_StartWorking(sed);
/* the cut buffer gets created when needed */
}
static char search[51];
static char replace[51];
void ted_fkey(sed_type sed)
/*
*/
{
int scancode, key, rlen, row, col;
menu_type cutbuf;
scancode = kb_Read();
if (sed_DoSpecial(sed, scancode))
return;
if (special_key(sed, scancode))
return;
switch (scancode) {
case ESC:
/* leave editor */
sed_ToggleExit(sed);
sed_SetBaton(sed, 0);
return;
case ALT_M:
/* toggle row-wise mark mode */
if (ted_GetMark(sed) == TED_NOMARK) {
ted_SetMark(sed, TED_MARK);
}
else {
ted_SetMark(sed, TED_NOMARK);
}
break;
case ALT_C:
/* toggle column-wise mark mode */
if (ted_GetMark(sed) == TED_NOMARK) {
ted_SetMark(sed, TED_COLMARK);
}
else {
ted_SetMark(sed, TED_NOMARK);
}
break;
case GREYMINUS:
/* cut marked region */
/* fall through to copy case */
case GREYPLUS:
/* copy marked region */
/* create a cut buffer, attach it to the field second data pointer */
if ((cutbuf = (menu_type)sed_GetFieldData(sed, sed_GetFieldNo(sed), 1)) == NULL) {
cutbuf = menu_Open();
sed_SetFieldData(sed, sed_GetFieldNo(sed), 1, (VOID *)cutbuf);
}
if (cutbuf != NULL) {
if (scancode == GREYMINUS) {
ted_BlockCut(sed, cutbuf);
}
else {
ted_BlockCopy(sed, cutbuf);
}
}
ted_SetMark(sed, TED_NOMARK);
break;
case INS:
/* paste the cut buffer */
if ((cutbuf = (menu_type)sed_GetFieldData(sed, sed_GetFieldNo(sed), 1)) != NULL
&& !ted_BlockPaste(sed, cutbuf)) {
/* probably hit ted_MaxSize, signal user */
tone();
}
break;
case DEL:
if (ted_GetMark(sed) != TED_NOMARK) {
/* delete the marked region (if there is one) */
ted_BlockDelete(sed);
ted_SetMark(sed, TED_NOMARK);
}
else {
/* Otherwise, delete a character */
ted_DeleteChar(sed);
}
break;
case ALT_S:
/* Search for a string */
if (pop_Search(FALSE, search, replace)) {
sed_BorderPrompt(sed, MSG_SEARCHING);
if (ted_Search(sed, search, TED_FORWARD | TED_AFTER)) {
sed_BorderPrompt(sed, NULL);
}
else {
sed_BorderPrompt(sed, MSG_NOTFOUND);
}
}
break;
case ALT_R:
/* Search and replace string */
/* Step 1 */
if (pop_Search(TRUE, search, replace)) {
sed_BorderPrompt(sed, MSG_SEARCHING);
/* Step 2 */
ted_GetPosition(sed, &row, &col);
key = (int)'y';
/* get the length of the replace string only once */
rlen = strlen(replace);
/* Step 3 */
if (ted_Search(sed, search, TED_FORWARD) == FALSE) {
sed_BorderPrompt(sed, MSG_NOTFOUND);
break;
}
/* Step 4 */
while (ted_Search(sed, search, TED_FORWARD)) {
/* Step 5 */
ted_SetMark(sed, TED_MARK);
/* Step 6 */
ted_Search(sed, search, TED_FORWARD | TED_AFTER);
/* make sure cursor isn't past the end of the pattern */
ted_GotoCursor(sed, ted_GetCursor(sed) - 1L);
/* Step 7 */
if ((char)key != 'G' && (char)key != 'g') {
sed_BorderPrompt(sed, MSG_REPLACEYNG);
if ((key = kb_Read()) == ESC) {
/* Stop searching */
ted_SetMark(sed, TED_NOMARK);
break;
}
key = ascii(key);
if (key == 'G' || key == 'g') {
/* Turn off refresh while globally replacing */
ted_SetRefresh(sed, TED_NOREFRESH);
sed_BorderPrompt(sed, MSG_REPLACING);
}
}
if (key == 'N' || key == 'n') {
/* Skip this one, move past current pattern */
ted_SetMark(sed, TED_NOMARK);
ted_GotoCursor(sed, ted_GetCursor(sed) + 1L);
}
else {
/* Step 8 */
ted_BlockDelete(sed);
ted_AddString(sed, replace, rlen);
}
}
/* Step 10 */
sed_BorderPrompt(sed, NULL);
ted_SetRefresh(sed, TED_REFRESH);
if (key != ESC) {
ted_GotoPosition(sed, row, col);
}
sed_RepaintText(sed);
}
break;
case UP:
ted_UpChar(sed);
break;
case DOWN:
ted_DownChar(sed);
break;
case LEFT:
ted_LeftChar(sed, TED_TABJUMP);
break;
case RIGHT:
ted_RightChar(sed, TED_TABJUMP);
break;
case HOME:
ted_GoHome(sed);
break;
case END:
ted_GoEnd(sed);
break;
case PGUP:
ted_PageUp(sed);
break;
case PGDN:
ted_PageDown(sed);
break;
case CTRL_HOME:
case CTRL_PGUP:
ted_GoTop(sed);
break;
case CTRL_END:
case CTRL_PGDN:
sed_BorderPrompt(sed, MSG_ENDOFTEXT);
ted_GoBottom(sed);
sed_BorderPrompt(sed, NULL);
break;
case ENTER:
if (ted_GetInsert(sed) == TED_INSERT) {
if (!ted_AddRow(sed)) {
/* probably hit ted_MaxSize, signal user */
tone();
}
}
else {
ted_DownChar(sed);
ted_GoHome(sed);
}
break;
case BACKSPACE:
if (ted_LeftChar(sed, TED_TABJUMP)) {
ted_DeleteChar(sed);
}
break;
case TAB:
if (!ted_AddChar(sed, '\t')) {
/* probably hit ted_MaxSize, signal user */
tone();
}
break;
default:
key = ascii(scancode);
if (isprint(key) || ((unsigned char) (key)) > 127) {
if (!ted_AddChar(sed, (unsigned char) key)) {
/* probably hit ted_MaxSize, signal user */
tone();
}
}
break;
}
/* reset baton */
sed_SetBaton(sed, -1);
}
void ted_senter(sed_type sed, int fieldno)
/*
Copy the string into the ted.
*/
{
unsigned int len;
char *text;
/* if limiting is set and the var is not NULL then sed_SetTB() */
if (ted_GetMaxSize(sed) > 0L && (text = (char *) sed_GetVar(sed, fieldno)) != NULL) {
len = strlen(text);
sed_ClearTB(sed);
sed_SetTB(sed, text, len);
sed_RewindTB(sed);
}
ted_SetInsert(sed, TED_INSERT);
}
void ted_sexit(sed_type sed, int fieldno)
/*
Copy the ted into the string.
*/
{
int len, row, col;
char *text;
menu_type cutbuf;
/* if limiting is set and the var is not NULL then sed_GetTB() */
if (sed_GetBaton(sed) != SED_ABORT && ted_GetMaxSize(sed) > 0L
&& (text = (char *) sed_GetVar(sed, fieldno)) != NULL) {
ted_GetPosition(sed, &row, &col);
/* go to top of textbuf, don't show it on screen */
sed_RewindTB(sed);
len = sed_GetTB(sed, text, (unsigned int)ted_GetMaxSize(sed), TED_SOFT);
/* always a '\n' at the end, replace it with a '\0' */
text[len - 1] = '\0';
/* restore cursor */
ted_GotoPosition(sed, row, col);
}
if ((cutbuf = (menu_type) sed_GetFieldData(sed, fieldno, 1)) != NULL) {
/* destroy the cut buffer (menu) */
menu_Destroy(cutbuf);
sed_SetFieldData(sed, fieldno, 1, NULL);
}
}
static int pop_Search(boolean mode, char *srch, char *repl)
/*
Get a search string, put it into srch.
if mode is TRUE
also get a replace string, put it into repl.
Return 0 if ESC is pressed.
*/
{
menu_type menu;
sed_type sed;
int ret;
menu = menu_Open();
menu_Printf(menu, "%s : @fw20[@[50,#]]", MSG_SEARCH, srch, &string_funcs);
if (mode == TRUE) {
menu_Printf(menu, "\n%s : @fw20[@[50,#]]", MSG_REPLACE, repl, &string_funcs);
}
sed = sed_Open(menu);
sed_SetBorder(sed, bd_1);
sed_SetPosition(sed, 10, 20);
sed_SetMouse(sed, sedmou_GreedyTrack);
sed_Repaint(sed);
ret = sed_Go(sed);
sed_Close(sed);
return(ret);
}