[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.7

1.7     ! nicm        1: /* $OpenBSD: cmd-choose-buffer.c,v 1.6 2012/06/25 14:08:55 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:
1.6       nicm       31: void   cmd_choose_buffer_callback(struct window_choose_data *);
                     32: void   cmd_choose_buffer_free(struct window_choose_data *);
1.1       nicm       33:
                     34: const struct cmd_entry cmd_choose_buffer_entry = {
                     35:        "choose-buffer", NULL,
1.5       nicm       36:        "F:t:", 0, 1,
                     37:        CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
1.4       nicm       38:        0,
                     39:        NULL,
                     40:        NULL,
                     41:        cmd_choose_buffer_exec
1.1       nicm       42: };
                     43:
                     44: int
                     45: cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
                     46: {
1.4       nicm       47:        struct args                     *args = self->args;
1.6       nicm       48:        struct window_choose_data       *cdata;
1.1       nicm       49:        struct winlink                  *wl;
                     50:        struct paste_buffer             *pb;
1.7     ! nicm       51:        char                            *action, *action_data;
        !            52:        const char                      *template;
1.1       nicm       53:        u_int                            idx;
                     54:
                     55:        if (ctx->curclient == NULL) {
                     56:                ctx->error(ctx, "must be run interactively");
                     57:                return (-1);
                     58:        }
                     59:
1.5       nicm       60:        if ((template = args_get(args, 'F')) == NULL)
                     61:                template = DEFAULT_BUFFER_LIST_TEMPLATE;
                     62:
1.4       nicm       63:        if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
1.1       nicm       64:                return (-1);
                     65:
1.2       nicm       66:        if (paste_get_top(&global_buffers) == NULL)
1.1       nicm       67:                return (0);
                     68:
                     69:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                     70:                return (0);
                     71:
1.7     ! nicm       72:        if (args->argc != 0)
        !            73:                action = xstrdup(args->argv[0]);
        !            74:        else
        !            75:                action = xstrdup("paste-buffer -b '%%'");
        !            76:
1.1       nicm       77:        idx = 0;
1.2       nicm       78:        while ((pb = paste_walk_stack(&global_buffers, &idx)) != NULL) {
1.6       nicm       79:                cdata = window_choose_data_create(ctx);
                     80:                cdata->idx = idx - 1;
                     81:                cdata->client->references++;
                     82:
                     83:                cdata->ft_template = xstrdup(template);
                     84:                format_add(cdata->ft, "line", "%u", idx - 1);
                     85:                format_paste_buffer(cdata->ft, pb);
1.5       nicm       86:
1.7     ! nicm       87:                xasprintf(&action_data, "%u", idx - 1);
        !            88:                cdata->command = cmd_template_replace(action, action_data, 1);
        !            89:                xfree(action_data);
        !            90:
1.6       nicm       91:                window_choose_add(wl->window->active, cdata);
1.1       nicm       92:        }
1.7     ! nicm       93:        xfree(action);
1.1       nicm       94:
                     95:        window_choose_ready(wl->window->active,
1.6       nicm       96:            0, cmd_choose_buffer_callback, cmd_choose_buffer_free);
1.1       nicm       97:
                     98:        return (0);
                     99: }
                    100:
                    101: void
1.6       nicm      102: cmd_choose_buffer_callback(struct window_choose_data *cdata)
1.1       nicm      103: {
1.6       nicm      104:        if (cdata == NULL)
1.1       nicm      105:                return;
                    106:        if (cdata->client->flags & CLIENT_DEAD)
                    107:                return;
                    108:
1.6       nicm      109:        window_choose_ctx(cdata);
1.1       nicm      110: }
                    111:
                    112: void
1.6       nicm      113: cmd_choose_buffer_free(struct window_choose_data *data)
1.1       nicm      114: {
1.6       nicm      115:        struct window_choose_data       *cdata = data;
                    116:
                    117:        if (cdata == NULL)
                    118:                return;
1.1       nicm      119:
                    120:        cdata->client->references--;
1.6       nicm      121:
1.7     ! nicm      122:        xfree(cdata->command);
1.6       nicm      123:        xfree(cdata->ft_template);
1.1       nicm      124:        xfree(cdata);
                    125: }