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

Annotation of src/usr.bin/tmux/cmd-choose-list.c, Revision 1.8

1.8     ! nicm        1: /* $Id: cmd-choose-list.c,v 1.7 2013/03/24 09:54:10 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2012 Thomas Adam <thomas@xteddy.org>
                      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 <string.h>
                     25:
                     26: #include "tmux.h"
                     27:
                     28: #define CMD_CHOOSE_LIST_DEFAULT_TEMPLATE "run-shell '%%'"
                     29:
                     30: /*
                     31:  * Enter choose mode to choose a custom list.
                     32:  */
                     33:
1.7       nicm       34: enum cmd_retval cmd_choose_list_exec(struct cmd *, struct cmd_q *);
1.1       nicm       35:
                     36: const struct cmd_entry cmd_choose_list_entry = {
                     37:        "choose-list", NULL,
                     38:        "l:t:", 0, 1,
                     39:        "[-l items] " CMD_TARGET_WINDOW_USAGE "[template]",
                     40:        0,
                     41:        NULL,
                     42:        cmd_choose_list_exec
                     43: };
                     44:
                     45: enum cmd_retval
1.7       nicm       46: cmd_choose_list_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       47: {
                     48:        struct args                     *args = self->args;
1.6       nicm       49:        struct client                   *c;
1.1       nicm       50:        struct winlink                  *wl;
1.2       nicm       51:        const char                      *list1;
                     52:        char                            *template, *item, *copy, *list;
1.1       nicm       53:        u_int                            idx;
                     54:
1.7       nicm       55:        if ((c = cmd_current_client(cmdq)) == NULL) {
                     56:                cmdq_error(cmdq, "no client available");
1.1       nicm       57:                return (CMD_RETURN_ERROR);
                     58:        }
                     59:
1.2       nicm       60:        if ((list1 = args_get(args, 'l')) == NULL)
1.1       nicm       61:                return (CMD_RETURN_ERROR);
                     62:
1.7       nicm       63:        if ((wl = cmd_find_window(cmdq, args_get(args, 't'), NULL)) == NULL)
1.1       nicm       64:                return (CMD_RETURN_ERROR);
                     65:
                     66:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                     67:                return (CMD_RETURN_NORMAL);
                     68:
                     69:        if (args->argc != 0)
                     70:                template = xstrdup(args->argv[0]);
                     71:        else
                     72:                template = xstrdup(CMD_CHOOSE_LIST_DEFAULT_TEMPLATE);
                     73:
1.2       nicm       74:        copy = list = xstrdup(list1);
1.1       nicm       75:        idx = 0;
1.2       nicm       76:        while ((item = strsep(&list, ",")) != NULL)
1.1       nicm       77:        {
1.2       nicm       78:                if (*item == '\0') /* no empty entries */
1.1       nicm       79:                        continue;
1.6       nicm       80:                window_choose_add_item(wl->window->active, c, wl, item,
1.1       nicm       81:                    template, idx);
                     82:                idx++;
                     83:        }
                     84:        free(copy);
1.3       nicm       85:
                     86:        if (idx == 0) {
                     87:                free(template);
                     88:                window_pane_reset_mode(wl->window->active);
                     89:                return (CMD_RETURN_ERROR);
                     90:        }
1.1       nicm       91:
1.5       nicm       92:        window_choose_ready(wl->window->active, 0, NULL);
1.1       nicm       93:
                     94:        free(template);
                     95:
                     96:        return (CMD_RETURN_NORMAL);
                     97: }