insert image into text buffer
Asked Answered
L

3

23

If I place

(insert-image (create-image "/tmp/test.png"))

in a buffer, place the cursor after the last parenthesis and evaluate it with C-x C-e, then the image /tmp/test.png is displayed in the buffer:

enter image description here

Pretty neat. But,

  1. I had to put the final parenthesis on a separate line, so the image is close to the left-hand side of the buffer. Is there a way to hide the (insert-image ...) text altogether?
  2. The text file contains the (insert-image ...) text only, not the image. I'm happy with that, but is there a way to tell emacs to automatically replace all the (insert-image ...) expressions by their corresponding images (after the file is opened) without me having to type C-x C-e after each one?
Leela answered 20/3, 2012 at 1:4 Comment(2)
insert-image actually doesn't insert any image into the buffer to save it on to the disk instead its just visual representation. you didn't mention the purpose. In org-mode you can link pictures in the buffer and display them if you want, and export them. is that what you are looking for.?Benniebenning
I'm lookimg for true inline images - where the image data exists in the emacs buffer, probably uuencoded, rather than a pointer to a file containing the image-data. It's a pain to manage separate files.Acrimony
O
30

Take a look at iimage-mode, the inline image minor mode. It's included since Emacs-23, IIRC.

M-xiimage-mode

Osithe answered 20/3, 2012 at 8:21 Comment(2)
+1: I did not know of this mode, but this is definitely the way to go: standard and not overfeatured.Superstructure
Here's a Wayback Machine link to the original link: web.archive.org/web/20120424055608/http://www.netlaputa.ne.jp/…Biosynthesis
M
35

Depending on exactly what you want to achieve, you might try one the the following ideas:

1. use org-mode as your buffer's major mode. You then have access to all the power of org-mode formatting, which includes linking to image files and displaying them:

an image without description
[[file:/tmp/image.png]]

an image with description
[[file:/tmp/image.png][my description]]

then you can call org-toggle-inline-images (C-c C-x C-v) to display images in the buffer (without a prefix argument, it will display only images without description; if you give a prefix argument, it will display all images)

2. write your own elisp code to insert images where you want them, and put it in an eval local pseudo-variable so that it is called when opening the file. For example:

foo
<HERE>
bar

# Local Variables:
#   eval: (progn (beginning-of-buffer)(search-forward "<HERE>")(insert-image (create-image "/tmp/image.png")))
# End:

You can of course wrap the elisp code into a neat function and simply call it from the eval local variable (which is cleaner, but forces you to have the function definition somewhere else, away from your file)

Memorable answered 20/3, 2012 at 7:54 Comment(0)
O
30

Take a look at iimage-mode, the inline image minor mode. It's included since Emacs-23, IIRC.

M-xiimage-mode

Osithe answered 20/3, 2012 at 8:21 Comment(2)
+1: I did not know of this mode, but this is definitely the way to go: standard and not overfeatured.Superstructure
Here's a Wayback Machine link to the original link: web.archive.org/web/20120424055608/http://www.netlaputa.ne.jp/…Biosynthesis
D
1

If you don't want the text (actually lisp code) in the buffer, don't type it into the buffer in the first place. Try M-x eval-expression and enter your lisp code after the Eval prompt:

(insert-image (create-image "/tmp/test.png"))

Then the image is inserted at point in the buffer. You can define a function like this:

(defun my-insert-image () (interactive) (insert-image (create-image "/tmp/test.png")))

Either type M-x eval-expression and the above defun or type it into a buffer and C-x C-e after it. Then you can insert the image using M-x my-insert-image.

Dinnerware answered 20/3, 2012 at 1:54 Comment(2)
Probably better to use (defun my-insert-image (image-file) (interactive "fImage File: ") (insert-image (create-image image-file))).Titular
Or as the manual says (gnu.org/software/emacs/manual/html_node/elisp/…) (defimage test-image ((:type xpm :file "~/test1.xpm") (:type xbm :file "~/test1.xbm"))) (insert-image test-image)Plenum

© 2022 - 2024 — McMap. All rights reserved.