Subversion Repositories SvarDOS

Rev

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

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