Browse Source

* usermap.c: Allow user-defined keyboard bindings for mcedit.
* usermap.h: Likewise.
* Makefile.am: Likewise.
* editcmd.c: Define new editor commands which had been coded
inline before.
This change also affects some other files.
* edit-widget.h: Invented a new type edit_key_map_type instead of
using long for.
* editkeys.c: Using that type.

Roland Illig 19 years ago
parent
commit
073907ec5f
10 changed files with 152 additions and 54 deletions
  1. 15 0
      edit/ChangeLog
  2. 1 1
      edit/Makefile.am
  3. 13 0
      edit/edit-widget.h
  4. 20 1
      edit/edit.c
  5. 6 0
      edit/edit.h
  6. 48 0
      edit/editcmd.c
  7. 10 0
      edit/editcmddef.h
  8. 33 50
      edit/editkeys.c
  9. 1 0
      edit/editmenu.c
  10. 5 2
      edit/editoptions.c

+ 15 - 0
edit/ChangeLog

@@ -1,3 +1,18 @@
+2005-07-20  Vitja Makarov  <vitja.makarov@gmail.com>
+
+	* usermap.c: Allow user-defined keyboard bindings for mcedit.
+	* usermap.h: Likewise.
+	* Makefile.am: Likewise.
+	* editcmd.c: Define new editor commands which had been coded
+	inline before.
+	This change also affects some other files.
+
+2005-07-20  Roland Illig  <roland.illig@gmx.de>
+
+	* edit-widget.h: Invented a new type edit_key_map_type instead of
+	using long for.
+	* editkeys.c: Using that type.
+
 2005-07-18  Roland Illig  <roland.illig@gmx.de>
 
 	* editkeys.c: When typing literal characters after C-q, allow

+ 1 - 1
edit/Makefile.am

@@ -9,6 +9,6 @@ endif
 libedit_a_SOURCES = \
 	bookmark.c edit.c editcmd.c editwidget.c editdraw.c editkeys.c \
 	editmenu.c editoptions.c editcmddef.h edit.h edit-widget.h \
-	editlock.c editlock.h syntax.c wordproc.c
+	editlock.c editlock.h syntax.c usermap.c wordproc.c
 
 EXTRA_DIST = ChangeLog

+ 13 - 0
edit/edit-widget.h

@@ -25,6 +25,11 @@ struct syntax_rule {
     unsigned char border;
 };
 
+typedef struct edit_key_map_type {
+    long key;
+    long command;
+} edit_key_map_type;
+
 struct WEdit {
     Widget widget;
 
@@ -102,6 +107,14 @@ struct WEdit {
     int macro_i;		/* index to macro[], -1 if not recording a macro */
     int macro_depth;		/* depth of the macro recursion */
     struct macro macro[MAX_MACRO_LENGTH];
+
+    /* user map stuff */
+    const edit_key_map_type *user_map;
+    const edit_key_map_type *ext_map;
+
+    int extmod;
+
+    char *labels[10];
 };
 
 #endif

+ 20 - 1
edit/edit.c

@@ -38,11 +38,13 @@
 #include "editlock.h"
 #include "edit-widget.h"
 #include "editcmddef.h"
+#include "usermap.h"
 
 #include "../src/cmd.h"		/* view_other_cmd() */
 #include "../src/user.h"	/* user_menu_cmd() */
 #include "../src/wtools.h"	/* query_dialog() */
 
+
 /*
    what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
    or EDIT_KEY_EMULATION_EMACS
@@ -558,6 +560,8 @@ edit_init (WEdit *edit, int lines, int columns, const char *filename,
 	edit_move_to_line (edit, line - 1);
     }
 
+    edit_load_user_map(edit);
+
     return edit;
 }
 
@@ -2112,7 +2116,7 @@ void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
 	edit->macro[edit->macro_i++].ch = char_for_insertion;
     }
 /* record the beginning of a set of editing actions initiated by a key press */
-    if (command != CK_Undo)
+    if (command != CK_Undo && command != CK_Ext_Mode)
 	edit_push_key_press (edit);
 
     edit_execute_cmd (edit, command, char_for_insertion);
@@ -2549,6 +2553,21 @@ edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
     case CK_Shell:
 	view_other_cmd ();
 	break;
+    case CK_Select_Codepage:
+	edit_select_codepage_cmd (edit);
+	break;
+    case CK_Insert_Literal:
+	edit_insert_literal_cmd (edit);
+	break;
+    case CK_Execute_Macro:
+	edit_execute_macro_cmd (edit);
+	break;
+    case CK_Begin_End_Macro:
+	edit_begin_end_macro_cmd (edit);
+	break;
+    case CK_Ext_Mode:
+	edit->extmod = 1;
+	break;
     default:
 	break;
     }

+ 6 - 0
edit/edit.h

@@ -35,6 +35,7 @@
 
 #define EDIT_KEY_EMULATION_NORMAL 0
 #define EDIT_KEY_EMULATION_EMACS  1
+#define EDIT_KEY_EMULATION_USER   2
 
 #define REDRAW_LINE          (1 << 0)
 #define REDRAW_LINE_ABOVE    (1 << 1)
@@ -198,6 +199,11 @@ int edit_copy_to_X_buf_cmd (WEdit * edit);
 int edit_cut_to_X_buf_cmd (WEdit * edit);
 void edit_paste_from_X_buf_cmd (WEdit * edit);
 
+void edit_select_codepage_cmd (WEdit *edit);
+void edit_insert_literal_cmd (WEdit *edit);
+void edit_execute_macro_cmd (WEdit *edit);
+void edit_begin_end_macro_cmd(WEdit *edit);
+
 void edit_paste_from_history (WEdit *edit);
 
 void edit_set_filename (WEdit *edit, const char *name);

+ 48 - 0
edit/editcmd.c

@@ -53,6 +53,7 @@
 #include "../src/dialog.h"	/* do_refresh() */
 #include "../src/wtools.h"	/* message() */
 #include "../src/charsets.h"
+#include "../src/selcodepage.h"
 
 #define edit_get_load_file(f,h) input_expand_dialog (h, _(" Enter file name: "), f)
 #define edit_get_save_file(f,h) input_expand_dialog (h, _(" Enter file name: "), f)
@@ -2894,3 +2895,50 @@ edit_complete_word_cmd (WEdit *edit)
     /* restore search parameters */
     edit_set_search_parameters (old_rs, old_rb, old_rr, old_rw, old_rc);
 }
+
+void
+edit_select_codepage_cmd (WEdit *edit)
+{
+#ifdef HAVE_CHARSET
+    do_select_codepage ();
+    edit->force = REDRAW_COMPLETELY;
+    edit_refresh_cmd (edit);
+#endif
+}
+
+void
+edit_insert_literal_cmd (WEdit *edit)
+{
+    int char_for_insertion =
+	    edit_raw_key_query (_(" Insert Literal "),
+				_(" Press any key: "), 0);
+    edit_execute_key_command (edit, -1,
+	ascii_alpha_to_cntrl (char_for_insertion));
+}
+
+void
+edit_execute_macro_cmd (WEdit *edit)
+{
+    int command =
+	    CK_Macro (edit_raw_key_query
+		      (_(" Execute Macro "), _(" Press macro hotkey: "),
+		       1));
+    if (command == CK_Macro (0))
+        command = CK_Insert_Char;
+
+    edit_execute_key_command (edit, command, -1);
+}
+
+void
+edit_begin_end_macro_cmd(WEdit *edit)
+{
+    int command;
+    
+    /* edit is a pointer to the widget */
+    if (edit) {
+	    command =
+		edit->macro_i <
+		0 ? CK_Begin_Record_Macro : CK_End_Record_Macro;
+	    edit_execute_key_command (edit, command, -1);
+    }
+}

+ 10 - 0
edit/editcmddef.h

@@ -148,6 +148,16 @@
 
 #define CK_Shell		801
 
+
+/* C-x or similar */
+#define CK_Ext_Mode		820
+
+
+#define CK_Select_Codepage	850
+#define CK_Insert_Literal	851
+#define CK_Execute_Macro	852
+#define CK_Begin_End_Macro	853
+
 /*
    Process a block through a shell command: CK_Pipe_Block(i) executes shell_cmd[i].
    shell_cmd[i] must process the file ~/cooledit.block and output ~/cooledit.block

+ 33 - 50
edit/editkeys.c

@@ -22,6 +22,7 @@
 
 #include <config.h>
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <sys/types.h>
@@ -47,7 +48,7 @@
  * Ordinary translations.  Note that the keys listed first take priority
  * when the key is assigned to more than one command.
  */
-static const long cooledit_key_map[] = {
+static const edit_key_map_type cooledit_key_map[] = {
     ALT ('b'), CK_Match_Bracket,
     ALT ('m'), CK_Mail,
     XCTRL ('f'), CK_Save_Block,
@@ -59,7 +60,7 @@ static const long cooledit_key_map[] = {
     0, 0
 };
 
-static long const emacs_key_map[] = {
+static const edit_key_map_type emacs_key_map[] = {
     ALT ('$'), CK_Pipe_Block (1),	/* spell check */
     ALT ('b'), CK_Word_Left,
     ALT ('f'), CK_Word_Right,
@@ -80,7 +81,7 @@ static long const emacs_key_map[] = {
     0, 0
 };
 
-static long const common_key_map[] = {
+static const edit_key_map_type common_key_map[] = {
     '\n', CK_Enter,
     '\t', CK_Tab,
 
@@ -112,6 +113,10 @@ static long const common_key_map[] = {
     XCTRL ('l'), CK_Refresh,
     XCTRL ('o'), CK_Shell,
     XCTRL ('u'), CK_Undo,
+    XCTRL ('t'), CK_Select_Codepage,
+    XCTRL ('q'), CK_Insert_Literal,
+    XCTRL ('a'), CK_Execute_Macro,
+    XCTRL ('r'), CK_Begin_End_Macro,
 
     KEY_F (1), CK_Help,
     KEY_F (2), CK_Save,
@@ -181,7 +186,8 @@ edit_translate_key (WEdit *edit, long x_key, int *cmd, int *ch)
     int command = CK_Insert_Char;
     int char_for_insertion = -1;
     int i = 0;
-    static const long *key_map;
+    int extmod = 0;
+    const edit_key_map_type *key_map = NULL;
 
     switch (edit_key_emulation) {
     case EDIT_KEY_EMULATION_NORMAL:
@@ -215,46 +221,25 @@ edit_translate_key (WEdit *edit, long x_key, int *cmd, int *ch)
 	    goto fin;
 	}
 	break;
-    }
-
-#ifdef HAVE_CHARSET
-    if (x_key == XCTRL ('t')) {
-	do_select_codepage ();
 
-	edit->force = REDRAW_COMPLETELY;
-	command = CK_Refresh;
-	goto fin;
-    }
-#endif
-
-    if (x_key == XCTRL ('q')) {
-	char_for_insertion = ascii_alpha_to_cntrl (
-	    edit_raw_key_query (_(" Insert Literal "),
-				_(" Press any key: "), 0));
-	goto fin;
-    }
-    if (x_key == XCTRL ('a')
-	&& edit_key_emulation != EDIT_KEY_EMULATION_EMACS) {
-	command =
-	    CK_Macro (edit_raw_key_query
-		      (_(" Execute Macro "), _(" Press macro hotkey: "),
-		       1));
-	if (command == CK_Macro (0))
-	    command = CK_Insert_Char;
-	goto fin;
-    }
-    /* edit is a pointer to the widget */
-    if (edit)
-	if (x_key == XCTRL ('r')) {
-	    command =
-		edit->macro_i <
-		0 ? CK_Begin_Record_Macro : CK_End_Record_Macro;
-	    goto fin;
+    case EDIT_KEY_EMULATION_USER:
+	if (edit->user_map != NULL) {
+	    if (edit->extmod && edit->ext_map != NULL) {
+		key_map = edit->ext_map;
+		extmod = 1;
+	    } else {
+		key_map = edit->user_map;
+	    }
+	    edit->extmod = 0;
+	} else {
+	    key_map = edit->user_map = cooledit_key_map;
 	}
-
+	break;
+    }
+    assert (key_map != NULL);
 
     /* an ordinary insertable character */
-    if (x_key < 256) {
+    if (x_key < 256 && !extmod) {
 	int c = convert_from_input_c (x_key);
 
 	if (is_printable (c)) {
@@ -264,21 +249,19 @@ edit_translate_key (WEdit *edit, long x_key, int *cmd, int *ch)
     }
 
     /* Commands specific to the key emulation */
-    i = 0;
-    while (key_map[i] && (key_map[i] != x_key))
-	i += 2;
-    if (key_map[i]) {
-	command = key_map[i + 1];
+    for (i = 0; key_map[i].key != 0 && key_map[i].key != x_key; i++)
+	continue;
+    if (key_map[i].key != 0) {
+	command = key_map[i].command;
 	goto fin;
     }
 
     /* Commands common for the key emulations */
     key_map = common_key_map;
-    i = 0;
-    while (key_map[i] && (key_map[i] != x_key))
-	i += 2;
-    if (key_map[i]) {
-	command = key_map[i + 1];
+    for (i = 0; key_map[i].key != 0 && key_map[i].key != x_key; i++)
+	continue;
+    if (key_map[i].key != 0) {
+	command = key_map[i].command;
 	goto fin;
     }
 

+ 1 - 0
edit/editmenu.c

@@ -443,6 +443,7 @@ edit_init_menu (void)
     struct Menu **EditMenuBar = g_new(struct Menu *, N_menus);
 
     switch (edit_key_emulation) {
+    default:
     case EDIT_KEY_EMULATION_NORMAL:
 	edit_init_menu_normal (EditMenuBar);
 	break;

+ 5 - 2
edit/editoptions.c

@@ -38,6 +38,7 @@
 #include "../src/global.h"
 
 #include "edit.h"
+#include "usermap.h"
 #include "../src/dialog.h"	/* B_CANCEL */
 #include "../src/wtools.h"	/* QuickDialog */
 
@@ -49,7 +50,7 @@
 #endif
 
 static const char *key_emu_str[] =
-{N_("Intuitive"), N_("Emacs"), NULL};
+{N_("Intuitive"), N_("Emacs"), N_("User-defined"), NULL};
 
 static const char *wrap_str[] =
 {N_("None"), N_("Dynamic paragraphing"), N_("Type writer wrap"), NULL};
@@ -128,7 +129,7 @@ edit_options_dialog (void)
 	 N_("Wrap mode"), 0, 0,
 	 0, 0, NULL},
 	/* 15 */
-	{quick_radio, 5, OPT_DLG_W, OPT_DLG_H - 13, OPT_DLG_H, "", 2, 0, 0,
+	{quick_radio, 5, OPT_DLG_W, OPT_DLG_H - 13, OPT_DLG_H, "", 3, 0, 0,
 	 const_cast(char **, key_emu_str), "keyemu"},
 	/* 16 */
 	{quick_label, 4, OPT_DLG_W, OPT_DLG_H - 14, OPT_DLG_H,
@@ -221,4 +222,6 @@ edit_options_dialog (void)
     /* Load or unload syntax rules if the option has changed */
     if (option_syntax_highlighting != old_syntax_hl)
 	edit_load_syntax (wedit, 0, 0);
+    /* Load usermap if it's needed */
+    edit_load_user_map (wedit);
 }

Some files were not shown because too many files changed in this diff