Subversion Repositories SvarDOS

Rev

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