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

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/terminal-title.sl	Sat Mar 14 11:44:11 2015 +0100
     1.3 @@ -0,0 +1,96 @@
     1.4 +% terminal-title.sl - set the terminal title
     1.5 +%
     1.6 +% Copyright (C) 2009 Guido Berhoerster <guido+slrn@berhoerster.name>
     1.7 +%
     1.8 +% Permission is hereby granted, free of charge, to any person obtaining
     1.9 +% a copy of this software and associated documentation files (the
    1.10 +% "Software"), to deal in the Software without restriction, including
    1.11 +% without limitation the rights to use, copy, modify, merge, publish,
    1.12 +% distribute, sublicense, and/or sell copies of the Software, and to
    1.13 +% permit persons to whom the Software is furnished to do so, subject to
    1.14 +% the following conditions:
    1.15 +%
    1.16 +% The above copyright notice and this permission notice shall be included
    1.17 +% in all copies or substantial portions of the Software.
    1.18 +%
    1.19 +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    1.20 +% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1.21 +% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    1.22 +% IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    1.23 +% CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    1.24 +% TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    1.25 +% SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    1.26 +
    1.27 +%open_log_file(make_home_filename("slrn-debug.log"));
    1.28 +%_traceback = 1;
    1.29 +
    1.30 +implements("TerminalTitle");
    1.31 +
    1.32 +static variable group_substitutions = Assoc_Type[Ref_Type];
    1.33 +group_substitutions["s"] = &server_name;
    1.34 +group_substitutions["v"] = &_slrn_version_string;
    1.35 +
    1.36 +static variable article_substitutions = Assoc_Type[Ref_Type];
    1.37 +article_substitutions["s"] = &server_name;
    1.38 +article_substitutions["v"] = &_slrn_version_string;
    1.39 +article_substitutions["n"] = &current_newsgroup;
    1.40 +
    1.41 +static variable config = struct
    1.42 +{
    1.43 +    article_title_format = "slrn %v -- Server: %s Group: %n",
    1.44 +    group_title_format = "slrn %v -- Server: %s"
    1.45 +};
    1.46 +
    1.47 +define format_str(str, substitutions)
    1.48 +{
    1.49 +    variable match_pos;
    1.50 +    variable match_len;
    1.51 +    variable format;
    1.52 +    variable temp;
    1.53 +    variable formatted_str = "";
    1.54 +
    1.55 +    while (string_match(str, "^\(%-?[0-9]*\(["R +
    1.56 +            strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1) ||
    1.57 +            string_match(str, "\([^%]%-?[0-9]*\(["R +
    1.58 +            strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1)) {
    1.59 +        (match_pos, match_len) = string_match_nth(1);
    1.60 +        temp = substr(str, 1, match_pos);
    1.61 +        (temp, ) = strreplace(temp, "%%", "%", strlen(temp));
    1.62 +        format = substr(str, match_pos + 1, match_len - 1) + "s";
    1.63 +        (match_pos, match_len) = string_match_nth(2);
    1.64 +        formatted_str += temp + sprintf(format,
    1.65 +            @substitutions[substr(str, match_pos + 1, match_len)]);
    1.66 +        str = substr(str, match_pos + 1 + match_len, -1);
    1.67 +    }
    1.68 +    (str, ) = strreplace(str, "%%", "%", strlen(str));
    1.69 +    formatted_str += str;
    1.70 +
    1.71 +    return formatted_str;
    1.72 +}
    1.73 +
    1.74 +define set_terminal_title(title)
    1.75 +{
    1.76 +    variable term = getenv("TERM");
    1.77 +
    1.78 +    if (string_match(term, "screen", 1)) {
    1.79 +        tt_send("\033_" + title + "\033\\");
    1.80 +    } else if (string_match(term, "xterm", 1)) {
    1.81 +        tt_send("\033]0;" + title + "\007");
    1.82 +    }
    1.83 +    return;
    1.84 +}
    1.85 +
    1.86 +define set_group_title()
    1.87 +{
    1.88 +    set_terminal_title(format_str(config.group_title_format,
    1.89 +            group_substitutions));
    1.90 +}
    1.91 +
    1.92 +define set_article_title()
    1.93 +{
    1.94 +    set_terminal_title(format_str(config.article_title_format,
    1.95 +            article_substitutions));
    1.96 +}
    1.97 +
    1.98 +() = register_hook("article_mode_hook", "TerminalTitle->set_article_title");
    1.99 +() = register_hook("group_mode_hook", "TerminalTitle->set_group_title");