Ticket #1868: mcedit hang up on replace with regexp.
[midnight-commander.git] / src / learn.c
blob39a8f1f6c35b8e92615a449b35cf875521217695
1 /*
2 Learn keys
4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Jakub Jelinek, 1995
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file learn.c
28 * \brief Source: learn keys module
31 #include <config.h>
33 #include <ctype.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
42 #include "lib/global.h"
44 #include "lib/tty/tty.h"
45 #include "lib/tty/key.h"
46 #include "lib/mcconfig.h" /* Save profile */
47 #include "lib/strescape.h"
48 #include "lib/strutil.h"
49 #include "lib/util.h" /* convert_controls() */
50 #include "lib/widget.h"
52 #include "src/filemanager/layout.h" /* mc_refresh() */
54 #include "setup.h"
55 #include "learn.h"
57 /*** global variables ****************************************************************************/
59 /*** file scope macro definitions ****************************************************************/
61 #define UX 4
62 #define UY 3
64 #define BY UY + 17
66 #define ROWS 13
67 #define COLSHIFT 23
69 #define BUTTONS 2
71 /*** file scope type declarations ****************************************************************/
73 typedef struct
75 Widget *button;
76 Widget *label;
77 int ok;
78 char *sequence;
79 } learnkey;
81 /*** file scope variables ************************************************************************/
83 static struct
85 int ret_cmd, flags, y, x;
86 const char *text;
87 } learn_but[BUTTONS] =
89 /* *INDENT-OFF */
91 B_CANCEL, NORMAL_BUTTON, 0, 39, N_("&Cancel")},
93 B_ENTER, DEFPUSH_BUTTON, 0, 25, N_("&Save")}
94 /* *INDENT-ON */
97 static Dlg_head *learn_dlg;
99 static learnkey *learnkeys = NULL;
100 static int learn_total;
101 static int learnok;
102 static int learnchanged;
103 static const char *learn_title = N_("Learn keys");
105 /*** file scope functions ************************************************************************/
106 /* --------------------------------------------------------------------------------------------- */
108 static int
109 learn_button (WButton * button, int action)
111 Dlg_head *d;
112 char *seq;
114 (void) button;
116 d = create_message (D_ERROR, _("Teach me a key"),
117 _("Please press the %s\n"
118 "and then wait until this message disappears.\n\n"
119 "Then, press it again to see if OK appears\n"
120 "next to its button.\n\n"
121 "If you want to escape, press a single Escape key\n"
122 "and wait as well."), _(key_name_conv_tab[action - B_USER].longname));
123 mc_refresh ();
124 if (learnkeys[action - B_USER].sequence != NULL)
126 g_free (learnkeys[action - B_USER].sequence);
127 learnkeys[action - B_USER].sequence = NULL;
129 seq = learn_key ();
131 if (seq)
133 /* Esc hides the dialog and do not allow definitions of
134 * regular characters
136 gboolean seq_ok = FALSE;
138 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
139 && strcmp (seq, "^m") && strcmp (seq, "^i") && (seq[1] || (*seq < ' ' || *seq > '~')))
142 learnchanged = 1;
143 learnkeys[action - B_USER].sequence = seq;
144 seq = convert_controls (seq);
145 seq_ok = define_sequence (key_name_conv_tab[action - B_USER].code, seq, MCKEY_NOACTION);
148 if (!seq_ok)
149 message (D_NORMAL, _("Cannot accept this key"), _("You have entered \"%s\""), seq);
151 g_free (seq);
154 dlg_run_done (d);
155 destroy_dlg (d);
156 dlg_select_widget (learnkeys[action - B_USER].button);
157 return 0; /* Do not kill learn_dlg */
160 /* --------------------------------------------------------------------------------------------- */
162 static int
163 learn_move (int right)
165 int i, totalcols;
167 totalcols = (learn_total - 1) / ROWS + 1;
168 for (i = 0; i < learn_total; i++)
169 if (learnkeys[i].button == (Widget *) learn_dlg->current->data)
171 if (right)
173 if (i < learn_total - ROWS)
174 i += ROWS;
175 else
176 i %= ROWS;
178 else
180 if (i / ROWS)
181 i -= ROWS;
182 else if (i + (totalcols - 1) * ROWS >= learn_total)
183 i += (totalcols - 2) * ROWS;
184 else
185 i += (totalcols - 1) * ROWS;
187 dlg_select_widget (learnkeys[i].button);
188 return 1;
190 return 0;
193 /* --------------------------------------------------------------------------------------------- */
195 static int
196 learn_check_key (int c)
198 int i;
200 for (i = 0; i < learn_total; i++)
202 if (key_name_conv_tab[i].code != c || learnkeys[i].ok)
203 continue;
205 dlg_select_widget (learnkeys[i].button);
206 /* TRANSLATORS: This label appears near learned keys. Keep it short. */
207 label_set_text ((WLabel *) learnkeys[i].label, _("OK"));
208 learnkeys[i].ok = 1;
209 learnok++;
210 if (learnok >= learn_total)
212 learn_dlg->ret_value = B_CANCEL;
213 if (learnchanged)
215 if (query_dialog (learn_title,
217 ("It seems that all your keys already\n"
218 "work fine. That's great."), D_ERROR, 2,
219 _("&Save"), _("&Discard")) == 0)
220 learn_dlg->ret_value = B_ENTER;
222 else
224 message (D_ERROR, learn_title,
226 ("Great! You have a complete terminal database!\n"
227 "All your keys work well."));
229 dlg_stop (learn_dlg);
231 return 1;
233 switch (c)
235 case KEY_LEFT:
236 case 'h':
237 return learn_move (0);
238 case KEY_RIGHT:
239 case 'l':
240 return learn_move (1);
241 case 'j':
242 dlg_one_down (learn_dlg);
243 return 1;
244 case 'k':
245 dlg_one_up (learn_dlg);
246 return 1;
249 /* Prevent from disappearing if a non-defined sequence is pressed
250 and contains a button hotkey. Only recognize hotkeys with ALT. */
251 if (c < 255 && g_ascii_isalnum (c))
252 return 1;
254 return 0;
257 /* --------------------------------------------------------------------------------------------- */
259 static cb_ret_t
260 learn_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
262 switch (msg)
264 case DLG_DRAW:
265 common_dialog_repaint (h);
266 return MSG_HANDLED;
268 case DLG_KEY:
269 return learn_check_key (parm);
271 default:
272 return default_dlg_callback (h, sender, msg, parm, data);
276 /* --------------------------------------------------------------------------------------------- */
278 static void
279 init_learn (void)
281 int x, y, i, j;
282 const key_code_name_t *key;
283 char buffer[BUF_TINY];
285 #ifdef ENABLE_NLS
286 static int i18n_flag = 0;
287 if (!i18n_flag)
289 learn_but[0].text = _(learn_but[0].text);
290 learn_but[0].x = 78 / 2 + 4;
292 learn_but[1].text = _(learn_but[1].text);
293 learn_but[1].x = 78 / 2 - (str_term_width1 (learn_but[1].text) + 9);
295 learn_title = _(learn_title);
296 i18n_flag = 1;
298 #endif /* ENABLE_NLS */
300 do_refresh ();
302 learn_dlg =
303 create_dlg (TRUE, 0, 0, 23, 78, dialog_colors, learn_callback,
304 "[Learn keys]", learn_title, DLG_CENTER | DLG_REVERSE);
306 for (i = 0; i < BUTTONS; i++)
307 add_widget (learn_dlg,
308 button_new (BY + learn_but[i].y, learn_but[i].x,
309 learn_but[i].ret_cmd, learn_but[i].flags, _(learn_but[i].text), 0));
311 x = UX;
312 y = UY;
313 for (key = key_name_conv_tab, j = 0;
314 key->name != NULL && strcmp (key->name, "kpleft"); key++, j++)
316 learnkeys = g_new (learnkey, j);
317 x += ((j - 1) / ROWS) * COLSHIFT;
318 y += (j - 1) % ROWS;
319 learn_total = j;
320 learnok = 0;
321 learnchanged = 0;
322 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--)
324 learnkeys[i].ok = 0;
325 learnkeys[i].sequence = NULL;
326 g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
327 add_widget (learn_dlg, learnkeys[i].button = (Widget *)
328 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer, learn_button));
329 add_widget (learn_dlg, learnkeys[i].label = (Widget *) label_new (y, x + 19, ""));
330 if (i % 13)
331 y--;
332 else
334 x -= COLSHIFT;
335 y = UY + ROWS - 1;
338 add_widget (learn_dlg,
339 label_new (UY + 14, 5,
340 _("Press all the keys mentioned here. After you have done it, check")));
341 add_widget (learn_dlg,
342 label_new (UY + 15, 5,
343 _("which keys are not marked with OK. Press space on the missing")));
344 add_widget (learn_dlg,
345 label_new (UY + 16, 5,
346 _("key, or click with the mouse to define it. Move around with Tab.")));
349 /* --------------------------------------------------------------------------------------------- */
351 static void
352 learn_done (void)
354 destroy_dlg (learn_dlg);
355 repaint_screen ();
358 /* --------------------------------------------------------------------------------------------- */
360 static void
361 learn_save (void)
363 int i;
364 int profile_changed = 0;
365 char *section = g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL);
366 char *esc_str;
368 for (i = 0; i < learn_total; i++)
370 if (learnkeys[i].sequence != NULL)
372 profile_changed = 1;
374 esc_str = strutils_escape (learnkeys[i].sequence, -1, ";\\", TRUE);
376 mc_config_set_string_raw_value (mc_main_config, section, key_name_conv_tab[i].name, esc_str);
378 g_free (esc_str);
382 /* On the one hand no good idea to save the complete setup but
383 * without 'Auto save setup' the new key-definitions will not be
384 * saved unless the user does an 'Options/Save Setup'.
385 * On the other hand a save-button that does not save anything to
386 * disk is much worse.
388 if (profile_changed)
389 mc_config_save_file (mc_main_config, NULL);
391 g_free (section);
394 /* --------------------------------------------------------------------------------------------- */
395 /*** public functions ****************************************************************************/
396 /* --------------------------------------------------------------------------------------------- */
398 void
399 learn_keys (void)
401 int save_old_esc_mode = old_esc_mode;
402 gboolean save_alternate_plus_minus = mc_global.tty.alternate_plus_minus;
404 /* old_esc_mode cannot work in learn keys dialog */
405 old_esc_mode = 0;
407 /* don't translate KP_ADD, KP_SUBTRACT and
408 KP_MULTIPLY to '+', '-' and '*' in
409 correct_key_code */
410 mc_global.tty.alternate_plus_minus = TRUE;
411 application_keypad_mode ();
412 init_learn ();
414 run_dlg (learn_dlg);
416 old_esc_mode = save_old_esc_mode;
417 mc_global.tty.alternate_plus_minus = save_alternate_plus_minus;
419 if (!mc_global.tty.alternate_plus_minus)
420 numeric_keypad_mode ();
422 switch (learn_dlg->ret_value)
424 case B_ENTER:
425 learn_save ();
426 break;
429 learn_done ();
432 /* --------------------------------------------------------------------------------------------- */