Subversion Repositories SvarDOS

Rev

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