Subversion Repositories SvarDOS

Rev

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

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