[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Midnight commander quick view.

From: Alin Soare
Subject: Midnight commander quick view.
Date: Thu, 11 Sep 2014 17:51:57 +0300

I sent a stub for quick-view like in mc a few weeks ago.

I sent here the latest version that I have been using for more than 10
days, and it seems stable. I did not need to modify it in the last past
10 days, even if I used it each day many times.

Switch qv on and off is done with C-c C-q . It is buffer local. If we
change the directory in dired, we have to reactivate it.

;;; -*- lexical-binding:t -*-

(require 'dired)

(defun qv ()
  (let ((config (current-window-configuration))
        (max-size 1e4)
        (qv-buffer (get-buffer-create "*quick view*")))
    (defvar exit-keybinding
      (lambda (&optional arg)
        (apply quick-view-object (list 'exit))))
    (defvar init-keybinding
      (lambda (&optional arg)
        (setq-local quick-view-object (qv))
        (apply quick-view-object '(init))
        (message "enter quick view")))
    (defun get-key () init-keybinding)
    (defun update ()
      (let ((f (dired-get-filename nil t)))
        (defun file-not-readable-p ()
          (and (stringp f)
               (file-regular-p f)
               (not (file-readable-p f))))
        (defun regular-file-p ()
          (and (stringp f)
               (file-regular-p f)
               (file-readable-p f)))
        (defun already-opened-file-p ()
          (and (stringp f)
               (file-regular-p f)
               (get-file-buffer f)))
        (with-current-buffer qv-buffer
          (setq buffer-read-only nil)
          (cond ((already-opened-file-p)
                 (insert-buffer-substring (get-file-buffer f)))
                  f nil 0 (min max-size (nth 7 (file-attributes f))))
                 (and (< max-size (nth 7 (file-attributes f)))
                      (message "header preview truncated at byte %d" max-size)))
                 (insert "-- file not readble --"))
                (t (erase-buffer)
                   (insert "-- no file at point --")))
          (set-buffer-modified-p nil)
          (setq buffer-read-only t
                buffer-file-name nil))))
    (defun init ()
          (save-window-excursion (other-window 1)
        (switch-to-buffer qv-buffer)
        (setq buffer-undo-list t))
      (add-hook 'post-command-hook 'update 'at-the-end t)
      (define-key dired-mode-map [(control ?c) (control ?q)]
      (setq undo-outer-limit max-size)
      (dired-hide-details-mode +1))
    (defun exit ()
      (setq-local quick-view-object nil)
      (remove-hook 'post-command-hook 'update t)
      (define-key dired-mode-map [(control ?c) (control ?q)]
        init-keybinding )
      (kill-buffer qv-buffer)
      (dired-hide-details-mode -1)
      (set-window-configuration config)
      (message "exit qv"))
    (lambda (message) (apply message '()))))

(define-key dired-mode-map [(control ?c) (control ?q)]
  (apply (qv) '(get-key)))

No GNUs is bad news.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]