Subversion Repositories SvarDOS

Rev

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

Rev 1364 Rev 1387
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
#ifdef DBG_REFRESH
296
#ifdef DBG_REFRESH
297
  static char m = 'a';
297
  static char m = 'a';
298
  m++;
298
  m++;
299
  if (m > 'z') m = 'a';
299
  if (m > 'z') m = 'a';
300
#endif
300
#endif
301
 
301
 
302
  /* rewind cursor line to first line that needs redrawing */
302
  /* rewind cursor line to first line that needs redrawing */
303
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
303
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
304
 
304
 
305
  /* iterate over lines and redraw whatever needs to be redrawn */
305
  /* iterate over lines and redraw whatever needs to be redrawn */
306
  for (; l != NULL; l = l->next, y++) {
306
  for (; l != NULL; l = l->next, y++) {
307
 
307
 
308
    /* skip lines that do not need to be refreshed */
308
    /* skip lines that do not need to be refreshed */
309
    if (y < uidirty.from) continue;
309
    if (y < uidirty.from) continue;
310
    if (y > uidirty.to) break;
310
    if (y > uidirty.to) break;
311
 
311
 
312
    x = 0;
312
    x = 0;
313
    if (db->xoffset < l->len) {
313
    if (db->xoffset < l->len) {
314
      unsigned char i, limit;
314
      unsigned char i, limit;
315
      if (l->len - db->xoffset < screenw) {
315
      if (l->len - db->xoffset < screenw) {
316
        limit = l->len;
316
        limit = l->len;
317
      } else {
317
      } else {
318
        limit = db->xoffset + screenw - 1;
318
        limit = db->xoffset + screenw - 1;
319
      }
319
      }
320
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
320
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
321
    }
321
    }
322
 
322
 
323
    /* write empty spaces until end of line */
323
    /* write empty spaces until end of line */
324
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenw - 1 - x);
324
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenw - 1 - x);
325
 
325
 
326
#ifdef DBG_REFRESH
326
#ifdef DBG_REFRESH
327
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
327
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
328
#endif
328
#endif
329
 
329
 
330
    if (y == screenh - 2) break;
330
    if (y == screenh - 2) break;
331
  }
331
  }
332
 
332
 
333
  /* fill all lines below if empty (and they need to be redrawn) */
333
  /* fill all lines below if empty (and they need to be redrawn) */
334
  if (l == NULL) {
334
  if (l == NULL) {
335
    while ((y < screenh - 1) && (y < uidirty.to)) {
335
    while ((y < screenh - 1) && (y < uidirty.to)) {
336
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenw - 1);
336
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenw - 1);
337
    }
337
    }
338
  }
338
  }
339
 
339
 
340
  /* scroll bar */
340
  /* scroll bar */
341
  for (y = 0; y < (screenh - 1); y++) {
341
  for (y = 0; y < (screenh - 1); y++) {
342
    mdr_cout_char(y, screenw - 1, SCROLL_CURSOR, SCHEME_SCROLL);
342
    mdr_cout_char(y, screenw - 1, SCROLL_CURSOR, SCHEME_SCROLL);
343
  }
343
  }
344
 
344
 
345
  /* scroll cursor */
345
  /* scroll cursor */
346
  if (db->totlines >= screenh) {
346
  if (db->totlines >= screenh) {
347
    unsigned short topline = db->curline - db->cursorposy;
347
    unsigned short topline = db->curline - db->cursorposy;
348
    unsigned short col;
348
    unsigned short col;
349
    unsigned short totlines = db->totlines - screenh + 1;
349
    unsigned short totlines = db->totlines - screenh + 1;
350
    if (db->totlines - screenh > screenh) {
350
    if (db->totlines - screenh > screenh) {
351
      col = topline / (totlines / (screenh - 1));
351
      col = topline / (totlines / (screenh - 1));
352
    } else {
352
    } else {
353
      col = topline * (screenh - 1) / totlines;
353
      col = topline * (screenh - 1) / totlines;
354
    }
354
    }
355
    if (col >= screenh - 1) col = screenh - 2;
355
    if (col >= screenh - 1) col = screenh - 2;
356
    mdr_cout_char(col, screenw - 1, ' ', SCHEME_SCROLL);
356
    mdr_cout_char(col, screenw - 1, ' ', SCHEME_SCROLL);
357
  }
357
  }
358
}
358
}
359
 
359
 
360
 
360
 
361
static void check_cursor_not_after_eol(struct file *db) {
361
static void check_cursor_not_after_eol(struct file *db) {
362
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
362
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
363
 
363
 
364
  if (db->cursor->len < db->xoffset) {
364
  if (db->cursor->len < db->xoffset) {
365
    db->cursorposx = 0;
365
    db->cursorposx = 0;
366
    db->xoffset = db->cursor->len;
366
    db->xoffset = db->cursor->len;
367
    uidirty.from = 0;
367
    uidirty.from = 0;
368
    uidirty.to = 0xff;
368
    uidirty.to = 0xff;
369
  } else {
369
  } else {
370
    db->cursorposx = db->cursor->len - db->xoffset;
370
    db->cursorposx = db->cursor->len - db->xoffset;
371
  }
371
  }
372
}
372
}
373
 
373
 
374
 
374
 
375
static void cursor_up(struct file *db) {
375
static void cursor_up(struct file *db) {
376
  if (db->cursor->prev != NULL) {
376
  if (db->cursor->prev != NULL) {
377
    db->curline -= 1;
377
    db->curline -= 1;
378
    db->cursor = db->cursor->prev;
378
    db->cursor = db->cursor->prev;
379
    if (db->cursorposy == 0) {
379
    if (db->cursorposy == 0) {
380
      uidirty.from = 0;
380
      uidirty.from = 0;
381
      uidirty.to = 0xff;
381
      uidirty.to = 0xff;
382
    } else {
382
    } else {
383
      db->cursorposy -= 1;
383
      db->cursorposy -= 1;
384
    }
384
    }
385
  }
385
  }
386
}
386
}
387
 
387
 
388
 
388
 
389
static void cursor_eol(struct file *db) {
389
static void cursor_eol(struct file *db) {
390
  /* adjust xoffset to make sure eol is visible on screen */
390
  /* adjust xoffset to make sure eol is visible on screen */
391
  if (db->xoffset > db->cursor->len) {
391
  if (db->xoffset > db->cursor->len) {
392
    db->xoffset = db->cursor->len - 1;
392
    db->xoffset = db->cursor->len - 1;
393
    uidirty.from = 0;
393
    uidirty.from = 0;
394
    uidirty.to = 0xff;
394
    uidirty.to = 0xff;
395
  }
395
  }
396
 
396
 
397
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
397
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
398
    db->xoffset = db->cursor->len - screenw + 2;
398
    db->xoffset = db->cursor->len - screenw + 2;
399
    uidirty.from = 0;
399
    uidirty.from = 0;
400
    uidirty.to = 0xff;
400
    uidirty.to = 0xff;
401
  }
401
  }
402
  db->cursorposx = db->cursor->len - db->xoffset;
402
  db->cursorposx = db->cursor->len - db->xoffset;
403
}
403
}
404
 
404
 
405
 
405
 
406
static void cursor_down(struct file *db) {
406
static void cursor_down(struct file *db) {
407
  if (db->cursor->next != NULL) {
407
  if (db->cursor->next != NULL) {
408
    db->curline += 1;
408
    db->curline += 1;
409
    db->cursor = db->cursor->next;
409
    db->cursor = db->cursor->next;
410
    if (db->cursorposy < screenh - 2) {
410
    if (db->cursorposy < screenh - 2) {
411
      db->cursorposy += 1;
411
      db->cursorposy += 1;
412
    } else {
412
    } else {
413
      uidirty.from = 0;
413
      uidirty.from = 0;
414
      uidirty.to = 0xff;
414
      uidirty.to = 0xff;
415
    }
415
    }
416
  }
416
  }
417
}
417
}
418
 
418
 
419
 
419
 
420
static void cursor_left(struct file *db) {
420
static void cursor_left(struct file *db) {
421
  if (db->cursorposx > 0) {
421
  if (db->cursorposx > 0) {
422
    db->cursorposx -= 1;
422
    db->cursorposx -= 1;
423
  } else if (db->xoffset > 0) {
423
  } else if (db->xoffset > 0) {
424
    db->xoffset -= 1;
424
    db->xoffset -= 1;
425
    uidirty.from = 0;
425
    uidirty.from = 0;
426
    uidirty.to = 0xff;
426
    uidirty.to = 0xff;
427
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
427
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
428
    cursor_up(db);
428
    cursor_up(db);
429
    cursor_eol(db);
429
    cursor_eol(db);
430
  }
430
  }
431
}
431
}
432
 
432
 
433
 
433
 
434
static void cursor_home(struct file *db) {
434
static void cursor_home(struct file *db) {
435
  db->cursorposx = 0;
435
  db->cursorposx = 0;
436
  if (db->xoffset != 0) {
436
  if (db->xoffset != 0) {
437
    db->xoffset = 0;
437
    db->xoffset = 0;
438
    uidirty.from = 0;
438
    uidirty.from = 0;
439
    uidirty.to = 0xff;
439
    uidirty.to = 0xff;
440
  }
440
  }
441
}
441
}
442
 
442
 
443
 
443
 
444
static void cursor_right(struct file *db) {
444
static void cursor_right(struct file *db) {
445
  if (db->cursor->len > db->xoffset + db->cursorposx) {
445
  if (db->cursor->len > db->xoffset + db->cursorposx) {
446
    if (db->cursorposx < screenw - 2) {
446
    if (db->cursorposx < screenw - 2) {
447
      db->cursorposx += 1;
447
      db->cursorposx += 1;
448
    } else {
448
    } else {
449
      db->xoffset += 1;
449
      db->xoffset += 1;
450
      uidirty.from = 0;
450
      uidirty.from = 0;
451
      uidirty.to = 0xff;
451
      uidirty.to = 0xff;
452
    }
452
    }
453
  } else if (db->cursor->next != NULL) { /* jump to start of next line */
453
  } else if (db->cursor->next != NULL) { /* jump to start of next line */
454
    cursor_down(db);
454
    cursor_down(db);
455
    cursor_home(db);
455
    cursor_home(db);
456
  }
456
  }
457
}
457
}
458
 
458
 
459
 
459
 
460
static void del(struct file *db) {
460
static void del(struct file *db) {
461
  if (db->cursorposx + db->xoffset < db->cursor->len) {
461
  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);
462
    _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 */
463
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
464
    uidirty.from = db->cursorposy;
464
    uidirty.from = db->cursorposy;
465
    uidirty.to = db->cursorposy;
465
    uidirty.to = db->cursorposy;
466
    db->modflag = '*';
466
    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) */
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) */
468
    struct line far *nextline = db->cursor->next;
468
    struct line far *nextline = db->cursor->next;
469
    if (db->cursor->next->len > 0) {
469
    if (db->cursor->next->len > 0) {
470
      if (curline_resize(db, db->cursor->len + db->cursor->next->len + 1) == 0) {
470
      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);
471
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
472
        db->cursor->len += db->cursor->next->len;
472
        db->cursor->len += db->cursor->next->len;
473
      }
473
      }
474
    }
474
    }
475
 
475
 
476
    db->cursor->next = db->cursor->next->next;
476
    db->cursor->next = db->cursor->next->next;
477
    db->cursor->next->prev = db->cursor;
477
    db->cursor->next->prev = db->cursor;
478
 
478
 
479
    line_free(nextline);
479
    line_free(nextline);
480
    uidirty.from = db->cursorposy;
480
    uidirty.from = db->cursorposy;
481
    uidirty.to = 0xff;
481
    uidirty.to = 0xff;
482
    db->totlines -= 1;
482
    db->totlines -= 1;
483
    db->modflag = '*';
483
    db->modflag = '*';
484
  }
484
  }
485
}
485
}
486
 
486
 
487
 
487
 
488
static void bkspc(struct file *db) {
488
static void bkspc(struct file *db) {
489
 
489
 
490
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
490
  /* 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;
491
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
492
 
492
 
493
  cursor_left(db);
493
  cursor_left(db);
494
  del(db);
494
  del(db);
495
}
495
}
496
 
496
 
497
 
497
 
498
/* returns 0 on success, 1 on file not found, 2 on other error */
498
/* returns 0 on success, 1 on file not found, 2 on other error */
499
static int loadfile(struct file *db, const char *fname) {
499
static int loadfile(struct file *db, const char *fname) {
500
  char buff[512]; /* read one entire sector at a time (faster) */
500
  char buff[512]; /* read one entire sector at a time (faster) */
501
  char *buffptr;
501
  char *buffptr;
502
  unsigned int len, llen;
502
  unsigned int len, llen;
503
  int fd;
503
  int fd;
504
  unsigned char eolfound;
504
  unsigned char eolfound;
505
 
505
 
506
  /* free the entire linked list of lines */
506
  /* free the entire linked list of lines */
507
  db_rewind(db);
507
  db_rewind(db);
508
  while (db->cursor) {
508
  while (db->cursor) {
509
    struct line far *victim;
509
    struct line far *victim;
510
    victim = db->cursor;
510
    victim = db->cursor;
511
    db->cursor = db->cursor->next;
511
    db->cursor = db->cursor->next;
512
    line_free(victim);
512
    line_free(victim);
513
  }
513
  }
514
 
514
 
515
  /* zero out the struct */
515
  /* zero out the struct */
516
  bzero(db, sizeof(struct file));
516
  bzero(db, sizeof(struct file));
517
 
517
 
518
  if (*fname == 0) goto SKIPLOADING;
518
  if (*fname == 0) goto SKIPLOADING;
519
 
519
 
520
  memcpy(db->fname, fname, strlen(fname));
520
  memcpy(db->fname, fname, strlen(fname));
521
 
521
 
522
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
522
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
523
    return(1);
523
    return(1);
524
  }
524
  }
525
 
525
 
526
  db->lfonly = 1;
526
  db->lfonly = 1;
527
 
527
 
528
  /* start by adding an empty line */
528
  /* start by adding an empty line */
529
  if (line_add(db, NULL, 0) != 0) {
529
  if (line_add(db, NULL, 0) != 0) {
530
    /* TODO ERROR HANDLING */
530
    /* TODO ERROR HANDLING */
531
  }
531
  }
532
 
532
 
533
  for (eolfound = 0;;) {
533
  for (eolfound = 0;;) {
534
    unsigned short consumedbytes;
534
    unsigned short consumedbytes;
535
 
535
 
536
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
536
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
537
    buffptr = buff;
537
    buffptr = buff;
538
 
538
 
539
    FINDLINE:
539
    FINDLINE:
540
 
540
 
541
    /* look for nearest \n (also expand tabs) */
541
    /* look for nearest \n (also expand tabs) */
542
    for (consumedbytes = 0;; consumedbytes++) {
542
    for (consumedbytes = 0;; consumedbytes++) {
543
 
543
 
544
      if (buffptr[consumedbytes] == '\t') {
544
      if (buffptr[consumedbytes] == '\t') {
545
        llen = consumedbytes;
545
        llen = consumedbytes;
546
        break;
546
        break;
547
      }
547
      }
548
 
548
 
549
      if (consumedbytes == len) {
549
      if (consumedbytes == len) {
550
        llen = consumedbytes;
550
        llen = consumedbytes;
551
        break;
551
        break;
552
      }
552
      }
553
      if (buffptr[consumedbytes] == '\r') {
553
      if (buffptr[consumedbytes] == '\r') {
554
        llen = consumedbytes;
554
        llen = consumedbytes;
555
        consumedbytes++;
555
        consumedbytes++;
556
        db->lfonly = 0;
556
        db->lfonly = 0;
557
        break;
557
        break;
558
      }
558
      }
559
      if (buffptr[consumedbytes] == '\n') {
559
      if (buffptr[consumedbytes] == '\n') {
560
        eolfound = 1;
560
        eolfound = 1;
561
        llen = consumedbytes;
561
        llen = consumedbytes;
562
        consumedbytes++;
562
        consumedbytes++;
563
        break;
563
        break;
564
      }
564
      }
565
    }
565
    }
566
 
566
 
567
    /* consumedbytes is the amount of bytes processed from buffptr,
567
    /* consumedbytes is the amount of bytes processed from buffptr,
568
     * llen is the length of line's payload (without its line terminator) */
568
     * llen is the length of line's payload (without its line terminator) */
569
 
569
 
570
    /* append content, if line is non-empty */
570
    /* append content, if line is non-empty */
571
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
571
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
572
      goto IOERR;
572
      goto IOERR;
573
    }
573
    }
574
 
574
 
575
    /* add a new line if necessary */
575
    /* add a new line if necessary */
576
    if (eolfound) {
576
    if (eolfound) {
577
      if (line_add(db, NULL, 0) != 0) {
577
      if (line_add(db, NULL, 0) != 0) {
578
        goto IOERR;
578
        goto IOERR;
579
      }
579
      }
580
      eolfound = 0;
580
      eolfound = 0;
581
    }
581
    }
582
 
582
 
583
    /* append 8 spaces if tab char found */
583
    /* append 8 spaces if tab char found */
584
    if ((consumedbytes < len) && (buffptr[consumedbytes] == '\t')) {
584
    if ((consumedbytes < len) && (buffptr[consumedbytes] == '\t')) {
585
      consumedbytes++;
585
      consumedbytes++;
586
      if (line_append(db, "        ", 8) != 0) {
586
      if (line_append(db, "        ", 8) != 0) {
587
        goto IOERR;
587
        goto IOERR;
588
      }
588
      }
589
    }
589
    }
590
 
590
 
591
    /* anything left? process the buffer leftover again */
591
    /* anything left? process the buffer leftover again */
592
    if (consumedbytes < len) {
592
    if (consumedbytes < len) {
593
      len -= consumedbytes;
593
      len -= consumedbytes;
594
      buffptr += consumedbytes;
594
      buffptr += consumedbytes;
595
      goto FINDLINE;
595
      goto FINDLINE;
596
    }
596
    }
597
 
597
 
598
  }
598
  }
599
 
599
 
600
  _dos_close(fd);
600
  _dos_close(fd);
601
 
601
 
602
  SKIPLOADING:
602
  SKIPLOADING:
603
 
603
 
604
  /* add an empty line at end if not present already, also rewind cursor to top of file */
604
  /* 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);
605
  if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
606
  db_rewind(db);
606
  db_rewind(db);
607
 
607
 
608
  return(0);
608
  return(0);
609
 
609
 
610
  IOERR:
610
  IOERR:
611
  _dos_close(fd);
611
  _dos_close(fd);
612
  return(2);
612
  return(2);
613
}
613
}
614
 
614
 
615
 
615
 
616
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
616
/* 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
617
 * of argc and argv, saves some 330 bytes of binary size
618
 * returns non-zero on error */
618
 * returns non-zero on error */
619
static int parseargv(struct file *dbarr) {
619
static int parseargv(struct file *dbarr) {
620
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
620
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
621
  unsigned short count = 0;
621
  unsigned short count = 0;
622
  char *arg;
622
  char *arg;
623
  unsigned short lastarg = 0;
623
  unsigned short lastarg = 0;
624
  int err;
624
  int err;
625
 
625
 
626
  while (!lastarg) {
626
  while (!lastarg) {
627
    /* jump to nearest arg */
627
    /* jump to nearest arg */
628
    while (*tail == ' ') {
628
    while (*tail == ' ') {
629
      *tail = 0;
629
      *tail = 0;
630
      tail++;
630
      tail++;
631
    }
631
    }
632
 
632
 
633
    if (*tail == '\r') {
633
    if (*tail == '\r') {
634
      *tail = 0;
634
      *tail = 0;
635
      break;
635
      break;
636
    }
636
    }
637
 
637
 
638
    arg = tail;
638
    arg = tail;
639
 
639
 
640
    /* jump to next delimiter */
640
    /* jump to next delimiter */
641
    while ((*tail != ' ') && (*tail != '\r')) tail++;
641
    while ((*tail != ' ') && (*tail != '\r')) tail++;
642
 
642
 
643
    /* if \r then remember this is the last arg */
643
    /* if \r then remember this is the last arg */
644
    if (*tail == '\r') lastarg = 1;
644
    if (*tail == '\r') lastarg = 1;
645
 
645
 
646
    *tail = 0;
646
    *tail = 0;
647
    tail++;
647
    tail++;
648
 
648
 
649
    /* look at the arg now */
649
    /* look at the arg now */
650
    if (*arg == '/') {
650
    if (*arg == '/') {
-
 
651
      const char far *self = mdr_dos_selfexe();
-
 
652
      unsigned short i;
-
 
653
      if (self == NULL) self = "sved";
-
 
654
      for (i = 0; self[i] != 0; i++) {
-
 
655
        if (self[i] == '\\') {
-
 
656
          self += i + 1;
-
 
657
          i = 0;
-
 
658
        }
-
 
659
      }
651
      mdr_coutraw_puts("Sved ver " PVER " Copyright (C) " PDATE " Mateusz Viste");
660
      mdr_coutraw_puts("sved ver " PVER " copyright (C) " PDATE " Mateusz Viste");
652
      mdr_coutraw_puts("");
661
      mdr_coutraw_crlf();
-
 
662
      mdr_coutraw_str(svarlang_str(1,0)); /* usage: */
-
 
663
      mdr_coutraw_char(' ');
-
 
664
      while (*self != 0) {
-
 
665
        mdr_coutraw_char(*self);
-
 
666
        self++;
-
 
667
      }
-
 
668
      mdr_coutraw_char(' ');
653
      mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
669
      mdr_coutraw_puts(svarlang_str(1,1)); /* args syntax */
654
      return(-1);
670
      return(-1);
655
    }
671
    }
656
 
672
 
657
    /* looks to be a filename */
673
    /* looks to be a filename */
658
    if (count == 10) {
674
    if (count == 10) {
659
      mdr_coutraw_puts(svarlang_str(0,12));
675
      mdr_coutraw_puts(svarlang_str(0,12));
660
      return(-1); /* too many files */
676
      return(-1); /* too many files */
661
    }
677
    }
662
 
678
 
663
    /* try loading it */
679
    /* try loading it */
-
 
680
    mdr_coutraw_str(svarlang_str(1,2));
-
 
681
    mdr_coutraw_char(' ');
664
    mdr_coutraw_puts(arg);
682
    mdr_coutraw_puts(arg);
665
    err = loadfile(&(dbarr[count]), arg);
683
    err = loadfile(&(dbarr[count]), arg);
666
    if (err) {
684
    if (err) {
667
      if (err == 1) { /* file not found */
685
      if (err == 1) { /* file not found */
668
        err = 11;
686
        err = 11;
669
      } else { /* general error */
687
      } else { /* general error */
670
        err = 10;
688
        err = 10;
671
      }
689
      }
672
      mdr_coutraw_puts(svarlang_str(0,err));
690
      mdr_coutraw_puts(svarlang_str(0,err));
673
      return(-1);
691
      return(-1);
674
    }
692
    }
675
    count++;
693
    count++;
676
  }
694
  }
677
 
695
 
678
  return(0);
696
  return(0);
679
}
697
}
680
 
698
 
681
 
699
 
682
static int savefile(const struct file *db, const char *newfname) {
700
static int savefile(const struct file *db, const char *newfname) {
683
  int fd;
701
  int fd;
684
  const struct line far *l;
702
  const struct line far *l;
685
  unsigned bytes;
703
  unsigned bytes;
686
  unsigned char eollen;
704
  unsigned char eollen;
687
  unsigned char eolbuf[2];
705
  unsigned char eolbuf[2];
688
  int errflag = 0;
706
  int errflag = 0;
689
 
707
 
690
  /* either create a new file if newfname provided, or... */
708
  /* either create a new file if newfname provided, or... */
691
  if (newfname) {
709
  if (newfname) {
692
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
710
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
693
  } else { /* ...open db->fname */
711
  } else { /* ...open db->fname */
694
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
712
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
695
  }
713
  }
696
 
714
 
697
  l = db->cursor;
715
  l = db->cursor;
698
  while (l->prev) l = l->prev;
716
  while (l->prev) l = l->prev;
699
 
717
 
700
  /* preset line terminators */
718
  /* preset line terminators */
701
  if (db->lfonly) {
719
  if (db->lfonly) {
702
    eolbuf[0] = '\n';
720
    eolbuf[0] = '\n';
703
    eollen = 1;
721
    eollen = 1;
704
  } else {
722
  } else {
705
    eolbuf[0] = '\r';
723
    eolbuf[0] = '\r';
706
    eolbuf[1] = '\n';
724
    eolbuf[1] = '\n';
707
    eollen = 2;
725
    eollen = 2;
708
  }
726
  }
709
 
727
 
710
  while (l) {
728
  while (l) {
711
    /* do not write the last empty line, it is only useful for edition */
729
    /* do not write the last empty line, it is only useful for edition */
712
    if (l->len != 0) {
730
    if (l->len != 0) {
713
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
731
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
714
    } else if (l->next == NULL) {
732
    } else if (l->next == NULL) {
715
      break;
733
      break;
716
    }
734
    }
717
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
735
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
718
    l = l->next;
736
    l = l->next;
719
  }
737
  }
720
 
738
 
721
  errflag |= _dos_close(fd);
739
  errflag |= _dos_close(fd);
722
 
740
 
723
  return(errflag);
741
  return(errflag);
724
}
742
}
725
 
743
 
726
 
744
 
727
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
745
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
728
  if (curline_resize(db, db->cursor->len + len) == 0) {
746
  if (curline_resize(db, db->cursor->len + len) == 0) {
729
    unsigned short off = db->xoffset + db->cursorposx;
747
    unsigned short off = db->xoffset + db->cursorposx;
730
    db->modflag = '*';
748
    db->modflag = '*';
731
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
749
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
732
    db->cursor->len += len;
750
    db->cursor->len += len;
733
    uidirty.from = db->cursorposy;
751
    uidirty.from = db->cursorposy;
734
    uidirty.to = db->cursorposy;
752
    uidirty.to = db->cursorposy;
735
    while (len--) {
753
    while (len--) {
736
      db->cursor->payload[off++] = *databuf;
754
      db->cursor->payload[off++] = *databuf;
737
      databuf++;
755
      databuf++;
738
      cursor_right(db);
756
      cursor_right(db);
739
    }
757
    }
740
  }
758
  }
741
}
759
}
742
 
760
 
743
 
761
 
744
/* recompute db->curline by counting nodes in linked list */
762
/* recompute db->curline by counting nodes in linked list */
745
static void recompute_curline(struct file *db) {
763
static void recompute_curline(struct file *db) {
746
  const struct line far *l = db->cursor;
764
  const struct line far *l = db->cursor;
747
 
765
 
748
  db->curline = 0;
766
  db->curline = 0;
749
  while (l->prev != NULL) {
767
  while (l->prev != NULL) {
750
    db->curline += 1;
768
    db->curline += 1;
751
    l = l->prev;
769
    l = l->prev;
752
  }
770
  }
753
}
771
}
754
 
772
 
755
 
773
 
756
enum MENU_ACTION {
774
enum MENU_ACTION {
757
  MENU_OPEN   = 0,
775
  MENU_OPEN   = 0,
758
  MENU_SAVE   = 1,
776
  MENU_SAVE   = 1,
759
  MENU_SAVEAS = 2,
777
  MENU_SAVEAS = 2,
760
  MENU_CLOSE  = 3,
778
  MENU_CLOSE  = 3,
761
  MENU_CHGEOL = 4,
779
  MENU_CHGEOL = 4,
762
  MENU_QUIT   = 5,
780
  MENU_QUIT   = 5,
763
  MENU_NONE   = 0xff
781
  MENU_NONE   = 0xff
764
};
782
};
765
 
783
 
766
static enum MENU_ACTION ui_menu(void) {
784
static enum MENU_ACTION ui_menu(void) {
767
  unsigned short i, curchoice, attr, x, slen;
785
  unsigned short i, curchoice, attr, x, slen;
768
  uidirty.from = 0;
786
  uidirty.from = 0;
769
  uidirty.to = 0xff;
787
  uidirty.to = 0xff;
770
  uidirty.statusbar = 1;
788
  uidirty.statusbar = 1;
771
 
789
 
772
  /* find out the longest string */
790
  /* find out the longest string */
773
  slen = 0;
791
  slen = 0;
774
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
792
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
775
    x = strlen(svarlang_str(8, i));
793
    x = strlen(svarlang_str(8, i));
776
    if (x > slen) slen = x;
794
    if (x > slen) slen = x;
777
  }
795
  }
778
 
796
 
779
  curchoice = MENU_OPEN;
797
  curchoice = MENU_OPEN;
780
  for (;;) {
798
  for (;;) {
781
    /* render menu */
799
    /* render menu */
782
    for (i = MENU_OPEN; i <= MENU_QUIT + 1; i++) {
800
    for (i = MENU_OPEN; i <= MENU_QUIT + 1; i++) {
783
      mdr_cout_char_rep(i, 0, ' ', SCHEME_MENU, slen+3);
801
      mdr_cout_char_rep(i, 0, ' ', SCHEME_MENU, slen+3);
784
      if (i == curchoice) {
802
      if (i == curchoice) {
785
        attr = SCHEME_MENU_CUR;
803
        attr = SCHEME_MENU_CUR;
786
        mdr_cout_char(i, 0, '>', SCHEME_MENU_SEL);
804
        mdr_cout_char(i, 0, '>', SCHEME_MENU_SEL);
787
      } else {
805
      } else {
788
        attr = SCHEME_MENU;
806
        attr = SCHEME_MENU;
789
      }
807
      }
790
      x = mdr_cout_str(i, 1, svarlang_str(8, i), attr, slen);
808
      x = mdr_cout_str(i, 1, svarlang_str(8, i), attr, slen);
791
      if (i == curchoice) {
809
      if (i == curchoice) {
792
        mdr_cout_char_rep(i, x + 1, ' ', SCHEME_MENU_SEL, slen - x + 1);
810
        mdr_cout_char_rep(i, x + 1, ' ', SCHEME_MENU_SEL, slen - x + 1);
793
        mdr_cout_locate(i, x + 1);
811
        mdr_cout_locate(i, x + 1);
794
      }
812
      }
795
    }
813
    }
796
    /* wait for key */
814
    /* wait for key */
797
    switch (keyb_getkey()) {
815
    switch (keyb_getkey()) {
798
      case '\r': return(curchoice); /* ENTER */
816
      case '\r': return(curchoice); /* ENTER */
799
      case 0x150: /* down */
817
      case 0x150: /* down */
800
        if (curchoice == MENU_QUIT) {
818
        if (curchoice == MENU_QUIT) {
801
          curchoice = MENU_OPEN;
819
          curchoice = MENU_OPEN;
802
        } else {
820
        } else {
803
          curchoice++;
821
          curchoice++;
804
        }
822
        }
805
        break;
823
        break;
806
      case 0x148: /* up */
824
      case 0x148: /* up */
807
        if (curchoice == MENU_OPEN) {
825
        if (curchoice == MENU_OPEN) {
808
          curchoice = MENU_QUIT;
826
          curchoice = MENU_QUIT;
809
        } else {
827
        } else {
810
          curchoice--;
828
          curchoice--;
811
        }
829
        }
812
        break;
830
        break;
813
      default: return(MENU_NONE);
831
      default: return(MENU_NONE);
814
    }
832
    }
815
  }
833
  }
816
}
834
}
817
 
835
 
818
 
836
 
819
static struct file *select_slot(struct file *dbarr, unsigned short curfile) {
837
static struct file *select_slot(struct file *dbarr, unsigned short curfile) {
820
  uidirty.from = 0;
838
  uidirty.from = 0;
821
  uidirty.to = 0xff;
839
  uidirty.to = 0xff;
822
  uidirty.statusbar = 1;
840
  uidirty.statusbar = 1;
823
 
841
 
824
  dbarr = &(dbarr[curfile]);
842
  dbarr = &(dbarr[curfile]);
825
  ui_basic(dbarr, curfile);
843
  ui_basic(dbarr, curfile);
826
  ui_refresh(dbarr);
844
  ui_refresh(dbarr);
827
  return(dbarr);
845
  return(dbarr);
828
}
846
}
829
 
847
 
830
 
848
 
831
/* main returns nothing, ie. sved always exits with a zero exit code
849
/* main returns nothing, ie. sved always exits with a zero exit code
832
 * (this saves 20 bytes of executable footprint) */
850
 * (this saves 20 bytes of executable footprint) */
833
void main(void) {
851
void main(void) {
834
  static struct file dbarr[10];
852
  static struct file dbarr[10];
835
  unsigned short curfile;
853
  unsigned short curfile;
836
  struct file *db = dbarr; /* visible file is the first slot by default */
854
  struct file *db = dbarr; /* visible file is the first slot by default */
837
 
855
 
838
  {
856
  {
839
    unsigned short i = 0;
857
    unsigned short i = 0;
840
    const char far *selfptr;
858
    const char far *selfptr;
841
    char self[128], lang[8];
859
    char self[128], lang[8];
842
    selfptr = mdr_dos_selfexe();
860
    selfptr = mdr_dos_selfexe();
843
    if (selfptr != NULL) {
861
    if (selfptr != NULL) {
844
      do {
862
      do {
845
        self[i] = selfptr[i];
863
        self[i] = selfptr[i];
846
      } while (self[i++] != 0);
864
      } while (self[i++] != 0);
847
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
865
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
848
    }
866
    }
849
  }
867
  }
850
 
868
 
851
  /* preload all slots with empty files */
869
  /* preload all slots with empty files */
852
  for (curfile = 9;; curfile--) {
870
  for (curfile = 9;; curfile--) {
853
    loadfile(&(dbarr[curfile]), "");
871
    loadfile(&(dbarr[curfile]), "");
854
    if (curfile == 0) break;
872
    if (curfile == 0) break;
855
  }
873
  }
856
 
874
 
857
  /* parse argv (and load files, if any passed on) */
875
  /* parse argv (and load files, if any passed on) */
858
  if (parseargv(dbarr) != 0) return;
876
  if (parseargv(dbarr) != 0) return;
859
 
877
 
860
  if (mdr_cout_init(&screenw, &screenh)) {
878
  if (mdr_cout_init(&screenw, &screenh)) {
861
    /* load color scheme if mdr_cout_init returns a color flag */
879
    /* load color scheme if mdr_cout_init returns a color flag */
862
    SCHEME_TEXT = 0x17;
880
    SCHEME_TEXT = 0x17;
863
    SCHEME_MENU = 0x70;
881
    SCHEME_MENU = 0x70;
864
    SCHEME_MENU_CUR = 0x2f;
882
    SCHEME_MENU_CUR = 0x2f;
865
    SCHEME_MENU_SEL = 0x22;
883
    SCHEME_MENU_SEL = 0x22;
866
    SCHEME_STBAR1 = 0x70;
884
    SCHEME_STBAR1 = 0x70;
867
    SCHEME_STBAR2 = 0x78;
885
    SCHEME_STBAR2 = 0x78;
868
    SCHEME_STBAR3 = 0x70;
886
    SCHEME_STBAR3 = 0x70;
869
    SCHEME_SCROLL = 0x70;
887
    SCHEME_SCROLL = 0x70;
870
    SCHEME_MSG = 0x8f;
888
    SCHEME_MSG = 0x8f;
871
    SCHEME_ERR = 0x4f;
889
    SCHEME_ERR = 0x4f;
872
  }
890
  }
873
 
891
 
874
  for (;;) {
892
  for (;;) {
875
    int k;
893
    int k;
876
 
894
 
877
    /* add an extra empty line if cursor is on last line and this line is not empty */
895
    /* add an extra empty line if cursor is on last line and this line is not empty */
878
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
896
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
879
      if (line_add(db, NULL, 0) == 0) {
897
      if (line_add(db, NULL, 0) == 0) {
880
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
898
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
881
      }
899
      }
882
    }
900
    }
883
 
901
 
884
    check_cursor_not_after_eol(db);
902
    check_cursor_not_after_eol(db);
885
    mdr_cout_locate(db->cursorposy, db->cursorposx);
903
    mdr_cout_locate(db->cursorposy, db->cursorposx);
886
 
904
 
887
    if (uidirty.from != 0xff) {
905
    if (uidirty.from != 0xff) {
888
      ui_refresh(db);
906
      ui_refresh(db);
889
      uidirty.from = 0xff;
907
      uidirty.from = 0xff;
890
    }
908
    }
891
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag)) {
909
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag)) {
892
      ui_basic(db, curfile);
910
      ui_basic(db, curfile);
893
      uidirty.statusbar = 0;
911
      uidirty.statusbar = 0;
894
      db->modflagprev = db->modflag;
912
      db->modflagprev = db->modflag;
895
    }
913
    }
896
#ifdef DBG_LINENUM
914
#ifdef DBG_LINENUM
897
      {
915
      {
898
        char ddd[10];
916
        char ddd[10];
899
        db->curline += 1;
917
        db->curline += 1;
900
        ddd[0] = '0' + db->curline / 100;
918
        ddd[0] = '0' + db->curline / 100;
901
        ddd[1] = '0' + (db->curline % 100) / 10;
919
        ddd[1] = '0' + (db->curline % 100) / 10;
902
        ddd[2] = '0' + (db->curline % 10);
920
        ddd[2] = '0' + (db->curline % 10);
903
        db->curline -= 1;
921
        db->curline -= 1;
904
        ddd[3] = '/';
922
        ddd[3] = '/';
905
        ddd[4] = '0' + db->totlines / 100;
923
        ddd[4] = '0' + db->totlines / 100;
906
        ddd[5] = '0' + (db->totlines % 100) / 10;
924
        ddd[5] = '0' + (db->totlines % 100) / 10;
907
        ddd[6] = '0' + (db->totlines % 10);
925
        ddd[6] = '0' + (db->totlines % 10);
908
        ddd[7] = 0;
926
        ddd[7] = 0;
909
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
927
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
910
      }
928
      }
911
#endif
929
#endif
912
 
930
 
913
    k = keyb_getkey();
931
    k = keyb_getkey();
914
 
932
 
915
    if (k == 0x150) { /* down */
933
    if (k == 0x150) { /* down */
916
      cursor_down(db);
934
      cursor_down(db);
917
 
935
 
918
    } else if (k == 0x148) { /* up */
936
    } else if (k == 0x148) { /* up */
919
      cursor_up(db);
937
      cursor_up(db);
920
 
938
 
921
    } else if (k == 0x14D) { /* right */
939
    } else if (k == 0x14D) { /* right */
922
      cursor_right(db);
940
      cursor_right(db);
923
 
941
 
924
    } else if (k == 0x14B) { /* left */
942
    } else if (k == 0x14B) { /* left */
925
      cursor_left(db);
943
      cursor_left(db);
926
 
944
 
927
    } else if (k == 0x149) { /* pgup */
945
    } else if (k == 0x149) { /* pgup */
928
      unsigned char dist = db->cursorposy + screenh - 1;
946
      unsigned char dist = db->cursorposy + screenh - 1;
929
      while ((dist != 0) && (db->cursor->prev != NULL)) {
947
      while ((dist != 0) && (db->cursor->prev != NULL)) {
930
        db->cursor = db->cursor->prev;
948
        db->cursor = db->cursor->prev;
931
        dist--;
949
        dist--;
932
      }
950
      }
933
      if (dist != 0) {
951
      if (dist != 0) {
934
        db->cursorposy = 0;
952
        db->cursorposy = 0;
935
        db->cursorposx = 0;
953
        db->cursorposx = 0;
936
      } else {
954
      } else {
937
        dist = db->cursorposy;
955
        dist = db->cursorposy;
938
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
956
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
939
      }
957
      }
940
      uidirty.from = 0;
958
      uidirty.from = 0;
941
      uidirty.to = 0xff;
959
      uidirty.to = 0xff;
942
      recompute_curline(db);
960
      recompute_curline(db);
943
 
961
 
944
    } else if (k == 0x151) { /* pgdown */
962
    } else if (k == 0x151) { /* pgdown */
945
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
963
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
946
      while ((dist != 0) && (db->cursor->next != NULL)) {
964
      while ((dist != 0) && (db->cursor->next != NULL)) {
947
        db->cursor = db->cursor->next;
965
        db->cursor = db->cursor->next;
948
        dist--;
966
        dist--;
949
      }
967
      }
950
      if (dist != 0) {
968
      if (dist != 0) {
951
        db->cursorposy = screenh - 2;
969
        db->cursorposy = screenh - 2;
952
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
970
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
953
        db->cursorposx = 0;
971
        db->cursorposx = 0;
954
      } else {
972
      } else {
955
        dist = screenh - 2 - db->cursorposy;
973
        dist = screenh - 2 - db->cursorposy;
956
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
974
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
957
      }
975
      }
958
      uidirty.from = 0;
976
      uidirty.from = 0;
959
      uidirty.to = 0xff;
977
      uidirty.to = 0xff;
960
      recompute_curline(db);
978
      recompute_curline(db);
961
 
979
 
962
    } else if (k == 0x147) { /* home */
980
    } else if (k == 0x147) { /* home */
963
       cursor_home(db);
981
       cursor_home(db);
964
 
982
 
965
    } else if (k == 0x14F) { /* end */
983
    } else if (k == 0x14F) { /* end */
966
       cursor_eol(db);
984
       cursor_eol(db);
967
 
985
 
968
    } else if (k == 0x1B) { /* ESC */
986
    } else if (k == 0x1B) { /* ESC */
969
      int quitnow = 0;
987
      int quitnow = 0;
970
      char fname[25];
988
      char fname[25];
971
      int saveflag = 0;
989
      int saveflag = 0;
972
 
990
 
973
      switch (ui_menu()) {
991
      switch (ui_menu()) {
974
 
992
 
975
        case MENU_NONE:
993
        case MENU_NONE:
976
          break;
994
          break;
977
 
995
 
978
        case MENU_OPEN:
996
        case MENU_OPEN:
979
          /* display a warning if unsaved changes are pending */
997
          /* display a warning if unsaved changes are pending */
980
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
998
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
981
 
999
 
982
          /* ask for filename */
1000
          /* ask for filename */
983
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1001
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
984
          if (fname[0] != 0) {
1002
          if (fname[0] != 0) {
985
            int err;
1003
            int err;
986
            err = loadfile(db, fname);
1004
            err = loadfile(db, fname);
987
            if (err != 0) {
1005
            if (err != 0) {
988
              if (err == 1) {
1006
              if (err == 1) {
989
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
1007
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
990
              } else {
1008
              } else {
991
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
1009
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
992
              }
1010
              }
993
              mdr_bios_tickswait(44); /* 3s */
1011
              mdr_bios_tickswait(44); /* 3s */
994
              loadfile(db, "");
1012
              loadfile(db, "");
995
            }
1013
            }
996
          }
1014
          }
997
          break;
1015
          break;
998
 
1016
 
999
        case MENU_SAVEAS:
1017
        case MENU_SAVEAS:
1000
          saveflag = 1;
1018
          saveflag = 1;
1001
          /* FALLTHRU */
1019
          /* FALLTHRU */
1002
        case MENU_SAVE:
1020
        case MENU_SAVE:
1003
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1021
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1004
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1022
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1005
            if (*fname == 0) break;
1023
            if (*fname == 0) break;
1006
            saveflag = savefile(db, fname);
1024
            saveflag = savefile(db, fname);
1007
            if (saveflag == 0) memcpy(db->fname, fname, sizeof(fname));
1025
            if (saveflag == 0) memcpy(db->fname, fname, sizeof(fname));
1008
          } else {
1026
          } else {
1009
            saveflag = savefile(db, NULL);
1027
            saveflag = savefile(db, NULL);
1010
          }
1028
          }
1011
 
1029
 
1012
          if (saveflag == 0) {
1030
          if (saveflag == 0) {
1013
            db->modflag = 0;
1031
            db->modflag = 0;
1014
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1032
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1015
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1033
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1016
          } else {
1034
          } else {
1017
            ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
1035
            ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
1018
            mdr_bios_tickswait(36); /* 2s */
1036
            mdr_bios_tickswait(36); /* 2s */
1019
          }
1037
          }
1020
          break;
1038
          break;
1021
 
1039
 
1022
        case MENU_CLOSE:
1040
        case MENU_CLOSE:
1023
          if (ui_confirm_if_unsaved(db) == 0) {
1041
          if (ui_confirm_if_unsaved(db) == 0) {
1024
            loadfile(db, "");
1042
            loadfile(db, "");
1025
          }
1043
          }
1026
          uidirty.from = 0;
1044
          uidirty.from = 0;
1027
          uidirty.to = 0xff;
1045
          uidirty.to = 0xff;
1028
          uidirty.statusbar = 1;
1046
          uidirty.statusbar = 1;
1029
          break;
1047
          break;
1030
 
1048
 
1031
        case MENU_CHGEOL:
1049
        case MENU_CHGEOL:
1032
          db->modflag = '*';
1050
          db->modflag = '*';
1033
          db->lfonly ^= 1;
1051
          db->lfonly ^= 1;
1034
          break;
1052
          break;
1035
 
1053
 
1036
        case MENU_QUIT:
1054
        case MENU_QUIT:
1037
          quitnow = 1;
1055
          quitnow = 1;
1038
          for (curfile = 0; curfile < 10; curfile++) {
1056
          for (curfile = 0; curfile < 10; curfile++) {
1039
            if (dbarr[curfile].modflag) {
1057
            if (dbarr[curfile].modflag) {
1040
              db = select_slot(dbarr, curfile);
1058
              db = select_slot(dbarr, curfile);
1041
              if (ui_confirm_if_unsaved(db) != 0) quitnow = 0;
1059
              if (ui_confirm_if_unsaved(db) != 0) quitnow = 0;
1042
            }
1060
            }
1043
          }
1061
          }
1044
          break;
1062
          break;
1045
      }
1063
      }
1046
 
1064
 
1047
      if (quitnow) break;
1065
      if (quitnow) break;
1048
 
1066
 
1049
    } else if (k == 0x0D) { /* ENTER */
1067
    } else if (k == 0x0D) { /* ENTER */
1050
      unsigned short off = db->xoffset + db->cursorposx;
1068
      unsigned short off = db->xoffset + db->cursorposx;
1051
      /* add a new line */
1069
      /* add a new line */
1052
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1070
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1053
        db->modflag = '*';
1071
        db->modflag = '*';
1054
        db->cursor = db->cursor->prev; /* back to original line */
1072
        db->cursor = db->cursor->prev; /* back to original line */
1055
        /* trim the line above */
1073
        /* trim the line above */
1056
        db->cursor->len = off;
1074
        db->cursor->len = off;
1057
        /* move cursor to the (new) line below */
1075
        /* move cursor to the (new) line below */
1058
        db->curline -= 1;
1076
        db->curline -= 1;
1059
        uidirty.from = db->cursorposy;
1077
        uidirty.from = db->cursorposy;
1060
        uidirty.to = 0xff;
1078
        uidirty.to = 0xff;
1061
        cursor_down(db);
1079
        cursor_down(db);
1062
        cursor_home(db);
1080
        cursor_home(db);
1063
      } else {
1081
      } else {
1064
        /* ERROR: OUT OF MEMORY */
1082
        /* ERROR: OUT OF MEMORY */
1065
      }
1083
      }
1066
 
1084
 
1067
    } else if (k == 0x153) {  /* DEL */
1085
    } else if (k == 0x153) {  /* DEL */
1068
      del(db);
1086
      del(db);
1069
 
1087
 
1070
    } else if (k == 0x008) { /* BKSPC */
1088
    } else if (k == 0x008) { /* BKSPC */
1071
      bkspc(db);
1089
      bkspc(db);
1072
 
1090
 
1073
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1091
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1074
      char c = k;
1092
      char c = k;
1075
      insert_in_line(db, &c, 1);
1093
      insert_in_line(db, &c, 1);
1076
 
1094
 
1077
    } else if (k == 0x009) { /* TAB */
1095
    } else if (k == 0x009) { /* TAB */
1078
      insert_in_line(db, "        ", 8);
1096
      insert_in_line(db, "        ", 8);
1079
 
1097
 
1080
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1098
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1081
      curfile = k - 0x13b;
1099
      curfile = k - 0x13b;
1082
      db = select_slot(dbarr, curfile);
1100
      db = select_slot(dbarr, curfile);
1083
 
1101
 
1084
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1102
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1085
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1103
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1086
      for (;;) {
1104
      for (;;) {
1087
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1105
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1088
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1106
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1089
        cursor_right(db);
1107
        cursor_right(db);
1090
      }
1108
      }
1091
      /* now skip to next non-space or end of file */
1109
      /* now skip to next non-space or end of file */
1092
      for (;;) {
1110
      for (;;) {
1093
        cursor_right(db);
1111
        cursor_right(db);
1094
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1112
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1095
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1113
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1096
      }
1114
      }
1097
 
1115
 
1098
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1116
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1099
      cursor_left(db);
1117
      cursor_left(db);
1100
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1118
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1101
      for (;;) {
1119
      for (;;) {
1102
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1120
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1103
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1121
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1104
        cursor_left(db);
1122
        cursor_left(db);
1105
      }
1123
      }
1106
      /* now skip to next space or start of file */
1124
      /* now skip to next space or start of file */
1107
      for (;;) {
1125
      for (;;) {
1108
        cursor_left(db);
1126
        cursor_left(db);
1109
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1127
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1110
          cursor_right(db);
1128
          cursor_right(db);
1111
          break;
1129
          break;
1112
        }
1130
        }
1113
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1131
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1114
      }
1132
      }
1115
 
1133
 
1116
#ifdef DBG_UNHKEYS
1134
#ifdef DBG_UNHKEYS
1117
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1135
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1118
      char buff[4];
1136
      char buff[4];
1119
      const char *HEX = "0123456789ABCDEF";
1137
      const char *HEX = "0123456789ABCDEF";
1120
      buff[0] = HEX[(k >> 8) & 15];
1138
      buff[0] = HEX[(k >> 8) & 15];
1121
      buff[1] = HEX[(k >> 4) & 15];
1139
      buff[1] = HEX[(k >> 4) & 15];
1122
      buff[2] = HEX[k & 15];
1140
      buff[2] = HEX[k & 15];
1123
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1141
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1124
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1142
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1125
      keyb_getkey();
1143
      keyb_getkey();
1126
      break;
1144
      break;
1127
#endif
1145
#endif
1128
    }
1146
    }
1129
  }
1147
  }
1130
 
1148
 
1131
  mdr_cout_close();
1149
  mdr_cout_close();
1132
 
1150
 
1133
  /* no need to free memory, DOS will do it for me */
1151
  /* no need to free memory, DOS will do it for me */
1134
 
1152
 
1135
  return;
1153
  return;
1136
}
1154
}
1137
 
1155