Subject: Re: bug#58506: Use ".dir-locals.eld" and ".dir-locals-2.eld" when they exist

From: Philip Kaludercic <[email-masked]>
Date: Sat, 15 Oct 2022 11:35:11 +0000

Lars Ingebrigtsen <[email-masked]> writes:

> Philip Kaludercic <[email-masked]> writes:
>
>> My only objection to "something like" this is that it appears to be Lisp
>> code, and people could get surprised when they try to add something that
>> isn't (eg. a regular `add-to-list' call that had a quote -- beginners
>> often get tripped up on these minor syntactic points).
>
> Then I guess `add-to-list' wouldn't be a good "directive" here.

Another idea could be to take inspiration from Guile's "Sandboxed
Evaluation"[0] and provide a "safe subset" of Elisp that can be
evaluated (with some additional checks).

E.g. the following would allow evaluating `add-to-list' if the list if
safe and the value is self-evaluating:

--8<---------------cut here---------------start------------->8---
(cl-defmethod safe-eval ((_fn (eql 'add-to-list)) &rest args)
  (when-let* ((list-var (nth 0 args))
	      (element (nth 1 args))
	      ((macroexp-const-p element))
	      (append (nth 2 args))
	      (new-list (if append
			    (append (symbol-value list-var) (list element))
			  (cons element (symbol-value list-var))))
	      ((safe-local-variable-p list-var new-list)))
    (add-to-list list-var element append)))
--8<---------------cut here---------------end--------------->8---

[0] https://www.gnu.org/software/guile/docs/docs-2.2/guile-ref/Sandboxed-Evaluation.html

>>> And we'd make the parser backwards/forwards compatible -- i.e., elements
>>> that are unknown to the Emacs version running would just be ignored.
>>
>> Ideally there would be a warning or some way you could find out that
>> variables aren't loaded, e.g. if you have a typo in
>> `blink-parenthesis-mode' vs. `blink-parentheses-mode'.
>
> Well, that would make things less backwards/forwards compatible --
> people would be getting warnings when flipping between different Emacs
> versions.

No, what I had in mind was not to trigger warnings but either to
highlight unused variables or provide a command that would check it for
you.