Subversion Repositories SvarDOS

Rev

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

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