Ken Hirsch wrote:
Here is an experimental patch for a new feature for gvim on MS Windows. I'm glad to hear any feedback about the code or the feature itself. I've tried to follow the conventions in the vim source code. I call the feature "ClipboardWatch".

The new feature triggers an event "ClipboardChanged" whenever the clipboard is changed (in any application). The Windows editor "NoteTab" implements this (as "pasteboard") and it is, at times, very handy.

The feature is activated by ":set clipboardwatch" or ":set cbw".

To my .vimrc, I've added
 if has("clipboardwatch")
   au! ClipboardChanged * exec "normal \"*gp$"
 endif
so that anything copied to the clipboard is pasted into the current buffer.


Notes--
 At http://kenhirsch.net/vim
 I have a downloadable zip file that includes these files:
   gvim.exe
   doc/autocmd.txt
   doc/eval.txt
   doc/map.txt
   doc/options.txt
   doc/tags
   doc/various.txt
 If you unzip directly into your Vim folder, usually
    "C:\Program Files\Vim\vim70",
you should be good to go. Some zip programs may not honor the full path, so you would have to copy the txt files to the vim70\doc directory.

 (This gvim.exe was compiled with
    nmake -f Make_mvc.mak GUI=yes DEFINES=-DFEAT_CBWATCH
  using Visual C++ Express 2005)


I implemented the function 'ui_clipboard_changed()' in ui.c by copying what seemed relevant from 'ui_focus_change()'. Not knowing much about vim internals, it may need fixing, but I haven't seen any problems yet.


I could implement a direct paste in addition to (or instead of) the event. This might be more convenient in most cases, if less flexible.


 I am using
   au! ClipboardChanged * exec "normal \"*gp$"

Ouch! You would have to make sure to ":enew" when going to other applications, or you'd find yourself with rubbish pasted at the cursor in the current Vim buffer. (You may want to add a FocusLost autocommand to make sure some particular "clipboard" file was current.)

To paste at end-of-file without moving the cursor:

        au! ClipboardChanged * exe 'norm m`' | $put + | norm ``


I added the $ at the end because, when the cursor is at the end of the file and you "*gp with complete lines, the cursor ends up at the beginning of the line. This might be considered a bug with "normal gp".


For X-Windows and MacOS, there doesn't seem to be any way for an application to get informed immediately (synchronously) of clipboard changes. X-windows seems to only notify the previous clipboard owner. I didn't see anything in the Mac API documentation that seemed right, but I'm not at all familiar with Mac programming, so I might have missed something. However, there's an open-source Mac utility called Jumpcut (http://jumpcut.sourceforge.net/) which attempts to provide similar functionality for Mac applications. It looks like it polls the clipboard once a second to see if it's changed. This leads to two conclusions:
   1. Polling the clipboard is the only way to do it on the Mac.
   2. Hey, that works! It could be done on X-Windows, too.

IIUC, using "* on X systems would (with your autocommand above) paste anything that got _selected_ with the mouse, even in the absence of an "Edit => Copy" command. Use "+ to limit yourself to what was explicitly copied or cut.


 The patch is relative to vim-7.0.220

 These files are patched:
   runtime/doc/autocmd.txt
   runtime/doc/eval.txt
   runtime/doc/map.txt
   runtime/doc/options.txt
   runtime/doc/tags
   runtime/doc/various.txt
   src/eval.c
   src/fileio.c
   src/gui_w32.c
   src/gui_w48.c
   src/option.c
   src/option.h
   src/os_mswin.c
   src/proto/gui_w32.pro
   src/proto/ui.pro
   src/ui.c
   src/vim.h



Ken Hirsch
Chapel Hill, NC
USA


A lot of files to patch! I wonder whether the game is worth the candle.


Best regards,
Tony.
--
Megaton Man:    "LOOK at them!  Helpless, tender creatures, relying on
                ME, waiting for ME to make my move!"

(from below):   "Move your ASS, Fat-head!"

Megaton Man:    "It is a MANDATE, and I am DUTY BOUND to OBEY!"

Reply via email to