Subversion Repositories SvarDOS

Rev

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

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