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