Subversion Repositories SvarDOS

Rev

Rev 1392 | Rev 1394 | 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
 
1338 mateusz.vi 26
#include <dos.h>      /* _dos_open(), _dos_read(), _dos_close(), ... */
27
#include <fcntl.h>    /* O_RDONLY, O_WRONLY */
1275 mateusz.vi 28
#include <string.h>
29
 
1312 mateusz.vi 30
#include "mdr\bios.h"
1275 mateusz.vi 31
#include "mdr\cout.h"
1307 mateusz.vi 32
#include "mdr\dos.h"
1275 mateusz.vi 33
#include "mdr\keyb.h"
34
 
1282 mateusz.vi 35
#include "svarlang\svarlang.h"
36
 
1326 mateusz.vi 37
 
1356 mateusz.vi 38
#define PVER "2023.0"
39
#define PDATE "2023"
40
 
1326 mateusz.vi 41
/*****************************************************************************
42
 * global variables and definitions                                          *
43
 *****************************************************************************/
44
 
1275 mateusz.vi 45
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
1333 mateusz.vi 46
static unsigned char SCHEME_TEXT   = 0x07,
1354 mateusz.vi 47
                     SCHEME_MENU   = 0x70,
48
                     SCHEME_MENU_CUR= 0x0f,
49
                     SCHEME_MENU_SEL= 0x00,
1333 mateusz.vi 50
                     SCHEME_STBAR1 = 0x70,
1358 mateusz.vi 51
                     SCHEME_STBAR2 = 0x70, /* greyed out information */
52
                     SCHEME_STBAR3 = 0x70, /* query */
1333 mateusz.vi 53
                     SCHEME_SCROLL = 0x70,
54
                     SCHEME_MSG    = 0x70,
1358 mateusz.vi 55
                     SCHEME_ERR    = 0x70;
1327 mateusz.vi 56
 
1326 mateusz.vi 57
static unsigned char screenw, screenh;
1275 mateusz.vi 58
 
1327 mateusz.vi 59
static struct {
60
    unsigned char from;
61
    unsigned char to;
1342 mateusz.vi 62
    unsigned char statusbar;
63
} uidirty = {0, 0xff, 1}; /* make sure to redraw entire UI at first run */
1327 mateusz.vi 64
 
1275 mateusz.vi 65
#define SCROLL_CURSOR 0xB1
66
 
67
struct line {
1288 mateusz.vi 68
  struct line far *prev;
69
  struct line far *next;
1275 mateusz.vi 70
  unsigned short len;
71
  char payload[1];
72
};
73
 
1315 mateusz.vi 74
struct file {
1288 mateusz.vi 75
  struct line far *cursor;
1275 mateusz.vi 76
  unsigned short xoffset;
1348 mateusz.vi 77
  unsigned short cursorposx;
78
  unsigned short cursorposy;
1328 mateusz.vi 79
  unsigned short totlines;
80
  unsigned short curline;
1330 mateusz.vi 81
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
82
  char modflag;  /* non-zero if file has been modified since last save */
1355 mateusz.vi 83
  char modflagprev;
1332 mateusz.vi 84
  char fname[128];
1275 mateusz.vi 85
};
86
 
87
 
1326 mateusz.vi 88
/*****************************************************************************
89
 * functions                                                                 *
90
 *****************************************************************************/
91
 
1339 mateusz.vi 92
static struct line far *line_calloc(unsigned short siz) {
1342 mateusz.vi 93
  struct line far *res;
1339 mateusz.vi 94
  unsigned int seg;
1341 mateusz.vi 95
  if (_dos_allocmem((sizeof(struct line) + siz + 15) / 16, &seg) != 0) return(NULL);
1342 mateusz.vi 96
  res = MK_FP(seg, 0);
97
  _fmemset(res, 0, sizeof(struct line) + siz);
1339 mateusz.vi 98
  return(MK_FP(seg, 0));
99
}
100
 
101
 
102
static void line_free(struct line far *ptr) {
103
  _dos_freemem(FP_SEG(ptr));
104
}
105
 
106
 
1348 mateusz.vi 107
static int curline_resize(struct file far *db, unsigned short newsiz) {
1339 mateusz.vi 108
  unsigned int maxavail;
109
  struct line far *newptr;
110
 
1341 mateusz.vi 111
  /* try resizing the block (much faster) */
112
  if (_dos_setblock((sizeof(struct line) + newsiz + 15) / 16, FP_SEG(db->cursor), &maxavail) == 0) return(0);
1339 mateusz.vi 113
 
114
  /* create a new block and copy data over */
115
  newptr = line_calloc(newsiz);
1340 mateusz.vi 116
  if (newptr == NULL) return(-1);
117
  _fmemmove(newptr, db->cursor, sizeof(struct line) + db->cursor->len);
118
 
119
  /* rewire the linked list */
120
  db->cursor = newptr;
121
  if (newptr->next) newptr->next->prev = newptr;
122
  if (newptr->prev) newptr->prev->next = newptr;
123
 
124
  return(0);
1339 mateusz.vi 125
}
126
 
127
 
1328 mateusz.vi 128
/* adds a new line at cursor position into file linked list and advance cursor
1324 mateusz.vi 129
 * returns non-zero on error */
1328 mateusz.vi 130
static int line_add(struct file *db, const char far *line, unsigned short slen) {
1288 mateusz.vi 131
  struct line far *l;
1275 mateusz.vi 132
 
1339 mateusz.vi 133
  l = line_calloc(slen);
1287 mateusz.vi 134
  if (l == NULL) return(-1);
135
 
1275 mateusz.vi 136
  l->prev = db->cursor;
137
  if (db->cursor) {
138
    l->next = db->cursor->next;
139
    db->cursor->next = l;
140
    l->next->prev = l;
141
  }
142
  db->cursor = l;
1342 mateusz.vi 143
  if (slen > 0) {
144
    _fmemmove(l->payload, line, slen);
145
    l->len = slen;
146
  }
1287 mateusz.vi 147
 
1328 mateusz.vi 148
  db->totlines += 1;
149
  db->curline += 1;
150
 
1287 mateusz.vi 151
  return(0);
1275 mateusz.vi 152
}
153
 
154
 
1348 mateusz.vi 155
static void ui_getstring(const char *query, char *s, unsigned short maxlen) {
156
  unsigned short len = 0;
157
  unsigned char y, x;
1332 mateusz.vi 158
  int k;
159
 
160
  if (maxlen == 0) return;
161
  maxlen--; /* make room for the nul terminator */
162
 
163
  y = screenh - 1;
164
 
165
  /* print query string */
1333 mateusz.vi 166
  x = mdr_cout_str(y, 0, query, SCHEME_STBAR3, 40);
167
  mdr_cout_char_rep(y, x++, ' ', SCHEME_STBAR3, screenw - x);
1332 mateusz.vi 168
 
169
  for (;;) {
170
    mdr_cout_locate(y, x + len);
171
    k = keyb_getkey();
172
 
1345 mateusz.vi 173
    switch (k) {
174
      case 0x1b: /* ESC */
175
        s[0] = 0;
176
        return;
177
      case '\r':
178
        s[len] = 0;
179
        return;
180
      case 0x08: /* BKSPC */
181
        if (len > 0) {
182
          len--;
183
          mdr_cout_char(y, x + len, ' ', SCHEME_STBAR3);
184
        }
185
        break;
186
      default:
187
        if ((k <= 0xff) && (k >= ' ') && (len < maxlen)) {
188
          mdr_cout_char(y, x + len, k, SCHEME_STBAR3);
189
          s[len++] = k;
190
        }
1332 mateusz.vi 191
    }
1345 mateusz.vi 192
  }
1332 mateusz.vi 193
 
194
}
195
 
196
 
1324 mateusz.vi 197
/* append a nul-terminated string to line at cursor position */
198
static int line_append(struct file *f, const char far *buf, unsigned short len) {
199
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
1340 mateusz.vi 200
  if (curline_resize(f, f->cursor->len + len) != 0) return(-1);
1337 mateusz.vi 201
  _fmemmove(f->cursor->payload + f->cursor->len, buf, len);
1324 mateusz.vi 202
  f->cursor->len += len;
203
 
204
  return(0);
205
}
206
 
207
 
1315 mateusz.vi 208
static void db_rewind(struct file *db) {
1275 mateusz.vi 209
  if (db->cursor == NULL) return;
210
  while (db->cursor->prev) db->cursor = db->cursor->prev;
1328 mateusz.vi 211
  db->curline = 0;
1275 mateusz.vi 212
}
213
 
214
 
1355 mateusz.vi 215
static void ui_basic(const struct file *db, unsigned short slotnum) {
1356 mateusz.vi 216
  const char *s = svarlang_strid(0); /* ESC=MENU */
1348 mateusz.vi 217
  unsigned short helpcol = screenw - strlen(s);
1275 mateusz.vi 218
 
1355 mateusz.vi 219
  /* slot number */
220
  {
221
    char slot[4] = "#00";
222
    slotnum++;
223
    slot[1] += (slotnum / 10);
224
    slot[2] += (slotnum % 10);
225
    mdr_cout_str(screenh - 1, 0, slot, SCHEME_STBAR2, 3);
226
  }
1313 mateusz.vi 227
 
1355 mateusz.vi 228
  /* fill rest of status bar with background */
229
  mdr_cout_char_rep(screenh - 1, 3, ' ', SCHEME_STBAR1, screenw - 1);
230
 
1393 mateusz.vi 231
  /* filename and modflag */
1330 mateusz.vi 232
  {
233
    const char *fn = db->fname;
1355 mateusz.vi 234
    unsigned short x;
1330 mateusz.vi 235
    if (*fn == 0) fn = svarlang_str(0, 1);
1355 mateusz.vi 236
    x = mdr_cout_str(screenh - 1, 4, fn, SCHEME_STBAR1, screenw);
1393 mateusz.vi 237
    if (db->modflag) mdr_cout_char(screenh - 1, 5 + x, '!', SCHEME_STBAR2);
1321 mateusz.vi 238
  }
1313 mateusz.vi 239
 
240
  /* eol type */
1330 mateusz.vi 241
  {
242
    const char *eoltype = "CRLF";
1353 mateusz.vi 243
    if (db->lfonly) eoltype += 2;
1335 mateusz.vi 244
    mdr_cout_str(screenh - 1, helpcol - 6, eoltype, SCHEME_STBAR1, 5);
1275 mateusz.vi 245
  }
1313 mateusz.vi 246
 
1335 mateusz.vi 247
  mdr_cout_str(screenh - 1, helpcol, s, SCHEME_STBAR2, 40);
1275 mateusz.vi 248
}
249
 
250
 
1331 mateusz.vi 251
static void ui_msg(const char *msg1, const char *msg2, unsigned char attr) {
1312 mateusz.vi 252
  unsigned short x, y, msglen, i;
1331 mateusz.vi 253
  unsigned char msg2flag = 0;
254
 
255
  msglen = strlen(msg1);
256
  if (msg2) {
257
    msg2flag = 1;
258
    i = strlen(msg2);
259
    if (i > msglen) msglen = i;
260
  }
261
 
262
  y = (screenh - 6) >> 1;
1312 mateusz.vi 263
  x = (screenw - msglen - 4) >> 1;
1331 mateusz.vi 264
  for (i = y+2+msg2flag; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
265
  x++;
1345 mateusz.vi 266
 
1331 mateusz.vi 267
  mdr_cout_str(y+1, x, msg1, attr, msglen);
268
  if (msg2) mdr_cout_str(y+2, x, msg2, attr, msglen);
1312 mateusz.vi 269
 
1327 mateusz.vi 270
  if (uidirty.from > y) uidirty.from = y;
1331 mateusz.vi 271
  if (uidirty.to < y+4) uidirty.to = y+4;
1312 mateusz.vi 272
}
273
 
274
 
1342 mateusz.vi 275
static unsigned char ui_confirm_if_unsaved(struct file *db) {
1355 mateusz.vi 276
  unsigned char r = 0;
1342 mateusz.vi 277
  if (db->modflag == 0) return(0);
278
 
1355 mateusz.vi 279
  mdr_cout_cursor_hide();
280
 
1342 mateusz.vi 281
  /* if file has been modified then ask for confirmation */
282
  ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
1355 mateusz.vi 283
  if (keyb_getkey() != '\r') r = 1;
1342 mateusz.vi 284
 
1355 mateusz.vi 285
  mdr_cout_cursor_show();
286
 
287
  return(r);
1342 mateusz.vi 288
}
289
 
290
 
1327 mateusz.vi 291
static void ui_refresh(const struct file *db) {
1318 mateusz.vi 292
  unsigned char x;
1310 mateusz.vi 293
  const struct line far *l;
1316 mateusz.vi 294
  unsigned char y = db->cursorposy;
1275 mateusz.vi 295
 
1392 mateusz.vi 296
  /* quit early if nothing to refresh */
297
  if (uidirty.from == 0xff) return;
298
 
1288 mateusz.vi 299
#ifdef DBG_REFRESH
1282 mateusz.vi 300
  static char m = 'a';
301
  m++;
302
  if (m > 'z') m = 'a';
1288 mateusz.vi 303
#endif
1282 mateusz.vi 304
 
1310 mateusz.vi 305
  /* rewind cursor line to first line that needs redrawing */
1327 mateusz.vi 306
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
1282 mateusz.vi 307
 
1310 mateusz.vi 308
  /* iterate over lines and redraw whatever needs to be redrawn */
309
  for (; l != NULL; l = l->next, y++) {
310
 
311
    /* skip lines that do not need to be refreshed */
1327 mateusz.vi 312
    if (y < uidirty.from) continue;
313
    if (y > uidirty.to) break;
1282 mateusz.vi 314
 
1318 mateusz.vi 315
    x = 0;
1275 mateusz.vi 316
    if (db->xoffset < l->len) {
1318 mateusz.vi 317
      unsigned char i, limit;
318
      if (l->len - db->xoffset < screenw) {
319
        limit = l->len;
320
      } else {
321
        limit = db->xoffset + screenw - 1;
322
      }
1333 mateusz.vi 323
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
1275 mateusz.vi 324
    }
1282 mateusz.vi 325
 
1318 mateusz.vi 326
    /* write empty spaces until end of line */
1333 mateusz.vi 327
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenw - 1 - x);
1318 mateusz.vi 328
 
1288 mateusz.vi 329
#ifdef DBG_REFRESH
1333 mateusz.vi 330
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
1288 mateusz.vi 331
#endif
1282 mateusz.vi 332
 
1275 mateusz.vi 333
    if (y == screenh - 2) break;
334
  }
1310 mateusz.vi 335
 
1319 mateusz.vi 336
  /* fill all lines below if empty (and they need to be redrawn) */
337
  if (l == NULL) {
1327 mateusz.vi 338
    while ((y < screenh - 1) && (y < uidirty.to)) {
1333 mateusz.vi 339
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenw - 1);
1319 mateusz.vi 340
    }
1318 mateusz.vi 341
  }
1328 mateusz.vi 342
 
343
  /* scroll bar */
344
  for (y = 0; y < (screenh - 1); y++) {
1333 mateusz.vi 345
    mdr_cout_char(y, screenw - 1, SCROLL_CURSOR, SCHEME_SCROLL);
1328 mateusz.vi 346
  }
347
 
348
  /* scroll cursor */
349
  if (db->totlines >= screenh) {
350
    unsigned short topline = db->curline - db->cursorposy;
351
    unsigned short col;
352
    unsigned short totlines = db->totlines - screenh + 1;
353
    if (db->totlines - screenh > screenh) {
354
      col = topline / (totlines / (screenh - 1));
355
    } else {
356
      col = topline * (screenh - 1) / totlines;
357
    }
358
    if (col >= screenh - 1) col = screenh - 2;
1333 mateusz.vi 359
    mdr_cout_char(col, screenw - 1, ' ', SCHEME_SCROLL);
1328 mateusz.vi 360
  }
1392 mateusz.vi 361
 
362
  /* clear out the dirty flag */
363
  uidirty.from = 0xff;
1275 mateusz.vi 364
}
365
 
366
 
1327 mateusz.vi 367
static void check_cursor_not_after_eol(struct file *db) {
1316 mateusz.vi 368
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
1275 mateusz.vi 369
 
370
  if (db->cursor->len < db->xoffset) {
1316 mateusz.vi 371
    db->cursorposx = 0;
1275 mateusz.vi 372
    db->xoffset = db->cursor->len;
1327 mateusz.vi 373
    uidirty.from = 0;
374
    uidirty.to = 0xff;
1275 mateusz.vi 375
  } else {
1316 mateusz.vi 376
    db->cursorposx = db->cursor->len - db->xoffset;
1275 mateusz.vi 377
  }
378
}
379
 
380
 
1327 mateusz.vi 381
static void cursor_up(struct file *db) {
1275 mateusz.vi 382
  if (db->cursor->prev != NULL) {
1328 mateusz.vi 383
    db->curline -= 1;
1275 mateusz.vi 384
    db->cursor = db->cursor->prev;
1316 mateusz.vi 385
    if (db->cursorposy == 0) {
1327 mateusz.vi 386
      uidirty.from = 0;
387
      uidirty.to = 0xff;
1275 mateusz.vi 388
    } else {
1316 mateusz.vi 389
      db->cursorposy -= 1;
1275 mateusz.vi 390
    }
391
  }
392
}
393
 
394
 
1327 mateusz.vi 395
static void cursor_eol(struct file *db) {
1275 mateusz.vi 396
  /* adjust xoffset to make sure eol is visible on screen */
1282 mateusz.vi 397
  if (db->xoffset > db->cursor->len) {
398
    db->xoffset = db->cursor->len - 1;
1327 mateusz.vi 399
    uidirty.from = 0;
400
    uidirty.to = 0xff;
1282 mateusz.vi 401
  }
402
 
403
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
404
    db->xoffset = db->cursor->len - screenw + 2;
1327 mateusz.vi 405
    uidirty.from = 0;
406
    uidirty.to = 0xff;
1282 mateusz.vi 407
  }
1316 mateusz.vi 408
  db->cursorposx = db->cursor->len - db->xoffset;
1275 mateusz.vi 409
}
410
 
411
 
1327 mateusz.vi 412
static void cursor_down(struct file *db) {
1276 mateusz.vi 413
  if (db->cursor->next != NULL) {
1328 mateusz.vi 414
    db->curline += 1;
1276 mateusz.vi 415
    db->cursor = db->cursor->next;
1316 mateusz.vi 416
    if (db->cursorposy < screenh - 2) {
417
      db->cursorposy += 1;
1276 mateusz.vi 418
    } else {
1327 mateusz.vi 419
      uidirty.from = 0;
420
      uidirty.to = 0xff;
1276 mateusz.vi 421
    }
422
  }
423
}
424
 
425
 
1327 mateusz.vi 426
static void cursor_left(struct file *db) {
1316 mateusz.vi 427
  if (db->cursorposx > 0) {
428
    db->cursorposx -= 1;
1302 mateusz.vi 429
  } else if (db->xoffset > 0) {
430
    db->xoffset -= 1;
1327 mateusz.vi 431
    uidirty.from = 0;
432
    uidirty.to = 0xff;
1302 mateusz.vi 433
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
1327 mateusz.vi 434
    cursor_up(db);
435
    cursor_eol(db);
1302 mateusz.vi 436
  }
437
}
438
 
439
 
1327 mateusz.vi 440
static void cursor_home(struct file *db) {
1316 mateusz.vi 441
  db->cursorposx = 0;
1282 mateusz.vi 442
  if (db->xoffset != 0) {
443
    db->xoffset = 0;
1327 mateusz.vi 444
    uidirty.from = 0;
445
    uidirty.to = 0xff;
1282 mateusz.vi 446
  }
1276 mateusz.vi 447
}
448
 
449
 
1327 mateusz.vi 450
static void cursor_right(struct file *db) {
1316 mateusz.vi 451
  if (db->cursor->len > db->xoffset + db->cursorposx) {
452
    if (db->cursorposx < screenw - 2) {
453
      db->cursorposx += 1;
1308 mateusz.vi 454
    } else {
455
      db->xoffset += 1;
1327 mateusz.vi 456
      uidirty.from = 0;
457
      uidirty.to = 0xff;
1308 mateusz.vi 458
    }
1357 mateusz.vi 459
  } else if (db->cursor->next != NULL) { /* jump to start of next line */
1327 mateusz.vi 460
    cursor_down(db);
461
    cursor_home(db);
1308 mateusz.vi 462
  }
463
}
464
 
465
 
1327 mateusz.vi 466
static void del(struct file *db) {
1316 mateusz.vi 467
  if (db->cursorposx + db->xoffset < db->cursor->len) {
468
    _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 469
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
1327 mateusz.vi 470
    uidirty.from = db->cursorposy;
471
    uidirty.to = db->cursorposy;
1393 mateusz.vi 472
    db->modflag = 1;
1292 mateusz.vi 473
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
474
    struct line far *nextline = db->cursor->next;
475
    if (db->cursor->next->len > 0) {
1340 mateusz.vi 476
      if (curline_resize(db, db->cursor->len + db->cursor->next->len + 1) == 0) {
1337 mateusz.vi 477
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
1292 mateusz.vi 478
        db->cursor->len += db->cursor->next->len;
479
      }
480
    }
1340 mateusz.vi 481
 
1292 mateusz.vi 482
    db->cursor->next = db->cursor->next->next;
483
    db->cursor->next->prev = db->cursor;
1340 mateusz.vi 484
 
1339 mateusz.vi 485
    line_free(nextline);
1327 mateusz.vi 486
    uidirty.from = db->cursorposy;
487
    uidirty.to = 0xff;
1328 mateusz.vi 488
    db->totlines -= 1;
1393 mateusz.vi 489
    db->modflag = 1;
1292 mateusz.vi 490
  }
491
}
492
 
493
 
1327 mateusz.vi 494
static void bkspc(struct file *db) {
1302 mateusz.vi 495
 
496
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
1316 mateusz.vi 497
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
1302 mateusz.vi 498
 
1327 mateusz.vi 499
  cursor_left(db);
500
  del(db);
1302 mateusz.vi 501
}
502
 
503
 
1346 mateusz.vi 504
/* returns 0 on success, 1 on file not found, 2 on other error */
1359 mateusz.vi 505
static int loadfile(struct file *db, const char *fname) {
1324 mateusz.vi 506
  char buff[512]; /* read one entire sector at a time (faster) */
507
  char *buffptr;
508
  unsigned int len, llen;
1287 mateusz.vi 509
  int fd;
1324 mateusz.vi 510
  unsigned char eolfound;
1287 mateusz.vi 511
 
1355 mateusz.vi 512
  /* free the entire linked list of lines */
513
  db_rewind(db);
514
  while (db->cursor) {
515
    struct line far *victim;
516
    victim = db->cursor;
517
    db->cursor = db->cursor->next;
518
    line_free(victim);
519
  }
1321 mateusz.vi 520
 
1355 mateusz.vi 521
  /* zero out the struct */
522
  bzero(db, sizeof(struct file));
523
 
1321 mateusz.vi 524
  if (*fname == 0) goto SKIPLOADING;
525
 
1355 mateusz.vi 526
  memcpy(db->fname, fname, strlen(fname));
527
 
1287 mateusz.vi 528
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
1346 mateusz.vi 529
    return(1);
1287 mateusz.vi 530
  }
531
 
1313 mateusz.vi 532
  db->lfonly = 1;
533
 
1324 mateusz.vi 534
  /* start by adding an empty line */
1328 mateusz.vi 535
  if (line_add(db, NULL, 0) != 0) {
1324 mateusz.vi 536
    /* TODO ERROR HANDLING */
537
  }
1287 mateusz.vi 538
 
1324 mateusz.vi 539
  for (eolfound = 0;;) {
540
    unsigned short consumedbytes;
541
 
542
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
543
    buffptr = buff;
544
 
545
    FINDLINE:
546
 
1361 mateusz.vi 547
    /* look for nearest \n (also expand tabs) */
1324 mateusz.vi 548
    for (consumedbytes = 0;; consumedbytes++) {
1361 mateusz.vi 549
 
550
      if (buffptr[consumedbytes] == '\t') {
551
        llen = consumedbytes;
552
        break;
553
      }
554
 
1324 mateusz.vi 555
      if (consumedbytes == len) {
556
        llen = consumedbytes;
557
        break;
1323 mateusz.vi 558
      }
1324 mateusz.vi 559
      if (buffptr[consumedbytes] == '\r') {
560
        llen = consumedbytes;
561
        consumedbytes++;
562
        db->lfonly = 0;
1320 mateusz.vi 563
        break;
564
      }
1324 mateusz.vi 565
      if (buffptr[consumedbytes] == '\n') {
566
        eolfound = 1;
567
        llen = consumedbytes;
568
        consumedbytes++;
569
        break;
570
      }
1287 mateusz.vi 571
    }
1324 mateusz.vi 572
 
573
    /* consumedbytes is the amount of bytes processed from buffptr,
574
     * llen is the length of line's payload (without its line terminator) */
575
 
576
    /* append content, if line is non-empty */
577
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
1339 mateusz.vi 578
      goto IOERR;
1287 mateusz.vi 579
    }
580
 
1324 mateusz.vi 581
    /* add a new line if necessary */
582
    if (eolfound) {
1328 mateusz.vi 583
      if (line_add(db, NULL, 0) != 0) {
1339 mateusz.vi 584
        goto IOERR;
1324 mateusz.vi 585
      }
586
      eolfound = 0;
587
    }
1287 mateusz.vi 588
 
1361 mateusz.vi 589
    /* append 8 spaces if tab char found */
590
    if ((consumedbytes < len) && (buffptr[consumedbytes] == '\t')) {
591
      consumedbytes++;
592
      if (line_append(db, "        ", 8) != 0) {
593
        goto IOERR;
594
      }
595
    }
596
 
1324 mateusz.vi 597
    /* anything left? process the buffer leftover again */
598
    if (consumedbytes < len) {
599
      len -= consumedbytes;
600
      buffptr += consumedbytes;
601
      goto FINDLINE;
602
    }
603
 
604
  }
605
 
1287 mateusz.vi 606
  _dos_close(fd);
607
 
1321 mateusz.vi 608
  SKIPLOADING:
609
 
1320 mateusz.vi 610
  /* add an empty line at end if not present already, also rewind cursor to top of file */
1339 mateusz.vi 611
  if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
612
  db_rewind(db);
1320 mateusz.vi 613
 
1346 mateusz.vi 614
  return(0);
1339 mateusz.vi 615
 
616
  IOERR:
617
  _dos_close(fd);
1346 mateusz.vi 618
  return(2);
1287 mateusz.vi 619
}
620
 
621
 
1356 mateusz.vi 622
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
623
 * of argc and argv, saves some 330 bytes of binary size
624
 * returns non-zero on error */
625
static int parseargv(struct file *dbarr) {
626
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
627
  unsigned short count = 0;
628
  char *arg;
629
  unsigned short lastarg = 0;
1359 mateusz.vi 630
  int err;
1356 mateusz.vi 631
 
632
  while (!lastarg) {
633
    /* jump to nearest arg */
634
    while (*tail == ' ') {
635
      *tail = 0;
636
      tail++;
637
    }
638
 
639
    if (*tail == '\r') {
640
      *tail = 0;
641
      break;
642
    }
643
 
644
    arg = tail;
645
 
646
    /* jump to next delimiter */
647
    while ((*tail != ' ') && (*tail != '\r')) tail++;
648
 
649
    /* if \r then remember this is the last arg */
650
    if (*tail == '\r') lastarg = 1;
651
 
652
    *tail = 0;
653
    tail++;
654
 
655
    /* look at the arg now */
656
    if (*arg == '/') {
1387 mateusz.vi 657
      const char far *self = mdr_dos_selfexe();
658
      unsigned short i;
659
      if (self == NULL) self = "sved";
660
      for (i = 0; self[i] != 0; i++) {
661
        if (self[i] == '\\') {
662
          self += i + 1;
663
          i = 0;
664
        }
665
      }
1390 mateusz.vi 666
      mdr_coutraw_str(svarlang_str(1,3));
667
      mdr_coutraw_str(" [");
668
      mdr_coutraw_str(svarlang_str(1,4));
669
      mdr_coutraw_puts(" " PVER "]");
670
      mdr_coutraw_puts("Copyright (C) " PDATE " Mateusz Viste");
1387 mateusz.vi 671
      mdr_coutraw_crlf();
672
      mdr_coutraw_str(svarlang_str(1,0)); /* usage: */
673
      mdr_coutraw_char(' ');
674
      while (*self != 0) {
675
        mdr_coutraw_char(*self);
676
        self++;
677
      }
678
      mdr_coutraw_char(' ');
679
      mdr_coutraw_puts(svarlang_str(1,1)); /* args syntax */
1356 mateusz.vi 680
      return(-1);
681
    }
682
 
683
    /* looks to be a filename */
684
    if (count == 10) {
685
      mdr_coutraw_puts(svarlang_str(0,12));
686
      return(-1); /* too many files */
687
    }
688
 
689
    /* try loading it */
1387 mateusz.vi 690
    mdr_coutraw_str(svarlang_str(1,2));
691
    mdr_coutraw_char(' ');
1356 mateusz.vi 692
    mdr_coutraw_puts(arg);
693
    err = loadfile(&(dbarr[count]), arg);
694
    if (err) {
695
      if (err == 1) { /* file not found */
696
        err = 11;
697
      } else { /* general error */
698
        err = 10;
699
      }
700
      mdr_coutraw_puts(svarlang_str(0,err));
701
      return(-1);
702
    }
703
    count++;
704
  }
705
 
706
  return(0);
707
}
708
 
709
 
1332 mateusz.vi 710
static int savefile(const struct file *db, const char *newfname) {
1311 mateusz.vi 711
  int fd;
712
  const struct line far *l;
713
  unsigned bytes;
1313 mateusz.vi 714
  unsigned char eollen;
715
  unsigned char eolbuf[2];
1332 mateusz.vi 716
  int errflag = 0;
1313 mateusz.vi 717
 
1332 mateusz.vi 718
  /* either create a new file if newfname provided, or... */
719
  if (newfname) {
720
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
721
  } else { /* ...open db->fname */
722
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
1311 mateusz.vi 723
  }
724
 
725
  l = db->cursor;
726
  while (l->prev) l = l->prev;
727
 
1313 mateusz.vi 728
  /* preset line terminators */
729
  if (db->lfonly) {
730
    eolbuf[0] = '\n';
731
    eollen = 1;
732
  } else {
733
    eolbuf[0] = '\r';
734
    eolbuf[1] = '\n';
735
    eollen = 2;
736
  }
737
 
1311 mateusz.vi 738
  while (l) {
1324 mateusz.vi 739
    /* do not write the last empty line, it is only useful for edition */
740
    if (l->len != 0) {
1332 mateusz.vi 741
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
1324 mateusz.vi 742
    } else if (l->next == NULL) {
743
      break;
744
    }
1332 mateusz.vi 745
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
1311 mateusz.vi 746
    l = l->next;
747
  }
748
 
1332 mateusz.vi 749
  errflag |= _dos_close(fd);
1330 mateusz.vi 750
 
1332 mateusz.vi 751
  return(errflag);
1311 mateusz.vi 752
}
753
 
754
 
1327 mateusz.vi 755
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
1340 mateusz.vi 756
  if (curline_resize(db, db->cursor->len + len) == 0) {
1317 mateusz.vi 757
    unsigned short off = db->xoffset + db->cursorposx;
1393 mateusz.vi 758
    db->modflag = 1;
1317 mateusz.vi 759
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
760
    db->cursor->len += len;
1327 mateusz.vi 761
    uidirty.from = db->cursorposy;
762
    uidirty.to = db->cursorposy;
1317 mateusz.vi 763
    while (len--) {
764
      db->cursor->payload[off++] = *databuf;
765
      databuf++;
1327 mateusz.vi 766
      cursor_right(db);
1317 mateusz.vi 767
    }
768
  }
769
}
770
 
771
 
1343 mateusz.vi 772
/* recompute db->curline by counting nodes in linked list */
773
static void recompute_curline(struct file *db) {
774
  const struct line far *l = db->cursor;
775
 
776
  db->curline = 0;
777
  while (l->prev != NULL) {
778
    db->curline += 1;
779
    l = l->prev;
780
  }
781
}
782
 
783
 
1354 mateusz.vi 784
enum MENU_ACTION {
1393 mateusz.vi 785
  MENU_NONE   = 0,
786
  MENU_OPEN   = 1,
787
  MENU_SAVE   = 2,
788
  MENU_SAVEAS = 3,
789
  MENU_CLOSE  = 4,
790
  MENU_CHGEOL = 5,
791
  MENU_QUIT   = 6,
1354 mateusz.vi 792
};
793
 
794
static enum MENU_ACTION ui_menu(void) {
795
  unsigned short i, curchoice, attr, x, slen;
1393 mateusz.vi 796
  unsigned short xorigin, yorigin;
1354 mateusz.vi 797
  uidirty.from = 0;
798
  uidirty.to = 0xff;
799
  uidirty.statusbar = 1;
800
 
801
  /* find out the longest string */
802
  slen = 0;
1355 mateusz.vi 803
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
1354 mateusz.vi 804
    x = strlen(svarlang_str(8, i));
805
    if (x > slen) slen = x;
806
  }
807
 
1393 mateusz.vi 808
  /* calculate where to draw the menu on screen */
809
  xorigin = (screenw - (slen + 5)) / 2;
810
  yorigin = (screenh - (MENU_QUIT - MENU_OPEN + 5)) / 2;
811
 
1355 mateusz.vi 812
  curchoice = MENU_OPEN;
1354 mateusz.vi 813
  for (;;) {
814
    /* render menu */
1393 mateusz.vi 815
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
816
      mdr_cout_char_rep(yorigin + i, xorigin, ' ', SCHEME_MENU, slen+4);
1354 mateusz.vi 817
      if (i == curchoice) {
818
        attr = SCHEME_MENU_CUR;
1393 mateusz.vi 819
        mdr_cout_char(yorigin + i, xorigin + 1, '>', SCHEME_MENU_SEL);
1354 mateusz.vi 820
      } else {
821
        attr = SCHEME_MENU;
822
      }
1393 mateusz.vi 823
      x = mdr_cout_str(yorigin + i, xorigin + 2, svarlang_str(8, i), attr, slen);
1354 mateusz.vi 824
      if (i == curchoice) {
1393 mateusz.vi 825
        mdr_cout_char_rep(yorigin + i, xorigin + x + 2, ' ', SCHEME_MENU_SEL, slen - x + 1);
826
        mdr_cout_locate(yorigin + i, xorigin + x + 2);
1354 mateusz.vi 827
      }
828
    }
829
    /* wait for key */
830
    switch (keyb_getkey()) {
831
      case '\r': return(curchoice); /* ENTER */
832
      case 0x150: /* down */
833
        if (curchoice == MENU_QUIT) {
1355 mateusz.vi 834
          curchoice = MENU_OPEN;
1354 mateusz.vi 835
        } else {
836
          curchoice++;
837
        }
838
        break;
839
      case 0x148: /* up */
1355 mateusz.vi 840
        if (curchoice == MENU_OPEN) {
1354 mateusz.vi 841
          curchoice = MENU_QUIT;
842
        } else {
843
          curchoice--;
844
        }
845
        break;
846
      default: return(MENU_NONE);
847
    }
848
  }
849
}
850
 
851
 
1355 mateusz.vi 852
static struct file *select_slot(struct file *dbarr, unsigned short curfile) {
853
  uidirty.from = 0;
854
  uidirty.to = 0xff;
855
  uidirty.statusbar = 1;
856
 
857
  dbarr = &(dbarr[curfile]);
858
  ui_basic(dbarr, curfile);
859
  ui_refresh(dbarr);
860
  return(dbarr);
861
}
862
 
863
 
1347 mateusz.vi 864
/* main returns nothing, ie. sved always exits with a zero exit code
865
 * (this saves 20 bytes of executable footprint) */
866
void main(void) {
1355 mateusz.vi 867
  static struct file dbarr[10];
868
  unsigned short curfile;
869
  struct file *db = dbarr; /* visible file is the first slot by default */
1275 mateusz.vi 870
 
1307 mateusz.vi 871
  {
1364 mateusz.vi 872
    unsigned short i = 0;
873
    const char far *selfptr;
874
    char self[128], lang[8];
875
    selfptr = mdr_dos_selfexe();
876
    if (selfptr != NULL) {
877
      do {
878
        self[i] = selfptr[i];
879
      } while (self[i++] != 0);
880
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
881
    }
1307 mateusz.vi 882
  }
1282 mateusz.vi 883
 
1355 mateusz.vi 884
  /* preload all slots with empty files */
885
  for (curfile = 9;; curfile--) {
886
    loadfile(&(dbarr[curfile]), "");
887
    if (curfile == 0) break;
888
  }
889
 
1356 mateusz.vi 890
  /* parse argv (and load files, if any passed on) */
891
  if (parseargv(dbarr) != 0) return;
1282 mateusz.vi 892
 
1333 mateusz.vi 893
  if (mdr_cout_init(&screenw, &screenh)) {
894
    /* load color scheme if mdr_cout_init returns a color flag */
895
    SCHEME_TEXT = 0x17;
1354 mateusz.vi 896
    SCHEME_MENU = 0x70;
1388 mateusz.vi 897
    SCHEME_MENU_CUR = 0x6f;
898
    SCHEME_MENU_SEL = 0x66;
1333 mateusz.vi 899
    SCHEME_STBAR1 = 0x70;
900
    SCHEME_STBAR2 = 0x78;
1392 mateusz.vi 901
    SCHEME_STBAR3 = 0x3f;
1333 mateusz.vi 902
    SCHEME_SCROLL = 0x70;
1388 mateusz.vi 903
    SCHEME_MSG = 0x6f;
1333 mateusz.vi 904
    SCHEME_ERR = 0x4f;
905
  }
1275 mateusz.vi 906
 
907
  for (;;) {
908
    int k;
909
 
1362 mateusz.vi 910
    /* add an extra empty line if cursor is on last line and this line is not empty */
911
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
912
      if (line_add(db, NULL, 0) == 0) {
913
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
914
      }
915
    }
916
 
1327 mateusz.vi 917
    check_cursor_not_after_eol(db);
1320 mateusz.vi 918
    mdr_cout_locate(db->cursorposy, db->cursorposx);
1275 mateusz.vi 919
 
1392 mateusz.vi 920
    ui_refresh(db);
921
 
1355 mateusz.vi 922
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag)) {
923
      ui_basic(db, curfile);
1342 mateusz.vi 924
      uidirty.statusbar = 0;
1355 mateusz.vi 925
      db->modflagprev = db->modflag;
1342 mateusz.vi 926
    }
1328 mateusz.vi 927
#ifdef DBG_LINENUM
928
      {
929
        char ddd[10];
930
        db->curline += 1;
931
        ddd[0] = '0' + db->curline / 100;
932
        ddd[1] = '0' + (db->curline % 100) / 10;
933
        ddd[2] = '0' + (db->curline % 10);
934
        db->curline -= 1;
935
        ddd[3] = '/';
936
        ddd[4] = '0' + db->totlines / 100;
937
        ddd[5] = '0' + (db->totlines % 100) / 10;
938
        ddd[6] = '0' + (db->totlines % 10);
939
        ddd[7] = 0;
1333 mateusz.vi 940
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
1328 mateusz.vi 941
      }
942
#endif
1275 mateusz.vi 943
 
944
    k = keyb_getkey();
1282 mateusz.vi 945
 
1275 mateusz.vi 946
    if (k == 0x150) { /* down */
1327 mateusz.vi 947
      cursor_down(db);
1275 mateusz.vi 948
 
949
    } else if (k == 0x148) { /* up */
1327 mateusz.vi 950
      cursor_up(db);
1275 mateusz.vi 951
 
952
    } else if (k == 0x14D) { /* right */
1327 mateusz.vi 953
      cursor_right(db);
1275 mateusz.vi 954
 
955
    } else if (k == 0x14B) { /* left */
1327 mateusz.vi 956
      cursor_left(db);
1275 mateusz.vi 957
 
1282 mateusz.vi 958
    } else if (k == 0x149) { /* pgup */
1334 mateusz.vi 959
      unsigned char dist = db->cursorposy + screenh - 1;
960
      while ((dist != 0) && (db->cursor->prev != NULL)) {
961
        db->cursor = db->cursor->prev;
962
        dist--;
963
      }
964
      if (dist != 0) {
965
        db->cursorposy = 0;
966
        db->cursorposx = 0;
967
      } else {
968
        dist = db->cursorposy;
969
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
970
      }
971
      uidirty.from = 0;
972
      uidirty.to = 0xff;
1343 mateusz.vi 973
      recompute_curline(db);
1282 mateusz.vi 974
 
975
    } else if (k == 0x151) { /* pgdown */
1334 mateusz.vi 976
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
977
      while ((dist != 0) && (db->cursor->next != NULL)) {
978
        db->cursor = db->cursor->next;
979
        dist--;
980
      }
981
      if (dist != 0) {
982
        db->cursorposy = screenh - 2;
983
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
984
        db->cursorposx = 0;
985
      } else {
986
        dist = screenh - 2 - db->cursorposy;
987
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
988
      }
989
      uidirty.from = 0;
990
      uidirty.to = 0xff;
1343 mateusz.vi 991
      recompute_curline(db);
1282 mateusz.vi 992
 
993
    } else if (k == 0x147) { /* home */
1327 mateusz.vi 994
       cursor_home(db);
1282 mateusz.vi 995
 
996
    } else if (k == 0x14F) { /* end */
1327 mateusz.vi 997
       cursor_eol(db);
1282 mateusz.vi 998
 
1275 mateusz.vi 999
    } else if (k == 0x1B) { /* ESC */
1354 mateusz.vi 1000
      int quitnow = 0;
1001
      char fname[25];
1002
      int saveflag = 0;
1393 mateusz.vi 1003
      enum MENU_ACTION ui_action;
1275 mateusz.vi 1004
 
1392 mateusz.vi 1005
      /* collect the exact menu action and clear the screen */
1006
      ui_action = ui_menu();
1007
      ui_refresh(db);
1354 mateusz.vi 1008
 
1392 mateusz.vi 1009
      switch (ui_action) {
1010
 
1354 mateusz.vi 1011
        case MENU_NONE:
1012
          break;
1013
 
1014
        case MENU_OPEN:
1015
          /* display a warning if unsaved changes are pending */
1016
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
1017
 
1018
          /* ask for filename */
1019
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1020
          if (fname[0] != 0) {
1359 mateusz.vi 1021
            int err;
1354 mateusz.vi 1022
            err = loadfile(db, fname);
1023
            if (err != 0) {
1024
              if (err == 1) {
1025
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
1026
              } else {
1027
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
1028
              }
1029
              mdr_bios_tickswait(44); /* 3s */
1355 mateusz.vi 1030
              loadfile(db, "");
1354 mateusz.vi 1031
            }
1032
          }
1393 mateusz.vi 1033
          uidirty.from = 0;
1034
          uidirty.to = 0xff;
1035
          uidirty.statusbar = 1;
1354 mateusz.vi 1036
          break;
1037
 
1038
        case MENU_SAVEAS:
1039
          saveflag = 1;
1040
          /* FALLTHRU */
1041
        case MENU_SAVE:
1042
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1043
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1044
            if (*fname == 0) break;
1045
            saveflag = savefile(db, fname);
1046
            if (saveflag == 0) memcpy(db->fname, fname, sizeof(fname));
1047
          } else {
1048
            saveflag = savefile(db, NULL);
1049
          }
1050
 
1051
          if (saveflag == 0) {
1052
            db->modflag = 0;
1053
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1054
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1055
          } else {
1056
            ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
1057
            mdr_bios_tickswait(36); /* 2s */
1058
          }
1059
          break;
1060
 
1355 mateusz.vi 1061
        case MENU_CLOSE:
1062
          if (ui_confirm_if_unsaved(db) == 0) {
1063
            loadfile(db, "");
1064
          }
1065
          uidirty.from = 0;
1066
          uidirty.to = 0xff;
1067
          uidirty.statusbar = 1;
1068
          break;
1069
 
1354 mateusz.vi 1070
        case MENU_CHGEOL:
1393 mateusz.vi 1071
          db->modflag = 1;
1354 mateusz.vi 1072
          db->lfonly ^= 1;
1073
          break;
1074
 
1075
        case MENU_QUIT:
1355 mateusz.vi 1076
          quitnow = 1;
1077
          for (curfile = 0; curfile < 10; curfile++) {
1078
            if (dbarr[curfile].modflag) {
1079
              db = select_slot(dbarr, curfile);
1080
              if (ui_confirm_if_unsaved(db) != 0) quitnow = 0;
1081
            }
1082
          }
1354 mateusz.vi 1083
          break;
1084
      }
1085
 
1086
      if (quitnow) break;
1087
 
1289 mateusz.vi 1088
    } else if (k == 0x0D) { /* ENTER */
1328 mateusz.vi 1089
      unsigned short off = db->xoffset + db->cursorposx;
1289 mateusz.vi 1090
      /* add a new line */
1328 mateusz.vi 1091
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1393 mateusz.vi 1092
        db->modflag = 1;
1328 mateusz.vi 1093
        db->cursor = db->cursor->prev; /* back to original line */
1289 mateusz.vi 1094
        /* trim the line above */
1328 mateusz.vi 1095
        db->cursor->len = off;
1289 mateusz.vi 1096
        /* move cursor to the (new) line below */
1343 mateusz.vi 1097
        db->curline -= 1;
1328 mateusz.vi 1098
        uidirty.from = db->cursorposy;
1327 mateusz.vi 1099
        uidirty.to = 0xff;
1328 mateusz.vi 1100
        cursor_down(db);
1101
        cursor_home(db);
1289 mateusz.vi 1102
      } else {
1103
        /* ERROR: OUT OF MEMORY */
1104
      }
1105
 
1292 mateusz.vi 1106
    } else if (k == 0x153) {  /* DEL */
1327 mateusz.vi 1107
      del(db);
1292 mateusz.vi 1108
 
1302 mateusz.vi 1109
    } else if (k == 0x008) { /* BKSPC */
1327 mateusz.vi 1110
      bkspc(db);
1302 mateusz.vi 1111
 
1308 mateusz.vi 1112
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1317 mateusz.vi 1113
      char c = k;
1327 mateusz.vi 1114
      insert_in_line(db, &c, 1);
1308 mateusz.vi 1115
 
1317 mateusz.vi 1116
    } else if (k == 0x009) { /* TAB */
1348 mateusz.vi 1117
      insert_in_line(db, "        ", 8);
1317 mateusz.vi 1118
 
1355 mateusz.vi 1119
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1120
      curfile = k - 0x13b;
1121
      db = select_slot(dbarr, curfile);
1309 mateusz.vi 1122
 
1325 mateusz.vi 1123
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1124
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1125
      for (;;) {
1126
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1127
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1327 mateusz.vi 1128
        cursor_right(db);
1325 mateusz.vi 1129
      }
1130
      /* now skip to next non-space or end of file */
1131
      for (;;) {
1327 mateusz.vi 1132
        cursor_right(db);
1325 mateusz.vi 1133
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1134
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1135
      }
1136
 
1137
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1327 mateusz.vi 1138
      cursor_left(db);
1325 mateusz.vi 1139
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1140
      for (;;) {
1141
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1142
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1327 mateusz.vi 1143
        cursor_left(db);
1325 mateusz.vi 1144
      }
1145
      /* now skip to next space or start of file */
1146
      for (;;) {
1327 mateusz.vi 1147
        cursor_left(db);
1325 mateusz.vi 1148
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1327 mateusz.vi 1149
          cursor_right(db);
1325 mateusz.vi 1150
          break;
1151
        }
1152
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1153
      }
1154
 
1333 mateusz.vi 1155
#ifdef DBG_UNHKEYS
1282 mateusz.vi 1156
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1157
      char buff[4];
1158
      const char *HEX = "0123456789ABCDEF";
1159
      buff[0] = HEX[(k >> 8) & 15];
1160
      buff[1] = HEX[(k >> 4) & 15];
1161
      buff[2] = HEX[k & 15];
1333 mateusz.vi 1162
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1163
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1275 mateusz.vi 1164
      keyb_getkey();
1165
      break;
1333 mateusz.vi 1166
#endif
1275 mateusz.vi 1167
    }
1168
  }
1169
 
1170
  mdr_cout_close();
1171
 
1320 mateusz.vi 1172
  /* no need to free memory, DOS will do it for me */
1275 mateusz.vi 1173
 
1347 mateusz.vi 1174
  return;
1275 mateusz.vi 1175
}