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?

bitops
  • 353
  • 1
  • 12

1 Answers1

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))))
jch
  • 5,720
  • 1
  • 23
  • 39