all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Midnight commander quick view.
@ 2014-09-11 14:51 Alin Soare
  0 siblings, 0 replies; 6+ messages in thread
From: Alin Soare @ 2014-09-11 14:51 UTC (permalink / raw)
  To: emacs-devel


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)
        (interactive)
        (apply quick-view-object (list 'exit))))
    (defvar init-keybinding
      (lambda (&optional arg)
        (interactive)
        (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)
                 (erase-buffer)
                 (insert-buffer-substring (get-file-buffer f)))
                ((regular-file-p)
                 (erase-buffer)
                 (insert-file-contents-literally
                  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)))
                ((file-not-readable-p)
                 (erase-buffer)
                 (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 ()
      (delete-other-windows)
      (split-window-sensibly)
      (balance-windows)
      (with-selected-window
          (save-window-excursion (other-window 1)
                                 (selected-window))
        (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)]
        exit-keybinding)
      (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.



^ permalink raw reply	[flat|nested] 6+ messages in thread
* Midnight Commander Quick View
@ 2014-08-25  2:26 Alin Soare
  2014-08-25 14:38 ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Alin Soare @ 2014-08-25  2:26 UTC (permalink / raw)
  To: emacs-devel


I needed quick view option, like in midnight commander, and I wrote this
code. 

I want to ask if you know something that already exists and does a
similar job. My code is not complete -- for example, for large files, I
want to see only the header, not to load all the file.


In my emacs this code works as I expect to work. I did not test it
outside -- but it should work as well.


---

(setq lexical-binding t)
(require 'dired)


(defun qv-update ()
  (let ((f (dired-get-filename t t)))
    (when (and (stringp f) (file-regular-p f)
               (< (nth 7 (file-attributes f)) 1e7))
      (when (not (equal (window-buffer)
                        (save-window-excursion
                          (window-buffer (other-window 1)))))
        (save-window-excursion
          (other-window 1)
          (and buffer-read-only
               (not (buffer-modified-p))
               (kill-buffer (window-buffer)))))
      (view-file-other-window f)
       (other-window 1))))

(defun qv ()
  (let ((i 0))
    (let ((config (current-window-configuration)))
      (delete-other-windows)
      (split-window-sensibly)
      (balance-windows)
      (add-hook 'post-command-hook 'qv-update)
      (lambda (message)
        (defun exit ()
          (setq-local quick-view-object nil)
          (define-key dired-mode-map
            [(control ?c) (control ?q)]
            qv-binding)
          (setq i (1- i))
          (set-window-configuration config)
          (remove-hook 'post-command-hook 'qv-update)
          (message "exit qv"))
        (apply message '())))))

(setq-local qv-binding
    (lambda (&optional arg)
      (interactive)
      (setq-local quick-view-object (qv))
      (define-key dired-mode-map [(control ?c) (control ?q)]
        (lambda (&optional arg)
          (interactive)
          (dired-next-line 1)
          (apply quick-view-object (list 'exit))))))

(define-key dired-mode-map
  [(control ?c) (control ?q)]
  qv-binding)












-- 
No GNUs is bad news.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-09-11 14:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-11 14:51 Midnight commander quick view Alin Soare
  -- strict thread matches above, loose matches on Subject: below --
2014-08-25  2:26 Midnight Commander Quick View Alin Soare
2014-08-25 14:38 ` Stefan Monnier
2014-08-25 19:30   ` Alin
2014-08-26 17:45     ` Stefan Monnier
2014-08-27  6:33   ` Alin

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.