4

I want to set the require-final-newline for every buffer to nil. In my config file, I have :

(setq require-final-newline nil)
(setq-default require-final-newline nil)

It appears to set the global value correctly. However in every buffer that I open, the local value is still t. Using describe-variable, I get :

require-final-newline is a variable defined in `files.el'.
Its value is t
Original value was nil
Local in buffer myfile.js; global value is nil

files.el is in /usr/local/Cellar/emacs/24.5/share/emacs/24.5/lisp/, so I guess I should not modify it. How do I set this local value to nil ?

Cœur
  • 37,241
  • 25
  • 195
  • 267
nha
  • 17,623
  • 13
  • 87
  • 133

1 Answers1

7

Firstly, there are modes which forcibly set require-final-newline on the basis that those kinds of files require a final newline. I don't believe js-mode is one of them, however.

Presumably you have some custom config which is causing this, perhaps set via js-mode-hook or prog-mode-hook.

Confirm with emacs -Q that this is not default behaviour, and then you can set about tracking down the culprit (I would just M-x rgrep for require-final-newline in your elisp files).

phils
  • 71,335
  • 11
  • 153
  • 198
  • 1
    After grepping, the culprit is probably `editorconfig` (it is not picking up my config file, but seem to set default values). What did you mean by `emacs -Q ` ? Command line ? If yes I am on osx and launching emacs via the command line does not seem to work out of the box unfortunately. – nha Oct 02 '15 at 12:09
  • Yes, that means running `emacs -Q` on the command line. Perhaps the directory for the `emacs` executable isn't in your shell's `PATH` environment variable? If you know where it lives, you can just run `/path/to/emacs -Q` for the appropriate path. (If you think you've found the configuration culprit, though, you can presumably test that by removing/commenting the line(s) in question and starting Emacs normally.) – phils Oct 02 '15 at 12:15
  • Yes it is probably `editorconfig` so I accepted your answer. I will have to see if I can override it's behaviour, but that would be another question if I don't find it. Same for emacs from the CLI. Thanks ! – nha Oct 02 '15 at 12:32
  • 1
    ⁺¹. FTR: turns out, c-mode and c++-mode require a newline at the end too, strangely enough it seems to be required by the standard. See also [this source code comment](https://github.com/emacs-mirror/emacs/blob/5e86db05fc836e708e1e0f791270c961160ca5d3/lisp/progmodes/cc-vars.el#L898). – Hi-Angel Sep 23 '19 at 09:11
  • On 26.1, `mode-require-final-newline` defaults to `t` (seen on `emacs -Q`); I don't know enough emacs to figure out why. `require-final-newline` somehow inherits from it. Repeating the snippet from @nha with `mode-require-final-newline` fixes the issue, at least temporarily. – EML Oct 03 '21 at 11:37
  • @EML `mode-require-final-newline` should have a non-nil value, and major modes where a final newline is mandatory should be setting `require-final-newline` to that value. Major modes should not be setting `require-final-newline` at all if a final newline is optional. If you're disabling `require-final-newline` in a mode which uses `mode-require-final-newline` then you're probably making a mistake (at minimum you're in conflict with the major mode's author's belief that a final newline is necessary). – phils Oct 03 '21 at 11:50
  • Even in fundamental mode, `mode-require-final-newline` is `t`. Yes, I agree that I'm in conflict with the author, who has made a judgement about what a "line" (*any* "line") is in Linux. Correctly or incorrectly, though, I'd like to change this for some buffers with non-POSIX text files, and setting `require-final-newline` in my init file isn't doing it for me; it just copies the `mode-require-final-newline` instead. So I'm hacking it by instead setting `mode-require-final-newline` in my init file... confused. – EML Oct 03 '21 at 12:17
  • The value of `mode-require-final-newline` is irrelevant in major modes which do not enforce the use of `require-final-newline` (hence `require-final-newline` is `nil` in `fundamental-mode` by default). Which major mode(s) are you having actual problems with? – phils Oct 06 '21 at 14:05