[
  {
    "path": "README.md",
    "content": "## Installation\n\n1. Copy `resize-font` to where URxvt looks for Perl extensions, e.g:\n   `$HOME/.urxvt/ext/`. For the exact path, see the urxvt(1) manual page on\n   perl-lib.\n\n2. In your `~/.Xresources` file, add `resize-font` to the\n   `urxvt.perl-ext-common` setting so URxvt loads the extension, e.g:\n\n        urxvt.perl-ext-common: default,tabbed,matcher,resize-font,-tabbed\n\n\n## Configuration\n\nAll configuration of `resize-font` is done in `~/.Xresources`.\n\n_Note that this extension requires you to set your font size\nthrough the `urxvt.font` property._\n\n### Fonts\n\nDefault font sizes can be specified in pixels, using the `pixelsize` attribute,\ne.g.:\n\n    urxvt.font: xft:Inconsolata:pixelsize=12\n\nOr they can be given in points, using the `size` attribute, e.g.: \n\n    urxvt.font: xft:Inconsolata:size=12\n\nFixed fonts are also supported, for example:\n\n    urxvt.font: 7x14\n\nAnd, finally, XLFD/X logical font description is supported as well, e.g.:\n\n    urxvt.font: -*-inconsolata-medium-*-normal-*-14-*-*-*-*-*-iso8859-*\n\n### Keybindings\n\nThe default keybindings look like this:\n\n    URxvt.keysym.C-minus:     resize-font:smaller\n    URxvt.keysym.C-plus:      resize-font:bigger\n    URxvt.keysym.C-equal:     resize-font:reset\n    URxvt.keysym.C-question:  resize-font:show\n\nKeybindings can be modified using the above syntax. For more information on how\nto specify keys, see the description of the `keysym` resource in the urxvt(1)\nmanual page.\n\n### Resize interval\n\nYou can also configure the number of steps to take when changing the font size:\n\n    URxvt.resize-font.step: 2\n\nAnd even fractions like 0.2 are supported.\n"
  },
  {
    "path": "resize-font",
    "content": "# vim:ft=perl:fenc=utf-8:tw=80\n# Copyright (c) 2009-, Simon Lundström <simmel@soy.se>\n# Copyright (c) 2014 Maarten de Vries <maarten@de-vri.es>\n#\n# Permission to use, copy, modify, and/or distribute this software for any\n# purpose with or without fee is hereby granted, provided that the above\n# copyright notice and this permission notice appear in all copies.\n#\n# THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n\n\nmy @fonts = (\n  {'name' => 'font',           'code' => 710},\n  {'name' => 'boldFont',       'code' => 711},\n  {'name' => 'italicFont',     'code' => 712},\n  {'name' => 'boldItalicFont', 'code' => 713},\n);\n\nmy @fixed = qw(4x6 5x7 5x8 6x9 6x10 6x12 6x13 7x13 7x14 8x13 8x16 9x15 9x18\n               10x20 12x24);\nmy $step;\n\nsub on_start {\n  my ($self) = @_;\n\n  foreach (@fonts) {\n    $_->{'default'} = $self->resource($_->{'name'});\n  }\n\n  $step = $self->x_resource(\"%.step\") || 2;\n}\n\nsub on_init {\n   my ($self) = @_;\n   my $commands = {\n     \"smaller\" => \"C-minus\",\n     \"bigger\"  => \"C-plus\",\n     \"reset\"   => \"C-equal\",\n     \"show\"    => \"C-question\",\n   };\n   bind_hotkeys($self, $commands);\n\n   ()\n}\n\nsub bind_hotkeys {\n  my ($self, $commands) = @_;\n\n  for (keys %$commands) {\n    my $hotkey = $$commands{$_};\n    my $hotkey_bound = $self->{'term'}->x_resource(\"keysym.$hotkey\");\n    if (!defined($hotkey_bound) ) {\n      # Support old-style key bindings\n      if ($self->x_resource(\"%.$_\")) {\n        $hotkey = $self->x_resource(\"%.$_\");\n      }\n\n      # FIXME If we're bound to a keysym, don't bind the default.\n      $self->bind_action($hotkey, \"%:$_\") or\n      warn \"unable to register '$hotkey' as hotkey for $_\";\n    }\n    else {\n      if ($hotkey_bound !~ /^resize-font:/) {\n        warn \"Hotkey $$commands{$_} already bound to $hotkey_bound, not \".\n             \"binding to resize-font:$_ by default.\";\n      }\n    }\n  }\n}\n\nsub on_action {\n  my ($self, $string) = @_;\n\n  if ($string eq \"bigger\") {\n    foreach (@fonts) {\n      next if not defined($_->{'default'});\n      update_font_size($self, $_, +$step);\n    }\n  }\n  elsif ($string eq \"smaller\") {\n    foreach (@fonts) {\n      next if not defined($_->{'default'});\n      update_font_size($self, $_, -$step);\n    }\n  }\n  elsif ($string eq \"reset\") {\n    foreach (@fonts) {\n      next if not defined($_->{'default'});\n      set_font($self, $_, $_->{'default'});\n    }\n  }\n  elsif ($string eq \"show\") {\n\n    my $term = $self->{'term'};\n    $term->{'resize-font'}{'overlay'} = {\n      ov => $term->overlay_simple(0, -1, format_font_info($self)),\n      to => urxvt::timer\n      ->new\n      ->start(urxvt::NOW + 1)\n      ->cb(sub {\n        delete $term->{'resize-font'}{'overlay'};\n      }),\n    };\n  }\n\n  ()\n}\n\nsub get_font {\n  my ($self, $name) = @_;\n  return $self->resource($name);\n}\n\nsub set_font {\n  my ($self, $font, $new) = @_;\n  $self->cmd_parse(sprintf(\"\\33]%d;%s\\007\", $font->{'code'}, $new));\n}\n\nsub round {\n  return sprintf(\"%.0f\", @_);\n}\n\nsub atleast {\n  my ($min, $val) = @_;\n  if (0 < abs $val && abs $val < $min){\n     return $val / abs($val)\n  }\n  return $val;\n}\n\nsub update_font_size {\n  my ($self, $font, $delta) = @_;\n  my $regex = qr\"(?<=size=)(\\d+(?:\\.\\d+)?)\";\n  my $current = get_font($self, $font->{'name'});\n\n  my ($index) = grep { $fixed[$_] eq $current } 0..$#fixed;\n  if ($index or $index eq 0) {\n    my $inc = $delta / abs($delta);\n    $index += $inc;\n    if ($index < 0) { $index = 0; }\n    if ($index > $#fixed) { $index = $#fixed; }\n    $current = $fixed[$index];\n  }\n  elsif ($current =~ /^-/) {\n    my @font = split(/-/, $current);\n    # https://en.wikipedia.org/wiki/X_logical_font_description\n    #Pixel size\n    if ($font[7] gt 0) {\n      $delta = atleast(1, $delta);\n      my $newsize = round($font[7]+$delta);\n      $font[7] = $newsize if ($newsize > 0)\n    }\n    #Point size\n    elsif ($font[8] gt 0) {\n      $delta = atleast(1, $delta*10);\n      my $newsize = round($font[8]+$delta);\n      $font[8] = $newsize if ($newsize > 0)\n    }\n    $current = join('-', @font);\n  }\n  else {\n    my $newsize = $1+$delta if ($current =~ /$regex/);\n    $current =~ s/$regex/$newsize/ge if ($newsize > 0);\n  }\n  set_font($self, $font, $current);\n}\n\nsub format_font_info {\n  my ($self) = @_;\n\n  my $width = 0;\n  foreach (@fonts) {\n    my $length = length($_->{'name'});\n    $width = $length > $width ? $length : $width;\n  }\n  ++$width;\n\n  my $info = '';\n  foreach (@fonts) {\n    $info .= sprintf(\"%-${width}s %s\\n\", $_->{'name'} . ':',\n             get_font($self, $_->{'name'}));\n  }\n\n  return $info;\n}\n"
  }
]