cmd-choose-buffer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <ctype.h>
  19. #include <stdlib.h>
  20. #include "tmux.h"
  21. /*
  22. * Enter choice mode to choose a buffer.
  23. */
  24. #define CHOOSE_BUFFER_TEMPLATE \
  25. "#{buffer_name}: #{buffer_size} bytes: #{buffer_sample}"
  26. enum cmd_retval cmd_choose_buffer_exec(struct cmd *, struct cmd_q *);
  27. const struct cmd_entry cmd_choose_buffer_entry = {
  28. .name = "choose-buffer",
  29. .alias = NULL,
  30. .args = { "F:t:", 0, 1 },
  31. .usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
  32. .tflag = CMD_WINDOW,
  33. .flags = 0,
  34. .exec = cmd_choose_buffer_exec
  35. };
  36. enum cmd_retval
  37. cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
  38. {
  39. struct args *args = self->args;
  40. struct client *c = cmdq->state.c;
  41. struct winlink *wl = cmdq->state.tflag.wl;
  42. struct window_choose_data *cdata;
  43. struct paste_buffer *pb;
  44. char *action, *action_data;
  45. const char *template;
  46. u_int idx;
  47. if (c == NULL) {
  48. cmdq_error(cmdq, "no client available");
  49. return (CMD_RETURN_ERROR);
  50. }
  51. if ((template = args_get(args, 'F')) == NULL)
  52. template = CHOOSE_BUFFER_TEMPLATE;
  53. if (paste_get_top(NULL) == NULL)
  54. return (CMD_RETURN_NORMAL);
  55. if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
  56. return (CMD_RETURN_NORMAL);
  57. if (args->argc != 0)
  58. action = xstrdup(args->argv[0]);
  59. else
  60. action = xstrdup("paste-buffer -b '%%'");
  61. idx = 0;
  62. pb = NULL;
  63. while ((pb = paste_walk(pb)) != NULL) {
  64. cdata = window_choose_data_create(TREE_OTHER, c, c->session);
  65. cdata->idx = idx;
  66. cdata->ft_template = xstrdup(template);
  67. format_defaults_paste_buffer(cdata->ft, pb);
  68. xasprintf(&action_data, "%s", paste_buffer_name(pb));
  69. cdata->command = cmd_template_replace(action, action_data, 1);
  70. free(action_data);
  71. window_choose_add(wl->window->active, cdata);
  72. idx++;
  73. }
  74. free(action);
  75. window_choose_ready(wl->window->active, 0, NULL);
  76. return (CMD_RETURN_NORMAL);
  77. }