new font-switcher Perl extension
Adam Spiers
rxvt-unicode at adamspiers.org
Sun Jan 25 11:51:02 CET 2009
On Sat, Jan 24, 2009 at 06:55:53PM +0100, Marc Lehmann wrote:
> > $term->{'font-switcher'}{'overlay'} = {
>
> In general, no superfluous ()'s or quotes, like above. It follows that _
> is preferred over -.
>
> As a sidenode, %$term is fully yours - best call it $self, because there
> is also $self->term, which is the terminal object - so there is no need
> for the extra font-switcher indirection.
>
> > unless (@font_list) {
> > timed_popup($term, "font-switcher-list resource was empty", 2);
>
> and spaces should be consistently before the (, not after or around. But
> this case should not have parentheses at all.
>
> > $term->resource('font', $new_font);
>
> And here, =>-quoting would be prefered, as in font => $new_font, but that
> is minor.
>
> Looks good so far - I would be happy to add it to the next urxvt release with
> if these were more consistent!
Latest version attached, which also includes the bold font patch from
Aron Griffis.
-------------- 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: 5x8|6x10|7x13|8x13|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 ($self, $text, $timeout) = @_;
$self->{font_switcher}{overlay} = {
ov => $self->overlay_simple (0, -1, $text),
to => urxvt::timer
->new
->start (urxvt::NOW + $timeout)
->cb (sub {
delete $self->{font_switcher}{overlay};
}),
};
}
sub on_init {
my ($self) = @_;
my $font_list = $self->x_resource ('font-switcher-list');
if ($font_list) {
@font_list = split /\s*\|\s*/, $font_list;
}
}
sub on_user_command {
my ($self, $string) = @_;
unless (@font_list) {
timed_popup ($self, "font-switcher-list resource was empty", 2);
return;
}
my $current_font = $self->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 ($self, "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];
# Thanks to Aron Griffis for this trick for handling bold fonts.
my $bold_font = ($new_font =~ /^xft:/) ? "$new_font:bold" : "";
timed_popup ($self, "$new_index $new_font", 1);
# This has no effect by itself, but is used for the next time we
# lookup. Also seems the Right Thing To Do.
$self->resource ( font => $new_font );
$self->resource (boldFont => $bold_font);
# There's no API for changing font yet...
# http://thread.gmane.org/gmane.comp.terminal-emulators.rxvt-unicode.general/255
# $self->set_fonts($new_font);
# so we do it via an escape sequence:
$self->cmd_parse (sprintf "\33]710;%s\007", $new_font);
$self->cmd_parse (sprintf "\33]711;%s\007", $bold_font);
$self->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 25 Jan 2009 10:49:48 -0000
@@ -372,9 +372,45 @@
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
+
+For xft fonts, the corresponding bold font is automatically set by
+appending C<:bold> to the font. For non-xft fonts, the corresponding
+bold font is set to the same font. Future versions of the extension
+might handle this better.
+
=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