Subject: bug#28598: 25.3; Errors in narrowed buffers in CC-mode

From: Alan Mackenzie <[email-masked]>
Date: Sun, 1 Oct 2017 18:55:49 +0000

Hello, George and John.

On Mon, Sep 25, 2017 at 10:51:00 -0700, John Wiegley wrote:
> >>>>> "GP" == George Plymale <[email-masked]> writes:

> GP> For those too lazy to follow the link, here's the gist: When I press `RET'
> GP> at the beginning or end of a narrowed buffer in CC-mode, I see errors like
> GP> this:

> GP> c-determine-limit: Args out of range: #<buffer dired.c>, 1, 1564

> Alan, do you have any thoughts on this one?

George, would you try the following patch, please?  The file
cc-engine.el is in ..../emacs-25.3/lisp/progmodes.  After applying the
patch, it is sufficient merely to recompile cc-engine.el.  If you want
any help with the patching/recompiling process, feel free to send me
private email.

The bug which you tripped over is already (almost) fixed in the emacs-26
branch of the git repository.  It is expected to be released some months
from now.

In the meantime, this patch should solve the problems in Emacs 25.3.
Please let me know if it doesn't.

--- cc-engine.20171001.eeel	2017-04-14 15:02:47.000000000 +0000
+++ cc-engine.el	2017-10-01 17:14:49.185254672 +0000
@@ -4682,18 +4682,29 @@
   ;; This doesn't preserve point.
   (let* ((pos (max (- start try-size) (point-min)))
 	 (base (c-state-semi-safe-place pos))
-	 (s (parse-partial-sexp base pos)))
-    (if (or (nth 4 s) (nth 3 s))	; comment or string
-	(nth 8 s)
+	 (s (save-restriction
+	      (widen)
+	      (parse-partial-sexp base pos)))
+	 (cand (if (or (nth 4 s) (nth 3 s)) ; comment or string
+		   (nth 8 s)
+		 pos)))
+    (if (>= cand (point-min))
+	cand
+      (parse-partial-sexp pos start nil nil s 'syntax-table)
       (point))))
 
 (defun c-determine-limit (how-far-back &optional start try-size)
-  ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
-  ;; (default point).  This is done by going back further in the buffer then
-  ;; searching forward for literals.  The position found won't be in a
-  ;; literal.  We start searching for the sought position TRY-SIZE (default
-  ;; twice HOW-FAR-BACK) bytes back from START.  This function must be fast.
-  ;; :-)
+  ;; Return a buffer position HOW-FAR-BACK non-literal characters from
+  ;; START (default point).  The starting position, either point or
+  ;; START may not be in a comment or string.
+  ;;
+  ;; The position found will not be before POINT-MIN and won't be in a
+  ;; literal.
+  ;;
+  ;; We start searching for the sought position TRY-SIZE (default
+  ;; twice HOW-FAR-BACK) bytes back from START.
+  ;;
+  ;; This function must be fast.  :-)
   (save-excursion
     (let* ((start (or start (point)))
 	   (try-size (or try-size (* 2 how-far-back)))
@@ -4715,7 +4726,8 @@
 		 'syntax-table))	; stop-comment
 
 	;; Gather details of the non-literal-bit - starting pos and size.
-	(setq size (- (if (or (nth 4 s) (nth 3 s))
+	(setq size (- (if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
+			      (nth 3 s))
 			  (nth 8 s)
 			(point))
 		      pos))
@@ -4723,7 +4735,8 @@
 	    (setq stack (cons (cons pos size) stack)))
 
 	;; Move forward to the end of the comment/string.
-	(if (or (nth 4 s) (nth 3 s))
+	(if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
+		(nth 3 s))
 	    (setq s (parse-partial-sexp
 		     (point)
 		     start
@@ -4747,6 +4760,8 @@
 	(+ (car elt) (- count how-far-back)))
        ((eq base (point-min))
 	(point-min))
+       ((> base (- start try-size)) ; Can only happen if we hit point-min.
+	(car elt))
        (t
 	(c-determine-limit (- how-far-back count) base try-size))))))

> -- 
> John Wiegley                  GPG fingerprint = 4710 CF98 AF9B 327B B80F
> http://newartisans.com                          60E1 46C4 BD1A 7AC1 4BA2

-- 
Alan Mackenzie (Nuremberg, Germany).