aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-01-06 22:10:37 +0100
committerMarc André Tanner <mat@brain-dump.org>2017-01-06 22:10:37 +0100
commit6d2d23ac62fe41dcac97b21985051cfc87e0362f (patch)
tree6225c105737f029c9293dc87d54b0b1cd13379fd
parent604c866bd0c034340efaea54ebbb86b6dc6e66f4 (diff)
downloadvis-6d2d23ac62fe41dcac97b21985051cfc87e0362f.tar.gz
vis-6d2d23ac62fe41dcac97b21985051cfc87e0362f.tar.xz
vis: simplify mode lookup for :map and :unmap
-rw-r--r--vis-cmds.c21
-rw-r--r--vis-modes.c12
-rw-r--r--vis.h1
3 files changed, 14 insertions, 20 deletions
diff --git a/vis-cmds.c b/vis-cmds.c
index a64e5f0..2f2ecf9 100644
--- a/vis-cmds.c
+++ b/vis-cmds.c
@@ -741,23 +741,6 @@ static bool cmd_help(Vis *vis, Win *win, Command *cmd, const char *argv[], Curso
return true;
}
-static enum VisMode str2vismode(const char *mode) {
- const char *modes[] = {
- [VIS_MODE_NORMAL] = "normal",
- [VIS_MODE_OPERATOR_PENDING] = "operator-pending",
- [VIS_MODE_VISUAL] = "visual",
- [VIS_MODE_VISUAL_LINE] = "visual-line",
- [VIS_MODE_INSERT] = "insert",
- [VIS_MODE_REPLACE] = "replace",
- };
-
- for (size_t i = 0; i < LENGTH(modes); i++) {
- if (mode && modes[i] && strcmp(mode, modes[i]) == 0)
- return i;
- }
- return VIS_MODE_INVALID;
-}
-
static bool cmd_langmap(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
const char *nonlatin = argv[1];
const char *latin = argv[2];
@@ -792,7 +775,7 @@ static bool cmd_langmap(Vis *vis, Win *win, Command *cmd, const char *argv[], Cu
static bool cmd_map(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
bool mapped = false;
bool local = strstr(argv[0], "-") != NULL;
- enum VisMode mode = str2vismode(argv[1]);
+ enum VisMode mode = vis_mode_from(vis, argv[1]);
if (local && !win)
return false;
@@ -820,7 +803,7 @@ err:
static bool cmd_unmap(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
bool local = strstr(argv[0], "-") != NULL;
- enum VisMode mode = str2vismode(argv[1]);
+ enum VisMode mode = vis_mode_from(vis, argv[1]);
const char *lhs = argv[2];
if (local && !win)
diff --git a/vis-modes.c b/vis-modes.c
index 1fe460d..c4f0027 100644
--- a/vis-modes.c
+++ b/vis-modes.c
@@ -1,4 +1,5 @@
#include <string.h>
+#include <strings.h>
#include "vis-core.h"
#include "util.h"
@@ -86,6 +87,15 @@ void vis_mode_switch(Vis *vis, enum VisMode mode) {
mode_set(vis, &vis_modes[mode]);
}
+enum VisMode vis_mode_from(Vis *vis, const char *name) {
+ for (size_t i = 0; i < LENGTH(vis_modes); i++) {
+ Mode *mode = &vis_modes[i];
+ if (!strcasecmp(mode->name, name))
+ return mode->id;
+ }
+ return VIS_MODE_INVALID;
+}
+
enum VisMode vis_mode_get(Vis *vis) {
return vis->mode->id;
}
@@ -241,7 +251,7 @@ Mode vis_modes[] = {
},
[VIS_MODE_VISUAL_LINE] = {
.id = VIS_MODE_VISUAL_LINE,
- .name = "VISUAL LINE",
+ .name = "VISUAL-LINE",
.parent = &vis_modes[VIS_MODE_VISUAL],
.status = "VISUAL-LINE",
.help = "",
diff --git a/vis.h b/vis.h
index 8f76405..7d909b5 100644
--- a/vis.h
+++ b/vis.h
@@ -169,6 +169,7 @@ enum VisMode {
void vis_mode_switch(Vis*, enum VisMode);
enum VisMode vis_mode_get(Vis*);
+enum VisMode vis_mode_from(Vis*, const char *name);
/* In the specified mode: map a given key to a binding (binding->key is ignored).
* Fails if a prefix of `key' is already mapped and `force' is false. Otherwise
* all such prefixes are unmapped. */