1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
//=======================================================================
// vedcnv.cpp: Source for vedTextEditor class
//=======================================================================
#include <v/vnotice.h>
#ifdef VIDE
#define VCmdWindow videCmdWindow
#include "videapp.h"
#include "vedcnv.h"
#include "videcmdw.h"
#else
#define VCmdWindow vedCmdWindow
#include "vedapp.h"
#include "vedcnv.h"
#include "vedcmdw.h"
#endif
const int maxBuff = 300;
//===================>>> vedTextEditor::vedTextEditor <<<====================
vedTextEditor::vedTextEditor(VCmdWindow* parent) :
vTextEditor((vCmdWindow*)parent,1)
{
}
//===================>>> vedTextEditor::~vedTextEditor <<<====================
vedTextEditor::~vedTextEditor()
{
}
#define isSpace(x) (x == ' ' || x == '\t')
#define isOp(x) (x=='+' || x=='-' || x=='*' || x=='/' || x=='<' || x=='>' \
|| x=='?' || x==':' || x==';' || x=='!' || x=='%' || x=='^' || x=='&' \
|| x=='\\' || x=='|' || x == '=' || x==',' || x=='\'' || x=='\"' \
|| x==')' || x=='(' || x=='[' || x==']' || x=='{' || x=='}')
// ======================>>> vTextEditor::paintLine <<<=====================
void vedTextEditor::paintLine(char* linout, int lineStart,
int hiStart, int hiLast, long lineNum)
{
// paint a line.
// linout: the line to output with tabs converted to spaces, etc.
// lineStart: where to begin printing the line (for hoiz. scrolling)
// hiStart, hiLast: reverse text attribute
// lineNum: the real line number in the buffer this is.
ChrAttr attrs[MAX_LINE+1]; // for attributes
int wasComment = 0;
int linlen = strlen(linout);
if (linlen <= 0) // only draw if there!
return;
for (int ix = 0 ; ix <= MAX_LINE ; ++ix) // assume normal
attrs[ix] = ChNormal;
// Parse the line for syntax
if (GetFileType() == CPP) // if a C file, parse
wasComment = parseC(linout,attrs,lineNum);
else if (GetFileType() == gccError)
{
int ig = 0;
while (isSpace(linout[ig]))
++ig;
if (linout[ig] == '>')
{
while (linout[ig])
attrs[ig++] = ChBlue;
}
else if (linout[ig] == '!')
{
while (linout[ig])
attrs[ig++] = ChRed;
}
}
// Now fill in highlight attributes
for (int ih = 0 ; linout[ih] != 0 ; ++ih)
{
if (ih >= hiStart && ih < hiLast)
attrs[ih] = ChHighlight; // override syntax colors
}
for (int ix = (wasComment) ? 0 : lineStart ; linout[ix] != 0 ; ++ix)
DrawChar(linout[ix],attrs[ix]);
}
#ifdef VIDE
//===================>>> vedTextEditor::TextMouseDown <<<====================
void vedTextEditor::TextMouseDown(int row, int col, int button)
{
int btn = (GetFileType() == gccError) ? 1 : button;
vTextEditor::TextMouseDown(row, col, btn); // translate to left
}
//===================>>> vedTextEditor::TextMouseUP <<<====================
void vedTextEditor::TextMouseUp(int row, int col, int button)
{
int btn = (GetFileType() == gccError) ? 1 : button;
vTextEditor::TextMouseUp(row, col, btn);
if (button == 3 && GetFileType() == gccError) // open error file
{
((VCmdWindow*)_parent)->GotoErrorLine();
}
}
#endif
//===================>>> vedTextEditor::parseC <<<====================
int vedTextEditor::parseC(char* linout, ChrAttr* attrs, long lineNum)
{
int ix = 0;
int wasComment = 0;
char token[MAX_LINE+1];
for (ix = 0 ; linout[ix] != 0 ; ++ix)
if (!isSpace(linout[ix]))
break; // skip leading spaces
while (linout[ix] != 0)
{
// gather a token
int iy;
for (iy = ix ; linout[iy] != 0 ; ++iy)
{
if (isOp(linout[iy]) || isSpace(linout[iy]) )
break;
}
// token goes from ix to iy
int from = ix;
if (ix == iy)
++iy; // single char tokens
char *tp;
for (tp = token ; from < iy ; ++from)
*tp++ = linout[from];
*tp = 0;
// OK, now highlight the token appropriately
if (isOp(*token))
{
if (*token == '"')
{
// A String...
int ij;
for (ij = ix ; linout[ij] ; ++ij)
{
attrs[ij] = ChRed;
if (linout[ij] == '\\')
{
attrs[ij+1] = ChRed;
++ij;
}
else if (linout[ij] == '"' && ij != ix)
break;
}
iy = ij+1;
}
else if (*token == '/' && linout[ix+1] == '/')
{
// A comment...
int ij;
for (ij = ix ; linout[ij] ; ++ij)
attrs[ij] = ChGreen | ChDimColor;
iy = ij;
wasComment = 1;
}
else if (*token == '/' && linout[ix+1] == '*')
{
/* A comment... */
int ij;
for (ij = ix ; linout[ij] ; ++ij)
{
attrs[ij] = ChYellow | ChDimColor;
if (linout[ij] =='/' && linout[ij-1] == '*') // "*/"
break;
}
wasComment = 1;
if (linout[ij] == 0)
break;
else
iy = ij+1;
}
else if (*token == '*' && linout[ix+1] == '/')
{
/* A comment... */
int ij;
for (ij = 0 ; linout[ij] && ij <= ix+1 ; ++ij)
{
attrs[ij] = ChYellow | ChDimColor;
if (linout[ij] =='/' && linout[ij-1] == '*')
break;
}
wasComment = 1;
if (linout[ij] == 0)
break;
else
iy = ij+1;
}
else
attrs[ix] |= ChBlue | ChDimColor;
}
else if (*token == '#')
{
for (int ij = ix ; ij < iy ; ++ij)
attrs[ij] |= ChCyan | ChDimColor;
}
else if (*token >= '0' && *token <= '9')
{
for (int ij = ix ; ij < iy ; ++ij)
attrs[ij] |= ChRed;
}
else if (isKeyWord(token))
{
for (int ij = ix ; ij < iy ; ++ij)
attrs[ij] |= ChBlue;
}
ix = iy;
}
return wasComment;
}
//===================>>> vedTextEditor::isKeyWord <<<====================
int vedTextEditor::isKeyWord(char* token)
{
char* keywords[] =
{
"FALSE", "NULL", "TRUE",
"asm", "auto", "bool", "break",
"case", "catch", "char", "class", "const", "const_cast",
"continue", "default", "delete", "do", "double",
"dynamic_cast", "else", "enum", "explicit", "extern",
"false", "float", "for", "friend", "goto", "if",
"inline", "int", "long", "mutable", "namespace", "new",
"operator", "private", "protected", "public", "register",
"reinterpret_cast", "return", "short", "signed", "sizeof",
"static", "static_cast", "struct", "switch", "template",
"this", "throw", "true", "try", "typedef", "typeid",
"typename", "union", "unsigned", "using", "virtual", "void",
"volatile", "wchar_t", "while", ""
};
for (int ix = 0 ; *keywords[ix] ; ++ix)
{
if (strcmp(token,keywords[ix]) == 0)
return 1;
}
return 0;
}
//===================>>> vedTextEditor::ChangeLoc <<<====================
void vedTextEditor::ChangeLoc(long line, int col)
{
((VCmdWindow*)_parent)->ChangeLoc(line,col);
}
//===================>>> vedTextEditor::ChangeInsMode <<<====================
void vedTextEditor::ChangeInsMode(int IsInsMode, char* msg)
{
((VCmdWindow*)_parent)->ChangeInsMode(IsInsMode, msg);
}
//===================>>> vedTextEditor::StatusMessage <<<====================
void vedTextEditor::StatusMessage(char *msg)
{
((VCmdWindow*)_parent)->StatusMessage(msg);
}
//===================>>> vedTextEditor::ErrorMsg <<<====================
void vedTextEditor::ErrorMsg(char *str)
{
}
//===================>>> vedTextEditor::ReadFile <<<====================
int vedTextEditor::ReadFile(char* name, int paintIt)
{
char buff[maxBuff+2];
if (!name || !*name)
return 0;
ifstream inFile(name);
if (!inFile)
return 0; // file not there
resetBuff(); // this buffer is empty
while (inFile.getline(buff,maxBuff))
{
if (!addLine(buff))
{
vNoticeDialog note(theApp);
note.Notice("File too big -- only paritally read.");
break;
}
}
inFile.close();
displayBuff(1,paintIt); // Now, display the buffer , don't paint
return 1;
}
//===================>>> vedTextEditor::SaveFile <<<====================
int vedTextEditor::SaveFile(char* name)
{
char buff[maxBuff+2];
char* prefix = "Saved ";
ofstream ofs(name);
if (!ofs)
{
StatusMessage("Can't save file");
return 0;
}
for (long lx = 1 ; lx <= GetLines() ; ++lx)
{
getLine(buff,maxBuff,lx);
ofs << buff << "\n";
}
ofs.close();
strcpy(buff,prefix);
int ix;
for (ix = strlen(prefix) ; ix < 40 && *name != 0 ; ++ix)
buff[ix] = *name++;
buff[ix] = 0;
StatusMessage(buff);
state.changes = 0;
return 1;
}
|