Subject: bug#19342: auto-fill scan-error in sh-mode

From: Stefan Monnier <[email-masked]>
Date: Sun, 29 Aug 2021 08:56:14 -0400

Lars Ingebrigtsen [2021-08-19 15:29:10] wrote:

> Glenn Morris <[email-masked]> writes:
>
>> Type a long string with spaces, past fill-column; eg:
>>
>> foo="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaa aaaaaaaaaa
>>
>> Auto-fill results in:
>>
>> Error: (scan-error "Containing expression ends prematurely" 75 75)
>
> This problem is still present in Emacs 28.  This is the backtrace:
>
> Debugger entered--Lisp error: (scan-error "Containing expression ends prematurely" 95 95)
>   signal(scan-error ("Containing expression ends prematurely" 95 95))
>   (if (and (car res) (= pos (point)) (not (if forw (eobp) (bobp)))) (
>   (let ((pos (point)) (res (if forw (smie-forward-sexp 'halfsexp) (sm
>   (while (/= n 0) (setq n (- n (if forw 1 -1))) (let ((pos (point)) (
>   (let ((forw (> n 0)) (forward-sexp-function nil)) (while (/= n 0) (
>   smie-forward-sexp-command(1)
>   forward-sexp(1)
>   (cond ((< 0 (length tok)) (assoc tok smie-grammar)) ((looking-at "\
>   (let ((tok (funcall smie-forward-token-function))) (cond ((< 0 (len
>   smie-indent-forward-token()
>   smie-indent-keyword()
>   smie--funcall(smie-indent-keyword)
>   run-hook-wrapped(smie--funcall smie-indent-keyword)
>   smie-indent-calculate()
>   smie-auto-fill(#f(compiled-function (&rest args) #<bytecode -0x1d0b7391ba5340b6>))
>
> The code that errors out is:
>
> (defun smie-indent-forward-token ()
> [...]
>      ((looking-at "\\s\"\\|\\s|")
>       (forward-sexp 1)
>
> That is, if we're auto-filling an unterminated string, it'll always bug
> out, apparently?

The behavior I see in the example above is that a newline
is inserted right before the last "aaaaaaaaaa", and an error message is
displayed in the echo area.

I'm not completely sure what behavior we'd like to see here instead.
Just a less scary error message?

We could also emit no error message at all, but since this is using
smie-auto-fill (i.e. syntax-aware auto-fill), it seems important to
point out that we couldn't use syntax-aware auto-filling because of
a problem in the syntax, which is what the error message is trying
to say.

        Stefan