[BACK]Return to cmd-choose-buffer.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-choose-buffer.c, Revision 1.3

1.3     ! nicm        1: /* $OpenBSD: cmd-choose-buffer.c,v 1.2 2010/12/30 23:16:18 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2010 Nicholas Marriott <nicm@users.sourceforge.net>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
                     21: #include <ctype.h>
                     22:
                     23: #include "tmux.h"
                     24:
                     25: /*
                     26:  * Enter choice mode to choose a buffer.
                     27:  */
                     28:
                     29: int    cmd_choose_buffer_exec(struct cmd *, struct cmd_ctx *);
                     30:
                     31: void   cmd_choose_buffer_callback(void *, int);
                     32: void   cmd_choose_buffer_free(void *);
                     33:
                     34: const struct cmd_entry cmd_choose_buffer_entry = {
                     35:        "choose-buffer", NULL,
                     36:        CMD_TARGET_WINDOW_USAGE " [template]",
                     37:        CMD_ARG01, "",
                     38:        cmd_target_init,
                     39:        cmd_target_parse,
                     40:        cmd_choose_buffer_exec,
                     41:        cmd_target_free,
                     42:        cmd_target_print
                     43: };
                     44:
                     45: struct cmd_choose_buffer_data {
                     46:        struct client   *client;
                     47:        char            *template;
                     48: };
                     49:
                     50: int
                     51: cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
                     52: {
                     53:        struct cmd_target_data          *data = self->data;
                     54:        struct cmd_choose_buffer_data   *cdata;
                     55:        struct winlink                  *wl;
                     56:        struct paste_buffer             *pb;
                     57:        u_int                            idx;
                     58:        char                            *tmp;
                     59:
                     60:        if (ctx->curclient == NULL) {
                     61:                ctx->error(ctx, "must be run interactively");
                     62:                return (-1);
                     63:        }
                     64:
                     65:        if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
                     66:                return (-1);
                     67:
1.2       nicm       68:        if (paste_get_top(&global_buffers) == NULL)
1.1       nicm       69:                return (0);
                     70:
                     71:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                     72:                return (0);
                     73:
                     74:        idx = 0;
1.2       nicm       75:        while ((pb = paste_walk_stack(&global_buffers, &idx)) != NULL) {
1.1       nicm       76:                tmp = paste_print(pb, 50);
                     77:                window_choose_add(wl->window->active, idx - 1,
                     78:                    "%u: %zu bytes: \"%s\"", idx - 1, pb->size, tmp);
                     79:                xfree(tmp);
                     80:        }
                     81:
                     82:        cdata = xmalloc(sizeof *cdata);
                     83:        if (data->arg != NULL)
                     84:                cdata->template = xstrdup(data->arg);
                     85:        else
                     86:                cdata->template = xstrdup("paste-buffer -b '%%'");
                     87:        cdata->client = ctx->curclient;
                     88:        cdata->client->references++;
                     89:
                     90:        window_choose_ready(wl->window->active,
                     91:            0, cmd_choose_buffer_callback, cmd_choose_buffer_free, cdata);
                     92:
                     93:        return (0);
                     94: }
                     95:
                     96: void
                     97: cmd_choose_buffer_callback(void *data, int idx)
                     98: {
                     99:        struct cmd_choose_buffer_data   *cdata = data;
                    100:        struct cmd_list                 *cmdlist;
                    101:        struct cmd_ctx                   ctx;
                    102:        char                            *template, *cause, tmp[16];
                    103:
                    104:        if (idx == -1)
                    105:                return;
                    106:        if (cdata->client->flags & CLIENT_DEAD)
                    107:                return;
                    108:
                    109:        xsnprintf(tmp, sizeof tmp, "%u", idx);
                    110:        template = cmd_template_replace(cdata->template, tmp, 1);
                    111:
                    112:        if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
                    113:                if (cause != NULL) {
                    114:                        *cause = toupper((u_char) *cause);
                    115:                        status_message_set(cdata->client, "%s", cause);
                    116:                        xfree(cause);
                    117:                }
                    118:                xfree(template);
                    119:                return;
                    120:        }
                    121:        xfree(template);
                    122:
                    123:        ctx.msgdata = NULL;
                    124:        ctx.curclient = cdata->client;
                    125:
                    126:        ctx.error = key_bindings_error;
                    127:        ctx.print = key_bindings_print;
                    128:        ctx.info = key_bindings_info;
                    129:
                    130:        ctx.cmdclient = NULL;
                    131:
                    132:        cmd_list_exec(cmdlist, &ctx);
                    133:        cmd_list_free(cmdlist);
                    134: }
                    135:
                    136: void
                    137: cmd_choose_buffer_free(void *data)
                    138: {
                    139:        struct cmd_choose_buffer_data   *cdata = data;
                    140:
                    141:        cdata->client->references--;
                    142:        xfree(cdata->template);
                    143:        xfree(cdata);
                    144: }