commit bfde63ffe01ad3b40b29b46fbec2f299ed8d9f5b parent 50f7e4c4ec1efdcac0ef8b38c7d8236900159aea Author: Tomas Hlavaty <tom@logand.com> Date: Thu, 21 May 2020 19:54:11 +0200 add emacs-framebuffer.el Diffstat:
A | emacs-framebuffer.el | | | 84 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 84 insertions(+), 0 deletions(-)
diff --git a/emacs-framebuffer.el b/emacs-framebuffer.el @@ -0,0 +1,84 @@ +;;; emacs-framebuffer.el +;;; +;;; Show images in console using Linux framebuffer. +;;; +;;; Copyright (C) 2020 Tomas Hlavaty <tom at logand dot com> +;;; +;;; License: GPLv3 or later +;;; +;;; Dependencies: +;;; +;;; - w3mimgdisplay from w3m terminal web browser +;;; +;;; - file program +;;; +;;; Example configuration: +;;; +;;; (require 'framebuffer) +;;; (eval-after-load 'dired +;;; '(progn +;;; (define-key dired-mode-map "I" 'framebuffer-image-file-dired) +;;; (define-key dired-mode-map "N" 'framebuffer-image-file-dired-next) +;;; (define-key dired-mode-map "P" 'framebuffer-image-file-dired-previous))) + +(defcustom framebuffer-width 1366 + "Specify the framebuffer width." + :type 'number + :group 'framebuffer) + +(defcustom framebuffer-height 768 + "Specify the framebuffer height." + :type 'number + :group 'framebuffer) + +(defun framebuffer-draw (x y w h file) + (with-temp-buffer + (insert (format "0;1;%d;%d;%d;%d;;;;;%s\n" x y w h (expand-file-name file))) + (call-process-region (point-min) (point-max) "w3mimgdisplay"))) + +(defun framebuffer-image-size (file) + (with-temp-buffer + (call-process "file" nil t nil (expand-file-name file)) + (search-backward-regexp ", \\([0-9]+\\)+x\\([0-9]+\\),") + (cons (read (match-string 1)) (read (match-string 2))))) + +(defun framebuffer-image-file (file) + (interactive "fFile: ") + (let* ((size (framebuffer-image-size file)) + (w (car size)) + (h (cdr size)) + (scale (min (/ framebuffer-width 1.0 w) (/ framebuffer-height 1.0 h))) + (ww (* scale w)) + (hh (* scale h))) + (framebuffer-draw (/ (- framebuffer-width ww) 2) + (/ (- framebuffer-height hh) 2) + ww + hh + file))) + +(defun framebuffer-image-buffer () + (interactive) + (framebuffer-image-file (buffer-file-name))) + +(defun framebuffer-image-file-dired () + (interactive) + (framebuffer-image-file (dired-file-name-at-point))) + +(defun framebuffer-dired-image-p () + (let ((f (dired-file-name-at-point))) + (and (not (file-directory-p f)) + (string-match "jpe?g\\|JPE?G\\|png\\|PNG" (file-name-extension f))))) + +(defun framebuffer-image-file-dired-next (arg) + (interactive "^p") + (while (not (progn + (dired-next-line (or arg 1)) + (framebuffer-dired-image-p)))) + (when (framebuffer-dired-image-p) + (framebuffer-image-file-dired))) + +(defun framebuffer-image-file-dired-previous (arg) + (interactive "^p") + (framebuffer-image-file-dired-next (or -1))) + +(provide 'framebuffer)