Subversion Repositories SvarDOS

Rev

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