It's a breaking workflow when I need to take a picture of screen (to clipboard), save it into somewhere and link it before starting to write something in markdown. This plugin is to fix that for vim users. It can also do the same job for image file if you have its absolute path in clipboard!
But Markdown Cannot Show Image!
Well I don't want to address this problem here but technically yes it can if you use a fancier terminal like iTerm2. Actually for markdown preview in vim, a practical way is to use GUI vim e.g. vimR or browser solution like markdown instant preview. However, if you are hardcore vimmer, probably solution as in image.vim will be preferrable. Maybe at some point, we could write another plugin to display colorful blocky image in markdown vim.
Search in Darkness
First we need to grab clipboard data and this is not trivial at all in Python. This one from PIL actually only support win32 unfortunately. So I did some research on stackoverflow: went through recommendations like Pyperclip (text-only and I used before but got issues due to its GUI dependencies in Ranger), TKinter (too heavy for a vim plugin), PythonMagick (I use Magick a lot, but this python version has no pip support, and is hard to install for normal users), Xerox (only support text, richxerox does extra for html but didn't support image)... I'm surprised that we don't have this in Python! Maybe because this type (clipboard) of GUI related feature have too much trap to cross platform. But I just need it work on Mac OSX and Linux.
From Scratch
Therefore, here's what I could do at this point:
For Mac OS X: we just need to brew install pngpaste
from here. Then use osascript to check the clipboard information and call this program to do the job.
For Linux: this answer looks promising.
Ok, let's quickly build a working plugin. Since we have access to clipboard and we could also check if clipboard contains image file name to copy it to local folder and link in markdown as well.
Hooray for Mac Vim user! Now, let's make it work in Linux.
Bumping Around
Again promising xclip
suggested from here doesn't work... and some suggests pygtk. Okay, looks like we are stuck in GTK but that should be fine (easier to install on linux and probably on mac as well) for most use cases. PyGtk is actually a bit old, and we'd better use GTK3 from here.
So I installed pyobject2
and python34-gobject
and try the test code. After making sure everything works, just follow the example snippet:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
image = clipboard.wait_for_image()
if image is not None:
image.savev('screenshot.png', 'png', [], [])
text = clipboard.wait_for_text()
if text is not None:
print(text)
It's so simple, neat and I just wanted to have it on Mac as well.
Revisit PyGObject on Mac
conda install -c conda-forge pygobject