Subversion Repositories SvarDOS

Rev

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