Last active
September 25, 2017 16:24
-
-
Save prakashk/a38672a0e9a7c147ff7f8e41da2e685b to your computer and use it in GitHub Desktop.
Display remote images in emacs eshell buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; The stackoverflow answer https://emacs.stackexchange.com/a/9737 | |
;; extends eshell/cat function to display images inline in *eshell* | |
;; buffer, so that | |
;; | |
;; eshell:~> cat my-image.png | |
;; | |
;; works just fine. | |
;; | |
;; However, this doesn't work if the eshell buffer's default-directory | |
;; is remote, and the image file is located on the remote host. cat just | |
;; dumps the contents of the image file into *eshell* buffer. | |
;; | |
;; I extend the function below to display remote images, albeit with a | |
;; little quirk. The image file name should be the full path including | |
;; the tramp protocol and host prefixes (for example: | |
;; /ssh:remote-host:/path/to/image.png) | |
;; | |
;; eshell:~> cat /ssh:remote-host:/path/to/image.png | |
;; | |
;; This is required even if you are already *in* the remote directory | |
;; containing the image. | |
;; | |
;; # this doesn't work | |
;; eshell:/ssh:remote-host:/path/to> cat image.png | |
;; | |
;; # this works | |
;; eshell:/ssh:remote-host:/path/to> cat $PWD/image.png | |
;; | |
;; TODO: | |
;; - fix the above quirk | |
;; - clean up local copy before returning | |
(defun my/iimage-mode-refresh--eshell/cat (orig-fun &rest args) | |
"Display image when using cat on it." | |
(cl-flet ((maybe-localize-remote-file (file) | |
(if (file-remote-p file) | |
(let ((local-file (concat "/tmp/" (file-name-nondirectory file)))) | |
(eshell/cp file local-file) | |
local-file) | |
file))) | |
(let ((image-path (cons default-directory iimage-mode-image-search-path))) | |
(dolist (arg args) | |
(let ((imagep nil) | |
file) | |
(with-silent-modifications | |
(save-excursion | |
(dolist (pair iimage-mode-image-regex-alist) | |
(when (and (not imagep) | |
(setq arg (maybe-localize-remote-file arg)) | |
(string-match (car pair) arg) | |
(setq file (match-string (cdr pair) arg)) | |
(setq file (locate-file file image-path))) | |
(setq imagep t) | |
(add-text-properties 0 (length arg) | |
`(display ,(create-image file) | |
modification-hooks | |
(iimage-modification-hook)) | |
arg) | |
(eshell-buffered-print arg) | |
(eshell-flush))))) | |
(when (not imagep) | |
(apply orig-fun (list arg))))) | |
(eshell-flush)))) | |
(advice-add 'eshell/cat :around #'my/iimage-mode-refresh--eshell/cat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment