Subversion Repositories SvarDOS

Rev

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