Subversion Repositories SvarDOS

Rev

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

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