46
46
#include <stdlib.h>
51
private void map_print_key(EditLine *, el_action_t *, const char *);
52
private void map_print_some_keys(EditLine *, el_action_t *, int, int);
49
private void map_print_key(EditLine *, el_action_t *, const Char *);
50
private void map_print_some_keys(EditLine *, el_action_t *, Int, Int);
53
51
private void map_print_all_keys(EditLine *);
54
52
private void map_init_nls(EditLine *);
55
53
private void map_init_meta(EditLine *);
904
902
EL_ABORT((el->errfile, "Vi insert map incorrect\n"));
907
el->el_map.alt = (el_action_t *)el_malloc(sizeof(el_action_t) * N_KEYS);
905
el->el_map.alt = el_malloc(sizeof(*el->el_map.alt) * N_KEYS);
908
906
if (el->el_map.alt == NULL)
910
el->el_map.key = (el_action_t *)el_malloc(sizeof(el_action_t) * N_KEYS);
908
el->el_map.key = el_malloc(sizeof(*el->el_map.key) * N_KEYS);
911
909
if (el->el_map.key == NULL)
913
911
el->el_map.emacs = el_map_emacs;
914
912
el->el_map.vic = el_map_vi_command;
915
913
el->el_map.vii = el_map_vi_insert;
916
el->el_map.help = (el_bindings_t *) el_malloc(sizeof(el_bindings_t) *
914
el->el_map.help = el_malloc(sizeof(*el->el_map.help) * EL_NUM_FCNS);
918
915
if (el->el_map.help == NULL)
920
917
(void) memcpy(el->el_map.help, help__get(),
921
sizeof(el_bindings_t) * EL_NUM_FCNS);
922
el->el_map.func = (el_func_t *)el_malloc(sizeof(el_func_t) *
918
sizeof(*el->el_map.help) * EL_NUM_FCNS);
919
el->el_map.func = el_malloc(sizeof(*el->el_map.func) * EL_NUM_FCNS);
924
920
if (el->el_map.func == NULL)
926
memcpy(el->el_map.func, func__get(), sizeof(el_func_t) * EL_NUM_FCNS);
922
memcpy(el->el_map.func, func__get(), sizeof(*el->el_map.func)
927
924
el->el_map.nfunc = EL_NUM_FCNS;
942
939
map_end(EditLine *el)
945
el_free((ptr_t) el->el_map.alt);
942
el_free(el->el_map.alt);
946
943
el->el_map.alt = NULL;
947
el_free((ptr_t) el->el_map.key);
944
el_free(el->el_map.key);
948
945
el->el_map.key = NULL;
949
946
el->el_map.emacs = NULL;
950
947
el->el_map.vic = NULL;
951
948
el->el_map.vii = NULL;
952
el_free((ptr_t) el->el_map.help);
949
el_free(el->el_map.help);
953
950
el->el_map.help = NULL;
954
el_free((ptr_t) el->el_map.func);
951
el_free(el->el_map.func);
955
952
el->el_map.func = NULL;
1008
1005
buf[1] = i & 0177;
1009
key_add(el, buf, key_map_cmd(el, (int) map[i]), XK_CMD);
1006
keymacro_add(el, buf, keymacro_map_cmd(el, (int) map[i]), XK_CMD);
1012
1009
map[(int) buf[0]] = ED_SEQUENCE_LEAD_IN;
1050
1047
map_init_emacs(EditLine *el)
1054
1051
el_action_t *key = el->el_map.key;
1055
1052
el_action_t *alt = el->el_map.alt;
1056
1053
const el_action_t *emacs = el->el_map.emacs;
1058
1055
el->el_map.type = MAP_EMACS;
1059
1056
el->el_map.current = el->el_map.key;
1062
1059
for (i = 0; i < N_KEYS; i++) {
1063
1060
key[i] = emacs[i];
1070
1067
buf[0] = CONTROL('X');
1071
1068
buf[1] = CONTROL('X');
1073
key_add(el, buf, key_map_cmd(el, EM_EXCHANGE_MARK), XK_CMD);
1070
keymacro_add(el, buf, keymacro_map_cmd(el, EM_EXCHANGE_MARK), XK_CMD);
1075
1072
tty_bind_char(el, 1);
1076
term_bind_arrow(el);
1073
terminal_bind_arrow(el);
1081
1078
* Set the editor
1084
map_set_editor(EditLine *el, char *editor)
1081
map_set_editor(EditLine *el, Char *editor)
1087
if (strcmp(editor, "emacs") == 0) {
1084
if (Strcmp(editor, STR("emacs")) == 0) {
1088
1085
map_init_emacs(el);
1091
if (strcmp(editor, "vi") == 0) {
1088
if (Strcmp(editor, STR("vi")) == 0) {
1092
1089
map_init_vi(el);
1121
1118
* Print the function description for 1 key
1124
map_print_key(EditLine *el, el_action_t *map, const char *in)
1121
map_print_key(EditLine *el, el_action_t *map, const Char *in)
1126
1123
char outbuf[EL_BUFSIZ];
1127
1124
el_bindings_t *bp, *ep;
1129
1126
if (in[0] == '\0' || in[1] == '\0') {
1130
(void) key__decode_str(in, outbuf, sizeof(outbuf), "");
1127
(void) keymacro__decode_str(in, outbuf, sizeof(outbuf), "");
1131
1128
ep = &el->el_map.help[el->el_map.nfunc];
1132
1129
for (bp = el->el_map.help; bp < ep; bp++)
1133
1130
if (bp->func == map[(unsigned char) *in]) {
1134
1131
(void) fprintf(el->el_outfile,
1135
"%s\t->\t%s\n", outbuf, bp->name);
1132
"%s\t->\t" FSTR "\n", outbuf, bp->name);
1136
keymacro_print(el, in);
1144
1141
* Print keys from first to last
1147
map_print_some_keys(EditLine *el, el_action_t *map, int first, int last)
1144
map_print_some_keys(EditLine *el, el_action_t *map, Int first, Int last)
1149
1146
el_bindings_t *bp, *ep;
1150
char firstbuf[2], lastbuf[2];
1147
Char firstbuf[2], lastbuf[2];
1151
1148
char unparsbuf[EL_BUFSIZ], extrabuf[EL_BUFSIZ];
1153
1150
firstbuf[0] = first;
1156
1153
lastbuf[1] = 0;
1157
1154
if (map[first] == ED_UNASSIGNED) {
1158
1155
if (first == last) {
1159
(void) key__decode_str(firstbuf, unparsbuf,
1156
(void) keymacro__decode_str(firstbuf, unparsbuf,
1160
1157
sizeof(unparsbuf), STRQQ);
1161
1158
(void) fprintf(el->el_outfile,
1162
1159
"%-15s-> is undefined\n", unparsbuf);
1167
1164
for (bp = el->el_map.help; bp < ep; bp++) {
1168
1165
if (bp->func == map[first]) {
1169
1166
if (first == last) {
1170
(void) key__decode_str(firstbuf, unparsbuf,
1167
(void) keymacro__decode_str(firstbuf, unparsbuf,
1171
1168
sizeof(unparsbuf), STRQQ);
1172
(void) fprintf(el->el_outfile, "%-15s-> %s\n",
1169
(void) fprintf(el->el_outfile, "%-15s-> " FSTR "\n",
1173
1170
unparsbuf, bp->name);
1175
(void) key__decode_str(firstbuf, unparsbuf,
1172
(void) keymacro__decode_str(firstbuf, unparsbuf,
1176
1173
sizeof(unparsbuf), STRQQ);
1177
(void) key__decode_str(lastbuf, extrabuf,
1174
(void) keymacro__decode_str(lastbuf, extrabuf,
1178
1175
sizeof(extrabuf), STRQQ);
1179
1176
(void) fprintf(el->el_outfile,
1180
"%-4s to %-7s-> %s\n",
1177
"%-4s to %-7s-> " FSTR "\n",
1181
1178
unparsbuf, extrabuf, bp->name);
1186
1183
#ifdef MAP_DEBUG
1187
1184
if (map == el->el_map.key) {
1188
(void) key__decode_str(firstbuf, unparsbuf,
1185
(void) keymacro__decode_str(firstbuf, unparsbuf,
1189
1186
sizeof(unparsbuf), STRQQ);
1190
1187
(void) fprintf(el->el_outfile,
1191
1188
"BUG!!! %s isn't bound to anything.\n", unparsbuf);
1192
1189
(void) fprintf(el->el_outfile, "el->el_map.key[%d] == %d\n",
1193
1190
first, el->el_map.key[first]);
1195
(void) key__decode_str(firstbuf, unparsbuf,
1192
(void) keymacro__decode_str(firstbuf, unparsbuf,
1196
1193
sizeof(unparsbuf), STRQQ);
1197
1194
(void) fprintf(el->el_outfile,
1198
1195
"BUG!!! %s isn't bound to anything.\n", unparsbuf);
1233
1230
map_print_some_keys(el, el->el_map.alt, prev, i - 1);
1235
1232
(void) fprintf(el->el_outfile, "Multi-character bindings\n");
1233
keymacro_print(el, STR(""));
1237
1234
(void) fprintf(el->el_outfile, "Arrow key bindings\n");
1238
term_print_arrow(el, "");
1235
terminal_print_arrow(el, STR(""));
1243
1240
* Add/remove/change bindings
1246
map_bind(EditLine *el, int argc, const char **argv)
1243
map_bind(EditLine *el, int argc, const Char **argv)
1248
1245
el_action_t *map;
1249
1246
int ntype, rem;
1251
char inbuf[EL_BUFSIZ];
1252
char outbuf[EL_BUFSIZ];
1253
const char *in = NULL;
1248
Char inbuf[EL_BUFSIZ];
1249
Char outbuf[EL_BUFSIZ];
1250
const Char *in = NULL;
1255
1252
el_bindings_t *bp, *ep;
1259
1256
if (argv == NULL)
1262
1259
map = el->el_map.key;
1263
1260
ntype = XK_CMD;
1289
1286
map_init_vi(el);
1293
1290
map_init_emacs(el);
1297
1294
ep = &el->el_map.help[el->el_map.nfunc];
1298
1295
for (bp = el->el_map.help; bp < ep; bp++)
1299
1296
(void) fprintf(el->el_outfile,
1297
"" FSTR "\n\t" FSTR "\n",
1301
1298
bp->name, bp->description);
1304
1301
(void) fprintf(el->el_errfile,
1305
"%s: Invalid switch `%c'.\n",
1302
"" FSTR ": Invalid switch `%c'.\n",
1306
1303
argv[0], p[1]);
1311
1308
if (argv[argc] == NULL) {
1312
1309
map_print_all_keys(el);
1316
1313
in = argv[argc++];
1317
1314
else if ((in = parse__string(inbuf, argv[argc++])) == NULL) {
1318
1315
(void) fprintf(el->el_errfile,
1319
"%s: Invalid \\ or ^ in instring.\n",
1316
"" FSTR ": Invalid \\ or ^ in instring.\n",
1325
(void) term_clear_arrow(el, in);
1322
(void) terminal_clear_arrow(el, in);
1329
(void) key_delete(el, in);
1326
(void) keymacro_delete(el, in);
1330
1327
else if (map[(unsigned char) *in] == ED_SEQUENCE_LEAD_IN)
1331
(void) key_delete(el, in);
1328
(void) keymacro_delete(el, in);
1333
1330
map[(unsigned char) *in] = ED_UNASSIGNED;
1336
1333
if (argv[argc] == NULL) {
1338
term_print_arrow(el, in);
1335
terminal_print_arrow(el, in);
1340
1337
map_print_key(el, map, in);
1344
1341
if (argv[argc + 1] != NULL) {
1342
bindkeymacro_usage();
1353
1350
if ((out = parse__string(outbuf, argv[argc])) == NULL) {
1354
1351
(void) fprintf(el->el_errfile,
1355
"%s: Invalid \\ or ^ in outstring.\n", argv[0]);
1352
"" FSTR ": Invalid \\ or ^ in outstring.\n", argv[0]);
1359
term_set_arrow(el, in, key_map_str(el, out), ntype);
1356
terminal_set_arrow(el, in, keymacro_map_str(el, out), ntype);
1361
key_add(el, in, key_map_str(el, out), ntype);
1358
keymacro_add(el, in, keymacro_map_str(el, out), ntype);
1362
1359
map[(unsigned char) *in] = ED_SEQUENCE_LEAD_IN;
1366
1363
if ((cmd = parse_cmd(el, argv[argc])) == -1) {
1367
1364
(void) fprintf(el->el_errfile,
1368
"%s: Invalid command `%s'.\n", argv[0], argv[argc]);
1365
"" FSTR ": Invalid command `" FSTR "'.\n",
1366
argv[0], argv[argc]);
1372
term_set_arrow(el, in, key_map_str(el, out), ntype);
1370
terminal_set_arrow(el, in, keymacro_map_str(el, out), ntype);
1375
key_add(el, in, key_map_cmd(el, cmd), ntype);
1373
keymacro_add(el, in, keymacro_map_cmd(el, cmd), ntype);
1376
1374
map[(unsigned char) *in] = ED_SEQUENCE_LEAD_IN;
1378
key_clear(el, map, in);
1379
map[(unsigned char) *in] = cmd;
1376
keymacro_clear(el, map, in);
1377
map[(unsigned char) *in] = (el_action_t)cmd;
1393
1391
* add a user defined function
1396
map_addfunc(EditLine *el, const char *name, const char *help, el_func_t func)
1394
map_addfunc(EditLine *el, const Char *name, const Char *help, el_func_t func)
1399
int nf = el->el_map.nfunc + 1;
1397
size_t nf = (size_t)el->el_map.nfunc + 1;
1401
1399
if (name == NULL || help == NULL || func == NULL)
1404
if ((p = el_realloc(el->el_map.func, nf * sizeof(el_func_t))) == NULL)
1406
el->el_map.func = (el_func_t *) p;
1407
if ((p = el_realloc(el->el_map.help, nf * sizeof(el_bindings_t)))
1402
if ((p = el_realloc(el->el_map.func, nf *
1403
sizeof(*el->el_map.func))) == NULL)
1405
el->el_map.func = p;
1406
if ((p = el_realloc(el->el_map.help, nf * sizeof(*el->el_map.help)))
1410
el->el_map.help = (el_bindings_t *) p;
1409
el->el_map.help = p;
1412
nf = el->el_map.nfunc;
1411
nf = (size_t)el->el_map.nfunc;
1413
1412
el->el_map.func[nf] = func;
1415
1414
el->el_map.help[nf].name = name;
1416
el->el_map.help[nf].func = nf;
1415
el->el_map.help[nf].func = (int)nf;
1417
1416
el->el_map.help[nf].description = help;
1418
1417
el->el_map.nfunc++;