all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: John Mastro <john.b.mastro@gmail.com>
To: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Cc: "jenia.ivlev" <jenia.ivlev@gmail.com>
Subject: Re: set-window-buffer bugs
Date: Sat, 5 Dec 2015 13:19:52 -0800	[thread overview]
Message-ID: <CAOj2CQQ-Le3-Sw-+MzfXy_U_cHfnSAWdG0mjiD23cXTWtzK-VA@mail.gmail.com> (raw)
In-Reply-To: <87fuzgboub.fsf@gmail.com>

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



  parent reply	other threads:[~2015-12-05 21:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2015-12-05 21:33   ` jenia.ivlev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAOj2CQQ-Le3-Sw-+MzfXy_U_cHfnSAWdG0mjiD23cXTWtzK-VA@mail.gmail.com \
    --to=john.b.mastro@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=jenia.ivlev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.