A.Ellett
A.Ellett

Reputation: 373

I cannot get Emacs to stop asking me about adding a newline at the end of file

Emacs keeps on asking me

Buffer text.txt does not end in newline.  And one? (y or n)

I am confused about why I cannot stop Emacs from making this inquiry. I've set both

require-final-newline
mode-require-final-newline

to nil in a hook to tex-mode.

I've verified that it's value is indeed nil when I'm working in my file. But nevertheless, Emacs continues to make the inquiry.

I've looked at the code for basic-save-buffer. The relevant part of the code is:

       (or (eq require-final-newline t)
           (eq require-final-newline 'visit-save)
           (and require-final-newline
            (y-or-n-p
             (format "Buffer %s does not end in newline.  Add one? "
                 (buffer-name)))))

It seems that Emacs should never get to the y-or-n-p since it appears as the second part of an and test where the first part evaluates to nil.

What's going on here?

My Emacs is GNU 27.2.

I've played a bit rewriting this code. As an example,

       (or (eq require-final-newline t)
           (eq require-final-newline 'visit-save)
           (and nil
            (y-or-n-p
             (format "Buffer %s does not end in newline.  Add one? "
                 (buffer-name)))))

Behaves as I would expect.

I've also tried something like:

       (or (eq require-final-newline t)
           (eq require-final-newline 'visit-save)
           (and require-final-newline
                        (progn
                          (message (concat "[2] require-final-newline: " (format "%S" require-final-newline)))
                          (sit-for 1)
                          t)
            (y-or-n-p
             (format "Buffer %s does not end in newline.  Add one? "
                 (buffer-name)))))

And the message reports that require-final-newline is nil but the only way to get to the message is for it to be t.

What's going on?

UPDATE

I am able to go in and customize the variable. And when I do that and set it to Don't add newlines, the annoying question goes away. But, I'm not happy with this. That seems to set this value globally regardless of the mode I'm in. That's not what I want. It's just for specific files and when in tex-mode that I want this to apply.

Further update:

@phils 's questions have been very useful, actually, for me to begin to debug what's going on here.

I think I've located the problem. I'm still interested how to solve it.

I'm using a .dir-locals.el to set a number of parameters for the local files I'm working on. If I remove the .dir-locals.el file and set things via hooks (as I formerly believed I was doing), everything behaves properly.

However, if I use the .dir-locals.el file set parameters, then things stop working properly. In the .dir-locals.el file I have the following line:

(nil . ((require-final-newline nil)))

This seems to be the problem. What's happening is that instead of require-final-newline getting set to nil it is getting set to (nil).

I can stick with the use of a hook to accomplish what I want. But, now I'm curious. Am I getting the syntax for .dir-locals.el wrong. Here's my full .dir-locals.el file

(
 (nil . ((fill-column . 70)))
 (nil . ((mode .tex)))
 (nil . ((require-final-newline nil)))
)

Upvotes: 1

Views: 383

Answers (1)

A.Ellett
A.Ellett

Reputation: 373

The problem I encountered is due to a syntax error in my .dir-locals.el file.

I have a line in that file which reads

(nil . ((require-final-newline nil)))

that is wrong. It sets require-final-newline to (nil), not nil.

If I correct the syntax to

(nil . ((require-final-newline . nil)))

everything works as desired and as expected.

Upvotes: 2

Related Questions