Subject: Re: bug#16526: 24.3.50; scroll-conservatively & c-mode regression

From: Stefan Monnier <[email-masked]>
Date: Thu, 03 Jul 2014 10:59:58 -0400

Hi Alan,

Can you try the patch below (for the emacs-24 branch), which tries to
make the existing cache in find_defun_start do something useful in the
case where find_defun_start returns BEGV?

        Stefan

=== modified file 'src/syntax.c'
--- src/syntax.c	2014-02-08 05:12:47 +0000
+++ src/syntax.c	2014-07-03 14:57:04 +0000
@@ -530,17 +530,6 @@
 {
   ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
 
-  if (!open_paren_in_column_0_is_defun_start)
-    {
-      find_start_value = BEGV;
-      find_start_value_byte = BEGV_BYTE;
-      find_start_buffer = current_buffer;
-      find_start_modiff = MODIFF;
-      find_start_begv = BEGV;
-      find_start_pos = pos;
-      return BEGV;
-    }
-
   /* Use previous finding, if it's valid and applies to this inquiry.  */
   if (current_buffer == find_start_buffer
       /* Reuse the defun-start even if POS is a little farther on.
@@ -552,6 +541,13 @@
       && MODIFF == find_start_modiff)
     return find_start_value;
 
+  if (!open_paren_in_column_0_is_defun_start)
+    {
+      find_start_value = BEGV;
+      find_start_value_byte = BEGV_BYTE;
+      goto found;
+    }
+
   /* Back up to start of line.  */
   scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
 
@@ -582,13 +578,14 @@
   /* Record what we found, for the next try.  */
   find_start_value = PT;
   find_start_value_byte = PT_BYTE;
+  TEMP_SET_PT_BOTH (opoint, opoint_byte);
+
+ found:
   find_start_buffer = current_buffer;
   find_start_modiff = MODIFF;
   find_start_begv = BEGV;
   find_start_pos = pos;
 
-  TEMP_SET_PT_BOTH (opoint, opoint_byte);
-
   return find_start_value;
 }
 
@@ -841,6 +838,7 @@
   else
     {
       struct lisp_parse_state state;
+      bool adjusted = false;
     lossage:
       /* We had two kinds of string delimiters mixed up
 	 together.  Decode this going forwards.
@@ -852,6 +850,8 @@
 	{
 	  defun_start = find_defun_start (comment_end, comment_end_byte);
 	  defun_start_byte = find_start_value_byte;
+	  if (defun_start != BEGV)
+	    adjusted = true;
 	}
       do
 	{
@@ -860,6 +860,16 @@
 			      comment_end, TYPE_MINIMUM (EMACS_INT),
 			      0, Qnil, 0);
 	  defun_start = comment_end;
+	  if (!adjusted)
+	    {
+	      adjusted = true;
+	      find_start_value
+		= CONSP (state.levelstarts) ? XINT (XCAR (state.levelstarts))
+		: state.thislevelstart >= 0 ? state.thislevelstart
+		: find_start_value;
+	      find_start_value_byte = CHAR_TO_BYTE (find_start_value);
+	    }
+
 	  if (state.incomment == (comnested ? 1 : -1)
 	      && state.comstyle == comstyle)
 	    from = state.comstr_start;