Subversion Repositories SvarDOS

Rev

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