Subversion Repositories SvarDOS

Rev

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

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