9

It seems like this should be pretty straightforward since Emacs knows how to display images.

In a buffer, let's say I have a URL to an image like:

http://centre.nikkeiplace.org/wp-content/uploads/2012/07/aikido11.jpg

I'd like to put the caret on that url, call a function like expand-image-from-url and have it render the image in-line.

Is this something that's easily possible?

1 Answer 1

12

If you don't mind the image being displayed in a different buffer, just do

M-x ffap

If you insist on the image appearing in the current buffer, you'll need to do just a little more work:

(require 'url)

(defun insert-image-from-url (&optional url)
  (interactive)
  (unless url (setq url (url-get-url-at-point)))
  (unless url
    (error "Couldn't find URL."))
  (let ((buffer (url-retrieve-synchronously url)))
    (unwind-protect
         (let ((data (with-current-buffer buffer
                       (goto-char (point-min))
                       (search-forward "\n\n")
                       (buffer-substring (point) (point-max)))))
           (insert-image (create-image data nil t)))
      (kill-buffer buffer))))
5
  • That's the thing, I want it displayed inline in the same buffer.
    – bitops
    May 2, 2015 at 21:30
  • 1
    You're a tough customer. Edited with some sample code.
    – jch
    May 2, 2015 at 21:33
  • Very nice! One thing to note: I had to grab some of the 'thingatpt' packages to get it to work. I suppose url-get-url-at-point is not a built-in command.
    – bitops
    May 2, 2015 at 23:00
  • 1
    You just need (require 'url).
    – jch
    May 2, 2015 at 23:03
  • Thank you! Works perfectly.
    – giessel
    May 2, 2019 at 19:05

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.