view terminal-title.sl @ 1:b5ca94cc74ae default tip

Change license to GPL-2.0+
author Guido Berhoerster <guido+slrn@berhoerster.name>
date Sat, 25 Jul 2015 17:12:50 +0200
parents 1964c513723a
children
line wrap: on
line source

% terminal-title.sl - set the terminal title
%
% Copyright (C) 2009 Guido Berhoerster <guido+slrn@berhoerster.name>
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

%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"] = &current_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");