Subversion Repositories SvarDOS

Rev

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