* Re: using mouse to select a file in dired
2010-10-04 19:42 using mouse to select a file in dired Ray Voith
@ 2010-10-05 8:20 ` Marc Mientki
2010-10-05 9:02 ` Marc Mientki
0 siblings, 1 reply; 3+ messages in thread
From: Marc Mientki @ 2010-10-05 8:20 UTC (permalink / raw)
To: help-gnu-emacs
Am 04.10.2010 21:42, schrieb Ray Voith:
> When I am in a dired and click on an entry, it opens it in
> another window.
> I prefer that it open it in the same window that the dired is in. How
> can I set up to do this?
Hmmm... let's see...
You click on a file in dired and Emacs do something not exactly
what you want. So we first look what happens when we cklick on
a file in dired. We type 'C-h k' and than simply click on some
file in dired buffer. We get this in a new buffer:
-- BEGIN ----------------------------------------------------------
<down-mouse-1> at that spot runs the command mouse-drag-region, which
is an interactive compiled Lisp function in `mouse.el'.
It is bound to <down-mouse-1>.
(mouse-drag-region START-EVENT)
Set the region to the text that the mouse is dragged over.
Highlight the drag area as you move the mouse.
This must be bound to a button-down mouse event.
In Transient Mark mode, the highlighting remains as long as the mark
remains active. Otherwise, it remains until the next input event.
If the click is in the echo area, display the `*Messages*' buffer.
----------------- up-event (short click) ----------------
<mouse-1> at that spot is remapped to <mouse-2>, which runs the
command dired-mouse-find-file-other-window, which is an interactive
compiled Lisp function in `dired.el'.
It is bound to <mouse-2>.
(dired-mouse-find-file-other-window EVENT)
In Dired, visit the file or directory name you click on.
----------------- up-event (long click) ----------------
Pressing <mouse-1> at that spot for longer than 450 milli-seconds
runs the command mouse-set-point, which is an interactive compiled
Lisp function in `mouse.el'.
It is bound to <triple-mouse-1>, <double-mouse-1>, <mouse-1>,
<right-fringe> <mouse-1>, <left-fringe> <mouse-1>.
(mouse-set-point EVENT)
Move point to the position clicked on with the mouse.
This should be bound to a mouse click event type.
-- END ------------------------------------------------------------
We look around for something suspicious and find an interesting
hint to the function dired-mouse-find-file-other-window in file
dired.el. This 'dired.el' is a link to the right place in the
file so we go on it with TAB and land in dired.el on definition
of dired-mouse-find-file-other-window. Actually, the name says
the whole story - "-OTHER-WINDOW". Unfortunately there is no
function like dired-mouse-find-file. The function dired-find-file
is no alternative because it handles no events and we need event
handling due to mouse operation. So there is no simple solution
like replacement of the keybinding of [mouse-2] to another
existing function. But what it does not exist, you have to do it
himself. So let's look at the function definition:
-- BEGIN ----------------------------------------------------------
(defun dired-mouse-find-file-other-window (event)
"In Dired, visit the file or directory name you click on."
(interactive "e")
(let (window pos file)
(save-excursion
(setq window (posn-window (event-end event))
pos (posn-point (event-end event)))
(if (not (windowp window))
(error "No file chosen"))
(set-buffer (window-buffer window))
(goto-char pos)
(setq file (dired-get-file-for-visit)))
(if (file-directory-p file)
(or (and (cdr dired-subdir-alist)
(dired-goto-subdir file))
(progn
(select-window window)
(dired-other-window file)))
(select-window window)
(find-file-other-window (file-name-sans-versions file t)))))
-- END ------------------------------------------------------------
As we can see most important story plays at the end. There is
a distinction, if we click on a directory or a file
- (if (file-directory-p file) ...). But no matter where we click,
the operation is executed in a new window - dired-other-window or
find-file-other-window. So the simplest solution is to replace
this both function to version that do the same job without window
change. Fortunately, there are similar correspondences. We take
dired in place of dired-other-window and find-file in place of
find-file-other-window and name the new function properly:
-- BEGIN ----------------------------------------------------------
(defun dired-mouse-find-file (event)
"In Dired, visit the file or directory name you click on."
(interactive "e")
(let (window pos file)
(save-excursion
(setq window (posn-window (event-end event))
pos (posn-point (event-end event)))
(if (not (windowp window))
(error "No file chosen"))
(set-buffer (window-buffer window))
(goto-char pos)
(setq file (dired-get-file-for-visit)))
(if (file-directory-p file)
(or (and (cdr dired-subdir-alist)
(dired-goto-subdir file))
(progn
(select-window window)
(dired file)))
(select-window window)
(find-file (file-name-sans-versions file t)))))
-- END ------------------------------------------------------------
The last step is to bind this new function to the right event in
the right key map. As seen above, the event was 'mouse-2'. The
name of the key-map we also learn in dired.el. We search simply for
'dired-mouse-find-file-other-window' and find:
(defvar dired-mode-map
;; This looks ugly when substitute-command-keys uses C-d instead d:
;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
(let ((map (make-keymap)))
(suppress-keymap map)
(define-key map [mouse-2] 'dired-mouse-find-file-other-window)
...
So all we need is:
(define-key dired-mode-map [mouse-2] 'dired-mouse-find-file)
Short test and it works! :-)
HTH
regards
Marc
^ permalink raw reply [flat|nested] 3+ messages in thread