]> jfr.im git - irssi-scripts.git/blob - title.pl
no autorun smshilight
[irssi-scripts.git] / title.pl
1 use strict;
2 use vars qw($VERSION %IRSSI);
3 use Irssi 20020120.0250 ();
4 $VERSION = "3.2b";
5 %IRSSI = (
6 authors => 'Timo Sirainen, David Leadbeater',
7 contact => 'tss@iki.fi, dgl@dgl.cx',
8 name => 'title',
9 description => 'Display configurable title as XTerm title',
10 license => 'GNU GPL',
11 url => 'http://irssi.dgl.cx/',
12 );
13
14 # Settings:
15 # title_string: The string used in the title, see below for explaination
16 # title_topic_length: The length to truncate the topic to (some terminals have
17 # problems with long titles).
18 # title_screen_window: (EXPERIMENTAL), sets the screen window title rather than
19 # the Xterm title.
20
21 # The $vars are normal Irssi vars (docs/special_vars.txt).
22 # $.var does some magic, adds a space at the begining and brackets around
23 # the item but only if it produces output.
24
25 # Here is some examples:
26 # The default:
27 # /set title_string Irssi: [$N@$tag]$.C$.winname$.act
28 # Quite nice with lots of info:
29 # /set title_string $cumode$winname$C$.M$.act$.topic
30 # Nickname with usermode
31 # /set title_string $N(+$usermode)
32
33 # To use this with screen you need some lines in your ~/.screenrc
34 # termcap xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
35 # terminfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
36 # This probably only works if you have $TERM set to xterm.
37
38 my %act;
39 use IO::Handle;
40
41 sub xterm_topic {
42 my($text) = @_;
43
44 STDERR->autoflush(1);
45 if(Irssi::settings_get_bool('title_screen_window')) {
46 print STDERR "\033k$text\033\\";
47 }else{
48 print STDERR "\033]0;$text\007";
49 }
50 }
51
52 sub refresh_topic {
53 my $title = Irssi::settings_get_str('title_string');
54 $title =~ s/\$([A-Za-z.,;:]+)/special_var($1)/eg;
55 xterm_topic($title);
56 }
57
58 sub special_var {
59 my($str) = @_;
60
61 my($begin,$end);
62 if($str =~ s/^\.//) {
63 $begin = ' [';
64 $end = ']';
65 }else{
66 $begin = $end = '';
67 }
68
69 my $result;
70 if($str eq 'topic') {
71 $result = topic_str();
72 }elsif($str eq 'act') {
73 $result = act_str();
74 }else{
75 my $item = ref Irssi::active_win() ? Irssi::active_win()->{active} : '';
76 $item = Irssi::active_server() unless ref $item;
77 return '' unless ref $item;
78
79 $result = $item->parse_special('$' . $str);
80 }
81
82 $begin = '(+', $end = ')' if $str eq 'M' && $begin;
83
84 return $result ? $begin . $result . $end : '';
85 }
86
87 sub topic_str {
88 my $server = Irssi::active_server();
89 my $item = ref Irssi::active_win() ? Irssi::active_win()->{active} : '';
90
91 if(ref $server && ref $item && $item->{type} eq 'CHANNEL') {
92 my $topic = $item->{topic};
93 # Remove colour and bold from topic...
94 $topic =~ s/\003(\d{1,2})(\,(\d{1,2})|)//g;
95 $topic =~ s/[\x00-\x1f]//g;
96 $topic = substr($topic, 0,Irssi::settings_get_int('title_topic_length'));
97 return $topic if length $topic;
98 }
99 return '';
100 }
101
102 sub act_str {
103 my @acts;
104 for my $winref(keys %act) {
105 # handle windows with items and not gracefully
106 my $window = Irssi::window_find_refnum($winref);
107 if(defined($window)) {
108 for my $win ($window->items or $window) {
109 if($win->{data_level} >= 3 || $win->{data_lev} >= 3) {
110 push(@acts,$win->{server}->{tag} . '/' . $win->{name});
111 } else {
112 delete($act{$winref});
113 }
114 }
115 } else {
116 delete($act{$winref});
117 }
118 }
119 return join(', ',@acts);
120 }
121
122 sub topic_changed {
123 my($chan) = @_;
124 return unless ref Irssi::active_win() &&
125 Irssi::active_win()->{active}->{type} eq 'CHANNEL';
126 return unless lc $chan->{name} eq lc Irssi::active_win()->{active}->{name};
127
128 refresh_topic();
129 }
130
131 sub hilight_win {
132 my($win) = @_;
133 return unless ref $win && $win->{data_level} >= 3;
134 $act{$win->{refnum}}++;
135
136 refresh_topic();
137 }
138
139 Irssi::signal_add_last('window changed', 'refresh_topic');
140 Irssi::signal_add_last('window item changed', 'refresh_topic');
141 Irssi::signal_add_last('window server changed', 'refresh_topic');
142 Irssi::signal_add_last('server nick changed', 'refresh_topic');
143 Irssi::signal_add_last('channel topic changed', 'topic_changed');
144 Irssi::signal_add_last('window hilight', 'hilight_win');
145 Irssi::signal_add_last('setup changed', 'refresh_topic');
146
147 Irssi::settings_add_str('misc', 'title_string', 'Irssi: [$N@$tag]$.C$.winname$.act');
148 Irssi::settings_add_int('misc', 'title_topic_length', 250);
149 Irssi::settings_add_bool('misc', 'title_screen_window', 0);
150