Subversion Repositories SvarDOS

Rev

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

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