summaryrefslogtreecommitdiff
path: root/urxvt/.urxvt/ext/resize
blob: f3b34fbcc88114e26bfda4476cc7cfde4dadb85d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# vim:ft=perl:fenc=utf-8
# Copyright (c) 2009-, Simon Lundström <simmel@soy.se>
# Copyright (c) 2014 Maarten de Vries <maarten@de-vri.es>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# Usage:
# Set your font in ~/.Xresources:
# urxvt.font: xft:Inconsolata:pixelsize=12
# to set it with pixels or
# urxvt.font: xft:Inconsolata:size=12
# to set it with points.

# And re-bind some keymappings (if you want, below are the defaults):
# URxvt.resize-font.smaller: C-minus
# URxvt.resize-font.bigger: C-plus
# URxvt.resize-font.reset: C-equal
# URxvt.resize-font.show: C-question
#

my @fonts = (
  {'name' => 'font',           'code' => 710},
  {'name' => 'boldFont',       'code' => 711},
  {'name' => 'italicFont',     'code' => 712},
  {'name' => 'boldItalicFont', 'code' => 713},
);

my @fixed = qw(4x6 5x7 5x8 6x9 6x10 6x12 6x13 7x13 7x14 8x13 8x16 9x15 9x18 10x20 12x24);

sub on_start {
  my ($self) = @_;

  foreach (@fonts) {
    $_->{'default'} = $self->resource($_->{'name'});
  }
}

sub on_init {
   my ($self) = @_;
   my $commands = {
     "smaller" => "C-minus",
     "bigger"  => "C-plus",
     "reset"   => "C-equal",
     "show"    => "C-question",
   };
   bind_hotkeys($self, $commands);

   ()
}

sub bind_hotkeys {
  my ($self, $commands) = @_;
  for (keys %$commands) {
    my $hotkey = $self->x_resource("resize-font.$_") || $$commands{$_};

    $self->parse_keysym($hotkey, "perl:resize-font:$_") or
    warn "unable to register '$hotkey' as hotkey for $_\n";
  }
}

sub on_user_command {
  my ($self, $string) = @_;
  my $regex = qr"(?!pixelsize=)(\d+)";

  if ($string =~ /bigger$/) {
    foreach (@fonts) {
      next if not defined($_->{'default'});
      update_font_size($self, $_, +2);
    }
  }
  elsif ($string =~ /smaller$/) {
    foreach (@fonts) {
      next if not defined($_->{'default'});
      update_font_size($self, $_, -2);
    }
  }
  elsif ($string =~ /reset$/) {
    foreach (@fonts) {
      next if not defined($_->{'default'});
      set_font($self, $_, $_->{'default'});
    }
  }
  elsif ($string =~ /show$/) {

    my $term = $self->{'term'};
    $term->{'resize-font'}{'overlay'} = {
      ov => $term->overlay_simple(0, -1, format_font_info($self)),
      to => urxvt::timer
      ->new
      ->start(urxvt::NOW + 1)
      ->cb(sub {
        delete $term->{'resize-font'}{'overlay'};
      }),
    };
  }

  ()
}

sub get_font {
  my ($self, $name) = @_;
  return $self->resource($name);
}

sub set_font {
  my ($self, $font, $new) = @_;
  $self->cmd_parse(sprintf("\33]%d;%s\007", $font->{'code'}, $new));
}

sub update_font_size {
  my ($self, $font, $delta) = @_;
  my $regex = qr"(?<=size=)(\d+)";
  my $current = get_font($self, $font->{'name'});

  my ($index) = grep { $fixed[$_] eq $current } 0..$#fixed;
  if ($index or $index eq 0) {
    my $inc = $delta / abs($delta);
    $index += $inc;
    if ($index < 0) { $index = 0; }
    if ($index > $#fixed) { $index = $#fixed; }
    $current = $fixed[$index];
  }
  else {
    $current =~ s/$regex/$1+$delta/e;
  }
  set_font($self, $font, $current);
}

sub format_font_info {
  my ($self) = @_;

  my $width = 0;
  foreach (@fonts) {
    my $length = length($_->{'name'});
    $width = $length > $width ? $length : $width;
  }
  ++$width;

  my $info = '';
  foreach (@fonts) {
    $info .= sprintf("%-${width}s %s\n", $_->{'name'} . ':', get_font($self, $_->{'name'}));
  }

  return $info;
}