Subversion Repositories SvarDOS

Rev

Rev 1390 | Rev 1393 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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