[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-readline] no way to make a new named keymap
From: |
Tom Tromey |
Subject: |
Re: [Bug-readline] no way to make a new named keymap |
Date: |
Tue, 11 Sep 2018 21:57:21 -0600 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.1.50 (gnu/linux) |
Chet> What do you think would be the best functional interface for that?
Chet> Something like rl_set_keymap_name (char *name, Keymap map)? Another
Chet> interface?
Here is a patch.
I wasn't sure if I should put the declaration in readline.h or keymap.h,
so I did both.
I dropped a "const" from keymap_names, but it could be re-added if you
don't mind casting away const.
I left the .info files out of the diff, since that seemed maybe more
convenient for you.
I wasn't sure if you'd rather rl_set_keymap_name copy the name or just
assume it will live long enough, so I chose copy.
I tested this by modifying rlbasic.c and editing my .inputrc. I can
send that stuff if you want it.
Tom
diff --git a/bind.c b/bind.c
index 2e05b46..ce3fc86 100644
--- a/bind.c
+++ b/bind.c
@@ -2255,10 +2255,13 @@ glean_key_from_name (char *name)
}
/* Auxiliary functions to manage keymaps. */
-static const struct {
- const char * const name;
+
+struct name_and_keymap {
+ const char *name;
Keymap map;
-} keymap_names[] = {
+};
+
+static struct name_and_keymap builtin_keymap_names[] = {
{ "emacs", emacs_standard_keymap },
{ "emacs-standard", emacs_standard_keymap },
{ "emacs-meta", emacs_meta_keymap },
@@ -2272,6 +2275,8 @@ static const struct {
{ (char *)0x0, (Keymap)0x0 }
};
+static struct name_and_keymap *keymap_names = builtin_keymap_names;
+
Keymap
rl_get_keymap_by_name (const char *name)
{
@@ -2283,6 +2288,33 @@ rl_get_keymap_by_name (const char *name)
return ((Keymap) NULL);
}
+void
+rl_set_keymap_name (const char *name, Keymap map)
+{
+ int i;
+ char *copy;
+
+ for (i = 0; keymap_names[i].name; i++)
+ ;
+
+ if (keymap_names == builtin_keymap_names)
+ {
+ keymap_names = xmalloc ((i + 2) * sizeof (struct name_and_keymap));
+ memcpy (keymap_names, builtin_keymap_names,
+ i * sizeof (struct name_and_keymap));
+ }
+ else
+ keymap_names = xrealloc (keymap_names,
+ (i + 2) * sizeof (struct name_and_keymap));
+
+ copy = xmalloc (strlen (name));
+ strcpy (copy, name);
+ keymap_names[i].name = copy;
+ keymap_names[i].map = map;
+ keymap_names[i + 1].name = NULL;
+ keymap_names[i + 1].map = NULL;
+}
+
char *
rl_get_keymap_name (Keymap map)
{
diff --git a/doc/rltech.texi b/doc/rltech.texi
index 7d22b2c..ad95d1f 100644
--- a/doc/rltech.texi
+++ b/doc/rltech.texi
@@ -720,6 +720,13 @@ Return the name matching @var{keymap}. @var{name} is one
which would
be supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}).
@end deftypefun
address@hidden void rl_set_keymap (const char *name, Keymap keymap)
+Set the name of @var{keymap}. This name will then be available for
+use in a @code{set keymap} inputrc line (@pxref{Readline Init File}).
+Readline will make a copy of @var{name}. There is no way to remove a
+named keymap, and a given name may only be registered once.
address@hidden deftypefun
+
@node Binding Keys
@subsection Binding Keys
diff --git a/doc/rluser.texi b/doc/rluser.texi
index 1c9acdc..6b14ef6 100644
--- a/doc/rluser.texi
+++ b/doc/rluser.texi
@@ -605,7 +605,7 @@ If this variable has not been given a value, the characters
@key{ESC} and
@item keymap
@vindex keymap
Sets Readline's idea of the current keymap for key binding commands.
-Acceptable @code{keymap} names are
+Built-in @code{keymap} names are
@code{emacs},
@code{emacs-standard},
@code{emacs-meta},
@@ -614,6 +614,7 @@ Acceptable @code{keymap} names are
@code{vi-move},
@code{vi-command}, and
@code{vi-insert}.
+However, an application may add additional names to this list.
@code{vi} is equivalent to @code{vi-command} (@code{vi-move} is also a
synonym); @code{emacs} is equivalent to @code{emacs-standard}.
The default value is @code{emacs}.
diff --git a/keymaps.h b/keymaps.h
index af8d5d9..d82b5ef 100644
--- a/keymaps.h
+++ b/keymaps.h
@@ -84,6 +84,11 @@ extern void rl_discard_keymap PARAMS((Keymap));
`emacs' or `emacs-meta' or `vi-insert'. */
extern Keymap rl_get_keymap_by_name PARAMS((const char *));
+/* Give a name to the keymap. Readline will make a copy of NAME.
+ There is no way to remove a named keymap, and a name may only be
+ registered once. */
+extern void rl_set_keymap_name PARAMS((const char *, Keymap));
+
/* Return the current keymap. */
extern Keymap rl_get_keymap PARAMS((void));
diff --git a/readline.h b/readline.h
index 4e08b2e..174c942 100644
--- a/readline.h
+++ b/readline.h
@@ -354,6 +354,7 @@ extern void rl_discard_keymap PARAMS((Keymap));
extern void rl_free_keymap PARAMS((Keymap));
extern Keymap rl_get_keymap_by_name PARAMS((const char *));
+extern void rl_set_keymap_name PARAMS((const char *, Keymap));
extern char *rl_get_keymap_name PARAMS((Keymap));
extern void rl_set_keymap PARAMS((Keymap));
extern Keymap rl_get_keymap PARAMS((void));