Subversion Repositories SvarDOS

Rev

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

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