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

view terminal-title.sl @ 0:1964c513723a

Initial revision
author Guido Berhoerster <guido+slrn@berhoerster.name>
date Sat Mar 14 11:44:11 2015 +0100 (2015-03-14)
parents
children b5ca94cc74ae
line source
1 % terminal-title.sl - set the terminal title
2 %
3 % Copyright (C) 2009 Guido Berhoerster <guido+slrn@berhoerster.name>
4 %
5 % Permission is hereby granted, free of charge, to any person obtaining
6 % a copy of this software and associated documentation files (the
7 % "Software"), to deal in the Software without restriction, including
8 % without limitation the rights to use, copy, modify, merge, publish,
9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 %
13 % The above copyright notice and this permission notice shall be included
14 % in all copies or substantial portions of the Software.
15 %
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 %open_log_file(make_home_filename("slrn-debug.log"));
25 %_traceback = 1;
27 implements("TerminalTitle");
29 static variable group_substitutions = Assoc_Type[Ref_Type];
30 group_substitutions["s"] = &server_name;
31 group_substitutions["v"] = &_slrn_version_string;
33 static variable article_substitutions = Assoc_Type[Ref_Type];
34 article_substitutions["s"] = &server_name;
35 article_substitutions["v"] = &_slrn_version_string;
36 article_substitutions["n"] = &current_newsgroup;
38 static variable config = struct
39 {
40 article_title_format = "slrn %v -- Server: %s Group: %n",
41 group_title_format = "slrn %v -- Server: %s"
42 };
44 define format_str(str, substitutions)
45 {
46 variable match_pos;
47 variable match_len;
48 variable format;
49 variable temp;
50 variable formatted_str = "";
52 while (string_match(str, "^\(%-?[0-9]*\(["R +
53 strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1) ||
54 string_match(str, "\([^%]%-?[0-9]*\(["R +
55 strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1)) {
56 (match_pos, match_len) = string_match_nth(1);
57 temp = substr(str, 1, match_pos);
58 (temp, ) = strreplace(temp, "%%", "%", strlen(temp));
59 format = substr(str, match_pos + 1, match_len - 1) + "s";
60 (match_pos, match_len) = string_match_nth(2);
61 formatted_str += temp + sprintf(format,
62 @substitutions[substr(str, match_pos + 1, match_len)]);
63 str = substr(str, match_pos + 1 + match_len, -1);
64 }
65 (str, ) = strreplace(str, "%%", "%", strlen(str));
66 formatted_str += str;
68 return formatted_str;
69 }
71 define set_terminal_title(title)
72 {
73 variable term = getenv("TERM");
75 if (string_match(term, "screen", 1)) {
76 tt_send("\033_" + title + "\033\\");
77 } else if (string_match(term, "xterm", 1)) {
78 tt_send("\033]0;" + title + "\007");
79 }
80 return;
81 }
83 define set_group_title()
84 {
85 set_terminal_title(format_str(config.group_title_format,
86 group_substitutions));
87 }
89 define set_article_title()
90 {
91 set_terminal_title(format_str(config.article_title_format,
92 article_substitutions));
93 }
95 () = register_hook("article_mode_hook", "TerminalTitle->set_article_title");
96 () = register_hook("group_mode_hook", "TerminalTitle->set_group_title");