all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* TIL: buffer-local variables are used over let-bound variables when using with-current-buffer in let
@ 2023-10-22  5:45 Rodrigo Morales
  2023-10-22  5:59 ` Eli Zaretskii
  0 siblings, 1 reply; 2+ messages in thread
From: Rodrigo Morales @ 2023-10-22  5:45 UTC (permalink / raw)
  To: uzibalqa via Users list for the GNU Emacs text editor

TIL: buffer-local variables are used over let-bound variables when using
with-current-buffer in let

Let's suppose we have a directory =/tmp/foo=

#+BEGIN_SRC elisp
(dired-create-directory "/tmp/foo")
#+END_SRC

Now, let's suppose we create a buffer while =/tmp/foo= is
=default-directory=.

#+BEGIN_SRC elisp
(let ((default-directory "/tmp/foo"))
 (get-buffer-create "*foo*"))
#+END_SRC

Now, let's suppose we have another directory =/tmp/bar=.

#+BEGIN_SRC elisp
(dired-create-directory "/tmp/bar")
#+END_SRC

The result of evaluation of the following sexp is =/tmp/foo=.
#+HEADER: :results value
#+BEGIN_SRC elisp
(let ((default-directory "/tmp/bar"))
 (with-current-buffer (get-buffer "*foo*")
  default-directory))
#+END_SRC

#+RESULTS:
#+begin_example
/tmp/foo
#+end_example

Some users would expect for the result to be  =/tmp/bar= because
=default-directory= has been modified in =let=. However that's not the case
because when we change to buffer =*foo*=, the variable =default-directory==
has another value.

I think this scenario arises when a variable is buffer-local and that
variable is modified in a =let= form and =with-current-buffer= is used
inside the =let= form. In such a case, the buffer-local value would be used
instead.


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

* Re: TIL: buffer-local variables are used over let-bound variables when using with-current-buffer in let
  2023-10-22  5:45 TIL: buffer-local variables are used over let-bound variables when using with-current-buffer in let Rodrigo Morales
@ 2023-10-22  5:59 ` Eli Zaretskii
  0 siblings, 0 replies; 2+ messages in thread
From: Eli Zaretskii @ 2023-10-22  5:59 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Rodrigo Morales <moralesrodrigo1100@gmail.com>
> Date: Sun, 22 Oct 2023 05:45:11 +0000
> 
> TIL: buffer-local variables are used over let-bound variables when using
> with-current-buffer in let
> 
> Let's suppose we have a directory =/tmp/foo=
> 
> #+BEGIN_SRC elisp
> (dired-create-directory "/tmp/foo")
> #+END_SRC
> 
> Now, let's suppose we create a buffer while =/tmp/foo= is
> =default-directory=.
> 
> #+BEGIN_SRC elisp
> (let ((default-directory "/tmp/foo"))
>  (get-buffer-create "*foo*"))
> #+END_SRC
> 
> Now, let's suppose we have another directory =/tmp/bar=.
> 
> #+BEGIN_SRC elisp
> (dired-create-directory "/tmp/bar")
> #+END_SRC
> 
> The result of evaluation of the following sexp is =/tmp/foo=.
> #+HEADER: :results value
> #+BEGIN_SRC elisp
> (let ((default-directory "/tmp/bar"))
>  (with-current-buffer (get-buffer "*foo*")
>   default-directory))
> #+END_SRC
> 
> #+RESULTS:
> #+begin_example
> /tmp/foo
> #+end_example
> 
> Some users would expect for the result to be  =/tmp/bar= because
> =default-directory= has been modified in =let=. However that's not the case
> because when we change to buffer =*foo*=, the variable =default-directory==
> has another value.

What you describe is precisely the expected behavior: 'let' rebinds
the binding that's currently in effect.  The ELisp manual says:

     *Warning:* When a variable has buffer-local bindings in one or more
  buffers, ‘let’ rebinds the binding that’s currently in effect.  For
  instance, if the current buffer has a buffer-local value, ‘let’
  temporarily rebinds that.  If no buffer-local bindings are in effect,
  ‘let’ rebinds the default value.  If inside the ‘let’ you then change to
  a different current buffer in which a different binding is in effect,
  you won’t see the ‘let’ binding any more.  And if you exit the ‘let’
  while still in the other buffer, you won’t see the unbinding occur
  (though it will occur properly).



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

end of thread, other threads:[~2023-10-22  5:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-22  5:45 TIL: buffer-local variables are used over let-bound variables when using with-current-buffer in let Rodrigo Morales
2023-10-22  5:59 ` Eli Zaretskii

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.