Subversion Repositories SvarDOS

Rev

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