1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
/*
* Copyright (C) 2012-2013 Robin Haberkorn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <locale.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <glib/gstdio.h>
#include <curses.h>
#include <Scintilla.h>
#include <ScintillaTerm.h>
#include "sciteco.h"
#include "cmdline.h"
#include "qregisters.h"
#include "ring.h"
#include "interface.h"
#include "interface-ncurses.h"
extern "C" {
static void scintilla_notify(Scintilla *sci, int idFrom,
void *notify, void *user_data);
}
#define UNNAMED_FILE "(Unnamed)"
#define SCI_COLOR_ATTR(f, b) \
COLOR_PAIR(SCI_COLOR_PAIR(f, b))
void
InterfaceNCurses::main(int &argc __attribute__((unused)),
char **&argv __attribute__((unused)))
{
init_screen();
cbreak();
noecho();
curs_set(0); /* Scintilla draws its own cursor */
setlocale(LC_CTYPE, ""); /* for displaying UTF-8 characters properly */
info_window = newwin(1, 0, 0, 0);
info_current = g_strdup(PACKAGE_NAME);
/* NOTE: Scintilla initializes color pairs */
sci = scintilla_new(scintilla_notify);
sci_window = scintilla_get_window(sci);
wresize(sci_window, LINES - 3, COLS);
mvwin(sci_window, 1, 0);
msg_window = newwin(1, 0, LINES - 2, 0);
cmdline_window = newwin(0, 0, LINES - 1, 0);
cmdline_current = NULL;
ssm(SCI_SETFOCUS, TRUE);
draw_info();
/* scintilla will be refreshed in event loop */
msg_clear();
cmdline_update("");
#ifndef PDCURSES_WIN32A
/* workaround: endwin() is somewhat broken in the win32a port */
endwin();
#endif
}
#ifdef __PDCURSES__
void
InterfaceNCurses::init_screen(void)
{
#ifdef PDCURSES_WIN32A
/* enables window resizing on Win32a port */
PDC_set_resize_limits(25, 0xFFFF, 80, 0xFFFF);
#endif
initscr();
screen_tty = NULL;
screen = NULL;
}
#else
void
InterfaceNCurses::init_screen(void)
{
/*
* Prevent the initial redraw and any escape sequences that may
* interfere with stdout, so we may use the terminal in
* cooked mode, for commandline help and batch processing.
* Scintilla must be initialized for batch processing to work.
* (Frankly I have no idea why this works!)
*/
screen_tty = g_fopen("/dev/tty", "r+b");
screen = newterm(NULL, screen_tty, screen_tty);
set_term(screen);
}
#endif /* !__PDCURSES__ */
void
InterfaceNCurses::resize_all_windows(void)
{
int lines, cols; /* screen dimensions */
getmaxyx(stdscr, lines, cols);
wresize(info_window, 1, cols);
wresize(sci_window, lines - 3, cols);
wresize(msg_window, 1, cols);
mvwin(msg_window, lines - 2, 0);
wresize(cmdline_window, 1, cols);
mvwin(cmdline_window, lines - 1, 0);
draw_info();
/* scintilla will be refreshed in event loop */
msg_clear(); /* FIXME: use saved message */
cmdline_update();
}
void
InterfaceNCurses::vmsg(MessageType type, const gchar *fmt, va_list ap)
{
static const chtype type2attr[] = {
SCI_COLOR_ATTR(COLOR_BLACK, COLOR_WHITE), /* MSG_USER */
SCI_COLOR_ATTR(COLOR_BLACK, COLOR_GREEN), /* MSG_INFO */
SCI_COLOR_ATTR(COLOR_BLACK, COLOR_YELLOW), /* MSG_WARNING */
SCI_COLOR_ATTR(COLOR_BLACK, COLOR_RED) /* MSG_ERROR */
};
#ifdef PDCURSES_WIN32A
stdio_vmsg(type, fmt, ap);
if (isendwin()) /* batch mode */
return;
#else
if (isendwin()) { /* batch mode */
stdio_vmsg(type, fmt, ap);
return;
}
#endif
wmove(msg_window, 0, 0);
wbkgdset(msg_window, ' ' | type2attr[type]);
vw_printw(msg_window, fmt, ap);
wclrtoeol(msg_window);
wrefresh(msg_window);
}
void
InterfaceNCurses::msg_clear(void)
{
if (isendwin()) /* batch mode */
return;
wmove(msg_window, 0, 0);
wbkgdset(msg_window, ' ' | SCI_COLOR_ATTR(COLOR_BLACK, COLOR_WHITE));
wclrtoeol(msg_window);
wrefresh(msg_window);
}
void
InterfaceNCurses::draw_info(void)
{
if (isendwin()) /* batch mode */
return;
wmove(info_window, 0, 0);
wbkgdset(info_window, ' ' | SCI_COLOR_ATTR(COLOR_BLACK, COLOR_WHITE));
waddstr(info_window, info_current);
wclrtoeol(info_window);
wrefresh(info_window);
}
void
InterfaceNCurses::info_update(QRegister *reg)
{
g_free(info_current);
info_current = g_strdup_printf("%s - <QRegister> %s", PACKAGE_NAME,
reg->name);
draw_info();
}
void
InterfaceNCurses::info_update(Buffer *buffer)
{
g_free(info_current);
info_current = g_strdup_printf("%s - <Buffer> %s%s", PACKAGE_NAME,
buffer->filename ? : UNNAMED_FILE,
buffer->dirty ? "*" : "");
draw_info();
}
void
InterfaceNCurses::cmdline_update(const gchar *cmdline)
{
size_t len;
int half_line = (getmaxx(stdscr) - 2) / 2;
const gchar *line;
if (cmdline) {
g_free(cmdline_current);
cmdline_current = g_strdup(cmdline);
} else {
cmdline = cmdline_current;
}
len = strlen(cmdline);
/* FIXME: optimize */
line = cmdline + len - MIN(len, half_line + len % half_line);
mvwaddch(cmdline_window, 0, 0, '*');
waddstr(cmdline_window, line);
waddch(cmdline_window, ' ' | A_REVERSE);
wclrtoeol(cmdline_window);
wrefresh(cmdline_window);
}
void
InterfaceNCurses::popup_add(PopupEntryType type __attribute__((unused)),
const gchar *name, bool highlight)
{
gchar *entry;
if (isendwin()) /* batch mode */
return;
entry = g_strconcat(highlight ? "*" : " ", name, NIL);
popup.longest = MAX(popup.longest, (gint)strlen(name));
popup.length++;
popup.list = g_slist_prepend(popup.list, entry);
}
void
InterfaceNCurses::popup_show(void)
{
int lines, cols; /* screen dimensions */
int popup_lines;
gint popup_cols;
gint cur_file, cur_line;
if (isendwin()) /* batch mode */
goto cleanup;
getmaxyx(stdscr, lines, cols);
popup.longest += 3;
popup.list = g_slist_reverse(popup.list);
/* popup_cols = floor(cols / popup.longest) */
popup_cols = MAX(cols / popup.longest, 1);
/* popup_lines = ceil(popup.length / popup_cols) */
popup_lines = popup.length / popup_cols;
if ((popup.length % popup_cols))
popup_lines++;
popup_lines = MIN(popup_lines, lines - 1);
/* window covers message, scintilla and info windows */
popup.window = newwin(popup_lines, 0, lines - 1 - popup_lines, 0);
wbkgdset(popup.window, ' ' | SCI_COLOR_ATTR(COLOR_BLACK, COLOR_BLUE));
cur_file = 0;
cur_line = 1;
for (GSList *cur = popup.list; cur; cur = g_slist_next(cur)) {
gchar *entry = (gchar *)cur->data;
if (cur_file && !(cur_file % popup_cols)) {
wclrtoeol(popup.window);
waddch(popup.window, '\n');
cur_line++;
}
cur_file++;
if (cur_line == popup_lines && !(cur_file % popup_cols) &&
cur_file < popup.length) {
(void)wattrset(popup.window, A_BOLD);
waddstr(popup.window, "...");
break;
}
(void)wattrset(popup.window, *entry == '*' ? A_BOLD : A_NORMAL);
waddstr(popup.window, entry + 1);
for (int i = popup.longest - strlen(entry) + 1; i; i--)
waddch(popup.window, ' ');
g_free(cur->data);
}
wclrtoeol(popup.window);
cleanup:
g_slist_free(popup.list);
popup.list = NULL;
popup.longest = popup.length = 0;
}
void
InterfaceNCurses::popup_clear(void)
{
if (!popup.window)
return;
redrawwin(info_window);
wrefresh(info_window);
redrawwin(sci_window);
scintilla_refresh(sci);
redrawwin(msg_window);
wrefresh(msg_window);
delwin(popup.window);
popup.window = NULL;
}
void
InterfaceNCurses::event_loop(void)
{
/* in commandline (visual) mode, enforce redraw */
wrefresh(curscr);
draw_info();
for (;;) {
int key;
/* also handles initial refresh (styles are configured...) */
scintilla_refresh(sci);
if (popup.window)
wrefresh(popup.window);
keypad(cmdline_window, Flags::ed & Flags::ED_FNKEYS);
/* no special <CTRL/C> handling */
raw();
key = wgetch(cmdline_window);
/* allow asynchronous interruptions on <CTRL/C> */
cbreak();
switch (key) {
#ifdef KEY_RESIZE
case ERR:
case KEY_RESIZE:
#ifdef PDCURSES
resize_term(0, 0);
#endif
resize_all_windows();
break;
#endif
case 0x7F: /* DEL */
case KEY_BACKSPACE:
cmdline_keypress('\b');
break;
case KEY_ENTER:
case '\r':
case '\n':
cmdline_keypress(get_eol());
break;
/*
* Function key macros
*/
#define FN(KEY) case KEY_##KEY: cmdline_fnmacro(#KEY); break
#define FNS(KEY) FN(KEY); FN(S##KEY)
FN(DOWN); FN(UP); FNS(LEFT); FNS(RIGHT);
FNS(HOME);
case KEY_F(0)...KEY_F(63): {
gchar macro_name[3+1];
g_snprintf(macro_name, sizeof(macro_name),
"F%d", key - KEY_F0);
cmdline_fnmacro(macro_name);
break;
}
FNS(DC);
FNS(IC);
FN(NPAGE); FN(PPAGE);
FNS(PRINT);
FN(A1); FN(A3); FN(B2); FN(C1); FN(C3);
FNS(END);
FNS(HELP);
#undef FNS
#undef FN
/*
* Control keys and keys with printable representation
*/
default:
if (key <= 0xFF)
cmdline_keypress((gchar)key);
}
sigint_occurred = FALSE;
}
}
InterfaceNCurses::Popup::~Popup()
{
if (window)
delwin(window);
if (list)
g_slist_free(list);
}
InterfaceNCurses::~InterfaceNCurses()
{
if (info_window)
delwin(info_window);
g_free(info_current);
/* also deletes curses window */
if (sci)
scintilla_delete(sci);
if (cmdline_window)
delwin(cmdline_window);
g_free(cmdline_current);
if (msg_window)
delwin(msg_window);
if (!isendwin())
endwin();
if (screen)
delscreen(screen);
if (screen_tty)
fclose(screen_tty);
}
/*
* Callbacks
*/
static void
scintilla_notify(Scintilla *sci, int idFrom, void *notify, void *user_data)
{
interface.process_notify((SCNotification *)notify);
}
|