Subversion Repositories SvarDOS

Rev

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