3 Minimum Profit - Programmer Text Editor
7 Copyright (C) 1991-2007 Angel Ortega <angel@triptico.com>
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 http://www.triptico.com
40 #include <sys/ioctl.h>
49 /* the curses attributes */
50 int * nc_attrs
= NULL
;
52 /* code for the 'normal' attribute */
53 static int normal_attr
= 0;
56 static WINDOW
* cw
= NULL
;
58 /* stack of windows */
59 static int n_stack
= 0;
60 static WINDOW
** w_stack
= NULL
;
63 static int last_attr
= 0;
66 static int timer_msecs
= 0;
67 static mpdm_t timer_func
= NULL
;
71 static void set_attr(void)
72 /* set the current and fill attributes */
74 wattrset(cw
, nc_attrs
[last_attr
]);
75 wbkgdset(cw
, ' ' | nc_attrs
[last_attr
]);
79 static void nc_sigwinch(int s
)
80 /* SIGWINCH signal handler */
84 #ifdef NCURSES_VERSION
85 /* Make sure that window size changes... */
88 int fd
= open("/dev/tty", O_RDWR
);
91 return; /* This should never have to happen! */
93 if (ioctl(fd
, TIOCGWINSZ
, &ws
) == 0)
94 resizeterm(ws
.ws_row
, ws
.ws_col
);
102 /* invalidate main window */
106 /* re-set dimensions */
107 v
= mpdm_hget_s(mp
, L
"window");
108 mpdm_hset_s(v
, L
"tx", MPDM_I(COLS
));
109 mpdm_hset_s(v
, L
"ty", MPDM_I(LINES
));
112 signal(SIGWINCH
, nc_sigwinch
);
116 #ifdef CONFOPT_WGET_WCH
117 int wget_wch(WINDOW
* w
, wint_t *ch
);
120 static wchar_t * nc_getwch(void)
121 /* gets a key as a wchar_t */
125 #ifdef CONFOPT_WGET_WCH
127 /* set timer period */
129 timeout(timer_msecs
);
131 if (wget_wch(stdscr
, (wint_t *)c
) == -1)
135 char tmp
[MB_CUR_MAX
+ 1];
145 /* set to non-blocking */
148 /* read all possible following characters */
150 while (n
< sizeof(tmp
) - 1 && (cc
= getch()) != ERR
)
153 /* sets input as blocking */
165 #define ctrl(k) ((k) & 31)
167 static mpdm_t
nc_getkey(mpdm_t args
)
168 /* reads a key and converts to an action */
170 static int shift
= 0;
174 /* any pending key? return it */
175 if ((k
= mp_pending_key()) != NULL
)
181 mpdm_exec(timer_func
, NULL
);
187 case L
'0': f
= L
"f10"; break;
188 case L
'1': f
= L
"f1"; break;
189 case L
'2': f
= L
"f2"; break;
190 case L
'3': f
= L
"f3"; break;
191 case L
'4': f
= L
"f4"; break;
192 case L
'5': f
= L
"f5"; break;
193 case L
'6': f
= L
"f6"; break;
194 case L
'7': f
= L
"f7"; break;
195 case L
'8': f
= L
"f8"; break;
196 case L
'9': f
= L
"f9"; break;
197 case KEY_LEFT
: f
= L
"ctrl-cursor-left"; break;
198 case KEY_RIGHT
: f
= L
"ctrl-cursor-right"; break;
199 case KEY_DOWN
: f
= L
"ctrl-cursor-down"; break;
200 case KEY_UP
: f
= L
"ctrl-cursor-up"; break;
201 case KEY_END
: f
= L
"ctrl-end"; break;
202 case KEY_HOME
: f
= L
"ctrl-home"; break;
203 case L
'\r': f
= L
"ctrl-enter"; break;
204 case L
'\e': f
= L
"escape"; break;
205 case KEY_ENTER
: f
= L
"ctrl-enter"; break;
206 case L
' ': f
= L
"ctrl-space"; break;
207 case L
'a': f
= L
"ctrl-a"; break;
208 case L
'b': f
= L
"ctrl-b"; break;
209 case L
'c': f
= L
"ctrl-c"; break;
210 case L
'd': f
= L
"ctrl-d"; break;
211 case L
'e': f
= L
"ctrl-e"; break;
212 case L
'f': f
= L
"ctrl-f"; break;
213 case L
'g': f
= L
"ctrl-g"; break;
214 case L
'h': f
= L
"ctrl-h"; break;
215 case L
'i': f
= L
"ctrl-i"; break;
216 case L
'j': f
= L
"ctrl-j"; break;
217 case L
'k': f
= L
"ctrl-k"; break;
218 case L
'l': f
= L
"ctrl-l"; break;
219 case L
'm': f
= L
"ctrl-m"; break;
220 case L
'n': f
= L
"ctrl-n"; break;
221 case L
'o': f
= L
"ctrl-o"; break;
222 case L
'p': f
= L
"ctrl-p"; break;
223 case L
'q': f
= L
"ctrl-q"; break;
224 case L
'r': f
= L
"ctrl-r"; break;
225 case L
's': f
= L
"ctrl-s"; break;
226 case L
't': f
= L
"ctrl-t"; break;
227 case L
'u': f
= L
"ctrl-u"; break;
228 case L
'v': f
= L
"ctrl-v"; break;
229 case L
'w': f
= L
"ctrl-w"; break;
230 case L
'x': f
= L
"ctrl-x"; break;
231 case L
'y': f
= L
"ctrl-y"; break;
232 case L
'z': f
= L
"ctrl-z"; break;
239 case KEY_LEFT
: f
= L
"cursor-left"; break;
240 case KEY_RIGHT
: f
= L
"cursor-right"; break;
241 case KEY_UP
: f
= L
"cursor-up"; break;
242 case KEY_DOWN
: f
= L
"cursor-down"; break;
243 case KEY_PPAGE
: f
= L
"page-up"; break;
244 case KEY_NPAGE
: f
= L
"page-down"; break;
245 case KEY_HOME
: f
= L
"home"; break;
246 case KEY_END
: f
= L
"end"; break;
247 case KEY_LL
: f
= L
"end"; break;
248 case KEY_IC
: f
= L
"insert"; break;
249 case KEY_DC
: f
= L
"delete"; break;
252 case L
'\b': f
= L
"backspace"; break;
254 case KEY_ENTER
: f
= L
"enter"; break;
255 case L
'\t': f
= L
"tab"; break;
256 case KEY_BTAB
: f
= L
"shift-tab"; break;
257 case L
' ': f
= L
"space"; break;
258 case KEY_F(1): f
= L
"f1"; break;
259 case KEY_F(2): f
= L
"f2"; break;
260 case KEY_F(3): f
= L
"f3"; break;
261 case KEY_F(4): f
= L
"f4"; break;
262 case KEY_F(5): f
= L
"f5"; break;
263 case KEY_F(6): f
= L
"f6"; break;
264 case KEY_F(7): f
= L
"f7"; break;
265 case KEY_F(8): f
= L
"f8"; break;
266 case KEY_F(9): f
= L
"f9"; break;
267 case KEY_F(10): f
= L
"f10"; break;
268 case ctrl(' '): f
= L
"ctrl-space"; break;
269 case ctrl('a'): f
= L
"ctrl-a"; break;
270 case ctrl('b'): f
= L
"ctrl-b"; break;
271 case ctrl('c'): f
= L
"ctrl-c"; break;
272 case ctrl('d'): f
= L
"ctrl-d"; break;
273 case ctrl('e'): f
= L
"ctrl-e"; break;
274 case ctrl('f'): f
= L
"ctrl-f"; break;
275 case ctrl('g'): f
= L
"ctrl-g"; break;
276 case ctrl('j'): f
= L
"ctrl-j"; break;
277 case ctrl('k'): f
= L
"ctrl-k"; break;
278 case ctrl('l'): f
= L
"ctrl-l"; break;
279 case ctrl('n'): f
= L
"ctrl-n"; break;
280 case ctrl('o'): f
= L
"ctrl-o"; break;
281 case ctrl('p'): f
= L
"ctrl-p"; break;
282 case ctrl('q'): f
= L
"ctrl-q"; break;
283 case ctrl('r'): f
= L
"ctrl-r"; break;
284 case ctrl('s'): f
= L
"ctrl-s"; break;
285 case ctrl('t'): f
= L
"ctrl-t"; break;
286 case ctrl('u'): f
= L
"ctrl-u"; break;
287 case ctrl('v'): f
= L
"ctrl-v"; break;
288 case ctrl('w'): f
= L
"ctrl-w"; break;
289 case ctrl('x'): f
= L
"ctrl-x"; break;
290 case ctrl('y'): f
= L
"ctrl-y"; break;
291 case ctrl('z'): f
= L
"ctrl-z"; break;
292 case L
'\e': shift
= 1; f
= NULL
; break;
296 /* no known key? do nothing */
300 return mp_process_keyseq(MPDM_S(f
));
304 static mpdm_t
nc_addwstr(mpdm_t str
)
307 wchar_t * wptr
= mpdm_string(str
);
309 #ifndef CONFOPT_ADDWSTR
312 cptr
= mpdm_wcstombs(wptr
, NULL
);
318 #endif /* CONFOPT_ADDWSTR */
324 static void draw_status(void)
325 /* draws the status bar */
329 t
= mp_build_status_line();
331 /* move to the last line, clear it and draw there */
332 wmove(cw
, LINES
- 1, 0);
333 wattrset(cw
, nc_attrs
[normal_attr
]);
337 /* draw the menu key hint, right-aligned */
338 if ((t
= mpdm_hget_s(mp
, L
"menu_key_hint")) == NULL
) {
339 t
= MPDM_LS(L
"ctrl-a: ");
340 t
= mpdm_strcat(t
, mpdm_gettext(MPDM_LS(L
"Menu")));
341 mpdm_hset_s(mp
, L
"menu_key_hint", t
);
344 wmove(cw
, LINES
- 1, COLS
- mpdm_size(t
));
349 static void nc_draw(mpdm_t doc
)
350 /* driver drawing function for cursesw */
359 for (n
= 0; n
< mpdm_size(d
); n
++) {
360 mpdm_t l
= mpdm_aget(d
, n
);
364 for (m
= 0; m
< mpdm_size(l
); m
++) {
368 /* get the attribute and the string */
369 attr
= mpdm_ival(mpdm_aget(l
, m
++));
372 wattrset(cw
, nc_attrs
[attr
]);
383 static void build_colors(void)
384 /* builds the colors */
392 #ifdef CONFOPT_TRANSPARENCY
393 use_default_colors();
395 #define DEFAULT_INK -1
396 #define DEFAULT_PAPER -1
398 #else /* CONFOPT_TRANSPARENCY */
400 #define DEFAULT_INK COLOR_BLACK
401 #define DEFAULT_PAPER COLOR_WHITE
405 /* gets the color definitions and attribute names */
406 colors
= mpdm_hget_s(mp
, L
"colors");
407 color_names
= mpdm_hget_s(mp
, L
"color_names");
408 l
= mpdm_keys(colors
);
411 /* redim the structures */
412 nc_attrs
= realloc(nc_attrs
, sizeof(int) * s
);
414 /* loop the colors */
415 for (n
= 0; n
< s
&& (c
= mpdm_aget(l
, n
)) != NULL
; n
++) {
416 mpdm_t d
= mpdm_hget(colors
, c
);
417 mpdm_t v
= mpdm_hget_s(d
, L
"text");
420 /* store the 'normal' attribute */
421 if (wcscmp(mpdm_string(c
), L
"normal") == 0)
425 mpdm_hset_s(d
, L
"attr", MPDM_I(n
));
427 /* get color indexes */
428 if ((c0
= mpdm_seek(color_names
, mpdm_aget(v
, 0), 1)) == -1 ||
429 (c1
= mpdm_seek(color_names
, mpdm_aget(v
, 1), 1)) == -1)
432 init_pair(n
+ 1, c0
- 1, c1
- 1);
433 cp
= COLOR_PAIR(n
+ 1);
436 v
= mpdm_hget_s(d
, L
"flags");
437 if (mpdm_seek_s(v
, L
"reverse", 1) != -1)
439 if (mpdm_seek_s(v
, L
"bright", 1) != -1)
441 if (mpdm_seek_s(v
, L
"underline", 1) != -1)
447 /* set the background filler */
448 wbkgdset(cw
, ' ' | nc_attrs
[normal_attr
]);
452 /** driver functions **/
454 static mpdm_t
ncursesw_drv_timer(mpdm_t a
)
456 mpdm_t func
= mpdm_aget(a
, 1);
459 timer_msecs
= mpdm_ival(mpdm_aget(a
, 0));
461 r
= mpdm_unref(timer_func
);
462 timer_func
= mpdm_ref(func
);
468 static mpdm_t
ncursesw_drv_main_loop(mpdm_t a
)
469 /* curses driver main loop */
471 while (! mp_exit_requested
) {
472 /* get current document and draw it */
473 nc_draw(mp_active());
475 /* get key and process it */
476 mp_process_event(nc_getkey(NULL
));
483 static mpdm_t
ncursesw_drv_shutdown(mpdm_t a
)
489 if ((v
= mpdm_hget_s(mp
, L
"exit_message")) != NULL
) {
490 mpdm_write_wcs(stdout
, mpdm_string(v
));
500 static mpdm_t
tui_addstr(mpdm_t a
)
501 /* TUI: add a string */
503 return nc_addwstr(mpdm_aget(a
, 0));
507 static mpdm_t
tui_move(mpdm_t a
)
508 /* TUI: move to a screen position */
510 /* curses' move() use y, x */
511 wmove(cw
, mpdm_ival(mpdm_aget(a
, 1)), mpdm_ival(mpdm_aget(a
, 0)));
513 /* if third argument is not NULL, clear line */
514 if (mpdm_aget(a
, 2) != NULL
)
521 static mpdm_t
tui_attr(mpdm_t a
)
522 /* TUI: set attribute for next string */
524 last_attr
= mpdm_ival(mpdm_aget(a
, 0));
532 static mpdm_t
tui_refresh(mpdm_t a
)
533 /* TUI: refresh the screen */
540 static mpdm_t
tui_getxy(mpdm_t a
)
541 /* TUI: returns the x and y cursor position */
549 mpdm_aset(v
, MPDM_I(x
), 0);
550 mpdm_aset(v
, MPDM_I(y
), 1);
556 static mpdm_t
tui_openpanel(mpdm_t a
)
557 /* opens a panel (creates new window) */
560 w_stack
= realloc(w_stack
, n_stack
* sizeof(WINDOW
*));
561 cw
= w_stack
[n_stack
- 1] = newwin(mpdm_ival(mpdm_aget(a
, 3)),
562 mpdm_ival(mpdm_aget(a
, 2)),
563 mpdm_ival(mpdm_aget(a
, 1)),
564 mpdm_ival(mpdm_aget(a
, 0)));
574 static mpdm_t
tui_closepanel(mpdm_t a
)
575 /* closes a panel (deletes last window) */
578 delwin(w_stack
[n_stack
]);
580 w_stack
= realloc(w_stack
, n_stack
* sizeof(WINDOW
*));
581 cw
= n_stack
== 0 ? stdscr
: w_stack
[n_stack
- 1];
590 static void register_functions(void)
595 drv
= mpdm_hget_s(mp
, L
"drv");
596 mpdm_hset_s(drv
, L
"timer", MPDM_X(ncursesw_drv_timer
));
597 mpdm_hset_s(drv
, L
"main_loop", MPDM_X(ncursesw_drv_main_loop
));
598 mpdm_hset_s(drv
, L
"shutdown", MPDM_X(ncursesw_drv_shutdown
));
600 tui
= mpsl_eval(MPDM_LS(L
"load('mp_tui.mpsl');"), NULL
);
602 /* FIXME: if tui failed, a fatal error must be shown */
604 /* if((e = mpdm_hget_s(mpdm_root(), L"ERROR")) != NULL)
606 mpdm_write_wcs(stdout, mpdm_string(e));
613 mpdm_hset_s(tui
, L
"getkey", MPDM_X(nc_getkey
));
614 mpdm_hset_s(tui
, L
"addstr", MPDM_X(tui_addstr
));
615 mpdm_hset_s(tui
, L
"move", MPDM_X(tui_move
));
616 mpdm_hset_s(tui
, L
"attr", MPDM_X(tui_attr
));
617 mpdm_hset_s(tui
, L
"refresh", MPDM_X(tui_refresh
));
618 mpdm_hset_s(tui
, L
"getxy", MPDM_X(tui_getxy
));
619 mpdm_hset_s(tui
, L
"openpanel", MPDM_X(tui_openpanel
));
620 mpdm_hset_s(tui
, L
"closepanel", MPDM_X(tui_closepanel
));
624 static mpdm_t
ncursesw_drv_startup(mpdm_t a
)
628 register_functions();
632 keypad(stdscr
, TRUE
);
639 v
= mpdm_hget_s(mp
, L
"window");
640 mpdm_hset_s(v
, L
"tx", MPDM_I(COLS
));
641 mpdm_hset_s(v
, L
"ty", MPDM_I(LINES
));
643 signal(SIGWINCH
, nc_sigwinch
);
651 int ncursesw_drv_detect(int * argc
, char *** argv
)
655 drv
= mpdm_hget_s(mp
, L
"drv");
656 mpdm_hset_s(drv
, L
"id", MPDM_LS(L
"curses"));
657 mpdm_hset_s(drv
, L
"startup", MPDM_X(ncursesw_drv_startup
));
662 #endif /* CONFOPT_CURSES */