guido+slrn@0: % terminal-title.sl - set the terminal title guido+slrn@0: % guido+slrn@0: % Copyright (C) 2009 Guido Berhoerster guido+slrn@0: % guido+slrn@0: % Permission is hereby granted, free of charge, to any person obtaining guido+slrn@0: % a copy of this software and associated documentation files (the guido+slrn@0: % "Software"), to deal in the Software without restriction, including guido+slrn@0: % without limitation the rights to use, copy, modify, merge, publish, guido+slrn@0: % distribute, sublicense, and/or sell copies of the Software, and to guido+slrn@0: % permit persons to whom the Software is furnished to do so, subject to guido+slrn@0: % the following conditions: guido+slrn@0: % guido+slrn@0: % The above copyright notice and this permission notice shall be included guido+slrn@0: % in all copies or substantial portions of the Software. guido+slrn@0: % guido+slrn@0: % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, guido+slrn@0: % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF guido+slrn@0: % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. guido+slrn@0: % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY guido+slrn@0: % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, guido+slrn@0: % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE guido+slrn@0: % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. guido+slrn@0: guido+slrn@0: %open_log_file(make_home_filename("slrn-debug.log")); guido+slrn@0: %_traceback = 1; guido+slrn@0: guido+slrn@0: implements("TerminalTitle"); guido+slrn@0: guido+slrn@0: static variable group_substitutions = Assoc_Type[Ref_Type]; guido+slrn@0: group_substitutions["s"] = &server_name; guido+slrn@0: group_substitutions["v"] = &_slrn_version_string; guido+slrn@0: guido+slrn@0: static variable article_substitutions = Assoc_Type[Ref_Type]; guido+slrn@0: article_substitutions["s"] = &server_name; guido+slrn@0: article_substitutions["v"] = &_slrn_version_string; guido+slrn@0: article_substitutions["n"] = ¤t_newsgroup; guido+slrn@0: guido+slrn@0: static variable config = struct guido+slrn@0: { guido+slrn@0: article_title_format = "slrn %v -- Server: %s Group: %n", guido+slrn@0: group_title_format = "slrn %v -- Server: %s" guido+slrn@0: }; guido+slrn@0: guido+slrn@0: define format_str(str, substitutions) guido+slrn@0: { guido+slrn@0: variable match_pos; guido+slrn@0: variable match_len; guido+slrn@0: variable format; guido+slrn@0: variable temp; guido+slrn@0: variable formatted_str = ""; guido+slrn@0: guido+slrn@0: while (string_match(str, "^\(%-?[0-9]*\(["R + guido+slrn@0: strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1) || guido+slrn@0: string_match(str, "\([^%]%-?[0-9]*\(["R + guido+slrn@0: strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1)) { guido+slrn@0: (match_pos, match_len) = string_match_nth(1); guido+slrn@0: temp = substr(str, 1, match_pos); guido+slrn@0: (temp, ) = strreplace(temp, "%%", "%", strlen(temp)); guido+slrn@0: format = substr(str, match_pos + 1, match_len - 1) + "s"; guido+slrn@0: (match_pos, match_len) = string_match_nth(2); guido+slrn@0: formatted_str += temp + sprintf(format, guido+slrn@0: @substitutions[substr(str, match_pos + 1, match_len)]); guido+slrn@0: str = substr(str, match_pos + 1 + match_len, -1); guido+slrn@0: } guido+slrn@0: (str, ) = strreplace(str, "%%", "%", strlen(str)); guido+slrn@0: formatted_str += str; guido+slrn@0: guido+slrn@0: return formatted_str; guido+slrn@0: } guido+slrn@0: guido+slrn@0: define set_terminal_title(title) guido+slrn@0: { guido+slrn@0: variable term = getenv("TERM"); guido+slrn@0: guido+slrn@0: if (string_match(term, "screen", 1)) { guido+slrn@0: tt_send("\033_" + title + "\033\\"); guido+slrn@0: } else if (string_match(term, "xterm", 1)) { guido+slrn@0: tt_send("\033]0;" + title + "\007"); guido+slrn@0: } guido+slrn@0: return; guido+slrn@0: } guido+slrn@0: guido+slrn@0: define set_group_title() guido+slrn@0: { guido+slrn@0: set_terminal_title(format_str(config.group_title_format, guido+slrn@0: group_substitutions)); guido+slrn@0: } guido+slrn@0: guido+slrn@0: define set_article_title() guido+slrn@0: { guido+slrn@0: set_terminal_title(format_str(config.article_title_format, guido+slrn@0: article_substitutions)); guido+slrn@0: } guido+slrn@0: guido+slrn@0: () = register_hook("article_mode_hook", "TerminalTitle->set_article_title"); guido+slrn@0: () = register_hook("group_mode_hook", "TerminalTitle->set_group_title");