Subversion Repositories SvarDOS

Rev

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