Subversion Repositories SvarDOS

Rev

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

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