tex: represent each keymap with its index in kmaps[] - neatvi - [fork] simple vi-type editor with UTF-8 support
(HTM) git clone git://src.adamsgaard.dk/neatvi
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit 9e76a7c658e3ea68a1d5de81cb4f1cf1a0f4f6d9
(DIR) parent 53f1da76a6d4918209eecc5251437407e8c7cf41
(HTM) Author: Ali Gholami Rudi <ali@rudi.ir>
Date: Thu, 4 May 2017 18:08:14 +0430
ex: represent each keymap with its index in kmaps[]
Diffstat:
M conf.c | 11 ++++++++---
M ex.c | 20 +++++---------------
M led.c | 22 +++++++++++-----------
M vi.c | 14 +++++++-------
M vi.h | 13 +++++++------
5 files changed, 38 insertions(+), 42 deletions(-)
---
(DIR) diff --git a/conf.c b/conf.c
t@@ -75,13 +75,18 @@ int conf_highlight_revdir(int *att)
return 0;
}
-char **conf_kmap(char *name)
+char **conf_kmap(int id)
+{
+ return kmaps[id];
+}
+
+int conf_kmapfind(char *name)
{
int i;
for (i = 0; i < LEN(kmaps); i++)
if (name && kmaps[i][0] && !strcmp(name, kmaps[i][0]))
- return kmaps[i];
- return kmap_en;
+ return i;
+ return 0;
}
char *conf_digraph(int c1, int c2)
(DIR) diff --git a/ex.c b/ex.c
t@@ -19,11 +19,11 @@ int xled = 1; /* use the line editor */
int xtd = +1; /* current text direction */
int xshape = 1; /* perform letter shaping */
int xorder = 1; /* change the order of characters */
+int xkmap = 0; /* the current keymap */
+int xkmap_alt = 1; /* the alternate keymap */
static char xkwd[EXLEN]; /* the last searched keyword */
static char xrep[EXLEN]; /* the last replacement */
static int xkwddir; /* the last search direction */
-static char *xkmap = "en"; /* the current keymap */
-static char xkmap2[8] = "fa"; /* the alternate keymap */
static struct buf {
char ft[32];
t@@ -111,16 +111,6 @@ char *ex_filetype(void)
return xhl ? bufs[0].ft : "";
}
-char **ex_kmap(void)
-{
- return &xkmap;
-}
-
-char *ex_kmapalt(void)
-{
- return xkmap2;
-}
-
/* read ex command location */
static char *ex_loc(char *s, char *loc)
{
t@@ -784,11 +774,11 @@ static int ec_cmap(char *ec)
ex_cmd(ec, cmd);
ex_arg(ec, arg);
if (arg[0])
- snprintf(xkmap2, sizeof(xkmap2), arg);
+ xkmap_alt = conf_kmapfind(arg);
else
- ex_print(xkmap);
+ ex_print(conf_kmap(xkmap)[0]);
if (arg[0] && !strchr(cmd, '!'))
- xkmap = xkmap2;
+ xkmap = xkmap_alt;
return 0;
}
(DIR) diff --git a/led.c b/led.c
t@@ -6,7 +6,7 @@
#include <unistd.h>
#include "vi.h"
-static char *kmap_map(char *kmap, int c)
+static char *kmap_map(int kmap, int c)
{
static char cs[4];
char **keymap = conf_kmap(kmap);
t@@ -160,7 +160,7 @@ static int led_lastword(char *s)
return r - s;
}
-static void led_printparts(char *ai, char *pref, char *main, char *post, char *kmap)
+static void led_printparts(char *ai, char *pref, char *main, char *post, int kmap)
{
struct sbuf *ln;
int off, pos;
t@@ -193,7 +193,7 @@ static void led_printparts(char *ai, char *pref, char *main, char *post, char *k
}
/* continue reading the character starting with c */
-static char *led_readchar(int c, char *kmap)
+static char *led_readchar(int c, int kmap)
{
static char buf[8];
int c1, c2;
t@@ -224,16 +224,16 @@ static char *led_readchar(int c, char *kmap)
}
/* read a character from the terminal */
-char *led_read(char **kmap)
+char *led_read(int *kmap)
{
int c = term_read();
while (!TK_INT(c)) {
switch (c) {
case TK_CTL('f'):
- *kmap = ex_kmapalt();
+ *kmap = xkmap_alt;
break;
case TK_CTL('e'):
- *kmap = "en";
+ *kmap = 0;
break;
default:
return led_readchar(c, *kmap);
t@@ -244,7 +244,7 @@ char *led_read(char **kmap)
}
/* read a line from the terminal */
-static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, char **kmap)
+static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, int *kmap)
{
struct sbuf *sb;
int ai_len = strlen(ai);
t@@ -260,10 +260,10 @@ static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, ch
c = term_read();
switch (c) {
case TK_CTL('f'):
- *kmap = ex_kmapalt();
+ *kmap = xkmap_alt;
continue;
case TK_CTL('e'):
- *kmap = "en";
+ *kmap = 0;
continue;
case TK_CTL('h'):
case 127:
t@@ -304,7 +304,7 @@ static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, ch
}
/* read an ex command */
-char *led_prompt(char *pref, char *post, char **kmap)
+char *led_prompt(char *pref, char *post, int *kmap)
{
int key;
int td = td_set(+2);
t@@ -325,7 +325,7 @@ char *led_prompt(char *pref, char *post, char **kmap)
}
/* read visual command input */
-char *led_input(char *pref, char *post, char **kmap)
+char *led_input(char *pref, char *post, int *kmap)
{
struct sbuf *sb = sbuf_make();
char ai[128];
(DIR) diff --git a/vi.c b/vi.c
t@@ -118,10 +118,10 @@ static void vi_back(int c)
static char *vi_char(void)
{
- return led_read(ex_kmap());
+ return led_read(&xkmap);
}
-static char *vi_prompt(char *msg, char **kmap)
+static char *vi_prompt(char *msg, int *kmap)
{
char *r, *s;
term_pos(xrows, led_pos(msg, 0));
t@@ -140,7 +140,7 @@ char *ex_read(char *msg)
struct sbuf *sb;
char c;
if (xled) {
- char *s = led_prompt(msg, "", ex_kmap());
+ char *s = led_prompt(msg, "", &xkmap);
if (s)
term_chr('\n');
return s;
t@@ -257,7 +257,7 @@ static int vi_search(int cmd, int cnt, int *row, int *off)
if (cmd == '/' || cmd == '?') {
char sign[4] = {cmd};
struct sbuf *sb;
- char *kw = vi_prompt(sign, ex_kmap());
+ char *kw = vi_prompt(sign, &xkmap);
char *re;
if (!kw)
return 1;
t@@ -655,7 +655,7 @@ static int charcount(char *text, char *post)
static char *vi_input(char *pref, char *post, int *row, int *off)
{
- char *rep = led_input(pref, post, ex_kmap());
+ char *rep = led_input(pref, post, &xkmap);
if (!rep)
return NULL;
*row = linecount(rep) - 1;
t@@ -737,7 +737,7 @@ static void vi_pipe(int r1, int r2)
{
char *text;
char *rep;
- char *kmap = NULL;
+ int kmap = 0;
char *cmd = vi_prompt("!", &kmap);
if (!cmd)
return;
t@@ -1042,7 +1042,7 @@ static void vi(void)
int xcol;
int mark;
char *ln;
- char *kmap = NULL;
+ int kmap = 0;
xtop = MAX(0, xrow - xrows / 2);
xoff = 0;
xcol = vi_off2col(xb, xrow, xoff);
(DIR) diff --git a/vi.h b/vi.h
t@@ -129,9 +129,9 @@ char *term_cmd(int *n);
#define TK_ESC (TK_CTL('['))
/* line-oriented input and output */
-char *led_prompt(char *pref, char *post, char **kmap);
-char *led_input(char *pref, char *post, char **kmap);
-char *led_read(char **kmap);
+char *led_prompt(char *pref, char *post, int *kmap);
+char *led_input(char *pref, char *post, int *kmap);
+char *led_read(int *kmap);
void led_print(char *msg, int row);
void led_printmsg(char *s, int row);
int led_pos(char *s, int pos);
t@@ -146,8 +146,6 @@ int ex_init(char **files);
void ex_done(void);
char *ex_path(void);
char *ex_filetype(void);
-char **ex_kmap(void);
-char *ex_kmapalt(void);
struct lbuf *ex_lbuf(void);
int ex_kwd(char **kwd, int *dir);
void ex_kwdset(char *kwd, int dir);
t@@ -185,7 +183,8 @@ int conf_placeholder(int idx, char **s, char **d, int *wid);
int conf_highlight(int idx, char **ft, int **att, char **pat, int *end);
int conf_filetype(int idx, char **ft, char **pat);
int conf_highlight_revdir(int *att);
-char **conf_kmap(char *name);
+char **conf_kmap(int id);
+int conf_kmapfind(char *name);
char *conf_digraph(int c1, int c2);
/* global variables */
t@@ -202,3 +201,5 @@ extern int xtd;
extern int xshape;
extern int xorder;
extern int xhl;
+extern int xkmap;
+extern int xkmap_alt;