* 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
* Re: Midnight Commander Quick View
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-27 6:33 ` Alin
0 siblings, 2 replies; 6+ messages in thread
From: Stefan Monnier @ 2014-08-25 14:38 UTC (permalink / raw)
To: Alin Soare; +Cc: emacs-devel
> (setq lexical-binding t)
Don't do that. This will set lexical-binding during *execution*,
whereas that option is needed during compilation.
Furthermore, it will set it such that it will affect everything rather
than only code in this file.
Stefan
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Midnight Commander Quick View
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
1 sibling, 1 reply; 6+ messages in thread
From: Alin @ 2014-08-25 19:30 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
Stefan Monnier <monnier@IRO.UMontreal.CA> writes:
>> (setq lexical-binding t)
>
> Don't do that. This will set lexical-binding during *execution*,
> whereas that option is needed during compilation.
> Furthermore, it will set it such that it will affect everything rather
> than only code in this file.
>
>
setting lexical-binding has nothing to do with the code (apart that it
must be set buffer-local, for this code to work).
Here is an updated version, that works quite nice, and is fast.
(defun qv ()
(let ((i 0))
(let ((config (current-window-configuration)))
(defun qv-remove-other-buffer ()
(save-window-excursion
(other-window 1)
(and buffer-read-only
(not (buffer-modified-p))
(kill-buffer (window-buffer)))))
(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)))))
(qv-remove-other-buffer))
(let ((auto-mode-alist))
(view-file-other-window f))
(other-window 1)
(message "View file %s. Press C-q to quit quick-view." f))))
(delete-other-windows)
(split-window-sensibly)
(balance-windows)
(add-hook 'post-command-hook 'qv-update 'at-the-end t)
(lambda (message)
(defun exit ()
(setq-local quick-view-object nil)
(qv-remove-other-buffer)
(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 t)
(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
* Re: Midnight Commander Quick View
2014-08-25 19:30 ` Alin
@ 2014-08-26 17:45 ` Stefan Monnier
0 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2014-08-26 17:45 UTC (permalink / raw)
To: Alin; +Cc: emacs-devel
> (setq-local qv-binding
A call to setq-local at top-level makes no sense.
What are you trying to do?
Stefan
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Midnight Commander Quick View
2014-08-25 14:38 ` Stefan Monnier
2014-08-25 19:30 ` Alin
@ 2014-08-27 6:33 ` Alin
1 sibling, 0 replies; 6+ messages in thread
From: Alin @ 2014-08-27 6:33 UTC (permalink / raw)
To: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 418 bytes --]
I extracted from my configuration files the code where I wrote quick
view like in midnight commander , and here is a version `test.el` that I
tested in `emacs -Q`. Put it in your load-path, and call `(load
"test.el")`. In dired-mode, enter and quit `qv` qith C-c C-q. It is
buffer-local everything, so if you pass in another directory it's no
more active, unless you reactivate it.
You will like it. Enjoy.
[-- Attachment #2: Midnight Commander Quick View for dired. --]
[-- Type: application/emacs-lisp, Size: 2003 bytes --]
[-- Attachment #3: Type: text/plain, Size: 29 bytes --]
--
No GNUs is bad news.
^ permalink raw reply [flat|nested] 6+ messages in thread
* 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
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-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
-- strict thread matches above, loose matches on Subject: below --
2014-09-11 14:51 Midnight commander quick view Alin Soare
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).