addons/slrn-macros/slrn-terminal-title-macro

view terminal-title.sl @ 1:b5ca94cc74ae

Change license to GPL-2.0+
author Guido Berhoerster <guido+slrn@berhoerster.name>
date Sat Jul 25 17:12:50 2015 +0200 (2015-07-25)
parents 1964c513723a
children
line source
1 % terminal-title.sl - set the terminal title
2 %
3 % Copyright (C) 2009 Guido Berhoerster <guido+slrn@berhoerster.name>
4 %
5 % This program is free software; you can redistribute it and/or
6 % modify it under the terms of the GNU General Public License
7 % as published by the Free Software Foundation; either version 2
8 % of the License, or (at your option) any later version.
9 %
10 % This program is distributed in the hope that it will be useful,
11 % but WITHOUT ANY WARRANTY; without even the implied warranty of
12 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 % GNU General Public License for more details.
14 %
15 % You should have received a copy of the GNU General Public License
16 % along with this program; if not, write to the Free Software
17 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 %open_log_file(make_home_filename("slrn-debug.log"));
20 %_traceback = 1;
22 implements("TerminalTitle");
24 static variable group_substitutions = Assoc_Type[Ref_Type];
25 group_substitutions["s"] = &server_name;
26 group_substitutions["v"] = &_slrn_version_string;
28 static variable article_substitutions = Assoc_Type[Ref_Type];
29 article_substitutions["s"] = &server_name;
30 article_substitutions["v"] = &_slrn_version_string;
31 article_substitutions["n"] = &current_newsgroup;
33 static variable config = struct
34 {
35 article_title_format = "slrn %v -- Server: %s Group: %n",
36 group_title_format = "slrn %v -- Server: %s"
37 };
39 define format_str(str, substitutions)
40 {
41 variable match_pos;
42 variable match_len;
43 variable format;
44 variable temp;
45 variable formatted_str = "";
47 while (string_match(str, "^\(%-?[0-9]*\(["R +
48 strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1) ||
49 string_match(str, "\([^%]%-?[0-9]*\(["R +
50 strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1)) {
51 (match_pos, match_len) = string_match_nth(1);
52 temp = substr(str, 1, match_pos);
53 (temp, ) = strreplace(temp, "%%", "%", strlen(temp));
54 format = substr(str, match_pos + 1, match_len - 1) + "s";
55 (match_pos, match_len) = string_match_nth(2);
56 formatted_str += temp + sprintf(format,
57 @substitutions[substr(str, match_pos + 1, match_len)]);
58 str = substr(str, match_pos + 1 + match_len, -1);
59 }
60 (str, ) = strreplace(str, "%%", "%", strlen(str));
61 formatted_str += str;
63 return formatted_str;
64 }
66 define set_terminal_title(title)
67 {
68 variable term = getenv("TERM");
70 if (string_match(term, "screen", 1)) {
71 tt_send("\033_" + title + "\033\\");
72 } else if (string_match(term, "xterm", 1)) {
73 tt_send("\033]0;" + title + "\007");
74 }
75 return;
76 }
78 define set_group_title()
79 {
80 set_terminal_title(format_str(config.group_title_format,
81 group_substitutions));
82 }
84 define set_article_title()
85 {
86 set_terminal_title(format_str(config.article_title_format,
87 article_substitutions));
88 }
90 () = register_hook("article_mode_hook", "TerminalTitle->set_article_title");
91 () = register_hook("group_mode_hook", "TerminalTitle->set_group_title");