Suppose I have a latex file like
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=\textwidth]{image1.jpg}
Some text
\includegraphics{image2.jpg}
Some text
\includegraphics{image3.jpg}
and so on...
\end{document}
Now I tried to create a function which opens the "nearest" image above the cursor:
(defun my-show-img ()
(interactive)
(save-excursion
(let (imgfile)
(while (not (looking-at "\\\\includegraphics\\[?.*\\]?{\\(?1:.*\\(\\.png\\|\\.jpg\\)\\)}"))
(forward-char -1))
(setq imgfile (match-string-no-properties 1))
(if (file-exists-p imgfile)
;(message imgfile)
(async-shell-command (concat "eog -w " imgfile))
)
)
)
)
For example if the cursor is somewhere between \includegraphics{image3.jpg}
and \includegraphics{image2.jpg}
it will open image2.jpg
.
Now I have several problems with this:
- When executing it, it opens a new (empty) buffer
*Async Shell Command*
. How can I prevent this? - If I execute it a second time (maybe cursor on another place, for example between
\includegraphics{image2.jpg}
and\includegraphics[width=\textwidth]{image1.jpg}
), it asks me "A command is running in the default buffer. Use a new buffer?". How can I prevent this message?
At the end I want to be able the following: Call something like enable-img-preview
then it opens eog
with the "nearest" image before the cursor (just like above), however if I move the cursor (or click with the mouse somewhere) it should automatically update the showing image with the image thats now the "nearest" before the cursor (without any messages or other buffers, see above). This behaviour should end when calling another function say disable-img-preview
.
How can I implement this feature? Since this probably needs a background process which watches the cursor position, it would be great to find a way which doesn't slow down emacs or takes much resources.
Other improvement suggestions for my source code are welcome.