Subversion Repositories SvarDOS

Rev

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