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

changeset 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
files README terminal-title.sl
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/README	Sat Mar 14 11:44:11 2015 +0100
     1.3 @@ -0,0 +1,83 @@
     1.4 +slrn Terminal Title Macro
     1.5 +=========================
     1.6 +
     1.7 +The slrn terminal title macro displays user-defined information in the
     1.8 +terminal title if supported by the terminal.
     1.9 +
    1.10 +Usage
    1.11 +-----
    1.12 +
    1.13 +The slrn terminal title macro can be used by including it in the .slrnrc user
    1.14 +initialization file via the `interpret` command, e.g. provided that the file
    1.15 +terminal-title.sl is located in one of the directories specified by the
    1.16 +macro_directory configuration variable:
    1.17 +
    1.18 +    interpret "terminal-title.sl"
    1.19 +
    1.20 +The terminal title macro can be configured through the following slang
    1.21 +variables:
    1.22 +
    1.23 +TerminalTitle->article_title_format
    1.24 +:   The string displayed in the terminal title in article mode.  The string
    1.25 +    may contain certain substitution identifiers in order to insert
    1.26 +    information from slrn at runtime.
    1.27 +
    1.28 +TerminalTitle->group_title_format
    1.29 +:   The string displayed in the terminal title in group mode.  The string
    1.30 +    may contain certain substitution identifiers in order to insert
    1.31 +    information from slrn at runtime.
    1.32 +
    1.33 +Substitution identifiers always start with a "%" sign.  The following
    1.34 +substitution identifiers can be referenced from
    1.35 +TerminalTitle->article_title_format and TerminalTitle->group_title_format:
    1.36 +
    1.37 +v
    1.38 +:   The slrn version.
    1.39 +
    1.40 +s
    1.41 +:   The name of the current news server.
    1.42 +
    1.43 +n
    1.44 +:   The name of the current newsgroup.
    1.45 +
    1.46 +Contact
    1.47 +-------
    1.48 +
    1.49 +Please send any feedback, translations or bug reports via email to
    1.50 +<guido+weechat@berhoerster.name>.
    1.51 +
    1.52 +Bug Reports
    1.53 +-----------
    1.54 +
    1.55 +When sending bug reports, please always mention the exact version of the
    1.56 +macro with which the issue occurs as well as the version of slrn, slang and
    1.57 +the operating system you are using and make sure that you provide sufficient
    1.58 +information to reproduce the issue and include any input, output, any error
    1.59 +messages and slang stack traces.
    1.60 +
    1.61 +License
    1.62 +-------
    1.63 +
    1.64 +Except otherwise noted, all files are Copyright (C) 2009 Guido Berhoerster and
    1.65 +distributed under the following license terms:
    1.66 +
    1.67 +Copyright (C) 2009 Guido Berhoerster <guido+slrn@berhoerster.name>
    1.68 +
    1.69 +Permission is hereby granted, free of charge, to any person obtaining
    1.70 +a copy of this software and associated documentation files (the
    1.71 +"Software"), to deal in the Software without restriction, including
    1.72 +without limitation the rights to use, copy, modify, merge, publish,
    1.73 +distribute, sublicense, and/or sell copies of the Software, and to
    1.74 +permit persons to whom the Software is furnished to do so, subject to
    1.75 +the following conditions:
    1.76 +
    1.77 +The above copyright notice and this permission notice shall be included
    1.78 +in all copies or substantial portions of the Software.
    1.79 +
    1.80 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    1.81 +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1.82 +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    1.83 +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    1.84 +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    1.85 +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    1.86 +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/terminal-title.sl	Sat Mar 14 11:44:11 2015 +0100
     2.3 @@ -0,0 +1,96 @@
     2.4 +% terminal-title.sl - set the terminal title
     2.5 +%
     2.6 +% Copyright (C) 2009 Guido Berhoerster <guido+slrn@berhoerster.name>
     2.7 +%
     2.8 +% Permission is hereby granted, free of charge, to any person obtaining
     2.9 +% a copy of this software and associated documentation files (the
    2.10 +% "Software"), to deal in the Software without restriction, including
    2.11 +% without limitation the rights to use, copy, modify, merge, publish,
    2.12 +% distribute, sublicense, and/or sell copies of the Software, and to
    2.13 +% permit persons to whom the Software is furnished to do so, subject to
    2.14 +% the following conditions:
    2.15 +%
    2.16 +% The above copyright notice and this permission notice shall be included
    2.17 +% in all copies or substantial portions of the Software.
    2.18 +%
    2.19 +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    2.20 +% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    2.21 +% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    2.22 +% IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    2.23 +% CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    2.24 +% TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    2.25 +% SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    2.26 +
    2.27 +%open_log_file(make_home_filename("slrn-debug.log"));
    2.28 +%_traceback = 1;
    2.29 +
    2.30 +implements("TerminalTitle");
    2.31 +
    2.32 +static variable group_substitutions = Assoc_Type[Ref_Type];
    2.33 +group_substitutions["s"] = &server_name;
    2.34 +group_substitutions["v"] = &_slrn_version_string;
    2.35 +
    2.36 +static variable article_substitutions = Assoc_Type[Ref_Type];
    2.37 +article_substitutions["s"] = &server_name;
    2.38 +article_substitutions["v"] = &_slrn_version_string;
    2.39 +article_substitutions["n"] = &current_newsgroup;
    2.40 +
    2.41 +static variable config = struct
    2.42 +{
    2.43 +    article_title_format = "slrn %v -- Server: %s Group: %n",
    2.44 +    group_title_format = "slrn %v -- Server: %s"
    2.45 +};
    2.46 +
    2.47 +define format_str(str, substitutions)
    2.48 +{
    2.49 +    variable match_pos;
    2.50 +    variable match_len;
    2.51 +    variable format;
    2.52 +    variable temp;
    2.53 +    variable formatted_str = "";
    2.54 +
    2.55 +    while (string_match(str, "^\(%-?[0-9]*\(["R +
    2.56 +            strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1) ||
    2.57 +            string_match(str, "\([^%]%-?[0-9]*\(["R +
    2.58 +            strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1)) {
    2.59 +        (match_pos, match_len) = string_match_nth(1);
    2.60 +        temp = substr(str, 1, match_pos);
    2.61 +        (temp, ) = strreplace(temp, "%%", "%", strlen(temp));
    2.62 +        format = substr(str, match_pos + 1, match_len - 1) + "s";
    2.63 +        (match_pos, match_len) = string_match_nth(2);
    2.64 +        formatted_str += temp + sprintf(format,
    2.65 +            @substitutions[substr(str, match_pos + 1, match_len)]);
    2.66 +        str = substr(str, match_pos + 1 + match_len, -1);
    2.67 +    }
    2.68 +    (str, ) = strreplace(str, "%%", "%", strlen(str));
    2.69 +    formatted_str += str;
    2.70 +
    2.71 +    return formatted_str;
    2.72 +}
    2.73 +
    2.74 +define set_terminal_title(title)
    2.75 +{
    2.76 +    variable term = getenv("TERM");
    2.77 +
    2.78 +    if (string_match(term, "screen", 1)) {
    2.79 +        tt_send("\033_" + title + "\033\\");
    2.80 +    } else if (string_match(term, "xterm", 1)) {
    2.81 +        tt_send("\033]0;" + title + "\007");
    2.82 +    }
    2.83 +    return;
    2.84 +}
    2.85 +
    2.86 +define set_group_title()
    2.87 +{
    2.88 +    set_terminal_title(format_str(config.group_title_format,
    2.89 +            group_substitutions));
    2.90 +}
    2.91 +
    2.92 +define set_article_title()
    2.93 +{
    2.94 +    set_terminal_title(format_str(config.article_title_format,
    2.95 +            article_substitutions));
    2.96 +}
    2.97 +
    2.98 +() = register_hook("article_mode_hook", "TerminalTitle->set_article_title");
    2.99 +() = register_hook("group_mode_hook", "TerminalTitle->set_group_title");