Subversion Repositories SvarDOS

Rev

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