Subversion Repositories SvarDOS

Rev

Rev 1473 | Rev 1537 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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