Skip to content

[Feature request] Smart paste to handle images and text #223

@ElleNajt

Description

@ElleNajt

I think it would be helpful if there was a paste function that pasted text if text was stored in the clipboard, and if there was an image in the clipboard it used this module to paste the image.

The following code achieves this on my system (doom emacs on gnu/linux with X11)

(defun org-clipboard-has-image-p ()
  (interactive)
  ;; timeout necessary so xclip doesn't hang
  ;; this shell command is returning "" in emacs, though correct output if not
  ;; an image/png is the given error message -- compare to the terminal output of
  ;; the command.
  ;; to get the correct output in emacs the shell command needs
  ;; to be run async, which is more complicated to use here.
  ;; I haven't bothered to figure out why, checking for "" or the Error message
  ;; for stability
  (let ((output (shell-command-to-string "timeout 0.01 xclip -selection clipboard -t image/png -o")))
    (not (or (string-empty-p output)
             (string-match-p "Error: target image/png not available" output)))))

(defun org-clipboard-download-smart ()
  (interactive)
  (if (org-clipboard-has-image-p)
      (org-download-clipboard)
    (evil-paste-after 1)))

(map! :map org-mode-map
      :n "p" #'org-clipboard-download-smart)

Presumably org-clipboard-has-image-p could be modified to work for other systems as in the org-download-clipboard function.

As the comment indicates, this is something of a hack, but maybe it can be useful to someone else anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions