all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nick Helm <nick@tenpoint.co.nz>
To: Emacs developers <emacs-devel@gnu.org>
Subject: mode-line size and position indicator
Date: Wed, 23 Aug 2017 15:20:27 +1200	[thread overview]
Message-ID: <m2bmn7ezr8.fsf@tenpoint.co.nz> (raw)

[-- Attachment #1: Type: text/plain, Size: 942 bytes --]

Hi all,

I've been trying to improve my C skills lately, so I thought I'd take a
crack at adding a small feature to xdisp.c.

I've written a simple mode-line indicator to visually show the relative
size and position of the window view on the current buffer. It works by
adding a new %-spec construct to decode_mode_spec.

The result is akin to a vertical scroll-bar, but it appears as a
horizontal mark on the mode-line. It may be useful to terminal users or
those who prefer to run with traditional scroll-bars turned off. I've
attached a screen-grab of what it looks like on my system.

The indicator is activated by adding %N~ to mode-line-format, where N is
a positive integer that sets the desired width of the indicator in char.

I'm under no illusions that this should be included in Emacs, and
admittedly, it is a very simple feature, but I'd appreciate any
comments or suggestions to improve the code. Patch attached.

Thanks,
Nick


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: xdisp.c.patch --]
[-- Type: text/x-patch, Size: 1584 bytes --]

--- a/src/xdisp.c	2016-10-07 15:47:28.000000000 +1300
+++ b/src/xdisp.c	2016-10-07 15:54:31.000000000 +1300
@@ -23810,6 +23813,45 @@ Add %-spec for a mode-line size and position indicator.
 	  }
       }
 
+      /* Indicate the relative size and position of the current window. */ 
+    case '~':
+      {
+	ptrdiff_t toppos = marker_position (w->start);
+	ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
+	ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b) + 1;
+	ptrdiff_t open;
+	ptrdiff_t close;
+	int i;
+	char *p = decode_mode_spec_buf;
+
+	/* It makes little sense to pad with spaces, so use WIDTH to
+	   enable the user to specify the size of the indicator. */
+
+	if (width < 3) width = 3; /* Set a minimum indicator size. */
+	width -= 2; /* Reserve space for open and close marks. */
+
+	if (total >= 1000000)
+	  /* Do it differently for a large value, to avoid overflow. */
+	  {
+	    open = ((toppos - BUF_BEGV (b)) + (total / width) - 1) / (total / width);
+	    close = ((botpos - BUF_BEGV (b) + 1) + (total / width) - 1) / (total / width);
+	  }
+	else
+	  {
+	    open = ((toppos - BUF_BEGV (b)) * width + total - 1) / total;
+	    close = ((botpos - BUF_BEGV (b) + 1) * width + total - 1) / total;
+	  }
+
+	/* Build the indicator string. */
+	for (i = 0; i < open; i++) *p++ = '-';
+	*p++ = '+';
+	for (i = 0; i < (close - open); i++) *p++ = '~';
+	*p++ = '+';
+	for (i = 0; i < (width - close); i++) *p++ = '-';
+	*p = 0;
+	return decode_mode_spec_buf;
+      }
+
     case 's':
       /* status of process */
       obj = Fget_buffer_process (Fcurrent_buffer ());

[-- Attachment #3: indicator.png --]
[-- Type: image/png, Size: 23067 bytes --]

             reply	other threads:[~2017-08-23  3:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-23  3:20 Nick Helm [this message]
2017-08-24 16:55 ` mode-line size and position indicator Eli Zaretskii
2017-08-24 19:26   ` John Yates
2017-08-25  2:47     ` Richard Stallman
2017-08-25  9:57   ` Nick Helm
2017-08-25 12:36     ` Eli Zaretskii
2017-08-26  7:50       ` Nick Helm
  -- strict thread matches above, loose matches on Subject: below --
2017-08-24 21:02 mark M
2017-08-25  6:27 ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2bmn7ezr8.fsf@tenpoint.co.nz \
    --to=nick@tenpoint.co.nz \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.