emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Buffer size indicator on the mode line.


From: Lute Kamstra
Subject: Re: Buffer size indicator on the mode line.
Date: Tue, 02 Sep 2003 11:42:38 +0200
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3.50 (gnu/linux)

Lute Kamstra <address@hidden> writes:

> I now implemented a human readable version as well (see patch
> below).  The size indicator now says "19% of 47k" instead.  I stole
> the algorithm from gnulib's human.c, written by Paul Eggert and
> Larry McVoy.  Is it customary to document such origins?  If yes,
> how?

I rewrote the function from scratch as the gnulib function has a bug
with respect to rounding.

> I'll also document the changes in the Emacs Manual and the Emacs Lisp
> Reference Manual.

Done.

If nobody objects, I'll commit the patch in a few days.

  Lute.


Index: etc/NEWS
===================================================================
RCS file: /cvsroot/emacs/emacs/etc/NEWS,v
retrieving revision 1.846
diff -c -r1.846 NEWS
*** etc/NEWS    1 Sep 2003 18:51:17 -0000       1.846
--- etc/NEWS    2 Sep 2003 09:27:10 -0000
***************
*** 1448,1453 ****
--- 1448,1457 ----
  
  * New modes and packages in 21.4
  
+ +++
+ ** The new global minor mode `size-indication-mode' (off by default)
+ shows the size of accessible part of the buffer on the mode line.
+ 
  ** GDB-Script-mode is used for files like .gdbinit.
  
  ---
***************
*** 2483,2488 ****
--- 2487,2497 ----
  
  ** Mode line display ignores text properties in the value
  of a variable whose `risky-local-variables' property is nil.
+ 
+ +++
+ ** The new `%i' and `%I' constructs for `mode-line-format' can be used
+ to display the size of the accessible part of the buffer on the mode
+ line.
  
  ---
  ** Indentation of simple and extended loop forms has been added to the
Index: lisp/ChangeLog
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/ChangeLog,v
retrieving revision 1.5371
diff -c -r1.5371 ChangeLog
*** lisp/ChangeLog      1 Sep 2003 21:36:56 -0000       1.5371
--- lisp/ChangeLog      2 Sep 2003 09:27:15 -0000
***************
*** 69,74 ****
--- 69,79 ----
        * simple.el (blink-matching-open): Work correctly on chars that
        are designated as parens through the syntax-table text property.
  
+ 2003-08-29  Lute Kamstra  <address@hidden>
+ 
+       * simple.el (size-indication-mode): New.
+       * bindings.el (mode-line-position): Add buffer size indicator.
+ 
  2003-08-29  Thierry Emery <address@hidden>  (tiny change)
  
        * kinsoku.el (kinsoku-longer, kinsoku-shorter): Do not choose a
Index: lisp/bindings.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/bindings.el,v
retrieving revision 1.122
diff -c -r1.122 bindings.el
*** lisp/bindings.el    1 Sep 2003 15:45:08 -0000       1.122
--- lisp/bindings.el    2 Sep 2003 09:27:15 -0000
***************
*** 312,317 ****
--- 312,319 ----
  
    (setq-default mode-line-position
      `((-3 . ,(propertize "%p" 'help-echo help-echo))
+       (size-indication-mode 
+        (8 ,(propertize " of %I" 'help-echo help-echo)))
        (line-number-mode
         ((column-number-mode
         (10 ,(propertize " (%l,%c)" 'help-echo help-echo))
Index: lisp/simple.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/simple.el,v
retrieving revision 1.619
diff -c -r1.619 simple.el
*** lisp/simple.el      1 Sep 2003 15:45:14 -0000       1.619
--- lisp/simple.el      2 Sep 2003 09:27:17 -0000
***************
*** 3454,3459 ****
--- 3454,3466 ----
  When Column Number mode is enabled, the column number appears
  in the mode line."
    :global t :group 'editing-basics :require nil)
+ 
+ (define-minor-mode size-indication-mode
+   "Toggle Size Indication mode.
+ With arg, turn Size Indication mode on iff arg is positive.  When
+ Size Indication mode is enabled, the size of the accessible part
+ of the buffer appears in the mode line."
+   :global t :group 'editing-basics :require nil)
  
  (defgroup paren-blinking nil
    "Blinking matching of parens and expressions."
Index: lispref/ChangeLog
===================================================================
RCS file: /cvsroot/emacs/emacs/lispref/ChangeLog,v
retrieving revision 1.90
diff -c -r1.90 ChangeLog
*** lispref/ChangeLog   1 Sep 2003 15:45:40 -0000       1.90
--- lispref/ChangeLog   2 Sep 2003 09:27:17 -0000
***************
*** 3,8 ****
--- 3,13 ----
        * display.texi (Overlay Properties): Clarify how priorities
        affect use of the properties.
  
+ 2003-08-29  Lute Kamstra  <address@hidden>
+ 
+       * modes.texi (%-Constructs): Document new `%i' and `%I'
+       constructs.
+ 
  2003-08-19  Luc Teirlinck  <address@hidden>
  
        * customize.texi (Type Keywords): Correct the description of
Index: lispref/modes.texi
===================================================================
RCS file: /cvsroot/emacs/emacs/lispref/modes.texi,v
retrieving revision 1.62
diff -c -r1.62 modes.texi
*** lispref/modes.texi  1 Sep 2003 15:45:41 -0000       1.62
--- lispref/modes.texi  2 Sep 2003 09:27:17 -0000
***************
*** 1481,1486 ****
--- 1481,1495 ----
  The title (only on a window system) or the name of the selected frame.
  @xref{Window Frame Parameters}.
  
+ @item %i
+ The size of the accessible part of the current buffer; basically
+ @code{(- (point-max) (point-min))}.
+ 
+ @item %I
+ Like @samp{%i}, but the size is printed in a more readable way by using
+ @samp{k} for 10^3, @samp{M} for 10^6, @samp{G} for 10^9, etc., to
+ abbreviate.
+ 
  @item %l
  The current line number of point, counting within the accessible portion
  of the buffer.
Index: man/ChangeLog
===================================================================
RCS file: /cvsroot/emacs/emacs/man/ChangeLog,v
retrieving revision 1.212
diff -c -r1.212 ChangeLog
*** man/ChangeLog       1 Sep 2003 15:45:44 -0000       1.212
--- man/ChangeLog       2 Sep 2003 09:27:17 -0000
***************
*** 2,7 ****
--- 2,15 ----
  
        * misc.texi (Saving Emacs Sessions): Correct previous change.
  
+ 2003-08-29  Lute Kamstra  <address@hidden>
+ 
+       * screen.texi (Mode Line): Say that POS comes before LINE.
+       Mention `size-indication-mode'.
+       * display.texi (Optional Mode Line): Document
+       `size-indication-mode'.
+       * basic.texi (Position Info): Mention `size-indication-mode'.
+ 
  2003-08-26  Per Abrahamsen  <address@hidden>
  
        * widget.texi (User Interface): Explain the need of static text
Index: man/basic.texi
===================================================================
RCS file: /cvsroot/emacs/emacs/man/basic.texi,v
retrieving revision 1.37
diff -c -r1.37 basic.texi
*** man/basic.texi      1 Sep 2003 15:45:45 -0000       1.37
--- man/basic.texi      2 Sep 2003 09:27:18 -0000
***************
*** 572,577 ****
--- 572,580 ----
  @item M-x hl-line-mode
  Enable or disable highlighting of the current line.  @xref{Cursor
  Display}.
+ @item M-x size-indication-mode
+ Toggle automatic display of the size of the buffer.
+ @xref{Optional Mode Line}.
  @end table
  
  @findex what-page
Index: man/display.texi
===================================================================
RCS file: /cvsroot/emacs/emacs/man/display.texi,v
retrieving revision 1.71
diff -c -r1.71 display.texi
*** man/display.texi    1 Sep 2003 15:45:45 -0000       1.71
--- man/display.texi    2 Sep 2003 09:27:18 -0000
***************
*** 756,768 ****
  @node Optional Mode Line
  @section Optional Mode Line Features
  
  @cindex line number display
  @cindex display of line number
  @findex line-number-mode
    The current line number of point appears in the mode line when Line
  Number mode is enabled.  Use the command @kbd{M-x line-number-mode} to
  turn this mode on and off; normally it is on.  The line number appears
! before the buffer percentage @var{pos}, with the letter @samp{L} to
  indicate what it is.  @xref{Minor Modes}, for more information about
  minor modes and about how to use this command.
  
--- 756,789 ----
  @node Optional Mode Line
  @section Optional Mode Line Features
  
+ @cindex buffer size display
+ @cindex display of buffer size
+ @findex size-indication-mode
+   The buffer percentage @var{pos} indicates the percentage of the buffer
+ above the top of the window.  You can additionally display the size of
+ the buffer by turning on Size Indication mode.  The size will be
+ displayed immediately following the buffer percentage like this:
+ 
+ @example
+ @var{POS} of @var{SIZE}
+ @end example
+ 
+ @noindent
+ Here @var{SIZE} is the human readable representation of the number of
+ characters in the buffer, which means that @samp{k} for 10^3, @samp{M}
+ for 10^6, @samp{G} for 10^9, etc., are used to abbreviate.
+ 
+ @cindex narrowing, and buffer size display
+   If you have narrowed the buffer (@pxref{Narrowing}), the size of the
+ accessible part of the buffer is shown.
+ 
  @cindex line number display
  @cindex display of line number
  @findex line-number-mode
    The current line number of point appears in the mode line when Line
  Number mode is enabled.  Use the command @kbd{M-x line-number-mode} to
  turn this mode on and off; normally it is on.  The line number appears
! after the buffer percentage @var{pos}, with the letter @samp{L} to
  indicate what it is.  @xref{Minor Modes}, for more information about
  minor modes and about how to use this command.
  
Index: man/screen.texi
===================================================================
RCS file: /cvsroot/emacs/emacs/man/screen.texi,v
retrieving revision 1.19
diff -c -r1.19 screen.texi
*** man/screen.texi     1 Sep 2003 15:45:46 -0000       1.19
--- man/screen.texi     2 Sep 2003 09:27:18 -0000
***************
*** 189,195 ****
    Normally, the mode line looks like this:
  
  @example
! address@hidden:@var{ch}  @var{buf}      @var{line} @var{pos}   (@var{major} 
@var{minor})------
  @end example
  
  @noindent
--- 189,195 ----
    Normally, the mode line looks like this:
  
  @example
! address@hidden:@var{ch}  @var{buf}      @var{pos} @var{line}   (@var{major} 
@var{minor})------
  @end example
  
  @noindent
***************
*** 211,229 ****
  takes place in.  When we speak of what some command does to ``the
  buffer,'' we are talking about the current buffer.
  
-   @var{line} is @samp{L} followed by the current line number of point.
- This is present when Line Number mode is enabled (which it normally is).
- You can optionally display the current column number too, by turning on
- Column Number mode (which is not enabled by default because it is
- somewhat slower).  @xref{Optional Mode Line}.
- 
    @var{pos} tells you whether there is additional text above the top of
  the window, or below the bottom.  If your buffer is small and it is all
  visible in the window, @var{pos} is @samp{All}.  Otherwise, it is
  @samp{Top} if you are looking at the beginning of the buffer, @samp{Bot}
  if you are looking at the end of the buffer, or @address@hidden, where
! @var{nn} is the percentage of the buffer above the top of the
! address@hidden
  
    @var{major} is the name of the @dfn{major mode} in effect in the
  buffer.  At any time, each buffer is in one and only one of the possible
--- 211,230 ----
  takes place in.  When we speak of what some command does to ``the
  buffer,'' we are talking about the current buffer.
  
    @var{pos} tells you whether there is additional text above the top of
  the window, or below the bottom.  If your buffer is small and it is all
  visible in the window, @var{pos} is @samp{All}.  Otherwise, it is
  @samp{Top} if you are looking at the beginning of the buffer, @samp{Bot}
  if you are looking at the end of the buffer, or @address@hidden, where
! @var{nn} is the percentage of the buffer above the top of the window.
! With Size Indication mode, you can display the size of the buffer as
! well.  @xref{Optional Mode Line}.
! 
!   @var{line} is @samp{L} followed by the current line number of point.
! This is present when Line Number mode is enabled (which it normally is).
! You can optionally display the current column number too, by turning on
! Column Number mode (which is not enabled by default because it is
! somewhat slower).  @xref{Optional Mode Line}.
  
    @var{major} is the name of the @dfn{major mode} in effect in the
  buffer.  At any time, each buffer is in one and only one of the possible
***************
*** 303,310 ****
  @xref{Variables}, for an explanation of how to set variables.
  
    @xref{Optional Mode Line}, for features that add other handy
! information to the mode line, such as the current column number of
! point, the current time, and whether new mail for you has arrived.
  
  The mode line is mouse-sensitive; when you move the mouse across
  various parts of it, Emacs displays help text to say what a click in
--- 304,312 ----
  @xref{Variables}, for an explanation of how to set variables.
  
    @xref{Optional Mode Line}, for features that add other handy
! information to the mode line, such as the size of the buffer, the
! current column number of point, the current time, and whether new mail
! for you has arrived.
  
  The mode line is mouse-sensitive; when you move the mouse across
  various parts of it, Emacs displays help text to say what a click in
Index: src/ChangeLog
===================================================================
RCS file: /cvsroot/emacs/emacs/src/ChangeLog,v
retrieving revision 1.3359
diff -c -r1.3359 ChangeLog
*** src/ChangeLog       1 Sep 2003 20:59:07 -0000       1.3359
--- src/ChangeLog       2 Sep 2003 09:27:20 -0000
***************
*** 53,58 ****
--- 53,65 ----
  
        * abbrev.c (Fexpand_abbrev): Insert before deleting.
  
+ 2003-08-29  Lute Kamstra  <address@hidden>
+ 
+       * xdisp.c (pint2hrstr): New function.
+       (decode_mode_spec): Add `%i' and `%I' specs.
+       * buffer.c (syms_of_buffer): Document `%i' and `%I' constructs
+       for `mode-line-format'.
+ 
  2003-08-29  Gerd Moellmann  <address@hidden>
  
        * xfns.c (lookup_rgb_color): Handle TrueColor visuals specially.
Index: src/buffer.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/buffer.c,v
retrieving revision 1.437
diff -c -r1.437 buffer.c
*** src/buffer.c        1 Sep 2003 15:45:51 -0000       1.437
--- src/buffer.c        2 Sep 2003 09:27:21 -0000
***************
*** 5372,5377 ****
--- 5372,5379 ----
    %c -- print the current column number (this makes editing slower).
          To make the column number update correctly in all cases,
        `column-number-mode' must be non-nil.
+   %i -- print the size of the buffer.
+   %I -- like %i, but use k, M, G, etc., to abbreviate.
    %p -- print percent of buffer above top of window, or Top, Bot or All.
    %P -- print percent of buffer above bottom of window, perhaps plus Top,
          or print Bottom or All.
Index: src/xdisp.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/xdisp.c,v
retrieving revision 1.846
diff -c -r1.846 xdisp.c
*** src/xdisp.c 1 Sep 2003 15:45:57 -0000       1.846
--- src/xdisp.c 2 Sep 2003 09:27:26 -0000
***************
*** 772,777 ****
--- 772,778 ----
  
  static int next_element_from_ellipsis P_ ((struct it *));
  static void pint2str P_ ((char *, int, int));
+ static void pint2hrstr P_ ((char *, int, int));
  static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
                                                        struct text_pos));
  static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
***************
*** 15582,15587 ****
--- 15583,15699 ----
      }
  }
  
+ /* Write a null-terminated, right justified decimal and "human
+    readable" representation of the nonnegative integer D to BUF using
+    a minimal field width WIDTH.        D should be smaller than 999.5e24. */
+ 
+ static const char power_letter[] =
+   {
+     0,         /* not used */
+     'k', /* kilo */
+     'M', /* mega */
+     'G', /* giga */
+     'T', /* tera */
+     'P', /* peta */
+     'E', /* exa */
+     'Z', /* zetta */
+     'Y'        /* yotta */
+   };
+ 
+ static void
+ pint2hrstr (buf, width, d)
+      char *buf;
+      int width;
+      int d;
+ {
+   /* We aim to represent the nonnegative integer D as
+      QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
+   int quotient = d;
+   int remainder = 0;
+   /* -1 means: do not use TENTHS. */
+   int tenths = -1;
+   int exponent = 0;
+ 
+   /* Length of QUOTIENT.TENTHS as a string. */
+   int length;
+ 
+   char * psuffix;
+   char * p;
+ 
+   if (1000 <= quotient)
+     {
+       /* Scale to the appropriate EXPONENT. */
+       do
+       {
+         remainder = quotient % 1000;
+         quotient /= 1000;
+         exponent++;
+       }
+       while (1000 <= quotient);
+ 
+       /* Round to nearest and decide whether to use TENTHS or not. */
+       if (quotient <= 9)
+       {
+         tenths = remainder / 100;
+         if (50 <= remainder % 100)
+           if (tenths < 9)
+             tenths++;
+           else
+             {
+               quotient++;
+               if (quotient == 10)
+                 tenths = -1;
+               else
+                 tenths = 0;
+             }
+       }
+       else
+       if (500 <= remainder)
+         if (quotient < 999)
+           quotient++;
+         else
+           {
+             quotient = 1;
+             exponent++;
+             tenths = 0;
+           }
+     }
+ 
+   /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
+   if (tenths == -1 && quotient <= 99)
+     if (quotient <= 9)
+       length = 1;
+     else
+       length = 2;
+   else
+     length = 3;
+   p = psuffix = buf + max (width, length);
+ 
+   /* Print EXPONENT. */
+   if (exponent)
+     *psuffix++ = power_letter[exponent];
+   *psuffix = '\0';
+ 
+   /* Print TENTHS. */
+   if (tenths >= 0)
+     {
+       *--p = '0' + tenths;
+       *--p = '.';
+     }
+ 
+   /* Print QUOTIENT. */
+   do
+     {
+       int digit = quotient % 10;
+       *--p =  '0' + digit;
+     }
+   while ((quotient /= 10) != 0);
+ 
+   /* Print leading spaces. */
+   while (buf < p)
+     *--p = ' ';
+ }
+ 
  /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
     If EOL_FLAG is 1, set also a mnemonic character for end-of-line
     type of CODING_SYSTEM.  Return updated pointer into BUF.  */
***************
*** 15783,15788 ****
--- 15895,15914 ----
      case 'f':
        obj = b->filename;
        break;
+ 
+     case 'i':
+       {
+       int size = ZV - BEGV;
+       pint2str (decode_mode_spec_buf, field_width, size);
+       return decode_mode_spec_buf;
+       }
+ 
+     case 'I':
+       {
+       int size = ZV - BEGV;
+       pint2hrstr (decode_mode_spec_buf, field_width, size);
+       return decode_mode_spec_buf;
+       }
  
      case 'l':
        {




reply via email to

[Prev in Thread] Current Thread [Next in Thread]