Subversion Repositories SvarDOS

Rev

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