1
votes

I am using org-mode to generate my PDF report. Each time the tex is generated, emacs asks me

Buffer hw1.tex<2> does not end in newline. Add one? (y or n) y

How can I get rid of this message and just add a new line automatically?

I tried setting mode-require-final-newline and require-final-newline to t, But this didn't change anything.

EDIT: I also tried setting mode-require-final-newline and require-final-newline to nil, and even though I can check that their value changed (via C-h v), the problem still persists.

Following the advice of the comments, I added a hook to change the value of those variables:

(add-hook 'org-mode-hook (lambda () (setq require-final-newline nil) (setq mode-require-final-newline nil)))

But again the problem persists.

EDIT:

Also, I think I found the command responsible for this:

(el-get 'sync my-packages)

, so I suspect some package I installed is missing with my configuration.

Here is the list of the packages I installed using el-get:

           auctex
           auto-complete
           autopair
           color-theme-solarized
           cdlatex-mode
           ecb
           ein
           elpy
           expand-region
           flycheck
           folding
           gnuplot-mode
           helm
           helm-descbinds
           jedi
           js2-mode
           jump-char
           key-chord
           latex-preview-pane
           lua-mode
           magit
           markdown-mode
           matlab-mode
           multiple-cursors
           ;nxhtml
           nyan-mode
           org-mode
           outshine
           popup
           popwin
           pyenv
           pydoc-info
           scss-mode
           yaml-mode
           yasnippet

Thanks!

1
It is the opposite -- i.e., (setq require-final-newline nil) -- should eliminate the prompt. To see what is happening and why, type M-x find-function RET basic-save-buffer RET See also M-x describe-variable RET require-final-newline RET How did I find this? I grepped the Emacs source-code for "does not end in newline". If a particular mode sets the value of require-final-newline, then you will likely need to reset the value to nil with a major-mode hook -- it happens so quickly, you won't notice the resetting. - lawlist
@lawlist Please turn your comment into a proper answer so it can be accepted as correct. - Thomas
@lawlist Can you please expand on why some modes reset the value of require-final-newline on the fly? - maroxe
I edited my response to include more details about some of the things I tried. - maroxe
(set (make-local-variable 'require-final-newline) mode-require-final-newline) derived by org-mode from outline-mode from text-mode (see answer below) takes a global variable require-final-newline and makes it buffer-local in org-mode. So when you set it in org-mode, your only setting it buffer-locally. Although beyond the scope of your question (yet related), you may someday be interested in using setq-default and setq-local to control the global and buffer-local values of a particular variable. - lawlist

1 Answers

2
votes

[The following solution was tested with no user configuration -- i.e., Emacs -Q -- using Emacs 24.5.1. Try loading Emacs with no user configuration and then add the solution and test. Then try it with a full user configuration -- if it doesn't work with a full user configuration, it may be necessary to bisect and comment out code until the culprit is found. It would probably be easier to just word-search the user-configuration for require-final-newline.]

As of the last stable public release of Emacs bearing version number 24.5.1, the default value of require-final-newline is nil. The doc-string of that variable states (in part) that "Certain major modes set this locally to the value obtained from mode-require-final-newline." The function basic-save-buffer in the library files.el checks the value of require-final-newline (among other criteria) when making a decision to prompt the user with (and require-final-newline (y-or-n-p (format "Buffer %s does not end in newline. Add one? " (buffer-name)))). When enabling org-mode with no user configuration -- e.g., Emacs -Q -- the value of require-final-newline is set locally in the buffer as t. The PARENT mode of org-mode is outline-mode; and the PARENT mode of outline-mode is text-mode. text-mode when enabled contains a line of code that looks like this: (set (make-local-variable 'require-final-newline) mode-require-final-newline). The variable mode-require-final-newline is defined in the library files.el, and the default value is t.

Now that we have done our due diligence, we can approach the problem from a couple of different angles. We could change the global value of mode-require-final-newline by changing it from t to nil: (setq mode-require-final-newline nil). Or, we could keep the new user-configuration buffer-local relating to org-mode:

(defun my-org-mode-config-fn ()
  (setq require-final-newline nil))

(add-hook 'org-mode-hook 'my-org-mode-config-fn)