masm11 / emacs

Mirror of GNU Emacs
http://www.gnu.org/software/emacs/
GNU General Public License v3.0
198 stars 14 forks source link

Support for copy image from clipboard #93

Closed yangsheng6810 closed 3 years ago

yangsheng6810 commented 3 years ago

Non-pgtk branch of Emacs supports retrieving the image copied to the clipboard. For example, in telega, I can use telega-chatbuf-attach-clipboard to attach an image I just copied. Another example would be in org-download, which inserts the image in clipboard in an org-mode buffer. However, in pgtk branch, this feature is not supported. An attempt to retrieve an image from the clipboard receives nothing.

masm11 commented 3 years ago

I confirmed org-download-clipboard works on pgtk emacs.

I executed wl-copy -t image/png < test2.png on a terminal, and did M-x org-download-clipboard on pgtk emacs.

yangsheng6810 commented 3 years ago

My fault. I only tested the telega method, and thought org-download-clipboard would have the same problem. After checking the code, it turns out org-download uses shell command (wl-paste/xclip/convert/pngpaste) to get the clipboard. On the other hand, telega-chatbuf-attach-clipboard uses gui-get-selection provided by Emacs with the following code snippet:

(defun telega-chatbuf-attach-clipboard (doc-p)
  "Attach clipboard image to the chatbuf as photo.
If `\\[universal-argument]' is given, then attach clipboard as document."
  (interactive "P")
  (let* ((selection-coding-system 'no-conversion) ;for rawdata
         (temporary-file-directory telega-temp-dir)
         (tmpfile (telega-temp-name "clipboard" ".png"))
         (coding-system-for-write 'binary))
    (if (eq system-type 'darwin)
        (progn
          ;; NOTE: On MacOS, try extracting clipboard using pngpaste
          (unless (executable-find "pngpaste")
            (error "Please install pngpaste to paste images"))
          (unless (= 0 (telega-screenshot-with-pngpaste tmpfile))
            (error "No image in CLIPBOARD")))
      (write-region (or (gui-get-selection 'CLIPBOARD 'image/png)
                        (error "No image in CLIPBOARD"))
                    nil tmpfile nil 'quiet))
    (telega-chatbuf--attach-tmp-photo tmpfile doc-p)))

On a normal Emacs branch, when there is image in the clipboard, (gui-get-selection 'CLIPBOARD 'image/png) would return a string of the image. But on pgtk emacs, it returns nil.

BTW, a quick ripgrep shows this also affects emacs-slack.

masm11 commented 3 years ago

I fixed on feature/pgtk in savannah. Please test it.

yangsheng6810 commented 3 years ago

Thanks. Works like a charm! // Test is done on i3wm on Xorg.