Subversion Repositories SvarDOS

Rev

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

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