Subversion Repositories SvarDOS

Rev

Rev 1552 | Rev 1556 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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