Subversion Repositories SvarDOS

Rev

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

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