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

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