1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/**
* encodingSwithcer (vimperator plugin)
* @author teramako teramako@gmail.com
* @version 0.1
*
* Usage:
*
* change encoding
* :set fileencoding = {encodeName}
* :set fenc = {encodeName}
*
* list available encodings
* :listencoding [expr]
* :lsenc [expr]
*
* change auto detector
* :set autodetector = {detectorName}
* :set audet = {detectorName}
*
* list available auto detectors
* :listdetector [expr]
* :lsdet [expr]
*/
(function(){
var encodings = [];
var detectors = [];
const Cc = Components.classes;
const Ci = Components.interfaces;
if (!RDF) var RDF = Cc['@mozilla.org/rdf/rdf-service;1'].getService(Ci.nsIRDFService);
if (!RDFCU) var RDFCU = Cc['@mozilla.org/rdf/container-utils;1'].getService(Ci.nsIRDFContainerUtils);
var cm = RDF.GetDataSource('rdf:charset-menu');
var sbService = Cc['@mozilla.org/intl/stringbundle;1'].getService(Ci.nsIStringBundleService);
var sbCharTitle = sbService.createBundle('chrome://global/locale/charsetTitles.properties');
CreateMenu('browser');
CreateMenu('more-menu');
var allEnum = cm.GetAllResources();
while (allEnum.hasMoreElements()){
var res = allEnum.getNext().QueryInterface(Ci.nsIRDFResource);
var value = res.Value;
if (RDFCU.IsContainer(cm, res) || value.indexOf('charset.') == 0 || value.indexOf('----') == 0) {
continue;
}
var label = sbCharTitle.GetStringFromName(value.toLowerCase() + '.title');
if (res.Value.indexOf('chardet.') == 0){
value = value.substr('chardet.'.length);
var buf = createDetector(value);
buf[1] = label;
detectors.push(buf);
} else {
encodings.push([value,label]);
}
}
function createDetector(name){
var i = name.indexOf('_');
if ( i > 0 ){
return [name.substr(0,i),null,name.substr(i)];
}
return [name,null,''];
}
function getDetector(name){
detectors.forEach(function(detector){
if (detector[0].toLowerCase() == name.toLowerCase()){
return detector[0] + detector[2];
}
});
}
function getEncoding(name){
encodings.forEach(function(encoding){
if (encoding[0].toLowerCase() == name.toLowerCase()){
return encoding[0];
}
});
}
function isValid(array, value){
return array.some(function(v){
return v[0].toLowerCase() == value.toLowerCase();
});
}
function completion(array, filter){
if (!filter) return array;
filter = filter.toLowerCase();
return array.filter(function(v){
return v[0].toLowerCase().indexOf(filter) == 0;
});
}
var sbCharDefault = sbService.createBundle(gPrefService.getDefaultBranch('intl.charset.').getCharPref('default'));
const DEFAULT_CHARSET = sbCharDefault.GetStringFromName('intl.charset.default');
liberator.options.add(['fileencoding','fenc'],'set the charactor encoding for the current page','string', DEFAULT_CHARSET,
{
setter: function(value){
if (!value) return;
value = getEncoding(value);
SetForcedCharset(value);
//SetDefaultCharacterSet(value);
BrowserSetForcedCharacterSet(value);
},
getter: function(){
return getBrowser().docShell.QueryInterface(Ci.nsIDocCharset).charset;
},
validator: function(value){
return isValid( encodings, value);
},
completer: function(filter){
return [0,completion( encodings, filter)];
}
}
);
var sbCharDetector = sbService.createBundle(gPrefService.getDefaultBranch('intl.charset.').getCharPref('detector'));
const DEFAULT_DETECTOR = createDetector(sbCharDetector.GetStringFromName('intl.charset.detector'))[0];
liberator.options.add(['autodetector','audet'],'set auto detect character encoding','string',DEFAULT_DETECTOR,
{
setter: function(value){
SetForcedDetector(true);
var pref = Cc['@mozilla.org/preferences-service;1'].getService(Ci.nsIPrefBranch);
var str = Cc['@mozilla.org/supports-string;1'].createInstance(Ci.nsISupportsString);
if (!value || value == 'off') {
str.data = '';
} else {
str.data = value = getDetector(value);
}
pref.setComplexValue('intl.charset.detector',Ci.nsISupportsString, str);
BrowserSetForcedCharacterSet(value);
},
getter: function(){
var elms = document.getElementById('charsetMenu').getElementsByAttribute('checed','true');
for (var i=0; i<elms.length; i++){
if (elms[i].getAttribute('name') == 'detectorGroup'){
var str = elms[i].getAttribute('id').substr('chardet.'.length);
return createDetector(str)[0];
}
}
},
validator: function(value){
return isValid( detectors, value);
},
completer: function(filter){
return [0,completion( detectors, filter)];
}
}
);
function listCharset(arg, current, list){
if (!arg) arg = '.';
var reg = new RegExp(arg,'i');
var str = [];
str.push('<table>');
list.forEach(function(i){
if (reg.test(i[0]) || reg.test(i[1])){
str.push('<tr>');
if (current == i[0]){
str.push('<td class="hl-Title">' + i[0] + '</td><td class="hl-Title">' + i[1] + '</td>');
} else {
str.push('<td>' + i[0] + '</td><td>' + i[1] + '</td>');
}
str.push('</tr>');
}
});
str.push('</table>');
liberator.echo( str.join(''), true);
}
liberator.commands.addUserCommand(['listencoding','lsenc'],'list all encodings',
function(arg){
listCharset(arg, liberator.options.fileencoding, encodings);
},{
completer: function(filter){
return [0,completion(encodings, filter)];
}
}
);
liberator.commands.addUserCommand(['listdetector','lsdet'],'list all auto detectors',
function(arg){
listCharset(arg, liberator.options.autodetector, detectors);
},{
completer: function(filter){
return [0,completion(detectors, filter)];
}
}
);
})();
// vim: set fdm=marker sw=4 ts=4 et:
|