Subversion Repositories SvarDOS

Rev

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

Rev 1327 Rev 1328
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\bios.h"
33
#include "mdr\cout.h"
33
#include "mdr\cout.h"
34
#include "mdr\dos.h"
34
#include "mdr\dos.h"
35
#include "mdr\keyb.h"
35
#include "mdr\keyb.h"
36
 
36
 
37
#include "svarlang\svarlang.h"
37
#include "svarlang\svarlang.h"
38
 
38
 
39
 
39
 
40
/*****************************************************************************
40
/*****************************************************************************
41
 * global variables and definitions                                          *
41
 * global variables and definitions                                          *
42
 *****************************************************************************/
42
 *****************************************************************************/
43
#define COL_TXT        0
43
#define COL_TXT        0
44
#define COL_STATUSBAR1 1
44
#define COL_STATUSBAR1 1
45
#define COL_STATUSBAR2 2
45
#define COL_STATUSBAR2 2
46
#define COL_SCROLLBAR  3
46
#define COL_SCROLLBAR  3
47
#define COL_MSG        4
47
#define COL_MSG        4
48
#define COL_ERR        5
48
#define COL_ERR        5
49
 
49
 
50
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
50
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
51
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70, 0x70, 0xf0};
51
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70, 0x70, 0xf0};
52
 
52
 
53
static unsigned char screenw, screenh;
53
static unsigned char screenw, screenh;
54
 
54
 
55
//static union {
-
 
56
static struct {
55
static struct {
57
    unsigned char from;
56
    unsigned char from;
58
    unsigned char to;
57
    unsigned char to;
59
//  };
-
 
60
//  unsigned short fromto;
-
 
61
} uidirty = {0, 0xff}; /* make sure to redraw entire UI at first run */
58
} uidirty = {0, 0xff}; /* make sure to redraw entire UI at first run */
62
//} uidirty = {0xff, 0};
-
 
63
 
59
 
64
#define SCROLL_CURSOR 0xB1
60
#define SCROLL_CURSOR 0xB1
65
 
61
 
66
struct line {
62
struct line {
67
  struct line far *prev;
63
  struct line far *prev;
68
  struct line far *next;
64
  struct line far *next;
69
  unsigned short len;
65
  unsigned short len;
70
  char payload[1];
66
  char payload[1];
71
};
67
};
72
 
68
 
73
struct file {
69
struct file {
74
  int fd;
70
  int fd;
75
  struct line far *cursor;
71
  struct line far *cursor;
76
  unsigned short xoffset;
72
  unsigned short xoffset;
77
  unsigned char cursorposx;
73
  unsigned char cursorposx;
78
  unsigned char cursorposy;
74
  unsigned char cursorposy;
-
 
75
  unsigned short totlines;
-
 
76
  unsigned short curline;
79
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
77
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
80
  char fname[1]; /* dynamically sized */
78
  char fname[1]; /* dynamically sized */
81
};
79
};
82
 
80
 
83
 
81
 
84
/*****************************************************************************
82
/*****************************************************************************
85
 * functions                                                                 *
83
 * functions                                                                 *
86
 *****************************************************************************/
84
 *****************************************************************************/
87
 
85
 
88
/* adds a new line at cursor position into file linked list and dvance cursor
86
/* adds a new line at cursor position into file linked list and advance cursor
89
 * returns non-zero on error */
87
 * returns non-zero on error */
90
static int line_add(struct file *db, const char far *line) {
88
static int line_add(struct file *db, const char far *line, unsigned short slen) {
91
  unsigned short slen;
-
 
92
  struct line far *l;
89
  struct line far *l;
93
 
90
 
94
  /* slen = strlen(line) (but for far pointer) */
-
 
95
  for (slen = 0; line[slen] != 0; slen++);
-
 
96
 
-
 
97
  /* trim out CR/LF line endings */
-
 
98
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
-
 
99
    slen -= 2;
-
 
100
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
-
 
101
    slen--;
-
 
102
  }
-
 
103
 
-
 
104
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
91
  l = _fcalloc(1, sizeof(struct line) + slen);
105
  if (l == NULL) return(-1);
92
  if (l == NULL) return(-1);
106
 
93
 
107
  l->prev = db->cursor;
94
  l->prev = db->cursor;
108
  if (db->cursor) {
95
  if (db->cursor) {
109
    l->next = db->cursor->next;
96
    l->next = db->cursor->next;
110
    db->cursor->next = l;
97
    db->cursor->next = l;
111
    l->next->prev = l;
98
    l->next->prev = l;
112
  }
99
  }
113
  db->cursor = l;
100
  db->cursor = l;
114
  _fmemcpy(l->payload, line, slen);
101
  _fmemcpy(l->payload, line, slen);
115
  l->len = slen;
102
  l->len = slen;
116
 
103
 
-
 
104
  db->totlines += 1;
-
 
105
  db->curline += 1;
-
 
106
 
117
  return(0);
107
  return(0);
118
}
108
}
119
 
109
 
120
 
110
 
121
/* append a nul-terminated string to line at cursor position */
111
/* append a nul-terminated string to line at cursor position */
122
static int line_append(struct file *f, const char far *buf, unsigned short len) {
112
static int line_append(struct file *f, const char far *buf, unsigned short len) {
123
  struct line far *n;
113
  struct line far *n;
124
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
114
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
125
  n = _frealloc(f->cursor, sizeof(struct line) + f->cursor->len + len);
115
  n = _frealloc(f->cursor, sizeof(struct line) + f->cursor->len + len);
126
  if (n == NULL) return(-1);
116
  if (n == NULL) return(-1);
127
  f->cursor = n;
117
  f->cursor = n;
128
  _fmemcpy(f->cursor->payload + f->cursor->len, buf, len);
118
  _fmemcpy(f->cursor->payload + f->cursor->len, buf, len);
129
  f->cursor->len += len;
119
  f->cursor->len += len;
130
 
120
 
131
  /* rewire the linked list */
121
  /* rewire the linked list */
132
  if (f->cursor->next) f->cursor->next->prev = f->cursor;
122
  if (f->cursor->next) f->cursor->next->prev = f->cursor;
133
  if (f->cursor->prev) f->cursor->prev->next = f->cursor;
123
  if (f->cursor->prev) f->cursor->prev->next = f->cursor;
134
 
124
 
135
  return(0);
125
  return(0);
136
}
126
}
137
 
127
 
138
 
128
 
139
static void db_rewind(struct file *db) {
129
static void db_rewind(struct file *db) {
140
  if (db->cursor == NULL) return;
130
  if (db->cursor == NULL) return;
141
  while (db->cursor->prev) db->cursor = db->cursor->prev;
131
  while (db->cursor->prev) db->cursor = db->cursor->prev;
-
 
132
  db->curline = 0;
142
}
133
}
143
 
134
 
144
 
135
 
145
static void load_colorscheme(void) {
136
static void load_colorscheme(void) {
146
  scheme[COL_TXT] = 0x17;
137
  scheme[COL_TXT] = 0x17;
147
  scheme[COL_STATUSBAR1] = 0x70;
138
  scheme[COL_STATUSBAR1] = 0x70;
148
  scheme[COL_STATUSBAR2] = 0x78;
139
  scheme[COL_STATUSBAR2] = 0x78;
149
  scheme[COL_SCROLLBAR] = 0x70;
140
  scheme[COL_SCROLLBAR] = 0x70;
150
  scheme[COL_MSG] = 0xf0;
141
  scheme[COL_MSG] = 0xf0;
151
  scheme[COL_ERR] = 0x4f;
142
  scheme[COL_ERR] = 0x4f;
152
}
143
}
153
 
144
 
154
 
145
 
155
static void ui_basic(const struct file *db) {
146
static void ui_basic(const struct file *db) {
156
  unsigned char i;
-
 
157
  const char *s = svarlang_strid(0); /* HELP */
147
  const char *s = svarlang_strid(0); /* HELP */
158
  unsigned char helpcol = screenw - (strlen(s) + 4);
148
  unsigned char helpcol = screenw - (strlen(s) + 4);
159
 
149
 
160
  /* fill status bar with background */
150
  /* fill status bar with background */
161
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
151
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
162
 
152
 
163
  /* filename */
153
  /* filename */
164
  if (db->fname[0] == 0) {
154
  if (db->fname[0] == 0) {
165
    mdr_cout_str(screenh - 1, 0, svarlang_str(0, 1), scheme[COL_STATUSBAR1], screenw);
155
    mdr_cout_str(screenh - 1, 0, svarlang_str(0, 1), scheme[COL_STATUSBAR1], screenw);
166
  } else {
156
  } else {
167
    mdr_cout_str(screenh - 1, 0, db->fname, scheme[COL_STATUSBAR1], screenw);
157
    mdr_cout_str(screenh - 1, 0, db->fname, scheme[COL_STATUSBAR1], screenw);
168
  }
158
  }
169
 
159
 
170
  /* eol type */
160
  /* eol type */
171
  if (db->lfonly) {
161
  if (db->lfonly) {
172
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
162
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
173
  } else {
163
  } else {
174
    mdr_cout_str(screenh - 1, helpcol - 5, "CRLF", scheme[COL_STATUSBAR1], 5);
164
    mdr_cout_str(screenh - 1, helpcol - 5, "CRLF", scheme[COL_STATUSBAR1], 5);
175
  }
165
  }
176
 
166
 
177
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
167
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
178
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
168
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
179
 
-
 
180
  /* scroll bar */
-
 
181
  for (i = 0; i < (screenh - 1); i++) {
-
 
182
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
-
 
183
  }
-
 
184
}
169
}
185
 
170
 
186
 
171
 
187
static void ui_msg(const char *msg, unsigned char attr) {
172
static void ui_msg(const char *msg, unsigned char attr) {
188
  unsigned short x, y, msglen, i;
173
  unsigned short x, y, msglen, i;
189
  msglen = strlen(msg);
174
  msglen = strlen(msg);
190
  y = (screenh - 4) >> 1;
175
  y = (screenh - 4) >> 1;
191
  x = (screenw - msglen - 4) >> 1;
176
  x = (screenw - msglen - 4) >> 1;
192
  for (i = y+2; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
177
  for (i = y+2; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
193
  mdr_cout_str(y+1, x+1, msg, attr, msglen);
178
  mdr_cout_str(y+1, x+1, msg, attr, msglen);
194
 
179
 
195
  if (uidirty.from > y) uidirty.from = y;
180
  if (uidirty.from > y) uidirty.from = y;
196
  if (uidirty.to < y+2) uidirty.to = y+2;
181
  if (uidirty.to < y+2) uidirty.to = y+2;
197
}
182
}
198
 
183
 
199
 
184
 
200
static void ui_help(void) {
185
static void ui_help(void) {
201
#define MAXLINLEN 35
186
#define MAXLINLEN 35
202
  unsigned short i, offset;
187
  unsigned short i, offset;
203
  offset = (screenw - MAXLINLEN + 2) >> 1;
188
  offset = (screenw - MAXLINLEN + 2) >> 1;
204
  mdr_cout_cursor_hide();
189
  mdr_cout_cursor_hide();
205
  for (i = 2; i <= 15; i++) {
190
  for (i = 2; i <= 15; i++) {
206
    mdr_cout_char_rep(i, offset - 2, ' ', scheme[COL_STATUSBAR1], MAXLINLEN + 2);
191
    mdr_cout_char_rep(i, offset - 2, ' ', scheme[COL_STATUSBAR1], MAXLINLEN + 2);
207
  }
192
  }
208
 
193
 
209
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
194
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
210
  for (i = 0; i <= 4; i++) {
195
  for (i = 0; i <= 4; i++) {
211
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
196
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
212
  }
197
  }
213
  mdr_cout_str(5 + 1 + i, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
198
  mdr_cout_str(5 + 1 + i, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
214
 
199
 
215
  /* Press any key */
200
  /* Press any key */
216
  mdr_cout_str(14, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
201
  mdr_cout_str(14, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
217
 
202
 
218
  keyb_getkey();
203
  keyb_getkey();
219
  mdr_cout_cursor_show();
204
  mdr_cout_cursor_show();
220
#undef MAXLINLEN
205
#undef MAXLINLEN
221
}
206
}
222
 
207
 
223
 
208
 
224
static void ui_refresh(const struct file *db) {
209
static void ui_refresh(const struct file *db) {
225
  unsigned char x;
210
  unsigned char x;
226
  const struct line far *l;
211
  const struct line far *l;
227
  unsigned char y = db->cursorposy;
212
  unsigned char y = db->cursorposy;
228
 
213
 
229
#ifdef DBG_REFRESH
214
#ifdef DBG_REFRESH
230
  static char m = 'a';
215
  static char m = 'a';
231
  m++;
216
  m++;
232
  if (m > 'z') m = 'a';
217
  if (m > 'z') m = 'a';
233
#endif
218
#endif
234
 
219
 
235
  /* rewind cursor line to first line that needs redrawing */
220
  /* rewind cursor line to first line that needs redrawing */
236
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
221
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
237
 
222
 
238
  /* iterate over lines and redraw whatever needs to be redrawn */
223
  /* iterate over lines and redraw whatever needs to be redrawn */
239
  for (; l != NULL; l = l->next, y++) {
224
  for (; l != NULL; l = l->next, y++) {
240
 
225
 
241
    /* skip lines that do not need to be refreshed */
226
    /* skip lines that do not need to be refreshed */
242
    if (y < uidirty.from) continue;
227
    if (y < uidirty.from) continue;
243
    if (y > uidirty.to) break;
228
    if (y > uidirty.to) break;
244
 
229
 
245
    x = 0;
230
    x = 0;
246
    if (db->xoffset < l->len) {
231
    if (db->xoffset < l->len) {
247
      unsigned char i, limit;
232
      unsigned char i, limit;
248
      if (l->len - db->xoffset < screenw) {
233
      if (l->len - db->xoffset < screenw) {
249
        limit = l->len;
234
        limit = l->len;
250
      } else {
235
      } else {
251
        limit = db->xoffset + screenw - 1;
236
        limit = db->xoffset + screenw - 1;
252
      }
237
      }
253
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], scheme[COL_TXT]);
238
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], scheme[COL_TXT]);
254
    }
239
    }
255
 
240
 
256
    /* write empty spaces until end of line */
241
    /* write empty spaces until end of line */
257
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', scheme[COL_TXT], screenw - 1 - x);
242
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', scheme[COL_TXT], screenw - 1 - x);
258
 
243
 
259
#ifdef DBG_REFRESH
244
#ifdef DBG_REFRESH
260
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
245
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
261
#endif
246
#endif
262
 
247
 
263
    if (y == screenh - 2) break;
248
    if (y == screenh - 2) break;
264
  }
249
  }
265
 
250
 
266
  /* fill all lines below if empty (and they need to be redrawn) */
251
  /* fill all lines below if empty (and they need to be redrawn) */
267
  if (l == NULL) {
252
  if (l == NULL) {
268
    while ((y < screenh - 1) && (y < uidirty.to)) {
253
    while ((y < screenh - 1) && (y < uidirty.to)) {
269
      mdr_cout_char_rep(y++, 0, ' ', scheme[COL_TXT], screenw - 1);
254
      mdr_cout_char_rep(y++, 0, ' ', scheme[COL_TXT], screenw - 1);
270
    }
255
    }
271
  }
256
  }
-
 
257
 
-
 
258
  /* scroll bar */
-
 
259
  for (y = 0; y < (screenh - 1); y++) {
-
 
260
    mdr_cout_char(y, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
-
 
261
  }
-
 
262
 
-
 
263
  /* scroll cursor */
-
 
264
  if (db->totlines >= screenh) {
-
 
265
    unsigned short topline = db->curline - db->cursorposy;
-
 
266
    unsigned short col;
-
 
267
    unsigned short totlines = db->totlines - screenh + 1;
-
 
268
    if (db->totlines - screenh > screenh) {
-
 
269
      col = topline / (totlines / (screenh - 1));
-
 
270
    } else {
-
 
271
      col = topline * (screenh - 1) / totlines;
-
 
272
    }
-
 
273
    if (col >= screenh - 1) col = screenh - 2;
-
 
274
    mdr_cout_char(col, screenw - 1, ' ', scheme[COL_SCROLLBAR]);
-
 
275
  }
272
}
276
}
273
 
277
 
274
 
278
 
275
static void check_cursor_not_after_eol(struct file *db) {
279
static void check_cursor_not_after_eol(struct file *db) {
276
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
280
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
277
 
281
 
278
  if (db->cursor->len < db->xoffset) {
282
  if (db->cursor->len < db->xoffset) {
279
    db->cursorposx = 0;
283
    db->cursorposx = 0;
280
    db->xoffset = db->cursor->len;
284
    db->xoffset = db->cursor->len;
281
    uidirty.from = 0;
285
    uidirty.from = 0;
282
    uidirty.to = 0xff;
286
    uidirty.to = 0xff;
283
  } else {
287
  } else {
284
    db->cursorposx = db->cursor->len - db->xoffset;
288
    db->cursorposx = db->cursor->len - db->xoffset;
285
  }
289
  }
286
}
290
}
287
 
291
 
288
 
292
 
289
static void cursor_up(struct file *db) {
293
static void cursor_up(struct file *db) {
290
  if (db->cursor->prev != NULL) {
294
  if (db->cursor->prev != NULL) {
-
 
295
    db->curline -= 1;
291
    db->cursor = db->cursor->prev;
296
    db->cursor = db->cursor->prev;
292
    if (db->cursorposy == 0) {
297
    if (db->cursorposy == 0) {
293
      uidirty.from = 0;
298
      uidirty.from = 0;
294
      uidirty.to = 0xff;
299
      uidirty.to = 0xff;
295
    } else {
300
    } else {
296
      db->cursorposy -= 1;
301
      db->cursorposy -= 1;
297
    }
302
    }
298
  }
303
  }
299
}
304
}
300
 
305
 
301
 
306
 
302
static void cursor_eol(struct file *db) {
307
static void cursor_eol(struct file *db) {
303
  /* adjust xoffset to make sure eol is visible on screen */
308
  /* adjust xoffset to make sure eol is visible on screen */
304
  if (db->xoffset > db->cursor->len) {
309
  if (db->xoffset > db->cursor->len) {
305
    db->xoffset = db->cursor->len - 1;
310
    db->xoffset = db->cursor->len - 1;
306
    uidirty.from = 0;
311
    uidirty.from = 0;
307
    uidirty.to = 0xff;
312
    uidirty.to = 0xff;
308
  }
313
  }
309
 
314
 
310
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
315
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
311
    db->xoffset = db->cursor->len - screenw + 2;
316
    db->xoffset = db->cursor->len - screenw + 2;
312
    uidirty.from = 0;
317
    uidirty.from = 0;
313
    uidirty.to = 0xff;
318
    uidirty.to = 0xff;
314
  }
319
  }
315
  db->cursorposx = db->cursor->len - db->xoffset;
320
  db->cursorposx = db->cursor->len - db->xoffset;
316
}
321
}
317
 
322
 
318
 
323
 
319
static void cursor_down(struct file *db) {
324
static void cursor_down(struct file *db) {
320
  if (db->cursor->next != NULL) {
325
  if (db->cursor->next != NULL) {
-
 
326
    db->curline += 1;
321
    db->cursor = db->cursor->next;
327
    db->cursor = db->cursor->next;
322
    if (db->cursorposy < screenh - 2) {
328
    if (db->cursorposy < screenh - 2) {
323
      db->cursorposy += 1;
329
      db->cursorposy += 1;
324
    } else {
330
    } else {
325
      uidirty.from = 0;
331
      uidirty.from = 0;
326
      uidirty.to = 0xff;
332
      uidirty.to = 0xff;
327
    }
333
    }
328
  }
334
  }
329
}
335
}
330
 
336
 
331
 
337
 
332
static void cursor_left(struct file *db) {
338
static void cursor_left(struct file *db) {
333
  if (db->cursorposx > 0) {
339
  if (db->cursorposx > 0) {
334
    db->cursorposx -= 1;
340
    db->cursorposx -= 1;
335
  } else if (db->xoffset > 0) {
341
  } else if (db->xoffset > 0) {
336
    db->xoffset -= 1;
342
    db->xoffset -= 1;
337
    uidirty.from = 0;
343
    uidirty.from = 0;
338
    uidirty.to = 0xff;
344
    uidirty.to = 0xff;
339
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
345
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
340
    cursor_up(db);
346
    cursor_up(db);
341
    cursor_eol(db);
347
    cursor_eol(db);
342
  }
348
  }
343
}
349
}
344
 
350
 
345
 
351
 
346
static void cursor_home(struct file *db) {
352
static void cursor_home(struct file *db) {
347
  db->cursorposx = 0;
353
  db->cursorposx = 0;
348
  if (db->xoffset != 0) {
354
  if (db->xoffset != 0) {
349
    db->xoffset = 0;
355
    db->xoffset = 0;
350
    uidirty.from = 0;
356
    uidirty.from = 0;
351
    uidirty.to = 0xff;
357
    uidirty.to = 0xff;
352
  }
358
  }
353
}
359
}
354
 
360
 
355
 
361
 
356
static void cursor_right(struct file *db) {
362
static void cursor_right(struct file *db) {
357
  if (db->cursor->len > db->xoffset + db->cursorposx) {
363
  if (db->cursor->len > db->xoffset + db->cursorposx) {
358
    if (db->cursorposx < screenw - 2) {
364
    if (db->cursorposx < screenw - 2) {
359
      db->cursorposx += 1;
365
      db->cursorposx += 1;
360
    } else {
366
    } else {
361
      db->xoffset += 1;
367
      db->xoffset += 1;
362
      uidirty.from = 0;
368
      uidirty.from = 0;
363
      uidirty.to = 0xff;
369
      uidirty.to = 0xff;
364
    }
370
    }
365
  } else {
371
  } else {
366
    cursor_down(db);
372
    cursor_down(db);
367
    cursor_home(db);
373
    cursor_home(db);
368
  }
374
  }
369
}
375
}
370
 
376
 
371
 
377
 
372
static void del(struct file *db) {
378
static void del(struct file *db) {
373
  if (db->cursorposx + db->xoffset < db->cursor->len) {
379
  if (db->cursorposx + db->xoffset < db->cursor->len) {
374
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
380
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
375
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
381
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
376
    uidirty.from = db->cursorposy;
382
    uidirty.from = db->cursorposy;
377
    uidirty.to = db->cursorposy;
383
    uidirty.to = db->cursorposy;
378
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
384
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
379
    struct line far *nextline = db->cursor->next;
385
    struct line far *nextline = db->cursor->next;
380
    if (db->cursor->next->len > 0) {
386
    if (db->cursor->next->len > 0) {
381
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
387
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
382
      if (newptr != NULL) {
388
      if (newptr != NULL) {
383
        db->cursor = newptr;
389
        db->cursor = newptr;
384
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
390
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
385
        db->cursor->len += db->cursor->next->len;
391
        db->cursor->len += db->cursor->next->len;
386
      }
392
      }
387
    }
393
    }
388
    db->cursor->next = db->cursor->next->next;
394
    db->cursor->next = db->cursor->next->next;
389
    db->cursor->next->prev = db->cursor;
395
    db->cursor->next->prev = db->cursor;
390
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
396
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
391
    _ffree(nextline);
397
    _ffree(nextline);
392
    uidirty.from = db->cursorposy;
398
    uidirty.from = db->cursorposy;
393
    uidirty.to = 0xff;
399
    uidirty.to = 0xff;
-
 
400
    db->totlines -= 1;
394
  }
401
  }
395
}
402
}
396
 
403
 
397
 
404
 
398
static void bkspc(struct file *db) {
405
static void bkspc(struct file *db) {
399
 
406
 
400
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
407
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
401
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
408
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
402
 
409
 
403
  cursor_left(db);
410
  cursor_left(db);
404
  del(db);
411
  del(db);
405
}
412
}
406
 
413
 
407
 
414
 
408
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
415
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
409
 * of argc and argv, saves some 330 bytes of binary size */
416
 * of argc and argv, saves some 330 bytes of binary size */
410
static char *parseargv(void) {
417
static char *parseargv(void) {
411
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
418
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
412
  unsigned char count = 0;
419
  unsigned char count = 0;
413
  char *argv[4];
420
  char *argv[4];
414
 
421
 
415
  while (count < 4) {
422
  while (count < 4) {
416
    /* jump to nearest arg */
423
    /* jump to nearest arg */
417
    while (*tail == ' ') {
424
    while (*tail == ' ') {
418
      *tail = 0;
425
      *tail = 0;
419
      tail++;
426
      tail++;
420
    }
427
    }
421
 
428
 
422
    if (*tail == '\r') {
429
    if (*tail == '\r') {
423
      *tail = 0;
430
      *tail = 0;
424
      break;
431
      break;
425
    }
432
    }
426
 
433
 
427
    argv[count++] = tail;
434
    argv[count++] = tail;
428
 
435
 
429
    /* jump to next delimiter */
436
    /* jump to next delimiter */
430
    while ((*tail != ' ') && (*tail != '\r')) tail++;
437
    while ((*tail != ' ') && (*tail != '\r')) tail++;
431
  }
438
  }
432
 
439
 
433
  /* check args now */
440
  /* check args now */
434
  if (count == 0) return("");
441
  if (count == 0) return("");
435
 
442
 
436
  return(argv[0]);
443
  return(argv[0]);
437
}
444
}
438
 
445
 
439
 
446
 
440
static struct file *loadfile(const char *fname) {
447
static struct file *loadfile(const char *fname) {
441
  char buff[512]; /* read one entire sector at a time (faster) */
448
  char buff[512]; /* read one entire sector at a time (faster) */
442
  char *buffptr;
449
  char *buffptr;
443
  unsigned int len, llen;
450
  unsigned int len, llen;
444
  int fd;
451
  int fd;
445
  unsigned char eolfound;
452
  unsigned char eolfound;
446
  struct file *db;
453
  struct file *db;
447
 
454
 
448
  len = strlen(fname) + 1;
455
  len = strlen(fname) + 1;
449
  db = calloc(1, sizeof(struct file) + len);
456
  db = calloc(1, sizeof(struct file) + len);
450
  if (db == NULL) return(NULL);
457
  if (db == NULL) return(NULL);
451
  memcpy(db->fname, fname, len);
458
  memcpy(db->fname, fname, len);
452
 
459
 
453
  if (*fname == 0) goto SKIPLOADING;
460
  if (*fname == 0) goto SKIPLOADING;
454
 
461
 
455
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
462
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
456
    mdr_coutraw_puts("Failed to open file:");
463
    mdr_coutraw_puts("Failed to open file:");
457
    mdr_coutraw_puts(fname);
464
    mdr_coutraw_puts(fname);
458
    free(db);
465
    free(db);
459
    return(NULL);
466
    return(NULL);
460
  }
467
  }
461
 
468
 
462
  db->lfonly = 1;
469
  db->lfonly = 1;
463
 
470
 
464
  /* start by adding an empty line */
471
  /* start by adding an empty line */
465
  if (line_add(db, "") != 0) {
472
  if (line_add(db, NULL, 0) != 0) {
466
    /* TODO ERROR HANDLING */
473
    /* TODO ERROR HANDLING */
467
  }
474
  }
468
 
475
 
469
  for (eolfound = 0;;) {
476
  for (eolfound = 0;;) {
470
    unsigned short consumedbytes;
477
    unsigned short consumedbytes;
471
 
478
 
472
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
479
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
473
    buffptr = buff;
480
    buffptr = buff;
474
 
481
 
475
    FINDLINE:
482
    FINDLINE:
476
 
483
 
477
    /* look for nearest \n */
484
    /* look for nearest \n */
478
    for (consumedbytes = 0;; consumedbytes++) {
485
    for (consumedbytes = 0;; consumedbytes++) {
479
      if (consumedbytes == len) {
486
      if (consumedbytes == len) {
480
        llen = consumedbytes;
487
        llen = consumedbytes;
481
        break;
488
        break;
482
      }
489
      }
483
      if (buffptr[consumedbytes] == '\r') {
490
      if (buffptr[consumedbytes] == '\r') {
484
        llen = consumedbytes;
491
        llen = consumedbytes;
485
        consumedbytes++;
492
        consumedbytes++;
486
        db->lfonly = 0;
493
        db->lfonly = 0;
487
        break;
494
        break;
488
      }
495
      }
489
      if (buffptr[consumedbytes] == '\n') {
496
      if (buffptr[consumedbytes] == '\n') {
490
        eolfound = 1;
497
        eolfound = 1;
491
        llen = consumedbytes;
498
        llen = consumedbytes;
492
        consumedbytes++;
499
        consumedbytes++;
493
        break;
500
        break;
494
      }
501
      }
495
    }
502
    }
496
 
503
 
497
    /* consumedbytes is the amount of bytes processed from buffptr,
504
    /* consumedbytes is the amount of bytes processed from buffptr,
498
     * llen is the length of line's payload (without its line terminator) */
505
     * llen is the length of line's payload (without its line terminator) */
499
 
506
 
500
    /* append content, if line is non-empty */
507
    /* append content, if line is non-empty */
501
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
508
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
502
      mdr_coutraw_puts("out of memory");
509
      mdr_coutraw_puts("out of memory");
503
      free(db);
510
      free(db);
504
      db = NULL;
511
      db = NULL;
505
      break;
512
      break;
506
    }
513
    }
507
 
514
 
508
    /* add a new line if necessary */
515
    /* add a new line if necessary */
509
    if (eolfound) {
516
    if (eolfound) {
510
      if (line_add(db, "") != 0) {
517
      if (line_add(db, NULL, 0) != 0) {
511
      /* TODO ERROR HANDLING */
518
      /* TODO ERROR HANDLING */
512
        mdr_coutraw_puts("out of memory");
519
        mdr_coutraw_puts("out of memory");
513
        free(db);
520
        free(db);
514
        db = NULL;
521
        db = NULL;
515
        break;
522
        break;
516
      }
523
      }
517
      eolfound = 0;
524
      eolfound = 0;
518
    }
525
    }
519
 
526
 
520
    /* anything left? process the buffer leftover again */
527
    /* anything left? process the buffer leftover again */
521
    if (consumedbytes < len) {
528
    if (consumedbytes < len) {
522
      len -= consumedbytes;
529
      len -= consumedbytes;
523
      buffptr += consumedbytes;
530
      buffptr += consumedbytes;
524
      goto FINDLINE;
531
      goto FINDLINE;
525
    }
532
    }
526
 
533
 
527
  }
534
  }
528
 
535
 
529
  _dos_close(fd);
536
  _dos_close(fd);
530
 
537
 
531
  SKIPLOADING:
538
  SKIPLOADING:
532
 
539
 
533
  /* add an empty line at end if not present already, also rewind cursor to top of file */
540
  /* add an empty line at end if not present already, also rewind cursor to top of file */
534
  if (db != NULL) {
541
  if (db != NULL) {
535
    if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, "");
542
    if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
536
    db_rewind(db);
543
    db_rewind(db);
537
  }
544
  }
538
 
545
 
539
  return(db);
546
  return(db);
540
}
547
}
541
 
548
 
542
 
549
 
543
static int savefile(const struct file *db) {
550
static int savefile(const struct file *db) {
544
  int fd;
551
  int fd;
545
  const struct line far *l;
552
  const struct line far *l;
546
  unsigned bytes;
553
  unsigned bytes;
547
  unsigned char eollen;
554
  unsigned char eollen;
548
  unsigned char eolbuf[2];
555
  unsigned char eolbuf[2];
549
 
556
 
550
  if (_dos_open(db->fname, O_WRONLY, &fd) != 0) {
557
  if (_dos_open(db->fname, O_WRONLY, &fd) != 0) {
551
    return(-1);
558
    return(-1);
552
  }
559
  }
553
 
560
 
554
  l = db->cursor;
561
  l = db->cursor;
555
  while (l->prev) l = l->prev;
562
  while (l->prev) l = l->prev;
556
 
563
 
557
  /* preset line terminators */
564
  /* preset line terminators */
558
  if (db->lfonly) {
565
  if (db->lfonly) {
559
    eolbuf[0] = '\n';
566
    eolbuf[0] = '\n';
560
    eollen = 1;
567
    eollen = 1;
561
  } else {
568
  } else {
562
    eolbuf[0] = '\r';
569
    eolbuf[0] = '\r';
563
    eolbuf[1] = '\n';
570
    eolbuf[1] = '\n';
564
    eollen = 2;
571
    eollen = 2;
565
  }
572
  }
566
 
573
 
567
  while (l) {
574
  while (l) {
568
    /* do not write the last empty line, it is only useful for edition */
575
    /* do not write the last empty line, it is only useful for edition */
569
    if (l->len != 0) {
576
    if (l->len != 0) {
570
      _dos_write(fd, l->payload, l->len, &bytes);
577
      _dos_write(fd, l->payload, l->len, &bytes);
571
    } else if (l->next == NULL) {
578
    } else if (l->next == NULL) {
572
      break;
579
      break;
573
    }
580
    }
574
    _dos_write(fd, eolbuf, eollen, &bytes);
581
    _dos_write(fd, eolbuf, eollen, &bytes);
575
    l = l->next;
582
    l = l->next;
576
  }
583
  }
577
 
584
 
578
  _dos_close(fd);
585
  _dos_close(fd);
579
  return(0);
586
  return(0);
580
}
587
}
581
 
588
 
582
 
589
 
583
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
590
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
584
  struct line far *n;
591
  struct line far *n;
585
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
592
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
586
  if (n != NULL) {
593
  if (n != NULL) {
587
    unsigned short off = db->xoffset + db->cursorposx;
594
    unsigned short off = db->xoffset + db->cursorposx;
588
    if (n->prev) n->prev->next = n;
595
    if (n->prev) n->prev->next = n;
589
    if (n->next) n->next->prev = n;
596
    if (n->next) n->next->prev = n;
590
    db->cursor = n;
597
    db->cursor = n;
591
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
598
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
592
    db->cursor->len += len;
599
    db->cursor->len += len;
593
    uidirty.from = db->cursorposy;
600
    uidirty.from = db->cursorposy;
594
    uidirty.to = db->cursorposy;
601
    uidirty.to = db->cursorposy;
595
    while (len--) {
602
    while (len--) {
596
      db->cursor->payload[off++] = *databuf;
603
      db->cursor->payload[off++] = *databuf;
597
      databuf++;
604
      databuf++;
598
      cursor_right(db);
605
      cursor_right(db);
599
    }
606
    }
600
  }
607
  }
601
}
608
}
602
 
609
 
603
 
610
 
604
int main(void) {
611
int main(void) {
605
  const char *fname;
612
  const char *fname;
606
  struct file *db;
613
  struct file *db;
607
 
614
 
608
  {
615
  {
609
    char nlspath[128], lang[8];
616
    char nlspath[128], lang[8];
610
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
617
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
611
  }
618
  }
612
 
619
 
613
  fname = parseargv();
620
  fname = parseargv();
614
 
621
 
615
  if (fname == NULL) {
622
  if (fname == NULL) {
616
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
623
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
617
    return(0);
624
    return(0);
618
  }
625
  }
619
 
626
 
620
  /* load file */
627
  /* load file */
621
  db = loadfile(fname);
628
  db = loadfile(fname);
622
  if (db == NULL) return(1);
629
  if (db == NULL) return(1);
623
 
630
 
624
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
631
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
625
  ui_basic(db);
632
  ui_basic(db);
626
 
633
 
627
  for (;;) {
634
  for (;;) {
628
    int k;
635
    int k;
629
 
636
 
630
    check_cursor_not_after_eol(db);
637
    check_cursor_not_after_eol(db);
631
    mdr_cout_locate(db->cursorposy, db->cursorposx);
638
    mdr_cout_locate(db->cursorposy, db->cursorposx);
632
 
639
 
633
    if (uidirty.from != 0xff) {
640
    if (uidirty.from != 0xff) {
634
      ui_refresh(db);
641
      ui_refresh(db);
635
      uidirty.from = 0xff;
642
      uidirty.from = 0xff;
636
    }
643
    }
-
 
644
#ifdef DBG_LINENUM
-
 
645
      {
-
 
646
        char ddd[10];
-
 
647
        db->curline += 1;
-
 
648
        ddd[0] = '0' + db->curline / 100;
-
 
649
        ddd[1] = '0' + (db->curline % 100) / 10;
-
 
650
        ddd[2] = '0' + (db->curline % 10);
-
 
651
        db->curline -= 1;
-
 
652
        ddd[3] = '/';
-
 
653
        ddd[4] = '0' + db->totlines / 100;
-
 
654
        ddd[5] = '0' + (db->totlines % 100) / 10;
-
 
655
        ddd[6] = '0' + (db->totlines % 10);
-
 
656
        ddd[7] = 0;
-
 
657
        mdr_cout_str(screenh - 1, 40, ddd, scheme[COL_STATUSBAR1], sizeof(ddd));
-
 
658
      }
-
 
659
#endif
637
 
660
 
638
    k = keyb_getkey();
661
    k = keyb_getkey();
639
 
662
 
640
    if (k == 0x150) { /* down */
663
    if (k == 0x150) { /* down */
641
      cursor_down(db);
664
      cursor_down(db);
642
 
665
 
643
    } else if (k == 0x148) { /* up */
666
    } else if (k == 0x148) { /* up */
644
      cursor_up(db);
667
      cursor_up(db);
645
 
668
 
646
    } else if (k == 0x14D) { /* right */
669
    } else if (k == 0x14D) { /* right */
647
      cursor_right(db);
670
      cursor_right(db);
648
 
671
 
649
    } else if (k == 0x14B) { /* left */
672
    } else if (k == 0x14B) { /* left */
650
      cursor_left(db);
673
      cursor_left(db);
651
 
674
 
652
    } else if (k == 0x149) { /* pgup */
675
    } else if (k == 0x149) { /* pgup */
653
      // TODO
676
      // TODO
654
 
677
 
655
    } else if (k == 0x151) { /* pgdown */
678
    } else if (k == 0x151) { /* pgdown */
656
      // TODO
679
      // TODO
657
 
680
 
658
    } else if (k == 0x147) { /* home */
681
    } else if (k == 0x147) { /* home */
659
       cursor_home(db);
682
       cursor_home(db);
660
 
683
 
661
    } else if (k == 0x14F) { /* end */
684
    } else if (k == 0x14F) { /* end */
662
       cursor_eol(db);
685
       cursor_eol(db);
663
 
686
 
664
    } else if (k == 0x1B) { /* ESC */
687
    } else if (k == 0x1B) { /* ESC */
665
      break;
688
      break;
666
 
689
 
667
    } else if (k == 0x0D) { /* ENTER */
690
    } else if (k == 0x0D) { /* ENTER */
-
 
691
      unsigned short off = db->xoffset + db->cursorposx;
668
      /* add a new line */
692
      /* add a new line */
669
      if (line_add(db, db->cursor->payload + db->xoffset + db->cursorposx) == 0) {
693
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
-
 
694
        db->cursor = db->cursor->prev; /* back to original line */
-
 
695
        db->curline -= 1;
670
        /* trim the line above */
696
        /* trim the line above */
671
        db->cursor->prev->len = db->xoffset + db->cursorposx;
697
        db->cursor->len = off;
672
        db->cursor->prev->payload[db->cursor->prev->len] = 0;
-
 
673
        /* move cursor to the (new) line below */
698
        /* move cursor to the (new) line below */
674
        db->cursorposx = 0;
-
 
675
        if (db->cursorposy < screenh - 2) {
-
 
676
          uidirty.from = db->cursorposy;
699
        uidirty.from = db->cursorposy;
677
          db->cursorposy++;
-
 
678
        } else {
-
 
679
          uidirty.from = 0;
-
 
680
        }
-
 
681
        uidirty.to = 0xff;
700
        uidirty.to = 0xff;
-
 
701
        cursor_down(db);
-
 
702
        cursor_home(db);
682
      } else {
703
      } else {
683
        /* ERROR: OUT OF MEMORY */
704
        /* ERROR: OUT OF MEMORY */
684
      }
705
      }
685
 
706
 
686
    } else if (k == 0x153) {  /* DEL */
707
    } else if (k == 0x153) {  /* DEL */
687
      del(db);
708
      del(db);
688
 
709
 
689
    } else if (k == 0x008) { /* BKSPC */
710
    } else if (k == 0x008) { /* BKSPC */
690
      bkspc(db);
711
      bkspc(db);
691
 
712
 
692
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
713
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
693
      char c = k;
714
      char c = k;
694
      insert_in_line(db, &c, 1);
715
      insert_in_line(db, &c, 1);
695
 
716
 
696
    } else if (k == 0x009) { /* TAB */
717
    } else if (k == 0x009) { /* TAB */
697
      const char *tab = "        ";
718
      const char *tab = "        ";
698
      insert_in_line(db, tab, 8);
719
      insert_in_line(db, tab, 8);
699
 
720
 
700
    } else if (k == 0x13b) { /* F1 */
721
    } else if (k == 0x13b) { /* F1 */
701
      ui_help();
722
      ui_help();
702
      uidirty.from = 0;
723
      uidirty.from = 0;
703
      uidirty.to = 0xff;
724
      uidirty.to = 0xff;
704
 
725
 
705
    } else if (k == 0x13f) { /* F5 */
726
    } else if (k == 0x13f) { /* F5 */
706
      if (savefile(db) == 0) {
727
      if (savefile(db) == 0) {
707
        ui_msg(svarlang_str(0, 2), scheme[COL_MSG]);
728
        ui_msg(svarlang_str(0, 2), scheme[COL_MSG]);
708
        mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
729
        mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
709
      } else {
730
      } else {
710
        ui_msg(svarlang_str(0, 3), scheme[COL_ERR]);
731
        ui_msg(svarlang_str(0, 3), scheme[COL_ERR]);
711
        mdr_bios_tickswait(36); /* 2s */
732
        mdr_bios_tickswait(36); /* 2s */
712
      }
733
      }
713
 
734
 
714
    } else if (k == 0x144) { /* F10 */
735
    } else if (k == 0x144) { /* F10 */
715
      db->lfonly ^= 1;
736
      db->lfonly ^= 1;
716
      ui_basic(db);
737
      ui_basic(db);
717
 
738
 
718
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
739
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
719
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
740
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
720
      for (;;) {
741
      for (;;) {
721
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
742
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
722
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
743
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
723
        cursor_right(db);
744
        cursor_right(db);
724
      }
745
      }
725
      /* now skip to next non-space or end of file */
746
      /* now skip to next non-space or end of file */
726
      for (;;) {
747
      for (;;) {
727
        cursor_right(db);
748
        cursor_right(db);
728
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
749
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
729
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
750
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
730
      }
751
      }
731
 
752
 
732
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
753
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
733
      cursor_left(db);
754
      cursor_left(db);
734
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
755
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
735
      for (;;) {
756
      for (;;) {
736
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
757
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
737
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
758
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
738
        cursor_left(db);
759
        cursor_left(db);
739
      }
760
      }
740
      /* now skip to next space or start of file */
761
      /* now skip to next space or start of file */
741
      for (;;) {
762
      for (;;) {
742
        cursor_left(db);
763
        cursor_left(db);
743
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
764
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
744
          cursor_right(db);
765
          cursor_right(db);
745
          break;
766
          break;
746
        }
767
        }
747
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
768
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
748
      }
769
      }
749
 
770
 
750
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
771
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
751
      char buff[4];
772
      char buff[4];
752
      const char *HEX = "0123456789ABCDEF";
773
      const char *HEX = "0123456789ABCDEF";
753
      buff[0] = HEX[(k >> 8) & 15];
774
      buff[0] = HEX[(k >> 8) & 15];
754
      buff[1] = HEX[(k >> 4) & 15];
775
      buff[1] = HEX[(k >> 4) & 15];
755
      buff[2] = HEX[k & 15];
776
      buff[2] = HEX[k & 15];
756
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
777
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
757
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
778
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
758
      keyb_getkey();
779
      keyb_getkey();
759
      break;
780
      break;
760
    }
781
    }
761
  }
782
  }
762
 
783
 
763
  mdr_cout_close();
784
  mdr_cout_close();
764
 
785
 
765
  /* no need to free memory, DOS will do it for me */
786
  /* no need to free memory, DOS will do it for me */
766
 
787
 
767
  return(0);
788
  return(0);
768
}
789
}
769
 
790