emacs-devel
[Top][All Lists]
Advanced

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

Difficulties and confusion associated with .dir-locals.el


From: João Távora
Subject: Difficulties and confusion associated with .dir-locals.el
Date: Tue, 07 Mar 2023 12:12:20 +0000

Yuan Fu writes:

>> Reading the docs, rust-analyzer allows per-user configuration via
>> :initializationOptions.  The syntax and supported options are
>> usually the same but the difficulties and confusion associated
>> with ~/.dir-locals.el are not there.

Yes, ~/.dir-locals.el is confusing to many.  A minor point of confusion
is the ".el" extension: it's _not_ an Emacs Lisp program.  A bigger one
in the "dir locals" name: accurate but somewhat removed from its main
use which is "project preferences".

An even bigger are its syntax, the various ways to add to it, the scope
of the variables.

As a first idea, I think a menu command "Edit project preferences"
somewhere in the menu could alleviate the situation, bringing the user
into a new or existing ~/.dir-locals.el file in the current project's
root, maybe with a nice template.  Maybe the project.el people can make
this happen.

Then there's this simple enhancement of the current M-x
add-dir-local-variable could be a slight improvement, also geared
towards guiding the user to the file that she probably wants to change.

--- a/lisp/files-x.el
+++ b/lisp/files-x.el
@@ -439,7 +439,9 @@ modify-dir-local-variable
                        (car (last (dir-locals--all-files (car dir-or-cache))))
                      (cadr dir-or-cache)))
                   ;; Try to make a proper file-name.
-                  (t (expand-file-name dir-locals-file))))
+                  (t (expand-file-name dir-locals-file
+                                       (let ((p (project-current)))
+                                         (and p (project-root p)))))))
            variables)
       ;; I can't be bothered to handle this case right now.
       ;; Dir locals were set directly from a class.  You need to

But 'add-dir-local-variable' and 'add-file-local-variable' remain very
daunting ways to do edits to a .dir-locals.el file.

They ask you three questions in that cramped minibuffer (mode, variable
and value), and there's no way to go back if you make a mistake.
There's doesn't seem to be good way to say "all major modes" (the nil
value). It's also not clear that the value is going to be read, not
evaluated.  And there's no way to tell it to evaluate a value.  When
you're done, there is no good way to understand what has happened (is
the file saved?  Are my variables applied?)

People have expressed desire for a better dir/file-local setting
mechanism for a single specific directory-local variable (one from
Eglot), but I think we should look at the problem generally.

Here are a some more ideas.

1. Enhance a-d-l-v and a-f-l-v with an option to pop to the
   ~/.dir-locals.el directly to edit the value of the given variable,
   with point in place.

2. Make a new major mode for ~/.dir-locals.  For example, C-c C-c would
   save the file and offer to revert the affected buffers.

3. A Flymake backend for that major mode which informs which variables
   have safe or risky values.  This backend could also check the top
   level schema (the form must be an alist).

4. In the a-d-l-v minifuffer, possiblity to use evaluation instead of
   reading via some simple syntax, like starting the expression with a
   ',' (comma).

João



reply via email to

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