small highlighting correction...
[midnight-commander.git] / src / learn.c
blobad41cb7a9c594ff00acffaffe31180cc4d89023c
1 /* Learn keys
2 Copyright (C) 1995 The Free Software Foundation
4 Written by: 1995 Jakub Jelinek
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <config.h>
22 #ifdef HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 #include <string.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <ctype.h>
31 #include "global.h"
32 #include "tty.h"
33 #include "win.h"
34 #include "color.h"
35 #include "dlg.h"
36 #include "widget.h"
37 #include "dialog.h" /* For do_refresh() */
38 #include "profile.h" /* Save profile */
39 #include "key.h"
40 #include "setup.h"
41 #include "main.h"
42 #include "learn.h"
43 #include "wtools.h"
45 #define UX 4
46 #define UY 3
48 #define BY UY + 17
50 #define ROWS 13
51 #define COLSHIFT 23
53 #define BUTTONS 2
55 static struct {
56 int ret_cmd, flags, y, x;
57 unsigned int hotkey;
58 char *text;
59 } learn_but[BUTTONS] = {
60 { B_CANCEL, NORMAL_BUTTON, 0, 39, 'C', N_("&Cancel") },
61 { B_ENTER, DEFPUSH_BUTTON, 0, 25, 'S', N_("&Save") }
64 static Dlg_head *learn_dlg;
65 typedef struct {
66 Widget *button;
67 Widget *label;
68 int ok;
69 char *sequence;
70 } learnkey;
71 static learnkey *learnkeys = NULL;
72 static int learn_total;
73 static int learnok;
74 static int learnchanged;
75 static char* learn_title = N_(" Learn keys ");
78 static int learn_button (int action, void *param)
80 unsigned char *seq;
81 Dlg_head *d = message (D_INSERT | 1, _(" Teach me a key "),
82 _("Please press the %s\n"
83 "and then wait until this message disappears.\n\n"
84 "Then, press it again to see if OK appears\n"
85 "next to its button.\n\n"
86 "If you want to escape, press a single Escape key\n"
87 "and wait as well."),
88 _(key_name_conv_tab [action - B_USER].longname));
89 mc_refresh ();
90 if (learnkeys [action - B_USER].sequence != NULL) {
91 g_free (learnkeys [action - B_USER].sequence);
92 learnkeys [action - B_USER].sequence = NULL;
94 seq = learn_key ();
96 if (seq){
97 /* Esc hides the dialog and do not allow definitions of
98 * regular characters
100 int seq_ok;
102 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
103 && strcmp (seq, "^m" ) && strcmp (seq, "^i" )
104 && (seq [1] || (*seq < ' ' || *seq > '~'))){
106 learnchanged = 1;
107 learnkeys [action - B_USER].sequence = seq;
108 seq = convert_controls (seq);
109 seq_ok = define_sequence (key_name_conv_tab [action - B_USER].code,
110 seq, MCKEY_NOACTION);
111 } else {
112 seq_ok = 0;
115 if (!seq_ok) {
116 message (0, _(" Cannot accept this key "),
117 _(" You have entered \"%s\""), seq);
120 g_free (seq);
123 dlg_run_done (d);
124 destroy_dlg (d);
125 dlg_select_widget (learn_dlg, learnkeys [action - B_USER].button);
126 return 0; /* Do not kill learn_dlg */
129 static int learn_move (int right)
131 int i, totalcols;
133 totalcols = (learn_total - 1) / ROWS + 1;
134 for (i = 0; i < learn_total; i++)
135 if (learnkeys [i].button == learn_dlg->current->widget) {
136 if (right) {
137 if (i < learn_total - ROWS)
138 i += ROWS;
139 else
140 i %= ROWS;
141 } else {
142 if (i / ROWS)
143 i -= ROWS;
144 else if (i + (totalcols - 1) * ROWS >= learn_total)
145 i += (totalcols - 2) * ROWS;
146 else
147 i += (totalcols - 1) * ROWS;
149 dlg_select_widget (learn_dlg, (void *) learnkeys [i].button);
150 return 1;
152 return 0;
155 static int learn_check_key (int c)
157 int i;
159 for (i = 0; i < learn_total; i++) {
160 if (key_name_conv_tab [i].code == c) {
161 if (!learnkeys [i].ok) {
162 dlg_select_widget (learn_dlg, learnkeys [i].button);
163 label_set_text ((WLabel *) learnkeys [i].label,
164 _("OK"));
165 learnkeys [i].ok = 1;
166 learnok++;
167 if (learnok >= learn_total) {
168 learn_dlg->ret_value = B_CANCEL;
169 if (learnchanged) {
170 if (query_dialog (learn_title,
171 _("It seems that all your keys already\n"
172 "work fine. That's great."),
173 1, 2, _("&Save"), _("&Discard")) == 0)
174 learn_dlg->ret_value = B_ENTER;
175 } else {
176 message (1, learn_title,
177 _("Great! You have a complete terminal database!\n"
178 "All your keys work well."));
180 dlg_stop (learn_dlg);
182 return 1;
186 switch (c) {
187 case KEY_LEFT:
188 case 'h':
189 return learn_move (0);
190 case KEY_RIGHT:
191 case 'l':
192 return learn_move (1);
193 case 'j':
194 dlg_one_down (learn_dlg);
195 return 1;
196 case 'k':
197 dlg_one_up (learn_dlg);
198 return 1;
201 /* Prevent from disappearing if a non-defined sequence is pressed
202 and contains s or c. Use ALT('s') or ALT('c'). */
203 if (c < 255 && isalpha(c))
205 c = toupper(c);
206 for (i = 0; i < BUTTONS; i++)
207 if (c == learn_but [i].hotkey)
208 return 1;
211 return 0;
214 static int learn_callback (Dlg_head * h, int Par, int Msg)
216 switch (Msg) {
217 case DLG_DRAW:
218 common_dialog_repaint (h);
219 break;
220 case DLG_KEY:
221 return learn_check_key (Par);
223 return 0;
226 static void init_learn (void)
228 int x, y, i, j;
229 key_code_name_t *key;
230 char buffer [BUF_TINY];
232 #ifdef ENABLE_NLS
233 static int i18n_flag = 0;
234 if (!i18n_flag)
236 char* cp;
238 learn_but [0].text = _(learn_but [0].text);
239 learn_but [0].x = 78 / 2 + 4;
241 learn_but [1].text = _(learn_but [1].text);
242 learn_but [1].x = 78 / 2 - (strlen (learn_but [1].text) + 9);
244 for (i = 0; i < BUTTONS; i++)
246 cp = strchr(learn_but [i].text, '&');
247 if (cp != NULL && *++cp != '\0')
248 learn_but [i].hotkey = toupper(*cp);
251 learn_title = _(learn_title);
252 i18n_flag = 1;
254 #endif /* ENABLE_NLS */
256 do_refresh ();
258 learn_dlg = create_dlg (0, 0, 23, 78, dialog_colors,
259 learn_callback, "[Learn keys]",
260 _("Learn keys"), DLG_CENTER);
262 #define XTRACT(i) BY+learn_but[i].y, learn_but[i].x, learn_but[i].ret_cmd, learn_but[i].flags, _(learn_but[i].text), 0, 0, NULL
264 for (i = 0; i < BUTTONS; i++)
265 add_widget (learn_dlg, button_new (XTRACT (i)));
267 x = UX;
268 y = UY;
269 for (key = key_name_conv_tab, j = 0; key->name != NULL &&
270 strcmp (key->name, "kpleft"); key++, j++);
271 learnkeys = g_new (learnkey, j);
272 x += ((j - 1) / ROWS) * COLSHIFT;
273 y += (j - 1) % ROWS;
274 learn_total = j;
275 learnok = 0;
276 learnchanged = 0;
277 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) {
278 learnkeys [i].ok = 0;
279 learnkeys [i].sequence = NULL;
280 g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
281 add_widget (learn_dlg, learnkeys [i].button = (Widget *)
282 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer, learn_button, 0, NULL));
283 add_widget (learn_dlg, learnkeys [i].label = (Widget *)
284 label_new (y, x + 19, "", NULL));
285 if (i % 13)
286 y--;
287 else {
288 x -= COLSHIFT;
289 y = UY + ROWS - 1;
292 add_widget (learn_dlg,
293 label_new (UY+14, 5, _("Press all the keys mentioned here. After you have done it, check"), NULL));
294 add_widget (learn_dlg,
295 label_new (UY+15, 5, _("which keys are not marked with OK. Press space on the missing"), NULL));
296 add_widget (learn_dlg,
297 label_new (UY+16, 5, _("key, or click with the mouse to define it. Move around with Tab."), NULL));
300 static void learn_done (void)
302 destroy_dlg (learn_dlg);
303 repaint_screen ();
306 static void
307 learn_save (void)
309 int i;
310 int profile_changed = 0;
311 char *section = g_strconcat ("terminal:", getenv ("TERM"), NULL);
313 for (i = 0; i < learn_total; i++) {
314 if (learnkeys [i].sequence != NULL) {
315 profile_changed = 1;
316 WritePrivateProfileString (section, key_name_conv_tab [i].name,
317 learnkeys [i].sequence, profile_name);
321 /* On the one hand no good idea to save the complete setup but
322 * without 'Auto save setup' the new key-definitions will not be
323 * saved unless the user does an 'Options/Save Setup'.
324 * On the other hand a save-button that does not save anything to
325 * disk is much worse.
327 if (profile_changed)
328 sync_profiles ();
330 g_free (section);
333 void learn_keys (void)
335 int save_old_esc_mode = old_esc_mode;
336 int save_alternate_plus_minus = alternate_plus_minus;
338 old_esc_mode = 0; /* old_esc_mode cannot work in learn keys dialog */
339 alternate_plus_minus = 1; /* don't translate KP_ADD, KP_SUBTRACT and
340 KP_MULTIPLY to '+', '-' and '*' in
341 correct_key_code */
342 application_keypad_mode ();
343 init_learn ();
345 run_dlg (learn_dlg);
347 old_esc_mode = save_old_esc_mode;
348 alternate_plus_minus = save_alternate_plus_minus;
350 if (!alternate_plus_minus)
351 numeric_keypad_mode ();
353 switch (learn_dlg->ret_value) {
354 case B_ENTER:
355 learn_save ();
356 break;
359 learn_done ();