0

I have this function:

(defun save-binary-content (binary path)
(let ((coding-system-for-write 'no-conversion))
(with-temp-buffer
  (toggle-enable-multibyte-characters)
  (set-buffer-file-coding-system 'raw-text)
  (insert binary)
  (write-file path t))))

binary is just some stuff that is taken from the system clipboard. I need to know the content type of what's in that variable. Is there an emacs command that allows me to detect the conntent type without using a temporary file?

2
  • What do you need the mime type for? The filesystem doesn’t care about mime types, so you don’t need it to save to a file.
    – db48x
    May 1, 2022 at 10:57
  • @db48x I need to know what i'm actually handling in order to do other operations after the save operation. In this specific case i'd like to automatically add an extension (if none provided). It's not necessary but it's useful to me
    – cidra
    May 1, 2022 at 11:01

1 Answer 1

1

You can check chapter 30.20 Window System Selections and chapter 30.21 Drag and Drop of the Emacs Lisp manual for the details, but Emacs mostly only supports text for both pastes and drag and drop actions. However, it may be possible for you to do what you want.

For pastes, you can see the list of formats Emacs knows about in the selection-converter-alist variable. These are mostly all different kinds of text, and some things that are very similar to text such as file names and user names, but the one called TARGETS is the most interesting. If you call gui-get-selection with a type of TARGETS, you will be sent not the data but a list of data types that are available. You can then pick the type you prefer and call gui-get-selection again with that type.

Handling drag–and–drop is similar, except that you want to set the x-dnd-test-function in the buffer to a function of your own. It will be called any time a drop is initiated, and it gets a list of data types that it can choose from. The default test function looks at the value of x-dnd-known-types; if any type from the drop is in that list, it will use the corresponding function to accept the drop. By default those are all various types of text.

Here is a random article I found online with related information and some simple examples: https://www.uninformativ.de/blog/postings/2017-04-02/0/POSTING-en.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.