Subversion Repositories SvarDOS

Rev

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