emacs-devel
[Top][All Lists]
Advanced

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

Re: Proposal: new mode-line `%'-construct %o meaning "Degree of travel o


From: Alan Mackenzie
Subject: Re: Proposal: new mode-line `%'-construct %o meaning "Degree of travel of window through buffer". [Patch]
Date: Sat, 20 May 2017 10:34:29 +0000
User-agent: Mutt/1.5.24 (2015-08-30)

On Mon, May 15, 2017 at 20:44:17 +0000, Alan Mackenzie wrote:
> Hello, Emacs.

> I've always been annoyed by the percentage output by the mode-line
> construct "%p" - so much so that I patched my personal copy of
> `decode-mode-spec' in xdisp.c over ten years ago (thanks for the tip
> then, Eli!).

[ .... ]

Here is a patch which implements %o and %q.  Feedback would be welcome,
of course.



diff --git a/doc/lispref/modes.texi b/doc/lispref/modes.texi
index eb72fcfd36..0e476b47a3 100644
--- a/doc/lispref/modes.texi
+++ b/doc/lispref/modes.texi
@@ -1972,6 +1972,14 @@ Mode Line Variables
 line number and the column number.
 @end defvar
 
address@hidden mode-line-percent-position
+This option is used in @code{mode-line-position}.  Its value specifies
+both the buffer percentage to display (one of @code{nil}, @code{"%o"},
address@hidden"%p"}, @code{"%P"} or @code{"%q"}, @pxref{%-Constructs}) and a
+width to space-fill or truncate to.  You are recommended to set this
+option with the @code{customize-variable} facility.
address@hidden defopt
+
 @defvar vc-mode
 The variable @code{vc-mode}, buffer-local in each buffer, records
 whether the buffer's visited file is maintained with version control,
@@ -2147,6 +2155,12 @@ %-Constructs
 @samp{Narrow} when narrowing is in effect; nothing otherwise (see
 @code{narrow-to-region} in @ref{Narrowing}).
 
address@hidden %o
+The degree of @dfn{travel} of the window through (the visible portion
+of) the buffer, i.e. the size of the text above the top of the window
+expressed as a percentage of all the text outside the window, or
address@hidden, @samp{Bottom} or @samp{All}.
+
 @item %p
 The percentage of the buffer text above the @strong{top} of window, or
 @samp{Top}, @samp{Bottom} or @samp{All}.  Note that the default mode
@@ -2158,6 +2172,10 @@ %-Constructs
 the text above the top), plus @samp{Top} if the top of the buffer is
 visible on screen; or @samp{Bottom} or @samp{All}.
 
address@hidden %q
+The percentages of text above both the @strong{top} and the
address@hidden of the window, separated by @samp{-}, or @samp{All}.
+
 @item %s
 The status of the subprocess belonging to the current buffer, obtained with
 @code{process-status}.  @xref{Process Information}.
diff --git a/etc/NEWS b/etc/NEWS
index 9be6ee0f3f..4a8696ace4 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -358,6 +358,16 @@ new mode line construct, '%C', which operates exactly as 
'%c' does
 except that it counts from one.)
 
 +++
+** New mode line constructs '%o' and '%q', and user option
+'mode-line-percent-position'.  '%o' displays the "degree of travel" of
+the window through the buffer.  Unlike the default '%p', this
+percentage approaches 100% as the window approaches the end of the
+buffer.  '%q' displays the percentage offsets of both the start and
+the end of the window, e.g. "5-17%".  The new option
+'mode-line-percent-position' makes it easier to switch between '%p',
+'%P', and these new constructs.
+
++++
 ** Two new user options 'list-matching-lines-jump-to-current-line' and
 'list-matching-lines-current-line-face' to show highlighted the current
 line in *Occur* buffer.
diff --git a/lisp/bindings.el b/lisp/bindings.el
index 85a5408717..1f5c8b3bbf 100644
--- a/lisp/bindings.el
+++ b/lisp/bindings.el
@@ -365,14 +365,32 @@ column-number-indicator-zero-based
   :group 'mode-line
   :version "26.1")
 
+(defcustom mode-line-percent-position '(-3 "%p")
+  "Specification of \"percentage offset\" of window through buffer
+This option specifies both the field width and the type of offset
+displayed in `mode-line-position', a component of the default
+`mode-line-format'."
+  :type `(radio
+          (const :tag "nil:  No offset is displayed" nil)
+          (const :tag "\"%o\": Proportion of \"travel\" of the window through 
the buffer"
+                 (-3 "%o"))
+          (const :tag "\"%p\": Percentage offset of top of window"
+                 (-3 "%p"))
+          (const :tag "\"%P\": Precentage offset of bottom of window"
+                 (-3 "%P"))
+          (const :tag "\"%q\": Offsets of both top and bottom of window"
+                 (6 "%q")))
+  :version "26.1"
+  :group 'mode-line)
+
 (defvar mode-line-position
-  `((-3 ,(propertize
-         "%p"
-         'local-map mode-line-column-line-number-mode-map
-         'mouse-face 'mode-line-highlight
-         ;; XXX needs better description
-         'help-echo "Size indication mode\n\
-mouse-1: Display Line and Column Mode Menu"))
+  `((:propertize
+     mode-line-percent-position
+     'local-map mode-line-column-line-number-mode-map
+     'mouse-face 'mode-line-highlight
+     ;; XXX needs better description
+     'help-echo "Size indication mode\n\
+mouse-1: Display Line and Column Mode Menu")
     (size-indication-mode
      (8 ,(propertize
          " of %I"
@@ -419,6 +437,7 @@ mode-line-position
   "Mode line construct for displaying the position in the buffer.
 Normally displays the buffer percentage and, optionally, the
 buffer size, the line number and the column number.")
+
 (put 'mode-line-position 'risky-local-variable t)
 
 (defvar mode-line-buffer-identification-keymap
diff --git a/src/xdisp.c b/src/xdisp.c
index cdea20993c..27349aeba3 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -23870,6 +23870,27 @@ decode_mode_spec (struct window *w, register int c, 
int field_width,
        return " Narrow";
       break;
 
+      /* Display the "degree of travel" of the window through the buffer.  */
+    case 'o':
+      {
+        ptrdiff_t toppos = marker_position (w->start);
+        ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
+        ptrdiff_t begv = BUF_BEGV (b);
+        ptrdiff_t zv = BUF_ZV (b);
+
+        if (zv <= botpos)
+          return toppos <= begv ? "All" : "Bottom";
+        else if (toppos <= begv)
+          return "Top";
+        else
+          {
+          sprintf (decode_mode_spec_buf, "%2d%%",
+                   percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
+          return decode_mode_spec_buf;
+          }
+      }
+
+      /* Display percentage of buffer above the top of the screen.  */
     case 'p':
       {
        ptrdiff_t pos = marker_position (w->start);
@@ -23907,6 +23928,33 @@ decode_mode_spec (struct window *w, register int c, 
int field_width,
          }
       }
 
+      /* Display percentage offsets of top and bottom of the window,
+         using "All" (but not "Top" or "Bottom") where appropriate.  */
+    case 'q':
+      {
+        ptrdiff_t toppos = marker_position (w->start);
+        ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
+        ptrdiff_t begv = BUF_BEGV (b);
+        ptrdiff_t zv = BUF_ZV (b);
+
+        if ((toppos <= begv) && (zv <= botpos))
+          return "All   ";
+
+        if (toppos <= begv)
+          strcpy (decode_mode_spec_buf, "0-");
+        else
+          sprintf (decode_mode_spec_buf, "%d-",
+                   percent99 (toppos - begv, zv - begv));
+
+        if (zv <= botpos)
+          strcat (decode_mode_spec_buf, "100%");
+        else
+          sprintf (&decode_mode_spec_buf [strlen (decode_mode_spec_buf)],
+                   "%d%%", percent99 (botpos - begv, zv - begv));
+
+        return decode_mode_spec_buf;
+      }
+
     case 's':
       /* status of process */
       obj = Fget_buffer_process (Fcurrent_buffer ());


> -- 
> Alan Mackenzie (Nuremberg, Germany).



reply via email to

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