Subversion Repositories SvarDOS

Rev

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