Ticket #2229: speed up of up/down moving in viewer in wrapped mode.
[midnight-commander.git] / src / learn.c
blob12023b98bcad5043c82d1a2c96b1e4e94c1231bc
1 /* Learn keys
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1995 Jakub Jelinek
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /** \file learn.c
23 * \brief Source: learn keys module
26 #include <config.h>
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/tty/key.h"
41 #include "lib/mcconfig.h" /* Save profile */
42 #include "lib/strescape.h"
43 #include "lib/strutil.h"
45 #include "dialog.h"
46 #include "widget.h"
47 #include "setup.h"
48 #include "layout.h" /* repaint_screen() */
49 #include "learn.h"
50 #include "wtools.h"
52 #define UX 4
53 #define UY 3
55 #define BY UY + 17
57 #define ROWS 13
58 #define COLSHIFT 23
60 #define BUTTONS 2
62 static struct {
63 int ret_cmd, flags, y, x;
64 const char *text;
65 } learn_but[BUTTONS] = {
66 /* *INDENT-OFF */
67 { B_CANCEL, NORMAL_BUTTON, 0, 39, N_("&Cancel") },
68 { B_ENTER, DEFPUSH_BUTTON, 0, 25, N_("&Save") }
69 /* *INDENT-ON */
72 static Dlg_head *learn_dlg;
73 typedef struct {
74 Widget *button;
75 Widget *label;
76 int ok;
77 char *sequence;
78 } learnkey;
79 static learnkey *learnkeys = NULL;
80 static int learn_total;
81 static int learnok;
82 static int learnchanged;
83 static const char* learn_title = N_("Learn keys");
86 static int learn_button (WButton *button, int action)
88 Dlg_head *d;
89 char *seq;
91 (void) button;
93 d = create_message (D_ERROR, _("Teach me a key"),
94 _("Please press the %s\n"
95 "and then wait until this message disappears.\n\n"
96 "Then, press it again to see if OK appears\n"
97 "next to its button.\n\n"
98 "If you want to escape, press a single Escape key\n"
99 "and wait as well."),
100 _(key_name_conv_tab [action - B_USER].longname));
101 mc_refresh ();
102 if (learnkeys [action - B_USER].sequence != NULL) {
103 g_free (learnkeys [action - B_USER].sequence);
104 learnkeys [action - B_USER].sequence = NULL;
106 seq = learn_key ();
108 if (seq){
109 /* Esc hides the dialog and do not allow definitions of
110 * regular characters
112 gboolean seq_ok = FALSE;
114 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
115 && strcmp (seq, "^m" ) && strcmp (seq, "^i" )
116 && (seq [1] || (*seq < ' ' || *seq > '~'))){
118 learnchanged = 1;
119 learnkeys [action - B_USER].sequence = seq;
120 seq = convert_controls (seq);
121 seq_ok = define_sequence (key_name_conv_tab [action - B_USER].code,
122 seq, MCKEY_NOACTION);
125 if (!seq_ok)
126 message (D_NORMAL, _("Cannot accept this key"),
127 _("You have entered \"%s\""), seq);
129 g_free (seq);
132 dlg_run_done (d);
133 destroy_dlg (d);
134 dlg_select_widget (learnkeys [action - B_USER].button);
135 return 0; /* Do not kill learn_dlg */
138 static int learn_move (int right)
140 int i, totalcols;
142 totalcols = (learn_total - 1) / ROWS + 1;
143 for (i = 0; i < learn_total; i++)
144 if (learnkeys [i].button == (Widget *) learn_dlg->current->data) {
145 if (right) {
146 if (i < learn_total - ROWS)
147 i += ROWS;
148 else
149 i %= ROWS;
150 } else {
151 if (i / ROWS)
152 i -= ROWS;
153 else if (i + (totalcols - 1) * ROWS >= learn_total)
154 i += (totalcols - 2) * ROWS;
155 else
156 i += (totalcols - 1) * ROWS;
158 dlg_select_widget (learnkeys [i].button);
159 return 1;
161 return 0;
164 static int
165 learn_check_key (int c)
167 int i;
169 for (i = 0; i < learn_total; i++) {
170 if (key_name_conv_tab[i].code != c || learnkeys[i].ok)
171 continue;
173 dlg_select_widget (learnkeys[i].button);
174 /* TRANSLATORS: This label appears near learned keys. Keep it short. */
175 label_set_text ((WLabel *) learnkeys[i].label, _("OK"));
176 learnkeys[i].ok = 1;
177 learnok++;
178 if (learnok >= learn_total) {
179 learn_dlg->ret_value = B_CANCEL;
180 if (learnchanged) {
181 if (query_dialog (learn_title,
183 ("It seems that all your keys already\n"
184 "work fine. That's great."), D_ERROR, 2,
185 _("&Save"), _("&Discard")) == 0)
186 learn_dlg->ret_value = B_ENTER;
187 } else {
188 message (D_ERROR, learn_title,
190 ("Great! You have a complete terminal database!\n"
191 "All your keys work well."));
193 dlg_stop (learn_dlg);
195 return 1;
197 switch (c) {
198 case KEY_LEFT:
199 case 'h':
200 return learn_move (0);
201 case KEY_RIGHT:
202 case 'l':
203 return learn_move (1);
204 case 'j':
205 dlg_one_down (learn_dlg);
206 return 1;
207 case 'k':
208 dlg_one_up (learn_dlg);
209 return 1;
212 /* Prevent from disappearing if a non-defined sequence is pressed
213 and contains a button hotkey. Only recognize hotkeys with ALT. */
214 if (c < 255 && g_ascii_isalnum (c))
215 return 1;
217 return 0;
220 static cb_ret_t
221 learn_callback (Dlg_head *h, Widget *sender,
222 dlg_msg_t msg, int parm, void *data)
224 switch (msg) {
225 case DLG_DRAW:
226 common_dialog_repaint (h);
227 return MSG_HANDLED;
229 case DLG_KEY:
230 return learn_check_key (parm);
232 default:
233 return default_dlg_callback (h, sender, msg, parm, data);
237 static void
238 init_learn (void)
240 int x, y, i, j;
241 const key_code_name_t *key;
242 char buffer[BUF_TINY];
244 #ifdef ENABLE_NLS
245 static int i18n_flag = 0;
246 if (!i18n_flag) {
247 learn_but[0].text = _(learn_but[0].text);
248 learn_but[0].x = 78 / 2 + 4;
250 learn_but[1].text = _(learn_but[1].text);
251 learn_but[1].x = 78 / 2 - (str_term_width1 (learn_but[1].text) + 9);
253 learn_title = _(learn_title);
254 i18n_flag = 1;
256 #endif /* ENABLE_NLS */
258 do_refresh ();
260 learn_dlg =
261 create_dlg (TRUE, 0, 0, 23, 78, dialog_colors, learn_callback,
262 "[Learn keys]", learn_title, DLG_CENTER | DLG_REVERSE);
264 for (i = 0; i < BUTTONS; i++)
265 add_widget (learn_dlg,
266 button_new (BY + learn_but[i].y, learn_but[i].x,
267 learn_but[i].ret_cmd, learn_but[i].flags,
268 _(learn_but[i].text), 0));
270 x = UX;
271 y = UY;
272 for (key = key_name_conv_tab, j = 0;
273 key->name != NULL && strcmp (key->name, "kpleft");
274 key++, j++)
276 learnkeys = g_new (learnkey, j);
277 x += ((j - 1) / ROWS) * COLSHIFT;
278 y += (j - 1) % ROWS;
279 learn_total = j;
280 learnok = 0;
281 learnchanged = 0;
282 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) {
283 learnkeys[i].ok = 0;
284 learnkeys[i].sequence = NULL;
285 g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
286 add_widget (learn_dlg, learnkeys[i].button = (Widget *)
287 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer,
288 learn_button));
289 add_widget (learn_dlg, learnkeys[i].label = (Widget *)
290 label_new (y, x + 19, ""));
291 if (i % 13)
292 y--;
293 else {
294 x -= COLSHIFT;
295 y = UY + ROWS - 1;
298 add_widget (learn_dlg,
299 label_new (UY + 14, 5,
301 ("Press all the keys mentioned here. After you have done it, check")));
302 add_widget (learn_dlg,
303 label_new (UY + 15, 5,
305 ("which keys are not marked with OK. Press space on the missing")));
306 add_widget (learn_dlg,
307 label_new (UY + 16, 5,
309 ("key, or click with the mouse to define it. Move around with Tab.")));
312 static void learn_done (void)
314 destroy_dlg (learn_dlg);
315 repaint_screen ();
318 static void
319 learn_save (void)
321 int i;
322 int profile_changed = 0;
323 char *section = g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL);
324 char *esc_str;
326 for (i = 0; i < learn_total; i++) {
327 if (learnkeys [i].sequence != NULL) {
328 profile_changed = 1;
330 esc_str = strutils_escape (learnkeys [i].sequence, -1, ";\\", TRUE);
332 mc_config_direct_set_string(mc_main_config, section,
333 key_name_conv_tab [i].name, esc_str);
335 g_free(esc_str);
339 /* On the one hand no good idea to save the complete setup but
340 * without 'Auto save setup' the new key-definitions will not be
341 * saved unless the user does an 'Options/Save Setup'.
342 * On the other hand a save-button that does not save anything to
343 * disk is much worse.
345 if (profile_changed)
346 mc_config_save_file (mc_main_config, NULL);
348 g_free (section);
351 void learn_keys (void)
353 int save_old_esc_mode = old_esc_mode;
354 int save_alternate_plus_minus = alternate_plus_minus;
356 old_esc_mode = 0; /* old_esc_mode cannot work in learn keys dialog */
357 alternate_plus_minus = 1; /* don't translate KP_ADD, KP_SUBTRACT and
358 KP_MULTIPLY to '+', '-' and '*' in
359 correct_key_code */
360 application_keypad_mode ();
361 init_learn ();
363 run_dlg (learn_dlg);
365 old_esc_mode = save_old_esc_mode;
366 alternate_plus_minus = save_alternate_plus_minus;
368 if (!alternate_plus_minus)
369 numeric_keypad_mode ();
371 switch (learn_dlg->ret_value) {
372 case B_ENTER:
373 learn_save ();
374 break;
377 learn_done ();