Subversion Repositories SvarDOS

Rev

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

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