new font-switcher Perl extension

Adam Spiers rxvt-unicode at adamspiers.org
Wed Jan 21 20:03:08 CET 2009


On Wed, Jan 21, 2009 at 04:27:35PM +0100, Marc Lehmann wrote:
> On Wed, Jan 21, 2009 at 01:42:54PM +0000, Adam Spiers <rxvt-unicode at adamspiers.org> wrote:
> > If you like it feel free to include it in the distribution.
> 
> Unfortunately, your license forbids this.

Doh, that explains the nagging feeling I got when I slapped v3 on there.

> If you would allow redistribution under the same license as urxvt (GPLv2),
> added the relevant documentation to uxvt.pm and could at least use a
> similar coding style as the other code, i would happily include it,
> though!

Other than changing 2-column indents to 3-column, I couldn't see any
major style clashes, so new version and patch against CVS HEAD both
attached ... let me know if I missed anything.

Switching seems to work slightly strangely with some of my own .pcf
fonts - sometimes taking a few seconds to update the terminal to the
new size - but more smoothly with the "standard" .pcf fonts such as
9x15, 10x20 etc., and also nicely with xft fonts, e.g.

   URxvt.font-switcher-list:  \
       xft:DejaVu Sans Mono:pixelsize=6  | \
       xft:DejaVu Sans Mono:pixelsize=7  | \
       xft:DejaVu Sans Mono:pixelsize=8  | \
       xft:DejaVu Sans Mono:pixelsize=9  | \
       xft:DejaVu Sans Mono:pixelsize=10 | \
       xft:DejaVu Sans Mono:pixelsize=11 | \
       xft:DejaVu Sans Mono:pixelsize=12 | \
       xft:DejaVu Sans Mono:pixelsize=13 | \
       xft:DejaVu Sans Mono:pixelsize=14 | \
       xft:DejaVu Sans Mono:pixelsize=15 | \
       xft:DejaVu Sans Mono:pixelsize=16 | \
       xft:DejaVu Sans Mono:pixelsize=17 | \
       xft:DejaVu Sans Mono:pixelsize=18

However in some cases during resize the window occasionally gets
repositioned relative to its top left corner.  Any idea why that
happens?  I'm guessing my window manager (openbox) might be involved
somehow.

Regards,
Adam
-------------- next part --------------
#! perl
#
# font-switcher plugin for rxvt-unicode
# Copyright (C) 2009 Adam Spiers <urxvt-font-switcher at adamspiers.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


# This rxvt-unicode extension allows on-the-fly changing of fonts by
# moving left/right through a chosen list of fonts.  The list is
# delimited by '|' and configurable via X resources, e.g.
#
#   URxvt.font-switcher-list:  Micro|nexus|9x15|10x20
#
# Next/previous keys should also be bound for moving through the list,
# e.g.
#
#   URxvt.keysym.M-C-comma:  perl:font-switcher:previous
#   URxvt.keysym.M-C-period: perl:font-switcher:next

use strict;
use warnings;

my @font_list;

sub timed_popup {
   my ($term, $text, $timeout) = @_;
   $term->{'font-switcher'}{'overlay'} = {
      ov => $term->overlay_simple (0, -1, $text),
      to => urxvt::timer
        ->new
        ->start (urxvt::NOW + $timeout)
        ->cb(sub {
                delete $term->{'font-switcher'}{'overlay'};
             }),
   };
}

sub on_init {
   my ($term) = @_;
   my $font_list = $term->x_resource('font-switcher-list');
   if ($font_list) {
      @font_list = split /\s*\|\s*/, $font_list;
   }
}


sub on_user_command {
   my ($term, $string) = @_;
   unless (@font_list) {
      timed_popup($term, "font-switcher-list resource was empty", 2);
      return;
   }

   my $current_font = $term->resource('font');
   my $current_index;
   for my $i (0 .. $#font_list) {
      if ($current_font eq $font_list[$i]) {
         $current_index = $i;
         last;
      }
   }

   unless ($string =~ /^font-switcher:(previous|next)$/i) {
      timed_popup($term, "keysym command should be 'font-switcher:previous' or 'font-switcher:next'", 2);
      return;
   }
   my $command = $1;
   my $direction = $command eq 'next' ? 1 : -1;

   # This way wraps round
   #   my $new_index = defined $current_index ?
   #     ($current_index + $direction) % @font_list : 0;

   # This way has a hard stop at the start or end of the list.
   my $new_index = defined $current_index ?
     ($current_index + $direction) : 0;
   $new_index = 0 if $new_index < 0;
   $new_index = $#font_list if $new_index > $#font_list;

   my $new_font = $font_list[$new_index];
   timed_popup($term, "$new_index $new_font", 0.5);

   # This has no effect by itself, but is used for the next time we
   # lookup.  Also seems the Right Thing To Do.
   $term->resource('font', $new_font);

   # There's no API for changing font yet...
   # http://thread.gmane.org/gmane.comp.terminal-emulators.rxvt-unicode.general/255
   # $term->set_fonts($new_font);

   # so we do it via an escape sequence:
   $term->cmd_parse(sprintf "\33]50;%s\007", $new_font);

   $term->want_refresh;         # seems to help
}
-------------- next part --------------
Index: src/urxvt.pm
===================================================================
RCS file: /schmorpforge/rxvt-unicode/src/urxvt.pm,v
retrieving revision 1.173
diff -u -r1.173 urxvt.pm
--- src/urxvt.pm	5 Nov 2008 12:45:36 -0000	1.173
+++ src/urxvt.pm	21 Jan 2009 19:02:48 -0000
@@ -372,9 +372,41 @@
 
 I<Note to xrdb users:> xrdb uses the C preprocessor, which might interpret
 the double C</> characters as comment start. Use C<\057\057> instead,
-which works regardless of wether xrdb is used to parse the resource file
+which works regardless of whether xrdb is used to parse the resource file
 or not.
 
+=item font-switcher
+
+Allows on-the-fly changing of fonts by moving left/right through a
+chosen list of fonts.  The list is delimited by C<|> and configurable
+via X resources, e.g.
+
+   URxvt.font-switcher-list:  5x8|6x10|7x13|8x13|9x15|10x20
+
+or
+
+   URxvt.font-switcher-list:               \
+       xft:DejaVu Sans Mono:pixelsize=6  | \
+       xft:DejaVu Sans Mono:pixelsize=7  | \
+       xft:DejaVu Sans Mono:pixelsize=8  | \
+       xft:DejaVu Sans Mono:pixelsize=9  | \
+       xft:DejaVu Sans Mono:pixelsize=10 | \
+       xft:DejaVu Sans Mono:pixelsize=11 | \
+       xft:DejaVu Sans Mono:pixelsize=12 | \
+       xft:DejaVu Sans Mono:pixelsize=13 | \
+       xft:DejaVu Sans Mono:pixelsize=14 | \
+       xft:DejaVu Sans Mono:pixelsize=15 | \
+       xft:DejaVu Sans Mono:pixelsize=16 | \
+       xft:DejaVu Sans Mono:pixelsize=17 | \
+       xft:DejaVu Sans Mono:pixelsize=18
+
+
+Next/previous keys should also be bound for moving through the list,
+e.g.
+
+   URxvt.keysym.M-C-comma:  perl:font-switcher:previous
+   URxvt.keysym.M-C-period: perl:font-switcher:next
+
 =item example-refresh-hooks
 
 Displays a very simple digital clock in the upper right corner of the


More information about the rxvt-unicode mailing list