Subversion Repositories SvarDOS

Rev

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

Rev 1309 Rev 1310
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>
26
#include <dos.h>
27
#include <fcntl.h>
27
#include <fcntl.h>
28
#include <stdlib.h>
28
#include <stdlib.h>
29
#include <string.h>
29
#include <string.h>
30
#include <malloc.h>   /* _fcalloc() */
30
#include <malloc.h>   /* _fcalloc() */
31
 
31
 
32
#include "mdr\cout.h"
32
#include "mdr\cout.h"
33
#include "mdr\dos.h"
33
#include "mdr\dos.h"
34
#include "mdr\keyb.h"
34
#include "mdr\keyb.h"
35
 
35
 
36
#include "svarlang\svarlang.h"
36
#include "svarlang\svarlang.h"
37
 
37
 
38
#define COL_TXT        0
38
#define COL_TXT        0
39
#define COL_STATUSBAR1 1
39
#define COL_STATUSBAR1 1
40
#define COL_STATUSBAR2 2
40
#define COL_STATUSBAR2 2
41
#define COL_SCROLLBAR  3
41
#define COL_SCROLLBAR  3
42
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
42
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
43
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
43
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
44
 
44
 
45
#define SCROLL_CURSOR 0xB1
45
#define SCROLL_CURSOR 0xB1
46
 
46
 
47
 
47
 
48
struct line {
48
struct line {
49
  struct line far *prev;
49
  struct line far *prev;
50
  struct line far *next;
50
  struct line far *next;
51
  unsigned short len;
51
  unsigned short len;
52
  char payload[1];
52
  char payload[1];
53
};
53
};
54
 
54
 
55
struct linedb {
55
struct linedb {
56
  struct line far *topscreen;
-
 
57
  struct line far *cursor;
56
  struct line far *cursor;
58
  unsigned short xoffset;
57
  unsigned short xoffset;
59
};
58
};
60
 
59
 
61
 
60
 
62
/* returns non-zero on error */
61
/* returns non-zero on error */
63
static int line_add(struct linedb *db, const char far *line) {
62
static int line_add(struct linedb *db, const char far *line) {
64
  unsigned short slen;
63
  unsigned short slen;
65
  struct line far *l;
64
  struct line far *l;
66
 
65
 
67
  /* slen = strlen(line) (but for far pointer) */
66
  /* slen = strlen(line) (but for far pointer) */
68
  for (slen = 0; line[slen] != 0; slen++);
67
  for (slen = 0; line[slen] != 0; slen++);
69
 
68
 
70
  /* trim out CR/LF line endings */
69
  /* trim out CR/LF line endings */
71
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
70
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
72
    slen -= 2;
71
    slen -= 2;
73
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
72
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
74
    slen--;
73
    slen--;
75
  }
74
  }
76
 
75
 
77
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
76
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
78
  if (l == NULL) return(-1);
77
  if (l == NULL) return(-1);
79
 
78
 
80
  l->prev = db->cursor;
79
  l->prev = db->cursor;
81
  if (db->cursor) {
80
  if (db->cursor) {
82
    l->next = db->cursor->next;
81
    l->next = db->cursor->next;
83
    db->cursor->next = l;
82
    db->cursor->next = l;
84
    l->next->prev = l;
83
    l->next->prev = l;
85
  }
84
  }
86
  db->cursor = l;
85
  db->cursor = l;
87
  _fmemcpy(l->payload, line, slen);
86
  _fmemcpy(l->payload, line, slen);
88
  l->len = slen;
87
  l->len = slen;
89
 
88
 
90
  return(0);
89
  return(0);
91
}
90
}
92
 
91
 
93
 
92
 
94
static void db_rewind(struct linedb *db) {
93
static void db_rewind(struct linedb *db) {
95
  if (db->cursor == NULL) return;
94
  if (db->cursor == NULL) return;
96
  while (db->cursor->prev) db->cursor = db->cursor->prev;
95
  while (db->cursor->prev) db->cursor = db->cursor->prev;
97
  db->topscreen = db->cursor;
-
 
98
}
96
}
99
 
97
 
100
 
98
 
101
static void load_colorscheme(void) {
99
static void load_colorscheme(void) {
102
  scheme[COL_TXT] = 0x17;
100
  scheme[COL_TXT] = 0x17;
103
  scheme[COL_STATUSBAR1] = 0x70;
101
  scheme[COL_STATUSBAR1] = 0x70;
104
  scheme[COL_STATUSBAR2] = 0x78;
102
  scheme[COL_STATUSBAR2] = 0x78;
105
  scheme[COL_SCROLLBAR] = 0x70;
103
  scheme[COL_SCROLLBAR] = 0x70;
106
}
104
}
107
 
105
 
108
 
106
 
109
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
107
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
110
  unsigned char i;
108
  unsigned char i;
111
  const char *s = svarlang_strid(0); /* HELP */
109
  const char *s = svarlang_strid(0); /* HELP */
112
  unsigned char helpcol = screenw - (strlen(s) + 4);
110
  unsigned char helpcol = screenw - (strlen(s) + 4);
113
 
111
 
114
  /* clear screen */
112
  /* clear screen */
115
  mdr_cout_cls(scheme[COL_TXT]);
113
  mdr_cout_cls(scheme[COL_TXT]);
116
 
114
 
117
  /* status bar */
115
  /* status bar */
118
  for (i = 0; i < helpcol; i++) {
116
  for (i = 0; i < helpcol; i++) {
119
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
117
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
120
    if (*fname != 0) fname++;
118
    if (*fname != 0) fname++;
121
  }
119
  }
122
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
120
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
123
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
121
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
124
 
122
 
125
  /* scroll bar */
123
  /* scroll bar */
126
  for (i = 0; i < (screenh - 1); i++) {
124
  for (i = 0; i < (screenh - 1); i++) {
127
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
125
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
128
  }
126
  }
129
}
127
}
130
 
128
 
131
 
129
 
132
static void ui_help(unsigned char screenw) {
130
static void ui_help(unsigned char screenw) {
133
#define MAXLINLEN 35
131
#define MAXLINLEN 35
134
  unsigned short i, x, offset;
132
  unsigned short i, x, offset;
135
  offset = (screenw - MAXLINLEN + 1) >> 1;
133
  offset = (screenw - MAXLINLEN + 1) >> 1;
136
  mdr_cout_cursor_hide();
134
  mdr_cout_cursor_hide();
137
  for (i = 2; i <= 12; i++) {
135
  for (i = 2; i <= 12; i++) {
138
    for (x = offset - 2; x < offset + MAXLINLEN; x++) mdr_cout_char(i, x, ' ', scheme[COL_STATUSBAR1]);
136
    for (x = offset - 2; x < offset + MAXLINLEN; x++) mdr_cout_char(i, x, ' ', scheme[COL_STATUSBAR1]);
139
  }
137
  }
140
 
138
 
141
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
139
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
142
  for (i = 0; i <= 2; i++) {
140
  for (i = 0; i <= 2; i++) {
143
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
141
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
144
  }
142
  }
145
  mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
143
  mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
146
  mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
144
  mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
147
 
145
 
148
  keyb_getkey();
146
  keyb_getkey();
149
  mdr_cout_cursor_show();
147
  mdr_cout_cursor_show();
150
#undef MAXLINLEN
148
#undef MAXLINLEN
151
}
149
}
152
 
150
 
153
 
151
 
154
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
152
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto, unsigned char y) {
155
  unsigned char y = 0;
-
 
156
  unsigned char len;
153
  unsigned char len;
157
  struct line far *l;
154
  const struct line far *l;
158
 
155
 
159
#ifdef DBG_REFRESH
156
#ifdef DBG_REFRESH
160
  static char m = 'a';
157
  static char m = 'a';
161
  m++;
158
  m++;
162
  if (m > 'z') m = 'a';
159
  if (m > 'z') m = 'a';
163
#endif
160
#endif
164
 
161
 
-
 
162
  /* rewind cursor line to first line that needs redrawing */
-
 
163
  for (l = db->cursor; y > uidirtyfrom; y--) l = l->prev;
-
 
164
 
-
 
165
  /* iterate over lines and redraw whatever needs to be redrawn */
165
  for (l = db->topscreen; l != NULL; l = l->next, y++) {
166
  for (; l != NULL; l = l->next, y++) {
166
 
167
 
167
    /* skip lines that do not to be refreshed */
168
    /* skip lines that do not need to be refreshed */
168
    if (y < uidirtyfrom) continue;
169
    if (y < uidirtyfrom) continue;
169
    if (y > uidirtyto) break;
170
    if (y > uidirtyto) break;
170
 
171
 
171
    if (db->xoffset < l->len) {
172
    if (db->xoffset < l->len) {
172
      for (len = 0; l->payload[len] != 0; len++) mdr_cout_char(y, len, l->payload[len], scheme[COL_TXT]);
173
      for (len = 0; l->payload[len] != 0; len++) mdr_cout_char(y, len, l->payload[len], scheme[COL_TXT]);
173
    } else {
174
    } else {
174
      len = 0;
175
      len = 0;
175
    }
176
    }
176
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
177
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
177
 
178
 
178
#ifdef DBG_REFRESH
179
#ifdef DBG_REFRESH
179
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
180
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
180
#endif
181
#endif
181
 
182
 
182
    if (y == screenh - 2) break;
183
    if (y == screenh - 2) break;
183
  }
184
  }
-
 
185
 
184
}
186
}
185
 
187
 
186
 
188
 
187
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
189
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
188
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
190
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
189
 
191
 
190
  if (db->cursor->len < db->xoffset) {
192
  if (db->cursor->len < db->xoffset) {
191
    *cursorpos = 0;
193
    *cursorpos = 0;
192
    db->xoffset = db->cursor->len;
194
    db->xoffset = db->cursor->len;
193
    *uidirtyfrom = 0;
195
    *uidirtyfrom = 0;
194
    *uidirtyto = 0xff;
196
    *uidirtyto = 0xff;
195
  } else {
197
  } else {
196
    *cursorpos = db->cursor->len - db->xoffset;
198
    *cursorpos = db->cursor->len - db->xoffset;
197
  }
199
  }
198
}
200
}
199
 
201
 
200
 
202
 
201
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
203
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
202
  if (db->cursor->prev != NULL) {
204
  if (db->cursor->prev != NULL) {
203
    db->cursor = db->cursor->prev;
205
    db->cursor = db->cursor->prev;
204
    if (*cursorposy == 0) {
206
    if (*cursorposy == 0) {
205
      db->topscreen = db->cursor;
-
 
206
      *uidirtyfrom = 0;
207
      *uidirtyfrom = 0;
207
      *uidirtyto = 0xff;
208
      *uidirtyto = 0xff;
208
    } else {
209
    } else {
209
      *cursorposy -= 1;
210
      *cursorposy -= 1;
210
    }
211
    }
211
  }
212
  }
212
}
213
}
213
 
214
 
214
 
215
 
215
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
216
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
216
  /* adjust xoffset to make sure eol is visible on screen */
217
  /* adjust xoffset to make sure eol is visible on screen */
217
  if (db->xoffset > db->cursor->len) {
218
  if (db->xoffset > db->cursor->len) {
218
    db->xoffset = db->cursor->len - 1;
219
    db->xoffset = db->cursor->len - 1;
219
    *uidirtyfrom = 0;
220
    *uidirtyfrom = 0;
220
    *uidirtyto = 0xff;
221
    *uidirtyto = 0xff;
221
  }
222
  }
222
 
223
 
223
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
224
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
224
    db->xoffset = db->cursor->len - screenw + 2;
225
    db->xoffset = db->cursor->len - screenw + 2;
225
    *uidirtyfrom = 0;
226
    *uidirtyfrom = 0;
226
    *uidirtyto = 0xff;
227
    *uidirtyto = 0xff;
227
  }
228
  }
228
  *cursorposx = db->cursor->len - db->xoffset;
229
  *cursorposx = db->cursor->len - db->xoffset;
229
}
230
}
230
 
231
 
231
 
232
 
232
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
233
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
233
  if (db->cursor->next != NULL) {
234
  if (db->cursor->next != NULL) {
234
    db->cursor = db->cursor->next;
235
    db->cursor = db->cursor->next;
235
    if (*cursorposy < screenh - 2) {
236
    if (*cursorposy < screenh - 2) {
236
      *cursorposy += 1;
237
      *cursorposy += 1;
237
    } else {
238
    } else {
238
      db->topscreen = db->topscreen->next;
-
 
239
      *uidirtyfrom = 0;
239
      *uidirtyfrom = 0;
240
      *uidirtyto = 0xff;
240
      *uidirtyto = 0xff;
241
    }
241
    }
242
  }
242
  }
243
}
243
}
244
 
244
 
245
 
245
 
246
static void cursor_left(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
246
static void cursor_left(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
247
  if (*cursorposx > 0) {
247
  if (*cursorposx > 0) {
248
    *cursorposx -= 1;
248
    *cursorposx -= 1;
249
  } else if (db->xoffset > 0) {
249
  } else if (db->xoffset > 0) {
250
    db->xoffset -= 1;
250
    db->xoffset -= 1;
251
    *uidirtyfrom = 0;
251
    *uidirtyfrom = 0;
252
    *uidirtyto = 0xff;
252
    *uidirtyto = 0xff;
253
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
253
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
254
    cursor_up(db, cursorposy, uidirtyfrom, uidirtyto);
254
    cursor_up(db, cursorposy, uidirtyfrom, uidirtyto);
255
    cursor_eol(db, cursorposx, screenw, uidirtyfrom, uidirtyto);
255
    cursor_eol(db, cursorposx, screenw, uidirtyfrom, uidirtyto);
256
  }
256
  }
257
}
257
}
258
 
258
 
259
 
259
 
260
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
260
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
261
  *cursorposx = 0;
261
  *cursorposx = 0;
262
  if (db->xoffset != 0) {
262
  if (db->xoffset != 0) {
263
    db->xoffset = 0;
263
    db->xoffset = 0;
264
    *uidirtyfrom = 0;
264
    *uidirtyfrom = 0;
265
    *uidirtyto = 0xff;
265
    *uidirtyto = 0xff;
266
  }
266
  }
267
}
267
}
268
 
268
 
269
 
269
 
270
static void cursor_right(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
270
static void cursor_right(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
271
  if (db->cursor->len > db->xoffset + *cursorposx) {
271
  if (db->cursor->len > db->xoffset + *cursorposx) {
272
    if (*cursorposx < screenw - 2) {
272
    if (*cursorposx < screenw - 2) {
273
      *cursorposx += 1;
273
      *cursorposx += 1;
274
    } else {
274
    } else {
275
      db->xoffset += 1;
275
      db->xoffset += 1;
276
      *uidirtyfrom = 0;
276
      *uidirtyfrom = 0;
277
      *uidirtyto = 0xff;
277
      *uidirtyto = 0xff;
278
    }
278
    }
279
  } else {
279
  } else {
280
    cursor_down(db, cursorposy, screenh, uidirtyfrom, uidirtyto);
280
    cursor_down(db, cursorposy, screenh, uidirtyfrom, uidirtyto);
281
    cursor_home(db, cursorposx, uidirtyfrom, uidirtyto);
281
    cursor_home(db, cursorposx, uidirtyfrom, uidirtyto);
282
  }
282
  }
283
}
283
}
284
 
284
 
285
 
285
 
286
static void del(struct linedb *db, unsigned char cursorposx, unsigned char cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
286
static void del(struct linedb *db, unsigned char cursorposx, unsigned char cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
287
  if (cursorposx + db->xoffset < db->cursor->len) {
287
  if (cursorposx + db->xoffset < db->cursor->len) {
288
    _fmemmove(db->cursor->payload + cursorposx + db->xoffset, db->cursor->payload + cursorposx + db->xoffset + 1, db->cursor->len - cursorposx - db->xoffset);
288
    _fmemmove(db->cursor->payload + cursorposx + db->xoffset, db->cursor->payload + cursorposx + db->xoffset + 1, db->cursor->len - cursorposx - db->xoffset);
289
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
289
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
290
    *uidirtyfrom = cursorposy;
290
    *uidirtyfrom = cursorposy;
291
    *uidirtyto = cursorposy;
291
    *uidirtyto = cursorposy;
292
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
292
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
293
    struct line far *nextline = db->cursor->next;
293
    struct line far *nextline = db->cursor->next;
294
    if (db->cursor->next->len > 0) {
294
    if (db->cursor->next->len > 0) {
295
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
295
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
296
      if (newptr != NULL) {
296
      if (newptr != NULL) {
297
        db->cursor = newptr;
297
        db->cursor = newptr;
298
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
298
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
299
        db->cursor->len += db->cursor->next->len;
299
        db->cursor->len += db->cursor->next->len;
300
        /* update db->topscreen if needed */
-
 
301
        if (cursorposy == 0) db->topscreen = db->cursor;
-
 
302
      }
300
      }
303
    }
301
    }
304
    db->cursor->next = db->cursor->next->next;
302
    db->cursor->next = db->cursor->next->next;
305
    db->cursor->next->prev = db->cursor;
303
    db->cursor->next->prev = db->cursor;
306
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
304
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
307
    _ffree(nextline);
305
    _ffree(nextline);
308
    *uidirtyfrom = cursorposy;
306
    *uidirtyfrom = cursorposy;
309
    *uidirtyto = 0xff;
307
    *uidirtyto = 0xff;
310
  }
308
  }
311
}
309
}
312
 
310
 
313
 
311
 
314
static void bkspc(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
312
static void bkspc(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
315
 
313
 
316
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
314
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
317
  if ((*cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
315
  if ((*cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
318
 
316
 
319
  cursor_left(db, cursorposx, cursorposy, screenw, uidirtyfrom, uidirtyto);
317
  cursor_left(db, cursorposx, cursorposy, screenw, uidirtyfrom, uidirtyto);
320
  del(db, *cursorposx, *cursorposy, uidirtyfrom, uidirtyto);
318
  del(db, *cursorposx, *cursorposy, uidirtyfrom, uidirtyto);
321
}
319
}
322
 
320
 
323
 
321
 
324
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
322
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
325
 * of argc and argv, saves some 330 bytes of binary size */
323
 * of argc and argv, saves some 330 bytes of binary size */
326
static char *parseargv(void) {
324
static char *parseargv(void) {
327
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
325
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
328
  unsigned char count = 0;
326
  unsigned char count = 0;
329
  char *argv[4];
327
  char *argv[4];
330
 
328
 
331
  while (count < 4) {
329
  while (count < 4) {
332
    /* jump to nearest arg */
330
    /* jump to nearest arg */
333
    while (*tail == ' ') {
331
    while (*tail == ' ') {
334
      *tail = 0;
332
      *tail = 0;
335
      tail++;
333
      tail++;
336
    }
334
    }
337
 
335
 
338
    if (*tail == '\r') {
336
    if (*tail == '\r') {
339
      *tail = 0;
337
      *tail = 0;
340
      break;
338
      break;
341
    }
339
    }
342
 
340
 
343
    argv[count++] = tail;
341
    argv[count++] = tail;
344
 
342
 
345
    /* jump to next delimiter */
343
    /* jump to next delimiter */
346
    while ((*tail != ' ') && (*tail != '\r')) tail++;
344
    while ((*tail != ' ') && (*tail != '\r')) tail++;
347
  }
345
  }
348
 
346
 
349
  /* check args now */
347
  /* check args now */
350
  if (count != 1) return(NULL);
348
  if (count != 1) return(NULL);
351
 
349
 
352
  return(argv[0]);
350
  return(argv[0]);
353
}
351
}
354
 
352
 
355
 
353
 
356
static int loadfile(struct linedb *db, const char *fname) {
354
static int loadfile(struct linedb *db, const char *fname) {
357
  char buff[1024];
355
  char buff[1024];
358
  unsigned int prevlen = 0, len, llen;
356
  unsigned int prevlen = 0, len, llen;
359
  int fd;
357
  int fd;
360
  int r = 0;
358
  int r = 0;
361
 
359
 
362
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
360
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
363
    mdr_coutraw_puts("Failed to open file:");
361
    mdr_coutraw_puts("Failed to open file:");
364
    mdr_coutraw_puts(fname);
362
    mdr_coutraw_puts(fname);
365
    return(-1);
363
    return(-1);
366
  }
364
  }
367
 
365
 
368
  do {
366
  do {
369
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
367
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
370
      len += prevlen;
368
      len += prevlen;
371
    } else {
369
    } else {
372
      len = prevlen;
370
      len = prevlen;
373
    }
371
    }
374
 
372
 
375
    /* look for nearest \n and replace with 0*/
373
    /* look for nearest \n and replace with 0*/
376
    for (llen = 0; buff[llen] != '\n'; llen++) {
374
    for (llen = 0; buff[llen] != '\n'; llen++) {
377
      if (llen == sizeof(buff)) break;
375
      if (llen == sizeof(buff)) break;
378
    }
376
    }
379
    buff[llen] = 0;
377
    buff[llen] = 0;
380
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
378
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
381
    if (line_add(db, buff) != 0) {
379
    if (line_add(db, buff) != 0) {
382
      mdr_coutraw_puts("out of memory");
380
      mdr_coutraw_puts("out of memory");
383
      r = -1;
381
      r = -1;
384
      break;
382
      break;
385
    }
383
    }
386
 
384
 
387
    len -= llen + 1;
385
    len -= llen + 1;
388
    memmove(buff, buff + llen + 1, len);
386
    memmove(buff, buff + llen + 1, len);
389
    prevlen = len;
387
    prevlen = len;
390
  } while (len > 0);
388
  } while (len > 0);
391
 
389
 
392
  _dos_close(fd);
390
  _dos_close(fd);
393
 
391
 
394
  return(r);
392
  return(r);
395
}
393
}
396
 
394
 
397
 
395
 
398
int main(void) {
396
int main(void) {
399
  const char *fname;
397
  const char *fname;
400
  struct linedb db;
398
  struct linedb db;
401
  unsigned char screenw = 0, screenh = 0;
399
  unsigned char screenw = 0, screenh = 0;
402
  unsigned char cursorposx = 0, cursorposy = 0;
400
  unsigned char cursorposx = 0, cursorposy = 0;
403
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
401
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
404
 
402
 
405
  bzero(&db, sizeof(db));
403
  bzero(&db, sizeof(db));
406
 
404
 
407
  {
405
  {
408
    char nlspath[128], lang[8];
406
    char nlspath[128], lang[8];
409
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
407
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
410
  }
408
  }
411
 
409
 
412
  fname = parseargv();
410
  fname = parseargv();
413
 
411
 
414
  if (fname == NULL) {
412
  if (fname == NULL) {
415
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
413
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
416
    return(0);
414
    return(0);
417
  }
415
  }
418
 
416
 
419
  /* load file */
417
  /* load file */
420
  if (loadfile(&db, fname) != 0) return(1);
418
  if (loadfile(&db, fname) != 0) return(1);
421
 
419
 
422
  /* add an empty line at end if not present already */
420
  /* add an empty line at end if not present already */
423
  if (db.cursor->len != 0) line_add(&db, "");
421
  if (db.cursor->len != 0) line_add(&db, "");
424
 
422
 
425
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
423
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
426
  ui_basic(screenw, screenh, fname);
424
  ui_basic(screenw, screenh, fname);
427
 
425
 
428
  db_rewind(&db);
426
  db_rewind(&db);
429
 
427
 
430
  for (;;) {
428
  for (;;) {
431
    int k;
429
    int k;
432
 
430
 
433
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
431
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
434
    mdr_cout_locate(cursorposy, cursorposx);
432
    mdr_cout_locate(cursorposy, cursorposx);
435
 
433
 
436
    if (uidirtyfrom != 0xff) {
434
    if (uidirtyfrom != 0xff) {
437
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
435
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto, cursorposy);
438
      uidirtyfrom = 0xff;
436
      uidirtyfrom = 0xff;
439
    }
437
    }
440
 
438
 
441
    k = keyb_getkey();
439
    k = keyb_getkey();
442
 
440
 
443
    if (k == 0x150) { /* down */
441
    if (k == 0x150) { /* down */
444
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
442
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
445
 
443
 
446
    } else if (k == 0x148) { /* up */
444
    } else if (k == 0x148) { /* up */
447
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
445
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
448
 
446
 
449
    } else if (k == 0x14D) { /* right */
447
    } else if (k == 0x14D) { /* right */
450
      cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
448
      cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
451
 
449
 
452
    } else if (k == 0x14B) { /* left */
450
    } else if (k == 0x14B) { /* left */
453
      cursor_left(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
451
      cursor_left(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
454
 
452
 
455
    } else if (k == 0x149) { /* pgup */
453
    } else if (k == 0x149) { /* pgup */
456
      // TODO
454
      // TODO
457
 
455
 
458
    } else if (k == 0x151) { /* pgdown */
456
    } else if (k == 0x151) { /* pgdown */
459
      // TODO
457
      // TODO
460
 
458
 
461
    } else if (k == 0x147) { /* home */
459
    } else if (k == 0x147) { /* home */
462
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
460
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
463
 
461
 
464
    } else if (k == 0x14F) { /* end */
462
    } else if (k == 0x14F) { /* end */
465
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
463
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
466
 
464
 
467
    } else if (k == 0x1B) { /* ESC */
465
    } else if (k == 0x1B) { /* ESC */
468
      break;
466
      break;
469
 
467
 
470
    } else if (k == 0x0D) { /* ENTER */
468
    } else if (k == 0x0D) { /* ENTER */
471
      /* add a new line */
469
      /* add a new line */
472
      if (line_add(&db, db.cursor->payload + db.xoffset + cursorposx) == 0) {
470
      if (line_add(&db, db.cursor->payload + db.xoffset + cursorposx) == 0) {
473
        /* trim the line above */
471
        /* trim the line above */
474
        db.cursor->prev->len = db.xoffset + cursorposx;
472
        db.cursor->prev->len = db.xoffset + cursorposx;
475
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
473
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
476
        /* move cursor to the (new) line below */
474
        /* move cursor to the (new) line below */
477
        cursorposx = 0;
475
        cursorposx = 0;
478
        if (cursorposy < screenh - 2) {
476
        if (cursorposy < screenh - 2) {
479
          uidirtyfrom = cursorposy;
477
          uidirtyfrom = cursorposy;
480
          cursorposy++;
478
          cursorposy++;
481
        } else {
479
        } else {
482
          db.topscreen = db.topscreen->next;
-
 
483
          uidirtyfrom = 0;
480
          uidirtyfrom = 0;
484
        }
481
        }
485
        uidirtyto = 0xff;
482
        uidirtyto = 0xff;
486
      } else {
483
      } else {
487
        /* ERROR: OUT OF MEMORY */
484
        /* ERROR: OUT OF MEMORY */
488
      }
485
      }
489
 
486
 
490
    } else if (k == 0x153) {  /* DEL */
487
    } else if (k == 0x153) {  /* DEL */
491
      del(&db, cursorposx, cursorposy, &uidirtyfrom, &uidirtyto);
488
      del(&db, cursorposx, cursorposy, &uidirtyfrom, &uidirtyto);
492
 
489
 
493
    } else if (k == 0x008) { /* BKSPC */
490
    } else if (k == 0x008) { /* BKSPC */
494
      bkspc(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
491
      bkspc(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
495
 
492
 
496
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
493
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
497
      struct line far *n;
494
      struct line far *n;
498
      n = _frealloc(db.cursor, sizeof(struct line) + db.cursor->len);
495
      n = _frealloc(db.cursor, sizeof(struct line) + db.cursor->len);
499
      if (n != NULL) {
496
      if (n != NULL) {
500
        unsigned short off = db.xoffset + cursorposx;
497
        unsigned short off = db.xoffset + cursorposx;
501
        if (n->prev) n->prev->next = n;
498
        if (n->prev) n->prev->next = n;
502
        if (n->next) n->next->prev = n;
499
        if (n->next) n->next->prev = n;
503
        if (db.topscreen == db.cursor) db.topscreen = n;
-
 
504
        db.cursor = n;
500
        db.cursor = n;
505
        _fmemmove(db.cursor->payload + off + 1, db.cursor->payload + off, db.cursor->len - off + 1);
501
        _fmemmove(db.cursor->payload + off + 1, db.cursor->payload + off, db.cursor->len - off + 1);
506
        db.cursor->len += 1;
502
        db.cursor->len += 1;
507
        uidirtyfrom = cursorposy;
503
        uidirtyfrom = cursorposy;
508
        uidirtyto = cursorposy;
504
        uidirtyto = cursorposy;
509
        db.cursor->payload[off] = k;
505
        db.cursor->payload[off] = k;
510
        cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
506
        cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
511
      }
507
      }
512
 
508
 
513
    } else if (k == 0x13b) { /* F1 */
509
    } else if (k == 0x13b) { /* F1 */
514
      ui_help(screenw);
510
      ui_help(screenw);
515
      uidirtyfrom = 0;
511
      uidirtyfrom = 0;
516
      uidirtyto = 0xff;
512
      uidirtyto = 0xff;
517
 
513
 
518
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
514
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
519
      char buff[4];
515
      char buff[4];
520
      const char *HEX = "0123456789ABCDEF";
516
      const char *HEX = "0123456789ABCDEF";
521
      buff[0] = HEX[(k >> 8) & 15];
517
      buff[0] = HEX[(k >> 8) & 15];
522
      buff[1] = HEX[(k >> 4) & 15];
518
      buff[1] = HEX[(k >> 4) & 15];
523
      buff[2] = HEX[k & 15];
519
      buff[2] = HEX[k & 15];
524
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
520
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
525
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
521
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
526
      keyb_getkey();
522
      keyb_getkey();
527
      break;
523
      break;
528
    }
524
    }
529
  }
525
  }
530
 
526
 
531
  mdr_cout_close();
527
  mdr_cout_close();
532
 
528
 
533
  /* TODO free memory */
529
  /* TODO free memory */
534
 
530
 
535
  return(0);
531
  return(0);
536
}
532
}
537
 
533