changeset 12598:43ebbbe0299f

(decode_mode_spec): New arg spec_width. Use pint2str for %l and %c. New code to output ??. (display_mode_element): New var minendcol. Pass new arg to decode_mode_spec. (pint2str): New function.
author Richard M. Stallman <rms@gnu.org>
date Tue, 18 Jul 1995 23:07:23 +0000
parents c2f4101915eb
children 284b8763066d
files src/xdisp.c
diffstat 1 files changed, 57 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/xdisp.c	Tue Jul 18 22:12:31 1995 +0000
+++ b/src/xdisp.c	Tue Jul 18 23:07:23 1995 +0000
@@ -3235,6 +3235,7 @@
 	      }
 	    else /* c == '%' */
 	      {
+		register int minendcol;
 		register int spec_width = 0;
 
 		/* We can't allow -ve args due to the "%-" construct */
@@ -3247,9 +3248,12 @@
 		    spec_width = spec_width * 10 + (c - '0');
 		  }
 
-		spec_width += hpos;
-		if (spec_width > maxendcol)
-		  spec_width = maxendcol;
+		minendcol = hpos + spec_width;
+		if (minendcol > maxendcol)
+		  {
+		    spec_width = maxendcol - hpos;
+		    minendcol = maxendcol;
+		  }
 
 		if (c == 'M')
 		  hpos = display_mode_element (w, vpos, hpos, depth,
@@ -3257,13 +3261,14 @@
 					       Vglobal_mode_string);
 		else if (c != 0)
 		  {
-		    char *spec = decode_mode_spec (w, c, maxendcol - hpos);
+		    char *spec = decode_mode_spec (w, c, spec_width,
+						   maxendcol - hpos);
 		    if (frame_title_ptr)
-		      hpos = store_frame_title (spec, spec_width, maxendcol);
+		      hpos = store_frame_title (spec, minendcol, maxendcol);
 		    else
 		      hpos = display_string (w, vpos, spec, -1,
 					     hpos, 0, 1,
-					     spec_width, maxendcol);
+					     minendcol, maxendcol);
 		  }
 	      }
 	  }
@@ -3397,15 +3402,47 @@
   return hpos;
 }
 
+/* Write a null-terminated, right justified decimal representation of
+   the positive integer D to BUF using a minimal field width WIDTH.  */
+
+static void
+pint2str (buf, width, d)
+     register char *buf;
+     register int width;
+     register int d;
+{
+  register char *p = buf;
+  
+  if (d <= 0)
+      *p++ = '0';
+  else
+      while (d > 0)
+      {
+	  *p++ = d % 10 + '0';
+	  d /= 10;
+      }
+  for (width -= (int) (p - buf); width > 0; --width) *p++ = ' ';
+  *p-- = '\0';
+  while (p > buf)
+  {
+      d = *buf;
+      *buf++ = *p;
+      *p-- = d;
+  }
+}
+
 /* Return a string for the output of a mode line %-spec for window W,
-   generated by character C and width MAXWIDTH.  */
+   generated by character C.  SPEC_WIDTH is the field width when
+   padding to the left (%c, %l).  The value returned from this
+   function will later be truncated to width MAXWIDTH. */
 
 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
 
 static char *
-decode_mode_spec (w, c, maxwidth)
+decode_mode_spec (w, c, spec_width, maxwidth)
      struct window *w;
      register char c;
+     register int spec_width;
      register int maxwidth;
 {
   Lisp_Object obj;
@@ -3504,7 +3541,7 @@
       {
 	int col = current_column ();
 	XSETFASTINT (w->column_number_displayed, col);
-	sprintf (decode_mode_spec_buf, "%d", col);
+	pint2str (decode_mode_spec_buf, spec_width, col);
 	return decode_mode_spec_buf;
       }
 
@@ -3542,14 +3579,14 @@
 	/* If we decided that this buffer isn't suitable for line numbers, 
 	   don't forget that too fast.  */
 	if (EQ (w->base_line_pos, w->buffer))
-	  return "??";
+	  goto no_value;
 
 	/* If the buffer is very big, don't waste time.  */
 	if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
 	  {
 	    w->base_line_pos = Qnil;
 	    w->base_line_number = Qnil;
-	    return "??";
+	    goto no_value;
 	  }
 
 	if (!NILP (w->base_line_number)
@@ -3599,7 +3636,7 @@
 	      {
 		w->base_line_pos = w->buffer;
 		w->base_line_number = Qnil;
-		return "??";
+		goto no_value;
 	      }
 
 	    XSETFASTINT (w->base_line_number, topline - nlines);
@@ -3613,8 +3650,15 @@
 	line_number_displayed = 1;
 
 	/* Make the string to show.  */
-	sprintf (decode_mode_spec_buf, "%d", topline + nlines);
+	pint2str (decode_mode_spec_buf, spec_width, topline + nlines);
 	return decode_mode_spec_buf;
+    no_value:
+        {
+	  char* p = decode_mode_spec_buf;
+	  for (spec_width -= 2; spec_width > 0; --spec_width) *p++ = ' ';
+	  strcpy (p, "??");
+	  return decode_mode_spec_buf;
+	}
       }
       break;