Add support for custom command aliases, this is an array option which

contains items of the form "alias=command". This is consulted when an
unknown command is parsed.
pull/746/head
nicm 2017-01-24 19:53:37 +00:00
parent 61fce272ea
commit 85338bb75f
6 changed files with 91 additions and 47 deletions

View File

@ -252,16 +252,13 @@ client_main(struct event_base *base, int argc, char **argv, int flags,
* flag.
*/
cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause);
if (cmdlist == NULL) {
fprintf(stderr, "%s\n", cause);
return (1);
if (cmdlist != NULL) {
TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
if (cmd->entry->flags & CMD_STARTSERVER)
cmdflags |= CMD_STARTSERVER;
}
cmd_list_free(cmdlist);
}
cmdflags &= ~CMD_STARTSERVER;
TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
if (cmd->entry->flags & CMD_STARTSERVER)
cmdflags |= CMD_STARTSERVER;
}
cmd_list_free(cmdlist);
}
/* Create client process structure (starts logging). */

View File

@ -54,17 +54,15 @@ cmd_string_ungetc(size_t *p)
(*p)--;
}
struct cmd_list *
cmd_string_parse(const char *s, const char *file, u_int line, char **cause)
int
cmd_string_split(const char *s, int *rargc, char ***rargv)
{
size_t p = 0;
int ch, i, argc = 0;
char **argv = NULL, *buf = NULL, *t;
const char *whitespace, *equals;
size_t len = 0;
struct cmd_list *cmdlist = NULL;
size_t p = 0;
int ch, argc = 0, append = 0;
char **argv = NULL, *buf = NULL, *t;
const char *whitespace, *equals;
size_t len = 0;
*cause = NULL;
for (;;) {
ch = cmd_string_getc(s, &p);
switch (ch) {
@ -115,43 +113,67 @@ cmd_string_parse(const char *s, const char *file, u_int line, char **cause)
argc--;
memmove(argv, argv + 1, argc * (sizeof *argv));
}
if (argc == 0)
goto out;
cmdlist = cmd_list_parse(argc, argv, file, line, cause);
goto out;
goto done;
case '~':
if (buf == NULL) {
t = cmd_string_expand_tilde(s, &p);
if (t == NULL)
goto error;
cmd_string_copy(&buf, t, &len);
if (buf != NULL) {
append = 1;
break;
}
/* FALLTHROUGH */
default:
if (len >= SIZE_MAX - 2)
t = cmd_string_expand_tilde(s, &p);
if (t == NULL)
goto error;
buf = xrealloc(buf, len + 1);
buf[len++] = ch;
cmd_string_copy(&buf, t, &len);
break;
default:
append = 1;
break;
}
if (append) {
if (len >= SIZE_MAX - 2)
goto error;
buf = xrealloc(buf, len + 1);
buf[len++] = ch;
}
append = 0;
}
done:
*rargc = argc;
*rargv = argv;
free(buf);
return (0);
error:
if (argv != NULL)
cmd_free_argv(argc, argv);
free(buf);
return (-1);
}
struct cmd_list *
cmd_string_parse(const char *s, const char *file, u_int line, char **cause)
{
struct cmd_list *cmdlist = NULL;
int argc;
char **argv;
*cause = NULL;
if (cmd_string_split(s, &argc, &argv) != 0)
goto error;
if (argc != 0) {
cmdlist = cmd_list_parse(argc, argv, file, line, cause);
if (cmdlist == NULL) {
cmd_free_argv(argc, argv);
goto error;
}
}
cmd_free_argv(argc, argv);
return (cmdlist);
error:
xasprintf(cause, "invalid or unknown command: %s", s);
out:
free(buf);
if (argv != NULL) {
for (i = 0; i < argc; i++)
free(argv[i]);
free(argv);
}
return (cmdlist);
return (NULL);
}
static void

View File

@ -383,7 +383,7 @@ key_bindings_init(void)
for (i = 0; i < nitems(defaults); i++) {
cmdlist = cmd_string_parse(defaults[i], "<default>", i, &cause);
if (cmdlist == NULL)
fatalx("bad default key");
fatalx("bad default key: %s", defaults[i]);
cmdq_append(NULL, cmdq_get_command(cmdlist, NULL, NULL, 0));
cmd_list_free(cmdlist);
}

2
pty.c
View File

@ -45,7 +45,7 @@ pty_fork(int ptmfd, int *fd, char *name, size_t namelen, struct winsize *ws)
struct ptmget ptm;
pid_t pid;
if ((ioctl(ptmfd, PTMGET, &ptm) == -1))
if (ioctl(ptmfd, PTMGET, &ptm) == -1)
return (-1);
strlcpy(name, ptm.sn, namelen);

24
tmux.1
View File

@ -2410,6 +2410,30 @@ Available server options are:
Set the number of buffers; as new buffers are added to the top of the stack,
old ones are removed from the bottom if necessary to maintain this maximum
length.
.It Xo Ic command-alias[]
.Ar name=value
.Xc
This is an array of custom aliases for commands.
If an unknown command matches
.Ar name ,
it is replaced with
.Ar value .
For example, after:
.Pp
.Dl set -s command-alias[2] zoom='resize-pane -Z'
.Pp
Using:
.Pp
.Dl zoom -t:.1
.Pp
Is equivalent to:
.Pp
.Dl resize-pane -Z -t:.1
.Pp
Note that aliases are expanded when a command is parsed rather than when it is
executed, so binding an alias with
.Ic bind-key
will bind the expanded form.
.It Ic default-terminal Ar terminal
Set the default terminal for new windows created in this session - the
default value of the

1
tmux.h
View File

@ -1828,6 +1828,7 @@ void printflike(2, 3) cmdq_print(struct cmdq_item *, const char *, ...);
void printflike(2, 3) cmdq_error(struct cmdq_item *, const char *, ...);
/* cmd-string.c */
int cmd_string_split(const char *, int *, char ***);
struct cmd_list *cmd_string_parse(const char *, const char *, u_int, char **);
/* cmd-wait-for.c */