help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Dynamic let for lexical scope?


From: Michael Heerdegen
Subject: Re: Dynamic let for lexical scope?
Date: Wed, 13 Feb 2019 03:59:49 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]