Subversion Repositories SvarDOS

Rev

Rev 1311 | Rev 1313 | 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
 
56
struct linedb {
1288 mateusz.vi 57
  struct line far *cursor;
1275 mateusz.vi 58
  unsigned short xoffset;
59
};
60
 
61
 
1287 mateusz.vi 62
/* returns non-zero on error */
1289 mateusz.vi 63
static int line_add(struct linedb *db, const char far *line) {
64
  unsigned short slen;
1288 mateusz.vi 65
  struct line far *l;
1275 mateusz.vi 66
 
1289 mateusz.vi 67
  /* slen = strlen(line) (but for far pointer) */
68
  for (slen = 0; line[slen] != 0; slen++);
69
 
1275 mateusz.vi 70
  /* trim out CR/LF line endings */
71
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
72
    slen -= 2;
73
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
74
    slen--;
75
  }
76
 
1288 mateusz.vi 77
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
1287 mateusz.vi 78
  if (l == NULL) return(-1);
79
 
1275 mateusz.vi 80
  l->prev = db->cursor;
81
  if (db->cursor) {
82
    l->next = db->cursor->next;
83
    db->cursor->next = l;
84
    l->next->prev = l;
85
  }
86
  db->cursor = l;
1288 mateusz.vi 87
  _fmemcpy(l->payload, line, slen);
1275 mateusz.vi 88
  l->len = slen;
1287 mateusz.vi 89
 
90
  return(0);
1275 mateusz.vi 91
}
92
 
93
 
1289 mateusz.vi 94
static void db_rewind(struct linedb *db) {
1275 mateusz.vi 95
  if (db->cursor == NULL) return;
96
  while (db->cursor->prev) db->cursor = db->cursor->prev;
97
}
98
 
99
 
1289 mateusz.vi 100
static void load_colorscheme(void) {
1275 mateusz.vi 101
  scheme[COL_TXT] = 0x17;
102
  scheme[COL_STATUSBAR1] = 0x70;
103
  scheme[COL_STATUSBAR2] = 0x78;
104
  scheme[COL_SCROLLBAR] = 0x70;
105
}
106
 
107
 
1282 mateusz.vi 108
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
1275 mateusz.vi 109
  unsigned char i;
1302 mateusz.vi 110
  const char *s = svarlang_strid(0); /* HELP */
1275 mateusz.vi 111
  unsigned char helpcol = screenw - (strlen(s) + 4);
112
 
113
  /* clear screen */
114
  mdr_cout_cls(scheme[COL_TXT]);
115
 
116
  /* status bar */
117
  for (i = 0; i < helpcol; i++) {
118
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
119
    if (*fname != 0) fname++;
120
  }
121
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
122
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
123
 
124
  /* scroll bar */
125
  for (i = 0; i < (screenh - 1); i++) {
126
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
127
  }
128
}
129
 
130
 
1312 mateusz.vi 131
static void ui_msg(const char *msg, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
132
  unsigned short x, y, msglen, i;
133
  msglen = strlen(msg);
134
  y = (screenh - 4) >> 1;
135
  x = (screenw - msglen - 4) >> 1;
136
  for (i = y+2; i >= y; i--) mdr_cout_char_rep(i, x, ' ', scheme[COL_STATUSBAR1], msglen + 2);
137
  mdr_cout_str(y+1, x+1, msg, scheme[COL_STATUSBAR1], msglen);
138
 
139
  if (*uidirtyfrom > y) *uidirtyfrom = y;
140
  if (*uidirtyto < y+2) *uidirtyto = y+2;
141
}
142
 
143
 
1309 mateusz.vi 144
static void ui_help(unsigned char screenw) {
145
#define MAXLINLEN 35
1312 mateusz.vi 146
  unsigned short i, offset;
1309 mateusz.vi 147
  offset = (screenw - MAXLINLEN + 1) >> 1;
148
  mdr_cout_cursor_hide();
149
  for (i = 2; i <= 12; i++) {
1312 mateusz.vi 150
    mdr_cout_char_rep(i, offset - 2, ' ', scheme[COL_STATUSBAR1], MAXLINLEN);
1309 mateusz.vi 151
  }
152
 
153
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
154
  for (i = 0; i <= 2; i++) {
155
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
156
  }
157
  mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
158
  mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
159
 
160
  keyb_getkey();
161
  mdr_cout_cursor_show();
162
#undef MAXLINLEN
163
}
164
 
165
 
1310 mateusz.vi 166
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto, unsigned char y) {
1275 mateusz.vi 167
  unsigned char len;
1310 mateusz.vi 168
  const struct line far *l;
1275 mateusz.vi 169
 
1288 mateusz.vi 170
#ifdef DBG_REFRESH
1282 mateusz.vi 171
  static char m = 'a';
172
  m++;
173
  if (m > 'z') m = 'a';
1288 mateusz.vi 174
#endif
1282 mateusz.vi 175
 
1310 mateusz.vi 176
  /* rewind cursor line to first line that needs redrawing */
177
  for (l = db->cursor; y > uidirtyfrom; y--) l = l->prev;
1282 mateusz.vi 178
 
1310 mateusz.vi 179
  /* iterate over lines and redraw whatever needs to be redrawn */
180
  for (; l != NULL; l = l->next, y++) {
181
 
182
    /* skip lines that do not need to be refreshed */
1289 mateusz.vi 183
    if (y < uidirtyfrom) continue;
184
    if (y > uidirtyto) break;
1282 mateusz.vi 185
 
1275 mateusz.vi 186
    if (db->xoffset < l->len) {
1288 mateusz.vi 187
      for (len = 0; l->payload[len] != 0; len++) mdr_cout_char(y, len, l->payload[len], scheme[COL_TXT]);
1275 mateusz.vi 188
    } else {
189
      len = 0;
190
    }
191
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
1282 mateusz.vi 192
 
1288 mateusz.vi 193
#ifdef DBG_REFRESH
1282 mateusz.vi 194
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
1288 mateusz.vi 195
#endif
1282 mateusz.vi 196
 
1275 mateusz.vi 197
    if (y == screenh - 2) break;
198
  }
1310 mateusz.vi 199
 
1275 mateusz.vi 200
}
201
 
202
 
1282 mateusz.vi 203
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 204
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
205
 
206
  if (db->cursor->len < db->xoffset) {
207
    *cursorpos = 0;
208
    db->xoffset = db->cursor->len;
1282 mateusz.vi 209
    *uidirtyfrom = 0;
210
    *uidirtyto = 0xff;
1275 mateusz.vi 211
  } else {
212
    *cursorpos = db->cursor->len - db->xoffset;
213
  }
214
}
215
 
216
 
1282 mateusz.vi 217
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 218
  if (db->cursor->prev != NULL) {
219
    db->cursor = db->cursor->prev;
220
    if (*cursorposy == 0) {
1282 mateusz.vi 221
      *uidirtyfrom = 0;
222
      *uidirtyto = 0xff;
1275 mateusz.vi 223
    } else {
224
      *cursorposy -= 1;
225
    }
226
  }
227
}
228
 
229
 
1282 mateusz.vi 230
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 231
  /* adjust xoffset to make sure eol is visible on screen */
1282 mateusz.vi 232
  if (db->xoffset > db->cursor->len) {
233
    db->xoffset = db->cursor->len - 1;
234
    *uidirtyfrom = 0;
235
    *uidirtyto = 0xff;
236
  }
237
 
238
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
239
    db->xoffset = db->cursor->len - screenw + 2;
240
    *uidirtyfrom = 0;
241
    *uidirtyto = 0xff;
242
  }
1275 mateusz.vi 243
  *cursorposx = db->cursor->len - db->xoffset;
244
}
245
 
246
 
1282 mateusz.vi 247
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1276 mateusz.vi 248
  if (db->cursor->next != NULL) {
249
    db->cursor = db->cursor->next;
250
    if (*cursorposy < screenh - 2) {
251
      *cursorposy += 1;
252
    } else {
1282 mateusz.vi 253
      *uidirtyfrom = 0;
254
      *uidirtyto = 0xff;
1276 mateusz.vi 255
    }
256
  }
257
}
258
 
259
 
1302 mateusz.vi 260
static void cursor_left(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
261
  if (*cursorposx > 0) {
262
    *cursorposx -= 1;
263
  } else if (db->xoffset > 0) {
264
    db->xoffset -= 1;
265
    *uidirtyfrom = 0;
266
    *uidirtyto = 0xff;
267
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
268
    cursor_up(db, cursorposy, uidirtyfrom, uidirtyto);
269
    cursor_eol(db, cursorposx, screenw, uidirtyfrom, uidirtyto);
270
  }
271
}
272
 
273
 
1282 mateusz.vi 274
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1276 mateusz.vi 275
  *cursorposx = 0;
1282 mateusz.vi 276
  if (db->xoffset != 0) {
277
    db->xoffset = 0;
278
    *uidirtyfrom = 0;
279
    *uidirtyto = 0xff;
280
  }
1276 mateusz.vi 281
}
282
 
283
 
1308 mateusz.vi 284
static void cursor_right(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
285
  if (db->cursor->len > db->xoffset + *cursorposx) {
286
    if (*cursorposx < screenw - 2) {
287
      *cursorposx += 1;
288
    } else {
289
      db->xoffset += 1;
290
      *uidirtyfrom = 0;
291
      *uidirtyto = 0xff;
292
    }
293
  } else {
294
    cursor_down(db, cursorposy, screenh, uidirtyfrom, uidirtyto);
295
    cursor_home(db, cursorposx, uidirtyfrom, uidirtyto);
296
  }
297
}
298
 
299
 
1292 mateusz.vi 300
static void del(struct linedb *db, unsigned char cursorposx, unsigned char cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
301
  if (cursorposx + db->xoffset < db->cursor->len) {
302
    _fmemmove(db->cursor->payload + cursorposx + db->xoffset, db->cursor->payload + cursorposx + db->xoffset + 1, db->cursor->len - cursorposx - db->xoffset);
303
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
304
    *uidirtyfrom = cursorposy;
305
    *uidirtyto = cursorposy;
306
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
307
    struct line far *nextline = db->cursor->next;
308
    if (db->cursor->next->len > 0) {
309
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
310
      if (newptr != NULL) {
311
        db->cursor = newptr;
312
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
313
        db->cursor->len += db->cursor->next->len;
314
      }
315
    }
316
    db->cursor->next = db->cursor->next->next;
317
    db->cursor->next->prev = db->cursor;
318
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
319
    _ffree(nextline);
320
    *uidirtyfrom = cursorposy;
321
    *uidirtyto = 0xff;
322
  }
323
}
324
 
325
 
1302 mateusz.vi 326
static void bkspc(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
327
 
328
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
329
  if ((*cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
330
 
331
  cursor_left(db, cursorposx, cursorposy, screenw, uidirtyfrom, uidirtyto);
332
  del(db, *cursorposx, *cursorposy, uidirtyfrom, uidirtyto);
333
}
334
 
335
 
1286 mateusz.vi 336
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
337
 * of argc and argv, saves some 330 bytes of binary size */
338
static char *parseargv(void) {
339
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
340
  unsigned char count = 0;
341
  char *argv[4];
342
 
343
  while (count < 4) {
344
    /* jump to nearest arg */
345
    while (*tail == ' ') {
346
      *tail = 0;
347
      tail++;
348
    }
349
 
350
    if (*tail == '\r') {
351
      *tail = 0;
352
      break;
353
    }
354
 
355
    argv[count++] = tail;
356
 
357
    /* jump to next delimiter */
358
    while ((*tail != ' ') && (*tail != '\r')) tail++;
359
  }
360
 
361
  /* check args now */
362
  if (count != 1) return(NULL);
363
 
364
  return(argv[0]);
365
}
366
 
367
 
1287 mateusz.vi 368
static int loadfile(struct linedb *db, const char *fname) {
369
  char buff[1024];
370
  unsigned int prevlen = 0, len, llen;
371
  int fd;
372
  int r = 0;
373
 
374
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
375
    mdr_coutraw_puts("Failed to open file:");
376
    mdr_coutraw_puts(fname);
377
    return(-1);
378
  }
379
 
380
  do {
381
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
382
      len += prevlen;
383
    } else {
384
      len = prevlen;
385
    }
386
 
387
    /* look for nearest \n and replace with 0*/
388
    for (llen = 0; buff[llen] != '\n'; llen++) {
389
      if (llen == sizeof(buff)) break;
390
    }
391
    buff[llen] = 0;
392
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
393
    if (line_add(db, buff) != 0) {
394
      mdr_coutraw_puts("out of memory");
395
      r = -1;
396
      break;
397
    }
398
 
399
    len -= llen + 1;
400
    memmove(buff, buff + llen + 1, len);
401
    prevlen = len;
402
  } while (len > 0);
403
 
404
  _dos_close(fd);
405
 
406
  return(r);
407
}
408
 
409
 
1311 mateusz.vi 410
static int savefile(const struct linedb *db, const char *fname) {
411
  int fd;
412
  const struct line far *l;
413
  unsigned bytes;
414
  if (_dos_open(fname, O_WRONLY, &fd) != 0) {
415
    return(-1);
416
  }
417
 
418
  l = db->cursor;
419
  while (l->prev) l = l->prev;
420
 
421
  while (l) {
422
    _dos_write(fd, l->payload, l->len, &bytes);
423
    _dos_write(fd, "\r\n", 2, &bytes);
424
    l = l->next;
425
  }
426
 
427
  _dos_close(fd);
428
  return(0);
429
}
430
 
431
 
1286 mateusz.vi 432
int main(void) {
433
  const char *fname;
1275 mateusz.vi 434
  struct linedb db;
435
  unsigned char screenw = 0, screenh = 0;
436
  unsigned char cursorposx = 0, cursorposy = 0;
1282 mateusz.vi 437
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
1275 mateusz.vi 438
 
439
  bzero(&db, sizeof(db));
440
 
1307 mateusz.vi 441
  {
442
    char nlspath[128], lang[8];
443
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
444
  }
1282 mateusz.vi 445
 
1286 mateusz.vi 446
  fname = parseargv();
447
 
448
  if (fname == NULL) {
1302 mateusz.vi 449
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
1275 mateusz.vi 450
    return(0);
451
  }
452
 
1282 mateusz.vi 453
  /* load file */
1287 mateusz.vi 454
  if (loadfile(&db, fname) != 0) return(1);
1282 mateusz.vi 455
 
1275 mateusz.vi 456
  /* add an empty line at end if not present already */
457
  if (db.cursor->len != 0) line_add(&db, "");
458
 
459
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
460
  ui_basic(screenw, screenh, fname);
461
 
462
  db_rewind(&db);
463
 
464
  for (;;) {
465
    int k;
466
 
1282 mateusz.vi 467
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 468
    mdr_cout_locate(cursorposy, cursorposx);
469
 
1282 mateusz.vi 470
    if (uidirtyfrom != 0xff) {
1310 mateusz.vi 471
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto, cursorposy);
1282 mateusz.vi 472
      uidirtyfrom = 0xff;
473
    }
1275 mateusz.vi 474
 
475
    k = keyb_getkey();
1282 mateusz.vi 476
 
1275 mateusz.vi 477
    if (k == 0x150) { /* down */
1282 mateusz.vi 478
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 479
 
480
    } else if (k == 0x148) { /* up */
1282 mateusz.vi 481
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 482
 
483
    } else if (k == 0x14D) { /* right */
1308 mateusz.vi 484
      cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 485
 
486
    } else if (k == 0x14B) { /* left */
1302 mateusz.vi 487
      cursor_left(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 488
 
1282 mateusz.vi 489
    } else if (k == 0x149) { /* pgup */
490
      // TODO
491
 
492
    } else if (k == 0x151) { /* pgdown */
493
      // TODO
494
 
495
    } else if (k == 0x147) { /* home */
496
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
497
 
498
    } else if (k == 0x14F) { /* end */
499
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
500
 
1275 mateusz.vi 501
    } else if (k == 0x1B) { /* ESC */
502
      break;
503
 
1289 mateusz.vi 504
    } else if (k == 0x0D) { /* ENTER */
505
      /* add a new line */
506
      if (line_add(&db, db.cursor->payload + db.xoffset + cursorposx) == 0) {
507
        /* trim the line above */
508
        db.cursor->prev->len = db.xoffset + cursorposx;
509
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
510
        /* move cursor to the (new) line below */
511
        cursorposx = 0;
512
        if (cursorposy < screenh - 2) {
513
          uidirtyfrom = cursorposy;
514
          cursorposy++;
515
        } else {
516
          uidirtyfrom = 0;
517
        }
518
        uidirtyto = 0xff;
519
      } else {
520
        /* ERROR: OUT OF MEMORY */
521
      }
522
 
1292 mateusz.vi 523
    } else if (k == 0x153) {  /* DEL */
524
      del(&db, cursorposx, cursorposy, &uidirtyfrom, &uidirtyto);
525
 
1302 mateusz.vi 526
    } else if (k == 0x008) { /* BKSPC */
527
      bkspc(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
528
 
1308 mateusz.vi 529
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
530
      struct line far *n;
531
      n = _frealloc(db.cursor, sizeof(struct line) + db.cursor->len);
532
      if (n != NULL) {
533
        unsigned short off = db.xoffset + cursorposx;
534
        if (n->prev) n->prev->next = n;
535
        if (n->next) n->next->prev = n;
536
        db.cursor = n;
537
        _fmemmove(db.cursor->payload + off + 1, db.cursor->payload + off, db.cursor->len - off + 1);
538
        db.cursor->len += 1;
539
        uidirtyfrom = cursorposy;
540
        uidirtyto = cursorposy;
541
        db.cursor->payload[off] = k;
542
        cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
543
      }
544
 
1309 mateusz.vi 545
    } else if (k == 0x13b) { /* F1 */
546
      ui_help(screenw);
547
      uidirtyfrom = 0;
548
      uidirtyto = 0xff;
549
 
1311 mateusz.vi 550
    } else if (k == 0x13f) { /* F5 */
1312 mateusz.vi 551
      const char *m[] = {"SAVED", "SAVING FILE FAILED"};
552
      int i;
553
      i = !!savefile(&db, fname);
554
      ui_msg(m[i], screenw, screenh, &uidirtyfrom, &uidirtyto);
555
      mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1311 mateusz.vi 556
 
1282 mateusz.vi 557
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
558
      char buff[4];
559
      const char *HEX = "0123456789ABCDEF";
560
      buff[0] = HEX[(k >> 8) & 15];
561
      buff[1] = HEX[(k >> 4) & 15];
562
      buff[2] = HEX[k & 15];
563
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
564
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
1275 mateusz.vi 565
      keyb_getkey();
566
      break;
567
    }
568
  }
569
 
570
  mdr_cout_close();
571
 
572
  /* TODO free memory */
573
 
574
  return(0);
575
}