├── README.md └── resize-font /README.md: -------------------------------------------------------------------------------- 1 | ## Installation 2 | 3 | 1. Copy `resize-font` to where URxvt looks for Perl extensions, e.g: 4 | `$HOME/.urxvt/ext/`. For the exact path, see the urxvt(1) manual page on 5 | perl-lib. 6 | 7 | 2. In your `~/.Xresources` file, add `resize-font` to the 8 | `urxvt.perl-ext-common` setting so URxvt loads the extension, e.g: 9 | 10 | urxvt.perl-ext-common: default,tabbed,matcher,resize-font,-tabbed 11 | 12 | 13 | ## Configuration 14 | 15 | All configuration of `resize-font` is done in `~/.Xresources`. 16 | 17 | _Note that this extension requires you to set your font size 18 | through the `urxvt.font` property._ 19 | 20 | ### Fonts 21 | 22 | Default font sizes can be specified in pixels, using the `pixelsize` attribute, 23 | e.g.: 24 | 25 | urxvt.font: xft:Inconsolata:pixelsize=12 26 | 27 | Or they can be given in points, using the `size` attribute, e.g.: 28 | 29 | urxvt.font: xft:Inconsolata:size=12 30 | 31 | Fixed fonts are also supported, for example: 32 | 33 | urxvt.font: 7x14 34 | 35 | And, finally, XLFD/X logical font description is supported as well, e.g.: 36 | 37 | urxvt.font: -*-inconsolata-medium-*-normal-*-14-*-*-*-*-*-iso8859-* 38 | 39 | ### Keybindings 40 | 41 | The default keybindings look like this: 42 | 43 | URxvt.keysym.C-minus: resize-font:smaller 44 | URxvt.keysym.C-plus: resize-font:bigger 45 | URxvt.keysym.C-equal: resize-font:reset 46 | URxvt.keysym.C-question: resize-font:show 47 | 48 | Keybindings can be modified using the above syntax. For more information on how 49 | to specify keys, see the description of the `keysym` resource in the urxvt(1) 50 | manual page. 51 | 52 | ### Resize interval 53 | 54 | You can also configure the number of steps to take when changing the font size: 55 | 56 | URxvt.resize-font.step: 2 57 | 58 | And even fractions like 0.2 are supported. 59 | -------------------------------------------------------------------------------- /resize-font: -------------------------------------------------------------------------------- 1 | # vim:ft=perl:fenc=utf-8:tw=80 2 | # Copyright (c) 2009-, Simon Lundström 3 | # Copyright (c) 2014 Maarten de Vries 4 | # 5 | # Permission to use, copy, modify, and/or distribute this software for any 6 | # purpose with or without fee is hereby granted, provided that the above 7 | # copyright notice and this permission notice appear in all copies. 8 | # 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | 17 | 18 | my @fonts = ( 19 | {'name' => 'font', 'code' => 710}, 20 | {'name' => 'boldFont', 'code' => 711}, 21 | {'name' => 'italicFont', 'code' => 712}, 22 | {'name' => 'boldItalicFont', 'code' => 713}, 23 | ); 24 | 25 | my @fixed = qw(4x6 5x7 5x8 6x9 6x10 6x12 6x13 7x13 7x14 8x13 8x16 9x15 9x18 26 | 10x20 12x24); 27 | my $step; 28 | 29 | sub on_start { 30 | my ($self) = @_; 31 | 32 | foreach (@fonts) { 33 | $_->{'default'} = $self->resource($_->{'name'}); 34 | } 35 | 36 | $step = $self->x_resource("%.step") || 2; 37 | } 38 | 39 | sub on_init { 40 | my ($self) = @_; 41 | my $commands = { 42 | "smaller" => "C-minus", 43 | "bigger" => "C-plus", 44 | "reset" => "C-equal", 45 | "show" => "C-question", 46 | }; 47 | bind_hotkeys($self, $commands); 48 | 49 | () 50 | } 51 | 52 | sub bind_hotkeys { 53 | my ($self, $commands) = @_; 54 | 55 | for (keys %$commands) { 56 | my $hotkey = $$commands{$_}; 57 | my $hotkey_bound = $self->{'term'}->x_resource("keysym.$hotkey"); 58 | if (!defined($hotkey_bound) ) { 59 | # Support old-style key bindings 60 | if ($self->x_resource("%.$_")) { 61 | $hotkey = $self->x_resource("%.$_"); 62 | } 63 | 64 | # FIXME If we're bound to a keysym, don't bind the default. 65 | $self->bind_action($hotkey, "%:$_") or 66 | warn "unable to register '$hotkey' as hotkey for $_"; 67 | } 68 | else { 69 | if ($hotkey_bound !~ /^resize-font:/) { 70 | warn "Hotkey $$commands{$_} already bound to $hotkey_bound, not ". 71 | "binding to resize-font:$_ by default."; 72 | } 73 | } 74 | } 75 | } 76 | 77 | sub on_action { 78 | my ($self, $string) = @_; 79 | 80 | if ($string eq "bigger") { 81 | foreach (@fonts) { 82 | next if not defined($_->{'default'}); 83 | update_font_size($self, $_, +$step); 84 | } 85 | } 86 | elsif ($string eq "smaller") { 87 | foreach (@fonts) { 88 | next if not defined($_->{'default'}); 89 | update_font_size($self, $_, -$step); 90 | } 91 | } 92 | elsif ($string eq "reset") { 93 | foreach (@fonts) { 94 | next if not defined($_->{'default'}); 95 | set_font($self, $_, $_->{'default'}); 96 | } 97 | } 98 | elsif ($string eq "show") { 99 | 100 | my $term = $self->{'term'}; 101 | $term->{'resize-font'}{'overlay'} = { 102 | ov => $term->overlay_simple(0, -1, format_font_info($self)), 103 | to => urxvt::timer 104 | ->new 105 | ->start(urxvt::NOW + 1) 106 | ->cb(sub { 107 | delete $term->{'resize-font'}{'overlay'}; 108 | }), 109 | }; 110 | } 111 | 112 | () 113 | } 114 | 115 | sub get_font { 116 | my ($self, $name) = @_; 117 | return $self->resource($name); 118 | } 119 | 120 | sub set_font { 121 | my ($self, $font, $new) = @_; 122 | $self->cmd_parse(sprintf("\33]%d;%s\007", $font->{'code'}, $new)); 123 | } 124 | 125 | sub round { 126 | return sprintf("%.0f", @_); 127 | } 128 | 129 | sub atleast { 130 | my ($min, $val) = @_; 131 | if (0 < abs $val && abs $val < $min){ 132 | return $val / abs($val) 133 | } 134 | return $val; 135 | } 136 | 137 | sub update_font_size { 138 | my ($self, $font, $delta) = @_; 139 | my $regex = qr"(?<=size=)(\d+(?:\.\d+)?)"; 140 | my $current = get_font($self, $font->{'name'}); 141 | 142 | my ($index) = grep { $fixed[$_] eq $current } 0..$#fixed; 143 | if ($index or $index eq 0) { 144 | my $inc = $delta / abs($delta); 145 | $index += $inc; 146 | if ($index < 0) { $index = 0; } 147 | if ($index > $#fixed) { $index = $#fixed; } 148 | $current = $fixed[$index]; 149 | } 150 | elsif ($current =~ /^-/) { 151 | my @font = split(/-/, $current); 152 | # https://en.wikipedia.org/wiki/X_logical_font_description 153 | #Pixel size 154 | if ($font[7] gt 0) { 155 | $delta = atleast(1, $delta); 156 | my $newsize = round($font[7]+$delta); 157 | $font[7] = $newsize if ($newsize > 0) 158 | } 159 | #Point size 160 | elsif ($font[8] gt 0) { 161 | $delta = atleast(1, $delta*10); 162 | my $newsize = round($font[8]+$delta); 163 | $font[8] = $newsize if ($newsize > 0) 164 | } 165 | $current = join('-', @font); 166 | } 167 | else { 168 | my $newsize = $1+$delta if ($current =~ /$regex/); 169 | $current =~ s/$regex/$newsize/ge if ($newsize > 0); 170 | } 171 | set_font($self, $font, $current); 172 | } 173 | 174 | sub format_font_info { 175 | my ($self) = @_; 176 | 177 | my $width = 0; 178 | foreach (@fonts) { 179 | my $length = length($_->{'name'}); 180 | $width = $length > $width ? $length : $width; 181 | } 182 | ++$width; 183 | 184 | my $info = ''; 185 | foreach (@fonts) { 186 | $info .= sprintf("%-${width}s %s\n", $_->{'name'} . ':', 187 | get_font($self, $_->{'name'})); 188 | } 189 | 190 | return $info; 191 | } 192 | --------------------------------------------------------------------------------