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

From: Stefan Monnier <[email-masked]>
Date: Fri, 06 May 2022 08:34:57 -0400

Lars Ingebrigtsen [2022-05-06 14:26:38] wrote:

> Stefan Monnier <[email-masked]> writes:
>
>>> Sure, sounds simpler...  uhm...  but what would a good point-max be
>>> here?  Just the `fc' point?
>>
>> I'd think `point-max` does the trick.
>
> Tried that, and it leads to breakages in other parts of the loop:
>
> Debugger entered--Lisp error: (scan-error "Containing expression ends prematurely" 74 74)
>   signal(scan-error ("Containing expression ends prematurely" 74 74))
>   smie-forward-sexp-command(1)
>   forward-sexp(1)
>   smie-indent-forward-token()
>   smie-auto-fill(#f(compiled-function (&rest args) #<bytecode -0x1ddee5fc6c36b776>))
>   apply(smie-auto-fill #f(compiled-function (&rest args) #<bytecode -0x1ddee5fc6c36b776>) nil)
>   #f(advice smie-auto-fill :around #f(compiled-function (&rest args)
> #<bytecode -0x1ddee5fc6c36b776>))()
>   internal-auto-fill()

I can't begin to imagine why we'd get that.
But how 'bout the patch below?

        Stefan

diff --git a/lisp/emacs-lisp/smie.el b/lisp/emacs-lisp/smie.el
index 2bab1319132..0da403c7053 100644
--- a/lisp/emacs-lisp/smie.el
+++ b/lisp/emacs-lisp/smie.el
@@ -1862,8 +1862,10 @@ smie-auto-fill
                          ;; here may not be accurate, but in practice it seems
                          ;; to work well enough.
                          (skip-chars-forward " \t")
-                         (let* ((newcol (smie-indent-calculate))
-                                (newgain (- (current-column) newcol)))
+                         (let* ((newgain (condition-case nil
+                                             (- (current-column)
+                                                (smie-indent-calculate))
+                                           (scan-error -1)))
                            (when (> newgain gain)
                              (setq gain newgain)
                              (setq bsf (point)))))