From 837aa12f9f5ecdab76f53f66db9f0ec05cd36a5e Mon Sep 17 00:00:00 2001
From: "Kenneth Benzie (Benie)" <benie@infektor.net>
Date: Wed, 3 Mar 2021 20:54:56 +0000
Subject: [PATCH] Replace osc52yank.vim with autoload script

---
 after/ftplugin/help.vim |  2 +-
 autoload/osc52.vim      | 27 +++++++++++++++++++++++++++
 plugin/mappings.vim     | 10 ++++++++--
 vimrc                   |  6 ------
 4 files changed, 36 insertions(+), 9 deletions(-)
 create mode 100644 autoload/osc52.vim

diff --git a/after/ftplugin/help.vim b/after/ftplugin/help.vim
index ccd86d5..90ca3e6 100644
--- a/after/ftplugin/help.vim
+++ b/after/ftplugin/help.vim
@@ -5,4 +5,4 @@ setlocal nospell
 setlocal scrolloff=0
 
 " Don't display line numbers
-setlocal nonumber norelativenumber
+setlocal nonumber norelativenumber signcolumn=no
diff --git a/autoload/osc52.vim b/autoload/osc52.vim
new file mode 100644
index 0000000..50c1c61
--- /dev/null
+++ b/autoload/osc52.vim
@@ -0,0 +1,27 @@
+" Use the OSC 52 escape sequence to copy text to the local system clipboard
+" when connected to a remote machine.
+
+" Add an autocmd to run after text is yanked.
+function! osc52#autocmd()
+  augroup osc52
+    autocmd!
+    autocmd TextYankPost * call osc52#copy()
+  augroup END
+endfunction
+
+function! osc52#copy()
+  " Only use OSC 52 when text is yanked with the z register.
+  if v:event['regname'] ==# 'z'
+    " Get the register contents and join the list into a string.
+    let l:content = join(v:event['regcontents'], "\n")
+    if v:event['regtype'] ==# 'V'
+      " Append a new line in linewise-visual mode to avoid surprises.
+      let l:content = l:content."\n"
+    endif
+    " Get the parent tty while being compatible with vim and neovim, originally
+    " from https://github.com/greymd/oscyank.vim
+    let l:tty = system('(tty || tty < /proc/$PPID/fd/0) 2> /dev/null | grep /dev/')
+    " Base 64 encode the content, and print OSC 52 escape sequence to the tty.
+    call system('base64 | xargs -0 printf "\\033]52;c;%s\\a" > '.l:tty, l:content)
+  endif
+endfunction
diff --git a/plugin/mappings.vim b/plugin/mappings.vim
index 8d6b467..05e8726 100644
--- a/plugin/mappings.vim
+++ b/plugin/mappings.vim
@@ -95,8 +95,14 @@ nnoremap <leader>tm :tabmove<Space>
 " Clear search highlights
 nnoremap <leader><Space> :nohlsearch<CR>
 
-if exists('loaded_oscyank')
-  noremap <leader>y :Oscyank<CR>
+if tmux#isOption('set-clipboard', 'on') || $SSH_CONNECTION !=# ''
+  " When connected to a remote session the + selection register is not
+  " available and the unnamed register is used instead. Add mappings using the
+  " z register instead.
+  noremap <leader>y "zy
+  noremap <leader>Y "zY
+  " Enable OSC 52 copy on yank.
+  call osc52#autocmd()
 else
   " System clipboard yank/put
   noremap <leader>y "+y
diff --git a/vimrc b/vimrc
index 8e72f14..bd718d1 100644
--- a/vimrc
+++ b/vimrc
@@ -183,9 +183,3 @@ Pack 'mustache/vim-mustache-handlebars'
 Pack 'joshglendenning/vim-caddyfile'
 Pack 'kbenzie/vim-khr'
 Pack 'jrozner/vim-antlr'
-
-" Plugins for remote integrations
-Pack 'greymd/oscyank.vim', {'type': 'opt'}
-if tmux#isOption('set-clipboard', 'on')
-  packadd oscyank.vim
-endif