Subversion Repositories SvarDOS

Rev

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