3 Minimum Profit - Programmer Text Editor
7 Copyright (C) 1991-2010 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
39 #include <sys/ioctl.h>
41 #include <ncursesw/ncurses.h>
50 /* the curses attributes */
53 /* code for the 'normal' attribute */
54 static int normal_attr
= 0;
57 static WINDOW
*cw
= NULL
;
59 /* stack of windows */
60 static int n_stack
= 0;
61 static WINDOW
**w_stack
= NULL
;
64 static int last_attr
= 0;
67 static int timer_msecs
= 0;
68 static mpdm_t timer_func
= NULL
;
72 static void set_attr(void)
73 /* set the current and fill attributes */
75 if (wattrset(cw
, nc_attrs
[last_attr
]) == OK
)
76 wbkgdset(cw
, ' ' | nc_attrs
[last_attr
]);
80 static void nc_sigwinch(int s
)
81 /* SIGWINCH signal handler */
85 #ifdef NCURSES_VERSION
86 /* Make sure that window size changes... */
89 int fd
= open("/dev/tty", O_RDWR
);
92 return; /* This should never have to happen! */
94 if (ioctl(fd
, TIOCGWINSZ
, &ws
) == 0)
95 resizeterm(ws
.ws_row
, ws
.ws_col
);
103 /* invalidate main window */
107 /* re-set dimensions */
108 v
= mpdm_hget_s(mp
, L
"window");
109 mpdm_hset_s(v
, L
"tx", MPDM_I(COLS
));
110 mpdm_hset_s(v
, L
"ty", MPDM_I(LINES
));
113 signal(SIGWINCH
, nc_sigwinch
);
117 #ifdef CONFOPT_WGET_WCH
118 int wget_wch(WINDOW
* w
, wint_t * ch
);
121 static wchar_t *nc_getwch(void)
122 /* gets a key as a wchar_t */
126 #ifdef CONFOPT_WGET_WCH
128 /* set timer period */
130 timeout(timer_msecs
);
132 if (wget_wch(stdscr
, (wint_t *) c
) == -1)
133 c
[0] = (wchar_t) - 1;
136 char tmp
[MB_CUR_MAX
+ 1];
146 /* set to non-blocking */
149 /* read all possible following characters */
151 while (n
< sizeof(tmp
) - 1 && (cc
= getch()) != ERR
)
154 /* sets input as blocking */
166 #define ctrl(k) ((k) & 31)
168 static mpdm_t
nc_getkey(mpdm_t args
, mpdm_t ctxt
)
169 /* reads a key and converts to an action */
171 static int shift
= 0;
175 /* any pending key? return it */
176 if ((k
= mp_pending_key()) != NULL
)
182 mpdm_void(mpdm_exec(timer_func
, NULL
, NULL
));
219 f
= L
"alt-cursor-left";
222 f
= L
"alt-cursor-right";
225 f
= L
"alt-cursor-down";
228 f
= L
"alt-cursor-up";
513 k
= mpdm_ref(MPDM_S(f
));
515 if ((t
= mp_process_keyseq(k
)) != k
) {
527 static mpdm_t
nc_addwstr(mpdm_t str
)
530 wchar_t *wptr
= mpdm_string(str
);
532 #ifndef CONFOPT_ADDWSTR
535 cptr
= mpdm_wcstombs(wptr
, NULL
);
541 #endif /* CONFOPT_ADDWSTR */
547 static mpdm_t
nc_doc_draw(mpdm_t args
, mpdm_t ctxt
)
548 /* draws the document part */
555 d
= mpdm_aget(args
, 0);
556 d
= mpdm_ref(mp_draw(d
, 0));
558 for (n
= 0; n
< mpdm_size(d
); n
++) {
559 mpdm_t l
= mpdm_aget(d
, n
);
563 for (m
= 0; m
< mpdm_size(l
); m
++) {
567 /* get the attribute and the string */
568 attr
= mpdm_ival(mpdm_aget(l
, m
++));
571 if (wattrset(cw
, nc_attrs
[attr
]) == OK
)
582 static void build_colors(void)
583 /* builds the colors */
591 #ifdef CONFOPT_TRANSPARENCY
592 use_default_colors();
594 #define DEFAULT_INK -1
595 #define DEFAULT_PAPER -1
597 #else /* CONFOPT_TRANSPARENCY */
599 #define DEFAULT_INK COLOR_BLACK
600 #define DEFAULT_PAPER COLOR_WHITE
604 /* gets the color definitions and attribute names */
605 colors
= mpdm_hget_s(mp
, L
"colors");
606 color_names
= mpdm_hget_s(mp
, L
"color_names");
607 l
= mpdm_ref(mpdm_keys(colors
));
610 /* redim the structures */
611 nc_attrs
= realloc(nc_attrs
, sizeof(int) * s
);
613 /* loop the colors */
614 for (n
= 0; n
< s
&& (c
= mpdm_aget(l
, n
)) != NULL
; n
++) {
615 mpdm_t d
= mpdm_hget(colors
, c
);
616 mpdm_t v
= mpdm_hget_s(d
, L
"text");
619 /* store the 'normal' attribute */
620 if (wcscmp(mpdm_string(c
), L
"normal") == 0)
624 mpdm_hset_s(d
, L
"attr", MPDM_I(n
));
626 /* get color indexes */
627 if ((c0
= mpdm_seek(color_names
, mpdm_aget(v
, 0), 1)) == -1 ||
628 (c1
= mpdm_seek(color_names
, mpdm_aget(v
, 1), 1)) == -1)
631 init_pair(n
+ 1, c0
- 1, c1
- 1);
632 cp
= COLOR_PAIR(n
+ 1);
635 v
= mpdm_hget_s(d
, L
"flags");
636 if (mpdm_seek_s(v
, L
"reverse", 1) != -1)
638 if (mpdm_seek_s(v
, L
"bright", 1) != -1)
640 if (mpdm_seek_s(v
, L
"underline", 1) != -1)
646 /* set the background filler */
647 wbkgdset(cw
, ' ' | nc_attrs
[normal_attr
]);
653 /** driver functions **/
655 static mpdm_t
ncursesw_drv_timer(mpdm_t a
, mpdm_t ctxt
)
657 mpdm_t func
= mpdm_aget(a
, 1);
659 timer_msecs
= mpdm_ival(mpdm_aget(a
, 0));
662 mpdm_unref(timer_func
);
669 static mpdm_t
ncursesw_drv_shutdown(mpdm_t a
, mpdm_t ctxt
)
675 if ((v
= mpdm_hget_s(mp
, L
"exit_message")) != NULL
) {
676 mpdm_write_wcs(stdout
, mpdm_string(v
));
686 static mpdm_t
tui_addstr(mpdm_t a
, mpdm_t ctxt
)
687 /* TUI: add a string */
689 return nc_addwstr(mpdm_aget(a
, 0));
693 static mpdm_t
tui_move(mpdm_t a
, mpdm_t ctxt
)
694 /* TUI: move to a screen position */
696 /* curses' move() use y, x */
697 wmove(cw
, mpdm_ival(mpdm_aget(a
, 1)), mpdm_ival(mpdm_aget(a
, 0)));
699 /* if third argument is not NULL, clear line */
700 if (mpdm_aget(a
, 2) != NULL
)
707 static mpdm_t
tui_attr(mpdm_t a
, mpdm_t ctxt
)
708 /* TUI: set attribute for next string */
710 last_attr
= mpdm_ival(mpdm_aget(a
, 0));
718 static mpdm_t
tui_refresh(mpdm_t a
, mpdm_t ctxt
)
719 /* TUI: refresh the screen */
726 static mpdm_t
tui_getxy(mpdm_t a
, mpdm_t ctxt
)
727 /* TUI: returns the x and y cursor position */
737 mpdm_aset(v
, MPDM_I(x
), 0);
738 mpdm_aset(v
, MPDM_I(y
), 1);
746 static mpdm_t
tui_openpanel(mpdm_t a
, mpdm_t ctxt
)
747 /* opens a panel (creates new window) */
750 w_stack
= realloc(w_stack
, n_stack
* sizeof(WINDOW
*));
751 cw
= w_stack
[n_stack
- 1] = newwin(mpdm_ival(mpdm_aget(a
, 3)),
752 mpdm_ival(mpdm_aget(a
, 2)),
753 mpdm_ival(mpdm_aget(a
, 1)),
754 mpdm_ival(mpdm_aget(a
, 0)));
764 static mpdm_t
tui_closepanel(mpdm_t a
, mpdm_t ctxt
)
765 /* closes a panel (deletes last window) */
768 delwin(w_stack
[n_stack
]);
770 w_stack
= realloc(w_stack
, n_stack
* sizeof(WINDOW
*));
771 cw
= n_stack
== 0 ? stdscr
: w_stack
[n_stack
- 1];
780 static void register_functions(void)
785 drv
= mpdm_hget_s(mp
, L
"drv");
786 mpdm_hset_s(drv
, L
"timer", MPDM_X(ncursesw_drv_timer
));
787 mpdm_hset_s(drv
, L
"shutdown", MPDM_X(ncursesw_drv_shutdown
));
789 tui
= mpsl_eval(MPDM_LS(L
"load('mp_tui.mpsl');"), NULL
, NULL
);
791 /* FIXME: if tui failed, a fatal error must be shown */
793 /* if((e = mpdm_hget_s(mpdm_root(), L"ERROR")) != NULL)
795 mpdm_write_wcs(stdout, mpdm_string(e));
802 mpdm_hset_s(tui
, L
"getkey", MPDM_X(nc_getkey
));
803 mpdm_hset_s(tui
, L
"addstr", MPDM_X(tui_addstr
));
804 mpdm_hset_s(tui
, L
"move", MPDM_X(tui_move
));
805 mpdm_hset_s(tui
, L
"attr", MPDM_X(tui_attr
));
806 mpdm_hset_s(tui
, L
"refresh", MPDM_X(tui_refresh
));
807 mpdm_hset_s(tui
, L
"getxy", MPDM_X(tui_getxy
));
808 mpdm_hset_s(tui
, L
"openpanel", MPDM_X(tui_openpanel
));
809 mpdm_hset_s(tui
, L
"closepanel", MPDM_X(tui_closepanel
));
810 mpdm_hset_s(tui
, L
"doc_draw", MPDM_X(nc_doc_draw
));
814 static mpdm_t
ncursesw_drv_startup(mpdm_t a
)
818 register_functions();
822 keypad(stdscr
, TRUE
);
829 v
= mpdm_hget_s(mp
, L
"window");
830 mpdm_hset_s(v
, L
"tx", MPDM_I(COLS
));
831 mpdm_hset_s(v
, L
"ty", MPDM_I(LINES
));
833 signal(SIGWINCH
, nc_sigwinch
);
841 int ncursesw_drv_detect(int *argc
, char ***argv
)
845 drv
= mpdm_hget_s(mp
, L
"drv");
846 mpdm_hset_s(drv
, L
"id", MPDM_LS(L
"curses"));
847 mpdm_hset_s(drv
, L
"startup", MPDM_X(ncursesw_drv_startup
));
852 #endif /* CONFOPT_CURSES */