Subversion Repositories SvarDOS

Rev

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

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