Subversion Repositories SvarDOS

Rev

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

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