Subversion Repositories SvarDOS

Rev

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