Subversion Repositories SvarDOS

Rev

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