all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Dynamic let for lexical scope?
@ 2019-02-12 23:23 Hi-Angel
  2019-02-13  2:59 ` Michael Heerdegen
  0 siblings, 1 reply; 3+ messages in thread
From: Hi-Angel @ 2019-02-12 23:23 UTC (permalink / raw)
  To: help-gnu-emacs

I have the following function:

(defun sort-lines-nocase (beg end)
  (let ((sort-fold-case t))
    (sort-lines nil beg end)))

It temporarily changes sort-fold-case. Now, when compiled with

;;; -*- lexical-binding: t -*-

at the top, it causes a warning "Unused lexical variable ‘sort-fold-case’".

I wonder, how do I temporarily change the global sort-fold-case
symbol, and then change it back, aside of surrounding the code with
multiple setq?



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

* Re: Dynamic let for lexical scope?
  2019-02-12 23:23 Dynamic let for lexical scope? Hi-Angel
@ 2019-02-13  2:59 ` Michael Heerdegen
  2019-02-13  6:39   ` Hi-Angel
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Heerdegen @ 2019-02-13  2:59 UTC (permalink / raw)
  To: Hi-Angel; +Cc: help-gnu-emacs

Hi-Angel <hiangel999@gmail.com> writes:

> I have the following function:
>
> (defun sort-lines-nocase (beg end)
>   (let ((sort-fold-case t))
>     (sort-lines nil beg end)))
>
> It temporarily changes sort-fold-case. Now, when compiled with
>
> ;;; -*- lexical-binding: t -*-
>
> at the top, it causes a warning "Unused lexical variable
> ‘sort-fold-case’".

The canonical answer is: add (defvar sort-fold-case) to get local
dynamical binding:

(defun sort-lines-nocase (beg end)
  (defvar sort-fold-case)
  (let ((sort-fold-case t))
    (sort-lines nil beg end)))

In your case, I guess the problem is that `sort-lines' is autoloaded and
the variable is not yet declared when you compile (or evaluate for the
first time).  So you could also add

  (eval-when-compile (require 'sort))

It's a matter of taste which alternative is better or cleaner.  If you
use more stuff from sort.el, it's probably better to `require' it.


Michael.



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

* Re: Dynamic let for lexical scope?
  2019-02-13  2:59 ` Michael Heerdegen
@ 2019-02-13  6:39   ` Hi-Angel
  0 siblings, 0 replies; 3+ messages in thread
From: Hi-Angel @ 2019-02-13  6:39 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

Thank you!

On Wed, 13 Feb 2019 at 05:59, Michael Heerdegen
<michael_heerdegen@web.de> wrote:
>
> Hi-Angel <hiangel999@gmail.com> writes:
>
> > I have the following function:
> >
> > (defun sort-lines-nocase (beg end)
> >   (let ((sort-fold-case t))
> >     (sort-lines nil beg end)))
> >
> > It temporarily changes sort-fold-case. Now, when compiled with
> >
> > ;;; -*- lexical-binding: t -*-
> >
> > at the top, it causes a warning "Unused lexical variable
> > ‘sort-fold-case’".
>
> The canonical answer is: add (defvar sort-fold-case) to get local
> dynamical binding:
>
> (defun sort-lines-nocase (beg end)
>   (defvar sort-fold-case)
>   (let ((sort-fold-case t))
>     (sort-lines nil beg end)))
>
> In your case, I guess the problem is that `sort-lines' is autoloaded and
> the variable is not yet declared when you compile (or evaluate for the
> first time).  So you could also add
>
>   (eval-when-compile (require 'sort))
>
> It's a matter of taste which alternative is better or cleaner.  If you
> use more stuff from sort.el, it's probably better to `require' it.
>
>
> Michael.



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

end of thread, other threads:[~2019-02-13  6:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-02-12 23:23 Dynamic let for lexical scope? Hi-Angel
2019-02-13  2:59 ` Michael Heerdegen
2019-02-13  6:39   ` Hi-Angel

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.