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

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