all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* set-window-buffer bugs
@ 2015-12-05 20:52 jenia.ivlev
  2015-12-05 21:04 ` jenia.ivlev
  2015-12-05 21:19 ` John Mastro
  0 siblings, 2 replies; 4+ messages in thread
From: jenia.ivlev @ 2015-12-05 20:52 UTC (permalink / raw)
  To: help-gnu-emacs

Hello.

Using dired, I want find-buffer-other-window to open in the exact
same window. 

So I made this little program:


    (define-key dired-mode-map "o" 'dired-find-file-other-opened-window)

    (defun dired-find-file-other-opened-window ()
        (interactive)
        (set-window-buffer (frame-first-window) (dired-get-filename 'no-dir))
        (dired-find-file-other-window))
        

It turns out that you needed to have visited that file already for
this to work.


Here is the backtrace for when it doesnt work:

    Debugger entered--entering a function:
    * #<subr set-window-buffer>(#<window 3 on *Backtrace*> "String.h" nil)
    * apply(#<subr set-window-buffer> (#<window 3 on *Backtrace*> "String.h"))
    * set-window-buffer(#<window 3 on *Backtrace*> "String.h")
    dired-find-file-other-opened-window()
    dired-fnd-fl-otr-opn-wnd-keep-cursor()
    call-interactively(dired-fnd-fl-otr-opn-wnd-keep-cursor nil nil)
    command-execute(dired-fnd-fl-otr-opn-wnd-keep-cursor)

After pressing `d` again, I get:

    * command-error-default-function((wrong-type-argument bufferp nil) "" apply)

And here is a backtrace where it does work (the file has been previous visited):

    Debugger entered--entering a function:
    * window-buffer(#<window 3 on *Backtrace*>)
    * linum-after-scroll(#<window 3 on *Backtrace*> 1)
    * #<subr set-window-buffer>(#<window 3 on *Backtrace*> "String.c" nil)
    * apply(#<subr set-window-buffer> (#<window 3 on *Backtrace*> "String.c"))
    * set-window-buffer(#<window 3 on *Backtrace*> "String.c")
    dired-find-file-other-opened-window()
    dired-fnd-fl-otr-opn-wnd-keep-cursor()
    call-interactively(dired-fnd-fl-otr-opn-wnd-keep-cursor nil nil)
    command-execute(dired-fnd-fl-otr-opn-wnd-keep-cursor)



I mean, I don't get it. Why do I need to have visited the file before?
And in any case, how do I fix this? I just want it to open in the
(frame-first-window) that's all.


Thanks




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

* Re: set-window-buffer bugs
  2015-12-05 20:52 set-window-buffer bugs jenia.ivlev
@ 2015-12-05 21:04 ` jenia.ivlev
  2015-12-05 21:19 ` John Mastro
  1 sibling, 0 replies; 4+ messages in thread
From: jenia.ivlev @ 2015-12-05 21:04 UTC (permalink / raw)
  To: help-gnu-emacs

To see where this is useful, divide you screen like this:


|TEXT FILE | DIRED BUFFER
|          |-------------
|          |IBUFFER BUFFER



I want the files in dired to always open in the left most window.
So again this program works perfectly well for files that you have
visited before, but not for the files you've never open yet




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

* Re: set-window-buffer bugs
  2015-12-05 20:52 set-window-buffer bugs jenia.ivlev
  2015-12-05 21:04 ` jenia.ivlev
@ 2015-12-05 21:19 ` John Mastro
  2015-12-05 21:33   ` jenia.ivlev
  1 sibling, 1 reply; 4+ messages in thread
From: John Mastro @ 2015-12-05 21:19 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org; +Cc: jenia.ivlev

jenia.ivlev <jenia.ivlev@gmail.com> wrote:
> Hello.
>
> Using dired, I want find-buffer-other-window to open in the exact
> same window.
>
> So I made this little program:
>
>
>     (define-key dired-mode-map "o" 'dired-find-file-other-opened-window)
>
>     (defun dired-find-file-other-opened-window ()
>         (interactive)
>         (set-window-buffer (frame-first-window) (dired-get-filename 'no-dir))
>         (dired-find-file-other-window))
>
>
> It turns out that you needed to have visited that file already for
> this to work.

[snip]

> I mean, I don't get it. Why do I need to have visited the file before?
> And in any case, how do I fix this? I just want it to open in the
> (frame-first-window) that's all.

Correct, the second argument to `set-window-buffer' must be a buffer (or
the name of a buffer), not the name of the file.

The distinction between buffers and files is an important one. As one
small practical consideration, there can be many files named "foo.txt",
but only one buffer named "foo.txt".

If you're not sure which (file or buffer) is appropriate for a given
function, check out its documentation (e.g. `C-h f set-window-buffer
RET'). In many cases, as with `set-window-buffer', the name itself gives
a hint.

All that said, I think the command below will do what you want. The
function `find-file-noselect' is the one that visits the file and
creates a buffer. If the file is already open, it returns the existing
buffer. It's a variant of `find-file' (the command bound to `C-x C-f')
with the difference that it doesn't display the buffer - useful to us
here because we want to handle that part ourselves.

(defun dired-find-file-other-opened-window ()
  (interactive)
  (let* ((file (dired-get-filename 'no-dir))
         (buffer (find-file-noselect file)))
    (set-window-buffer (frame-first-window) buffer)))

Hope that helps

-- 
john



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

* Re: set-window-buffer bugs
  2015-12-05 21:19 ` John Mastro
@ 2015-12-05 21:33   ` jenia.ivlev
  0 siblings, 0 replies; 4+ messages in thread
From: jenia.ivlev @ 2015-12-05 21:33 UTC (permalink / raw)
  To: help-gnu-emacs

Great. Works perfectly.

Thanks a lot John




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

end of thread, other threads:[~2015-12-05 21:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-12-05 20:52 set-window-buffer bugs jenia.ivlev
2015-12-05 21:04 ` jenia.ivlev
2015-12-05 21:19 ` John Mastro
2015-12-05 21:33   ` jenia.ivlev

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.