Skip to content

Commit

Permalink
support for primary clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremybobbin authored and martanne committed Jul 17, 2020
1 parent f34f6d9 commit 9cb69f7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
4 changes: 4 additions & 0 deletions man/vis-clipboard.1
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
.Pp
.Nm vis-clipboard
.Fl -copy
.Op Fl -selection Ar selection
.Pp
.Nm vis-clipboard
.Fl -paste
.Op Fl -selection Ar selection
.
.Sh DESCRIPTION
.Nm vis-clipboard
Expand Down Expand Up @@ -50,6 +52,8 @@ In this mode,
.Nm vis-clipboard
reads the content of the system clipboard,
and writes it to standard output.
.It Fl -selection Ar selection
specify which selection to use, options are "primary" or "clipboard"
.El
.
.Sh ENVIRONMENT
Expand Down
44 changes: 31 additions & 13 deletions vis-clipboard
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ vc_fatal() {
}

vc_usage() {
vc_fatal "`basename $0` [--usable|--copy|--paste]"
vc_fatal "`basename $0` [--selection sel] [--usable|--copy|--paste]"
}

vc_determine_command() {
Expand All @@ -30,6 +30,10 @@ vc_determine_command() {
done
fi

if [ "$sel" = "primary" ]; then
vc_fatal "clipboard primary selection is not supported on your platform"
fi

if type pbcopy >/dev/null 2>&1; then
echo 'mac'
return 0
Expand Down Expand Up @@ -76,27 +80,35 @@ vc_paste() {
}

vc_wlclipboard_copy() {
wl-copy -t TEXT
if [ "$sel" = "primary" ]; then
wl-copy --primary -t TEXT
else
wl-copy -t TEXT
fi
}

vc_wlclipboard_paste() {
wl-paste -t text
if [ "$sel" = "primary" ]; then
wl-paste --primary -t text
else
wl-paste -t text
fi
}

vc_xsel_copy() {
xsel -bi
xsel --"$sel" -bi
}

vc_xsel_paste() {
xsel -bo
xsel --"$sel" -bo
}

vc_xclip_copy() {
xclip -selection clipboard -i >/dev/null 2>&1
xclip -selection "$sel" -i >/dev/null 2>&1
}

vc_xclip_paste() {
xclip -selection clipboard -o
xclip -selection "$sel" -o
}

vc_mac_copy() {
Expand All @@ -115,11 +127,17 @@ vc_cygwin_paste() {
cat /dev/clipboard
}

case "$1" in
--usable) vc_usable;;
--copy) vc_copy;;
--paste) vc_paste;;
*) ;;
esac
while [ $# -gt 0 ]; do
case "$1" in
--usable) fn=vc_usable;;
--copy) fn=vc_copy;;
--paste) fn=vc_paste;;
--selection) shift; sel="$1";;
*) ;;
esac
shift
done

sel=${sel:-"clipboard"} $fn

vc_usage
23 changes: 17 additions & 6 deletions vis-registers.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,21 @@ const char *register_slot_get(Vis *vis, Register *reg, size_t slot, size_t *len)
case REGISTER_CLIPBOARD:
{
Buffer buferr;
enum VisRegister id = reg - vis->registers;
const char *cmd[] = { VIS_CLIPBOARD, "--paste", "--selection", NULL, NULL };
buffer_init(&buferr);
Buffer *buf = array_get(&reg->values, slot);
if (!buf)
return NULL;
buffer_clear(buf);

if (id == VIS_REG_PRIMARY)
cmd[3] = "primary";
else
cmd[3] = "clipboard";
int status = vis_pipe(vis, vis->win->file,
&(Filerange){ .start = 0, .end = 0 },
(const char*[]){ VIS_CLIPBOARD, "--paste", NULL },
buf, read_buffer, &buferr, read_buffer);
cmd, buf, read_buffer, &buferr, read_buffer);

if (status != 0)
vis_info_show(vis, "Command failed %s", buffer_content0(&buferr));
Expand Down Expand Up @@ -152,11 +157,17 @@ bool register_slot_put_range(Vis *vis, Register *reg, size_t slot, Text *txt, Fi
case REGISTER_CLIPBOARD:
{
Buffer buferr;
const char *cmd[] = { VIS_CLIPBOARD, "--copy", "--selection", NULL, NULL };
enum VisRegister id = reg - vis->registers;
buffer_init(&buferr);

if (id == VIS_REG_PRIMARY)
cmd[3] = "primary";
else
cmd[3] = "clipboard";

int status = vis_pipe(vis, vis->win->file, range,
(const char*[]){ VIS_CLIPBOARD, "--copy", NULL },
NULL, NULL, &buferr, read_buffer);
cmd, NULL, NULL, &buferr, read_buffer);

if (status != 0)
vis_info_show(vis, "Command failed %s", buffer_content0(&buferr));
Expand Down Expand Up @@ -187,7 +198,6 @@ bool register_resize(Register *reg, size_t count) {

enum VisRegister vis_register_from(Vis *vis, char reg) {
switch (reg) {
case '+': return VIS_REG_CLIPBOARD;
case '@': return VIS_MACRO_LAST_RECORDED;
}

Expand Down Expand Up @@ -275,7 +285,8 @@ const RegisterDef vis_registers[] = {
[VIS_REG_9] = { '9', VIS_HELP("9th sub-expression match") },
[VIS_REG_AMPERSAND] = { '&', VIS_HELP("Last regex match") },
[VIS_REG_BLACKHOLE] = { '_', VIS_HELP("/dev/null register") },
[VIS_REG_CLIPBOARD] = { '*', VIS_HELP("System clipboard register, see vis-clipboard(1)") },
[VIS_REG_PRIMARY] = { '*', VIS_HELP("Primary clipboard register, see vis-clipboard(1)") },
[VIS_REG_CLIPBOARD] = { '+', VIS_HELP("System clipboard register, see vis-clipboard(1)") },
[VIS_REG_DOT] = { '.', VIS_HELP("Last inserted text") },
[VIS_REG_SEARCH] = { '/', VIS_HELP("Last search pattern") },
[VIS_REG_COMMAND] = { ':', VIS_HELP("Last :-command") },
Expand Down
1 change: 1 addition & 0 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ Vis *vis_new(Ui *ui, VisEvent *event) {
register_init(&vis->registers[i]);
vis->registers[VIS_REG_BLACKHOLE].type = REGISTER_BLACKHOLE;
vis->registers[VIS_REG_CLIPBOARD].type = REGISTER_CLIPBOARD;
vis->registers[VIS_REG_PRIMARY].type = REGISTER_CLIPBOARD;
vis->registers[VIS_REG_NUMBER].type = REGISTER_NUMBER;
array_init(&vis->operators);
array_init(&vis->motions);
Expand Down
1 change: 1 addition & 0 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ enum VisRegister {
VIS_REG_9,
VIS_REG_BLACKHOLE, /* /dev/null register */
VIS_REG_CLIPBOARD, /* system clipboard register */
VIS_REG_PRIMARY, /* system primary clipboard register */
VIS_REG_DOT, /* last inserted text, copy of VIS_MACRO_OPERATOR */
VIS_REG_SEARCH, /* last used search pattern "/ */
VIS_REG_COMMAND, /* last used :-command ": */
Expand Down

0 comments on commit 9cb69f7

Please sign in to comment.