Subversion Repositories SvarDOS

Rev

Rev 1320 | Rev 1322 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1283 mateusz.vi 1
/* Sved, the SvarDOS editor
2
 *
3
 * Copyright (C) 2023 Mateusz Viste
4
 *
5
 * Sved is released under the terms of the MIT license.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to
9
 * deal in the Software without restriction, including without limitation the
10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
 * sell copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
 * IN THE SOFTWARE.
24
 */
1275 mateusz.vi 25
 
1282 mateusz.vi 26
#include <dos.h>
27
#include <fcntl.h>
1275 mateusz.vi 28
#include <stdlib.h>
29
#include <string.h>
1288 mateusz.vi 30
#include <malloc.h>   /* _fcalloc() */
1275 mateusz.vi 31
 
1312 mateusz.vi 32
#include "mdr\bios.h"
1275 mateusz.vi 33
#include "mdr\cout.h"
1307 mateusz.vi 34
#include "mdr\dos.h"
1275 mateusz.vi 35
#include "mdr\keyb.h"
36
 
1282 mateusz.vi 37
#include "svarlang\svarlang.h"
38
 
1275 mateusz.vi 39
#define COL_TXT        0
40
#define COL_STATUSBAR1 1
41
#define COL_STATUSBAR2 2
42
#define COL_SCROLLBAR  3
43
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
44
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
45
 
46
#define SCROLL_CURSOR 0xB1
47
 
48
 
49
struct line {
1288 mateusz.vi 50
  struct line far *prev;
51
  struct line far *next;
1275 mateusz.vi 52
  unsigned short len;
53
  char payload[1];
54
};
55
 
1315 mateusz.vi 56
struct file {
1320 mateusz.vi 57
  int fd;
1288 mateusz.vi 58
  struct line far *cursor;
1275 mateusz.vi 59
  unsigned short xoffset;
1316 mateusz.vi 60
  unsigned char cursorposx;
61
  unsigned char cursorposy;
1313 mateusz.vi 62
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
1320 mateusz.vi 63
  char fname[1]; /* dynamically sized */
1275 mateusz.vi 64
};
65
 
66
 
1287 mateusz.vi 67
/* returns non-zero on error */
1315 mateusz.vi 68
static int line_add(struct file *db, const char far *line) {
1289 mateusz.vi 69
  unsigned short slen;
1288 mateusz.vi 70
  struct line far *l;
1275 mateusz.vi 71
 
1289 mateusz.vi 72
  /* slen = strlen(line) (but for far pointer) */
73
  for (slen = 0; line[slen] != 0; slen++);
74
 
1275 mateusz.vi 75
  /* trim out CR/LF line endings */
76
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
77
    slen -= 2;
78
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
79
    slen--;
80
  }
81
 
1288 mateusz.vi 82
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
1287 mateusz.vi 83
  if (l == NULL) return(-1);
84
 
1275 mateusz.vi 85
  l->prev = db->cursor;
86
  if (db->cursor) {
87
    l->next = db->cursor->next;
88
    db->cursor->next = l;
89
    l->next->prev = l;
90
  }
91
  db->cursor = l;
1288 mateusz.vi 92
  _fmemcpy(l->payload, line, slen);
1275 mateusz.vi 93
  l->len = slen;
1287 mateusz.vi 94
 
95
  return(0);
1275 mateusz.vi 96
}
97
 
98
 
1315 mateusz.vi 99
static void db_rewind(struct file *db) {
1275 mateusz.vi 100
  if (db->cursor == NULL) return;
101
  while (db->cursor->prev) db->cursor = db->cursor->prev;
102
}
103
 
104
 
1289 mateusz.vi 105
static void load_colorscheme(void) {
1275 mateusz.vi 106
  scheme[COL_TXT] = 0x17;
107
  scheme[COL_STATUSBAR1] = 0x70;
108
  scheme[COL_STATUSBAR2] = 0x78;
109
  scheme[COL_SCROLLBAR] = 0x70;
110
}
111
 
112
 
1320 mateusz.vi 113
static void ui_basic(unsigned char screenw, unsigned char screenh, const struct file *db) {
1275 mateusz.vi 114
  unsigned char i;
1302 mateusz.vi 115
  const char *s = svarlang_strid(0); /* HELP */
1275 mateusz.vi 116
  unsigned char helpcol = screenw - (strlen(s) + 4);
117
 
1313 mateusz.vi 118
  /* fill status bar with background */
119
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
120
 
121
  /* filename */
1321 mateusz.vi 122
  if (db->fname[0] == 0) {
123
    mdr_cout_str(screenh - 1, 0, svarlang_str(0, 1), scheme[COL_STATUSBAR1], screenw);
124
  } else {
125
    mdr_cout_str(screenh - 1, 0, db->fname, scheme[COL_STATUSBAR1], screenw);
126
  }
1313 mateusz.vi 127
 
128
  /* eol type */
129
  if (db->lfonly) {
130
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
131
  } else {
132
    mdr_cout_str(screenh - 1, helpcol - 5, "CRLF", scheme[COL_STATUSBAR1], 5);
1275 mateusz.vi 133
  }
1313 mateusz.vi 134
 
1275 mateusz.vi 135
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
136
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
137
 
138
  /* scroll bar */
139
  for (i = 0; i < (screenh - 1); i++) {
140
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
141
  }
142
}
143
 
144
 
1312 mateusz.vi 145
static void ui_msg(const char *msg, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
146
  unsigned short x, y, msglen, i;
147
  msglen = strlen(msg);
148
  y = (screenh - 4) >> 1;
149
  x = (screenw - msglen - 4) >> 1;
150
  for (i = y+2; i >= y; i--) mdr_cout_char_rep(i, x, ' ', scheme[COL_STATUSBAR1], msglen + 2);
151
  mdr_cout_str(y+1, x+1, msg, scheme[COL_STATUSBAR1], msglen);
152
 
153
  if (*uidirtyfrom > y) *uidirtyfrom = y;
154
  if (*uidirtyto < y+2) *uidirtyto = y+2;
155
}
156
 
157
 
1309 mateusz.vi 158
static void ui_help(unsigned char screenw) {
159
#define MAXLINLEN 35
1312 mateusz.vi 160
  unsigned short i, offset;
1309 mateusz.vi 161
  offset = (screenw - MAXLINLEN + 1) >> 1;
162
  mdr_cout_cursor_hide();
163
  for (i = 2; i <= 12; i++) {
1312 mateusz.vi 164
    mdr_cout_char_rep(i, offset - 2, ' ', scheme[COL_STATUSBAR1], MAXLINLEN);
1309 mateusz.vi 165
  }
166
 
167
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
168
  for (i = 0; i <= 2; i++) {
169
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
170
  }
171
  mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
172
  mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
173
 
174
  keyb_getkey();
175
  mdr_cout_cursor_show();
176
#undef MAXLINLEN
177
}
178
 
179
 
1316 mateusz.vi 180
static void ui_refresh(const struct file *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
1318 mateusz.vi 181
  unsigned char x;
1310 mateusz.vi 182
  const struct line far *l;
1316 mateusz.vi 183
  unsigned char y = db->cursorposy;
1275 mateusz.vi 184
 
1288 mateusz.vi 185
#ifdef DBG_REFRESH
1282 mateusz.vi 186
  static char m = 'a';
187
  m++;
188
  if (m > 'z') m = 'a';
1288 mateusz.vi 189
#endif
1282 mateusz.vi 190
 
1310 mateusz.vi 191
  /* rewind cursor line to first line that needs redrawing */
192
  for (l = db->cursor; y > uidirtyfrom; y--) l = l->prev;
1282 mateusz.vi 193
 
1310 mateusz.vi 194
  /* iterate over lines and redraw whatever needs to be redrawn */
195
  for (; l != NULL; l = l->next, y++) {
196
 
197
    /* skip lines that do not need to be refreshed */
1289 mateusz.vi 198
    if (y < uidirtyfrom) continue;
199
    if (y > uidirtyto) break;
1282 mateusz.vi 200
 
1318 mateusz.vi 201
    x = 0;
1275 mateusz.vi 202
    if (db->xoffset < l->len) {
1318 mateusz.vi 203
      unsigned char i, limit;
204
      if (l->len - db->xoffset < screenw) {
205
        limit = l->len;
206
      } else {
207
        limit = db->xoffset + screenw - 1;
208
      }
209
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], scheme[COL_TXT]);
1275 mateusz.vi 210
    }
1282 mateusz.vi 211
 
1318 mateusz.vi 212
    /* write empty spaces until end of line */
213
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', scheme[COL_TXT], screenw - 1 - x);
214
 
1288 mateusz.vi 215
#ifdef DBG_REFRESH
1282 mateusz.vi 216
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
1288 mateusz.vi 217
#endif
1282 mateusz.vi 218
 
1275 mateusz.vi 219
    if (y == screenh - 2) break;
220
  }
1310 mateusz.vi 221
 
1319 mateusz.vi 222
  /* fill all lines below if empty (and they need to be redrawn) */
223
  if (l == NULL) {
224
    while ((y < screenh - 1) && (y < uidirtyto)) {
225
      mdr_cout_char_rep(y++, 0, ' ', scheme[COL_TXT], screenw - 1);
226
    }
1318 mateusz.vi 227
  }
1275 mateusz.vi 228
}
229
 
230
 
1316 mateusz.vi 231
static void check_cursor_not_after_eol(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
232
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
1275 mateusz.vi 233
 
234
  if (db->cursor->len < db->xoffset) {
1316 mateusz.vi 235
    db->cursorposx = 0;
1275 mateusz.vi 236
    db->xoffset = db->cursor->len;
1282 mateusz.vi 237
    *uidirtyfrom = 0;
238
    *uidirtyto = 0xff;
1275 mateusz.vi 239
  } else {
1316 mateusz.vi 240
    db->cursorposx = db->cursor->len - db->xoffset;
1275 mateusz.vi 241
  }
242
}
243
 
244
 
1316 mateusz.vi 245
static void cursor_up(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 246
  if (db->cursor->prev != NULL) {
247
    db->cursor = db->cursor->prev;
1316 mateusz.vi 248
    if (db->cursorposy == 0) {
1282 mateusz.vi 249
      *uidirtyfrom = 0;
250
      *uidirtyto = 0xff;
1275 mateusz.vi 251
    } else {
1316 mateusz.vi 252
      db->cursorposy -= 1;
1275 mateusz.vi 253
    }
254
  }
255
}
256
 
257
 
1316 mateusz.vi 258
static void cursor_eol(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 259
  /* adjust xoffset to make sure eol is visible on screen */
1282 mateusz.vi 260
  if (db->xoffset > db->cursor->len) {
261
    db->xoffset = db->cursor->len - 1;
262
    *uidirtyfrom = 0;
263
    *uidirtyto = 0xff;
264
  }
265
 
266
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
267
    db->xoffset = db->cursor->len - screenw + 2;
268
    *uidirtyfrom = 0;
269
    *uidirtyto = 0xff;
270
  }
1316 mateusz.vi 271
  db->cursorposx = db->cursor->len - db->xoffset;
1275 mateusz.vi 272
}
273
 
274
 
1316 mateusz.vi 275
static void cursor_down(struct file *db, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1276 mateusz.vi 276
  if (db->cursor->next != NULL) {
277
    db->cursor = db->cursor->next;
1316 mateusz.vi 278
    if (db->cursorposy < screenh - 2) {
279
      db->cursorposy += 1;
1276 mateusz.vi 280
    } else {
1282 mateusz.vi 281
      *uidirtyfrom = 0;
282
      *uidirtyto = 0xff;
1276 mateusz.vi 283
    }
284
  }
285
}
286
 
287
 
1316 mateusz.vi 288
static void cursor_left(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
289
  if (db->cursorposx > 0) {
290
    db->cursorposx -= 1;
1302 mateusz.vi 291
  } else if (db->xoffset > 0) {
292
    db->xoffset -= 1;
293
    *uidirtyfrom = 0;
294
    *uidirtyto = 0xff;
295
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
1316 mateusz.vi 296
    cursor_up(db, uidirtyfrom, uidirtyto);
297
    cursor_eol(db, screenw, uidirtyfrom, uidirtyto);
1302 mateusz.vi 298
  }
299
}
300
 
301
 
1316 mateusz.vi 302
static void cursor_home(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
303
  db->cursorposx = 0;
1282 mateusz.vi 304
  if (db->xoffset != 0) {
305
    db->xoffset = 0;
306
    *uidirtyfrom = 0;
307
    *uidirtyto = 0xff;
308
  }
1276 mateusz.vi 309
}
310
 
311
 
1316 mateusz.vi 312
static void cursor_right(struct file *db, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
313
  if (db->cursor->len > db->xoffset + db->cursorposx) {
314
    if (db->cursorposx < screenw - 2) {
315
      db->cursorposx += 1;
1308 mateusz.vi 316
    } else {
317
      db->xoffset += 1;
318
      *uidirtyfrom = 0;
319
      *uidirtyto = 0xff;
320
    }
321
  } else {
1316 mateusz.vi 322
    cursor_down(db, screenh, uidirtyfrom, uidirtyto);
323
    cursor_home(db, uidirtyfrom, uidirtyto);
1308 mateusz.vi 324
  }
325
}
326
 
327
 
1316 mateusz.vi 328
static void del(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
329
  if (db->cursorposx + db->xoffset < db->cursor->len) {
330
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
1292 mateusz.vi 331
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
1316 mateusz.vi 332
    *uidirtyfrom = db->cursorposy;
333
    *uidirtyto = db->cursorposy;
1292 mateusz.vi 334
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
335
    struct line far *nextline = db->cursor->next;
336
    if (db->cursor->next->len > 0) {
337
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
338
      if (newptr != NULL) {
339
        db->cursor = newptr;
340
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
341
        db->cursor->len += db->cursor->next->len;
342
      }
343
    }
344
    db->cursor->next = db->cursor->next->next;
345
    db->cursor->next->prev = db->cursor;
346
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
347
    _ffree(nextline);
1316 mateusz.vi 348
    *uidirtyfrom = db->cursorposy;
1292 mateusz.vi 349
    *uidirtyto = 0xff;
350
  }
351
}
352
 
353
 
1316 mateusz.vi 354
static void bkspc(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1302 mateusz.vi 355
 
356
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
1316 mateusz.vi 357
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
1302 mateusz.vi 358
 
1316 mateusz.vi 359
  cursor_left(db, screenw, uidirtyfrom, uidirtyto);
360
  del(db, uidirtyfrom, uidirtyto);
1302 mateusz.vi 361
}
362
 
363
 
1286 mateusz.vi 364
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
365
 * of argc and argv, saves some 330 bytes of binary size */
366
static char *parseargv(void) {
367
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
368
  unsigned char count = 0;
369
  char *argv[4];
370
 
371
  while (count < 4) {
372
    /* jump to nearest arg */
373
    while (*tail == ' ') {
374
      *tail = 0;
375
      tail++;
376
    }
377
 
378
    if (*tail == '\r') {
379
      *tail = 0;
380
      break;
381
    }
382
 
383
    argv[count++] = tail;
384
 
385
    /* jump to next delimiter */
386
    while ((*tail != ' ') && (*tail != '\r')) tail++;
387
  }
388
 
389
  /* check args now */
1321 mateusz.vi 390
  if (count == 0) return("");
1286 mateusz.vi 391
 
392
  return(argv[0]);
393
}
394
 
395
 
1320 mateusz.vi 396
static struct file *loadfile(const char *fname) {
1287 mateusz.vi 397
  char buff[1024];
398
  unsigned int prevlen = 0, len, llen;
399
  int fd;
1320 mateusz.vi 400
  struct file *db;
1287 mateusz.vi 401
 
1321 mateusz.vi 402
  len = strlen(fname) + 1;
403
  db = calloc(1, sizeof(struct file) + len);
404
  if (db == NULL) return(NULL);
405
  memcpy(db->fname, fname, len);
406
 
407
  if (*fname == 0) goto SKIPLOADING;
408
 
1287 mateusz.vi 409
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
410
    mdr_coutraw_puts("Failed to open file:");
411
    mdr_coutraw_puts(fname);
1321 mateusz.vi 412
    free(db);
1320 mateusz.vi 413
    return(NULL);
1287 mateusz.vi 414
  }
415
 
1313 mateusz.vi 416
  db->lfonly = 1;
417
 
1287 mateusz.vi 418
  do {
419
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
420
      len += prevlen;
421
    } else {
422
      len = prevlen;
423
    }
424
 
425
    /* look for nearest \n and replace with 0*/
426
    for (llen = 0; buff[llen] != '\n'; llen++) {
1320 mateusz.vi 427
      if (llen == sizeof(buff)) { /* TODO line too long: handle it in some classy way */
428
        break;
429
      }
1287 mateusz.vi 430
    }
431
    buff[llen] = 0;
1313 mateusz.vi 432
    if ((llen > 0) && (buff[llen - 1] == '\r')) {
433
      buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
434
      db->lfonly = 0;
435
    }
1287 mateusz.vi 436
    if (line_add(db, buff) != 0) {
437
      mdr_coutraw_puts("out of memory");
1320 mateusz.vi 438
      free(db);
439
      db = NULL;
1287 mateusz.vi 440
      break;
441
    }
442
 
443
    len -= llen + 1;
444
    memmove(buff, buff + llen + 1, len);
445
    prevlen = len;
446
  } while (len > 0);
447
 
448
  _dos_close(fd);
449
 
1321 mateusz.vi 450
  SKIPLOADING:
451
 
1320 mateusz.vi 452
  /* add an empty line at end if not present already, also rewind cursor to top of file */
453
  if (db != NULL) {
1321 mateusz.vi 454
    if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, "");
1320 mateusz.vi 455
    db_rewind(db);
456
  }
457
 
458
  return(db);
1287 mateusz.vi 459
}
460
 
461
 
1320 mateusz.vi 462
static int savefile(const struct file *db) {
1311 mateusz.vi 463
  int fd;
464
  const struct line far *l;
465
  unsigned bytes;
1313 mateusz.vi 466
  unsigned char eollen;
467
  unsigned char eolbuf[2];
468
 
1320 mateusz.vi 469
  if (_dos_open(db->fname, O_WRONLY, &fd) != 0) {
1311 mateusz.vi 470
    return(-1);
471
  }
472
 
473
  l = db->cursor;
474
  while (l->prev) l = l->prev;
475
 
1313 mateusz.vi 476
  /* preset line terminators */
477
  if (db->lfonly) {
478
    eolbuf[0] = '\n';
479
    eollen = 1;
480
  } else {
481
    eolbuf[0] = '\r';
482
    eolbuf[1] = '\n';
483
    eollen = 2;
484
  }
485
 
1311 mateusz.vi 486
  while (l) {
487
    _dos_write(fd, l->payload, l->len, &bytes);
1313 mateusz.vi 488
    _dos_write(fd, eolbuf, eollen, &bytes);
1311 mateusz.vi 489
    l = l->next;
490
  }
491
 
492
  _dos_close(fd);
493
  return(0);
494
}
495
 
496
 
1317 mateusz.vi 497
static void insert_in_line(struct file *db, const char *databuf, unsigned short len, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
498
  struct line far *n;
499
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
500
  if (n != NULL) {
501
    unsigned short off = db->xoffset + db->cursorposx;
502
    if (n->prev) n->prev->next = n;
503
    if (n->next) n->next->prev = n;
504
    db->cursor = n;
505
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
506
    db->cursor->len += len;
507
    *uidirtyfrom = db->cursorposy;
508
    *uidirtyto = db->cursorposy;
509
    while (len--) {
510
      db->cursor->payload[off++] = *databuf;
511
      databuf++;
512
      cursor_right(db, screenw, screenh, uidirtyfrom, uidirtyto);
513
    }
514
  }
515
}
516
 
517
 
1286 mateusz.vi 518
int main(void) {
519
  const char *fname;
1320 mateusz.vi 520
  struct file *db;
1275 mateusz.vi 521
  unsigned char screenw = 0, screenh = 0;
1282 mateusz.vi 522
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
1275 mateusz.vi 523
 
1307 mateusz.vi 524
  {
525
    char nlspath[128], lang[8];
526
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
527
  }
1282 mateusz.vi 528
 
1286 mateusz.vi 529
  fname = parseargv();
530
 
531
  if (fname == NULL) {
1302 mateusz.vi 532
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
1275 mateusz.vi 533
    return(0);
534
  }
535
 
1282 mateusz.vi 536
  /* load file */
1320 mateusz.vi 537
  db = loadfile(fname);
538
  if (db == NULL) return(1);
1282 mateusz.vi 539
 
1275 mateusz.vi 540
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
1320 mateusz.vi 541
  ui_basic(screenw, screenh, db);
1275 mateusz.vi 542
 
543
  for (;;) {
544
    int k;
545
 
1320 mateusz.vi 546
    check_cursor_not_after_eol(db, &uidirtyfrom, &uidirtyto);
547
    mdr_cout_locate(db->cursorposy, db->cursorposx);
1275 mateusz.vi 548
 
1282 mateusz.vi 549
    if (uidirtyfrom != 0xff) {
1320 mateusz.vi 550
      ui_refresh(db, screenw, screenh, uidirtyfrom, uidirtyto);
1282 mateusz.vi 551
      uidirtyfrom = 0xff;
552
    }
1275 mateusz.vi 553
 
554
    k = keyb_getkey();
1282 mateusz.vi 555
 
1275 mateusz.vi 556
    if (k == 0x150) { /* down */
1320 mateusz.vi 557
      cursor_down(db, screenh, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 558
 
559
    } else if (k == 0x148) { /* up */
1320 mateusz.vi 560
      cursor_up(db, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 561
 
562
    } else if (k == 0x14D) { /* right */
1320 mateusz.vi 563
      cursor_right(db, screenw, screenh, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 564
 
565
    } else if (k == 0x14B) { /* left */
1320 mateusz.vi 566
      cursor_left(db, screenw, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 567
 
1282 mateusz.vi 568
    } else if (k == 0x149) { /* pgup */
569
      // TODO
570
 
571
    } else if (k == 0x151) { /* pgdown */
572
      // TODO
573
 
574
    } else if (k == 0x147) { /* home */
1320 mateusz.vi 575
       cursor_home(db, &uidirtyfrom, &uidirtyto);
1282 mateusz.vi 576
 
577
    } else if (k == 0x14F) { /* end */
1320 mateusz.vi 578
       cursor_eol(db, screenw, &uidirtyfrom, &uidirtyto);
1282 mateusz.vi 579
 
1275 mateusz.vi 580
    } else if (k == 0x1B) { /* ESC */
581
      break;
582
 
1289 mateusz.vi 583
    } else if (k == 0x0D) { /* ENTER */
584
      /* add a new line */
1320 mateusz.vi 585
      if (line_add(db, db->cursor->payload + db->xoffset + db->cursorposx) == 0) {
1289 mateusz.vi 586
        /* trim the line above */
1320 mateusz.vi 587
        db->cursor->prev->len = db->xoffset + db->cursorposx;
588
        db->cursor->prev->payload[db->cursor->prev->len] = 0;
1289 mateusz.vi 589
        /* move cursor to the (new) line below */
1320 mateusz.vi 590
        db->cursorposx = 0;
591
        if (db->cursorposy < screenh - 2) {
592
          uidirtyfrom = db->cursorposy;
593
          db->cursorposy++;
1289 mateusz.vi 594
        } else {
595
          uidirtyfrom = 0;
596
        }
597
        uidirtyto = 0xff;
598
      } else {
599
        /* ERROR: OUT OF MEMORY */
600
      }
601
 
1292 mateusz.vi 602
    } else if (k == 0x153) {  /* DEL */
1320 mateusz.vi 603
      del(db, &uidirtyfrom, &uidirtyto);
1292 mateusz.vi 604
 
1302 mateusz.vi 605
    } else if (k == 0x008) { /* BKSPC */
1320 mateusz.vi 606
      bkspc(db, screenw, &uidirtyfrom, &uidirtyto);
1302 mateusz.vi 607
 
1308 mateusz.vi 608
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1317 mateusz.vi 609
      char c = k;
1320 mateusz.vi 610
      insert_in_line(db, &c, 1, screenw, screenh, &uidirtyfrom, &uidirtyto);
1308 mateusz.vi 611
 
1317 mateusz.vi 612
    } else if (k == 0x009) { /* TAB */
613
      const char *tab = "        ";
1320 mateusz.vi 614
      insert_in_line(db, tab, 8, screenw, screenh, &uidirtyfrom, &uidirtyto);
1317 mateusz.vi 615
 
1309 mateusz.vi 616
    } else if (k == 0x13b) { /* F1 */
617
      ui_help(screenw);
618
      uidirtyfrom = 0;
619
      uidirtyto = 0xff;
620
 
1311 mateusz.vi 621
    } else if (k == 0x13f) { /* F5 */
1312 mateusz.vi 622
      const char *m[] = {"SAVED", "SAVING FILE FAILED"};
623
      int i;
1320 mateusz.vi 624
      i = !!savefile(db);
1312 mateusz.vi 625
      ui_msg(m[i], screenw, screenh, &uidirtyfrom, &uidirtyto);
626
      mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1311 mateusz.vi 627
 
1314 mateusz.vi 628
    } else if (k == 0x144) { /* F10 */
1320 mateusz.vi 629
      db->lfonly ^= 1;
630
      ui_basic(screenw, screenh, db);
1314 mateusz.vi 631
 
1282 mateusz.vi 632
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
633
      char buff[4];
634
      const char *HEX = "0123456789ABCDEF";
635
      buff[0] = HEX[(k >> 8) & 15];
636
      buff[1] = HEX[(k >> 4) & 15];
637
      buff[2] = HEX[k & 15];
638
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
639
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
1275 mateusz.vi 640
      keyb_getkey();
641
      break;
642
    }
643
  }
644
 
645
  mdr_cout_close();
646
 
1320 mateusz.vi 647
  /* no need to free memory, DOS will do it for me */
1275 mateusz.vi 648
 
649
  return(0);
650
}