emacs-framebuffer

Emacs library to show images and documents in console using Linux framebuffer
Log | Files | Refs

commit 961196e1d461190f2e237c65dc34e78ee063ffce
parent e99f1fc059555a07291c6b437f3feb0c39ad404c
Author: Tomas Hlavaty <tom@logand.com>
Date:   Sat, 17 Oct 2020 19:56:42 +0200

handle remote files

local files:

- some are directly displayed using w3mimgdisplay

- some need conversions which are cached, based on sha256sum of the
  file content

remote files:

- always cached, based on the sha256sum of the file name because this
  needs to be fast without remote access

Diffstat:
Memacs-framebuffer.el | 49++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 38 insertions(+), 11 deletions(-)

diff --git a/emacs-framebuffer.el b/emacs-framebuffer.el @@ -122,11 +122,40 @@ (cons (read (match-string 1)) (read (match-string 2)))) (cons framebuffer-default-width framebuffer-default-height)))))) +(defun framebuffer-local-cache-directory () + (let ((z (or framebuffer-cache-directory + (let ((default-directory "/")) + (temporary-file-directory))))) + (if (file-remote-p z) + (error "framebuffer-cache-directory is remote %s" z) + z))) + +(defun framebuffer-local-cache-file (name extension) + (concat (framebuffer-local-cache-directory) "/" name "." extension)) + (defun framebuffer-file-hash (file) (with-temp-buffer - (call-process "sha256sum" nil t nil (expand-file-name file)) + (if (file-remote-p file) + (let ((f (framebuffer-local-cache-file "emacs-framebuffer" "tmp"))) + (with-temp-file f + (insert (expand-file-name file))) + (call-process "sha256sum" nil t nil (expand-file-name f))) + (call-process "sha256sum" nil t nil (expand-file-name file))) (buffer-substring (point-min) (+ (point-min) 64)))) +(defun framebuffer-file-readable-p (file) + (when (file-readable-p file) + file)) + +(defun framebuffer-cache-remote (file) + (if (file-remote-p file) + (let ((z (framebuffer-local-cache-file (framebuffer-file-hash file) + (file-name-extension file)))) + (unless (file-readable-p z) + (copy-file file z)) + z) + file)) + (defun framebuffer-cache-abiword (format ifile ofile) (case format ((abw odt rtf doc docx) @@ -134,20 +163,17 @@ (call-process "abiword" nil nil nil "-t" (expand-file-name ofile) (expand-file-name ifile))) - (when (file-readable-p ofile) - ofile)))) + ofile))) (defun framebuffer-cache-to-pdf (format file) (framebuffer-cache-abiword format file - (concat (or framebuffer-cache-directory (temporary-file-directory)) - "/" - (framebuffer-file-hash file) ".pdf"))) + (framebuffer-local-cache-file (framebuffer-file-hash file) "pdf"))) (defun framebuffer-cache-pdf-to-png (file page) (let* ((page (format "%s" (or page 1))) - (output (concat (or framebuffer-cache-directory (temporary-file-directory)) + (output (concat (framebuffer-local-cache-directory) "/" (framebuffer-file-hash file) "-" page)) (ofile (concat output ".png"))) @@ -156,10 +182,7 @@ ofile)) (defun framebuffer-cache-svg-to-png (file) - (let ((ofile (concat (or framebuffer-cache-directory (temporary-file-directory)) - "/" - (framebuffer-file-hash file) - ".png"))) + (let ((ofile (framebuffer-local-cache-file (framebuffer-file-hash file) "png"))) (unless (file-readable-p ofile) (call-process "convert" nil nil nil (expand-file-name file) ofile)) ofile)) @@ -185,6 +208,7 @@ "w3mimgdisplay")) (defun framebuffer-draw (x y w h sx sy sw sh file) + (setq file (framebuffer-cache-remote file)) (let ((format (framebuffer-file-format file))) (case format (svg @@ -256,6 +280,7 @@ (cons (read (match-string 1)) (read (match-string 2)))))) (defun framebuffer-image-size (file) + (setq file (framebuffer-cache-remote file)) (with-temp-buffer (set-buffer-multibyte nil) (insert-file-contents-literally file) @@ -406,6 +431,7 @@ (defun framebuffer-image-file (file) (interactive "fFile: ") + (setq file (framebuffer-cache-remote file)) (destructuring-bind (w &rest h) (framebuffer-image-size file) (destructuring-bind (fw &rest fh) (framebuffer-size) (let* ((scale (min (/ fw 1.0 w) (/ fh 1.0 h))) @@ -497,6 +523,7 @@ z))))) (defun framebuffer-image-npages (file) + (setq file (framebuffer-cache-remote file)) (let ((format (framebuffer-file-format file))) (case format (pdf