Annotation of embedaddon/tmux/cmd-choose-buffer.c, revision 1.1.1.1

1.1       misho       1: /* $OpenBSD$ */
                      2: 
                      3: /*
                      4:  * Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
                      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: #include <stdlib.h>
                     23: 
                     24: #include "tmux.h"
                     25: 
                     26: /*
                     27:  * Enter choice mode to choose a buffer.
                     28:  */
                     29: 
                     30: #define CHOOSE_BUFFER_TEMPLATE                                         \
                     31:        "#{buffer_name}: #{buffer_size} bytes: #{buffer_sample}"
                     32: 
                     33: static enum cmd_retval cmd_choose_buffer_exec(struct cmd *,
                     34:                            struct cmdq_item *);
                     35: 
                     36: const struct cmd_entry cmd_choose_buffer_entry = {
                     37:        .name = "choose-buffer",
                     38:        .alias = NULL,
                     39: 
                     40:        .args = { "F:t:", 0, 1 },
                     41:        .usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
                     42: 
                     43:        .tflag = CMD_WINDOW,
                     44: 
                     45:        .flags = 0,
                     46:        .exec = cmd_choose_buffer_exec
                     47: };
                     48: 
                     49: static enum cmd_retval
                     50: cmd_choose_buffer_exec(struct cmd *self, struct cmdq_item *item)
                     51: {
                     52:        struct args                     *args = self->args;
                     53:        struct client                   *c = item->state.c;
                     54:        struct winlink                  *wl = item->state.tflag.wl;
                     55:        struct window_choose_data       *cdata;
                     56:        struct paste_buffer             *pb;
                     57:        char                            *action, *action_data;
                     58:        const char                      *template;
                     59:        u_int                            idx;
                     60: 
                     61:        if (c == NULL) {
                     62:                cmdq_error(item, "no client available");
                     63:                return (CMD_RETURN_ERROR);
                     64:        }
                     65: 
                     66:        if ((template = args_get(args, 'F')) == NULL)
                     67:                template = CHOOSE_BUFFER_TEMPLATE;
                     68: 
                     69:        if (paste_get_top(NULL) == NULL)
                     70:                return (CMD_RETURN_NORMAL);
                     71: 
                     72:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                     73:                return (CMD_RETURN_NORMAL);
                     74: 
                     75:        if (args->argc != 0)
                     76:                action = xstrdup(args->argv[0]);
                     77:        else
                     78:                action = xstrdup("paste-buffer -b '%%'");
                     79: 
                     80:        idx = 0;
                     81:        pb = NULL;
                     82:        while ((pb = paste_walk(pb)) != NULL) {
                     83:                cdata = window_choose_data_create(TREE_OTHER, c, c->session);
                     84:                cdata->idx = idx;
                     85: 
                     86:                cdata->ft_template = xstrdup(template);
                     87:                format_defaults_paste_buffer(cdata->ft, pb);
                     88: 
                     89:                xasprintf(&action_data, "%s", paste_buffer_name(pb));
                     90:                cdata->command = cmd_template_replace(action, action_data, 1);
                     91:                free(action_data);
                     92: 
                     93:                window_choose_add(wl->window->active, cdata);
                     94:                idx++;
                     95:        }
                     96:        free(action);
                     97: 
                     98:        window_choose_ready(wl->window->active, 0, NULL);
                     99: 
                    100:        return (CMD_RETURN_NORMAL);
                    101: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>