1

If I have a long line in a javascript file such as:

  let args = [options.loadRaw ? buffer : path.resolve(filePath)].concat(options.commands);

At some point when I keep adding to that line, it auto-formats into:

  let args = [options.loadRaw ? buffer :
              path.resolve(filePath)].concat(options.commands); // test

I'm trying to figure out what is causing this, because it's pretty jarring. I'd rather run this myself manually when I want to like with clang-format. I'm using js2-mode so I have a feeling it's that but I can't figure out what it may be or if it can be disabled.

I know it's pretty vague but I don't know what else to say. I'm mainly wondering if there's a way I can find out for myself. I did C-h k before pressing the space key but it's just self-insert-command, though I noticed that there's this in the help buffer for that function:

After insertion, the value of ‘auto-fill-function’ is called if the
‘auto-fill-chars’ table has a non-nil value for the inserted character.

But I checked and auto-fill-function is nil, otherwise that definitely seemed like the culprit. Then I saw this at the end of the help buffer:

At the end, it runs ‘post-self-insert-hook’.

Whose value is:

(sp--post-self-insert-hook-handler helm-find-files--reset-level-tree electric-pair-post-self-insert-function electric-indent-post-self-insert-function blink-paren-post-self-insert-function)

I'm not sure if this is correct, but I did (setq post-self-insert-hook nil) in the buffer and then pressed space and it still re-formatted the line, so maybe it's something else? I'm entirely at a loss.

I'm not sure if this is related, but to see if it was js2-mode buffers I also just noticed some other behavior when in the *stratch* buffer. If I type out a sexp with just one repeated over and over, once I get to the fill-column it starts adding spaces, like this:

(one one one one one one one one one one one one one one one one one one one one      one one one one one)
                                                                                ^ fill column here (column 80)

That's certainly different from what happens in js2-mode buffers though, where the line is entirely re-formatted onto separate lines so that it fits within the fill-column.

I'm on emacs 25 in case that matters.

EDIT: Disabling auto-fill-mode fixes the issue. The problem is that I want to keep auto-fill-mode. It looks like js2-line-break is the culprit:

  (set (make-local-variable 'comment-line-break-function) #'js2-line-break)

My configuration of auto-fill-mode is:

  (defun my-prog-auto-fill ()
    (setq-local comment-auto-fill-only-comments t)
    (auto-fill-mode 1))

  (add-hook 'prog-mode-hook #'my-prog-auto-fill)

What I don't understand is why, if I setup auto-fill to only apply to comments, and the variable that js2-line-break is being assigned to is called comment-line-break-function which gives the impression that it only applies when within a comment, why then is js2-line-break being called despite not being within a comment?

1 Answer 1

1

So this was due to auto-fill-mode. I was under the mistaken impression that comment-auto-fill-only-comments would make auto-fill-mode only apply to comments, but actually it seems like it only applies to comment-indent-new-line.

The problem is that in js2-mode this is set:

(set (make-local-variable 'comment-line-break-function) #'js2-line-break)

Then default-indent-new-line calls that:

(funcall comment-line-break-function soft)

and default-indent-new-line is called by do-auto-fill, the default function used for filling.

I ended up fixing my setup so that auto-fill-mode only applies to comments. I'm not sure if this approach is correct, but it seems to work:

  (defun my-fill-nobreak-predicate ()
    (not (nth 4 (syntax-ppss))))

  (defun my-prog-auto-fill ()
    (setq-local fill-nobreak-predicate #'my-fill-nobreak-predicate)
    (auto-fill-mode 1))

  (add-hook 'prog-mode-hook #'my-prog-auto-fill)

I wish there was a separate built-in mode like auto-fill-prog-mode, so that I could still opt-in and toggle regular auto-fill-mode whenever I felt like it. But this will do for now I suppose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.