cmd-paste-buffer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 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 <stdlib.h>
  19. #include <string.h>
  20. #include "tmux.h"
  21. /*
  22. * Paste paste buffer if present.
  23. */
  24. enum cmd_retval cmd_paste_buffer_exec(struct cmd *, struct cmd_q *);
  25. void cmd_paste_buffer_filter(struct window_pane *,
  26. const char *, size_t, const char *, int);
  27. const struct cmd_entry cmd_paste_buffer_entry = {
  28. .name = "paste-buffer",
  29. .alias = "pasteb",
  30. .args = { "db:prs:t:", 0, 0 },
  31. .usage = "[-dpr] [-s separator] " CMD_BUFFER_USAGE " "
  32. CMD_TARGET_PANE_USAGE,
  33. .tflag = CMD_PANE,
  34. .flags = 0,
  35. .exec = cmd_paste_buffer_exec
  36. };
  37. enum cmd_retval
  38. cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
  39. {
  40. struct args *args = self->args;
  41. struct window_pane *wp = cmdq->state.tflag.wp;
  42. struct paste_buffer *pb;
  43. const char *sepstr, *bufname, *bufdata, *bufend, *line;
  44. size_t seplen, bufsize;
  45. int bracket = args_has(args, 'p');
  46. bufname = NULL;
  47. if (args_has(args, 'b'))
  48. bufname = args_get(args, 'b');
  49. if (bufname == NULL)
  50. pb = paste_get_top(NULL);
  51. else {
  52. pb = paste_get_name(bufname);
  53. if (pb == NULL) {
  54. cmdq_error(cmdq, "no buffer %s", bufname);
  55. return (CMD_RETURN_ERROR);
  56. }
  57. }
  58. if (pb != NULL && ~wp->flags & PANE_INPUTOFF) {
  59. sepstr = args_get(args, 's');
  60. if (sepstr == NULL) {
  61. if (args_has(args, 'r'))
  62. sepstr = "\n";
  63. else
  64. sepstr = "\r";
  65. }
  66. seplen = strlen(sepstr);
  67. if (bracket && (wp->screen->mode & MODE_BRACKETPASTE))
  68. bufferevent_write(wp->event, "\033[200~", 6);
  69. bufdata = paste_buffer_data(pb, &bufsize);
  70. bufend = bufdata + bufsize;
  71. for (;;) {
  72. line = memchr(bufdata, '\n', bufend - bufdata);
  73. if (line == NULL)
  74. break;
  75. bufferevent_write(wp->event, bufdata, line - bufdata);
  76. bufferevent_write(wp->event, sepstr, seplen);
  77. bufdata = line + 1;
  78. }
  79. if (bufdata != bufend)
  80. bufferevent_write(wp->event, bufdata, bufend - bufdata);
  81. if (bracket && (wp->screen->mode & MODE_BRACKETPASTE))
  82. bufferevent_write(wp->event, "\033[201~", 6);
  83. }
  84. if (pb != NULL && args_has(args, 'd'))
  85. paste_free(pb);
  86. return (CMD_RETURN_NORMAL);
  87. }