Subversion Repositories SvarDOS

Rev

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

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