Subversion Repositories SvarDOS

Rev

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

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