Strona domowa

Witam na mojej prywatnej stronie internetowej!

[If this is all Polish to you, click here: English]

Uwaga: z oczywistych powodów nie mogę zagwarantować swojej nieomylności, choć staram się o zgodność tego, co piszę, z Prawdą. Jest również oczywiste, że nie gwarantuję takiej zgodności w przypadku komentarzy. Umieszczenie linku do strony spoza niniejszego serwisu nie musi oznaczać, że podzielam poglądy autora tej strony, a jedynie, że uważam ją za wartościową z takich czy innych powodów.

Marcin ‘mbork’ Borkowski

2018-09-23 add-dir-local-variable and dotted pairs

Last time I wrote about directory variables. I mentioned that the eval keyword apparently didn’t work with them.

Well, I was very wrong, and my mistake was a popular one. Since it is easy to fall for it, I decided to warn here about the pitfall.

Assume that you want Emacs to say “Hello” every time you visit some file. You say M-x add-dir-local-variable, type eval, then (message "Hello"), and get this in your file:

# Local Variables:
# eval: (message "Hello")
# End:

(Of course, the comment syntax may depend on the major mode.) All well.

Now assume that you want Emacs to say “Hello” every time you visit a file in some directory. You say M-x add-dir-local-variable, type the mode name (optionally), type eval and then (message "Hello"). Emacs created a .dir-locals.el file with these contents:

;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

  (eval message "Hello")))

What I did is I changed the last two lines to

  (eval (message "Hello"))))

thinking… Well, I’m not sure what and whether I was thinking. ;-)

But what I should have done was either leave it alone, or change it into the equivalent

  (eval . (message "Hello"))))

i.e., the “dotted pair” syntax. Of course, the docs say this (even if implicitly), and even give examples, although you have to look into the Elisp manual to learn the whole truth.

By the way, the workaround I found by trial and error (the one with anonymous functions) worked because, as the docs kindly say, “Anonymous functions are valid wherever function names are.” You can check it by evalling e.g. this:

((lambda (x) (message "Hello %s!" x)) "world")

Normally you wouldn’t want to do this, of course, but it works.

Last but not least, the thread I mentioned last week contains an interesting discussion on making add-dir-local-variable output the dotted-pair syntax for alists, and the consensus was that it can’t be done (at least not easily). A bit disappointing – but only a bit, since this is not a big deal for me anyway.

Take care and beware of dotted pairs and alists!

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page