Mercurial > addons > slrn-macros > slrn-terminal-title-macro
annotate 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 |
rev | line source |
---|---|
0
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
1 % terminal-title.sl - set the terminal title |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
2 % |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
3 % Copyright (C) 2009 Guido Berhoerster <guido+slrn@berhoerster.name> |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
4 % |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
5 % Permission is hereby granted, free of charge, to any person obtaining |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
6 % a copy of this software and associated documentation files (the |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
7 % "Software"), to deal in the Software without restriction, including |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
8 % without limitation the rights to use, copy, modify, merge, publish, |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
9 % distribute, sublicense, and/or sell copies of the Software, and to |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
10 % permit persons to whom the Software is furnished to do so, subject to |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
11 % the following conditions: |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
12 % |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
13 % The above copyright notice and this permission notice shall be included |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
14 % in all copies or substantial portions of the Software. |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
15 % |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
23 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
24 %open_log_file(make_home_filename("slrn-debug.log")); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
25 %_traceback = 1; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
26 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
27 implements("TerminalTitle"); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
28 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
29 static variable group_substitutions = Assoc_Type[Ref_Type]; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
30 group_substitutions["s"] = &server_name; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
31 group_substitutions["v"] = &_slrn_version_string; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
32 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
33 static variable article_substitutions = Assoc_Type[Ref_Type]; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
34 article_substitutions["s"] = &server_name; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
35 article_substitutions["v"] = &_slrn_version_string; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
36 article_substitutions["n"] = ¤t_newsgroup; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
37 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
38 static variable config = struct |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
39 { |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
40 article_title_format = "slrn %v -- Server: %s Group: %n", |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
41 group_title_format = "slrn %v -- Server: %s" |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
42 }; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
43 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
44 define format_str(str, substitutions) |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
45 { |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
46 variable match_pos; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
47 variable match_len; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
48 variable format; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
49 variable temp; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
50 variable formatted_str = ""; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
51 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
52 while (string_match(str, "^\(%-?[0-9]*\(["R + |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
53 strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1) || |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
54 string_match(str, "\([^%]%-?[0-9]*\(["R + |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
55 strjoin(assoc_get_keys(substitutions), "") + "]\)\)"R, 1)) { |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
56 (match_pos, match_len) = string_match_nth(1); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
57 temp = substr(str, 1, match_pos); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
58 (temp, ) = strreplace(temp, "%%", "%", strlen(temp)); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
59 format = substr(str, match_pos + 1, match_len - 1) + "s"; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
60 (match_pos, match_len) = string_match_nth(2); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
61 formatted_str += temp + sprintf(format, |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
62 @substitutions[substr(str, match_pos + 1, match_len)]); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
63 str = substr(str, match_pos + 1 + match_len, -1); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
64 } |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
65 (str, ) = strreplace(str, "%%", "%", strlen(str)); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
66 formatted_str += str; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
67 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
68 return formatted_str; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
69 } |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
70 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
71 define set_terminal_title(title) |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
72 { |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
73 variable term = getenv("TERM"); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
74 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
75 if (string_match(term, "screen", 1)) { |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
76 tt_send("\033_" + title + "\033\\"); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
77 } else if (string_match(term, "xterm", 1)) { |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
78 tt_send("\033]0;" + title + "\007"); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
79 } |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
80 return; |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
81 } |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
82 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
83 define set_group_title() |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
84 { |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
85 set_terminal_title(format_str(config.group_title_format, |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
86 group_substitutions)); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
87 } |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
88 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
89 define set_article_title() |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
90 { |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
91 set_terminal_title(format_str(config.article_title_format, |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
92 article_substitutions)); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
93 } |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
94 |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
95 () = register_hook("article_mode_hook", "TerminalTitle->set_article_title"); |
1964c513723a
Initial revision
Guido Berhoerster <guido+slrn@berhoerster.name>
parents:
diff
changeset
|
96 () = register_hook("group_mode_hook", "TerminalTitle->set_group_title"); |