Subversion Repositories SvarDOS

Rev

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

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