Add a windowonly generic command and use it where appropriate. Also trim includes and unused.

This commit is contained in:
Nicholas Marriott 2008-06-02 22:09:49 +00:00
parent 95cc21c251
commit 8731755ab4
18 changed files with 187 additions and 296 deletions

3
TODO
View File

@ -69,8 +69,7 @@
- poll(2) is broken on OS X/Darwin, a workaround for this would be nice
- different screen model? layers perhaps? hmm
- cfg file improvements: * comments to EOL
- index on *-mode and other stuff that targets a window but is currently
sessiononly
- select-window can become windowonly...
---
[18:20] *priteau* i found something in tmux that could be tweaked to be better

View File

@ -1,4 +1,4 @@
/* $Id: cmd-bind-key.c,v 1.10 2008-06-02 21:08:36 nicm Exp $ */
/* $Id: cmd-bind-key.c,v 1.11 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -90,7 +90,7 @@ error:
}
void
cmd_bind_key_exec(void *ptr, unused struct cmd_ctx *ctx)
cmd_bind_key_exec(void *ptr, struct cmd_ctx *ctx)
{
struct cmd_bind_key_data *data = ptr;

View File

@ -1,4 +1,4 @@
/* $Id: cmd-copy-mode.c,v 1.6 2008-06-02 21:36:51 nicm Exp $ */
/* $Id: cmd-copy-mode.c,v 1.7 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,9 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include <stdlib.h>
#include "tmux.h"
/*
@ -31,24 +28,24 @@ void cmd_copy_mode_exec(void *, struct cmd_ctx *);
const struct cmd_entry cmd_copy_mode_entry = {
"copy-mode", NULL,
CMD_SESSIONONLY_USAGE,
CMD_WINDOWONLY_USAGE,
0,
cmd_sessiononly_parse,
cmd_windowonly_parse,
cmd_copy_mode_exec,
cmd_sessiononly_send,
cmd_sessiononly_recv,
cmd_sessiononly_free
cmd_windowonly_send,
cmd_windowonly_recv,
cmd_windowonly_free
};
void
cmd_copy_mode_exec(unused void *ptr, struct cmd_ctx *ctx)
cmd_copy_mode_exec(void *ptr, struct cmd_ctx *ctx)
{
struct session *s;
struct winlink *wl;
if ((s = cmd_sessiononly_get(ptr, ctx)) == NULL)
if ((wl = cmd_windowonly_get(ptr, ctx, NULL)) == NULL)
return;
window_set_mode(s->curw->window, &window_copy_mode);
window_set_mode(wl->window, &window_copy_mode);
if (ctx->cmdclient != NULL)
server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);

View File

@ -1,4 +1,4 @@
/* $Id: cmd-detach-client.c,v 1.2 2008-06-02 18:08:16 nicm Exp $ */
/* $Id: cmd-detach-client.c,v 1.3 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,8 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include "tmux.h"
/*

View File

@ -1,4 +1,4 @@
/* $Id: cmd-generic.c,v 1.2 2008-06-02 21:08:36 nicm Exp $ */
/* $Id: cmd-generic.c,v 1.3 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
@ -19,6 +19,7 @@
#include <sys/types.h>
#include <getopt.h>
#include <stdlib.h>
#include "tmux.h"
@ -30,6 +31,11 @@ struct cmd_sessiononly_data {
char *sname;
};
struct cmd_windowonly_data {
char *sname;
int idx;
};
int
cmd_clientonly_parse(
struct cmd *self, void **ptr, int argc, char **argv, char **cause)
@ -173,3 +179,105 @@ cmd_sessiononly_get(void *ptr, struct cmd_ctx *ctx)
return (cmd_find_session(ctx, data->sname));
return (cmd_find_session(ctx, NULL));
}
int
cmd_windowonly_parse(
struct cmd *self, void **ptr, int argc, char **argv, char **cause)
{
struct cmd_windowonly_data *data;
int opt;
const char *errstr;
*ptr = data = xmalloc(sizeof *data);
data->sname = NULL;
data->idx = -1;
while ((opt = getopt(argc, argv, "i:s:")) != EOF) {
switch (opt) {
case 'i':
data->idx = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL) {
xasprintf(cause, "index %s", errstr);
goto error;
}
break;
case 's':
data->sname = xstrdup(optarg);
break;
default:
goto usage;
}
}
argc -= optind;
argv += optind;
if (argc != 0)
goto usage;
return (0);
usage:
xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
error:
self->entry->free(data);
return (-1);
}
void
cmd_windowonly_send(void *ptr, struct buffer *b)
{
struct cmd_windowonly_data *data = ptr;
buffer_write(b, data, sizeof *data);
cmd_send_string(b, data->sname);
}
void
cmd_windowonly_recv(void **ptr, struct buffer *b)
{
struct cmd_windowonly_data *data;
*ptr = data = xmalloc(sizeof *data);
buffer_read(b, data, sizeof *data);
data->sname = cmd_recv_string(b);
}
void
cmd_windowonly_free(void *ptr)
{
struct cmd_windowonly_data *data = ptr;
if (data->sname != NULL)
xfree(data->sname);
xfree(data);
}
struct winlink *
cmd_windowonly_get(void *ptr, struct cmd_ctx *ctx, struct session **sp)
{
struct cmd_windowonly_data *data = ptr;
struct session *s;
struct winlink *wl;
int idx;
if (data != NULL) {
s = cmd_find_session(ctx, data->sname);
idx = data->idx;
} else {
s = cmd_find_session(ctx, NULL);
idx = -1;
}
if (s == NULL)
return (NULL);
if (sp != NULL)
*sp = s;
if (idx == -1)
return (s->curw);
if ((wl = winlink_find_by_index(&s->windows, idx)) == NULL) {
ctx->error(ctx, "no window %d", idx);
return (NULL);
}
return (wl);
}

View File

@ -1,4 +1,4 @@
/* $Id: cmd-has-session.c,v 1.5 2008-06-02 21:08:36 nicm Exp $ */
/* $Id: cmd-has-session.c,v 1.6 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,9 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include <string.h>
#include "tmux.h"
/*

View File

@ -1,4 +1,4 @@
/* $Id: cmd-kill-session.c,v 1.6 2008-06-02 18:08:16 nicm Exp $ */
/* $Id: cmd-kill-session.c,v 1.7 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,9 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include <stdlib.h>
#include "tmux.h"
/*

View File

@ -1,4 +1,4 @@
/* $Id: cmd-kill-window.c,v 1.9 2008-06-02 21:08:36 nicm Exp $ */
/* $Id: cmd-kill-window.c,v 1.10 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,103 +18,37 @@
#include <sys/types.h>
#include <getopt.h>
#include <stdlib.h>
#include "tmux.h"
/*
* Destroy window.
*/
int cmd_kill_window_parse(struct cmd *, void **, int, char **, char **);
void cmd_kill_window_exec(void *, struct cmd_ctx *);
void cmd_kill_window_send(void *, struct buffer *);
void cmd_kill_window_recv(void **, struct buffer *);
void cmd_kill_window_free(void *);
struct cmd_kill_window_data {
char *sname;
int idx;
};
const struct cmd_entry cmd_kill_window_entry = {
"kill-window", "killw",
"[-i index] [-s session-name]",
CMD_WINDOWONLY_USAGE,
0,
cmd_kill_window_parse,
cmd_windowonly_parse,
cmd_kill_window_exec,
cmd_kill_window_send,
cmd_kill_window_recv,
cmd_kill_window_free
cmd_windowonly_send,
cmd_windowonly_recv,
cmd_windowonly_free
};
int
cmd_kill_window_parse(
struct cmd *self, void **ptr, int argc, char **argv, char **cause)
{
struct cmd_kill_window_data *data;
const char *errstr;
int opt;
*ptr = data = xmalloc(sizeof *data);
data->sname = NULL;
data->idx = -1;
while ((opt = getopt(argc, argv, "i:s:")) != EOF) {
switch (opt) {
case 'i':
data->idx = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL) {
xasprintf(cause, "index %s", errstr);
goto error;
}
break;
case 's':
data->sname = xstrdup(optarg);
break;
default:
goto usage;
}
}
argc -= optind;
argv += optind;
if (argc != 0)
goto usage;
return (0);
usage:
xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
error:
cmd_kill_window_free(data);
return (-1);
}
void
cmd_kill_window_exec(void *ptr, struct cmd_ctx *ctx)
{
struct cmd_kill_window_data *data = ptr, std = { NULL, -1 };
struct session *s;
struct client *c;
struct winlink *wl;
u_int i;
int destroyed;
struct winlink *wl;
struct session *s;
struct client *c;
u_int i;
int destroyed;
if (data == NULL)
data = &std;
if ((s = cmd_find_session(ctx, data->sname)) == NULL)
if ((wl = cmd_windowonly_get(ptr, ctx, &s)) == NULL)
return;
if (data->idx == -1)
wl = s->curw;
else if ((wl = winlink_find_by_index(&s->windows, data->idx)) == NULL) {
ctx->error(ctx, "no window %d", data->idx);
return;
}
destroyed = session_detach(s, wl);
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
c = ARRAY_ITEM(&clients, i);
@ -130,31 +64,3 @@ cmd_kill_window_exec(void *ptr, struct cmd_ctx *ctx)
if (ctx->cmdclient != NULL)
server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
}
void
cmd_kill_window_send(void *ptr, struct buffer *b)
{
struct cmd_kill_window_data *data = ptr;
buffer_write(b, data, sizeof *data);
cmd_send_string(b, data->sname);
}
void
cmd_kill_window_recv(void **ptr, struct buffer *b)
{
struct cmd_kill_window_data *data;
*ptr = data = xmalloc(sizeof *data);
buffer_read(b, data, sizeof *data);
}
void
cmd_kill_window_free(void *ptr)
{
struct cmd_kill_window_data *data = ptr;
if (data->sname != NULL)
xfree(data->sname);
xfree(data);
}

View File

@ -1,4 +1,4 @@
/* $Id: cmd-last-window.c,v 1.7 2008-06-02 18:08:16 nicm Exp $ */
/* $Id: cmd-last-window.c,v 1.8 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,9 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include <string.h>
#include "tmux.h"
/*

View File

@ -1,4 +1,4 @@
/* $Id: cmd-list-windows.c,v 1.16 2008-06-02 18:08:16 nicm Exp $ */
/* $Id: cmd-list-windows.c,v 1.17 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,7 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include <unistd.h>
#include "tmux.h"
@ -41,7 +40,7 @@ const struct cmd_entry cmd_list_windows_entry = {
};
void
cmd_list_windows_exec(unused void *ptr, struct cmd_ctx *ctx)
cmd_list_windows_exec(void *ptr, struct cmd_ctx *ctx)
{
struct session *s;
struct winlink *wl;

View File

@ -1,4 +1,4 @@
/* $Id: cmd-next-window.c,v 1.7 2008-06-02 18:08:16 nicm Exp $ */
/* $Id: cmd-next-window.c,v 1.8 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,9 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include <string.h>
#include "tmux.h"
/*

View File

@ -1,4 +1,4 @@
/* $Id: cmd-paste-buffer.c,v 1.4 2008-06-02 21:36:51 nicm Exp $ */
/* $Id: cmd-paste-buffer.c,v 1.5 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,8 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include "tmux.h"
@ -32,27 +30,27 @@ void cmd_paste_buffer_exec(void *, struct cmd_ctx *);
const struct cmd_entry cmd_paste_buffer_entry = {
"paste-buffer", "paste",
CMD_SESSIONONLY_USAGE,
CMD_WINDOWONLY_USAGE,
0,
cmd_sessiononly_parse,
cmd_windowonly_parse,
cmd_paste_buffer_exec,
cmd_sessiononly_send,
cmd_sessiononly_recv,
cmd_sessiononly_free
cmd_windowonly_send,
cmd_windowonly_recv,
cmd_windowonly_free
};
void
cmd_paste_buffer_exec(unused void *ptr, struct cmd_ctx *ctx)
cmd_paste_buffer_exec(void *ptr, struct cmd_ctx *ctx)
{
struct session *s;
struct window *w;
struct winlink *wl;
if ((s = cmd_sessiononly_get(ptr, ctx)) == NULL)
if ((wl = cmd_windowonly_get(ptr, ctx, NULL)) == NULL)
return;
w = s->curw->window;
if (paste_buffer != NULL && *paste_buffer != '\0')
buffer_write(w->out, paste_buffer, strlen(paste_buffer));
if (paste_buffer != NULL && *paste_buffer != '\0') {
buffer_write(
wl->window->out, paste_buffer, strlen(paste_buffer));
}
if (ctx->cmdclient != NULL)
server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);

View File

@ -1,4 +1,4 @@
/* $Id: cmd-previous-window.c,v 1.7 2008-06-02 18:08:16 nicm Exp $ */
/* $Id: cmd-previous-window.c,v 1.8 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,9 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include <string.h>
#include "tmux.h"
/*

View File

@ -1,4 +1,4 @@
/* $Id: cmd-refresh-client.c,v 1.2 2008-06-02 18:08:16 nicm Exp $ */
/* $Id: cmd-refresh-client.c,v 1.3 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,8 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include "tmux.h"
/*

View File

@ -1,4 +1,4 @@
/* $Id: cmd-scroll-mode.c,v 1.8 2008-06-02 21:36:51 nicm Exp $ */
/* $Id: cmd-scroll-mode.c,v 1.9 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,9 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include <stdlib.h>
#include "tmux.h"
/*
@ -31,24 +28,24 @@ void cmd_scroll_mode_exec(void *, struct cmd_ctx *);
const struct cmd_entry cmd_scroll_mode_entry = {
"scroll-mode", NULL,
CMD_SESSIONONLY_USAGE,
CMD_WINDOWONLY_USAGE,
0,
cmd_sessiononly_parse,
cmd_windowonly_parse,
cmd_scroll_mode_exec,
cmd_sessiononly_send,
cmd_sessiononly_recv,
cmd_sessiononly_free
cmd_windowonly_send,
cmd_windowonly_recv,
cmd_windowonly_free
};
void
cmd_scroll_mode_exec(void *ptr, struct cmd_ctx *ctx)
{
struct session *s;
struct winlink *wl;
if ((s = cmd_sessiononly_get(ptr, ctx)) == NULL)
if ((wl = cmd_windowonly_get(ptr, ctx, NULL)) == NULL)
return;
window_set_mode(s->curw->window, &window_scroll_mode);
window_set_mode(wl->window, &window_scroll_mode);
if (ctx->cmdclient != NULL)
server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);

View File

@ -1,4 +1,4 @@
/* $Id: cmd-send-prefix.c,v 1.8 2008-06-02 18:08:16 nicm Exp $ */
/* $Id: cmd-send-prefix.c,v 1.9 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,9 +18,6 @@
#include <sys/types.h>
#include <getopt.h>
#include <unistd.h>
#include "tmux.h"
/*

View File

@ -1,4 +1,4 @@
/* $Id: cmd-unlink-window.c,v 1.7 2008-06-02 21:08:36 nicm Exp $ */
/* $Id: cmd-unlink-window.c,v 1.8 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,108 +18,38 @@
#include <sys/types.h>
#include <getopt.h>
#include <stdlib.h>
#include "tmux.h"
/*
* Unlink a window, unless it would be destroyed by doing so (only one link).
*/
int cmd_unlink_window_parse(struct cmd *, void **, int, char **, char **);
void cmd_unlink_window_exec(void *, struct cmd_ctx *);
void cmd_unlink_window_send(void *, struct buffer *);
void cmd_unlink_window_recv(void **, struct buffer *);
void cmd_unlink_window_free(void *);
struct cmd_unlink_window_data {
char *sname;
int idx;
};
const struct cmd_entry cmd_unlink_window_entry = {
"unlink-window", "unlinkw",
"[-i index] [-s session-name]",
CMD_WINDOWONLY_USAGE,
0,
cmd_unlink_window_parse,
cmd_windowonly_parse,
cmd_unlink_window_exec,
cmd_unlink_window_send,
cmd_unlink_window_recv,
cmd_unlink_window_free
cmd_windowonly_send,
cmd_windowonly_recv,
cmd_windowonly_free
};
int
cmd_unlink_window_parse(
struct cmd *self, void **ptr, int argc, char **argv, char **cause)
{
struct cmd_unlink_window_data *data;
const char *errstr;
int opt;
*ptr = data = xmalloc(sizeof *data);
data->sname = NULL;
data->idx = -1;
while ((opt = getopt(argc, argv, "i:s:")) != EOF) {
switch (opt) {
case 'i':
data->idx = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL) {
xasprintf(cause, "index %s", errstr);
goto error;
}
break;
case 's':
data->sname = xstrdup(optarg);
break;
default:
goto usage;
}
}
argc -= optind;
argv += optind;
if (argc != 0)
goto usage;
return (0);
usage:
xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
error:
cmd_unlink_window_free(data);
return (-1);
}
void
cmd_unlink_window_exec(void *ptr, struct cmd_ctx *ctx)
{
struct cmd_unlink_window_data *data = ptr;
struct session *s;
struct client *c;
struct winlink *wl;
u_int i;
int destroyed;
struct winlink *wl;
struct session *s;
struct client *c;
u_int i;
int destroyed;
if (data == NULL)
if ((wl = cmd_windowonly_get(ptr, ctx, &s)) == NULL)
return;
if ((s = cmd_find_session(ctx, data->sname)) == NULL)
return;
if (data->idx < 0)
data->idx = -1;
if (data->idx == -1)
wl = s->curw;
else {
wl = winlink_find_by_index(&s->windows, data->idx);
if (wl == NULL) {
ctx->error(ctx, "no window %d", data->idx);
return;
}
}
if (wl->window->references == 1) {
ctx->error(ctx, "window is only linked to one session");
return;
@ -140,31 +70,3 @@ cmd_unlink_window_exec(void *ptr, struct cmd_ctx *ctx)
if (ctx->cmdclient != NULL)
server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
}
void
cmd_unlink_window_send(void *ptr, struct buffer *b)
{
struct cmd_unlink_window_data *data = ptr;
buffer_write(b, data, sizeof *data);
cmd_send_string(b, data->sname);
}
void
cmd_unlink_window_recv(void **ptr, struct buffer *b)
{
struct cmd_unlink_window_data *data;
*ptr = data = xmalloc(sizeof *data);
buffer_read(b, data, sizeof *data);
}
void
cmd_unlink_window_free(void *ptr)
{
struct cmd_unlink_window_data *data = ptr;
if (data->sname != NULL)
xfree(data->sname);
xfree(data);
}

9
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.119 2008-06-02 21:36:51 nicm Exp $ */
/* $Id: tmux.h,v 1.120 2008-06-02 22:09:49 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -780,6 +780,13 @@ void cmd_sessiononly_send(void *, struct buffer *);
void cmd_sessiononly_recv(void **, struct buffer *);
void cmd_sessiononly_free(void *);
struct session *cmd_sessiononly_get(void *, struct cmd_ctx *);
#define CMD_WINDOWONLY_USAGE "[-i index] [-s session-name]"
int cmd_windowonly_parse(struct cmd *, void **, int, char **, char **);
void cmd_windowonly_exec(void *, struct cmd_ctx *);
void cmd_windowonly_send(void *, struct buffer *);
void cmd_windowonly_recv(void **, struct buffer *);
void cmd_windowonly_free(void *);
struct winlink *cmd_windowonly_get(void *, struct cmd_ctx *, struct session **);
/* client.c */
int client_init(const char *, struct client_ctx *, int);